1
0
Fork 0

Codechange: Use EncodedString for Query window. (#13528)

This removes the need to separately capture and store global parameters.
pull/13533/head
Peter Nelson 2025-02-11 17:30:16 +00:00 committed by GitHub
parent 08e451e6d9
commit 984da2455b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 67 additions and 55 deletions

View File

@ -822,11 +822,9 @@ struct DepotWindow : Window {
case WID_D_SELL_ALL:
/* Only open the confirmation window if there are anything to sell */
if (!this->vehicle_list.empty() || !this->wagon_list.empty()) {
SetDParam(0, this->type);
SetDParam(1, this->GetDestinationIndex());
ShowQuery(
STR_DEPOT_CAPTION,
STR_DEPOT_SELL_CONFIRMATION_TEXT,
GetEncodedString(STR_DEPOT_CAPTION, this->type, this->GetDestinationIndex()),
GetEncodedString(STR_DEPOT_SELL_CONFIRMATION_TEXT),
this,
DepotSellAllConfirmationCallback
);

View File

@ -825,14 +825,20 @@ public:
if (this->abstract_filetype == FT_SAVEGAME || this->abstract_filetype == FT_SCENARIO) {
_file_to_saveload.name = FiosMakeSavegameName(this->filename_editbox.text.GetText());
if (FioCheckFileExists(_file_to_saveload.name, Subdirectory::SAVE_DIR)) {
ShowQuery(STR_SAVELOAD_OVERWRITE_TITLE, STR_SAVELOAD_OVERWRITE_WARNING, this, SaveLoadWindow::SaveGameConfirmationCallback);
ShowQuery(
GetEncodedString(STR_SAVELOAD_OVERWRITE_TITLE),
GetEncodedString(STR_SAVELOAD_OVERWRITE_WARNING),
this, SaveLoadWindow::SaveGameConfirmationCallback);
} else {
_switch_mode = SM_SAVE_GAME;
}
} else {
_file_to_saveload.name = FiosMakeHeightmapName(this->filename_editbox.text.GetText());
if (FioCheckFileExists(_file_to_saveload.name, Subdirectory::SAVE_DIR)) {
ShowQuery(STR_SAVELOAD_OVERWRITE_TITLE, STR_SAVELOAD_OVERWRITE_WARNING, this, SaveLoadWindow::SaveHeightmapConfirmationCallback);
ShowQuery(
GetEncodedString(STR_SAVELOAD_OVERWRITE_TITLE),
GetEncodedString(STR_SAVELOAD_OVERWRITE_WARNING),
this, SaveLoadWindow::SaveHeightmapConfirmationCallback);
} else {
_switch_mode = SM_SAVE_HEIGHTMAP;
}

View File

@ -713,8 +713,8 @@ struct GenerateLandscapeWindow : public Window {
map_y * 2 < (1U << _settings_newgame.game_creation.map_y) ||
map_y / 2 > (1U << _settings_newgame.game_creation.map_y))) {
ShowQuery(
STR_WARNING_HEIGHTMAP_SCALE_CAPTION,
STR_WARNING_HEIGHTMAP_SCALE_MESSAGE,
GetEncodedString(STR_WARNING_HEIGHTMAP_SCALE_CAPTION),
GetEncodedString(STR_WARNING_HEIGHTMAP_SCALE_MESSAGE),
this,
LandscapeGenerationCallback);
} else {
@ -1381,8 +1381,8 @@ struct GenerateProgressWindow : public Window {
case WID_GP_ABORT:
SetMouseCursorBusy(false);
ShowQuery(
STR_GENERATION_ABORT_CAPTION,
STR_GENERATION_ABORT_MESSAGE,
GetEncodedString(STR_GENERATION_ABORT_CAPTION),
GetEncodedString(STR_GENERATION_ABORT_MESSAGE),
this,
AbortGeneratingWorldCallback
);

View File

@ -841,7 +841,10 @@ public:
case WID_GL_DELETE_GROUP: { // Delete the selected group
this->group_confirm = this->vli.ToGroupID();
ShowQuery(STR_QUERY_GROUP_DELETE_CAPTION, STR_GROUP_DELETE_QUERY_TEXT, this, DeleteGroupCallback);
ShowQuery(
GetEncodedString(STR_QUERY_GROUP_DELETE_CAPTION),
GetEncodedString(STR_GROUP_DELETE_QUERY_TEXT),
this, DeleteGroupCallback);
break;
}

View File

@ -641,14 +641,20 @@ public:
case WID_DPI_CREATE_RANDOM_INDUSTRIES_WIDGET: {
assert(_game_mode == GM_EDITOR);
this->HandleButtonClick(WID_DPI_CREATE_RANDOM_INDUSTRIES_WIDGET);
ShowQuery(STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_CAPTION, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_QUERY, nullptr, AskManyRandomIndustriesCallback);
ShowQuery(
GetEncodedString(STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_CAPTION),
GetEncodedString(STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_QUERY),
nullptr, AskManyRandomIndustriesCallback);
break;
}
case WID_DPI_REMOVE_ALL_INDUSTRIES_WIDGET: {
assert(_game_mode == GM_EDITOR);
this->HandleButtonClick(WID_DPI_REMOVE_ALL_INDUSTRIES_WIDGET);
ShowQuery(STR_FUND_INDUSTRY_REMOVE_ALL_INDUSTRIES_CAPTION, STR_FUND_INDUSTRY_REMOVE_ALL_INDUSTRIES_QUERY, nullptr, AskRemoveAllIndustriesCallback);
ShowQuery(
GetEncodedString(STR_FUND_INDUSTRY_REMOVE_ALL_INDUSTRIES_CAPTION),
GetEncodedString(STR_FUND_INDUSTRY_REMOVE_ALL_INDUSTRIES_QUERY),
nullptr, AskRemoveAllIndustriesCallback);
break;
}

View File

@ -479,8 +479,8 @@ static void AskExitGameCallback(Window *, bool confirmed)
void AskExitGame()
{
ShowQuery(
STR_QUIT_CAPTION,
STR_QUIT_ARE_YOU_SURE_YOU_WANT_TO_EXIT_OPENTTD,
GetEncodedString(STR_QUIT_CAPTION),
GetEncodedString(STR_QUIT_ARE_YOU_SURE_YOU_WANT_TO_EXIT_OPENTTD),
nullptr,
AskExitGameCallback,
true
@ -499,8 +499,8 @@ static void AskExitToGameMenuCallback(Window *, bool confirmed)
void AskExitToGameMenu()
{
ShowQuery(
STR_ABANDON_GAME_CAPTION,
(_game_mode != GM_EDITOR) ? STR_ABANDON_GAME_QUERY : STR_ABANDON_SCENARIO_QUERY,
GetEncodedString(STR_ABANDON_GAME_CAPTION),
GetEncodedString((_game_mode != GM_EDITOR) ? STR_ABANDON_GAME_QUERY : STR_ABANDON_SCENARIO_QUERY),
nullptr,
AskExitToGameMenuCallback,
true

View File

@ -186,8 +186,8 @@ CommandCost CmdPause(DoCommandFlag flags, PauseMode mode, bool pause)
if (flags & DC_EXEC) {
if (mode == PM_PAUSED_NORMAL && _pause_mode & PM_PAUSED_ERROR) {
ShowQuery(
STR_NEWGRF_UNPAUSE_WARNING_TITLE,
STR_NEWGRF_UNPAUSE_WARNING,
GetEncodedString(STR_NEWGRF_UNPAUSE_WARNING_TITLE),
GetEncodedString(STR_NEWGRF_UNPAUSE_WARNING),
nullptr,
AskUnsafeUnpauseCallback
);

View File

@ -1065,20 +1065,15 @@ void ShowQueryString(std::string_view str, StringID caption, uint maxsize, Windo
*/
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
std::vector<StringParameterData> params; ///< local copy of #_global_string_params
StringID message; ///< message shown for query window
EncodedString caption; ///< caption for query window.
EncodedString message; ///< message for query window.
QueryWindow(WindowDesc &desc, StringID caption, StringID message, Window *parent, QueryCallbackProc *callback) : Window(desc)
QueryWindow(WindowDesc &desc, EncodedString &&caption, EncodedString &&message, Window *parent, QueryCallbackProc *callback)
: Window(desc), proc(callback), caption(std::move(caption)), message(std::move(message))
{
/* Create a backup of the variadic arguments to strings because it will be
* overridden pretty often. We will copy these back for drawing */
CopyOutDParam(this->params, 10);
this->message = message;
this->proc = callback;
this->parent = parent;
this->parent = parent;
this->CreateNestedTree();
this->GetWidget<NWidgetCore>(WID_Q_CAPTION)->SetString(caption);
this->FinishInitNested(WN_CONFIRM_POPUP_QUERY);
}
@ -1100,8 +1095,7 @@ struct QueryWindow : public Window {
{
switch (widget) {
case WID_Q_CAPTION:
case WID_Q_TEXT:
CopyInDParam(this->params);
SetDParamStr(0, this->caption.GetDecodedString());
break;
}
}
@ -1110,14 +1104,14 @@ struct QueryWindow : public Window {
{
if (widget != WID_Q_TEXT) return;
size = GetStringMultiLineBoundingBox(this->message, size);
size = GetStringMultiLineBoundingBox(this->message.GetDecodedString(), size);
}
void DrawWidget(const Rect &r, WidgetID widget) const override
{
if (widget != WID_Q_TEXT) return;
DrawStringMultiLine(r, this->message, TC_FROMSTRING, SA_CENTER);
DrawStringMultiLine(r, this->message.GetDecodedString(), TC_FROMSTRING, SA_CENTER);
}
void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
@ -1166,7 +1160,7 @@ struct QueryWindow : public Window {
static constexpr NWidgetPart _nested_query_widgets[] = {
NWidget(NWID_HORIZONTAL),
NWidget(WWT_CLOSEBOX, COLOUR_RED),
NWidget(WWT_CAPTION, COLOUR_RED, WID_Q_CAPTION), // The caption's string is set in the constructor
NWidget(WWT_CAPTION, COLOUR_RED, WID_Q_CAPTION), SetToolTip(STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
EndContainer(),
NWidget(WWT_PANEL, COLOUR_RED),
NWidget(NWID_VERTICAL), SetPIP(0, WidgetDimensions::unscaled.vsep_wide, 0), SetPadding(WidgetDimensions::unscaled.modalpopup),
@ -1196,7 +1190,7 @@ static WindowDesc _query_desc(
* @param callback callback function pointer to set in the window descriptor
* @param focus whether the window should be focussed (by default false)
*/
void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallbackProc *callback, bool focus)
void ShowQuery(EncodedString &&caption, EncodedString &&message, Window *parent, QueryCallbackProc *callback, bool focus)
{
if (parent == nullptr) parent = GetMainWindow();
@ -1210,6 +1204,6 @@ void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallback
break;
}
QueryWindow *q = new QueryWindow(_query_desc, caption, message, parent, callback);
QueryWindow *q = new QueryWindow(_query_desc, std::move(caption), std::move(message), parent, callback);
if (focus) SetFocusedWindow(q);
}

View File

@ -869,7 +869,10 @@ public:
if (_accepted_external_search) {
this->OpenExternalSearch();
} else {
ShowQuery(STR_CONTENT_SEARCH_EXTERNAL_DISCLAIMER_CAPTION, STR_CONTENT_SEARCH_EXTERNAL_DISCLAIMER, this, ExternalSearchDisclaimerCallback);
ShowQuery(
GetEncodedString(STR_CONTENT_SEARCH_EXTERNAL_DISCLAIMER_CAPTION),
GetEncodedString(STR_CONTENT_SEARCH_EXTERNAL_DISCLAIMER),
this, ExternalSearchDisclaimerCallback);
}
break;
}

View File

@ -1823,40 +1823,39 @@ public:
break;
case WID_CL_MATRIX: {
StringID text = STR_NULL;
QueryCallbackProc *callback = nullptr;
EncodedString text;
switch (index) {
case DD_CLIENT_ADMIN_KICK:
_admin_client_id = this->dd_client_id;
text = STR_NETWORK_CLIENT_LIST_ASK_CLIENT_KICK;
callback = AdminClientKickCallback;
SetDParamStr(0, NetworkClientInfo::GetByClientID(_admin_client_id)->client_name);
text = GetEncodedString(STR_NETWORK_CLIENT_LIST_ASK_CLIENT_KICK, NetworkClientInfo::GetByClientID(_admin_client_id)->client_name);
break;
case DD_CLIENT_ADMIN_BAN:
_admin_client_id = this->dd_client_id;
text = STR_NETWORK_CLIENT_LIST_ASK_CLIENT_BAN;
callback = AdminClientBanCallback;
SetDParamStr(0, NetworkClientInfo::GetByClientID(_admin_client_id)->client_name);
text = GetEncodedString(STR_NETWORK_CLIENT_LIST_ASK_CLIENT_BAN, NetworkClientInfo::GetByClientID(_admin_client_id)->client_name);
break;
case DD_COMPANY_ADMIN_RESET:
_admin_company_id = this->dd_company_id;
text = STR_NETWORK_CLIENT_LIST_ASK_COMPANY_RESET;
callback = AdminCompanyResetCallback;
SetDParam(0, _admin_company_id);
text = GetEncodedString(STR_NETWORK_CLIENT_LIST_ASK_COMPANY_RESET, _admin_company_id);
break;
default:
NOT_REACHED();
}
assert(text != STR_NULL);
assert(callback != nullptr);
/* Always ask confirmation for all admin actions. */
ShowQuery(STR_NETWORK_CLIENT_LIST_ASK_CAPTION, text, this, callback);
ShowQuery(
GetEncodedString(STR_NETWORK_CLIENT_LIST_ASK_CAPTION),
std::move(text),
this, callback);
break;
}

View File

@ -1097,8 +1097,8 @@ struct NewGRFWindow : public Window, NewGRFScanCallback {
if (!this->editable) break;
if (this->execute) {
ShowQuery(
STR_NEWGRF_POPUP_CAUTION_CAPTION,
STR_NEWGRF_CONFIRMATION_TEXT,
GetEncodedString(STR_NEWGRF_POPUP_CAUTION_CAPTION),
GetEncodedString(STR_NEWGRF_CONFIRMATION_TEXT),
this,
NewGRFConfirmationCallback
);

View File

@ -858,9 +858,9 @@ void MakeScreenshotWithConfirm(ScreenshotType t)
if (width * height > 8192 * 8192) {
/* Ask for confirmation */
_confirmed_screenshot_type = t;
SetDParam(0, width);
SetDParam(1, height);
ShowQuery(STR_WARNING_SCREENSHOT_SIZE_CAPTION, STR_WARNING_SCREENSHOT_SIZE_MESSAGE, nullptr, ScreenshotConfirmationCallback);
ShowQuery(
GetEncodedString(STR_WARNING_SCREENSHOT_SIZE_CAPTION),
GetEncodedString(STR_WARNING_SCREENSHOT_SIZE_MESSAGE, width, height), nullptr, ScreenshotConfirmationCallback);
} else {
/* Less than 64M pixels, just do it */
MakeScreenshot(t, {});

View File

@ -2547,8 +2547,8 @@ struct GameSettingsWindow : Window {
case WID_GS_RESET_ALL:
ShowQuery(
STR_CONFIG_SETTING_RESET_ALL_CONFIRMATION_DIALOG_CAPTION,
STR_CONFIG_SETTING_RESET_ALL_CONFIRMATION_DIALOG_TEXT,
GetEncodedString(STR_CONFIG_SETTING_RESET_ALL_CONFIRMATION_DIALOG_CAPTION),
GetEncodedString(STR_CONFIG_SETTING_RESET_ALL_CONFIRMATION_DIALOG_TEXT),
this,
ResetAllSettingsConfirmationCallback
);

View File

@ -638,7 +638,10 @@ struct ScenarioEditorLandscapeGenerationWindow : Window {
break;
case WID_ETT_RESET_LANDSCAPE: // Reset landscape
ShowQuery(STR_QUERY_RESET_LANDSCAPE_CAPTION, STR_RESET_LANDSCAPE_CONFIRMATION_TEXT, nullptr, ResetLandscapeConfirmationCallback);
ShowQuery(
GetEncodedString(STR_QUERY_RESET_LANDSCAPE_CAPTION),
GetEncodedString(STR_RESET_LANDSCAPE_CONFIRMATION_TEXT),
nullptr, ResetLandscapeConfirmationCallback);
break;
default: NOT_REACHED();

View File

@ -28,7 +28,7 @@ DECLARE_ENUM_AS_BIT_SET(QueryStringFlags)
typedef void QueryCallbackProc(Window*, bool);
void ShowQueryString(std::string_view str, StringID caption, uint max_len, Window *parent, CharSetFilter afilter, QueryStringFlags flags);
void ShowQuery(StringID caption, StringID message, Window *w, QueryCallbackProc *callback, bool focus = false);
void ShowQuery(EncodedString &&caption, EncodedString &&message, Window *w, QueryCallbackProc *callback, bool focus = false);
/** The number of 'characters' on the on-screen keyboard. */
static const uint OSK_KEYBOARD_ENTRIES = 50;