# bridge_gui.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "gui.h"
    00014 #include "command_func.h"
    00015 #include "rail.h"
    00016 #include "strings_func.h"
    00017 #include "window_func.h"
    00018 #include "sound_func.h"
    00019 #include "gfx_func.h"
    00020 #include "tunnelbridge.h"
    00021 #include "sortlist_type.h"
    00022 #include "widgets/dropdown_func.h"
    00023 #include "core/geometry_func.hpp"
    00024 #include "cmd_helper.h"
    00025 #include "tunnelbridge_map.h"
    00026 #include "road_gui.h"
    00027
    00028 #include "table/strings.h"
    00029
    00031 static BridgeType _last_railbridge_type = 0;
    00033 static BridgeType _last_roadbridge_type = 0;
    00034
    00038 struct BuildBridgeData {
    00039   BridgeType index;
    00040   const BridgeSpec *spec;
    00041   Money cost;
    00042 };
    00043
    00044 typedef GUIList<BuildBridgeData> GUIBridgeList;
    00045
    00057 void CcBuildBridge(const CommandCost &result, TileIndex end_tile, uint32 p1, uint32 p2)
    00058 {
    00059   if (result.Failed()) return;
    00060   SndPlayTileFx(SND_27_BLACKSMITH_ANVIL, end_tile);
    00061
    00062   TransportType transport_type = Extract<TransportType, 15, 2>(p2);
    00063
    00064   if (transport_type == TRANSPORT_ROAD) {
    00065     DiagDirection end_direction = ReverseDiagDir(GetTunnelBridgeDirection(end_tile));
    00066     ConnectRoadToStructure(end_tile, end_direction);
    00067
    00068     DiagDirection start_direction = ReverseDiagDir(GetTunnelBridgeDirection(p1));
    00069     ConnectRoadToStructure(p1, start_direction);
    00070   }
    00071 }
    00072
    00074 enum BuildBridgeSelectionWidgets {
    00075   BBSW_CAPTION,
    00076   BBSW_DROPDOWN_ORDER,
    00077   BBSW_DROPDOWN_CRITERIA,
    00078   BBSW_BRIDGE_LIST,
    00079   BBSW_SCROLLBAR,
    00080 };
    00081
    00083 class BuildBridgeWindow : public Window {
    00084 private:
    00085   /* Runtime saved values */
    00086   static uint16 last_size;
    00087   static Listing last_sorting;
    00088
    00089   /* Constants for sorting the bridges */
    00090   static const StringID sorter_names[];
    00091   static GUIBridgeList::SortFunction * const sorter_funcs[];
    00092
    00093   /* Internal variables */
    00094   TileIndex start_tile;
    00095   TileIndex end_tile;
    00096   uint32 type;
    00097   GUIBridgeList *bridges;
    00098   int bridgetext_offset;
    00099   Scrollbar *vscroll;
    00100
    00102   static int CDECL BridgeIndexSorter(const BuildBridgeData *a, const BuildBridgeData *b)
    00103   {
    00104     return a->index - b->index;
    00105   }
    00106
    00108   static int CDECL BridgePriceSorter(const BuildBridgeData *a, const BuildBridgeData *b)
    00109   {
    00110     return a->cost - b->cost;
    00111   }
    00112
    00114   static int CDECL BridgeSpeedSorter(const BuildBridgeData *a, const BuildBridgeData *b)
    00115   {
    00116     return a->spec->speed - b->spec->speed;
    00117   }
    00118
    00119   void BuildBridge(uint8 i)
    00120   {
    00121     switch ((TransportType)(this->type >> 15)) {
    00122       case TRANSPORT_RAIL: _last_railbridge_type = this->bridges->Get(i)->index; break;
    00123       case TRANSPORT_ROAD: _last_roadbridge_type = this->bridges->Get(i)->index; break;
    00124       default: break;
    00125     }
    00126     DoCommandP(this->end_tile, this->start_tile, this->type | this->bridges->Get(i)->index,
    00127           CMD_BUILD_BRIDGE | CMD_MSG(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE), CcBuildBridge);
    00128   }
    00129
    00131   void SortBridgeList()
    00132   {
    00133     this->bridges->Sort();
    00134
    00135     /* Display the current sort variant */
    00136     this->GetWidget<NWidgetCore>(BBSW_DROPDOWN_CRITERIA)->widget_data = this->sorter_names[this->bridges->SortType()];
    00137
    00138     /* Set the modified widgets dirty */
    00139     this->SetWidgetDirty(BBSW_DROPDOWN_CRITERIA);
    00140     this->SetWidgetDirty(BBSW_BRIDGE_LIST);
    00141   }
    00142
    00143 public:
    00144   BuildBridgeWindow(const WindowDesc *desc, TileIndex start, TileIndex end, uint32 br_type, GUIBridgeList *bl) : Window(),
    00145     start_tile(start),
    00146     end_tile(end),
    00147     type(br_type),
    00148     bridges(bl)
    00149   {
    00150     this->CreateNestedTree(desc);
    00151     this->vscroll = this->GetScrollbar(BBSW_SCROLLBAR);
    00152     /* Change the data, or the caption of the gui. Set it to road or rail, accordingly. */
    00153     this->GetWidget<NWidgetCore>(BBSW_CAPTION)->widget_data = (GB(this->type, 15, 2) == TRANSPORT_ROAD) ? STR_SELECT_ROAD_BRIDGE_CAPTION : STR_SELECT_RAIL_BRIDGE_CAPTION;
    00154     this->FinishInitNested(desc, GB(br_type, 15, 2)); // Initializes 'this->bridgetext_offset'.
    00155
    00156     this->parent = FindWindowById(WC_BUILD_TOOLBAR, GB(this->type, 15, 2));
    00157     this->bridges->SetListing(this->last_sorting);
    00158     this->bridges->SetSortFuncs(this->sorter_funcs);
    00159     this->bridges->NeedResort();
    00160     this->SortBridgeList();
    00161
    00162     this->vscroll->SetCount(bl->Length());
    00163     if (this->last_size < this->vscroll->GetCapacity()) this->last_size = this->vscroll->GetCapacity();
    00164     if (this->last_size > this->vscroll->GetCount()) this->last_size = this->vscroll->GetCount();
    00165     /* Resize the bridge selection window if we used a bigger one the last time. */
    00166     if (this->last_size > this->vscroll->GetCapacity()) {
    00167       ResizeWindow(this, 0, (this->last_size - this->vscroll->GetCapacity()) * this->resize.step_height);
    00168     }
    00169     this->GetWidget<NWidgetCore>(BBSW_BRIDGE_LIST)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00170   }
    00171
    00172   ~BuildBridgeWindow()
    00173   {
    00174     this->last_sorting = this->bridges->GetListing();
    00175
    00176     delete bridges;
    00177   }
    00178
    00179   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00180   {
    00181     switch (widget) {
    00182       case BBSW_DROPDOWN_ORDER: {
    00183         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
    00184         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the string is centred and it also looks better.
    00185         d.height += padding.height;
    00186         *size = maxdim(*size, d);
    00187         break;
    00188       }
    00189       case BBSW_DROPDOWN_CRITERIA: {
    00190         Dimension d = {0, 0};
    00191         for (const StringID *str = this->sorter_names; *str != INVALID_STRING_ID; str++) {
    00192           d = maxdim(d, GetStringBoundingBox(*str));
    00193         }
    00194         d.width += padding.width;
    00195         d.height += padding.height;
    00196         *size = maxdim(*size, d);
    00197         break;
    00198       }
    00199       case BBSW_BRIDGE_LIST: {
    00200         Dimension sprite_dim = {0, 0}; // Biggest bridge sprite dimension
    00201         Dimension text_dim   = {0, 0}; // Biggest text dimension
    00202         for (int i = 0; i < (int)this->bridges->Length(); i++) {
    00203           const BridgeSpec *b = this->bridges->Get(i)->spec;
    00204           sprite_dim = maxdim(sprite_dim, GetSpriteSize(b->sprite));
    00205
    00206           SetDParam(2, this->bridges->Get(i)->cost);
    00207           SetDParam(1, b->speed);
    00208           SetDParam(0, b->material);
    00209           text_dim = maxdim(text_dim, GetStringBoundingBox(_game_mode == GM_EDITOR ? STR_SELECT_BRIDGE_SCENEDIT_INFO : STR_SELECT_BRIDGE_INFO));
    00210         }
    00211         sprite_dim.height++; // Sprite is rendered one pixel down in the matrix field.
    00212         text_dim.height++; // Allowing the bottom row pixels to be rendered on the edge of the matrix field.
    00213         resize->height = max(sprite_dim.height, text_dim.height) + 2; // Max of both sizes + account for matrix edges.
    00214
    00215         this->bridgetext_offset = WD_MATRIX_LEFT + sprite_dim.width + 1; // Left edge of text, 1 pixel distance from the sprite.
    00216         size->width = this->bridgetext_offset + text_dim.width + WD_MATRIX_RIGHT;
    00217         size->height = 4 * resize->height; // Smallest bridge gui is 4 entries high in the matrix.
    00218         break;
    00219       }
    00220     }
    00221   }
    00222
    00223   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
    00224   {
    00225     /* Position the window so hopefully the first bridge from the list is under the mouse pointer. */
    00226     NWidgetBase *list = this->GetWidget<NWidgetBase>(BBSW_BRIDGE_LIST);
    00227     Point corner; // point of the top left corner of the window.
    00228     corner.y = Clamp(_cursor.pos.y - list->pos_y - 5, GetMainViewTop(), GetMainViewBottom() - sm_height);
    00229     corner.x = Clamp(_cursor.pos.x - list->pos_x - 5, 0, _screen.width - sm_width);
    00230     return corner;
    00231   }
    00232
    00233   virtual void DrawWidget(const Rect &r, int widget) const
    00234   {
    00235     switch (widget) {
    00236       case BBSW_DROPDOWN_ORDER:
    00237         this->DrawSortButtonState(widget, this->bridges->IsDescSortOrder() ? SBS_DOWN : SBS_UP);
    00238         break;
    00239
    00240       case BBSW_BRIDGE_LIST: {
    00241         uint y = r.top;
    00242         for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < (int)this->bridges->Length(); i++) {
    00243           const BridgeSpec *b = this->bridges->Get(i)->spec;
    00244
    00245           SetDParam(2, this->bridges->Get(i)->cost);
    00246           SetDParam(1, b->speed);
    00247           SetDParam(0, b->material);
    00248
    00249           DrawSprite(b->sprite, b->pal, r.left + WD_MATRIX_LEFT, y + this->resize.step_height - 1 - GetSpriteSize(b->sprite).height);
    00250           DrawStringMultiLine(r.left + this->bridgetext_offset, r.right, y + 2, y + this->resize.step_height,
    00251               _game_mode == GM_EDITOR ? STR_SELECT_BRIDGE_SCENEDIT_INFO : STR_SELECT_BRIDGE_INFO);
    00252           y += this->resize.step_height;
    00253         }
    00254         break;
    00255       }
    00256     }
    00257   }
    00258
    00259   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00260   {
    00261     const uint8 i = keycode - '1';
    00262     if (i < 9 && i < this->bridges->Length()) {
    00263       /* Build the requested bridge */
    00264       this->BuildBridge(i);
    00265       delete this;
    00266       return ES_HANDLED;
    00267     }
    00268     return ES_NOT_HANDLED;
    00269   }
    00270
    00271   virtual void OnClick(Point pt, int widget, int click_count)
    00272   {
    00273     switch (widget) {
    00274       default: break;
    00275       case BBSW_BRIDGE_LIST: {
    00276         uint i = this->vscroll->GetScrolledRowFromWidget(pt.y, this, BBSW_BRIDGE_LIST);
    00277         if (i < this->bridges->Length()) {
    00278           this->BuildBridge(i);
    00279           delete this;
    00280         }
    00281         break;
    00282       }
    00283
    00284       case BBSW_DROPDOWN_ORDER:
    00285         this->bridges->ToggleSortOrder();
    00286         this->SetDirty();
    00287         break;
    00288
    00289       case BBSW_DROPDOWN_CRITERIA:
    00290         ShowDropDownMenu(this, this->sorter_names, this->bridges->SortType(), BBSW_DROPDOWN_CRITERIA, 0, 0);
    00291         break;
    00292     }
    00293   }
    00294
    00295   virtual void OnDropdownSelect(int widget, int index)
    00296   {
    00297     if (widget == BBSW_DROPDOWN_CRITERIA && this->bridges->SortType() != index) {
    00298       this->bridges->SetSortType(index);
    00299
    00300       this->SortBridgeList();
    00301     }
    00302   }
    00303
    00304   virtual void OnResize()
    00305   {
    00306     this->vscroll->SetCapacityFromWidget(this, BBSW_BRIDGE_LIST);
    00307     this->GetWidget<NWidgetCore>(BBSW_BRIDGE_LIST)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00308
    00309     this->last_size = max(this->vscroll->GetCapacity(), this->last_size);
    00310   }
    00311 };
    00312
    00314 uint16 BuildBridgeWindow::last_size = 4;
    00316 Listing BuildBridgeWindow::last_sorting = {true, 2};
    00317
    00319 GUIBridgeList::SortFunction * const BuildBridgeWindow::sorter_funcs[] = {
    00320   &BridgeIndexSorter,
    00321   &BridgePriceSorter,
    00322   &BridgeSpeedSorter
    00323 };
    00324
    00326 const StringID BuildBridgeWindow::sorter_names[] = {
    00327   STR_SORT_BY_NUMBER,
    00328   STR_SORT_BY_COST,
    00329   STR_SORT_BY_MAX_SPEED,
    00330   INVALID_STRING_ID
    00331 };
    00332
    00334 static const NWidgetPart _nested_build_bridge_widgets[] = {
    00335   /* Header */
    00336   NWidget(NWID_HORIZONTAL),
    00337     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00338     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, BBSW_CAPTION), SetDataTip(STR_SELECT_RAIL_BRIDGE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00339   EndContainer(),
    00340
    00341   NWidget(NWID_HORIZONTAL),
    00342     NWidget(NWID_VERTICAL),
    00343       /* Sort order + criteria buttons */
    00344       NWidget(NWID_HORIZONTAL),
    00345         NWidget(WWT_TEXTBTN, COLOUR_DARK_GREEN, BBSW_DROPDOWN_ORDER), SetFill(1, 0), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
    00346         NWidget(WWT_DROPDOWN, COLOUR_DARK_GREEN, BBSW_DROPDOWN_CRITERIA), SetFill(1, 0), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
    00347       EndContainer(),
    00348       /* Matrix. */
    00349       NWidget(WWT_MATRIX, COLOUR_DARK_GREEN, BBSW_BRIDGE_LIST), SetFill(1, 0), SetResize(0, 22), SetDataTip(0x401, STR_SELECT_BRIDGE_SELECTION_TOOLTIP), SetScrollbar(BBSW_SCROLLBAR),
    00350     EndContainer(),
    00351
    00352     /* scrollbar + resize button */
    00353     NWidget(NWID_VERTICAL),
    00354       NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, BBSW_SCROLLBAR),
    00355       NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
    00356     EndContainer(),
    00357   EndContainer(),
    00358 };
    00359
    00361 static const WindowDesc _build_bridge_desc(
    00362   WDP_AUTO, 200, 114,
    00363   WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
    00364   WDF_CONSTRUCTION,
    00365   _nested_build_bridge_widgets, lengthof(_nested_build_bridge_widgets)
    00366 );
    00367
    00378 void ShowBuildBridgeWindow(TileIndex start, TileIndex end, TransportType transport_type, byte road_rail_type)
    00379 {
    00380   DeleteWindowByClass(WC_BUILD_BRIDGE);
    00381
    00382   /* Data type for the bridge.
    00383    * Bit 16,15 = transport type,
    00384    *     14..8 = road/rail types,
    00385    *      7..0 = type of bridge */
    00386   uint32 type = (transport_type << 15) | (road_rail_type << 8);
    00387
    00388   /* The bridge length without ramps. */
    00389   const uint bridge_len = GetTunnelBridgeLength(start, end);
    00390
    00391   /* If Ctrl is being pressed, check wether the last bridge built is available
    00392    * If so, return this bridge type. Otherwise continue normally.
    00393    * We store bridge types for each transport type, so we have to check for
    00394    * the transport type beforehand.
    00395    */
    00396   BridgeType last_bridge_type = 0;
    00397   switch (transport_type) {
    00398     case TRANSPORT_ROAD: last_bridge_type = _last_roadbridge_type; break;
    00399     case TRANSPORT_RAIL: last_bridge_type = _last_railbridge_type; break;
    00400     default: break; // water ways and air routes don't have bridge types
    00401   }
    00402   if (_ctrl_pressed && CheckBridgeAvailability(last_bridge_type, bridge_len).Succeeded()) {
    00403     DoCommandP(end, start, type | last_bridge_type, CMD_BUILD_BRIDGE | CMD_MSG(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE), CcBuildBridge);
    00404     return;
    00405   }
    00406
    00407   /* only query bridge building possibility once, result is the same for all bridges!
    00408    * returns CMD_ERROR on failure, and price on success */
    00409   StringID errmsg = INVALID_STRING_ID;
    00410   CommandCost ret = DoCommand(end, start, type, CommandFlagsToDCFlags(GetCommandFlags(CMD_BUILD_BRIDGE)) | DC_QUERY_COST, CMD_BUILD_BRIDGE);
    00411
    00412   GUIBridgeList *bl = NULL;
    00413   if (ret.Failed()) {
    00414     errmsg = ret.GetErrorMessage();
    00415   } else {
    00416     /* check which bridges can be built */
    00417     const uint tot_bridgedata_len = CalcBridgeLenCostFactor(bridge_len + 2);
    00418
    00419     bl = new GUIBridgeList();
    00420
    00421     Money infra_cost = 0;
    00422     switch (transport_type) {
    00423       case TRANSPORT_ROAD: infra_cost = (bridge_len + 2) * _price[PR_BUILD_ROAD] * 2; break;
    00424       case TRANSPORT_RAIL: infra_cost = (bridge_len + 2) * RailBuildCost((RailType)road_rail_type); break;
    00425       default: break;
    00426     }
    00427
    00428     /* loop for all bridgetypes */
    00429     for (BridgeType brd_type = 0; brd_type != MAX_BRIDGES; brd_type++) {
    00430       if (CheckBridgeAvailability(brd_type, bridge_len).Succeeded()) {
    00431         /* bridge is accepted, add to list */
    00432         BuildBridgeData *item = bl->Append();
    00433         item->index = brd_type;
    00434         item->spec = GetBridgeSpec(brd_type);
    00435         /* Add to terraforming & bulldozing costs the cost of the
    00436          * bridge itself (not computed with DC_QUERY_COST) */
    00437         item->cost = ret.GetCost() + (((int64)tot_bridgedata_len * _price[PR_BUILD_BRIDGE] * item->spec->price) >> 8) + infra_cost;
    00438       }
    00439     }
    00440   }
    00441
    00442   if (bl != NULL && bl->Length() != 0) {
    00443     new BuildBridgeWindow(&_build_bridge_desc, start, end, type, bl);
    00444   } else {
    00445     delete bl;
    00446     ShowErrorMessage(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE, errmsg, WL_INFO, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE);
    00447   }
    00448 }