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