From 428e1c364eb52ccabd0aefc128f120e0c9af1843 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sun, 3 Sep 2023 00:29:36 +0100 Subject: [PATCH] Enable picker tool for rail signals --- src/rail_gui.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++-- src/rail_gui.h | 1 + src/toolbar_gui.cpp | 2 +- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/rail_gui.cpp b/src/rail_gui.cpp index 718cdcb084..34d1d3525b 100644 --- a/src/rail_gui.cpp +++ b/src/rail_gui.cpp @@ -2064,9 +2064,9 @@ public: } } - inline SignalType TypeForClick(uint id) const + static SignalType TypeForClick(uint id) { - switch(id) { + switch (id) { case 0: return SIGTYPE_NORMAL; case 1: return SIGTYPE_ENTRY; case 2: return SIGTYPE_EXIT; @@ -2081,6 +2081,23 @@ public: } } + static uint ClickForType(SignalType type) + { + switch (type) { + case SIGTYPE_NORMAL: return 0; + case SIGTYPE_ENTRY: return 1; + case SIGTYPE_EXIT: return 2; + case SIGTYPE_COMBO: return 3; + case SIGTYPE_PROG: return 4; + case SIGTYPE_PBS: return 5; + case SIGTYPE_PBS_ONEWAY: return 6; + case SIGTYPE_NO_ENTRY: return 7; + default: + assert(!"Bad signal type"); + return 0; + } + } + void OnClick(Point pt, int widget, int click_count) override { switch (widget) { @@ -2886,3 +2903,49 @@ void ShowBuildRailStationPickerAndSelect(StationType station_type, const Station if (station_window != nullptr) station_window->SelectClassAndSpec(class_id, spec_id); } } + +static void OpenBuildSignalWindow(BuildRailToolbarWindow *w, SignalVariant variant, SignalType type, uint8 style) +{ + if (!w->IsWidgetLowered(WID_RAT_BUILD_SIGNALS)) { + w->OnHotkey(WID_RAT_BUILD_SIGNALS); + } + + BuildSignalWindow *signal_window = dynamic_cast(FindWindowById(WC_BUILD_SIGNAL, TRANSPORT_RAIL)); + if (signal_window == nullptr) return; + + signal_window->OnDropdownSelect(WID_BS_STYLE, style); + + if (_settings_client.gui.signal_gui_mode == SIGNAL_GUI_PATH && _settings_game.vehicle.train_braking_model != TBM_REALISTIC && !IsPbsSignalNonExtended(type) && !IsNoEntrySignal(type)) { + signal_window->OnClick(Point(), WID_BS_TOGGLE_SIZE, 1); + } + + signal_window->OnClick(Point(), ((variant == SIG_SEMAPHORE) ? WID_BS_SEMAPHORE_NORM : WID_BS_ELECTRIC_NORM) + BuildSignalWindow::ClickForType(type), 1); +} + +void ShowBuildRailToolbarWithPickTile(RailType railtype, TileIndex tile) +{ + BuildRailToolbarWindow *w = static_cast(ShowBuildRailToolbar(railtype)); + if (w == nullptr) return; + + if (IsPlainRailTile(tile) || IsRailTunnelBridgeTile(tile)) { + TrackBits trackbits = TrackdirBitsToTrackBits(GetTileTrackdirBits(tile, TRANSPORT_RAIL, 0)); + if (trackbits & TRACK_BIT_VERT) { // N-S direction + trackbits = (_tile_fract_coords.x <= _tile_fract_coords.y) ? TRACK_BIT_RIGHT : TRACK_BIT_LEFT; + } + + if (trackbits & TRACK_BIT_HORZ) { // E-W direction + trackbits = (_tile_fract_coords.x + _tile_fract_coords.y <= 15) ? TRACK_BIT_UPPER : TRACK_BIT_LOWER; + } + + Track track = FindFirstTrack(trackbits); + if (track != INVALID_TRACK) { + if (IsTileType(tile, MP_RAILWAY) && HasTrack(tile, track) && HasSignalOnTrack(tile, track)) { + OpenBuildSignalWindow(w, GetSignalVariant(tile, track), GetSignalType(tile, track), GetSignalStyle(tile, track)); + } + if (IsRailTunnelBridgeTile(tile) && IsTunnelBridgeWithSignalSimulation(tile) && HasTrack(GetTunnelBridgeTrackBits(tile), track)) { + OpenBuildSignalWindow(w, IsTunnelBridgeSemaphore(tile) ? SIG_SEMAPHORE : SIG_ELECTRIC, + IsTunnelBridgePBS(tile) ? SIGTYPE_PBS_ONEWAY : SIGTYPE_NORMAL, GetTunnelBridgeSignalStyle(tile)); + } + } + } +} diff --git a/src/rail_gui.h b/src/rail_gui.h index 5a8deca087..eba1fe581f 100644 --- a/src/rail_gui.h +++ b/src/rail_gui.h @@ -15,6 +15,7 @@ #include "widgets/dropdown_type.h" struct Window *ShowBuildRailToolbar(RailType railtype); +void ShowBuildRailToolbarWithPickTile(RailType railtype, TileIndex tile); void ShowBuildRailStationPickerAndSelect(StationType station_type, const StationSpec *spec); void ReinitGuiAfterToggleElrail(bool disable); void ResetSignalVariant(int32 = 0); diff --git a/src/toolbar_gui.cpp b/src/toolbar_gui.cpp index 0da0368c81..c7bbd2480b 100644 --- a/src/toolbar_gui.cpp +++ b/src/toolbar_gui.cpp @@ -1140,7 +1140,7 @@ enum HelpMenuEntries { static void ShowBuildRailToolbarFromTile(TileIndex tile) { _last_built_railtype = GetRailType(tile); - ShowBuildRailToolbar(_last_built_railtype); + ShowBuildRailToolbarWithPickTile(_last_built_railtype, tile); } static void ShowBuildRoadToolbarFromTile(TileIndex tile)