Change: Let AI developers edit non-editable AI/Game Script Parameters (#8895)
[openttd-github.git] / src / rail_gui.cpp
blob8089c939cb6893da4f3f83839a1e07c11b4cc9d7
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"
35 #include "querystring_gui.h"
36 #include "sortlist_type.h"
37 #include "stringfilter_type.h"
38 #include "string_func.h"
39 #include "station_cmd.h"
40 #include "tunnelbridge_cmd.h"
41 #include "waypoint_cmd.h"
42 #include "rail_cmd.h"
44 #include "station_map.h"
45 #include "tunnelbridge_map.h"
47 #include "widgets/rail_widget.h"
49 #include "safeguards.h"
52 static RailType _cur_railtype; ///< Rail type of the current build-rail toolbar.
53 static bool _remove_button_clicked; ///< Flag whether 'remove' toggle-button is currently enabled
54 static DiagDirection _build_depot_direction; ///< Currently selected depot direction
55 static byte _waypoint_count = 1; ///< Number of waypoint types
56 static byte _cur_waypoint_type; ///< Currently selected waypoint type
57 static bool _convert_signal_button; ///< convert signal button in the signal GUI pressed
58 static SignalVariant _cur_signal_variant; ///< set the signal variant (for signal GUI)
59 static SignalType _cur_signal_type; ///< set the signal type (for signal GUI)
61 struct RailStationGUISettings {
62 Axis orientation; ///< Currently selected rail station orientation
64 bool newstations; ///< Are custom station definitions available?
65 StationClassID station_class; ///< Currently selected custom station class (if newstations is \c true )
66 byte station_type; ///< %Station type within the currently selected custom station class (if newstations is \c true )
67 byte station_count; ///< Number of custom stations (if newstations is \c true )
69 static RailStationGUISettings _railstation; ///< Settings of the station builder GUI
72 static void HandleStationPlacement(TileIndex start, TileIndex end);
73 static void ShowBuildTrainDepotPicker(Window *parent);
74 static void ShowBuildWaypointPicker(Window *parent);
75 static Window *ShowStationBuilder(Window *parent);
76 static void ShowSignalBuilder(Window *parent);
78 /**
79 * Check whether a station type can be build.
80 * @return true if building is allowed.
82 static bool IsStationAvailable(const StationSpec *statspec)
84 if (statspec == nullptr || !HasBit(statspec->callback_mask, CBM_STATION_AVAIL)) return true;
86 uint16 cb_res = GetStationCallback(CBID_STATION_AVAILABILITY, 0, 0, statspec, nullptr, INVALID_TILE);
87 if (cb_res == CALLBACK_FAILED) return true;
89 return Convert8bitBooleanCallback(statspec->grf_prop.grffile, CBID_STATION_AVAILABILITY, cb_res);
92 void CcPlaySound_CONSTRUCTION_RAIL(Commands cmd, const CommandCost &result, TileIndex tile)
94 if (result.Succeeded() && _settings_client.sound.confirm) SndPlayTileFx(SND_20_CONSTRUCTION_RAIL, tile);
97 static void GenericPlaceRail(TileIndex tile, Track track)
99 if (_remove_button_clicked) {
100 Command<CMD_REMOVE_SINGLE_RAIL>::Post(STR_ERROR_CAN_T_REMOVE_RAILROAD_TRACK, CcPlaySound_CONSTRUCTION_RAIL,
101 tile, track);
102 } else {
103 Command<CMD_BUILD_SINGLE_RAIL>::Post(STR_ERROR_CAN_T_BUILD_RAILROAD_TRACK, CcPlaySound_CONSTRUCTION_RAIL,
104 tile, _cur_railtype, track, _settings_client.gui.auto_remove_signals);
109 * Try to add an additional rail-track at the entrance of a depot
110 * @param tile Tile to use for adding the rail-track
111 * @param dir Direction to check for already present tracks
112 * @param track Track to add
113 * @see CcRailDepot()
115 static void PlaceExtraDepotRail(TileIndex tile, DiagDirection dir, Track track)
117 if (GetRailTileType(tile) == RAIL_TILE_DEPOT) return;
118 if (GetRailTileType(tile) == RAIL_TILE_SIGNALS && !_settings_client.gui.auto_remove_signals) return;
119 if ((GetTrackBits(tile) & DiagdirReachesTracks(dir)) == 0) return;
121 Command<CMD_BUILD_SINGLE_RAIL>::Post(tile, _cur_railtype, track, _settings_client.gui.auto_remove_signals);
124 /** Additional pieces of track to add at the entrance of a depot. */
125 static const Track _place_depot_extra_track[12] = {
126 TRACK_LEFT, TRACK_UPPER, TRACK_UPPER, TRACK_RIGHT, // First additional track for directions 0..3
127 TRACK_X, TRACK_Y, TRACK_X, TRACK_Y, // Second additional track
128 TRACK_LOWER, TRACK_LEFT, TRACK_RIGHT, TRACK_LOWER, // Third additional track
131 /** Direction to check for existing track pieces. */
132 static const DiagDirection _place_depot_extra_dir[12] = {
133 DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, DIAGDIR_SW,
134 DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE, DIAGDIR_SE,
135 DIAGDIR_NW, DIAGDIR_NE, DIAGDIR_NW, DIAGDIR_NE,
138 void CcRailDepot(Commands cmd, const CommandCost &result, TileIndex tile, RailType rt, DiagDirection dir)
140 if (result.Failed()) return;
142 if (_settings_client.sound.confirm) SndPlayTileFx(SND_20_CONSTRUCTION_RAIL, tile);
143 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
145 tile += TileOffsByDiagDir(dir);
147 if (IsTileType(tile, MP_RAILWAY)) {
148 PlaceExtraDepotRail(tile, _place_depot_extra_dir[dir], _place_depot_extra_track[dir]);
149 PlaceExtraDepotRail(tile, _place_depot_extra_dir[dir + 4], _place_depot_extra_track[dir + 4]);
150 PlaceExtraDepotRail(tile, _place_depot_extra_dir[dir + 8], _place_depot_extra_track[dir + 8]);
155 * Place a rail waypoint.
156 * @param tile Position to start dragging a waypoint.
158 static void PlaceRail_Waypoint(TileIndex tile)
160 if (_remove_button_clicked) {
161 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_REMOVE_STATION);
162 return;
165 Axis axis = GetAxisForNewWaypoint(tile);
166 if (IsValidAxis(axis)) {
167 /* Valid tile for waypoints */
168 VpStartPlaceSizing(tile, axis == AXIS_X ? VPM_X_LIMITED : VPM_Y_LIMITED, DDSP_BUILD_STATION);
169 VpSetPlaceSizingLimit(_settings_game.station.station_spread);
170 } else {
171 /* Tile where we can't build rail waypoints. This is always going to fail,
172 * but provides the user with a proper error message. */
173 Command<CMD_BUILD_RAIL_WAYPOINT>::Post(STR_ERROR_CAN_T_BUILD_TRAIN_WAYPOINT, tile, AXIS_X, 1, 1, STAT_CLASS_WAYP, 0, INVALID_STATION, false);
177 void CcStation(Commands cmd, const CommandCost &result, TileIndex tile)
179 if (result.Failed()) return;
181 if (_settings_client.sound.confirm) SndPlayTileFx(SND_20_CONSTRUCTION_RAIL, tile);
182 /* Only close the station builder window if the default station and non persistent building is chosen. */
183 if (_railstation.station_class == STAT_CLASS_DFLT && _railstation.station_type == 0 && !_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
187 * Place a rail station.
188 * @param tile Position to place or start dragging a station.
190 static void PlaceRail_Station(TileIndex tile)
192 if (_remove_button_clicked) {
193 VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED, DDSP_REMOVE_STATION);
194 VpSetPlaceSizingLimit(-1);
195 } else if (_settings_client.gui.station_dragdrop) {
196 VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED, DDSP_BUILD_STATION);
197 VpSetPlaceSizingLimit(_settings_game.station.station_spread);
198 } else {
199 int w = _settings_client.gui.station_numtracks;
200 int h = _settings_client.gui.station_platlength;
201 if (!_railstation.orientation) Swap(w, h);
203 RailStationGUISettings params = _railstation;
204 RailType rt = _cur_railtype;
205 byte numtracks = _settings_client.gui.station_numtracks;
206 byte platlength = _settings_client.gui.station_platlength;
207 bool adjacent = _ctrl_pressed;
209 auto proc = [=](bool test, StationID to_join) -> bool {
210 if (test) {
211 return Command<CMD_BUILD_RAIL_STATION>::Do(CommandFlagsToDCFlags(GetCommandFlags<CMD_BUILD_RAIL_STATION>()), tile, rt, params.orientation, numtracks, platlength, params.station_class, params.station_type, INVALID_STATION, adjacent).Succeeded();
212 } else {
213 return Command<CMD_BUILD_RAIL_STATION>::Post(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION, CcStation, tile, rt, params.orientation, numtracks, platlength, params.station_class, params.station_type, to_join, adjacent);
217 ShowSelectStationIfNeeded(TileArea(tile, w, h), proc);
222 * Build a new signal or edit/remove a present signal, use CmdBuildSingleSignal() or CmdRemoveSingleSignal() in rail_cmd.cpp
224 * @param tile The tile where the signal will build or edit
226 static void GenericPlaceSignals(TileIndex tile)
228 TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_RAIL, 0));
230 if (trackbits & TRACK_BIT_VERT) { // N-S direction
231 trackbits = (_tile_fract_coords.x <= _tile_fract_coords.y) ? TRACK_BIT_RIGHT : TRACK_BIT_LEFT;
234 if (trackbits & TRACK_BIT_HORZ) { // E-W direction
235 trackbits = (_tile_fract_coords.x + _tile_fract_coords.y <= 15) ? TRACK_BIT_UPPER : TRACK_BIT_LOWER;
238 Track track = FindFirstTrack(trackbits);
240 if (_remove_button_clicked) {
241 Command<CMD_REMOVE_SIGNALS>::Post(STR_ERROR_CAN_T_REMOVE_SIGNALS_FROM, CcPlaySound_CONSTRUCTION_RAIL, tile, track);
242 } else {
243 /* Which signals should we cycle through? */
244 SignalType cycle_start = _settings_client.gui.cycle_signal_types == SIGNAL_CYCLE_ALL && _settings_client.gui.signal_gui_mode == SIGNAL_GUI_ALL ? SIGTYPE_NORMAL : SIGTYPE_PBS;
246 if (FindWindowById(WC_BUILD_SIGNAL, 0) != nullptr) {
247 /* signal GUI is used */
248 Command<CMD_BUILD_SIGNALS>::Post(_convert_signal_button ? STR_ERROR_SIGNAL_CAN_T_CONVERT_SIGNALS_HERE : STR_ERROR_CAN_T_BUILD_SIGNALS_HERE, CcPlaySound_CONSTRUCTION_RAIL,
249 tile, track, _cur_signal_type, _cur_signal_variant, _convert_signal_button, false, _ctrl_pressed, cycle_start, SIGTYPE_LAST, 0, 0);
250 } else {
251 SignalVariant sigvar = _cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC;
252 Command<CMD_BUILD_SIGNALS>::Post(STR_ERROR_CAN_T_BUILD_SIGNALS_HERE, CcPlaySound_CONSTRUCTION_RAIL,
253 tile, track, _settings_client.gui.default_signal_type, sigvar, false, false, _ctrl_pressed, cycle_start, SIGTYPE_LAST, 0, 0);
260 * Start placing a rail bridge.
261 * @param tile Position of the first tile of the bridge.
262 * @param w Rail toolbar window.
264 static void PlaceRail_Bridge(TileIndex tile, Window *w)
266 if (IsBridgeTile(tile)) {
267 TileIndex other_tile = GetOtherTunnelBridgeEnd(tile);
268 Point pt = {0, 0};
269 w->OnPlaceMouseUp(VPM_X_OR_Y, DDSP_BUILD_BRIDGE, pt, other_tile, tile);
270 } else {
271 VpStartPlaceSizing(tile, VPM_X_OR_Y, DDSP_BUILD_BRIDGE);
275 /** Command callback for building a tunnel */
276 void CcBuildRailTunnel(Commands cmd, const CommandCost &result, TileIndex tile)
278 if (result.Succeeded()) {
279 if (_settings_client.sound.confirm) SndPlayTileFx(SND_20_CONSTRUCTION_RAIL, tile);
280 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
281 } else {
282 SetRedErrorSquare(_build_tunnel_endtile);
287 * Toggles state of the Remove button of Build rail toolbar
288 * @param w window the button belongs to
290 static void ToggleRailButton_Remove(Window *w)
292 CloseWindowById(WC_SELECT_STATION, 0);
293 w->ToggleWidgetLoweredState(WID_RAT_REMOVE);
294 w->SetWidgetDirty(WID_RAT_REMOVE);
295 _remove_button_clicked = w->IsWidgetLowered(WID_RAT_REMOVE);
296 SetSelectionRed(_remove_button_clicked);
300 * Updates the Remove button because of Ctrl state change
301 * @param w window the button belongs to
302 * @return true iff the remove button was changed
304 static bool RailToolbar_CtrlChanged(Window *w)
306 if (w->IsWidgetDisabled(WID_RAT_REMOVE)) return false;
308 /* allow ctrl to switch remove mode only for these widgets */
309 for (uint i = WID_RAT_BUILD_NS; i <= WID_RAT_BUILD_STATION; i++) {
310 if ((i <= WID_RAT_AUTORAIL || i >= WID_RAT_BUILD_WAYPOINT) && w->IsWidgetLowered(i)) {
311 ToggleRailButton_Remove(w);
312 return true;
316 return false;
321 * The "remove"-button click proc of the build-rail toolbar.
322 * @param w Build-rail toolbar window
323 * @see BuildRailToolbarWindow::OnClick()
325 static void BuildRailClick_Remove(Window *w)
327 if (w->IsWidgetDisabled(WID_RAT_REMOVE)) return;
328 ToggleRailButton_Remove(w);
329 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
331 /* handle station builder */
332 if (w->IsWidgetLowered(WID_RAT_BUILD_STATION)) {
333 if (_remove_button_clicked) {
334 /* starting drag & drop remove */
335 if (!_settings_client.gui.station_dragdrop) {
336 SetTileSelectSize(1, 1);
337 } else {
338 VpSetPlaceSizingLimit(-1);
340 } else {
341 /* starting station build mode */
342 if (!_settings_client.gui.station_dragdrop) {
343 int x = _settings_client.gui.station_numtracks;
344 int y = _settings_client.gui.station_platlength;
345 if (_railstation.orientation == 0) Swap(x, y);
346 SetTileSelectSize(x, y);
347 } else {
348 VpSetPlaceSizingLimit(_settings_game.station.station_spread);
354 static void DoRailroadTrack(Track track)
356 if (_remove_button_clicked) {
357 Command<CMD_REMOVE_RAILROAD_TRACK>::Post(STR_ERROR_CAN_T_REMOVE_RAILROAD_TRACK, CcPlaySound_CONSTRUCTION_RAIL,
358 TileVirtXY(_thd.selend.x, _thd.selend.y), TileVirtXY(_thd.selstart.x, _thd.selstart.y), track);
359 } else {
360 Command<CMD_BUILD_RAILROAD_TRACK>::Post(STR_ERROR_CAN_T_BUILD_RAILROAD_TRACK, CcPlaySound_CONSTRUCTION_RAIL,
361 TileVirtXY(_thd.selend.x, _thd.selend.y), TileVirtXY(_thd.selstart.x, _thd.selstart.y), _cur_railtype, track, _settings_client.gui.auto_remove_signals, false);
365 static void HandleAutodirPlacement()
367 Track trackstat = static_cast<Track>( _thd.drawstyle & HT_DIR_MASK); // 0..5
369 if (_thd.drawstyle & HT_RAIL) { // one tile case
370 GenericPlaceRail(TileVirtXY(_thd.selend.x, _thd.selend.y), trackstat);
371 return;
374 DoRailroadTrack(trackstat);
378 * Build new signals or remove signals or (if only one tile marked) edit a signal.
380 * If one tile marked abort and use GenericPlaceSignals()
381 * else use CmdBuildSingleSignal() or CmdRemoveSingleSignal() in rail_cmd.cpp to build many signals
383 static void HandleAutoSignalPlacement()
385 Track track = (Track)GB(_thd.drawstyle, 0, 3); // 0..5
387 if ((_thd.drawstyle & HT_DRAG_MASK) == HT_RECT) { // one tile case
388 GenericPlaceSignals(TileVirtXY(_thd.selend.x, _thd.selend.y));
389 return;
392 /* _settings_client.gui.drag_signals_density is given as a parameter such that each user
393 * in a network game can specify their own signal density */
394 if (_remove_button_clicked) {
395 Command<CMD_REMOVE_SIGNAL_TRACK>::Post(STR_ERROR_CAN_T_REMOVE_SIGNALS_FROM, CcPlaySound_CONSTRUCTION_RAIL,
396 TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y), track, _ctrl_pressed);
397 } else {
398 bool sig_gui = FindWindowById(WC_BUILD_SIGNAL, 0) != nullptr;
399 SignalType sigtype = sig_gui ? _cur_signal_type : _settings_client.gui.default_signal_type;
400 SignalVariant sigvar = sig_gui ? _cur_signal_variant : (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC);
401 Command<CMD_BUILD_SIGNAL_TRACK>::Post(STR_ERROR_CAN_T_BUILD_SIGNALS_HERE, CcPlaySound_CONSTRUCTION_RAIL,
402 TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y), track, sigtype, sigvar, false, _ctrl_pressed, !_settings_client.gui.drag_signals_fixed_distance, _settings_client.gui.drag_signals_density);
407 /** Rail toolbar management class. */
408 struct BuildRailToolbarWindow : Window {
409 RailType railtype; ///< Rail type to build.
410 int last_user_action; ///< Last started user action.
412 BuildRailToolbarWindow(WindowDesc *desc, RailType railtype) : Window(desc)
414 this->InitNested(TRANSPORT_RAIL);
415 this->SetupRailToolbar(railtype);
416 this->DisableWidget(WID_RAT_REMOVE);
417 this->last_user_action = WIDGET_LIST_END;
419 if (_settings_client.gui.link_terraform_toolbar) ShowTerraformToolbar(this);
422 void Close() override
424 if (this->IsWidgetLowered(WID_RAT_BUILD_STATION)) SetViewportCatchmentStation(nullptr, true);
425 if (_settings_client.gui.link_terraform_toolbar) CloseWindowById(WC_SCEN_LAND_GEN, 0, false);
426 this->Window::Close();
430 * Configures the rail toolbar for railtype given
431 * @param railtype the railtype to display
433 void SetupRailToolbar(RailType railtype)
435 this->railtype = railtype;
436 const RailtypeInfo *rti = GetRailTypeInfo(railtype);
438 assert(railtype < RAILTYPE_END);
439 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_NS)->widget_data = rti->gui_sprites.build_ns_rail;
440 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_X)->widget_data = rti->gui_sprites.build_x_rail;
441 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_EW)->widget_data = rti->gui_sprites.build_ew_rail;
442 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_Y)->widget_data = rti->gui_sprites.build_y_rail;
443 this->GetWidget<NWidgetCore>(WID_RAT_AUTORAIL)->widget_data = rti->gui_sprites.auto_rail;
444 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_DEPOT)->widget_data = rti->gui_sprites.build_depot;
445 this->GetWidget<NWidgetCore>(WID_RAT_CONVERT_RAIL)->widget_data = rti->gui_sprites.convert_rail;
446 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_TUNNEL)->widget_data = rti->gui_sprites.build_tunnel;
450 * Switch to another rail type.
451 * @param railtype New rail type.
453 void ModifyRailType(RailType railtype)
455 this->SetupRailToolbar(railtype);
456 this->ReInit();
459 void UpdateRemoveWidgetStatus(int clicked_widget)
461 switch (clicked_widget) {
462 case WID_RAT_REMOVE:
463 /* If it is the removal button that has been clicked, do nothing,
464 * as it is up to the other buttons to drive removal status */
465 return;
467 case WID_RAT_BUILD_NS:
468 case WID_RAT_BUILD_X:
469 case WID_RAT_BUILD_EW:
470 case WID_RAT_BUILD_Y:
471 case WID_RAT_AUTORAIL:
472 case WID_RAT_BUILD_WAYPOINT:
473 case WID_RAT_BUILD_STATION:
474 case WID_RAT_BUILD_SIGNALS:
475 /* Removal button is enabled only if the rail/signal/waypoint/station
476 * button is still lowered. Once raised, it has to be disabled */
477 this->SetWidgetDisabledState(WID_RAT_REMOVE, !this->IsWidgetLowered(clicked_widget));
478 break;
480 default:
481 /* When any other buttons than rail/signal/waypoint/station, raise and
482 * disable the removal button */
483 this->DisableWidget(WID_RAT_REMOVE);
484 this->RaiseWidget(WID_RAT_REMOVE);
485 break;
489 void SetStringParameters(int widget) const override
491 if (widget == WID_RAT_CAPTION) {
492 const RailtypeInfo *rti = GetRailTypeInfo(this->railtype);
493 if (rti->max_speed > 0) {
494 SetDParam(0, STR_TOOLBAR_RAILTYPE_VELOCITY);
495 SetDParam(1, rti->strings.toolbar_caption);
496 SetDParam(2, rti->max_speed);
497 } else {
498 SetDParam(0, rti->strings.toolbar_caption);
503 void OnClick(Point pt, int widget, int click_count) override
505 if (widget < WID_RAT_BUILD_NS) return;
507 _remove_button_clicked = false;
508 switch (widget) {
509 case WID_RAT_BUILD_NS:
510 HandlePlacePushButton(this, WID_RAT_BUILD_NS, GetRailTypeInfo(_cur_railtype)->cursor.rail_ns, HT_LINE | HT_DIR_VL);
511 this->last_user_action = widget;
512 break;
514 case WID_RAT_BUILD_X:
515 HandlePlacePushButton(this, WID_RAT_BUILD_X, GetRailTypeInfo(_cur_railtype)->cursor.rail_swne, HT_LINE | HT_DIR_X);
516 this->last_user_action = widget;
517 break;
519 case WID_RAT_BUILD_EW:
520 HandlePlacePushButton(this, WID_RAT_BUILD_EW, GetRailTypeInfo(_cur_railtype)->cursor.rail_ew, HT_LINE | HT_DIR_HL);
521 this->last_user_action = widget;
522 break;
524 case WID_RAT_BUILD_Y:
525 HandlePlacePushButton(this, WID_RAT_BUILD_Y, GetRailTypeInfo(_cur_railtype)->cursor.rail_nwse, HT_LINE | HT_DIR_Y);
526 this->last_user_action = widget;
527 break;
529 case WID_RAT_AUTORAIL:
530 HandlePlacePushButton(this, WID_RAT_AUTORAIL, GetRailTypeInfo(_cur_railtype)->cursor.autorail, HT_RAIL);
531 this->last_user_action = widget;
532 break;
534 case WID_RAT_DEMOLISH:
535 HandlePlacePushButton(this, WID_RAT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
536 this->last_user_action = widget;
537 break;
539 case WID_RAT_BUILD_DEPOT:
540 if (HandlePlacePushButton(this, WID_RAT_BUILD_DEPOT, GetRailTypeInfo(_cur_railtype)->cursor.depot, HT_RECT)) {
541 ShowBuildTrainDepotPicker(this);
542 this->last_user_action = widget;
544 break;
546 case WID_RAT_BUILD_WAYPOINT:
547 this->last_user_action = widget;
548 _waypoint_count = StationClass::Get(STAT_CLASS_WAYP)->GetSpecCount();
549 if (HandlePlacePushButton(this, WID_RAT_BUILD_WAYPOINT, SPR_CURSOR_WAYPOINT, HT_RECT) && _waypoint_count > 1) {
550 ShowBuildWaypointPicker(this);
552 break;
554 case WID_RAT_BUILD_STATION:
555 if (HandlePlacePushButton(this, WID_RAT_BUILD_STATION, SPR_CURSOR_RAIL_STATION, HT_RECT)) {
556 ShowStationBuilder(this);
557 this->last_user_action = widget;
559 break;
561 case WID_RAT_BUILD_SIGNALS: {
562 this->last_user_action = widget;
563 bool started = HandlePlacePushButton(this, WID_RAT_BUILD_SIGNALS, ANIMCURSOR_BUILDSIGNALS, HT_RECT);
564 if (started != _ctrl_pressed) {
565 ShowSignalBuilder(this);
567 break;
570 case WID_RAT_BUILD_BRIDGE:
571 HandlePlacePushButton(this, WID_RAT_BUILD_BRIDGE, SPR_CURSOR_BRIDGE, HT_RECT);
572 this->last_user_action = widget;
573 break;
575 case WID_RAT_BUILD_TUNNEL:
576 HandlePlacePushButton(this, WID_RAT_BUILD_TUNNEL, GetRailTypeInfo(_cur_railtype)->cursor.tunnel, HT_SPECIAL);
577 this->last_user_action = widget;
578 break;
580 case WID_RAT_REMOVE:
581 BuildRailClick_Remove(this);
582 break;
584 case WID_RAT_CONVERT_RAIL:
585 HandlePlacePushButton(this, WID_RAT_CONVERT_RAIL, GetRailTypeInfo(_cur_railtype)->cursor.convert, HT_RECT | HT_DIAGONAL);
586 this->last_user_action = widget;
587 break;
589 default: NOT_REACHED();
591 this->UpdateRemoveWidgetStatus(widget);
592 if (_ctrl_pressed) RailToolbar_CtrlChanged(this);
595 EventState OnHotkey(int hotkey) override
597 MarkTileDirtyByTile(TileVirtXY(_thd.pos.x, _thd.pos.y)); // redraw tile selection
598 return Window::OnHotkey(hotkey);
601 void OnPlaceObject(Point pt, TileIndex tile) override
603 switch (this->last_user_action) {
604 case WID_RAT_BUILD_NS:
605 VpStartPlaceSizing(tile, VPM_FIX_VERTICAL | VPM_RAILDIRS, DDSP_PLACE_RAIL);
606 break;
608 case WID_RAT_BUILD_X:
609 VpStartPlaceSizing(tile, VPM_FIX_Y | VPM_RAILDIRS, DDSP_PLACE_RAIL);
610 break;
612 case WID_RAT_BUILD_EW:
613 VpStartPlaceSizing(tile, VPM_FIX_HORIZONTAL | VPM_RAILDIRS, DDSP_PLACE_RAIL);
614 break;
616 case WID_RAT_BUILD_Y:
617 VpStartPlaceSizing(tile, VPM_FIX_X | VPM_RAILDIRS, DDSP_PLACE_RAIL);
618 break;
620 case WID_RAT_AUTORAIL:
621 VpStartPlaceSizing(tile, VPM_RAILDIRS, DDSP_PLACE_RAIL);
622 break;
624 case WID_RAT_DEMOLISH:
625 PlaceProc_DemolishArea(tile);
626 break;
628 case WID_RAT_BUILD_DEPOT:
629 Command<CMD_BUILD_TRAIN_DEPOT>::Post(STR_ERROR_CAN_T_BUILD_TRAIN_DEPOT, CcRailDepot, tile, _cur_railtype, _build_depot_direction);
630 break;
632 case WID_RAT_BUILD_WAYPOINT:
633 PlaceRail_Waypoint(tile);
634 break;
636 case WID_RAT_BUILD_STATION:
637 PlaceRail_Station(tile);
638 break;
640 case WID_RAT_BUILD_SIGNALS:
641 VpStartPlaceSizing(tile, VPM_SIGNALDIRS, DDSP_BUILD_SIGNALS);
642 break;
644 case WID_RAT_BUILD_BRIDGE:
645 PlaceRail_Bridge(tile, this);
646 break;
648 case WID_RAT_BUILD_TUNNEL:
649 Command<CMD_BUILD_TUNNEL>::Post(STR_ERROR_CAN_T_BUILD_TUNNEL_HERE, CcBuildRailTunnel, tile, TRANSPORT_RAIL, _cur_railtype);
650 break;
652 case WID_RAT_CONVERT_RAIL:
653 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CONVERT_RAIL);
654 break;
656 default: NOT_REACHED();
660 void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) override
662 /* no dragging if you have pressed the convert button */
663 if (FindWindowById(WC_BUILD_SIGNAL, 0) != nullptr && _convert_signal_button && this->IsWidgetLowered(WID_RAT_BUILD_SIGNALS)) return;
665 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
668 void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) override
670 if (pt.x != -1) {
671 switch (select_proc) {
672 default: NOT_REACHED();
673 case DDSP_BUILD_BRIDGE:
674 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
675 ShowBuildBridgeWindow(start_tile, end_tile, TRANSPORT_RAIL, _cur_railtype);
676 break;
678 case DDSP_PLACE_RAIL:
679 HandleAutodirPlacement();
680 break;
682 case DDSP_BUILD_SIGNALS:
683 HandleAutoSignalPlacement();
684 break;
686 case DDSP_DEMOLISH_AREA:
687 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
688 break;
690 case DDSP_CONVERT_RAIL:
691 Command<CMD_CONVERT_RAIL>::Post(STR_ERROR_CAN_T_CONVERT_RAIL, CcPlaySound_CONSTRUCTION_RAIL, end_tile, start_tile, _cur_railtype, _ctrl_pressed);
692 break;
694 case DDSP_REMOVE_STATION:
695 case DDSP_BUILD_STATION:
696 if (this->IsWidgetLowered(WID_RAT_BUILD_STATION)) {
697 /* Station */
698 if (_remove_button_clicked) {
699 bool keep_rail = !_ctrl_pressed;
700 Command<CMD_REMOVE_FROM_RAIL_STATION>::Post(STR_ERROR_CAN_T_REMOVE_PART_OF_STATION, CcPlaySound_CONSTRUCTION_RAIL, end_tile, start_tile, keep_rail);
701 } else {
702 HandleStationPlacement(start_tile, end_tile);
704 } else {
705 /* Waypoint */
706 if (_remove_button_clicked) {
707 bool keep_rail = !_ctrl_pressed;
708 Command<CMD_REMOVE_FROM_RAIL_WAYPOINT>::Post(STR_ERROR_CAN_T_REMOVE_TRAIN_WAYPOINT, CcPlaySound_CONSTRUCTION_RAIL, end_tile, start_tile, keep_rail);
709 } else {
710 TileArea ta(start_tile, end_tile);
711 Axis axis = select_method == VPM_X_LIMITED ? AXIS_X : AXIS_Y;
712 bool adjacent = _ctrl_pressed;
713 byte waypoint_type = _cur_waypoint_type;
715 auto proc = [=](bool test, StationID to_join) -> bool {
716 if (test) {
717 return Command<CMD_BUILD_RAIL_WAYPOINT>::Do(CommandFlagsToDCFlags(GetCommandFlags<CMD_BUILD_RAIL_WAYPOINT>()), ta.tile, axis, ta.w, ta.h, STAT_CLASS_WAYP, waypoint_type, INVALID_STATION, adjacent).Succeeded();
718 } else {
719 return Command<CMD_BUILD_RAIL_WAYPOINT>::Post(STR_ERROR_CAN_T_BUILD_TRAIN_WAYPOINT, CcPlaySound_CONSTRUCTION_RAIL, ta.tile, axis, ta.w, ta.h, STAT_CLASS_WAYP, waypoint_type, to_join, adjacent);
723 ShowSelectWaypointIfNeeded(ta, proc);
726 break;
731 void OnPlaceObjectAbort() override
733 if (this->IsWidgetLowered(WID_RAT_BUILD_STATION)) SetViewportCatchmentStation(nullptr, true);
735 this->RaiseButtons();
736 this->DisableWidget(WID_RAT_REMOVE);
737 this->SetWidgetDirty(WID_RAT_REMOVE);
739 CloseWindowById(WC_BUILD_SIGNAL, TRANSPORT_RAIL);
740 CloseWindowById(WC_BUILD_STATION, TRANSPORT_RAIL);
741 CloseWindowById(WC_BUILD_DEPOT, TRANSPORT_RAIL);
742 CloseWindowById(WC_BUILD_WAYPOINT, TRANSPORT_RAIL);
743 CloseWindowById(WC_SELECT_STATION, 0);
744 CloseWindowByClass(WC_BUILD_BRIDGE);
747 void OnPlacePresize(Point pt, TileIndex tile) override
749 Command<CMD_BUILD_TUNNEL>::Do(DC_AUTO, tile, TRANSPORT_RAIL, _cur_railtype);
750 VpSetPresizeRange(tile, _build_tunnel_endtile == 0 ? tile : _build_tunnel_endtile);
753 EventState OnCTRLStateChange() override
755 /* do not toggle Remove button by Ctrl when placing station */
756 if (!this->IsWidgetLowered(WID_RAT_BUILD_STATION) && !this->IsWidgetLowered(WID_RAT_BUILD_WAYPOINT) && RailToolbar_CtrlChanged(this)) return ES_HANDLED;
757 return ES_NOT_HANDLED;
760 static HotkeyList hotkeys;
764 * Handler for global hotkeys of the BuildRailToolbarWindow.
765 * @param hotkey Hotkey
766 * @return ES_HANDLED if hotkey was accepted.
768 static EventState RailToolbarGlobalHotkeys(int hotkey)
770 if (_game_mode != GM_NORMAL) return ES_NOT_HANDLED;
771 extern RailType _last_built_railtype;
772 Window *w = ShowBuildRailToolbar(_last_built_railtype);
773 if (w == nullptr) return ES_NOT_HANDLED;
774 return w->OnHotkey(hotkey);
777 const uint16 _railtoolbar_autorail_keys[] = {'5', 'A' | WKC_GLOBAL_HOTKEY, 0};
779 static Hotkey railtoolbar_hotkeys[] = {
780 Hotkey('1', "build_ns", WID_RAT_BUILD_NS),
781 Hotkey('2', "build_x", WID_RAT_BUILD_X),
782 Hotkey('3', "build_ew", WID_RAT_BUILD_EW),
783 Hotkey('4', "build_y", WID_RAT_BUILD_Y),
784 Hotkey(_railtoolbar_autorail_keys, "autorail", WID_RAT_AUTORAIL),
785 Hotkey('6', "demolish", WID_RAT_DEMOLISH),
786 Hotkey('7', "depot", WID_RAT_BUILD_DEPOT),
787 Hotkey('8', "waypoint", WID_RAT_BUILD_WAYPOINT),
788 Hotkey('9', "station", WID_RAT_BUILD_STATION),
789 Hotkey('S', "signal", WID_RAT_BUILD_SIGNALS),
790 Hotkey('B', "bridge", WID_RAT_BUILD_BRIDGE),
791 Hotkey('T', "tunnel", WID_RAT_BUILD_TUNNEL),
792 Hotkey('R', "remove", WID_RAT_REMOVE),
793 Hotkey('C', "convert", WID_RAT_CONVERT_RAIL),
794 HOTKEY_LIST_END
796 HotkeyList BuildRailToolbarWindow::hotkeys("railtoolbar", railtoolbar_hotkeys, RailToolbarGlobalHotkeys);
798 static const NWidgetPart _nested_build_rail_widgets[] = {
799 NWidget(NWID_HORIZONTAL),
800 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
801 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_RAT_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
802 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
803 EndContainer(),
804 NWidget(NWID_HORIZONTAL),
805 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_NS),
806 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NS, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
807 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_X),
808 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NE, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
809 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_EW),
810 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_EW, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
811 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_Y),
812 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NW, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
813 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_AUTORAIL),
814 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_AUTORAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_AUTORAIL),
816 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), EndContainer(),
818 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_DEMOLISH),
819 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
820 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_DEPOT),
821 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DEPOT_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_TRAIN_DEPOT_FOR_BUILDING),
822 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_WAYPOINT),
823 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_WAYPOINT, STR_RAIL_TOOLBAR_TOOLTIP_CONVERT_RAIL_TO_WAYPOINT),
824 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_STATION),
825 SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_RAIL_STATION, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_STATION),
826 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_SIGNALS),
827 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_SIGNALS, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_SIGNALS),
828 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_BRIDGE),
829 SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_BRIDGE, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_BRIDGE),
830 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_TUNNEL),
831 SetFill(0, 1), SetMinimalSize(20, 22), SetDataTip(SPR_IMG_TUNNEL_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TUNNEL),
832 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_REMOVE),
833 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_REMOVE, STR_RAIL_TOOLBAR_TOOLTIP_TOGGLE_BUILD_REMOVE_FOR),
834 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_CONVERT_RAIL),
835 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_CONVERT_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_CONVERT_RAIL),
836 EndContainer(),
839 static WindowDesc _build_rail_desc(
840 WDP_ALIGN_TOOLBAR, "toolbar_rail", 0, 0,
841 WC_BUILD_TOOLBAR, WC_NONE,
842 WDF_CONSTRUCTION,
843 _nested_build_rail_widgets, lengthof(_nested_build_rail_widgets),
844 &BuildRailToolbarWindow::hotkeys
849 * Open the build rail toolbar window for a specific rail type.
851 * If the terraform toolbar is linked to the toolbar, that window is also opened.
853 * @param railtype Rail type to open the window for
854 * @return newly opened rail toolbar, or nullptr if the toolbar could not be opened.
856 Window *ShowBuildRailToolbar(RailType railtype)
858 if (!Company::IsValidID(_local_company)) return nullptr;
859 if (!ValParamRailtype(railtype)) return nullptr;
861 CloseWindowByClass(WC_BUILD_TOOLBAR);
862 _cur_railtype = railtype;
863 _remove_button_clicked = false;
864 return new BuildRailToolbarWindow(&_build_rail_desc, railtype);
867 /* TODO: For custom stations, respect their allowed platforms/lengths bitmasks!
868 * --pasky */
870 static void HandleStationPlacement(TileIndex start, TileIndex end)
872 TileArea ta(start, end);
873 uint numtracks = ta.w;
874 uint platlength = ta.h;
876 if (_railstation.orientation == AXIS_X) Swap(numtracks, platlength);
878 RailStationGUISettings params = _railstation;
879 RailType rt = _cur_railtype;
880 bool adjacent = _ctrl_pressed;
882 auto proc = [=](bool test, StationID to_join) -> bool {
883 if (test) {
884 return Command<CMD_BUILD_RAIL_STATION>::Do(CommandFlagsToDCFlags(GetCommandFlags<CMD_BUILD_RAIL_STATION>()), ta.tile, rt, params.orientation, numtracks, platlength, params.station_class, params.station_type, INVALID_STATION, adjacent).Succeeded();
885 } else {
886 return Command<CMD_BUILD_RAIL_STATION>::Post(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION, CcStation, ta.tile, rt, params.orientation, numtracks, platlength, params.station_class, params.station_type, to_join, adjacent);
890 ShowSelectStationIfNeeded(ta, proc);
893 /** Enum referring to the Hotkeys in the build rail station window */
894 enum BuildRalStationHotkeys {
895 BRASHK_FOCUS_FILTER_BOX, ///< Focus the edit box for editing the filter string
898 struct BuildRailStationWindow : public PickerWindowBase {
899 private:
900 uint line_height; ///< Height of a single line in the newstation selection matrix (#WID_BRAS_NEWST_LIST widget).
901 uint coverage_height; ///< Height of the coverage texts.
902 Scrollbar *vscroll; ///< Vertical scrollbar of the new station list.
903 Scrollbar *vscroll2; ///< Vertical scrollbar of the matrix with new stations.
905 typedef GUIList<StationClassID, StringFilter &> GUIStationClassList; ///< Type definition for the list to hold available station classes.
907 static const uint EDITBOX_MAX_SIZE = 16; ///< The maximum number of characters for the filter edit box.
909 static Listing last_sorting; ///< Default sorting of #GUIStationClassList.
910 static Filtering last_filtering; ///< Default filtering of #GUIStationClassList.
911 static GUIStationClassList::SortFunction * const sorter_funcs[]; ///< Sort functions of the #GUIStationClassList.
912 static GUIStationClassList::FilterFunction * const filter_funcs[]; ///< Filter functions of the #GUIStationClassList.
913 GUIStationClassList station_classes; ///< Available station classes.
914 StringFilter string_filter; ///< Filter for available station classes.
915 QueryString filter_editbox; ///< Filter editbox.
918 * Scrolls #WID_BRAS_NEWST_SCROLL so that the selected station class is visible.
920 * Note that this method should be called shortly after SelectClassAndStation() which will ensure
921 * an actual existing station class is selected, or the one at position 0 which will always be
922 * the default TTD rail station.
924 void EnsureSelectedStationClassIsVisible()
926 uint pos = 0;
927 for (auto station_class : this->station_classes) {
928 if (station_class == _railstation.station_class) break;
929 pos++;
931 this->vscroll->SetCount((int)this->station_classes.size());
932 this->vscroll->ScrollTowards(pos);
936 * Verify whether the currently selected station size is allowed after selecting a new station class/type.
937 * If not, change the station size variables ( _settings_client.gui.station_numtracks and _settings_client.gui.station_platlength ).
938 * @param statspec Specification of the new station class/type
940 void CheckSelectedSize(const StationSpec *statspec)
942 if (statspec == nullptr || _settings_client.gui.station_dragdrop) return;
944 /* If current number of tracks is not allowed, make it as big as possible */
945 if (HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
946 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
947 _settings_client.gui.station_numtracks = 1;
948 if (statspec->disallowed_platforms != UINT8_MAX) {
949 while (HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
950 _settings_client.gui.station_numtracks++;
952 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
956 if (HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
957 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
958 _settings_client.gui.station_platlength = 1;
959 if (statspec->disallowed_lengths != UINT8_MAX) {
960 while (HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
961 _settings_client.gui.station_platlength++;
963 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
968 public:
969 BuildRailStationWindow(WindowDesc *desc, Window *parent, bool newstation) : PickerWindowBase(desc, parent), filter_editbox(EDITBOX_MAX_SIZE * MAX_CHAR_LENGTH, EDITBOX_MAX_SIZE)
971 this->coverage_height = 2 * FONT_HEIGHT_NORMAL + 3 * WD_PAR_VSEP_NORMAL;
972 this->vscroll = nullptr;
973 _railstation.newstations = newstation;
975 this->CreateNestedTree();
976 NWidgetStacked *newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_ADDITIONS);
977 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
978 newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_MATRIX);
979 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
980 newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_DEFSIZE);
981 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
982 newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_RESIZE);
983 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
984 /* Hide the station class filter if no stations other than the default one are available. */
985 this->GetWidget<NWidgetStacked>(WID_BRAS_FILTER_CONTAINER)->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
986 if (newstation) {
987 this->vscroll = this->GetScrollbar(WID_BRAS_NEWST_SCROLL);
988 this->vscroll2 = this->GetScrollbar(WID_BRAS_MATRIX_SCROLL);
990 this->querystrings[WID_BRAS_FILTER_EDITBOX] = &this->filter_editbox;
991 this->station_classes.SetListing(this->last_sorting);
992 this->station_classes.SetFiltering(this->last_filtering);
993 this->station_classes.SetSortFuncs(this->sorter_funcs);
994 this->station_classes.SetFilterFuncs(this->filter_funcs);
997 this->station_classes.ForceRebuild();
999 BuildStationClassesAvailable();
1000 SelectClassAndStation();
1002 this->FinishInitNested(TRANSPORT_RAIL);
1004 this->LowerWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
1005 if (_settings_client.gui.station_dragdrop) {
1006 this->LowerWidget(WID_BRAS_PLATFORM_DRAG_N_DROP);
1007 } else {
1008 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1009 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1011 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_OFF, !_settings_client.gui.station_show_coverage);
1012 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_ON, _settings_client.gui.station_show_coverage);
1014 if (!newstation) {
1015 _railstation.station_class = StationClassID::STAT_CLASS_DFLT;
1016 _railstation.station_type = 0;
1017 this->vscroll2 = nullptr;
1018 } else {
1019 _railstation.station_count = StationClass::Get(_railstation.station_class)->GetSpecCount();
1020 _railstation.station_type = std::min<int>(_railstation.station_type, _railstation.station_count - 1);
1022 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX);
1023 matrix->SetScrollbar(this->vscroll2);
1024 matrix->SetCount(_railstation.station_count);
1025 matrix->SetClicked(_railstation.station_type);
1027 EnsureSelectedStationClassIsVisible();
1030 this->InvalidateData();
1033 void Close() override
1035 CloseWindowById(WC_SELECT_STATION, 0);
1036 this->PickerWindowBase::Close();
1039 /** Sort station classes by StationClassID. */
1040 static bool StationClassIDSorter(StationClassID const &a, StationClassID const &b)
1042 return a < b;
1045 /** Filter station classes by class name. */
1046 static bool CDECL TagNameFilter(StationClassID const * sc, StringFilter &filter)
1048 char buffer[DRAW_STRING_BUFFER];
1049 GetString(buffer, StationClass::Get(*sc)->name, lastof(buffer));
1051 filter.ResetState();
1052 filter.AddLine(buffer);
1053 return filter.GetState();
1056 /** Builds the filter list of available station classes. */
1057 void BuildStationClassesAvailable()
1059 if (!this->station_classes.NeedRebuild()) return;
1061 this->station_classes.clear();
1063 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1064 StationClassID station_class_id = (StationClassID)i;
1065 if (station_class_id == StationClassID::STAT_CLASS_WAYP) {
1066 // Skip waypoints.
1067 continue;
1069 StationClass *station_class = StationClass::Get(station_class_id);
1070 if (station_class->GetUISpecCount() == 0) continue;
1071 station_classes.push_back(station_class_id);
1074 if (_railstation.newstations) {
1075 this->station_classes.Filter(this->string_filter);
1076 this->station_classes.shrink_to_fit();
1077 this->station_classes.RebuildDone();
1078 this->station_classes.Sort();
1080 this->vscroll->SetCount((uint)this->station_classes.size());
1085 * Checks if the previously selected current station class and station
1086 * can be shown as selected to the user when the dialog is opened.
1088 void SelectClassAndStation()
1090 if (_railstation.station_class == StationClassID::STAT_CLASS_DFLT) {
1091 /* This happens during the first time the window is open during the game life cycle. */
1092 this->SelectOtherClass(StationClassID::STAT_CLASS_DFLT);
1093 } else {
1094 /* Check if the previously selected station class is not available anymore as a
1095 * result of starting a new game without the corresponding NewGRF. */
1096 bool available = false;
1097 for (uint i = 0; i < StationClass::GetClassCount(); ++i) {
1098 if ((StationClassID)i == _railstation.station_class) {
1099 available = true;
1100 break;
1104 this->SelectOtherClass(available ? _railstation.station_class : StationClassID::STAT_CLASS_DFLT);
1109 * Select the specified station class.
1110 * @param station_class Station class select.
1112 void SelectOtherClass(StationClassID station_class)
1114 _railstation.station_class = station_class;
1117 void OnInvalidateData(int data = 0, bool gui_scope = true) override
1119 if (!gui_scope) return;
1121 this->BuildStationClassesAvailable();
1124 EventState OnHotkey(int hotkey) override
1126 switch (hotkey) {
1127 case BRASHK_FOCUS_FILTER_BOX:
1128 this->SetFocusedWidget(WID_BRAS_FILTER_EDITBOX);
1129 SetFocusedWindow(this); // The user has asked to give focus to the text box, so make sure this window is focused.
1130 break;
1132 default:
1133 return ES_NOT_HANDLED;
1136 return ES_HANDLED;
1139 void OnEditboxChanged(int wid) override
1141 string_filter.SetFilterTerm(this->filter_editbox.text.buf);
1142 this->station_classes.SetFilterState(!string_filter.IsEmpty());
1143 this->station_classes.ForceRebuild();
1144 this->InvalidateData();
1147 void OnPaint() override
1149 bool newstations = _railstation.newstations;
1150 const StationSpec *statspec = newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : nullptr;
1152 if (_settings_client.gui.station_dragdrop) {
1153 SetTileSelectSize(1, 1);
1154 } else {
1155 int x = _settings_client.gui.station_numtracks;
1156 int y = _settings_client.gui.station_platlength;
1157 if (_railstation.orientation == AXIS_X) Swap(x, y);
1158 if (!_remove_button_clicked) {
1159 SetTileSelectSize(x, y);
1163 int rad = (_settings_game.station.modified_catchment) ? CA_TRAIN : CA_UNMODIFIED;
1165 if (_settings_client.gui.station_show_coverage) SetTileSelectBigSize(-rad, -rad, 2 * rad, 2 * rad);
1167 for (uint bits = 0; bits < 7; bits++) {
1168 bool disable = bits >= _settings_game.station.station_spread;
1169 if (statspec == nullptr) {
1170 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_NUM_1, disable);
1171 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_LEN_1, disable);
1172 } else {
1173 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_NUM_1, HasBit(statspec->disallowed_platforms, bits) || disable);
1174 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_LEN_1, HasBit(statspec->disallowed_lengths, bits) || disable);
1178 this->DrawWidgets();
1180 if (this->IsShaded()) return;
1181 /* 'Accepts' and 'Supplies' texts. */
1182 Rect r = this->GetWidget<NWidgetBase>(WID_BRAS_COVERAGE_TEXTS)->GetCurrentRect();
1183 int top = r.top + ScaleGUITrad(WD_PAR_VSEP_NORMAL);
1184 top = DrawStationCoverageAreaText(r.left, r.right, top, SCT_ALL, rad, false) + ScaleGUITrad(WD_PAR_VSEP_NORMAL);
1185 top = DrawStationCoverageAreaText(r.left, r.right, top, SCT_ALL, rad, true) + ScaleGUITrad(WD_PAR_VSEP_NORMAL);
1186 /* Resize background if the window is too small.
1187 * Never make the window smaller to avoid oscillating if the size change affects the acceptance.
1188 * (This is the case, if making the window bigger moves the mouse into the window.) */
1189 if (top > r.bottom) {
1190 this->coverage_height += top - r.bottom;
1191 this->ReInit();
1195 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1197 switch (widget) {
1198 case WID_BRAS_NEWST_LIST: {
1199 Dimension d = {0, 0};
1200 for (auto station_class : this->station_classes) {
1201 d = maxdim(d, GetStringBoundingBox(StationClass::Get(station_class)->name));
1203 size->width = std::max(size->width, d.width + padding.width);
1204 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
1205 size->height = 5 * this->line_height;
1206 resize->height = this->line_height;
1207 break;
1210 case WID_BRAS_SHOW_NEWST_TYPE: {
1211 if (!_railstation.newstations) {
1212 size->width = 0;
1213 size->height = 0;
1214 break;
1217 /* If newstations exist, compute the non-zero minimal size. */
1218 Dimension d = {0, 0};
1219 StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
1220 for (auto station_class : this->station_classes) {
1221 StationClass *stclass = StationClass::Get(station_class);
1222 for (uint j = 0; j < stclass->GetSpecCount(); j++) {
1223 const StationSpec *statspec = stclass->GetSpec(j);
1224 SetDParam(0, (statspec != nullptr && statspec->name != 0) ? statspec->name : STR_STATION_CLASS_DFLT);
1225 d = maxdim(d, GetStringBoundingBox(str));
1228 size->width = std::max(size->width, d.width + padding.width);
1229 break;
1232 case WID_BRAS_PLATFORM_DIR_X:
1233 case WID_BRAS_PLATFORM_DIR_Y:
1234 case WID_BRAS_IMAGE:
1235 size->width = ScaleGUITrad(64) + 2;
1236 size->height = ScaleGUITrad(58) + 2;
1237 break;
1239 case WID_BRAS_COVERAGE_TEXTS:
1240 size->height = this->coverage_height;
1241 break;
1243 case WID_BRAS_MATRIX:
1244 fill->height = 1;
1245 resize->height = 1;
1246 break;
1250 void DrawWidget(const Rect &r, int widget) const override
1252 DrawPixelInfo tmp_dpi;
1254 switch (GB(widget, 0, 16)) {
1255 case WID_BRAS_PLATFORM_DIR_X:
1256 /* Set up a clipping area for the '/' station preview */
1257 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1258 DrawPixelInfo *old_dpi = _cur_dpi;
1259 _cur_dpi = &tmp_dpi;
1260 int x = ScaleGUITrad(31) + 1;
1261 int y = r.bottom - r.top - ScaleGUITrad(31);
1262 if (!DrawStationTile(x, y, _cur_railtype, AXIS_X, _railstation.station_class, _railstation.station_type)) {
1263 StationPickerDrawSprite(x, y, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2);
1265 _cur_dpi = old_dpi;
1267 break;
1269 case WID_BRAS_PLATFORM_DIR_Y:
1270 /* Set up a clipping area for the '\' station preview */
1271 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1272 DrawPixelInfo *old_dpi = _cur_dpi;
1273 _cur_dpi = &tmp_dpi;
1274 int x = ScaleGUITrad(31) + 1;
1275 int y = r.bottom - r.top - ScaleGUITrad(31);
1276 if (!DrawStationTile(x, y, _cur_railtype, AXIS_Y, _railstation.station_class, _railstation.station_type)) {
1277 StationPickerDrawSprite(x, y, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 3);
1279 _cur_dpi = old_dpi;
1281 break;
1283 case WID_BRAS_NEWST_LIST: {
1284 uint statclass = 0;
1285 uint row = 0;
1286 for (auto station_class : this->station_classes) {
1287 if (this->vscroll->IsVisible(statclass)) {
1288 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, row * this->line_height + r.top + WD_MATRIX_TOP,
1289 StationClass::Get(station_class)->name,
1290 station_class == _railstation.station_class ? TC_WHITE : TC_BLACK);
1291 row++;
1293 statclass++;
1295 break;
1298 case WID_BRAS_IMAGE: {
1299 byte type = GB(widget, 16, 16);
1300 assert(type < _railstation.station_count);
1301 /* Check station availability callback */
1302 const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(type);
1303 if (!IsStationAvailable(statspec)) {
1304 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
1307 /* Set up a clipping area for the station preview. */
1308 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1309 DrawPixelInfo *old_dpi = _cur_dpi;
1310 _cur_dpi = &tmp_dpi;
1311 int x = ScaleGUITrad(31) + 1;
1312 int y = r.bottom - r.top - ScaleGUITrad(31);
1313 if (!DrawStationTile(x, y, _cur_railtype, _railstation.orientation, _railstation.station_class, type)) {
1314 StationPickerDrawSprite(x, y, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2 + _railstation.orientation);
1316 _cur_dpi = old_dpi;
1318 break;
1323 void OnResize() override
1325 if (this->vscroll != nullptr) { // New stations available.
1326 this->vscroll->SetCapacityFromWidget(this, WID_BRAS_NEWST_LIST);
1330 void SetStringParameters(int widget) const override
1332 if (widget == WID_BRAS_SHOW_NEWST_TYPE) {
1333 const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type);
1334 SetDParam(0, (statspec != nullptr && statspec->name != 0) ? statspec->name : STR_STATION_CLASS_DFLT);
1338 void OnClick(Point pt, int widget, int click_count) override
1340 switch (GB(widget, 0, 16)) {
1341 case WID_BRAS_PLATFORM_DIR_X:
1342 case WID_BRAS_PLATFORM_DIR_Y:
1343 this->RaiseWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
1344 _railstation.orientation = (Axis)(widget - WID_BRAS_PLATFORM_DIR_X);
1345 this->LowerWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
1346 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1347 this->SetDirty();
1348 CloseWindowById(WC_SELECT_STATION, 0);
1349 break;
1351 case WID_BRAS_PLATFORM_NUM_1:
1352 case WID_BRAS_PLATFORM_NUM_2:
1353 case WID_BRAS_PLATFORM_NUM_3:
1354 case WID_BRAS_PLATFORM_NUM_4:
1355 case WID_BRAS_PLATFORM_NUM_5:
1356 case WID_BRAS_PLATFORM_NUM_6:
1357 case WID_BRAS_PLATFORM_NUM_7: {
1358 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1359 this->RaiseWidget(WID_BRAS_PLATFORM_DRAG_N_DROP);
1361 _settings_client.gui.station_numtracks = widget - WID_BRAS_PLATFORM_NUM_BEGIN;
1362 _settings_client.gui.station_dragdrop = false;
1364 _settings_client.gui.station_dragdrop = false;
1366 const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : nullptr;
1367 if (statspec != nullptr && HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
1368 /* The previously selected number of platforms in invalid */
1369 for (uint i = 0; i < 7; i++) {
1370 if (!HasBit(statspec->disallowed_lengths, i)) {
1371 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1372 _settings_client.gui.station_platlength = i + 1;
1373 break;
1378 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1379 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1380 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1381 this->SetDirty();
1382 CloseWindowById(WC_SELECT_STATION, 0);
1383 break;
1386 case WID_BRAS_PLATFORM_LEN_1:
1387 case WID_BRAS_PLATFORM_LEN_2:
1388 case WID_BRAS_PLATFORM_LEN_3:
1389 case WID_BRAS_PLATFORM_LEN_4:
1390 case WID_BRAS_PLATFORM_LEN_5:
1391 case WID_BRAS_PLATFORM_LEN_6:
1392 case WID_BRAS_PLATFORM_LEN_7: {
1393 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1394 this->RaiseWidget(WID_BRAS_PLATFORM_DRAG_N_DROP);
1396 _settings_client.gui.station_platlength = widget - WID_BRAS_PLATFORM_LEN_BEGIN;
1397 _settings_client.gui.station_dragdrop = false;
1399 _settings_client.gui.station_dragdrop = false;
1401 const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : nullptr;
1402 if (statspec != nullptr && HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
1403 /* The previously selected number of tracks in invalid */
1404 for (uint i = 0; i < 7; i++) {
1405 if (!HasBit(statspec->disallowed_platforms, i)) {
1406 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1407 _settings_client.gui.station_numtracks = i + 1;
1408 break;
1413 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1414 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1415 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1416 this->SetDirty();
1417 CloseWindowById(WC_SELECT_STATION, 0);
1418 break;
1421 case WID_BRAS_PLATFORM_DRAG_N_DROP: {
1422 _settings_client.gui.station_dragdrop ^= true;
1424 this->ToggleWidgetLoweredState(WID_BRAS_PLATFORM_DRAG_N_DROP);
1426 /* get the first allowed length/number of platforms */
1427 const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : nullptr;
1428 if (statspec != nullptr && HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
1429 for (uint i = 0; i < 7; i++) {
1430 if (!HasBit(statspec->disallowed_lengths, i)) {
1431 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1432 _settings_client.gui.station_platlength = i + 1;
1433 break;
1437 if (statspec != nullptr && HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
1438 for (uint i = 0; i < 7; i++) {
1439 if (!HasBit(statspec->disallowed_platforms, i)) {
1440 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1441 _settings_client.gui.station_numtracks = i + 1;
1442 break;
1447 this->SetWidgetLoweredState(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN, !_settings_client.gui.station_dragdrop);
1448 this->SetWidgetLoweredState(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN, !_settings_client.gui.station_dragdrop);
1449 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1450 this->SetDirty();
1451 CloseWindowById(WC_SELECT_STATION, 0);
1452 break;
1455 case WID_BRAS_HIGHLIGHT_OFF:
1456 case WID_BRAS_HIGHLIGHT_ON:
1457 _settings_client.gui.station_show_coverage = (widget != WID_BRAS_HIGHLIGHT_OFF);
1459 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_OFF, !_settings_client.gui.station_show_coverage);
1460 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_ON, _settings_client.gui.station_show_coverage);
1461 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1462 this->SetDirty();
1463 SetViewportCatchmentStation(nullptr, true);
1464 break;
1466 case WID_BRAS_NEWST_LIST: {
1467 int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_BRAS_NEWST_LIST);
1468 if (y >= (int)this->station_classes.size()) return;
1469 StationClassID station_class_id = this->station_classes[y];
1470 if (_railstation.station_class != station_class_id) {
1471 StationClass *station_class = StationClass::Get(station_class_id);
1472 _railstation.station_class = station_class_id;
1473 _railstation.station_count = station_class->GetSpecCount();
1474 _railstation.station_type = 0;
1476 this->CheckSelectedSize(station_class->GetSpec(_railstation.station_type));
1478 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX);
1479 matrix->SetCount(_railstation.station_count);
1480 matrix->SetClicked(_railstation.station_type);
1482 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1483 this->SetDirty();
1484 CloseWindowById(WC_SELECT_STATION, 0);
1485 break;
1488 case WID_BRAS_IMAGE: {
1489 int y = GB(widget, 16, 16);
1490 if (y >= _railstation.station_count) return;
1492 /* Check station availability callback */
1493 const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(y);
1494 if (!IsStationAvailable(statspec)) return;
1496 _railstation.station_type = y;
1498 this->CheckSelectedSize(statspec);
1499 this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX)->SetClicked(_railstation.station_type);
1501 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1502 this->SetDirty();
1503 CloseWindowById(WC_SELECT_STATION, 0);
1504 break;
1509 void OnRealtimeTick(uint delta_ms) override
1511 CheckRedrawStationCoverage(this);
1514 static HotkeyList hotkeys;
1518 * Handler for global hotkeys of the BuildRailStationWindow.
1519 * @param hotkey Hotkey
1520 * @return ES_HANDLED if hotkey was accepted.
1522 static EventState BuildRailStationGlobalHotkeys(int hotkey)
1524 if (_game_mode == GM_MENU) return ES_NOT_HANDLED;
1525 Window *w = ShowStationBuilder(FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL));
1526 if (w == nullptr) return ES_NOT_HANDLED;
1527 return w->OnHotkey(hotkey);
1530 static Hotkey buildrailstation_hotkeys[] = {
1531 Hotkey('F', "focus_filter_box", BRASHK_FOCUS_FILTER_BOX),
1532 HOTKEY_LIST_END
1534 HotkeyList BuildRailStationWindow::hotkeys("buildrailstation", buildrailstation_hotkeys, BuildRailStationGlobalHotkeys);
1536 Listing BuildRailStationWindow::last_sorting = { false, 0 };
1537 Filtering BuildRailStationWindow::last_filtering = { false, 0 };
1539 BuildRailStationWindow::GUIStationClassList::SortFunction * const BuildRailStationWindow::sorter_funcs[] = {
1540 &StationClassIDSorter,
1543 BuildRailStationWindow::GUIStationClassList::FilterFunction * const BuildRailStationWindow::filter_funcs[] = {
1544 &TagNameFilter,
1547 static const NWidgetPart _nested_station_builder_widgets[] = {
1548 NWidget(NWID_HORIZONTAL),
1549 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1550 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_STATION_BUILD_RAIL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1551 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
1552 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_DEFSIZE),
1553 NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
1554 EndContainer(),
1555 EndContainer(),
1556 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1557 NWidget(NWID_HORIZONTAL), SetPadding(2, 0, 0, 2),
1558 NWidget(NWID_VERTICAL),
1559 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_FILTER_CONTAINER),
1560 NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 2, 0),
1561 NWidget(WWT_TEXT, COLOUR_DARK_GREEN), SetFill(0, 1), SetDataTip(STR_LIST_FILTER_TITLE, STR_NULL),
1562 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_BRAS_FILTER_EDITBOX), SetFill(1, 0), SetResize(1, 0),
1563 SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
1564 EndContainer(),
1565 EndContainer(),
1566 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_ADDITIONS),
1567 NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 2, 0),
1568 NWidget(WWT_MATRIX, COLOUR_GREY, WID_BRAS_NEWST_LIST), SetMinimalSize(122, 71), SetFill(1, 0),
1569 SetMatrixDataTip(1, 0, STR_STATION_BUILD_STATION_CLASS_TOOLTIP), SetScrollbar(WID_BRAS_NEWST_SCROLL),
1570 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_BRAS_NEWST_SCROLL),
1571 EndContainer(),
1572 EndContainer(),
1573 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_ORIENTATION, STR_NULL), SetPadding(1, 2, 0, 0),
1574 NWidget(NWID_HORIZONTAL),
1575 NWidget(NWID_SPACER), SetMinimalSize(7, 0), SetFill(1, 0),
1576 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(),
1577 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1578 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(),
1579 NWidget(NWID_SPACER), SetMinimalSize(7, 0), SetFill(1, 0),
1580 EndContainer(),
1581 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),
1582 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_NUMBER_OF_TRACKS, STR_NULL), SetPadding(0, 2, 0, 2),
1583 NWidget(NWID_HORIZONTAL),
1584 NWidget(NWID_SPACER), SetFill(1, 0),
1585 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_1), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_1, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1586 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_2), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_2, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1587 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_3), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_3, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1588 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_4), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_4, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1589 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_5), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_5, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1590 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_6), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_6, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1591 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_7), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_7, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1592 NWidget(NWID_SPACER), SetFill(1, 0),
1593 EndContainer(),
1594 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_PLATFORM_LENGTH, STR_NULL), SetPadding(2, 2, 0, 2),
1595 NWidget(NWID_HORIZONTAL),
1596 NWidget(NWID_SPACER), SetFill(1, 0),
1597 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_1), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_1, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1598 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_2), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_2, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1599 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_3), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_3, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1600 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_4), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_4, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1601 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_5), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_5, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1602 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_6), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_6, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1603 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_7), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_7, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1604 NWidget(NWID_SPACER), SetFill(1, 0),
1605 EndContainer(),
1606 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1607 NWidget(NWID_HORIZONTAL),
1608 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1609 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),
1610 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1611 EndContainer(),
1612 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetDataTip(STR_STATION_BUILD_COVERAGE_AREA_TITLE, STR_NULL), SetPadding(WD_FRAMERECT_TOP, WD_FRAMERECT_RIGHT, WD_FRAMERECT_BOTTOM, WD_FRAMERECT_LEFT), SetFill(1, 0),
1613 NWidget(NWID_HORIZONTAL),
1614 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1615 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_HIGHLIGHT_OFF), SetMinimalSize(60, 12),
1616 SetDataTip(STR_STATION_BUILD_COVERAGE_OFF, STR_STATION_BUILD_COVERAGE_AREA_OFF_TOOLTIP),
1617 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_HIGHLIGHT_ON), SetMinimalSize(60, 12),
1618 SetDataTip(STR_STATION_BUILD_COVERAGE_ON, STR_STATION_BUILD_COVERAGE_AREA_ON_TOOLTIP),
1619 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1620 EndContainer(),
1621 EndContainer(),
1622 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_MATRIX),
1623 /* We need an additional background for the matrix, as the matrix cannot handle the scrollbar due to not being an NWidgetCore. */
1624 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(WID_BRAS_MATRIX_SCROLL),
1625 NWidget(NWID_HORIZONTAL),
1626 NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BRAS_MATRIX), SetScrollbar(WID_BRAS_MATRIX_SCROLL), SetPIP(0, 2, 0),
1627 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BRAS_IMAGE), SetMinimalSize(66, 60),
1628 SetFill(0, 0), SetResize(0, 0), SetDataTip(0x0, STR_STATION_BUILD_STATION_TYPE_TOOLTIP), SetScrollbar(WID_BRAS_MATRIX_SCROLL),
1629 EndContainer(),
1630 EndContainer(),
1631 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BRAS_MATRIX_SCROLL),
1632 EndContainer(),
1633 EndContainer(),
1634 EndContainer(),
1635 EndContainer(),
1636 NWidget(NWID_HORIZONTAL),
1637 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BRAS_COVERAGE_TEXTS), SetPadding(0, WD_FRAMERECT_RIGHT, 0, WD_FRAMERECT_LEFT), SetFill(1, 1), SetResize(1, 0),
1638 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_RESIZE),
1639 NWidget(NWID_VERTICAL),
1640 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(0, 1), EndContainer(),
1641 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
1642 EndContainer(),
1643 EndContainer(),
1644 EndContainer(),
1645 EndContainer(),
1648 /** High level window description of the station-build window (default & newGRF) */
1649 static WindowDesc _station_builder_desc(
1650 WDP_AUTO, "build_station_rail", 350, 0,
1651 WC_BUILD_STATION, WC_BUILD_TOOLBAR,
1652 WDF_CONSTRUCTION,
1653 _nested_station_builder_widgets, lengthof(_nested_station_builder_widgets),
1654 &BuildRailStationWindow::hotkeys
1657 /** Open station build window */
1658 static Window *ShowStationBuilder(Window *parent)
1660 bool newstations = StationClass::GetClassCount() > 2 || StationClass::Get(STAT_CLASS_DFLT)->GetSpecCount() != 1;
1661 return new BuildRailStationWindow(&_station_builder_desc, parent, newstations);
1664 struct BuildSignalWindow : public PickerWindowBase {
1665 private:
1666 Dimension sig_sprite_size; ///< Maximum size of signal GUI sprites.
1667 int sig_sprite_bottom_offset; ///< Maximum extent of signal GUI sprite from reference point towards bottom.
1670 * Draw dynamic a signal-sprite in a button in the signal GUI
1671 * Draw the sprite +1px to the right and down if the button is lowered
1673 * @param widget_index index of this widget in the window
1674 * @param image the sprite to draw
1676 void DrawSignalSprite(byte widget_index, SpriteID image) const
1678 Point offset;
1679 Dimension sprite_size = GetSpriteSize(image, &offset);
1680 const NWidgetBase *widget = this->GetWidget<NWidgetBase>(widget_index);
1681 int x = widget->pos_x - offset.x +
1682 (widget->current_x - sprite_size.width + offset.x) / 2; // centered
1683 int y = widget->pos_y - sig_sprite_bottom_offset + WD_IMGBTN_TOP +
1684 (widget->current_y - WD_IMGBTN_TOP - WD_IMGBTN_BOTTOM + sig_sprite_size.height) / 2; // aligned to bottom
1686 DrawSprite(image, PAL_NONE,
1687 x + this->IsWidgetLowered(widget_index),
1688 y + this->IsWidgetLowered(widget_index));
1691 /** Show or hide buttons for non-path signals in the signal GUI */
1692 void SetSignalUIMode()
1694 bool show_non_path_signals = (_settings_client.gui.signal_gui_mode == SIGNAL_GUI_ALL);
1696 this->GetWidget<NWidgetStacked>(WID_BS_SEMAPHORE_NORM_SEL)->SetDisplayedPlane(show_non_path_signals ? 0 : SZSP_NONE);
1697 this->GetWidget<NWidgetStacked>(WID_BS_ELECTRIC_NORM_SEL)->SetDisplayedPlane(show_non_path_signals ? 0 : SZSP_NONE);
1698 this->GetWidget<NWidgetStacked>(WID_BS_SEMAPHORE_ENTRY_SEL)->SetDisplayedPlane(show_non_path_signals ? 0 : SZSP_NONE);
1699 this->GetWidget<NWidgetStacked>(WID_BS_ELECTRIC_ENTRY_SEL)->SetDisplayedPlane(show_non_path_signals ? 0 : SZSP_NONE);
1700 this->GetWidget<NWidgetStacked>(WID_BS_SEMAPHORE_EXIT_SEL)->SetDisplayedPlane(show_non_path_signals ? 0 : SZSP_NONE);
1701 this->GetWidget<NWidgetStacked>(WID_BS_ELECTRIC_EXIT_SEL)->SetDisplayedPlane(show_non_path_signals ? 0 : SZSP_NONE);
1702 this->GetWidget<NWidgetStacked>(WID_BS_SEMAPHORE_COMBO_SEL)->SetDisplayedPlane(show_non_path_signals ? 0 : SZSP_NONE);
1703 this->GetWidget<NWidgetStacked>(WID_BS_ELECTRIC_COMBO_SEL)->SetDisplayedPlane(show_non_path_signals ? 0 : SZSP_NONE);
1706 public:
1707 BuildSignalWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1709 this->CreateNestedTree();
1710 this->SetSignalUIMode();
1711 this->FinishInitNested(TRANSPORT_RAIL);
1712 this->OnInvalidateData();
1715 void Close() override
1717 _convert_signal_button = false;
1718 this->PickerWindowBase::Close();
1721 void OnInit() override
1723 /* Calculate maximum signal sprite size. */
1724 this->sig_sprite_size.width = 0;
1725 this->sig_sprite_size.height = 0;
1726 this->sig_sprite_bottom_offset = 0;
1727 const RailtypeInfo *rti = GetRailTypeInfo(_cur_railtype);
1728 for (uint type = SIGTYPE_NORMAL; type < SIGTYPE_END; type++) {
1729 for (uint variant = SIG_ELECTRIC; variant <= SIG_SEMAPHORE; variant++) {
1730 for (uint lowered = 0; lowered < 2; lowered++) {
1731 Point offset;
1732 Dimension sprite_size = GetSpriteSize(rti->gui_sprites.signals[type][variant][lowered], &offset);
1733 this->sig_sprite_bottom_offset = std::max<int>(this->sig_sprite_bottom_offset, sprite_size.height);
1734 this->sig_sprite_size.width = std::max<int>(this->sig_sprite_size.width, sprite_size.width - offset.x);
1735 this->sig_sprite_size.height = std::max<int>(this->sig_sprite_size.height, sprite_size.height - offset.y);
1741 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1743 if (widget == WID_BS_DRAG_SIGNALS_DENSITY_LABEL) {
1744 /* Two digits for signals density. */
1745 size->width = std::max(size->width, 2 * GetDigitWidth() + padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT);
1746 } else if (IsInsideMM(widget, WID_BS_SEMAPHORE_NORM, WID_BS_ELECTRIC_PBS_OWAY + 1)) {
1747 size->width = std::max(size->width, this->sig_sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT);
1748 size->height = std::max(size->height, this->sig_sprite_size.height + WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM);
1749 } else if (widget == WID_BS_CAPTION) {
1750 size->width += WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
1754 void SetStringParameters(int widget) const override
1756 switch (widget) {
1757 case WID_BS_DRAG_SIGNALS_DENSITY_LABEL:
1758 SetDParam(0, _settings_client.gui.drag_signals_density);
1759 break;
1763 void DrawWidget(const Rect &r, int widget) const override
1765 if (IsInsideMM(widget, WID_BS_SEMAPHORE_NORM, WID_BS_ELECTRIC_PBS_OWAY + 1)) {
1766 /* Extract signal from widget number. */
1767 int type = (widget - WID_BS_SEMAPHORE_NORM) % SIGTYPE_END;
1768 int var = SIG_SEMAPHORE - (widget - WID_BS_SEMAPHORE_NORM) / SIGTYPE_END; // SignalVariant order is reversed compared to the widgets.
1769 SpriteID sprite = GetRailTypeInfo(_cur_railtype)->gui_sprites.signals[type][var][this->IsWidgetLowered(widget)];
1771 this->DrawSignalSprite(widget, sprite);
1775 void OnClick(Point pt, int widget, int click_count) override
1777 switch (widget) {
1778 case WID_BS_SEMAPHORE_NORM:
1779 case WID_BS_SEMAPHORE_ENTRY:
1780 case WID_BS_SEMAPHORE_EXIT:
1781 case WID_BS_SEMAPHORE_COMBO:
1782 case WID_BS_SEMAPHORE_PBS:
1783 case WID_BS_SEMAPHORE_PBS_OWAY:
1784 case WID_BS_ELECTRIC_NORM:
1785 case WID_BS_ELECTRIC_ENTRY:
1786 case WID_BS_ELECTRIC_EXIT:
1787 case WID_BS_ELECTRIC_COMBO:
1788 case WID_BS_ELECTRIC_PBS:
1789 case WID_BS_ELECTRIC_PBS_OWAY:
1790 this->RaiseWidget((_cur_signal_variant == SIG_ELECTRIC ? WID_BS_ELECTRIC_NORM : WID_BS_SEMAPHORE_NORM) + _cur_signal_type);
1792 _cur_signal_type = (SignalType)((uint)((widget - WID_BS_SEMAPHORE_NORM) % (SIGTYPE_LAST + 1)));
1793 _cur_signal_variant = widget >= WID_BS_ELECTRIC_NORM ? SIG_ELECTRIC : SIG_SEMAPHORE;
1795 /* Update default (last-used) signal type in config file. */
1796 _settings_client.gui.default_signal_type = _cur_signal_type;
1798 /* If 'remove' button of rail build toolbar is active, disable it. */
1799 if (_remove_button_clicked) {
1800 Window *w = FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL);
1801 if (w != nullptr) ToggleRailButton_Remove(w);
1804 break;
1806 case WID_BS_CONVERT:
1807 _convert_signal_button = !_convert_signal_button;
1808 break;
1810 case WID_BS_DRAG_SIGNALS_DENSITY_DECREASE:
1811 if (_settings_client.gui.drag_signals_density > 1) {
1812 _settings_client.gui.drag_signals_density--;
1813 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_SETTINGS);
1815 break;
1817 case WID_BS_DRAG_SIGNALS_DENSITY_INCREASE:
1818 if (_settings_client.gui.drag_signals_density < 20) {
1819 _settings_client.gui.drag_signals_density++;
1820 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_SETTINGS);
1822 break;
1824 case WID_BS_TOGGLE_SIZE:
1825 _settings_client.gui.signal_gui_mode = (_settings_client.gui.signal_gui_mode == SIGNAL_GUI_ALL) ? SIGNAL_GUI_PATH : SIGNAL_GUI_ALL;
1826 this->SetSignalUIMode();
1827 this->ReInit();
1828 break;
1830 default: break;
1833 this->InvalidateData();
1837 * Some data on this window has become invalid.
1838 * @param data Information about the changed data.
1839 * @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.
1841 void OnInvalidateData(int data = 0, bool gui_scope = true) override
1843 if (!gui_scope) return;
1844 this->LowerWidget((_cur_signal_variant == SIG_ELECTRIC ? WID_BS_ELECTRIC_NORM : WID_BS_SEMAPHORE_NORM) + _cur_signal_type);
1846 this->SetWidgetLoweredState(WID_BS_CONVERT, _convert_signal_button);
1848 this->SetWidgetDisabledState(WID_BS_DRAG_SIGNALS_DENSITY_DECREASE, _settings_client.gui.drag_signals_density == 1);
1849 this->SetWidgetDisabledState(WID_BS_DRAG_SIGNALS_DENSITY_INCREASE, _settings_client.gui.drag_signals_density == 20);
1853 /** Nested widget definition of the build signal window */
1854 static const NWidgetPart _nested_signal_builder_widgets[] = {
1855 NWidget(NWID_HORIZONTAL),
1856 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1857 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_BS_CAPTION), SetDataTip(STR_BUILD_SIGNAL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1858 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_BS_TOGGLE_SIZE), SetDataTip(SPR_LARGE_SMALL_WINDOW, STR_BUILD_SIGNAL_TOGGLE_ADVANCED_SIGNAL_TOOLTIP),
1859 EndContainer(),
1860 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
1861 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1862 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BS_SEMAPHORE_NORM_SEL),
1863 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_NORM), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_NORM_TOOLTIP), EndContainer(), SetFill(1, 1),
1864 EndContainer(),
1865 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BS_SEMAPHORE_ENTRY_SEL),
1866 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_ENTRY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_ENTRY_TOOLTIP), EndContainer(), SetFill(1, 1),
1867 EndContainer(),
1868 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BS_SEMAPHORE_EXIT_SEL),
1869 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_EXIT), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_EXIT_TOOLTIP), EndContainer(), SetFill(1, 1),
1870 EndContainer(),
1871 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BS_SEMAPHORE_COMBO_SEL),
1872 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_COMBO), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_COMBO_TOOLTIP), EndContainer(), SetFill(1, 1),
1873 EndContainer(),
1874 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_PBS), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_PBS_TOOLTIP), EndContainer(), SetFill(1, 1),
1875 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),
1876 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_BS_CONVERT), SetDataTip(SPR_IMG_SIGNAL_CONVERT, STR_BUILD_SIGNAL_CONVERT_TOOLTIP), SetFill(1, 1),
1877 EndContainer(),
1878 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1879 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BS_ELECTRIC_NORM_SEL),
1880 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_NORM), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_NORM_TOOLTIP), EndContainer(), SetFill(1, 1),
1881 EndContainer(),
1882 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BS_ELECTRIC_ENTRY_SEL),
1883 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_ENTRY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_ENTRY_TOOLTIP), EndContainer(), SetFill(1, 1),
1884 EndContainer(),
1885 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BS_ELECTRIC_EXIT_SEL),
1886 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_EXIT), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_EXIT_TOOLTIP), EndContainer(), SetFill(1, 1),
1887 EndContainer(),
1888 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BS_ELECTRIC_COMBO_SEL),
1889 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_COMBO), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_COMBO_TOOLTIP), EndContainer(), SetFill(1, 1),
1890 EndContainer(),
1891 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_PBS), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_PBS_TOOLTIP), EndContainer(), SetFill(1, 1),
1892 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),
1893 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_DRAG_SIGNALS_DENSITY_TOOLTIP), SetFill(1, 1),
1894 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),
1895 NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1896 NWidget(NWID_SPACER), SetFill(1, 0),
1897 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),
1898 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),
1899 NWidget(NWID_SPACER), SetFill(1, 0),
1900 EndContainer(),
1901 NWidget(NWID_SPACER), SetMinimalSize(0, 2), SetFill(1, 0),
1902 EndContainer(),
1903 EndContainer(),
1904 EndContainer(),
1907 /** Signal selection window description */
1908 static WindowDesc _signal_builder_desc(
1909 WDP_AUTO, "build_signal", 0, 0,
1910 WC_BUILD_SIGNAL, WC_BUILD_TOOLBAR,
1911 WDF_CONSTRUCTION,
1912 _nested_signal_builder_widgets, lengthof(_nested_signal_builder_widgets)
1916 * Open the signal selection window
1918 static void ShowSignalBuilder(Window *parent)
1920 new BuildSignalWindow(&_signal_builder_desc, parent);
1923 struct BuildRailDepotWindow : public PickerWindowBase {
1924 BuildRailDepotWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1926 this->InitNested(TRANSPORT_RAIL);
1927 this->LowerWidget(_build_depot_direction + WID_BRAD_DEPOT_NE);
1930 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1932 if (!IsInsideMM(widget, WID_BRAD_DEPOT_NE, WID_BRAD_DEPOT_NW + 1)) return;
1934 size->width = ScaleGUITrad(64) + 2;
1935 size->height = ScaleGUITrad(48) + 2;
1938 void DrawWidget(const Rect &r, int widget) const override
1940 if (!IsInsideMM(widget, WID_BRAD_DEPOT_NE, WID_BRAD_DEPOT_NW + 1)) return;
1942 DrawTrainDepotSprite(r.left + 1 + ScaleGUITrad(31), r.bottom - ScaleGUITrad(31), widget - WID_BRAD_DEPOT_NE + DIAGDIR_NE, _cur_railtype);
1945 void OnClick(Point pt, int widget, int click_count) override
1947 switch (widget) {
1948 case WID_BRAD_DEPOT_NE:
1949 case WID_BRAD_DEPOT_SE:
1950 case WID_BRAD_DEPOT_SW:
1951 case WID_BRAD_DEPOT_NW:
1952 this->RaiseWidget(_build_depot_direction + WID_BRAD_DEPOT_NE);
1953 _build_depot_direction = (DiagDirection)(widget - WID_BRAD_DEPOT_NE);
1954 this->LowerWidget(_build_depot_direction + WID_BRAD_DEPOT_NE);
1955 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1956 this->SetDirty();
1957 break;
1962 /** Nested widget definition of the build rail depot window */
1963 static const NWidgetPart _nested_build_depot_widgets[] = {
1964 NWidget(NWID_HORIZONTAL),
1965 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1966 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_BUILD_DEPOT_TRAIN_ORIENTATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1967 EndContainer(),
1968 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1969 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1970 NWidget(NWID_HORIZONTAL_LTR),
1971 NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
1972 NWidget(NWID_VERTICAL),
1973 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_NW), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1974 EndContainer(),
1975 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1976 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_SW), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1977 EndContainer(),
1978 EndContainer(),
1979 NWidget(NWID_SPACER), SetMinimalSize(2, 0),
1980 NWidget(NWID_VERTICAL),
1981 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_NE), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1982 EndContainer(),
1983 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1984 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_SE), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1985 EndContainer(),
1986 EndContainer(),
1987 NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
1988 EndContainer(),
1989 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1990 EndContainer(),
1993 static WindowDesc _build_depot_desc(
1994 WDP_AUTO, nullptr, 0, 0,
1995 WC_BUILD_DEPOT, WC_BUILD_TOOLBAR,
1996 WDF_CONSTRUCTION,
1997 _nested_build_depot_widgets, lengthof(_nested_build_depot_widgets)
2000 static void ShowBuildTrainDepotPicker(Window *parent)
2002 new BuildRailDepotWindow(&_build_depot_desc, parent);
2005 struct BuildRailWaypointWindow : PickerWindowBase {
2006 BuildRailWaypointWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
2008 this->CreateNestedTree();
2010 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX);
2011 matrix->SetScrollbar(this->GetScrollbar(WID_BRW_SCROLL));
2013 this->FinishInitNested(TRANSPORT_RAIL);
2015 matrix->SetCount(_waypoint_count);
2016 if (_cur_waypoint_type >= _waypoint_count) _cur_waypoint_type = 0;
2017 matrix->SetClicked(_cur_waypoint_type);
2020 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
2022 switch (widget) {
2023 case WID_BRW_WAYPOINT_MATRIX:
2024 /* Three blobs high and wide. */
2025 size->width += resize->width * 2;
2026 size->height += resize->height * 2;
2028 /* Resizing in X direction only at blob size, but at pixel level in Y. */
2029 resize->height = 1;
2030 break;
2032 case WID_BRW_WAYPOINT:
2033 size->width = ScaleGUITrad(64) + 2;
2034 size->height = ScaleGUITrad(58) + 2;
2035 break;
2039 void DrawWidget(const Rect &r, int widget) const override
2041 switch (GB(widget, 0, 16)) {
2042 case WID_BRW_WAYPOINT: {
2043 byte type = GB(widget, 16, 16);
2044 const StationSpec *statspec = StationClass::Get(STAT_CLASS_WAYP)->GetSpec(type);
2045 DrawWaypointSprite(r.left + 1 + ScaleGUITrad(31), r.bottom - ScaleGUITrad(31), type, _cur_railtype);
2047 if (!IsStationAvailable(statspec)) {
2048 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
2054 void OnClick(Point pt, int widget, int click_count) override
2056 switch (GB(widget, 0, 16)) {
2057 case WID_BRW_WAYPOINT: {
2058 byte type = GB(widget, 16, 16);
2059 this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX)->SetClicked(_cur_waypoint_type);
2061 /* Check station availability callback */
2062 const StationSpec *statspec = StationClass::Get(STAT_CLASS_WAYP)->GetSpec(type);
2063 if (!IsStationAvailable(statspec)) return;
2065 _cur_waypoint_type = type;
2066 this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX)->SetClicked(_cur_waypoint_type);
2067 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
2068 this->SetDirty();
2069 break;
2075 /** Nested widget definition for the build NewGRF rail waypoint window */
2076 static const NWidgetPart _nested_build_waypoint_widgets[] = {
2077 NWidget(NWID_HORIZONTAL),
2078 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
2079 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_WAYPOINT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2080 NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
2081 EndContainer(),
2082 NWidget(NWID_HORIZONTAL),
2083 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
2084 NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BRW_WAYPOINT_MATRIX), SetPIP(0, 2, 0), SetPadding(3), SetScrollbar(WID_BRW_SCROLL),
2085 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BRW_WAYPOINT), SetMinimalSize(66, 60), SetDataTip(0x0, STR_WAYPOINT_GRAPHICS_TOOLTIP), SetScrollbar(WID_BRW_SCROLL), EndContainer(),
2086 EndContainer(),
2087 EndContainer(),
2088 NWidget(NWID_VERTICAL),
2089 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BRW_SCROLL),
2090 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
2091 EndContainer(),
2092 EndContainer(),
2095 static WindowDesc _build_waypoint_desc(
2096 WDP_AUTO, "build_waypoint", 0, 0,
2097 WC_BUILD_WAYPOINT, WC_BUILD_TOOLBAR,
2098 WDF_CONSTRUCTION,
2099 _nested_build_waypoint_widgets, lengthof(_nested_build_waypoint_widgets)
2102 static void ShowBuildWaypointPicker(Window *parent)
2104 new BuildRailWaypointWindow(&_build_waypoint_desc, parent);
2108 * Initialize rail building GUI settings
2110 void InitializeRailGui()
2112 _build_depot_direction = DIAGDIR_NW;
2113 _railstation.station_class = StationClassID::STAT_CLASS_DFLT;
2117 * Re-initialize rail-build toolbar after toggling support for electric trains
2118 * @param disable Boolean whether electric trains are disabled (removed from the game)
2120 void ReinitGuiAfterToggleElrail(bool disable)
2122 extern RailType _last_built_railtype;
2123 if (disable && _last_built_railtype == RAILTYPE_ELECTRIC) {
2124 _last_built_railtype = _cur_railtype = RAILTYPE_RAIL;
2125 BuildRailToolbarWindow *w = dynamic_cast<BuildRailToolbarWindow *>(FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL));
2126 if (w != nullptr) w->ModifyRailType(_cur_railtype);
2128 MarkWholeScreenDirty();
2131 /** Set the initial (default) railtype to use */
2132 static void SetDefaultRailGui()
2134 if (_local_company == COMPANY_SPECTATOR || !Company::IsValidID(_local_company)) return;
2136 extern RailType _last_built_railtype;
2137 RailType rt;
2138 switch (_settings_client.gui.default_rail_type) {
2139 case 2: {
2140 /* Find the most used rail type */
2141 uint count[RAILTYPE_END];
2142 memset(count, 0, sizeof(count));
2143 for (TileIndex t = 0; t < MapSize(); t++) {
2144 if (IsTileType(t, MP_RAILWAY) || IsLevelCrossingTile(t) || HasStationTileRail(t) ||
2145 (IsTileType(t, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL)) {
2146 count[GetRailType(t)]++;
2150 rt = static_cast<RailType>(std::max_element(count + RAILTYPE_BEGIN, count + RAILTYPE_END) - count);
2151 if (count[rt] > 0) break;
2153 /* No rail, just get the first available one */
2154 FALLTHROUGH;
2156 case 0: {
2157 /* Use first available type */
2158 std::vector<RailType>::const_iterator it = std::find_if(_sorted_railtypes.begin(), _sorted_railtypes.end(),
2159 [](RailType r){ return HasRailtypeAvail(_local_company, r); });
2160 rt = it != _sorted_railtypes.end() ? *it : RAILTYPE_BEGIN;
2161 break;
2163 case 1: {
2164 /* Use last available type */
2165 std::vector<RailType>::const_reverse_iterator it = std::find_if(_sorted_railtypes.rbegin(), _sorted_railtypes.rend(),
2166 [](RailType r){ return HasRailtypeAvail(_local_company, r); });
2167 rt = it != _sorted_railtypes.rend() ? *it : RAILTYPE_BEGIN;
2168 break;
2170 default:
2171 NOT_REACHED();
2174 _last_built_railtype = _cur_railtype = rt;
2175 BuildRailToolbarWindow *w = dynamic_cast<BuildRailToolbarWindow *>(FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL));
2176 if (w != nullptr) w->ModifyRailType(_cur_railtype);
2180 * Updates the current signal variant used in the signal GUI
2181 * to the one adequate to current year.
2182 * @param new_value needed to be called when a setting changes
2184 void ResetSignalVariant(int32 new_value)
2186 SignalVariant new_variant = (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC);
2188 if (new_variant != _cur_signal_variant) {
2189 Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
2190 if (w != nullptr) {
2191 w->SetDirty();
2192 w->RaiseWidget((_cur_signal_variant == SIG_ELECTRIC ? WID_BS_ELECTRIC_NORM : WID_BS_SEMAPHORE_NORM) + _cur_signal_type);
2194 _cur_signal_variant = new_variant;
2199 * Resets the rail GUI - sets default railtype to build
2200 * and resets the signal GUI
2202 void InitializeRailGUI()
2204 SetDefaultRailGui();
2206 _convert_signal_button = false;
2207 _cur_signal_type = _settings_client.gui.default_signal_type;
2208 ResetSignalVariant();
2212 * Create a drop down list for all the rail types of the local company.
2213 * @param for_replacement Whether this list is for the replacement window.
2214 * @param all_option Whether to add an 'all types' item.
2215 * @return The populated and sorted #DropDownList.
2217 DropDownList GetRailTypeDropDownList(bool for_replacement, bool all_option)
2219 RailTypes used_railtypes;
2220 RailTypes avail_railtypes;
2222 const Company *c = Company::Get(_local_company);
2224 /* Find the used railtypes. */
2225 if (for_replacement) {
2226 avail_railtypes = GetCompanyRailtypes(c->index, false);
2227 used_railtypes = GetRailTypes(false);
2228 } else {
2229 avail_railtypes = c->avail_railtypes;
2230 used_railtypes = GetRailTypes(true);
2233 DropDownList list;
2235 if (all_option) {
2236 list.emplace_back(new DropDownListStringItem(STR_REPLACE_ALL_RAILTYPE, INVALID_RAILTYPE, false));
2239 Dimension d = { 0, 0 };
2240 /* Get largest icon size, to ensure text is aligned on each menu item. */
2241 if (!for_replacement) {
2242 for (const auto &rt : _sorted_railtypes) {
2243 if (!HasBit(used_railtypes, rt)) continue;
2244 const RailtypeInfo *rti = GetRailTypeInfo(rt);
2245 d = maxdim(d, GetSpriteSize(rti->gui_sprites.build_x_rail));
2249 for (const auto &rt : _sorted_railtypes) {
2250 /* If it's not used ever, don't show it to the user. */
2251 if (!HasBit(used_railtypes, rt)) continue;
2253 const RailtypeInfo *rti = GetRailTypeInfo(rt);
2255 StringID str = for_replacement ? rti->strings.replace_text : (rti->max_speed > 0 ? STR_TOOLBAR_RAILTYPE_VELOCITY : STR_JUST_STRING);
2256 DropDownListParamStringItem *item;
2257 if (for_replacement) {
2258 item = new DropDownListParamStringItem(str, rt, !HasBit(avail_railtypes, rt));
2259 } else {
2260 DropDownListIconItem *iconitem = new DropDownListIconItem(rti->gui_sprites.build_x_rail, PAL_NONE, str, rt, !HasBit(avail_railtypes, rt));
2261 iconitem->SetDimension(d);
2262 item = iconitem;
2264 item->SetParam(0, rti->strings.menu_text);
2265 item->SetParam(1, rti->max_speed);
2266 list.emplace_back(item);
2269 if (list.size() == 0) {
2270 /* Empty dropdowns are not allowed */
2271 list.emplace_back(new DropDownListStringItem(STR_NONE, INVALID_RAILTYPE, true));
2274 return list;