OpenTTD
rail_gui.cpp
Go to the documentation of this file.
1 /* $Id: rail_gui.cpp 26906 2014-09-22 08:01:25Z peter1138 $ */
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 
49 static byte _waypoint_count = 1;
50 static byte _cur_waypoint_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 
60 
61  bool newstations;
63  byte station_type;
65 };
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 
87 }
88 
89 void CcPlaySound1E(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,
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  CcPlaySound1E);
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 
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 
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 {
158  return;
159  }
160 
161  Axis axis = GetAxisForNewWaypoint(tile);
162  if (IsValidAxis(axis)) {
163  /* Valid tile for waypoints */
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 {
189  VpSetPlaceSizingLimit(-1);
192  VpSetPlaceSizingLimit(_settings_game.station.station_spread);
193  } else {
195  uint32 p2 = _railstation.station_class | _railstation.station_type << 8 | INVALID_STATION << 16;
196 
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 
212 {
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 
226  DoCommandP(tile, track, 0, CMD_REMOVE_SIGNALS | CMD_MSG(STR_ERROR_CAN_T_REMOVE_SIGNALS_FROM), CcPlaySound1E);
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);
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  CcPlaySound1E);
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 {
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 {
281  }
282 }
283 
289 {
294  SetSelectionRed(_remove_button_clicked);
295 }
296 
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)) {
310  return true;
311  }
312  }
313 
314  return false;
315 }
316 
317 
324 {
325  if (w->IsWidgetDisabled(WID_RAT_REMOVE)) return;
327  if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
328 
329  /* handle station builder */
332  /* starting drag & drop remove */
334  SetTileSelectSize(1, 1);
335  } else {
336  VpSetPlaceSizingLimit(-1);
337  }
338  } else {
339  /* starting station build mode */
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),
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  CcPlaySound1E);
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 
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);
398  } else {
399  SB(p2, 3, 1, 0);
401  SB(p2, 6, 1, _ctrl_pressed);
402  SB(p2, 7, 3, _default_signal_type[_settings_client.gui.default_signal_type]);
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,
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  CcPlaySound1E);
414 }
415 
416 
421 
423  {
424  this->InitNested(TRANSPORT_RAIL);
425  this->SetupRailToolbar(railtype);
428 
430  }
431 
433  {
435  }
436 
442  {
443  this->railtype = railtype;
444  const RailtypeInfo *rti = GetRailTypeInfo(railtype);
445 
446  assert(railtype < RAILTYPE_END);
447  this->GetWidget<NWidgetCore>(WID_RAT_BUILD_NS)->widget_data = rti->gui_sprites.build_ns_rail;
449  this->GetWidget<NWidgetCore>(WID_RAT_BUILD_EW)->widget_data = rti->gui_sprites.build_ew_rail;
451  this->GetWidget<NWidgetCore>(WID_RAT_AUTORAIL)->widget_data = rti->gui_sprites.auto_rail;
453  this->GetWidget<NWidgetCore>(WID_RAT_CONVERT_RAIL)->widget_data = rti->gui_sprites.convert_rail;
455  }
456 
462  {
463  this->SetupRailToolbar(railtype);
464  this->ReInit();
465  }
466 
467  void UpdateRemoveWidgetStatus(int clicked_widget)
468  {
469  switch (clicked_widget) {
470  case WID_RAT_REMOVE:
471  /* If it is the removal button that has been clicked, do nothing,
472  * as it is up to the other buttons to drive removal status */
473  return;
474 
475  case WID_RAT_BUILD_NS:
476  case WID_RAT_BUILD_X:
477  case WID_RAT_BUILD_EW:
478  case WID_RAT_BUILD_Y:
479  case WID_RAT_AUTORAIL:
483  /* Removal button is enabled only if the rail/signal/waypoint/station
484  * button is still lowered. Once raised, it has to be disabled */
485  this->SetWidgetDisabledState(WID_RAT_REMOVE, !this->IsWidgetLowered(clicked_widget));
486  break;
487 
488  default:
489  /* When any other buttons than rail/signal/waypoint/station, raise and
490  * disable the removal button */
493  break;
494  }
495  }
496 
497  virtual void SetStringParameters(int widget) const
498  {
499  if (widget == WID_RAT_CAPTION) {
500  const RailtypeInfo *rti = GetRailTypeInfo(this->railtype);
501  if (rti->max_speed > 0) {
502  SetDParam(0, STR_TOOLBAR_RAILTYPE_VELOCITY);
504  SetDParam(2, rti->max_speed);
505  } else {
507  }
508  }
509  }
510 
511  virtual void OnClick(Point pt, int widget, int click_count)
512  {
513  if (widget < WID_RAT_BUILD_NS) return;
514 
515  _remove_button_clicked = false;
516  switch (widget) {
517  case WID_RAT_BUILD_NS:
519  this->last_user_action = widget;
520  break;
521 
522  case WID_RAT_BUILD_X:
524  this->last_user_action = widget;
525  break;
526 
527  case WID_RAT_BUILD_EW:
529  this->last_user_action = widget;
530  break;
531 
532  case WID_RAT_BUILD_Y:
534  this->last_user_action = widget;
535  break;
536 
537  case WID_RAT_AUTORAIL:
539  this->last_user_action = widget;
540  break;
541 
542  case WID_RAT_DEMOLISH:
544  this->last_user_action = widget;
545  break;
546 
547  case WID_RAT_BUILD_DEPOT:
549  ShowBuildTrainDepotPicker(this);
550  this->last_user_action = widget;
551  }
552  break;
553 
555  this->last_user_action = widget;
557  if (HandlePlacePushButton(this, WID_RAT_BUILD_WAYPOINT, SPR_CURSOR_WAYPOINT, HT_RECT) && _waypoint_count > 1) {
558  ShowBuildWaypointPicker(this);
559  }
560  break;
561 
563  if (HandlePlacePushButton(this, WID_RAT_BUILD_STATION, SPR_CURSOR_RAIL_STATION, HT_RECT)) {
564  ShowStationBuilder(this);
565  this->last_user_action = widget;
566  }
567  break;
568 
569  case WID_RAT_BUILD_SIGNALS: {
570  this->last_user_action = widget;
573  ShowSignalBuilder(this);
574  }
575  break;
576  }
577 
579  HandlePlacePushButton(this, WID_RAT_BUILD_BRIDGE, SPR_CURSOR_BRIDGE, HT_RECT);
580  this->last_user_action = widget;
581  break;
582 
585  this->last_user_action = widget;
586  break;
587 
588  case WID_RAT_REMOVE:
589  BuildRailClick_Remove(this);
590  break;
591 
594  this->last_user_action = widget;
595  break;
596 
597  default: NOT_REACHED();
598  }
599  this->UpdateRemoveWidgetStatus(widget);
601  }
602 
603  virtual EventState OnHotkey(int hotkey)
604  {
605  MarkTileDirtyByTile(TileVirtXY(_thd.pos.x, _thd.pos.y)); // redraw tile selection
606  return Window::OnHotkey(hotkey);
607  }
608 
609  virtual void OnPlaceObject(Point pt, TileIndex tile)
610  {
611  switch (this->last_user_action) {
612  case WID_RAT_BUILD_NS:
614  break;
615 
616  case WID_RAT_BUILD_X:
618  break;
619 
620  case WID_RAT_BUILD_EW:
622  break;
623 
624  case WID_RAT_BUILD_Y:
626  break;
627 
628  case WID_RAT_AUTORAIL:
630  break;
631 
632  case WID_RAT_DEMOLISH:
634  break;
635 
636  case WID_RAT_BUILD_DEPOT:
638  CMD_BUILD_TRAIN_DEPOT | CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_DEPOT),
639  CcRailDepot);
640  break;
641 
643  PlaceRail_Waypoint(tile);
644  break;
645 
647  PlaceRail_Station(tile);
648  break;
649 
652  break;
653 
655  PlaceRail_Bridge(tile, this);
656  break;
657 
659  DoCommandP(tile, _cur_railtype | (TRANSPORT_RAIL << 8), 0, CMD_BUILD_TUNNEL | CMD_MSG(STR_ERROR_CAN_T_BUILD_TUNNEL_HERE), CcBuildRailTunnel);
660  break;
661 
664  break;
665 
666  default: NOT_REACHED();
667  }
668  }
669 
670  virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
671  {
672  /* no dragging if you have pressed the convert button */
674 
675  VpSelectTilesWithMethod(pt.x, pt.y, select_method);
676  }
677 
678  virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
679  {
680  if (pt.x != -1) {
681  switch (select_proc) {
682  default: NOT_REACHED();
683  case DDSP_BUILD_BRIDGE:
684  if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
685  ShowBuildBridgeWindow(start_tile, end_tile, TRANSPORT_RAIL, _cur_railtype);
686  break;
687 
688  case DDSP_PLACE_RAIL:
689  HandleAutodirPlacement();
690  break;
691 
692  case DDSP_BUILD_SIGNALS:
694  break;
695 
696  case DDSP_DEMOLISH_AREA:
697  GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
698  break;
699 
700  case DDSP_CONVERT_RAIL:
701  DoCommandP(end_tile, start_tile, _cur_railtype | (_ctrl_pressed ? 0x10 : 0), CMD_CONVERT_RAIL | CMD_MSG(STR_ERROR_CAN_T_CONVERT_RAIL), CcPlaySound10);
702  break;
703 
704  case DDSP_REMOVE_STATION:
705  case DDSP_BUILD_STATION:
707  /* Station */
709  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);
710  } else {
711  HandleStationPlacement(start_tile, end_tile);
712  }
713  } else {
714  /* Waypoint */
716  DoCommandP(end_tile, start_tile, _ctrl_pressed ? 0 : 1, CMD_REMOVE_FROM_RAIL_WAYPOINT | CMD_MSG(STR_ERROR_CAN_T_REMOVE_TRAIN_WAYPOINT), CcPlaySound1E);
717  } else {
718  TileArea ta(start_tile, end_tile);
719  uint32 p1 = _cur_railtype | (select_method == VPM_FIX_X ? AXIS_X : AXIS_Y) << 4 | ta.w << 8 | ta.h << 16 | _ctrl_pressed << 24;
720  uint32 p2 = STAT_CLASS_WAYP | _cur_waypoint_type << 8 | INVALID_STATION << 16;
721 
722  CommandContainer cmdcont = { ta.tile, p1, p2, CMD_BUILD_RAIL_WAYPOINT | CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_WAYPOINT), CcPlaySound1E, "" };
723  ShowSelectWaypointIfNeeded(cmdcont, ta);
724  }
725  }
726  break;
727  }
728  }
729  }
730 
731  virtual void OnPlaceObjectAbort()
732  {
733  this->RaiseButtons();
734  this->DisableWidget(WID_RAT_REMOVE);
735  this->SetWidgetDirty(WID_RAT_REMOVE);
736 
743  }
744 
745  virtual void OnPlacePresize(Point pt, TileIndex tile)
746  {
749  }
750 
751  virtual EventState OnCTRLStateChange()
752  {
753  /* do not toggle Remove button by Ctrl when placing station */
754  if (!this->IsWidgetLowered(WID_RAT_BUILD_STATION) && !this->IsWidgetLowered(WID_RAT_BUILD_WAYPOINT) && RailToolbar_CtrlChanged(this)) return ES_HANDLED;
755  return ES_NOT_HANDLED;
756  }
757 
758  static HotkeyList hotkeys;
759 };
760 
767 {
768  if (_game_mode != GM_NORMAL || !CanBuildVehicleInfrastructure(VEH_TRAIN)) return ES_NOT_HANDLED;
769  extern RailType _last_built_railtype;
770  Window *w = ShowBuildRailToolbar(_last_built_railtype);
771  if (w == NULL) return ES_NOT_HANDLED;
772  return w->OnHotkey(hotkey);
773 }
774 
775 const uint16 _railtoolbar_autorail_keys[] = {'5', 'A' | WKC_GLOBAL_HOTKEY, 0};
776 
777 static Hotkey railtoolbar_hotkeys[] = {
778  Hotkey('1', "build_ns", WID_RAT_BUILD_NS),
779  Hotkey('2', "build_x", WID_RAT_BUILD_X),
780  Hotkey('3', "build_ew", WID_RAT_BUILD_EW),
781  Hotkey('4', "build_y", WID_RAT_BUILD_Y),
782  Hotkey(_railtoolbar_autorail_keys, "autorail", WID_RAT_AUTORAIL),
783  Hotkey('6', "demolish", WID_RAT_DEMOLISH),
784  Hotkey('7', "depot", WID_RAT_BUILD_DEPOT),
785  Hotkey('8', "waypoint", WID_RAT_BUILD_WAYPOINT),
786  Hotkey('9', "station", WID_RAT_BUILD_STATION),
787  Hotkey('S', "signal", WID_RAT_BUILD_SIGNALS),
788  Hotkey('B', "bridge", WID_RAT_BUILD_BRIDGE),
789  Hotkey('T', "tunnel", WID_RAT_BUILD_TUNNEL),
790  Hotkey('R', "remove", WID_RAT_REMOVE),
791  Hotkey('C', "convert", WID_RAT_CONVERT_RAIL),
792  HOTKEY_LIST_END
793 };
794 HotkeyList BuildRailToolbarWindow::hotkeys("railtoolbar", railtoolbar_hotkeys, RailToolbarGlobalHotkeys);
795 
796 static const NWidgetPart _nested_build_rail_widgets[] = {
798  NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
799  NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_RAT_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
800  NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
801  EndContainer(),
803  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_NS),
804  SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NS, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
805  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_X),
806  SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NE, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
807  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_EW),
808  SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_EW, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
809  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_Y),
810  SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NW, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
811  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_AUTORAIL),
812  SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_AUTORAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_AUTORAIL),
813 
814  NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), SetDataTip(0x0, STR_NULL), EndContainer(),
815 
816  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_DEMOLISH),
817  SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
818  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_DEPOT),
819  SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DEPOT_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_TRAIN_DEPOT_FOR_BUILDING),
820  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_WAYPOINT),
821  SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_WAYPOINT, STR_RAIL_TOOLBAR_TOOLTIP_CONVERT_RAIL_TO_WAYPOINT),
822  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_STATION),
823  SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_RAIL_STATION, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_STATION),
824  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_SIGNALS),
825  SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_SIGNALS, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_SIGNALS),
826  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_BRIDGE),
827  SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_BRIDGE, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_BRIDGE),
828  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_BUILD_TUNNEL),
829  SetFill(0, 1), SetMinimalSize(20, 22), SetDataTip(SPR_IMG_TUNNEL_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TUNNEL),
830  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_REMOVE),
831  SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_REMOVE, STR_RAIL_TOOLBAR_TOOLTIP_TOGGLE_BUILD_REMOVE_FOR),
832  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_RAT_CONVERT_RAIL),
833  SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_CONVERT_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_CONVERT_RAIL),
834  EndContainer(),
835 };
836 
837 static WindowDesc _build_rail_desc(
838  WDP_ALIGN_TOOLBAR, "toolbar_rail", 0, 0,
841  _nested_build_rail_widgets, lengthof(_nested_build_rail_widgets),
842  &BuildRailToolbarWindow::hotkeys
843 );
844 
845 
855 {
856  if (!Company::IsValidID(_local_company)) return NULL;
857  if (!ValParamRailtype(railtype)) return NULL;
858 
860  _cur_railtype = railtype;
861  _remove_button_clicked = false;
862  return new BuildRailToolbarWindow(&_build_rail_desc, railtype);
863 }
864 
865 /* TODO: For custom stations, respect their allowed platforms/lengths bitmasks!
866  * --pasky */
867 
868 static void HandleStationPlacement(TileIndex start, TileIndex end)
869 {
870  TileArea ta(start, end);
871  uint numtracks = ta.w;
872  uint platlength = ta.h;
873 
874  if (_railstation.orientation == AXIS_X) Swap(numtracks, platlength);
875 
876  uint32 p1 = _cur_railtype | _railstation.orientation << 4 | numtracks << 8 | platlength << 16 | _ctrl_pressed << 24;
877  uint32 p2 = _railstation.station_class | _railstation.station_type << 8 | INVALID_STATION << 16;
878 
879  CommandContainer cmdcont = { ta.tile, p1, p2, CMD_BUILD_RAIL_STATION | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION), CcStation, "" };
880  ShowSelectStationIfNeeded(cmdcont, ta);
881 }
882 
884 private:
885  uint line_height;
889 
895  void CheckSelectedSize(const StationSpec *statspec)
896  {
897  if (statspec == NULL || _settings_client.gui.station_dragdrop) return;
898 
899  /* If current number of tracks is not allowed, make it as big as possible */
903  if (statspec->disallowed_platforms != UINT8_MAX) {
906  }
908  }
909  }
910 
914  if (statspec->disallowed_lengths != UINT8_MAX) {
917  }
919  }
920  }
921  }
922 
923 public:
924  BuildRailStationWindow(WindowDesc *desc, Window *parent, bool newstation) : PickerWindowBase(desc, parent)
925  {
927  this->vscroll = NULL;
928  _railstation.newstations = newstation;
929 
930  this->CreateNestedTree();
931  NWidgetStacked *newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_ADDITIONS);
932  newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
933  newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_MATRIX);
934  newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
935  newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_DEFSIZE);
936  newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
937  newst_additions = this->GetWidget<NWidgetStacked>(WID_BRAS_SHOW_NEWST_RESIZE);
938  newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
939  if (newstation) {
942  }
944 
945  this->LowerWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
948  } else {
951  }
954 
955  if (!newstation || _railstation.station_class >= (int)StationClass::GetClassCount()) {
956  /* New stations are not available or changed, so ensure the default station
957  * type is 'selected'. */
958  _railstation.station_class = STAT_CLASS_DFLT;
959  _railstation.station_type = 0;
960  this->vscroll2 = NULL;
961  }
962  if (newstation) {
963  _railstation.station_count = StationClass::Get(_railstation.station_class)->GetSpecCount();
964  _railstation.station_type = min(_railstation.station_type, _railstation.station_count - 1);
965 
966  int count = 0;
967  for (uint i = 0; i < StationClass::GetClassCount(); i++) {
968  if (i == STAT_CLASS_WAYP) continue;
969  count++;
970  }
971  this->vscroll->SetCount(count);
972  this->vscroll->SetPosition(Clamp(_railstation.station_class - 2, 0, max(this->vscroll->GetCount() - this->vscroll->GetCapacity(), 0)));
973 
974  NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX);
975  matrix->SetScrollbar(this->vscroll2);
976  matrix->SetCount(_railstation.station_count);
977  matrix->SetClicked(_railstation.station_type);
978  }
979  }
980 
981  virtual ~BuildRailStationWindow()
982  {
984  }
985 
986  virtual void OnPaint()
987  {
988  bool newstations = _railstation.newstations;
989  const StationSpec *statspec = newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : NULL;
990 
992  SetTileSelectSize(1, 1);
993  } else {
996  if (_railstation.orientation == AXIS_X) Swap(x, y);
997  if (!_remove_button_clicked) {
998  SetTileSelectSize(x, y);
999  }
1000  }
1001 
1003 
1004  if (_settings_client.gui.station_show_coverage) SetTileSelectBigSize(-rad, -rad, 2 * rad, 2 * rad);
1005 
1006  for (uint bits = 0; bits < 7; bits++) {
1007  bool disable = bits >= _settings_game.station.station_spread;
1008  if (statspec == NULL) {
1009  this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_NUM_1, disable);
1010  this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_LEN_1, disable);
1011  } else {
1012  this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_NUM_1, HasBit(statspec->disallowed_platforms, bits) || disable);
1013  this->SetWidgetDisabledState(bits + WID_BRAS_PLATFORM_LEN_1, HasBit(statspec->disallowed_lengths, bits) || disable);
1014  }
1015  }
1016 
1017  this->DrawWidgets();
1018 
1019  /* 'Accepts' and 'Supplies' texts. */
1020  NWidgetBase *cov = this->GetWidget<NWidgetBase>(WID_BRAS_COVERAGE_TEXTS);
1021  int top = cov->pos_y + WD_PAR_VSEP_NORMAL;
1022  int left = cov->pos_x + WD_FRAMERECT_LEFT;
1023  int right = cov->pos_x + cov->current_x - WD_FRAMERECT_RIGHT;
1024  int bottom = cov->pos_y + cov->current_y;
1025  top = DrawStationCoverageAreaText(left, right, top, SCT_ALL, rad, false) + WD_PAR_VSEP_NORMAL;
1026  top = DrawStationCoverageAreaText(left, right, top, SCT_ALL, rad, true) + WD_PAR_VSEP_NORMAL;
1027  /* Resize background if the window is too small.
1028  * Never make the window smaller to avoid oscillating if the size change affects the acceptance.
1029  * (This is the case, if making the window bigger moves the mouse into the window.) */
1030  if (top > bottom) {
1031  this->coverage_height += top - bottom;
1032  this->ReInit();
1033  }
1034  }
1035 
1036  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1037  {
1038  switch (widget) {
1039  case WID_BRAS_NEWST_LIST: {
1040  Dimension d = {0, 0};
1041  for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1042  if (i == STAT_CLASS_WAYP) continue;
1044  }
1045  size->width = max(size->width, d.width + padding.width);
1047  size->height = 5 * this->line_height;
1048  resize->height = this->line_height;
1049  break;
1050  }
1051 
1052  case WID_BRAS_SHOW_NEWST_TYPE: {
1053  if (!_railstation.newstations) {
1054  size->width = 0;
1055  size->height = 0;
1056  break;
1057  }
1058 
1059  /* If newstations exist, compute the non-zero minimal size. */
1060  Dimension d = {0, 0};
1061  StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
1062  for (StationClassID statclass = STAT_CLASS_BEGIN; statclass < (StationClassID)StationClass::GetClassCount(); statclass++) {
1063  if (statclass == STAT_CLASS_WAYP) continue;
1064  StationClass *stclass = StationClass::Get(statclass);
1065  for (uint16 j = 0; j < stclass->GetSpecCount(); j++) {
1066  const StationSpec *statspec = stclass->GetSpec(j);
1067  SetDParam(0, (statspec != NULL && statspec->name != 0) ? statspec->name : STR_STATION_CLASS_DFLT);
1068  d = maxdim(d, GetStringBoundingBox(str));
1069  }
1070  }
1071  size->width = max(size->width, d.width + padding.width);
1072  break;
1073  }
1074 
1077  case WID_BRAS_IMAGE:
1078  size->width = UnScaleByZoom(64 * 4, ZOOM_LVL_GUI) + 2;
1079  size->height = UnScaleByZoom(58 * 4, ZOOM_LVL_GUI) + 2;
1080  break;
1081 
1083  size->height = this->coverage_height;
1084  break;
1085 
1086  case WID_BRAS_MATRIX:
1087  fill->height = 1;
1088  resize->height = 1;
1089  break;
1090  }
1091  }
1092 
1093  virtual void DrawWidget(const Rect &r, int widget) const
1094  {
1095  DrawPixelInfo tmp_dpi;
1096 
1097  switch (GB(widget, 0, 16)) {
1099  /* Set up a clipping area for the '/' station preview */
1100  if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1101  DrawPixelInfo *old_dpi = _cur_dpi;
1102  _cur_dpi = &tmp_dpi;
1103  int x = UnScaleByZoom(31 * 4, ZOOM_LVL_GUI) + 1;
1104  int y = r.bottom - r.top - UnScaleByZoom(31 * 4, ZOOM_LVL_GUI);
1105  if (!DrawStationTile(x, y, _cur_railtype, AXIS_X, _railstation.station_class, _railstation.station_type)) {
1106  StationPickerDrawSprite(x, y, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2);
1107  }
1108  _cur_dpi = old_dpi;
1109  }
1110  break;
1111 
1113  /* Set up a clipping area for the '\' station preview */
1114  if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1115  DrawPixelInfo *old_dpi = _cur_dpi;
1116  _cur_dpi = &tmp_dpi;
1117  int x = UnScaleByZoom(31 * 4, ZOOM_LVL_GUI) + 1;
1118  int y = r.bottom - r.top - UnScaleByZoom(31 * 4, ZOOM_LVL_GUI);
1119  if (!DrawStationTile(x, y, _cur_railtype, AXIS_Y, _railstation.station_class, _railstation.station_type)) {
1120  StationPickerDrawSprite(x, y, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 3);
1121  }
1122  _cur_dpi = old_dpi;
1123  }
1124  break;
1125 
1126  case WID_BRAS_NEWST_LIST: {
1127  uint statclass = 0;
1128  uint row = 0;
1129  for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1130  if (i == STAT_CLASS_WAYP) continue;
1131  if (this->vscroll->IsVisible(statclass)) {
1132  DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, row * this->line_height + r.top + WD_MATRIX_TOP,
1134  (StationClassID)i == _railstation.station_class ? TC_WHITE : TC_BLACK);
1135  row++;
1136  }
1137  statclass++;
1138  }
1139  break;
1140  }
1141 
1142  case WID_BRAS_IMAGE: {
1143  byte type = GB(widget, 16, 16);
1144  assert(type < _railstation.station_count);
1145  /* Check station availability callback */
1146  const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(type);
1147  if (!IsStationAvailable(statspec)) {
1148  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
1149  }
1150 
1151  /* Set up a clipping area for the station preview. */
1152  if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
1153  DrawPixelInfo *old_dpi = _cur_dpi;
1154  _cur_dpi = &tmp_dpi;
1155  int x = UnScaleByZoom(31 * 4, ZOOM_LVL_GUI) + 1;
1156  int y = r.bottom - r.top - UnScaleByZoom(31 * 4, ZOOM_LVL_GUI);
1157  if (!DrawStationTile(x, y, _cur_railtype, _railstation.orientation, _railstation.station_class, type)) {
1158  StationPickerDrawSprite(x, y, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2 + _railstation.orientation);
1159  }
1160  _cur_dpi = old_dpi;
1161  }
1162  break;
1163  }
1164  }
1165  }
1166 
1167  virtual void OnResize()
1168  {
1169  if (this->vscroll != NULL) { // New stations available.
1171  }
1172  }
1173 
1174  virtual void SetStringParameters(int widget) const
1175  {
1176  if (widget == WID_BRAS_SHOW_NEWST_TYPE) {
1177  const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type);
1178  SetDParam(0, (statspec != NULL && statspec->name != 0) ? statspec->name : STR_STATION_CLASS_DFLT);
1179  }
1180  }
1181 
1182  virtual void OnClick(Point pt, int widget, int click_count)
1183  {
1184  switch (GB(widget, 0, 16)) {
1187  this->RaiseWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
1188  _railstation.orientation = (Axis)(widget - WID_BRAS_PLATFORM_DIR_X);
1189  this->LowerWidget(_railstation.orientation + WID_BRAS_PLATFORM_DIR_X);
1190  if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1191  this->SetDirty();
1193  break;
1194 
1201  case WID_BRAS_PLATFORM_NUM_7: {
1204 
1207 
1209 
1210  const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : NULL;
1211  if (statspec != NULL && HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
1212  /* The previously selected number of platforms in invalid */
1213  for (uint i = 0; i < 7; i++) {
1214  if (!HasBit(statspec->disallowed_lengths, i)) {
1217  break;
1218  }
1219  }
1220  }
1221 
1222  this->LowerWidget(_settings_client.gui.station_numtracks + WID_BRAS_PLATFORM_NUM_BEGIN);
1224  if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1225  this->SetDirty();
1227  break;
1228  }
1229 
1236  case WID_BRAS_PLATFORM_LEN_7: {
1239 
1242 
1244 
1245  const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : NULL;
1246  if (statspec != NULL && HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
1247  /* The previously selected number of tracks in invalid */
1248  for (uint i = 0; i < 7; i++) {
1249  if (!HasBit(statspec->disallowed_platforms, i)) {
1252  break;
1253  }
1254  }
1255  }
1256 
1258  this->LowerWidget(_settings_client.gui.station_platlength + WID_BRAS_PLATFORM_LEN_BEGIN);
1259  if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1260  this->SetDirty();
1262  break;
1263  }
1264 
1267 
1269 
1270  /* get the first allowed length/number of platforms */
1271  const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class)->GetSpec(_railstation.station_type) : NULL;
1272  if (statspec != NULL && HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
1273  for (uint i = 0; i < 7; i++) {
1274  if (!HasBit(statspec->disallowed_lengths, i)) {
1277  break;
1278  }
1279  }
1280  }
1281  if (statspec != NULL && HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
1282  for (uint i = 0; i < 7; i++) {
1283  if (!HasBit(statspec->disallowed_platforms, i)) {
1286  break;
1287  }
1288  }
1289  }
1290 
1293  if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1294  this->SetDirty();
1296  break;
1297  }
1298 
1300  case WID_BRAS_HIGHLIGHT_ON:
1302 
1305  if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1306  this->SetDirty();
1307  break;
1308 
1309  case WID_BRAS_NEWST_LIST: {
1310  int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_BRAS_NEWST_LIST, 0, this->line_height);
1311  if (y >= (int)StationClass::GetClassCount()) return;
1312  for (uint i = 0; i < StationClass::GetClassCount(); i++) {
1313  if (i == STAT_CLASS_WAYP) continue;
1314  if (y == 0) {
1315  if (_railstation.station_class != (StationClassID)i) {
1316  _railstation.station_class = (StationClassID)i;
1317  StationClass *stclass = StationClass::Get(_railstation.station_class);
1318  _railstation.station_count = stclass->GetSpecCount();
1319  _railstation.station_type = min((int)_railstation.station_type, max(0, (int)_railstation.station_count - 1));
1320 
1321  this->CheckSelectedSize(stclass->GetSpec(_railstation.station_type));
1322 
1323  NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX);
1324  matrix->SetCount(_railstation.station_count);
1325  matrix->SetClicked(_railstation.station_type);
1326  }
1327  if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1328  this->SetDirty();
1330  break;
1331  }
1332  y--;
1333  }
1334  break;
1335  }
1336 
1337  case WID_BRAS_IMAGE: {
1338  int y = GB(widget, 16, 16);
1339  if (y >= _railstation.station_count) return;
1340 
1341  /* Check station availability callback */
1342  const StationSpec *statspec = StationClass::Get(_railstation.station_class)->GetSpec(y);
1343  if (!IsStationAvailable(statspec)) return;
1344 
1345  _railstation.station_type = y;
1346 
1347  this->CheckSelectedSize(statspec);
1348  this->GetWidget<NWidgetMatrix>(WID_BRAS_MATRIX)->SetClicked(_railstation.station_type);
1349 
1350  if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1351  this->SetDirty();
1353  break;
1354  }
1355  }
1356  }
1357 
1358  virtual void OnTick()
1359  {
1361  }
1362 };
1363 
1364 static const NWidgetPart _nested_station_builder_widgets[] = {
1366  NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1367  NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_STATION_BUILD_RAIL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1369  NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
1370  EndContainer(),
1371  EndContainer(),
1372  NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1376  NWidget(NWID_HORIZONTAL), SetPIP(7, 0, 7), SetPadding(2, 0, 1, 0),
1377  NWidget(WWT_MATRIX, COLOUR_GREY, WID_BRAS_NEWST_LIST), SetMinimalSize(122, 71), SetFill(1, 0),
1378  SetMatrixDataTip(1, 0, STR_STATION_BUILD_STATION_CLASS_TOOLTIP), SetScrollbar(WID_BRAS_NEWST_SCROLL),
1380  EndContainer(),
1381  EndContainer(),
1382  NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_ORIENTATION, STR_NULL), SetPadding(1, 2, 0, 2),
1384  NWidget(NWID_SPACER), SetMinimalSize(7, 0), SetFill(1, 0),
1385  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(),
1386  NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1387  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(),
1388  NWidget(NWID_SPACER), SetMinimalSize(7, 0), SetFill(1, 0),
1389  EndContainer(),
1390  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),
1391  NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_NUMBER_OF_TRACKS, STR_NULL), SetPadding(0, 2, 0, 2),
1393  NWidget(NWID_SPACER), SetFill(1, 0),
1394  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_1), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_1, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1395  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_2), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_2, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1396  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_3), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_3, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1397  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_4), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_4, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1398  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_5), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_5, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1399  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_6), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_6, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1400  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_NUM_7), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_7, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
1401  NWidget(NWID_SPACER), SetFill(1, 0),
1402  EndContainer(),
1403  NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_PLATFORM_LENGTH, STR_NULL), SetPadding(2, 2, 0, 2),
1405  NWidget(NWID_SPACER), SetFill(1, 0),
1406  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_1), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_1, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1407  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_2), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_2, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1408  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_3), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_3, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1409  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_4), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_4, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1410  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_5), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_5, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1411  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_6), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_6, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1412  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_PLATFORM_LEN_7), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_7, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
1413  NWidget(NWID_SPACER), SetFill(1, 0),
1414  EndContainer(),
1417  NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1418  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),
1419  NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1420  EndContainer(),
1421  NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_COVERAGE_AREA_TITLE, STR_NULL), SetPadding(3, 2, 0, 2),
1423  NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1424  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_HIGHLIGHT_OFF), SetMinimalSize(60, 12),
1425  SetDataTip(STR_STATION_BUILD_COVERAGE_OFF, STR_STATION_BUILD_COVERAGE_AREA_OFF_TOOLTIP),
1426  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BRAS_HIGHLIGHT_ON), SetMinimalSize(60, 12),
1427  SetDataTip(STR_STATION_BUILD_COVERAGE_ON, STR_STATION_BUILD_COVERAGE_AREA_ON_TOOLTIP),
1428  NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
1429  EndContainer(),
1430  EndContainer(),
1432  /* We need an additional background for the matrix, as the matrix cannot handle the scrollbar due to not being an NWidgetCore. */
1433  NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(WID_BRAS_MATRIX_SCROLL),
1435  NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BRAS_MATRIX), SetScrollbar(WID_BRAS_MATRIX_SCROLL), SetPIP(0, 2, 0), SetPadding(2, 0, 0, 0),
1436  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BRAS_IMAGE), SetMinimalSize(66, 60),
1437  SetFill(0, 0), SetResize(0, 0), SetDataTip(0x0, STR_STATION_BUILD_STATION_TYPE_TOOLTIP), SetScrollbar(WID_BRAS_MATRIX_SCROLL),
1438  EndContainer(),
1439  EndContainer(),
1440  NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BRAS_MATRIX_SCROLL),
1441  EndContainer(),
1442  EndContainer(),
1443  EndContainer(),
1444  EndContainer(),
1446  NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BRAS_COVERAGE_TEXTS), SetFill(1, 1), SetResize(1, 0),
1449  NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(0, 1), EndContainer(),
1450  NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
1451  EndContainer(),
1452  EndContainer(),
1453  EndContainer(),
1454  EndContainer(),
1455 };
1456 
1459  WDP_AUTO, "build_station_rail", 350, 0,
1462  _nested_station_builder_widgets, lengthof(_nested_station_builder_widgets)
1463 );
1464 
1466 static void ShowStationBuilder(Window *parent)
1467 {
1468  bool newstations = StationClass::GetClassCount() > 2 || StationClass::Get(STAT_CLASS_DFLT)->GetSpecCount() != 1;
1469  new BuildRailStationWindow(&_station_builder_desc, parent, newstations);
1470 }
1471 
1473 private:
1476 
1484  void DrawSignalSprite(byte widget_index, SpriteID image) const
1485  {
1486  Point offset;
1487  Dimension sprite_size = GetSpriteSize(image, &offset);
1488  const NWidgetBase *widget = this->GetWidget<NWidgetBase>(widget_index);
1489  int x = widget->pos_x - offset.x +
1490  (widget->current_x - sprite_size.width + offset.x) / 2; // centered
1491  int y = widget->pos_y - sig_sprite_bottom_offset + WD_IMGBTN_TOP +
1492  (widget->current_y - WD_IMGBTN_TOP - WD_IMGBTN_BOTTOM + sig_sprite_size.height) / 2; // aligned to bottom
1493 
1494  DrawSprite(image, PAL_NONE,
1495  x + this->IsWidgetLowered(widget_index),
1496  y + this->IsWidgetLowered(widget_index));
1497  }
1498 
1499 public:
1500  BuildSignalWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1501  {
1502  this->InitNested(TRANSPORT_RAIL);
1503  this->OnInvalidateData();
1504  }
1505 
1507  {
1508  _convert_signal_button = false;
1509  }
1510 
1511  virtual void OnInit()
1512  {
1513  /* Calculate maximum signal sprite size. */
1514  this->sig_sprite_size.width = 0;
1515  this->sig_sprite_size.height = 0;
1516  this->sig_sprite_bottom_offset = 0;
1518  for (uint type = SIGTYPE_NORMAL; type < SIGTYPE_END; type++) {
1519  for (uint variant = SIG_ELECTRIC; variant <= SIG_SEMAPHORE; variant++) {
1520  for (uint lowered = 0; lowered < 2; lowered++) {
1521  Point offset;
1522  Dimension sprite_size = GetSpriteSize(rti->gui_sprites.signals[type][variant][lowered], &offset);
1523  this->sig_sprite_bottom_offset = max<int>(this->sig_sprite_bottom_offset, sprite_size.height);
1524  this->sig_sprite_size.width = max<int>(this->sig_sprite_size.width, sprite_size.width - offset.x);
1525  this->sig_sprite_size.height = max<int>(this->sig_sprite_size.height, sprite_size.height - offset.y);
1526  }
1527  }
1528  }
1529  }
1530 
1531  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1532  {
1533  if (widget == WID_BS_DRAG_SIGNALS_DENSITY_LABEL) {
1534  /* Two digits for signals density. */
1535  size->width = max(size->width, 2 * GetDigitWidth() + padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT);
1536  } else if (IsInsideMM(widget, WID_BS_SEMAPHORE_NORM, WID_BS_ELECTRIC_PBS_OWAY + 1)) {
1537  size->width = max(size->width, this->sig_sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT);
1538  size->height = max(size->height, this->sig_sprite_size.height + WD_IMGBTN_TOP + WD_IMGBTN_BOTTOM);
1539  }
1540  }
1541 
1542  virtual void SetStringParameters(int widget) const
1543  {
1544  switch (widget) {
1547  break;
1548  }
1549  }
1550 
1551  virtual void DrawWidget(const Rect &r, int widget) const
1552  {
1554  /* Extract signal from widget number. */
1555  int type = (widget - WID_BS_SEMAPHORE_NORM) % SIGTYPE_END;
1556  int var = SIG_SEMAPHORE - (widget - WID_BS_SEMAPHORE_NORM) / SIGTYPE_END; // SignalVariant order is reversed compared to the widgets.
1557  SpriteID sprite = GetRailTypeInfo(_cur_railtype)->gui_sprites.signals[type][var][this->IsWidgetLowered(widget)];
1558 
1559  this->DrawSignalSprite(widget, sprite);
1560  }
1561  }
1562 
1563  virtual void OnClick(Point pt, int widget, int click_count)
1564  {
1565  switch (widget) {
1566  case WID_BS_SEMAPHORE_NORM:
1568  case WID_BS_SEMAPHORE_EXIT:
1570  case WID_BS_SEMAPHORE_PBS:
1572  case WID_BS_ELECTRIC_NORM:
1573  case WID_BS_ELECTRIC_ENTRY:
1574  case WID_BS_ELECTRIC_EXIT:
1575  case WID_BS_ELECTRIC_COMBO:
1576  case WID_BS_ELECTRIC_PBS:
1579 
1580  _cur_signal_type = (SignalType)((uint)((widget - WID_BS_SEMAPHORE_NORM) % (SIGTYPE_LAST + 1)));
1582 
1583  /* If 'remove' button of rail build toolbar is active, disable it. */
1584  if (_remove_button_clicked) {
1586  if (w != NULL) ToggleRailButton_Remove(w);
1587  }
1588 
1589  break;
1590 
1591  case WID_BS_CONVERT:
1593  break;
1594 
1599  }
1600  break;
1601 
1606  }
1607  break;
1608 
1609  default: break;
1610  }
1611 
1612  this->InvalidateData();
1613  }
1614 
1620  virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1621  {
1622  if (!gui_scope) return;
1624 
1626 
1629  }
1630 };
1631 
1635  NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1636  NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_BUILD_SIGNAL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1637  EndContainer(),
1640  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_NORM), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_NORM_TOOLTIP), EndContainer(), SetFill(1, 1),
1641  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_ENTRY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_ENTRY_TOOLTIP), EndContainer(), SetFill(1, 1),
1642  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_EXIT), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_EXIT_TOOLTIP), EndContainer(), SetFill(1, 1),
1643  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_COMBO), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_COMBO_TOOLTIP), EndContainer(), SetFill(1, 1),
1644  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_SEMAPHORE_PBS), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_PBS_TOOLTIP), EndContainer(), SetFill(1, 1),
1645  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),
1646  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_BS_CONVERT), SetDataTip(SPR_IMG_SIGNAL_CONVERT, STR_BUILD_SIGNAL_CONVERT_TOOLTIP), SetFill(1, 1),
1647  EndContainer(),
1649  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_NORM), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_NORM_TOOLTIP), EndContainer(), SetFill(1, 1),
1650  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_ENTRY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_ENTRY_TOOLTIP), EndContainer(), SetFill(1, 1),
1651  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_EXIT), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_EXIT_TOOLTIP), EndContainer(), SetFill(1, 1),
1652  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_COMBO), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_COMBO_TOOLTIP), EndContainer(), SetFill(1, 1),
1653  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BS_ELECTRIC_PBS), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_PBS_TOOLTIP), EndContainer(), SetFill(1, 1),
1654  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),
1655  NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_DRAG_SIGNALS_DENSITY_TOOLTIP), SetFill(1, 1),
1656  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),
1657  NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1658  NWidget(NWID_SPACER), SetFill(1, 0),
1659  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),
1660  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),
1661  NWidget(NWID_SPACER), SetFill(1, 0),
1662  EndContainer(),
1663  NWidget(NWID_SPACER), SetMinimalSize(0, 2), SetFill(1, 0),
1664  EndContainer(),
1665  EndContainer(),
1666  EndContainer(),
1667 };
1668 
1671  WDP_AUTO, "build_signal", 0, 0,
1674  _nested_signal_builder_widgets, lengthof(_nested_signal_builder_widgets)
1675 );
1676 
1680 static void ShowSignalBuilder(Window *parent)
1681 {
1683 }
1684 
1686  BuildRailDepotWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1687  {
1688  this->InitNested(TRANSPORT_RAIL);
1690  }
1691 
1692  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1693  {
1694  if (!IsInsideMM(widget, WID_BRAD_DEPOT_NE, WID_BRAD_DEPOT_NW + 1)) return;
1695 
1696  size->width = UnScaleByZoom(64 * 4, ZOOM_LVL_GUI) + 2;
1697  size->height = UnScaleByZoom(48 * 4, ZOOM_LVL_GUI) + 2;
1698  }
1699 
1700  virtual void DrawWidget(const Rect &r, int widget) const
1701  {
1702  if (!IsInsideMM(widget, WID_BRAD_DEPOT_NE, WID_BRAD_DEPOT_NW + 1)) return;
1703 
1704  DrawTrainDepotSprite(r.left + 1 + UnScaleByZoom(31 * 4, ZOOM_LVL_GUI), r.bottom - UnScaleByZoom(31 * 4, ZOOM_LVL_GUI), widget - WID_BRAD_DEPOT_NE + DIAGDIR_NE, _cur_railtype);
1705  }
1706 
1707  virtual void OnClick(Point pt, int widget, int click_count)
1708  {
1709  switch (widget) {
1710  case WID_BRAD_DEPOT_NE:
1711  case WID_BRAD_DEPOT_SE:
1712  case WID_BRAD_DEPOT_SW:
1713  case WID_BRAD_DEPOT_NW:
1717  if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1718  this->SetDirty();
1719  break;
1720  }
1721  }
1722 };
1723 
1727  NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1728  NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_BUILD_DEPOT_TRAIN_ORIENTATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1729  EndContainer(),
1730  NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1733  NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
1735  NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_NW), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1736  EndContainer(),
1738  NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_SW), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1739  EndContainer(),
1740  EndContainer(),
1743  NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_NE), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1744  EndContainer(),
1746  NWidget(WWT_PANEL, COLOUR_GREY, WID_BRAD_DEPOT_SE), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
1747  EndContainer(),
1748  EndContainer(),
1749  NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
1750  EndContainer(),
1752  EndContainer(),
1753 };
1754 
1755 static WindowDesc _build_depot_desc(
1756  WDP_AUTO, NULL, 0, 0,
1759  _nested_build_depot_widgets, lengthof(_nested_build_depot_widgets)
1760 );
1761 
1762 static void ShowBuildTrainDepotPicker(Window *parent)
1763 {
1764  new BuildRailDepotWindow(&_build_depot_desc, parent);
1765 }
1766 
1768  BuildRailWaypointWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
1769  {
1770  this->CreateNestedTree();
1771 
1772  NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX);
1773  matrix->SetScrollbar(this->GetScrollbar(WID_BRW_SCROLL));
1774 
1776 
1777  matrix->SetCount(_waypoint_count);
1778  matrix->SetClicked(_cur_waypoint_type);
1779  }
1780 
1781  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1782  {
1783  switch (widget) {
1785  /* Three blobs high and wide. */
1786  size->width += resize->width * 2;
1787  size->height += resize->height * 2;
1788 
1789  /* Resizing in X direction only at blob size, but at pixel level in Y. */
1790  resize->height = 1;
1791  break;
1792 
1793  case WID_BRW_WAYPOINT:
1794  size->width = UnScaleByZoom(64 * 4, ZOOM_LVL_GUI) + 2;
1795  size->height = UnScaleByZoom(58 * 4, ZOOM_LVL_GUI) + 2;
1796  break;
1797  }
1798  }
1799 
1800  virtual void DrawWidget(const Rect &r, int widget) const
1801  {
1802  switch (GB(widget, 0, 16)) {
1803  case WID_BRW_WAYPOINT: {
1804  byte type = GB(widget, 16, 16);
1805  const StationSpec *statspec = StationClass::Get(STAT_CLASS_WAYP)->GetSpec(type);
1806  DrawWaypointSprite(r.left + 1 + UnScaleByZoom(31 * 4, ZOOM_LVL_GUI), r.bottom - UnScaleByZoom(31 * 4, ZOOM_LVL_GUI), type, _cur_railtype);
1807 
1808  if (!IsStationAvailable(statspec)) {
1809  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
1810  }
1811  }
1812  }
1813  }
1814 
1815  virtual void OnClick(Point pt, int widget, int click_count)
1816  {
1817  switch (GB(widget, 0, 16)) {
1818  case WID_BRW_WAYPOINT: {
1819  byte type = GB(widget, 16, 16);
1820  this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX)->SetClicked(_cur_waypoint_type);
1821 
1822  /* Check station availability callback */
1823  const StationSpec *statspec = StationClass::Get(STAT_CLASS_WAYP)->GetSpec(type);
1824  if (!IsStationAvailable(statspec)) return;
1825 
1826  _cur_waypoint_type = type;
1827  this->GetWidget<NWidgetMatrix>(WID_BRW_WAYPOINT_MATRIX)->SetClicked(_cur_waypoint_type);
1828  if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1829  this->SetDirty();
1830  break;
1831  }
1832  }
1833  }
1834 };
1835 
1839  NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1840  NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_WAYPOINT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1841  NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
1842  EndContainer(),
1845  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BRW_WAYPOINT), SetMinimalSize(66, 60), SetDataTip(0x0, STR_WAYPOINT_GRAPHICS_TOOLTIP), SetScrollbar(WID_BRW_SCROLL), EndContainer(),
1846  EndContainer(),
1848  NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BRW_SCROLL),
1849  NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
1850  EndContainer(),
1851  EndContainer(),
1852 };
1853 
1854 static WindowDesc _build_waypoint_desc(
1855  WDP_AUTO, "build_waypoint", 0, 0,
1858  _nested_build_waypoint_widgets, lengthof(_nested_build_waypoint_widgets)
1859 );
1860 
1861 static void ShowBuildWaypointPicker(Window *parent)
1862 {
1863  new BuildRailWaypointWindow(&_build_waypoint_desc, parent);
1864 }
1865 
1870 {
1872 }
1873 
1878 void ReinitGuiAfterToggleElrail(bool disable)
1879 {
1880  extern RailType _last_built_railtype;
1881  if (disable && _last_built_railtype == RAILTYPE_ELECTRIC) {
1882  _last_built_railtype = _cur_railtype = RAILTYPE_RAIL;
1884  if (w != NULL) w->ModifyRailType(_cur_railtype);
1885  }
1887 }
1888 
1890 static void SetDefaultRailGui()
1891 {
1893 
1894  extern RailType _last_built_railtype;
1896  if (rt == DEF_RAILTYPE_MOST_USED) {
1897  /* Find the most used rail type */
1898  RailType count[RAILTYPE_END];
1899  memset(count, 0, sizeof(count));
1900  for (TileIndex t = 0; t < MapSize(); t++) {
1903  count[GetRailType(t)]++;
1904  }
1905  }
1906 
1907  rt = RAILTYPE_RAIL;
1908  for (RailType r = RAILTYPE_ELECTRIC; r < RAILTYPE_END; r++) {
1909  if (count[r] >= count[rt]) rt = r;
1910  }
1911 
1912  /* No rail, just get the first available one */
1913  if (count[rt] == 0) rt = DEF_RAILTYPE_FIRST;
1914  }
1915  switch (rt) {
1916  case DEF_RAILTYPE_FIRST:
1917  rt = RAILTYPE_RAIL;
1918  while (rt < RAILTYPE_END && !HasRailtypeAvail(_local_company, rt)) rt++;
1919  break;
1920 
1921  case DEF_RAILTYPE_LAST:
1923  break;
1924 
1925  default:
1926  break;
1927  }
1928 
1929  _last_built_railtype = _cur_railtype = rt;
1931  if (w != NULL) w->ModifyRailType(_cur_railtype);
1932 }
1933 
1940 bool ResetSignalVariant(int32 p)
1941 {
1943 
1944  if (new_variant != _cur_signal_variant) {
1946  if (w != NULL) {
1947  w->SetDirty();
1949  }
1950  _cur_signal_variant = new_variant;
1951  }
1952 
1953  return true;
1954 }
1955 
1961 {
1963 
1964  _convert_signal_button = false;
1965  _cur_signal_type = _default_signal_type[_settings_client.gui.default_signal_type];
1967 }
1968 
1975 static int CDECL CompareRailTypes(const DropDownListItem * const *first, const DropDownListItem * const *second)
1976 {
1977  return GetRailTypeInfo((RailType)(*first)->result)->sorting_order - GetRailTypeInfo((RailType)(*second)->result)->sorting_order;
1978 }
1979 
1986 {
1987  RailTypes used_railtypes = RAILTYPES_NONE;
1988 
1989  /* Find the used railtypes. */
1990  Engine *e;
1991  FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
1992  if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue;
1993 
1994  used_railtypes |= GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes;
1995  }
1996 
1997  /* Get the date introduced railtypes as well. */
1998  used_railtypes = AddDateIntroducedRailTypes(used_railtypes, MAX_DAY);
1999 
2000  const Company *c = Company::Get(_local_company);
2001  DropDownList *list = new DropDownList();
2002  for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
2003  /* If it's not used ever, don't show it to the user. */
2004  if (!HasBit(used_railtypes, rt)) continue;
2005 
2006  const RailtypeInfo *rti = GetRailTypeInfo(rt);
2007  /* Skip rail type if it has no label */
2008  if (rti->label == 0) continue;
2009 
2010  StringID str = for_replacement ? rti->strings.replace_text : (rti->max_speed > 0 ? STR_TOOLBAR_RAILTYPE_VELOCITY : STR_JUST_STRING);
2012  item->SetParam(0, rti->strings.menu_text);
2013  item->SetParam(1, rti->max_speed);
2014  *list->Append() = item;
2015  }
2016  QSortT(list->Begin(), list->Length(), CompareRailTypes);
2017  return list;
2018 }