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@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@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@9367: /* Runtime saved values */ skidd13@9292: static uint last_size; skidd13@9367: static Listing last_sorting; skidd13@8020: skidd13@9367: /* Constants for sorting the bridges */ skidd13@9367: static const StringID sorter_names[]; glx@9370: static GUIBridgeList::SortFunction *const sorter_funcs[]; skidd13@9367: skidd13@9367: /* Internal variables */ skidd13@9283: TileIndex start_tile; skidd13@9283: TileIndex end_tile; skidd13@9283: uint32 type; skidd13@9292: GUIBridgeList *bridges; truelight@0: skidd13@9367: /** Sort the bridges by their index */ smatz@9376: static int CDECL BridgeIndexSorter(const BuildBridgeData *a, const BuildBridgeData *b) skidd13@9367: { skidd13@9367: return a->index - b->index; skidd13@9367: } skidd13@9367: skidd13@9367: /** Sort the bridges by their price */ smatz@9376: static int CDECL BridgePriceSorter(const BuildBridgeData *a, const BuildBridgeData *b) skidd13@9367: { skidd13@9367: return a->cost - b->cost; skidd13@9367: } skidd13@9367: skidd13@9367: /** Sort the bridges by their maximum speed */ smatz@9376: static int CDECL BridgeSpeedSorter(const BuildBridgeData *a, const BuildBridgeData *b) skidd13@9367: { skidd13@9367: return a->spec->speed - b->spec->speed; skidd13@9367: } skidd13@9367: skidd13@9283: void BuildBridge(uint8 i) skidd13@9283: { peter1138@9365: DoCommandP(this->end_tile, this->start_tile, this->type | this->bridges->Get(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@9367: this->bridges->Sort(); skidd13@9292: skidd13@9292: /* Display the current sort variant */ skidd13@9367: this->widget[BBSW_DROPDOWN_CRITERIA].data = this->sorter_names[this->bridges->SortType()]; 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@9367: this->bridges->SetListing(this->last_sorting); skidd13@9367: this->bridges->SetSortFuncs(this->sorter_funcs); rubidium@9480: this->bridges->NeedResort(); 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; peter1138@9365: this->vscroll.count = bl->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@9367: this->last_sorting = this->bridges->GetListing(); skidd13@9367: skidd13@9283: delete bridges; skidd13@9283: } skidd13@9283: skidd13@9283: virtual void OnPaint() skidd13@9283: { skidd13@9283: this->DrawWidgets(); skidd13@9283: skidd13@9529: this->DrawSortButtonState(BBSW_DROPDOWN_ORDER, this->bridges->IsDescSortOrder() ? SBS_DOWN : SBS_UP); skidd13@9283: skidd13@9292: uint y = this->widget[BBSW_BRIDGE_LIST].top + 2; skidd13@9292: peter1138@9365: for (int i = this->vscroll.pos; (i < (this->vscroll.cap + this->vscroll.pos)) && (i < (int)this->bridges->Length()); i++) { peter1138@9365: const BridgeSpec *b = this->bridges->Get(i)->spec; skidd13@9292: peter1138@9365: SetDParam(2, this->bridges->Get(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'; peter1138@9365: if (i < 9 && i < this->bridges->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; peter1138@9365: if (i < this->bridges->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@9367: this->bridges->ToggleSortOrder(); skidd13@9367: this->SetDirty(); skidd13@9292: break; skidd13@9292: skidd13@9292: case BBSW_DROPDOWN_CRITERIA: skidd13@9367: ShowDropDownMenu(this, this->sorter_names, this->bridges->SortType(), 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@9367: if (widget == BBSW_DROPDOWN_CRITERIA && this->bridges->SortType() != index) { skidd13@9367: this->bridges->SetSortType(index); skidd13@9292: 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; peter1138@9365: SetVScrollCount(this, this->bridges->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; skidd13@9367: /* Set the default sorting for the bridges */ skidd13@9367: Listing BuildBridgeWindow::last_sorting = {false, 0}; skidd13@9367: skidd13@9367: /* Availible bridge sorting functions */ skidd13@9367: GUIBridgeList::SortFunction* const BuildBridgeWindow::sorter_funcs[] = { skidd13@9367: &BridgeIndexSorter, skidd13@9367: &BridgePriceSorter, skidd13@9367: &BridgeSpeedSorter skidd13@9367: }; skidd13@9367: skidd13@9367: /* Names of the sorting functions */ skidd13@9367: const StringID BuildBridgeWindow::sorter_names[] = { skidd13@9367: STR_SORT_BY_NUMBER, skidd13@9367: STR_ENGINE_SORT_COST, skidd13@9367: STR_SORT_BY_MAX_SPEED, skidd13@9367: INVALID_STRING_ID skidd13@9367: }; 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: * 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: peter1138@9365: bl = new GUIBridgeList(); peter1138@9365: 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 */ peter1138@9365: BuildBridgeData *item = bl->Append(); peter1138@9365: item->index = brd_type; peter1138@9365: 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) */ peter1138@9365: item->cost = ret.GetCost() + (((int64)tot_bridgedata_len * _price.build_bridge * item->spec->price) >> 8); truelight@0: } truelight@0: } truelight@0: } truelight@0: peter1138@9365: if (bl != NULL && bl->Length() != 0) { skidd13@9283: new BuildBridgeWindow(&_build_bridge_desc, start, end, type, bl); truelight@0: } else { peter1138@9365: if (bl != NULL) delete bl; celestar@3422: ShowErrorMessage(errmsg, STR_5015_CAN_T_BUILD_BRIDGE_HERE, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE); truelight@0: } truelight@0: }