2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 /** @file bridge_gui.cpp Graphical user interface for bridge construction */
12 #include "command_func.h"
15 #include "strings_func.h"
16 #include "window_func.h"
17 #include "sound_func.h"
19 #include "tunnelbridge.h"
20 #include "sortlist_type.h"
21 #include "widgets/dropdown_func.h"
22 #include "core/geometry_func.hpp"
23 #include "tunnelbridge_map.h"
25 #include "tunnelbridge_cmd.h"
27 #include "widgets/bridge_widget.h"
29 #include "table/strings.h"
31 #include "safeguards.h"
33 /** The type of the last built rail bridge */
34 static BridgeType _last_railbridge_type
= 0;
35 /** The type of the last built road bridge */
36 static BridgeType _last_roadbridge_type
= 0;
39 * Carriage for the data we need if we want to build a bridge
41 struct BuildBridgeData
{
43 const BridgeSpec
*spec
;
47 typedef GUIList
<BuildBridgeData
> GUIBridgeList
; ///< List of bridges, used in #BuildBridgeWindow.
50 * Callback executed after a build Bridge CMD has been called
52 * @param result Whether the build succeeded
54 * @param end_tile End tile of the bridge.
55 * @param tile_start start tile
56 * @param transport_type transport type.
58 void CcBuildBridge(Commands cmd
, const CommandCost
&result
, TileIndex end_tile
, TileIndex tile_start
, TransportType transport_type
, BridgeType
, byte
)
60 if (result
.Failed()) return;
61 if (_settings_client
.sound
.confirm
) SndPlayTileFx(SND_27_CONSTRUCTION_BRIDGE
, end_tile
);
63 if (transport_type
== TRANSPORT_ROAD
) {
64 DiagDirection end_direction
= ReverseDiagDir(GetTunnelBridgeDirection(end_tile
));
65 ConnectRoadToStructure(end_tile
, end_direction
);
67 DiagDirection start_direction
= ReverseDiagDir(GetTunnelBridgeDirection(tile_start
));
68 ConnectRoadToStructure(tile_start
, start_direction
);
72 /** Window class for handling the bridge-build GUI. */
73 class BuildBridgeWindow
: public Window
{
75 /* Runtime saved values */
76 static Listing last_sorting
; ///< Last setting of the sort.
78 /* Constants for sorting the bridges */
79 static const StringID sorter_names
[];
80 static GUIBridgeList::SortFunction
* const sorter_funcs
[];
82 /* Internal variables */
85 TransportType transport_type
;
87 GUIBridgeList
*bridges
;
88 int bridgetext_offset
; ///< Horizontal offset of the text describing the bridge properties in #WID_BBS_BRIDGE_LIST relative to the left edge.
91 /** Sort the bridges by their index */
92 static bool BridgeIndexSorter(const BuildBridgeData
&a
, const BuildBridgeData
&b
)
94 return a
.index
< b
.index
;
97 /** Sort the bridges by their price */
98 static bool BridgePriceSorter(const BuildBridgeData
&a
, const BuildBridgeData
&b
)
100 return a
.cost
< b
.cost
;
103 /** Sort the bridges by their maximum speed */
104 static bool BridgeSpeedSorter(const BuildBridgeData
&a
, const BuildBridgeData
&b
)
106 return a
.spec
->speed
< b
.spec
->speed
;
109 void BuildBridge(uint8 i
)
111 switch (this->transport_type
) {
112 case TRANSPORT_RAIL
: _last_railbridge_type
= this->bridges
->at(i
).index
; break;
113 case TRANSPORT_ROAD
: _last_roadbridge_type
= this->bridges
->at(i
).index
; break;
116 Command
<CMD_BUILD_BRIDGE
>::Post(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE
, CcBuildBridge
,
117 this->end_tile
, this->start_tile
, this->transport_type
, this->bridges
->at(i
).index
, this->road_rail_type
);
120 /** Sort the builable bridges */
121 void SortBridgeList()
123 this->bridges
->Sort();
125 /* Display the current sort variant */
126 this->GetWidget
<NWidgetCore
>(WID_BBS_DROPDOWN_CRITERIA
)->widget_data
= this->sorter_names
[this->bridges
->SortType()];
128 /* Set the modified widgets dirty */
129 this->SetWidgetDirty(WID_BBS_DROPDOWN_CRITERIA
);
130 this->SetWidgetDirty(WID_BBS_BRIDGE_LIST
);
134 BuildBridgeWindow(WindowDesc
*desc
, TileIndex start
, TileIndex end
, TransportType transport_type
, byte road_rail_type
, GUIBridgeList
*bl
) : Window(desc
),
137 transport_type(transport_type
),
138 road_rail_type(road_rail_type
),
141 this->CreateNestedTree();
142 this->vscroll
= this->GetScrollbar(WID_BBS_SCROLLBAR
);
143 /* Change the data, or the caption of the gui. Set it to road or rail, accordingly. */
144 this->GetWidget
<NWidgetCore
>(WID_BBS_CAPTION
)->widget_data
= (transport_type
== TRANSPORT_ROAD
) ? STR_SELECT_ROAD_BRIDGE_CAPTION
: STR_SELECT_RAIL_BRIDGE_CAPTION
;
145 this->FinishInitNested(transport_type
); // Initializes 'this->bridgetext_offset'.
147 this->parent
= FindWindowById(WC_BUILD_TOOLBAR
, transport_type
);
148 this->bridges
->SetListing(this->last_sorting
);
149 this->bridges
->SetSortFuncs(this->sorter_funcs
);
150 this->bridges
->NeedResort();
151 this->SortBridgeList();
153 this->vscroll
->SetCount((uint
)bl
->size());
158 this->last_sorting
= this->bridges
->GetListing();
163 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
166 case WID_BBS_DROPDOWN_ORDER
: {
167 Dimension d
= GetStringBoundingBox(this->GetWidget
<NWidgetCore
>(widget
)->widget_data
);
168 d
.width
+= padding
.width
+ Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
169 d
.height
+= padding
.height
;
170 *size
= maxdim(*size
, d
);
173 case WID_BBS_DROPDOWN_CRITERIA
: {
174 Dimension d
= {0, 0};
175 for (const StringID
*str
= this->sorter_names
; *str
!= INVALID_STRING_ID
; str
++) {
176 d
= maxdim(d
, GetStringBoundingBox(*str
));
178 d
.width
+= padding
.width
;
179 d
.height
+= padding
.height
;
180 *size
= maxdim(*size
, d
);
183 case WID_BBS_BRIDGE_LIST
: {
184 Dimension sprite_dim
= {0, 0}; // Biggest bridge sprite dimension
185 Dimension text_dim
= {0, 0}; // Biggest text dimension
186 for (int i
= 0; i
< (int)this->bridges
->size(); i
++) {
187 const BridgeSpec
*b
= this->bridges
->at(i
).spec
;
188 sprite_dim
= maxdim(sprite_dim
, GetSpriteSize(b
->sprite
));
190 SetDParam(2, this->bridges
->at(i
).cost
);
191 SetDParam(1, b
->speed
);
192 SetDParam(0, b
->material
);
193 text_dim
= maxdim(text_dim
, GetStringBoundingBox(_game_mode
== GM_EDITOR
? STR_SELECT_BRIDGE_SCENEDIT_INFO
: STR_SELECT_BRIDGE_INFO
));
195 sprite_dim
.height
++; // Sprite is rendered one pixel down in the matrix field.
196 text_dim
.height
++; // Allowing the bottom row pixels to be rendered on the edge of the matrix field.
197 resize
->height
= std::max(sprite_dim
.height
, text_dim
.height
) + 2; // Max of both sizes + account for matrix edges.
199 this->bridgetext_offset
= WD_MATRIX_LEFT
+ sprite_dim
.width
+ 1; // Left edge of text, 1 pixel distance from the sprite.
200 size
->width
= this->bridgetext_offset
+ text_dim
.width
+ WD_MATRIX_RIGHT
;
201 size
->height
= 4 * resize
->height
; // Smallest bridge gui is 4 entries high in the matrix.
207 Point
OnInitialPosition(int16 sm_width
, int16 sm_height
, int window_number
) override
209 /* Position the window so hopefully the first bridge from the list is under the mouse pointer. */
210 NWidgetBase
*list
= this->GetWidget
<NWidgetBase
>(WID_BBS_BRIDGE_LIST
);
211 Point corner
; // point of the top left corner of the window.
212 corner
.y
= Clamp(_cursor
.pos
.y
- list
->pos_y
- 5, GetMainViewTop(), GetMainViewBottom() - sm_height
);
213 corner
.x
= Clamp(_cursor
.pos
.x
- list
->pos_x
- 5, 0, _screen
.width
- sm_width
);
217 void DrawWidget(const Rect
&r
, int widget
) const override
220 case WID_BBS_DROPDOWN_ORDER
:
221 this->DrawSortButtonState(widget
, this->bridges
->IsDescSortOrder() ? SBS_DOWN
: SBS_UP
);
224 case WID_BBS_BRIDGE_LIST
: {
226 for (int i
= this->vscroll
->GetPosition(); this->vscroll
->IsVisible(i
) && i
< (int)this->bridges
->size(); i
++) {
227 const BridgeSpec
*b
= this->bridges
->at(i
).spec
;
229 SetDParam(2, this->bridges
->at(i
).cost
);
230 SetDParam(1, b
->speed
);
231 SetDParam(0, b
->material
);
233 DrawSprite(b
->sprite
, b
->pal
, r
.left
+ WD_MATRIX_LEFT
, y
+ this->resize
.step_height
- 1 - GetSpriteSize(b
->sprite
).height
);
234 DrawStringMultiLine(r
.left
+ this->bridgetext_offset
, r
.right
, y
+ 2, y
+ this->resize
.step_height
,
235 _game_mode
== GM_EDITOR
? STR_SELECT_BRIDGE_SCENEDIT_INFO
: STR_SELECT_BRIDGE_INFO
);
236 y
+= this->resize
.step_height
;
243 EventState
OnKeyPress(WChar key
, uint16 keycode
) override
245 const uint8 i
= keycode
- '1';
246 if (i
< 9 && i
< this->bridges
->size()) {
247 /* Build the requested bridge */
248 this->BuildBridge(i
);
252 return ES_NOT_HANDLED
;
255 void OnClick(Point pt
, int widget
, int click_count
) override
259 case WID_BBS_BRIDGE_LIST
: {
260 uint i
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_BBS_BRIDGE_LIST
);
261 if (i
< this->bridges
->size()) {
262 this->BuildBridge(i
);
268 case WID_BBS_DROPDOWN_ORDER
:
269 this->bridges
->ToggleSortOrder();
273 case WID_BBS_DROPDOWN_CRITERIA
:
274 ShowDropDownMenu(this, this->sorter_names
, this->bridges
->SortType(), WID_BBS_DROPDOWN_CRITERIA
, 0, 0);
279 void OnDropdownSelect(int widget
, int index
) override
281 if (widget
== WID_BBS_DROPDOWN_CRITERIA
&& this->bridges
->SortType() != index
) {
282 this->bridges
->SetSortType(index
);
284 this->SortBridgeList();
288 void OnResize() override
290 this->vscroll
->SetCapacityFromWidget(this, WID_BBS_BRIDGE_LIST
);
294 /** Set the default sorting for the bridges */
295 Listing
BuildBridgeWindow::last_sorting
= {true, 2};
297 /** Available bridge sorting functions. */
298 GUIBridgeList::SortFunction
* const BuildBridgeWindow::sorter_funcs
[] = {
304 /** Names of the sorting functions. */
305 const StringID
BuildBridgeWindow::sorter_names
[] = {
308 STR_SORT_BY_MAX_SPEED
,
312 /** Widgets of the bridge gui. */
313 static const NWidgetPart _nested_build_bridge_widgets
[] = {
315 NWidget(NWID_HORIZONTAL
),
316 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
317 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
, WID_BBS_CAPTION
), SetDataTip(STR_SELECT_RAIL_BRIDGE_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
318 NWidget(WWT_DEFSIZEBOX
, COLOUR_DARK_GREEN
),
321 NWidget(NWID_HORIZONTAL
),
322 NWidget(NWID_VERTICAL
),
323 /* Sort order + criteria buttons */
324 NWidget(NWID_HORIZONTAL
),
325 NWidget(WWT_TEXTBTN
, COLOUR_DARK_GREEN
, WID_BBS_DROPDOWN_ORDER
), SetFill(1, 0), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
),
326 NWidget(WWT_DROPDOWN
, COLOUR_DARK_GREEN
, WID_BBS_DROPDOWN_CRITERIA
), SetFill(1, 0), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA
),
329 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
),
332 /* scrollbar + resize button */
333 NWidget(NWID_VERTICAL
),
334 NWidget(NWID_VSCROLLBAR
, COLOUR_DARK_GREEN
, WID_BBS_SCROLLBAR
),
335 NWidget(WWT_RESIZEBOX
, COLOUR_DARK_GREEN
),
340 /** Window definition for the rail bridge selection window. */
341 static WindowDesc
_build_bridge_desc(
342 WDP_AUTO
, "build_bridge", 200, 114,
343 WC_BUILD_BRIDGE
, WC_BUILD_TOOLBAR
,
345 _nested_build_bridge_widgets
, lengthof(_nested_build_bridge_widgets
)
349 * Prepare the data for the build a bridge window.
350 * If we can't build a bridge under the given conditions
351 * show an error message.
353 * @param start The start tile of the bridge
354 * @param end The end tile of the bridge
355 * @param transport_type The transport type
356 * @param road_rail_type The road/rail type
358 void ShowBuildBridgeWindow(TileIndex start
, TileIndex end
, TransportType transport_type
, byte road_rail_type
)
360 CloseWindowByClass(WC_BUILD_BRIDGE
);
362 /* The bridge length without ramps. */
363 const uint bridge_len
= GetTunnelBridgeLength(start
, end
);
365 /* If Ctrl is being pressed, check whether the last bridge built is available
366 * If so, return this bridge type. Otherwise continue normally.
367 * We store bridge types for each transport type, so we have to check for
368 * the transport type beforehand.
370 BridgeType last_bridge_type
= 0;
371 switch (transport_type
) {
372 case TRANSPORT_ROAD
: last_bridge_type
= _last_roadbridge_type
; break;
373 case TRANSPORT_RAIL
: last_bridge_type
= _last_railbridge_type
; break;
374 default: break; // water ways and air routes don't have bridge types
376 if (_ctrl_pressed
&& CheckBridgeAvailability(last_bridge_type
, bridge_len
).Succeeded()) {
377 Command
<CMD_BUILD_BRIDGE
>::Post(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE
, CcBuildBridge
, end
, start
, transport_type
, last_bridge_type
, road_rail_type
);
381 /* only query bridge building possibility once, result is the same for all bridges!
382 * returns CMD_ERROR on failure, and price on success */
383 StringID errmsg
= INVALID_STRING_ID
;
384 CommandCost ret
= Command
<CMD_BUILD_BRIDGE
>::Do(CommandFlagsToDCFlags(GetCommandFlags
<CMD_BUILD_BRIDGE
>()) | DC_QUERY_COST
, end
, start
, transport_type
, 0, road_rail_type
);
386 GUIBridgeList
*bl
= nullptr;
388 errmsg
= ret
.GetErrorMessage();
390 /* check which bridges can be built */
391 const uint tot_bridgedata_len
= CalcBridgeLenCostFactor(bridge_len
+ 2);
393 bl
= new GUIBridgeList();
395 Money infra_cost
= 0;
396 switch (transport_type
) {
397 case TRANSPORT_ROAD
: {
398 /* In case we add a new road type as well, we must be aware of those costs. */
399 RoadType road_rt
= INVALID_ROADTYPE
;
400 RoadType tram_rt
= INVALID_ROADTYPE
;
401 if (IsBridgeTile(start
)) {
402 road_rt
= GetRoadTypeRoad(start
);
403 tram_rt
= GetRoadTypeTram(start
);
405 if (RoadTypeIsRoad((RoadType
)road_rail_type
)) {
406 road_rt
= (RoadType
)road_rail_type
;
408 tram_rt
= (RoadType
)road_rail_type
;
411 if (road_rt
!= INVALID_ROADTYPE
) infra_cost
+= (bridge_len
+ 2) * 2 * RoadBuildCost(road_rt
);
412 if (tram_rt
!= INVALID_ROADTYPE
) infra_cost
+= (bridge_len
+ 2) * 2 * RoadBuildCost(tram_rt
);
416 case TRANSPORT_RAIL
: infra_cost
= (bridge_len
+ 2) * RailBuildCost((RailType
)road_rail_type
); break;
420 bool any_available
= false;
421 CommandCost type_check
;
422 /* loop for all bridgetypes */
423 for (BridgeType brd_type
= 0; brd_type
!= MAX_BRIDGES
; brd_type
++) {
424 type_check
= CheckBridgeAvailability(brd_type
, bridge_len
);
425 if (type_check
.Succeeded()) {
426 /* bridge is accepted, add to list */
427 BuildBridgeData
&item
= bl
->emplace_back();
428 item
.index
= brd_type
;
429 item
.spec
= GetBridgeSpec(brd_type
);
430 /* Add to terraforming & bulldozing costs the cost of the
431 * bridge itself (not computed with DC_QUERY_COST) */
432 item
.cost
= ret
.GetCost() + (((int64
)tot_bridgedata_len
* _price
[PR_BUILD_BRIDGE
] * item
.spec
->price
) >> 8) + infra_cost
;
433 any_available
= true;
436 /* give error cause if no bridges available here*/
439 errmsg
= type_check
.GetErrorMessage();
443 if (bl
!= nullptr && bl
->size() != 0) {
444 new BuildBridgeWindow(&_build_bridge_desc
, start
, end
, transport_type
, road_rail_type
, bl
);
447 ShowErrorMessage(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE
, errmsg
, WL_INFO
, TileX(end
) * TILE_SIZE
, TileY(end
) * TILE_SIZE
);