Fix: Stopped ships shouldn't block depots (#8578)
[openttd-github.git] / src / rail_gui.cpp
blob77792f2761e9f8ab26c20f9c56f7cc5287d9d978
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 rail_gui.cpp %File for dealing with rail construction user interface */
10 #include "stdafx.h"
11 #include "gui.h"
12 #include "window_gui.h"
13 #include "station_gui.h"
14 #include "terraform_gui.h"
15 #include "viewport_func.h"
16 #include "command_func.h"
17 #include "waypoint_func.h"
18 #include "newgrf_station.h"
19 #include "company_base.h"
20 #include "strings_func.h"
21 #include "window_func.h"
22 #include "date_func.h"
23 #include "sound_func.h"
24 #include "company_func.h"
25 #include "widgets/dropdown_type.h"
26 #include "tunnelbridge.h"
27 #include "tilehighlight_func.h"
28 #include "spritecache.h"
29 #include "core/geometry_func.hpp"
30 #include "hotkeys.h"
31 #include "engine_base.h"
32 #include "vehicle_func.h"
33 #include "zoom_func.h"
34 #include "rail_gui.h"
36 #include "station_map.h"
37 #include "tunnelbridge_map.h"
39 #include "widgets/rail_widget.h"
41 #include "safeguards.h"
44 static RailType _cur_railtype; ///< Rail type of the current build-rail toolbar.
45 static bool _remove_button_clicked; ///< Flag whether 'remove' toggle-button is currently enabled
46 static DiagDirection _build_depot_direction; ///< Currently selected depot direction
47 static byte _waypoint_count = 1; ///< Number of waypoint types
48 static byte _cur_waypoint_type; ///< Currently selected waypoint type
49 static bool _convert_signal_button; ///< convert signal button in the signal GUI pressed
50 static SignalVariant _cur_signal_variant; ///< set the signal variant (for signal GUI)
51 static SignalType _cur_signal_type; ///< set the signal type (for signal GUI)
53 /* Map the setting: default_signal_type to the corresponding signal type */
54 static const SignalType _default_signal_type[] = {SIGTYPE_NORMAL, SIGTYPE_PBS, SIGTYPE_PBS_ONEWAY};
56 struct RailStationGUISettings {
57 Axis orientation; ///< Currently selected rail station orientation
59 bool newstations; ///< Are custom station definitions available?
60 StationClassID station_class; ///< Currently selected custom station class (if newstations is \c true )
61 byte station_type; ///< %Station type within the currently selected custom station class (if newstations is \c true )
62 byte station_count; ///< Number of custom stations (if newstations is \c true )
64 static RailStationGUISettings _railstation; ///< Settings of the station builder GUI
67 static void HandleStationPlacement(TileIndex start, TileIndex end);
68 static void ShowBuildTrainDepotPicker(Window *parent);
69 static void ShowBuildWaypointPicker(Window *parent);
70 static void ShowStationBuilder(Window *parent);
71 static void ShowSignalBuilder(Window *parent);
73 /**
74 * Check whether a station type can be build.
75 * @return true if building is allowed.
77 static bool IsStationAvailable(const StationSpec *statspec)
79 if (statspec == nullptr || !HasBit(statspec->callback_mask, CBM_STATION_AVAIL)) return true;
81 uint16 cb_res = GetStationCallback(CBID_STATION_AVAILABILITY, 0, 0, statspec, nullptr, INVALID_TILE);
82 if (cb_res == CALLBACK_FAILED) return true;
84 return Convert8bitBooleanCallback(statspec->grf_prop.grffile, CBID_STATION_AVAILABILITY, cb_res);
87 void CcPlaySound_SPLAT_RAIL(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
89 if (result.Succeeded() && _settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_RAIL, tile);
92 static void GenericPlaceRail(TileIndex tile, int cmd)
94 DoCommandP(tile, _cur_railtype, cmd | (_settings_client.gui.auto_remove_signals << 3),
95 _remove_button_clicked ?
96 CMD_REMOVE_SINGLE_RAIL | CMD_MSG(STR_ERROR_CAN_T_REMOVE_RAILROAD_TRACK) :
97 CMD_BUILD_SINGLE_RAIL | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_TRACK),
98 CcPlaySound_SPLAT_RAIL);
102 * Try to add an additional rail-track at the entrance of a depot
103 * @param tile Tile to use for adding the rail-track
104 * @param dir Direction to check for already present tracks
105 * @param track Track to add
106 * @see CcRailDepot()
108 static void PlaceExtraDepotRail(TileIndex tile, DiagDirection dir, Track track)
110 if (GetRailTileType(tile) == RAIL_TILE_DEPOT) return;
111 if (GetRailTileType(tile) == RAIL_TILE_SIGNALS && !_settings_client.gui.auto_remove_signals) return;
112 if ((GetTrackBits(tile) & DiagdirReachesTracks(dir)) == 0) return;
114 DoCommandP(tile, _cur_railtype, track | (_settings_client.gui.auto_remove_signals << 3), CMD_BUILD_SINGLE_RAIL);
117 /** Additional pieces of track to add at the entrance of a depot. */
118 static const Track _place_depot_extra_track[12] = {
119 TRACK_LEFT, TRACK_UPPER, TRACK_UPPER, TRACK_RIGHT, // First additional track for directions 0..3
120 TRACK_X, TRACK_Y, TRACK_X, TRACK_Y, // Second additional track
121 TRACK_LOWER, TRACK_LEFT, TRACK_RIGHT, TRACK_LOWER, // Third additional track
124 /** Direction to check for existing track pieces. */
125 static const DiagDirection _place_depot_extra_dir[12] = {
126 DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, DIAGDIR_SW,
127 DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE, DIAGDIR_SE,
128 DIAGDIR_NW, DIAGDIR_NE, DIAGDIR_NW, DIAGDIR_NE,
131 void CcRailDepot(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
133 if (result.Failed()) return;
135 DiagDirection dir = (DiagDirection)p2;
137 if (_settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_RAIL, tile);
138 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
140 tile += TileOffsByDiagDir(dir);
142 if (IsTileType(tile, MP_RAILWAY)) {
143 PlaceExtraDepotRail(tile, _place_depot_extra_dir[dir], _place_depot_extra_track[dir]);
144 PlaceExtraDepotRail(tile, _place_depot_extra_dir[dir + 4], _place_depot_extra_track[dir + 4]);
145 PlaceExtraDepotRail(tile, _place_depot_extra_dir[dir + 8], _place_depot_extra_track[dir + 8]);
150 * Place a rail waypoint.
151 * @param tile Position to start dragging a waypoint.
153 static void PlaceRail_Waypoint(TileIndex tile)
155 if (_remove_button_clicked) {
156 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_REMOVE_STATION);
157 return;
160 Axis axis = GetAxisForNewWaypoint(tile);
161 if (IsValidAxis(axis)) {
162 /* Valid tile for waypoints */
163 VpStartPlaceSizing(tile, axis == AXIS_X ? VPM_X_LIMITED : VPM_Y_LIMITED, DDSP_BUILD_STATION);
164 VpSetPlaceSizingLimit(_settings_game.station.station_spread);
165 } else {
166 /* Tile where we can't build rail waypoints. This is always going to fail,
167 * but provides the user with a proper error message. */
168 DoCommandP(tile, 1 << 8 | 1 << 16, STAT_CLASS_WAYP | INVALID_STATION << 16, CMD_BUILD_RAIL_WAYPOINT | CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_WAYPOINT));
172 void CcStation(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
174 if (result.Failed()) return;
176 if (_settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_RAIL, tile);
177 /* Only close the station builder window if the default station and non persistent building is chosen. */
178 if (_railstation.station_class == STAT_CLASS_DFLT && _railstation.station_type == 0 && !_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
182 * Place a rail station.
183 * @param tile Position to place or start dragging a station.
185 static void PlaceRail_Station(TileIndex tile)
187 if (_remove_button_clicked) {
188 VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED, DDSP_REMOVE_STATION);
189 VpSetPlaceSizingLimit(-1);
190 } else if (_settings_client.gui.station_dragdrop) {
191 VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED, DDSP_BUILD_STATION);
192 VpSetPlaceSizingLimit(_settings_game.station.station_spread);
193 } else {
194 uint32 p1 = _cur_railtype | _railstation.orientation << 6 | _settings_client.gui.station_numtracks << 8 | _settings_client.gui.station_platlength << 16 | _ctrl_pressed << 24;
195 uint32 p2 = _railstation.station_class | _railstation.station_type << 8 | INVALID_STATION << 16;
197 int w = _settings_client.gui.station_numtracks;
198 int h = _settings_client.gui.station_platlength;
199 if (!_railstation.orientation) Swap(w, h);
201 CommandContainer cmdcont = { tile, p1, p2, CMD_BUILD_RAIL_STATION | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION), CcStation, "" };
202 ShowSelectStationIfNeeded(cmdcont, TileArea(tile, w, h));
207 * Build a new signal or edit/remove a present signal, use CmdBuildSingleSignal() or CmdRemoveSingleSignal() in rail_cmd.cpp
209 * @param tile The tile where the signal will build or edit
211 static void GenericPlaceSignals(TileIndex tile)
213 TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_RAIL, 0));
215 if (trackbits & TRACK_BIT_VERT) { // N-S direction
216 trackbits = (_tile_fract_coords.x <= _tile_fract_coords.y) ? TRACK_BIT_RIGHT : TRACK_BIT_LEFT;
219 if (trackbits & TRACK_BIT_HORZ) { // E-W direction
220 trackbits = (_tile_fract_coords.x + _tile_fract_coords.y <= 15) ? TRACK_BIT_UPPER : TRACK_BIT_LOWER;
223 Track track = FindFirstTrack(trackbits);
225 if (_remove_button_clicked) {
226 DoCommandP(tile, track, 0, CMD_REMOVE_SIGNALS | CMD_MSG(STR_ERROR_CAN_T_REMOVE_SIGNALS_FROM), CcPlaySound_SPLAT_RAIL);
227 } else {
228 const Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
230 /* Map the setting cycle_signal_types to the lower and upper allowed signal type. */
231 static const uint cycle_bounds[] = {SIGTYPE_NORMAL | (SIGTYPE_LAST_NOPBS << 3), SIGTYPE_PBS | (SIGTYPE_LAST << 3), SIGTYPE_NORMAL | (SIGTYPE_LAST << 3)};
233 /* various bitstuffed elements for CmdBuildSingleSignal() */
234 uint32 p1 = track;
236 if (w != nullptr) {
237 /* signal GUI is used */
238 SB(p1, 3, 1, _ctrl_pressed);
239 SB(p1, 4, 1, _cur_signal_variant);
240 SB(p1, 5, 3, _cur_signal_type);
241 SB(p1, 8, 1, _convert_signal_button);
242 SB(p1, 9, 6, cycle_bounds[_settings_client.gui.cycle_signal_types]);
243 } else {
244 SB(p1, 3, 1, _ctrl_pressed);
245 SB(p1, 4, 1, (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC));
246 SB(p1, 5, 3, _default_signal_type[_settings_client.gui.default_signal_type]);
247 SB(p1, 8, 1, 0);
248 SB(p1, 9, 6, cycle_bounds[_settings_client.gui.cycle_signal_types]);
251 DoCommandP(tile, p1, 0, CMD_BUILD_SIGNALS |
252 CMD_MSG((w != nullptr && _convert_signal_button) ? STR_ERROR_SIGNAL_CAN_T_CONVERT_SIGNALS_HERE : STR_ERROR_CAN_T_BUILD_SIGNALS_HERE),
253 CcPlaySound_SPLAT_RAIL);
258 * Start placing a rail bridge.
259 * @param tile Position of the first tile of the bridge.
260 * @param w Rail toolbar window.
262 static void PlaceRail_Bridge(TileIndex tile, Window *w)
264 if (IsBridgeTile(tile)) {
265 TileIndex other_tile = GetOtherTunnelBridgeEnd(tile);
266 Point pt = {0, 0};
267 w->OnPlaceMouseUp(VPM_X_OR_Y, DDSP_BUILD_BRIDGE, pt, other_tile, tile);
268 } else {
269 VpStartPlaceSizing(tile, VPM_X_OR_Y, DDSP_BUILD_BRIDGE);
273 /** Command callback for building a tunnel */
274 void CcBuildRailTunnel(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
276 if (result.Succeeded()) {
277 if (_settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_RAIL, tile);
278 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
279 } else {
280 SetRedErrorSquare(_build_tunnel_endtile);
285 * Toggles state of the Remove button of Build rail toolbar
286 * @param w window the button belongs to
288 static void ToggleRailButton_Remove(Window *w)
290 DeleteWindowById(WC_SELECT_STATION, 0);
291 w->ToggleWidgetLoweredState(WID_RAT_REMOVE);
292 w->SetWidgetDirty(WID_RAT_REMOVE);
293 _remove_button_clicked = w->IsWidgetLowered(WID_RAT_REMOVE);
294 SetSelectionRed(_remove_button_clicked);
298 * Updates the Remove button because of Ctrl state change
299 * @param w window the button belongs to
300 * @return true iff the remove button was changed
302 static bool RailToolbar_CtrlChanged(Window *w)
304 if (w->IsWidgetDisabled(WID_RAT_REMOVE)) return false;
306 /* allow ctrl to switch remove mode only for these widgets */
307 for (uint i = WID_RAT_BUILD_NS; i <= WID_RAT_BUILD_STATION; i++) {
308 if ((i <= WID_RAT_AUTORAIL || i >= WID_RAT_BUILD_WAYPOINT) && w->IsWidgetLowered(i)) {
309 ToggleRailButton_Remove(w);
310 return true;
314 return false;
319 * The "remove"-button click proc of the build-rail toolbar.
320 * @param w Build-rail toolbar window
321 * @see BuildRailToolbarWindow::OnClick()
323 static void BuildRailClick_Remove(Window *w)
325 if (w->IsWidgetDisabled(WID_RAT_REMOVE)) return;
326 ToggleRailButton_Remove(w);
327 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
329 /* handle station builder */
330 if (w->IsWidgetLowered(WID_RAT_BUILD_STATION)) {
331 if (_remove_button_clicked) {
332 /* starting drag & drop remove */
333 if (!_settings_client.gui.station_dragdrop) {
334 SetTileSelectSize(1, 1);
335 } else {
336 VpSetPlaceSizingLimit(-1);
338 } else {
339 /* starting station build mode */
340 if (!_settings_client.gui.station_dragdrop) {
341 int x = _settings_client.gui.station_numtracks;
342 int y = _settings_client.gui.station_platlength;
343 if (_railstation.orientation == 0) Swap(x, y);
344 SetTileSelectSize(x, y);
345 } else {
346 VpSetPlaceSizingLimit(_settings_game.station.station_spread);
352 static void DoRailroadTrack(int mode)
354 uint32 p2 = _cur_railtype | (mode << 6) | (_settings_client.gui.auto_remove_signals << 11);
355 DoCommandP(TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y), p2,
356 _remove_button_clicked ?
357 CMD_REMOVE_RAILROAD_TRACK | CMD_MSG(STR_ERROR_CAN_T_REMOVE_RAILROAD_TRACK) :
358 CMD_BUILD_RAILROAD_TRACK | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_TRACK),
359 CcPlaySound_SPLAT_RAIL);
362 static void HandleAutodirPlacement()
364 int trackstat = _thd.drawstyle & HT_DIR_MASK; // 0..5
366 if (_thd.drawstyle & HT_RAIL) { // one tile case
367 GenericPlaceRail(TileVirtXY(_thd.selend.x, _thd.selend.y), trackstat);
368 return;
371 DoRailroadTrack(trackstat);
375 * Build new signals or remove signals or (if only one tile marked) edit a signal.
377 * If one tile marked abort and use GenericPlaceSignals()
378 * else use CmdBuildSingleSignal() or CmdRemoveSingleSignal() in rail_cmd.cpp to build many signals
380 static void HandleAutoSignalPlacement()
382 uint32 p2 = GB(_thd.drawstyle, 0, 3); // 0..5
384 if ((_thd.drawstyle & HT_DRAG_MASK) == HT_RECT) { // one tile case
385 GenericPlaceSignals(TileVirtXY(_thd.selend.x, _thd.selend.y));
386 return;
389 const Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
391 if (w != nullptr) {
392 /* signal GUI is used */
393 SB(p2, 3, 1, 0);
394 SB(p2, 4, 1, _cur_signal_variant);
395 SB(p2, 6, 1, _ctrl_pressed);
396 SB(p2, 7, 3, _cur_signal_type);
397 SB(p2, 24, 8, _settings_client.gui.drag_signals_density);
398 SB(p2, 10, 1, !_settings_client.gui.drag_signals_fixed_distance);
399 } else {
400 SB(p2, 3, 1, 0);
401 SB(p2, 4, 1, (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC));
402 SB(p2, 6, 1, _ctrl_pressed);
403 SB(p2, 7, 3, _default_signal_type[_settings_client.gui.default_signal_type]);
404 SB(p2, 24, 8, _settings_client.gui.drag_signals_density);
405 SB(p2, 10, 1, !_settings_client.gui.drag_signals_fixed_distance);
408 /* _settings_client.gui.drag_signals_density is given as a parameter such that each user
409 * in a network game can specify his/her own signal density */
410 DoCommandP(TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y), p2,
411 _remove_button_clicked ?
412 CMD_REMOVE_SIGNAL_TRACK | CMD_MSG(STR_ERROR_CAN_T_REMOVE_SIGNALS_FROM) :
413 CMD_BUILD_SIGNAL_TRACK | CMD_MSG(STR_ERROR_CAN_T_BUILD_SIGNALS_HERE),
414 CcPlaySound_SPLAT_RAIL);
418 /** Rail toolbar management class. */
419 struct BuildRailToolbarWindow : Window {
420 RailType railtype; ///< Rail type to build.
421 int last_user_action; ///< Last started user action.
423 BuildRailToolbarWindow(WindowDesc *desc, RailType railtype) : Window(desc)
425 this->InitNested(TRANSPORT_RAIL);
426 this->SetupRailToolbar(railtype);
427 this->DisableWidget(WID_RAT_REMOVE);
428 this->last_user_action = WIDGET_LIST_END;
430 if (_settings_client.gui.link_terraform_toolbar) ShowTerraformToolbar(this);
433 ~BuildRailToolbarWindow()
435 if (this->IsWidgetLowered(WID_RAT_BUILD_STATION)) SetViewportCatchmentStation(nullptr, true);
436 if (_settings_client.gui.link_terraform_toolbar) DeleteWindowById(WC_SCEN_LAND_GEN, 0, false);
440 * Configures the rail toolbar for railtype given
441 * @param railtype the railtype to display
443 void SetupRailToolbar(RailType railtype)
445 this->railtype = railtype;
446 const RailtypeInfo *rti = GetRailTypeInfo(railtype);
448 assert(railtype < RAILTYPE_END);
449 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_NS)->widget_data = rti->gui_sprites.build_ns_rail;
450 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_X)->widget_data = rti->gui_sprites.build_x_rail;
451 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_EW)->widget_data = rti->gui_sprites.build_ew_rail;
452 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_Y)->widget_data = rti->gui_sprites.build_y_rail;
453 this->GetWidget<NWidgetCore>(WID_RAT_AUTORAIL)->widget_data = rti->gui_sprites.auto_rail;
454 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_DEPOT)->widget_data = rti->gui_sprites.build_depot;
455 this->GetWidget<NWidgetCore>(WID_RAT_CONVERT_RAIL)->widget_data = rti->gui_sprites.convert_rail;
456 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_TUNNEL)->widget_data = rti->gui_sprites.build_tunnel;
460 * Switch to another rail type.
461 * @param railtype New rail type.
463 void ModifyRailType(RailType railtype)
465 this->SetupRailToolbar(railtype);
466 this->ReInit();
469 void UpdateRemoveWidgetStatus(int clicked_widget)
471 switch (clicked_widget) {
472 case WID_RAT_REMOVE:
473 /* If it is the removal button that has been clicked, do nothing,
474 * as it is up to the other buttons to drive removal status */
475 return;
477 case WID_RAT_BUILD_NS:
478 case WID_RAT_BUILD_X:
479 case WID_RAT_BUILD_EW:
480 case WID_RAT_BUILD_Y:
481 case WID_RAT_AUTORAIL:
482 case WID_RAT_BUILD_WAYPOINT:
483 case WID_RAT_BUILD_STATION:
484 case WID_RAT_BUILD_SIGNALS:
485 /* Removal button is enabled only if the rail/signal/waypoint/station
486 * button is still lowered. Once raised, it has to be disabled */
487 this->SetWidgetDisabledState(WID_RAT_REMOVE, !this->IsWidgetLowered(clicked_widget));
488 break;
490 default:
491 /* When any other buttons than rail/signal/waypoint/station, raise and
492 * disable the removal button */
493 this->DisableWidget(WID_RAT_REMOVE);
494 this->RaiseWidget(WID_RAT_REMOVE);
495 break;
499 void SetStringParameters(int widget) const override
501 if (widget == WID_RAT_CAPTION) {
502 const RailtypeInfo *rti = GetRailTypeInfo(this->railtype);
503 if (rti->max_speed > 0) {
504 SetDParam(0, STR_TOOLBAR_RAILTYPE_VELOCITY);
505 SetDParam(1, rti->strings.toolbar_caption);
506 SetDParam(2, rti->max_speed);
507 } else {
508 SetDParam(0, rti->strings.toolbar_caption);
513 void OnClick(Point pt, int widget, int click_count) override
515 if (widget < WID_RAT_BUILD_NS) return;
517 _remove_button_clicked = false;
518 switch (widget) {
519 case WID_RAT_BUILD_NS:
520 HandlePlacePushButton(this, WID_RAT_BUILD_NS, GetRailTypeInfo(_cur_railtype)->cursor.rail_ns, HT_LINE | HT_DIR_VL);
521 this->last_user_action = widget;
522 break;
524 case WID_RAT_BUILD_X:
525 HandlePlacePushButton(this, WID_RAT_BUILD_X, GetRailTypeInfo(_cur_railtype)->cursor.rail_swne, HT_LINE | HT_DIR_X);
526 this->last_user_action = widget;
527 break;
529 case WID_RAT_BUILD_EW:
530 HandlePlacePushButton(this, WID_RAT_BUILD_EW, GetRailTypeInfo(_cur_railtype)->cursor.rail_ew, HT_LINE | HT_DIR_HL);
531 this->last_user_action = widget;
532 break;
534 case WID_RAT_BUILD_Y:
535 HandlePlacePushButton(this, WID_RAT_BUILD_Y, GetRailTypeInfo(_cur_railtype)->cursor.rail_nwse, HT_LINE | HT_DIR_Y);
536 this->last_user_action = widget;
537 break;
539 case WID_RAT_AUTORAIL:
540 HandlePlacePushButton(this, WID_RAT_AUTORAIL, GetRailTypeInfo(_cur_railtype)->cursor.autorail, HT_RAIL);
541 this->last_user_action = widget;
542 break;
544 case WID_RAT_DEMOLISH:
545 HandlePlacePushButton(this, WID_RAT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
546 this->last_user_action = widget;
547 break;
549 case WID_RAT_BUILD_DEPOT:
550 if (HandlePlacePushButton(this, WID_RAT_BUILD_DEPOT, GetRailTypeInfo(_cur_railtype)->cursor.depot, HT_RECT)) {
551 ShowBuildTrainDepotPicker(this);
552 this->last_user_action = widget;
554 break;
556 case WID_RAT_BUILD_WAYPOINT:
557 this->last_user_action = widget;
558 _waypoint_count = StationClass::Get(STAT_CLASS_WAYP)->GetSpecCount();
559 if (HandlePlacePushButton(this, WID_RAT_BUILD_WAYPOINT, SPR_CURSOR_WAYPOINT, HT_RECT) && _waypoint_count > 1) {
560 ShowBuildWaypointPicker(this);
562 break;
564 case WID_RAT_BUILD_STATION:
565 if (HandlePlacePushButton(this, WID_RAT_BUILD_STATION, SPR_CURSOR_RAIL_STATION, HT_RECT)) {
566 ShowStationBuilder(this);
567 this->last_user_action = widget;
569 break;
571 case WID_RAT_BUILD_SIGNALS: {
572 this->last_user_action = widget;
573 bool started = HandlePlacePushButton(this, WID_RAT_BUILD_SIGNALS, ANIMCURSOR_BUILDSIGNALS, HT_RECT);
574 if (started && _settings_client.gui.enable_signal_gui != _ctrl_pressed) {
575 ShowSignalBuilder(this);
577 break;
580 case WID_RAT_BUILD_BRIDGE:
581 HandlePlacePushButton(this, WID_RAT_BUILD_BRIDGE, SPR_CURSOR_BRIDGE, HT_RECT);
582 this->last_user_action = widget;
583 break;
585 case WID_RAT_BUILD_TUNNEL:
586 HandlePlacePushButton(this, WID_RAT_BUILD_TUNNEL, GetRailTypeInfo(_cur_railtype)->cursor.tunnel, HT_SPECIAL);
587 this->last_user_action = widget;
588 break;
590 case WID_RAT_REMOVE:
591 BuildRailClick_Remove(this);
592 break;
594 case WID_RAT_CONVERT_RAIL:
595 HandlePlacePushButton(this, WID_RAT_CONVERT_RAIL, GetRailTypeInfo(_cur_railtype)->cursor.convert, HT_RECT | HT_DIAGONAL);
596 this->last_user_action = widget;
597 break;
599 default: NOT_REACHED();
601 this->UpdateRemoveWidgetStatus(widget);
602 if (_ctrl_pressed) RailToolbar_CtrlChanged(this);
605 EventState OnHotkey(int hotkey) override
607 MarkTileDirtyByTile(TileVirtXY(_thd.pos.x, _thd.pos.y)); // redraw tile selection
608 return Window::OnHotkey(hotkey);
611 void OnPlaceObject(Point pt, TileIndex tile) override
613 switch (this->last_user_action) {
614 case WID_RAT_BUILD_NS:
615 VpStartPlaceSizing(tile, VPM_FIX_VERTICAL | VPM_RAILDIRS, DDSP_PLACE_RAIL);
616 break;
618 case WID_RAT_BUILD_X:
619 VpStartPlaceSizing(tile, VPM_FIX_Y | VPM_RAILDIRS, DDSP_PLACE_RAIL);
620 break;
622 case WID_RAT_BUILD_EW:
623 VpStartPlaceSizing(tile, VPM_FIX_HORIZONTAL | VPM_RAILDIRS, DDSP_PLACE_RAIL);
624 break;
626 case WID_RAT_BUILD_Y:
627 VpStartPlaceSizing(tile, VPM_FIX_X | VPM_RAILDIRS, DDSP_PLACE_RAIL);
628 break;
630 case WID_RAT_AUTORAIL:
631 VpStartPlaceSizing(tile, VPM_RAILDIRS, DDSP_PLACE_RAIL);
632 break;
634 case WID_RAT_DEMOLISH:
635 PlaceProc_DemolishArea(tile);
636 break;
638 case WID_RAT_BUILD_DEPOT:
639 DoCommandP(tile, _cur_railtype, _build_depot_direction,
640 CMD_BUILD_TRAIN_DEPOT | CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_DEPOT),
641 CcRailDepot);
642 break;
644 case WID_RAT_BUILD_WAYPOINT:
645 PlaceRail_Waypoint(tile);
646 break;
648 case WID_RAT_BUILD_STATION:
649 PlaceRail_Station(tile);
650 break;
652 case WID_RAT_BUILD_SIGNALS:
653 VpStartPlaceSizing(tile, VPM_SIGNALDIRS, DDSP_BUILD_SIGNALS);
654 break;
656 case WID_RAT_BUILD_BRIDGE:
657 PlaceRail_Bridge(tile, this);
658 break;
660 case WID_RAT_BUILD_TUNNEL:
661 DoCommandP(tile, _cur_railtype | (TRANSPORT_RAIL << 8), 0, CMD_BUILD_TUNNEL | CMD_MSG(STR_ERROR_CAN_T_BUILD_TUNNEL_HERE), CcBuildRailTunnel);
662 break;
664 case WID_RAT_CONVERT_RAIL:
665 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CONVERT_RAIL);
666 break;
668 default: NOT_REACHED();
672 void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) override
674 /* no dragging if you have pressed the convert button */
675 if (FindWindowById(WC_BUILD_SIGNAL, 0) != nullptr && _convert_signal_button && this->IsWidgetLowered(WID_RAT_BUILD_SIGNALS)) return;
677 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
680 void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) override
682 if (pt.x != -1) {
683 switch (select_proc) {
684 default: NOT_REACHED();
685 case DDSP_BUILD_BRIDGE:
686 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
687 ShowBuildBridgeWindow(start_tile, end_tile, TRANSPORT_RAIL, _cur_railtype);
688 break;
690 case DDSP_PLACE_RAIL:
691 HandleAutodirPlacement();
692 break;
694 case DDSP_BUILD_SIGNALS:
695 HandleAutoSignalPlacement();
696 break;
698 case DDSP_DEMOLISH_AREA:
699 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
700 break;
702 case DDSP_CONVERT_RAIL:
703 DoCommandP(end_tile, start_tile, _cur_railtype | (_ctrl_pressed ? 1 << 6 : 0), CMD_CONVERT_RAIL | CMD_MSG(STR_ERROR_CAN_T_CONVERT_RAIL), CcPlaySound_SPLAT_RAIL);
704 break;
706 case DDSP_REMOVE_STATION:
707 case DDSP_BUILD_STATION:
708 if (this->IsWidgetLowered(WID_RAT_BUILD_STATION)) {
709 /* Station */
710 if (_remove_button_clicked) {
711 DoCommandP(end_tile, start_tile, _ctrl_pressed ? 0 : 1, CMD_REMOVE_FROM_RAIL_STATION | CMD_MSG(STR_ERROR_CAN_T_REMOVE_PART_OF_STATION), CcPlaySound_SPLAT_RAIL);
712 } else {
713 HandleStationPlacement(start_tile, end_tile);
715 } else {
716 /* Waypoint */
717 if (_remove_button_clicked) {
718 DoCommandP(end_tile, start_tile, _ctrl_pressed ? 0 : 1, CMD_REMOVE_FROM_RAIL_WAYPOINT | CMD_MSG(STR_ERROR_CAN_T_REMOVE_TRAIN_WAYPOINT), CcPlaySound_SPLAT_RAIL);
719 } else {
720 TileArea ta(start_tile, end_tile);
721 uint32 p1 = _cur_railtype | (select_method == VPM_X_LIMITED ? AXIS_X : AXIS_Y) << 6 | ta.w << 8 | ta.h << 16 | _ctrl_pressed << 24;
722 uint32 p2 = STAT_CLASS_WAYP | _cur_waypoint_type << 8 | INVALID_STATION << 16;
724 CommandContainer cmdcont = { ta.tile, p1, p2, CMD_BUILD_RAIL_WAYPOINT | CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_WAYPOINT), CcPlaySound_SPLAT_RAIL, "" };
725 ShowSelectWaypointIfNeeded(cmdcont, ta);
728 break;
733 void OnPlaceObjectAbort() override
735 if (this->IsWidgetLowered(WID_RAT_BUILD_STATION)) SetViewportCatchmentStation(nullptr, true);
737 this->RaiseButtons();
738 this->DisableWidget(WID_RAT_REMOVE);
739 this->SetWidgetDirty(WID_RAT_REMOVE);
741 DeleteWindowById(WC_BUILD_SIGNAL, TRANSPORT_RAIL);
742 DeleteWindowById(WC_BUILD_STATION, TRANSPORT_RAIL);
743 DeleteWindowById(WC_BUILD_DEPOT, TRANSPORT_RAIL);
744 DeleteWindowById(WC_BUILD_WAYPOINT, TRANSPORT_RAIL);
745 DeleteWindowById(WC_SELECT_STATION, 0);
746 DeleteWindowByClass(WC_BUILD_BRIDGE);
749 void OnPlacePresize(Point pt, TileIndex tile) override
751 DoCommand(tile, _cur_railtype | (TRANSPORT_RAIL << 8), 0, DC_AUTO, CMD_BUILD_TUNNEL);
752 VpSetPresizeRange(tile, _build_tunnel_endtile == 0 ? tile : _build_tunnel_endtile);
755 EventState OnCTRLStateChange() override
757 /* do not toggle Remove button by Ctrl when placing station */
758 if (!this->IsWidgetLowered(WID_RAT_BUILD_STATION) && !this->IsWidgetLowered(WID_RAT_BUILD_WAYPOINT) && RailToolbar_CtrlChanged(this)) return ES_HANDLED;
759 return ES_NOT_HANDLED;
762 static HotkeyList hotkeys;
766 * Handler for global hotkeys of the BuildRailToolbarWindow.
767 * @param hotkey Hotkey
768 * @return ES_HANDLED if hotkey was accepted.
770 static EventState RailToolbarGlobalHotkeys(int hotkey)
772 if (_game_mode != GM_NORMAL) return ES_NOT_HANDLED;
773 extern RailType _last_built_railtype;
774 Window *w = ShowBuildRailToolbar(_last_built_railtype);
775 if (w == nullptr) return ES_NOT_HANDLED;
776 return w->OnHotkey(hotkey);
779 const uint16 _railtoolbar_autorail_keys[] = {'5', 'A' | WKC_GLOBAL_HOTKEY, 0};
781 static Hotkey railtoolbar_hotkeys[] = {
782 Hotkey('1', "build_ns", WID_RAT_BUILD_NS),
783 Hotkey('2', "build_x", WID_RAT_BUILD_X),
784 Hotkey('3', "build_ew", WID_RAT_BUILD_EW),
785 Hotkey('4', "build_y", WID_RAT_BUILD_Y),
786 Hotkey(_railtoolbar_autorail_keys, "autorail", WID_RAT_AUTORAIL),
787 Hotkey('6', "demolish", WID_RAT_DEMOLISH),
788 Hotkey('7', "depot", WID_RAT_BUILD_DEPOT),
789 Hotkey('8', "waypoint", WID_RAT_BUILD_WAYPOINT),
790 Hotkey('9', "station", WID_RAT_BUILD_STATION),
791 Hotkey('S', "signal", WID_RAT_BUILD_SIGNALS),
792 Hotkey('B', "bridge", WID_RAT_BUILD_BRIDGE),
793 Hotkey('T', "tunnel", WID_RAT_BUILD_TUNNEL),
794 Hotkey('R', "remove", WID_RAT_REMOVE),
795 Hotkey('C', "convert", WID_RAT_CONVERT_RAIL),
796 HOTKEY_LIST_END
798 HotkeyList BuildRailToolbarWindow::hotkeys("railtoolbar", railtoolbar_hotkeys, RailToolbarGlobalHotkeys);
800 static const NWidgetPart _nested_build_rail_widgets[] = {
801 NWidget(NWID_HORIZONTAL),
802 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
803 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_RAT_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
804 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
805 EndContainer(),
806 NWidget(NWID_HORIZONTAL),
807 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_NS),
808 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NS, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
809 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_X),
810 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NE, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
811 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_EW),
812 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_EW, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
813 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_Y),
814 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NW, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
815 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_AUTORAIL),
816 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_AUTORAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_AUTORAIL),
818 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), SetDataTip(0x0, STR_NULL), EndContainer(),
820 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_DEMOLISH),
821 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
822 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_DEPOT),
823 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DEPOT_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_TRAIN_DEPOT_FOR_BUILDING),
824 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_WAYPOINT),
825 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_WAYPOINT, STR_RAIL_TOOLBAR_TOOLTIP_CONVERT_RAIL_TO_WAYPOINT),
826 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_STATION),
827 SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_RAIL_STATION, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_STATION),
828 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_SIGNALS),
829 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_SIGNALS, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_SIGNALS),
830 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_BRIDGE),
831 SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_BRIDGE, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_BRIDGE),
832 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_TUNNEL),
833 SetFill(0, 1), SetMinimalSize(20, 22), SetDataTip(SPR_IMG_TUNNEL_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TUNNEL),
834 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_REMOVE),
835 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_REMOVE, STR_RAIL_TOOLBAR_TOOLTIP_TOGGLE_BUILD_REMOVE_FOR),
836 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_CONVERT_RAIL),
837 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_CONVERT_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_CONVERT_RAIL),
838 EndContainer(),
841 static WindowDesc _build_rail_desc(
842 WDP_ALIGN_TOOLBAR, "toolbar_rail", 0, 0,
843 WC_BUILD_TOOLBAR, WC_NONE,
844 WDF_CONSTRUCTION,
845 _nested_build_rail_widgets, lengthof(_nested_build_rail_widgets),
846 &BuildRailToolbarWindow::hotkeys
851 * Open the build rail toolbar window for a specific rail type.
853 * If the terraform toolbar is linked to the toolbar, that window is also opened.
855 * @param railtype Rail type to open the window for
856 * @return newly opened rail toolbar, or nullptr if the toolbar could not be opened.
858 Window *ShowBuildRailToolbar(RailType railtype)
860 if (!Company::IsValidID(_local_company)) return nullptr;
861 if (!ValParamRailtype(railtype)) return nullptr;
863 DeleteWindowByClass(WC_BUILD_TOOLBAR);
864 _cur_railtype = railtype;
865 _remove_button_clicked = false;
866 return new BuildRailToolbarWindow(&_build_rail_desc, railtype);
869 /* TODO: For custom stations, respect their allowed platforms/lengths bitmasks!
870 * --pasky */
872 static void HandleStationPlacement(TileIndex start, TileIndex end)
874 TileArea ta(start, end);
875 uint numtracks = ta.w;
876 uint platlength = ta.h;
878 if (_railstation.orientation == AXIS_X) Swap(numtracks, platlength);
880 uint32 p1 = _cur_railtype | _railstation.orientation << 6 | numtracks << 8 | platlength << 16 | _ctrl_pressed << 24;
881 uint32 p2 = _railstation.station_class | _railstation.station_type << 8 | INVALID_STATION << 16;
883 CommandContainer cmdcont = { ta.tile, p1, p2, CMD_BUILD_RAIL_STATION | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION), CcStation, "" };
884 ShowSelectStationIfNeeded(cmdcont, ta);
887 struct BuildRailStationWindow : public PickerWindowBase {
888 private:
889 uint line_height; ///< Height of a single line in the newstation selection matrix (#WID_BRAS_NEWST_LIST widget).
890 uint coverage_height; ///< Height of the coverage texts.
891 Scrollbar *vscroll; ///< Vertical scrollbar of the new station list.
892 Scrollbar *vscroll2; ///< Vertical scrollbar of the matrix with new stations.
895 * Verify whether the currently selected station size is allowed after selecting a new station class/type.
896 * If not, change the station size variables ( _settings_client.gui.station_numtracks and _settings_client.gui.station_platlength ).
897 * @param statspec Specification of the new station class/type
899 void CheckSelectedSize(const StationSpec *statspec)
901 if (statspec == nullptr || _settings_client.gui.station_dragdrop) return;
903 /* If current number of tracks is not allowed, make it as big as possible */
904 if (HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
905 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
906 _settings_client.gui.station_numtracks = 1;
907 if (statspec->disallowed_platforms != UINT8_MAX) {
908 while (HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
909 _settings_client.gui.station_numtracks++;
911 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
915 if (HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
916 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
917 _settings_client.gui.station_platlength = 1;
918 if (statspec->disallowed_lengths != UINT8_MAX) {
919 while (HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
920 _settings_client.gui.station_platlength++;
922 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
927 public:
928 BuildRailStationWindow(WindowDesc *desc, Window *parent, bool newstation) : PickerWindowBase(desc, parent)
930 this->coverage_height = 2 * FONT_HEIGHT_NORMAL + 3 * WD_PAR_VSEP_NORMAL;
931 this->vscroll = nullptr;
932 _railstation.newstations = newstation;
934 this->CreateNestedTree();
935 NWidgetStacked *newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_ADDITIONS);
936 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
937 newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_MATRIX);
938 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
939 newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_DEFSIZE);
940 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
941 newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_RESIZE);
942 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
943 if (newstation) {
944 this->vscroll = this->GetScrollbar(WID_BRAS_NEWST_SCROLL);
945 this->vscroll2 = this->GetScrollbar(WID_BRAS_MATRIX_SCROLL);
947 this->FinishInitNested(TRANSPORT_RAIL);
949 this->LowerWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
950 if (_settings_client.gui.station_dragdrop) {
951 this->LowerWidget(WID_BRAS_PLATFORM_DRAG_N_DROP);
952 } else {
953 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
954 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
956 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_OFF, !_settings_client.gui.station_show_coverage);
957 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_ON, _settings_client.gui.station_show_coverage);
959 if (!newstation || _railstation.station_class >= (int)StationClass::GetClassCount()) {
960 /* New stations are not available or changed, so ensure the default station
961 * type is 'selected'. */
962 _railstation.station_class = STAT_CLASS_DFLT;
963 _railstation.station_type = 0;
964 this->vscroll2 = nullptr;
966 if (newstation) {
967 _railstation.station_count = StationClass::Get(_railstation.station_class)->GetSpecCount();
968 _railstation.station_type = std::min<int>(_railstation.station_type, _railstation.station_count - 1);
970 int count = 0;
971 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
972 if (i == STAT_CLASS_WAYP) continue;
973 count++;
975 this->vscroll->SetCount(count);
976 this->vscroll->SetPosition(Clamp(_railstation.station_class - 2, 0, std::max(this->vscroll->GetCount() - this->vscroll->GetCapacity(), 0)));
978 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX);
979 matrix->SetScrollbar(this->vscroll2);
980 matrix->SetCount(_railstation.station_count);
981 matrix->SetClicked(_railstation.station_type);
985 virtual ~BuildRailStationWindow()
987 DeleteWindowById(WC_SELECT_STATION, 0);
990 void OnPaint() override
992 bool newstations = _railstation.newstations;
993 const StationSpec *statspec = newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : nullptr;
995 if (_settings_client.gui.station_dragdrop) {
996 SetTileSelectSize(1, 1);
997 } else {
998 int x = _settings_client.gui.station_numtracks;
999 int y = _settings_client.gui.station_platlength;
1000 if (_railstation.orientation == AXIS_X) Swap(x, y);
1001 if (!_remove_button_clicked) {
1002 SetTileSelectSize(x, y);
1006 int rad = (_settings_game.station.modified_catchment) ? CA_TRAIN : CA_UNMODIFIED;
1008 if (_settings_client.gui.station_show_coverage) SetTileSelectBigSize(-rad, -rad, 2 * rad, 2 * rad);
1010 for (uint bits = 0; bits < 7; bits++) {
1011 bool disable = bits >= _settings_game.station.station_spread;
1012 if (statspec == nullptr) {
1013 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_NUM_1, disable);
1014 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_LEN_1, disable);
1015 } else {
1016 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_NUM_1, HasBit(statspec->disallowed_platforms, bits) || disable);
1017 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_LEN_1, HasBit(statspec->disallowed_lengths, bits) || disable);
1021 this->DrawWidgets();
1023 if (this->IsShaded()) return;
1024 /* 'Accepts' and 'Supplies' texts. */
1025 NWidgetBase *cov = this->GetWidget<NWidgetBase>(WID_BRAS_COVERAGE_TEXTS);
1026 int top = cov->pos_y + WD_PAR_VSEP_NORMAL;
1027 int left = cov->pos_x + WD_FRAMERECT_LEFT;
1028 int right = cov->pos_x + cov->current_x - WD_FRAMERECT_RIGHT;
1029 int bottom = cov->pos_y + cov->current_y;
1030 top = DrawStationCoverageAreaText(left, right, top, SCT_ALL, rad, false) + WD_PAR_VSEP_NORMAL;
1031 top = DrawStationCoverageAreaText(left, right, top, SCT_ALL, rad, true) + WD_PAR_VSEP_NORMAL;
1032 /* Resize background if the window is too small.
1033 * Never make the window smaller to avoid oscillating if the size change affects the acceptance.
1034 * (This is the case, if making the window bigger moves the mouse into the window.) */
1035 if (top > bottom) {
1036 this->coverage_height += top - bottom;
1037 this->ReInit();
1041 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1043 switch (widget) {
1044 case WID_BRAS_NEWST_LIST: {
1045 Dimension d = {0, 0};
1046 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1047 if (i == STAT_CLASS_WAYP) continue;
1048 d = maxdim(d, GetStringBoundingBox(StationClass::Get((StationClassID)i)->name));
1050 size->width = std::max(size->width, d.width + padding.width);
1051 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
1052 size->height = 5 * this->line_height;
1053 resize->height = this->line_height;
1054 break;
1057 case WID_BRAS_SHOW_NEWST_TYPE: {
1058 if (!_railstation.newstations) {
1059 size->width = 0;
1060 size->height = 0;
1061 break;
1064 /* If newstations exist, compute the non-zero minimal size. */
1065 Dimension d = {0, 0};
1066 StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
1067 for (StationClassID statclass = STAT_CLASS_BEGIN; statclass < (StationClassID)StationClass::GetClassCount(); statclass++) {
1068 if (statclass == STAT_CLASS_WAYP) continue;
1069 StationClass *stclass = StationClass::Get(statclass);
1070 for (uint16 j = 0; j < stclass->GetSpecCount(); j++) {
1071 const StationSpec *statspec = stclass->GetSpec(j);
1072 SetDParam(0, (statspec != nullptr && statspec->name != 0) ? statspec->name : STR_STATION_CLASS_DFLT);
1073 d = maxdim(d, GetStringBoundingBox(str));
1076 size->width = std::max(size->width, d.width + padding.width);
1077 break;
1080 case WID_BRAS_PLATFORM_DIR_X:
1081 case WID_BRAS_PLATFORM_DIR_Y:
1082 case WID_BRAS_IMAGE:
1083 size->width = ScaleGUITrad(64) + 2;
1084 size->height = ScaleGUITrad(58) + 2;
1085 break;
1087 case WID_BRAS_COVERAGE_TEXTS:
1088 size->height = this->coverage_height;
1089 break;
1091 case WID_BRAS_MATRIX:
1092 fill->height = 1;
1093 resize->height = 1;
1094 break;
1098 void DrawWidget(const Rect &r, int widget) const override
1100 DrawPixelInfo tmp_dpi;
1102 switch (GB(widget, 0, 16)) {
1103 case WID_BRAS_PLATFORM_DIR_X:
1104 /* Set up a clipping area for the '/' station preview */
1105 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1106 DrawPixelInfo *old_dpi = _cur_dpi;
1107 _cur_dpi = &tmp_dpi;
1108 int x = ScaleGUITrad(31) + 1;
1109 int y = r.bottom - r.top - ScaleGUITrad(31);
1110 if (!DrawStationTile(x, y, _cur_railtype, AXIS_X, _railstation.station_class, _railstation.station_type)) {
1111 StationPickerDrawSprite(x, y, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2);
1113 _cur_dpi = old_dpi;
1115 break;
1117 case WID_BRAS_PLATFORM_DIR_Y:
1118 /* Set up a clipping area for the '\' station preview */
1119 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1120 DrawPixelInfo *old_dpi = _cur_dpi;
1121 _cur_dpi = &tmp_dpi;
1122 int x = ScaleGUITrad(31) + 1;
1123 int y = r.bottom - r.top - ScaleGUITrad(31);
1124 if (!DrawStationTile(x, y, _cur_railtype, AXIS_Y, _railstation.station_class, _railstation.station_type)) {
1125 StationPickerDrawSprite(x, y, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 3);
1127 _cur_dpi = old_dpi;
1129 break;
1131 case WID_BRAS_NEWST_LIST: {
1132 uint statclass = 0;
1133 uint row = 0;
1134 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1135 if (i == STAT_CLASS_WAYP) continue;
1136 if (this->vscroll->IsVisible(statclass)) {
1137 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, row * this->line_height + r.top + WD_MATRIX_TOP,
1138 StationClass::Get((StationClassID)i)->name,
1139 (StationClassID)i == _railstation.station_class ? TC_WHITE : TC_BLACK);
1140 row++;
1142 statclass++;
1144 break;
1147 case WID_BRAS_IMAGE: {
1148 byte type = GB(widget, 16, 16);
1149 assert(type < _railstation.station_count);
1150 /* Check station availability callback */
1151 const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(type);
1152 if (!IsStationAvailable(statspec)) {
1153 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
1156 /* Set up a clipping area for the station preview. */
1157 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1158 DrawPixelInfo *old_dpi = _cur_dpi;
1159 _cur_dpi = &tmp_dpi;
1160 int x = ScaleGUITrad(31) + 1;
1161 int y = r.bottom - r.top - ScaleGUITrad(31);
1162 if (!DrawStationTile(x, y, _cur_railtype, _railstation.orientation, _railstation.station_class, type)) {
1163 StationPickerDrawSprite(x, y, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2 + _railstation.orientation);
1165 _cur_dpi = old_dpi;
1167 break;
1172 void OnResize() override
1174 if (this->vscroll != nullptr) { // New stations available.
1175 this->vscroll->SetCapacityFromWidget(this, WID_BRAS_NEWST_LIST);
1179 void SetStringParameters(int widget) const override
1181 if (widget == WID_BRAS_SHOW_NEWST_TYPE) {
1182 const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type);
1183 SetDParam(0, (statspec != nullptr && statspec->name != 0) ? statspec->name : STR_STATION_CLASS_DFLT);
1187 void OnClick(Point pt, int widget, int click_count) override
1189 switch (GB(widget, 0, 16)) {
1190 case WID_BRAS_PLATFORM_DIR_X:
1191 case WID_BRAS_PLATFORM_DIR_Y:
1192 this->RaiseWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
1193 _railstation.orientation = (Axis)(widget - WID_BRAS_PLATFORM_DIR_X);
1194 this->LowerWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
1195 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1196 this->SetDirty();
1197 DeleteWindowById(WC_SELECT_STATION, 0);
1198 break;
1200 case WID_BRAS_PLATFORM_NUM_1:
1201 case WID_BRAS_PLATFORM_NUM_2:
1202 case WID_BRAS_PLATFORM_NUM_3:
1203 case WID_BRAS_PLATFORM_NUM_4:
1204 case WID_BRAS_PLATFORM_NUM_5:
1205 case WID_BRAS_PLATFORM_NUM_6:
1206 case WID_BRAS_PLATFORM_NUM_7: {
1207 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1208 this->RaiseWidget(WID_BRAS_PLATFORM_DRAG_N_DROP);
1210 _settings_client.gui.station_numtracks = widget - WID_BRAS_PLATFORM_NUM_BEGIN;
1211 _settings_client.gui.station_dragdrop = false;
1213 _settings_client.gui.station_dragdrop = false;
1215 const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : nullptr;
1216 if (statspec != nullptr && HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
1217 /* The previously selected number of platforms in invalid */
1218 for (uint i = 0; i < 7; i++) {
1219 if (!HasBit(statspec->disallowed_lengths, i)) {
1220 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1221 _settings_client.gui.station_platlength = i + 1;
1222 break;
1227 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1228 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1229 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1230 this->SetDirty();
1231 DeleteWindowById(WC_SELECT_STATION, 0);
1232 break;
1235 case WID_BRAS_PLATFORM_LEN_1:
1236 case WID_BRAS_PLATFORM_LEN_2:
1237 case WID_BRAS_PLATFORM_LEN_3:
1238 case WID_BRAS_PLATFORM_LEN_4:
1239 case WID_BRAS_PLATFORM_LEN_5:
1240 case WID_BRAS_PLATFORM_LEN_6:
1241 case WID_BRAS_PLATFORM_LEN_7: {
1242 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1243 this->RaiseWidget(WID_BRAS_PLATFORM_DRAG_N_DROP);
1245 _settings_client.gui.station_platlength = widget - WID_BRAS_PLATFORM_LEN_BEGIN;
1246 _settings_client.gui.station_dragdrop = false;
1248 _settings_client.gui.station_dragdrop = false;
1250 const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : nullptr;
1251 if (statspec != nullptr && HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
1252 /* The previously selected number of tracks in invalid */
1253 for (uint i = 0; i < 7; i++) {
1254 if (!HasBit(statspec->disallowed_platforms, i)) {
1255 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1256 _settings_client.gui.station_numtracks = i + 1;
1257 break;
1262 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1263 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1264 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1265 this->SetDirty();
1266 DeleteWindowById(WC_SELECT_STATION, 0);
1267 break;
1270 case WID_BRAS_PLATFORM_DRAG_N_DROP: {
1271 _settings_client.gui.station_dragdrop ^= true;
1273 this->ToggleWidgetLoweredState(WID_BRAS_PLATFORM_DRAG_N_DROP);
1275 /* get the first allowed length/number of platforms */
1276 const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : nullptr;
1277 if (statspec != nullptr && HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
1278 for (uint i = 0; i < 7; i++) {
1279 if (!HasBit(statspec->disallowed_lengths, i)) {
1280 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1281 _settings_client.gui.station_platlength = i + 1;
1282 break;
1286 if (statspec != nullptr && HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
1287 for (uint i = 0; i < 7; i++) {
1288 if (!HasBit(statspec->disallowed_platforms, i)) {
1289 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1290 _settings_client.gui.station_numtracks = i + 1;
1291 break;
1296 this->SetWidgetLoweredState(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN, !_settings_client.gui.station_dragdrop);
1297 this->SetWidgetLoweredState(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN, !_settings_client.gui.station_dragdrop);
1298 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1299 this->SetDirty();
1300 DeleteWindowById(WC_SELECT_STATION, 0);
1301 break;
1304 case WID_BRAS_HIGHLIGHT_OFF:
1305 case WID_BRAS_HIGHLIGHT_ON:
1306 _settings_client.gui.station_show_coverage = (widget != WID_BRAS_HIGHLIGHT_OFF);
1308 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_OFF, !_settings_client.gui.station_show_coverage);
1309 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_ON, _settings_client.gui.station_show_coverage);
1310 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1311 this->SetDirty();
1312 SetViewportCatchmentStation(nullptr, true);
1313 break;
1315 case WID_BRAS_NEWST_LIST: {
1316 int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_BRAS_NEWST_LIST, 0, this->line_height);
1317 if (y >= (int)StationClass::GetClassCount()) return;
1318 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1319 if (i == STAT_CLASS_WAYP) continue;
1320 if (y == 0) {
1321 if (_railstation.station_class != (StationClassID)i) {
1322 _railstation.station_class = (StationClassID)i;
1323 StationClass *stclass = StationClass::Get(_railstation.station_class);
1324 _railstation.station_count = stclass->GetSpecCount();
1325 _railstation.station_type = std::min((int)_railstation.station_type, std::max(0, (int)_railstation.station_count - 1));
1327 this->CheckSelectedSize(stclass->GetSpec(_railstation.station_type));
1329 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX);
1330 matrix->SetCount(_railstation.station_count);
1331 matrix->SetClicked(_railstation.station_type);
1333 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1334 this->SetDirty();
1335 DeleteWindowById(WC_SELECT_STATION, 0);
1336 break;
1338 y--;
1340 break;
1343 case WID_BRAS_IMAGE: {
1344 int y = GB(widget, 16, 16);
1345 if (y >= _railstation.station_count) return;
1347 /* Check station availability callback */
1348 const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(y);
1349 if (!IsStationAvailable(statspec)) return;
1351 _railstation.station_type = y;
1353 this->CheckSelectedSize(statspec);
1354 this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX)->SetClicked(_railstation.station_type);
1356 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1357 this->SetDirty();
1358 DeleteWindowById(WC_SELECT_STATION, 0);
1359 break;
1364 void OnRealtimeTick(uint delta_ms) override
1366 CheckRedrawStationCoverage(this);
1370 static const NWidgetPart _nested_station_builder_widgets[] = {
1371 NWidget(NWID_HORIZONTAL),
1372 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1373 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_STATION_BUILD_RAIL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1374 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
1375 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_DEFSIZE),
1376 NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
1377 EndContainer(),
1378 EndContainer(),
1379 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1380 NWidget(NWID_HORIZONTAL),
1381 NWidget(NWID_VERTICAL),
1382 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_ADDITIONS),
1383 NWidget(NWID_HORIZONTAL), SetPIP(7, 0, 7), SetPadding(2, 0, 1, 0),
1384 NWidget(WWT_MATRIX, COLOUR_GREY, WID_BRAS_NEWST_LIST), SetMinimalSize(122, 71), SetFill(1, 0),
1385 SetMatrixDataTip(1, 0, STR_STATION_BUILD_STATION_CLASS_TOOLTIP), SetScrollbar(WID_BRAS_NEWST_SCROLL),
1386 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_BRAS_NEWST_SCROLL),
1387 EndContainer(),
1388 EndContainer(),
1389 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_ORIENTATION, STR_NULL), SetPadding(1, 2, 0, 2),
1390 NWidget(NWID_HORIZONTAL),
1391 NWidget(NWID_SPACER), SetMinimalSize(7, 0), SetFill(1, 0),
1392 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAS_PLATFORM_DIR_X), SetMinimalSize(66, 60), SetFill(0, 0), SetDataTip(0x0, STR_STATION_BUILD_RAILROAD_ORIENTATION_TOOLTIP), EndContainer(),
1393 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1394 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAS_PLATFORM_DIR_Y), SetMinimalSize(66, 60), SetFill(0, 0), SetDataTip(0x0, STR_STATION_BUILD_RAILROAD_ORIENTATION_TOOLTIP), EndContainer(),
1395 NWidget(NWID_SPACER), SetMinimalSize(7, 0), SetFill(1, 0),
1396 EndContainer(),
1397 NWidget(WWT_LABEL, COLOUR_DARK_GREEN, WID_BRAS_SHOW_NEWST_TYPE), SetMinimalSize(144, 11), SetDataTip(STR_ORANGE_STRING, STR_NULL), SetPadding(1, 2, 4, 2),
1398 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_NUMBER_OF_TRACKS, STR_NULL), SetPadding(0, 2, 0, 2),
1399 NWidget(NWID_HORIZONTAL),
1400 NWidget(NWID_SPACER), SetFill(1, 0),
1401 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_1), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_1, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1402 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_2), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_2, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1403 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_3), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_3, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1404 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_4), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_4, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1405 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_5), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_5, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1406 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_6), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_6, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1407 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_7), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_7, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1408 NWidget(NWID_SPACER), SetFill(1, 0),
1409 EndContainer(),
1410 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_PLATFORM_LENGTH, STR_NULL), SetPadding(2, 2, 0, 2),
1411 NWidget(NWID_HORIZONTAL),
1412 NWidget(NWID_SPACER), SetFill(1, 0),
1413 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_1), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_1, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1414 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_2), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_2, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1415 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_3), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_3, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1416 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_4), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_4, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1417 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_5), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_5, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1418 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_6), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_6, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1419 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_7), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_7, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1420 NWidget(NWID_SPACER), SetFill(1, 0),
1421 EndContainer(),
1422 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1423 NWidget(NWID_HORIZONTAL),
1424 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1425 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_DRAG_N_DROP), SetMinimalSize(75, 12), SetDataTip(STR_STATION_BUILD_DRAG_DROP, STR_STATION_BUILD_DRAG_DROP_TOOLTIP),
1426 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1427 EndContainer(),
1428 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_COVERAGE_AREA_TITLE, STR_NULL), SetPadding(3, 2, 0, 2),
1429 NWidget(NWID_HORIZONTAL),
1430 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1431 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_HIGHLIGHT_OFF), SetMinimalSize(60, 12),
1432 SetDataTip(STR_STATION_BUILD_COVERAGE_OFF, STR_STATION_BUILD_COVERAGE_AREA_OFF_TOOLTIP),
1433 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_HIGHLIGHT_ON), SetMinimalSize(60, 12),
1434 SetDataTip(STR_STATION_BUILD_COVERAGE_ON, STR_STATION_BUILD_COVERAGE_AREA_ON_TOOLTIP),
1435 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1436 EndContainer(),
1437 EndContainer(),
1438 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_MATRIX),
1439 /* We need an additional background for the matrix, as the matrix cannot handle the scrollbar due to not being an NWidgetCore. */
1440 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(WID_BRAS_MATRIX_SCROLL),
1441 NWidget(NWID_HORIZONTAL),
1442 NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BRAS_MATRIX), SetScrollbar(WID_BRAS_MATRIX_SCROLL), SetPIP(0, 2, 0), SetPadding(2, 0, 0, 0),
1443 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BRAS_IMAGE), SetMinimalSize(66, 60),
1444 SetFill(0, 0), SetResize(0, 0), SetDataTip(0x0, STR_STATION_BUILD_STATION_TYPE_TOOLTIP), SetScrollbar(WID_BRAS_MATRIX_SCROLL),
1445 EndContainer(),
1446 EndContainer(),
1447 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BRAS_MATRIX_SCROLL),
1448 EndContainer(),
1449 EndContainer(),
1450 EndContainer(),
1451 EndContainer(),
1452 NWidget(NWID_HORIZONTAL),
1453 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BRAS_COVERAGE_TEXTS), SetFill(1, 1), SetResize(1, 0),
1454 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_RESIZE),
1455 NWidget(NWID_VERTICAL),
1456 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(0, 1), EndContainer(),
1457 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
1458 EndContainer(),
1459 EndContainer(),
1460 EndContainer(),
1461 EndContainer(),
1464 /** High level window description of the station-build window (default & newGRF) */
1465 static WindowDesc _station_builder_desc(
1466 WDP_AUTO, "build_station_rail", 350, 0,
1467 WC_BUILD_STATION, WC_BUILD_TOOLBAR,
1468 WDF_CONSTRUCTION,
1469 _nested_station_builder_widgets, lengthof(_nested_station_builder_widgets)
1472 /** Open station build window */
1473 static void ShowStationBuilder(Window *parent)
1475 bool newstations = StationClass::GetClassCount() > 2 || StationClass::Get(STAT_CLASS_DFLT)->GetSpecCount() != 1;
1476 new BuildRailStationWindow(&_station_builder_desc, parent, newstations);
1479 struct BuildSignalWindow : public PickerWindowBase {
1480 private:
1481 Dimension sig_sprite_size; ///< Maximum size of signal GUI sprites.
1482 int sig_sprite_bottom_offset; ///< Maximum extent of signal GUI sprite from reference point towards bottom.
1485 * Draw dynamic a signal-sprite in a button in the signal GUI
1486 * Draw the sprite +1px to the right and down if the button is lowered
1488 * @param widget_index index of this widget in the window
1489 * @param image the sprite to draw
1491 void DrawSignalSprite(byte widget_index, SpriteID image) const
1493 Point offset;
1494 Dimension sprite_size = GetSpriteSize(image, &offset);
1495 const NWidgetBase *widget = this->GetWidget<NWidgetBase>(widget_index);
1496 int x = widget->pos_x - offset.x +
1497 (widget->current_x - sprite_size.width + offset.x) / 2; // centered
1498 int y = widget->pos_y - sig_sprite_bottom_offset + WD_IMGBTN_TOP +
1499 (widget->current_y - WD_IMGBTN_TOP - WD_IMGBTN_BOTTOM + sig_sprite_size.height) / 2; // aligned to bottom
1501 DrawSprite(image, PAL_NONE,
1502 x + this->IsWidgetLowered(widget_index),
1503 y + this->IsWidgetLowered(widget_index));
1506 public:
1507 BuildSignalWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1509 this->InitNested(TRANSPORT_RAIL);
1510 this->OnInvalidateData();
1513 ~BuildSignalWindow()
1515 _convert_signal_button = false;
1518 void OnInit() override
1520 /* Calculate maximum signal sprite size. */
1521 this->sig_sprite_size.width = 0;
1522 this->sig_sprite_size.height = 0;
1523 this->sig_sprite_bottom_offset = 0;
1524 const RailtypeInfo *rti = GetRailTypeInfo(_cur_railtype);
1525 for (uint type = SIGTYPE_NORMAL; type < SIGTYPE_END; type++) {
1526 for (uint variant = SIG_ELECTRIC; variant <= SIG_SEMAPHORE; variant++) {
1527 for (uint lowered = 0; lowered < 2; lowered++) {
1528 Point offset;
1529 Dimension sprite_size = GetSpriteSize(rti->gui_sprites.signals[type][variant][lowered], &offset);
1530 this->sig_sprite_bottom_offset = std::max<int>(this->sig_sprite_bottom_offset, sprite_size.height);
1531 this->sig_sprite_size.width = std::max<int>(this->sig_sprite_size.width, sprite_size.width - offset.x);
1532 this->sig_sprite_size.height = std::max<int>(this->sig_sprite_size.height, sprite_size.height - offset.y);
1538 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1540 if (widget == WID_BS_DRAG_SIGNALS_DENSITY_LABEL) {
1541 /* Two digits for signals density. */
1542 size->width = std::max(size->width, 2 * GetDigitWidth() + padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT);
1543 } else if (IsInsideMM(widget, WID_BS_SEMAPHORE_NORM, WID_BS_ELECTRIC_PBS_OWAY + 1)) {
1544 size->width = std::max(size->width, this->sig_sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT);
1545 size->height = std::max(size->height, this->sig_sprite_size.height + WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM);
1549 void SetStringParameters(int widget) const override
1551 switch (widget) {
1552 case WID_BS_DRAG_SIGNALS_DENSITY_LABEL:
1553 SetDParam(0, _settings_client.gui.drag_signals_density);
1554 break;
1558 void DrawWidget(const Rect &r, int widget) const override
1560 if (IsInsideMM(widget, WID_BS_SEMAPHORE_NORM, WID_BS_ELECTRIC_PBS_OWAY + 1)) {
1561 /* Extract signal from widget number. */
1562 int type = (widget - WID_BS_SEMAPHORE_NORM) % SIGTYPE_END;
1563 int var = SIG_SEMAPHORE - (widget - WID_BS_SEMAPHORE_NORM) / SIGTYPE_END; // SignalVariant order is reversed compared to the widgets.
1564 SpriteID sprite = GetRailTypeInfo(_cur_railtype)->gui_sprites.signals[type][var][this->IsWidgetLowered(widget)];
1566 this->DrawSignalSprite(widget, sprite);
1570 void OnClick(Point pt, int widget, int click_count) override
1572 switch (widget) {
1573 case WID_BS_SEMAPHORE_NORM:
1574 case WID_BS_SEMAPHORE_ENTRY:
1575 case WID_BS_SEMAPHORE_EXIT:
1576 case WID_BS_SEMAPHORE_COMBO:
1577 case WID_BS_SEMAPHORE_PBS:
1578 case WID_BS_SEMAPHORE_PBS_OWAY:
1579 case WID_BS_ELECTRIC_NORM:
1580 case WID_BS_ELECTRIC_ENTRY:
1581 case WID_BS_ELECTRIC_EXIT:
1582 case WID_BS_ELECTRIC_COMBO:
1583 case WID_BS_ELECTRIC_PBS:
1584 case WID_BS_ELECTRIC_PBS_OWAY:
1585 this->RaiseWidget((_cur_signal_variant == SIG_ELECTRIC ? WID_BS_ELECTRIC_NORM : WID_BS_SEMAPHORE_NORM) + _cur_signal_type);
1587 _cur_signal_type = (SignalType)((uint)((widget - WID_BS_SEMAPHORE_NORM) % (SIGTYPE_LAST + 1)));
1588 _cur_signal_variant = widget >= WID_BS_ELECTRIC_NORM ? SIG_ELECTRIC : SIG_SEMAPHORE;
1590 /* If 'remove' button of rail build toolbar is active, disable it. */
1591 if (_remove_button_clicked) {
1592 Window *w = FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL);
1593 if (w != nullptr) ToggleRailButton_Remove(w);
1596 break;
1598 case WID_BS_CONVERT:
1599 _convert_signal_button = !_convert_signal_button;
1600 break;
1602 case WID_BS_DRAG_SIGNALS_DENSITY_DECREASE:
1603 if (_settings_client.gui.drag_signals_density > 1) {
1604 _settings_client.gui.drag_signals_density--;
1605 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_SETTINGS);
1607 break;
1609 case WID_BS_DRAG_SIGNALS_DENSITY_INCREASE:
1610 if (_settings_client.gui.drag_signals_density < 20) {
1611 _settings_client.gui.drag_signals_density++;
1612 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_SETTINGS);
1614 break;
1616 default: break;
1619 this->InvalidateData();
1623 * Some data on this window has become invalid.
1624 * @param data Information about the changed data.
1625 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
1627 void OnInvalidateData(int data = 0, bool gui_scope = true) override
1629 if (!gui_scope) return;
1630 this->LowerWidget((_cur_signal_variant == SIG_ELECTRIC ? WID_BS_ELECTRIC_NORM : WID_BS_SEMAPHORE_NORM) + _cur_signal_type);
1632 this->SetWidgetLoweredState(WID_BS_CONVERT, _convert_signal_button);
1634 this->SetWidgetDisabledState(WID_BS_DRAG_SIGNALS_DENSITY_DECREASE, _settings_client.gui.drag_signals_density == 1);
1635 this->SetWidgetDisabledState(WID_BS_DRAG_SIGNALS_DENSITY_INCREASE, _settings_client.gui.drag_signals_density == 20);
1639 /** Nested widget definition of the build signal window */
1640 static const NWidgetPart _nested_signal_builder_widgets[] = {
1641 NWidget(NWID_HORIZONTAL),
1642 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1643 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_BUILD_SIGNAL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1644 EndContainer(),
1645 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
1646 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1647 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_NORM), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_NORM_TOOLTIP), EndContainer(), SetFill(1, 1),
1648 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_ENTRY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_ENTRY_TOOLTIP), EndContainer(), SetFill(1, 1),
1649 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_EXIT), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_EXIT_TOOLTIP), EndContainer(), SetFill(1, 1),
1650 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_COMBO), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_COMBO_TOOLTIP), EndContainer(), SetFill(1, 1),
1651 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_PBS), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_PBS_TOOLTIP), EndContainer(), SetFill(1, 1),
1652 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_PBS_OWAY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_PBS_OWAY_TOOLTIP), EndContainer(), SetFill(1, 1),
1653 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_BS_CONVERT), SetDataTip(SPR_IMG_SIGNAL_CONVERT, STR_BUILD_SIGNAL_CONVERT_TOOLTIP), SetFill(1, 1),
1654 EndContainer(),
1655 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1656 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_NORM), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_NORM_TOOLTIP), EndContainer(), SetFill(1, 1),
1657 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_ENTRY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_ENTRY_TOOLTIP), EndContainer(), SetFill(1, 1),
1658 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_EXIT), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_EXIT_TOOLTIP), EndContainer(), SetFill(1, 1),
1659 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_COMBO), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_COMBO_TOOLTIP), EndContainer(), SetFill(1, 1),
1660 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_PBS), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_PBS_TOOLTIP), EndContainer(), SetFill(1, 1),
1661 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_PBS_OWAY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_PBS_OWAY_TOOLTIP), EndContainer(), SetFill(1, 1),
1662 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_DRAG_SIGNALS_DENSITY_TOOLTIP), SetFill(1, 1),
1663 NWidget(WWT_LABEL, COLOUR_DARK_GREEN, WID_BS_DRAG_SIGNALS_DENSITY_LABEL), SetDataTip(STR_ORANGE_INT, STR_BUILD_SIGNAL_DRAG_SIGNALS_DENSITY_TOOLTIP), SetFill(1, 1),
1664 NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1665 NWidget(NWID_SPACER), SetFill(1, 0),
1666 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_BS_DRAG_SIGNALS_DENSITY_DECREASE), SetMinimalSize(9, 12), SetDataTip(AWV_DECREASE, STR_BUILD_SIGNAL_DRAG_SIGNALS_DENSITY_DECREASE_TOOLTIP),
1667 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_BS_DRAG_SIGNALS_DENSITY_INCREASE), SetMinimalSize(9, 12), SetDataTip(AWV_INCREASE, STR_BUILD_SIGNAL_DRAG_SIGNALS_DENSITY_INCREASE_TOOLTIP),
1668 NWidget(NWID_SPACER), SetFill(1, 0),
1669 EndContainer(),
1670 NWidget(NWID_SPACER), SetMinimalSize(0, 2), SetFill(1, 0),
1671 EndContainer(),
1672 EndContainer(),
1673 EndContainer(),
1676 /** Signal selection window description */
1677 static WindowDesc _signal_builder_desc(
1678 WDP_AUTO, "build_signal", 0, 0,
1679 WC_BUILD_SIGNAL, WC_BUILD_TOOLBAR,
1680 WDF_CONSTRUCTION,
1681 _nested_signal_builder_widgets, lengthof(_nested_signal_builder_widgets)
1685 * Open the signal selection window
1687 static void ShowSignalBuilder(Window *parent)
1689 new BuildSignalWindow(&_signal_builder_desc, parent);
1692 struct BuildRailDepotWindow : public PickerWindowBase {
1693 BuildRailDepotWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1695 this->InitNested(TRANSPORT_RAIL);
1696 this->LowerWidget(_build_depot_direction + WID_BRAD_DEPOT_NE);
1699 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1701 if (!IsInsideMM(widget, WID_BRAD_DEPOT_NE, WID_BRAD_DEPOT_NW + 1)) return;
1703 size->width = ScaleGUITrad(64) + 2;
1704 size->height = ScaleGUITrad(48) + 2;
1707 void DrawWidget(const Rect &r, int widget) const override
1709 if (!IsInsideMM(widget, WID_BRAD_DEPOT_NE, WID_BRAD_DEPOT_NW + 1)) return;
1711 DrawTrainDepotSprite(r.left + 1 + ScaleGUITrad(31), r.bottom - ScaleGUITrad(31), widget - WID_BRAD_DEPOT_NE + DIAGDIR_NE, _cur_railtype);
1714 void OnClick(Point pt, int widget, int click_count) override
1716 switch (widget) {
1717 case WID_BRAD_DEPOT_NE:
1718 case WID_BRAD_DEPOT_SE:
1719 case WID_BRAD_DEPOT_SW:
1720 case WID_BRAD_DEPOT_NW:
1721 this->RaiseWidget(_build_depot_direction + WID_BRAD_DEPOT_NE);
1722 _build_depot_direction = (DiagDirection)(widget - WID_BRAD_DEPOT_NE);
1723 this->LowerWidget(_build_depot_direction + WID_BRAD_DEPOT_NE);
1724 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1725 this->SetDirty();
1726 break;
1731 /** Nested widget definition of the build rail depot window */
1732 static const NWidgetPart _nested_build_depot_widgets[] = {
1733 NWidget(NWID_HORIZONTAL),
1734 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1735 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_BUILD_DEPOT_TRAIN_ORIENTATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1736 EndContainer(),
1737 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1738 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1739 NWidget(NWID_HORIZONTAL_LTR),
1740 NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
1741 NWidget(NWID_VERTICAL),
1742 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_NW), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1743 EndContainer(),
1744 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1745 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_SW), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1746 EndContainer(),
1747 EndContainer(),
1748 NWidget(NWID_SPACER), SetMinimalSize(2, 0),
1749 NWidget(NWID_VERTICAL),
1750 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_NE), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1751 EndContainer(),
1752 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1753 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_SE), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1754 EndContainer(),
1755 EndContainer(),
1756 NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
1757 EndContainer(),
1758 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1759 EndContainer(),
1762 static WindowDesc _build_depot_desc(
1763 WDP_AUTO, nullptr, 0, 0,
1764 WC_BUILD_DEPOT, WC_BUILD_TOOLBAR,
1765 WDF_CONSTRUCTION,
1766 _nested_build_depot_widgets, lengthof(_nested_build_depot_widgets)
1769 static void ShowBuildTrainDepotPicker(Window *parent)
1771 new BuildRailDepotWindow(&_build_depot_desc, parent);
1774 struct BuildRailWaypointWindow : PickerWindowBase {
1775 BuildRailWaypointWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1777 this->CreateNestedTree();
1779 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX);
1780 matrix->SetScrollbar(this->GetScrollbar(WID_BRW_SCROLL));
1782 this->FinishInitNested(TRANSPORT_RAIL);
1784 matrix->SetCount(_waypoint_count);
1785 matrix->SetClicked(_cur_waypoint_type);
1788 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1790 switch (widget) {
1791 case WID_BRW_WAYPOINT_MATRIX:
1792 /* Three blobs high and wide. */
1793 size->width += resize->width * 2;
1794 size->height += resize->height * 2;
1796 /* Resizing in X direction only at blob size, but at pixel level in Y. */
1797 resize->height = 1;
1798 break;
1800 case WID_BRW_WAYPOINT:
1801 size->width = ScaleGUITrad(64) + 2;
1802 size->height = ScaleGUITrad(58) + 2;
1803 break;
1807 void DrawWidget(const Rect &r, int widget) const override
1809 switch (GB(widget, 0, 16)) {
1810 case WID_BRW_WAYPOINT: {
1811 byte type = GB(widget, 16, 16);
1812 const StationSpec *statspec = StationClass::Get(STAT_CLASS_WAYP)->GetSpec(type);
1813 DrawWaypointSprite(r.left + 1 + ScaleGUITrad(31), r.bottom - ScaleGUITrad(31), type, _cur_railtype);
1815 if (!IsStationAvailable(statspec)) {
1816 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
1822 void OnClick(Point pt, int widget, int click_count) override
1824 switch (GB(widget, 0, 16)) {
1825 case WID_BRW_WAYPOINT: {
1826 byte type = GB(widget, 16, 16);
1827 this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX)->SetClicked(_cur_waypoint_type);
1829 /* Check station availability callback */
1830 const StationSpec *statspec = StationClass::Get(STAT_CLASS_WAYP)->GetSpec(type);
1831 if (!IsStationAvailable(statspec)) return;
1833 _cur_waypoint_type = type;
1834 this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX)->SetClicked(_cur_waypoint_type);
1835 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1836 this->SetDirty();
1837 break;
1843 /** Nested widget definition for the build NewGRF rail waypoint window */
1844 static const NWidgetPart _nested_build_waypoint_widgets[] = {
1845 NWidget(NWID_HORIZONTAL),
1846 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1847 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_WAYPOINT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1848 NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
1849 EndContainer(),
1850 NWidget(NWID_HORIZONTAL),
1851 NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BRW_WAYPOINT_MATRIX), SetPIP(3, 2, 3), SetScrollbar(WID_BRW_SCROLL),
1852 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BRW_WAYPOINT), SetMinimalSize(66, 60), SetDataTip(0x0, STR_WAYPOINT_GRAPHICS_TOOLTIP), SetScrollbar(WID_BRW_SCROLL), EndContainer(),
1853 EndContainer(),
1854 NWidget(NWID_VERTICAL),
1855 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BRW_SCROLL),
1856 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
1857 EndContainer(),
1858 EndContainer(),
1861 static WindowDesc _build_waypoint_desc(
1862 WDP_AUTO, "build_waypoint", 0, 0,
1863 WC_BUILD_WAYPOINT, WC_BUILD_TOOLBAR,
1864 WDF_CONSTRUCTION,
1865 _nested_build_waypoint_widgets, lengthof(_nested_build_waypoint_widgets)
1868 static void ShowBuildWaypointPicker(Window *parent)
1870 new BuildRailWaypointWindow(&_build_waypoint_desc, parent);
1874 * Initialize rail building GUI settings
1876 void InitializeRailGui()
1878 _build_depot_direction = DIAGDIR_NW;
1882 * Re-initialize rail-build toolbar after toggling support for electric trains
1883 * @param disable Boolean whether electric trains are disabled (removed from the game)
1885 void ReinitGuiAfterToggleElrail(bool disable)
1887 extern RailType _last_built_railtype;
1888 if (disable && _last_built_railtype == RAILTYPE_ELECTRIC) {
1889 _last_built_railtype = _cur_railtype = RAILTYPE_RAIL;
1890 BuildRailToolbarWindow *w = dynamic_cast<BuildRailToolbarWindow *>(FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL));
1891 if (w != nullptr) w->ModifyRailType(_cur_railtype);
1893 MarkWholeScreenDirty();
1896 /** Set the initial (default) railtype to use */
1897 static void SetDefaultRailGui()
1899 if (_local_company == COMPANY_SPECTATOR || !Company::IsValidID(_local_company)) return;
1901 extern RailType _last_built_railtype;
1902 RailType rt;
1903 switch (_settings_client.gui.default_rail_type) {
1904 case 2: {
1905 /* Find the most used rail type */
1906 uint count[RAILTYPE_END];
1907 memset(count, 0, sizeof(count));
1908 for (TileIndex t = 0; t < MapSize(); t++) {
1909 if (IsTileType(t, MP_RAILWAY) || IsLevelCrossingTile(t) || HasStationTileRail(t) ||
1910 (IsTileType(t, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL)) {
1911 count[GetRailType(t)]++;
1915 rt = static_cast<RailType>(std::max_element(count + RAILTYPE_BEGIN, count + RAILTYPE_END) - count);
1916 if (count[rt] > 0) break;
1918 /* No rail, just get the first available one */
1919 FALLTHROUGH;
1921 case 0: {
1922 /* Use first available type */
1923 std::vector<RailType>::const_iterator it = std::find_if(_sorted_railtypes.begin(), _sorted_railtypes.end(),
1924 [](RailType r){ return HasRailtypeAvail(_local_company, r); });
1925 rt = it != _sorted_railtypes.end() ? *it : RAILTYPE_BEGIN;
1926 break;
1928 case 1: {
1929 /* Use last available type */
1930 std::vector<RailType>::const_reverse_iterator it = std::find_if(_sorted_railtypes.rbegin(), _sorted_railtypes.rend(),
1931 [](RailType r){ return HasRailtypeAvail(_local_company, r); });
1932 rt = it != _sorted_railtypes.rend() ? *it : RAILTYPE_BEGIN;
1933 break;
1935 default:
1936 NOT_REACHED();
1939 _last_built_railtype = _cur_railtype = rt;
1940 BuildRailToolbarWindow *w = dynamic_cast<BuildRailToolbarWindow *>(FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL));
1941 if (w != nullptr) w->ModifyRailType(_cur_railtype);
1945 * Updates the current signal variant used in the signal GUI
1946 * to the one adequate to current year.
1947 * @param p needed to be called when a setting changes
1948 * @return success, needed for settings
1950 bool ResetSignalVariant(int32 p)
1952 SignalVariant new_variant = (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC);
1954 if (new_variant != _cur_signal_variant) {
1955 Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
1956 if (w != nullptr) {
1957 w->SetDirty();
1958 w->RaiseWidget((_cur_signal_variant == SIG_ELECTRIC ? WID_BS_ELECTRIC_NORM : WID_BS_SEMAPHORE_NORM) + _cur_signal_type);
1960 _cur_signal_variant = new_variant;
1963 return true;
1967 * Resets the rail GUI - sets default railtype to build
1968 * and resets the signal GUI
1970 void InitializeRailGUI()
1972 SetDefaultRailGui();
1974 _convert_signal_button = false;
1975 _cur_signal_type = _default_signal_type[_settings_client.gui.default_signal_type];
1976 ResetSignalVariant();
1980 * Create a drop down list for all the rail types of the local company.
1981 * @param for_replacement Whether this list is for the replacement window.
1982 * @param all_option Whether to add an 'all types' item.
1983 * @return The populated and sorted #DropDownList.
1985 DropDownList GetRailTypeDropDownList(bool for_replacement, bool all_option)
1987 RailTypes used_railtypes;
1988 RailTypes avail_railtypes;
1990 const Company *c = Company::Get(_local_company);
1992 /* Find the used railtypes. */
1993 if (for_replacement) {
1994 avail_railtypes = GetCompanyRailtypes(c->index, false);
1995 used_railtypes = GetRailTypes(false);
1996 } else {
1997 avail_railtypes = c->avail_railtypes;
1998 used_railtypes = GetRailTypes(true);
2001 DropDownList list;
2003 if (all_option) {
2004 list.emplace_back(new DropDownListStringItem(STR_REPLACE_ALL_RAILTYPE, INVALID_RAILTYPE, false));
2007 Dimension d = { 0, 0 };
2008 RailType rt;
2009 /* Get largest icon size, to ensure text is aligned on each menu item. */
2010 if (!for_replacement) {
2011 FOR_ALL_SORTED_RAILTYPES(rt) {
2012 if (!HasBit(used_railtypes, rt)) continue;
2013 const RailtypeInfo *rti = GetRailTypeInfo(rt);
2014 d = maxdim(d, GetSpriteSize(rti->gui_sprites.build_x_rail));
2018 FOR_ALL_SORTED_RAILTYPES(rt) {
2019 /* If it's not used ever, don't show it to the user. */
2020 if (!HasBit(used_railtypes, rt)) continue;
2022 const RailtypeInfo *rti = GetRailTypeInfo(rt);
2024 StringID str = for_replacement ? rti->strings.replace_text : (rti->max_speed > 0 ? STR_TOOLBAR_RAILTYPE_VELOCITY : STR_JUST_STRING);
2025 DropDownListParamStringItem *item;
2026 if (for_replacement) {
2027 item = new DropDownListParamStringItem(str, rt, !HasBit(avail_railtypes, rt));
2028 } else {
2029 DropDownListIconItem *iconitem = new DropDownListIconItem(rti->gui_sprites.build_x_rail, PAL_NONE, str, rt, !HasBit(avail_railtypes, rt));
2030 iconitem->SetDimension(d);
2031 item = iconitem;
2033 item->SetParam(0, rti->strings.menu_text);
2034 item->SetParam(1, rti->max_speed);
2035 list.emplace_back(item);
2038 if (list.size() == 0) {
2039 /* Empty dropdowns are not allowed */
2040 list.emplace_back(new DropDownListStringItem(STR_NONE, INVALID_RAILTYPE, true));
2043 return list;