2014-05-02 22:21:08 +01:00
|
|
|
/*****************************************************************************
|
2024-12-31 20:30:04 +01:00
|
|
|
* Copyright (c) 2014-2025 OpenRCT2 developers
|
2014-05-02 22:21:08 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +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 22:21:08 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2014-05-02 22:21:08 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2018-11-21 23:16:04 +01:00
|
|
|
#include <iterator>
|
2018-01-03 00:13:37 +01:00
|
|
|
#include <openrct2-ui/interface/Widget.h>
|
2025-01-19 15:50:54 +01:00
|
|
|
#include <openrct2-ui/windows/Windows.h>
|
2018-06-22 23:21:44 +02:00
|
|
|
#include <openrct2/Context.h>
|
2024-07-09 16:23:31 +02:00
|
|
|
#include <openrct2/Diagnostic.h>
|
2018-06-22 23:21:44 +02:00
|
|
|
#include <openrct2/Game.h>
|
2024-01-24 22:17:32 +01:00
|
|
|
#include <openrct2/GameState.h>
|
2018-06-22 23:21:44 +02:00
|
|
|
#include <openrct2/OpenRCT2.h>
|
2025-02-17 07:24:03 -05:00
|
|
|
#include <openrct2/audio/Audio.h>
|
2018-06-22 23:21:44 +02:00
|
|
|
#include <openrct2/config/Config.h>
|
2025-02-24 04:43:06 -05:00
|
|
|
#include <openrct2/network/Network.h>
|
2025-01-19 10:58:28 +01:00
|
|
|
#include <openrct2/ui/WindowManager.h>
|
2018-06-22 23:21:44 +02:00
|
|
|
#include <openrct2/windows/Intent.h>
|
2014-05-02 22:21:08 +01:00
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
namespace OpenRCT2::Ui::Windows
|
|
|
|
|
{
|
|
|
|
|
static constexpr int32_t WH_SAVE = 54;
|
|
|
|
|
static constexpr int32_t WW_SAVE = 260;
|
|
|
|
|
static constexpr int32_t WH_QUIT = 38;
|
|
|
|
|
static constexpr int32_t WW_QUIT = 177;
|
2024-01-24 22:17:32 +01:00
|
|
|
|
2024-09-04 21:11:26 +02:00
|
|
|
enum WindowSavePromptWidgetIdx
|
|
|
|
|
{
|
|
|
|
|
WIDX_BACKGROUND,
|
|
|
|
|
WIDX_TITLE,
|
|
|
|
|
WIDX_CLOSE,
|
|
|
|
|
WIDX_LABEL,
|
|
|
|
|
WIDX_SAVE,
|
|
|
|
|
WIDX_DONT_SAVE,
|
|
|
|
|
WIDX_CANCEL
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
// clang-format off
|
2025-01-18 14:45:17 +02:00
|
|
|
static constexpr Widget _savePromptWidgets[] = {
|
2025-01-19 16:59:45 +01:00
|
|
|
WINDOW_SHIM_WHITE(kStringIdNone, WW_SAVE, WH_SAVE),
|
|
|
|
|
MakeWidget({ 2, 19}, {256, 12}, WindowWidgetType::LabelCentred, WindowColour::Primary, kStringIdEmpty ), // question/label
|
2024-09-04 21:11:26 +02:00
|
|
|
MakeWidget({ 8, 35}, { 78, 14}, WindowWidgetType::Button, WindowColour::Primary, STR_SAVE_PROMPT_SAVE ), // save
|
|
|
|
|
MakeWidget({ 91, 35}, { 78, 14}, WindowWidgetType::Button, WindowColour::Primary, STR_SAVE_PROMPT_DONT_SAVE), // don't save
|
|
|
|
|
MakeWidget({174, 35}, { 78, 14}, WindowWidgetType::Button, WindowColour::Primary, STR_SAVE_PROMPT_CANCEL ), // cancel
|
|
|
|
|
};
|
2024-03-10 13:52:35 +00:00
|
|
|
// clang-format on
|
2014-05-02 22:21:08 +01:00
|
|
|
|
2024-09-04 21:11:26 +02:00
|
|
|
enum WindowQuitPromptWidgetIdx
|
|
|
|
|
{
|
|
|
|
|
WQIDX_BACKGROUND,
|
|
|
|
|
WQIDX_TITLE,
|
|
|
|
|
WQIDX_CLOSE,
|
|
|
|
|
WQIDX_OK,
|
|
|
|
|
WQIDX_CANCEL
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// clang-format off
|
2025-01-18 14:45:17 +02:00
|
|
|
static constexpr Widget _quitPromptWidgets[] = {
|
2024-09-04 21:11:26 +02:00
|
|
|
WINDOW_SHIM_WHITE(STR_QUIT_GAME_PROMPT_TITLE, WW_QUIT, WH_QUIT),
|
|
|
|
|
MakeWidget({ 8, 19}, {78, 14}, WindowWidgetType::Button, WindowColour::Primary, STR_OK ), // ok
|
|
|
|
|
MakeWidget({91, 19}, {78, 14}, WindowWidgetType::Button, WindowColour::Primary, STR_CANCEL), // cancel
|
|
|
|
|
};
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
static constexpr StringId 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 },
|
|
|
|
|
{ STR_NEW_GAME, STR_SAVE_BEFORE_QUITTING },
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-01 15:44:57 +01:00
|
|
|
static void WindowSavePromptCallback(ModalResult result, const utf8* path)
|
2022-10-29 05:19:59 -07:00
|
|
|
{
|
2025-03-01 15:44:57 +01:00
|
|
|
if (result == ModalResult::ok)
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
GameLoadOrQuitNoSavePrompt();
|
|
|
|
|
}
|
2022-10-29 05:19:59 -07:00
|
|
|
}
|
|
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
class SavePromptWindow final : public Window
|
2022-10-29 05:19:59 -07:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
private:
|
|
|
|
|
PromptMode _promptMode;
|
2022-10-29 05:19:59 -07:00
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
public:
|
|
|
|
|
SavePromptWindow(PromptMode promptMode)
|
|
|
|
|
: _promptMode(promptMode)
|
2022-10-29 05:19:59 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
void OnOpen() override
|
2022-10-29 05:19:59 -07:00
|
|
|
{
|
2025-03-05 21:45:04 +01:00
|
|
|
bool canSave = !(isInTrackDesignerOrManager());
|
2024-03-10 13:52:35 +00:00
|
|
|
|
2025-01-18 14:45:17 +02:00
|
|
|
if (canSave)
|
|
|
|
|
SetWidgets(_savePromptWidgets);
|
|
|
|
|
else
|
|
|
|
|
SetWidgets(_quitPromptWidgets);
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
InitScrollWidgets();
|
|
|
|
|
|
|
|
|
|
// Pause the game if not network play.
|
|
|
|
|
if (NetworkGetMode() == NETWORK_MODE_NONE)
|
2024-01-15 08:26:58 +01:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
gGamePaused |= GAME_PAUSED_MODAL;
|
|
|
|
|
Audio::StopAll();
|
2024-01-15 08:26:58 +01:00
|
|
|
}
|
2024-03-10 13:52:35 +00:00
|
|
|
|
2025-01-25 13:46:08 +01:00
|
|
|
auto* windowMgr = Ui::GetWindowManager();
|
|
|
|
|
windowMgr->InvalidateByClass(WindowClass::TopToolbar);
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
if (canSave)
|
2024-01-15 08:26:58 +01:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
StringId stringId = window_save_prompt_labels[EnumValue(_promptMode)][0];
|
2025-03-05 21:45:04 +01:00
|
|
|
if (stringId == STR_LOAD_GAME_PROMPT_TITLE && gLegacyScene == LegacyScene::scenarioEditor)
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
stringId = STR_LOAD_LANDSCAPE_PROMPT_TITLE;
|
|
|
|
|
}
|
2025-03-05 21:45:04 +01:00
|
|
|
else if (stringId == STR_QUIT_GAME_PROMPT_TITLE && gLegacyScene == LegacyScene::scenarioEditor)
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
stringId = STR_QUIT_SCENARIO_EDITOR;
|
|
|
|
|
}
|
|
|
|
|
widgets[WIDX_TITLE].text = stringId;
|
|
|
|
|
widgets[WIDX_LABEL].text = window_save_prompt_labels[EnumValue(_promptMode)][1];
|
2024-01-15 08:26:58 +01:00
|
|
|
}
|
2022-10-29 05:19:59 -07:00
|
|
|
}
|
|
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
void OnClose() override
|
2022-10-29 05:19:59 -07:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
// Unpause the game
|
|
|
|
|
if (NetworkGetMode() == NETWORK_MODE_NONE)
|
|
|
|
|
{
|
|
|
|
|
gGamePaused &= ~GAME_PAUSED_MODAL;
|
|
|
|
|
Audio::Resume();
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-25 13:46:08 +01:00
|
|
|
auto* windowMgr = Ui::GetWindowManager();
|
|
|
|
|
windowMgr->InvalidateByClass(WindowClass::TopToolbar);
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnMouseUp(WidgetIndex widgetIndex) override
|
|
|
|
|
{
|
2025-03-05 21:45:04 +01:00
|
|
|
if (gLegacyScene == LegacyScene::titleSequence || gLegacyScene == LegacyScene::trackDesigner
|
|
|
|
|
|| gLegacyScene == LegacyScene::trackDesignsManager)
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
switch (widgetIndex)
|
|
|
|
|
{
|
|
|
|
|
case WQIDX_OK:
|
|
|
|
|
GameLoadOrQuitNoSavePrompt();
|
|
|
|
|
break;
|
|
|
|
|
case WQIDX_CLOSE:
|
|
|
|
|
case WQIDX_CANCEL:
|
|
|
|
|
Close();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-29 05:19:59 -07:00
|
|
|
switch (widgetIndex)
|
|
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
case WIDX_SAVE:
|
|
|
|
|
{
|
|
|
|
|
std::unique_ptr<Intent> intent;
|
|
|
|
|
|
2025-03-05 21:45:04 +01:00
|
|
|
if (isInEditorMode())
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
intent = std::make_unique<Intent>(WindowClass::Loadsave);
|
2025-03-06 22:06:39 +01:00
|
|
|
intent->PutEnumExtra<LoadSaveAction>(INTENT_EXTRA_LOADSAVE_ACTION, LoadSaveAction::save);
|
|
|
|
|
intent->PutEnumExtra<LoadSaveType>(INTENT_EXTRA_LOADSAVE_TYPE, LoadSaveType::landscape);
|
2025-03-20 19:47:53 +01:00
|
|
|
intent->PutExtra(INTENT_EXTRA_PATH, getGameState().scenarioName);
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
intent = CreateSaveGameAsIntent();
|
|
|
|
|
}
|
2022-10-29 05:19:59 -07:00
|
|
|
Close();
|
2024-12-04 20:43:55 +02:00
|
|
|
intent->PutExtra(INTENT_EXTRA_CALLBACK, reinterpret_cast<CloseCallback>(WindowSavePromptCallback));
|
2024-03-10 13:52:35 +00:00
|
|
|
ContextOpenIntent(intent.get());
|
2022-10-29 05:19:59 -07:00
|
|
|
break;
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
case WIDX_DONT_SAVE:
|
|
|
|
|
GameLoadOrQuitNoSavePrompt();
|
|
|
|
|
return;
|
|
|
|
|
case WIDX_CLOSE:
|
|
|
|
|
case WIDX_CANCEL:
|
|
|
|
|
Close();
|
|
|
|
|
return;
|
2022-10-29 05:19:59 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
void OnDraw(DrawPixelInfo& dpi) override
|
2022-10-29 05:19:59 -07:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
DrawWidgets(dpi);
|
2022-10-29 05:19:59 -07:00
|
|
|
}
|
|
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
void OnResize() override
|
|
|
|
|
{
|
|
|
|
|
ResizeFrame();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-17 02:37:46 -04:00
|
|
|
WindowBase* SavePromptOpen()
|
2022-10-29 05:19:59 -07:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
PromptMode prompt_mode = gSavePromptMode;
|
2025-03-01 15:48:02 +01:00
|
|
|
if (prompt_mode == PromptMode::quit)
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
2025-03-01 15:48:02 +01:00
|
|
|
prompt_mode = PromptMode::saveBeforeQuit;
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
2023-05-19 10:17:38 +02:00
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
// do not show save prompt if we're in the title demo and click on load game
|
2025-03-05 21:45:04 +01:00
|
|
|
if (gLegacyScene == LegacyScene::titleSequence)
|
2018-06-22 23:21:44 +02:00
|
|
|
{
|
2023-01-17 13:24:51 +01:00
|
|
|
GameLoadOrQuitNoSavePrompt();
|
2017-08-06 05:22:00 +02:00
|
|
|
return nullptr;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2015-06-09 19:05:59 +01:00
|
|
|
|
2024-05-10 12:10:21 +02:00
|
|
|
if (!Config::Get().general.ConfirmationPrompt)
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
/* game_load_or_quit_no_save_prompt() will exec requested task and close this window
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2014-05-24 01:00:58 +02:00
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
if (gScreenAge < 3840 && NetworkGetMode() == NETWORK_MODE_NONE)
|
|
|
|
|
{
|
|
|
|
|
GameLoadOrQuitNoSavePrompt();
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-02 22:21:08 +01:00
|
|
|
|
2025-01-19 10:58:28 +01:00
|
|
|
auto* windowMgr = GetWindowManager();
|
|
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
// Check if window is already open
|
2025-01-19 10:58:28 +01:00
|
|
|
auto* window = windowMgr->BringToFrontByClass(WindowClass::SavePrompt);
|
2024-03-10 13:52:35 +00:00
|
|
|
if (window != nullptr)
|
|
|
|
|
{
|
2025-01-19 18:49:18 +01:00
|
|
|
windowMgr->Close(*window);
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
2019-08-06 17:59:17 +02:00
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
if (EnumValue(prompt_mode) >= std::size(window_save_prompt_labels))
|
|
|
|
|
{
|
|
|
|
|
LOG_WARNING("Invalid save prompt mode %u", prompt_mode);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t width = WW_SAVE;
|
|
|
|
|
int32_t height = WH_SAVE;
|
2025-03-05 21:45:04 +01:00
|
|
|
if (isInTrackDesignerOrManager())
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
width = WW_QUIT;
|
|
|
|
|
height = WH_QUIT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto savePromptWindow = std::make_unique<SavePromptWindow>(prompt_mode);
|
2025-01-19 10:58:28 +01:00
|
|
|
return windowMgr->Create(
|
2024-03-10 13:52:35 +00:00
|
|
|
std::move(savePromptWindow), WindowClass::SavePrompt, {}, width, height,
|
|
|
|
|
WF_TRANSPARENT | WF_STICK_TO_FRONT | WF_CENTRE_SCREEN | WF_AUTO_POSITION);
|
|
|
|
|
}
|
|
|
|
|
} // namespace OpenRCT2::Ui::Windows
|