# bridge_gui.cpp

Go to the documentation of this file.

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