OpenRCT2/src/openrct2-ui/windows/SavePrompt.cpp

273 lines
8.5 KiB
C++
Raw Normal View History

2014-05-02 23:21:08 +02:00
/*****************************************************************************
* Copyright (c) 2014-2018 OpenRCT2 developers
2014-05-02 23:21:08 +02:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2014-05-02 23:21:08 +02:00
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
2014-05-02 23:21:08 +02:00
*****************************************************************************/
2018-11-21 23:16:04 +01:00
#include <iterator>
2018-06-22 23:21:44 +02:00
#include <openrct2-ui/interface/Widget.h>
2017-08-06 21:20:05 +02:00
#include <openrct2-ui/windows/Window.h>
2017-09-12 00:04:03 +02:00
#include <openrct2/Context.h>
2017-11-30 18:17:06 +01:00
#include <openrct2/Game.h>
2018-06-22 23:21:44 +02:00
#include <openrct2/OpenRCT2.h>
#include <openrct2/audio/audio.h>
#include <openrct2/config/Config.h>
2018-01-06 18:32:25 +01:00
#include <openrct2/localisation/Localisation.h>
2018-03-19 23:28:40 +01:00
#include <openrct2/scenario/Scenario.h>
2018-06-22 23:21:44 +02:00
#include <openrct2/windows/Intent.h>
2014-05-02 23:21:08 +02:00
// clang-format off
enum WINDOW_SAVE_PROMPT_WIDGET_IDX {
WIDX_BACKGROUND,
WIDX_TITLE,
WIDX_CLOSE,
WIDX_LABEL,
WIDX_SAVE,
WIDX_DONT_SAVE,
WIDX_CANCEL
2014-05-02 23:21:08 +02:00
};
static rct_widget window_save_prompt_widgets[] = {
{ WWT_FRAME, 0, 0, 259, 0, 53, STR_NONE, STR_NONE }, // panel / background
{ WWT_CAPTION, 0, 1, 258, 1, 14, 0, STR_WINDOW_TITLE_TIP }, // title bar
{ WWT_CLOSEBOX, 0, 247, 257, 2, 13, STR_CLOSE_X_WHITE, STR_CLOSE_WINDOW_TIP }, // close x button
{ WWT_LABEL_CENTRED, 0, 2, 257, 19, 30, 0, STR_NONE }, // question/label
{ WWT_BUTTON, 0, 8, 85, 35, 48, STR_SAVE_PROMPT_SAVE, STR_NONE }, // save
{ WWT_BUTTON, 0, 91, 168, 35, 48, STR_SAVE_PROMPT_DONT_SAVE, STR_NONE }, // don't save
{ WWT_BUTTON, 0, 174, 251, 35, 48, STR_SAVE_PROMPT_CANCEL, STR_NONE }, // cancel
{ WIDGETS_END },
2014-05-02 23:21:08 +02:00
};
enum WINDOW_QUIT_PROMPT_WIDGET_IDX {
WQIDX_BACKGROUND,
WQIDX_TITLE,
WQIDX_CLOSE,
WQIDX_OK,
WQIDX_CANCEL
};
static rct_widget window_quit_prompt_widgets[] = {
{ WWT_FRAME, 0, 0, 176, 0, 37, STR_NONE, STR_NONE }, // panel / background
{ WWT_CAPTION, 0, 1, 175, 1, 14, STR_QUIT_GAME_PROMPT_TITLE, STR_WINDOW_TITLE_TIP }, // title bar
{ WWT_CLOSEBOX, 0, 164, 174, 2, 13, STR_CLOSE_X_WHITE, STR_CLOSE_WINDOW_TIP }, // close x button
{ WWT_BUTTON, 0, 8, 85, 19, 32, STR_OK, STR_NONE }, // ok
{ WWT_BUTTON, 0, 91, 168, 19, 32, STR_CANCEL, STR_NONE }, // cancel
{ WIDGETS_END },
};
static constexpr const rct_string_id window_save_prompt_labels[][2] = {
{ STR_LOAD_GAME_PROMPT_TITLE, STR_SAVE_BEFORE_LOADING },
{ STR_QUIT_GAME_PROMPT_TITLE, STR_SAVE_BEFORE_QUITTING },
{ STR_QUIT_GAME_2_PROMPT_TITLE, STR_SAVE_BEFORE_QUITTING_2 },
};
static void window_save_prompt_close(rct_window *w);
2017-05-01 15:41:45 +02:00
static void window_save_prompt_mouseup(rct_window *w, rct_widgetindex widgetIndex);
static void window_save_prompt_paint(rct_window *w, rct_drawpixelinfo *dpi);
static void window_save_prompt_callback(int32_t result, const utf8 * path);
2014-05-02 23:21:08 +02:00
static rct_window_event_list window_save_prompt_events = {
window_save_prompt_close,
window_save_prompt_mouseup,
2017-08-15 10:07:44 +02:00
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
window_save_prompt_paint,
2017-08-15 10:07:44 +02:00
nullptr
2014-05-02 23:21:08 +02:00
};
// clang-format on
2014-05-02 23:21:08 +02:00
/**
*
* rct2: 0x0066DCBE
*/
2018-06-22 23:21:44 +02:00
rct_window* window_save_prompt_open()
2014-05-02 23:21:08 +02:00
{
int32_t width, height;
rct_string_id stringId;
rct_window* window;
uint8_t prompt_mode;
2018-06-22 23:21:44 +02:00
rct_widget* widgets;
uint64_t enabled_widgets;
prompt_mode = gSavePromptMode;
if (prompt_mode == PM_QUIT)
prompt_mode = PM_SAVE_BEFORE_QUIT;
2014-05-02 23:21:08 +02:00
// do not show save prompt if we're in the title demo and click on load game
2018-06-22 23:21:44 +02:00
if (gScreenFlags & SCREEN_FLAGS_TITLE_DEMO)
{
game_load_or_quit_no_save_prompt();
2017-08-06 05:22:00 +02:00
return nullptr;
}
2018-06-22 23:21:44 +02:00
if (!gConfigGeneral.confirmation_prompt)
{
/* game_load_or_quit_no_save_prompt() will exec requested task and close this window
2018-06-22 23:21:44 +02:00
* immediately again.
* TODO restructure these functions when we're sure game_load_or_quit_no_save_prompt()
* and game_load_or_quit() are not called by the original binary anymore.
*/
2018-06-22 23:21:44 +02:00
if (gScreenAge < 3840)
{
game_load_or_quit_no_save_prompt();
2017-08-06 05:22:00 +02:00
return nullptr;
}
}
// Check if window is already open
window = window_bring_to_front_by_class(WC_SAVE_PROMPT);
2018-06-22 23:21:44 +02:00
if (window)
{
window_close(window);
}
2018-06-22 23:21:44 +02:00
if (gScreenFlags & (SCREEN_FLAGS_TRACK_DESIGNER | SCREEN_FLAGS_TRACK_MANAGER))
{
widgets = window_quit_prompt_widgets;
2018-06-22 23:21:44 +02:00
enabled_widgets = (1 << WQIDX_CLOSE) | (1 << WQIDX_OK) | (1 << WQIDX_CANCEL);
width = 177;
height = 38;
2018-06-22 23:21:44 +02:00
}
else
{
widgets = window_save_prompt_widgets;
2018-06-22 23:21:44 +02:00
enabled_widgets = (1 << WIDX_CLOSE) | (1 << WIDX_SAVE) | (1 << WIDX_DONT_SAVE) | (1 << WIDX_CANCEL);
width = 260;
height = 54;
}
2018-11-21 23:16:04 +01:00
if (prompt_mode >= std::size(window_save_prompt_labels))
2018-06-22 23:21:44 +02:00
{
log_warning("Invalid save prompt mode %u", prompt_mode);
2017-08-06 05:22:00 +02:00
return nullptr;
}
window = window_create_centred(
width, height, &window_save_prompt_events, WC_SAVE_PROMPT, WF_TRANSPARENT | WF_STICK_TO_FRONT);
2014-05-02 23:21:08 +02:00
window->widgets = widgets;
window->enabled_widgets = enabled_widgets;
window_init_scroll_widgets(window);
2014-05-02 23:21:08 +02:00
// Pause the game
gGamePaused |= GAME_PAUSED_MODAL;
audio_stop_all_music_and_sounds();
window_invalidate_by_class(WC_TOP_TOOLBAR);
2014-05-02 23:21:08 +02:00
stringId = window_save_prompt_labels[prompt_mode][0];
if (stringId == STR_LOAD_GAME_PROMPT_TITLE && gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR)
stringId = STR_LOAD_LANDSCAPE_PROMPT_TITLE;
if (stringId == STR_QUIT_GAME_PROMPT_TITLE && gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR)
stringId = STR_QUIT_SCENARIO_EDITOR;
window_save_prompt_widgets[WIDX_TITLE].text = stringId;
window_save_prompt_widgets[WIDX_LABEL].text = window_save_prompt_labels[prompt_mode][1];
2017-08-06 05:22:00 +02:00
return window;
2014-05-02 23:21:08 +02:00
}
/**
*
* rct2: 0x0066DF17
*/
2018-06-22 23:21:44 +02:00
static void window_save_prompt_close(rct_window* w)
2014-05-02 23:21:08 +02:00
{
// Unpause the game
gGamePaused &= ~GAME_PAUSED_MODAL;
audio_unpause_sounds();
window_invalidate_by_class(WC_TOP_TOOLBAR);
2014-05-02 23:21:08 +02:00
}
/**
*
* rct2: 0x0066DDF2
*/
2018-06-22 23:21:44 +02:00
static void window_save_prompt_mouseup(rct_window* w, rct_widgetindex widgetIndex)
2014-05-02 23:21:08 +02:00
{
2018-06-22 23:21:44 +02:00
if (gScreenFlags & (SCREEN_FLAGS_TITLE_DEMO | SCREEN_FLAGS_TRACK_DESIGNER | SCREEN_FLAGS_TRACK_MANAGER))
{
switch (widgetIndex)
{
case WQIDX_OK:
game_load_or_quit_no_save_prompt();
break;
case WQIDX_CLOSE:
case WQIDX_CANCEL:
window_close(w);
break;
}
return;
2018-06-22 23:21:44 +02:00
}
else
{
2017-09-12 00:04:03 +02:00
switch (widgetIndex)
{
2018-06-22 23:21:44 +02:00
case WIDX_SAVE:
2017-09-12 00:04:03 +02:00
{
2018-06-22 23:21:44 +02:00
Intent* intent;
if (gScreenFlags & (SCREEN_FLAGS_EDITOR))
{
intent = new Intent(WC_LOADSAVE);
intent->putExtra(INTENT_EXTRA_LOADSAVE_TYPE, LOADSAVETYPE_SAVE | LOADSAVETYPE_LANDSCAPE);
intent->putExtra(INTENT_EXTRA_PATH, std::string{ gS6Info.name });
}
else
{
intent = (Intent*)create_save_game_as_intent();
}
window_close(w);
intent->putExtra(INTENT_EXTRA_CALLBACK, (void*)window_save_prompt_callback);
context_open_intent(intent);
delete intent;
break;
}
2018-06-22 23:21:44 +02:00
case WIDX_DONT_SAVE:
game_load_or_quit_no_save_prompt();
return;
case WIDX_CLOSE:
case WIDX_CANCEL:
window_close(w);
return;
}
}
2014-05-02 23:21:08 +02:00
}
2018-06-22 23:21:44 +02:00
static void window_save_prompt_paint(rct_window* w, rct_drawpixelinfo* dpi)
2014-05-02 23:21:08 +02:00
{
window_draw_widgets(w, dpi);
2015-11-02 21:43:55 +01:00
}
2018-06-22 23:21:44 +02:00
static void window_save_prompt_callback(int32_t result, const utf8* path)
2015-11-02 21:43:55 +01:00
{
2018-06-22 23:21:44 +02:00
if (result == MODAL_RESULT_OK)
{
game_load_or_quit_no_save_prompt();
}
2015-12-21 05:03:37 +01:00
}