tron@2186: /* $Id$ */ tron@2186: belugas@6443: /** @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@9723: #include "window_gui.h" rubidium@9723: #include "command_func.h" rubidium@9723: #include "economy_func.h" tron@2159: #include "variables.h" celestar@2262: #include "bridge.h" rubidium@9723: #include "strings_func.h" rubidium@9723: #include "window_func.h" rubidium@9723: #include "sound_func.h" rubidium@9723: #include "map_func.h" rubidium@9723: #include "viewport_func.h" rubidium@9723: #include "gfx_func.h" rubidium@9724: #include "tunnelbridge.h" glx@10645: #include "sortlist_type.h" glx@10645: #include "widgets/dropdown_func.h" rubidium@9724: rubidium@9724: #include "table/strings.h" truelight@0: glx@10645: /** glx@10645: * Carriage for the data we need if we want to build a bridge glx@10645: */ glx@10645: struct BuildBridgeData { glx@10645: BridgeType index; glx@10645: const BridgeSpec *spec; glx@10645: Money cost; glx@10645: }; truelight@0: glx@10645: typedef GUIList GUIBridgeList; glx@10645: glx@10645: /** glx@10645: * Callback executed after a build Bridge CMD has been called glx@10645: * glx@10645: * @param scucess True if the build succeded glx@10645: * @param tile The tile where the command has been executed glx@10645: * @param p1 not used glx@10645: * @param p2 not used glx@10645: */ 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: rubidium@9723: /* Names of the build bridge selection window */ rubidium@9723: enum BuildBridgeSelectionWidgets { rubidium@9723: BBSW_CLOSEBOX = 0, rubidium@9723: BBSW_CAPTION, glx@10645: BBSW_DROPDOWN_ORDER, glx@10645: BBSW_DROPDOWN_CRITERIA, rubidium@9723: BBSW_BRIDGE_LIST, rubidium@9723: BBSW_SCROLLBAR, rubidium@9723: BBSW_RESIZEBOX rubidium@9723: }; rubidium@9723: glx@10645: class BuildBridgeWindow : public Window { glx@10645: private: glx@10776: /* Runtime saved values */ glx@10645: static uint last_size; glx@10776: static Listing last_sorting; rubidium@9723: glx@10776: /* Constants for sorting the bridges */ glx@10776: static const StringID sorter_names[]; glx@10776: static GUIBridgeList::SortFunction *const sorter_funcs[]; glx@10776: glx@10776: /* Internal variables */ glx@10645: TileIndex start_tile; glx@10645: TileIndex end_tile; glx@10645: uint32 type; glx@10645: GUIBridgeList *bridges; truelight@0: glx@10776: /** Sort the bridges by their index */ glx@10776: static int CDECL BridgeIndexSorter(const BuildBridgeData *a, const BuildBridgeData *b) glx@10776: { glx@10776: return a->index - b->index; glx@10776: } glx@10776: glx@10776: /** Sort the bridges by their price */ glx@10776: static int CDECL BridgePriceSorter(const BuildBridgeData *a, const BuildBridgeData *b) glx@10776: { glx@10776: return a->cost - b->cost; glx@10776: } glx@10776: glx@10776: /** Sort the bridges by their maximum speed */ glx@10776: static int CDECL BridgeSpeedSorter(const BuildBridgeData *a, const BuildBridgeData *b) glx@10776: { glx@10776: return a->spec->speed - b->spec->speed; glx@10776: } glx@10776: glx@10645: void BuildBridge(uint8 i) glx@10645: { glx@10776: DoCommandP(this->end_tile, this->start_tile, this->type | this->bridges->Get(i)->index, glx@10645: CcBuildBridge, CMD_BUILD_BRIDGE | CMD_MSG(STR_5015_CAN_T_BUILD_BRIDGE_HERE)); glx@10645: } truelight@0: glx@10645: /** Sort the builable bridges */ glx@10645: void SortBridgeList() glx@10645: { glx@10776: this->bridges->Sort(); glx@10645: glx@10645: /* Display the current sort variant */ glx@10776: this->widget[BBSW_DROPDOWN_CRITERIA].data = this->sorter_names[this->bridges->SortType()]; glx@10645: glx@10645: /* Set the modified widgets dirty */ glx@10645: this->InvalidateWidget(BBSW_DROPDOWN_CRITERIA); glx@10645: this->InvalidateWidget(BBSW_BRIDGE_LIST); glx@10645: } glx@10645: glx@10645: public: glx@10645: BuildBridgeWindow(const WindowDesc *desc, TileIndex start, TileIndex end, uint32 br_type, GUIBridgeList *bl) : Window(desc), glx@10645: start_tile(start), glx@10645: end_tile(end), glx@10645: type(br_type), glx@10645: bridges(bl) glx@10645: { glx@10776: this->bridges->SetListing(this->last_sorting); glx@10776: this->bridges->SetSortFuncs(this->sorter_funcs); glx@10920: this->bridges->NeedResort(); glx@10645: this->SortBridgeList(); glx@10645: glx@10645: /* Change the data, or the caption of the gui. Set it to road or rail, accordingly */ glx@10645: this->widget[BBSW_CAPTION].data = (GB(this->type, 15, 2) == TRANSPORT_ROAD) ? STR_1803_SELECT_ROAD_BRIDGE : STR_100D_SELECT_RAIL_BRIDGE; glx@10645: glx@10645: this->resize.step_height = 22; glx@10776: this->vscroll.count = bl->Length(); glx@10645: glx@10645: if (this->last_size <= 4) { glx@10645: this->vscroll.cap = 4; glx@10645: } else { glx@10645: /* Resize the bridge selection window if we used a bigger one the last time */ glx@10645: this->vscroll.cap = (this->vscroll.count > this->last_size) ? this->last_size : this->vscroll.count; glx@10645: ResizeWindow(this, 0, (this->vscroll.cap - 4) * this->resize.step_height); glx@10645: this->widget[BBSW_BRIDGE_LIST].data = (this->vscroll.cap << 8) + 1; rubidium@9723: } peter1138@6530: glx@10645: this->FindWindowPlacementAndResize(desc); glx@10645: } peter1138@6530: glx@10645: ~BuildBridgeWindow() glx@10645: { glx@10776: this->last_sorting = this->bridges->GetListing(); glx@10776: glx@10645: delete bridges; glx@10645: } truelight@193: glx@10645: virtual void OnPaint() glx@10645: { glx@10645: this->DrawWidgets(); glx@10645: glx@10645: this->DrawSortButtonState(BBSW_DROPDOWN_ORDER, (this->bridges->flags & VL_DESC) ? SBS_DOWN : SBS_UP); glx@10645: glx@10645: uint y = this->widget[BBSW_BRIDGE_LIST].top + 2; glx@10645: glx@10776: for (int i = this->vscroll.pos; (i < (this->vscroll.cap + this->vscroll.pos)) && (i < (int)this->bridges->Length()); i++) { glx@10776: const BridgeSpec *b = this->bridges->Get(i)->spec; glx@10645: glx@10776: SetDParam(2, this->bridges->Get(i)->cost); glx@10645: SetDParam(1, b->speed * 10 / 16); glx@10645: SetDParam(0, b->material); glx@10645: glx@10645: DrawSprite(b->sprite, b->pal, 3, y); glx@10645: DrawString(44, y, STR_500D, TC_FROMSTRING); glx@10645: y += this->resize.step_height; glx@10645: glx@10645: } glx@10645: } glx@10645: glx@10645: virtual EventState OnKeyPress(uint16 key, uint16 keycode) glx@10645: { glx@10645: const uint8 i = keycode - '1'; glx@10776: if (i < 9 && i < this->bridges->Length()) { glx@10645: /* Build the requested bridge */ glx@10645: this->BuildBridge(i); glx@10645: delete this; glx@10645: return ES_HANDLED; glx@10645: } glx@10645: return ES_NOT_HANDLED; glx@10645: } glx@10645: glx@10645: virtual void OnClick(Point pt, int widget) glx@10645: { glx@10645: switch (widget) { glx@10645: default: break; glx@10645: case BBSW_BRIDGE_LIST: { glx@10645: uint i = ((int)pt.y - this->widget[BBSW_BRIDGE_LIST].top) / this->resize.step_height; glx@10645: if (i < this->vscroll.cap) { glx@10645: i += this->vscroll.pos; glx@10776: if (i < this->bridges->Length()) { glx@10645: this->BuildBridge(i); glx@10645: delete this; rubidium@9723: } rubidium@9723: } glx@10645: } break; rubidium@9723: glx@10645: case BBSW_DROPDOWN_ORDER: glx@10776: this->bridges->ToggleSortOrder(); glx@10776: this->SetDirty(); glx@10645: break; glx@10645: glx@10645: case BBSW_DROPDOWN_CRITERIA: glx@10776: ShowDropDownMenu(this, this->sorter_names, this->bridges->SortType(), BBSW_DROPDOWN_CRITERIA, 0, 0); glx@10645: break; glx@10645: } truelight@0: } glx@10645: glx@10645: virtual void OnDropdownSelect(int widget, int index) glx@10645: { glx@10776: if (widget == BBSW_DROPDOWN_CRITERIA && this->bridges->SortType() != index) { glx@10776: this->bridges->SetSortType(index); glx@10645: glx@10645: this->SortBridgeList(); glx@10645: } glx@10645: } glx@10645: glx@10645: virtual void OnResize(Point new_size, Point delta) glx@10645: { glx@10645: this->vscroll.cap += delta.y / (int)this->resize.step_height; glx@10645: this->widget[BBSW_BRIDGE_LIST].data = (this->vscroll.cap << 8) + 1; glx@10776: SetVScrollCount(this, this->bridges->Length()); glx@10645: glx@10645: this->last_size = this->vscroll.cap; glx@10645: } glx@10645: }; glx@10645: glx@10645: /* Set the default size of the Build Bridge Window */ glx@10645: uint BuildBridgeWindow::last_size = 4; glx@10776: /* Set the default sorting for the bridges */ glx@10776: Listing BuildBridgeWindow::last_sorting = {false, 0}; glx@10776: glx@10776: /* Availible bridge sorting functions */ glx@10776: GUIBridgeList::SortFunction* const BuildBridgeWindow::sorter_funcs[] = { glx@10776: &BridgeIndexSorter, glx@10776: &BridgePriceSorter, glx@10776: &BridgeSpeedSorter glx@10776: }; glx@10776: glx@10776: /* Names of the sorting functions */ glx@10776: const StringID BuildBridgeWindow::sorter_names[] = { glx@10776: STR_SORT_BY_NUMBER, glx@10776: STR_ENGINE_SORT_COST, glx@10776: STR_SORT_BY_MAX_SPEED, glx@10776: INVALID_STRING_ID glx@10776: }; truelight@0: rubidium@9723: /* Widget definition for the rail bridge selection window */ truelight@0: static const Widget _build_bridge_widgets[] = { rubidium@9723: { WWT_CLOSEBOX, RESIZE_NONE, 7, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, // BBSW_CLOSEBOX rubidium@9723: { WWT_CAPTION, RESIZE_NONE, 7, 11, 199, 0, 13, STR_100D_SELECT_RAIL_BRIDGE, STR_018C_WINDOW_TITLE_DRAG_THIS}, // BBSW_CAPTION glx@10645: glx@10645: { WWT_TEXTBTN, RESIZE_NONE, 7, 0, 80, 14, 25, STR_SORT_BY, STR_SORT_ORDER_TIP}, // BBSW_DROPDOWN_ORDER glx@10645: { WWT_DROPDOWN, RESIZE_NONE, 7, 81, 199, 14, 25, 0x0, STR_SORT_CRITERIA_TIP}, // BBSW_DROPDOWN_CRITERIA glx@10645: glx@10645: { WWT_MATRIX, RESIZE_BOTTOM, 7, 0, 187, 26, 113, 0x401, STR_101F_BRIDGE_SELECTION_CLICK}, // BBSW_BRIDGE_LIST glx@10645: { WWT_SCROLLBAR, RESIZE_BOTTOM, 7, 188, 199, 26, 101, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, // BBSW_SCROLLBAR glx@10645: { WWT_RESIZEBOX, RESIZE_TB, 7, 188, 199, 102, 113, 0x0, STR_RESIZE_BUTTON}, // BBSW_RESIZEBOX darkvater@176: { WIDGETS_END}, truelight@0: }; truelight@0: rubidium@9723: /* Window definition for the rail bridge selection window */ truelight@0: static const WindowDesc _build_bridge_desc = { glx@10645: WDP_AUTO, WDP_AUTO, 200, 114, 200, 114, Darkvater@5070: WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR, rubidium@9723: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_RESIZABLE, truelight@0: _build_bridge_widgets, truelight@0: }; truelight@0: glx@10645: /** glx@10645: * Prepare the data for the build a bridge window. glx@10645: * If we can't build a bridge under the given conditions glx@10645: * show an error message. glx@10645: * glx@10645: * @parma start The start tile of the bridge glx@10645: * @param end The end tile of the bridge glx@10645: * @param transport_type The transport type glx@10645: * @param bridge_type The bridge type glx@10645: */ glx@9732: void ShowBuildBridgeWindow(TileIndex start, TileIndex end, TransportType transport_type, byte bridge_type) truelight@0: { truelight@0: DeleteWindowById(WC_BUILD_BRIDGE, 0); truelight@0: glx@10645: /* Data type for the bridge. glx@10645: * Bit 16,15 = transport type, glx@10645: * 14..8 = road/rail pieces, glx@10645: * 7..0 = type of bridge */ glx@10645: uint32 type = (transport_type << 15) | (bridge_type << 8); truelight@0: rubidium@9723: /* only query bridge building possibility once, result is the same for all bridges! rubidium@9723: * returns CMD_ERROR on failure, and price on success */ rubidium@9723: StringID errmsg = INVALID_STRING_ID; glx@10645: CommandCost ret = DoCommand(end, start, type, DC_AUTO | DC_QUERY_COST, CMD_BUILD_BRIDGE); truelight@0: glx@10645: GUIBridgeList *bl = NULL; peter1138@2737: if (CmdFailed(ret)) { truelight@0: errmsg = _error_message; tron@2548: } else { rubidium@9723: /* check which bridges can be built rubidium@9723: * get absolute bridge length rubidium@9723: * length of the middle parts of the bridge */ rubidium@9724: const uint bridge_len = GetTunnelBridgeLength(start, end); rubidium@9723: /* total length of bridge */ rubidium@9723: const uint tot_bridgedata_len = CalcBridgeLenCostFactor(bridge_len + 2); truelight@0: glx@10776: bl = new GUIBridgeList(); glx@10776: rubidium@9723: /* loop for all bridgetypes */ glx@9732: for (BridgeType brd_type = 0; brd_type != MAX_BRIDGES; brd_type++) { glx@9732: if (CheckBridge_Stuff(brd_type, bridge_len)) { rubidium@9723: /* bridge is accepted, add to list */ glx@10776: BuildBridgeData *item = bl->Append(); glx@10776: item->index = brd_type; glx@10776: item->spec = GetBridgeSpec(brd_type); rubidium@9723: /* Add to terraforming & bulldozing costs the cost of the rubidium@9723: * bridge itself (not computed with DC_QUERY_COST) */ glx@10776: item->cost = ret.GetCost() + (((int64)tot_bridgedata_len * _price.build_bridge * item->spec->price) >> 8); truelight@0: } truelight@0: } truelight@0: } truelight@0: glx@10776: if (bl != NULL && bl->Length() != 0) { glx@10645: new BuildBridgeWindow(&_build_bridge_desc, start, end, type, bl); truelight@0: } else { glx@10776: 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: }