Codefix: Documentation comment in IndustryDirectoryWindow (#13059)
[openttd-github.git] / src / bridge_gui.cpp
blob51b4107fa5d1773140ae6ec9222857022e9943e5
1 /*
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/>.
6 */
8 /** @file bridge_gui.cpp Graphical user interface for bridge construction */
10 #include "stdafx.h"
11 #include "error.h"
12 #include "command_func.h"
13 #include "rail.h"
14 #include "road.h"
15 #include "strings_func.h"
16 #include "window_func.h"
17 #include "sound_func.h"
18 #include "gfx_func.h"
19 #include "tunnelbridge.h"
20 #include "sortlist_type.h"
21 #include "dropdown_func.h"
22 #include "core/geometry_func.hpp"
23 #include "tunnelbridge_map.h"
24 #include "road_gui.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;
38 /**
39 * Carriage for the data we need if we want to build a bridge
41 struct BuildBridgeData {
42 BridgeType index;
43 const BridgeSpec *spec;
44 Money cost;
47 typedef GUIList<BuildBridgeData> GUIBridgeList; ///< List of bridges, used in #BuildBridgeWindow.
49 /**
50 * Callback executed after a build Bridge CMD has been called
52 * @param result Whether the build succeeded
53 * @param end_tile End tile of the bridge.
54 * @param tile_start start tile
55 * @param transport_type transport type.
57 void CcBuildBridge(Commands, const CommandCost &result, TileIndex end_tile, TileIndex tile_start, TransportType transport_type, BridgeType, uint8_t)
59 if (result.Failed()) return;
60 if (_settings_client.sound.confirm) SndPlayTileFx(SND_27_CONSTRUCTION_BRIDGE, end_tile);
62 if (transport_type == TRANSPORT_ROAD) {
63 DiagDirection end_direction = ReverseDiagDir(GetTunnelBridgeDirection(end_tile));
64 ConnectRoadToStructure(end_tile, end_direction);
66 DiagDirection start_direction = ReverseDiagDir(GetTunnelBridgeDirection(tile_start));
67 ConnectRoadToStructure(tile_start, start_direction);
71 /** Window class for handling the bridge-build GUI. */
72 class BuildBridgeWindow : public Window {
73 private:
74 /* Runtime saved values */
75 static Listing last_sorting; ///< Last setting of the sort.
77 /* Constants for sorting the bridges */
78 static inline const StringID sorter_names[] = {
79 STR_SORT_BY_NUMBER,
80 STR_SORT_BY_COST,
81 STR_SORT_BY_MAX_SPEED,
83 static const std::initializer_list<GUIBridgeList::SortFunction * const> sorter_funcs;
85 /* Internal variables */
86 TileIndex start_tile;
87 TileIndex end_tile;
88 TransportType transport_type;
89 uint8_t road_rail_type;
90 GUIBridgeList bridges;
91 int icon_width; ///< Scaled width of the the bridge icon sprite.
92 Scrollbar *vscroll;
94 /** Sort the bridges by their index */
95 static bool BridgeIndexSorter(const BuildBridgeData &a, const BuildBridgeData &b)
97 return a.index < b.index;
100 /** Sort the bridges by their price */
101 static bool BridgePriceSorter(const BuildBridgeData &a, const BuildBridgeData &b)
103 return a.cost < b.cost;
106 /** Sort the bridges by their maximum speed */
107 static bool BridgeSpeedSorter(const BuildBridgeData &a, const BuildBridgeData &b)
109 return a.spec->speed < b.spec->speed;
112 void BuildBridge(BridgeType type)
114 switch (this->transport_type) {
115 case TRANSPORT_RAIL: _last_railbridge_type = type; break;
116 case TRANSPORT_ROAD: _last_roadbridge_type = type; break;
117 default: break;
119 Command<CMD_BUILD_BRIDGE>::Post(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE, CcBuildBridge,
120 this->end_tile, this->start_tile, this->transport_type, type, this->road_rail_type);
123 /** Sort the builable bridges */
124 void SortBridgeList()
126 this->bridges.Sort();
128 /* Display the current sort variant */
129 this->GetWidget<NWidgetCore>(WID_BBS_DROPDOWN_CRITERIA)->widget_data = BuildBridgeWindow::sorter_names[this->bridges.SortType()];
131 /* Set the modified widgets dirty */
132 this->SetWidgetDirty(WID_BBS_DROPDOWN_CRITERIA);
133 this->SetWidgetDirty(WID_BBS_BRIDGE_LIST);
137 * Get the StringID to draw in the selection list and set the appropriate DParams.
138 * @param bridge_data the bridge to get the StringID of.
139 * @return the StringID.
141 StringID GetBridgeSelectString(const BuildBridgeData &bridge_data) const
143 SetDParam(0, bridge_data.spec->material);
144 SetDParam(1, PackVelocity(bridge_data.spec->speed, static_cast<VehicleType>(this->transport_type)));
145 SetDParam(2, bridge_data.cost);
146 /* If the bridge has no meaningful speed limit, don't display it. */
147 if (bridge_data.spec->speed == UINT16_MAX) {
148 return _game_mode == GM_EDITOR ? STR_SELECT_BRIDGE_INFO_NAME : STR_SELECT_BRIDGE_INFO_NAME_COST;
150 return _game_mode == GM_EDITOR ? STR_SELECT_BRIDGE_INFO_NAME_MAX_SPEED : STR_SELECT_BRIDGE_INFO_NAME_MAX_SPEED_COST;
153 public:
154 BuildBridgeWindow(WindowDesc &desc, TileIndex start, TileIndex end, TransportType transport_type, uint8_t road_rail_type, GUIBridgeList &&bl) : Window(desc),
155 start_tile(start),
156 end_tile(end),
157 transport_type(transport_type),
158 road_rail_type(road_rail_type),
159 bridges(std::move(bl))
161 this->CreateNestedTree();
162 this->vscroll = this->GetScrollbar(WID_BBS_SCROLLBAR);
163 /* Change the data, or the caption of the gui. Set it to road or rail, accordingly. */
164 this->GetWidget<NWidgetCore>(WID_BBS_CAPTION)->widget_data = (transport_type == TRANSPORT_ROAD) ? STR_SELECT_ROAD_BRIDGE_CAPTION : STR_SELECT_RAIL_BRIDGE_CAPTION;
165 this->FinishInitNested(transport_type); // Initializes 'this->icon_width'.
167 this->parent = FindWindowById(WC_BUILD_TOOLBAR, transport_type);
168 this->bridges.SetListing(BuildBridgeWindow::last_sorting);
169 this->bridges.SetSortFuncs(BuildBridgeWindow::sorter_funcs);
170 this->bridges.NeedResort();
171 this->SortBridgeList();
173 this->vscroll->SetCount(this->bridges.size());
176 ~BuildBridgeWindow()
178 BuildBridgeWindow::last_sorting = this->bridges.GetListing();
181 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
183 switch (widget) {
184 case WID_BBS_DROPDOWN_ORDER: {
185 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
186 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
187 d.height += padding.height;
188 size = maxdim(size, d);
189 break;
191 case WID_BBS_DROPDOWN_CRITERIA: {
192 Dimension d = GetStringListBoundingBox(BuildBridgeWindow::sorter_names);
193 d.width += padding.width;
194 d.height += padding.height;
195 size = maxdim(size, d);
196 break;
198 case WID_BBS_BRIDGE_LIST: {
199 Dimension sprite_dim = {0, 0}; // Biggest bridge sprite dimension
200 Dimension text_dim = {0, 0}; // Biggest text dimension
201 for (const BuildBridgeData &bridge_data : this->bridges) {
202 sprite_dim = maxdim(sprite_dim, GetScaledSpriteSize(bridge_data.spec->sprite));
203 text_dim = maxdim(text_dim, GetStringBoundingBox(GetBridgeSelectString(bridge_data)));
205 resize.height = std::max(sprite_dim.height, text_dim.height) + padding.height; // Max of both sizes + account for matrix edges.
207 this->icon_width = sprite_dim.width; // Width of bridge icon.
208 size.width = this->icon_width + WidgetDimensions::scaled.hsep_normal + text_dim.width + padding.width;
209 size.height = 4 * resize.height; // Smallest bridge gui is 4 entries high in the matrix.
210 break;
215 Point OnInitialPosition([[maybe_unused]] int16_t sm_width, [[maybe_unused]] int16_t sm_height, [[maybe_unused]] int window_number) override
217 /* Position the window so hopefully the first bridge from the list is under the mouse pointer. */
218 NWidgetBase *list = this->GetWidget<NWidgetBase>(WID_BBS_BRIDGE_LIST);
219 Point corner; // point of the top left corner of the window.
220 corner.y = Clamp(_cursor.pos.y - list->pos_y - 5, GetMainViewTop(), GetMainViewBottom() - sm_height);
221 corner.x = Clamp(_cursor.pos.x - list->pos_x - 5, 0, _screen.width - sm_width);
222 return corner;
225 void DrawWidget(const Rect &r, WidgetID widget) const override
227 switch (widget) {
228 case WID_BBS_DROPDOWN_ORDER:
229 this->DrawSortButtonState(widget, this->bridges.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
230 break;
232 case WID_BBS_BRIDGE_LIST: {
233 Rect tr = r.WithHeight(this->resize.step_height).Shrink(WidgetDimensions::scaled.matrix);
234 bool rtl = _current_text_dir == TD_RTL;
235 auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->bridges);
236 for (auto it = first; it != last; ++it) {
237 const BridgeSpec *b = it->spec;
238 DrawSpriteIgnorePadding(b->sprite, b->pal, tr.WithWidth(this->icon_width, rtl), SA_HOR_CENTER | SA_BOTTOM);
239 DrawStringMultiLine(tr.Indent(this->icon_width + WidgetDimensions::scaled.hsep_normal, rtl), GetBridgeSelectString(*it));
240 tr = tr.Translate(0, this->resize.step_height);
242 break;
247 EventState OnKeyPress([[maybe_unused]] char32_t key, uint16_t keycode) override
249 const uint8_t i = keycode - '1';
250 if (i < 9 && i < this->bridges.size()) {
251 /* Build the requested bridge */
252 this->BuildBridge(this->bridges[i].index);
253 this->Close();
254 return ES_HANDLED;
256 return ES_NOT_HANDLED;
259 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
261 switch (widget) {
262 default: break;
263 case WID_BBS_BRIDGE_LIST: {
264 auto it = this->vscroll->GetScrolledItemFromWidget(this->bridges, pt.y, this, WID_BBS_BRIDGE_LIST);
265 if (it != this->bridges.end()) {
266 this->BuildBridge(it->index);
267 this->Close();
269 break;
272 case WID_BBS_DROPDOWN_ORDER:
273 this->bridges.ToggleSortOrder();
274 this->SetDirty();
275 break;
277 case WID_BBS_DROPDOWN_CRITERIA:
278 ShowDropDownMenu(this, BuildBridgeWindow::sorter_names, this->bridges.SortType(), WID_BBS_DROPDOWN_CRITERIA, 0, 0);
279 break;
283 void OnDropdownSelect(WidgetID widget, int index) override
285 if (widget == WID_BBS_DROPDOWN_CRITERIA && this->bridges.SortType() != index) {
286 this->bridges.SetSortType(index);
288 this->SortBridgeList();
292 void OnResize() override
294 this->vscroll->SetCapacityFromWidget(this, WID_BBS_BRIDGE_LIST);
298 /** Set the default sorting for the bridges */
299 Listing BuildBridgeWindow::last_sorting = {true, 2};
301 /** Available bridge sorting functions. */
302 const std::initializer_list<GUIBridgeList::SortFunction * const> BuildBridgeWindow::sorter_funcs = {
303 &BridgeIndexSorter,
304 &BridgePriceSorter,
305 &BridgeSpeedSorter
308 /** Widgets of the bridge gui. */
309 static constexpr NWidgetPart _nested_build_bridge_widgets[] = {
310 /* Header */
311 NWidget(NWID_HORIZONTAL),
312 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
313 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_BBS_CAPTION), SetDataTip(STR_SELECT_RAIL_BRIDGE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
314 NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
315 EndContainer(),
317 NWidget(NWID_HORIZONTAL),
318 NWidget(NWID_VERTICAL),
319 /* Sort order + criteria buttons */
320 NWidget(NWID_HORIZONTAL),
321 NWidget(WWT_TEXTBTN, COLOUR_DARK_GREEN, WID_BBS_DROPDOWN_ORDER), SetFill(1, 0), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
322 NWidget(WWT_DROPDOWN, COLOUR_DARK_GREEN, WID_BBS_DROPDOWN_CRITERIA), SetFill(1, 0), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
323 EndContainer(),
324 /* Matrix. */
325 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),
326 EndContainer(),
328 /* scrollbar + resize button */
329 NWidget(NWID_VERTICAL),
330 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BBS_SCROLLBAR),
331 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
332 EndContainer(),
333 EndContainer(),
336 /** Window definition for the rail bridge selection window. */
337 static WindowDesc _build_bridge_desc(
338 WDP_AUTO, "build_bridge", 200, 114,
339 WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
340 WDF_CONSTRUCTION,
341 _nested_build_bridge_widgets
345 * Prepare the data for the build a bridge window.
346 * If we can't build a bridge under the given conditions
347 * show an error message.
349 * @param start The start tile of the bridge
350 * @param end The end tile of the bridge
351 * @param transport_type The transport type
352 * @param road_rail_type The road/rail type
354 void ShowBuildBridgeWindow(TileIndex start, TileIndex end, TransportType transport_type, uint8_t road_rail_type)
356 CloseWindowByClass(WC_BUILD_BRIDGE);
358 /* The bridge length without ramps. */
359 const uint bridge_len = GetTunnelBridgeLength(start, end);
361 /* If Ctrl is being pressed, check whether the last bridge built is available
362 * If so, return this bridge type. Otherwise continue normally.
363 * We store bridge types for each transport type, so we have to check for
364 * the transport type beforehand.
366 BridgeType last_bridge_type = 0;
367 switch (transport_type) {
368 case TRANSPORT_ROAD: last_bridge_type = _last_roadbridge_type; break;
369 case TRANSPORT_RAIL: last_bridge_type = _last_railbridge_type; break;
370 default: break; // water ways and air routes don't have bridge types
372 if (_ctrl_pressed && CheckBridgeAvailability(last_bridge_type, bridge_len).Succeeded()) {
373 Command<CMD_BUILD_BRIDGE>::Post(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE, CcBuildBridge, end, start, transport_type, last_bridge_type, road_rail_type);
374 return;
377 /* only query bridge building possibility once, result is the same for all bridges!
378 * returns CMD_ERROR on failure, and price on success */
379 StringID errmsg = INVALID_STRING_ID;
380 CommandCost ret = Command<CMD_BUILD_BRIDGE>::Do(CommandFlagsToDCFlags(GetCommandFlags<CMD_BUILD_BRIDGE>()) | DC_QUERY_COST, end, start, transport_type, 0, road_rail_type);
382 GUIBridgeList bl;
383 if (ret.Failed()) {
384 errmsg = ret.GetErrorMessage();
385 } else {
386 /* check which bridges can be built */
387 const uint tot_bridgedata_len = CalcBridgeLenCostFactor(bridge_len + 2);
389 Money infra_cost = 0;
390 switch (transport_type) {
391 case TRANSPORT_ROAD: {
392 /* In case we add a new road type as well, we must be aware of those costs. */
393 RoadType road_rt = INVALID_ROADTYPE;
394 RoadType tram_rt = INVALID_ROADTYPE;
395 if (IsBridgeTile(start)) {
396 road_rt = GetRoadTypeRoad(start);
397 tram_rt = GetRoadTypeTram(start);
399 if (RoadTypeIsRoad((RoadType)road_rail_type)) {
400 road_rt = (RoadType)road_rail_type;
401 } else {
402 tram_rt = (RoadType)road_rail_type;
405 if (road_rt != INVALID_ROADTYPE) infra_cost += (bridge_len + 2) * 2 * RoadBuildCost(road_rt);
406 if (tram_rt != INVALID_ROADTYPE) infra_cost += (bridge_len + 2) * 2 * RoadBuildCost(tram_rt);
408 break;
410 case TRANSPORT_RAIL: infra_cost = (bridge_len + 2) * RailBuildCost((RailType)road_rail_type); break;
411 default: break;
414 bool any_available = false;
415 CommandCost type_check;
416 /* loop for all bridgetypes */
417 for (BridgeType brd_type = 0; brd_type != MAX_BRIDGES; brd_type++) {
418 type_check = CheckBridgeAvailability(brd_type, bridge_len);
419 if (type_check.Succeeded()) {
420 /* bridge is accepted, add to list */
421 BuildBridgeData &item = bl.emplace_back();
422 item.index = brd_type;
423 item.spec = GetBridgeSpec(brd_type);
424 /* Add to terraforming & bulldozing costs the cost of the
425 * bridge itself (not computed with DC_QUERY_COST) */
426 item.cost = ret.GetCost() + (((int64_t)tot_bridgedata_len * _price[PR_BUILD_BRIDGE] * item.spec->price) >> 8) + infra_cost;
427 any_available = true;
430 /* give error cause if no bridges available here*/
431 if (!any_available)
433 errmsg = type_check.GetErrorMessage();
437 if (!bl.empty()) {
438 new BuildBridgeWindow(_build_bridge_desc, start, end, transport_type, road_rail_type, std::move(bl));
439 } else {
440 ShowErrorMessage(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE, errmsg, WL_INFO, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE);