2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file rail_gui.cpp %File for dealing with rail construction user interface */
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"
31 #include "engine_base.h"
32 #include "vehicle_func.h"
33 #include "zoom_func.h"
35 #include "querystring_gui.h"
36 #include "sortlist_type.h"
37 #include "stringfilter_type.h"
38 #include "string_func.h"
40 #include "station_map.h"
41 #include "tunnelbridge_map.h"
43 #include "widgets/rail_widget.h"
45 #include "safeguards.h"
48 static RailType _cur_railtype
; ///< Rail type of the current build-rail toolbar.
49 static bool _remove_button_clicked
; ///< Flag whether 'remove' toggle-button is currently enabled
50 static DiagDirection _build_depot_direction
; ///< Currently selected depot direction
51 static byte _waypoint_count
= 1; ///< Number of waypoint types
52 static byte _cur_waypoint_type
; ///< Currently selected waypoint type
53 static bool _convert_signal_button
; ///< convert signal button in the signal GUI pressed
54 static SignalVariant _cur_signal_variant
; ///< set the signal variant (for signal GUI)
55 static SignalType _cur_signal_type
; ///< set the signal type (for signal GUI)
57 struct RailStationGUISettings
{
58 Axis orientation
; ///< Currently selected rail station orientation
60 bool newstations
; ///< Are custom station definitions available?
61 StationClassID station_class
; ///< Currently selected custom station class (if newstations is \c true )
62 byte station_type
; ///< %Station type within the currently selected custom station class (if newstations is \c true )
63 byte station_count
; ///< Number of custom stations (if newstations is \c true )
65 static RailStationGUISettings _railstation
; ///< Settings of the station builder GUI
68 static void HandleStationPlacement(TileIndex start
, TileIndex end
);
69 static void ShowBuildTrainDepotPicker(Window
*parent
);
70 static void ShowBuildWaypointPicker(Window
*parent
);
71 static Window
*ShowStationBuilder(Window
*parent
);
72 static void ShowSignalBuilder(Window
*parent
);
75 * Check whether a station type can be build.
76 * @return true if building is allowed.
78 static bool IsStationAvailable(const StationSpec
*statspec
)
80 if (statspec
== nullptr || !HasBit(statspec
->callback_mask
, CBM_STATION_AVAIL
)) return true;
82 uint16 cb_res
= GetStationCallback(CBID_STATION_AVAILABILITY
, 0, 0, statspec
, nullptr, INVALID_TILE
);
83 if (cb_res
== CALLBACK_FAILED
) return true;
85 return Convert8bitBooleanCallback(statspec
->grf_prop
.grffile
, CBID_STATION_AVAILABILITY
, cb_res
);
88 void CcPlaySound_CONSTRUCTION_RAIL(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
, uint32 cmd
)
90 if (result
.Succeeded() && _settings_client
.sound
.confirm
) SndPlayTileFx(SND_20_CONSTRUCTION_RAIL
, tile
);
93 static void GenericPlaceRail(TileIndex tile
, int cmd
)
95 DoCommandP(tile
, _cur_railtype
, cmd
| (_settings_client
.gui
.auto_remove_signals
<< 3),
96 _remove_button_clicked
?
97 CMD_REMOVE_SINGLE_RAIL
| CMD_MSG(STR_ERROR_CAN_T_REMOVE_RAILROAD_TRACK
) :
98 CMD_BUILD_SINGLE_RAIL
| CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_TRACK
),
99 CcPlaySound_CONSTRUCTION_RAIL
);
103 * Try to add an additional rail-track at the entrance of a depot
104 * @param tile Tile to use for adding the rail-track
105 * @param dir Direction to check for already present tracks
106 * @param track Track to add
109 static void PlaceExtraDepotRail(TileIndex tile
, DiagDirection dir
, Track track
)
111 if (GetRailTileType(tile
) == RAIL_TILE_DEPOT
) return;
112 if (GetRailTileType(tile
) == RAIL_TILE_SIGNALS
&& !_settings_client
.gui
.auto_remove_signals
) return;
113 if ((GetTrackBits(tile
) & DiagdirReachesTracks(dir
)) == 0) return;
115 DoCommandP(tile
, _cur_railtype
, track
| (_settings_client
.gui
.auto_remove_signals
<< 3), CMD_BUILD_SINGLE_RAIL
);
118 /** Additional pieces of track to add at the entrance of a depot. */
119 static const Track _place_depot_extra_track
[12] = {
120 TRACK_LEFT
, TRACK_UPPER
, TRACK_UPPER
, TRACK_RIGHT
, // First additional track for directions 0..3
121 TRACK_X
, TRACK_Y
, TRACK_X
, TRACK_Y
, // Second additional track
122 TRACK_LOWER
, TRACK_LEFT
, TRACK_RIGHT
, TRACK_LOWER
, // Third additional track
125 /** Direction to check for existing track pieces. */
126 static const DiagDirection _place_depot_extra_dir
[12] = {
127 DIAGDIR_SE
, DIAGDIR_SW
, DIAGDIR_SE
, DIAGDIR_SW
,
128 DIAGDIR_SW
, DIAGDIR_NW
, DIAGDIR_NE
, DIAGDIR_SE
,
129 DIAGDIR_NW
, DIAGDIR_NE
, DIAGDIR_NW
, DIAGDIR_NE
,
132 void CcRailDepot(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
, uint32 cmd
)
134 if (result
.Failed()) return;
136 DiagDirection dir
= (DiagDirection
)p2
;
138 if (_settings_client
.sound
.confirm
) SndPlayTileFx(SND_20_CONSTRUCTION_RAIL
, tile
);
139 if (!_settings_client
.gui
.persistent_buildingtools
) ResetObjectToPlace();
141 tile
+= TileOffsByDiagDir(dir
);
143 if (IsTileType(tile
, MP_RAILWAY
)) {
144 PlaceExtraDepotRail(tile
, _place_depot_extra_dir
[dir
], _place_depot_extra_track
[dir
]);
145 PlaceExtraDepotRail(tile
, _place_depot_extra_dir
[dir
+ 4], _place_depot_extra_track
[dir
+ 4]);
146 PlaceExtraDepotRail(tile
, _place_depot_extra_dir
[dir
+ 8], _place_depot_extra_track
[dir
+ 8]);
151 * Place a rail waypoint.
152 * @param tile Position to start dragging a waypoint.
154 static void PlaceRail_Waypoint(TileIndex tile
)
156 if (_remove_button_clicked
) {
157 VpStartPlaceSizing(tile
, VPM_X_AND_Y
, DDSP_REMOVE_STATION
);
161 Axis axis
= GetAxisForNewWaypoint(tile
);
162 if (IsValidAxis(axis
)) {
163 /* Valid tile for waypoints */
164 VpStartPlaceSizing(tile
, axis
== AXIS_X
? VPM_X_LIMITED
: VPM_Y_LIMITED
, DDSP_BUILD_STATION
);
165 VpSetPlaceSizingLimit(_settings_game
.station
.station_spread
);
167 /* Tile where we can't build rail waypoints. This is always going to fail,
168 * but provides the user with a proper error message. */
169 DoCommandP(tile
, 1 << 8 | 1 << 16, STAT_CLASS_WAYP
| INVALID_STATION
<< 16, CMD_BUILD_RAIL_WAYPOINT
| CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_WAYPOINT
));
173 void CcStation(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
, uint32 cmd
)
175 if (result
.Failed()) return;
177 if (_settings_client
.sound
.confirm
) SndPlayTileFx(SND_20_CONSTRUCTION_RAIL
, tile
);
178 /* Only close the station builder window if the default station and non persistent building is chosen. */
179 if (_railstation
.station_class
== STAT_CLASS_DFLT
&& _railstation
.station_type
== 0 && !_settings_client
.gui
.persistent_buildingtools
) ResetObjectToPlace();
183 * Place a rail station.
184 * @param tile Position to place or start dragging a station.
186 static void PlaceRail_Station(TileIndex tile
)
188 if (_remove_button_clicked
) {
189 VpStartPlaceSizing(tile
, VPM_X_AND_Y_LIMITED
, DDSP_REMOVE_STATION
);
190 VpSetPlaceSizingLimit(-1);
191 } else if (_settings_client
.gui
.station_dragdrop
) {
192 VpStartPlaceSizing(tile
, VPM_X_AND_Y_LIMITED
, DDSP_BUILD_STATION
);
193 VpSetPlaceSizingLimit(_settings_game
.station
.station_spread
);
195 uint32 p1
= _cur_railtype
| _railstation
.orientation
<< 6 | _settings_client
.gui
.station_numtracks
<< 8 | _settings_client
.gui
.station_platlength
<< 16 | _ctrl_pressed
<< 24;
196 uint32 p2
= _railstation
.station_class
| _railstation
.station_type
<< 8 | INVALID_STATION
<< 16;
198 int w
= _settings_client
.gui
.station_numtracks
;
199 int h
= _settings_client
.gui
.station_platlength
;
200 if (!_railstation
.orientation
) Swap(w
, h
);
202 CommandContainer cmdcont
= { tile
, p1
, p2
, CMD_BUILD_RAIL_STATION
| CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION
), CcStation
, "" };
203 ShowSelectStationIfNeeded(cmdcont
, TileArea(tile
, w
, h
));
208 * Build a new signal or edit/remove a present signal, use CmdBuildSingleSignal() or CmdRemoveSingleSignal() in rail_cmd.cpp
210 * @param tile The tile where the signal will build or edit
212 static void GenericPlaceSignals(TileIndex tile
)
214 TrackBits trackbits
= TrackStatusToTrackBits(GetTileTrackStatus(tile
, TRANSPORT_RAIL
, 0));
216 if (trackbits
& TRACK_BIT_VERT
) { // N-S direction
217 trackbits
= (_tile_fract_coords
.x
<= _tile_fract_coords
.y
) ? TRACK_BIT_RIGHT
: TRACK_BIT_LEFT
;
220 if (trackbits
& TRACK_BIT_HORZ
) { // E-W direction
221 trackbits
= (_tile_fract_coords
.x
+ _tile_fract_coords
.y
<= 15) ? TRACK_BIT_UPPER
: TRACK_BIT_LOWER
;
224 Track track
= FindFirstTrack(trackbits
);
226 if (_remove_button_clicked
) {
227 DoCommandP(tile
, track
, 0, CMD_REMOVE_SIGNALS
| CMD_MSG(STR_ERROR_CAN_T_REMOVE_SIGNALS_FROM
), CcPlaySound_CONSTRUCTION_RAIL
);
229 const Window
*w
= FindWindowById(WC_BUILD_SIGNAL
, 0);
231 /* various bitstuffed elements for CmdBuildSingleSignal() */
234 /* Which signals should we cycle through? */
237 if (_settings_client
.gui
.cycle_signal_types
== SIGNAL_CYCLE_ALL
&& _settings_client
.gui
.signal_gui_mode
== SIGNAL_GUI_ALL
) {
238 cycle_types
= SIGTYPE_NORMAL
| (SIGTYPE_LAST
<< 3);
240 cycle_types
= SIGTYPE_PBS
| (SIGTYPE_LAST
<< 3);
244 /* signal GUI is used */
245 SB(p1
, 3, 1, _ctrl_pressed
);
246 SB(p1
, 4, 1, _cur_signal_variant
);
247 SB(p1
, 5, 3, _cur_signal_type
);
248 SB(p1
, 8, 1, _convert_signal_button
);
249 SB(p1
, 9, 6, cycle_types
);
251 SB(p1
, 3, 1, _ctrl_pressed
);
252 SB(p1
, 4, 1, (_cur_year
< _settings_client
.gui
.semaphore_build_before
? SIG_SEMAPHORE
: SIG_ELECTRIC
));
253 SB(p1
, 5, 3, SIGTYPE_PBS_ONEWAY
);
255 SB(p1
, 9, 6, cycle_types
);
258 DoCommandP(tile
, p1
, 0, CMD_BUILD_SIGNALS
|
259 CMD_MSG((w
!= nullptr && _convert_signal_button
) ? STR_ERROR_SIGNAL_CAN_T_CONVERT_SIGNALS_HERE
: STR_ERROR_CAN_T_BUILD_SIGNALS_HERE
),
260 CcPlaySound_CONSTRUCTION_RAIL
);
265 * Start placing a rail bridge.
266 * @param tile Position of the first tile of the bridge.
267 * @param w Rail toolbar window.
269 static void PlaceRail_Bridge(TileIndex tile
, Window
*w
)
271 if (IsBridgeTile(tile
)) {
272 TileIndex other_tile
= GetOtherTunnelBridgeEnd(tile
);
274 w
->OnPlaceMouseUp(VPM_X_OR_Y
, DDSP_BUILD_BRIDGE
, pt
, other_tile
, tile
);
276 VpStartPlaceSizing(tile
, VPM_X_OR_Y
, DDSP_BUILD_BRIDGE
);
280 /** Command callback for building a tunnel */
281 void CcBuildRailTunnel(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
, uint32 cmd
)
283 if (result
.Succeeded()) {
284 if (_settings_client
.sound
.confirm
) SndPlayTileFx(SND_20_CONSTRUCTION_RAIL
, tile
);
285 if (!_settings_client
.gui
.persistent_buildingtools
) ResetObjectToPlace();
287 SetRedErrorSquare(_build_tunnel_endtile
);
292 * Toggles state of the Remove button of Build rail toolbar
293 * @param w window the button belongs to
295 static void ToggleRailButton_Remove(Window
*w
)
297 CloseWindowById(WC_SELECT_STATION
, 0);
298 w
->ToggleWidgetLoweredState(WID_RAT_REMOVE
);
299 w
->SetWidgetDirty(WID_RAT_REMOVE
);
300 _remove_button_clicked
= w
->IsWidgetLowered(WID_RAT_REMOVE
);
301 SetSelectionRed(_remove_button_clicked
);
305 * Updates the Remove button because of Ctrl state change
306 * @param w window the button belongs to
307 * @return true iff the remove button was changed
309 static bool RailToolbar_CtrlChanged(Window
*w
)
311 if (w
->IsWidgetDisabled(WID_RAT_REMOVE
)) return false;
313 /* allow ctrl to switch remove mode only for these widgets */
314 for (uint i
= WID_RAT_BUILD_NS
; i
<= WID_RAT_BUILD_STATION
; i
++) {
315 if ((i
<= WID_RAT_AUTORAIL
|| i
>= WID_RAT_BUILD_WAYPOINT
) && w
->IsWidgetLowered(i
)) {
316 ToggleRailButton_Remove(w
);
326 * The "remove"-button click proc of the build-rail toolbar.
327 * @param w Build-rail toolbar window
328 * @see BuildRailToolbarWindow::OnClick()
330 static void BuildRailClick_Remove(Window
*w
)
332 if (w
->IsWidgetDisabled(WID_RAT_REMOVE
)) return;
333 ToggleRailButton_Remove(w
);
334 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
336 /* handle station builder */
337 if (w
->IsWidgetLowered(WID_RAT_BUILD_STATION
)) {
338 if (_remove_button_clicked
) {
339 /* starting drag & drop remove */
340 if (!_settings_client
.gui
.station_dragdrop
) {
341 SetTileSelectSize(1, 1);
343 VpSetPlaceSizingLimit(-1);
346 /* starting station build mode */
347 if (!_settings_client
.gui
.station_dragdrop
) {
348 int x
= _settings_client
.gui
.station_numtracks
;
349 int y
= _settings_client
.gui
.station_platlength
;
350 if (_railstation
.orientation
== 0) Swap(x
, y
);
351 SetTileSelectSize(x
, y
);
353 VpSetPlaceSizingLimit(_settings_game
.station
.station_spread
);
359 static void DoRailroadTrack(int mode
)
361 uint32 p2
= _cur_railtype
| (mode
<< 6) | (_settings_client
.gui
.auto_remove_signals
<< 11);
362 DoCommandP(TileVirtXY(_thd
.selstart
.x
, _thd
.selstart
.y
), TileVirtXY(_thd
.selend
.x
, _thd
.selend
.y
), p2
,
363 _remove_button_clicked
?
364 CMD_REMOVE_RAILROAD_TRACK
| CMD_MSG(STR_ERROR_CAN_T_REMOVE_RAILROAD_TRACK
) :
365 CMD_BUILD_RAILROAD_TRACK
| CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_TRACK
),
366 CcPlaySound_CONSTRUCTION_RAIL
);
369 static void HandleAutodirPlacement()
371 int trackstat
= _thd
.drawstyle
& HT_DIR_MASK
; // 0..5
373 if (_thd
.drawstyle
& HT_RAIL
) { // one tile case
374 GenericPlaceRail(TileVirtXY(_thd
.selend
.x
, _thd
.selend
.y
), trackstat
);
378 DoRailroadTrack(trackstat
);
382 * Build new signals or remove signals or (if only one tile marked) edit a signal.
384 * If one tile marked abort and use GenericPlaceSignals()
385 * else use CmdBuildSingleSignal() or CmdRemoveSingleSignal() in rail_cmd.cpp to build many signals
387 static void HandleAutoSignalPlacement()
389 uint32 p2
= GB(_thd
.drawstyle
, 0, 3); // 0..5
391 if ((_thd
.drawstyle
& HT_DRAG_MASK
) == HT_RECT
) { // one tile case
392 GenericPlaceSignals(TileVirtXY(_thd
.selend
.x
, _thd
.selend
.y
));
396 const Window
*w
= FindWindowById(WC_BUILD_SIGNAL
, 0);
399 /* signal GUI is used */
401 SB(p2
, 4, 1, _cur_signal_variant
);
402 SB(p2
, 6, 1, _ctrl_pressed
);
403 SB(p2
, 7, 3, _cur_signal_type
);
404 SB(p2
, 24, 8, _settings_client
.gui
.drag_signals_density
);
405 SB(p2
, 10, 1, !_settings_client
.gui
.drag_signals_fixed_distance
);
408 SB(p2
, 4, 1, (_cur_year
< _settings_client
.gui
.semaphore_build_before
? SIG_SEMAPHORE
: SIG_ELECTRIC
));
409 SB(p2
, 6, 1, _ctrl_pressed
);
410 SB(p2
, 7, 3, SIGTYPE_PBS_ONEWAY
);
411 SB(p2
, 24, 8, _settings_client
.gui
.drag_signals_density
);
412 SB(p2
, 10, 1, !_settings_client
.gui
.drag_signals_fixed_distance
);
415 /* _settings_client.gui.drag_signals_density is given as a parameter such that each user
416 * in a network game can specify their own signal density */
417 DoCommandP(TileVirtXY(_thd
.selstart
.x
, _thd
.selstart
.y
), TileVirtXY(_thd
.selend
.x
, _thd
.selend
.y
), p2
,
418 _remove_button_clicked
?
419 CMD_REMOVE_SIGNAL_TRACK
| CMD_MSG(STR_ERROR_CAN_T_REMOVE_SIGNALS_FROM
) :
420 CMD_BUILD_SIGNAL_TRACK
| CMD_MSG(STR_ERROR_CAN_T_BUILD_SIGNALS_HERE
),
421 CcPlaySound_CONSTRUCTION_RAIL
);
425 /** Rail toolbar management class. */
426 struct BuildRailToolbarWindow
: Window
{
427 RailType railtype
; ///< Rail type to build.
428 int last_user_action
; ///< Last started user action.
430 BuildRailToolbarWindow(WindowDesc
*desc
, RailType railtype
) : Window(desc
)
432 this->InitNested(TRANSPORT_RAIL
);
433 this->SetupRailToolbar(railtype
);
434 this->DisableWidget(WID_RAT_REMOVE
);
435 this->last_user_action
= WIDGET_LIST_END
;
437 if (_settings_client
.gui
.link_terraform_toolbar
) ShowTerraformToolbar(this);
440 void Close() override
442 if (this->IsWidgetLowered(WID_RAT_BUILD_STATION
)) SetViewportCatchmentStation(nullptr, true);
443 if (_settings_client
.gui
.link_terraform_toolbar
) CloseWindowById(WC_SCEN_LAND_GEN
, 0, false);
444 this->Window::Close();
448 * Configures the rail toolbar for railtype given
449 * @param railtype the railtype to display
451 void SetupRailToolbar(RailType railtype
)
453 this->railtype
= railtype
;
454 const RailtypeInfo
*rti
= GetRailTypeInfo(railtype
);
456 assert(railtype
< RAILTYPE_END
);
457 this->GetWidget
<NWidgetCore
>(WID_RAT_BUILD_NS
)->widget_data
= rti
->gui_sprites
.build_ns_rail
;
458 this->GetWidget
<NWidgetCore
>(WID_RAT_BUILD_X
)->widget_data
= rti
->gui_sprites
.build_x_rail
;
459 this->GetWidget
<NWidgetCore
>(WID_RAT_BUILD_EW
)->widget_data
= rti
->gui_sprites
.build_ew_rail
;
460 this->GetWidget
<NWidgetCore
>(WID_RAT_BUILD_Y
)->widget_data
= rti
->gui_sprites
.build_y_rail
;
461 this->GetWidget
<NWidgetCore
>(WID_RAT_AUTORAIL
)->widget_data
= rti
->gui_sprites
.auto_rail
;
462 this->GetWidget
<NWidgetCore
>(WID_RAT_BUILD_DEPOT
)->widget_data
= rti
->gui_sprites
.build_depot
;
463 this->GetWidget
<NWidgetCore
>(WID_RAT_CONVERT_RAIL
)->widget_data
= rti
->gui_sprites
.convert_rail
;
464 this->GetWidget
<NWidgetCore
>(WID_RAT_BUILD_TUNNEL
)->widget_data
= rti
->gui_sprites
.build_tunnel
;
468 * Switch to another rail type.
469 * @param railtype New rail type.
471 void ModifyRailType(RailType railtype
)
473 this->SetupRailToolbar(railtype
);
477 void UpdateRemoveWidgetStatus(int clicked_widget
)
479 switch (clicked_widget
) {
481 /* If it is the removal button that has been clicked, do nothing,
482 * as it is up to the other buttons to drive removal status */
485 case WID_RAT_BUILD_NS
:
486 case WID_RAT_BUILD_X
:
487 case WID_RAT_BUILD_EW
:
488 case WID_RAT_BUILD_Y
:
489 case WID_RAT_AUTORAIL
:
490 case WID_RAT_BUILD_WAYPOINT
:
491 case WID_RAT_BUILD_STATION
:
492 case WID_RAT_BUILD_SIGNALS
:
493 /* Removal button is enabled only if the rail/signal/waypoint/station
494 * button is still lowered. Once raised, it has to be disabled */
495 this->SetWidgetDisabledState(WID_RAT_REMOVE
, !this->IsWidgetLowered(clicked_widget
));
499 /* When any other buttons than rail/signal/waypoint/station, raise and
500 * disable the removal button */
501 this->DisableWidget(WID_RAT_REMOVE
);
502 this->RaiseWidget(WID_RAT_REMOVE
);
507 void SetStringParameters(int widget
) const override
509 if (widget
== WID_RAT_CAPTION
) {
510 const RailtypeInfo
*rti
= GetRailTypeInfo(this->railtype
);
511 if (rti
->max_speed
> 0) {
512 SetDParam(0, STR_TOOLBAR_RAILTYPE_VELOCITY
);
513 SetDParam(1, rti
->strings
.toolbar_caption
);
514 SetDParam(2, rti
->max_speed
);
516 SetDParam(0, rti
->strings
.toolbar_caption
);
521 void OnClick(Point pt
, int widget
, int click_count
) override
523 if (widget
< WID_RAT_BUILD_NS
) return;
525 _remove_button_clicked
= false;
527 case WID_RAT_BUILD_NS
:
528 HandlePlacePushButton(this, WID_RAT_BUILD_NS
, GetRailTypeInfo(_cur_railtype
)->cursor
.rail_ns
, HT_LINE
| HT_DIR_VL
);
529 this->last_user_action
= widget
;
532 case WID_RAT_BUILD_X
:
533 HandlePlacePushButton(this, WID_RAT_BUILD_X
, GetRailTypeInfo(_cur_railtype
)->cursor
.rail_swne
, HT_LINE
| HT_DIR_X
);
534 this->last_user_action
= widget
;
537 case WID_RAT_BUILD_EW
:
538 HandlePlacePushButton(this, WID_RAT_BUILD_EW
, GetRailTypeInfo(_cur_railtype
)->cursor
.rail_ew
, HT_LINE
| HT_DIR_HL
);
539 this->last_user_action
= widget
;
542 case WID_RAT_BUILD_Y
:
543 HandlePlacePushButton(this, WID_RAT_BUILD_Y
, GetRailTypeInfo(_cur_railtype
)->cursor
.rail_nwse
, HT_LINE
| HT_DIR_Y
);
544 this->last_user_action
= widget
;
547 case WID_RAT_AUTORAIL
:
548 HandlePlacePushButton(this, WID_RAT_AUTORAIL
, GetRailTypeInfo(_cur_railtype
)->cursor
.autorail
, HT_RAIL
);
549 this->last_user_action
= widget
;
552 case WID_RAT_DEMOLISH
:
553 HandlePlacePushButton(this, WID_RAT_DEMOLISH
, ANIMCURSOR_DEMOLISH
, HT_RECT
| HT_DIAGONAL
);
554 this->last_user_action
= widget
;
557 case WID_RAT_BUILD_DEPOT
:
558 if (HandlePlacePushButton(this, WID_RAT_BUILD_DEPOT
, GetRailTypeInfo(_cur_railtype
)->cursor
.depot
, HT_RECT
)) {
559 ShowBuildTrainDepotPicker(this);
560 this->last_user_action
= widget
;
564 case WID_RAT_BUILD_WAYPOINT
:
565 this->last_user_action
= widget
;
566 _waypoint_count
= StationClass::Get(STAT_CLASS_WAYP
)->GetSpecCount();
567 if (HandlePlacePushButton(this, WID_RAT_BUILD_WAYPOINT
, SPR_CURSOR_WAYPOINT
, HT_RECT
) && _waypoint_count
> 1) {
568 ShowBuildWaypointPicker(this);
572 case WID_RAT_BUILD_STATION
:
573 if (HandlePlacePushButton(this, WID_RAT_BUILD_STATION
, SPR_CURSOR_RAIL_STATION
, HT_RECT
)) {
574 ShowStationBuilder(this);
575 this->last_user_action
= widget
;
579 case WID_RAT_BUILD_SIGNALS
: {
580 this->last_user_action
= widget
;
581 bool started
= HandlePlacePushButton(this, WID_RAT_BUILD_SIGNALS
, ANIMCURSOR_BUILDSIGNALS
, HT_RECT
);
582 if (started
!= _ctrl_pressed
) {
583 ShowSignalBuilder(this);
588 case WID_RAT_BUILD_BRIDGE
:
589 HandlePlacePushButton(this, WID_RAT_BUILD_BRIDGE
, SPR_CURSOR_BRIDGE
, HT_RECT
);
590 this->last_user_action
= widget
;
593 case WID_RAT_BUILD_TUNNEL
:
594 HandlePlacePushButton(this, WID_RAT_BUILD_TUNNEL
, GetRailTypeInfo(_cur_railtype
)->cursor
.tunnel
, HT_SPECIAL
);
595 this->last_user_action
= widget
;
599 BuildRailClick_Remove(this);
602 case WID_RAT_CONVERT_RAIL
:
603 HandlePlacePushButton(this, WID_RAT_CONVERT_RAIL
, GetRailTypeInfo(_cur_railtype
)->cursor
.convert
, HT_RECT
| HT_DIAGONAL
);
604 this->last_user_action
= widget
;
607 default: NOT_REACHED();
609 this->UpdateRemoveWidgetStatus(widget
);
610 if (_ctrl_pressed
) RailToolbar_CtrlChanged(this);
613 EventState
OnHotkey(int hotkey
) override
615 MarkTileDirtyByTile(TileVirtXY(_thd
.pos
.x
, _thd
.pos
.y
)); // redraw tile selection
616 return Window::OnHotkey(hotkey
);
619 void OnPlaceObject(Point pt
, TileIndex tile
) override
621 switch (this->last_user_action
) {
622 case WID_RAT_BUILD_NS
:
623 VpStartPlaceSizing(tile
, VPM_FIX_VERTICAL
| VPM_RAILDIRS
, DDSP_PLACE_RAIL
);
626 case WID_RAT_BUILD_X
:
627 VpStartPlaceSizing(tile
, VPM_FIX_Y
| VPM_RAILDIRS
, DDSP_PLACE_RAIL
);
630 case WID_RAT_BUILD_EW
:
631 VpStartPlaceSizing(tile
, VPM_FIX_HORIZONTAL
| VPM_RAILDIRS
, DDSP_PLACE_RAIL
);
634 case WID_RAT_BUILD_Y
:
635 VpStartPlaceSizing(tile
, VPM_FIX_X
| VPM_RAILDIRS
, DDSP_PLACE_RAIL
);
638 case WID_RAT_AUTORAIL
:
639 VpStartPlaceSizing(tile
, VPM_RAILDIRS
, DDSP_PLACE_RAIL
);
642 case WID_RAT_DEMOLISH
:
643 PlaceProc_DemolishArea(tile
);
646 case WID_RAT_BUILD_DEPOT
:
647 DoCommandP(tile
, _cur_railtype
, _build_depot_direction
,
648 CMD_BUILD_TRAIN_DEPOT
| CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_DEPOT
),
652 case WID_RAT_BUILD_WAYPOINT
:
653 PlaceRail_Waypoint(tile
);
656 case WID_RAT_BUILD_STATION
:
657 PlaceRail_Station(tile
);
660 case WID_RAT_BUILD_SIGNALS
:
661 VpStartPlaceSizing(tile
, VPM_SIGNALDIRS
, DDSP_BUILD_SIGNALS
);
664 case WID_RAT_BUILD_BRIDGE
:
665 PlaceRail_Bridge(tile
, this);
668 case WID_RAT_BUILD_TUNNEL
:
669 DoCommandP(tile
, _cur_railtype
| (TRANSPORT_RAIL
<< 8), 0, CMD_BUILD_TUNNEL
| CMD_MSG(STR_ERROR_CAN_T_BUILD_TUNNEL_HERE
), CcBuildRailTunnel
);
672 case WID_RAT_CONVERT_RAIL
:
673 VpStartPlaceSizing(tile
, VPM_X_AND_Y
, DDSP_CONVERT_RAIL
);
676 default: NOT_REACHED();
680 void OnPlaceDrag(ViewportPlaceMethod select_method
, ViewportDragDropSelectionProcess select_proc
, Point pt
) override
682 /* no dragging if you have pressed the convert button */
683 if (FindWindowById(WC_BUILD_SIGNAL
, 0) != nullptr && _convert_signal_button
&& this->IsWidgetLowered(WID_RAT_BUILD_SIGNALS
)) return;
685 VpSelectTilesWithMethod(pt
.x
, pt
.y
, select_method
);
688 void OnPlaceMouseUp(ViewportPlaceMethod select_method
, ViewportDragDropSelectionProcess select_proc
, Point pt
, TileIndex start_tile
, TileIndex end_tile
) override
691 switch (select_proc
) {
692 default: NOT_REACHED();
693 case DDSP_BUILD_BRIDGE
:
694 if (!_settings_client
.gui
.persistent_buildingtools
) ResetObjectToPlace();
695 ShowBuildBridgeWindow(start_tile
, end_tile
, TRANSPORT_RAIL
, _cur_railtype
);
698 case DDSP_PLACE_RAIL
:
699 HandleAutodirPlacement();
702 case DDSP_BUILD_SIGNALS
:
703 HandleAutoSignalPlacement();
706 case DDSP_DEMOLISH_AREA
:
707 GUIPlaceProcDragXY(select_proc
, start_tile
, end_tile
);
710 case DDSP_CONVERT_RAIL
:
711 DoCommandP(end_tile
, start_tile
, _cur_railtype
| (_ctrl_pressed
? 1 << 6 : 0), CMD_CONVERT_RAIL
| CMD_MSG(STR_ERROR_CAN_T_CONVERT_RAIL
), CcPlaySound_CONSTRUCTION_RAIL
);
714 case DDSP_REMOVE_STATION
:
715 case DDSP_BUILD_STATION
:
716 if (this->IsWidgetLowered(WID_RAT_BUILD_STATION
)) {
718 if (_remove_button_clicked
) {
719 DoCommandP(end_tile
, start_tile
, _ctrl_pressed
? 0 : 1, CMD_REMOVE_FROM_RAIL_STATION
| CMD_MSG(STR_ERROR_CAN_T_REMOVE_PART_OF_STATION
), CcPlaySound_CONSTRUCTION_RAIL
);
721 HandleStationPlacement(start_tile
, end_tile
);
725 if (_remove_button_clicked
) {
726 DoCommandP(end_tile
, start_tile
, _ctrl_pressed
? 0 : 1, CMD_REMOVE_FROM_RAIL_WAYPOINT
| CMD_MSG(STR_ERROR_CAN_T_REMOVE_TRAIN_WAYPOINT
), CcPlaySound_CONSTRUCTION_RAIL
);
728 TileArea
ta(start_tile
, end_tile
);
729 uint32 p1
= _cur_railtype
| (select_method
== VPM_X_LIMITED
? AXIS_X
: AXIS_Y
) << 6 | ta
.w
<< 8 | ta
.h
<< 16 | _ctrl_pressed
<< 24;
730 uint32 p2
= STAT_CLASS_WAYP
| _cur_waypoint_type
<< 8 | INVALID_STATION
<< 16;
732 CommandContainer cmdcont
= { ta
.tile
, p1
, p2
, CMD_BUILD_RAIL_WAYPOINT
| CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_WAYPOINT
), CcPlaySound_CONSTRUCTION_RAIL
, "" };
733 ShowSelectWaypointIfNeeded(cmdcont
, ta
);
741 void OnPlaceObjectAbort() override
743 if (this->IsWidgetLowered(WID_RAT_BUILD_STATION
)) SetViewportCatchmentStation(nullptr, true);
745 this->RaiseButtons();
746 this->DisableWidget(WID_RAT_REMOVE
);
747 this->SetWidgetDirty(WID_RAT_REMOVE
);
749 CloseWindowById(WC_BUILD_SIGNAL
, TRANSPORT_RAIL
);
750 CloseWindowById(WC_BUILD_STATION
, TRANSPORT_RAIL
);
751 CloseWindowById(WC_BUILD_DEPOT
, TRANSPORT_RAIL
);
752 CloseWindowById(WC_BUILD_WAYPOINT
, TRANSPORT_RAIL
);
753 CloseWindowById(WC_SELECT_STATION
, 0);
754 CloseWindowByClass(WC_BUILD_BRIDGE
);
757 void OnPlacePresize(Point pt
, TileIndex tile
) override
759 DoCommand(tile
, _cur_railtype
| (TRANSPORT_RAIL
<< 8), 0, DC_AUTO
, CMD_BUILD_TUNNEL
);
760 VpSetPresizeRange(tile
, _build_tunnel_endtile
== 0 ? tile
: _build_tunnel_endtile
);
763 EventState
OnCTRLStateChange() override
765 /* do not toggle Remove button by Ctrl when placing station */
766 if (!this->IsWidgetLowered(WID_RAT_BUILD_STATION
) && !this->IsWidgetLowered(WID_RAT_BUILD_WAYPOINT
) && RailToolbar_CtrlChanged(this)) return ES_HANDLED
;
767 return ES_NOT_HANDLED
;
770 static HotkeyList hotkeys
;
774 * Handler for global hotkeys of the BuildRailToolbarWindow.
775 * @param hotkey Hotkey
776 * @return ES_HANDLED if hotkey was accepted.
778 static EventState
RailToolbarGlobalHotkeys(int hotkey
)
780 if (_game_mode
!= GM_NORMAL
) return ES_NOT_HANDLED
;
781 extern RailType _last_built_railtype
;
782 Window
*w
= ShowBuildRailToolbar(_last_built_railtype
);
783 if (w
== nullptr) return ES_NOT_HANDLED
;
784 return w
->OnHotkey(hotkey
);
787 const uint16 _railtoolbar_autorail_keys
[] = {'5', 'A' | WKC_GLOBAL_HOTKEY
, 0};
789 static Hotkey railtoolbar_hotkeys
[] = {
790 Hotkey('1', "build_ns", WID_RAT_BUILD_NS
),
791 Hotkey('2', "build_x", WID_RAT_BUILD_X
),
792 Hotkey('3', "build_ew", WID_RAT_BUILD_EW
),
793 Hotkey('4', "build_y", WID_RAT_BUILD_Y
),
794 Hotkey(_railtoolbar_autorail_keys
, "autorail", WID_RAT_AUTORAIL
),
795 Hotkey('6', "demolish", WID_RAT_DEMOLISH
),
796 Hotkey('7', "depot", WID_RAT_BUILD_DEPOT
),
797 Hotkey('8', "waypoint", WID_RAT_BUILD_WAYPOINT
),
798 Hotkey('9', "station", WID_RAT_BUILD_STATION
),
799 Hotkey('S', "signal", WID_RAT_BUILD_SIGNALS
),
800 Hotkey('B', "bridge", WID_RAT_BUILD_BRIDGE
),
801 Hotkey('T', "tunnel", WID_RAT_BUILD_TUNNEL
),
802 Hotkey('R', "remove", WID_RAT_REMOVE
),
803 Hotkey('C', "convert", WID_RAT_CONVERT_RAIL
),
806 HotkeyList
BuildRailToolbarWindow::hotkeys("railtoolbar", railtoolbar_hotkeys
, RailToolbarGlobalHotkeys
);
808 static const NWidgetPart _nested_build_rail_widgets
[] = {
809 NWidget(NWID_HORIZONTAL
),
810 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
811 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
, WID_RAT_CAPTION
), SetDataTip(STR_WHITE_STRING
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
812 NWidget(WWT_STICKYBOX
, COLOUR_DARK_GREEN
),
814 NWidget(NWID_HORIZONTAL
),
815 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_RAT_BUILD_NS
),
816 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NS
, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK
),
817 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_RAT_BUILD_X
),
818 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NE
, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK
),
819 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_RAT_BUILD_EW
),
820 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_EW
, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK
),
821 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_RAT_BUILD_Y
),
822 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NW
, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK
),
823 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_RAT_AUTORAIL
),
824 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_AUTORAIL
, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_AUTORAIL
),
826 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
), SetMinimalSize(4, 22), SetDataTip(0x0, STR_NULL
), EndContainer(),
828 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_RAT_DEMOLISH
),
829 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DYNAMITE
, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC
),
830 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_RAT_BUILD_DEPOT
),
831 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DEPOT_RAIL
, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_TRAIN_DEPOT_FOR_BUILDING
),
832 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_RAT_BUILD_WAYPOINT
),
833 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_WAYPOINT
, STR_RAIL_TOOLBAR_TOOLTIP_CONVERT_RAIL_TO_WAYPOINT
),
834 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_RAT_BUILD_STATION
),
835 SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_RAIL_STATION
, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_STATION
),
836 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_RAT_BUILD_SIGNALS
),
837 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_SIGNALS
, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_SIGNALS
),
838 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_RAT_BUILD_BRIDGE
),
839 SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_BRIDGE
, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_BRIDGE
),
840 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_RAT_BUILD_TUNNEL
),
841 SetFill(0, 1), SetMinimalSize(20, 22), SetDataTip(SPR_IMG_TUNNEL_RAIL
, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TUNNEL
),
842 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_RAT_REMOVE
),
843 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_REMOVE
, STR_RAIL_TOOLBAR_TOOLTIP_TOGGLE_BUILD_REMOVE_FOR
),
844 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_RAT_CONVERT_RAIL
),
845 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_CONVERT_RAIL
, STR_RAIL_TOOLBAR_TOOLTIP_CONVERT_RAIL
),
849 static WindowDesc
_build_rail_desc(
850 WDP_ALIGN_TOOLBAR
, "toolbar_rail", 0, 0,
851 WC_BUILD_TOOLBAR
, WC_NONE
,
853 _nested_build_rail_widgets
, lengthof(_nested_build_rail_widgets
),
854 &BuildRailToolbarWindow::hotkeys
859 * Open the build rail toolbar window for a specific rail type.
861 * If the terraform toolbar is linked to the toolbar, that window is also opened.
863 * @param railtype Rail type to open the window for
864 * @return newly opened rail toolbar, or nullptr if the toolbar could not be opened.
866 Window
*ShowBuildRailToolbar(RailType railtype
)
868 if (!Company::IsValidID(_local_company
)) return nullptr;
869 if (!ValParamRailtype(railtype
)) return nullptr;
871 CloseWindowByClass(WC_BUILD_TOOLBAR
);
872 _cur_railtype
= railtype
;
873 _remove_button_clicked
= false;
874 return new BuildRailToolbarWindow(&_build_rail_desc
, railtype
);
877 /* TODO: For custom stations, respect their allowed platforms/lengths bitmasks!
880 static void HandleStationPlacement(TileIndex start
, TileIndex end
)
882 TileArea
ta(start
, end
);
883 uint numtracks
= ta
.w
;
884 uint platlength
= ta
.h
;
886 if (_railstation
.orientation
== AXIS_X
) Swap(numtracks
, platlength
);
888 uint32 p1
= _cur_railtype
| _railstation
.orientation
<< 6 | numtracks
<< 8 | platlength
<< 16 | _ctrl_pressed
<< 24;
889 uint32 p2
= _railstation
.station_class
| _railstation
.station_type
<< 8 | INVALID_STATION
<< 16;
891 CommandContainer cmdcont
= { ta
.tile
, p1
, p2
, CMD_BUILD_RAIL_STATION
| CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION
), CcStation
, "" };
892 ShowSelectStationIfNeeded(cmdcont
, ta
);
895 /** Enum referring to the Hotkeys in the build rail station window */
896 enum BuildRalStationHotkeys
{
897 BRASHK_FOCUS_FILTER_BOX
, ///< Focus the edit box for editing the filter string
900 struct BuildRailStationWindow
: public PickerWindowBase
{
902 uint line_height
; ///< Height of a single line in the newstation selection matrix (#WID_BRAS_NEWST_LIST widget).
903 uint coverage_height
; ///< Height of the coverage texts.
904 Scrollbar
*vscroll
; ///< Vertical scrollbar of the new station list.
905 Scrollbar
*vscroll2
; ///< Vertical scrollbar of the matrix with new stations.
907 typedef GUIList
<StationClassID
, StringFilter
&> GUIStationClassList
; ///< Type definition for the list to hold available station classes.
909 static const uint EDITBOX_MAX_SIZE
= 16; ///< The maximum number of characters for the filter edit box.
911 static Listing last_sorting
; ///< Default sorting of #GUIStationClassList.
912 static Filtering last_filtering
; ///< Default filtering of #GUIStationClassList.
913 static GUIStationClassList::SortFunction
* const sorter_funcs
[]; ///< Sort functions of the #GUIStationClassList.
914 static GUIStationClassList::FilterFunction
* const filter_funcs
[]; ///< Filter functions of the #GUIStationClassList.
915 GUIStationClassList station_classes
; ///< Available station classes.
916 StringFilter string_filter
; ///< Filter for available station classes.
917 QueryString filter_editbox
; ///< Filter editbox.
920 * Scrolls #WID_BRAS_NEWST_SCROLL so that the selected station class is visible.
922 * Note that this method should be called shortly after SelectClassAndStation() which will ensure
923 * an actual existing station class is selected, or the one at position 0 which will always be
924 * the default TTD rail station.
926 void EnsureSelectedStationClassIsVisible()
929 for (auto station_class
: this->station_classes
) {
930 if (station_class
== _railstation
.station_class
) break;
933 this->vscroll
->SetCount((int)this->station_classes
.size());
934 this->vscroll
->ScrollTowards(pos
);
938 * Verify whether the currently selected station size is allowed after selecting a new station class/type.
939 * If not, change the station size variables ( _settings_client.gui.station_numtracks and _settings_client.gui.station_platlength ).
940 * @param statspec Specification of the new station class/type
942 void CheckSelectedSize(const StationSpec
*statspec
)
944 if (statspec
== nullptr || _settings_client
.gui
.station_dragdrop
) return;
946 /* If current number of tracks is not allowed, make it as big as possible */
947 if (HasBit(statspec
->disallowed_platforms
, _settings_client
.gui
.station_numtracks
- 1)) {
948 this->RaiseWidget(_settings_client
.gui
.station_numtracks
+ WID_BRAS_PLATFORM_NUM_BEGIN
);
949 _settings_client
.gui
.station_numtracks
= 1;
950 if (statspec
->disallowed_platforms
!= UINT8_MAX
) {
951 while (HasBit(statspec
->disallowed_platforms
, _settings_client
.gui
.station_numtracks
- 1)) {
952 _settings_client
.gui
.station_numtracks
++;
954 this->LowerWidget(_settings_client
.gui
.station_numtracks
+ WID_BRAS_PLATFORM_NUM_BEGIN
);
958 if (HasBit(statspec
->disallowed_lengths
, _settings_client
.gui
.station_platlength
- 1)) {
959 this->RaiseWidget(_settings_client
.gui
.station_platlength
+ WID_BRAS_PLATFORM_LEN_BEGIN
);
960 _settings_client
.gui
.station_platlength
= 1;
961 if (statspec
->disallowed_lengths
!= UINT8_MAX
) {
962 while (HasBit(statspec
->disallowed_lengths
, _settings_client
.gui
.station_platlength
- 1)) {
963 _settings_client
.gui
.station_platlength
++;
965 this->LowerWidget(_settings_client
.gui
.station_platlength
+ WID_BRAS_PLATFORM_LEN_BEGIN
);
971 BuildRailStationWindow(WindowDesc
*desc
, Window
*parent
, bool newstation
) : PickerWindowBase(desc
, parent
), filter_editbox(EDITBOX_MAX_SIZE
* MAX_CHAR_LENGTH
, EDITBOX_MAX_SIZE
)
973 this->coverage_height
= 2 * FONT_HEIGHT_NORMAL
+ 3 * WD_PAR_VSEP_NORMAL
;
974 this->vscroll
= nullptr;
975 _railstation
.newstations
= newstation
;
977 this->CreateNestedTree();
978 NWidgetStacked
*newst_additions
= this->GetWidget
<NWidgetStacked
>(WID_BRAS_SHOW_NEWST_ADDITIONS
);
979 newst_additions
->SetDisplayedPlane(newstation
? 0 : SZSP_NONE
);
980 newst_additions
= this->GetWidget
<NWidgetStacked
>(WID_BRAS_SHOW_NEWST_MATRIX
);
981 newst_additions
->SetDisplayedPlane(newstation
? 0 : SZSP_NONE
);
982 newst_additions
= this->GetWidget
<NWidgetStacked
>(WID_BRAS_SHOW_NEWST_DEFSIZE
);
983 newst_additions
->SetDisplayedPlane(newstation
? 0 : SZSP_NONE
);
984 newst_additions
= this->GetWidget
<NWidgetStacked
>(WID_BRAS_SHOW_NEWST_RESIZE
);
985 newst_additions
->SetDisplayedPlane(newstation
? 0 : SZSP_NONE
);
986 /* Hide the station class filter if no stations other than the default one are available. */
987 this->GetWidget
<NWidgetStacked
>(WID_BRAS_FILTER_CONTAINER
)->SetDisplayedPlane(newstation
? 0 : SZSP_NONE
);
989 this->vscroll
= this->GetScrollbar(WID_BRAS_NEWST_SCROLL
);
990 this->vscroll2
= this->GetScrollbar(WID_BRAS_MATRIX_SCROLL
);
992 this->querystrings
[WID_BRAS_FILTER_EDITBOX
] = &this->filter_editbox
;
993 this->station_classes
.SetListing(this->last_sorting
);
994 this->station_classes
.SetFiltering(this->last_filtering
);
995 this->station_classes
.SetSortFuncs(this->sorter_funcs
);
996 this->station_classes
.SetFilterFuncs(this->filter_funcs
);
999 this->station_classes
.ForceRebuild();
1001 BuildStationClassesAvailable();
1002 SelectClassAndStation();
1004 this->FinishInitNested(TRANSPORT_RAIL
);
1006 this->LowerWidget(_railstation
.orientation
+ WID_BRAS_PLATFORM_DIR_X
);
1007 if (_settings_client
.gui
.station_dragdrop
) {
1008 this->LowerWidget(WID_BRAS_PLATFORM_DRAG_N_DROP
);
1010 this->LowerWidget(_settings_client
.gui
.station_numtracks
+ WID_BRAS_PLATFORM_NUM_BEGIN
);
1011 this->LowerWidget(_settings_client
.gui
.station_platlength
+ WID_BRAS_PLATFORM_LEN_BEGIN
);
1013 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_OFF
, !_settings_client
.gui
.station_show_coverage
);
1014 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_ON
, _settings_client
.gui
.station_show_coverage
);
1017 _railstation
.station_class
= StationClassID::STAT_CLASS_DFLT
;
1018 _railstation
.station_type
= 0;
1019 this->vscroll2
= nullptr;
1021 _railstation
.station_count
= StationClass::Get(_railstation
.station_class
)->GetSpecCount();
1022 _railstation
.station_type
= std::min
<int>(_railstation
.station_type
, _railstation
.station_count
- 1);
1024 NWidgetMatrix
*matrix
= this->GetWidget
<NWidgetMatrix
>(WID_BRAS_MATRIX
);
1025 matrix
->SetScrollbar(this->vscroll2
);
1026 matrix
->SetCount(_railstation
.station_count
);
1027 matrix
->SetClicked(_railstation
.station_type
);
1029 EnsureSelectedStationClassIsVisible();
1032 this->InvalidateData();
1035 void Close() override
1037 CloseWindowById(WC_SELECT_STATION
, 0);
1038 this->PickerWindowBase::Close();
1041 /** Sort station classes by StationClassID. */
1042 static bool StationClassIDSorter(StationClassID
const &a
, StationClassID
const &b
)
1047 /** Filter station classes by class name. */
1048 static bool CDECL
TagNameFilter(StationClassID
const * sc
, StringFilter
&filter
)
1050 char buffer
[DRAW_STRING_BUFFER
];
1051 GetString(buffer
, StationClass::Get(*sc
)->name
, lastof(buffer
));
1053 filter
.ResetState();
1054 filter
.AddLine(buffer
);
1055 return filter
.GetState();
1058 /** Builds the filter list of available station classes. */
1059 void BuildStationClassesAvailable()
1061 if (!this->station_classes
.NeedRebuild()) return;
1063 this->station_classes
.clear();
1065 for (uint i
= 0; i
< StationClass::GetClassCount(); i
++) {
1066 StationClassID station_class_id
= (StationClassID
)i
;
1067 if (station_class_id
== StationClassID::STAT_CLASS_WAYP
) {
1071 StationClass
*station_class
= StationClass::Get(station_class_id
);
1072 if (station_class
->GetUISpecCount() == 0) continue;
1073 station_classes
.push_back(station_class_id
);
1076 if (_railstation
.newstations
) {
1077 this->station_classes
.Filter(this->string_filter
);
1078 this->station_classes
.shrink_to_fit();
1079 this->station_classes
.RebuildDone();
1080 this->station_classes
.Sort();
1082 this->vscroll
->SetCount((uint
)this->station_classes
.size());
1087 * Checks if the previously selected current station class and station
1088 * can be shown as selected to the user when the dialog is opened.
1090 void SelectClassAndStation()
1092 if (_railstation
.station_class
== StationClassID::STAT_CLASS_DFLT
) {
1093 /* This happens during the first time the window is open during the game life cycle. */
1094 this->SelectOtherClass(StationClassID::STAT_CLASS_DFLT
);
1096 /* Check if the previously selected station class is not available anymore as a
1097 * result of starting a new game without the corresponding NewGRF. */
1098 bool available
= false;
1099 for (uint i
= 0; i
< StationClass::GetClassCount(); ++i
) {
1100 if ((StationClassID
)i
== _railstation
.station_class
) {
1106 this->SelectOtherClass(available
? _railstation
.station_class
: StationClassID::STAT_CLASS_DFLT
);
1111 * Select the specified station class.
1112 * @param station_class Station class select.
1114 void SelectOtherClass(StationClassID station_class
)
1116 _railstation
.station_class
= station_class
;
1119 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
1121 if (!gui_scope
) return;
1123 this->BuildStationClassesAvailable();
1126 EventState
OnHotkey(int hotkey
) override
1129 case BRASHK_FOCUS_FILTER_BOX
:
1130 this->SetFocusedWidget(WID_BRAS_FILTER_EDITBOX
);
1131 SetFocusedWindow(this); // The user has asked to give focus to the text box, so make sure this window is focused.
1135 return ES_NOT_HANDLED
;
1141 void OnEditboxChanged(int wid
) override
1143 string_filter
.SetFilterTerm(this->filter_editbox
.text
.buf
);
1144 this->station_classes
.SetFilterState(!string_filter
.IsEmpty());
1145 this->station_classes
.ForceRebuild();
1146 this->InvalidateData();
1149 void OnPaint() override
1151 bool newstations
= _railstation
.newstations
;
1152 const StationSpec
*statspec
= newstations
? StationClass::Get(_railstation
.station_class
)->GetSpec(_railstation
.station_type
) : nullptr;
1154 if (_settings_client
.gui
.station_dragdrop
) {
1155 SetTileSelectSize(1, 1);
1157 int x
= _settings_client
.gui
.station_numtracks
;
1158 int y
= _settings_client
.gui
.station_platlength
;
1159 if (_railstation
.orientation
== AXIS_X
) Swap(x
, y
);
1160 if (!_remove_button_clicked
) {
1161 SetTileSelectSize(x
, y
);
1165 int rad
= (_settings_game
.station
.modified_catchment
) ? CA_TRAIN
: CA_UNMODIFIED
;
1167 if (_settings_client
.gui
.station_show_coverage
) SetTileSelectBigSize(-rad
, -rad
, 2 * rad
, 2 * rad
);
1169 for (uint bits
= 0; bits
< 7; bits
++) {
1170 bool disable
= bits
>= _settings_game
.station
.station_spread
;
1171 if (statspec
== nullptr) {
1172 this->SetWidgetDisabledState(bits
+ WID_BRAS_PLATFORM_NUM_1
, disable
);
1173 this->SetWidgetDisabledState(bits
+ WID_BRAS_PLATFORM_LEN_1
, disable
);
1175 this->SetWidgetDisabledState(bits
+ WID_BRAS_PLATFORM_NUM_1
, HasBit(statspec
->disallowed_platforms
, bits
) || disable
);
1176 this->SetWidgetDisabledState(bits
+ WID_BRAS_PLATFORM_LEN_1
, HasBit(statspec
->disallowed_lengths
, bits
) || disable
);
1180 this->DrawWidgets();
1182 if (this->IsShaded()) return;
1183 /* 'Accepts' and 'Supplies' texts. */
1184 NWidgetBase
*cov
= this->GetWidget
<NWidgetBase
>(WID_BRAS_COVERAGE_TEXTS
);
1185 int top
= cov
->pos_y
+ WD_PAR_VSEP_NORMAL
;
1186 int left
= cov
->pos_x
+ WD_FRAMERECT_LEFT
;
1187 int right
= cov
->pos_x
+ cov
->current_x
- WD_FRAMERECT_RIGHT
;
1188 int bottom
= cov
->pos_y
+ cov
->current_y
;
1189 top
= DrawStationCoverageAreaText(left
, right
, top
, SCT_ALL
, rad
, false) + WD_PAR_VSEP_NORMAL
;
1190 top
= DrawStationCoverageAreaText(left
, right
, top
, SCT_ALL
, rad
, true) + WD_PAR_VSEP_NORMAL
;
1191 /* Resize background if the window is too small.
1192 * Never make the window smaller to avoid oscillating if the size change affects the acceptance.
1193 * (This is the case, if making the window bigger moves the mouse into the window.) */
1195 this->coverage_height
+= top
- bottom
;
1200 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
1203 case WID_BRAS_NEWST_LIST
: {
1204 Dimension d
= {0, 0};
1205 for (auto station_class
: this->station_classes
) {
1206 d
= maxdim(d
, GetStringBoundingBox(StationClass::Get(station_class
)->name
));
1208 size
->width
= std::max(size
->width
, d
.width
+ padding
.width
);
1209 this->line_height
= FONT_HEIGHT_NORMAL
+ WD_MATRIX_TOP
+ WD_MATRIX_BOTTOM
;
1210 size
->height
= 5 * this->line_height
;
1211 resize
->height
= this->line_height
;
1215 case WID_BRAS_SHOW_NEWST_TYPE
: {
1216 if (!_railstation
.newstations
) {
1222 /* If newstations exist, compute the non-zero minimal size. */
1223 Dimension d
= {0, 0};
1224 StringID str
= this->GetWidget
<NWidgetCore
>(widget
)->widget_data
;
1225 for (auto station_class
: this->station_classes
) {
1226 StationClass
*stclass
= StationClass::Get(station_class
);
1227 for (uint j
= 0; j
< stclass
->GetSpecCount(); j
++) {
1228 const StationSpec
*statspec
= stclass
->GetSpec(j
);
1229 SetDParam(0, (statspec
!= nullptr && statspec
->name
!= 0) ? statspec
->name
: STR_STATION_CLASS_DFLT
);
1230 d
= maxdim(d
, GetStringBoundingBox(str
));
1233 size
->width
= std::max(size
->width
, d
.width
+ padding
.width
);
1237 case WID_BRAS_PLATFORM_DIR_X
:
1238 case WID_BRAS_PLATFORM_DIR_Y
:
1239 case WID_BRAS_IMAGE
:
1240 size
->width
= ScaleGUITrad(64) + 2;
1241 size
->height
= ScaleGUITrad(58) + 2;
1244 case WID_BRAS_COVERAGE_TEXTS
:
1245 size
->height
= this->coverage_height
;
1248 case WID_BRAS_MATRIX
:
1255 void DrawWidget(const Rect
&r
, int widget
) const override
1257 DrawPixelInfo tmp_dpi
;
1259 switch (GB(widget
, 0, 16)) {
1260 case WID_BRAS_PLATFORM_DIR_X
:
1261 /* Set up a clipping area for the '/' station preview */
1262 if (FillDrawPixelInfo(&tmp_dpi
, r
.left
, r
.top
, r
.right
- r
.left
+ 1, r
.bottom
- r
.top
+ 1)) {
1263 DrawPixelInfo
*old_dpi
= _cur_dpi
;
1264 _cur_dpi
= &tmp_dpi
;
1265 int x
= ScaleGUITrad(31) + 1;
1266 int y
= r
.bottom
- r
.top
- ScaleGUITrad(31);
1267 if (!DrawStationTile(x
, y
, _cur_railtype
, AXIS_X
, _railstation
.station_class
, _railstation
.station_type
)) {
1268 StationPickerDrawSprite(x
, y
, STATION_RAIL
, _cur_railtype
, INVALID_ROADTYPE
, 2);
1274 case WID_BRAS_PLATFORM_DIR_Y
:
1275 /* Set up a clipping area for the '\' station preview */
1276 if (FillDrawPixelInfo(&tmp_dpi
, r
.left
, r
.top
, r
.right
- r
.left
+ 1, r
.bottom
- r
.top
+ 1)) {
1277 DrawPixelInfo
*old_dpi
= _cur_dpi
;
1278 _cur_dpi
= &tmp_dpi
;
1279 int x
= ScaleGUITrad(31) + 1;
1280 int y
= r
.bottom
- r
.top
- ScaleGUITrad(31);
1281 if (!DrawStationTile(x
, y
, _cur_railtype
, AXIS_Y
, _railstation
.station_class
, _railstation
.station_type
)) {
1282 StationPickerDrawSprite(x
, y
, STATION_RAIL
, _cur_railtype
, INVALID_ROADTYPE
, 3);
1288 case WID_BRAS_NEWST_LIST
: {
1291 for (auto station_class
: this->station_classes
) {
1292 if (this->vscroll
->IsVisible(statclass
)) {
1293 DrawString(r
.left
+ WD_MATRIX_LEFT
, r
.right
- WD_MATRIX_RIGHT
, row
* this->line_height
+ r
.top
+ WD_MATRIX_TOP
,
1294 StationClass::Get(station_class
)->name
,
1295 station_class
== _railstation
.station_class
? TC_WHITE
: TC_BLACK
);
1303 case WID_BRAS_IMAGE
: {
1304 byte type
= GB(widget
, 16, 16);
1305 assert(type
< _railstation
.station_count
);
1306 /* Check station availability callback */
1307 const StationSpec
*statspec
= StationClass::Get(_railstation
.station_class
)->GetSpec(type
);
1308 if (!IsStationAvailable(statspec
)) {
1309 GfxFillRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, PC_BLACK
, FILLRECT_CHECKER
);
1312 /* Set up a clipping area for the station preview. */
1313 if (FillDrawPixelInfo(&tmp_dpi
, r
.left
, r
.top
, r
.right
- r
.left
+ 1, r
.bottom
- r
.top
+ 1)) {
1314 DrawPixelInfo
*old_dpi
= _cur_dpi
;
1315 _cur_dpi
= &tmp_dpi
;
1316 int x
= ScaleGUITrad(31) + 1;
1317 int y
= r
.bottom
- r
.top
- ScaleGUITrad(31);
1318 if (!DrawStationTile(x
, y
, _cur_railtype
, _railstation
.orientation
, _railstation
.station_class
, type
)) {
1319 StationPickerDrawSprite(x
, y
, STATION_RAIL
, _cur_railtype
, INVALID_ROADTYPE
, 2 + _railstation
.orientation
);
1328 void OnResize() override
1330 if (this->vscroll
!= nullptr) { // New stations available.
1331 this->vscroll
->SetCapacityFromWidget(this, WID_BRAS_NEWST_LIST
);
1335 void SetStringParameters(int widget
) const override
1337 if (widget
== WID_BRAS_SHOW_NEWST_TYPE
) {
1338 const StationSpec
*statspec
= StationClass::Get(_railstation
.station_class
)->GetSpec(_railstation
.station_type
);
1339 SetDParam(0, (statspec
!= nullptr && statspec
->name
!= 0) ? statspec
->name
: STR_STATION_CLASS_DFLT
);
1343 void OnClick(Point pt
, int widget
, int click_count
) override
1345 switch (GB(widget
, 0, 16)) {
1346 case WID_BRAS_PLATFORM_DIR_X
:
1347 case WID_BRAS_PLATFORM_DIR_Y
:
1348 this->RaiseWidget(_railstation
.orientation
+ WID_BRAS_PLATFORM_DIR_X
);
1349 _railstation
.orientation
= (Axis
)(widget
- WID_BRAS_PLATFORM_DIR_X
);
1350 this->LowerWidget(_railstation
.orientation
+ WID_BRAS_PLATFORM_DIR_X
);
1351 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
1353 CloseWindowById(WC_SELECT_STATION
, 0);
1356 case WID_BRAS_PLATFORM_NUM_1
:
1357 case WID_BRAS_PLATFORM_NUM_2
:
1358 case WID_BRAS_PLATFORM_NUM_3
:
1359 case WID_BRAS_PLATFORM_NUM_4
:
1360 case WID_BRAS_PLATFORM_NUM_5
:
1361 case WID_BRAS_PLATFORM_NUM_6
:
1362 case WID_BRAS_PLATFORM_NUM_7
: {
1363 this->RaiseWidget(_settings_client
.gui
.station_numtracks
+ WID_BRAS_PLATFORM_NUM_BEGIN
);
1364 this->RaiseWidget(WID_BRAS_PLATFORM_DRAG_N_DROP
);
1366 _settings_client
.gui
.station_numtracks
= widget
- WID_BRAS_PLATFORM_NUM_BEGIN
;
1367 _settings_client
.gui
.station_dragdrop
= false;
1369 _settings_client
.gui
.station_dragdrop
= false;
1371 const StationSpec
*statspec
= _railstation
.newstations
? StationClass::Get(_railstation
.station_class
)->GetSpec(_railstation
.station_type
) : nullptr;
1372 if (statspec
!= nullptr && HasBit(statspec
->disallowed_lengths
, _settings_client
.gui
.station_platlength
- 1)) {
1373 /* The previously selected number of platforms in invalid */
1374 for (uint i
= 0; i
< 7; i
++) {
1375 if (!HasBit(statspec
->disallowed_lengths
, i
)) {
1376 this->RaiseWidget(_settings_client
.gui
.station_platlength
+ WID_BRAS_PLATFORM_LEN_BEGIN
);
1377 _settings_client
.gui
.station_platlength
= i
+ 1;
1383 this->LowerWidget(_settings_client
.gui
.station_numtracks
+ WID_BRAS_PLATFORM_NUM_BEGIN
);
1384 this->LowerWidget(_settings_client
.gui
.station_platlength
+ WID_BRAS_PLATFORM_LEN_BEGIN
);
1385 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
1387 CloseWindowById(WC_SELECT_STATION
, 0);
1391 case WID_BRAS_PLATFORM_LEN_1
:
1392 case WID_BRAS_PLATFORM_LEN_2
:
1393 case WID_BRAS_PLATFORM_LEN_3
:
1394 case WID_BRAS_PLATFORM_LEN_4
:
1395 case WID_BRAS_PLATFORM_LEN_5
:
1396 case WID_BRAS_PLATFORM_LEN_6
:
1397 case WID_BRAS_PLATFORM_LEN_7
: {
1398 this->RaiseWidget(_settings_client
.gui
.station_platlength
+ WID_BRAS_PLATFORM_LEN_BEGIN
);
1399 this->RaiseWidget(WID_BRAS_PLATFORM_DRAG_N_DROP
);
1401 _settings_client
.gui
.station_platlength
= widget
- WID_BRAS_PLATFORM_LEN_BEGIN
;
1402 _settings_client
.gui
.station_dragdrop
= false;
1404 _settings_client
.gui
.station_dragdrop
= false;
1406 const StationSpec
*statspec
= _railstation
.newstations
? StationClass::Get(_railstation
.station_class
)->GetSpec(_railstation
.station_type
) : nullptr;
1407 if (statspec
!= nullptr && HasBit(statspec
->disallowed_platforms
, _settings_client
.gui
.station_numtracks
- 1)) {
1408 /* The previously selected number of tracks in invalid */
1409 for (uint i
= 0; i
< 7; i
++) {
1410 if (!HasBit(statspec
->disallowed_platforms
, i
)) {
1411 this->RaiseWidget(_settings_client
.gui
.station_numtracks
+ WID_BRAS_PLATFORM_NUM_BEGIN
);
1412 _settings_client
.gui
.station_numtracks
= i
+ 1;
1418 this->LowerWidget(_settings_client
.gui
.station_numtracks
+ WID_BRAS_PLATFORM_NUM_BEGIN
);
1419 this->LowerWidget(_settings_client
.gui
.station_platlength
+ WID_BRAS_PLATFORM_LEN_BEGIN
);
1420 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
1422 CloseWindowById(WC_SELECT_STATION
, 0);
1426 case WID_BRAS_PLATFORM_DRAG_N_DROP
: {
1427 _settings_client
.gui
.station_dragdrop
^= true;
1429 this->ToggleWidgetLoweredState(WID_BRAS_PLATFORM_DRAG_N_DROP
);
1431 /* get the first allowed length/number of platforms */
1432 const StationSpec
*statspec
= _railstation
.newstations
? StationClass::Get(_railstation
.station_class
)->GetSpec(_railstation
.station_type
) : nullptr;
1433 if (statspec
!= nullptr && HasBit(statspec
->disallowed_lengths
, _settings_client
.gui
.station_platlength
- 1)) {
1434 for (uint i
= 0; i
< 7; i
++) {
1435 if (!HasBit(statspec
->disallowed_lengths
, i
)) {
1436 this->RaiseWidget(_settings_client
.gui
.station_platlength
+ WID_BRAS_PLATFORM_LEN_BEGIN
);
1437 _settings_client
.gui
.station_platlength
= i
+ 1;
1442 if (statspec
!= nullptr && HasBit(statspec
->disallowed_platforms
, _settings_client
.gui
.station_numtracks
- 1)) {
1443 for (uint i
= 0; i
< 7; i
++) {
1444 if (!HasBit(statspec
->disallowed_platforms
, i
)) {
1445 this->RaiseWidget(_settings_client
.gui
.station_numtracks
+ WID_BRAS_PLATFORM_NUM_BEGIN
);
1446 _settings_client
.gui
.station_numtracks
= i
+ 1;
1452 this->SetWidgetLoweredState(_settings_client
.gui
.station_numtracks
+ WID_BRAS_PLATFORM_NUM_BEGIN
, !_settings_client
.gui
.station_dragdrop
);
1453 this->SetWidgetLoweredState(_settings_client
.gui
.station_platlength
+ WID_BRAS_PLATFORM_LEN_BEGIN
, !_settings_client
.gui
.station_dragdrop
);
1454 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
1456 CloseWindowById(WC_SELECT_STATION
, 0);
1460 case WID_BRAS_HIGHLIGHT_OFF
:
1461 case WID_BRAS_HIGHLIGHT_ON
:
1462 _settings_client
.gui
.station_show_coverage
= (widget
!= WID_BRAS_HIGHLIGHT_OFF
);
1464 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_OFF
, !_settings_client
.gui
.station_show_coverage
);
1465 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_ON
, _settings_client
.gui
.station_show_coverage
);
1466 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
1468 SetViewportCatchmentStation(nullptr, true);
1471 case WID_BRAS_NEWST_LIST
: {
1472 int y
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_BRAS_NEWST_LIST
);
1473 if (y
>= (int)this->station_classes
.size()) return;
1474 StationClassID station_class_id
= this->station_classes
[y
];
1475 if (_railstation
.station_class
!= station_class_id
) {
1476 StationClass
*station_class
= StationClass::Get(station_class_id
);
1477 _railstation
.station_class
= station_class_id
;
1478 _railstation
.station_count
= station_class
->GetSpecCount();
1479 _railstation
.station_type
= 0;
1481 this->CheckSelectedSize(station_class
->GetSpec(_railstation
.station_type
));
1483 NWidgetMatrix
*matrix
= this->GetWidget
<NWidgetMatrix
>(WID_BRAS_MATRIX
);
1484 matrix
->SetCount(_railstation
.station_count
);
1485 matrix
->SetClicked(_railstation
.station_type
);
1487 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
1489 CloseWindowById(WC_SELECT_STATION
, 0);
1493 case WID_BRAS_IMAGE
: {
1494 int y
= GB(widget
, 16, 16);
1495 if (y
>= _railstation
.station_count
) return;
1497 /* Check station availability callback */
1498 const StationSpec
*statspec
= StationClass::Get(_railstation
.station_class
)->GetSpec(y
);
1499 if (!IsStationAvailable(statspec
)) return;
1501 _railstation
.station_type
= y
;
1503 this->CheckSelectedSize(statspec
);
1504 this->GetWidget
<NWidgetMatrix
>(WID_BRAS_MATRIX
)->SetClicked(_railstation
.station_type
);
1506 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
1508 CloseWindowById(WC_SELECT_STATION
, 0);
1514 void OnRealtimeTick(uint delta_ms
) override
1516 CheckRedrawStationCoverage(this);
1519 static HotkeyList hotkeys
;
1523 * Handler for global hotkeys of the BuildRailStationWindow.
1524 * @param hotkey Hotkey
1525 * @return ES_HANDLED if hotkey was accepted.
1527 static EventState
BuildRailStationGlobalHotkeys(int hotkey
)
1529 if (_game_mode
== GM_MENU
) return ES_NOT_HANDLED
;
1530 Window
*w
= ShowStationBuilder(FindWindowById(WC_BUILD_TOOLBAR
, TRANSPORT_RAIL
));
1531 if (w
== nullptr) return ES_NOT_HANDLED
;
1532 return w
->OnHotkey(hotkey
);
1535 static Hotkey buildrailstation_hotkeys
[] = {
1536 Hotkey('F', "focus_filter_box", BRASHK_FOCUS_FILTER_BOX
),
1539 HotkeyList
BuildRailStationWindow::hotkeys("buildrailstation", buildrailstation_hotkeys
, BuildRailStationGlobalHotkeys
);
1541 Listing
BuildRailStationWindow::last_sorting
= { false, 0 };
1542 Filtering
BuildRailStationWindow::last_filtering
= { false, 0 };
1544 BuildRailStationWindow::GUIStationClassList::SortFunction
* const BuildRailStationWindow::sorter_funcs
[] = {
1545 &StationClassIDSorter
,
1548 BuildRailStationWindow::GUIStationClassList::FilterFunction
* const BuildRailStationWindow::filter_funcs
[] = {
1552 static const NWidgetPart _nested_station_builder_widgets
[] = {
1553 NWidget(NWID_HORIZONTAL
),
1554 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
1555 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
), SetDataTip(STR_STATION_BUILD_RAIL_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1556 NWidget(WWT_SHADEBOX
, COLOUR_DARK_GREEN
),
1557 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_BRAS_SHOW_NEWST_DEFSIZE
),
1558 NWidget(WWT_DEFSIZEBOX
, COLOUR_DARK_GREEN
),
1561 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
),
1562 NWidget(NWID_HORIZONTAL
), SetPadding(2, 0, 0, 2),
1563 NWidget(NWID_VERTICAL
),
1564 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_BRAS_FILTER_CONTAINER
),
1565 NWidget(NWID_HORIZONTAL
), SetPadding(0, 5, 2, 0),
1566 NWidget(WWT_TEXT
, COLOUR_DARK_GREEN
), SetFill(0, 1), SetDataTip(STR_LIST_FILTER_TITLE
, STR_NULL
),
1567 NWidget(WWT_EDITBOX
, COLOUR_GREY
, WID_BRAS_FILTER_EDITBOX
), SetFill(1, 0), SetResize(1, 0),
1568 SetDataTip(STR_LIST_FILTER_OSKTITLE
, STR_LIST_FILTER_TOOLTIP
),
1571 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_BRAS_SHOW_NEWST_ADDITIONS
),
1572 NWidget(NWID_HORIZONTAL
), SetPadding(0, 5, 2, 0),
1573 NWidget(WWT_MATRIX
, COLOUR_GREY
, WID_BRAS_NEWST_LIST
), SetMinimalSize(122, 71), SetFill(1, 0),
1574 SetMatrixDataTip(1, 0, STR_STATION_BUILD_STATION_CLASS_TOOLTIP
), SetScrollbar(WID_BRAS_NEWST_SCROLL
),
1575 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_BRAS_NEWST_SCROLL
),
1578 NWidget(WWT_LABEL
, COLOUR_DARK_GREEN
), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_ORIENTATION
, STR_NULL
), SetPadding(1, 2, 0, 0),
1579 NWidget(NWID_HORIZONTAL
),
1580 NWidget(NWID_SPACER
), SetMinimalSize(7, 0), SetFill(1, 0),
1581 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(),
1582 NWidget(NWID_SPACER
), SetMinimalSize(2, 0), SetFill(1, 0),
1583 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(),
1584 NWidget(NWID_SPACER
), SetMinimalSize(7, 0), SetFill(1, 0),
1586 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),
1587 NWidget(WWT_LABEL
, COLOUR_DARK_GREEN
), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_NUMBER_OF_TRACKS
, STR_NULL
), SetPadding(0, 2, 0, 2),
1588 NWidget(NWID_HORIZONTAL
),
1589 NWidget(NWID_SPACER
), SetFill(1, 0),
1590 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BRAS_PLATFORM_NUM_1
), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_1
, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP
),
1591 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BRAS_PLATFORM_NUM_2
), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_2
, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP
),
1592 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BRAS_PLATFORM_NUM_3
), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_3
, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP
),
1593 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BRAS_PLATFORM_NUM_4
), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_4
, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP
),
1594 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BRAS_PLATFORM_NUM_5
), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_5
, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP
),
1595 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BRAS_PLATFORM_NUM_6
), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_6
, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP
),
1596 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BRAS_PLATFORM_NUM_7
), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_7
, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP
),
1597 NWidget(NWID_SPACER
), SetFill(1, 0),
1599 NWidget(WWT_LABEL
, COLOUR_DARK_GREEN
), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_PLATFORM_LENGTH
, STR_NULL
), SetPadding(2, 2, 0, 2),
1600 NWidget(NWID_HORIZONTAL
),
1601 NWidget(NWID_SPACER
), SetFill(1, 0),
1602 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BRAS_PLATFORM_LEN_1
), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_1
, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP
),
1603 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BRAS_PLATFORM_LEN_2
), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_2
, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP
),
1604 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BRAS_PLATFORM_LEN_3
), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_3
, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP
),
1605 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BRAS_PLATFORM_LEN_4
), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_4
, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP
),
1606 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BRAS_PLATFORM_LEN_5
), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_5
, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP
),
1607 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BRAS_PLATFORM_LEN_6
), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_6
, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP
),
1608 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BRAS_PLATFORM_LEN_7
), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_7
, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP
),
1609 NWidget(NWID_SPACER
), SetFill(1, 0),
1611 NWidget(NWID_SPACER
), SetMinimalSize(0, 2),
1612 NWidget(NWID_HORIZONTAL
),
1613 NWidget(NWID_SPACER
), SetMinimalSize(2, 0), SetFill(1, 0),
1614 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
),
1615 NWidget(NWID_SPACER
), SetMinimalSize(2, 0), SetFill(1, 0),
1617 NWidget(WWT_LABEL
, COLOUR_DARK_GREEN
), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_COVERAGE_AREA_TITLE
, STR_NULL
), SetPadding(3, 2, 0, 0),
1618 NWidget(NWID_HORIZONTAL
),
1619 NWidget(NWID_SPACER
), SetMinimalSize(2, 0), SetFill(1, 0),
1620 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BRAS_HIGHLIGHT_OFF
), SetMinimalSize(60, 12),
1621 SetDataTip(STR_STATION_BUILD_COVERAGE_OFF
, STR_STATION_BUILD_COVERAGE_AREA_OFF_TOOLTIP
),
1622 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BRAS_HIGHLIGHT_ON
), SetMinimalSize(60, 12),
1623 SetDataTip(STR_STATION_BUILD_COVERAGE_ON
, STR_STATION_BUILD_COVERAGE_AREA_ON_TOOLTIP
),
1624 NWidget(NWID_SPACER
), SetMinimalSize(2, 0), SetFill(1, 0),
1627 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_BRAS_SHOW_NEWST_MATRIX
),
1628 /* We need an additional background for the matrix, as the matrix cannot handle the scrollbar due to not being an NWidgetCore. */
1629 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
), SetScrollbar(WID_BRAS_MATRIX_SCROLL
),
1630 NWidget(NWID_HORIZONTAL
),
1631 NWidget(NWID_MATRIX
, COLOUR_DARK_GREEN
, WID_BRAS_MATRIX
), SetScrollbar(WID_BRAS_MATRIX_SCROLL
), SetPIP(0, 2, 0),
1632 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_BRAS_IMAGE
), SetMinimalSize(66, 60),
1633 SetFill(0, 0), SetResize(0, 0), SetDataTip(0x0, STR_STATION_BUILD_STATION_TYPE_TOOLTIP
), SetScrollbar(WID_BRAS_MATRIX_SCROLL
),
1636 NWidget(NWID_VSCROLLBAR
, COLOUR_DARK_GREEN
, WID_BRAS_MATRIX_SCROLL
),
1641 NWidget(NWID_HORIZONTAL
),
1642 NWidget(WWT_EMPTY
, INVALID_COLOUR
, WID_BRAS_COVERAGE_TEXTS
), SetPadding(2, 5, 0, 1), SetFill(1, 1), SetResize(1, 0),
1643 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_BRAS_SHOW_NEWST_RESIZE
),
1644 NWidget(NWID_VERTICAL
),
1645 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
), SetFill(0, 1), EndContainer(),
1646 NWidget(WWT_RESIZEBOX
, COLOUR_DARK_GREEN
),
1653 /** High level window description of the station-build window (default & newGRF) */
1654 static WindowDesc
_station_builder_desc(
1655 WDP_AUTO
, "build_station_rail", 350, 0,
1656 WC_BUILD_STATION
, WC_BUILD_TOOLBAR
,
1658 _nested_station_builder_widgets
, lengthof(_nested_station_builder_widgets
),
1659 &BuildRailStationWindow::hotkeys
1662 /** Open station build window */
1663 static Window
*ShowStationBuilder(Window
*parent
)
1665 bool newstations
= StationClass::GetClassCount() > 2 || StationClass::Get(STAT_CLASS_DFLT
)->GetSpecCount() != 1;
1666 return new BuildRailStationWindow(&_station_builder_desc
, parent
, newstations
);
1669 struct BuildSignalWindow
: public PickerWindowBase
{
1671 Dimension sig_sprite_size
; ///< Maximum size of signal GUI sprites.
1672 int sig_sprite_bottom_offset
; ///< Maximum extent of signal GUI sprite from reference point towards bottom.
1675 * Draw dynamic a signal-sprite in a button in the signal GUI
1676 * Draw the sprite +1px to the right and down if the button is lowered
1678 * @param widget_index index of this widget in the window
1679 * @param image the sprite to draw
1681 void DrawSignalSprite(byte widget_index
, SpriteID image
) const
1684 Dimension sprite_size
= GetSpriteSize(image
, &offset
);
1685 const NWidgetBase
*widget
= this->GetWidget
<NWidgetBase
>(widget_index
);
1686 int x
= widget
->pos_x
- offset
.x
+
1687 (widget
->current_x
- sprite_size
.width
+ offset
.x
) / 2; // centered
1688 int y
= widget
->pos_y
- sig_sprite_bottom_offset
+ WD_IMGBTN_TOP
+
1689 (widget
->current_y
- WD_IMGBTN_TOP
- WD_IMGBTN_BOTTOM
+ sig_sprite_size
.height
) / 2; // aligned to bottom
1691 DrawSprite(image
, PAL_NONE
,
1692 x
+ this->IsWidgetLowered(widget_index
),
1693 y
+ this->IsWidgetLowered(widget_index
));
1696 /** Show or hide buttons for non-path signals in the signal GUI */
1697 void SetSignalUIMode()
1699 bool show_non_path_signals
= (_settings_client
.gui
.signal_gui_mode
== SIGNAL_GUI_ALL
);
1701 this->GetWidget
<NWidgetStacked
>(WID_BS_SEMAPHORE_NORM_SEL
)->SetDisplayedPlane(show_non_path_signals
? 0 : SZSP_NONE
);
1702 this->GetWidget
<NWidgetStacked
>(WID_BS_ELECTRIC_NORM_SEL
)->SetDisplayedPlane(show_non_path_signals
? 0 : SZSP_NONE
);
1703 this->GetWidget
<NWidgetStacked
>(WID_BS_SEMAPHORE_ENTRY_SEL
)->SetDisplayedPlane(show_non_path_signals
? 0 : SZSP_NONE
);
1704 this->GetWidget
<NWidgetStacked
>(WID_BS_ELECTRIC_ENTRY_SEL
)->SetDisplayedPlane(show_non_path_signals
? 0 : SZSP_NONE
);
1705 this->GetWidget
<NWidgetStacked
>(WID_BS_SEMAPHORE_EXIT_SEL
)->SetDisplayedPlane(show_non_path_signals
? 0 : SZSP_NONE
);
1706 this->GetWidget
<NWidgetStacked
>(WID_BS_ELECTRIC_EXIT_SEL
)->SetDisplayedPlane(show_non_path_signals
? 0 : SZSP_NONE
);
1707 this->GetWidget
<NWidgetStacked
>(WID_BS_SEMAPHORE_COMBO_SEL
)->SetDisplayedPlane(show_non_path_signals
? 0 : SZSP_NONE
);
1708 this->GetWidget
<NWidgetStacked
>(WID_BS_ELECTRIC_COMBO_SEL
)->SetDisplayedPlane(show_non_path_signals
? 0 : SZSP_NONE
);
1712 BuildSignalWindow(WindowDesc
*desc
, Window
*parent
) : PickerWindowBase(desc
, parent
)
1714 this->CreateNestedTree();
1715 this->SetSignalUIMode();
1716 this->FinishInitNested(TRANSPORT_RAIL
);
1717 this->OnInvalidateData();
1720 void Close() override
1722 _convert_signal_button
= false;
1723 this->PickerWindowBase::Close();
1726 void OnInit() override
1728 /* Calculate maximum signal sprite size. */
1729 this->sig_sprite_size
.width
= 0;
1730 this->sig_sprite_size
.height
= 0;
1731 this->sig_sprite_bottom_offset
= 0;
1732 const RailtypeInfo
*rti
= GetRailTypeInfo(_cur_railtype
);
1733 for (uint type
= SIGTYPE_NORMAL
; type
< SIGTYPE_END
; type
++) {
1734 for (uint variant
= SIG_ELECTRIC
; variant
<= SIG_SEMAPHORE
; variant
++) {
1735 for (uint lowered
= 0; lowered
< 2; lowered
++) {
1737 Dimension sprite_size
= GetSpriteSize(rti
->gui_sprites
.signals
[type
][variant
][lowered
], &offset
);
1738 this->sig_sprite_bottom_offset
= std::max
<int>(this->sig_sprite_bottom_offset
, sprite_size
.height
);
1739 this->sig_sprite_size
.width
= std::max
<int>(this->sig_sprite_size
.width
, sprite_size
.width
- offset
.x
);
1740 this->sig_sprite_size
.height
= std::max
<int>(this->sig_sprite_size
.height
, sprite_size
.height
- offset
.y
);
1746 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
1748 if (widget
== WID_BS_DRAG_SIGNALS_DENSITY_LABEL
) {
1749 /* Two digits for signals density. */
1750 size
->width
= std::max(size
->width
, 2 * GetDigitWidth() + padding
.width
+ WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
);
1751 } else if (IsInsideMM(widget
, WID_BS_SEMAPHORE_NORM
, WID_BS_ELECTRIC_PBS_OWAY
+ 1)) {
1752 size
->width
= std::max(size
->width
, this->sig_sprite_size
.width
+ WD_IMGBTN_LEFT
+ WD_IMGBTN_RIGHT
);
1753 size
->height
= std::max(size
->height
, this->sig_sprite_size
.height
+ WD_IMGBTN_TOP
+ WD_IMGBTN_BOTTOM
);
1754 } else if (widget
== WID_BS_CAPTION
) {
1755 size
->width
+= WD_FRAMETEXT_LEFT
+ WD_FRAMETEXT_RIGHT
;
1759 void SetStringParameters(int widget
) const override
1762 case WID_BS_DRAG_SIGNALS_DENSITY_LABEL
:
1763 SetDParam(0, _settings_client
.gui
.drag_signals_density
);
1768 void DrawWidget(const Rect
&r
, int widget
) const override
1770 if (IsInsideMM(widget
, WID_BS_SEMAPHORE_NORM
, WID_BS_ELECTRIC_PBS_OWAY
+ 1)) {
1771 /* Extract signal from widget number. */
1772 int type
= (widget
- WID_BS_SEMAPHORE_NORM
) % SIGTYPE_END
;
1773 int var
= SIG_SEMAPHORE
- (widget
- WID_BS_SEMAPHORE_NORM
) / SIGTYPE_END
; // SignalVariant order is reversed compared to the widgets.
1774 SpriteID sprite
= GetRailTypeInfo(_cur_railtype
)->gui_sprites
.signals
[type
][var
][this->IsWidgetLowered(widget
)];
1776 this->DrawSignalSprite(widget
, sprite
);
1780 void OnClick(Point pt
, int widget
, int click_count
) override
1783 case WID_BS_SEMAPHORE_NORM
:
1784 case WID_BS_SEMAPHORE_ENTRY
:
1785 case WID_BS_SEMAPHORE_EXIT
:
1786 case WID_BS_SEMAPHORE_COMBO
:
1787 case WID_BS_SEMAPHORE_PBS
:
1788 case WID_BS_SEMAPHORE_PBS_OWAY
:
1789 case WID_BS_ELECTRIC_NORM
:
1790 case WID_BS_ELECTRIC_ENTRY
:
1791 case WID_BS_ELECTRIC_EXIT
:
1792 case WID_BS_ELECTRIC_COMBO
:
1793 case WID_BS_ELECTRIC_PBS
:
1794 case WID_BS_ELECTRIC_PBS_OWAY
:
1795 this->RaiseWidget((_cur_signal_variant
== SIG_ELECTRIC
? WID_BS_ELECTRIC_NORM
: WID_BS_SEMAPHORE_NORM
) + _cur_signal_type
);
1797 _cur_signal_type
= (SignalType
)((uint
)((widget
- WID_BS_SEMAPHORE_NORM
) % (SIGTYPE_LAST
+ 1)));
1798 _cur_signal_variant
= widget
>= WID_BS_ELECTRIC_NORM
? SIG_ELECTRIC
: SIG_SEMAPHORE
;
1800 /* If 'remove' button of rail build toolbar is active, disable it. */
1801 if (_remove_button_clicked
) {
1802 Window
*w
= FindWindowById(WC_BUILD_TOOLBAR
, TRANSPORT_RAIL
);
1803 if (w
!= nullptr) ToggleRailButton_Remove(w
);
1808 case WID_BS_CONVERT
:
1809 _convert_signal_button
= !_convert_signal_button
;
1812 case WID_BS_DRAG_SIGNALS_DENSITY_DECREASE
:
1813 if (_settings_client
.gui
.drag_signals_density
> 1) {
1814 _settings_client
.gui
.drag_signals_density
--;
1815 SetWindowDirty(WC_GAME_OPTIONS
, WN_GAME_OPTIONS_GAME_SETTINGS
);
1819 case WID_BS_DRAG_SIGNALS_DENSITY_INCREASE
:
1820 if (_settings_client
.gui
.drag_signals_density
< 20) {
1821 _settings_client
.gui
.drag_signals_density
++;
1822 SetWindowDirty(WC_GAME_OPTIONS
, WN_GAME_OPTIONS_GAME_SETTINGS
);
1829 this->InvalidateData();
1833 * Some data on this window has become invalid.
1834 * @param data Information about the changed data.
1835 * @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.
1837 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
1839 if (!gui_scope
) return;
1840 this->LowerWidget((_cur_signal_variant
== SIG_ELECTRIC
? WID_BS_ELECTRIC_NORM
: WID_BS_SEMAPHORE_NORM
) + _cur_signal_type
);
1842 this->SetWidgetLoweredState(WID_BS_CONVERT
, _convert_signal_button
);
1844 this->SetWidgetDisabledState(WID_BS_DRAG_SIGNALS_DENSITY_DECREASE
, _settings_client
.gui
.drag_signals_density
== 1);
1845 this->SetWidgetDisabledState(WID_BS_DRAG_SIGNALS_DENSITY_INCREASE
, _settings_client
.gui
.drag_signals_density
== 20);
1849 /** Nested widget definition of the build signal window */
1850 static const NWidgetPart _nested_signal_builder_widgets
[] = {
1851 NWidget(NWID_HORIZONTAL
),
1852 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
1853 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
, WID_BS_CAPTION
), SetDataTip(STR_BUILD_SIGNAL_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1855 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
),
1856 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
1857 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_BS_SEMAPHORE_NORM_SEL
),
1858 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_BS_SEMAPHORE_NORM
), SetDataTip(STR_NULL
, STR_BUILD_SIGNAL_SEMAPHORE_NORM_TOOLTIP
), EndContainer(), SetFill(1, 1),
1860 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_BS_SEMAPHORE_ENTRY_SEL
),
1861 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_BS_SEMAPHORE_ENTRY
), SetDataTip(STR_NULL
, STR_BUILD_SIGNAL_SEMAPHORE_ENTRY_TOOLTIP
), EndContainer(), SetFill(1, 1),
1863 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_BS_SEMAPHORE_EXIT_SEL
),
1864 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_BS_SEMAPHORE_EXIT
), SetDataTip(STR_NULL
, STR_BUILD_SIGNAL_SEMAPHORE_EXIT_TOOLTIP
), EndContainer(), SetFill(1, 1),
1866 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_BS_SEMAPHORE_COMBO_SEL
),
1867 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_BS_SEMAPHORE_COMBO
), SetDataTip(STR_NULL
, STR_BUILD_SIGNAL_SEMAPHORE_COMBO_TOOLTIP
), EndContainer(), SetFill(1, 1),
1869 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_BS_SEMAPHORE_PBS
), SetDataTip(STR_NULL
, STR_BUILD_SIGNAL_SEMAPHORE_PBS_TOOLTIP
), EndContainer(), SetFill(1, 1),
1870 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),
1871 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_BS_CONVERT
), SetDataTip(SPR_IMG_SIGNAL_CONVERT
, STR_BUILD_SIGNAL_CONVERT_TOOLTIP
), SetFill(1, 1),
1873 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
1874 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_BS_ELECTRIC_NORM_SEL
),
1875 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_BS_ELECTRIC_NORM
), SetDataTip(STR_NULL
, STR_BUILD_SIGNAL_ELECTRIC_NORM_TOOLTIP
), EndContainer(), SetFill(1, 1),
1877 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_BS_ELECTRIC_ENTRY_SEL
),
1878 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_BS_ELECTRIC_ENTRY
), SetDataTip(STR_NULL
, STR_BUILD_SIGNAL_ELECTRIC_ENTRY_TOOLTIP
), EndContainer(), SetFill(1, 1),
1880 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_BS_ELECTRIC_EXIT_SEL
),
1881 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_BS_ELECTRIC_EXIT
), SetDataTip(STR_NULL
, STR_BUILD_SIGNAL_ELECTRIC_EXIT_TOOLTIP
), EndContainer(), SetFill(1, 1),
1883 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_BS_ELECTRIC_COMBO_SEL
),
1884 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_BS_ELECTRIC_COMBO
), SetDataTip(STR_NULL
, STR_BUILD_SIGNAL_ELECTRIC_COMBO_TOOLTIP
), EndContainer(), SetFill(1, 1),
1886 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_BS_ELECTRIC_PBS
), SetDataTip(STR_NULL
, STR_BUILD_SIGNAL_ELECTRIC_PBS_TOOLTIP
), EndContainer(), SetFill(1, 1),
1887 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),
1888 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
), SetDataTip(STR_NULL
, STR_BUILD_SIGNAL_DRAG_SIGNALS_DENSITY_TOOLTIP
), SetFill(1, 1),
1889 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),
1890 NWidget(NWID_HORIZONTAL
), SetPIP(2, 0, 2),
1891 NWidget(NWID_SPACER
), SetFill(1, 0),
1892 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
),
1893 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
),
1894 NWidget(NWID_SPACER
), SetFill(1, 0),
1896 NWidget(NWID_SPACER
), SetMinimalSize(0, 2), SetFill(1, 0),
1902 /** Signal selection window description */
1903 static WindowDesc
_signal_builder_desc(
1904 WDP_AUTO
, "build_signal", 0, 0,
1905 WC_BUILD_SIGNAL
, WC_BUILD_TOOLBAR
,
1907 _nested_signal_builder_widgets
, lengthof(_nested_signal_builder_widgets
)
1911 * Open the signal selection window
1913 static void ShowSignalBuilder(Window
*parent
)
1915 new BuildSignalWindow(&_signal_builder_desc
, parent
);
1918 struct BuildRailDepotWindow
: public PickerWindowBase
{
1919 BuildRailDepotWindow(WindowDesc
*desc
, Window
*parent
) : PickerWindowBase(desc
, parent
)
1921 this->InitNested(TRANSPORT_RAIL
);
1922 this->LowerWidget(_build_depot_direction
+ WID_BRAD_DEPOT_NE
);
1925 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
1927 if (!IsInsideMM(widget
, WID_BRAD_DEPOT_NE
, WID_BRAD_DEPOT_NW
+ 1)) return;
1929 size
->width
= ScaleGUITrad(64) + 2;
1930 size
->height
= ScaleGUITrad(48) + 2;
1933 void DrawWidget(const Rect
&r
, int widget
) const override
1935 if (!IsInsideMM(widget
, WID_BRAD_DEPOT_NE
, WID_BRAD_DEPOT_NW
+ 1)) return;
1937 DrawTrainDepotSprite(r
.left
+ 1 + ScaleGUITrad(31), r
.bottom
- ScaleGUITrad(31), widget
- WID_BRAD_DEPOT_NE
+ DIAGDIR_NE
, _cur_railtype
);
1940 void OnClick(Point pt
, int widget
, int click_count
) override
1943 case WID_BRAD_DEPOT_NE
:
1944 case WID_BRAD_DEPOT_SE
:
1945 case WID_BRAD_DEPOT_SW
:
1946 case WID_BRAD_DEPOT_NW
:
1947 this->RaiseWidget(_build_depot_direction
+ WID_BRAD_DEPOT_NE
);
1948 _build_depot_direction
= (DiagDirection
)(widget
- WID_BRAD_DEPOT_NE
);
1949 this->LowerWidget(_build_depot_direction
+ WID_BRAD_DEPOT_NE
);
1950 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
1957 /** Nested widget definition of the build rail depot window */
1958 static const NWidgetPart _nested_build_depot_widgets
[] = {
1959 NWidget(NWID_HORIZONTAL
),
1960 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
1961 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
), SetDataTip(STR_BUILD_DEPOT_TRAIN_ORIENTATION_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1963 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
),
1964 NWidget(NWID_SPACER
), SetMinimalSize(0, 3),
1965 NWidget(NWID_HORIZONTAL_LTR
),
1966 NWidget(NWID_SPACER
), SetMinimalSize(3, 0), SetFill(1, 0),
1967 NWidget(NWID_VERTICAL
),
1968 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BRAD_DEPOT_NW
), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP
),
1970 NWidget(NWID_SPACER
), SetMinimalSize(0, 2),
1971 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BRAD_DEPOT_SW
), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP
),
1974 NWidget(NWID_SPACER
), SetMinimalSize(2, 0),
1975 NWidget(NWID_VERTICAL
),
1976 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BRAD_DEPOT_NE
), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP
),
1978 NWidget(NWID_SPACER
), SetMinimalSize(0, 2),
1979 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BRAD_DEPOT_SE
), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP
),
1982 NWidget(NWID_SPACER
), SetMinimalSize(3, 0), SetFill(1, 0),
1984 NWidget(NWID_SPACER
), SetMinimalSize(0, 3),
1988 static WindowDesc
_build_depot_desc(
1989 WDP_AUTO
, nullptr, 0, 0,
1990 WC_BUILD_DEPOT
, WC_BUILD_TOOLBAR
,
1992 _nested_build_depot_widgets
, lengthof(_nested_build_depot_widgets
)
1995 static void ShowBuildTrainDepotPicker(Window
*parent
)
1997 new BuildRailDepotWindow(&_build_depot_desc
, parent
);
2000 struct BuildRailWaypointWindow
: PickerWindowBase
{
2001 BuildRailWaypointWindow(WindowDesc
*desc
, Window
*parent
) : PickerWindowBase(desc
, parent
)
2003 this->CreateNestedTree();
2005 NWidgetMatrix
*matrix
= this->GetWidget
<NWidgetMatrix
>(WID_BRW_WAYPOINT_MATRIX
);
2006 matrix
->SetScrollbar(this->GetScrollbar(WID_BRW_SCROLL
));
2008 this->FinishInitNested(TRANSPORT_RAIL
);
2010 matrix
->SetCount(_waypoint_count
);
2011 matrix
->SetClicked(_cur_waypoint_type
);
2014 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
2017 case WID_BRW_WAYPOINT_MATRIX
:
2018 /* Three blobs high and wide. */
2019 size
->width
+= resize
->width
* 2;
2020 size
->height
+= resize
->height
* 2;
2022 /* Resizing in X direction only at blob size, but at pixel level in Y. */
2026 case WID_BRW_WAYPOINT
:
2027 size
->width
= ScaleGUITrad(64) + 2;
2028 size
->height
= ScaleGUITrad(58) + 2;
2033 void DrawWidget(const Rect
&r
, int widget
) const override
2035 switch (GB(widget
, 0, 16)) {
2036 case WID_BRW_WAYPOINT
: {
2037 byte type
= GB(widget
, 16, 16);
2038 const StationSpec
*statspec
= StationClass::Get(STAT_CLASS_WAYP
)->GetSpec(type
);
2039 DrawWaypointSprite(r
.left
+ 1 + ScaleGUITrad(31), r
.bottom
- ScaleGUITrad(31), type
, _cur_railtype
);
2041 if (!IsStationAvailable(statspec
)) {
2042 GfxFillRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, PC_BLACK
, FILLRECT_CHECKER
);
2048 void OnClick(Point pt
, int widget
, int click_count
) override
2050 switch (GB(widget
, 0, 16)) {
2051 case WID_BRW_WAYPOINT
: {
2052 byte type
= GB(widget
, 16, 16);
2053 this->GetWidget
<NWidgetMatrix
>(WID_BRW_WAYPOINT_MATRIX
)->SetClicked(_cur_waypoint_type
);
2055 /* Check station availability callback */
2056 const StationSpec
*statspec
= StationClass::Get(STAT_CLASS_WAYP
)->GetSpec(type
);
2057 if (!IsStationAvailable(statspec
)) return;
2059 _cur_waypoint_type
= type
;
2060 this->GetWidget
<NWidgetMatrix
>(WID_BRW_WAYPOINT_MATRIX
)->SetClicked(_cur_waypoint_type
);
2061 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
2069 /** Nested widget definition for the build NewGRF rail waypoint window */
2070 static const NWidgetPart _nested_build_waypoint_widgets
[] = {
2071 NWidget(NWID_HORIZONTAL
),
2072 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
2073 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
), SetDataTip(STR_WAYPOINT_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
2074 NWidget(WWT_DEFSIZEBOX
, COLOUR_DARK_GREEN
),
2076 NWidget(NWID_HORIZONTAL
),
2077 NWidget(NWID_MATRIX
, COLOUR_DARK_GREEN
, WID_BRW_WAYPOINT_MATRIX
), SetPIP(3, 2, 3), SetScrollbar(WID_BRW_SCROLL
),
2078 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_BRW_WAYPOINT
), SetMinimalSize(66, 60), SetDataTip(0x0, STR_WAYPOINT_GRAPHICS_TOOLTIP
), SetScrollbar(WID_BRW_SCROLL
), EndContainer(),
2080 NWidget(NWID_VERTICAL
),
2081 NWidget(NWID_VSCROLLBAR
, COLOUR_DARK_GREEN
, WID_BRW_SCROLL
),
2082 NWidget(WWT_RESIZEBOX
, COLOUR_DARK_GREEN
),
2087 static WindowDesc
_build_waypoint_desc(
2088 WDP_AUTO
, "build_waypoint", 0, 0,
2089 WC_BUILD_WAYPOINT
, WC_BUILD_TOOLBAR
,
2091 _nested_build_waypoint_widgets
, lengthof(_nested_build_waypoint_widgets
)
2094 static void ShowBuildWaypointPicker(Window
*parent
)
2096 new BuildRailWaypointWindow(&_build_waypoint_desc
, parent
);
2100 * Initialize rail building GUI settings
2102 void InitializeRailGui()
2104 _build_depot_direction
= DIAGDIR_NW
;
2105 _railstation
.station_class
= StationClassID::STAT_CLASS_DFLT
;
2109 * Re-initialize rail-build toolbar after toggling support for electric trains
2110 * @param disable Boolean whether electric trains are disabled (removed from the game)
2112 void ReinitGuiAfterToggleElrail(bool disable
)
2114 extern RailType _last_built_railtype
;
2115 if (disable
&& _last_built_railtype
== RAILTYPE_ELECTRIC
) {
2116 _last_built_railtype
= _cur_railtype
= RAILTYPE_RAIL
;
2117 BuildRailToolbarWindow
*w
= dynamic_cast<BuildRailToolbarWindow
*>(FindWindowById(WC_BUILD_TOOLBAR
, TRANSPORT_RAIL
));
2118 if (w
!= nullptr) w
->ModifyRailType(_cur_railtype
);
2120 MarkWholeScreenDirty();
2123 /** Set the initial (default) railtype to use */
2124 static void SetDefaultRailGui()
2126 if (_local_company
== COMPANY_SPECTATOR
|| !Company::IsValidID(_local_company
)) return;
2128 extern RailType _last_built_railtype
;
2130 switch (_settings_client
.gui
.default_rail_type
) {
2132 /* Find the most used rail type */
2133 uint count
[RAILTYPE_END
];
2134 memset(count
, 0, sizeof(count
));
2135 for (TileIndex t
= 0; t
< MapSize(); t
++) {
2136 if (IsTileType(t
, MP_RAILWAY
) || IsLevelCrossingTile(t
) || HasStationTileRail(t
) ||
2137 (IsTileType(t
, MP_TUNNELBRIDGE
) && GetTunnelBridgeTransportType(t
) == TRANSPORT_RAIL
)) {
2138 count
[GetRailType(t
)]++;
2142 rt
= static_cast<RailType
>(std::max_element(count
+ RAILTYPE_BEGIN
, count
+ RAILTYPE_END
) - count
);
2143 if (count
[rt
] > 0) break;
2145 /* No rail, just get the first available one */
2149 /* Use first available type */
2150 std::vector
<RailType
>::const_iterator it
= std::find_if(_sorted_railtypes
.begin(), _sorted_railtypes
.end(),
2151 [](RailType r
){ return HasRailtypeAvail(_local_company
, r
); });
2152 rt
= it
!= _sorted_railtypes
.end() ? *it
: RAILTYPE_BEGIN
;
2156 /* Use last available type */
2157 std::vector
<RailType
>::const_reverse_iterator it
= std::find_if(_sorted_railtypes
.rbegin(), _sorted_railtypes
.rend(),
2158 [](RailType r
){ return HasRailtypeAvail(_local_company
, r
); });
2159 rt
= it
!= _sorted_railtypes
.rend() ? *it
: RAILTYPE_BEGIN
;
2166 _last_built_railtype
= _cur_railtype
= rt
;
2167 BuildRailToolbarWindow
*w
= dynamic_cast<BuildRailToolbarWindow
*>(FindWindowById(WC_BUILD_TOOLBAR
, TRANSPORT_RAIL
));
2168 if (w
!= nullptr) w
->ModifyRailType(_cur_railtype
);
2172 * Updates the current signal variant used in the signal GUI
2173 * to the one adequate to current year.
2174 * @param new_value needed to be called when a setting changes
2176 void ResetSignalVariant(int32 new_value
)
2178 SignalVariant new_variant
= (_cur_year
< _settings_client
.gui
.semaphore_build_before
? SIG_SEMAPHORE
: SIG_ELECTRIC
);
2180 if (new_variant
!= _cur_signal_variant
) {
2181 Window
*w
= FindWindowById(WC_BUILD_SIGNAL
, 0);
2184 w
->RaiseWidget((_cur_signal_variant
== SIG_ELECTRIC
? WID_BS_ELECTRIC_NORM
: WID_BS_SEMAPHORE_NORM
) + _cur_signal_type
);
2186 _cur_signal_variant
= new_variant
;
2191 * Resets the rail GUI - sets default railtype to build
2192 * and resets the signal GUI
2194 void InitializeRailGUI()
2196 SetDefaultRailGui();
2198 _convert_signal_button
= false;
2199 _cur_signal_type
= SIGTYPE_PBS_ONEWAY
;
2200 ResetSignalVariant();
2204 * Create a drop down list for all the rail types of the local company.
2205 * @param for_replacement Whether this list is for the replacement window.
2206 * @param all_option Whether to add an 'all types' item.
2207 * @return The populated and sorted #DropDownList.
2209 DropDownList
GetRailTypeDropDownList(bool for_replacement
, bool all_option
)
2211 RailTypes used_railtypes
;
2212 RailTypes avail_railtypes
;
2214 const Company
*c
= Company::Get(_local_company
);
2216 /* Find the used railtypes. */
2217 if (for_replacement
) {
2218 avail_railtypes
= GetCompanyRailtypes(c
->index
, false);
2219 used_railtypes
= GetRailTypes(false);
2221 avail_railtypes
= c
->avail_railtypes
;
2222 used_railtypes
= GetRailTypes(true);
2228 list
.emplace_back(new DropDownListStringItem(STR_REPLACE_ALL_RAILTYPE
, INVALID_RAILTYPE
, false));
2231 Dimension d
= { 0, 0 };
2232 /* Get largest icon size, to ensure text is aligned on each menu item. */
2233 if (!for_replacement
) {
2234 for (const auto &rt
: _sorted_railtypes
) {
2235 if (!HasBit(used_railtypes
, rt
)) continue;
2236 const RailtypeInfo
*rti
= GetRailTypeInfo(rt
);
2237 d
= maxdim(d
, GetSpriteSize(rti
->gui_sprites
.build_x_rail
));
2241 for (const auto &rt
: _sorted_railtypes
) {
2242 /* If it's not used ever, don't show it to the user. */
2243 if (!HasBit(used_railtypes
, rt
)) continue;
2245 const RailtypeInfo
*rti
= GetRailTypeInfo(rt
);
2247 StringID str
= for_replacement
? rti
->strings
.replace_text
: (rti
->max_speed
> 0 ? STR_TOOLBAR_RAILTYPE_VELOCITY
: STR_JUST_STRING
);
2248 DropDownListParamStringItem
*item
;
2249 if (for_replacement
) {
2250 item
= new DropDownListParamStringItem(str
, rt
, !HasBit(avail_railtypes
, rt
));
2252 DropDownListIconItem
*iconitem
= new DropDownListIconItem(rti
->gui_sprites
.build_x_rail
, PAL_NONE
, str
, rt
, !HasBit(avail_railtypes
, rt
));
2253 iconitem
->SetDimension(d
);
2256 item
->SetParam(0, rti
->strings
.menu_text
);
2257 item
->SetParam(1, rti
->max_speed
);
2258 list
.emplace_back(item
);
2261 if (list
.size() == 0) {
2262 /* Empty dropdowns are not allowed */
2263 list
.emplace_back(new DropDownListStringItem(STR_NONE
, INVALID_RAILTYPE
, true));