tron@2186: /* $Id$ */ tron@2186: belugas@6117: /** @file bridge_gui.cpp Graphical user interface for bridge construction */ celestar@2262: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" truelight@0: #include "gui.h" rubidium@8107: #include "window_gui.h" rubidium@8116: #include "command_func.h" rubidium@8116: #include "economy_func.h" tron@2159: #include "variables.h" celestar@2262: #include "bridge.h" rubidium@8114: #include "strings_func.h" rubidium@8131: #include "window_func.h" rubidium@8157: #include "sound_func.h" rubidium@8139: #include "map_func.h" rubidium@8224: #include "viewport_func.h" rubidium@8224: #include "gfx_func.h" smatz@8398: #include "tunnelbridge.h" skidd13@9283: #include "sortlist_type.h" skidd13@9292: #include "widgets/dropdown_func.h" truelight@0: rubidium@8264: #include "table/strings.h" rubidium@8264: skidd13@9292: /* Save the sorting during runtime */ skidd13@9292: static Listing _bridge_sorting = {false, 0}; skidd13@9292: skidd13@9283: /** skidd13@9283: * Carriage for the data we need if we want to build a bridge skidd13@9283: */ skidd13@9283: struct BuildBridgeData { skidd13@9283: BridgeType index; skidd13@9283: const BridgeSpec *spec; skidd13@9283: Money cost; skidd13@9283: }; skidd13@8020: skidd13@9283: typedef GUIList GUIBridgeList; truelight@0: skidd13@9292: /** Sort the bridges by their index */ skidd13@9292: static int CDECL BridgeIndexSorter(const void *a, const void *b) skidd13@9292: { skidd13@9292: const BuildBridgeData* ba = (BuildBridgeData*)a; skidd13@9292: const BuildBridgeData* bb = (BuildBridgeData*)b; skidd13@9292: int r = ba->index - bb->index; skidd13@9292: skidd13@9292: return (_bridge_sorting.order) ? -r : r; skidd13@9292: } skidd13@9292: skidd13@9292: /** Sort the bridges by their price */ skidd13@9292: static int CDECL BridgePriceSorter(const void *a, const void *b) skidd13@9292: { skidd13@9292: const BuildBridgeData* ba = (BuildBridgeData*)a; skidd13@9292: const BuildBridgeData* bb = (BuildBridgeData*)b; skidd13@9292: int r = ba->cost - bb->cost; skidd13@9292: skidd13@9292: return (_bridge_sorting.order) ? -r : r; skidd13@9292: } skidd13@9292: skidd13@9292: /** Sort the bridges by their maximum speed */ skidd13@9292: static int CDECL BridgeSpeedSorter(const void *a, const void *b) skidd13@9292: { skidd13@9292: const BuildBridgeData* ba = (BuildBridgeData*)a; skidd13@9292: const BuildBridgeData* bb = (BuildBridgeData*)b; skidd13@9292: int r = ba->spec->speed - bb->spec->speed; skidd13@9292: skidd13@9292: return (_bridge_sorting.order) ? -r : r; skidd13@9292: } skidd13@9292: skidd13@9292: typedef int CDECL BridgeSortListingTypeFunction(const void*, const void*); skidd13@9292: skidd13@9292: /* Availible bridge sorting functions */ skidd13@9292: static BridgeSortListingTypeFunction* const _bridge_sorter[] = { skidd13@9292: &BridgeIndexSorter, skidd13@9292: &BridgePriceSorter, skidd13@9292: &BridgeSpeedSorter skidd13@9292: }; skidd13@9292: skidd13@9292: /* Names of the sorting functions */ skidd13@9292: static const StringID _bridge_sort_listing[] = { skidd13@9292: STR_SORT_BY_NUMBER, skidd13@9292: STR_ENGINE_SORT_COST, skidd13@9292: STR_SORT_BY_MAX_SPEED, skidd13@9292: INVALID_STRING_ID skidd13@9292: }; skidd13@9292: skidd13@9283: /** skidd13@9283: * Callback executed after a build Bridge CMD has been called skidd13@9283: * skidd13@9283: * @param scucess True if the build succeded skidd13@9283: * @param tile The tile where the command has been executed skidd13@9283: * @param p1 not used skidd13@9283: * @param p2 not used skidd13@9283: */ tron@1977: void CcBuildBridge(bool success, TileIndex tile, uint32 p1, uint32 p2) truelight@0: { tron@541: if (success) SndPlayTileFx(SND_27_BLACKSMITH_ANVIL, tile); truelight@0: } truelight@0: skidd13@8020: /* Names of the build bridge selection window */ skidd13@8020: enum BuildBridgeSelectionWidgets { skidd13@8020: BBSW_CLOSEBOX = 0, skidd13@8020: BBSW_CAPTION, skidd13@9292: BBSW_DROPDOWN_ORDER, skidd13@9292: BBSW_DROPDOWN_CRITERIA, skidd13@8020: BBSW_BRIDGE_LIST, skidd13@8020: BBSW_SCROLLBAR, skidd13@8020: BBSW_RESIZEBOX skidd13@8020: }; skidd13@8020: skidd13@9283: class BuildBridgeWindow : public Window { skidd13@9283: private: skidd13@9283: /* The last size of the build bridge window skidd13@9283: * is saved during runtime */ skidd13@9292: static uint last_size; skidd13@8020: skidd13@9283: TileIndex start_tile; skidd13@9283: TileIndex end_tile; skidd13@9283: uint32 type; skidd13@9292: GUIBridgeList *bridges; truelight@0: skidd13@9283: void BuildBridge(uint8 i) skidd13@9283: { skidd13@9283: DoCommandP(this->end_tile, this->start_tile, this->type | this->bridges->sort_list[i].index, skidd13@9283: CcBuildBridge, CMD_BUILD_BRIDGE | CMD_MSG(STR_5015_CAN_T_BUILD_BRIDGE_HERE)); skidd13@9283: } truelight@0: skidd13@9292: /** Sort the builable bridges */ skidd13@9292: void SortBridgeList() skidd13@9292: { skidd13@9292: /* Skip sorting if resort bit is not set */ skidd13@9292: if (!(bridges->flags & VL_RESORT)) return; skidd13@9292: skidd13@9292: qsort(this->bridges->sort_list, this->bridges->list_length, sizeof(this->bridges->sort_list[0]), _bridge_sorter[_bridge_sorting.criteria]); skidd13@9292: skidd13@9292: /* Display the current sort variant */ skidd13@9292: this->widget[BBSW_DROPDOWN_CRITERIA].data = _bridge_sort_listing[this->bridges->sort_type]; skidd13@9292: skidd13@9292: bridges->flags &= ~VL_RESORT; skidd13@9292: skidd13@9292: /* Set the modified widgets dirty */ skidd13@9292: this->InvalidateWidget(BBSW_DROPDOWN_CRITERIA); skidd13@9292: this->InvalidateWidget(BBSW_BRIDGE_LIST); skidd13@9292: } skidd13@9292: skidd13@9283: public: skidd13@9292: BuildBridgeWindow(const WindowDesc *desc, TileIndex start, TileIndex end, uint32 br_type, GUIBridgeList *bl) : Window(desc), skidd13@9283: start_tile(start), skidd13@9283: end_tile(end), skidd13@9283: type(br_type), skidd13@9283: bridges(bl) skidd13@9283: { skidd13@9292: this->SortBridgeList(); truelight@0: skidd13@9283: /* Change the data, or the caption of the gui. Set it to road or rail, accordingly */ skidd13@9283: this->widget[BBSW_CAPTION].data = (GB(this->type, 15, 2) == TRANSPORT_ROAD) ? STR_1803_SELECT_ROAD_BRIDGE : STR_100D_SELECT_RAIL_BRIDGE; skidd13@9283: skidd13@9283: this->resize.step_height = 22; skidd13@9283: this->vscroll.count = bl->list_length; skidd13@9283: skidd13@9283: if (this->last_size <= 4) { skidd13@9283: this->vscroll.cap = 4; skidd13@9283: } else { skidd13@9283: /* Resize the bridge selection window if we used a bigger one the last time */ skidd13@9283: this->vscroll.cap = (this->vscroll.count > this->last_size) ? this->last_size : this->vscroll.count; skidd13@9283: ResizeWindow(this, 0, (this->vscroll.cap - 4) * this->resize.step_height); skidd13@9283: this->widget[BBSW_BRIDGE_LIST].data = (this->vscroll.cap << 8) + 1; skidd13@7983: } peter1138@6204: skidd13@9283: this->FindWindowPlacementAndResize(desc); skidd13@9283: } peter1138@6204: skidd13@9283: ~BuildBridgeWindow() skidd13@9283: { skidd13@9283: free(this->bridges->sort_list); skidd13@9283: delete bridges; skidd13@9283: } skidd13@9283: skidd13@9283: virtual void OnPaint() skidd13@9283: { skidd13@9283: this->DrawWidgets(); skidd13@9283: skidd13@9292: this->DrawSortButtonState(BBSW_DROPDOWN_ORDER, (this->bridges->flags & VL_DESC) ? SBS_DOWN : SBS_UP); skidd13@9283: skidd13@9292: uint y = this->widget[BBSW_BRIDGE_LIST].top + 2; skidd13@9292: glx@9320: for (int i = this->vscroll.pos; (i < (this->vscroll.cap + this->vscroll.pos)) && (i < this->bridges->list_length); i++) { skidd13@9292: const BridgeSpec *b = this->bridges->sort_list[i].spec; skidd13@9292: skidd13@9292: SetDParam(2, this->bridges->sort_list[i].cost); skidd13@9283: SetDParam(1, b->speed * 10 / 16); skidd13@9283: SetDParam(0, b->material); skidd13@9283: skidd13@9283: DrawSprite(b->sprite, b->pal, 3, y); skidd13@9283: DrawString(44, y, STR_500D, TC_FROMSTRING); skidd13@9283: y += this->resize.step_height; skidd13@9292: truelight@0: } skidd13@9283: } truelight@193: rubidium@9285: virtual EventState OnKeyPress(uint16 key, uint16 keycode) skidd13@9283: { skidd13@9283: const uint8 i = keycode - '1'; skidd13@9283: if (i < 9 && i < this->bridges->list_length) { skidd13@9283: /* Build the requested bridge */ skidd13@9283: this->BuildBridge(i); skidd13@9283: delete this; rubidium@9285: return ES_HANDLED; skidd13@9283: } rubidium@9285: return ES_NOT_HANDLED; skidd13@9283: } skidd13@9283: skidd13@9283: virtual void OnClick(Point pt, int widget) skidd13@9283: { skidd13@9292: switch (widget) { skidd13@9292: default: break; skidd13@9292: case BBSW_BRIDGE_LIST: { skidd13@9292: uint i = ((int)pt.y - this->widget[BBSW_BRIDGE_LIST].top) / this->resize.step_height; skidd13@9292: if (i < this->vscroll.cap) { skidd13@9292: i += this->vscroll.pos; skidd13@9292: if (i < this->bridges->list_length) { skidd13@9292: this->BuildBridge(i); skidd13@9292: delete this; skidd13@9292: } skidd13@7983: } skidd13@9292: } break; skidd13@9292: skidd13@9292: case BBSW_DROPDOWN_ORDER: skidd13@9292: /* Revers the sort order */ skidd13@9292: this->bridges->flags ^= VL_DESC; skidd13@9292: _bridge_sorting.order = !_bridge_sorting.order; skidd13@9292: skidd13@9292: this->bridges->flags |= VL_RESORT; skidd13@9292: this->SortBridgeList(); skidd13@9292: break; skidd13@9292: skidd13@9292: case BBSW_DROPDOWN_CRITERIA: skidd13@9292: ShowDropDownMenu(this, _bridge_sort_listing, bridges->sort_type, BBSW_DROPDOWN_CRITERIA, 0, 0); skidd13@9292: break; skidd13@9292: } skidd13@9292: } skidd13@9292: skidd13@9292: virtual void OnDropdownSelect(int widget, int index) skidd13@9292: { skidd13@9292: if (widget == BBSW_DROPDOWN_CRITERIA && this->bridges->sort_type != index) { skidd13@9292: this->bridges->sort_type = index; skidd13@9292: _bridge_sorting.criteria = index; skidd13@9292: skidd13@9292: this->bridges->flags |= VL_RESORT; skidd13@9292: this->SortBridgeList(); skidd13@9283: } skidd13@9283: } skidd13@7983: skidd13@9283: virtual void OnResize(Point new_size, Point delta) skidd13@9283: { skidd13@9283: this->vscroll.cap += delta.y / (int)this->resize.step_height; skidd13@9283: this->widget[BBSW_BRIDGE_LIST].data = (this->vscroll.cap << 8) + 1; skidd13@9283: SetVScrollCount(this, this->bridges->list_length); skidd13@8020: skidd13@9283: this->last_size = this->vscroll.cap; truelight@0: } skidd13@9283: }; skidd13@9283: skidd13@9283: /* Set the default size of the Build Bridge Window */ skidd13@9292: uint BuildBridgeWindow::last_size = 4; truelight@0: skidd13@8020: /* Widget definition for the rail bridge selection window */ truelight@0: static const Widget _build_bridge_widgets[] = { skidd13@8020: { WWT_CLOSEBOX, RESIZE_NONE, 7, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, // BBSW_CLOSEBOX skidd13@8020: { WWT_CAPTION, RESIZE_NONE, 7, 11, 199, 0, 13, STR_100D_SELECT_RAIL_BRIDGE, STR_018C_WINDOW_TITLE_DRAG_THIS}, // BBSW_CAPTION skidd13@9292: skidd13@9292: { WWT_TEXTBTN, RESIZE_NONE, 7, 0, 80, 14, 25, STR_SORT_BY, STR_SORT_ORDER_TIP}, // BBSW_DROPDOWN_ORDER skidd13@9292: { WWT_DROPDOWN, RESIZE_NONE, 7, 81, 199, 14, 25, 0x0, STR_SORT_CRITERIA_TIP}, // BBSW_DROPDOWN_CRITERIA skidd13@9292: skidd13@9292: { WWT_MATRIX, RESIZE_BOTTOM, 7, 0, 187, 26, 113, 0x401, STR_101F_BRIDGE_SELECTION_CLICK}, // BBSW_BRIDGE_LIST skidd13@9292: { WWT_SCROLLBAR, RESIZE_BOTTOM, 7, 188, 199, 26, 101, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, // BBSW_SCROLLBAR skidd13@9292: { WWT_RESIZEBOX, RESIZE_TB, 7, 188, 199, 102, 113, 0x0, STR_RESIZE_BUTTON}, // BBSW_RESIZEBOX darkvater@176: { WIDGETS_END}, truelight@0: }; truelight@0: skidd13@8020: /* Window definition for the rail bridge selection window */ truelight@0: static const WindowDesc _build_bridge_desc = { skidd13@9292: WDP_AUTO, WDP_AUTO, 200, 114, 200, 114, Darkvater@5070: WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR, skidd13@7983: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_RESIZABLE, truelight@0: _build_bridge_widgets, truelight@0: }; truelight@0: skidd13@9283: /** skidd13@9283: * Add a buildable bridge to the list. skidd13@9283: * If the list is empty a new one is created. skidd13@9283: * skidd13@9283: * @param bl The list which we want to manage skidd13@9283: * @param item The item to add skidd13@9283: * @return The pointer to the list skidd13@9283: */ skidd13@9283: static GUIBridgeList *PushBridgeList(GUIBridgeList *bl, BuildBridgeData item) skidd13@9283: { skidd13@9283: if (bl == NULL) { skidd13@9283: /* Create the list if needed */ skidd13@9283: bl = new GUIBridgeList(); skidd13@9283: bl->flags |= VL_RESORT; skidd13@9292: if (_bridge_sorting.order) bl->flags |= VL_DESC; skidd13@9283: bl->list_length = 1; skidd13@9292: bl->sort_type = _bridge_sorting.criteria; skidd13@9283: } else { skidd13@9283: /* Resize the list */ skidd13@9283: bl->list_length++; skidd13@9283: } skidd13@9283: skidd13@9283: bl->sort_list = ReallocT(bl->sort_list, bl->list_length); skidd13@9283: skidd13@9283: bl->sort_list[bl->list_length - 1] = item; skidd13@9283: skidd13@9283: return bl; skidd13@9283: } skidd13@9283: skidd13@9283: /** skidd13@9283: * Prepare the data for the build a bridge window. skidd13@9283: * If we can't build a bridge under the given conditions skidd13@9283: * show an error message. skidd13@9283: * skidd13@9283: * @parma start The start tile of the bridge skidd13@9283: * @param end The end tile of the bridge skidd13@9283: * @param transport_type The transport type skidd13@9283: * @param bridge_type The bridge type skidd13@9283: */ belugas@8557: void ShowBuildBridgeWindow(TileIndex start, TileIndex end, TransportType transport_type, byte bridge_type) truelight@0: { truelight@0: DeleteWindowById(WC_BUILD_BRIDGE, 0); truelight@0: skidd13@9283: /* Data type for the bridge. skidd13@9283: * Bit 16,15 = transport type, skidd13@9283: * 14..8 = road/rail pieces, skidd13@9283: * 7..0 = type of bridge */ skidd13@9283: uint32 type = (transport_type << 15) | (bridge_type << 8); truelight@0: skidd13@7983: /* only query bridge building possibility once, result is the same for all bridges! skidd13@7983: * returns CMD_ERROR on failure, and price on success */ skidd13@7983: StringID errmsg = INVALID_STRING_ID; skidd13@9283: CommandCost ret = DoCommand(end, start, type, DC_AUTO | DC_QUERY_COST, CMD_BUILD_BRIDGE); truelight@0: skidd13@9283: GUIBridgeList *bl = NULL; peter1138@2737: if (CmdFailed(ret)) { truelight@0: errmsg = _error_message; tron@2548: } else { skidd13@7983: /* check which bridges can be built skidd13@7983: * get absolute bridge length skidd13@7983: * length of the middle parts of the bridge */ smatz@8398: const uint bridge_len = GetTunnelBridgeLength(start, end); skidd13@7983: /* total length of bridge */ skidd13@7983: const uint tot_bridgedata_len = CalcBridgeLenCostFactor(bridge_len + 2); truelight@0: skidd13@7983: /* loop for all bridgetypes */ belugas@8532: for (BridgeType brd_type = 0; brd_type != MAX_BRIDGES; brd_type++) { belugas@8532: if (CheckBridge_Stuff(brd_type, bridge_len)) { skidd13@7983: /* bridge is accepted, add to list */ skidd13@9283: BuildBridgeData item; skidd13@9283: item.index = brd_type; skidd13@9283: item.spec = GetBridgeSpec(brd_type); skidd13@7983: /* Add to terraforming & bulldozing costs the cost of the skidd13@7983: * bridge itself (not computed with DC_QUERY_COST) */ skidd13@9283: item.cost = ret.GetCost() + (((int64)tot_bridgedata_len * _price.build_bridge * item.spec->price) >> 8); skidd13@9283: bl = PushBridgeList(bl, item); truelight@0: } truelight@0: } truelight@0: } truelight@0: skidd13@9283: if (bl != NULL) { skidd13@9283: new BuildBridgeWindow(&_build_bridge_desc, start, end, type, bl); truelight@0: } else { celestar@3422: ShowErrorMessage(errmsg, STR_5015_CAN_T_BUILD_BRIDGE_HERE, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE); truelight@0: } truelight@0: }