bridge_gui.cpp

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

Generated on Thu Dec 23 23:41:23 2010 for OpenTTD by  doxygen 1.6.1