diff --git a/src/citymania/cm_station_gui.cpp b/src/citymania/cm_station_gui.cpp index 27b730385d..5db4f21407 100644 --- a/src/citymania/cm_station_gui.cpp +++ b/src/citymania/cm_station_gui.cpp @@ -81,6 +81,7 @@ namespace StationAction { }; StationAction::Mode _station_action = StationAction::Create{}; +StationID _selected_join_station = INVALID_STATION; static const int MAX_TILE_EXTENT_LEFT = ZOOM_LVL_BASE * TILE_PIXELS; ///< Maximum left extent of tile relative to north corner. static const int MAX_TILE_EXTENT_RIGHT = ZOOM_LVL_BASE * TILE_PIXELS; ///< Maximum right extent of tile relative to north corner. @@ -129,9 +130,9 @@ void OnStationDeleted(const Station *station) { // } } -// const Station *_last_built_station; +const Station *_last_built_station; void OnStationPartBuilt(const Station *station) { - // _last_built_station = station; + _last_built_station = station; // CheckRedrawStationCoverage(); } @@ -625,12 +626,16 @@ bool HasSelectedStationHighlight() { } static void UpdateStationAction(std::optional area, up cmdptr) { - if (UseImprovedStationJoin()) return; - _station_action = StationAction::Create{}; - if (!area.has_value()) return; + if (UseImprovedStationJoin()) { + auto join_area = GetStationJoinArea(_selected_join_station); + if (!join_area.Intersects(*area)) return; + _station_action = StationAction::Join{_selected_join_station}; + return; + } + if (_fn_mod) { if (!_settings_game.station.distant_join_stations) return; // TODO ctrl with overbuilding errors out in vanilla, shows empty picker here @@ -1040,9 +1045,13 @@ void StationSelectAction::HandleMouseRelease() { // TODO station sign click if (!IsValidTile(this->cur_tile)) return; _station_action = StationAction::Create{}; + _selected_join_station = INVALID_STATION; if (IsTileType(this->cur_tile, MP_STATION)) { auto st = Station::GetByTile(this->cur_tile); - if (st) _station_action = StationAction::Join{st->index}; + if (st) { + _station_action = StationAction::Join{st->index}; + _selected_join_station = st->index; + } } } @@ -1094,16 +1103,28 @@ StationBuildTool::StationBuildTool() { extern void ShowSelectStationWindow(TileArea ta, StationPickerCmdProc&& proc); +template +bool PostBuildStationCommand(Taction *action, Tcallback callback, Targ arg, StationID join_to) { + auto cmd = action->GetCommand(arg, join_to); + if (UseImprovedStationJoin()) { + cmd->with_callback([](bool res)->bool { + if (!res) return false; + if (_last_built_station == nullptr) return false; + _selected_join_station = _last_built_station->index; + return true; + }); + } + return cmd ? cmd->post(callback) : false; +} + template bool ExecuteBuildCommand(Taction *action, Tcallback callback, Targ arg) { std::visit(Overload{ [&](StationAction::Join &a) { - auto cmd = action->GetCommand(arg, a.station); - return cmd ? cmd->post(callback) : false; + return PostBuildStationCommand(action, callback, arg, a.station); }, [&](StationAction::Create &) { - auto cmd = action->GetCommand(arg, NEW_STATION); - return cmd ? cmd->post(callback) : false; + return PostBuildStationCommand(action, callback, arg, NEW_STATION); }, [&](StationAction::Picker &) { auto cmd = action->GetCommand(arg, INVALID_STATION);