Translations update
[openttd/fttd.git] / src / bridge_gui.cpp
blob83855c43c1d20bd86cc1c184f9490d502003b052
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 "bridge.h"
26 #include "map/common.h"
27 #include "map/road.h"
28 #include "map/tunnelbridge.h"
29 #include "road_gui.h"
31 #include "widgets/bridge_widget.h"
33 #include "table/strings.h"
35 /** The type of the last built rail bridge */
36 static BridgeType _last_railbridge_type = 0;
37 /** The type of the last built road bridge */
38 static BridgeType _last_roadbridge_type = 0;
40 /**
41 * Carriage for the data we need if we want to build a bridge
43 struct BuildBridgeData {
44 BridgeType index;
45 const BridgeSpec *spec;
46 Money cost;
49 typedef GUIList<BuildBridgeData> GUIBridgeList; ///< List of bridges, used in #BuildBridgeWindow.
51 /**
52 * Callback executed after a build Bridge CMD has been called
54 * @param result Whether the build succeeded
55 * @param end_tile End tile of the bridge.
56 * @param p1 packed start tile coords (~ dx)
57 * @param p2 various bitstuffed elements
58 * - p2 = (bit 0- 7) - bridge type (hi bh)
59 * - p2 = (bit 8-11) - rail type or road types.
60 * - p2 = (bit 12-13) - transport type.
62 void CcBuildBridge(const CommandCost &result, TileIndex end_tile, uint32 p1, uint32 p2)
64 if (result.Failed()) return;
65 if (_settings_client.sound.confirm) SndPlayTileFx(SND_27_BLACKSMITH_ANVIL, end_tile);
67 TransportType transport_type = Extract<TransportType, 12, 2>(p2);
69 if (transport_type == TRANSPORT_ROAD) {
70 DiagDirection end_direction = ReverseDiagDir(GetTunnelBridgeDirection(end_tile));
71 ConnectRoadToStructure(end_tile, end_direction);
73 DiagDirection start_direction = ReverseDiagDir(GetTunnelBridgeDirection(p1));
74 ConnectRoadToStructure(p1, start_direction);
78 /**
79 * Get the error string when building a bridge
81 * @param end_tile End tile of the bridge.
82 * @param p1 packed start tile coords (~ dx)
83 * @param p2 various bitstuffed elements
84 * - p2 = (bit 0- 7) - bridge type (hi bh)
85 * - p2 = (bit 8-11) - rail type or road types.
86 * - p2 = (bit 12-13) - transport type.
88 StringID GetErrBuildBridge(TileIndex end_tile, uint32 p1, uint32 p2, const char *text)
90 TransportType transport_type = Extract<TransportType, 12, 2>(p2);
92 if (transport_type == TRANSPORT_WATER) {
93 return STR_ERROR_CAN_T_BUILD_AQUEDUCT_HERE;
94 } else {
95 return STR_ERROR_CAN_T_BUILD_BRIDGE_HERE;
99 /** Window class for handling the bridge-build GUI. */
100 class BuildBridgeWindow : public Window {
101 private:
102 /* Runtime saved values */
103 static Listing last_sorting; ///< Last setting of the sort.
105 /* Constants for sorting the bridges */
106 static const StringID sorter_names[];
107 static GUIBridgeList::SortFunction * const sorter_funcs[];
109 /* Internal variables */
110 TileIndex start_tile;
111 TileIndex end_tile;
112 uint32 type;
113 GUIBridgeList *bridges;
114 int bridgetext_offset; ///< Horizontal offset of the text describing the bridge properties in #WID_BBS_BRIDGE_LIST relative to the left edge.
115 Scrollbar *vscroll;
117 /** Sort the bridges by their index */
118 static int CDECL BridgeIndexSorter(const BuildBridgeData *a, const BuildBridgeData *b)
120 return a->index - b->index;
123 /** Sort the bridges by their price */
124 static int CDECL BridgePriceSorter(const BuildBridgeData *a, const BuildBridgeData *b)
126 return a->cost - b->cost;
129 /** Sort the bridges by their maximum speed */
130 static int CDECL BridgeSpeedSorter(const BuildBridgeData *a, const BuildBridgeData *b)
132 return a->spec->speed - b->spec->speed;
135 void BuildBridge(uint8 i)
137 switch ((TransportType)(this->type >> 12)) {
138 case TRANSPORT_RAIL: _last_railbridge_type = this->bridges->Get(i)->index; break;
139 case TRANSPORT_ROAD: _last_roadbridge_type = this->bridges->Get(i)->index; break;
140 default: break;
142 DoCommandP(this->end_tile, this->start_tile, this->type | this->bridges->Get(i)->index, CMD_BUILD_BRIDGE);
145 /** Sort the builable bridges */
146 void SortBridgeList()
148 this->bridges->Sort();
150 /* Display the current sort variant */
151 this->GetWidget<NWidgetCore>(WID_BBS_DROPDOWN_CRITERIA)->widget_data = this->sorter_names[this->bridges->SortType()];
153 /* Set the modified widgets dirty */
154 this->SetWidgetDirty(WID_BBS_DROPDOWN_CRITERIA);
155 this->SetWidgetDirty(WID_BBS_BRIDGE_LIST);
158 public:
159 BuildBridgeWindow(WindowDesc *desc, TileIndex start, TileIndex end, uint32 br_type, GUIBridgeList *bl) : Window(desc),
160 start_tile(start),
161 end_tile(end),
162 type(br_type),
163 bridges(bl)
165 this->CreateNestedTree();
166 this->vscroll = this->GetScrollbar(WID_BBS_SCROLLBAR);
167 /* Change the data, or the caption of the gui. Set it to road or rail, accordingly. */
168 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;
169 this->FinishInitNested(GB(br_type, 12, 2)); // Initializes 'this->bridgetext_offset'.
171 this->parent = FindWindowById(WC_BUILD_TOOLBAR, GB(this->type, 12, 2));
172 this->bridges->SetListing(this->last_sorting);
173 this->bridges->SetSortFuncs(this->sorter_funcs);
174 this->bridges->NeedResort();
175 this->SortBridgeList();
177 this->vscroll->SetCount(bl->Length());
180 ~BuildBridgeWindow()
182 this->last_sorting = this->bridges->GetListing();
184 delete bridges;
187 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
189 switch (widget) {
190 case WID_BBS_DROPDOWN_ORDER: {
191 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
192 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
193 d.height += padding.height;
194 *size = maxdim(*size, d);
195 break;
197 case WID_BBS_DROPDOWN_CRITERIA: {
198 Dimension d = {0, 0};
199 for (const StringID *str = this->sorter_names; *str != INVALID_STRING_ID; str++) {
200 d = maxdim(d, GetStringBoundingBox(*str));
202 d.width += padding.width;
203 d.height += padding.height;
204 *size = maxdim(*size, d);
205 break;
207 case WID_BBS_BRIDGE_LIST: {
208 Dimension sprite_dim = {0, 0}; // Biggest bridge sprite dimension
209 Dimension text_dim = {0, 0}; // Biggest text dimension
210 for (int i = 0; i < (int)this->bridges->Length(); i++) {
211 const BridgeSpec *b = this->bridges->Get(i)->spec;
212 sprite_dim = maxdim(sprite_dim, GetSpriteSize(b->sprite));
214 SetDParam(2, this->bridges->Get(i)->cost);
215 SetDParam(1, b->speed);
216 SetDParam(0, b->material);
217 text_dim = maxdim(text_dim, GetStringBoundingBox(_game_mode == GM_EDITOR ? STR_SELECT_BRIDGE_SCENEDIT_INFO : STR_SELECT_BRIDGE_INFO));
219 sprite_dim.height++; // Sprite is rendered one pixel down in the matrix field.
220 text_dim.height++; // Allowing the bottom row pixels to be rendered on the edge of the matrix field.
221 resize->height = max(sprite_dim.height, text_dim.height) + 2; // Max of both sizes + account for matrix edges.
223 this->bridgetext_offset = WD_MATRIX_LEFT + sprite_dim.width + 1; // Left edge of text, 1 pixel distance from the sprite.
224 size->width = this->bridgetext_offset + text_dim.width + WD_MATRIX_RIGHT;
225 size->height = 4 * resize->height; // Smallest bridge gui is 4 entries high in the matrix.
226 break;
231 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
233 /* Position the window so hopefully the first bridge from the list is under the mouse pointer. */
234 NWidgetBase *list = this->GetWidget<NWidgetBase>(WID_BBS_BRIDGE_LIST);
235 Point corner; // point of the top left corner of the window.
236 corner.y = Clamp(_cursor.pos.y - list->pos_y - 5, GetMainViewTop(), GetMainViewBottom() - sm_height);
237 corner.x = Clamp(_cursor.pos.x - list->pos_x - 5, 0, _screen.width - sm_width);
238 return corner;
241 virtual void DrawWidget(const Rect &r, int widget) const
243 switch (widget) {
244 case WID_BBS_DROPDOWN_ORDER:
245 this->DrawSortButtonState(widget, this->bridges->IsDescSortOrder() ? SBS_DOWN : SBS_UP);
246 break;
248 case WID_BBS_BRIDGE_LIST: {
249 uint y = r.top;
250 for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < (int)this->bridges->Length(); i++) {
251 const BridgeSpec *b = this->bridges->Get(i)->spec;
253 SetDParam(2, this->bridges->Get(i)->cost);
254 SetDParam(1, b->speed);
255 SetDParam(0, b->material);
257 DrawSprite(b->sprite, b->pal, r.left + WD_MATRIX_LEFT, y + this->resize.step_height - 1 - GetSpriteSize(b->sprite).height);
258 DrawStringMultiLine(r.left + this->bridgetext_offset, r.right, y + 2, y + this->resize.step_height,
259 _game_mode == GM_EDITOR ? STR_SELECT_BRIDGE_SCENEDIT_INFO : STR_SELECT_BRIDGE_INFO);
260 y += this->resize.step_height;
262 break;
267 virtual EventState OnKeyPress(WChar key, uint16 keycode)
269 const uint8 i = keycode - '1';
270 if (i < 9 && i < this->bridges->Length()) {
271 /* Build the requested bridge */
272 this->BuildBridge(i);
273 delete this;
274 return ES_HANDLED;
276 return ES_NOT_HANDLED;
279 virtual void OnClick(Point pt, int widget, int click_count)
281 switch (widget) {
282 default: break;
283 case WID_BBS_BRIDGE_LIST: {
284 uint i = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_BBS_BRIDGE_LIST);
285 if (i < this->bridges->Length()) {
286 this->BuildBridge(i);
287 delete this;
289 break;
292 case WID_BBS_DROPDOWN_ORDER:
293 this->bridges->ToggleSortOrder();
294 this->SetDirty();
295 break;
297 case WID_BBS_DROPDOWN_CRITERIA:
298 ShowDropDownMenu(this, this->sorter_names, this->bridges->SortType(), WID_BBS_DROPDOWN_CRITERIA, 0, 0);
299 break;
303 virtual void OnDropdownSelect(int widget, int index)
305 if (widget == WID_BBS_DROPDOWN_CRITERIA && this->bridges->SortType() != index) {
306 this->bridges->SetSortType(index);
308 this->SortBridgeList();
312 virtual void OnResize()
314 this->vscroll->SetCapacityFromWidget(this, WID_BBS_BRIDGE_LIST);
318 /** Set the default sorting for the bridges */
319 Listing BuildBridgeWindow::last_sorting = {true, 2};
321 /** Available bridge sorting functions. */
322 GUIBridgeList::SortFunction * const BuildBridgeWindow::sorter_funcs[] = {
323 &BridgeIndexSorter,
324 &BridgePriceSorter,
325 &BridgeSpeedSorter
328 /** Names of the sorting functions. */
329 const StringID BuildBridgeWindow::sorter_names[] = {
330 STR_SORT_BY_NUMBER,
331 STR_SORT_BY_COST,
332 STR_SORT_BY_MAX_SPEED,
333 INVALID_STRING_ID
336 /** Widgets of the bridge gui. */
337 static const NWidgetPart _nested_build_bridge_widgets[] = {
338 /* Header */
339 NWidget(NWID_HORIZONTAL),
340 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
341 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_BBS_CAPTION), SetDataTip(STR_SELECT_RAIL_BRIDGE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
342 NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
343 EndContainer(),
345 NWidget(NWID_HORIZONTAL),
346 NWidget(NWID_VERTICAL),
347 /* Sort order + criteria buttons */
348 NWidget(NWID_HORIZONTAL),
349 NWidget(WWT_TEXTBTN, COLOUR_DARK_GREEN, WID_BBS_DROPDOWN_ORDER), SetFill(1, 0), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
350 NWidget(WWT_DROPDOWN, COLOUR_DARK_GREEN, WID_BBS_DROPDOWN_CRITERIA), SetFill(1, 0), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
351 EndContainer(),
352 /* Matrix. */
353 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),
354 EndContainer(),
356 /* scrollbar + resize button */
357 NWidget(NWID_VERTICAL),
358 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BBS_SCROLLBAR),
359 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
360 EndContainer(),
361 EndContainer(),
364 /** Window definition for the rail bridge selection window. */
365 static WindowDesc _build_bridge_desc(
366 WDP_AUTO, "build_bridge", 200, 114,
367 WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
368 WDF_CONSTRUCTION,
369 _nested_build_bridge_widgets, lengthof(_nested_build_bridge_widgets)
373 * Prepare the data for the build a bridge window.
374 * If we can't build a bridge under the given conditions
375 * show an error message.
377 * @param start The start tile of the bridge
378 * @param end The end tile of the bridge
379 * @param transport_type The transport type
380 * @param road_rail_type The road/rail type
382 void ShowBuildBridgeWindow(TileIndex start, TileIndex end, TransportType transport_type, byte road_rail_type)
384 DeleteWindowByClass(WC_BUILD_BRIDGE);
386 /* Data type for the bridge.
387 * Bit 13,12 = transport type,
388 * 11..8 = road/rail types,
389 * 7..0 = type of bridge */
390 uint32 type = (transport_type << 12) | (road_rail_type << 8);
392 /* The bridge length without ramps. */
393 const uint bridge_len = GetTunnelBridgeLength(start, end);
395 /* If Ctrl is being pressed, check whether the last bridge built is available
396 * If so, return this bridge type. Otherwise continue normally.
397 * We store bridge types for each transport type, so we have to check for
398 * the transport type beforehand.
400 BridgeType last_bridge_type = 0;
401 switch (transport_type) {
402 case TRANSPORT_ROAD: last_bridge_type = _last_roadbridge_type; break;
403 case TRANSPORT_RAIL: last_bridge_type = _last_railbridge_type; break;
404 default: break; // water ways and air routes don't have bridge types
406 if (_ctrl_pressed && CheckBridgeAvailability(last_bridge_type, bridge_len).Succeeded()) {
407 DoCommandP(end, start, type | last_bridge_type, CMD_BUILD_BRIDGE);
408 return;
411 /* only query bridge building possibility once, result is the same for all bridges!
412 * returns CMD_ERROR on failure, and price on success */
413 StringID errmsg = INVALID_STRING_ID;
414 CommandCost ret = DoCommand(end, start, type, CommandFlagsToDCFlags(GetCommandFlags(CMD_BUILD_BRIDGE)) | DC_QUERY_COST, CMD_BUILD_BRIDGE);
416 GUIBridgeList *bl = NULL;
417 if (ret.Failed()) {
418 errmsg = ret.GetErrorMessage();
419 } else {
420 /* check which bridges can be built */
421 const uint tot_bridgedata_len = CalcBridgeLenCostFactor(bridge_len + 2);
423 bl = new GUIBridgeList();
425 Money infra_cost = 0;
426 switch (transport_type) {
427 case TRANSPORT_ROAD:
428 infra_cost = (bridge_len + 2) * _price[PR_BUILD_ROAD] * 2;
429 /* In case we add a new road type as well, we must be aware of those costs. */
430 if (IsRoadBridgeTile(start)) infra_cost *= CountBits(GetRoadTypes(start) | (RoadTypes)road_rail_type);
431 break;
432 case TRANSPORT_RAIL: infra_cost = (bridge_len + 2) * RailBuildCost((RailType)road_rail_type); break;
433 default: break;
436 /* loop for all bridgetypes */
437 for (BridgeType brd_type = 0; brd_type != MAX_BRIDGES; brd_type++) {
438 if (CheckBridgeAvailability(brd_type, bridge_len).Succeeded()) {
439 /* bridge is accepted, add to list */
440 BuildBridgeData *item = bl->Append();
441 item->index = brd_type;
442 item->spec = GetBridgeSpec(brd_type);
443 /* Add to terraforming & bulldozing costs the cost of the
444 * bridge itself (not computed with DC_QUERY_COST) */
445 item->cost = ret.GetCost() + (((int64)tot_bridgedata_len * _price[PR_BUILD_BRIDGE] * item->spec->price) >> 8) + infra_cost;
450 if (bl != NULL && bl->Length() != 0) {
451 new BuildBridgeWindow(&_build_bridge_desc, start, end, type, bl);
452 } else {
453 delete bl;
454 ShowErrorMessage(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE, errmsg, WL_INFO, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE);