(svn r27707) -Fix: Make the result of NewGRF's CARGO_NAME string code reliably print...
[openttd.git] / src / bridge_gui.cpp
blob797ead1f516fac9d3b3c95f87d7ef5578e429b62
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file bridge_gui.cpp Graphical user interface for bridge construction */
12 #include "stdafx.h"
13 #include "error.h"
14 #include "command_func.h"
15 #include "rail.h"
16 #include "strings_func.h"
17 #include "window_func.h"
18 #include "sound_func.h"
19 #include "gfx_func.h"
20 #include "tunnelbridge.h"
21 #include "sortlist_type.h"
22 #include "widgets/dropdown_func.h"
23 #include "core/geometry_func.hpp"
24 #include "cmd_helper.h"
25 #include "tunnelbridge_map.h"
26 #include "road_gui.h"
28 #include "widgets/bridge_widget.h"
30 #include "table/strings.h"
32 #include "safeguards.h"
34 /** The type of the last built rail bridge */
35 static BridgeType _last_railbridge_type = 0;
36 /** The type of the last built road bridge */
37 static BridgeType _last_roadbridge_type = 0;
39 /**
40 * Carriage for the data we need if we want to build a bridge
42 struct BuildBridgeData {
43 BridgeType index;
44 const BridgeSpec *spec;
45 Money cost;
48 typedef GUIList<BuildBridgeData> GUIBridgeList; ///< List of bridges, used in #BuildBridgeWindow.
50 /**
51 * Callback executed after a build Bridge CMD has been called
53 * @param result Whether the build succeeded
54 * @param end_tile End tile of the bridge.
55 * @param p1 packed start tile coords (~ dx)
56 * @param p2 various bitstuffed elements
57 * - p2 = (bit 0- 7) - bridge type (hi bh)
58 * - p2 = (bit 8-11) - rail type or road types.
59 * - p2 = (bit 15-16) - transport type.
61 void CcBuildBridge(const CommandCost &result, TileIndex end_tile, uint32 p1, uint32 p2)
63 if (result.Failed()) return;
64 if (_settings_client.sound.confirm) SndPlayTileFx(SND_27_BLACKSMITH_ANVIL, end_tile);
66 TransportType transport_type = Extract<TransportType, 15, 2>(p2);
68 if (transport_type == TRANSPORT_ROAD) {
69 DiagDirection end_direction = ReverseDiagDir(GetTunnelBridgeDirection(end_tile));
70 ConnectRoadToStructure(end_tile, end_direction);
72 DiagDirection start_direction = ReverseDiagDir(GetTunnelBridgeDirection(p1));
73 ConnectRoadToStructure(p1, start_direction);
77 /** Window class for handling the bridge-build GUI. */
78 class BuildBridgeWindow : public Window {
79 private:
80 /* Runtime saved values */
81 static Listing last_sorting; ///< Last setting of the sort.
83 /* Constants for sorting the bridges */
84 static const StringID sorter_names[];
85 static GUIBridgeList::SortFunction * const sorter_funcs[];
87 /* Internal variables */
88 TileIndex start_tile;
89 TileIndex end_tile;
90 uint32 type;
91 GUIBridgeList *bridges;
92 int bridgetext_offset; ///< Horizontal offset of the text describing the bridge properties in #WID_BBS_BRIDGE_LIST relative to the left edge.
93 Scrollbar *vscroll;
95 /** Sort the bridges by their index */
96 static int CDECL BridgeIndexSorter(const BuildBridgeData *a, const BuildBridgeData *b)
98 return a->index - b->index;
101 /** Sort the bridges by their price */
102 static int CDECL BridgePriceSorter(const BuildBridgeData *a, const BuildBridgeData *b)
104 return a->cost - b->cost;
107 /** Sort the bridges by their maximum speed */
108 static int CDECL BridgeSpeedSorter(const BuildBridgeData *a, const BuildBridgeData *b)
110 return a->spec->speed - b->spec->speed;
113 void BuildBridge(uint8 i)
115 switch ((TransportType)(this->type >> 15)) {
116 case TRANSPORT_RAIL: _last_railbridge_type = this->bridges->Get(i)->index; break;
117 case TRANSPORT_ROAD: _last_roadbridge_type = this->bridges->Get(i)->index; break;
118 default: break;
120 DoCommandP(this->end_tile, this->start_tile, this->type | this->bridges->Get(i)->index,
121 CMD_BUILD_BRIDGE | CMD_MSG(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE), CcBuildBridge);
124 /** Sort the builable bridges */
125 void SortBridgeList()
127 this->bridges->Sort();
129 /* Display the current sort variant */
130 this->GetWidget<NWidgetCore>(WID_BBS_DROPDOWN_CRITERIA)->widget_data = this->sorter_names[this->bridges->SortType()];
132 /* Set the modified widgets dirty */
133 this->SetWidgetDirty(WID_BBS_DROPDOWN_CRITERIA);
134 this->SetWidgetDirty(WID_BBS_BRIDGE_LIST);
137 public:
138 BuildBridgeWindow(WindowDesc *desc, TileIndex start, TileIndex end, uint32 br_type, GUIBridgeList *bl) : Window(desc),
139 start_tile(start),
140 end_tile(end),
141 type(br_type),
142 bridges(bl)
144 this->CreateNestedTree();
145 this->vscroll = this->GetScrollbar(WID_BBS_SCROLLBAR);
146 /* Change the data, or the caption of the gui. Set it to road or rail, accordingly. */
147 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;
148 this->FinishInitNested(GB(br_type, 15, 2)); // Initializes 'this->bridgetext_offset'.
150 this->parent = FindWindowById(WC_BUILD_TOOLBAR, GB(this->type, 15, 2));
151 this->bridges->SetListing(this->last_sorting);
152 this->bridges->SetSortFuncs(this->sorter_funcs);
153 this->bridges->NeedResort();
154 this->SortBridgeList();
156 this->vscroll->SetCount(bl->Length());
159 ~BuildBridgeWindow()
161 this->last_sorting = this->bridges->GetListing();
163 delete bridges;
166 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
168 switch (widget) {
169 case WID_BBS_DROPDOWN_ORDER: {
170 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
171 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
172 d.height += padding.height;
173 *size = maxdim(*size, d);
174 break;
176 case WID_BBS_DROPDOWN_CRITERIA: {
177 Dimension d = {0, 0};
178 for (const StringID *str = this->sorter_names; *str != INVALID_STRING_ID; str++) {
179 d = maxdim(d, GetStringBoundingBox(*str));
181 d.width += padding.width;
182 d.height += padding.height;
183 *size = maxdim(*size, d);
184 break;
186 case WID_BBS_BRIDGE_LIST: {
187 Dimension sprite_dim = {0, 0}; // Biggest bridge sprite dimension
188 Dimension text_dim = {0, 0}; // Biggest text dimension
189 for (int i = 0; i < (int)this->bridges->Length(); i++) {
190 const BridgeSpec *b = this->bridges->Get(i)->spec;
191 sprite_dim = maxdim(sprite_dim, GetSpriteSize(b->sprite));
193 SetDParam(2, this->bridges->Get(i)->cost);
194 SetDParam(1, b->speed);
195 SetDParam(0, b->material);
196 text_dim = maxdim(text_dim, GetStringBoundingBox(_game_mode == GM_EDITOR ? STR_SELECT_BRIDGE_SCENEDIT_INFO : STR_SELECT_BRIDGE_INFO));
198 sprite_dim.height++; // Sprite is rendered one pixel down in the matrix field.
199 text_dim.height++; // Allowing the bottom row pixels to be rendered on the edge of the matrix field.
200 resize->height = max(sprite_dim.height, text_dim.height) + 2; // Max of both sizes + account for matrix edges.
202 this->bridgetext_offset = WD_MATRIX_LEFT + sprite_dim.width + 1; // Left edge of text, 1 pixel distance from the sprite.
203 size->width = this->bridgetext_offset + text_dim.width + WD_MATRIX_RIGHT;
204 size->height = 4 * resize->height; // Smallest bridge gui is 4 entries high in the matrix.
205 break;
210 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
212 /* Position the window so hopefully the first bridge from the list is under the mouse pointer. */
213 NWidgetBase *list = this->GetWidget<NWidgetBase>(WID_BBS_BRIDGE_LIST);
214 Point corner; // point of the top left corner of the window.
215 corner.y = Clamp(_cursor.pos.y - list->pos_y - 5, GetMainViewTop(), GetMainViewBottom() - sm_height);
216 corner.x = Clamp(_cursor.pos.x - list->pos_x - 5, 0, _screen.width - sm_width);
217 return corner;
220 virtual void DrawWidget(const Rect &r, int widget) const
222 switch (widget) {
223 case WID_BBS_DROPDOWN_ORDER:
224 this->DrawSortButtonState(widget, this->bridges->IsDescSortOrder() ? SBS_DOWN : SBS_UP);
225 break;
227 case WID_BBS_BRIDGE_LIST: {
228 uint y = r.top;
229 for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < (int)this->bridges->Length(); i++) {
230 const BridgeSpec *b = this->bridges->Get(i)->spec;
232 SetDParam(2, this->bridges->Get(i)->cost);
233 SetDParam(1, b->speed);
234 SetDParam(0, b->material);
236 DrawSprite(b->sprite, b->pal, r.left + WD_MATRIX_LEFT, y + this->resize.step_height - 1 - GetSpriteSize(b->sprite).height);
237 DrawStringMultiLine(r.left + this->bridgetext_offset, r.right, y + 2, y + this->resize.step_height,
238 _game_mode == GM_EDITOR ? STR_SELECT_BRIDGE_SCENEDIT_INFO : STR_SELECT_BRIDGE_INFO);
239 y += this->resize.step_height;
241 break;
246 virtual EventState OnKeyPress(WChar key, uint16 keycode)
248 const uint8 i = keycode - '1';
249 if (i < 9 && i < this->bridges->Length()) {
250 /* Build the requested bridge */
251 this->BuildBridge(i);
252 delete this;
253 return ES_HANDLED;
255 return ES_NOT_HANDLED;
258 virtual void OnClick(Point pt, int widget, int click_count)
260 switch (widget) {
261 default: break;
262 case WID_BBS_BRIDGE_LIST: {
263 uint i = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_BBS_BRIDGE_LIST);
264 if (i < this->bridges->Length()) {
265 this->BuildBridge(i);
266 delete this;
268 break;
271 case WID_BBS_DROPDOWN_ORDER:
272 this->bridges->ToggleSortOrder();
273 this->SetDirty();
274 break;
276 case WID_BBS_DROPDOWN_CRITERIA:
277 ShowDropDownMenu(this, this->sorter_names, this->bridges->SortType(), WID_BBS_DROPDOWN_CRITERIA, 0, 0);
278 break;
282 virtual void OnDropdownSelect(int widget, int index)
284 if (widget == WID_BBS_DROPDOWN_CRITERIA && this->bridges->SortType() != index) {
285 this->bridges->SetSortType(index);
287 this->SortBridgeList();
291 virtual void OnResize()
293 this->vscroll->SetCapacityFromWidget(this, WID_BBS_BRIDGE_LIST);
297 /** Set the default sorting for the bridges */
298 Listing BuildBridgeWindow::last_sorting = {true, 2};
300 /** Available bridge sorting functions. */
301 GUIBridgeList::SortFunction * const BuildBridgeWindow::sorter_funcs[] = {
302 &BridgeIndexSorter,
303 &BridgePriceSorter,
304 &BridgeSpeedSorter
307 /** Names of the sorting functions. */
308 const StringID BuildBridgeWindow::sorter_names[] = {
309 STR_SORT_BY_NUMBER,
310 STR_SORT_BY_COST,
311 STR_SORT_BY_MAX_SPEED,
312 INVALID_STRING_ID
315 /** Widgets of the bridge gui. */
316 static const NWidgetPart _nested_build_bridge_widgets[] = {
317 /* Header */
318 NWidget(NWID_HORIZONTAL),
319 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
320 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_BBS_CAPTION), SetDataTip(STR_SELECT_RAIL_BRIDGE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
321 NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
322 EndContainer(),
324 NWidget(NWID_HORIZONTAL),
325 NWidget(NWID_VERTICAL),
326 /* Sort order + criteria buttons */
327 NWidget(NWID_HORIZONTAL),
328 NWidget(WWT_TEXTBTN, COLOUR_DARK_GREEN, WID_BBS_DROPDOWN_ORDER), SetFill(1, 0), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
329 NWidget(WWT_DROPDOWN, COLOUR_DARK_GREEN, WID_BBS_DROPDOWN_CRITERIA), SetFill(1, 0), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
330 EndContainer(),
331 /* Matrix. */
332 NWidget(WWT_MATRIX, COLOUR_DARK_GREEN, WID_BBS_BRIDGE_LIST), SetFill(1, 0), SetResize(0, 22), SetMatrixDataTip(1, 0, STR_SELECT_BRIDGE_SELECTION_TOOLTIP), SetScrollbar(WID_BBS_SCROLLBAR),
333 EndContainer(),
335 /* scrollbar + resize button */
336 NWidget(NWID_VERTICAL),
337 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BBS_SCROLLBAR),
338 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
339 EndContainer(),
340 EndContainer(),
343 /** Window definition for the rail bridge selection window. */
344 static WindowDesc _build_bridge_desc(
345 WDP_AUTO, "build_bridge", 200, 114,
346 WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
347 WDF_CONSTRUCTION,
348 _nested_build_bridge_widgets, lengthof(_nested_build_bridge_widgets)
352 * Prepare the data for the build a bridge window.
353 * If we can't build a bridge under the given conditions
354 * show an error message.
356 * @param start The start tile of the bridge
357 * @param end The end tile of the bridge
358 * @param transport_type The transport type
359 * @param road_rail_type The road/rail type
361 void ShowBuildBridgeWindow(TileIndex start, TileIndex end, TransportType transport_type, byte road_rail_type)
363 DeleteWindowByClass(WC_BUILD_BRIDGE);
365 /* Data type for the bridge.
366 * Bit 16,15 = transport type,
367 * 14..8 = road/rail types,
368 * 7..0 = type of bridge */
369 uint32 type = (transport_type << 15) | (road_rail_type << 8);
371 /* The bridge length without ramps. */
372 const uint bridge_len = GetTunnelBridgeLength(start, end);
374 /* If Ctrl is being pressed, check whether the last bridge built is available
375 * If so, return this bridge type. Otherwise continue normally.
376 * We store bridge types for each transport type, so we have to check for
377 * the transport type beforehand.
379 BridgeType last_bridge_type = 0;
380 switch (transport_type) {
381 case TRANSPORT_ROAD: last_bridge_type = _last_roadbridge_type; break;
382 case TRANSPORT_RAIL: last_bridge_type = _last_railbridge_type; break;
383 default: break; // water ways and air routes don't have bridge types
385 if (_ctrl_pressed && CheckBridgeAvailability(last_bridge_type, bridge_len).Succeeded()) {
386 DoCommandP(end, start, type | last_bridge_type, CMD_BUILD_BRIDGE | CMD_MSG(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE), CcBuildBridge);
387 return;
390 /* only query bridge building possibility once, result is the same for all bridges!
391 * returns CMD_ERROR on failure, and price on success */
392 StringID errmsg = INVALID_STRING_ID;
393 CommandCost ret = DoCommand(end, start, type, CommandFlagsToDCFlags(GetCommandFlags(CMD_BUILD_BRIDGE)) | DC_QUERY_COST, CMD_BUILD_BRIDGE);
395 GUIBridgeList *bl = NULL;
396 if (ret.Failed()) {
397 errmsg = ret.GetErrorMessage();
398 } else {
399 /* check which bridges can be built */
400 const uint tot_bridgedata_len = CalcBridgeLenCostFactor(bridge_len + 2);
402 bl = new GUIBridgeList();
404 Money infra_cost = 0;
405 switch (transport_type) {
406 case TRANSPORT_ROAD:
407 infra_cost = (bridge_len + 2) * _price[PR_BUILD_ROAD] * 2;
408 /* In case we add a new road type as well, we must be aware of those costs. */
409 if (IsBridgeTile(start)) infra_cost *= CountBits(GetRoadTypes(start) | (RoadTypes)road_rail_type);
410 break;
411 case TRANSPORT_RAIL: infra_cost = (bridge_len + 2) * RailBuildCost((RailType)road_rail_type); break;
412 default: break;
415 /* loop for all bridgetypes */
416 for (BridgeType brd_type = 0; brd_type != MAX_BRIDGES; brd_type++) {
417 if (CheckBridgeAvailability(brd_type, bridge_len).Succeeded()) {
418 /* bridge is accepted, add to list */
419 BuildBridgeData *item = bl->Append();
420 item->index = brd_type;
421 item->spec = GetBridgeSpec(brd_type);
422 /* Add to terraforming & bulldozing costs the cost of the
423 * bridge itself (not computed with DC_QUERY_COST) */
424 item->cost = ret.GetCost() + (((int64)tot_bridgedata_len * _price[PR_BUILD_BRIDGE] * item->spec->price) >> 8) + infra_cost;
429 if (bl != NULL && bl->Length() != 0) {
430 new BuildBridgeWindow(&_build_bridge_desc, start, end, type, bl);
431 } else {
432 delete bl;
433 ShowErrorMessage(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE, errmsg, WL_INFO, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE);