Rearrange storage of reserved tracks for railway tiles
[openttd/fttd.git] / src / rail_gui.cpp
blob4c04a015e1e188be7435e9ae690bb34adc77b252
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 "tile_cmd.h"
38 #include "station_func.h"
39 #include "rail.h"
41 #include "map/rail.h"
42 #include "map/depot.h"
43 #include "map/bridge.h"
45 #include "widgets/rail_widget.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 /* Map the setting: default_signal_type to the corresponding signal type */
58 static const SignalType _default_signal_type[] = {SIGTYPE_NORMAL, SIGTYPE_PBS, SIGTYPE_PBS_ONEWAY};
60 struct RailStationGUISettings {
61 Axis orientation; ///< Currently selected rail station orientation
63 bool newstations; ///< Are custom station definitions available?
64 StationClassID station_class; ///< Currently selected custom station class (if newstations is \c true )
65 byte station_type; ///< %Station type within the currently selected custom station class (if newstations is \c true )
66 byte station_count; ///< Number of custom stations (if newstations is \c true )
68 static RailStationGUISettings _railstation; ///< Settings of the station builder GUI
71 static void HandleStationPlacement(TileIndex start, TileIndex end);
72 static void ShowBuildTrainDepotPicker(Window *parent);
73 static void ShowBuildWaypointPicker(Window *parent);
74 static void ShowStationBuilder(Window *parent);
75 static void ShowSignalBuilder(Window *parent);
77 /**
78 * Check whether a station type can be build.
79 * @return true if building is allowed.
81 static bool IsStationAvailable(const StationSpec *statspec)
83 if (statspec == NULL || !HasBit(statspec->callback_mask, CBM_STATION_AVAIL)) return true;
85 uint16 cb_res = GetStationCallback(CBID_STATION_AVAILABILITY, 0, 0, statspec, NULL, INVALID_TILE);
86 if (cb_res == CALLBACK_FAILED) return true;
88 return Convert8bitBooleanCallback(statspec->grf_prop.grffile, CBID_STATION_AVAILABILITY, cb_res);
91 void CcPlaySound1E(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
93 if (result.Succeeded() && _settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_2, tile);
96 void CcRailDepot(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
98 struct PlaceDepotExtraData {
99 DiagDirection dir; ///< direction to check
100 Track track; ///< track to build
103 static const PlaceDepotExtraData place_depot_extra_data[4][3] = {
104 { {DIAGDIR_SE, TRACK_LEFT }, {DIAGDIR_SW, TRACK_X}, {DIAGDIR_NW, TRACK_LOWER} },
105 { {DIAGDIR_SW, TRACK_UPPER}, {DIAGDIR_NW, TRACK_Y}, {DIAGDIR_NE, TRACK_LEFT } },
106 { {DIAGDIR_SE, TRACK_UPPER}, {DIAGDIR_NE, TRACK_X}, {DIAGDIR_NW, TRACK_RIGHT} },
107 { {DIAGDIR_SW, TRACK_RIGHT}, {DIAGDIR_SE, TRACK_Y}, {DIAGDIR_NE, TRACK_LOWER} },
110 if (result.Failed()) return;
112 DiagDirection dir = (DiagDirection)p2;
114 if (_settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_2, tile);
115 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
117 tile += TileOffsByDiagDir(dir);
119 if (IsNormalRailTile(tile) && !HasSignalOnTrack(tile, TRACK_UPPER) && !HasSignalOnTrack(tile, TRACK_LOWER)) {
120 for (int i = 0; i < 3; i++) {
121 if ((GetTrackBits(tile) & DiagdirReachesTracks(place_depot_extra_data[dir][i].dir)) != TRACK_BIT_NONE) {
122 DoCommandP(tile, _cur_railtype, place_depot_extra_data[dir][i].track, CMD_BUILD_SINGLE_RAIL);
129 * Place a rail waypoint.
130 * @param tile Position to start dragging a waypoint.
132 static void PlaceRail_Waypoint(TileIndex tile)
134 if (_remove_button_clicked) {
135 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_REMOVE_STATION);
136 return;
139 Axis axis = GetAxisForNewWaypoint(tile);
140 if (IsValidAxis(axis)) {
141 /* Valid tile for waypoints */
142 VpStartPlaceSizing(tile, axis == AXIS_X ? VPM_FIX_X : VPM_FIX_Y, DDSP_BUILD_STATION);
143 } else {
144 /* Tile where we can't build rail waypoints. This is always going to fail,
145 * but provides the user with a proper error message. */
146 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));
150 void CcStation(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
152 if (result.Failed()) return;
154 if (_settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_2, tile);
155 /* Only close the station builder window if the default station and non persistent building is chosen. */
156 if (_railstation.station_class == STAT_CLASS_DFLT && _railstation.station_type == 0 && !_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
160 * Place a rail station.
161 * @param tile Position to place or start dragging a station.
163 static void PlaceRail_Station(TileIndex tile)
165 if (_remove_button_clicked) {
166 VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED, DDSP_REMOVE_STATION);
167 VpSetPlaceSizingLimit(-1);
168 } else if (_settings_client.gui.station_dragdrop) {
169 VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED, DDSP_BUILD_STATION);
170 VpSetPlaceSizingLimit(_settings_game.station.station_spread);
171 } else {
172 uint32 p1 = _cur_railtype | _railstation.orientation << 4 | _settings_client.gui.station_numtracks << 8 | _settings_client.gui.station_platlength << 16 | _ctrl_pressed << 24;
173 uint32 p2 = _railstation.station_class | _railstation.station_type << 8 | INVALID_STATION << 16;
175 int w = _settings_client.gui.station_numtracks;
176 int h = _settings_client.gui.station_platlength;
177 if (!_railstation.orientation) Swap(w, h);
179 CommandContainer cmdcont = { tile, p1, p2, CMD_BUILD_RAIL_STATION | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION), CcStation, "" };
180 ShowSelectStationIfNeeded(cmdcont, TileArea(tile, w, h));
185 * Build a new signal or edit/remove a present signal, use CmdBuildSingleSignal() or CmdRemoveSingleSignal() in rail_cmd.cpp
187 * @param tile The tile where the signal will build or edit
189 static void GenericPlaceSignals(TileIndex tile)
191 TrackBits trackbits = TrackStatusToTrackBits(GetTileRailwayStatus(tile));
193 if (trackbits & TRACK_BIT_VERT) { // N-S direction
194 trackbits = (_tile_fract_coords.x <= _tile_fract_coords.y) ? TRACK_BIT_RIGHT : TRACK_BIT_LEFT;
197 if (trackbits & TRACK_BIT_HORZ) { // E-W direction
198 trackbits = (_tile_fract_coords.x + _tile_fract_coords.y <= 15) ? TRACK_BIT_UPPER : TRACK_BIT_LOWER;
201 Track track = FindFirstTrack(trackbits);
203 if (_remove_button_clicked) {
204 DoCommandP(tile, track, 0, CMD_REMOVE_SIGNALS | CMD_MSG(STR_ERROR_CAN_T_REMOVE_SIGNALS_FROM), CcPlaySound1E);
205 } else {
206 const Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
208 /* Map the setting cycle_signal_types to the allowed signal bitmask. */
209 static const uint signal_mask[] = {
210 (1 << SIGTYPE_PBS) - 1,
211 (1 << SIGTYPE_END) - (1 << SIGTYPE_PBS),
212 (1 << SIGTYPE_END) - 1,
215 /* various bitstuffed elements for CmdBuildSingleSignal() */
216 uint32 p1 = track;
218 if (w == NULL) {
219 /* signal GUI not used */
220 SB(p1, 4, 1, ((_cur_year < _settings_client.gui.semaphore_build_before) ^ _ctrl_pressed) ? SIG_SEMAPHORE : SIG_ELECTRIC);
221 SB(p1, 5, 3, _default_signal_type[_settings_client.gui.default_signal_type]);
222 SB(p1, 17, 3, _ctrl_pressed ? SIGNALS_CYCLE_TYPE : SIGNALS_BUILD);
223 } else if (!_convert_signal_button) {
224 /* signal GUI is used, not converting */
225 SB(p1, 4, 1, _cur_signal_variant ^ _ctrl_pressed);
226 SB(p1, 5, 3, _cur_signal_type);
227 SB(p1, 17, 3, _ctrl_pressed ? SIGNALS_CYCLE_TYPE : SIGNALS_BUILD);
228 } else {
229 /* signal GUI is used, converting */
230 SB(p1, 4, 1, _cur_signal_variant ^ _ctrl_pressed);
231 SB(p1, 5, 3, _cur_signal_type);
232 SB(p1, 17, 3, _ctrl_pressed ? SIGNALS_TOGGLE_VARIANT : SIGNALS_CONVERT);
235 DoCommandP(tile, p1, signal_mask[_settings_client.gui.cycle_signal_types], CMD_BUILD_SIGNALS |
236 CMD_MSG((w != NULL && _convert_signal_button) ? STR_ERROR_SIGNAL_CAN_T_CONVERT_SIGNALS_HERE : STR_ERROR_CAN_T_BUILD_SIGNALS_HERE),
237 CcPlaySound1E);
242 * Start placing a rail bridge.
243 * @param tile Position of the first tile of the bridge.
244 * @param w Rail toolbar window.
246 static void PlaceRail_Bridge(TileIndex tile, Window *w)
248 if (IsBridgeHeadTile(tile)) {
249 TileIndex other_tile = GetOtherBridgeEnd(tile);
250 Point pt = {0, 0};
251 w->OnPlaceMouseUp(VPM_X_OR_Y, DDSP_BUILD_BRIDGE, pt, other_tile, tile);
252 } else {
253 VpStartPlaceSizing(tile, VPM_X_OR_Y, DDSP_BUILD_BRIDGE);
257 /** Command callback for building a tunnel */
258 void CcBuildRailTunnel(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
260 if (result.Succeeded()) {
261 if (_settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_2, tile);
262 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
263 } else {
264 SetRedErrorSquare(_build_tunnel_endtile);
269 * Toggles state of the Remove button of Build rail toolbar
270 * @param w window the button belongs to
272 static void ToggleRailButton_Remove(Window *w)
274 DeleteWindowById(WC_SELECT_STATION, 0);
275 w->ToggleWidgetLoweredState(WID_RAT_REMOVE);
276 w->SetWidgetDirty(WID_RAT_REMOVE);
277 _remove_button_clicked = w->IsWidgetLowered(WID_RAT_REMOVE);
278 SetSelectionRed(_remove_button_clicked);
282 * Updates the Remove button because of Ctrl state change
283 * @param w window the button belongs to
284 * @return true iff the remove button was changed
286 static bool RailToolbar_CtrlChanged(Window *w)
288 if (w->IsWidgetDisabled(WID_RAT_REMOVE)) return false;
290 /* allow ctrl to switch remove mode only for these widgets */
291 for (uint i = WID_RAT_BUILD_NS; i <= WID_RAT_BUILD_STATION; i++) {
292 if ((i <= WID_RAT_AUTORAIL || i >= WID_RAT_BUILD_WAYPOINT) && w->IsWidgetLowered(i)) {
293 ToggleRailButton_Remove(w);
294 return true;
298 return false;
303 * The "remove"-button click proc of the build-rail toolbar.
304 * @param w Build-rail toolbar window
305 * @see BuildRailToolbarWindow::OnClick()
307 static void BuildRailClick_Remove(Window *w)
309 if (w->IsWidgetDisabled(WID_RAT_REMOVE)) return;
310 ToggleRailButton_Remove(w);
311 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
313 /* handle station builder */
314 if (w->IsWidgetLowered(WID_RAT_BUILD_STATION)) {
315 if (_remove_button_clicked) {
316 /* starting drag & drop remove */
317 if (!_settings_client.gui.station_dragdrop) {
318 SetTileSelectSize(1, 1);
319 } else {
320 VpSetPlaceSizingLimit(-1);
322 } else {
323 /* starting station build mode */
324 if (!_settings_client.gui.station_dragdrop) {
325 int x = _settings_client.gui.station_numtracks;
326 int y = _settings_client.gui.station_platlength;
327 if (_railstation.orientation == 0) Swap(x, y);
328 SetTileSelectSize(x, y);
329 } else {
330 VpSetPlaceSizingLimit(_settings_game.station.station_spread);
336 static void HandleAutodirPlacement()
338 TileIndex endtile = TileVirtXY(_thd.selend.x, _thd.selend.y);
339 Track track = (Track)(_thd.drawstyle & HT_DIR_MASK); // 0..5
341 if (_thd.drawstyle & HT_RAIL) { // one tile case
342 DoCommandP(endtile, _cur_railtype, track,
343 _remove_button_clicked ?
344 CMD_REMOVE_SINGLE_RAIL | CMD_MSG(STR_ERROR_CAN_T_REMOVE_RAILROAD_TRACK) :
345 CMD_BUILD_SINGLE_RAIL | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_TRACK),
346 CcPlaySound1E);
347 return;
350 DoCommandP(TileVirtXY(_thd.selstart.x, _thd.selstart.y), endtile, _cur_railtype | (track << 4),
351 _remove_button_clicked ?
352 CMD_REMOVE_RAILROAD_TRACK | CMD_MSG(STR_ERROR_CAN_T_REMOVE_RAILROAD_TRACK) :
353 CMD_BUILD_RAILROAD_TRACK | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_TRACK),
354 CcPlaySound1E);
358 * Build new signals or remove signals or (if only one tile marked) edit a signal.
360 * If one tile marked abort and use GenericPlaceSignals()
361 * else use CmdBuildSingleSignal() or CmdRemoveSingleSignal() in rail_cmd.cpp to build many signals
363 static void HandleAutoSignalPlacement()
365 uint32 p2 = GB(_thd.drawstyle, 0, 3); // 0..5
367 if ((_thd.drawstyle & HT_DRAG_MASK) == HT_RECT) { // one tile case
368 GenericPlaceSignals(TileVirtXY(_thd.selend.x, _thd.selend.y));
369 return;
372 const Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
374 if (w != NULL) {
375 /* signal GUI is used */
376 SB(p2, 4, 1, _cur_signal_variant);
377 SB(p2, 6, 1, _ctrl_pressed);
378 SB(p2, 7, 3, _cur_signal_type);
379 SB(p2, 24, 8, _settings_client.gui.drag_signals_density);
380 SB(p2, 10, 1, !_settings_client.gui.drag_signals_fixed_distance);
381 } else {
382 SB(p2, 4, 1, (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC));
383 SB(p2, 6, 1, _ctrl_pressed);
384 SB(p2, 7, 3, _default_signal_type[_settings_client.gui.default_signal_type]);
385 SB(p2, 24, 8, _settings_client.gui.drag_signals_density);
386 SB(p2, 10, 1, !_settings_client.gui.drag_signals_fixed_distance);
389 /* _settings_client.gui.drag_signals_density is given as a parameter such that each user
390 * in a network game can specify his/her own signal density */
391 DoCommandP(TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y), p2,
392 _remove_button_clicked ?
393 CMD_REMOVE_SIGNAL_TRACK | CMD_MSG(STR_ERROR_CAN_T_REMOVE_SIGNALS_FROM) :
394 CMD_BUILD_SIGNAL_TRACK | CMD_MSG(STR_ERROR_CAN_T_BUILD_SIGNALS_HERE),
395 CcPlaySound1E);
399 /** Rail toolbar management class. */
400 struct BuildRailToolbarWindow : Window {
401 RailType railtype; ///< Rail type to build.
402 int last_user_action; ///< Last started user action.
404 BuildRailToolbarWindow(WindowDesc *desc, RailType railtype) : Window(desc)
406 this->InitNested(TRANSPORT_RAIL);
407 this->SetupRailToolbar(railtype);
408 this->DisableWidget(WID_RAT_REMOVE);
409 this->last_user_action = WIDGET_LIST_END;
411 if (_settings_client.gui.link_terraform_toolbar) ShowTerraformToolbar(this);
414 ~BuildRailToolbarWindow()
416 if (_settings_client.gui.link_terraform_toolbar) DeleteWindowById(WC_SCEN_LAND_GEN, 0, false);
420 * Configures the rail toolbar for railtype given
421 * @param railtype the railtype to display
423 void SetupRailToolbar(RailType railtype)
425 this->railtype = railtype;
426 const RailtypeInfo *rti = GetRailTypeInfo(railtype);
428 assert(railtype < RAILTYPE_END);
429 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_NS)->widget_data = rti->gui_sprites.build_ns_rail;
430 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_X)->widget_data = rti->gui_sprites.build_x_rail;
431 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_EW)->widget_data = rti->gui_sprites.build_ew_rail;
432 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_Y)->widget_data = rti->gui_sprites.build_y_rail;
433 this->GetWidget<NWidgetCore>(WID_RAT_AUTORAIL)->widget_data = rti->gui_sprites.auto_rail;
434 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_DEPOT)->widget_data = rti->gui_sprites.build_depot;
435 this->GetWidget<NWidgetCore>(WID_RAT_CONVERT_RAIL)->widget_data = rti->gui_sprites.convert_rail;
436 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_TUNNEL)->widget_data = rti->gui_sprites.build_tunnel;
440 * Switch to another rail type.
441 * @param railtype New rail type.
443 void ModifyRailType(RailType railtype)
445 this->SetupRailToolbar(railtype);
446 this->ReInit();
449 void UpdateRemoveWidgetStatus(int clicked_widget)
451 switch (clicked_widget) {
452 case WID_RAT_REMOVE:
453 /* If it is the removal button that has been clicked, do nothing,
454 * as it is up to the other buttons to drive removal status */
455 return;
457 case WID_RAT_BUILD_NS:
458 case WID_RAT_BUILD_X:
459 case WID_RAT_BUILD_EW:
460 case WID_RAT_BUILD_Y:
461 case WID_RAT_AUTORAIL:
462 case WID_RAT_BUILD_WAYPOINT:
463 case WID_RAT_BUILD_STATION:
464 case WID_RAT_BUILD_SIGNALS:
465 /* Removal button is enabled only if the rail/signal/waypoint/station
466 * button is still lowered. Once raised, it has to be disabled */
467 this->SetWidgetDisabledState(WID_RAT_REMOVE, !this->IsWidgetLowered(clicked_widget));
468 break;
470 default:
471 /* When any other buttons than rail/signal/waypoint/station, raise and
472 * disable the removal button */
473 this->DisableWidget(WID_RAT_REMOVE);
474 this->RaiseWidget(WID_RAT_REMOVE);
475 break;
479 virtual void SetStringParameters(int widget) const
481 if (widget == WID_RAT_CAPTION) {
482 const RailtypeInfo *rti = GetRailTypeInfo(this->railtype);
483 if (rti->max_speed > 0) {
484 SetDParam(0, STR_TOOLBAR_RAILTYPE_VELOCITY);
485 SetDParam(1, rti->strings.toolbar_caption);
486 SetDParam(2, rti->max_speed);
487 } else {
488 SetDParam(0, rti->strings.toolbar_caption);
493 virtual void OnClick(Point pt, int widget, int click_count)
495 if (widget < WID_RAT_BUILD_NS) return;
497 _remove_button_clicked = false;
498 switch (widget) {
499 case WID_RAT_BUILD_NS:
500 HandlePlacePushButton(this, WID_RAT_BUILD_NS, GetRailTypeInfo(_cur_railtype)->cursor.rail_ns, HT_LINE | HT_DIR_VL);
501 this->last_user_action = widget;
502 break;
504 case WID_RAT_BUILD_X:
505 HandlePlacePushButton(this, WID_RAT_BUILD_X, GetRailTypeInfo(_cur_railtype)->cursor.rail_swne, HT_LINE | HT_DIR_X);
506 this->last_user_action = widget;
507 break;
509 case WID_RAT_BUILD_EW:
510 HandlePlacePushButton(this, WID_RAT_BUILD_EW, GetRailTypeInfo(_cur_railtype)->cursor.rail_ew, HT_LINE | HT_DIR_HL);
511 this->last_user_action = widget;
512 break;
514 case WID_RAT_BUILD_Y:
515 HandlePlacePushButton(this, WID_RAT_BUILD_Y, GetRailTypeInfo(_cur_railtype)->cursor.rail_nwse, HT_LINE | HT_DIR_Y);
516 this->last_user_action = widget;
517 break;
519 case WID_RAT_AUTORAIL:
520 HandlePlacePushButton(this, WID_RAT_AUTORAIL, GetRailTypeInfo(_cur_railtype)->cursor.autorail, HT_RAIL);
521 this->last_user_action = widget;
522 break;
524 case WID_RAT_DEMOLISH:
525 HandlePlacePushButton(this, WID_RAT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
526 this->last_user_action = widget;
527 break;
529 case WID_RAT_BUILD_DEPOT:
530 if (HandlePlacePushButton(this, WID_RAT_BUILD_DEPOT, GetRailTypeInfo(_cur_railtype)->cursor.depot, HT_RECT)) {
531 ShowBuildTrainDepotPicker(this);
532 this->last_user_action = widget;
534 break;
536 case WID_RAT_BUILD_WAYPOINT:
537 this->last_user_action = widget;
538 _waypoint_count = StationClass::Get(STAT_CLASS_WAYP)->GetSpecCount();
539 if (HandlePlacePushButton(this, WID_RAT_BUILD_WAYPOINT, SPR_CURSOR_WAYPOINT, HT_RECT) && _waypoint_count > 1) {
540 ShowBuildWaypointPicker(this);
542 break;
544 case WID_RAT_BUILD_STATION:
545 if (HandlePlacePushButton(this, WID_RAT_BUILD_STATION, SPR_CURSOR_RAIL_STATION, HT_RECT)) {
546 ShowStationBuilder(this);
547 this->last_user_action = widget;
549 break;
551 case WID_RAT_BUILD_SIGNALS: {
552 this->last_user_action = widget;
553 bool started = HandlePlacePushButton(this, WID_RAT_BUILD_SIGNALS, ANIMCURSOR_BUILDSIGNALS, HT_RECT);
554 if (started && _settings_client.gui.enable_signal_gui != _ctrl_pressed) {
555 ShowSignalBuilder(this);
557 break;
560 case WID_RAT_BUILD_BRIDGE:
561 HandlePlacePushButton(this, WID_RAT_BUILD_BRIDGE, SPR_CURSOR_BRIDGE, HT_RECT);
562 this->last_user_action = widget;
563 break;
565 case WID_RAT_BUILD_TUNNEL:
566 HandlePlacePushButton(this, WID_RAT_BUILD_TUNNEL, GetRailTypeInfo(_cur_railtype)->cursor.tunnel, HT_SPECIAL);
567 this->last_user_action = widget;
568 break;
570 case WID_RAT_REMOVE:
571 BuildRailClick_Remove(this);
572 break;
574 case WID_RAT_CONVERT_RAIL:
575 HandlePlacePushButton(this, WID_RAT_CONVERT_RAIL, GetRailTypeInfo(_cur_railtype)->cursor.convert, HT_RECT | HT_DIAGONAL);
576 this->last_user_action = widget;
577 break;
579 default: NOT_REACHED();
581 this->UpdateRemoveWidgetStatus(widget);
582 if (_ctrl_pressed) RailToolbar_CtrlChanged(this);
585 virtual EventState OnHotkey(int hotkey)
587 MarkTileDirtyByTile(TileVirtXY(_thd.pos.x, _thd.pos.y)); // redraw tile selection
588 return Window::OnHotkey(hotkey);
591 virtual void OnPlaceObject(Point pt, TileIndex tile)
593 switch (this->last_user_action) {
594 case WID_RAT_BUILD_NS:
595 VpStartPlaceSizing(tile, VPM_FIX_VERTICAL | VPM_RAILDIRS, DDSP_PLACE_RAIL);
596 break;
598 case WID_RAT_BUILD_X:
599 VpStartPlaceSizing(tile, VPM_FIX_Y | VPM_RAILDIRS, DDSP_PLACE_RAIL);
600 break;
602 case WID_RAT_BUILD_EW:
603 VpStartPlaceSizing(tile, VPM_FIX_HORIZONTAL | VPM_RAILDIRS, DDSP_PLACE_RAIL);
604 break;
606 case WID_RAT_BUILD_Y:
607 VpStartPlaceSizing(tile, VPM_FIX_X | VPM_RAILDIRS, DDSP_PLACE_RAIL);
608 break;
610 case WID_RAT_AUTORAIL:
611 VpStartPlaceSizing(tile, VPM_RAILDIRS, DDSP_PLACE_RAIL);
612 break;
614 case WID_RAT_DEMOLISH:
615 PlaceProc_DemolishArea(tile);
616 break;
618 case WID_RAT_BUILD_DEPOT:
619 DoCommandP(tile, _cur_railtype, _build_depot_direction,
620 CMD_BUILD_TRAIN_DEPOT | CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_DEPOT),
621 CcRailDepot);
622 break;
624 case WID_RAT_BUILD_WAYPOINT:
625 PlaceRail_Waypoint(tile);
626 break;
628 case WID_RAT_BUILD_STATION:
629 PlaceRail_Station(tile);
630 break;
632 case WID_RAT_BUILD_SIGNALS:
633 VpStartPlaceSizing(tile, VPM_SIGNALDIRS, DDSP_BUILD_SIGNALS);
634 break;
636 case WID_RAT_BUILD_BRIDGE:
637 PlaceRail_Bridge(tile, this);
638 break;
640 case WID_RAT_BUILD_TUNNEL:
641 DoCommandP(tile, _cur_railtype | (TRANSPORT_RAIL << 8), 0, CMD_BUILD_TUNNEL | CMD_MSG(STR_ERROR_CAN_T_BUILD_TUNNEL_HERE), CcBuildRailTunnel);
642 break;
644 case WID_RAT_CONVERT_RAIL:
645 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CONVERT_RAIL);
646 break;
648 default: NOT_REACHED();
652 virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
654 /* no dragging if you have pressed the convert button */
655 if (FindWindowById(WC_BUILD_SIGNAL, 0) != NULL && _convert_signal_button && this->IsWidgetLowered(WID_RAT_BUILD_SIGNALS)) return;
657 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
660 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
662 if (pt.x != -1) {
663 switch (select_proc) {
664 default: NOT_REACHED();
665 case DDSP_BUILD_BRIDGE:
666 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
667 ShowBuildBridgeWindow(start_tile, end_tile, TRANSPORT_RAIL, _cur_railtype);
668 break;
670 case DDSP_PLACE_RAIL:
671 HandleAutodirPlacement();
672 break;
674 case DDSP_BUILD_SIGNALS:
675 HandleAutoSignalPlacement();
676 break;
678 case DDSP_DEMOLISH_AREA:
679 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
680 break;
682 case DDSP_CONVERT_RAIL:
683 DoCommandP(end_tile, start_tile, _cur_railtype | (_ctrl_pressed ? 0x10 : 0), CMD_CONVERT_RAIL | CMD_MSG(STR_ERROR_CAN_T_CONVERT_RAIL), CcPlaySound10);
684 break;
686 case DDSP_REMOVE_STATION:
687 case DDSP_BUILD_STATION:
688 if (this->IsWidgetLowered(WID_RAT_BUILD_STATION)) {
689 /* Station */
690 if (_remove_button_clicked) {
691 DoCommandP(end_tile, start_tile, _ctrl_pressed ? 0 : 1, CMD_REMOVE_FROM_RAIL_STATION | CMD_MSG(STR_ERROR_CAN_T_REMOVE_PART_OF_STATION), CcPlaySound1E);
692 } else {
693 HandleStationPlacement(start_tile, end_tile);
695 } else {
696 /* Waypoint */
697 if (_remove_button_clicked) {
698 DoCommandP(end_tile, start_tile, _ctrl_pressed ? 0 : 1, CMD_REMOVE_FROM_RAIL_WAYPOINT | CMD_MSG(STR_ERROR_CAN_T_REMOVE_TRAIN_WAYPOINT), CcPlaySound1E);
699 } else {
700 TileArea ta(start_tile, end_tile);
701 uint32 p1 = _cur_railtype | (select_method == VPM_FIX_X ? AXIS_X : AXIS_Y) << 4 | ta.w << 8 | ta.h << 16 | _ctrl_pressed << 24;
702 uint32 p2 = STAT_CLASS_WAYP | _cur_waypoint_type << 8 | INVALID_STATION << 16;
704 CommandContainer cmdcont = { ta.tile, p1, p2, CMD_BUILD_RAIL_WAYPOINT | CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_WAYPOINT), CcPlaySound1E, "" };
705 ShowSelectWaypointIfNeeded(cmdcont, ta);
708 break;
713 virtual void OnPlaceObjectAbort()
715 this->RaiseButtons();
716 this->DisableWidget(WID_RAT_REMOVE);
717 this->SetWidgetDirty(WID_RAT_REMOVE);
719 DeleteWindowById(WC_BUILD_SIGNAL, TRANSPORT_RAIL);
720 DeleteWindowById(WC_BUILD_STATION, TRANSPORT_RAIL);
721 DeleteWindowById(WC_BUILD_DEPOT, TRANSPORT_RAIL);
722 DeleteWindowById(WC_BUILD_WAYPOINT, TRANSPORT_RAIL);
723 DeleteWindowById(WC_SELECT_STATION, 0);
724 DeleteWindowByClass(WC_BUILD_BRIDGE);
727 virtual void OnPlacePresize(Point pt, TileIndex tile)
729 DoCommand(tile, _cur_railtype | (TRANSPORT_RAIL << 8), 0, DC_AUTO, CMD_BUILD_TUNNEL);
730 VpSetPresizeRange(tile, _build_tunnel_endtile == 0 ? tile : _build_tunnel_endtile);
733 virtual EventState OnCTRLStateChange()
735 /* do not toggle Remove button by Ctrl when placing station */
736 if (!this->IsWidgetLowered(WID_RAT_BUILD_STATION) && !this->IsWidgetLowered(WID_RAT_BUILD_WAYPOINT) && RailToolbar_CtrlChanged(this)) return ES_HANDLED;
737 return ES_NOT_HANDLED;
740 static HotkeyList hotkeys;
744 * Handler for global hotkeys of the BuildRailToolbarWindow.
745 * @param hotkey Hotkey
746 * @return ES_HANDLED if hotkey was accepted.
748 static EventState RailToolbarGlobalHotkeys(int hotkey)
750 if (_game_mode != GM_NORMAL || !CanBuildVehicleInfrastructure(VEH_TRAIN)) return ES_NOT_HANDLED;
751 extern RailType _last_built_railtype;
752 Window *w = ShowBuildRailToolbar(_last_built_railtype);
753 if (w == NULL) return ES_NOT_HANDLED;
754 return w->OnHotkey(hotkey);
757 const uint16 _railtoolbar_autorail_keys[] = {'5', 'A' | WKC_GLOBAL_HOTKEY, 0};
759 static Hotkey railtoolbar_hotkeys[] = {
760 Hotkey('1', "build_ns", WID_RAT_BUILD_NS),
761 Hotkey('2', "build_x", WID_RAT_BUILD_X),
762 Hotkey('3', "build_ew", WID_RAT_BUILD_EW),
763 Hotkey('4', "build_y", WID_RAT_BUILD_Y),
764 Hotkey(_railtoolbar_autorail_keys, "autorail", WID_RAT_AUTORAIL),
765 Hotkey('6', "demolish", WID_RAT_DEMOLISH),
766 Hotkey('7', "depot", WID_RAT_BUILD_DEPOT),
767 Hotkey('8', "waypoint", WID_RAT_BUILD_WAYPOINT),
768 Hotkey('9', "station", WID_RAT_BUILD_STATION),
769 Hotkey('S', "signal", WID_RAT_BUILD_SIGNALS),
770 Hotkey('B', "bridge", WID_RAT_BUILD_BRIDGE),
771 Hotkey('T', "tunnel", WID_RAT_BUILD_TUNNEL),
772 Hotkey('R', "remove", WID_RAT_REMOVE),
773 Hotkey('C', "convert", WID_RAT_CONVERT_RAIL),
774 HOTKEY_LIST_END
776 HotkeyList BuildRailToolbarWindow::hotkeys("railtoolbar", railtoolbar_hotkeys, RailToolbarGlobalHotkeys);
778 static const NWidgetPart _nested_build_rail_widgets[] = {
779 NWidget(NWID_HORIZONTAL),
780 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
781 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_RAT_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
782 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
783 EndContainer(),
784 NWidget(NWID_HORIZONTAL),
785 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_NS),
786 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NS, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
787 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_X),
788 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NE, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
789 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_EW),
790 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_EW, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
791 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_Y),
792 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NW, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
793 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_AUTORAIL),
794 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_AUTORAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_AUTORAIL),
796 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), SetDataTip(0x0, STR_NULL), EndContainer(),
798 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_DEMOLISH),
799 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
800 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_DEPOT),
801 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DEPOT_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_TRAIN_DEPOT_FOR_BUILDING),
802 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_WAYPOINT),
803 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_WAYPOINT, STR_RAIL_TOOLBAR_TOOLTIP_CONVERT_RAIL_TO_WAYPOINT),
804 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_STATION),
805 SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_RAIL_STATION, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_STATION),
806 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_SIGNALS),
807 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_SIGNALS, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_SIGNALS),
808 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_BRIDGE),
809 SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_BRIDGE, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_BRIDGE),
810 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_TUNNEL),
811 SetFill(0, 1), SetMinimalSize(20, 22), SetDataTip(SPR_IMG_TUNNEL_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TUNNEL),
812 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_REMOVE),
813 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_REMOVE, STR_RAIL_TOOLBAR_TOOLTIP_TOGGLE_BUILD_REMOVE_FOR),
814 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_CONVERT_RAIL),
815 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_CONVERT_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_CONVERT_RAIL),
816 EndContainer(),
819 static WindowDesc _build_rail_desc(
820 WDP_ALIGN_TOOLBAR, "toolbar_rail", 0, 0,
821 WC_BUILD_TOOLBAR, WC_NONE,
822 WDF_CONSTRUCTION,
823 _nested_build_rail_widgets, lengthof(_nested_build_rail_widgets),
824 &BuildRailToolbarWindow::hotkeys
829 * Open the build rail toolbar window for a specific rail type.
831 * If the terraform toolbar is linked to the toolbar, that window is also opened.
833 * @param railtype Rail type to open the window for
834 * @return newly opened rail toolbar, or NULL if the toolbar could not be opened.
836 Window *ShowBuildRailToolbar(RailType railtype)
838 if (!Company::IsValidID(_local_company)) return NULL;
839 if (!ValParamRailtype(railtype)) return NULL;
841 DeleteWindowByClass(WC_BUILD_TOOLBAR);
842 _cur_railtype = railtype;
843 _remove_button_clicked = false;
844 return new BuildRailToolbarWindow(&_build_rail_desc, railtype);
847 /* TODO: For custom stations, respect their allowed platforms/lengths bitmasks!
848 * --pasky */
850 static void HandleStationPlacement(TileIndex start, TileIndex end)
852 TileArea ta(start, end);
853 uint numtracks = ta.w;
854 uint platlength = ta.h;
856 if (_railstation.orientation == AXIS_X) Swap(numtracks, platlength);
858 uint32 p1 = _cur_railtype | _railstation.orientation << 4 | numtracks << 8 | platlength << 16 | _ctrl_pressed << 24;
859 uint32 p2 = _railstation.station_class | _railstation.station_type << 8 | INVALID_STATION << 16;
861 CommandContainer cmdcont = { ta.tile, p1, p2, CMD_BUILD_RAIL_STATION | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION), CcStation, "" };
862 ShowSelectStationIfNeeded(cmdcont, ta);
865 struct BuildRailStationWindow : public PickerWindowBase {
866 private:
867 uint line_height; ///< Height of a single line in the newstation selection matrix (#WID_BRAS_NEWST_LIST widget).
868 uint coverage_height; ///< Height of the coverage texts.
869 Scrollbar *vscroll; ///< Vertical scrollbar of the new station list.
870 Scrollbar *vscroll2; ///< Vertical scrollbar of the matrix with new stations.
873 * Verify whether the currently selected station size is allowed after selecting a new station class/type.
874 * If not, change the station size variables ( _settings_client.gui.station_numtracks and _settings_client.gui.station_platlength ).
875 * @param statspec Specification of the new station class/type
877 void CheckSelectedSize(const StationSpec *statspec)
879 if (statspec == NULL || _settings_client.gui.station_dragdrop) return;
881 /* If current number of tracks is not allowed, make it as big as possible (which is always less than currently selected) */
882 if (HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
883 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
884 _settings_client.gui.station_numtracks = 1;
885 while (HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
886 _settings_client.gui.station_numtracks++;
888 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
891 if (HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
892 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
893 _settings_client.gui.station_platlength = 1;
894 while (HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
895 _settings_client.gui.station_platlength++;
897 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
901 public:
902 BuildRailStationWindow(WindowDesc *desc, Window *parent, bool newstation) : PickerWindowBase(desc, parent)
904 this->coverage_height = 2 * FONT_HEIGHT_NORMAL + 3 * WD_PAR_VSEP_NORMAL;
905 this->vscroll = NULL;
906 _railstation.newstations = newstation;
908 this->CreateNestedTree();
909 NWidgetStacked *newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_ADDITIONS);
910 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
911 newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_MATRIX);
912 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
913 newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_DEFSIZE);
914 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
915 newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_RESIZE);
916 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
917 if (newstation) {
918 this->vscroll = this->GetScrollbar(WID_BRAS_NEWST_SCROLL);
919 this->vscroll2 = this->GetScrollbar(WID_BRAS_MATRIX_SCROLL);
921 this->FinishInitNested(TRANSPORT_RAIL);
923 this->LowerWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
924 if (_settings_client.gui.station_dragdrop) {
925 this->LowerWidget(WID_BRAS_PLATFORM_DRAG_N_DROP);
926 } else {
927 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
928 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
930 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_OFF, !_settings_client.gui.station_show_coverage);
931 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_ON, _settings_client.gui.station_show_coverage);
933 if (!newstation || _railstation.station_class >= (int)StationClass::GetClassCount()) {
934 /* New stations are not available or changed, so ensure the default station
935 * type is 'selected'. */
936 _railstation.station_class = STAT_CLASS_DFLT;
937 _railstation.station_type = 0;
938 this->vscroll2 = NULL;
940 if (newstation) {
941 _railstation.station_count = StationClass::Get(_railstation.station_class)->GetSpecCount();
942 _railstation.station_type = min(_railstation.station_type, _railstation.station_count - 1);
944 int count = 0;
945 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
946 if (i == STAT_CLASS_WAYP) continue;
947 count++;
949 this->vscroll->SetCount(count);
950 this->vscroll->SetPosition(Clamp(_railstation.station_class - 2, 0, max(this->vscroll->GetCount() - this->vscroll->GetCapacity(), 0)));
952 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX);
953 matrix->SetScrollbar(this->vscroll2);
954 matrix->SetCount(_railstation.station_count);
955 matrix->SetClicked(_railstation.station_type);
959 virtual ~BuildRailStationWindow()
961 DeleteWindowById(WC_SELECT_STATION, 0);
964 virtual void OnPaint()
966 bool newstations = _railstation.newstations;
967 const StationSpec *statspec = newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : NULL;
969 if (_settings_client.gui.station_dragdrop) {
970 SetTileSelectSize(1, 1);
971 } else {
972 int x = _settings_client.gui.station_numtracks;
973 int y = _settings_client.gui.station_platlength;
974 if (_railstation.orientation == AXIS_X) Swap(x, y);
975 if (!_remove_button_clicked) {
976 SetTileSelectSize(x, y);
980 int rad = (_settings_game.station.modified_catchment) ? CA_TRAIN : CA_UNMODIFIED;
982 if (_settings_client.gui.station_show_coverage) SetTileSelectBigSize(-rad, -rad, 2 * rad, 2 * rad);
984 for (uint bits = 0; bits < 7; bits++) {
985 bool disable = bits >= _settings_game.station.station_spread;
986 if (statspec == NULL) {
987 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_NUM_1, disable);
988 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_LEN_1, disable);
989 } else {
990 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_NUM_1, HasBit(statspec->disallowed_platforms, bits) || disable);
991 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_LEN_1, HasBit(statspec->disallowed_lengths, bits) || disable);
995 this->DrawWidgets();
997 /* 'Accepts' and 'Supplies' texts. */
998 NWidgetBase *cov = this->GetWidget<NWidgetBase>(WID_BRAS_COVERAGE_TEXTS);
999 int top = cov->pos_y + WD_PAR_VSEP_NORMAL;
1000 int left = cov->pos_x + WD_FRAMERECT_LEFT;
1001 int right = cov->pos_x + cov->current_x - WD_FRAMERECT_RIGHT;
1002 int bottom = cov->pos_y + cov->current_y;
1003 top = DrawStationCoverageAreaText(left, right, top, SCT_ALL, rad, false) + WD_PAR_VSEP_NORMAL;
1004 top = DrawStationCoverageAreaText(left, right, top, SCT_ALL, rad, true) + WD_PAR_VSEP_NORMAL;
1005 /* Resize background if the window is too small.
1006 * Never make the window smaller to avoid oscillating if the size change affects the acceptance.
1007 * (This is the case, if making the window bigger moves the mouse into the window.) */
1008 if (top > bottom) {
1009 this->coverage_height += top - bottom;
1010 this->ReInit();
1014 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1016 switch (widget) {
1017 case WID_BRAS_NEWST_LIST: {
1018 Dimension d = {0, 0};
1019 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1020 if (i == STAT_CLASS_WAYP) continue;
1021 SetDParam(0, StationClass::Get((StationClassID)i)->name);
1022 d = maxdim(d, GetStringBoundingBox(STR_BLACK_STRING));
1024 size->width = max(size->width, d.width + padding.width);
1025 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
1026 size->height = 5 * this->line_height;
1027 resize->height = this->line_height;
1028 break;
1031 case WID_BRAS_SHOW_NEWST_TYPE: {
1032 if (!_railstation.newstations) {
1033 size->width = 0;
1034 size->height = 0;
1035 break;
1038 /* If newstations exist, compute the non-zero minimal size. */
1039 Dimension d = {0, 0};
1040 StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
1041 for (StationClassID statclass = STAT_CLASS_BEGIN; statclass < (StationClassID)StationClass::GetClassCount(); statclass++) {
1042 if (statclass == STAT_CLASS_WAYP) continue;
1043 StationClass *stclass = StationClass::Get(statclass);
1044 for (uint16 j = 0; j < stclass->GetSpecCount(); j++) {
1045 const StationSpec *statspec = stclass->GetSpec(j);
1046 SetDParam(0, (statspec != NULL && statspec->name != 0) ? statspec->name : STR_STATION_CLASS_DFLT);
1047 d = maxdim(d, GetStringBoundingBox(str));
1050 size->width = max(size->width, d.width + padding.width);
1051 break;
1054 case WID_BRAS_COVERAGE_TEXTS:
1055 size->height = this->coverage_height;
1056 break;
1058 case WID_BRAS_MATRIX:
1059 fill->height = 1;
1060 resize->height = 1;
1061 break;
1065 virtual void DrawWidget(const Rect &r, int widget) const
1067 DrawPixelInfo tmp_dpi;
1069 switch (GB(widget, 0, 16)) {
1070 case WID_BRAS_PLATFORM_DIR_X:
1071 /* Set up a clipping area for the '/' station preview */
1072 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1073 DrawPixelInfo *old_dpi = _cur_dpi;
1074 _cur_dpi = &tmp_dpi;
1075 if (!DrawStationTile(32, 28, _cur_railtype, AXIS_X, _railstation.station_class, _railstation.station_type)) {
1076 StationPickerDrawSprite(32, 28, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2);
1078 _cur_dpi = old_dpi;
1080 break;
1082 case WID_BRAS_PLATFORM_DIR_Y:
1083 /* Set up a clipping area for the '\' station preview */
1084 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1085 DrawPixelInfo *old_dpi = _cur_dpi;
1086 _cur_dpi = &tmp_dpi;
1087 if (!DrawStationTile(32, 28, _cur_railtype, AXIS_Y, _railstation.station_class, _railstation.station_type)) {
1088 StationPickerDrawSprite(32, 28, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 3);
1090 _cur_dpi = old_dpi;
1092 break;
1094 case WID_BRAS_NEWST_LIST: {
1095 uint statclass = 0;
1096 uint row = 0;
1097 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1098 if (i == STAT_CLASS_WAYP) continue;
1099 if (this->vscroll->IsVisible(statclass)) {
1100 SetDParam(0, StationClass::Get((StationClassID)i)->name);
1101 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, row * this->line_height + r.top + WD_MATRIX_TOP, STR_JUST_STRING,
1102 (StationClassID)i == _railstation.station_class ? TC_WHITE : TC_BLACK);
1103 row++;
1105 statclass++;
1107 break;
1110 case WID_BRAS_IMAGE: {
1111 byte type = GB(widget, 16, 16);
1112 assert(type < _railstation.station_count);
1113 /* Check station availability callback */
1114 const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(type);
1115 if (!IsStationAvailable(statspec)) {
1116 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
1119 /* Set up a clipping area for the station preview. */
1120 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1121 DrawPixelInfo *old_dpi = _cur_dpi;
1122 _cur_dpi = &tmp_dpi;
1123 if (!DrawStationTile(32, 28, _cur_railtype, _railstation.orientation, _railstation.station_class, type)) {
1124 StationPickerDrawSprite(32, 28, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2 + _railstation.orientation);
1126 _cur_dpi = old_dpi;
1128 break;
1133 virtual void OnResize()
1135 if (this->vscroll != NULL) { // New stations available.
1136 this->vscroll->SetCapacityFromWidget(this, WID_BRAS_NEWST_LIST);
1140 virtual void SetStringParameters(int widget) const
1142 if (widget == WID_BRAS_SHOW_NEWST_TYPE) {
1143 const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type);
1144 SetDParam(0, (statspec != NULL && statspec->name != 0) ? statspec->name : STR_STATION_CLASS_DFLT);
1148 virtual void OnClick(Point pt, int widget, int click_count)
1150 switch (GB(widget, 0, 16)) {
1151 case WID_BRAS_PLATFORM_DIR_X:
1152 case WID_BRAS_PLATFORM_DIR_Y:
1153 this->RaiseWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
1154 _railstation.orientation = (Axis)(widget - WID_BRAS_PLATFORM_DIR_X);
1155 this->LowerWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
1156 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1157 this->SetDirty();
1158 DeleteWindowById(WC_SELECT_STATION, 0);
1159 break;
1161 case WID_BRAS_PLATFORM_NUM_1:
1162 case WID_BRAS_PLATFORM_NUM_2:
1163 case WID_BRAS_PLATFORM_NUM_3:
1164 case WID_BRAS_PLATFORM_NUM_4:
1165 case WID_BRAS_PLATFORM_NUM_5:
1166 case WID_BRAS_PLATFORM_NUM_6:
1167 case WID_BRAS_PLATFORM_NUM_7: {
1168 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1169 this->RaiseWidget(WID_BRAS_PLATFORM_DRAG_N_DROP);
1171 _settings_client.gui.station_numtracks = widget - WID_BRAS_PLATFORM_NUM_BEGIN;
1172 _settings_client.gui.station_dragdrop = false;
1174 _settings_client.gui.station_dragdrop = false;
1176 const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : NULL;
1177 if (statspec != NULL && HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
1178 /* The previously selected number of platforms in invalid */
1179 for (uint i = 0; i < 7; i++) {
1180 if (!HasBit(statspec->disallowed_lengths, i)) {
1181 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1182 _settings_client.gui.station_platlength = i + 1;
1183 break;
1188 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1189 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1190 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1191 this->SetDirty();
1192 DeleteWindowById(WC_SELECT_STATION, 0);
1193 break;
1196 case WID_BRAS_PLATFORM_LEN_1:
1197 case WID_BRAS_PLATFORM_LEN_2:
1198 case WID_BRAS_PLATFORM_LEN_3:
1199 case WID_BRAS_PLATFORM_LEN_4:
1200 case WID_BRAS_PLATFORM_LEN_5:
1201 case WID_BRAS_PLATFORM_LEN_6:
1202 case WID_BRAS_PLATFORM_LEN_7: {
1203 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1204 this->RaiseWidget(WID_BRAS_PLATFORM_DRAG_N_DROP);
1206 _settings_client.gui.station_platlength = widget - WID_BRAS_PLATFORM_LEN_BEGIN;
1207 _settings_client.gui.station_dragdrop = false;
1209 _settings_client.gui.station_dragdrop = false;
1211 const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : NULL;
1212 if (statspec != NULL && HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
1213 /* The previously selected number of tracks in invalid */
1214 for (uint i = 0; i < 7; i++) {
1215 if (!HasBit(statspec->disallowed_platforms, i)) {
1216 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1217 _settings_client.gui.station_numtracks = i + 1;
1218 break;
1223 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1224 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1225 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1226 this->SetDirty();
1227 DeleteWindowById(WC_SELECT_STATION, 0);
1228 break;
1231 case WID_BRAS_PLATFORM_DRAG_N_DROP: {
1232 _settings_client.gui.station_dragdrop ^= true;
1234 this->ToggleWidgetLoweredState(WID_BRAS_PLATFORM_DRAG_N_DROP);
1236 /* get the first allowed length/number of platforms */
1237 const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : NULL;
1238 if (statspec != NULL && HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
1239 for (uint i = 0; i < 7; i++) {
1240 if (!HasBit(statspec->disallowed_lengths, i)) {
1241 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1242 _settings_client.gui.station_platlength = i + 1;
1243 break;
1247 if (statspec != NULL && HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
1248 for (uint i = 0; i < 7; i++) {
1249 if (!HasBit(statspec->disallowed_platforms, i)) {
1250 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1251 _settings_client.gui.station_numtracks = i + 1;
1252 break;
1257 this->SetWidgetLoweredState(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN, !_settings_client.gui.station_dragdrop);
1258 this->SetWidgetLoweredState(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN, !_settings_client.gui.station_dragdrop);
1259 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1260 this->SetDirty();
1261 DeleteWindowById(WC_SELECT_STATION, 0);
1262 break;
1265 case WID_BRAS_HIGHLIGHT_OFF:
1266 case WID_BRAS_HIGHLIGHT_ON:
1267 _settings_client.gui.station_show_coverage = (widget != WID_BRAS_HIGHLIGHT_OFF);
1269 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_OFF, !_settings_client.gui.station_show_coverage);
1270 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_ON, _settings_client.gui.station_show_coverage);
1271 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1272 this->SetDirty();
1273 break;
1275 case WID_BRAS_NEWST_LIST: {
1276 int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_BRAS_NEWST_LIST, 0, this->line_height);
1277 if (y >= (int)StationClass::GetClassCount()) return;
1278 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1279 if (i == STAT_CLASS_WAYP) continue;
1280 if (y == 0) {
1281 if (_railstation.station_class != (StationClassID)i) {
1282 _railstation.station_class = (StationClassID)i;
1283 StationClass *stclass = StationClass::Get(_railstation.station_class);
1284 _railstation.station_count = stclass->GetSpecCount();
1285 _railstation.station_type = min((int)_railstation.station_type, max(0, (int)_railstation.station_count - 1));
1287 this->CheckSelectedSize(stclass->GetSpec(_railstation.station_type));
1289 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX);
1290 matrix->SetCount(_railstation.station_count);
1291 matrix->SetClicked(_railstation.station_type);
1293 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1294 this->SetDirty();
1295 DeleteWindowById(WC_SELECT_STATION, 0);
1296 break;
1298 y--;
1300 break;
1303 case WID_BRAS_IMAGE: {
1304 int y = GB(widget, 16, 16);
1305 if (y >= _railstation.station_count) return;
1307 /* Check station availability callback */
1308 const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(y);
1309 if (!IsStationAvailable(statspec)) return;
1311 _railstation.station_type = y;
1313 this->CheckSelectedSize(statspec);
1314 this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX)->SetClicked(_railstation.station_type);
1316 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1317 this->SetDirty();
1318 DeleteWindowById(WC_SELECT_STATION, 0);
1319 break;
1324 virtual void OnTick()
1326 CheckRedrawStationCoverage(this);
1330 static const NWidgetPart _nested_station_builder_widgets[] = {
1331 NWidget(NWID_HORIZONTAL),
1332 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1333 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_STATION_BUILD_RAIL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1334 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_DEFSIZE),
1335 NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
1336 EndContainer(),
1337 EndContainer(),
1338 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1339 NWidget(NWID_HORIZONTAL),
1340 NWidget(NWID_VERTICAL),
1341 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_ADDITIONS),
1342 NWidget(NWID_HORIZONTAL), SetPIP(7, 0, 7), SetPadding(2, 0, 1, 0),
1343 NWidget(WWT_MATRIX, COLOUR_GREY, WID_BRAS_NEWST_LIST), SetMinimalSize(122, 71), SetFill(1, 0),
1344 SetMatrixDataTip(1, 0, STR_STATION_BUILD_STATION_CLASS_TOOLTIP), SetScrollbar(WID_BRAS_NEWST_SCROLL),
1345 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_BRAS_NEWST_SCROLL),
1346 EndContainer(),
1347 EndContainer(),
1348 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_ORIENTATION, STR_NULL), SetPadding(1, 2, 0, 2),
1349 NWidget(NWID_HORIZONTAL),
1350 NWidget(NWID_SPACER), SetMinimalSize(7, 0), SetFill(1, 0),
1351 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(),
1352 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1353 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(),
1354 NWidget(NWID_SPACER), SetMinimalSize(7, 0), SetFill(1, 0),
1355 EndContainer(),
1356 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),
1357 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_NUMBER_OF_TRACKS, STR_NULL), SetPadding(0, 2, 0, 2),
1358 NWidget(NWID_HORIZONTAL),
1359 NWidget(NWID_SPACER), SetFill(1, 0),
1360 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_1), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_1, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1361 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_2), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_2, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1362 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_3), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_3, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1363 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_4), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_4, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1364 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_5), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_5, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1365 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_6), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_6, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1366 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_7), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_7, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1367 NWidget(NWID_SPACER), SetFill(1, 0),
1368 EndContainer(),
1369 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_PLATFORM_LENGTH, STR_NULL), SetPadding(2, 2, 0, 2),
1370 NWidget(NWID_HORIZONTAL),
1371 NWidget(NWID_SPACER), SetFill(1, 0),
1372 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_1), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_1, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1373 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_2), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_2, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1374 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_3), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_3, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1375 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_4), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_4, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1376 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_5), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_5, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1377 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_6), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_6, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1378 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_7), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_7, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1379 NWidget(NWID_SPACER), SetFill(1, 0),
1380 EndContainer(),
1381 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1382 NWidget(NWID_HORIZONTAL),
1383 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1384 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),
1385 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1386 EndContainer(),
1387 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_COVERAGE_AREA_TITLE, STR_NULL), SetPadding(3, 2, 0, 2),
1388 NWidget(NWID_HORIZONTAL),
1389 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1390 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_HIGHLIGHT_OFF), SetMinimalSize(60, 12),
1391 SetDataTip(STR_STATION_BUILD_COVERAGE_OFF, STR_STATION_BUILD_COVERAGE_AREA_OFF_TOOLTIP),
1392 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_HIGHLIGHT_ON), SetMinimalSize(60, 12),
1393 SetDataTip(STR_STATION_BUILD_COVERAGE_ON, STR_STATION_BUILD_COVERAGE_AREA_ON_TOOLTIP),
1394 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1395 EndContainer(),
1396 EndContainer(),
1397 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_MATRIX),
1398 /* We need an additional background for the matrix, as the matrix cannot handle the scrollbar due to not being an NWidgetCore. */
1399 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(WID_BRAS_MATRIX_SCROLL),
1400 NWidget(NWID_HORIZONTAL),
1401 NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BRAS_MATRIX), SetScrollbar(WID_BRAS_MATRIX_SCROLL), SetPIP(0, 2, 0), SetPadding(2, 0, 0, 0),
1402 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BRAS_IMAGE), SetMinimalSize(66, 60),
1403 SetFill(0, 0), SetResize(0, 0), SetDataTip(0x0, STR_STATION_BUILD_STATION_TYPE_TOOLTIP), SetScrollbar(WID_BRAS_MATRIX_SCROLL),
1404 EndContainer(),
1405 EndContainer(),
1406 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BRAS_MATRIX_SCROLL),
1407 EndContainer(),
1408 EndContainer(),
1409 EndContainer(),
1410 EndContainer(),
1411 NWidget(NWID_HORIZONTAL),
1412 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BRAS_COVERAGE_TEXTS), SetFill(1, 1), SetResize(1, 0),
1413 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_RESIZE),
1414 NWidget(NWID_VERTICAL),
1415 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(0, 1), EndContainer(),
1416 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
1417 EndContainer(),
1418 EndContainer(),
1419 EndContainer(),
1420 EndContainer(),
1423 /** High level window description of the station-build window (default & newGRF) */
1424 static WindowDesc _station_builder_desc(
1425 WDP_AUTO, "build_station_rail", 350, 0,
1426 WC_BUILD_STATION, WC_BUILD_TOOLBAR,
1427 WDF_CONSTRUCTION,
1428 _nested_station_builder_widgets, lengthof(_nested_station_builder_widgets)
1431 /** Open station build window */
1432 static void ShowStationBuilder(Window *parent)
1434 bool newstations = StationClass::GetClassCount() > 2 || StationClass::Get(STAT_CLASS_DFLT)->GetSpecCount() != 1;
1435 new BuildRailStationWindow(&_station_builder_desc, parent, newstations);
1438 struct BuildSignalWindow : public PickerWindowBase {
1439 private:
1440 Dimension sig_sprite_size; ///< Maximum size of signal GUI sprites.
1441 int sig_sprite_bottom_offset; ///< Maximum extent of signal GUI sprite from reference point towards bottom.
1444 * Draw dynamic a signal-sprite in a button in the signal GUI
1445 * Draw the sprite +1px to the right and down if the button is lowered
1447 * @param widget_index index of this widget in the window
1448 * @param image the sprite to draw
1450 void DrawSignalSprite(byte widget_index, SpriteID image) const
1452 Point offset;
1453 Dimension sprite_size = GetSpriteSize(image, &offset);
1454 const NWidgetBase *widget = this->GetWidget<NWidgetBase>(widget_index);
1455 int x = widget->pos_x - offset.x +
1456 (widget->current_x - sprite_size.width + offset.x) / 2; // centered
1457 int y = widget->pos_y - sig_sprite_bottom_offset + WD_IMGBTN_TOP +
1458 (widget->current_y - WD_IMGBTN_TOP - WD_IMGBTN_BOTTOM + sig_sprite_size.height) / 2; // aligned to bottom
1460 DrawSprite(image, PAL_NONE,
1461 x + this->IsWidgetLowered(widget_index),
1462 y + this->IsWidgetLowered(widget_index));
1465 public:
1466 BuildSignalWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1468 this->InitNested(TRANSPORT_RAIL);
1469 this->OnInvalidateData();
1472 ~BuildSignalWindow()
1474 _convert_signal_button = false;
1477 virtual void OnInit()
1479 /* Calculate maximum signal sprite size. */
1480 this->sig_sprite_size.width = 0;
1481 this->sig_sprite_size.height = 0;
1482 this->sig_sprite_bottom_offset = 0;
1483 const RailtypeInfo *rti = GetRailTypeInfo(_cur_railtype);
1484 for (uint type = SIGTYPE_NORMAL; type < SIGTYPE_END; type++) {
1485 for (uint variant = SIG_ELECTRIC; variant <= SIG_SEMAPHORE; variant++) {
1486 for (uint lowered = 0; lowered < 2; lowered++) {
1487 Point offset;
1488 Dimension sprite_size = GetSpriteSize(rti->gui_sprites.signals[type][variant][lowered], &offset);
1489 this->sig_sprite_bottom_offset = max<int>(this->sig_sprite_bottom_offset, sprite_size.height);
1490 this->sig_sprite_size.width = max<int>(this->sig_sprite_size.width, sprite_size.width - offset.x);
1491 this->sig_sprite_size.height = max<int>(this->sig_sprite_size.height, sprite_size.height - offset.y);
1497 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1499 if (widget == WID_BS_DRAG_SIGNALS_DENSITY_LABEL) {
1500 /* Two digits for signals density. */
1501 size->width = max(size->width, 2 * GetDigitWidth() + padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT);
1502 } else if (IsInsideMM(widget, WID_BS_SEMAPHORE_NORM, WID_BS_ELECTRIC_PBS_OWAY + 1)) {
1503 size->width = max(size->width, this->sig_sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT);
1504 size->height = max(size->height, this->sig_sprite_size.height + WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM);
1508 virtual void SetStringParameters(int widget) const
1510 switch (widget) {
1511 case WID_BS_DRAG_SIGNALS_DENSITY_LABEL:
1512 SetDParam(0, _settings_client.gui.drag_signals_density);
1513 break;
1517 virtual void DrawWidget(const Rect &r, int widget) const
1519 if (IsInsideMM(widget, WID_BS_SEMAPHORE_NORM, WID_BS_ELECTRIC_PBS_OWAY + 1)) {
1520 /* Extract signal from widget number. */
1521 int type = (widget - WID_BS_SEMAPHORE_NORM) % SIGTYPE_END;
1522 int var = SIG_SEMAPHORE - (widget - WID_BS_SEMAPHORE_NORM) / SIGTYPE_END; // SignalVariant order is reversed compared to the widgets.
1523 SpriteID sprite = GetRailTypeInfo(_cur_railtype)->gui_sprites.signals[type][var][this->IsWidgetLowered(widget)];
1525 this->DrawSignalSprite(widget, sprite);
1529 virtual void OnClick(Point pt, int widget, int click_count)
1531 switch (widget) {
1532 case WID_BS_SEMAPHORE_NORM:
1533 case WID_BS_SEMAPHORE_ENTRY:
1534 case WID_BS_SEMAPHORE_EXIT:
1535 case WID_BS_SEMAPHORE_COMBO:
1536 case WID_BS_SEMAPHORE_PBS:
1537 case WID_BS_SEMAPHORE_PBS_OWAY:
1538 case WID_BS_ELECTRIC_NORM:
1539 case WID_BS_ELECTRIC_ENTRY:
1540 case WID_BS_ELECTRIC_EXIT:
1541 case WID_BS_ELECTRIC_COMBO:
1542 case WID_BS_ELECTRIC_PBS:
1543 case WID_BS_ELECTRIC_PBS_OWAY:
1544 this->RaiseWidget((_cur_signal_variant == SIG_ELECTRIC ? WID_BS_ELECTRIC_NORM : WID_BS_SEMAPHORE_NORM) + _cur_signal_type);
1546 _cur_signal_type = (SignalType)((uint)((widget - WID_BS_SEMAPHORE_NORM) % (SIGTYPE_LAST + 1)));
1547 _cur_signal_variant = widget >= WID_BS_ELECTRIC_NORM ? SIG_ELECTRIC : SIG_SEMAPHORE;
1549 /* If 'remove' button of rail build toolbar is active, disable it. */
1550 if (_remove_button_clicked) {
1551 Window *w = FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL);
1552 if (w != NULL) ToggleRailButton_Remove(w);
1555 break;
1557 case WID_BS_CONVERT:
1558 _convert_signal_button = !_convert_signal_button;
1559 break;
1561 case WID_BS_DRAG_SIGNALS_DENSITY_DECREASE:
1562 if (_settings_client.gui.drag_signals_density > 1) {
1563 _settings_client.gui.drag_signals_density--;
1564 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_SETTINGS);
1566 break;
1568 case WID_BS_DRAG_SIGNALS_DENSITY_INCREASE:
1569 if (_settings_client.gui.drag_signals_density < 20) {
1570 _settings_client.gui.drag_signals_density++;
1571 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_SETTINGS);
1573 break;
1575 default: break;
1578 this->InvalidateData();
1582 * Some data on this window has become invalid.
1583 * @param data Information about the changed data.
1584 * @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.
1586 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1588 if (!gui_scope) return;
1589 this->LowerWidget((_cur_signal_variant == SIG_ELECTRIC ? WID_BS_ELECTRIC_NORM : WID_BS_SEMAPHORE_NORM) + _cur_signal_type);
1591 this->SetWidgetLoweredState(WID_BS_CONVERT, _convert_signal_button);
1593 this->SetWidgetDisabledState(WID_BS_DRAG_SIGNALS_DENSITY_DECREASE, _settings_client.gui.drag_signals_density == 1);
1594 this->SetWidgetDisabledState(WID_BS_DRAG_SIGNALS_DENSITY_INCREASE, _settings_client.gui.drag_signals_density == 20);
1598 /** Nested widget definition of the build signal window */
1599 static const NWidgetPart _nested_signal_builder_widgets[] = {
1600 NWidget(NWID_HORIZONTAL),
1601 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1602 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_BUILD_SIGNAL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1603 EndContainer(),
1604 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
1605 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1606 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_NORM), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_NORM_TOOLTIP), EndContainer(), SetFill(1, 1),
1607 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_ENTRY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_ENTRY_TOOLTIP), EndContainer(), SetFill(1, 1),
1608 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_EXIT), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_EXIT_TOOLTIP), EndContainer(), SetFill(1, 1),
1609 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_COMBO), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_COMBO_TOOLTIP), EndContainer(), SetFill(1, 1),
1610 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_PBS), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_PBS_TOOLTIP), EndContainer(), SetFill(1, 1),
1611 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),
1612 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_BS_CONVERT), SetDataTip(SPR_IMG_SIGNAL_CONVERT, STR_BUILD_SIGNAL_CONVERT_TOOLTIP), SetFill(1, 1),
1613 EndContainer(),
1614 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1615 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_NORM), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_NORM_TOOLTIP), EndContainer(), SetFill(1, 1),
1616 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_ENTRY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_ENTRY_TOOLTIP), EndContainer(), SetFill(1, 1),
1617 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_EXIT), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_EXIT_TOOLTIP), EndContainer(), SetFill(1, 1),
1618 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_COMBO), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_COMBO_TOOLTIP), EndContainer(), SetFill(1, 1),
1619 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_PBS), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_PBS_TOOLTIP), EndContainer(), SetFill(1, 1),
1620 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),
1621 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_DRAG_SIGNALS_DENSITY_TOOLTIP), SetFill(1, 1),
1622 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),
1623 NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1624 NWidget(NWID_SPACER), SetFill(1, 0),
1625 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),
1626 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),
1627 NWidget(NWID_SPACER), SetFill(1, 0),
1628 EndContainer(),
1629 NWidget(NWID_SPACER), SetMinimalSize(0, 2), SetFill(1, 0),
1630 EndContainer(),
1631 EndContainer(),
1632 EndContainer(),
1635 /** Signal selection window description */
1636 static WindowDesc _signal_builder_desc(
1637 WDP_AUTO, "build_signal", 0, 0,
1638 WC_BUILD_SIGNAL, WC_BUILD_TOOLBAR,
1639 WDF_CONSTRUCTION,
1640 _nested_signal_builder_widgets, lengthof(_nested_signal_builder_widgets)
1644 * Open the signal selection window
1646 static void ShowSignalBuilder(Window *parent)
1648 new BuildSignalWindow(&_signal_builder_desc, parent);
1651 struct BuildRailDepotWindow : public PickerWindowBase {
1652 BuildRailDepotWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1654 this->InitNested(TRANSPORT_RAIL);
1655 this->LowerWidget(_build_depot_direction + WID_BRAD_DEPOT_NE);
1658 virtual void DrawWidget(const Rect &r, int widget) const
1660 if (!IsInsideMM(widget, WID_BRAD_DEPOT_NE, WID_BRAD_DEPOT_NW + 1)) return;
1662 DrawTrainDepotSprite(r.left - 1, r.top, widget - WID_BRAD_DEPOT_NE + DIAGDIR_NE, _cur_railtype);
1665 virtual void OnClick(Point pt, int widget, int click_count)
1667 switch (widget) {
1668 case WID_BRAD_DEPOT_NE:
1669 case WID_BRAD_DEPOT_SE:
1670 case WID_BRAD_DEPOT_SW:
1671 case WID_BRAD_DEPOT_NW:
1672 this->RaiseWidget(_build_depot_direction + WID_BRAD_DEPOT_NE);
1673 _build_depot_direction = (DiagDirection)(widget - WID_BRAD_DEPOT_NE);
1674 this->LowerWidget(_build_depot_direction + WID_BRAD_DEPOT_NE);
1675 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1676 this->SetDirty();
1677 break;
1682 /** Nested widget definition of the build rail depot window */
1683 static const NWidgetPart _nested_build_depot_widgets[] = {
1684 NWidget(NWID_HORIZONTAL),
1685 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1686 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_BUILD_DEPOT_TRAIN_ORIENTATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1687 EndContainer(),
1688 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1689 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1690 NWidget(NWID_HORIZONTAL_LTR),
1691 NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
1692 NWidget(NWID_VERTICAL),
1693 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_NW), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1694 EndContainer(),
1695 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1696 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_SW), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1697 EndContainer(),
1698 EndContainer(),
1699 NWidget(NWID_SPACER), SetMinimalSize(2, 0),
1700 NWidget(NWID_VERTICAL),
1701 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_NE), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1702 EndContainer(),
1703 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1704 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_SE), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1705 EndContainer(),
1706 EndContainer(),
1707 NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
1708 EndContainer(),
1709 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1710 EndContainer(),
1713 static WindowDesc _build_depot_desc(
1714 WDP_AUTO, NULL, 0, 0,
1715 WC_BUILD_DEPOT, WC_BUILD_TOOLBAR,
1716 WDF_CONSTRUCTION,
1717 _nested_build_depot_widgets, lengthof(_nested_build_depot_widgets)
1720 static void ShowBuildTrainDepotPicker(Window *parent)
1722 new BuildRailDepotWindow(&_build_depot_desc, parent);
1725 struct BuildRailWaypointWindow : PickerWindowBase {
1726 BuildRailWaypointWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1728 this->CreateNestedTree();
1730 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX);
1731 matrix->SetScrollbar(this->GetScrollbar(WID_BRW_SCROLL));
1733 this->FinishInitNested(TRANSPORT_RAIL);
1735 matrix->SetCount(_waypoint_count);
1736 matrix->SetClicked(_cur_waypoint_type);
1739 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1741 switch (widget) {
1742 case WID_BRW_WAYPOINT_MATRIX:
1743 /* Three blobs high and wide. */
1744 size->width += resize->width * 2;
1745 size->height += resize->height * 2;
1747 /* Resizing in X direction only at blob size, but at pixel level in Y. */
1748 resize->height = 1;
1749 break;
1753 virtual void DrawWidget(const Rect &r, int widget) const
1755 switch (GB(widget, 0, 16)) {
1756 case WID_BRW_WAYPOINT: {
1757 byte type = GB(widget, 16, 16);
1758 const StationSpec *statspec = StationClass::Get(STAT_CLASS_WAYP)->GetSpec(type);
1759 DrawWaypointSprite(r.left + TILE_PIXELS, r.bottom - TILE_PIXELS, type, _cur_railtype);
1761 if (!IsStationAvailable(statspec)) {
1762 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
1768 virtual void OnClick(Point pt, int widget, int click_count)
1770 switch (GB(widget, 0, 16)) {
1771 case WID_BRW_WAYPOINT: {
1772 byte type = GB(widget, 16, 16);
1773 this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX)->SetClicked(_cur_waypoint_type);
1775 /* Check station availability callback */
1776 const StationSpec *statspec = StationClass::Get(STAT_CLASS_WAYP)->GetSpec(type);
1777 if (!IsStationAvailable(statspec)) return;
1779 _cur_waypoint_type = type;
1780 this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX)->SetClicked(_cur_waypoint_type);
1781 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1782 this->SetDirty();
1783 break;
1789 /** Nested widget definition for the build NewGRF rail waypoint window */
1790 static const NWidgetPart _nested_build_waypoint_widgets[] = {
1791 NWidget(NWID_HORIZONTAL),
1792 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1793 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_WAYPOINT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1794 NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
1795 EndContainer(),
1796 NWidget(NWID_HORIZONTAL),
1797 NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BRW_WAYPOINT_MATRIX), SetPIP(3, 2, 3), SetScrollbar(WID_BRW_SCROLL),
1798 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BRW_WAYPOINT), SetMinimalSize(66, 60), SetDataTip(0x0, STR_WAYPOINT_GRAPHICS_TOOLTIP), SetScrollbar(WID_BRW_SCROLL), EndContainer(),
1799 EndContainer(),
1800 NWidget(NWID_VERTICAL),
1801 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BRW_SCROLL),
1802 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
1803 EndContainer(),
1804 EndContainer(),
1807 static WindowDesc _build_waypoint_desc(
1808 WDP_AUTO, "build_waypoint", 0, 0,
1809 WC_BUILD_WAYPOINT, WC_BUILD_TOOLBAR,
1810 WDF_CONSTRUCTION,
1811 _nested_build_waypoint_widgets, lengthof(_nested_build_waypoint_widgets)
1814 static void ShowBuildWaypointPicker(Window *parent)
1816 new BuildRailWaypointWindow(&_build_waypoint_desc, parent);
1820 * Initialize rail building GUI settings
1822 void InitializeRailGui()
1824 _build_depot_direction = DIAGDIR_NW;
1828 * Re-initialize rail-build toolbar after toggling support for electric trains
1829 * @param disable Boolean whether electric trains are disabled (removed from the game)
1831 void ReinitGuiAfterToggleElrail(bool disable)
1833 extern RailType _last_built_railtype;
1834 if (disable && _last_built_railtype == RAILTYPE_ELECTRIC) {
1835 _last_built_railtype = _cur_railtype = RAILTYPE_RAIL;
1836 BuildRailToolbarWindow *w = dynamic_cast<BuildRailToolbarWindow *>(FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL));
1837 if (w != NULL) w->ModifyRailType(_cur_railtype);
1839 MarkWholeScreenDirty();
1842 /** Set the initial (default) railtype to use */
1843 static void SetDefaultRailGui()
1845 if (_local_company == COMPANY_SPECTATOR || !Company::IsValidID(_local_company)) return;
1847 extern RailType _last_built_railtype;
1848 RailType rt = (RailType)(_settings_client.gui.default_rail_type + RAILTYPE_END);
1849 if (rt == DEF_RAILTYPE_MOST_USED) {
1850 /* Find the most used rail type */
1851 RailType count[RAILTYPE_END];
1852 memset(count, 0, sizeof(count));
1853 for (TileIndex t = 0; t < MapSize(); t++) {
1854 if (IsRailwayTile(t) || IsRailDepotTile(t) || IsLevelCrossingTile(t) || HasStationTileRail(t) ||
1855 maptile_is_rail_tunnel(t)) {
1856 count[GetRailType(t)]++;
1860 rt = RAILTYPE_RAIL;
1861 for (RailType r = RAILTYPE_ELECTRIC; r < RAILTYPE_END; r++) {
1862 if (count[r] >= count[rt]) rt = r;
1865 /* No rail, just get the first available one */
1866 if (count[rt] == 0) rt = DEF_RAILTYPE_FIRST;
1868 switch (rt) {
1869 case DEF_RAILTYPE_FIRST:
1870 rt = RAILTYPE_RAIL;
1871 while (rt < RAILTYPE_END && !HasRailtypeAvail(_local_company, rt)) rt++;
1872 break;
1874 case DEF_RAILTYPE_LAST:
1875 rt = GetBestRailtype(_local_company);
1876 break;
1878 default:
1879 break;
1882 _last_built_railtype = _cur_railtype = rt;
1883 BuildRailToolbarWindow *w = dynamic_cast<BuildRailToolbarWindow *>(FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL));
1884 if (w != NULL) w->ModifyRailType(_cur_railtype);
1888 * Updates the current signal variant used in the signal GUI
1889 * to the one adequate to current year.
1890 * @param p needed to be called when a setting changes
1891 * @return success, needed for settings
1893 bool ResetSignalVariant(int32 p)
1895 SignalVariant new_variant = (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC);
1897 if (new_variant != _cur_signal_variant) {
1898 Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
1899 if (w != NULL) {
1900 w->SetDirty();
1901 w->RaiseWidget((_cur_signal_variant == SIG_ELECTRIC ? WID_BS_ELECTRIC_NORM : WID_BS_SEMAPHORE_NORM) + _cur_signal_type);
1903 _cur_signal_variant = new_variant;
1906 return true;
1910 * Resets the rail GUI - sets default railtype to build
1911 * and resets the signal GUI
1913 void InitializeRailGUI()
1915 SetDefaultRailGui();
1917 _convert_signal_button = false;
1918 _cur_signal_type = _default_signal_type[_settings_client.gui.default_signal_type];
1919 ResetSignalVariant();
1923 * Compare railtypes based on their sorting order.
1924 * @param first The railtype to compare to.
1925 * @param second The railtype to compare.
1926 * @return True iff the first should be sorted before the second.
1928 static int CDECL CompareRailTypes(const DropDownListItem * const *first, const DropDownListItem * const *second)
1930 return GetRailTypeInfo((RailType)(*first)->result)->sorting_order - GetRailTypeInfo((RailType)(*second)->result)->sorting_order;
1934 * Create a drop down list for all the rail types of the local company.
1935 * @param for_replacement Whether this list is for the replacement window.
1936 * @return The populated and sorted #DropDownList.
1938 DropDownList *GetRailTypeDropDownList(bool for_replacement)
1940 RailTypes used_railtypes = RAILTYPES_NONE;
1942 /* Find the used railtypes. */
1943 Engine *e;
1944 FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
1945 if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue;
1947 used_railtypes |= GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes;
1950 /* Get the date introduced railtypes as well. */
1951 used_railtypes = AddDateIntroducedRailTypes(used_railtypes, MAX_DAY);
1953 const Company *c = Company::Get(_local_company);
1954 DropDownList *list = new DropDownList();
1955 for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
1956 /* If it's not used ever, don't show it to the user. */
1957 if (!HasBit(used_railtypes, rt)) continue;
1959 const RailtypeInfo *rti = GetRailTypeInfo(rt);
1960 /* Skip rail type if it has no label */
1961 if (rti->label == 0) continue;
1963 StringID str = for_replacement ? rti->strings.replace_text : (rti->max_speed > 0 ? STR_TOOLBAR_RAILTYPE_VELOCITY : STR_JUST_STRING);
1964 DropDownListParamStringItem *item = new DropDownListParamStringItem(str, rt, !HasBit(c->avail_railtypes, rt));
1965 item->SetParam(0, rti->strings.menu_text);
1966 item->SetParam(1, rti->max_speed);
1967 *list->Append() = item;
1969 QSortT(list->Begin(), list->Length(), CompareRailTypes);
1970 return list;