# bridge_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: bridge_gui.cpp 25668 2013-08-05 20:36:24Z michi_cc $ */
    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 "error.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 "widgets/bridge_widget.h"
    00029
    00030 #include "table/strings.h"
    00031
    00033 static BridgeType _last_railbridge_type = 0;
    00035 static BridgeType _last_roadbridge_type = 0;
    00036
    00040 struct BuildBridgeData {
    00041   BridgeType index;
    00042   const BridgeSpec *spec;
    00043   Money cost;
    00044 };
    00045
    00046 typedef GUIList<BuildBridgeData> GUIBridgeList;
    00047
    00059 void CcBuildBridge(const CommandCost &result, TileIndex end_tile, uint32 p1, uint32 p2)
    00060 {
    00061   if (result.Failed()) return;
    00062   if (_settings_client.sound.confirm) SndPlayTileFx(SND_27_BLACKSMITH_ANVIL, end_tile);
    00063
    00064   TransportType transport_type = Extract<TransportType, 15, 2>(p2);
    00065
    00066   if (transport_type == TRANSPORT_ROAD) {
    00067     DiagDirection end_direction = ReverseDiagDir(GetTunnelBridgeDirection(end_tile));
    00068     ConnectRoadToStructure(end_tile, end_direction);
    00069
    00070     DiagDirection start_direction = ReverseDiagDir(GetTunnelBridgeDirection(p1));
    00071     ConnectRoadToStructure(p1, start_direction);
    00072   }
    00073 }
    00074
    00076 class BuildBridgeWindow : public Window {
    00077 private:
    00078   /* Runtime saved values */
    00079   static Listing last_sorting;
    00080
    00081   /* Constants for sorting the bridges */
    00082   static const StringID sorter_names[];
    00083   static GUIBridgeList::SortFunction * const sorter_funcs[];
    00084
    00085   /* Internal variables */
    00086   TileIndex start_tile;
    00087   TileIndex end_tile;
    00088   uint32 type;
    00089   GUIBridgeList *bridges;
    00090   int bridgetext_offset;
    00091   Scrollbar *vscroll;
    00092
    00094   static int CDECL BridgeIndexSorter(const BuildBridgeData *a, const BuildBridgeData *b)
    00095   {
    00096     return a->index - b->index;
    00097   }
    00098
    00100   static int CDECL BridgePriceSorter(const BuildBridgeData *a, const BuildBridgeData *b)
    00101   {
    00102     return a->cost - b->cost;
    00103   }
    00104
    00106   static int CDECL BridgeSpeedSorter(const BuildBridgeData *a, const BuildBridgeData *b)
    00107   {
    00108     return a->spec->speed - b->spec->speed;
    00109   }
    00110
    00111   void BuildBridge(uint8 i)
    00112   {
    00113     switch ((TransportType)(this->type >> 15)) {
    00114       case TRANSPORT_RAIL: _last_railbridge_type = this->bridges->Get(i)->index; break;
    00115       case TRANSPORT_ROAD: _last_roadbridge_type = this->bridges->Get(i)->index; break;
    00116       default: break;
    00117     }
    00118     DoCommandP(this->end_tile, this->start_tile, this->type | this->bridges->Get(i)->index,
    00119           CMD_BUILD_BRIDGE | CMD_MSG(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE), CcBuildBridge);
    00120   }
    00121
    00123   void SortBridgeList()
    00124   {
    00125     this->bridges->Sort();
    00126
    00127     /* Display the current sort variant */
    00128     this->GetWidget<NWidgetCore>(WID_BBS_DROPDOWN_CRITERIA)->widget_data = this->sorter_names[this->bridges->SortType()];
    00129
    00130     /* Set the modified widgets dirty */
    00131     this->SetWidgetDirty(WID_BBS_DROPDOWN_CRITERIA);
    00132     this->SetWidgetDirty(WID_BBS_BRIDGE_LIST);
    00133   }
    00134
    00135 public:
    00136   BuildBridgeWindow(WindowDesc *desc, TileIndex start, TileIndex end, uint32 br_type, GUIBridgeList *bl) : Window(desc),
    00137     start_tile(start),
    00138     end_tile(end),
    00139     type(br_type),
    00140     bridges(bl)
    00141   {
    00142     this->CreateNestedTree();
    00143     this->vscroll = this->GetScrollbar(WID_BBS_SCROLLBAR);
    00144     /* Change the data, or the caption of the gui. Set it to road or rail, accordingly. */
    00145     this->GetWidget<NWidgetCore>(WID_BBS_CAPTION)->widget_data = (GB(this->type, 15, 2) == TRANSPORT_ROAD) ? STR_SELECT_ROAD_BRIDGE_CAPTION : STR_SELECT_RAIL_BRIDGE_CAPTION;
    00146     this->FinishInitNested(GB(br_type, 15, 2)); // Initializes 'this->bridgetext_offset'.
    00147
    00148     this->parent = FindWindowById(WC_BUILD_TOOLBAR, GB(this->type, 15, 2));
    00149     this->bridges->SetListing(this->last_sorting);
    00150     this->bridges->SetSortFuncs(this->sorter_funcs);
    00151     this->bridges->NeedResort();
    00152     this->SortBridgeList();
    00153
    00154     this->vscroll->SetCount(bl->Length());
    00155   }
    00156
    00157   ~BuildBridgeWindow()
    00158   {
    00159     this->last_sorting = this->bridges->GetListing();
    00160
    00161     delete bridges;
    00162   }
    00163
    00164   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00165   {
    00166     switch (widget) {
    00167       case WID_BBS_DROPDOWN_ORDER: {
    00168         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
    00169         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the string is centred and it also looks better.
    00170         d.height += padding.height;
    00171         *size = maxdim(*size, d);
    00172         break;
    00173       }
    00174       case WID_BBS_DROPDOWN_CRITERIA: {
    00175         Dimension d = {0, 0};
    00176         for (const StringID *str = this->sorter_names; *str != INVALID_STRING_ID; str++) {
    00177           d = maxdim(d, GetStringBoundingBox(*str));
    00178         }
    00179         d.width += padding.width;
    00180         d.height += padding.height;
    00181         *size = maxdim(*size, d);
    00182         break;
    00183       }
    00184       case WID_BBS_BRIDGE_LIST: {
    00185         Dimension sprite_dim = {0, 0}; // Biggest bridge sprite dimension
    00186         Dimension text_dim   = {0, 0}; // Biggest text dimension
    00187         for (int i = 0; i < (int)this->bridges->Length(); i++) {
    00188           const BridgeSpec *b = this->bridges->Get(i)->spec;
    00189           sprite_dim = maxdim(sprite_dim, GetSpriteSize(b->sprite));
    00190
    00191           SetDParam(2, this->bridges->Get(i)->cost);
    00192           SetDParam(1, b->speed);
    00193           SetDParam(0, b->material);
    00194           text_dim = maxdim(text_dim, GetStringBoundingBox(_game_mode == GM_EDITOR ? STR_SELECT_BRIDGE_SCENEDIT_INFO : STR_SELECT_BRIDGE_INFO));
    00195         }
    00196         sprite_dim.height++; // Sprite is rendered one pixel down in the matrix field.
    00197         text_dim.height++; // Allowing the bottom row pixels to be rendered on the edge of the matrix field.
    00198         resize->height = max(sprite_dim.height, text_dim.height) + 2; // Max of both sizes + account for matrix edges.
    00199
    00200         this->bridgetext_offset = WD_MATRIX_LEFT + sprite_dim.width + 1; // Left edge of text, 1 pixel distance from the sprite.
    00201         size->width = this->bridgetext_offset + text_dim.width + WD_MATRIX_RIGHT;
    00202         size->height = 4 * resize->height; // Smallest bridge gui is 4 entries high in the matrix.
    00203         break;
    00204       }
    00205     }
    00206   }
    00207
    00208   virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
    00209   {
    00210     /* Position the window so hopefully the first bridge from the list is under the mouse pointer. */
    00211     NWidgetBase *list = this->GetWidget<NWidgetBase>(WID_BBS_BRIDGE_LIST);
    00212     Point corner; // point of the top left corner of the window.
    00213     corner.y = Clamp(_cursor.pos.y - list->pos_y - 5, GetMainViewTop(), GetMainViewBottom() - sm_height);
    00214     corner.x = Clamp(_cursor.pos.x - list->pos_x - 5, 0, _screen.width - sm_width);
    00215     return corner;
    00216   }
    00217
    00218   virtual void DrawWidget(const Rect &r, int widget) const
    00219   {
    00220     switch (widget) {
    00221       case WID_BBS_DROPDOWN_ORDER:
    00222         this->DrawSortButtonState(widget, this->bridges->IsDescSortOrder() ? SBS_DOWN : SBS_UP);
    00223         break;
    00224
    00225       case WID_BBS_BRIDGE_LIST: {
    00226         uint y = r.top;
    00227         for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < (int)this->bridges->Length(); i++) {
    00228           const BridgeSpec *b = this->bridges->Get(i)->spec;
    00229
    00230           SetDParam(2, this->bridges->Get(i)->cost);
    00231           SetDParam(1, b->speed);
    00232           SetDParam(0, b->material);
    00233
    00234           DrawSprite(b->sprite, b->pal, r.left + WD_MATRIX_LEFT, y + this->resize.step_height - 1 - GetSpriteSize(b->sprite).height);
    00235           DrawStringMultiLine(r.left + this->bridgetext_offset, r.right, y + 2, y + this->resize.step_height,
    00236               _game_mode == GM_EDITOR ? STR_SELECT_BRIDGE_SCENEDIT_INFO : STR_SELECT_BRIDGE_INFO);
    00237           y += this->resize.step_height;
    00238         }
    00239         break;
    00240       }
    00241     }
    00242   }
    00243
    00244   virtual EventState OnKeyPress(WChar key, uint16 keycode)
    00245   {
    00246     const uint8 i = keycode - '1';
    00247     if (i < 9 && i < this->bridges->Length()) {
    00248       /* Build the requested bridge */
    00249       this->BuildBridge(i);
    00250       delete this;
    00251       return ES_HANDLED;
    00252     }
    00253     return ES_NOT_HANDLED;
    00254   }
    00255
    00256   virtual void OnClick(Point pt, int widget, int click_count)
    00257   {
    00258     switch (widget) {
    00259       default: break;
    00260       case WID_BBS_BRIDGE_LIST: {
    00261         uint i = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_BBS_BRIDGE_LIST);
    00262         if (i < this->bridges->Length()) {
    00263           this->BuildBridge(i);
    00264           delete this;
    00265         }
    00266         break;
    00267       }
    00268
    00269       case WID_BBS_DROPDOWN_ORDER:
    00270         this->bridges->ToggleSortOrder();
    00271         this->SetDirty();
    00272         break;
    00273
    00274       case WID_BBS_DROPDOWN_CRITERIA:
    00275         ShowDropDownMenu(this, this->sorter_names, this->bridges->SortType(), WID_BBS_DROPDOWN_CRITERIA, 0, 0);
    00276         break;
    00277     }
    00278   }
    00279
    00280   virtual void OnDropdownSelect(int widget, int index)
    00281   {
    00282     if (widget == WID_BBS_DROPDOWN_CRITERIA && this->bridges->SortType() != index) {
    00283       this->bridges->SetSortType(index);
    00284
    00285       this->SortBridgeList();
    00286     }
    00287   }
    00288
    00289   virtual void OnResize()
    00290   {
    00291     this->vscroll->SetCapacityFromWidget(this, WID_BBS_BRIDGE_LIST);
    00292   }
    00293 };
    00294
    00296 Listing BuildBridgeWindow::last_sorting = {true, 2};
    00297
    00299 GUIBridgeList::SortFunction * const BuildBridgeWindow::sorter_funcs[] = {
    00300   &BridgeIndexSorter,
    00301   &BridgePriceSorter,
    00302   &BridgeSpeedSorter
    00303 };
    00304
    00306 const StringID BuildBridgeWindow::sorter_names[] = {
    00307   STR_SORT_BY_NUMBER,
    00308   STR_SORT_BY_COST,
    00309   STR_SORT_BY_MAX_SPEED,
    00310   INVALID_STRING_ID
    00311 };
    00312
    00314 static const NWidgetPart _nested_build_bridge_widgets[] = {
    00315   /* Header */
    00316   NWidget(NWID_HORIZONTAL),
    00317     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00318     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_BBS_CAPTION), SetDataTip(STR_SELECT_RAIL_BRIDGE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00319     NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
    00320   EndContainer(),
    00321
    00322   NWidget(NWID_HORIZONTAL),
    00323     NWidget(NWID_VERTICAL),
    00324       /* Sort order + criteria buttons */
    00325       NWidget(NWID_HORIZONTAL),
    00326         NWidget(WWT_TEXTBTN, COLOUR_DARK_GREEN, WID_BBS_DROPDOWN_ORDER), SetFill(1, 0), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
    00327         NWidget(WWT_DROPDOWN, COLOUR_DARK_GREEN, WID_BBS_DROPDOWN_CRITERIA), SetFill(1, 0), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
    00328       EndContainer(),
    00329       /* Matrix. */
    00330       NWidget(WWT_MATRIX, COLOUR_DARK_GREEN, WID_BBS_BRIDGE_LIST), SetFill(1, 0), SetResize(0, 22), SetMatrixDataTip(1, 0, STR_SELECT_BRIDGE_SELECTION_TOOLTIP), SetScrollbar(WID_BBS_SCROLLBAR),
    00331     EndContainer(),
    00332
    00333     /* scrollbar + resize button */
    00334     NWidget(NWID_VERTICAL),
    00335       NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BBS_SCROLLBAR),
    00336       NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
    00337     EndContainer(),
    00338   EndContainer(),
    00339 };
    00340
    00342 static WindowDesc _build_bridge_desc(
    00343   WDP_AUTO, "build_bridge", 200, 114,
    00344   WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
    00345   WDF_CONSTRUCTION,
    00346   _nested_build_bridge_widgets, lengthof(_nested_build_bridge_widgets)
    00347 );
    00348
    00359 void ShowBuildBridgeWindow(TileIndex start, TileIndex end, TransportType transport_type, byte road_rail_type)
    00360 {
    00361   DeleteWindowByClass(WC_BUILD_BRIDGE);
    00362
    00363   /* Data type for the bridge.
    00364    * Bit 16,15 = transport type,
    00365    *     14..8 = road/rail types,
    00366    *      7..0 = type of bridge */
    00367   uint32 type = (transport_type << 15) | (road_rail_type << 8);
    00368
    00369   /* The bridge length without ramps. */
    00370   const uint bridge_len = GetTunnelBridgeLength(start, end);
    00371
    00372   /* If Ctrl is being pressed, check whether the last bridge built is available
    00373    * If so, return this bridge type. Otherwise continue normally.
    00374    * We store bridge types for each transport type, so we have to check for
    00375    * the transport type beforehand.
    00376    */
    00377   BridgeType last_bridge_type = 0;
    00378   switch (transport_type) {
    00379     case TRANSPORT_ROAD: last_bridge_type = _last_roadbridge_type; break;
    00380     case TRANSPORT_RAIL: last_bridge_type = _last_railbridge_type; break;
    00381     default: break; // water ways and air routes don't have bridge types
    00382   }
    00383   if (_ctrl_pressed && CheckBridgeAvailability(last_bridge_type, bridge_len).Succeeded()) {
    00384     DoCommandP(end, start, type | last_bridge_type, CMD_BUILD_BRIDGE | CMD_MSG(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE), CcBuildBridge);
    00385     return;
    00386   }
    00387
    00388   /* only query bridge building possibility once, result is the same for all bridges!
    00389    * returns CMD_ERROR on failure, and price on success */
    00390   StringID errmsg = INVALID_STRING_ID;
    00391   CommandCost ret = DoCommand(end, start, type, CommandFlagsToDCFlags(GetCommandFlags(CMD_BUILD_BRIDGE)) | DC_QUERY_COST, CMD_BUILD_BRIDGE);
    00392
    00393   GUIBridgeList *bl = NULL;
    00394   if (ret.Failed()) {
    00395     errmsg = ret.GetErrorMessage();
    00396   } else {
    00397     /* check which bridges can be built */
    00398     const uint tot_bridgedata_len = CalcBridgeLenCostFactor(bridge_len + 2);
    00399
    00400     bl = new GUIBridgeList();
    00401
    00402     Money infra_cost = 0;
    00403     switch (transport_type) {
    00404       case TRANSPORT_ROAD:
    00405         infra_cost = (bridge_len + 2) * _price[PR_BUILD_ROAD] * 2;
    00406         /* In case we add a new road type as well, we must be aware of those costs. */
    00407         if (IsBridgeTile(start)) infra_cost *= CountBits(GetRoadTypes(start) | (RoadTypes)road_rail_type);
    00408         break;
    00409       case TRANSPORT_RAIL: infra_cost = (bridge_len + 2) * RailBuildCost((RailType)road_rail_type); break;
    00410       default: break;
    00411     }
    00412
    00413     /* loop for all bridgetypes */
    00414     for (BridgeType brd_type = 0; brd_type != MAX_BRIDGES; brd_type++) {
    00415       if (CheckBridgeAvailability(brd_type, bridge_len).Succeeded()) {
    00416         /* bridge is accepted, add to list */
    00417         BuildBridgeData *item = bl->Append();
    00418         item->index = brd_type;
    00419         item->spec = GetBridgeSpec(brd_type);
    00420         /* Add to terraforming & bulldozing costs the cost of the
    00421          * bridge itself (not computed with DC_QUERY_COST) */
    00422         item->cost = ret.GetCost() + (((int64)tot_bridgedata_len * _price[PR_BUILD_BRIDGE] * item->spec->price) >> 8) + infra_cost;
    00423       }
    00424     }
    00425   }
    00426
    00427   if (bl != NULL && bl->Length() != 0) {
    00428     new BuildBridgeWindow(&_build_bridge_desc, start, end, type, bl);
    00429   } else {
    00430     delete bl;
    00431     ShowErrorMessage(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE, errmsg, WL_INFO, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE);
    00432   }
    00433 }