Codechange: Use lambdas instead of CommandContainer to manage station picker commands.

This commit is contained in:
Michael Lutz
2021-10-29 00:56:07 +02:00
parent a05fd7aa50
commit 996b16de70
9 changed files with 110 additions and 84 deletions

View File

@@ -2264,13 +2264,13 @@ static const NWidgetPart _nested_select_station_widgets[] = {
*/
template <class T>
struct SelectStationWindow : Window {
CommandContainer select_station_cmd; ///< Command to build new station
StationPickerCmdProc select_station_proc;
TileArea area; ///< Location of new station
Scrollbar *vscroll;
SelectStationWindow(WindowDesc *desc, const CommandContainer &cmd, TileArea ta) :
SelectStationWindow(WindowDesc *desc, TileArea ta, StationPickerCmdProc&& proc) :
Window(desc),
select_station_cmd(cmd),
select_station_proc(std::move(proc)),
area(ta)
{
this->CreateNestedTree();
@@ -2341,12 +2341,8 @@ struct SelectStationWindow : Window {
if (distant_join && st_index >= _stations_nearby_list.size()) return;
/* Insert station to be joined into stored command */
SB(this->select_station_cmd.p2, 16, 16,
(distant_join ? _stations_nearby_list[st_index] : NEW_STATION));
/* Execute stored Command */
DoCommandP(&this->select_station_cmd);
this->select_station_proc(false, distant_join ? _stations_nearby_list[st_index] : NEW_STATION);
/* Close Window; this might cause double frees! */
CloseWindowById(WC_SELECT_STATION, 0);
@@ -2412,7 +2408,7 @@ static WindowDesc _select_station_desc(
* @return whether we need to show the station selection window.
*/
template <class T>
static bool StationJoinerNeeded(const CommandContainer &cmd, TileArea ta)
static bool StationJoinerNeeded(TileArea ta, const StationPickerCmdProc &proc)
{
/* Only show selection if distant join is enabled in the settings */
if (!_settings_game.station.distant_join_stations) return false;
@@ -2430,7 +2426,7 @@ static bool StationJoinerNeeded(const CommandContainer &cmd, TileArea ta)
if (!_ctrl_pressed) return false;
/* Now check if we could build there */
if (DoCommand(&cmd, CommandFlagsToDCFlags(GetCommandFlags(cmd.cmd))).Failed()) return false;
if (!proc(true, INVALID_STATION)) return false;
/* Test for adjacent station or station below selection.
* If adjacent-stations is disabled and we are building next to a station, do not show the selection window.
@@ -2446,32 +2442,32 @@ static bool StationJoinerNeeded(const CommandContainer &cmd, TileArea ta)
* @tparam the class to find stations for
*/
template <class T>
void ShowSelectBaseStationIfNeeded(const CommandContainer &cmd, TileArea ta)
void ShowSelectBaseStationIfNeeded(TileArea ta, StationPickerCmdProc&& proc)
{
if (StationJoinerNeeded<T>(cmd, ta)) {
if (StationJoinerNeeded<T>(ta, proc)) {
if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
new SelectStationWindow<T>(&_select_station_desc, cmd, ta);
new SelectStationWindow<T>(&_select_station_desc, ta, std::move(proc));
} else {
DoCommandP(&cmd);
proc(false, INVALID_STATION);
}
}
/**
* Show the station selection window when needed. If not, build the station.
* @param cmd Command to build the station.
* @param ta Area to build the station in
* @param proc Function called to execute the build command.
*/
void ShowSelectStationIfNeeded(const CommandContainer &cmd, TileArea ta)
void ShowSelectStationIfNeeded(TileArea ta, StationPickerCmdProc proc)
{
ShowSelectBaseStationIfNeeded<Station>(cmd, ta);
ShowSelectBaseStationIfNeeded<Station>(ta, std::move(proc));
}
/**
* Show the waypoint selection window when needed. If not, build the waypoint.
* @param cmd Command to build the waypoint.
* @param ta Area to build the waypoint in
* @param proc Function called to execute the build command.
*/
void ShowSelectWaypointIfNeeded(const CommandContainer &cmd, TileArea ta)
void ShowSelectWaypointIfNeeded(TileArea ta, StationPickerCmdProc proc)
{
ShowSelectBaseStationIfNeeded<Waypoint>(cmd, ta);
ShowSelectBaseStationIfNeeded<Waypoint>(ta, std::move(proc));
}