OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

rail_gui.cpp
Go to the documentation of this file.
1 /* $Id: rail_gui.cpp 27571 2016-05-22 10:07:48Z frosch $ */
2
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 */
9
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
38 #include "station_map.h"
39 #include "tunnelbridge_map.h"
40
41 #include "widgets/rail_widget.h"
42
43 #include "safeguards.h"
44
45
46 static RailType _cur_railtype;
47 static bool _remove_button_clicked;
48 static DiagDirection _build_depot_direction;
49 static byte _waypoint_count = 1;
50 static byte _cur_waypoint_type;
51 static bool _convert_signal_button;
52 static SignalVariant _cur_signal_variant;
53 static SignalType _cur_signal_type;
54
55 /* Map the setting: default_signal_type to the corresponding signal type */
56 static const SignalType _default_signal_type[] = {SIGTYPE_NORMAL, SIGTYPE_PBS, SIGTYPE_PBS_ONEWAY};
57
58 struct RailStationGUISettings {
59 Axis orientation;
60
61 bool newstations;
62 StationClassID station_class;
63 byte station_type;
64 byte station_count;
65 };
66 static RailStationGUISettings _railstation;
67
68
69 static void HandleStationPlacement(TileIndex start, TileIndex end);
70 static void ShowBuildTrainDepotPicker(Window *parent);
71 static void ShowBuildWaypointPicker(Window *parent);
72 static void ShowStationBuilder(Window *parent);
73 static void ShowSignalBuilder(Window *parent);
74
79 static bool IsStationAvailable(const StationSpec *statspec)
80 {
81 if (statspec == NULL || !HasBit(statspec->callback_mask, CBM_STATION_AVAIL)) return true;
82
83 uint16 cb_res = GetStationCallback(CBID_STATION_AVAILABILITY, 0, 0, statspec, NULL, INVALID_TILE);
84 if (cb_res == CALLBACK_FAILED) return true;
85
86 return Convert8bitBooleanCallback(statspec->grf_prop.grffile, CBID_STATION_AVAILABILITY, cb_res);
87 }
88
89 void CcPlaySound_SPLAT_RAIL(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
90 {
91 if (result.Succeeded() && _settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_RAIL, tile);
92 }
93
94 static void GenericPlaceRail(TileIndex tile, int cmd)
95 {
96 DoCommandP(tile, _cur_railtype, cmd,
97 _remove_button_clicked ?
98 CMD_REMOVE_SINGLE_RAIL | CMD_MSG(STR_ERROR_CAN_T_REMOVE_RAILROAD_TRACK) :
99 CMD_BUILD_SINGLE_RAIL | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_TRACK),
100 CcPlaySound_SPLAT_RAIL);
101 }
102
110 static void PlaceExtraDepotRail(TileIndex tile, DiagDirection dir, Track track)
111 {
112 if (GetRailTileType(tile) != RAIL_TILE_NORMAL) return;
113 if ((GetTrackBits(tile) & DiagdirReachesTracks(dir)) == 0) return;
114
115 DoCommandP(tile, _cur_railtype, track, CMD_BUILD_SINGLE_RAIL);
116 }
117
119 static const Track _place_depot_extra_track[12] = {
120 TRACK_LEFT, TRACK_UPPER, TRACK_UPPER, TRACK_RIGHT, // First additional track for directions 0..3
121 TRACK_X, TRACK_Y, TRACK_X, TRACK_Y, // Second additional track
122 TRACK_LOWER, TRACK_LEFT, TRACK_RIGHT, TRACK_LOWER, // Third additional track
123 };
124
126 static const DiagDirection _place_depot_extra_dir[12] = {
127 DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, DIAGDIR_SW,
128 DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE, DIAGDIR_SE,
129 DIAGDIR_NW, DIAGDIR_NE, DIAGDIR_NW, DIAGDIR_NE,
130 };
131
132 void CcRailDepot(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
133 {
134 if (result.Failed()) return;
135
136 DiagDirection dir = (DiagDirection)p2;
137
138 if (_settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_RAIL, tile);
139 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
140
141 tile += TileOffsByDiagDir(dir);
142
143 if (IsTileType(tile, MP_RAILWAY)) {
144 PlaceExtraDepotRail(tile, _place_depot_extra_dir[dir], _place_depot_extra_track[dir]);
145 PlaceExtraDepotRail(tile, _place_depot_extra_dir[dir + 4], _place_depot_extra_track[dir + 4]);
146 PlaceExtraDepotRail(tile, _place_depot_extra_dir[dir + 8], _place_depot_extra_track[dir + 8]);
147 }
148 }
149
154 static void PlaceRail_Waypoint(TileIndex tile)
155 {
156 if (_remove_button_clicked) {
157 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_REMOVE_STATION);
158 return;
159 }
160
161 Axis axis = GetAxisForNewWaypoint(tile);
162 if (IsValidAxis(axis)) {
163 /* Valid tile for waypoints */
164 VpStartPlaceSizing(tile, axis == AXIS_X ? VPM_FIX_X : VPM_FIX_Y, DDSP_BUILD_STATION);
165 } else {
166 /* Tile where we can't build rail waypoints. This is always going to fail,
167 * but provides the user with a proper error message. */
168 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));
169 }
170 }
171
172 void CcStation(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
173 {
174 if (result.Failed()) return;
175
176 if (_settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_RAIL, tile);
177 /* Only close the station builder window if the default station and non persistent building is chosen. */
178 if (_railstation.station_class == STAT_CLASS_DFLT && _railstation.station_type == 0 && !_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
179 }
180
185 static void PlaceRail_Station(TileIndex tile)
186 {
187 if (_remove_button_clicked) {
188 VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED, DDSP_REMOVE_STATION);
189 VpSetPlaceSizingLimit(-1);
190 } else if (_settings_client.gui.station_dragdrop) {
191 VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED, DDSP_BUILD_STATION);
192 VpSetPlaceSizingLimit(_settings_game.station.station_spread);
193 } else {
194 uint32 p1 = _cur_railtype | _railstation.orientation << 4 | _settings_client.gui.station_numtracks << 8 | _settings_client.gui.station_platlength << 16 | _ctrl_pressed << 24;
195 uint32 p2 = _railstation.station_class | _railstation.station_type << 8 | INVALID_STATION << 16;
196
197 int w = _settings_client.gui.station_numtracks;
198 int h = _settings_client.gui.station_platlength;
199 if (!_railstation.orientation) Swap(w, h);
200
201 CommandContainer cmdcont = { tile, p1, p2, CMD_BUILD_RAIL_STATION | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION), CcStation, "" };
202 ShowSelectStationIfNeeded(cmdcont, TileArea(tile, w, h));
203 }
204 }
205
211 static void GenericPlaceSignals(TileIndex tile)
212 {
213 TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_RAIL, 0));
214
215 if (trackbits & TRACK_BIT_VERT) { // N-S direction
216 trackbits = (_tile_fract_coords.x <= _tile_fract_coords.y) ? TRACK_BIT_RIGHT : TRACK_BIT_LEFT;
217 }
218
219 if (trackbits & TRACK_BIT_HORZ) { // E-W direction
220 trackbits = (_tile_fract_coords.x + _tile_fract_coords.y <= 15) ? TRACK_BIT_UPPER : TRACK_BIT_LOWER;
221 }
222
223 Track track = FindFirstTrack(trackbits);
224
225 if (_remove_button_clicked) {
226 DoCommandP(tile, track, 0, CMD_REMOVE_SIGNALS | CMD_MSG(STR_ERROR_CAN_T_REMOVE_SIGNALS_FROM), CcPlaySound_SPLAT_RAIL);
227 } else {
228 const Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
229
230 /* Map the setting cycle_signal_types to the lower and upper allowed signal type. */
231 static const uint cycle_bounds[] = {SIGTYPE_NORMAL | (SIGTYPE_LAST_NOPBS << 3), SIGTYPE_PBS | (SIGTYPE_LAST << 3), SIGTYPE_NORMAL | (SIGTYPE_LAST << 3)};
232
233 /* various bitstuffed elements for CmdBuildSingleSignal() */
234 uint32 p1 = track;
235
236 if (w != NULL) {
237 /* signal GUI is used */
238 SB(p1, 3, 1, _ctrl_pressed);
239 SB(p1, 4, 1, _cur_signal_variant);
240 SB(p1, 5, 3, _cur_signal_type);
241 SB(p1, 8, 1, _convert_signal_button);
242 SB(p1, 9, 6, cycle_bounds[_settings_client.gui.cycle_signal_types]);
243 } else {
244 SB(p1, 3, 1, _ctrl_pressed);
245 SB(p1, 4, 1, (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC));
246 SB(p1, 5, 3, _default_signal_type[_settings_client.gui.default_signal_type]);
247 SB(p1, 8, 1, 0);
248 SB(p1, 9, 6, cycle_bounds[_settings_client.gui.cycle_signal_types]);
249 }
250
251 DoCommandP(tile, p1, 0, CMD_BUILD_SIGNALS |
252 CMD_MSG((w != NULL && _convert_signal_button) ? STR_ERROR_SIGNAL_CAN_T_CONVERT_SIGNALS_HERE : STR_ERROR_CAN_T_BUILD_SIGNALS_HERE),
253 CcPlaySound_SPLAT_RAIL);
254 }
255 }
256
262 static void PlaceRail_Bridge(TileIndex tile, Window *w)
263 {
264 if (IsBridgeTile(tile)) {
265 TileIndex other_tile = GetOtherTunnelBridgeEnd(tile);
266 Point pt = {0, 0};
267 w->OnPlaceMouseUp(VPM_X_OR_Y, DDSP_BUILD_BRIDGE, pt, other_tile, tile);
268 } else {
269 VpStartPlaceSizing(tile, VPM_X_OR_Y, DDSP_BUILD_BRIDGE);
270 }
271 }
272
274 void CcBuildRailTunnel(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
275 {
276 if (result.Succeeded()) {
277 if (_settings_client.sound.confirm) SndPlayTileFx(SND_20_SPLAT_RAIL, tile);
278 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
279 } else {
280 SetRedErrorSquare(_build_tunnel_endtile);
281 }
282 }
283
288 static void ToggleRailButton_Remove(Window *w)
289 {
290 DeleteWindowById(WC_SELECT_STATION, 0);
291 w->ToggleWidgetLoweredState(WID_RAT_REMOVE);
292 w->SetWidgetDirty(WID_RAT_REMOVE);
293 _remove_button_clicked = w->IsWidgetLowered(WID_RAT_REMOVE);
294 SetSelectionRed(_remove_button_clicked);
295 }
296
302 static bool RailToolbar_CtrlChanged(Window *w)
303 {
304 if (w->IsWidgetDisabled(WID_RAT_REMOVE)) return false;
305
306 /* allow ctrl to switch remove mode only for these widgets */
307 for (uint i = WID_RAT_BUILD_NS; i <= WID_RAT_BUILD_STATION; i++) {
308 if ((i <= WID_RAT_AUTORAIL || i >= WID_RAT_BUILD_WAYPOINT) && w->IsWidgetLowered(i)) {
309 ToggleRailButton_Remove(w);
310 return true;
311 }
312 }
313
314 return false;
315 }
316
317
323 static void BuildRailClick_Remove(Window *w)
324 {
325 if (w->IsWidgetDisabled(WID_RAT_REMOVE)) return;
326 ToggleRailButton_Remove(w);
327 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
328
329 /* handle station builder */
330 if (w->IsWidgetLowered(WID_RAT_BUILD_STATION)) {
331 if (_remove_button_clicked) {
332 /* starting drag & drop remove */
333 if (!_settings_client.gui.station_dragdrop) {
334 SetTileSelectSize(1, 1);
335 } else {
336 VpSetPlaceSizingLimit(-1);
337 }
338 } else {
339 /* starting station build mode */
340 if (!_settings_client.gui.station_dragdrop) {
341 int x = _settings_client.gui.station_numtracks;
342 int y = _settings_client.gui.station_platlength;
343 if (_railstation.orientation == 0) Swap(x, y);
344 SetTileSelectSize(x, y);
345 } else {
346 VpSetPlaceSizingLimit(_settings_game.station.station_spread);
347 }
348 }
349 }
350 }
351
352 static void DoRailroadTrack(int mode)
353 {
354 DoCommandP(TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y), _cur_railtype | (mode << 4),
355 _remove_button_clicked ?
356 CMD_REMOVE_RAILROAD_TRACK | CMD_MSG(STR_ERROR_CAN_T_REMOVE_RAILROAD_TRACK) :
357 CMD_BUILD_RAILROAD_TRACK | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_TRACK),
358 CcPlaySound_SPLAT_RAIL);
359 }
360
361 static void HandleAutodirPlacement()
362 {
363 int trackstat = _thd.drawstyle & HT_DIR_MASK; // 0..5
364
365 if (_thd.drawstyle & HT_RAIL) { // one tile case
366 GenericPlaceRail(TileVirtXY(_thd.selend.x, _thd.selend.y), trackstat);
367 return;
368 }
369
370 DoRailroadTrack(trackstat);
371 }
372
379 static void HandleAutoSignalPlacement()
380 {
381 uint32 p2 = GB(_thd.drawstyle, 0, 3); // 0..5
382
383 if ((_thd.drawstyle & HT_DRAG_MASK) == HT_RECT) { // one tile case
384 GenericPlaceSignals(TileVirtXY(_thd.selend.x, _thd.selend.y));
385 return;
386 }
387
388 const Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
389
390 if (w != NULL) {
391 /* signal GUI is used */
392 SB(p2, 3, 1, 0);
393 SB(p2, 4, 1, _cur_signal_variant);
394 SB(p2, 6, 1, _ctrl_pressed);
395 SB(p2, 7, 3, _cur_signal_type);
396 SB(p2, 24, 8, _settings_client.gui.drag_signals_density);
397 SB(p2, 10, 1, !_settings_client.gui.drag_signals_fixed_distance);
398 } else {
399 SB(p2, 3, 1, 0);
400 SB(p2, 4, 1, (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC));
401 SB(p2, 6, 1, _ctrl_pressed);
402 SB(p2, 7, 3, _default_signal_type[_settings_client.gui.default_signal_type]);
403 SB(p2, 24, 8, _settings_client.gui.drag_signals_density);
404 SB(p2, 10, 1, !_settings_client.gui.drag_signals_fixed_distance);
405 }
406
407 /* _settings_client.gui.drag_signals_density is given as a parameter such that each user
408 * in a network game can specify his/her own signal density */
409 DoCommandP(TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y), p2,
410 _remove_button_clicked ?
411 CMD_REMOVE_SIGNAL_TRACK | CMD_MSG(STR_ERROR_CAN_T_REMOVE_SIGNALS_FROM) :
412 CMD_BUILD_SIGNAL_TRACK | CMD_MSG(STR_ERROR_CAN_T_BUILD_SIGNALS_HERE),
413 CcPlaySound_SPLAT_RAIL);
414 }
415
416
418 struct BuildRailToolbarWindow : Window {
419 RailType railtype;
420 int last_user_action;
421
422 BuildRailToolbarWindow(WindowDesc *desc, RailType railtype) : Window(desc)
423 {
424 this->InitNested(TRANSPORT_RAIL);
425 this->SetupRailToolbar(railtype);
426 this->DisableWidget(WID_RAT_REMOVE);
427 this->last_user_action = WIDGET_LIST_END;
428
429 if (_settings_client.gui.link_terraform_toolbar) ShowTerraformToolbar(this);
430 }
431
432 ~BuildRailToolbarWindow()
433 {
434 if (_settings_client.gui.link_terraform_toolbar) DeleteWindowById(WC_SCEN_LAND_GEN, 0, false);
435 }
436
442 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
443 {
444 if (!gui_scope) return;
445
446 if (!CanBuildVehicleInfrastructure(VEH_TRAIN)) delete this;
447 }
448
453 void SetupRailToolbar(RailType railtype)
454 {
455 this->railtype = railtype;
456 const RailtypeInfo *rti = GetRailTypeInfo(railtype);
457
458 assert(railtype < RAILTYPE_END);
459 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_NS)->widget_data = rti->gui_sprites.build_ns_rail;
460 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_X)->widget_data = rti->gui_sprites.build_x_rail;
461 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_EW)->widget_data = rti->gui_sprites.build_ew_rail;
462 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_Y)->widget_data = rti->gui_sprites.build_y_rail;
463 this->GetWidget<NWidgetCore>(WID_RAT_AUTORAIL)->widget_data = rti->gui_sprites.auto_rail;
464 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_DEPOT)->widget_data = rti->gui_sprites.build_depot;
465 this->GetWidget<NWidgetCore>(WID_RAT_CONVERT_RAIL)->widget_data = rti->gui_sprites.convert_rail;
466 this->GetWidget<NWidgetCore>(WID_RAT_BUILD_TUNNEL)->widget_data = rti->gui_sprites.build_tunnel;
467 }
468
473 void ModifyRailType(RailType railtype)
474 {
475 this->SetupRailToolbar(railtype);
476 this->ReInit();
477 }
478
479 void UpdateRemoveWidgetStatus(int clicked_widget)
480 {
481 switch (clicked_widget) {
482 case WID_RAT_REMOVE:
483 /* If it is the removal button that has been clicked, do nothing,
484 * as it is up to the other buttons to drive removal status */
485 return;
486
487 case WID_RAT_BUILD_NS:
488 case WID_RAT_BUILD_X:
489 case WID_RAT_BUILD_EW:
490 case WID_RAT_BUILD_Y:
491 case WID_RAT_AUTORAIL:
492 case WID_RAT_BUILD_WAYPOINT:
493 case WID_RAT_BUILD_STATION:
494 case WID_RAT_BUILD_SIGNALS:
495 /* Removal button is enabled only if the rail/signal/waypoint/station
496 * button is still lowered. Once raised, it has to be disabled */
497 this->SetWidgetDisabledState(WID_RAT_REMOVE, !this->IsWidgetLowered(clicked_widget));
498 break;
499
500 default:
501 /* When any other buttons than rail/signal/waypoint/station, raise and
502 * disable the removal button */
503 this->DisableWidget(WID_RAT_REMOVE);
504 this->RaiseWidget(WID_RAT_REMOVE);
505 break;
506 }
507 }
508
509 virtual void SetStringParameters(int widget) const
510 {
511 if (widget == WID_RAT_CAPTION) {
512 const RailtypeInfo *rti = GetRailTypeInfo(this->railtype);
513 if (rti->max_speed > 0) {
514 SetDParam(0, STR_TOOLBAR_RAILTYPE_VELOCITY);
515 SetDParam(1, rti->strings.toolbar_caption);
516 SetDParam(2, rti->max_speed);
517 } else {
518 SetDParam(0, rti->strings.toolbar_caption);
519 }
520 }
521 }
522
523 virtual void OnClick(Point pt, int widget, int click_count)
524 {
525 if (widget < WID_RAT_BUILD_NS) return;
526
527 _remove_button_clicked = false;
528 switch (widget) {
529 case WID_RAT_BUILD_NS:
530 HandlePlacePushButton(this, WID_RAT_BUILD_NS, GetRailTypeInfo(_cur_railtype)->cursor.rail_ns, HT_LINE | HT_DIR_VL);
531 this->last_user_action = widget;
532 break;
533
534 case WID_RAT_BUILD_X:
535 HandlePlacePushButton(this, WID_RAT_BUILD_X, GetRailTypeInfo(_cur_railtype)->cursor.rail_swne, HT_LINE | HT_DIR_X);
536 this->last_user_action = widget;
537 break;
538
539 case WID_RAT_BUILD_EW:
540 HandlePlacePushButton(this, WID_RAT_BUILD_EW, GetRailTypeInfo(_cur_railtype)->cursor.rail_ew, HT_LINE | HT_DIR_HL);
541 this->last_user_action = widget;
542 break;
543
544 case WID_RAT_BUILD_Y:
545 HandlePlacePushButton(this, WID_RAT_BUILD_Y, GetRailTypeInfo(_cur_railtype)->cursor.rail_nwse, HT_LINE | HT_DIR_Y);
546 this->last_user_action = widget;
547 break;
548
549 case WID_RAT_AUTORAIL:
550 HandlePlacePushButton(this, WID_RAT_AUTORAIL, GetRailTypeInfo(_cur_railtype)->cursor.autorail, HT_RAIL);
551 this->last_user_action = widget;
552 break;
553
554 case WID_RAT_DEMOLISH:
555 HandlePlacePushButton(this, WID_RAT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
556 this->last_user_action = widget;
557 break;
558
559 case WID_RAT_BUILD_DEPOT:
560 if (HandlePlacePushButton(this, WID_RAT_BUILD_DEPOT, GetRailTypeInfo(_cur_railtype)->cursor.depot, HT_RECT)) {
561 ShowBuildTrainDepotPicker(this);
562 this->last_user_action = widget;
563 }
564 break;
565
566 case WID_RAT_BUILD_WAYPOINT:
567 this->last_user_action = widget;
568 _waypoint_count = StationClass::Get(STAT_CLASS_WAYP)->GetSpecCount();
569 if (HandlePlacePushButton(this, WID_RAT_BUILD_WAYPOINT, SPR_CURSOR_WAYPOINT, HT_RECT) && _waypoint_count > 1) {
570 ShowBuildWaypointPicker(this);
571 }
572 break;
573
574 case WID_RAT_BUILD_STATION:
575 if (HandlePlacePushButton(this, WID_RAT_BUILD_STATION, SPR_CURSOR_RAIL_STATION, HT_RECT)) {
576 ShowStationBuilder(this);
577 this->last_user_action = widget;
578 }
579 break;
580
581 case WID_RAT_BUILD_SIGNALS: {
582 this->last_user_action = widget;
583 bool started = HandlePlacePushButton(this, WID_RAT_BUILD_SIGNALS, ANIMCURSOR_BUILDSIGNALS, HT_RECT);
584 if (started && _settings_client.gui.enable_signal_gui != _ctrl_pressed) {
585 ShowSignalBuilder(this);
586 }
587 break;
588 }
589
590 case WID_RAT_BUILD_BRIDGE:
591 HandlePlacePushButton(this, WID_RAT_BUILD_BRIDGE, SPR_CURSOR_BRIDGE, HT_RECT);
592 this->last_user_action = widget;
593 break;
594
595 case WID_RAT_BUILD_TUNNEL:
596 HandlePlacePushButton(this, WID_RAT_BUILD_TUNNEL, GetRailTypeInfo(_cur_railtype)->cursor.tunnel, HT_SPECIAL);
597 this->last_user_action = widget;
598 break;
599
600 case WID_RAT_REMOVE:
601 BuildRailClick_Remove(this);
602 break;
603
604 case WID_RAT_CONVERT_RAIL:
605 HandlePlacePushButton(this, WID_RAT_CONVERT_RAIL, GetRailTypeInfo(_cur_railtype)->cursor.convert, HT_RECT | HT_DIAGONAL);
606 this->last_user_action = widget;
607 break;
608
609 default: NOT_REACHED();
610 }
611 this->UpdateRemoveWidgetStatus(widget);
612 if (_ctrl_pressed) RailToolbar_CtrlChanged(this);
613 }
614
615 virtual EventState OnHotkey(int hotkey)
616 {
617 MarkTileDirtyByTile(TileVirtXY(_thd.pos.x, _thd.pos.y)); // redraw tile selection
618 return Window::OnHotkey(hotkey);
619 }
620
621 virtual void OnPlaceObject(Point pt, TileIndex tile)
622 {
623 switch (this->last_user_action) {
624 case WID_RAT_BUILD_NS:
625 VpStartPlaceSizing(tile, VPM_FIX_VERTICAL | VPM_RAILDIRS, DDSP_PLACE_RAIL);
626 break;
627
628 case WID_RAT_BUILD_X:
629 VpStartPlaceSizing(tile, VPM_FIX_Y | VPM_RAILDIRS, DDSP_PLACE_RAIL);
630 break;
631
632 case WID_RAT_BUILD_EW:
633 VpStartPlaceSizing(tile, VPM_FIX_HORIZONTAL | VPM_RAILDIRS, DDSP_PLACE_RAIL);
634 break;
635
636 case WID_RAT_BUILD_Y:
637 VpStartPlaceSizing(tile, VPM_FIX_X | VPM_RAILDIRS, DDSP_PLACE_RAIL);
638 break;
639
640 case WID_RAT_AUTORAIL:
641 VpStartPlaceSizing(tile, VPM_RAILDIRS, DDSP_PLACE_RAIL);
642 break;
643
644 case WID_RAT_DEMOLISH:
645 PlaceProc_DemolishArea(tile);
646 break;
647
648 case WID_RAT_BUILD_DEPOT:
649 DoCommandP(tile, _cur_railtype, _build_depot_direction,
650 CMD_BUILD_TRAIN_DEPOT | CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_DEPOT),
651 CcRailDepot);
652 break;
653
654 case WID_RAT_BUILD_WAYPOINT:
655 PlaceRail_Waypoint(tile);
656 break;
657
658 case WID_RAT_BUILD_STATION:
659 PlaceRail_Station(tile);
660 break;
661
662 case WID_RAT_BUILD_SIGNALS:
663 VpStartPlaceSizing(tile, VPM_SIGNALDIRS, DDSP_BUILD_SIGNALS);
664 break;
665
666 case WID_RAT_BUILD_BRIDGE:
667 PlaceRail_Bridge(tile, this);
668 break;
669
670 case WID_RAT_BUILD_TUNNEL:
671 DoCommandP(tile, _cur_railtype | (TRANSPORT_RAIL << 8), 0, CMD_BUILD_TUNNEL | CMD_MSG(STR_ERROR_CAN_T_BUILD_TUNNEL_HERE), CcBuildRailTunnel);
672 break;
673
674 case WID_RAT_CONVERT_RAIL:
675 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CONVERT_RAIL);
676 break;
677
678 default: NOT_REACHED();
679 }
680 }
681
682 virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
683 {
684 /* no dragging if you have pressed the convert button */
685 if (FindWindowById(WC_BUILD_SIGNAL, 0) != NULL && _convert_signal_button && this->IsWidgetLowered(WID_RAT_BUILD_SIGNALS)) return;
686
687 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
688 }
689
690 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
691 {
692 if (pt.x != -1) {
693 switch (select_proc) {
694 default: NOT_REACHED();
695 case DDSP_BUILD_BRIDGE:
696 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
697 ShowBuildBridgeWindow(start_tile, end_tile, TRANSPORT_RAIL, _cur_railtype);
698 break;
699
700 case DDSP_PLACE_RAIL:
701 HandleAutodirPlacement();
702 break;
703
704 case DDSP_BUILD_SIGNALS:
705 HandleAutoSignalPlacement();
706 break;
707
708 case DDSP_DEMOLISH_AREA:
709 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
710 break;
711
712 case DDSP_CONVERT_RAIL:
713 DoCommandP(end_tile, start_tile, _cur_railtype | (_ctrl_pressed ? 0x10 : 0), CMD_CONVERT_RAIL | CMD_MSG(STR_ERROR_CAN_T_CONVERT_RAIL), CcPlaySound_SPLAT_RAIL);
714 break;
715
716 case DDSP_REMOVE_STATION:
717 case DDSP_BUILD_STATION:
718 if (this->IsWidgetLowered(WID_RAT_BUILD_STATION)) {
719 /* Station */
720 if (_remove_button_clicked) {
721 DoCommandP(end_tile, start_tile, _ctrl_pressed ? 0 : 1, CMD_REMOVE_FROM_RAIL_STATION | CMD_MSG(STR_ERROR_CAN_T_REMOVE_PART_OF_STATION), CcPlaySound_SPLAT_RAIL);
722 } else {
723 HandleStationPlacement(start_tile, end_tile);
724 }
725 } else {
726 /* Waypoint */
727 if (_remove_button_clicked) {
728 DoCommandP(end_tile, start_tile, _ctrl_pressed ? 0 : 1, CMD_REMOVE_FROM_RAIL_WAYPOINT | CMD_MSG(STR_ERROR_CAN_T_REMOVE_TRAIN_WAYPOINT), CcPlaySound_SPLAT_RAIL);
729 } else {
730 TileArea ta(start_tile, end_tile);
731 uint32 p1 = _cur_railtype | (select_method == VPM_FIX_X ? AXIS_X : AXIS_Y) << 4 | ta.w << 8 | ta.h << 16 | _ctrl_pressed << 24;
732 uint32 p2 = STAT_CLASS_WAYP | _cur_waypoint_type << 8 | INVALID_STATION << 16;
733
734 CommandContainer cmdcont = { ta.tile, p1, p2, CMD_BUILD_RAIL_WAYPOINT | CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_WAYPOINT), CcPlaySound_SPLAT_RAIL, "" };
735 ShowSelectWaypointIfNeeded(cmdcont, ta);
736 }
737 }
738 break;
739 }
740 }
741 }
742
743 virtual void OnPlaceObjectAbort()
744 {
745 this->RaiseButtons();
746 this->DisableWidget(WID_RAT_REMOVE);
747 this->SetWidgetDirty(WID_RAT_REMOVE);
748
749 DeleteWindowById(WC_BUILD_SIGNAL, TRANSPORT_RAIL);
750 DeleteWindowById(WC_BUILD_STATION, TRANSPORT_RAIL);
751 DeleteWindowById(WC_BUILD_DEPOT, TRANSPORT_RAIL);
752 DeleteWindowById(WC_BUILD_WAYPOINT, TRANSPORT_RAIL);
753 DeleteWindowById(WC_SELECT_STATION, 0);
754 DeleteWindowByClass(WC_BUILD_BRIDGE);
755 }
756
757 virtual void OnPlacePresize(Point pt, TileIndex tile)
758 {
759 DoCommand(tile, _cur_railtype | (TRANSPORT_RAIL << 8), 0, DC_AUTO, CMD_BUILD_TUNNEL);
760 VpSetPresizeRange(tile, _build_tunnel_endtile == 0 ? tile : _build_tunnel_endtile);
761 }
762
763 virtual EventState OnCTRLStateChange()
764 {
765 /* do not toggle Remove button by Ctrl when placing station */
766 if (!this->IsWidgetLowered(WID_RAT_BUILD_STATION) && !this->IsWidgetLowered(WID_RAT_BUILD_WAYPOINT) && RailToolbar_CtrlChanged(this)) return ES_HANDLED;
767 return ES_NOT_HANDLED;
768 }
769
770 static HotkeyList hotkeys;
771 };
772
778 static EventState RailToolbarGlobalHotkeys(int hotkey)
779 {
780 if (_game_mode != GM_NORMAL || !CanBuildVehicleInfrastructure(VEH_TRAIN)) return ES_NOT_HANDLED;
781 extern RailType _last_built_railtype;
782 Window *w = ShowBuildRailToolbar(_last_built_railtype);
783 if (w == NULL) return ES_NOT_HANDLED;
784 return w->OnHotkey(hotkey);
785 }
786
787 const uint16 _railtoolbar_autorail_keys[] = {'5', 'A' | WKC_GLOBAL_HOTKEY, 0};
788
789 static Hotkey railtoolbar_hotkeys[] = {
790 Hotkey('1', "build_ns", WID_RAT_BUILD_NS),
791 Hotkey('2', "build_x", WID_RAT_BUILD_X),
792 Hotkey('3', "build_ew", WID_RAT_BUILD_EW),
793 Hotkey('4', "build_y", WID_RAT_BUILD_Y),
794 Hotkey(_railtoolbar_autorail_keys, "autorail", WID_RAT_AUTORAIL),
795 Hotkey('6', "demolish", WID_RAT_DEMOLISH),
796 Hotkey('7', "depot", WID_RAT_BUILD_DEPOT),
797 Hotkey('8', "waypoint", WID_RAT_BUILD_WAYPOINT),
798 Hotkey('9', "station", WID_RAT_BUILD_STATION),
799 Hotkey('S', "signal", WID_RAT_BUILD_SIGNALS),
800 Hotkey('B', "bridge", WID_RAT_BUILD_BRIDGE),
801 Hotkey('T', "tunnel", WID_RAT_BUILD_TUNNEL),
802 Hotkey('R', "remove", WID_RAT_REMOVE),
803 Hotkey('C', "convert", WID_RAT_CONVERT_RAIL),
804 HOTKEY_LIST_END
805 };
806 HotkeyList BuildRailToolbarWindow::hotkeys("railtoolbar", railtoolbar_hotkeys, RailToolbarGlobalHotkeys);
807
808 static const NWidgetPart _nested_build_rail_widgets[] = {
809 NWidget(NWID_HORIZONTAL),
810 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
811 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_RAT_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
812 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
813 EndContainer(),
814 NWidget(NWID_HORIZONTAL),
815 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_NS),
816 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NS, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
817 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_X),
818 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NE, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
819 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_EW),
820 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_EW, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
821 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_Y),
822 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NW, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
823 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_AUTORAIL),
824 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_AUTORAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_AUTORAIL),
825
826 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), SetDataTip(0x0, STR_NULL), EndContainer(),
827
828 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_DEMOLISH),
829 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
830 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_DEPOT),
831 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DEPOT_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_TRAIN_DEPOT_FOR_BUILDING),
832 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_WAYPOINT),
833 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_WAYPOINT, STR_RAIL_TOOLBAR_TOOLTIP_CONVERT_RAIL_TO_WAYPOINT),
834 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_STATION),
835 SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_RAIL_STATION, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_STATION),
836 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_SIGNALS),
837 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_SIGNALS, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_SIGNALS),
838 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_BRIDGE),
839 SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_BRIDGE, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_BRIDGE),
840 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_TUNNEL),
841 SetFill(0, 1), SetMinimalSize(20, 22), SetDataTip(SPR_IMG_TUNNEL_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TUNNEL),
842 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_REMOVE),
843 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_REMOVE, STR_RAIL_TOOLBAR_TOOLTIP_TOGGLE_BUILD_REMOVE_FOR),
844 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_CONVERT_RAIL),
845 SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_CONVERT_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_CONVERT_RAIL),
846 EndContainer(),
847 };
848
849 static WindowDesc _build_rail_desc(
850 WDP_ALIGN_TOOLBAR, "toolbar_rail", 0, 0,
851 WC_BUILD_TOOLBAR, WC_NONE,
852 WDF_CONSTRUCTION,
853 _nested_build_rail_widgets, lengthof(_nested_build_rail_widgets),
854 &BuildRailToolbarWindow::hotkeys
855 );
856
857
866 Window *ShowBuildRailToolbar(RailType railtype)
867 {
868 if (!Company::IsValidID(_local_company)) return NULL;
869 if (!ValParamRailtype(railtype)) return NULL;
870
871 DeleteWindowByClass(WC_BUILD_TOOLBAR);
872 _cur_railtype = railtype;
873 _remove_button_clicked = false;
874 return new BuildRailToolbarWindow(&_build_rail_desc, railtype);
875 }
876
877 /* TODO: For custom stations, respect their allowed platforms/lengths bitmasks!
878 * --pasky */
879
880 static void HandleStationPlacement(TileIndex start, TileIndex end)
881 {
882 TileArea ta(start, end);
883 uint numtracks = ta.w;
884 uint platlength = ta.h;
885
886 if (_railstation.orientation == AXIS_X) Swap(numtracks, platlength);
887
888 uint32 p1 = _cur_railtype | _railstation.orientation << 4 | numtracks << 8 | platlength << 16 | _ctrl_pressed << 24;
889 uint32 p2 = _railstation.station_class | _railstation.station_type << 8 | INVALID_STATION << 16;
890
891 CommandContainer cmdcont = { ta.tile, p1, p2, CMD_BUILD_RAIL_STATION | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION), CcStation, "" };
892 ShowSelectStationIfNeeded(cmdcont, ta);
893 }
894
895 struct BuildRailStationWindow : public PickerWindowBase {
896 private:
897 uint line_height;
898 uint coverage_height;
899 Scrollbar *vscroll;
900 Scrollbar *vscroll2;
901
907 void CheckSelectedSize(const StationSpec *statspec)
908 {
909 if (statspec == NULL || _settings_client.gui.station_dragdrop) return;
910
911 /* If current number of tracks is not allowed, make it as big as possible */
912 if (HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
913 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
914 _settings_client.gui.station_numtracks = 1;
915 if (statspec->disallowed_platforms != UINT8_MAX) {
916 while (HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
917 _settings_client.gui.station_numtracks++;
918 }
919 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
920 }
921 }
922
923 if (HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
924 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
925 _settings_client.gui.station_platlength = 1;
926 if (statspec->disallowed_lengths != UINT8_MAX) {
927 while (HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
928 _settings_client.gui.station_platlength++;
929 }
930 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
931 }
932 }
933 }
934
935 public:
936 BuildRailStationWindow(WindowDesc *desc, Window *parent, bool newstation) : PickerWindowBase(desc, parent)
937 {
938 this->coverage_height = 2 * FONT_HEIGHT_NORMAL + 3 * WD_PAR_VSEP_NORMAL;
939 this->vscroll = NULL;
940 _railstation.newstations = newstation;
941
942 this->CreateNestedTree();
943 NWidgetStacked *newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_ADDITIONS);
944 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
945 newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_MATRIX);
946 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
947 newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_DEFSIZE);
948 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
949 newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_RESIZE);
950 newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
951 if (newstation) {
952 this->vscroll = this->GetScrollbar(WID_BRAS_NEWST_SCROLL);
953 this->vscroll2 = this->GetScrollbar(WID_BRAS_MATRIX_SCROLL);
954 }
955 this->FinishInitNested(TRANSPORT_RAIL);
956
957 this->LowerWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
958 if (_settings_client.gui.station_dragdrop) {
959 this->LowerWidget(WID_BRAS_PLATFORM_DRAG_N_DROP);
960 } else {
961 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
962 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
963 }
964 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_OFF, !_settings_client.gui.station_show_coverage);
965 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_ON, _settings_client.gui.station_show_coverage);
966
967 if (!newstation || _railstation.station_class >= (int)StationClass::GetClassCount()) {
968 /* New stations are not available or changed, so ensure the default station
969 * type is 'selected'. */
970 _railstation.station_class = STAT_CLASS_DFLT;
971 _railstation.station_type = 0;
972 this->vscroll2 = NULL;
973 }
974 if (newstation) {
975 _railstation.station_count = StationClass::Get(_railstation.station_class)->GetSpecCount();
976 _railstation.station_type = min(_railstation.station_type, _railstation.station_count - 1);
977
978 int count = 0;
979 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
980 if (i == STAT_CLASS_WAYP) continue;
981 count++;
982 }
983 this->vscroll->SetCount(count);
984 this->vscroll->SetPosition(Clamp(_railstation.station_class - 2, 0, max(this->vscroll->GetCount() - this->vscroll->GetCapacity(), 0)));
985
986 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX);
987 matrix->SetScrollbar(this->vscroll2);
988 matrix->SetCount(_railstation.station_count);
989 matrix->SetClicked(_railstation.station_type);
990 }
991 }
992
993 virtual ~BuildRailStationWindow()
994 {
995 DeleteWindowById(WC_SELECT_STATION, 0);
996 }
997
998 virtual void OnPaint()
999 {
1000 bool newstations = _railstation.newstations;
1001 const StationSpec *statspec = newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : NULL;
1002
1003 if (_settings_client.gui.station_dragdrop) {
1004 SetTileSelectSize(1, 1);
1005 } else {
1006 int x = _settings_client.gui.station_numtracks;
1007 int y = _settings_client.gui.station_platlength;
1008 if (_railstation.orientation == AXIS_X) Swap(x, y);
1009 if (!_remove_button_clicked) {
1010 SetTileSelectSize(x, y);
1011 }
1012 }
1013
1014 int rad = (_settings_game.station.modified_catchment) ? CA_TRAIN : CA_UNMODIFIED;
1015
1016 if (_settings_client.gui.station_show_coverage) SetTileSelectBigSize(-rad, -rad, 2 * rad, 2 * rad);
1017
1018 for (uint bits = 0; bits < 7; bits++) {
1019 bool disable = bits >= _settings_game.station.station_spread;
1020 if (statspec == NULL) {
1021 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_NUM_1, disable);
1022 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_LEN_1, disable);
1023 } else {
1024 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_NUM_1, HasBit(statspec->disallowed_platforms, bits) || disable);
1025 this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_LEN_1, HasBit(statspec->disallowed_lengths, bits) || disable);
1026 }
1027 }
1028
1029 this->DrawWidgets();
1030
1031 /* 'Accepts' and 'Supplies' texts. */
1032 NWidgetBase *cov = this->GetWidget<NWidgetBase>(WID_BRAS_COVERAGE_TEXTS);
1033 int top = cov->pos_y + WD_PAR_VSEP_NORMAL;
1034 int left = cov->pos_x + WD_FRAMERECT_LEFT;
1035 int right = cov->pos_x + cov->current_x - WD_FRAMERECT_RIGHT;
1036 int bottom = cov->pos_y + cov->current_y;
1037 top = DrawStationCoverageAreaText(left, right, top, SCT_ALL, rad, false) + WD_PAR_VSEP_NORMAL;
1038 top = DrawStationCoverageAreaText(left, right, top, SCT_ALL, rad, true) + WD_PAR_VSEP_NORMAL;
1039 /* Resize background if the window is too small.
1040 * Never make the window smaller to avoid oscillating if the size change affects the acceptance.
1041 * (This is the case, if making the window bigger moves the mouse into the window.) */
1042 if (top > bottom) {
1043 this->coverage_height += top - bottom;
1044 this->ReInit();
1045 }
1046 }
1047
1048 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1049 {
1050 switch (widget) {
1051 case WID_BRAS_NEWST_LIST: {
1052 Dimension d = {0, 0};
1053 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1054 if (i == STAT_CLASS_WAYP) continue;
1055 d = maxdim(d, GetStringBoundingBox(StationClass::Get((StationClassID)i)->name));
1056 }
1057 size->width = max(size->width, d.width + padding.width);
1058 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
1059 size->height = 5 * this->line_height;
1060 resize->height = this->line_height;
1061 break;
1062 }
1063
1064 case WID_BRAS_SHOW_NEWST_TYPE: {
1065 if (!_railstation.newstations) {
1066 size->width = 0;
1067 size->height = 0;
1068 break;
1069 }
1070
1071 /* If newstations exist, compute the non-zero minimal size. */
1072 Dimension d = {0, 0};
1073 StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
1074 for (StationClassID statclass = STAT_CLASS_BEGIN; statclass < (StationClassID)StationClass::GetClassCount(); statclass++) {
1075 if (statclass == STAT_CLASS_WAYP) continue;
1076 StationClass *stclass = StationClass::Get(statclass);
1077 for (uint16 j = 0; j < stclass->GetSpecCount(); j++) {
1078 const StationSpec *statspec = stclass->GetSpec(j);
1079 SetDParam(0, (statspec != NULL && statspec->name != 0) ? statspec->name : STR_STATION_CLASS_DFLT);
1080 d = maxdim(d, GetStringBoundingBox(str));
1081 }
1082 }
1083 size->width = max(size->width, d.width + padding.width);
1084 break;
1085 }
1086
1087 case WID_BRAS_PLATFORM_DIR_X:
1088 case WID_BRAS_PLATFORM_DIR_Y:
1089 case WID_BRAS_IMAGE:
1090 size->width = ScaleGUITrad(64) + 2;
1091 size->height = ScaleGUITrad(58) + 2;
1092 break;
1093
1094 case WID_BRAS_COVERAGE_TEXTS:
1095 size->height = this->coverage_height;
1096 break;
1097
1098 case WID_BRAS_MATRIX:
1099 fill->height = 1;
1100 resize->height = 1;
1101 break;
1102 }
1103 }
1104
1105 virtual void DrawWidget(const Rect &r, int widget) const
1106 {
1107 DrawPixelInfo tmp_dpi;
1108
1109 switch (GB(widget, 0, 16)) {
1110 case WID_BRAS_PLATFORM_DIR_X:
1111 /* Set up a clipping area for the '/' station preview */
1112 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1113 DrawPixelInfo *old_dpi = _cur_dpi;
1114 _cur_dpi = &tmp_dpi;
1115 int x = ScaleGUITrad(31) + 1;
1116 int y = r.bottom - r.top - ScaleGUITrad(31);
1117 if (!DrawStationTile(x, y, _cur_railtype, AXIS_X, _railstation.station_class, _railstation.station_type)) {
1118 StationPickerDrawSprite(x, y, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2);
1119 }
1120 _cur_dpi = old_dpi;
1121 }
1122 break;
1123
1124 case WID_BRAS_PLATFORM_DIR_Y:
1125 /* Set up a clipping area for the '\' station preview */
1126 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1127 DrawPixelInfo *old_dpi = _cur_dpi;
1128 _cur_dpi = &tmp_dpi;
1129 int x = ScaleGUITrad(31) + 1;
1130 int y = r.bottom - r.top - ScaleGUITrad(31);
1131 if (!DrawStationTile(x, y, _cur_railtype, AXIS_Y, _railstation.station_class, _railstation.station_type)) {
1132 StationPickerDrawSprite(x, y, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 3);
1133 }
1134 _cur_dpi = old_dpi;
1135 }
1136 break;
1137
1138 case WID_BRAS_NEWST_LIST: {
1139 uint statclass = 0;
1140 uint row = 0;
1141 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1142 if (i == STAT_CLASS_WAYP) continue;
1143 if (this->vscroll->IsVisible(statclass)) {
1144 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, row * this->line_height + r.top + WD_MATRIX_TOP,
1145 StationClass::Get((StationClassID)i)->name,
1146 (StationClassID)i == _railstation.station_class ? TC_WHITE : TC_BLACK);
1147 row++;
1148 }
1149 statclass++;
1150 }
1151 break;
1152 }
1153
1154 case WID_BRAS_IMAGE: {
1155 byte type = GB(widget, 16, 16);
1156 assert(type < _railstation.station_count);
1157 /* Check station availability callback */
1158 const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(type);
1159 if (!IsStationAvailable(statspec)) {
1160 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
1161 }
1162
1163 /* Set up a clipping area for the station preview. */
1164 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1165 DrawPixelInfo *old_dpi = _cur_dpi;
1166 _cur_dpi = &tmp_dpi;
1167 int x = ScaleGUITrad(31) + 1;
1168 int y = r.bottom - r.top - ScaleGUITrad(31);
1169 if (!DrawStationTile(x, y, _cur_railtype, _railstation.orientation, _railstation.station_class, type)) {
1170 StationPickerDrawSprite(x, y, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2 + _railstation.orientation);
1171 }
1172 _cur_dpi = old_dpi;
1173 }
1174 break;
1175 }
1176 }
1177 }
1178
1179 virtual void OnResize()
1180 {
1181 if (this->vscroll != NULL) { // New stations available.
1182 this->vscroll->SetCapacityFromWidget(this, WID_BRAS_NEWST_LIST);
1183 }
1184 }
1185
1186 virtual void SetStringParameters(int widget) const
1187 {
1188 if (widget == WID_BRAS_SHOW_NEWST_TYPE) {
1189 const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type);
1190 SetDParam(0, (statspec != NULL && statspec->name != 0) ? statspec->name : STR_STATION_CLASS_DFLT);
1191 }
1192 }
1193
1194 virtual void OnClick(Point pt, int widget, int click_count)
1195 {
1196 switch (GB(widget, 0, 16)) {
1197 case WID_BRAS_PLATFORM_DIR_X:
1198 case WID_BRAS_PLATFORM_DIR_Y:
1199 this->RaiseWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
1200 _railstation.orientation = (Axis)(widget - WID_BRAS_PLATFORM_DIR_X);
1201 this->LowerWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
1202 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1203 this->SetDirty();
1204 DeleteWindowById(WC_SELECT_STATION, 0);
1205 break;
1206
1207 case WID_BRAS_PLATFORM_NUM_1:
1208 case WID_BRAS_PLATFORM_NUM_2:
1209 case WID_BRAS_PLATFORM_NUM_3:
1210 case WID_BRAS_PLATFORM_NUM_4:
1211 case WID_BRAS_PLATFORM_NUM_5:
1212 case WID_BRAS_PLATFORM_NUM_6:
1213 case WID_BRAS_PLATFORM_NUM_7: {
1214 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1215 this->RaiseWidget(WID_BRAS_PLATFORM_DRAG_N_DROP);
1216
1217 _settings_client.gui.station_numtracks = widget - WID_BRAS_PLATFORM_NUM_BEGIN;
1218 _settings_client.gui.station_dragdrop = false;
1219
1220 _settings_client.gui.station_dragdrop = false;
1221
1222 const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : NULL;
1223 if (statspec != NULL && HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
1224 /* The previously selected number of platforms in invalid */
1225 for (uint i = 0; i < 7; i++) {
1226 if (!HasBit(statspec->disallowed_lengths, i)) {
1227 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1228 _settings_client.gui.station_platlength = i + 1;
1229 break;
1230 }
1231 }
1232 }
1233
1234 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1235 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1236 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1237 this->SetDirty();
1238 DeleteWindowById(WC_SELECT_STATION, 0);
1239 break;
1240 }
1241
1242 case WID_BRAS_PLATFORM_LEN_1:
1243 case WID_BRAS_PLATFORM_LEN_2:
1244 case WID_BRAS_PLATFORM_LEN_3:
1245 case WID_BRAS_PLATFORM_LEN_4:
1246 case WID_BRAS_PLATFORM_LEN_5:
1247 case WID_BRAS_PLATFORM_LEN_6:
1248 case WID_BRAS_PLATFORM_LEN_7: {
1249 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1250 this->RaiseWidget(WID_BRAS_PLATFORM_DRAG_N_DROP);
1251
1252 _settings_client.gui.station_platlength = widget - WID_BRAS_PLATFORM_LEN_BEGIN;
1253 _settings_client.gui.station_dragdrop = false;
1254
1255 _settings_client.gui.station_dragdrop = false;
1256
1257 const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : NULL;
1258 if (statspec != NULL && HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
1259 /* The previously selected number of tracks in invalid */
1260 for (uint i = 0; i < 7; i++) {
1261 if (!HasBit(statspec->disallowed_platforms, i)) {
1262 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1263 _settings_client.gui.station_numtracks = i + 1;
1264 break;
1265 }
1266 }
1267 }
1268
1269 this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1270 this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1271 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1272 this->SetDirty();
1273 DeleteWindowById(WC_SELECT_STATION, 0);
1274 break;
1275 }
1276
1277 case WID_BRAS_PLATFORM_DRAG_N_DROP: {
1278 _settings_client.gui.station_dragdrop ^= true;
1279
1280 this->ToggleWidgetLoweredState(WID_BRAS_PLATFORM_DRAG_N_DROP);
1281
1282 /* get the first allowed length/number of platforms */
1283 const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : NULL;
1284 if (statspec != NULL && HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
1285 for (uint i = 0; i < 7; i++) {
1286 if (!HasBit(statspec->disallowed_lengths, i)) {
1287 this->RaiseWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1288 _settings_client.gui.station_platlength = i + 1;
1289 break;
1290 }
1291 }
1292 }
1293 if (statspec != NULL && HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
1294 for (uint i = 0; i < 7; i++) {
1295 if (!HasBit(statspec->disallowed_platforms, i)) {
1296 this->RaiseWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1297 _settings_client.gui.station_numtracks = i + 1;
1298 break;
1299 }
1300 }
1301 }
1302
1303 this->SetWidgetLoweredState(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN, !_settings_client.gui.station_dragdrop);
1304 this->SetWidgetLoweredState(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN, !_settings_client.gui.station_dragdrop);
1305 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1306 this->SetDirty();
1307 DeleteWindowById(WC_SELECT_STATION, 0);
1308 break;
1309 }
1310
1311 case WID_BRAS_HIGHLIGHT_OFF:
1312 case WID_BRAS_HIGHLIGHT_ON:
1313 _settings_client.gui.station_show_coverage = (widget != WID_BRAS_HIGHLIGHT_OFF);
1314
1315 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_OFF, !_settings_client.gui.station_show_coverage);
1316 this->SetWidgetLoweredState(WID_BRAS_HIGHLIGHT_ON, _settings_client.gui.station_show_coverage);
1317 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1318 this->SetDirty();
1319 break;
1320
1321 case WID_BRAS_NEWST_LIST: {
1322 int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_BRAS_NEWST_LIST, 0, this->line_height);
1323 if (y >= (int)StationClass::GetClassCount()) return;
1324 for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1325 if (i == STAT_CLASS_WAYP) continue;
1326 if (y == 0) {
1327 if (_railstation.station_class != (StationClassID)i) {
1328 _railstation.station_class = (StationClassID)i;
1329 StationClass *stclass = StationClass::Get(_railstation.station_class);
1330 _railstation.station_count = stclass->GetSpecCount();
1331 _railstation.station_type = min((int)_railstation.station_type, max(0, (int)_railstation.station_count - 1));
1332
1333 this->CheckSelectedSize(stclass->GetSpec(_railstation.station_type));
1334
1335 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX);
1336 matrix->SetCount(_railstation.station_count);
1337 matrix->SetClicked(_railstation.station_type);
1338 }
1339 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1340 this->SetDirty();
1341 DeleteWindowById(WC_SELECT_STATION, 0);
1342 break;
1343 }
1344 y--;
1345 }
1346 break;
1347 }
1348
1349 case WID_BRAS_IMAGE: {
1350 int y = GB(widget, 16, 16);
1351 if (y >= _railstation.station_count) return;
1352
1353 /* Check station availability callback */
1354 const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(y);
1355 if (!IsStationAvailable(statspec)) return;
1356
1357 _railstation.station_type = y;
1358
1359 this->CheckSelectedSize(statspec);
1360 this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX)->SetClicked(_railstation.station_type);
1361
1362 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1363 this->SetDirty();
1364 DeleteWindowById(WC_SELECT_STATION, 0);
1365 break;
1366 }
1367 }
1368 }
1369
1370 virtual void OnTick()
1371 {
1372 CheckRedrawStationCoverage(this);
1373 }
1374 };
1375
1376 static const NWidgetPart _nested_station_builder_widgets[] = {
1377 NWidget(NWID_HORIZONTAL),
1378 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1379 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_STATION_BUILD_RAIL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1380 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_DEFSIZE),
1381 NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
1382 EndContainer(),
1383 EndContainer(),
1384 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1385 NWidget(NWID_HORIZONTAL),
1386 NWidget(NWID_VERTICAL),
1387 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_ADDITIONS),
1388 NWidget(NWID_HORIZONTAL), SetPIP(7, 0, 7), SetPadding(2, 0, 1, 0),
1389 NWidget(WWT_MATRIX, COLOUR_GREY, WID_BRAS_NEWST_LIST), SetMinimalSize(122, 71), SetFill(1, 0),
1390 SetMatrixDataTip(1, 0, STR_STATION_BUILD_STATION_CLASS_TOOLTIP), SetScrollbar(WID_BRAS_NEWST_SCROLL),
1391 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_BRAS_NEWST_SCROLL),
1392 EndContainer(),
1393 EndContainer(),
1394 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_ORIENTATION, STR_NULL), SetPadding(1, 2, 0, 2),
1395 NWidget(NWID_HORIZONTAL),
1396 NWidget(NWID_SPACER), SetMinimalSize(7, 0), SetFill(1, 0),
1397 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(),
1398 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1399 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(),
1400 NWidget(NWID_SPACER), SetMinimalSize(7, 0), SetFill(1, 0),
1401 EndContainer(),
1402 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),
1403 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_NUMBER_OF_TRACKS, STR_NULL), SetPadding(0, 2, 0, 2),
1404 NWidget(NWID_HORIZONTAL),
1405 NWidget(NWID_SPACER), SetFill(1, 0),
1406 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_1), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_1, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1407 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_2), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_2, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1408 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_3), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_3, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1409 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_4), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_4, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1410 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_5), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_5, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1411 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_6), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_6, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1412 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_7), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_7, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1413 NWidget(NWID_SPACER), SetFill(1, 0),
1414 EndContainer(),
1415 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_PLATFORM_LENGTH, STR_NULL), SetPadding(2, 2, 0, 2),
1416 NWidget(NWID_HORIZONTAL),
1417 NWidget(NWID_SPACER), SetFill(1, 0),
1418 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_1), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_1, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1419 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_2), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_2, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1420 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_3), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_3, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1421 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_4), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_4, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1422 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_5), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_5, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1423 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_6), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_6, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1424 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_7), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_7, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1425 NWidget(NWID_SPACER), SetFill(1, 0),
1426 EndContainer(),
1427 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1428 NWidget(NWID_HORIZONTAL),
1429 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1430 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),
1431 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1432 EndContainer(),
1433 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_COVERAGE_AREA_TITLE, STR_NULL), SetPadding(3, 2, 0, 2),
1434 NWidget(NWID_HORIZONTAL),
1435 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1436 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_HIGHLIGHT_OFF), SetMinimalSize(60, 12),
1437 SetDataTip(STR_STATION_BUILD_COVERAGE_OFF, STR_STATION_BUILD_COVERAGE_AREA_OFF_TOOLTIP),
1438 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_HIGHLIGHT_ON), SetMinimalSize(60, 12),
1439 SetDataTip(STR_STATION_BUILD_COVERAGE_ON, STR_STATION_BUILD_COVERAGE_AREA_ON_TOOLTIP),
1440 NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1441 EndContainer(),
1442 EndContainer(),
1443 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_MATRIX),
1444 /* We need an additional background for the matrix, as the matrix cannot handle the scrollbar due to not being an NWidgetCore. */
1445 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(WID_BRAS_MATRIX_SCROLL),
1446 NWidget(NWID_HORIZONTAL),
1447 NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BRAS_MATRIX), SetScrollbar(WID_BRAS_MATRIX_SCROLL), SetPIP(0, 2, 0), SetPadding(2, 0, 0, 0),
1448 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BRAS_IMAGE), SetMinimalSize(66, 60),
1449 SetFill(0, 0), SetResize(0, 0), SetDataTip(0x0, STR_STATION_BUILD_STATION_TYPE_TOOLTIP), SetScrollbar(WID_BRAS_MATRIX_SCROLL),
1450 EndContainer(),
1451 EndContainer(),
1452 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BRAS_MATRIX_SCROLL),
1453 EndContainer(),
1454 EndContainer(),
1455 EndContainer(),
1456 EndContainer(),
1457 NWidget(NWID_HORIZONTAL),
1458 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BRAS_COVERAGE_TEXTS), SetFill(1, 1), SetResize(1, 0),
1459 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BRAS_SHOW_NEWST_RESIZE),
1460 NWidget(NWID_VERTICAL),
1461 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(0, 1), EndContainer(),
1462 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
1463 EndContainer(),
1464 EndContainer(),
1465 EndContainer(),
1466 EndContainer(),
1467 };
1468
1470 static WindowDesc _station_builder_desc(
1471 WDP_AUTO, "build_station_rail", 350, 0,
1472 WC_BUILD_STATION, WC_BUILD_TOOLBAR,
1473 WDF_CONSTRUCTION,
1474 _nested_station_builder_widgets, lengthof(_nested_station_builder_widgets)
1475 );
1476
1478 static void ShowStationBuilder(Window *parent)
1479 {
1480 bool newstations = StationClass::GetClassCount() > 2 || StationClass::Get(STAT_CLASS_DFLT)->GetSpecCount() != 1;
1481 new BuildRailStationWindow(&_station_builder_desc, parent, newstations);
1482 }
1483
1484 struct BuildSignalWindow : public PickerWindowBase {
1485 private:
1486 Dimension sig_sprite_size;
1487 int sig_sprite_bottom_offset;
1488
1496 void DrawSignalSprite(byte widget_index, SpriteID image) const
1497 {
1498 Point offset;
1499 Dimension sprite_size = GetSpriteSize(image, &offset);
1500 const NWidgetBase *widget = this->GetWidget<NWidgetBase>(widget_index);
1501 int x = widget->pos_x - offset.x +
1502 (widget->current_x - sprite_size.width + offset.x) / 2; // centered
1503 int y = widget->pos_y - sig_sprite_bottom_offset + WD_IMGBTN_TOP +
1504 (widget->current_y - WD_IMGBTN_TOP - WD_IMGBTN_BOTTOM + sig_sprite_size.height) / 2; // aligned to bottom
1505
1506 DrawSprite(image, PAL_NONE,
1507 x + this->IsWidgetLowered(widget_index),
1508 y + this->IsWidgetLowered(widget_index));
1509 }
1510
1511 public:
1512 BuildSignalWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1513 {
1514 this->InitNested(TRANSPORT_RAIL);
1515 this->OnInvalidateData();
1516 }
1517
1518 ~BuildSignalWindow()
1519 {
1520 _convert_signal_button = false;
1521 }
1522
1523 virtual void OnInit()
1524 {
1525 /* Calculate maximum signal sprite size. */
1526 this->sig_sprite_size.width = 0;
1527 this->sig_sprite_size.height = 0;
1528 this->sig_sprite_bottom_offset = 0;
1529 const RailtypeInfo *rti = GetRailTypeInfo(_cur_railtype);
1530 for (uint type = SIGTYPE_NORMAL; type < SIGTYPE_END; type++) {
1531 for (uint variant = SIG_ELECTRIC; variant <= SIG_SEMAPHORE; variant++) {
1532 for (uint lowered = 0; lowered < 2; lowered++) {
1533 Point offset;
1534 Dimension sprite_size = GetSpriteSize(rti->gui_sprites.signals[type][variant][lowered], &offset);
1535 this->sig_sprite_bottom_offset = max<int>(this->sig_sprite_bottom_offset, sprite_size.height);
1536 this->sig_sprite_size.width = max<int>(this->sig_sprite_size.width, sprite_size.width - offset.x);
1537 this->sig_sprite_size.height = max<int>(this->sig_sprite_size.height, sprite_size.height - offset.y);
1538 }
1539 }
1540 }
1541 }
1542
1543 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1544 {
1545 if (widget == WID_BS_DRAG_SIGNALS_DENSITY_LABEL) {
1546 /* Two digits for signals density. */
1547 size->width = max(size->width, 2 * GetDigitWidth() + padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT);
1548 } else if (IsInsideMM(widget, WID_BS_SEMAPHORE_NORM, WID_BS_ELECTRIC_PBS_OWAY + 1)) {
1549 size->width = max(size->width, this->sig_sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT);
1550 size->height = max(size->height, this->sig_sprite_size.height + WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM);
1551 }
1552 }
1553
1554 virtual void SetStringParameters(int widget) const
1555 {
1556 switch (widget) {
1557 case WID_BS_DRAG_SIGNALS_DENSITY_LABEL:
1558 SetDParam(0, _settings_client.gui.drag_signals_density);
1559 break;
1560 }
1561 }
1562
1563 virtual void DrawWidget(const Rect &r, int widget) const
1564 {
1565 if (IsInsideMM(widget, WID_BS_SEMAPHORE_NORM, WID_BS_ELECTRIC_PBS_OWAY + 1)) {
1566 /* Extract signal from widget number. */
1567 int type = (widget - WID_BS_SEMAPHORE_NORM) % SIGTYPE_END;
1568 int var = SIG_SEMAPHORE - (widget - WID_BS_SEMAPHORE_NORM) / SIGTYPE_END; // SignalVariant order is reversed compared to the widgets.
1569 SpriteID sprite = GetRailTypeInfo(_cur_railtype)->gui_sprites.signals[type][var][this->IsWidgetLowered(widget)];
1570
1571 this->DrawSignalSprite(widget, sprite);
1572 }
1573 }
1574
1575 virtual void OnClick(Point pt, int widget, int click_count)
1576 {
1577 switch (widget) {
1578 case WID_BS_SEMAPHORE_NORM:
1579 case WID_BS_SEMAPHORE_ENTRY:
1580 case WID_BS_SEMAPHORE_EXIT:
1581 case WID_BS_SEMAPHORE_COMBO:
1582 case WID_BS_SEMAPHORE_PBS:
1583 case WID_BS_SEMAPHORE_PBS_OWAY:
1584 case WID_BS_ELECTRIC_NORM:
1585 case WID_BS_ELECTRIC_ENTRY:
1586 case WID_BS_ELECTRIC_EXIT:
1587 case WID_BS_ELECTRIC_COMBO:
1588 case WID_BS_ELECTRIC_PBS:
1589 case WID_BS_ELECTRIC_PBS_OWAY:
1590 this->RaiseWidget((_cur_signal_variant == SIG_ELECTRIC ? WID_BS_ELECTRIC_NORM : WID_BS_SEMAPHORE_NORM) + _cur_signal_type);
1591
1592 _cur_signal_type = (SignalType)((uint)((widget - WID_BS_SEMAPHORE_NORM) % (SIGTYPE_LAST + 1)));
1593 _cur_signal_variant = widget >= WID_BS_ELECTRIC_NORM ? SIG_ELECTRIC : SIG_SEMAPHORE;
1594
1595 /* If 'remove' button of rail build toolbar is active, disable it. */
1596 if (_remove_button_clicked) {
1597 Window *w = FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL);
1598 if (w != NULL) ToggleRailButton_Remove(w);
1599 }
1600
1601 break;
1602
1603 case WID_BS_CONVERT:
1604 _convert_signal_button = !_convert_signal_button;
1605 break;
1606
1607 case WID_BS_DRAG_SIGNALS_DENSITY_DECREASE:
1608 if (_settings_client.gui.drag_signals_density > 1) {
1609 _settings_client.gui.drag_signals_density--;
1610 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_SETTINGS);
1611 }
1612 break;
1613
1614 case WID_BS_DRAG_SIGNALS_DENSITY_INCREASE:
1615 if (_settings_client.gui.drag_signals_density < 20) {
1616 _settings_client.gui.drag_signals_density++;
1617 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_SETTINGS);
1618 }
1619 break;
1620
1621 default: break;
1622 }
1623
1624 this->InvalidateData();
1625 }
1626
1632 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1633 {
1634 if (!gui_scope) return;
1635 this->LowerWidget((_cur_signal_variant == SIG_ELECTRIC ? WID_BS_ELECTRIC_NORM : WID_BS_SEMAPHORE_NORM) + _cur_signal_type);
1636
1637 this->SetWidgetLoweredState(WID_BS_CONVERT, _convert_signal_button);
1638
1639 this->SetWidgetDisabledState(WID_BS_DRAG_SIGNALS_DENSITY_DECREASE, _settings_client.gui.drag_signals_density == 1);
1640 this->SetWidgetDisabledState(WID_BS_DRAG_SIGNALS_DENSITY_INCREASE, _settings_client.gui.drag_signals_density == 20);
1641 }
1642 };
1643
1645 static const NWidgetPart _nested_signal_builder_widgets[] = {
1646 NWidget(NWID_HORIZONTAL),
1647 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1648 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_BUILD_SIGNAL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1649 EndContainer(),
1650 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
1651 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1652 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_NORM), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_NORM_TOOLTIP), EndContainer(), SetFill(1, 1),
1653 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_ENTRY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_ENTRY_TOOLTIP), EndContainer(), SetFill(1, 1),
1654 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_EXIT), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_EXIT_TOOLTIP), EndContainer(), SetFill(1, 1),
1655 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_COMBO), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_COMBO_TOOLTIP), EndContainer(), SetFill(1, 1),
1656 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_PBS), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_PBS_TOOLTIP), EndContainer(), SetFill(1, 1),
1657 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),
1658 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_BS_CONVERT), SetDataTip(SPR_IMG_SIGNAL_CONVERT, STR_BUILD_SIGNAL_CONVERT_TOOLTIP), SetFill(1, 1),
1659 EndContainer(),
1660 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1661 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_NORM), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_NORM_TOOLTIP), EndContainer(), SetFill(1, 1),
1662 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_ENTRY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_ENTRY_TOOLTIP), EndContainer(), SetFill(1, 1),
1663 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_EXIT), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_EXIT_TOOLTIP), EndContainer(), SetFill(1, 1),
1664 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_COMBO), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_COMBO_TOOLTIP), EndContainer(), SetFill(1, 1),
1665 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_PBS), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_PBS_TOOLTIP), EndContainer(), SetFill(1, 1),
1666 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),
1667 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_DRAG_SIGNALS_DENSITY_TOOLTIP), SetFill(1, 1),
1668 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),
1669 NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1670 NWidget(NWID_SPACER), SetFill(1, 0),
1671 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),
1672 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),
1673 NWidget(NWID_SPACER), SetFill(1, 0),
1674 EndContainer(),
1675 NWidget(NWID_SPACER), SetMinimalSize(0, 2), SetFill(1, 0),
1676 EndContainer(),
1677 EndContainer(),
1678 EndContainer(),
1679 };
1680
1682 static WindowDesc _signal_builder_desc(
1683 WDP_AUTO, "build_signal", 0, 0,
1684 WC_BUILD_SIGNAL, WC_BUILD_TOOLBAR,
1685 WDF_CONSTRUCTION,
1686 _nested_signal_builder_widgets, lengthof(_nested_signal_builder_widgets)
1687 );
1688
1692 static void ShowSignalBuilder(Window *parent)
1693 {
1694 new BuildSignalWindow(&_signal_builder_desc, parent);
1695 }
1696
1697 struct BuildRailDepotWindow : public PickerWindowBase {
1698 BuildRailDepotWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1699 {
1700 this->InitNested(TRANSPORT_RAIL);
1701 this->LowerWidget(_build_depot_direction + WID_BRAD_DEPOT_NE);
1702 }
1703
1704 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1705 {
1706 if (!IsInsideMM(widget, WID_BRAD_DEPOT_NE, WID_BRAD_DEPOT_NW + 1)) return;
1707
1708 size->width = ScaleGUITrad(64) + 2;
1709 size->height = ScaleGUITrad(48) + 2;
1710 }
1711
1712 virtual void DrawWidget(const Rect &r, int widget) const
1713 {
1714 if (!IsInsideMM(widget, WID_BRAD_DEPOT_NE, WID_BRAD_DEPOT_NW + 1)) return;
1715
1716 DrawTrainDepotSprite(r.left + 1 + ScaleGUITrad(31), r.bottom - ScaleGUITrad(31), widget - WID_BRAD_DEPOT_NE + DIAGDIR_NE, _cur_railtype);
1717 }
1718
1719 virtual void OnClick(Point pt, int widget, int click_count)
1720 {
1721 switch (widget) {
1722 case WID_BRAD_DEPOT_NE:
1723 case WID_BRAD_DEPOT_SE:
1724 case WID_BRAD_DEPOT_SW:
1725 case WID_BRAD_DEPOT_NW:
1726 this->RaiseWidget(_build_depot_direction + WID_BRAD_DEPOT_NE);
1727 _build_depot_direction = (DiagDirection)(widget - WID_BRAD_DEPOT_NE);
1728 this->LowerWidget(_build_depot_direction + WID_BRAD_DEPOT_NE);
1729 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1730 this->SetDirty();
1731 break;
1732 }
1733 }
1734 };
1735
1737 static const NWidgetPart _nested_build_depot_widgets[] = {
1738 NWidget(NWID_HORIZONTAL),
1739 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1740 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_BUILD_DEPOT_TRAIN_ORIENTATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1741 EndContainer(),
1742 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1743 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1744 NWidget(NWID_HORIZONTAL_LTR),
1745 NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
1746 NWidget(NWID_VERTICAL),
1747 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_NW), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1748 EndContainer(),
1749 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1750 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_SW), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1751 EndContainer(),
1752 EndContainer(),
1753 NWidget(NWID_SPACER), SetMinimalSize(2, 0),
1754 NWidget(NWID_VERTICAL),
1755 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_NE), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1756 EndContainer(),
1757 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1758 NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_SE), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1759 EndContainer(),
1760 EndContainer(),
1761 NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
1762 EndContainer(),
1763 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1764 EndContainer(),
1765 };
1766
1767 static WindowDesc _build_depot_desc(
1768 WDP_AUTO, NULL, 0, 0,
1769 WC_BUILD_DEPOT, WC_BUILD_TOOLBAR,
1770 WDF_CONSTRUCTION,
1771 _nested_build_depot_widgets, lengthof(_nested_build_depot_widgets)
1772 );
1773
1774 static void ShowBuildTrainDepotPicker(Window *parent)
1775 {
1776 new BuildRailDepotWindow(&_build_depot_desc, parent);
1777 }
1778
1779 struct BuildRailWaypointWindow : PickerWindowBase {
1780 BuildRailWaypointWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1781 {
1782 this->CreateNestedTree();
1783
1784 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX);
1785 matrix->SetScrollbar(this->GetScrollbar(WID_BRW_SCROLL));
1786
1787 this->FinishInitNested(TRANSPORT_RAIL);
1788
1789 matrix->SetCount(_waypoint_count);
1790 matrix->SetClicked(_cur_waypoint_type);
1791 }
1792
1793 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1794 {
1795 switch (widget) {
1796 case WID_BRW_WAYPOINT_MATRIX:
1797 /* Three blobs high and wide. */
1798 size->width += resize->width * 2;
1799 size->height += resize->height * 2;
1800
1801 /* Resizing in X direction only at blob size, but at pixel level in Y. */
1802 resize->height = 1;
1803 break;
1804
1805 case WID_BRW_WAYPOINT:
1806 size->width = ScaleGUITrad(64) + 2;
1807 size->height = ScaleGUITrad(58) + 2;
1808 break;
1809 }
1810 }
1811
1812 virtual void DrawWidget(const Rect &r, int widget) const
1813 {
1814 switch (GB(widget, 0, 16)) {
1815 case WID_BRW_WAYPOINT: {
1816 byte type = GB(widget, 16, 16);
1817 const StationSpec *statspec = StationClass::Get(STAT_CLASS_WAYP)->GetSpec(type);
1818 DrawWaypointSprite(r.left + 1 + ScaleGUITrad(31), r.bottom - ScaleGUITrad(31), type, _cur_railtype);
1819
1820 if (!IsStationAvailable(statspec)) {
1821 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
1822 }
1823 }
1824 }
1825 }
1826
1827 virtual void OnClick(Point pt, int widget, int click_count)
1828 {
1829 switch (GB(widget, 0, 16)) {
1830 case WID_BRW_WAYPOINT: {
1831 byte type = GB(widget, 16, 16);
1832 this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX)->SetClicked(_cur_waypoint_type);
1833
1834 /* Check station availability callback */
1835 const StationSpec *statspec = StationClass::Get(STAT_CLASS_WAYP)->GetSpec(type);
1836 if (!IsStationAvailable(statspec)) return;
1837
1838 _cur_waypoint_type = type;
1839 this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX)->SetClicked(_cur_waypoint_type);
1840 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1841 this->SetDirty();
1842 break;
1843 }
1844 }
1845 }
1846 };
1847
1849 static const NWidgetPart _nested_build_waypoint_widgets[] = {
1850 NWidget(NWID_HORIZONTAL),
1851 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1852 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_WAYPOINT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1853 NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
1854 EndContainer(),
1855 NWidget(NWID_HORIZONTAL),
1856 NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BRW_WAYPOINT_MATRIX), SetPIP(3, 2, 3), SetScrollbar(WID_BRW_SCROLL),
1857 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BRW_WAYPOINT), SetMinimalSize(66, 60), SetDataTip(0x0, STR_WAYPOINT_GRAPHICS_TOOLTIP), SetScrollbar(WID_BRW_SCROLL), EndContainer(),
1858 EndContainer(),
1859 NWidget(NWID_VERTICAL),
1860 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BRW_SCROLL),
1861 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
1862 EndContainer(),
1863 EndContainer(),
1864 };
1865
1866 static WindowDesc _build_waypoint_desc(
1867 WDP_AUTO, "build_waypoint", 0, 0,
1868 WC_BUILD_WAYPOINT, WC_BUILD_TOOLBAR,
1869 WDF_CONSTRUCTION,
1870 _nested_build_waypoint_widgets, lengthof(_nested_build_waypoint_widgets)
1871 );
1872
1873 static void ShowBuildWaypointPicker(Window *parent)
1874 {
1875 new BuildRailWaypointWindow(&_build_waypoint_desc, parent);
1876 }
1877
1881 void InitializeRailGui()
1882 {
1883 _build_depot_direction = DIAGDIR_NW;
1884 }
1885
1890 void ReinitGuiAfterToggleElrail(bool disable)
1891 {
1892 extern RailType _last_built_railtype;
1893 if (disable && _last_built_railtype == RAILTYPE_ELECTRIC) {
1894 _last_built_railtype = _cur_railtype = RAILTYPE_RAIL;
1895 BuildRailToolbarWindow *w = dynamic_cast<BuildRailToolbarWindow *>(FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL));
1896 if (w != NULL) w->ModifyRailType(_cur_railtype);
1897 }
1898 MarkWholeScreenDirty();
1899 }
1900
1902 static void SetDefaultRailGui()
1903 {
1904 if (_local_company == COMPANY_SPECTATOR || !Company::IsValidID(_local_company)) return;
1905
1906 extern RailType _last_built_railtype;
1907 RailType rt = (RailType)(_settings_client.gui.default_rail_type + RAILTYPE_END);
1908 if (rt == DEF_RAILTYPE_MOST_USED) {
1909 /* Find the most used rail type */
1910 RailType count[RAILTYPE_END];
1911 memset(count, 0, sizeof(count));
1912 for (TileIndex t = 0; t < MapSize(); t++) {
1913 if (IsTileType(t, MP_RAILWAY) || IsLevelCrossingTile(t) || HasStationTileRail(t) ||
1914 (IsTileType(t, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL)) {
1915 count[GetRailType(t)]++;
1916 }
1917 }
1918
1919 rt = RAILTYPE_RAIL;
1920 for (RailType r = RAILTYPE_ELECTRIC; r < RAILTYPE_END; r++) {
1921 if (count[r] >= count[rt]) rt = r;
1922 }
1923
1924 /* No rail, just get the first available one */
1925 if (count[rt] == 0) rt = DEF_RAILTYPE_FIRST;
1926 }
1927 switch (rt) {
1928 case DEF_RAILTYPE_FIRST:
1929 rt = RAILTYPE_RAIL;
1930 while (rt < RAILTYPE_END && !HasRailtypeAvail(_local_company, rt)) rt++;
1931 break;
1932
1933 case DEF_RAILTYPE_LAST:
1934 rt = GetBestRailtype(_local_company);
1935 break;
1936
1937 default:
1938 break;
1939 }
1940
1941 _last_built_railtype = _cur_railtype = rt;
1942 BuildRailToolbarWindow *w = dynamic_cast<BuildRailToolbarWindow *>(FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL));
1943 if (w != NULL) w->ModifyRailType(_cur_railtype);
1944 }
1945
1952 bool ResetSignalVariant(int32 p)
1953 {
1954 SignalVariant new_variant = (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC);
1955
1956 if (new_variant != _cur_signal_variant) {
1957 Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
1958 if (w != NULL) {
1959 w->SetDirty();
1960 w->RaiseWidget((_cur_signal_variant == SIG_ELECTRIC ? WID_BS_ELECTRIC_NORM : WID_BS_SEMAPHORE_NORM) + _cur_signal_type);
1961 }
1962 _cur_signal_variant = new_variant;
1963 }
1964
1965 return true;
1966 }
1967
1972 void InitializeRailGUI()
1973 {
1974 SetDefaultRailGui();
1975
1976 _convert_signal_button = false;
1977 _cur_signal_type = _default_signal_type[_settings_client.gui.default_signal_type];
1978 ResetSignalVariant();
1979 }
1980
1986 DropDownList *GetRailTypeDropDownList(bool for_replacement)
1987 {
1988 RailTypes used_railtypes = RAILTYPES_NONE;
1989
1990 /* Find the used railtypes. */
1991 Engine *e;
1992 FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
1993 if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue;
1994
1995 used_railtypes |= GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes;
1996 }
1997
1998 /* Get the date introduced railtypes as well. */
1999 used_railtypes = AddDateIntroducedRailTypes(used_railtypes, MAX_DAY);
2000
2001 const Company *c = Company::Get(_local_company);
2002 DropDownList *list = new DropDownList();
2003 RailType rt;
2004 FOR_ALL_SORTED_RAILTYPES(rt) {
2005 /* If it's not used ever, don't show it to the user. */
2006 if (!HasBit(used_railtypes, rt)) continue;
2007
2008 const RailtypeInfo *rti = GetRailTypeInfo(rt);
2009
2010 StringID str = for_replacement ? rti->strings.replace_text : (rti->max_speed > 0 ? STR_TOOLBAR_RAILTYPE_VELOCITY : STR_JUST_STRING);
2011 DropDownListParamStringItem *item = new DropDownListParamStringItem(str, rt, !HasBit(c->avail_railtypes, rt));
2012 item->SetParam(0, rti->strings.menu_text);
2013 item->SetParam(1, rti->max_speed);
2014 *list->Append() = item;
2015 }
2016 return list;
2017 }