Use CommandContainer for demolish area callback state storage

This commit is contained in:
Jonathan G Rennison
2021-06-15 14:18:03 +01:00
parent 184ade43eb
commit a49d91fa39
4 changed files with 23 additions and 27 deletions

View File

@@ -12,8 +12,6 @@
#include "landscape.h" #include "landscape.h"
#include "error.h" #include "error.h"
#include "gui.h" #include "gui.h"
#include <utility>
#include "command_func.h" #include "command_func.h"
#include "company_func.h" #include "company_func.h"
#include "town.h" #include "town.h"
@@ -1232,19 +1230,19 @@ void ShowQueryString(StringID str, StringID caption, uint maxsize, Window *paren
* Window used for asking the user a YES/NO question. * Window used for asking the user a YES/NO question.
*/ */
struct QueryWindow : public Window { struct QueryWindow : public Window {
QueryCallbackProc proc; ///< callback function executed on closing of popup. Window* points to parent, bool is true if 'yes' clicked, false otherwise QueryCallbackProc *proc; ///< callback function executed on closing of popup. Window* points to parent, bool is true if 'yes' clicked, false otherwise
uint64 params[10]; ///< local copy of #_global_string_params uint64 params[10]; ///< local copy of #_global_string_params
StringID message; ///< message shown for query window StringID message; ///< message shown for query window
StringID caption; ///< title of window StringID caption; ///< title of window
QueryWindow(WindowDesc *desc, StringID caption, StringID message, Window *parent, QueryCallbackProc callback) : Window(desc) QueryWindow(WindowDesc *desc, StringID caption, StringID message, Window *parent, QueryCallbackProc *callback) : Window(desc)
{ {
/* Create a backup of the variadic arguments to strings because it will be /* Create a backup of the variadic arguments to strings because it will be
* overridden pretty often. We will copy these back for drawing */ * overridden pretty often. We will copy these back for drawing */
CopyOutDParam(this->params, 0, lengthof(this->params)); CopyOutDParam(this->params, 0, lengthof(this->params));
this->caption = caption; this->caption = caption;
this->message = message; this->message = message;
this->proc = std::move(callback); this->proc = callback;
this->parent = parent; this->parent = parent;
this->InitNested(WN_CONFIRM_POPUP_QUERY); this->InitNested(WN_CONFIRM_POPUP_QUERY);
@@ -1301,7 +1299,7 @@ struct QueryWindow : public Window {
case WID_Q_YES: { case WID_Q_YES: {
/* in the Generate New World window, clicking 'Yes' causes /* in the Generate New World window, clicking 'Yes' causes
* DeleteNonVitalWindows() to be called - we shouldn't be in a window then */ * DeleteNonVitalWindows() to be called - we shouldn't be in a window then */
QueryCallbackProc proc = this->proc; QueryCallbackProc *proc = this->proc;
Window *parent = this->parent; Window *parent = this->parent;
/* Prevent the destructor calling the callback function */ /* Prevent the destructor calling the callback function */
this->proc = nullptr; this->proc = nullptr;
@@ -1366,9 +1364,9 @@ static WindowDesc _query_desc(
* @param message string that will be shown for the window * @param message string that will be shown for the window
* @param parent pointer to parent window, if this pointer is nullptr the parent becomes * @param parent pointer to parent window, if this pointer is nullptr the parent becomes
* the main window WC_MAIN_WINDOW * the main window WC_MAIN_WINDOW
* @param callback callback function to set in the window descriptor * @param callback callback function pointer to set in the window descriptor
*/ */
void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallbackProc callback) void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallbackProc *callback)
{ {
if (parent == nullptr) parent = FindWindowById(WC_MAIN_WINDOW, 0); if (parent == nullptr) parent = FindWindowById(WC_MAIN_WINDOW, 0);
@@ -1376,7 +1374,7 @@ void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallback
if (w->window_class != WC_CONFIRM_POPUP_QUERY) continue; if (w->window_class != WC_CONFIRM_POPUP_QUERY) continue;
const QueryWindow *qw = (const QueryWindow *)w; const QueryWindow *qw = (const QueryWindow *)w;
if (qw->parent != parent) continue; if (qw->parent != parent || qw->proc != callback) continue;
delete qw; delete qw;
break; break;

View File

@@ -2141,7 +2141,7 @@ public:
case WID_CL_MATRIX: { case WID_CL_MATRIX: {
StringID text = STR_NULL; StringID text = STR_NULL;
QueryCallbackProc callback = nullptr; QueryCallbackProc *callback = nullptr;
switch (index) { switch (index) {
case DD_CLIENT_ADMIN_KICK: case DD_CLIENT_ADMIN_KICK:

View File

@@ -120,6 +120,14 @@ static bool IsIndustryOrRailStationInArea(TileIndex start_tile, TileIndex end_ti
return destroying_industry_or_station; return destroying_industry_or_station;
} }
static CommandContainer _demolish_area_command;
static void DemolishAreaConfirmationCallback(Window*, bool confirmed) {
if (confirmed) {
DoCommandP(&_demolish_area_command);
}
}
/** /**
* A central place to handle all X_AND_Y dragged GUI functions. * A central place to handle all X_AND_Y dragged GUI functions.
* @param proc Procedure related to the dragging * @param proc Procedure related to the dragging
@@ -140,20 +148,13 @@ bool GUIPlaceProcDragXY(ViewportDragDropSelectionProcess proc, TileIndex start_t
switch (proc) { switch (proc) {
case DDSP_DEMOLISH_AREA: { case DDSP_DEMOLISH_AREA: {
const bool should_query_first = IsIndustryOrRailStationInArea(start_tile, end_tile, _ctrl_pressed); _demolish_area_command = NewCommandContainerBasic(end_tile, start_tile, _ctrl_pressed ? 1 : 0, CMD_CLEAR_AREA | CMD_MSG(STR_ERROR_CAN_T_CLEAR_THIS_AREA), CcPlaySound_EXPLOSION);
const auto cmd_clear_area = [=](Window*, bool confirmed) { if (IsIndustryOrRailStationInArea(start_tile, end_tile, _ctrl_pressed)) {
if (confirmed) { ShowQuery(STR_QUERY_CLEAR_AREA_CAPTION, STR_CLEAR_AREA_CONFIRMATION_TEXT, nullptr, DemolishAreaConfirmationCallback);
DoCommandP(end_tile, start_tile, _ctrl_pressed ? 1 : 0, CMD_CLEAR_AREA | CMD_MSG(STR_ERROR_CAN_T_CLEAR_THIS_AREA), CcPlaySound_EXPLOSION);
}
};
if (should_query_first) {
ShowQuery(STR_QUERY_CLEAR_AREA_CAPTION, STR_CLEAR_AREA_CONFIRMATION_TEXT, nullptr, cmd_clear_area);
} else { } else {
cmd_clear_area(nullptr, true); DemolishAreaConfirmationCallback(nullptr, true);
} }
break; break;
} }
case DDSP_RAISE_AND_LEVEL_AREA: case DDSP_RAISE_AND_LEVEL_AREA:

View File

@@ -10,13 +10,10 @@
#ifndef TEXTBUF_GUI_H #ifndef TEXTBUF_GUI_H
#define TEXTBUF_GUI_H #define TEXTBUF_GUI_H
#include "window_type.h"
#include "string_type.h" #include "string_type.h"
#include "strings_type.h" #include "strings_type.h"
#include <functional>
struct Window;
/** Flags used in ShowQueryString() call */ /** Flags used in ShowQueryString() call */
enum QueryStringFlags { enum QueryStringFlags {
QSF_NONE = 0, QSF_NONE = 0,
@@ -29,10 +26,10 @@ enum QueryStringFlags {
DECLARE_ENUM_AS_BIT_SET(QueryStringFlags) DECLARE_ENUM_AS_BIT_SET(QueryStringFlags)
/** Callback procedure for the ShowQuery method. */ /** Callback procedure for the ShowQuery method. */
typedef std::function<void(Window*, bool)> QueryCallbackProc; typedef void QueryCallbackProc(Window*, bool);
void ShowQueryString(StringID str, StringID caption, uint max_len, Window *parent, CharSetFilter afilter, QueryStringFlags flags); void ShowQueryString(StringID str, StringID caption, uint max_len, Window *parent, CharSetFilter afilter, QueryStringFlags flags);
void ShowQuery(StringID caption, StringID message, Window *w, QueryCallbackProc callback); void ShowQuery(StringID caption, StringID message, Window *w, QueryCallbackProc *callback);
/** The number of 'characters' on the on-screen keyboard. */ /** The number of 'characters' on the on-screen keyboard. */
static const uint OSK_KEYBOARD_ENTRIES = 50; static const uint OSK_KEYBOARD_ENTRIES = 50;