Update readme and changelog for v1.27.0
[openttd-joker.git] / src / rail_gui.cpp
blobaebc6e0e69abd79b89f1df301c1495910244c71a
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
10 /** @file rail_gui.cpp %File for dealing with rail construction user interface */
12 #include "stdafx.h"
13 #include "gui.h"
14 #include "window_gui.h"
15 #include "station_gui.h"
16 #include "terraform_gui.h"
17 #include "viewport_func.h"
18 #include "command_func.h"
19 #include "waypoint_func.h"
20 #include "newgrf_station.h"
21 #include "company_base.h"
22 #include "strings_func.h"
23 #include "window_func.h"
24 #include "date_func.h"
25 #include "sound_func.h"
26 #include "company_func.h"
27 #include "widgets/dropdown_type.h"
28 #include "tunnelbridge.h"
29 #include "tilehighlight_func.h"
30 #include "spritecache.h"
31 #include "core/geometry_func.hpp"
32 #include "hotkeys.h"
33 #include "engine_base.h"
34 #include "vehicle_func.h"
35 #include "zoom_func.h"
36 #include "rail_gui.h"
37 #include "tracerestrict.h"
38 #include "logic_signals.h"
39 #include "error.h"
41 #include "station_map.h"
42 #include "tunnelbridge_map.h"
44 #include "widgets/rail_widget.h"
46 #include "safeguards.h"
49 static RailType _cur_railtype; ///< Rail type of the current build-rail toolbar.
50 static bool _remove_button_clicked; ///< Flag whether 'remove' toggle-button is currently enabled
51 static DiagDirection _build_depot_direction; ///< Currently selected depot direction
52 static byte _waypoint_count = 1; ///< Number of waypoint types
53 static byte _cur_waypoint_type; ///< Currently selected waypoint type
54 static bool _convert_signal_button; ///< convert signal button in the signal GUI pressed
55 static bool _trace_restrict_button; ///< trace restrict button in the signal GUI pressed
56 static SignalVariant _cur_signal_variant; ///< set the signal variant (for signal GUI)
57 static SignalType _cur_signal_type; ///< set the signal type (for signal GUI)
59 extern TileIndex _rail_track_endtile;
61 /* Map the setting: default_signal_type to the corresponding signal type */
62 static const SignalType _default_signal_type[] = {SIGTYPE_NORMAL, SIGTYPE_PBS, SIGTYPE_PBS_ONEWAY};
64 static const int HOTKEY_POLYRAIL = 0x1000;
65 static const int HOTKEY_NEW_POLYRAIL = 0x1001;
67 struct RailStationGUISettings {
68 Axis orientation; ///< Currently selected rail station orientation
70 bool newstations; ///< Are custom station definitions available?
71 StationClassID station_class; ///< Currently selected custom station class (if newstations is \c true )
72 byte station_type; ///< %Station type within the currently selected custom station class (if newstations is \c true )
73 byte station_count; ///< Number of custom stations (if newstations is \c true )
75 static RailStationGUISettings _railstation; ///< Settings of the station builder GUI
78 static void HandleStationPlacement(TileIndex start, TileIndex end);
79 static void ShowBuildTrainDepotPicker(Window *parent);
80 static void ShowBuildWaypointPicker(Window *parent);
81 static void ShowStationBuilder(Window *parent);
82 static void ShowSignalBuilder(Window *parent);
84 /**
85 * Check whether a station type can be build.
86 * @return true if building is allowed.
88 static bool IsStationAvailable(const StationSpec *statspec)
90 if (statspec == nullptr || !HasBit(statspec->callback_mask, CBM_STATION_AVAIL)) return true;
92 uint16 cb_res = GetStationCallback(CBID_STATION_AVAILABILITY, 0, 0, statspec, nullptr, INVALID_TILE);
93 if (cb_res == CALLBACK_FAILED) return true;
95 return Convert8bitBooleanCallback(statspec->grf_prop.grffile, CBID_STATION_AVAILABILITY, cb_res);
98 void CcPlaySound_SPLAT_RAIL(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
100 if (result.Succeeded() && _settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_RAIL, tile);
103 static CommandContainer GenericPlaceRailCmd(TileIndex tile, Track track)
105 CommandContainer ret = {
106 tile, // tile
107 (uint32)_cur_railtype, // p1
108 (uint32)track, // p2
109 _remove_button_clicked ?
110 CMD_REMOVE_SINGLE_RAIL | CMD_MSG(STR_ERROR_CAN_T_REMOVE_RAILROAD_TRACK) :
111 CMD_BUILD_SINGLE_RAIL | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_TRACK), // cmd
112 CcPlaySound_SPLAT_RAIL, // callback
113 0, // binary_length
114 "" // text
117 return ret;
121 * Try to add an additional rail-track at the entrance of a depot
122 * @param tile Tile to use for adding the rail-track
123 * @param dir Direction to check for already present tracks
124 * @param track Track to add
125 * @see CcRailDepot()
127 static void PlaceExtraDepotRail(TileIndex tile, DiagDirection dir, Track track)
129 if (GetRailTileType(tile) != RAIL_TILE_NORMAL) return;
130 if ((GetTrackBits(tile) & DiagdirReachesTracks(dir)) == 0) return;
132 DoCommandP(tile, _cur_railtype, track, CMD_BUILD_SINGLE_RAIL);
135 /** Additional pieces of track to add at the entrance of a depot. */
136 static const Track _place_depot_extra_track[12] = {
137 TRACK_LEFT, TRACK_UPPER, TRACK_UPPER, TRACK_RIGHT, // First additional track for directions 0..3
138 TRACK_X, TRACK_Y, TRACK_X, TRACK_Y, // Second additional track
139 TRACK_LOWER, TRACK_LEFT, TRACK_RIGHT, TRACK_LOWER, // Third additional track
142 /** Direction to check for existing track pieces. */
143 static const DiagDirection _place_depot_extra_dir[12] = {
144 DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, DIAGDIR_SW,
145 DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE, DIAGDIR_SE,
146 DIAGDIR_NW, DIAGDIR_NE, DIAGDIR_NW, DIAGDIR_NE,
149 void CcRailDepot(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
151 if (result.Failed()) return;
153 DiagDirection dir = (DiagDirection)p2;
155 if (_settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_RAIL, tile);
156 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
158 tile += TileOffsByDiagDir(dir);
160 if (IsTileType(tile, MP_RAILWAY)) {
161 PlaceExtraDepotRail(tile, _place_depot_extra_dir[dir], _place_depot_extra_track[dir]);
162 PlaceExtraDepotRail(tile, _place_depot_extra_dir[dir + 4], _place_depot_extra_track[dir + 4]);
163 PlaceExtraDepotRail(tile, _place_depot_extra_dir[dir + 8], _place_depot_extra_track[dir + 8]);
168 * Place a rail waypoint.
169 * @param tile Position to start dragging a waypoint.
171 static void PlaceRail_Waypoint(TileIndex tile)
173 if (_remove_button_clicked) {
174 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_REMOVE_STATION);
175 return;
178 Axis axis = GetAxisForNewWaypoint(tile);
179 if (IsValidAxis(axis)) {
180 /* Valid tile for waypoints */
181 VpStartPlaceSizing(tile, axis == AXIS_X ? VPM_X_LIMITED : VPM_Y_LIMITED, DDSP_BUILD_STATION);
182 VpSetPlaceSizingLimit(_settings_game.station.station_spread);
183 } else {
184 /* Tile where we can't build rail waypoints. This is always going to fail,
185 * but provides the user with a proper error message. */
186 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));
190 void CcStation(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
192 if (result.Failed()) return;
194 if (_settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_RAIL, tile);
195 /* Only close the station builder window if the default station and non persistent building is chosen. */
196 if (_railstation.station_class == STAT_CLASS_DFLT && _railstation.station_type == 0 && !_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
200 * Place a rail station.
201 * @param tile Position to place or start dragging a station.
203 static void PlaceRail_Station(TileIndex tile)
205 if (_remove_button_clicked) {
206 VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED, DDSP_REMOVE_STATION);
207 VpSetPlaceSizingLimit(-1);
208 } else if (_settings_client.gui.station_dragdrop) {
209 VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED, DDSP_BUILD_STATION);
210 VpSetPlaceSizingLimit(_settings_game.station.station_spread);
211 } else {
212 uint32 p1 = _cur_railtype | _railstation.orientation << 5 | _settings_client.gui.station_numtracks << 8 | _settings_client.gui.station_platlength << 16 | _ctrl_pressed << 24;
213 uint32 p2 = _railstation.station_class | _railstation.station_type << 8 | INVALID_STATION << 16;
215 int w = _settings_client.gui.station_numtracks;
216 int h = _settings_client.gui.station_platlength;
217 if (!_railstation.orientation) Swap(w, h);
219 CommandContainer cmdcont = { tile, p1, p2, CMD_BUILD_RAIL_STATION | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION), CcStation, 0, "" };
220 ShowSelectStationIfNeeded(cmdcont, TileArea(tile, w, h));
225 * Build a new signal or edit/remove a present signal, use CmdBuildSingleSignal() or CmdRemoveSingleSignal() in rail_cmd.cpp
227 * @param tile The tile where the signal will build or edit
229 static void GenericPlaceSignals(TileIndex tile)
231 TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_RAIL, 0));
233 if (trackbits & TRACK_BIT_VERT) { // N-S direction
234 trackbits = (_tile_fract_coords.x <= _tile_fract_coords.y) ? TRACK_BIT_RIGHT : TRACK_BIT_LEFT;
237 if (trackbits & TRACK_BIT_HORZ) { // E-W direction
238 trackbits = (_tile_fract_coords.x + _tile_fract_coords.y <= 15) ? TRACK_BIT_UPPER : TRACK_BIT_LOWER;
241 Track track = FindFirstTrack(trackbits);
243 if (_remove_button_clicked) {
244 DoCommandP(tile, track, 0, CMD_REMOVE_SIGNALS | CMD_MSG(STR_ERROR_CAN_T_REMOVE_SIGNALS_FROM), CcPlaySound_SPLAT_RAIL);
245 } else if (_trace_restrict_button) {
246 if (IsPlainRailTile(tile) && HasTrack(tile, track) && HasSignalOnTrack(tile, track)) {
247 ShowTraceRestrictProgramWindow(tile, track);
249 } else {
250 const Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
252 /* Map the setting cycle_signal_types to the lower and upper allowed signal type. */
253 static const uint cycle_bounds[] = {SIGTYPE_NORMAL | (SIGTYPE_LAST_NOPBS << 3), SIGTYPE_PBS | (SIGTYPE_LAST << 3), SIGTYPE_NORMAL | (SIGTYPE_LAST << 3)};
255 /* various bitstuffed elements for CmdBuildSingleSignal() */
256 uint32 p1 = track;
258 if (w != nullptr) {
259 /* signal GUI is used */
260 SB(p1, 3, 1, _ctrl_pressed);
261 SB(p1, 4, 1, _cur_signal_variant);
262 SB(p1, 5, 3, _cur_signal_type);
263 SB(p1, 8, 1, _convert_signal_button);
264 SB(p1, 9, 6, cycle_bounds[_settings_client.gui.cycle_signal_types]);
265 } else {
266 SB(p1, 3, 1, _ctrl_pressed);
267 SB(p1, 4, 1, (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC));
268 SB(p1, 5, 3, _default_signal_type[_settings_client.gui.default_signal_type]);
269 SB(p1, 8, 1, 0);
270 SB(p1, 9, 6, cycle_bounds[_settings_client.gui.cycle_signal_types]);
273 DoCommandP(tile, p1, 0, CMD_BUILD_SIGNALS |
274 CMD_MSG((w != nullptr && _convert_signal_button) ? STR_ERROR_SIGNAL_CAN_T_CONVERT_SIGNALS_HERE : STR_ERROR_CAN_T_BUILD_SIGNALS_HERE),
275 CcPlaySound_SPLAT_RAIL);
280 * Start placing a rail bridge.
281 * @param tile Position of the first tile of the bridge.
282 * @param w Rail toolbar window.
284 static void PlaceRail_Bridge(TileIndex tile, Window *w)
286 if (IsBridgeTile(tile)) {
287 TileIndex other_tile = GetOtherTunnelBridgeEnd(tile);
288 Point pt = {0, 0};
289 w->OnPlaceMouseUp(VPM_X_OR_Y, DDSP_BUILD_BRIDGE, pt, other_tile, tile);
290 } else {
291 VpStartPlaceSizing(tile, VPM_X_OR_Y, DDSP_BUILD_BRIDGE);
295 /** Command callback for building a tunnel */
296 void CcBuildRailTunnel(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
298 if (result.Succeeded()) {
299 if (_settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_RAIL, tile);
300 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
301 StoreRailPlacementEndpoints(tile, _build_tunnel_endtile, TileX(tile) == TileX(_build_tunnel_endtile) ? TRACK_Y : TRACK_X, false);
302 } else {
303 SetRedErrorSquare(_build_tunnel_endtile);
308 * Toggles state of the Remove button of Build rail toolbar
309 * @param w window the button belongs to
311 static void ToggleRailButton_Remove(Window *w)
313 DeleteWindowById(WC_SELECT_STATION, 0);
314 w->ToggleWidgetLoweredState(WID_RAT_REMOVE);
315 w->SetWidgetDirty(WID_RAT_REMOVE);
316 _remove_button_clicked = w->IsWidgetLowered(WID_RAT_REMOVE);
317 SetSelectionRed(_remove_button_clicked);
321 * Updates the Remove button because of Ctrl state change
322 * @param w window the button belongs to
323 * @return true iff the remove button was changed
325 static bool RailToolbar_CtrlChanged(Window *w)
327 if (w->IsWidgetDisabled(WID_RAT_REMOVE)) return false;
329 /* allow ctrl to switch remove mode only for these widgets */
330 for (uint i = WID_RAT_BUILD_NS; i <= WID_RAT_BUILD_STATION; i++) {
331 if ((i <= WID_RAT_POLYRAIL || i >= WID_RAT_BUILD_WAYPOINT) && w->IsWidgetLowered(i)) {
332 ToggleRailButton_Remove(w);
333 return true;
337 return false;
342 * The "remove"-button click proc of the build-rail toolbar.
343 * @param w Build-rail toolbar window
344 * @see BuildRailToolbarWindow::OnClick()
346 static void BuildRailClick_Remove(Window *w)
348 if (w->IsWidgetDisabled(WID_RAT_REMOVE)) return;
349 ToggleRailButton_Remove(w);
350 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
352 /* handle station builder */
353 if (w->IsWidgetLowered(WID_RAT_BUILD_STATION)) {
354 if (_remove_button_clicked) {
355 /* starting drag & drop remove */
356 if (!_settings_client.gui.station_dragdrop) {
357 SetTileSelectSize(1, 1);
358 } else {
359 VpSetPlaceSizingLimit(-1);
361 } else {
362 /* starting station build mode */
363 if (!_settings_client.gui.station_dragdrop) {
364 int x = _settings_client.gui.station_numtracks;
365 int y = _settings_client.gui.station_platlength;
366 if (_railstation.orientation == 0) Swap(x, y);
367 SetTileSelectSize(x, y);
368 } else {
369 VpSetPlaceSizingLimit(_settings_game.station.station_spread);
375 static CommandContainer DoRailroadTrackCmd(TileIndex start_tile, TileIndex end_tile, Track track)
377 CommandContainer ret = {
378 start_tile, // tile
379 end_tile, // p1
380 (uint32)(_cur_railtype | (track << 5)), // p2
381 (uint32)(_remove_button_clicked ?
382 CMD_REMOVE_RAILROAD_TRACK | CMD_MSG(STR_ERROR_CAN_T_REMOVE_RAILROAD_TRACK) :
383 CMD_BUILD_RAILROAD_TRACK | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_TRACK)), // cmd
384 CcPlaySound_SPLAT_RAIL, // callback
385 0u, // binary_length
386 "" // text
389 return ret;
392 static void HandleAutodirPlacement()
394 Track track = (Track)(_thd.drawstyle & HT_DIR_MASK); // 0..5
395 TileIndex start_tile = TileVirtXY(_thd.selstart.x, _thd.selstart.y);
396 TileIndex end_tile = TileVirtXY(_thd.selend.x, _thd.selend.y);
398 CommandContainer cmd = (_thd.drawstyle & HT_RAIL) ?
399 GenericPlaceRailCmd(end_tile, track) : // one tile case
400 DoRailroadTrackCmd(start_tile, end_tile, track); // multitile selection
402 /* When overbuilding existing tracks in polyline mode we just want to move the
403 * snap point without altering the user with the "already built" error. Don't
404 * execute the command right away, firstly check if tracks are being overbuilt. */
405 if (!(_thd.place_mode & HT_POLY) || _shift_pressed ||
406 DoCommand(&cmd, DC_AUTO | DC_NO_WATER).GetErrorMessage() != STR_ERROR_ALREADY_BUILT) {
407 /* place tracks */
408 if (!DoCommandP(&cmd)) return;
411 /* save new snap points for the polyline tool */
412 if (!_shift_pressed && _rail_track_endtile != INVALID_TILE) {
413 StoreRailPlacementEndpoints(start_tile, _rail_track_endtile, track, true);
418 * Build new signals or remove signals or (if only one tile marked) edit a signal.
420 * If one tile marked abort and use GenericPlaceSignals()
421 * else use CmdBuildSingleSignal() or CmdRemoveSingleSignal() in rail_cmd.cpp to build many signals
423 static void HandleAutoSignalPlacement()
425 uint32 p2 = GB(_thd.drawstyle, 0, 3); // 0..5
427 if ((_thd.drawstyle & HT_DRAG_MASK) == HT_RECT) { // one tile case
428 GenericPlaceSignals(TileVirtXY(_thd.selend.x, _thd.selend.y));
429 return;
432 const Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
434 if (w != nullptr) {
435 /* signal GUI is used */
436 SB(p2, 3, 1, 0);
437 SB(p2, 4, 1, _cur_signal_variant);
438 SB(p2, 6, 1, _ctrl_pressed);
439 SB(p2, 7, 3, _cur_signal_type);
440 SB(p2, 24, 8, _settings_client.gui.drag_signals_density);
441 SB(p2, 10, 1, !_settings_client.gui.drag_signals_fixed_distance);
442 } else {
443 SB(p2, 3, 1, 0);
444 SB(p2, 4, 1, (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC));
445 SB(p2, 6, 1, _ctrl_pressed);
446 SB(p2, 7, 3, _default_signal_type[_settings_client.gui.default_signal_type]);
447 SB(p2, 24, 8, _settings_client.gui.drag_signals_density);
448 SB(p2, 10, 1, !_settings_client.gui.drag_signals_fixed_distance);
451 /* _settings_client.gui.drag_signals_density is given as a parameter such that each user
452 * in a network game can specify his/her own signal density */
453 DoCommandP(TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y), p2,
454 _remove_button_clicked ?
455 CMD_REMOVE_SIGNAL_TRACK | CMD_MSG(STR_ERROR_CAN_T_REMOVE_SIGNALS_FROM) :
456 CMD_BUILD_SIGNAL_TRACK | CMD_MSG(STR_ERROR_CAN_T_BUILD_SIGNALS_HERE),
457 CcPlaySound_SPLAT_RAIL);
461 /** Rail toolbar management class. */
462 struct BuildRailToolbarWindow : Window {
463 RailType railtype; ///< Rail type to build.
464 int last_user_action; ///< Last started user action.
466 BuildRailToolbarWindow(WindowDesc *desc, RailType railtype) : Window(desc)
468 this->InitNested(TRANSPORT_RAIL);
469 this->SetupRailToolbar(railtype);
470 this->DisableWidget(WID_RAT_REMOVE);
471 this->last_user_action = WIDGET_LIST_END;
473 if (_settings_client.gui.link_terraform_toolbar) ShowTerraformToolbar(this);
476 ~BuildRailToolbarWindow()
478 if (_settings_client.gui.link_terraform_toolbar) DeleteWindowById(WC_SCEN_LAND_GEN, 0, false);
482 * Some data on this window has become invalid.
483 * @param data Information about the changed data.
484 * @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.
486 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
488 if (!gui_scope) return;
490 if (!CanBuildVehicleInfrastructure(VEH_TRAIN)) delete this;
494 * Configures the rail toolbar for railtype given
495 * @param railtype the railtype to display
497 void SetupRailToolbar(RailType railtype)
499 this->railtype = railtype;
500 const RailtypeInfo *rti = GetRailTypeInfo(railtype);
502 assert(railtype < RAILTYPE_END);
503 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_NS)->widget_data = rti->gui_sprites.build_ns_rail;
504 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_X)->widget_data = rti->gui_sprites.build_x_rail;
505 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_EW)->widget_data = rti->gui_sprites.build_ew_rail;
506 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_Y)->widget_data = rti->gui_sprites.build_y_rail;
507 this->GetWidget<NWidgetCore>(WID_RAT_AUTORAIL)->widget_data = rti->gui_sprites.auto_rail;
508 this->GetWidget<NWidgetCore>(WID_RAT_POLYRAIL)->widget_data = rti->gui_sprites.auto_rail;
509 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_DEPOT)->widget_data = rti->gui_sprites.build_depot;
510 this->GetWidget<NWidgetCore>(WID_RAT_CONVERT_RAIL)->widget_data = rti->gui_sprites.convert_rail;
511 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_TUNNEL)->widget_data = rti->gui_sprites.build_tunnel;
515 * Switch to another rail type.
516 * @param railtype New rail type.
518 void ModifyRailType(RailType railtype)
520 this->SetupRailToolbar(railtype);
521 this->ReInit();
524 void UpdateRemoveWidgetStatus(int clicked_widget)
526 switch (clicked_widget) {
527 case WID_RAT_REMOVE:
528 /* If it is the removal button that has been clicked, do nothing,
529 * as it is up to the other buttons to drive removal status */
530 return;
532 case WID_RAT_BUILD_NS:
533 case WID_RAT_BUILD_X:
534 case WID_RAT_BUILD_EW:
535 case WID_RAT_BUILD_Y:
536 case WID_RAT_AUTORAIL:
537 case WID_RAT_POLYRAIL:
538 case WID_RAT_BUILD_WAYPOINT:
539 case WID_RAT_BUILD_STATION:
540 case WID_RAT_BUILD_SIGNALS:
541 /* Removal button is enabled only if the rail/signal/waypoint/station
542 * button is still lowered. Once raised, it has to be disabled */
543 this->SetWidgetDisabledState(WID_RAT_REMOVE, !this->IsWidgetLowered(clicked_widget));
544 break;
546 default:
547 /* When any other buttons than rail/signal/waypoint/station, raise and
548 * disable the removal button */
549 this->DisableWidget(WID_RAT_REMOVE);
550 this->RaiseWidget(WID_RAT_REMOVE);
551 break;
555 virtual void SetStringParameters(int widget) const
557 if (widget == WID_RAT_CAPTION) {
558 const RailtypeInfo *rti = GetRailTypeInfo(this->railtype);
559 if (rti->max_speed > 0) {
560 SetDParam(0, STR_TOOLBAR_RAILTYPE_VELOCITY);
561 SetDParam(1, rti->strings.toolbar_caption);
562 SetDParam(2, rti->max_speed);
563 } else {
564 SetDParam(0, rti->strings.toolbar_caption);
569 virtual void DrawWidget(const Rect &r, int widget) const
571 if (widget == WID_RAT_POLYRAIL) {
572 Dimension d = GetSpriteSize(SPR_BLOT);
573 uint offset = this->IsWidgetLowered(WID_RAT_POLYRAIL) ? 1 : 0;
574 DrawSprite(SPR_BLOT, PALETTE_TO_GREY, (r.left + r.right - d.width) / 2 + offset, (r.top + r.bottom - d.height) / 2 + offset);
578 virtual void OnClick(Point pt, int widget, int click_count)
580 if (widget < WID_RAT_BUILD_NS) return;
582 _remove_button_clicked = false;
583 switch (widget) {
584 case WID_RAT_BUILD_NS:
585 HandlePlacePushButton(this, WID_RAT_BUILD_NS, GetRailTypeInfo(_cur_railtype)->cursor.rail_ns, HT_LINE | HT_DIR_VL);
586 this->last_user_action = widget;
587 break;
589 case WID_RAT_BUILD_X:
590 HandlePlacePushButton(this, WID_RAT_BUILD_X, GetRailTypeInfo(_cur_railtype)->cursor.rail_swne, HT_LINE | HT_DIR_X);
591 this->last_user_action = widget;
592 break;
594 case WID_RAT_BUILD_EW:
595 HandlePlacePushButton(this, WID_RAT_BUILD_EW, GetRailTypeInfo(_cur_railtype)->cursor.rail_ew, HT_LINE | HT_DIR_HL);
596 this->last_user_action = widget;
597 break;
599 case WID_RAT_BUILD_Y:
600 HandlePlacePushButton(this, WID_RAT_BUILD_Y, GetRailTypeInfo(_cur_railtype)->cursor.rail_nwse, HT_LINE | HT_DIR_Y);
601 this->last_user_action = widget;
602 break;
604 case WID_RAT_AUTORAIL:
605 HandlePlacePushButton(this, WID_RAT_AUTORAIL, GetRailTypeInfo(_cur_railtype)->cursor.autorail, HT_RAIL);
606 this->last_user_action = widget;
607 break;
609 case WID_RAT_POLYRAIL: {
610 bool was_snap = CurrentlySnappingRailPlacement();
611 bool was_open = this->IsWidgetLowered(WID_RAT_POLYRAIL);
612 bool do_snap;
613 bool do_open;
614 /* "polyrail" hotkey - activate polyline tool in snapping mode, close the tool if snapping mode is already active
615 * "new_polyrail" hotkey - activate polyline tool in non-snapping (new line) mode, close the tool if non-snapping mode is already active
616 * button ctrl-clicking - switch between snapping and non-snapping modes, open the tool in non-snapping mode if it is closed
617 * button clicking - open the tool in non-snapping mode, close the tool if it is opened */
618 if (this->last_user_action == HOTKEY_POLYRAIL) {
619 do_snap = true;
620 do_open = !was_open || !was_snap;
621 } else if (this->last_user_action == HOTKEY_NEW_POLYRAIL) {
622 do_snap = false;
623 do_open = !was_open || was_snap;
624 } else if (_ctrl_pressed) {
625 do_snap = !was_open || !was_snap;
626 do_open = true;
627 } else {
628 do_snap = false;
629 do_open = !was_open;
631 /* close the tool explicitly so it can be re-opened in different snapping mode */
632 if (was_open) ResetObjectToPlace();
633 /* open the tool in desired mode */
634 if (do_open && HandlePlacePushButton(this, WID_RAT_POLYRAIL, GetRailTypeInfo(railtype)->cursor.autorail, do_snap ? (HT_RAIL | HT_POLY) : (HT_RAIL | HT_NEW_POLY))) {
635 /* if we are re-opening the tool but we couldn't switch the snapping
636 * then close the tool instead of appearing to be doing nothing */
637 if (was_open && do_snap != CurrentlySnappingRailPlacement()) ResetObjectToPlace();
639 this->last_user_action = WID_RAT_POLYRAIL;
640 break;
643 case WID_RAT_DEMOLISH:
644 HandlePlacePushButton(this, WID_RAT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
645 this->last_user_action = widget;
646 break;
648 case WID_RAT_BUILD_DEPOT:
649 if (HandlePlacePushButton(this, WID_RAT_BUILD_DEPOT, GetRailTypeInfo(_cur_railtype)->cursor.depot, HT_RECT)) {
650 ShowBuildTrainDepotPicker(this);
651 this->last_user_action = widget;
653 break;
655 case WID_RAT_BUILD_WAYPOINT:
656 this->last_user_action = widget;
657 _waypoint_count = StationClass::Get(STAT_CLASS_WAYP)->GetSpecCount();
658 if (HandlePlacePushButton(this, WID_RAT_BUILD_WAYPOINT, SPR_CURSOR_WAYPOINT, HT_RECT) && _waypoint_count > 1) {
659 ShowBuildWaypointPicker(this);
661 break;
663 case WID_RAT_BUILD_STATION:
664 if (HandlePlacePushButton(this, WID_RAT_BUILD_STATION, SPR_CURSOR_RAIL_STATION, HT_RECT)) {
665 ShowStationBuilder(this);
666 this->last_user_action = widget;
668 break;
670 case WID_RAT_BUILD_SIGNALS: {
671 this->last_user_action = widget;
672 bool started = HandlePlacePushButton(this, WID_RAT_BUILD_SIGNALS, ANIMCURSOR_BUILDSIGNALS, HT_RECT);
673 if (started && _settings_client.gui.enable_signal_gui != _ctrl_pressed) {
674 ShowSignalBuilder(this);
676 break;
679 case WID_RAT_PROGRAM_SIGNALS:
680 HandlePlacePushButton(this, WID_RAT_PROGRAM_SIGNALS, SPR_CURSOR_MOUSE, HT_RECT);
681 this->last_user_action = widget;
682 break;
684 case WID_RAT_BUILD_BRIDGE:
685 HandlePlacePushButton(this, WID_RAT_BUILD_BRIDGE, SPR_CURSOR_BRIDGE, HT_RECT);
686 this->last_user_action = widget;
687 break;
689 case WID_RAT_BUILD_TUNNEL:
690 HandlePlacePushButton(this, WID_RAT_BUILD_TUNNEL, GetRailTypeInfo(_cur_railtype)->cursor.tunnel, HT_SPECIAL | HT_TUNNEL);
691 this->last_user_action = widget;
692 break;
694 case WID_RAT_REMOVE:
695 BuildRailClick_Remove(this);
696 break;
698 case WID_RAT_CONVERT_RAIL:
699 HandlePlacePushButton(this, WID_RAT_CONVERT_RAIL, GetRailTypeInfo(_cur_railtype)->cursor.convert, HT_RECT | HT_DIAGONAL);
700 this->last_user_action = widget;
701 break;
703 default: NOT_REACHED();
705 this->UpdateRemoveWidgetStatus(widget);
706 if (_ctrl_pressed) RailToolbar_CtrlChanged(this);
709 virtual EventState OnHotkey(int hotkey)
711 MarkTileDirtyByTile(TileVirtXY(_thd.pos.x, _thd.pos.y)); // redraw tile selection
713 if (hotkey == HOTKEY_POLYRAIL || hotkey == HOTKEY_NEW_POLYRAIL) {
714 /* Indicate to the OnClick that the action comes from a hotkey rather
715 * then from a click and that the CTRL state should be ignored. */
716 this->last_user_action = hotkey;
717 hotkey = WID_RAT_POLYRAIL;
720 return this->Window::OnHotkey(hotkey);
723 virtual void OnPlaceObject(Point pt, TileIndex tile)
725 switch (this->last_user_action) {
726 case WID_RAT_BUILD_NS:
727 VpStartPlaceSizing(tile, VPM_FIX_VERTICAL | VPM_RAILDIRS, DDSP_PLACE_RAIL);
728 break;
730 case WID_RAT_BUILD_X:
731 VpStartPlaceSizing(tile, VPM_FIX_Y | VPM_RAILDIRS, DDSP_PLACE_RAIL);
732 break;
734 case WID_RAT_BUILD_EW:
735 VpStartPlaceSizing(tile, VPM_FIX_HORIZONTAL | VPM_RAILDIRS, DDSP_PLACE_RAIL);
736 break;
738 case WID_RAT_BUILD_Y:
739 VpStartPlaceSizing(tile, VPM_FIX_X | VPM_RAILDIRS, DDSP_PLACE_RAIL);
740 break;
742 case WID_RAT_AUTORAIL:
743 case WID_RAT_POLYRAIL:
744 VpStartPlaceSizing(tile, VPM_RAILDIRS, DDSP_PLACE_RAIL);
745 break;
747 case WID_RAT_DEMOLISH:
748 PlaceProc_DemolishArea(tile);
749 break;
751 case WID_RAT_BUILD_DEPOT:
752 DoCommandP(tile, _cur_railtype, _build_depot_direction,
753 CMD_BUILD_TRAIN_DEPOT | CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_DEPOT),
754 CcRailDepot);
755 break;
757 case WID_RAT_BUILD_WAYPOINT:
758 PlaceRail_Waypoint(tile);
759 break;
761 case WID_RAT_BUILD_STATION:
762 PlaceRail_Station(tile);
763 break;
765 case WID_RAT_BUILD_SIGNALS:
766 VpStartPlaceSizing(tile, VPM_SIGNALDIRS, DDSP_BUILD_SIGNALS);
767 break;
769 case WID_RAT_PROGRAM_SIGNALS:
770 if (IsPlainRailTile(tile)) {
771 Track track = SignalTrackFromTile(tile);
773 if (HasSignalOnTrack(tile, track) && IsLogicSignal(tile, track)) {
774 ShowSignalProgramWindow(FindSignalProgram(tile, track));
775 ResetObjectToPlace();
776 return;
779 ShowErrorMessage(STR_ERROR_PROGRAM_NOT_LOGIC_SIGNAL, INVALID_STRING_ID, WL_ERROR);
780 break;
782 case WID_RAT_BUILD_BRIDGE:
783 PlaceRail_Bridge(tile, this);
784 break;
786 case WID_RAT_BUILD_TUNNEL:
787 DoCommandP(tile, _cur_railtype | (TRANSPORT_RAIL << 8), 0, CMD_BUILD_TUNNEL | CMD_MSG(STR_ERROR_CAN_T_BUILD_TUNNEL_HERE), CcBuildRailTunnel);
788 break;
790 case WID_RAT_CONVERT_RAIL:
791 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CONVERT_RAIL);
792 break;
794 default: NOT_REACHED();
798 virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
800 /* no dragging if you have pressed the convert button */
801 if (FindWindowById(WC_BUILD_SIGNAL, 0) != nullptr && _convert_signal_button && this->IsWidgetLowered(WID_RAT_BUILD_SIGNALS)) return;
803 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
806 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
808 if (pt.x != -1) {
809 switch (select_proc) {
810 default: NOT_REACHED();
811 case DDSP_BUILD_BRIDGE:
812 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
813 ShowBuildBridgeWindow(start_tile, end_tile, TRANSPORT_RAIL, _cur_railtype);
814 break;
816 case DDSP_PLACE_RAIL:
817 HandleAutodirPlacement();
818 break;
820 case DDSP_BUILD_SIGNALS:
821 HandleAutoSignalPlacement();
822 break;
824 case DDSP_DEMOLISH_AREA:
825 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
826 break;
828 case DDSP_CONVERT_RAIL:
829 DoCommandP(end_tile, start_tile, _cur_railtype | (_ctrl_pressed ? (1 << 5) : 0), CMD_CONVERT_RAIL | CMD_MSG(STR_ERROR_CAN_T_CONVERT_RAIL), CcPlaySound_SPLAT_RAIL);
830 break;
832 case DDSP_REMOVE_STATION:
833 case DDSP_BUILD_STATION:
834 if (this->IsWidgetLowered(WID_RAT_BUILD_STATION)) {
835 /* Station */
836 if (_remove_button_clicked) {
837 DoCommandP(end_tile, start_tile, _ctrl_pressed ? 0 : 1, CMD_REMOVE_FROM_RAIL_STATION | CMD_MSG(STR_ERROR_CAN_T_REMOVE_PART_OF_STATION), CcPlaySound_SPLAT_RAIL);
838 } else {
839 HandleStationPlacement(start_tile, end_tile);
841 } else {
842 /* Waypoint */
843 if (_remove_button_clicked) {
844 DoCommandP(end_tile, start_tile, _ctrl_pressed ? 0 : 1, CMD_REMOVE_FROM_RAIL_WAYPOINT | CMD_MSG(STR_ERROR_CAN_T_REMOVE_TRAIN_WAYPOINT), CcPlaySound_SPLAT_RAIL);
845 } else {
846 TileArea ta(start_tile, end_tile);
847 uint32 p1 = _cur_railtype | (select_method == VPM_X_LIMITED ? AXIS_X : AXIS_Y) << 4 | ta.w << 8 | ta.h << 16 | _ctrl_pressed << 24;
848 uint32 p2 = STAT_CLASS_WAYP | _cur_waypoint_type << 8 | INVALID_STATION << 16;
850 CommandContainer cmdcont = { ta.tile, p1, p2, CMD_BUILD_RAIL_WAYPOINT | CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_WAYPOINT), CcPlaySound_SPLAT_RAIL, 0, "" };
851 ShowSelectWaypointIfNeeded(cmdcont, ta);
854 break;
859 virtual void OnPlaceObjectAbort()
861 this->RaiseButtons();
862 this->DisableWidget(WID_RAT_REMOVE);
863 this->SetWidgetDirty(WID_RAT_REMOVE);
865 DeleteWindowById(WC_BUILD_SIGNAL, TRANSPORT_RAIL);
866 DeleteWindowById(WC_BUILD_STATION, TRANSPORT_RAIL);
867 DeleteWindowById(WC_BUILD_DEPOT, TRANSPORT_RAIL);
868 DeleteWindowById(WC_BUILD_WAYPOINT, TRANSPORT_RAIL);
869 DeleteWindowById(WC_SELECT_STATION, 0);
870 DeleteWindowByClass(WC_BUILD_BRIDGE);
873 virtual void OnPlacePresize(Point pt, TileIndex tile)
875 DoCommand(tile, _cur_railtype | (TRANSPORT_RAIL << 8), 0, DC_AUTO, CMD_BUILD_TUNNEL);
876 VpSetPresizeRange(tile, _build_tunnel_endtile == 0 ? tile : _build_tunnel_endtile);
879 virtual EventState OnCTRLStateChange()
881 /* do not toggle Remove button by Ctrl when placing station */
882 if (!this->IsWidgetLowered(WID_RAT_BUILD_STATION) && !this->IsWidgetLowered(WID_RAT_BUILD_WAYPOINT) && RailToolbar_CtrlChanged(this)) return ES_HANDLED;
883 return ES_NOT_HANDLED;
886 static HotkeyList hotkeys;
890 * Handler for global hotkeys of the BuildRailToolbarWindow.
891 * @param hotkey Hotkey
892 * @return ES_HANDLED if hotkey was accepted.
894 static EventState RailToolbarGlobalHotkeys(int hotkey)
896 if (_game_mode != GM_NORMAL || !CanBuildVehicleInfrastructure(VEH_TRAIN)) return ES_NOT_HANDLED;
897 extern RailType _last_built_railtype;
898 Window *w = ShowBuildRailToolbar(_last_built_railtype);
899 if (w == nullptr) return ES_NOT_HANDLED;
900 return w->OnHotkey(hotkey);
903 const uint16 _railtoolbar_autorail_keys[] = {'5', 'A' | WKC_GLOBAL_HOTKEY, 0};
904 const uint16 _railtoolbar_polyrail_keys[] = {'5' | WKC_CTRL, 'A' | WKC_CTRL | WKC_GLOBAL_HOTKEY, 0};
905 const uint16 _railtoolbar_new_poly_keys[] = {'5' | WKC_CTRL | WKC_SHIFT, 'A' | WKC_CTRL | WKC_SHIFT | WKC_GLOBAL_HOTKEY, 0};
907 static Hotkey railtoolbar_hotkeys[] = {
908 Hotkey('1', "build_ns", WID_RAT_BUILD_NS),
909 Hotkey('2', "build_x", WID_RAT_BUILD_X),
910 Hotkey('3', "build_ew", WID_RAT_BUILD_EW),
911 Hotkey('4', "build_y", WID_RAT_BUILD_Y),
912 Hotkey(_railtoolbar_autorail_keys, "autorail", WID_RAT_AUTORAIL),
913 Hotkey(_railtoolbar_polyrail_keys, "polyrail", HOTKEY_POLYRAIL),
914 Hotkey(_railtoolbar_new_poly_keys, "new_polyrail", HOTKEY_NEW_POLYRAIL),
915 Hotkey('6', "demolish", WID_RAT_DEMOLISH),
916 Hotkey('7', "depot", WID_RAT_BUILD_DEPOT),
917 Hotkey('8', "waypoint", WID_RAT_BUILD_WAYPOINT),
918 Hotkey('9', "station", WID_RAT_BUILD_STATION),
919 Hotkey('S', "signal", WID_RAT_BUILD_SIGNALS),
920 Hotkey('B', "bridge", WID_RAT_BUILD_BRIDGE),
921 Hotkey('T', "tunnel", WID_RAT_BUILD_TUNNEL),
922 Hotkey('R', "remove", WID_RAT_REMOVE),
923 Hotkey('C', "convert", WID_RAT_CONVERT_RAIL),
924 HOTKEY_LIST_END
926 HotkeyList BuildRailToolbarWindow::hotkeys("railtoolbar", railtoolbar_hotkeys, RailToolbarGlobalHotkeys);
928 static const NWidgetPart _nested_build_rail_widgets[] = {
929 NWidget(NWID_HORIZONTAL),
930 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
931 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_RAT_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
932 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
933 EndContainer(),
934 NWidget(NWID_HORIZONTAL),
935 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_NS),
936 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NS, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
937 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_X),
938 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NE, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
939 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_EW),
940 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_EW, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
941 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_Y),
942 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NW, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
943 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_AUTORAIL),
944 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_AUTORAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_AUTORAIL),
945 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_POLYRAIL),
946 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_AUTORAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_POLYRAIL),
948 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), SetDataTip(0x0, STR_NULL), EndContainer(),
950 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_DEMOLISH),
951 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
952 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_DEPOT),
953 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DEPOT_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_TRAIN_DEPOT_FOR_BUILDING),
954 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_WAYPOINT),
955 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_WAYPOINT, STR_RAIL_TOOLBAR_TOOLTIP_CONVERT_RAIL_TO_WAYPOINT),
956 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_STATION),
957 SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_RAIL_STATION, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_STATION),
958 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_SIGNALS),
959 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_SIGNALS, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_SIGNALS),
960 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_PROGRAM_SIGNALS),
961 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_SETTINGS, STR_RAIL_TOOLBAR_TOOLTIP_PROGRAM_SIGNAL),
962 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_BRIDGE),
963 SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_BRIDGE, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_BRIDGE),
964 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_TUNNEL),
965 SetFill(0, 1), SetMinimalSize(20, 22), SetDataTip(SPR_IMG_TUNNEL_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TUNNEL),
966 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_REMOVE),
967 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_REMOVE, STR_RAIL_TOOLBAR_TOOLTIP_TOGGLE_BUILD_REMOVE_FOR),
968 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_CONVERT_RAIL),
969 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_CONVERT_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_CONVERT_RAIL),
970 EndContainer(),
973 static WindowDesc _build_rail_desc(
974 WDP_ALIGN_TOOLBAR, "toolbar_rail", 0, 0,
975 WC_BUILD_TOOLBAR, WC_NONE,
976 WDF_CONSTRUCTION,
977 _nested_build_rail_widgets, lengthof(_nested_build_rail_widgets),
978 &BuildRailToolbarWindow::hotkeys
983 * Open the build rail toolbar window for a specific rail type.
985 * If the terraform toolbar is linked to the toolbar, that window is also opened.
987 * @param railtype Rail type to open the window for
988 * @return newly opened rail toolbar, or nullptr if the toolbar could not be opened.
990 Window *ShowBuildRailToolbar(RailType railtype)
992 if (!Company::IsValidID(_local_company)) return nullptr;
993 if (!ValParamRailtype(railtype)) return nullptr;
995 DeleteWindowByClass(WC_BUILD_TOOLBAR);
996 _cur_railtype = railtype;
997 _remove_button_clicked = false;
998 return new BuildRailToolbarWindow(&_build_rail_desc, railtype);
1001 /* TODO: For custom stations, respect their allowed platforms/lengths bitmasks!
1002 * --pasky */
1004 static void HandleStationPlacement(TileIndex start, TileIndex end)
1006 TileArea ta(start, end);
1007 uint numtracks = ta.w;
1008 uint platlength = ta.h;
1010 if (_railstation.orientation == AXIS_X) Swap(numtracks, platlength);
1012 uint32 p1 = _cur_railtype | _railstation.orientation << 5 | numtracks << 8 | platlength << 16 | _ctrl_pressed << 24;
1013 uint32 p2 = _railstation.station_class | _railstation.station_type << 8 | INVALID_STATION << 16;
1015 CommandContainer cmdcont = { ta.tile, p1, p2, CMD_BUILD_RAIL_STATION | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION), CcStation, 0, "" };
1016 ShowSelectStationIfNeeded(cmdcont, ta);
1019 struct BuildRailStationWindow : public PickerWindowBase {
1020 private:
1021 uint line_height; ///< Height of a single line in the newstation selection matrix (#WID_BRAS_NEWST_LIST widget).
1022 uint coverage_height; ///< Height of the coverage texts.
1023 Scrollbar *vscroll; ///< Vertical scrollbar of the new station list.
1024 Scrollbar *vscroll2; ///< Vertical scrollbar of the matrix with new stations.
1027 * Verify whether the currently selected station size is allowed after selecting a new station class/type.
1028 * If not, change the station size variables ( _settings_client.gui.station_numtracks and _settings_client.gui.station_platlength ).
1029 * @param statspec Specification of the new station class/type
1031 void CheckSelectedSize(const StationSpec *statspec)
1033 if (statspec == nullptr || _settings_client.gui.station_dragdrop) return;
1035 /* If current number of tracks is not allowed, make it as big as possible */
1036 if (HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
1037 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1038 _settings_client.gui.station_numtracks = 1;
1039 if (statspec->disallowed_platforms != UINT8_MAX) {
1040 while (HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
1041 _settings_client.gui.station_numtracks++;
1043 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1047 if (HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
1048 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1049 _settings_client.gui.station_platlength = 1;
1050 if (statspec->disallowed_lengths != UINT8_MAX) {
1051 while (HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
1052 _settings_client.gui.station_platlength++;
1054 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1059 public:
1060 BuildRailStationWindow(WindowDesc *desc, Window *parent, bool newstation) : PickerWindowBase(desc, parent)
1062 this->coverage_height = 2 * FONT_HEIGHT_NORMAL + 3 * WD_PAR_VSEP_NORMAL;
1063 this->vscroll = nullptr;
1064 _railstation.newstations = newstation;
1066 this->CreateNestedTree();
1067 NWidgetStacked *newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_ADDITIONS);
1068 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
1069 newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_MATRIX);
1070 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
1071 newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_DEFSIZE);
1072 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
1073 newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_RESIZE);
1074 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
1075 if (newstation) {
1076 this->vscroll = this->GetScrollbar(WID_BRAS_NEWST_SCROLL);
1077 this->vscroll2 = this->GetScrollbar(WID_BRAS_MATRIX_SCROLL);
1079 this->FinishInitNested(TRANSPORT_RAIL);
1081 this->LowerWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
1082 if (_settings_client.gui.station_dragdrop) {
1083 this->LowerWidget(WID_BRAS_PLATFORM_DRAG_N_DROP);
1084 } else {
1085 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1086 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1088 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_OFF, !_settings_client.gui.station_show_coverage);
1089 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_ON, _settings_client.gui.station_show_coverage);
1091 if (!newstation || _railstation.station_class >= (int)StationClass::GetClassCount()) {
1092 /* New stations are not available or changed, so ensure the default station
1093 * type is 'selected'. */
1094 _railstation.station_class = STAT_CLASS_DFLT;
1095 _railstation.station_type = 0;
1096 this->vscroll2 = nullptr;
1098 if (newstation) {
1099 _railstation.station_count = StationClass::Get(_railstation.station_class)->GetSpecCount();
1100 _railstation.station_type = min(_railstation.station_type, _railstation.station_count - 1);
1102 int count = 0;
1103 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1104 if (i == STAT_CLASS_WAYP) continue;
1105 count++;
1107 this->vscroll->SetCount(count);
1108 this->vscroll->SetPosition(Clamp(_railstation.station_class - 2, 0, max(this->vscroll->GetCount() - this->vscroll->GetCapacity(), 0)));
1110 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX);
1111 matrix->SetScrollbar(this->vscroll2);
1112 matrix->SetCount(_railstation.station_count);
1113 matrix->SetClicked(_railstation.station_type);
1117 virtual ~BuildRailStationWindow()
1119 DeleteWindowById(WC_SELECT_STATION, 0);
1122 virtual void OnPaint()
1124 bool newstations = _railstation.newstations;
1125 const StationSpec *statspec = newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : nullptr;
1127 if (_settings_client.gui.station_dragdrop) {
1128 SetTileSelectSize(1, 1);
1129 } else {
1130 int x = _settings_client.gui.station_numtracks;
1131 int y = _settings_client.gui.station_platlength;
1132 if (_railstation.orientation == AXIS_X) Swap(x, y);
1133 if (!_remove_button_clicked) {
1134 SetTileSelectSize(x, y);
1138 int rad = (_settings_game.station.modified_catchment) ? CA_TRAIN : CA_UNMODIFIED;
1140 if (_settings_client.gui.station_show_coverage) SetTileSelectBigSize(-rad, -rad, 2 * rad, 2 * rad);
1142 for (uint bits = 0; bits < 7; bits++) {
1143 bool disable = bits >= _settings_game.station.station_spread;
1144 if (statspec == nullptr) {
1145 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_NUM_1, disable);
1146 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_LEN_1, disable);
1147 } else {
1148 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_NUM_1, HasBit(statspec->disallowed_platforms, bits) || disable);
1149 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_LEN_1, HasBit(statspec->disallowed_lengths, bits) || disable);
1153 this->DrawWidgets();
1155 /* 'Accepts' and 'Supplies' texts. */
1156 NWidgetBase *cov = this->GetWidget<NWidgetBase>(WID_BRAS_COVERAGE_TEXTS);
1157 int top = cov->pos_y + WD_PAR_VSEP_NORMAL;
1158 int left = cov->pos_x + WD_FRAMERECT_LEFT;
1159 int right = cov->pos_x + cov->current_x - WD_FRAMERECT_RIGHT;
1160 int bottom = cov->pos_y + cov->current_y;
1161 top = DrawStationCoverageAreaText(left, right, top, SCT_ALL, rad, false) + WD_PAR_VSEP_NORMAL;
1162 top = DrawStationCoverageAreaText(left, right, top, SCT_ALL, rad, true) + WD_PAR_VSEP_NORMAL;
1163 /* Resize background if the window is too small.
1164 * Never make the window smaller to avoid oscillating if the size change affects the acceptance.
1165 * (This is the case, if making the window bigger moves the mouse into the window.) */
1166 if (top > bottom) {
1167 this->coverage_height += top - bottom;
1168 this->ReInit();
1172 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1174 switch (widget) {
1175 case WID_BRAS_NEWST_LIST: {
1176 Dimension d = {0, 0};
1177 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1178 if (i == STAT_CLASS_WAYP) continue;
1179 d = maxdim(d, GetStringBoundingBox(StationClass::Get((StationClassID)i)->name));
1181 size->width = max(size->width, d.width + padding.width);
1182 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
1183 size->height = 5 * this->line_height;
1184 resize->height = this->line_height;
1185 break;
1188 case WID_BRAS_SHOW_NEWST_TYPE: {
1189 if (!_railstation.newstations) {
1190 size->width = 0;
1191 size->height = 0;
1192 break;
1195 /* If newstations exist, compute the non-zero minimal size. */
1196 Dimension d = {0, 0};
1197 StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
1198 for (StationClassID statclass = STAT_CLASS_BEGIN; statclass < (StationClassID)StationClass::GetClassCount(); statclass++) {
1199 if (statclass == STAT_CLASS_WAYP) continue;
1200 StationClass *stclass = StationClass::Get(statclass);
1201 for (uint16 j = 0; j < stclass->GetSpecCount(); j++) {
1202 const StationSpec *statspec = stclass->GetSpec(j);
1203 SetDParam(0, (statspec != nullptr && statspec->name != 0) ? statspec->name : STR_STATION_CLASS_DFLT);
1204 d = maxdim(d, GetStringBoundingBox(str));
1207 size->width = max(size->width, d.width + padding.width);
1208 size->width = min(size->width, ScaleGUITrad(400));
1209 break;
1212 case WID_BRAS_PLATFORM_DIR_X:
1213 case WID_BRAS_PLATFORM_DIR_Y:
1214 case WID_BRAS_IMAGE:
1215 size->width = ScaleGUITrad(64) + 2;
1216 size->height = ScaleGUITrad(58) + 2;
1217 break;
1219 case WID_BRAS_COVERAGE_TEXTS:
1220 size->height = this->coverage_height;
1221 break;
1223 case WID_BRAS_MATRIX:
1224 fill->height = 1;
1225 resize->height = 1;
1226 break;
1230 virtual void DrawWidget(const Rect &r, int widget) const
1232 DrawPixelInfo tmp_dpi;
1234 switch (GB(widget, 0, 16)) {
1235 case WID_BRAS_PLATFORM_DIR_X:
1236 /* Set up a clipping area for the '/' station preview */
1237 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1238 DrawPixelInfo *old_dpi = _cur_dpi;
1239 _cur_dpi = &tmp_dpi;
1240 int x = ScaleGUITrad(31) + 1;
1241 int y = r.bottom - r.top - ScaleGUITrad(31);
1242 if (!DrawStationTile(x, y, _cur_railtype, AXIS_X, _railstation.station_class, _railstation.station_type)) {
1243 StationPickerDrawSprite(x, y, STATION_RAIL, _cur_railtype, RoadTypeIdentifier(), 2);
1245 _cur_dpi = old_dpi;
1247 break;
1249 case WID_BRAS_PLATFORM_DIR_Y:
1250 /* Set up a clipping area for the '\' station preview */
1251 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1252 DrawPixelInfo *old_dpi = _cur_dpi;
1253 _cur_dpi = &tmp_dpi;
1254 int x = ScaleGUITrad(31) + 1;
1255 int y = r.bottom - r.top - ScaleGUITrad(31);
1256 if (!DrawStationTile(x, y, _cur_railtype, AXIS_Y, _railstation.station_class, _railstation.station_type)) {
1257 StationPickerDrawSprite(x, y, STATION_RAIL, _cur_railtype, RoadTypeIdentifier(), 3);
1259 _cur_dpi = old_dpi;
1261 break;
1263 case WID_BRAS_NEWST_LIST: {
1264 uint statclass = 0;
1265 uint row = 0;
1266 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1267 if (i == STAT_CLASS_WAYP) continue;
1268 if (this->vscroll->IsVisible(statclass)) {
1269 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, row * this->line_height + r.top + WD_MATRIX_TOP,
1270 StationClass::Get((StationClassID)i)->name,
1271 (StationClassID)i == _railstation.station_class ? TC_WHITE : TC_BLACK);
1272 row++;
1274 statclass++;
1276 break;
1279 case WID_BRAS_IMAGE: {
1280 byte type = GB(widget, 16, 16);
1281 assert(type < _railstation.station_count);
1282 /* Check station availability callback */
1283 const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(type);
1284 if (!IsStationAvailable(statspec)) {
1285 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
1288 /* Set up a clipping area for the station preview. */
1289 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1290 DrawPixelInfo *old_dpi = _cur_dpi;
1291 _cur_dpi = &tmp_dpi;
1292 int x = ScaleGUITrad(31) + 1;
1293 int y = r.bottom - r.top - ScaleGUITrad(31);
1294 if (!DrawStationTile(x, y, _cur_railtype, _railstation.orientation, _railstation.station_class, type)) {
1295 StationPickerDrawSprite(x, y, STATION_RAIL, _cur_railtype, RoadTypeIdentifier(), 2 + _railstation.orientation);
1297 _cur_dpi = old_dpi;
1299 break;
1304 virtual void OnResize()
1306 if (this->vscroll != nullptr) { // New stations available.
1307 this->vscroll->SetCapacityFromWidget(this, WID_BRAS_NEWST_LIST);
1311 virtual void SetStringParameters(int widget) const
1313 if (widget == WID_BRAS_SHOW_NEWST_TYPE) {
1314 const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type);
1315 SetDParam(0, (statspec != nullptr && statspec->name != 0) ? statspec->name : STR_STATION_CLASS_DFLT);
1319 virtual void OnClick(Point pt, int widget, int click_count)
1321 switch (GB(widget, 0, 16)) {
1322 case WID_BRAS_PLATFORM_DIR_X:
1323 case WID_BRAS_PLATFORM_DIR_Y:
1324 this->RaiseWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
1325 _railstation.orientation = (Axis)(widget - WID_BRAS_PLATFORM_DIR_X);
1326 this->LowerWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
1327 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1328 this->SetDirty();
1329 DeleteWindowById(WC_SELECT_STATION, 0);
1330 break;
1332 case WID_BRAS_PLATFORM_NUM_1:
1333 case WID_BRAS_PLATFORM_NUM_2:
1334 case WID_BRAS_PLATFORM_NUM_3:
1335 case WID_BRAS_PLATFORM_NUM_4:
1336 case WID_BRAS_PLATFORM_NUM_5:
1337 case WID_BRAS_PLATFORM_NUM_6:
1338 case WID_BRAS_PLATFORM_NUM_7: {
1339 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1340 this->RaiseWidget(WID_BRAS_PLATFORM_DRAG_N_DROP);
1342 _settings_client.gui.station_numtracks = widget - WID_BRAS_PLATFORM_NUM_BEGIN;
1343 _settings_client.gui.station_dragdrop = false;
1345 _settings_client.gui.station_dragdrop = false;
1347 const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : nullptr;
1348 if (statspec != nullptr && HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
1349 /* The previously selected number of platforms in invalid */
1350 for (uint i = 0; i < 7; i++) {
1351 if (!HasBit(statspec->disallowed_lengths, i)) {
1352 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1353 _settings_client.gui.station_platlength = i + 1;
1354 break;
1359 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1360 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1361 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1362 this->SetDirty();
1363 DeleteWindowById(WC_SELECT_STATION, 0);
1364 break;
1367 case WID_BRAS_PLATFORM_LEN_1:
1368 case WID_BRAS_PLATFORM_LEN_2:
1369 case WID_BRAS_PLATFORM_LEN_3:
1370 case WID_BRAS_PLATFORM_LEN_4:
1371 case WID_BRAS_PLATFORM_LEN_5:
1372 case WID_BRAS_PLATFORM_LEN_6:
1373 case WID_BRAS_PLATFORM_LEN_7: {
1374 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1375 this->RaiseWidget(WID_BRAS_PLATFORM_DRAG_N_DROP);
1377 _settings_client.gui.station_platlength = widget - WID_BRAS_PLATFORM_LEN_BEGIN;
1378 _settings_client.gui.station_dragdrop = false;
1380 _settings_client.gui.station_dragdrop = false;
1382 const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : nullptr;
1383 if (statspec != nullptr && HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
1384 /* The previously selected number of tracks in invalid */
1385 for (uint i = 0; i < 7; i++) {
1386 if (!HasBit(statspec->disallowed_platforms, i)) {
1387 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1388 _settings_client.gui.station_numtracks = i + 1;
1389 break;
1394 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1395 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1396 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1397 this->SetDirty();
1398 DeleteWindowById(WC_SELECT_STATION, 0);
1399 break;
1402 case WID_BRAS_PLATFORM_DRAG_N_DROP: {
1403 _settings_client.gui.station_dragdrop ^= true;
1405 this->ToggleWidgetLoweredState(WID_BRAS_PLATFORM_DRAG_N_DROP);
1407 /* get the first allowed length/number of platforms */
1408 const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : nullptr;
1409 if (statspec != nullptr && HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
1410 for (uint i = 0; i < 7; i++) {
1411 if (!HasBit(statspec->disallowed_lengths, i)) {
1412 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1413 _settings_client.gui.station_platlength = i + 1;
1414 break;
1418 if (statspec != nullptr && HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
1419 for (uint i = 0; i < 7; i++) {
1420 if (!HasBit(statspec->disallowed_platforms, i)) {
1421 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1422 _settings_client.gui.station_numtracks = i + 1;
1423 break;
1428 this->SetWidgetLoweredState(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN, !_settings_client.gui.station_dragdrop);
1429 this->SetWidgetLoweredState(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN, !_settings_client.gui.station_dragdrop);
1430 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1431 this->SetDirty();
1432 DeleteWindowById(WC_SELECT_STATION, 0);
1433 break;
1436 case WID_BRAS_HIGHLIGHT_OFF:
1437 case WID_BRAS_HIGHLIGHT_ON:
1438 _settings_client.gui.station_show_coverage = (widget != WID_BRAS_HIGHLIGHT_OFF);
1440 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_OFF, !_settings_client.gui.station_show_coverage);
1441 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_ON, _settings_client.gui.station_show_coverage);
1442 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1443 this->SetDirty();
1444 break;
1446 case WID_BRAS_NEWST_LIST: {
1447 int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_BRAS_NEWST_LIST, 0, this->line_height);
1448 if (y >= (int)StationClass::GetClassCount()) return;
1449 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1450 if (i == STAT_CLASS_WAYP) continue;
1451 if (y == 0) {
1452 if (_railstation.station_class != (StationClassID)i) {
1453 _railstation.station_class = (StationClassID)i;
1454 StationClass *stclass = StationClass::Get(_railstation.station_class);
1455 _railstation.station_count = stclass->GetSpecCount();
1456 _railstation.station_type = min((int)_railstation.station_type, max(0, (int)_railstation.station_count - 1));
1458 this->CheckSelectedSize(stclass->GetSpec(_railstation.station_type));
1460 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX);
1461 matrix->SetCount(_railstation.station_count);
1462 matrix->SetClicked(_railstation.station_type);
1464 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1465 this->SetDirty();
1466 DeleteWindowById(WC_SELECT_STATION, 0);
1467 break;
1469 y--;
1471 break;
1474 case WID_BRAS_IMAGE: {
1475 int y = GB(widget, 16, 16);
1476 if (y >= _railstation.station_count) return;
1478 /* Check station availability callback */
1479 const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(y);
1480 if (!IsStationAvailable(statspec)) return;
1482 _railstation.station_type = y;
1484 this->CheckSelectedSize(statspec);
1485 this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX)->SetClicked(_railstation.station_type);
1487 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1488 this->SetDirty();
1489 DeleteWindowById(WC_SELECT_STATION, 0);
1490 break;
1495 virtual void OnTick()
1497 CheckRedrawStationCoverage(this);
1501 static const NWidgetPart _nested_station_builder_widgets[] = {
1502 NWidget(NWID_HORIZONTAL),
1503 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1504 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_STATION_BUILD_RAIL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1505 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_DEFSIZE),
1506 NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
1507 EndContainer(),
1508 EndContainer(),
1509 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1510 NWidget(NWID_HORIZONTAL),
1511 NWidget(NWID_VERTICAL),
1512 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_ADDITIONS),
1513 NWidget(NWID_HORIZONTAL), SetPIP(7, 0, 7), SetPadding(2, 0, 1, 0),
1514 NWidget(WWT_MATRIX, COLOUR_GREY, WID_BRAS_NEWST_LIST), SetMinimalSize(122, 71), SetFill(1, 0),
1515 SetMatrixDataTip(1, 0, STR_STATION_BUILD_STATION_CLASS_TOOLTIP), SetScrollbar(WID_BRAS_NEWST_SCROLL),
1516 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_BRAS_NEWST_SCROLL),
1517 EndContainer(),
1518 EndContainer(),
1519 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_ORIENTATION, STR_NULL), SetPadding(1, 2, 0, 2),
1520 NWidget(NWID_HORIZONTAL),
1521 NWidget(NWID_SPACER), SetMinimalSize(7, 0), SetFill(1, 0),
1522 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(),
1523 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1524 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(),
1525 NWidget(NWID_SPACER), SetMinimalSize(7, 0), SetFill(1, 0),
1526 EndContainer(),
1527 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),
1528 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_NUMBER_OF_TRACKS, STR_NULL), SetPadding(0, 2, 0, 2),
1529 NWidget(NWID_HORIZONTAL),
1530 NWidget(NWID_SPACER), SetFill(1, 0),
1531 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_1), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_1, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1532 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_2), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_2, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1533 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_3), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_3, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1534 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_4), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_4, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1535 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_5), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_5, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1536 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_6), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_6, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1537 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_7), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_7, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1538 NWidget(NWID_SPACER), SetFill(1, 0),
1539 EndContainer(),
1540 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_PLATFORM_LENGTH, STR_NULL), SetPadding(2, 2, 0, 2),
1541 NWidget(NWID_HORIZONTAL),
1542 NWidget(NWID_SPACER), SetFill(1, 0),
1543 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_1), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_1, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1544 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_2), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_2, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1545 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_3), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_3, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1546 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_4), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_4, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1547 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_5), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_5, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1548 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_6), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_6, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1549 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_7), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_7, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1550 NWidget(NWID_SPACER), SetFill(1, 0),
1551 EndContainer(),
1552 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1553 NWidget(NWID_HORIZONTAL),
1554 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1555 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),
1556 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1557 EndContainer(),
1558 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_COVERAGE_AREA_TITLE, STR_NULL), SetPadding(3, 2, 0, 2),
1559 NWidget(NWID_HORIZONTAL),
1560 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1561 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_HIGHLIGHT_OFF), SetMinimalSize(60, 12),
1562 SetDataTip(STR_STATION_BUILD_COVERAGE_OFF, STR_STATION_BUILD_COVERAGE_AREA_OFF_TOOLTIP),
1563 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_HIGHLIGHT_ON), SetMinimalSize(60, 12),
1564 SetDataTip(STR_STATION_BUILD_COVERAGE_ON, STR_STATION_BUILD_COVERAGE_AREA_ON_TOOLTIP),
1565 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1566 EndContainer(),
1567 EndContainer(),
1568 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_MATRIX),
1569 /* We need an additional background for the matrix, as the matrix cannot handle the scrollbar due to not being an NWidgetCore. */
1570 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(WID_BRAS_MATRIX_SCROLL),
1571 NWidget(NWID_HORIZONTAL),
1572 NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BRAS_MATRIX), SetScrollbar(WID_BRAS_MATRIX_SCROLL), SetPIP(0, 2, 0), SetPadding(2, 0, 0, 0),
1573 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BRAS_IMAGE), SetMinimalSize(66, 60),
1574 SetFill(0, 0), SetResize(0, 0), SetDataTip(0x0, STR_STATION_BUILD_STATION_TYPE_TOOLTIP), SetScrollbar(WID_BRAS_MATRIX_SCROLL),
1575 EndContainer(),
1576 EndContainer(),
1577 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BRAS_MATRIX_SCROLL),
1578 EndContainer(),
1579 EndContainer(),
1580 EndContainer(),
1581 EndContainer(),
1582 NWidget(NWID_HORIZONTAL),
1583 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BRAS_COVERAGE_TEXTS), SetFill(1, 1), SetResize(1, 0),
1584 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_RESIZE),
1585 NWidget(NWID_VERTICAL),
1586 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(0, 1), EndContainer(),
1587 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
1588 EndContainer(),
1589 EndContainer(),
1590 EndContainer(),
1591 EndContainer(),
1594 /** High level window description of the station-build window (default & newGRF) */
1595 static WindowDesc _station_builder_desc(
1596 WDP_AUTO, "build_station_rail", 350, 0,
1597 WC_BUILD_STATION, WC_BUILD_TOOLBAR,
1598 WDF_CONSTRUCTION,
1599 _nested_station_builder_widgets, lengthof(_nested_station_builder_widgets)
1602 /** Open station build window */
1603 static void ShowStationBuilder(Window *parent)
1605 bool newstations = StationClass::GetClassCount() > 2 || StationClass::Get(STAT_CLASS_DFLT)->GetSpecCount() != 1;
1606 new BuildRailStationWindow(&_station_builder_desc, parent, newstations);
1609 struct BuildSignalWindow : public PickerWindowBase {
1610 private:
1611 Dimension sig_sprite_size; ///< Maximum size of signal GUI sprites.
1612 int sig_sprite_bottom_offset; ///< Maximum extent of signal GUI sprite from reference point towards bottom.
1615 * Draw dynamic a signal-sprite in a button in the signal GUI
1616 * Draw the sprite +1px to the right and down if the button is lowered
1618 * @param widget_index index of this widget in the window
1619 * @param image the sprite to draw
1621 void DrawSignalSprite(byte widget_index, SpriteID image) const
1623 Point offset;
1624 Dimension sprite_size = GetSpriteSize(image, &offset);
1625 const NWidgetBase *widget = this->GetWidget<NWidgetBase>(widget_index);
1626 int x = widget->pos_x - offset.x +
1627 (widget->current_x - sprite_size.width + offset.x) / 2; // centered
1628 int y = widget->pos_y - sig_sprite_bottom_offset + WD_IMGBTN_TOP +
1629 (widget->current_y - WD_IMGBTN_TOP - WD_IMGBTN_BOTTOM + sig_sprite_size.height) / 2; // aligned to bottom
1631 DrawSprite(image, PAL_NONE,
1632 x + this->IsWidgetLowered(widget_index),
1633 y + this->IsWidgetLowered(widget_index));
1636 public:
1637 BuildSignalWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1639 this->InitNested(TRANSPORT_RAIL);
1640 this->OnInvalidateData();
1643 ~BuildSignalWindow()
1645 _convert_signal_button = false;
1646 _trace_restrict_button = false;
1649 virtual void OnInit()
1651 /* Calculate maximum signal sprite size. */
1652 this->sig_sprite_size.width = 0;
1653 this->sig_sprite_size.height = 0;
1654 this->sig_sprite_bottom_offset = 0;
1655 const RailtypeInfo *rti = GetRailTypeInfo(_cur_railtype);
1656 for (uint type = SIGTYPE_NORMAL; type < SIGTYPE_END; type++) {
1657 for (uint variant = SIG_ELECTRIC; variant <= SIG_SEMAPHORE; variant++) {
1658 for (uint lowered = 0; lowered < 2; lowered++) {
1659 Point offset;
1660 Dimension sprite_size = GetSpriteSize(rti->gui_sprites.signals[type][variant][lowered], &offset);
1661 this->sig_sprite_bottom_offset = max<int>(this->sig_sprite_bottom_offset, sprite_size.height);
1662 this->sig_sprite_size.width = max<int>(this->sig_sprite_size.width, sprite_size.width - offset.x);
1663 this->sig_sprite_size.height = max<int>(this->sig_sprite_size.height, sprite_size.height - offset.y);
1669 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1671 if (widget == WID_BS_DRAG_SIGNALS_DENSITY_LABEL) {
1672 /* Two digits for signals density. */
1673 size->width = max(size->width, 2 * GetDigitWidth() + padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT);
1674 } else if (IsInsideMM(widget, WID_BS_SEMAPHORE_NORM, WID_BS_ELECTRIC_PBS_OWAY + 1)) {
1675 size->width = max(size->width, this->sig_sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT);
1676 size->height = max(size->height, this->sig_sprite_size.height + WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM);
1680 virtual void SetStringParameters(int widget) const
1682 switch (widget) {
1683 case WID_BS_DRAG_SIGNALS_DENSITY_LABEL:
1684 SetDParam(0, _settings_client.gui.drag_signals_density);
1685 break;
1689 virtual void DrawWidget(const Rect &r, int widget) const
1691 if (IsInsideMM(widget, WID_BS_SEMAPHORE_NORM, WID_BS_ELECTRIC_LOGIC + 1)) {
1692 /* Extract signal from widget number. */
1693 int type = (widget - WID_BS_SEMAPHORE_NORM) % SIGTYPE_END;
1694 int var = SIG_SEMAPHORE - (widget - WID_BS_SEMAPHORE_NORM) / SIGTYPE_END; // SignalVariant order is reversed compared to the widgets.
1695 SpriteID sprite = GetRailTypeInfo(_cur_railtype)->gui_sprites.signals[type][var][this->IsWidgetLowered(widget)];
1697 this->DrawSignalSprite(widget, sprite);
1701 virtual void OnClick(Point pt, int widget, int click_count)
1703 switch (widget) {
1704 case WID_BS_SEMAPHORE_NORM:
1705 case WID_BS_SEMAPHORE_ENTRY:
1706 case WID_BS_SEMAPHORE_EXIT:
1707 case WID_BS_SEMAPHORE_COMBO:
1708 case WID_BS_SEMAPHORE_PBS:
1709 case WID_BS_SEMAPHORE_PBS_OWAY:
1710 case WID_BS_SEMAPHORE_LOGIC:
1711 case WID_BS_ELECTRIC_NORM:
1712 case WID_BS_ELECTRIC_ENTRY:
1713 case WID_BS_ELECTRIC_EXIT:
1714 case WID_BS_ELECTRIC_COMBO:
1715 case WID_BS_ELECTRIC_PBS:
1716 case WID_BS_ELECTRIC_PBS_OWAY:
1717 case WID_BS_ELECTRIC_LOGIC:
1718 this->RaiseWidget((_cur_signal_variant == SIG_ELECTRIC ? WID_BS_ELECTRIC_NORM : WID_BS_SEMAPHORE_NORM) + _cur_signal_type);
1720 _cur_signal_type = (SignalType)((uint)((widget - WID_BS_SEMAPHORE_NORM) % (SIGTYPE_LAST + 1)));
1721 _cur_signal_variant = widget >= WID_BS_ELECTRIC_NORM ? SIG_ELECTRIC : SIG_SEMAPHORE;
1723 /* If 'remove' button of rail build toolbar is active, disable it. */
1724 if (_remove_button_clicked) {
1725 Window *w = FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL);
1726 if (w != nullptr) ToggleRailButton_Remove(w);
1729 break;
1731 case WID_BS_CONVERT:
1732 _convert_signal_button = !_convert_signal_button;
1733 if (_convert_signal_button) _trace_restrict_button = false;
1734 break;
1736 case WID_BS_TRACE_RESTRICT:
1737 _trace_restrict_button = !_trace_restrict_button;
1738 if (_trace_restrict_button) _convert_signal_button = false;
1739 break;
1741 case WID_BS_DRAG_SIGNALS_DENSITY_DECREASE:
1742 if (_settings_client.gui.drag_signals_density > 1) {
1743 _settings_client.gui.drag_signals_density--;
1744 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_SETTINGS);
1746 break;
1748 case WID_BS_DRAG_SIGNALS_DENSITY_INCREASE:
1749 if (_settings_client.gui.drag_signals_density < 20) {
1750 _settings_client.gui.drag_signals_density++;
1751 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_SETTINGS);
1753 break;
1755 default: break;
1758 this->InvalidateData();
1762 * Some data on this window has become invalid.
1763 * @param data Information about the changed data.
1764 * @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.
1766 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1768 if (!gui_scope) return;
1769 this->LowerWidget((_cur_signal_variant == SIG_ELECTRIC ? WID_BS_ELECTRIC_NORM : WID_BS_SEMAPHORE_NORM) + _cur_signal_type);
1771 this->SetWidgetLoweredState(WID_BS_CONVERT, _convert_signal_button);
1772 this->SetWidgetLoweredState(WID_BS_TRACE_RESTRICT, _trace_restrict_button);
1774 this->SetWidgetDisabledState(WID_BS_DRAG_SIGNALS_DENSITY_DECREASE, _settings_client.gui.drag_signals_density == 1);
1775 this->SetWidgetDisabledState(WID_BS_DRAG_SIGNALS_DENSITY_INCREASE, _settings_client.gui.drag_signals_density == 20);
1779 /** Nested widget definition of the build signal window */
1780 static const NWidgetPart _nested_signal_builder_widgets[] = {
1781 NWidget(NWID_HORIZONTAL),
1782 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1783 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_BUILD_SIGNAL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1784 EndContainer(),
1785 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
1786 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1787 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_NORM), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_NORM_TOOLTIP), EndContainer(), SetFill(1, 1),
1788 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_ENTRY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_ENTRY_TOOLTIP), EndContainer(), SetFill(1, 1),
1789 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_EXIT), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_EXIT_TOOLTIP), EndContainer(), SetFill(1, 1),
1790 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_COMBO), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_COMBO_TOOLTIP), EndContainer(), SetFill(1, 1),
1791 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_PBS), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_PBS_TOOLTIP), EndContainer(), SetFill(1, 1),
1792 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),
1793 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_LOGIC), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_LOGIC_TOOLTIP), EndContainer(), SetFill(1, 1),
1794 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_BS_CONVERT), SetDataTip(SPR_IMG_SIGNAL_CONVERT, STR_BUILD_SIGNAL_CONVERT_TOOLTIP), SetFill(1, 1),
1795 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_BS_TRACE_RESTRICT), SetDataTip(SPR_IMG_SETTINGS, STR_TRACE_RESTRICT_SIGNAL_GUI_TOOLTIP), SetFill(1, 1),
1796 EndContainer(),
1797 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1798 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_NORM), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_NORM_TOOLTIP), EndContainer(), SetFill(1, 1),
1799 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_ENTRY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_ENTRY_TOOLTIP), EndContainer(), SetFill(1, 1),
1800 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_EXIT), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_EXIT_TOOLTIP), EndContainer(), SetFill(1, 1),
1801 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_COMBO), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_COMBO_TOOLTIP), EndContainer(), SetFill(1, 1),
1802 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_PBS), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_PBS_TOOLTIP), EndContainer(), SetFill(1, 1),
1803 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),
1804 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_LOGIC), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_LOGIC_TOOLTIP), EndContainer(), SetFill(1, 1),
1805 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_DRAG_SIGNALS_DENSITY_TOOLTIP), SetFill(1, 1),
1806 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),
1807 NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1808 NWidget(NWID_SPACER), SetFill(1, 0),
1809 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),
1810 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),
1811 NWidget(NWID_SPACER), SetFill(1, 0),
1812 EndContainer(),
1813 NWidget(NWID_SPACER), SetMinimalSize(0, 2), SetFill(1, 0),
1814 EndContainer(),
1815 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), EndContainer(), SetFill(1, 1),
1816 EndContainer(),
1817 EndContainer(),
1820 /** Signal selection window description */
1821 static WindowDesc _signal_builder_desc(
1822 WDP_AUTO, "build_signal", 0, 0,
1823 WC_BUILD_SIGNAL, WC_BUILD_TOOLBAR,
1824 WDF_CONSTRUCTION,
1825 _nested_signal_builder_widgets, lengthof(_nested_signal_builder_widgets)
1829 * Open the signal selection window
1831 static void ShowSignalBuilder(Window *parent)
1833 new BuildSignalWindow(&_signal_builder_desc, parent);
1836 struct BuildRailDepotWindow : public PickerWindowBase {
1837 BuildRailDepotWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1839 this->InitNested(TRANSPORT_RAIL);
1840 this->LowerWidget(_build_depot_direction + WID_BRAD_DEPOT_NE);
1843 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1845 if (!IsInsideMM(widget, WID_BRAD_DEPOT_NE, WID_BRAD_DEPOT_NW + 1)) return;
1847 size->width = ScaleGUITrad(64) + 2;
1848 size->height = ScaleGUITrad(48) + 2;
1851 virtual void DrawWidget(const Rect &r, int widget) const
1853 if (!IsInsideMM(widget, WID_BRAD_DEPOT_NE, WID_BRAD_DEPOT_NW + 1)) return;
1855 DrawTrainDepotSprite(r.left + 1 + ScaleGUITrad(31), r.bottom - ScaleGUITrad(31), widget - WID_BRAD_DEPOT_NE + DIAGDIR_NE, _cur_railtype);
1858 virtual void OnClick(Point pt, int widget, int click_count)
1860 switch (widget) {
1861 case WID_BRAD_DEPOT_NE:
1862 case WID_BRAD_DEPOT_SE:
1863 case WID_BRAD_DEPOT_SW:
1864 case WID_BRAD_DEPOT_NW:
1865 this->RaiseWidget(_build_depot_direction + WID_BRAD_DEPOT_NE);
1866 _build_depot_direction = (DiagDirection)(widget - WID_BRAD_DEPOT_NE);
1867 this->LowerWidget(_build_depot_direction + WID_BRAD_DEPOT_NE);
1868 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1869 this->SetDirty();
1870 break;
1875 /** Nested widget definition of the build rail depot window */
1876 static const NWidgetPart _nested_build_depot_widgets[] = {
1877 NWidget(NWID_HORIZONTAL),
1878 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1879 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_BUILD_DEPOT_TRAIN_ORIENTATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1880 EndContainer(),
1881 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1882 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1883 NWidget(NWID_HORIZONTAL_LTR),
1884 NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
1885 NWidget(NWID_VERTICAL),
1886 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_NW), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1887 EndContainer(),
1888 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1889 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_SW), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1890 EndContainer(),
1891 EndContainer(),
1892 NWidget(NWID_SPACER), SetMinimalSize(2, 0),
1893 NWidget(NWID_VERTICAL),
1894 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_NE), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1895 EndContainer(),
1896 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1897 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_SE), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1898 EndContainer(),
1899 EndContainer(),
1900 NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
1901 EndContainer(),
1902 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1903 EndContainer(),
1906 static WindowDesc _build_depot_desc(
1907 WDP_AUTO, nullptr, 0, 0,
1908 WC_BUILD_DEPOT, WC_BUILD_TOOLBAR,
1909 WDF_CONSTRUCTION,
1910 _nested_build_depot_widgets, lengthof(_nested_build_depot_widgets)
1913 static void ShowBuildTrainDepotPicker(Window *parent)
1915 new BuildRailDepotWindow(&_build_depot_desc, parent);
1918 struct BuildRailWaypointWindow : PickerWindowBase {
1919 BuildRailWaypointWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1921 this->CreateNestedTree();
1923 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX);
1924 matrix->SetScrollbar(this->GetScrollbar(WID_BRW_SCROLL));
1926 this->FinishInitNested(TRANSPORT_RAIL);
1928 matrix->SetCount(_waypoint_count);
1929 matrix->SetClicked(_cur_waypoint_type);
1932 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1934 switch (widget) {
1935 case WID_BRW_WAYPOINT_MATRIX:
1936 /* Three blobs high and wide. */
1937 size->width += resize->width * 2;
1938 size->height += resize->height * 2;
1940 /* Resizing in X direction only at blob size, but at pixel level in Y. */
1941 resize->height = 1;
1942 break;
1944 case WID_BRW_WAYPOINT:
1945 size->width = ScaleGUITrad(64) + 2;
1946 size->height = ScaleGUITrad(58) + 2;
1947 break;
1951 virtual void DrawWidget(const Rect &r, int widget) const
1953 switch (GB(widget, 0, 16)) {
1954 case WID_BRW_WAYPOINT: {
1955 byte type = GB(widget, 16, 16);
1956 const StationSpec *statspec = StationClass::Get(STAT_CLASS_WAYP)->GetSpec(type);
1957 DrawWaypointSprite(r.left + 1 + ScaleGUITrad(31), r.bottom - ScaleGUITrad(31), type, _cur_railtype);
1959 if (!IsStationAvailable(statspec)) {
1960 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
1966 virtual void OnClick(Point pt, int widget, int click_count)
1968 switch (GB(widget, 0, 16)) {
1969 case WID_BRW_WAYPOINT: {
1970 byte type = GB(widget, 16, 16);
1971 this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX)->SetClicked(_cur_waypoint_type);
1973 /* Check station availability callback */
1974 const StationSpec *statspec = StationClass::Get(STAT_CLASS_WAYP)->GetSpec(type);
1975 if (!IsStationAvailable(statspec)) return;
1977 _cur_waypoint_type = type;
1978 this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX)->SetClicked(_cur_waypoint_type);
1979 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1980 this->SetDirty();
1981 break;
1987 /** Nested widget definition for the build NewGRF rail waypoint window */
1988 static const NWidgetPart _nested_build_waypoint_widgets[] = {
1989 NWidget(NWID_HORIZONTAL),
1990 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1991 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_WAYPOINT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1992 NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
1993 EndContainer(),
1994 NWidget(NWID_HORIZONTAL),
1995 NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BRW_WAYPOINT_MATRIX), SetPIP(3, 2, 3), SetScrollbar(WID_BRW_SCROLL),
1996 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BRW_WAYPOINT), SetMinimalSize(66, 60), SetDataTip(0x0, STR_WAYPOINT_GRAPHICS_TOOLTIP), SetScrollbar(WID_BRW_SCROLL), EndContainer(),
1997 EndContainer(),
1998 NWidget(NWID_VERTICAL),
1999 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BRW_SCROLL),
2000 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
2001 EndContainer(),
2002 EndContainer(),
2005 static WindowDesc _build_waypoint_desc(
2006 WDP_AUTO, "build_waypoint", 0, 0,
2007 WC_BUILD_WAYPOINT, WC_BUILD_TOOLBAR,
2008 WDF_CONSTRUCTION,
2009 _nested_build_waypoint_widgets, lengthof(_nested_build_waypoint_widgets)
2012 static void ShowBuildWaypointPicker(Window *parent)
2014 new BuildRailWaypointWindow(&_build_waypoint_desc, parent);
2018 * Initialize rail building GUI settings
2020 void InitializeRailGui()
2022 _build_depot_direction = DIAGDIR_NW;
2026 * Re-initialize rail-build toolbar after toggling support for electric trains
2027 * @param disable Boolean whether electric trains are disabled (removed from the game)
2029 void ReinitGuiAfterToggleElrail(bool disable)
2031 extern RailType _last_built_railtype;
2032 if (disable && _last_built_railtype == RAILTYPE_ELECTRIC) {
2033 _last_built_railtype = _cur_railtype = RAILTYPE_RAIL;
2034 BuildRailToolbarWindow *w = dynamic_cast<BuildRailToolbarWindow *>(FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL));
2035 if (w != nullptr) w->ModifyRailType(_cur_railtype);
2037 MarkWholeScreenDirty();
2040 /** Set the initial (default) railtype to use */
2041 static void SetDefaultRailGui()
2043 if (_local_company == COMPANY_SPECTATOR || !Company::IsValidID(_local_company)) return;
2045 extern RailType _last_built_railtype;
2046 RailType rt = (RailType)(_settings_client.gui.default_rail_type + RAILTYPE_END);
2047 if (rt == DEF_RAILTYPE_MOST_USED) {
2048 /* Find the most used rail type */
2049 RailType count[RAILTYPE_END];
2050 memset(count, 0, sizeof(count));
2051 for (TileIndex t = 0; t < MapSize(); t++) {
2052 if (IsTileType(t, MP_RAILWAY) || IsLevelCrossingTile(t) || HasStationTileRail(t) ||
2053 (IsTileType(t, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL)) {
2054 count[GetRailType(t)]++;
2058 rt = RAILTYPE_RAIL;
2059 for (RailType r = RAILTYPE_ELECTRIC; r < RAILTYPE_END; r++) {
2060 if (count[r] >= count[rt]) rt = r;
2063 /* No rail, just get the first available one */
2064 if (count[rt] == 0) rt = DEF_RAILTYPE_FIRST;
2066 switch (rt) {
2067 case DEF_RAILTYPE_FIRST:
2068 rt = RAILTYPE_RAIL;
2069 while (rt < RAILTYPE_END && !HasRailtypeAvail(_local_company, rt)) rt++;
2070 break;
2072 case DEF_RAILTYPE_LAST:
2073 rt = GetBestRailtype(_local_company);
2074 break;
2076 default:
2077 break;
2080 _last_built_railtype = _cur_railtype = rt;
2081 BuildRailToolbarWindow *w = dynamic_cast<BuildRailToolbarWindow *>(FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL));
2082 if (w != nullptr) w->ModifyRailType(_cur_railtype);
2086 * Updates the current signal variant used in the signal GUI
2087 * to the one adequate to current year.
2088 * @param p needed to be called when a setting changes
2089 * @return success, needed for settings
2091 bool ResetSignalVariant(int32 p)
2093 SignalVariant new_variant = (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC);
2095 if (new_variant != _cur_signal_variant) {
2096 Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
2097 if (w != nullptr) {
2098 w->SetDirty();
2099 w->RaiseWidget((_cur_signal_variant == SIG_ELECTRIC ? WID_BS_ELECTRIC_NORM : WID_BS_SEMAPHORE_NORM) + _cur_signal_type);
2101 _cur_signal_variant = new_variant;
2104 return true;
2108 * Resets the rail GUI - sets default railtype to build
2109 * and resets the signal GUI
2111 void InitializeRailGUI()
2113 SetDefaultRailGui();
2115 _convert_signal_button = false;
2116 _trace_restrict_button = false;
2117 _cur_signal_type = _default_signal_type[_settings_client.gui.default_signal_type];
2118 ResetSignalVariant();
2122 * Create a drop down list for all the rail types of the local company.
2123 * @param for_replacement Whether this list is for the replacement window.
2124 * @param all_option Whether to add an 'all types' item.
2125 * @return The populated and sorted #DropDownList.
2127 DropDownList *GetRailTypeDropDownList(bool for_replacement, bool all_option)
2129 RailTypes used_railtypes = RAILTYPES_NONE;
2131 /* Find the used railtypes. */
2132 Engine *e;
2133 FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
2134 if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue;
2136 used_railtypes |= GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes;
2139 /* Get the date introduced railtypes as well. */
2140 used_railtypes = AddDateIntroducedRailTypes(used_railtypes, MAX_DAY);
2142 const Company *c = Company::Get(_local_company);
2143 DropDownList *list = new DropDownList();
2145 if (all_option) {
2146 DropDownListStringItem *item = new DropDownListStringItem(STR_REPLACE_ALL_RAILTYPE, INVALID_RAILTYPE, false);
2147 *list->Append() = item;
2150 RailType rt;
2151 FOR_ALL_SORTED_RAILTYPES(rt) {
2152 /* If it's not used ever, don't show it to the user. */
2153 if (!HasBit(used_railtypes, rt)) continue;
2155 const RailtypeInfo *rti = GetRailTypeInfo(rt);
2157 StringID str = for_replacement ? rti->strings.replace_text : (rti->max_speed > 0 ? STR_TOOLBAR_RAILTYPE_VELOCITY : STR_JUST_STRING);
2158 DropDownListParamStringItem *item = new DropDownListParamStringItem(str, rt, !HasBit(c->avail_railtypes, rt));
2159 item->SetParam(0, rti->strings.menu_text);
2160 item->SetParam(1, rti->max_speed);
2161 *list->Append() = item;
2164 if (list->Length() == 0) {
2165 /* Empty dropdowns are not allowed */
2166 *list->Append() = new DropDownListStringItem(STR_NONE, INVALID_RAILTYPE, true);
2169 return list;