# bridge_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: bridge_gui.cpp 19928 2010-06-04 21:14:22Z rubidium $ */
    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 "economy_func.h"
    00016 #include "bridge.h"
    00017 #include "rail.h"
    00018 #include "strings_func.h"
    00019 #include "window_func.h"
    00020 #include "sound_func.h"
    00021 #include "gfx_func.h"
    00022 #include "tunnelbridge.h"
    00023 #include "sortlist_type.h"
    00024 #include "widgets/dropdown_func.h"
    00025 #include "core/geometry_func.hpp"
    00026
    00027 #include "table/strings.h"
    00028
    00030 static BridgeType _last_railbridge_type = 0;
    00032 static BridgeType _last_roadbridge_type = 0;
    00033
    00037 struct BuildBridgeData {
    00038   BridgeType index;
    00039   const BridgeSpec *spec;
    00040   Money cost;
    00041 };
    00042
    00043 typedef GUIList<BuildBridgeData> GUIBridgeList;
    00044
    00053 void CcBuildBridge(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
    00054 {
    00055   if (result.Succeeded()) SndPlayTileFx(SND_27_BLACKSMITH_ANVIL, tile);
    00056 }
    00057
    00058 /* Names of the build bridge selection window */
    00059 enum BuildBridgeSelectionWidgets {
    00060   BBSW_CAPTION,
    00061   BBSW_DROPDOWN_ORDER,
    00062   BBSW_DROPDOWN_CRITERIA,
    00063   BBSW_BRIDGE_LIST,
    00064   BBSW_SCROLLBAR,
    00065 };
    00066
    00067 class BuildBridgeWindow : public Window {
    00068 private:
    00069   /* Runtime saved values */
    00070   static uint16 last_size;
    00071   static Listing last_sorting;
    00072
    00073   /* Constants for sorting the bridges */
    00074   static const StringID sorter_names[];
    00075   static GUIBridgeList::SortFunction * const sorter_funcs[];
    00076
    00077   /* Internal variables */
    00078   TileIndex start_tile;
    00079   TileIndex end_tile;
    00080   uint32 type;
    00081   GUIBridgeList *bridges;
    00082   int bridgetext_offset;
    00083
    00085   static int CDECL BridgeIndexSorter(const BuildBridgeData *a, const BuildBridgeData *b)
    00086   {
    00087     return a->index - b->index;
    00088   }
    00089
    00091   static int CDECL BridgePriceSorter(const BuildBridgeData *a, const BuildBridgeData *b)
    00092   {
    00093     return a->cost - b->cost;
    00094   }
    00095
    00097   static int CDECL BridgeSpeedSorter(const BuildBridgeData *a, const BuildBridgeData *b)
    00098   {
    00099     return a->spec->speed - b->spec->speed;
    00100   }
    00101
    00102   void BuildBridge(uint8 i)
    00103   {
    00104     switch ((TransportType)(this->type >> 15)) {
    00105       case TRANSPORT_RAIL: _last_railbridge_type = this->bridges->Get(i)->index; break;
    00106       case TRANSPORT_ROAD: _last_roadbridge_type = this->bridges->Get(i)->index; break;
    00107       default: break;
    00108     }
    00109     DoCommandP(this->end_tile, this->start_tile, this->type | this->bridges->Get(i)->index,
    00110           CMD_BUILD_BRIDGE | CMD_MSG(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE), CcBuildBridge);
    00111   }
    00112
    00114   void SortBridgeList()
    00115   {
    00116     this->bridges->Sort();
    00117
    00118     /* Display the current sort variant */
    00119     this->GetWidget<NWidgetCore>(BBSW_DROPDOWN_CRITERIA)->widget_data = this->sorter_names[this->bridges->SortType()];
    00120
    00121     /* Set the modified widgets dirty */
    00122     this->SetWidgetDirty(BBSW_DROPDOWN_CRITERIA);
    00123     this->SetWidgetDirty(BBSW_BRIDGE_LIST);
    00124   }
    00125
    00126 public:
    00127   BuildBridgeWindow(const WindowDesc *desc, TileIndex start, TileIndex end, uint32 br_type, GUIBridgeList *bl) : Window(),
    00128     start_tile(start),
    00129     end_tile(end),
    00130     type(br_type),
    00131     bridges(bl)
    00132   {
    00133     this->CreateNestedTree(desc);
    00134     /* Change the data, or the caption of the gui. Set it to road or rail, accordingly. */
    00135     this->GetWidget<NWidgetCore>(BBSW_CAPTION)->widget_data = (GB(this->type, 15, 2) == TRANSPORT_ROAD) ? STR_SELECT_ROAD_BRIDGE_CAPTION : STR_SELECT_RAIL_BRIDGE_CAPTION;
    00136     this->FinishInitNested(desc, GB(br_type, 15, 2)); // Initializes 'this->bridgetext_offset'.
    00137
    00138     this->parent = FindWindowById(WC_BUILD_TOOLBAR, GB(this->type, 15, 2));
    00139     this->bridges->SetListing(this->last_sorting);
    00140     this->bridges->SetSortFuncs(this->sorter_funcs);
    00141     this->bridges->NeedResort();
    00142     this->SortBridgeList();
    00143
    00144     this->vscroll.SetCount(bl->Length());
    00145     if (this->last_size < this->vscroll.GetCapacity()) this->last_size = this->vscroll.GetCapacity();
    00146     if (this->last_size > this->vscroll.GetCount()) this->last_size = this->vscroll.GetCount();
    00147     /* Resize the bridge selection window if we used a bigger one the last time. */
    00148     if (this->last_size > this->vscroll.GetCapacity()) {
    00149       ResizeWindow(this, 0, (this->last_size - this->vscroll.GetCapacity()) * this->resize.step_height);
    00150     }
    00151     this->GetWidget<NWidgetCore>(BBSW_BRIDGE_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00152   }
    00153
    00154   ~BuildBridgeWindow()
    00155   {
    00156     this->last_sorting = this->bridges->GetListing();
    00157
    00158     delete bridges;
    00159   }
    00160
    00161   virtual void OnPaint()
    00162   {
    00163     this->DrawWidgets();
    00164   }
    00165
    00166   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00167   {
    00168     switch (widget) {
    00169       case BBSW_DROPDOWN_ORDER: {
    00170         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
    00171         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the word is centered, also looks nice.
    00172         d.height += padding.height;
    00173         *size = maxdim(*size, d);
    00174         break;
    00175       }
    00176       case BBSW_DROPDOWN_CRITERIA: {
    00177         Dimension d = {0, 0};
    00178         for (const StringID *str = this->sorter_names; *str != INVALID_STRING_ID; str++) {
    00179           d = maxdim(d, GetStringBoundingBox(*str));
    00180         }
    00181         d.width += padding.width;
    00182         d.height += padding.height;
    00183         *size = maxdim(*size, d);
    00184         break;
    00185       }
    00186       case BBSW_BRIDGE_LIST: {
    00187         Dimension sprite_dim = {0, 0}; // Biggest bridge sprite dimension
    00188         Dimension text_dim   = {0, 0}; // Biggest text dimension
    00189         for (int i = 0; i < (int)this->bridges->Length(); i++) {
    00190           const BridgeSpec *b = this->bridges->Get(i)->spec;
    00191           sprite_dim = maxdim(sprite_dim, GetSpriteSize(b->sprite));
    00192
    00193           SetDParam(2, this->bridges->Get(i)->cost);
    00194           SetDParam(1, b->speed);
    00195           SetDParam(0, b->material);
    00196           text_dim = maxdim(text_dim, GetStringBoundingBox(STR_SELECT_BRIDGE_INFO));
    00197         }
    00198         sprite_dim.height++; // Sprite is rendered one pixel down in the matrix field.
    00199         text_dim.height++; // Allowing the bottom row pixels to be rendered on the edge of the matrix field.
    00200         resize->height = max(sprite_dim.height, text_dim.height) + 2; // Max of both sizes + account for matrix edges.
    00201
    00202         this->bridgetext_offset = WD_MATRIX_LEFT + sprite_dim.width + 1; // Left edge of text, 1 pixel distance from the sprite.
    00203         size->width = this->bridgetext_offset + text_dim.width + WD_MATRIX_RIGHT;
    00204         size->height = 4 * resize->height; // Smallest bridge gui is 4 entries high in the matrix.
    00205         break;
    00206       }
    00207     }
    00208   }
    00209
    00210   virtual void DrawWidget(const Rect &r, int widget) const
    00211   {
    00212     switch (widget) {
    00213       case BBSW_DROPDOWN_ORDER:
    00214         this->DrawSortButtonState(widget, this->bridges->IsDescSortOrder() ? SBS_DOWN : SBS_UP);
    00215         break;
    00216
    00217       case BBSW_BRIDGE_LIST: {
    00218         uint y = r.top;
    00219         for (int i = this->vscroll.GetPosition(); this->vscroll.IsVisible(i) && i < (int)this->bridges->Length(); i++) {
    00220           const BridgeSpec *b = this->bridges->Get(i)->spec;
    00221
    00222           SetDParam(2, this->bridges->Get(i)->cost);
    00223           SetDParam(1, b->speed);
    00224           SetDParam(0, b->material);
    00225
    00226           DrawSprite(b->sprite, b->pal, r.left + WD_MATRIX_LEFT, y + this->resize.step_height - 1 - GetSpriteSize(b->sprite).height);
    00227           DrawStringMultiLine(r.left + this->bridgetext_offset, r.right, y + 2, y + this->resize.step_height, STR_SELECT_BRIDGE_INFO);
    00228           y += this->resize.step_height;
    00229         }
    00230         break;
    00231       }
    00232     }
    00233   }
    00234
    00235   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00236   {
    00237     const uint8 i = keycode - '1';
    00238     if (i < 9 && i < this->bridges->Length()) {
    00239       /* Build the requested bridge */
    00240       this->BuildBridge(i);
    00241       delete this;
    00242       return ES_HANDLED;
    00243     }
    00244     return ES_NOT_HANDLED;
    00245   }
    00246
    00247   virtual void OnClick(Point pt, int widget, int click_count)
    00248   {
    00249     switch (widget) {
    00250       default: break;
    00251       case BBSW_BRIDGE_LIST: {
    00252         uint i = ((int)pt.y - this->GetWidget<NWidgetBase>(BBSW_BRIDGE_LIST)->pos_y) / this->resize.step_height;
    00253         if (i < this->vscroll.GetCapacity()) {
    00254           i += this->vscroll.GetPosition();
    00255           if (i < this->bridges->Length()) {
    00256             this->BuildBridge(i);
    00257             delete this;
    00258           }
    00259         }
    00260       } break;
    00261
    00262       case BBSW_DROPDOWN_ORDER:
    00263         this->bridges->ToggleSortOrder();
    00264         this->SetDirty();
    00265         break;
    00266
    00267       case BBSW_DROPDOWN_CRITERIA:
    00268         ShowDropDownMenu(this, this->sorter_names, this->bridges->SortType(), BBSW_DROPDOWN_CRITERIA, 0, 0);
    00269         break;
    00270     }
    00271   }
    00272
    00273   virtual void OnDropdownSelect(int widget, int index)
    00274   {
    00275     if (widget == BBSW_DROPDOWN_CRITERIA && this->bridges->SortType() != index) {
    00276       this->bridges->SetSortType(index);
    00277
    00278       this->SortBridgeList();
    00279     }
    00280   }
    00281
    00282   virtual void OnResize()
    00283   {
    00284     this->vscroll.SetCapacityFromWidget(this, BBSW_BRIDGE_LIST);
    00285     this->GetWidget<NWidgetCore>(BBSW_BRIDGE_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00286
    00287     this->last_size = max(this->vscroll.GetCapacity(), this->last_size);
    00288   }
    00289 };
    00290
    00291 /* Set the default size of the Build Bridge Window */
    00292 uint16 BuildBridgeWindow::last_size = 4;
    00293 /* Set the default sorting for the bridges */
    00294 Listing BuildBridgeWindow::last_sorting = {false, 0};
    00295
    00296 /* Availible bridge sorting functions */
    00297 GUIBridgeList::SortFunction * const BuildBridgeWindow::sorter_funcs[] = {
    00298   &BridgeIndexSorter,
    00299   &BridgePriceSorter,
    00300   &BridgeSpeedSorter
    00301 };
    00302
    00303 /* Names of the sorting functions */
    00304 const StringID BuildBridgeWindow::sorter_names[] = {
    00305   STR_SORT_BY_NUMBER,
    00306   STR_SORT_BY_COST,
    00307   STR_SORT_BY_MAX_SPEED,
    00308   INVALID_STRING_ID
    00309 };
    00310
    00311 static const NWidgetPart _nested_build_bridge_widgets[] = {
    00312   /* Header */
    00313   NWidget(NWID_HORIZONTAL),
    00314     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00315     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, BBSW_CAPTION), SetDataTip(STR_SELECT_RAIL_BRIDGE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00316   EndContainer(),
    00317
    00318   NWidget(NWID_HORIZONTAL),
    00319     NWidget(NWID_VERTICAL),
    00320       /* Sort order + criteria buttons */
    00321       NWidget(NWID_HORIZONTAL),
    00322         NWidget(WWT_TEXTBTN, COLOUR_DARK_GREEN, BBSW_DROPDOWN_ORDER), SetFill(1, 0), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
    00323         NWidget(WWT_DROPDOWN, COLOUR_DARK_GREEN, BBSW_DROPDOWN_CRITERIA), SetFill(1, 0), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
    00324       EndContainer(),
    00325       /* Matrix. */
    00326       NWidget(WWT_MATRIX, COLOUR_DARK_GREEN, BBSW_BRIDGE_LIST), SetFill(1, 0), SetResize(0, 22), SetDataTip(0x401, STR_SELECT_BRIDGE_SELECTION_TOOLTIP),
    00327     EndContainer(),
    00328
    00329     /* scrollbar + resize button */
    00330     NWidget(NWID_VERTICAL),
    00331       NWidget(WWT_SCROLLBAR, COLOUR_DARK_GREEN, BBSW_SCROLLBAR),
    00332       NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
    00333     EndContainer(),
    00334   EndContainer(),
    00335 };
    00336
    00337 /* Window definition for the rail bridge selection window */
    00338 static const WindowDesc _build_bridge_desc(
    00339   WDP_AUTO, 200, 114,
    00340   WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
    00341   WDF_CONSTRUCTION,
    00342   _nested_build_bridge_widgets, lengthof(_nested_build_bridge_widgets)
    00343 );
    00344
    00355 void ShowBuildBridgeWindow(TileIndex start, TileIndex end, TransportType transport_type, byte road_rail_type)
    00356 {
    00357   DeleteWindowByClass(WC_BUILD_BRIDGE);
    00358
    00359   /* Data type for the bridge.
    00360    * Bit 16,15 = transport type,
    00361    *     14..8 = road/rail types,
    00362    *      7..0 = type of bridge */
    00363   uint32 type = (transport_type << 15) | (road_rail_type << 8);
    00364
    00365   /* The bridge length without ramps. */
    00366   const uint bridge_len = GetTunnelBridgeLength(start, end);
    00367
    00368   /* If Ctrl is being pressed, check wether the last bridge built is available
    00369    * If so, return this bridge type. Otherwise continue normally.
    00370    * We store bridge types for each transport type, so we have to check for
    00371    * the transport type beforehand.
    00372    */
    00373   BridgeType last_bridge_type = 0;
    00374   switch (transport_type) {
    00375     case TRANSPORT_ROAD: last_bridge_type = _last_roadbridge_type; break;
    00376     case TRANSPORT_RAIL: last_bridge_type = _last_railbridge_type; break;
    00377     default: break; // water ways and air routes don't have bridge types
    00378   }
    00379   if (_ctrl_pressed && CheckBridgeAvailability(last_bridge_type, bridge_len).Succeeded()) {
    00380     DoCommandP(end, start, type | last_bridge_type, CMD_BUILD_BRIDGE | CMD_MSG(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE), CcBuildBridge);
    00381     return;
    00382   }
    00383
    00384   /* only query bridge building possibility once, result is the same for all bridges!
    00385    * returns CMD_ERROR on failure, and price on success */
    00386   StringID errmsg = INVALID_STRING_ID;
    00387   CommandCost ret = DoCommand(end, start, type, DC_AUTO | DC_QUERY_COST, CMD_BUILD_BRIDGE);
    00388
    00389   GUIBridgeList *bl = NULL;
    00390   if (ret.Failed()) {
    00391     errmsg = _error_message;
    00392   } else {
    00393     /* check which bridges can be built */
    00394     const uint tot_bridgedata_len = CalcBridgeLenCostFactor(bridge_len + 2);
    00395
    00396     bl = new GUIBridgeList();
    00397
    00398     Money infra_cost = 0;
    00399     switch (transport_type) {
    00400       case TRANSPORT_ROAD: infra_cost = (bridge_len + 2) * _price[PR_BUILD_ROAD] * 2; break;
    00401       case TRANSPORT_RAIL: infra_cost = (bridge_len + 2) * RailBuildCost((RailType)road_rail_type); break;
    00402       default: break;
    00403     }
    00404
    00405     /* loop for all bridgetypes */
    00406     for (BridgeType brd_type = 0; brd_type != MAX_BRIDGES; brd_type++) {
    00407       if (CheckBridgeAvailability(brd_type, bridge_len).Succeeded()) {
    00408         /* bridge is accepted, add to list */
    00409         BuildBridgeData *item = bl->Append();
    00410         item->index = brd_type;
    00411         item->spec = GetBridgeSpec(brd_type);
    00412         /* Add to terraforming & bulldozing costs the cost of the
    00413          * bridge itself (not computed with DC_QUERY_COST) */
    00414         item->cost = ret.GetCost() + (((int64)tot_bridgedata_len * _price[PR_BUILD_BRIDGE] * item->spec->price) >> 8) + infra_cost;
    00415       }
    00416     }
    00417   }
    00418
    00419   if (bl != NULL && bl->Length() != 0) {
    00420     new BuildBridgeWindow(&_build_bridge_desc, start, end, type, bl);
    00421   } else {
    00422     delete bl;
    00423     ShowErrorMessage(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE, errmsg, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE);
    00424   }
    00425 }