Add: INR currency (#8136)
[openttd-github.git] / src / rail_gui.cpp
blobc0a3f4457494d9a5bfe19cefa876e1bc279d09ae
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,
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_NORMAL) return;
111 if ((GetTrackBits(tile) & DiagdirReachesTracks(dir)) == 0) return;
113 DoCommandP(tile, _cur_railtype, track, CMD_BUILD_SINGLE_RAIL);
116 /** Additional pieces of track to add at the entrance of a depot. */
117 static const Track _place_depot_extra_track[12] = {
118 TRACK_LEFT, TRACK_UPPER, TRACK_UPPER, TRACK_RIGHT, // First additional track for directions 0..3
119 TRACK_X, TRACK_Y, TRACK_X, TRACK_Y, // Second additional track
120 TRACK_LOWER, TRACK_LEFT, TRACK_RIGHT, TRACK_LOWER, // Third additional track
123 /** Direction to check for existing track pieces. */
124 static const DiagDirection _place_depot_extra_dir[12] = {
125 DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, DIAGDIR_SW,
126 DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE, DIAGDIR_SE,
127 DIAGDIR_NW, DIAGDIR_NE, DIAGDIR_NW, DIAGDIR_NE,
130 void CcRailDepot(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
132 if (result.Failed()) return;
134 DiagDirection dir = (DiagDirection)p2;
136 if (_settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_RAIL, tile);
137 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
139 tile += TileOffsByDiagDir(dir);
141 if (IsTileType(tile, MP_RAILWAY)) {
142 PlaceExtraDepotRail(tile, _place_depot_extra_dir[dir], _place_depot_extra_track[dir]);
143 PlaceExtraDepotRail(tile, _place_depot_extra_dir[dir + 4], _place_depot_extra_track[dir + 4]);
144 PlaceExtraDepotRail(tile, _place_depot_extra_dir[dir + 8], _place_depot_extra_track[dir + 8]);
149 * Place a rail waypoint.
150 * @param tile Position to start dragging a waypoint.
152 static void PlaceRail_Waypoint(TileIndex tile)
154 if (_remove_button_clicked) {
155 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_REMOVE_STATION);
156 return;
159 Axis axis = GetAxisForNewWaypoint(tile);
160 if (IsValidAxis(axis)) {
161 /* Valid tile for waypoints */
162 VpStartPlaceSizing(tile, axis == AXIS_X ? VPM_X_LIMITED : VPM_Y_LIMITED, DDSP_BUILD_STATION);
163 VpSetPlaceSizingLimit(_settings_game.station.station_spread);
164 } else {
165 /* Tile where we can't build rail waypoints. This is always going to fail,
166 * but provides the user with a proper error message. */
167 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));
171 void CcStation(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
173 if (result.Failed()) return;
175 if (_settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_RAIL, tile);
176 /* Only close the station builder window if the default station and non persistent building is chosen. */
177 if (_railstation.station_class == STAT_CLASS_DFLT && _railstation.station_type == 0 && !_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
181 * Place a rail station.
182 * @param tile Position to place or start dragging a station.
184 static void PlaceRail_Station(TileIndex tile)
186 if (_remove_button_clicked) {
187 VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED, DDSP_REMOVE_STATION);
188 VpSetPlaceSizingLimit(-1);
189 } else if (_settings_client.gui.station_dragdrop) {
190 VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED, DDSP_BUILD_STATION);
191 VpSetPlaceSizingLimit(_settings_game.station.station_spread);
192 } else {
193 uint32 p1 = _cur_railtype | _railstation.orientation << 6 | _settings_client.gui.station_numtracks << 8 | _settings_client.gui.station_platlength << 16 | _ctrl_pressed << 24;
194 uint32 p2 = _railstation.station_class | _railstation.station_type << 8 | INVALID_STATION << 16;
196 int w = _settings_client.gui.station_numtracks;
197 int h = _settings_client.gui.station_platlength;
198 if (!_railstation.orientation) Swap(w, h);
200 CommandContainer cmdcont = { tile, p1, p2, CMD_BUILD_RAIL_STATION | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION), CcStation, "" };
201 ShowSelectStationIfNeeded(cmdcont, TileArea(tile, w, h));
206 * Build a new signal or edit/remove a present signal, use CmdBuildSingleSignal() or CmdRemoveSingleSignal() in rail_cmd.cpp
208 * @param tile The tile where the signal will build or edit
210 static void GenericPlaceSignals(TileIndex tile)
212 TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_RAIL, 0));
214 if (trackbits & TRACK_BIT_VERT) { // N-S direction
215 trackbits = (_tile_fract_coords.x <= _tile_fract_coords.y) ? TRACK_BIT_RIGHT : TRACK_BIT_LEFT;
218 if (trackbits & TRACK_BIT_HORZ) { // E-W direction
219 trackbits = (_tile_fract_coords.x + _tile_fract_coords.y <= 15) ? TRACK_BIT_UPPER : TRACK_BIT_LOWER;
222 Track track = FindFirstTrack(trackbits);
224 if (_remove_button_clicked) {
225 DoCommandP(tile, track, 0, CMD_REMOVE_SIGNALS | CMD_MSG(STR_ERROR_CAN_T_REMOVE_SIGNALS_FROM), CcPlaySound_SPLAT_RAIL);
226 } else {
227 const Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
229 /* Map the setting cycle_signal_types to the lower and upper allowed signal type. */
230 static const uint cycle_bounds[] = {SIGTYPE_NORMAL | (SIGTYPE_LAST_NOPBS << 3), SIGTYPE_PBS | (SIGTYPE_LAST << 3), SIGTYPE_NORMAL | (SIGTYPE_LAST << 3)};
232 /* various bitstuffed elements for CmdBuildSingleSignal() */
233 uint32 p1 = track;
235 if (w != nullptr) {
236 /* signal GUI is used */
237 SB(p1, 3, 1, _ctrl_pressed);
238 SB(p1, 4, 1, _cur_signal_variant);
239 SB(p1, 5, 3, _cur_signal_type);
240 SB(p1, 8, 1, _convert_signal_button);
241 SB(p1, 9, 6, cycle_bounds[_settings_client.gui.cycle_signal_types]);
242 } else {
243 SB(p1, 3, 1, _ctrl_pressed);
244 SB(p1, 4, 1, (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC));
245 SB(p1, 5, 3, _default_signal_type[_settings_client.gui.default_signal_type]);
246 SB(p1, 8, 1, 0);
247 SB(p1, 9, 6, cycle_bounds[_settings_client.gui.cycle_signal_types]);
250 DoCommandP(tile, p1, 0, CMD_BUILD_SIGNALS |
251 CMD_MSG((w != nullptr && _convert_signal_button) ? STR_ERROR_SIGNAL_CAN_T_CONVERT_SIGNALS_HERE : STR_ERROR_CAN_T_BUILD_SIGNALS_HERE),
252 CcPlaySound_SPLAT_RAIL);
257 * Start placing a rail bridge.
258 * @param tile Position of the first tile of the bridge.
259 * @param w Rail toolbar window.
261 static void PlaceRail_Bridge(TileIndex tile, Window *w)
263 if (IsBridgeTile(tile)) {
264 TileIndex other_tile = GetOtherTunnelBridgeEnd(tile);
265 Point pt = {0, 0};
266 w->OnPlaceMouseUp(VPM_X_OR_Y, DDSP_BUILD_BRIDGE, pt, other_tile, tile);
267 } else {
268 VpStartPlaceSizing(tile, VPM_X_OR_Y, DDSP_BUILD_BRIDGE);
272 /** Command callback for building a tunnel */
273 void CcBuildRailTunnel(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
275 if (result.Succeeded()) {
276 if (_settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_RAIL, tile);
277 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
278 } else {
279 SetRedErrorSquare(_build_tunnel_endtile);
284 * Toggles state of the Remove button of Build rail toolbar
285 * @param w window the button belongs to
287 static void ToggleRailButton_Remove(Window *w)
289 DeleteWindowById(WC_SELECT_STATION, 0);
290 w->ToggleWidgetLoweredState(WID_RAT_REMOVE);
291 w->SetWidgetDirty(WID_RAT_REMOVE);
292 _remove_button_clicked = w->IsWidgetLowered(WID_RAT_REMOVE);
293 SetSelectionRed(_remove_button_clicked);
297 * Updates the Remove button because of Ctrl state change
298 * @param w window the button belongs to
299 * @return true iff the remove button was changed
301 static bool RailToolbar_CtrlChanged(Window *w)
303 if (w->IsWidgetDisabled(WID_RAT_REMOVE)) return false;
305 /* allow ctrl to switch remove mode only for these widgets */
306 for (uint i = WID_RAT_BUILD_NS; i <= WID_RAT_BUILD_STATION; i++) {
307 if ((i <= WID_RAT_AUTORAIL || i >= WID_RAT_BUILD_WAYPOINT) && w->IsWidgetLowered(i)) {
308 ToggleRailButton_Remove(w);
309 return true;
313 return false;
318 * The "remove"-button click proc of the build-rail toolbar.
319 * @param w Build-rail toolbar window
320 * @see BuildRailToolbarWindow::OnClick()
322 static void BuildRailClick_Remove(Window *w)
324 if (w->IsWidgetDisabled(WID_RAT_REMOVE)) return;
325 ToggleRailButton_Remove(w);
326 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
328 /* handle station builder */
329 if (w->IsWidgetLowered(WID_RAT_BUILD_STATION)) {
330 if (_remove_button_clicked) {
331 /* starting drag & drop remove */
332 if (!_settings_client.gui.station_dragdrop) {
333 SetTileSelectSize(1, 1);
334 } else {
335 VpSetPlaceSizingLimit(-1);
337 } else {
338 /* starting station build mode */
339 if (!_settings_client.gui.station_dragdrop) {
340 int x = _settings_client.gui.station_numtracks;
341 int y = _settings_client.gui.station_platlength;
342 if (_railstation.orientation == 0) Swap(x, y);
343 SetTileSelectSize(x, y);
344 } else {
345 VpSetPlaceSizingLimit(_settings_game.station.station_spread);
351 static void DoRailroadTrack(int mode)
353 DoCommandP(TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y), _cur_railtype | (mode << 6),
354 _remove_button_clicked ?
355 CMD_REMOVE_RAILROAD_TRACK | CMD_MSG(STR_ERROR_CAN_T_REMOVE_RAILROAD_TRACK) :
356 CMD_BUILD_RAILROAD_TRACK | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_TRACK),
357 CcPlaySound_SPLAT_RAIL);
360 static void HandleAutodirPlacement()
362 int trackstat = _thd.drawstyle & HT_DIR_MASK; // 0..5
364 if (_thd.drawstyle & HT_RAIL) { // one tile case
365 GenericPlaceRail(TileVirtXY(_thd.selend.x, _thd.selend.y), trackstat);
366 return;
369 DoRailroadTrack(trackstat);
373 * Build new signals or remove signals or (if only one tile marked) edit a signal.
375 * If one tile marked abort and use GenericPlaceSignals()
376 * else use CmdBuildSingleSignal() or CmdRemoveSingleSignal() in rail_cmd.cpp to build many signals
378 static void HandleAutoSignalPlacement()
380 uint32 p2 = GB(_thd.drawstyle, 0, 3); // 0..5
382 if ((_thd.drawstyle & HT_DRAG_MASK) == HT_RECT) { // one tile case
383 GenericPlaceSignals(TileVirtXY(_thd.selend.x, _thd.selend.y));
384 return;
387 const Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
389 if (w != nullptr) {
390 /* signal GUI is used */
391 SB(p2, 3, 1, 0);
392 SB(p2, 4, 1, _cur_signal_variant);
393 SB(p2, 6, 1, _ctrl_pressed);
394 SB(p2, 7, 3, _cur_signal_type);
395 SB(p2, 24, 8, _settings_client.gui.drag_signals_density);
396 SB(p2, 10, 1, !_settings_client.gui.drag_signals_fixed_distance);
397 } else {
398 SB(p2, 3, 1, 0);
399 SB(p2, 4, 1, (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC));
400 SB(p2, 6, 1, _ctrl_pressed);
401 SB(p2, 7, 3, _default_signal_type[_settings_client.gui.default_signal_type]);
402 SB(p2, 24, 8, _settings_client.gui.drag_signals_density);
403 SB(p2, 10, 1, !_settings_client.gui.drag_signals_fixed_distance);
406 /* _settings_client.gui.drag_signals_density is given as a parameter such that each user
407 * in a network game can specify his/her own signal density */
408 DoCommandP(TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y), p2,
409 _remove_button_clicked ?
410 CMD_REMOVE_SIGNAL_TRACK | CMD_MSG(STR_ERROR_CAN_T_REMOVE_SIGNALS_FROM) :
411 CMD_BUILD_SIGNAL_TRACK | CMD_MSG(STR_ERROR_CAN_T_BUILD_SIGNALS_HERE),
412 CcPlaySound_SPLAT_RAIL);
416 /** Rail toolbar management class. */
417 struct BuildRailToolbarWindow : Window {
418 RailType railtype; ///< Rail type to build.
419 int last_user_action; ///< Last started user action.
421 BuildRailToolbarWindow(WindowDesc *desc, RailType railtype) : Window(desc)
423 this->InitNested(TRANSPORT_RAIL);
424 this->SetupRailToolbar(railtype);
425 this->DisableWidget(WID_RAT_REMOVE);
426 this->last_user_action = WIDGET_LIST_END;
428 if (_settings_client.gui.link_terraform_toolbar) ShowTerraformToolbar(this);
431 ~BuildRailToolbarWindow()
433 if (this->IsWidgetLowered(WID_RAT_BUILD_STATION)) SetViewportCatchmentStation(nullptr, true);
434 if (_settings_client.gui.link_terraform_toolbar) DeleteWindowById(WC_SCEN_LAND_GEN, 0, false);
438 * Some data on this window has become invalid.
439 * @param data Information about the changed data.
440 * @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.
442 void OnInvalidateData(int data = 0, bool gui_scope = true) override
444 if (!gui_scope) return;
446 if (!CanBuildVehicleInfrastructure(VEH_TRAIN)) delete this;
450 * Configures the rail toolbar for railtype given
451 * @param railtype the railtype to display
453 void SetupRailToolbar(RailType railtype)
455 this->railtype = railtype;
456 const RailtypeInfo *rti = GetRailTypeInfo(railtype);
458 assert(railtype < RAILTYPE_END);
459 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_NS)->widget_data = rti->gui_sprites.build_ns_rail;
460 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_X)->widget_data = rti->gui_sprites.build_x_rail;
461 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_EW)->widget_data = rti->gui_sprites.build_ew_rail;
462 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_Y)->widget_data = rti->gui_sprites.build_y_rail;
463 this->GetWidget<NWidgetCore>(WID_RAT_AUTORAIL)->widget_data = rti->gui_sprites.auto_rail;
464 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_DEPOT)->widget_data = rti->gui_sprites.build_depot;
465 this->GetWidget<NWidgetCore>(WID_RAT_CONVERT_RAIL)->widget_data = rti->gui_sprites.convert_rail;
466 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_TUNNEL)->widget_data = rti->gui_sprites.build_tunnel;
470 * Switch to another rail type.
471 * @param railtype New rail type.
473 void ModifyRailType(RailType railtype)
475 this->SetupRailToolbar(railtype);
476 this->ReInit();
479 void UpdateRemoveWidgetStatus(int clicked_widget)
481 switch (clicked_widget) {
482 case WID_RAT_REMOVE:
483 /* If it is the removal button that has been clicked, do nothing,
484 * as it is up to the other buttons to drive removal status */
485 return;
487 case WID_RAT_BUILD_NS:
488 case WID_RAT_BUILD_X:
489 case WID_RAT_BUILD_EW:
490 case WID_RAT_BUILD_Y:
491 case WID_RAT_AUTORAIL:
492 case WID_RAT_BUILD_WAYPOINT:
493 case WID_RAT_BUILD_STATION:
494 case WID_RAT_BUILD_SIGNALS:
495 /* Removal button is enabled only if the rail/signal/waypoint/station
496 * button is still lowered. Once raised, it has to be disabled */
497 this->SetWidgetDisabledState(WID_RAT_REMOVE, !this->IsWidgetLowered(clicked_widget));
498 break;
500 default:
501 /* When any other buttons than rail/signal/waypoint/station, raise and
502 * disable the removal button */
503 this->DisableWidget(WID_RAT_REMOVE);
504 this->RaiseWidget(WID_RAT_REMOVE);
505 break;
509 void SetStringParameters(int widget) const override
511 if (widget == WID_RAT_CAPTION) {
512 const RailtypeInfo *rti = GetRailTypeInfo(this->railtype);
513 if (rti->max_speed > 0) {
514 SetDParam(0, STR_TOOLBAR_RAILTYPE_VELOCITY);
515 SetDParam(1, rti->strings.toolbar_caption);
516 SetDParam(2, rti->max_speed);
517 } else {
518 SetDParam(0, rti->strings.toolbar_caption);
523 void OnClick(Point pt, int widget, int click_count) override
525 if (widget < WID_RAT_BUILD_NS) return;
527 _remove_button_clicked = false;
528 switch (widget) {
529 case WID_RAT_BUILD_NS:
530 HandlePlacePushButton(this, WID_RAT_BUILD_NS, GetRailTypeInfo(_cur_railtype)->cursor.rail_ns, HT_LINE | HT_DIR_VL);
531 this->last_user_action = widget;
532 break;
534 case WID_RAT_BUILD_X:
535 HandlePlacePushButton(this, WID_RAT_BUILD_X, GetRailTypeInfo(_cur_railtype)->cursor.rail_swne, HT_LINE | HT_DIR_X);
536 this->last_user_action = widget;
537 break;
539 case WID_RAT_BUILD_EW:
540 HandlePlacePushButton(this, WID_RAT_BUILD_EW, GetRailTypeInfo(_cur_railtype)->cursor.rail_ew, HT_LINE | HT_DIR_HL);
541 this->last_user_action = widget;
542 break;
544 case WID_RAT_BUILD_Y:
545 HandlePlacePushButton(this, WID_RAT_BUILD_Y, GetRailTypeInfo(_cur_railtype)->cursor.rail_nwse, HT_LINE | HT_DIR_Y);
546 this->last_user_action = widget;
547 break;
549 case WID_RAT_AUTORAIL:
550 HandlePlacePushButton(this, WID_RAT_AUTORAIL, GetRailTypeInfo(_cur_railtype)->cursor.autorail, HT_RAIL);
551 this->last_user_action = widget;
552 break;
554 case WID_RAT_DEMOLISH:
555 HandlePlacePushButton(this, WID_RAT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
556 this->last_user_action = widget;
557 break;
559 case WID_RAT_BUILD_DEPOT:
560 if (HandlePlacePushButton(this, WID_RAT_BUILD_DEPOT, GetRailTypeInfo(_cur_railtype)->cursor.depot, HT_RECT)) {
561 ShowBuildTrainDepotPicker(this);
562 this->last_user_action = widget;
564 break;
566 case WID_RAT_BUILD_WAYPOINT:
567 this->last_user_action = widget;
568 _waypoint_count = StationClass::Get(STAT_CLASS_WAYP)->GetSpecCount();
569 if (HandlePlacePushButton(this, WID_RAT_BUILD_WAYPOINT, SPR_CURSOR_WAYPOINT, HT_RECT) && _waypoint_count > 1) {
570 ShowBuildWaypointPicker(this);
572 break;
574 case WID_RAT_BUILD_STATION:
575 if (HandlePlacePushButton(this, WID_RAT_BUILD_STATION, SPR_CURSOR_RAIL_STATION, HT_RECT)) {
576 ShowStationBuilder(this);
577 this->last_user_action = widget;
579 break;
581 case WID_RAT_BUILD_SIGNALS: {
582 this->last_user_action = widget;
583 bool started = HandlePlacePushButton(this, WID_RAT_BUILD_SIGNALS, ANIMCURSOR_BUILDSIGNALS, HT_RECT);
584 if (started && _settings_client.gui.enable_signal_gui != _ctrl_pressed) {
585 ShowSignalBuilder(this);
587 break;
590 case WID_RAT_BUILD_BRIDGE:
591 HandlePlacePushButton(this, WID_RAT_BUILD_BRIDGE, SPR_CURSOR_BRIDGE, HT_RECT);
592 this->last_user_action = widget;
593 break;
595 case WID_RAT_BUILD_TUNNEL:
596 HandlePlacePushButton(this, WID_RAT_BUILD_TUNNEL, GetRailTypeInfo(_cur_railtype)->cursor.tunnel, HT_SPECIAL);
597 this->last_user_action = widget;
598 break;
600 case WID_RAT_REMOVE:
601 BuildRailClick_Remove(this);
602 break;
604 case WID_RAT_CONVERT_RAIL:
605 HandlePlacePushButton(this, WID_RAT_CONVERT_RAIL, GetRailTypeInfo(_cur_railtype)->cursor.convert, HT_RECT | HT_DIAGONAL);
606 this->last_user_action = widget;
607 break;
609 default: NOT_REACHED();
611 this->UpdateRemoveWidgetStatus(widget);
612 if (_ctrl_pressed) RailToolbar_CtrlChanged(this);
615 EventState OnHotkey(int hotkey) override
617 MarkTileDirtyByTile(TileVirtXY(_thd.pos.x, _thd.pos.y)); // redraw tile selection
618 return Window::OnHotkey(hotkey);
621 void OnPlaceObject(Point pt, TileIndex tile) override
623 switch (this->last_user_action) {
624 case WID_RAT_BUILD_NS:
625 VpStartPlaceSizing(tile, VPM_FIX_VERTICAL | VPM_RAILDIRS, DDSP_PLACE_RAIL);
626 break;
628 case WID_RAT_BUILD_X:
629 VpStartPlaceSizing(tile, VPM_FIX_Y | VPM_RAILDIRS, DDSP_PLACE_RAIL);
630 break;
632 case WID_RAT_BUILD_EW:
633 VpStartPlaceSizing(tile, VPM_FIX_HORIZONTAL | VPM_RAILDIRS, DDSP_PLACE_RAIL);
634 break;
636 case WID_RAT_BUILD_Y:
637 VpStartPlaceSizing(tile, VPM_FIX_X | VPM_RAILDIRS, DDSP_PLACE_RAIL);
638 break;
640 case WID_RAT_AUTORAIL:
641 VpStartPlaceSizing(tile, VPM_RAILDIRS, DDSP_PLACE_RAIL);
642 break;
644 case WID_RAT_DEMOLISH:
645 PlaceProc_DemolishArea(tile);
646 break;
648 case WID_RAT_BUILD_DEPOT:
649 DoCommandP(tile, _cur_railtype, _build_depot_direction,
650 CMD_BUILD_TRAIN_DEPOT | CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_DEPOT),
651 CcRailDepot);
652 break;
654 case WID_RAT_BUILD_WAYPOINT:
655 PlaceRail_Waypoint(tile);
656 break;
658 case WID_RAT_BUILD_STATION:
659 PlaceRail_Station(tile);
660 break;
662 case WID_RAT_BUILD_SIGNALS:
663 VpStartPlaceSizing(tile, VPM_SIGNALDIRS, DDSP_BUILD_SIGNALS);
664 break;
666 case WID_RAT_BUILD_BRIDGE:
667 PlaceRail_Bridge(tile, this);
668 break;
670 case WID_RAT_BUILD_TUNNEL:
671 DoCommandP(tile, _cur_railtype | (TRANSPORT_RAIL << 8), 0, CMD_BUILD_TUNNEL | CMD_MSG(STR_ERROR_CAN_T_BUILD_TUNNEL_HERE), CcBuildRailTunnel);
672 break;
674 case WID_RAT_CONVERT_RAIL:
675 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CONVERT_RAIL);
676 break;
678 default: NOT_REACHED();
682 void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) override
684 /* no dragging if you have pressed the convert button */
685 if (FindWindowById(WC_BUILD_SIGNAL, 0) != nullptr && _convert_signal_button && this->IsWidgetLowered(WID_RAT_BUILD_SIGNALS)) return;
687 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
690 void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) override
692 if (pt.x != -1) {
693 switch (select_proc) {
694 default: NOT_REACHED();
695 case DDSP_BUILD_BRIDGE:
696 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
697 ShowBuildBridgeWindow(start_tile, end_tile, TRANSPORT_RAIL, _cur_railtype);
698 break;
700 case DDSP_PLACE_RAIL:
701 HandleAutodirPlacement();
702 break;
704 case DDSP_BUILD_SIGNALS:
705 HandleAutoSignalPlacement();
706 break;
708 case DDSP_DEMOLISH_AREA:
709 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
710 break;
712 case DDSP_CONVERT_RAIL:
713 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);
714 break;
716 case DDSP_REMOVE_STATION:
717 case DDSP_BUILD_STATION:
718 if (this->IsWidgetLowered(WID_RAT_BUILD_STATION)) {
719 /* Station */
720 if (_remove_button_clicked) {
721 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);
722 } else {
723 HandleStationPlacement(start_tile, end_tile);
725 } else {
726 /* Waypoint */
727 if (_remove_button_clicked) {
728 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);
729 } else {
730 TileArea ta(start_tile, end_tile);
731 uint32 p1 = _cur_railtype | (select_method == VPM_X_LIMITED ? AXIS_X : AXIS_Y) << 6 | ta.w << 8 | ta.h << 16 | _ctrl_pressed << 24;
732 uint32 p2 = STAT_CLASS_WAYP | _cur_waypoint_type << 8 | INVALID_STATION << 16;
734 CommandContainer cmdcont = { ta.tile, p1, p2, CMD_BUILD_RAIL_WAYPOINT | CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_WAYPOINT), CcPlaySound_SPLAT_RAIL, "" };
735 ShowSelectWaypointIfNeeded(cmdcont, ta);
738 break;
743 void OnPlaceObjectAbort() override
745 if (this->IsWidgetLowered(WID_RAT_BUILD_STATION)) SetViewportCatchmentStation(nullptr, true);
747 this->RaiseButtons();
748 this->DisableWidget(WID_RAT_REMOVE);
749 this->SetWidgetDirty(WID_RAT_REMOVE);
751 DeleteWindowById(WC_BUILD_SIGNAL, TRANSPORT_RAIL);
752 DeleteWindowById(WC_BUILD_STATION, TRANSPORT_RAIL);
753 DeleteWindowById(WC_BUILD_DEPOT, TRANSPORT_RAIL);
754 DeleteWindowById(WC_BUILD_WAYPOINT, TRANSPORT_RAIL);
755 DeleteWindowById(WC_SELECT_STATION, 0);
756 DeleteWindowByClass(WC_BUILD_BRIDGE);
759 void OnPlacePresize(Point pt, TileIndex tile) override
761 DoCommand(tile, _cur_railtype | (TRANSPORT_RAIL << 8), 0, DC_AUTO, CMD_BUILD_TUNNEL);
762 VpSetPresizeRange(tile, _build_tunnel_endtile == 0 ? tile : _build_tunnel_endtile);
765 EventState OnCTRLStateChange() override
767 /* do not toggle Remove button by Ctrl when placing station */
768 if (!this->IsWidgetLowered(WID_RAT_BUILD_STATION) && !this->IsWidgetLowered(WID_RAT_BUILD_WAYPOINT) && RailToolbar_CtrlChanged(this)) return ES_HANDLED;
769 return ES_NOT_HANDLED;
772 static HotkeyList hotkeys;
776 * Handler for global hotkeys of the BuildRailToolbarWindow.
777 * @param hotkey Hotkey
778 * @return ES_HANDLED if hotkey was accepted.
780 static EventState RailToolbarGlobalHotkeys(int hotkey)
782 if (_game_mode != GM_NORMAL || !CanBuildVehicleInfrastructure(VEH_TRAIN)) return ES_NOT_HANDLED;
783 extern RailType _last_built_railtype;
784 Window *w = ShowBuildRailToolbar(_last_built_railtype);
785 if (w == nullptr) return ES_NOT_HANDLED;
786 return w->OnHotkey(hotkey);
789 const uint16 _railtoolbar_autorail_keys[] = {'5', 'A' | WKC_GLOBAL_HOTKEY, 0};
791 static Hotkey railtoolbar_hotkeys[] = {
792 Hotkey('1', "build_ns", WID_RAT_BUILD_NS),
793 Hotkey('2', "build_x", WID_RAT_BUILD_X),
794 Hotkey('3', "build_ew", WID_RAT_BUILD_EW),
795 Hotkey('4', "build_y", WID_RAT_BUILD_Y),
796 Hotkey(_railtoolbar_autorail_keys, "autorail", WID_RAT_AUTORAIL),
797 Hotkey('6', "demolish", WID_RAT_DEMOLISH),
798 Hotkey('7', "depot", WID_RAT_BUILD_DEPOT),
799 Hotkey('8', "waypoint", WID_RAT_BUILD_WAYPOINT),
800 Hotkey('9', "station", WID_RAT_BUILD_STATION),
801 Hotkey('S', "signal", WID_RAT_BUILD_SIGNALS),
802 Hotkey('B', "bridge", WID_RAT_BUILD_BRIDGE),
803 Hotkey('T', "tunnel", WID_RAT_BUILD_TUNNEL),
804 Hotkey('R', "remove", WID_RAT_REMOVE),
805 Hotkey('C', "convert", WID_RAT_CONVERT_RAIL),
806 HOTKEY_LIST_END
808 HotkeyList BuildRailToolbarWindow::hotkeys("railtoolbar", railtoolbar_hotkeys, RailToolbarGlobalHotkeys);
810 static const NWidgetPart _nested_build_rail_widgets[] = {
811 NWidget(NWID_HORIZONTAL),
812 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
813 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_RAT_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
814 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
815 EndContainer(),
816 NWidget(NWID_HORIZONTAL),
817 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_NS),
818 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NS, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
819 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_X),
820 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NE, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
821 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_EW),
822 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_EW, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
823 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_Y),
824 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NW, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
825 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_AUTORAIL),
826 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_AUTORAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_AUTORAIL),
828 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), SetDataTip(0x0, STR_NULL), EndContainer(),
830 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_DEMOLISH),
831 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
832 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_DEPOT),
833 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DEPOT_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_TRAIN_DEPOT_FOR_BUILDING),
834 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_WAYPOINT),
835 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_WAYPOINT, STR_RAIL_TOOLBAR_TOOLTIP_CONVERT_RAIL_TO_WAYPOINT),
836 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_STATION),
837 SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_RAIL_STATION, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_STATION),
838 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_SIGNALS),
839 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_SIGNALS, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_SIGNALS),
840 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_BRIDGE),
841 SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_BRIDGE, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_BRIDGE),
842 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_TUNNEL),
843 SetFill(0, 1), SetMinimalSize(20, 22), SetDataTip(SPR_IMG_TUNNEL_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TUNNEL),
844 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_REMOVE),
845 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_REMOVE, STR_RAIL_TOOLBAR_TOOLTIP_TOGGLE_BUILD_REMOVE_FOR),
846 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_CONVERT_RAIL),
847 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_CONVERT_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_CONVERT_RAIL),
848 EndContainer(),
851 static WindowDesc _build_rail_desc(
852 WDP_ALIGN_TOOLBAR, "toolbar_rail", 0, 0,
853 WC_BUILD_TOOLBAR, WC_NONE,
854 WDF_CONSTRUCTION,
855 _nested_build_rail_widgets, lengthof(_nested_build_rail_widgets),
856 &BuildRailToolbarWindow::hotkeys
861 * Open the build rail toolbar window for a specific rail type.
863 * If the terraform toolbar is linked to the toolbar, that window is also opened.
865 * @param railtype Rail type to open the window for
866 * @return newly opened rail toolbar, or nullptr if the toolbar could not be opened.
868 Window *ShowBuildRailToolbar(RailType railtype)
870 if (!Company::IsValidID(_local_company)) return nullptr;
871 if (!ValParamRailtype(railtype)) return nullptr;
873 DeleteWindowByClass(WC_BUILD_TOOLBAR);
874 _cur_railtype = railtype;
875 _remove_button_clicked = false;
876 return new BuildRailToolbarWindow(&_build_rail_desc, railtype);
879 /* TODO: For custom stations, respect their allowed platforms/lengths bitmasks!
880 * --pasky */
882 static void HandleStationPlacement(TileIndex start, TileIndex end)
884 TileArea ta(start, end);
885 uint numtracks = ta.w;
886 uint platlength = ta.h;
888 if (_railstation.orientation == AXIS_X) Swap(numtracks, platlength);
890 uint32 p1 = _cur_railtype | _railstation.orientation << 6 | numtracks << 8 | platlength << 16 | _ctrl_pressed << 24;
891 uint32 p2 = _railstation.station_class | _railstation.station_type << 8 | INVALID_STATION << 16;
893 CommandContainer cmdcont = { ta.tile, p1, p2, CMD_BUILD_RAIL_STATION | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION), CcStation, "" };
894 ShowSelectStationIfNeeded(cmdcont, ta);
897 struct BuildRailStationWindow : public PickerWindowBase {
898 private:
899 uint line_height; ///< Height of a single line in the newstation selection matrix (#WID_BRAS_NEWST_LIST widget).
900 uint coverage_height; ///< Height of the coverage texts.
901 Scrollbar *vscroll; ///< Vertical scrollbar of the new station list.
902 Scrollbar *vscroll2; ///< Vertical scrollbar of the matrix with new stations.
905 * Verify whether the currently selected station size is allowed after selecting a new station class/type.
906 * If not, change the station size variables ( _settings_client.gui.station_numtracks and _settings_client.gui.station_platlength ).
907 * @param statspec Specification of the new station class/type
909 void CheckSelectedSize(const StationSpec *statspec)
911 if (statspec == nullptr || _settings_client.gui.station_dragdrop) return;
913 /* If current number of tracks is not allowed, make it as big as possible */
914 if (HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
915 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
916 _settings_client.gui.station_numtracks = 1;
917 if (statspec->disallowed_platforms != UINT8_MAX) {
918 while (HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
919 _settings_client.gui.station_numtracks++;
921 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
925 if (HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
926 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
927 _settings_client.gui.station_platlength = 1;
928 if (statspec->disallowed_lengths != UINT8_MAX) {
929 while (HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
930 _settings_client.gui.station_platlength++;
932 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
937 public:
938 BuildRailStationWindow(WindowDesc *desc, Window *parent, bool newstation) : PickerWindowBase(desc, parent)
940 this->coverage_height = 2 * FONT_HEIGHT_NORMAL + 3 * WD_PAR_VSEP_NORMAL;
941 this->vscroll = nullptr;
942 _railstation.newstations = newstation;
944 this->CreateNestedTree();
945 NWidgetStacked *newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_ADDITIONS);
946 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
947 newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_MATRIX);
948 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
949 newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_DEFSIZE);
950 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
951 newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_RESIZE);
952 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
953 if (newstation) {
954 this->vscroll = this->GetScrollbar(WID_BRAS_NEWST_SCROLL);
955 this->vscroll2 = this->GetScrollbar(WID_BRAS_MATRIX_SCROLL);
957 this->FinishInitNested(TRANSPORT_RAIL);
959 this->LowerWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
960 if (_settings_client.gui.station_dragdrop) {
961 this->LowerWidget(WID_BRAS_PLATFORM_DRAG_N_DROP);
962 } else {
963 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
964 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
966 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_OFF, !_settings_client.gui.station_show_coverage);
967 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_ON, _settings_client.gui.station_show_coverage);
969 if (!newstation || _railstation.station_class >= (int)StationClass::GetClassCount()) {
970 /* New stations are not available or changed, so ensure the default station
971 * type is 'selected'. */
972 _railstation.station_class = STAT_CLASS_DFLT;
973 _railstation.station_type = 0;
974 this->vscroll2 = nullptr;
976 if (newstation) {
977 _railstation.station_count = StationClass::Get(_railstation.station_class)->GetSpecCount();
978 _railstation.station_type = min(_railstation.station_type, _railstation.station_count - 1);
980 int count = 0;
981 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
982 if (i == STAT_CLASS_WAYP) continue;
983 count++;
985 this->vscroll->SetCount(count);
986 this->vscroll->SetPosition(Clamp(_railstation.station_class - 2, 0, max(this->vscroll->GetCount() - this->vscroll->GetCapacity(), 0)));
988 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX);
989 matrix->SetScrollbar(this->vscroll2);
990 matrix->SetCount(_railstation.station_count);
991 matrix->SetClicked(_railstation.station_type);
995 virtual ~BuildRailStationWindow()
997 DeleteWindowById(WC_SELECT_STATION, 0);
1000 void OnPaint() override
1002 bool newstations = _railstation.newstations;
1003 const StationSpec *statspec = newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : nullptr;
1005 if (_settings_client.gui.station_dragdrop) {
1006 SetTileSelectSize(1, 1);
1007 } else {
1008 int x = _settings_client.gui.station_numtracks;
1009 int y = _settings_client.gui.station_platlength;
1010 if (_railstation.orientation == AXIS_X) Swap(x, y);
1011 if (!_remove_button_clicked) {
1012 SetTileSelectSize(x, y);
1016 int rad = (_settings_game.station.modified_catchment) ? CA_TRAIN : CA_UNMODIFIED;
1018 if (_settings_client.gui.station_show_coverage) SetTileSelectBigSize(-rad, -rad, 2 * rad, 2 * rad);
1020 for (uint bits = 0; bits < 7; bits++) {
1021 bool disable = bits >= _settings_game.station.station_spread;
1022 if (statspec == nullptr) {
1023 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_NUM_1, disable);
1024 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_LEN_1, disable);
1025 } else {
1026 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_NUM_1, HasBit(statspec->disallowed_platforms, bits) || disable);
1027 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_LEN_1, HasBit(statspec->disallowed_lengths, bits) || disable);
1031 this->DrawWidgets();
1033 if (this->IsShaded()) return;
1034 /* 'Accepts' and 'Supplies' texts. */
1035 NWidgetBase *cov = this->GetWidget<NWidgetBase>(WID_BRAS_COVERAGE_TEXTS);
1036 int top = cov->pos_y + WD_PAR_VSEP_NORMAL;
1037 int left = cov->pos_x + WD_FRAMERECT_LEFT;
1038 int right = cov->pos_x + cov->current_x - WD_FRAMERECT_RIGHT;
1039 int bottom = cov->pos_y + cov->current_y;
1040 top = DrawStationCoverageAreaText(left, right, top, SCT_ALL, rad, false) + WD_PAR_VSEP_NORMAL;
1041 top = DrawStationCoverageAreaText(left, right, top, SCT_ALL, rad, true) + WD_PAR_VSEP_NORMAL;
1042 /* Resize background if the window is too small.
1043 * Never make the window smaller to avoid oscillating if the size change affects the acceptance.
1044 * (This is the case, if making the window bigger moves the mouse into the window.) */
1045 if (top > bottom) {
1046 this->coverage_height += top - bottom;
1047 this->ReInit();
1051 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1053 switch (widget) {
1054 case WID_BRAS_NEWST_LIST: {
1055 Dimension d = {0, 0};
1056 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1057 if (i == STAT_CLASS_WAYP) continue;
1058 d = maxdim(d, GetStringBoundingBox(StationClass::Get((StationClassID)i)->name));
1060 size->width = max(size->width, d.width + padding.width);
1061 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
1062 size->height = 5 * this->line_height;
1063 resize->height = this->line_height;
1064 break;
1067 case WID_BRAS_SHOW_NEWST_TYPE: {
1068 if (!_railstation.newstations) {
1069 size->width = 0;
1070 size->height = 0;
1071 break;
1074 /* If newstations exist, compute the non-zero minimal size. */
1075 Dimension d = {0, 0};
1076 StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
1077 for (StationClassID statclass = STAT_CLASS_BEGIN; statclass < (StationClassID)StationClass::GetClassCount(); statclass++) {
1078 if (statclass == STAT_CLASS_WAYP) continue;
1079 StationClass *stclass = StationClass::Get(statclass);
1080 for (uint16 j = 0; j < stclass->GetSpecCount(); j++) {
1081 const StationSpec *statspec = stclass->GetSpec(j);
1082 SetDParam(0, (statspec != nullptr && statspec->name != 0) ? statspec->name : STR_STATION_CLASS_DFLT);
1083 d = maxdim(d, GetStringBoundingBox(str));
1086 size->width = max(size->width, d.width + padding.width);
1087 break;
1090 case WID_BRAS_PLATFORM_DIR_X:
1091 case WID_BRAS_PLATFORM_DIR_Y:
1092 case WID_BRAS_IMAGE:
1093 size->width = ScaleGUITrad(64) + 2;
1094 size->height = ScaleGUITrad(58) + 2;
1095 break;
1097 case WID_BRAS_COVERAGE_TEXTS:
1098 size->height = this->coverage_height;
1099 break;
1101 case WID_BRAS_MATRIX:
1102 fill->height = 1;
1103 resize->height = 1;
1104 break;
1108 void DrawWidget(const Rect &r, int widget) const override
1110 DrawPixelInfo tmp_dpi;
1112 switch (GB(widget, 0, 16)) {
1113 case WID_BRAS_PLATFORM_DIR_X:
1114 /* Set up a clipping area for the '/' station preview */
1115 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1116 DrawPixelInfo *old_dpi = _cur_dpi;
1117 _cur_dpi = &tmp_dpi;
1118 int x = ScaleGUITrad(31) + 1;
1119 int y = r.bottom - r.top - ScaleGUITrad(31);
1120 if (!DrawStationTile(x, y, _cur_railtype, AXIS_X, _railstation.station_class, _railstation.station_type)) {
1121 StationPickerDrawSprite(x, y, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2);
1123 _cur_dpi = old_dpi;
1125 break;
1127 case WID_BRAS_PLATFORM_DIR_Y:
1128 /* Set up a clipping area for the '\' station preview */
1129 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1130 DrawPixelInfo *old_dpi = _cur_dpi;
1131 _cur_dpi = &tmp_dpi;
1132 int x = ScaleGUITrad(31) + 1;
1133 int y = r.bottom - r.top - ScaleGUITrad(31);
1134 if (!DrawStationTile(x, y, _cur_railtype, AXIS_Y, _railstation.station_class, _railstation.station_type)) {
1135 StationPickerDrawSprite(x, y, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 3);
1137 _cur_dpi = old_dpi;
1139 break;
1141 case WID_BRAS_NEWST_LIST: {
1142 uint statclass = 0;
1143 uint row = 0;
1144 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1145 if (i == STAT_CLASS_WAYP) continue;
1146 if (this->vscroll->IsVisible(statclass)) {
1147 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, row * this->line_height + r.top + WD_MATRIX_TOP,
1148 StationClass::Get((StationClassID)i)->name,
1149 (StationClassID)i == _railstation.station_class ? TC_WHITE : TC_BLACK);
1150 row++;
1152 statclass++;
1154 break;
1157 case WID_BRAS_IMAGE: {
1158 byte type = GB(widget, 16, 16);
1159 assert(type < _railstation.station_count);
1160 /* Check station availability callback */
1161 const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(type);
1162 if (!IsStationAvailable(statspec)) {
1163 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
1166 /* Set up a clipping area for the station preview. */
1167 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1168 DrawPixelInfo *old_dpi = _cur_dpi;
1169 _cur_dpi = &tmp_dpi;
1170 int x = ScaleGUITrad(31) + 1;
1171 int y = r.bottom - r.top - ScaleGUITrad(31);
1172 if (!DrawStationTile(x, y, _cur_railtype, _railstation.orientation, _railstation.station_class, type)) {
1173 StationPickerDrawSprite(x, y, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2 + _railstation.orientation);
1175 _cur_dpi = old_dpi;
1177 break;
1182 void OnResize() override
1184 if (this->vscroll != nullptr) { // New stations available.
1185 this->vscroll->SetCapacityFromWidget(this, WID_BRAS_NEWST_LIST);
1189 void SetStringParameters(int widget) const override
1191 if (widget == WID_BRAS_SHOW_NEWST_TYPE) {
1192 const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type);
1193 SetDParam(0, (statspec != nullptr && statspec->name != 0) ? statspec->name : STR_STATION_CLASS_DFLT);
1197 void OnClick(Point pt, int widget, int click_count) override
1199 switch (GB(widget, 0, 16)) {
1200 case WID_BRAS_PLATFORM_DIR_X:
1201 case WID_BRAS_PLATFORM_DIR_Y:
1202 this->RaiseWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
1203 _railstation.orientation = (Axis)(widget - WID_BRAS_PLATFORM_DIR_X);
1204 this->LowerWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
1205 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1206 this->SetDirty();
1207 DeleteWindowById(WC_SELECT_STATION, 0);
1208 break;
1210 case WID_BRAS_PLATFORM_NUM_1:
1211 case WID_BRAS_PLATFORM_NUM_2:
1212 case WID_BRAS_PLATFORM_NUM_3:
1213 case WID_BRAS_PLATFORM_NUM_4:
1214 case WID_BRAS_PLATFORM_NUM_5:
1215 case WID_BRAS_PLATFORM_NUM_6:
1216 case WID_BRAS_PLATFORM_NUM_7: {
1217 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1218 this->RaiseWidget(WID_BRAS_PLATFORM_DRAG_N_DROP);
1220 _settings_client.gui.station_numtracks = widget - WID_BRAS_PLATFORM_NUM_BEGIN;
1221 _settings_client.gui.station_dragdrop = false;
1223 _settings_client.gui.station_dragdrop = false;
1225 const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : nullptr;
1226 if (statspec != nullptr && HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
1227 /* The previously selected number of platforms in invalid */
1228 for (uint i = 0; i < 7; i++) {
1229 if (!HasBit(statspec->disallowed_lengths, i)) {
1230 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1231 _settings_client.gui.station_platlength = i + 1;
1232 break;
1237 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1238 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1239 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1240 this->SetDirty();
1241 DeleteWindowById(WC_SELECT_STATION, 0);
1242 break;
1245 case WID_BRAS_PLATFORM_LEN_1:
1246 case WID_BRAS_PLATFORM_LEN_2:
1247 case WID_BRAS_PLATFORM_LEN_3:
1248 case WID_BRAS_PLATFORM_LEN_4:
1249 case WID_BRAS_PLATFORM_LEN_5:
1250 case WID_BRAS_PLATFORM_LEN_6:
1251 case WID_BRAS_PLATFORM_LEN_7: {
1252 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1253 this->RaiseWidget(WID_BRAS_PLATFORM_DRAG_N_DROP);
1255 _settings_client.gui.station_platlength = widget - WID_BRAS_PLATFORM_LEN_BEGIN;
1256 _settings_client.gui.station_dragdrop = false;
1258 _settings_client.gui.station_dragdrop = false;
1260 const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : nullptr;
1261 if (statspec != nullptr && HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
1262 /* The previously selected number of tracks in invalid */
1263 for (uint i = 0; i < 7; i++) {
1264 if (!HasBit(statspec->disallowed_platforms, i)) {
1265 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1266 _settings_client.gui.station_numtracks = i + 1;
1267 break;
1272 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1273 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1274 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1275 this->SetDirty();
1276 DeleteWindowById(WC_SELECT_STATION, 0);
1277 break;
1280 case WID_BRAS_PLATFORM_DRAG_N_DROP: {
1281 _settings_client.gui.station_dragdrop ^= true;
1283 this->ToggleWidgetLoweredState(WID_BRAS_PLATFORM_DRAG_N_DROP);
1285 /* get the first allowed length/number of platforms */
1286 const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : nullptr;
1287 if (statspec != nullptr && HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
1288 for (uint i = 0; i < 7; i++) {
1289 if (!HasBit(statspec->disallowed_lengths, i)) {
1290 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1291 _settings_client.gui.station_platlength = i + 1;
1292 break;
1296 if (statspec != nullptr && HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
1297 for (uint i = 0; i < 7; i++) {
1298 if (!HasBit(statspec->disallowed_platforms, i)) {
1299 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1300 _settings_client.gui.station_numtracks = i + 1;
1301 break;
1306 this->SetWidgetLoweredState(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN, !_settings_client.gui.station_dragdrop);
1307 this->SetWidgetLoweredState(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN, !_settings_client.gui.station_dragdrop);
1308 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1309 this->SetDirty();
1310 DeleteWindowById(WC_SELECT_STATION, 0);
1311 break;
1314 case WID_BRAS_HIGHLIGHT_OFF:
1315 case WID_BRAS_HIGHLIGHT_ON:
1316 _settings_client.gui.station_show_coverage = (widget != WID_BRAS_HIGHLIGHT_OFF);
1318 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_OFF, !_settings_client.gui.station_show_coverage);
1319 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_ON, _settings_client.gui.station_show_coverage);
1320 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1321 this->SetDirty();
1322 break;
1324 case WID_BRAS_NEWST_LIST: {
1325 int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_BRAS_NEWST_LIST, 0, this->line_height);
1326 if (y >= (int)StationClass::GetClassCount()) return;
1327 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1328 if (i == STAT_CLASS_WAYP) continue;
1329 if (y == 0) {
1330 if (_railstation.station_class != (StationClassID)i) {
1331 _railstation.station_class = (StationClassID)i;
1332 StationClass *stclass = StationClass::Get(_railstation.station_class);
1333 _railstation.station_count = stclass->GetSpecCount();
1334 _railstation.station_type = min((int)_railstation.station_type, max(0, (int)_railstation.station_count - 1));
1336 this->CheckSelectedSize(stclass->GetSpec(_railstation.station_type));
1338 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX);
1339 matrix->SetCount(_railstation.station_count);
1340 matrix->SetClicked(_railstation.station_type);
1342 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1343 this->SetDirty();
1344 DeleteWindowById(WC_SELECT_STATION, 0);
1345 break;
1347 y--;
1349 break;
1352 case WID_BRAS_IMAGE: {
1353 int y = GB(widget, 16, 16);
1354 if (y >= _railstation.station_count) return;
1356 /* Check station availability callback */
1357 const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(y);
1358 if (!IsStationAvailable(statspec)) return;
1360 _railstation.station_type = y;
1362 this->CheckSelectedSize(statspec);
1363 this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX)->SetClicked(_railstation.station_type);
1365 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1366 this->SetDirty();
1367 DeleteWindowById(WC_SELECT_STATION, 0);
1368 break;
1373 void OnRealtimeTick(uint delta_ms) override
1375 CheckRedrawStationCoverage(this);
1379 static const NWidgetPart _nested_station_builder_widgets[] = {
1380 NWidget(NWID_HORIZONTAL),
1381 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1382 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_STATION_BUILD_RAIL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1383 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
1384 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_DEFSIZE),
1385 NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
1386 EndContainer(),
1387 EndContainer(),
1388 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1389 NWidget(NWID_HORIZONTAL),
1390 NWidget(NWID_VERTICAL),
1391 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_ADDITIONS),
1392 NWidget(NWID_HORIZONTAL), SetPIP(7, 0, 7), SetPadding(2, 0, 1, 0),
1393 NWidget(WWT_MATRIX, COLOUR_GREY, WID_BRAS_NEWST_LIST), SetMinimalSize(122, 71), SetFill(1, 0),
1394 SetMatrixDataTip(1, 0, STR_STATION_BUILD_STATION_CLASS_TOOLTIP), SetScrollbar(WID_BRAS_NEWST_SCROLL),
1395 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_BRAS_NEWST_SCROLL),
1396 EndContainer(),
1397 EndContainer(),
1398 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_ORIENTATION, STR_NULL), SetPadding(1, 2, 0, 2),
1399 NWidget(NWID_HORIZONTAL),
1400 NWidget(NWID_SPACER), SetMinimalSize(7, 0), SetFill(1, 0),
1401 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(),
1402 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1403 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(),
1404 NWidget(NWID_SPACER), SetMinimalSize(7, 0), SetFill(1, 0),
1405 EndContainer(),
1406 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),
1407 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_NUMBER_OF_TRACKS, STR_NULL), SetPadding(0, 2, 0, 2),
1408 NWidget(NWID_HORIZONTAL),
1409 NWidget(NWID_SPACER), SetFill(1, 0),
1410 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_1), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_1, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1411 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_2), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_2, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1412 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_3), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_3, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1413 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_4), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_4, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1414 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_5), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_5, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1415 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_6), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_6, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1416 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_7), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_7, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1417 NWidget(NWID_SPACER), SetFill(1, 0),
1418 EndContainer(),
1419 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_PLATFORM_LENGTH, STR_NULL), SetPadding(2, 2, 0, 2),
1420 NWidget(NWID_HORIZONTAL),
1421 NWidget(NWID_SPACER), SetFill(1, 0),
1422 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_1), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_1, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1423 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_2), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_2, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1424 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_3), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_3, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1425 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_4), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_4, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1426 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_5), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_5, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1427 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_6), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_6, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1428 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_7), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_7, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1429 NWidget(NWID_SPACER), SetFill(1, 0),
1430 EndContainer(),
1431 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1432 NWidget(NWID_HORIZONTAL),
1433 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1434 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),
1435 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1436 EndContainer(),
1437 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_COVERAGE_AREA_TITLE, STR_NULL), SetPadding(3, 2, 0, 2),
1438 NWidget(NWID_HORIZONTAL),
1439 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1440 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_HIGHLIGHT_OFF), SetMinimalSize(60, 12),
1441 SetDataTip(STR_STATION_BUILD_COVERAGE_OFF, STR_STATION_BUILD_COVERAGE_AREA_OFF_TOOLTIP),
1442 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_HIGHLIGHT_ON), SetMinimalSize(60, 12),
1443 SetDataTip(STR_STATION_BUILD_COVERAGE_ON, STR_STATION_BUILD_COVERAGE_AREA_ON_TOOLTIP),
1444 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1445 EndContainer(),
1446 EndContainer(),
1447 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_MATRIX),
1448 /* We need an additional background for the matrix, as the matrix cannot handle the scrollbar due to not being an NWidgetCore. */
1449 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(WID_BRAS_MATRIX_SCROLL),
1450 NWidget(NWID_HORIZONTAL),
1451 NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BRAS_MATRIX), SetScrollbar(WID_BRAS_MATRIX_SCROLL), SetPIP(0, 2, 0), SetPadding(2, 0, 0, 0),
1452 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BRAS_IMAGE), SetMinimalSize(66, 60),
1453 SetFill(0, 0), SetResize(0, 0), SetDataTip(0x0, STR_STATION_BUILD_STATION_TYPE_TOOLTIP), SetScrollbar(WID_BRAS_MATRIX_SCROLL),
1454 EndContainer(),
1455 EndContainer(),
1456 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BRAS_MATRIX_SCROLL),
1457 EndContainer(),
1458 EndContainer(),
1459 EndContainer(),
1460 EndContainer(),
1461 NWidget(NWID_HORIZONTAL),
1462 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BRAS_COVERAGE_TEXTS), SetFill(1, 1), SetResize(1, 0),
1463 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_RESIZE),
1464 NWidget(NWID_VERTICAL),
1465 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(0, 1), EndContainer(),
1466 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
1467 EndContainer(),
1468 EndContainer(),
1469 EndContainer(),
1470 EndContainer(),
1473 /** High level window description of the station-build window (default & newGRF) */
1474 static WindowDesc _station_builder_desc(
1475 WDP_AUTO, "build_station_rail", 350, 0,
1476 WC_BUILD_STATION, WC_BUILD_TOOLBAR,
1477 WDF_CONSTRUCTION,
1478 _nested_station_builder_widgets, lengthof(_nested_station_builder_widgets)
1481 /** Open station build window */
1482 static void ShowStationBuilder(Window *parent)
1484 bool newstations = StationClass::GetClassCount() > 2 || StationClass::Get(STAT_CLASS_DFLT)->GetSpecCount() != 1;
1485 new BuildRailStationWindow(&_station_builder_desc, parent, newstations);
1488 struct BuildSignalWindow : public PickerWindowBase {
1489 private:
1490 Dimension sig_sprite_size; ///< Maximum size of signal GUI sprites.
1491 int sig_sprite_bottom_offset; ///< Maximum extent of signal GUI sprite from reference point towards bottom.
1494 * Draw dynamic a signal-sprite in a button in the signal GUI
1495 * Draw the sprite +1px to the right and down if the button is lowered
1497 * @param widget_index index of this widget in the window
1498 * @param image the sprite to draw
1500 void DrawSignalSprite(byte widget_index, SpriteID image) const
1502 Point offset;
1503 Dimension sprite_size = GetSpriteSize(image, &offset);
1504 const NWidgetBase *widget = this->GetWidget<NWidgetBase>(widget_index);
1505 int x = widget->pos_x - offset.x +
1506 (widget->current_x - sprite_size.width + offset.x) / 2; // centered
1507 int y = widget->pos_y - sig_sprite_bottom_offset + WD_IMGBTN_TOP +
1508 (widget->current_y - WD_IMGBTN_TOP - WD_IMGBTN_BOTTOM + sig_sprite_size.height) / 2; // aligned to bottom
1510 DrawSprite(image, PAL_NONE,
1511 x + this->IsWidgetLowered(widget_index),
1512 y + this->IsWidgetLowered(widget_index));
1515 public:
1516 BuildSignalWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1518 this->InitNested(TRANSPORT_RAIL);
1519 this->OnInvalidateData();
1522 ~BuildSignalWindow()
1524 _convert_signal_button = false;
1527 void OnInit() override
1529 /* Calculate maximum signal sprite size. */
1530 this->sig_sprite_size.width = 0;
1531 this->sig_sprite_size.height = 0;
1532 this->sig_sprite_bottom_offset = 0;
1533 const RailtypeInfo *rti = GetRailTypeInfo(_cur_railtype);
1534 for (uint type = SIGTYPE_NORMAL; type < SIGTYPE_END; type++) {
1535 for (uint variant = SIG_ELECTRIC; variant <= SIG_SEMAPHORE; variant++) {
1536 for (uint lowered = 0; lowered < 2; lowered++) {
1537 Point offset;
1538 Dimension sprite_size = GetSpriteSize(rti->gui_sprites.signals[type][variant][lowered], &offset);
1539 this->sig_sprite_bottom_offset = max<int>(this->sig_sprite_bottom_offset, sprite_size.height);
1540 this->sig_sprite_size.width = max<int>(this->sig_sprite_size.width, sprite_size.width - offset.x);
1541 this->sig_sprite_size.height = max<int>(this->sig_sprite_size.height, sprite_size.height - offset.y);
1547 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1549 if (widget == WID_BS_DRAG_SIGNALS_DENSITY_LABEL) {
1550 /* Two digits for signals density. */
1551 size->width = max(size->width, 2 * GetDigitWidth() + padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT);
1552 } else if (IsInsideMM(widget, WID_BS_SEMAPHORE_NORM, WID_BS_ELECTRIC_PBS_OWAY + 1)) {
1553 size->width = max(size->width, this->sig_sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT);
1554 size->height = max(size->height, this->sig_sprite_size.height + WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM);
1558 void SetStringParameters(int widget) const override
1560 switch (widget) {
1561 case WID_BS_DRAG_SIGNALS_DENSITY_LABEL:
1562 SetDParam(0, _settings_client.gui.drag_signals_density);
1563 break;
1567 void DrawWidget(const Rect &r, int widget) const override
1569 if (IsInsideMM(widget, WID_BS_SEMAPHORE_NORM, WID_BS_ELECTRIC_PBS_OWAY + 1)) {
1570 /* Extract signal from widget number. */
1571 int type = (widget - WID_BS_SEMAPHORE_NORM) % SIGTYPE_END;
1572 int var = SIG_SEMAPHORE - (widget - WID_BS_SEMAPHORE_NORM) / SIGTYPE_END; // SignalVariant order is reversed compared to the widgets.
1573 SpriteID sprite = GetRailTypeInfo(_cur_railtype)->gui_sprites.signals[type][var][this->IsWidgetLowered(widget)];
1575 this->DrawSignalSprite(widget, sprite);
1579 void OnClick(Point pt, int widget, int click_count) override
1581 switch (widget) {
1582 case WID_BS_SEMAPHORE_NORM:
1583 case WID_BS_SEMAPHORE_ENTRY:
1584 case WID_BS_SEMAPHORE_EXIT:
1585 case WID_BS_SEMAPHORE_COMBO:
1586 case WID_BS_SEMAPHORE_PBS:
1587 case WID_BS_SEMAPHORE_PBS_OWAY:
1588 case WID_BS_ELECTRIC_NORM:
1589 case WID_BS_ELECTRIC_ENTRY:
1590 case WID_BS_ELECTRIC_EXIT:
1591 case WID_BS_ELECTRIC_COMBO:
1592 case WID_BS_ELECTRIC_PBS:
1593 case WID_BS_ELECTRIC_PBS_OWAY:
1594 this->RaiseWidget((_cur_signal_variant == SIG_ELECTRIC ? WID_BS_ELECTRIC_NORM : WID_BS_SEMAPHORE_NORM) + _cur_signal_type);
1596 _cur_signal_type = (SignalType)((uint)((widget - WID_BS_SEMAPHORE_NORM) % (SIGTYPE_LAST + 1)));
1597 _cur_signal_variant = widget >= WID_BS_ELECTRIC_NORM ? SIG_ELECTRIC : SIG_SEMAPHORE;
1599 /* If 'remove' button of rail build toolbar is active, disable it. */
1600 if (_remove_button_clicked) {
1601 Window *w = FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL);
1602 if (w != nullptr) ToggleRailButton_Remove(w);
1605 break;
1607 case WID_BS_CONVERT:
1608 _convert_signal_button = !_convert_signal_button;
1609 break;
1611 case WID_BS_DRAG_SIGNALS_DENSITY_DECREASE:
1612 if (_settings_client.gui.drag_signals_density > 1) {
1613 _settings_client.gui.drag_signals_density--;
1614 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_SETTINGS);
1616 break;
1618 case WID_BS_DRAG_SIGNALS_DENSITY_INCREASE:
1619 if (_settings_client.gui.drag_signals_density < 20) {
1620 _settings_client.gui.drag_signals_density++;
1621 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_SETTINGS);
1623 break;
1625 default: break;
1628 this->InvalidateData();
1632 * Some data on this window has become invalid.
1633 * @param data Information about the changed data.
1634 * @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.
1636 void OnInvalidateData(int data = 0, bool gui_scope = true) override
1638 if (!gui_scope) return;
1639 this->LowerWidget((_cur_signal_variant == SIG_ELECTRIC ? WID_BS_ELECTRIC_NORM : WID_BS_SEMAPHORE_NORM) + _cur_signal_type);
1641 this->SetWidgetLoweredState(WID_BS_CONVERT, _convert_signal_button);
1643 this->SetWidgetDisabledState(WID_BS_DRAG_SIGNALS_DENSITY_DECREASE, _settings_client.gui.drag_signals_density == 1);
1644 this->SetWidgetDisabledState(WID_BS_DRAG_SIGNALS_DENSITY_INCREASE, _settings_client.gui.drag_signals_density == 20);
1648 /** Nested widget definition of the build signal window */
1649 static const NWidgetPart _nested_signal_builder_widgets[] = {
1650 NWidget(NWID_HORIZONTAL),
1651 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1652 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_BUILD_SIGNAL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1653 EndContainer(),
1654 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
1655 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1656 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_NORM), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_NORM_TOOLTIP), EndContainer(), SetFill(1, 1),
1657 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_ENTRY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_ENTRY_TOOLTIP), EndContainer(), SetFill(1, 1),
1658 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_EXIT), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_EXIT_TOOLTIP), EndContainer(), SetFill(1, 1),
1659 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_COMBO), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_COMBO_TOOLTIP), EndContainer(), SetFill(1, 1),
1660 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_PBS), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_PBS_TOOLTIP), EndContainer(), SetFill(1, 1),
1661 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),
1662 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_BS_CONVERT), SetDataTip(SPR_IMG_SIGNAL_CONVERT, STR_BUILD_SIGNAL_CONVERT_TOOLTIP), SetFill(1, 1),
1663 EndContainer(),
1664 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1665 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_NORM), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_NORM_TOOLTIP), EndContainer(), SetFill(1, 1),
1666 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_ENTRY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_ENTRY_TOOLTIP), EndContainer(), SetFill(1, 1),
1667 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_EXIT), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_EXIT_TOOLTIP), EndContainer(), SetFill(1, 1),
1668 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_COMBO), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_COMBO_TOOLTIP), EndContainer(), SetFill(1, 1),
1669 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_PBS), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_PBS_TOOLTIP), EndContainer(), SetFill(1, 1),
1670 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),
1671 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_DRAG_SIGNALS_DENSITY_TOOLTIP), SetFill(1, 1),
1672 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),
1673 NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1674 NWidget(NWID_SPACER), SetFill(1, 0),
1675 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),
1676 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),
1677 NWidget(NWID_SPACER), SetFill(1, 0),
1678 EndContainer(),
1679 NWidget(NWID_SPACER), SetMinimalSize(0, 2), SetFill(1, 0),
1680 EndContainer(),
1681 EndContainer(),
1682 EndContainer(),
1685 /** Signal selection window description */
1686 static WindowDesc _signal_builder_desc(
1687 WDP_AUTO, "build_signal", 0, 0,
1688 WC_BUILD_SIGNAL, WC_BUILD_TOOLBAR,
1689 WDF_CONSTRUCTION,
1690 _nested_signal_builder_widgets, lengthof(_nested_signal_builder_widgets)
1694 * Open the signal selection window
1696 static void ShowSignalBuilder(Window *parent)
1698 new BuildSignalWindow(&_signal_builder_desc, parent);
1701 struct BuildRailDepotWindow : public PickerWindowBase {
1702 BuildRailDepotWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1704 this->InitNested(TRANSPORT_RAIL);
1705 this->LowerWidget(_build_depot_direction + WID_BRAD_DEPOT_NE);
1708 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1710 if (!IsInsideMM(widget, WID_BRAD_DEPOT_NE, WID_BRAD_DEPOT_NW + 1)) return;
1712 size->width = ScaleGUITrad(64) + 2;
1713 size->height = ScaleGUITrad(48) + 2;
1716 void DrawWidget(const Rect &r, int widget) const override
1718 if (!IsInsideMM(widget, WID_BRAD_DEPOT_NE, WID_BRAD_DEPOT_NW + 1)) return;
1720 DrawTrainDepotSprite(r.left + 1 + ScaleGUITrad(31), r.bottom - ScaleGUITrad(31), widget - WID_BRAD_DEPOT_NE + DIAGDIR_NE, _cur_railtype);
1723 void OnClick(Point pt, int widget, int click_count) override
1725 switch (widget) {
1726 case WID_BRAD_DEPOT_NE:
1727 case WID_BRAD_DEPOT_SE:
1728 case WID_BRAD_DEPOT_SW:
1729 case WID_BRAD_DEPOT_NW:
1730 this->RaiseWidget(_build_depot_direction + WID_BRAD_DEPOT_NE);
1731 _build_depot_direction = (DiagDirection)(widget - WID_BRAD_DEPOT_NE);
1732 this->LowerWidget(_build_depot_direction + WID_BRAD_DEPOT_NE);
1733 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1734 this->SetDirty();
1735 break;
1740 /** Nested widget definition of the build rail depot window */
1741 static const NWidgetPart _nested_build_depot_widgets[] = {
1742 NWidget(NWID_HORIZONTAL),
1743 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1744 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_BUILD_DEPOT_TRAIN_ORIENTATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1745 EndContainer(),
1746 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1747 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1748 NWidget(NWID_HORIZONTAL_LTR),
1749 NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
1750 NWidget(NWID_VERTICAL),
1751 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_NW), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1752 EndContainer(),
1753 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1754 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_SW), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1755 EndContainer(),
1756 EndContainer(),
1757 NWidget(NWID_SPACER), SetMinimalSize(2, 0),
1758 NWidget(NWID_VERTICAL),
1759 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_NE), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1760 EndContainer(),
1761 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1762 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_SE), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1763 EndContainer(),
1764 EndContainer(),
1765 NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
1766 EndContainer(),
1767 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1768 EndContainer(),
1771 static WindowDesc _build_depot_desc(
1772 WDP_AUTO, nullptr, 0, 0,
1773 WC_BUILD_DEPOT, WC_BUILD_TOOLBAR,
1774 WDF_CONSTRUCTION,
1775 _nested_build_depot_widgets, lengthof(_nested_build_depot_widgets)
1778 static void ShowBuildTrainDepotPicker(Window *parent)
1780 new BuildRailDepotWindow(&_build_depot_desc, parent);
1783 struct BuildRailWaypointWindow : PickerWindowBase {
1784 BuildRailWaypointWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1786 this->CreateNestedTree();
1788 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX);
1789 matrix->SetScrollbar(this->GetScrollbar(WID_BRW_SCROLL));
1791 this->FinishInitNested(TRANSPORT_RAIL);
1793 matrix->SetCount(_waypoint_count);
1794 matrix->SetClicked(_cur_waypoint_type);
1797 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1799 switch (widget) {
1800 case WID_BRW_WAYPOINT_MATRIX:
1801 /* Three blobs high and wide. */
1802 size->width += resize->width * 2;
1803 size->height += resize->height * 2;
1805 /* Resizing in X direction only at blob size, but at pixel level in Y. */
1806 resize->height = 1;
1807 break;
1809 case WID_BRW_WAYPOINT:
1810 size->width = ScaleGUITrad(64) + 2;
1811 size->height = ScaleGUITrad(58) + 2;
1812 break;
1816 void DrawWidget(const Rect &r, int widget) const override
1818 switch (GB(widget, 0, 16)) {
1819 case WID_BRW_WAYPOINT: {
1820 byte type = GB(widget, 16, 16);
1821 const StationSpec *statspec = StationClass::Get(STAT_CLASS_WAYP)->GetSpec(type);
1822 DrawWaypointSprite(r.left + 1 + ScaleGUITrad(31), r.bottom - ScaleGUITrad(31), type, _cur_railtype);
1824 if (!IsStationAvailable(statspec)) {
1825 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
1831 void OnClick(Point pt, int widget, int click_count) override
1833 switch (GB(widget, 0, 16)) {
1834 case WID_BRW_WAYPOINT: {
1835 byte type = GB(widget, 16, 16);
1836 this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX)->SetClicked(_cur_waypoint_type);
1838 /* Check station availability callback */
1839 const StationSpec *statspec = StationClass::Get(STAT_CLASS_WAYP)->GetSpec(type);
1840 if (!IsStationAvailable(statspec)) return;
1842 _cur_waypoint_type = type;
1843 this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX)->SetClicked(_cur_waypoint_type);
1844 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1845 this->SetDirty();
1846 break;
1852 /** Nested widget definition for the build NewGRF rail waypoint window */
1853 static const NWidgetPart _nested_build_waypoint_widgets[] = {
1854 NWidget(NWID_HORIZONTAL),
1855 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1856 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_WAYPOINT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1857 NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
1858 EndContainer(),
1859 NWidget(NWID_HORIZONTAL),
1860 NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BRW_WAYPOINT_MATRIX), SetPIP(3, 2, 3), SetScrollbar(WID_BRW_SCROLL),
1861 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BRW_WAYPOINT), SetMinimalSize(66, 60), SetDataTip(0x0, STR_WAYPOINT_GRAPHICS_TOOLTIP), SetScrollbar(WID_BRW_SCROLL), EndContainer(),
1862 EndContainer(),
1863 NWidget(NWID_VERTICAL),
1864 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BRW_SCROLL),
1865 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
1866 EndContainer(),
1867 EndContainer(),
1870 static WindowDesc _build_waypoint_desc(
1871 WDP_AUTO, "build_waypoint", 0, 0,
1872 WC_BUILD_WAYPOINT, WC_BUILD_TOOLBAR,
1873 WDF_CONSTRUCTION,
1874 _nested_build_waypoint_widgets, lengthof(_nested_build_waypoint_widgets)
1877 static void ShowBuildWaypointPicker(Window *parent)
1879 new BuildRailWaypointWindow(&_build_waypoint_desc, parent);
1883 * Initialize rail building GUI settings
1885 void InitializeRailGui()
1887 _build_depot_direction = DIAGDIR_NW;
1891 * Re-initialize rail-build toolbar after toggling support for electric trains
1892 * @param disable Boolean whether electric trains are disabled (removed from the game)
1894 void ReinitGuiAfterToggleElrail(bool disable)
1896 extern RailType _last_built_railtype;
1897 if (disable && _last_built_railtype == RAILTYPE_ELECTRIC) {
1898 _last_built_railtype = _cur_railtype = RAILTYPE_RAIL;
1899 BuildRailToolbarWindow *w = dynamic_cast<BuildRailToolbarWindow *>(FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL));
1900 if (w != nullptr) w->ModifyRailType(_cur_railtype);
1902 MarkWholeScreenDirty();
1905 /** Set the initial (default) railtype to use */
1906 static void SetDefaultRailGui()
1908 if (_local_company == COMPANY_SPECTATOR || !Company::IsValidID(_local_company)) return;
1910 extern RailType _last_built_railtype;
1911 RailType rt;
1912 switch (_settings_client.gui.default_rail_type) {
1913 case 2: {
1914 /* Find the most used rail type */
1915 uint count[RAILTYPE_END];
1916 memset(count, 0, sizeof(count));
1917 for (TileIndex t = 0; t < MapSize(); t++) {
1918 if (IsTileType(t, MP_RAILWAY) || IsLevelCrossingTile(t) || HasStationTileRail(t) ||
1919 (IsTileType(t, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL)) {
1920 count[GetRailType(t)]++;
1924 rt = static_cast<RailType>(std::max_element(count + RAILTYPE_BEGIN, count + RAILTYPE_END) - count);
1925 if (count[rt] > 0) break;
1927 /* No rail, just get the first available one */
1928 FALLTHROUGH;
1930 case 0: {
1931 /* Use first available type */
1932 std::vector<RailType>::const_iterator it = std::find_if(_sorted_railtypes.begin(), _sorted_railtypes.end(),
1933 [](RailType r){ return HasRailtypeAvail(_local_company, r); });
1934 rt = it != _sorted_railtypes.end() ? *it : RAILTYPE_BEGIN;
1935 break;
1937 case 1: {
1938 /* Use last available type */
1939 std::vector<RailType>::const_reverse_iterator it = std::find_if(_sorted_railtypes.rbegin(), _sorted_railtypes.rend(),
1940 [](RailType r){ return HasRailtypeAvail(_local_company, r); });
1941 rt = it != _sorted_railtypes.rend() ? *it : RAILTYPE_BEGIN;
1942 break;
1944 default:
1945 NOT_REACHED();
1948 _last_built_railtype = _cur_railtype = rt;
1949 BuildRailToolbarWindow *w = dynamic_cast<BuildRailToolbarWindow *>(FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL));
1950 if (w != nullptr) w->ModifyRailType(_cur_railtype);
1954 * Updates the current signal variant used in the signal GUI
1955 * to the one adequate to current year.
1956 * @param p needed to be called when a setting changes
1957 * @return success, needed for settings
1959 bool ResetSignalVariant(int32 p)
1961 SignalVariant new_variant = (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC);
1963 if (new_variant != _cur_signal_variant) {
1964 Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
1965 if (w != nullptr) {
1966 w->SetDirty();
1967 w->RaiseWidget((_cur_signal_variant == SIG_ELECTRIC ? WID_BS_ELECTRIC_NORM : WID_BS_SEMAPHORE_NORM) + _cur_signal_type);
1969 _cur_signal_variant = new_variant;
1972 return true;
1976 * Resets the rail GUI - sets default railtype to build
1977 * and resets the signal GUI
1979 void InitializeRailGUI()
1981 SetDefaultRailGui();
1983 _convert_signal_button = false;
1984 _cur_signal_type = _default_signal_type[_settings_client.gui.default_signal_type];
1985 ResetSignalVariant();
1989 * Create a drop down list for all the rail types of the local company.
1990 * @param for_replacement Whether this list is for the replacement window.
1991 * @param all_option Whether to add an 'all types' item.
1992 * @return The populated and sorted #DropDownList.
1994 DropDownList GetRailTypeDropDownList(bool for_replacement, bool all_option)
1996 RailTypes used_railtypes;
1997 RailTypes avail_railtypes;
1999 const Company *c = Company::Get(_local_company);
2001 /* Find the used railtypes. */
2002 if (for_replacement) {
2003 avail_railtypes = GetCompanyRailtypes(c->index, false);
2004 used_railtypes = GetRailTypes(false);
2005 } else {
2006 avail_railtypes = c->avail_railtypes;
2007 used_railtypes = GetRailTypes(true);
2010 DropDownList list;
2012 if (all_option) {
2013 list.emplace_back(new DropDownListStringItem(STR_REPLACE_ALL_RAILTYPE, INVALID_RAILTYPE, false));
2016 Dimension d = { 0, 0 };
2017 RailType rt;
2018 /* Get largest icon size, to ensure text is aligned on each menu item. */
2019 if (!for_replacement) {
2020 FOR_ALL_SORTED_RAILTYPES(rt) {
2021 if (!HasBit(used_railtypes, rt)) continue;
2022 const RailtypeInfo *rti = GetRailTypeInfo(rt);
2023 d = maxdim(d, GetSpriteSize(rti->gui_sprites.build_x_rail));
2027 FOR_ALL_SORTED_RAILTYPES(rt) {
2028 /* If it's not used ever, don't show it to the user. */
2029 if (!HasBit(used_railtypes, rt)) continue;
2031 const RailtypeInfo *rti = GetRailTypeInfo(rt);
2033 StringID str = for_replacement ? rti->strings.replace_text : (rti->max_speed > 0 ? STR_TOOLBAR_RAILTYPE_VELOCITY : STR_JUST_STRING);
2034 DropDownListParamStringItem *item;
2035 if (for_replacement) {
2036 item = new DropDownListParamStringItem(str, rt, !HasBit(avail_railtypes, rt));
2037 } else {
2038 DropDownListIconItem *iconitem = new DropDownListIconItem(rti->gui_sprites.build_x_rail, PAL_NONE, str, rt, !HasBit(avail_railtypes, rt));
2039 iconitem->SetDimension(d);
2040 item = iconitem;
2042 item->SetParam(0, rti->strings.menu_text);
2043 item->SetParam(1, rti->max_speed);
2044 list.emplace_back(item);
2047 if (list.size() == 0) {
2048 /* Empty dropdowns are not allowed */
2049 list.emplace_back(new DropDownListStringItem(STR_NONE, INVALID_RAILTYPE, true));
2052 return list;