bridge_gui.cpp

Go to the documentation of this file.
00001 /* $Id: bridge_gui.cpp 21673 2010-12-31 10:15:35Z smatz $ */
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 "bridge.h"
00016 #include "rail.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 #include "openttd.h"
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 
00059 enum BuildBridgeSelectionWidgets {
00060   BBSW_CAPTION,
00061   BBSW_DROPDOWN_ORDER,
00062   BBSW_DROPDOWN_CRITERIA,
00063   BBSW_BRIDGE_LIST,
00064   BBSW_SCROLLBAR,
00065 };
00066 
00068 class BuildBridgeWindow : public Window {
00069 private:
00070   /* Runtime saved values */
00071   static uint16 last_size;     
00072   static Listing last_sorting; 
00073 
00074   /* Constants for sorting the bridges */
00075   static const StringID sorter_names[];
00076   static GUIBridgeList::SortFunction * const sorter_funcs[];
00077 
00078   /* Internal variables */
00079   TileIndex start_tile;
00080   TileIndex end_tile;
00081   uint32 type;
00082   GUIBridgeList *bridges;
00083   int bridgetext_offset; 
00084   Scrollbar *vscroll;
00085 
00087   static int CDECL BridgeIndexSorter(const BuildBridgeData *a, const BuildBridgeData *b)
00088   {
00089     return a->index - b->index;
00090   }
00091 
00093   static int CDECL BridgePriceSorter(const BuildBridgeData *a, const BuildBridgeData *b)
00094   {
00095     return a->cost - b->cost;
00096   }
00097 
00099   static int CDECL BridgeSpeedSorter(const BuildBridgeData *a, const BuildBridgeData *b)
00100   {
00101     return a->spec->speed - b->spec->speed;
00102   }
00103 
00104   void BuildBridge(uint8 i)
00105   {
00106     switch ((TransportType)(this->type >> 15)) {
00107       case TRANSPORT_RAIL: _last_railbridge_type = this->bridges->Get(i)->index; break;
00108       case TRANSPORT_ROAD: _last_roadbridge_type = this->bridges->Get(i)->index; break;
00109       default: break;
00110     }
00111     DoCommandP(this->end_tile, this->start_tile, this->type | this->bridges->Get(i)->index,
00112           CMD_BUILD_BRIDGE | CMD_MSG(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE), CcBuildBridge);
00113   }
00114 
00116   void SortBridgeList()
00117   {
00118     this->bridges->Sort();
00119 
00120     /* Display the current sort variant */
00121     this->GetWidget<NWidgetCore>(BBSW_DROPDOWN_CRITERIA)->widget_data = this->sorter_names[this->bridges->SortType()];
00122 
00123     /* Set the modified widgets dirty */
00124     this->SetWidgetDirty(BBSW_DROPDOWN_CRITERIA);
00125     this->SetWidgetDirty(BBSW_BRIDGE_LIST);
00126   }
00127 
00128 public:
00129   BuildBridgeWindow(const WindowDesc *desc, TileIndex start, TileIndex end, uint32 br_type, GUIBridgeList *bl) : Window(),
00130     start_tile(start),
00131     end_tile(end),
00132     type(br_type),
00133     bridges(bl)
00134   {
00135     this->CreateNestedTree(desc);
00136     this->vscroll = this->GetScrollbar(BBSW_SCROLLBAR);
00137     /* Change the data, or the caption of the gui. Set it to road or rail, accordingly. */
00138     this->GetWidget<NWidgetCore>(BBSW_CAPTION)->widget_data = (GB(this->type, 15, 2) == TRANSPORT_ROAD) ? STR_SELECT_ROAD_BRIDGE_CAPTION : STR_SELECT_RAIL_BRIDGE_CAPTION;
00139     this->FinishInitNested(desc, GB(br_type, 15, 2)); // Initializes 'this->bridgetext_offset'.
00140 
00141     this->parent = FindWindowById(WC_BUILD_TOOLBAR, GB(this->type, 15, 2));
00142     this->bridges->SetListing(this->last_sorting);
00143     this->bridges->SetSortFuncs(this->sorter_funcs);
00144     this->bridges->NeedResort();
00145     this->SortBridgeList();
00146 
00147     this->vscroll->SetCount(bl->Length());
00148     if (this->last_size < this->vscroll->GetCapacity()) this->last_size = this->vscroll->GetCapacity();
00149     if (this->last_size > this->vscroll->GetCount()) this->last_size = this->vscroll->GetCount();
00150     /* Resize the bridge selection window if we used a bigger one the last time. */
00151     if (this->last_size > this->vscroll->GetCapacity()) {
00152       ResizeWindow(this, 0, (this->last_size - this->vscroll->GetCapacity()) * this->resize.step_height);
00153     }
00154     this->GetWidget<NWidgetCore>(BBSW_BRIDGE_LIST)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
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 BBSW_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 BBSW_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 BBSW_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(const WindowDesc *desc, 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>(BBSW_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 BBSW_DROPDOWN_ORDER:
00222         this->DrawSortButtonState(widget, this->bridges->IsDescSortOrder() ? SBS_DOWN : SBS_UP);
00223         break;
00224 
00225       case BBSW_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(uint16 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 BBSW_BRIDGE_LIST: {
00261         uint i = this->vscroll->GetScrolledRowFromWidget(pt.y, this, BBSW_BRIDGE_LIST);
00262         if (i < this->bridges->Length()) {
00263           this->BuildBridge(i);
00264           delete this;
00265         }
00266         break;
00267       }
00268 
00269       case BBSW_DROPDOWN_ORDER:
00270         this->bridges->ToggleSortOrder();
00271         this->SetDirty();
00272         break;
00273 
00274       case BBSW_DROPDOWN_CRITERIA:
00275         ShowDropDownMenu(this, this->sorter_names, this->bridges->SortType(), BBSW_DROPDOWN_CRITERIA, 0, 0);
00276         break;
00277     }
00278   }
00279 
00280   virtual void OnDropdownSelect(int widget, int index)
00281   {
00282     if (widget == BBSW_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, BBSW_BRIDGE_LIST);
00292     this->GetWidget<NWidgetCore>(BBSW_BRIDGE_LIST)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00293 
00294     this->last_size = max(this->vscroll->GetCapacity(), this->last_size);
00295   }
00296 };
00297 
00299 uint16 BuildBridgeWindow::last_size = 4;
00301 Listing BuildBridgeWindow::last_sorting = {true, 2};
00302 
00304 GUIBridgeList::SortFunction * const BuildBridgeWindow::sorter_funcs[] = {
00305   &BridgeIndexSorter,
00306   &BridgePriceSorter,
00307   &BridgeSpeedSorter
00308 };
00309 
00311 const StringID BuildBridgeWindow::sorter_names[] = {
00312   STR_SORT_BY_NUMBER,
00313   STR_SORT_BY_COST,
00314   STR_SORT_BY_MAX_SPEED,
00315   INVALID_STRING_ID
00316 };
00317 
00319 static const NWidgetPart _nested_build_bridge_widgets[] = {
00320   /* Header */
00321   NWidget(NWID_HORIZONTAL),
00322     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00323     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, BBSW_CAPTION), SetDataTip(STR_SELECT_RAIL_BRIDGE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00324   EndContainer(),
00325 
00326   NWidget(NWID_HORIZONTAL),
00327     NWidget(NWID_VERTICAL),
00328       /* Sort order + criteria buttons */
00329       NWidget(NWID_HORIZONTAL),
00330         NWidget(WWT_TEXTBTN, COLOUR_DARK_GREEN, BBSW_DROPDOWN_ORDER), SetFill(1, 0), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
00331         NWidget(WWT_DROPDOWN, COLOUR_DARK_GREEN, BBSW_DROPDOWN_CRITERIA), SetFill(1, 0), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
00332       EndContainer(),
00333       /* Matrix. */
00334       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),
00335     EndContainer(),
00336 
00337     /* scrollbar + resize button */
00338     NWidget(NWID_VERTICAL),
00339       NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, BBSW_SCROLLBAR),
00340       NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
00341     EndContainer(),
00342   EndContainer(),
00343 };
00344 
00346 static const WindowDesc _build_bridge_desc(
00347   WDP_AUTO, 200, 114,
00348   WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
00349   WDF_CONSTRUCTION,
00350   _nested_build_bridge_widgets, lengthof(_nested_build_bridge_widgets)
00351 );
00352 
00363 void ShowBuildBridgeWindow(TileIndex start, TileIndex end, TransportType transport_type, byte road_rail_type)
00364 {
00365   DeleteWindowByClass(WC_BUILD_BRIDGE);
00366 
00367   /* Data type for the bridge.
00368    * Bit 16,15 = transport type,
00369    *     14..8 = road/rail types,
00370    *      7..0 = type of bridge */
00371   uint32 type = (transport_type << 15) | (road_rail_type << 8);
00372 
00373   /* The bridge length without ramps. */
00374   const uint bridge_len = GetTunnelBridgeLength(start, end);
00375 
00376   /* If Ctrl is being pressed, check wether the last bridge built is available
00377    * If so, return this bridge type. Otherwise continue normally.
00378    * We store bridge types for each transport type, so we have to check for
00379    * the transport type beforehand.
00380    */
00381   BridgeType last_bridge_type = 0;
00382   switch (transport_type) {
00383     case TRANSPORT_ROAD: last_bridge_type = _last_roadbridge_type; break;
00384     case TRANSPORT_RAIL: last_bridge_type = _last_railbridge_type; break;
00385     default: break; // water ways and air routes don't have bridge types
00386   }
00387   if (_ctrl_pressed && CheckBridgeAvailability(last_bridge_type, bridge_len).Succeeded()) {
00388     DoCommandP(end, start, type | last_bridge_type, CMD_BUILD_BRIDGE | CMD_MSG(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE), CcBuildBridge);
00389     return;
00390   }
00391 
00392   /* only query bridge building possibility once, result is the same for all bridges!
00393    * returns CMD_ERROR on failure, and price on success */
00394   StringID errmsg = INVALID_STRING_ID;
00395   CommandCost ret = DoCommand(end, start, type, CommandFlagsToDCFlags(GetCommandFlags(CMD_BUILD_BRIDGE)) | DC_QUERY_COST, CMD_BUILD_BRIDGE);
00396 
00397   GUIBridgeList *bl = NULL;
00398   if (ret.Failed()) {
00399     errmsg = ret.GetErrorMessage();
00400   } else {
00401     /* check which bridges can be built */
00402     const uint tot_bridgedata_len = CalcBridgeLenCostFactor(bridge_len + 2);
00403 
00404     bl = new GUIBridgeList();
00405 
00406     Money infra_cost = 0;
00407     switch (transport_type) {
00408       case TRANSPORT_ROAD: infra_cost = (bridge_len + 2) * _price[PR_BUILD_ROAD] * 2; 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 }

Generated on Fri Dec 31 17:15:29 2010 for OpenTTD by  doxygen 1.6.1