Files
OpenRCT2-Web/src/openrct2-ui/windows/LoadSave.cpp
T

1171 lines
40 KiB
C++
Raw Normal View History

2015-02-14 02:16:03 +00:00
/*****************************************************************************
2023-01-01 04:58:01 -06:00
* Copyright (c) 2014-2023 OpenRCT2 developers
*
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
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2015-02-14 02:16:03 +00:00
#include <algorithm>
2018-01-04 00:36:33 +01:00
#include <ctime>
2018-11-21 23:16:04 +01:00
#include <iterator>
#include <memory>
2018-02-08 00:10:55 +01:00
#include <openrct2-ui/interface/Widget.h>
2018-01-08 15:20:59 +01:00
#include <openrct2-ui/windows/Window.h>
#include <openrct2/Context.h>
2018-06-22 23:21:44 +02:00
#include <openrct2/Editor.h>
#include <openrct2/FileClassifier.h>
#include <openrct2/Game.h>
2019-07-21 10:33:15 +01:00
#include <openrct2/GameState.h>
#include <openrct2/PlatformEnvironment.h>
2018-06-22 23:21:44 +02:00
#include <openrct2/config/Config.h>
#include <openrct2/core/File.h>
#include <openrct2/core/FileScanner.h>
2017-09-12 00:04:03 +02:00
#include <openrct2/core/Guard.hpp>
#include <openrct2/core/Path.hpp>
#include <openrct2/core/String.hpp>
2021-12-11 18:06:06 -05:00
#include <openrct2/localisation/Formatter.h>
2018-01-06 18:32:25 +01:00
#include <openrct2/localisation/Localisation.h>
2022-02-18 21:57:00 +01:00
#include <openrct2/platform/Platform.h>
2019-06-30 11:23:06 +01:00
#include <openrct2/rct2/T6Exporter.h>
2018-06-22 23:21:44 +02:00
#include <openrct2/ride/TrackDesign.h>
#include <openrct2/scenario/Scenario.h>
2018-01-08 15:20:59 +01:00
#include <openrct2/title/TitleScreen.h>
2020-11-08 13:29:44 +01:00
#include <openrct2/ui/UiContext.h>
2017-12-13 13:02:24 +01:00
#include <openrct2/util/Util.h>
2018-01-08 15:20:59 +01:00
#include <openrct2/windows/Intent.h>
2018-06-22 23:21:44 +02:00
#include <openrct2/world/Park.h>
2018-01-08 15:20:59 +01:00
#include <string>
#include <vector>
2015-02-14 02:16:03 +00:00
#pragma region Widgets
static constexpr StringId WINDOW_TITLE = STR_NONE;
static constexpr int32_t WW = 350;
static constexpr int32_t WH = 400;
2015-02-14 02:16:03 +00:00
2022-10-02 19:17:36 +02:00
static constexpr uint16_t DATE_TIME_GAP = 2;
2018-01-08 15:20:59 +01:00
enum
{
2017-06-06 23:24:18 +02:00
WIDX_BACKGROUND,
WIDX_TITLE,
WIDX_CLOSE,
2018-01-07 00:30:57 +01:00
WIDX_RESIZE,
2017-06-06 23:24:18 +02:00
WIDX_DEFAULT,
WIDX_UP,
WIDX_NEW_FOLDER,
WIDX_NEW_FILE,
WIDX_SORT_NAME,
WIDX_SORT_DATE,
WIDX_SCROLL,
WIDX_BROWSE,
2015-02-14 02:16:03 +00:00
};
2022-10-02 19:17:36 +02:00
// clang-format off
2022-12-24 16:50:29 +01:00
static Widget window_loadsave_widgets[] =
2018-01-08 15:20:59 +01:00
{
2020-05-09 16:44:21 +02:00
WINDOW_SHIM(WINDOW_TITLE, WW, WH),
2022-10-02 19:17:36 +02:00
MakeWidget({ 0, WH - 1}, { WW, 1}, WindowWidgetType::Resize, WindowColour::Secondary ), // WIDX_RESIZE
MakeWidget({ 4, 36}, { 84, 14}, WindowWidgetType::Button, WindowColour::Primary, STR_LOADSAVE_DEFAULT, STR_LOADSAVE_DEFAULT_TIP), // WIDX_DEFAULT
MakeWidget({ 88, 36}, { 84, 14}, WindowWidgetType::Button, WindowColour::Primary, STR_FILEBROWSER_ACTION_UP ), // WIDX_UP
MakeWidget({ 172, 36}, { 87, 14}, WindowWidgetType::Button, WindowColour::Primary, STR_FILEBROWSER_ACTION_NEW_FOLDER ), // WIDX_NEW_FOLDER
MakeWidget({ 259, 36}, { 87, 14}, WindowWidgetType::Button, WindowColour::Primary, STR_FILEBROWSER_ACTION_NEW_FILE ), // WIDX_NEW_FILE
MakeWidget({ 4, 55}, {170, 14}, WindowWidgetType::TableHeader, WindowColour::Primary ), // WIDX_SORT_NAME
MakeWidget({(WW - 5) / 2 + 1, 55}, {170, 14}, WindowWidgetType::TableHeader, WindowColour::Primary ), // WIDX_SORT_DATE
MakeWidget({ 4, 68}, {342, 303}, WindowWidgetType::Scroll, WindowColour::Primary, SCROLL_VERTICAL ), // WIDX_SCROLL
MakeWidget({ 4, WH - 24}, {197, 19}, WindowWidgetType::Button, WindowColour::Primary, STR_FILEBROWSER_USE_SYSTEM_WINDOW ), // WIDX_BROWSE
2021-09-26 11:11:42 +02:00
WIDGETS_END,
2015-02-14 02:16:03 +00:00
};
// clang-format on
2015-02-14 02:16:03 +00:00
#pragma endregion
2023-06-21 21:15:13 +01:00
static WindowBase* WindowOverwritePromptOpen(const std::string_view name, const std::string_view path);
2022-10-02 19:17:36 +02:00
2018-01-08 15:20:59 +01:00
enum
{
2017-06-06 23:24:18 +02:00
TYPE_DIRECTORY,
TYPE_FILE,
2015-06-22 13:49:35 +02:00
};
2018-02-14 09:42:26 +01:00
struct LoadSaveListItem
2018-01-08 15:20:59 +01:00
{
2021-07-24 23:41:50 +02:00
std::string name{};
std::string path{};
time_t date_modified{ 0 };
std::string date_formatted{};
std::string time_formatted{};
uint8_t type{ 0 };
bool loaded{ false };
2018-02-14 09:42:26 +01:00
};
2015-02-14 02:16:03 +00:00
static std::function<void(int32_t result, std::string_view)> _loadSaveCallback;
2019-06-30 11:23:06 +01:00
static TrackDesign* _trackDesign;
2015-11-02 20:43:55 +00:00
static std::vector<LoadSaveListItem> _listItems;
static char _directory[MAX_PATH];
static char _shortenedDirectory[MAX_PATH];
static char _parentDirectory[MAX_PATH];
2022-03-10 00:12:05 +01:00
static u8string _extensionPattern;
static u8string _defaultPath;
static int32_t _type;
2015-02-14 02:16:03 +00:00
2022-10-02 19:17:36 +02:00
static bool ListItemSort(LoadSaveListItem& a, LoadSaveListItem& b)
{
if (a.type != b.type)
return a.type - b.type < 0;
2022-10-02 19:17:36 +02:00
switch (gConfigGeneral.LoadSaveSort)
{
case Sort::NameAscending:
return StrLogicalCmp(a.name.c_str(), b.name.c_str()) < 0;
2022-10-02 19:17:36 +02:00
case Sort::NameDescending:
return -StrLogicalCmp(a.name.c_str(), b.name.c_str()) < 0;
2022-10-02 19:17:36 +02:00
case Sort::DateDescending:
return -difftime(a.date_modified, b.date_modified) < 0;
case Sort::DateAscending:
return difftime(a.date_modified, b.date_modified) < 0;
}
return StrLogicalCmp(a.name.c_str(), b.name.c_str()) < 0;
2022-10-02 19:17:36 +02:00
}
2015-02-14 02:16:03 +00:00
2022-10-02 19:17:36 +02:00
static void SetAndSaveConfigPath(u8string& config_str, u8string_view path)
{
config_str = Path::GetDirectory(path);
ConfigSaveDefault();
}
static bool IsValidPath(const char* path)
{
// HACK This is needed because tracks get passed through with td?
// I am sure this will change eventually to use the new FileScanner
// which handles multiple patterns
auto filename = Path::GetFileNameWithoutExtension(path);
return Platform::IsFilenameValid(filename);
}
2015-02-14 02:16:03 +00:00
static u8string GetLastDirectoryByType(int32_t type)
2016-07-11 23:48:24 +02:00
{
switch (type & 0x0E)
{
case LOADSAVETYPE_GAME:
2022-10-16 17:27:01 +02:00
return gConfigGeneral.LastSaveGameDirectory;
case LOADSAVETYPE_LANDSCAPE:
2022-10-16 17:27:01 +02:00
return gConfigGeneral.LastSaveLandscapeDirectory;
case LOADSAVETYPE_SCENARIO:
2022-10-16 17:27:01 +02:00
return gConfigGeneral.LastSaveScenarioDirectory;
case LOADSAVETYPE_TRACK:
2022-10-16 17:27:01 +02:00
return gConfigGeneral.LastSaveTrackDirectory;
default:
return u8string();
}
}
static u8string GetInitialDirectoryByType(const int32_t type)
{
std::optional<OpenRCT2::DIRID> subdir = std::nullopt;
switch (type & 0x0E)
{
case LOADSAVETYPE_GAME:
subdir = OpenRCT2::DIRID::SAVE;
break;
case LOADSAVETYPE_LANDSCAPE:
subdir = OpenRCT2::DIRID::LANDSCAPE;
break;
case LOADSAVETYPE_SCENARIO:
subdir = OpenRCT2::DIRID::SCENARIO;
break;
case LOADSAVETYPE_TRACK:
subdir = OpenRCT2::DIRID::TRACK;
break;
case LOADSAVETYPE_HEIGHTMAP:
subdir = OpenRCT2::DIRID::HEIGHTMAP;
break;
}
auto env = OpenRCT2::GetContext()->GetPlatformEnvironment();
if (subdir.has_value())
return env->GetDirectoryPath(OpenRCT2::DIRBASE::USER, subdir.value());
else
return env->GetDirectoryPath(OpenRCT2::DIRBASE::USER);
}
static const char* GetFilterPatternByType(const int32_t type, const bool isSave)
{
switch (type & 0x0E)
{
case LOADSAVETYPE_GAME:
2021-10-27 14:21:14 +02:00
return isSave ? "*.park" : "*.park;*.sv6;*.sc6;*.sc4;*.sv4;*.sv7;*.sea";
case LOADSAVETYPE_LANDSCAPE:
2021-10-27 14:21:14 +02:00
return isSave ? "*.park" : "*.park;*.sc6;*.sv6;*.sc4;*.sv4;*.sv7;*.sea";
case LOADSAVETYPE_SCENARIO:
2021-10-27 14:21:14 +02:00
return isSave ? "*.park" : "*.park;*.sc6;*.sc4";
case LOADSAVETYPE_TRACK:
return isSave ? "*.td6" : "*.td6;*.td4";
case LOADSAVETYPE_HEIGHTMAP:
return "*.bmp;*.png";
default:
Guard::Fail("Unsupported load/save directory type.");
}
return "";
}
2022-03-10 00:12:05 +01:00
static u8string RemovePatternWildcard(u8string_view pattern)
{
while (pattern.length() >= 1 && pattern.front() == '*')
{
pattern.remove_prefix(1);
}
return u8string{ pattern };
}
2022-10-02 19:17:36 +02:00
static u8string GetDir(const int32_t type)
{
u8string result = GetLastDirectoryByType(type);
if (result.empty() || !Path::DirectoryExists(result))
{
result = GetInitialDirectoryByType(type);
}
return result;
2016-07-11 23:48:24 +02:00
}
2022-10-02 19:17:36 +02:00
static void InvokeCallback(int32_t result, const utf8* path)
2015-02-14 02:16:03 +00:00
{
2022-10-02 19:17:36 +02:00
if (_loadSaveCallback != nullptr)
{
2022-10-02 19:17:36 +02:00
_loadSaveCallback(result, path);
}
2015-02-14 02:16:03 +00:00
}
2022-10-02 19:17:36 +02:00
static void Select(const char* path)
2015-02-14 02:16:03 +00:00
{
2022-10-02 19:17:36 +02:00
if (!IsValidPath(path))
2018-01-08 15:20:59 +01:00
{
2022-10-02 19:17:36 +02:00
ContextShowError(STR_ERROR_INVALID_CHARACTERS, STR_NONE, {});
return;
2018-01-07 00:30:57 +01:00
}
2022-10-02 19:17:36 +02:00
char pathBuffer[MAX_PATH];
SafeStrCpy(pathBuffer, path, sizeof(pathBuffer));
2022-10-02 19:17:36 +02:00
switch (_type & 0x0F)
2018-01-08 15:20:59 +01:00
{
2022-10-02 19:17:36 +02:00
case (LOADSAVETYPE_LOAD | LOADSAVETYPE_GAME):
SetAndSaveConfigPath(gConfigGeneral.LastSaveGameDirectory, pathBuffer);
if (OpenRCT2::GetContext()->LoadParkFromFile(pathBuffer))
{
InvokeCallback(MODAL_RESULT_OK, pathBuffer);
WindowCloseByClass(WindowClass::Loadsave);
GfxInvalidateScreen();
}
else
{
// Not the best message...
ContextShowError(STR_LOAD_GAME, STR_FAILED_TO_LOAD_FILE_CONTAINS_INVALID_DATA, {});
InvokeCallback(MODAL_RESULT_FAIL, pathBuffer);
}
2022-10-02 19:17:36 +02:00
break;
case (LOADSAVETYPE_SAVE | LOADSAVETYPE_GAME):
SetAndSaveConfigPath(gConfigGeneral.LastSaveGameDirectory, pathBuffer);
2023-01-17 20:46:55 +01:00
if (ScenarioSave(pathBuffer, gConfigGeneral.SavePluginData ? 1 : 0))
2022-10-02 19:17:36 +02:00
{
gScenarioSavePath = pathBuffer;
gCurrentLoadedPath = pathBuffer;
gIsAutosaveLoaded = false;
gFirstTimeSaving = false;
WindowCloseByClass(WindowClass::Loadsave);
GfxInvalidateScreen();
2022-10-02 19:17:36 +02:00
InvokeCallback(MODAL_RESULT_OK, pathBuffer);
}
else
{
ContextShowError(STR_SAVE_GAME, STR_GAME_SAVE_FAILED, {});
InvokeCallback(MODAL_RESULT_FAIL, pathBuffer);
}
break;
case (LOADSAVETYPE_LOAD | LOADSAVETYPE_LANDSCAPE):
SetAndSaveConfigPath(gConfigGeneral.LastSaveLandscapeDirectory, pathBuffer);
if (Editor::LoadLandscape(pathBuffer))
{
gCurrentLoadedPath = pathBuffer;
GfxInvalidateScreen();
2022-10-02 19:17:36 +02:00
InvokeCallback(MODAL_RESULT_OK, pathBuffer);
}
else
{
// Not the best message...
ContextShowError(STR_LOAD_LANDSCAPE, STR_FAILED_TO_LOAD_FILE_CONTAINS_INVALID_DATA, {});
InvokeCallback(MODAL_RESULT_FAIL, pathBuffer);
}
break;
case (LOADSAVETYPE_SAVE | LOADSAVETYPE_LANDSCAPE):
SetAndSaveConfigPath(gConfigGeneral.LastSaveLandscapeDirectory, pathBuffer);
gScenarioFileName = std::string(String::ToStringView(pathBuffer, std::size(pathBuffer)));
2023-01-17 20:46:55 +01:00
if (ScenarioSave(pathBuffer, gConfigGeneral.SavePluginData ? 3 : 2))
2022-10-02 19:17:36 +02:00
{
gCurrentLoadedPath = pathBuffer;
WindowCloseByClass(WindowClass::Loadsave);
GfxInvalidateScreen();
2022-10-02 19:17:36 +02:00
InvokeCallback(MODAL_RESULT_OK, pathBuffer);
}
else
{
ContextShowError(STR_SAVE_LANDSCAPE, STR_LANDSCAPE_SAVE_FAILED, {});
InvokeCallback(MODAL_RESULT_FAIL, pathBuffer);
}
break;
case (LOADSAVETYPE_SAVE | LOADSAVETYPE_SCENARIO):
{
SetAndSaveConfigPath(gConfigGeneral.LastSaveScenarioDirectory, pathBuffer);
int32_t parkFlagsBackup = gParkFlags;
gParkFlags &= ~PARK_FLAGS_SPRITES_INITIALISED;
gEditorStep = EditorStep::Invalid;
gScenarioFileName = std::string(String::ToStringView(pathBuffer, std::size(pathBuffer)));
2023-01-17 20:46:55 +01:00
int32_t success = ScenarioSave(pathBuffer, gConfigGeneral.SavePluginData ? 3 : 2);
2022-10-02 19:17:36 +02:00
gParkFlags = parkFlagsBackup;
if (success)
{
WindowCloseByClass(WindowClass::Loadsave);
2022-10-02 19:17:36 +02:00
InvokeCallback(MODAL_RESULT_OK, pathBuffer);
2023-01-17 20:46:55 +01:00
TitleLoad();
2022-10-02 19:17:36 +02:00
}
else
{
ContextShowError(STR_FILE_DIALOG_TITLE_SAVE_SCENARIO, STR_SCENARIO_SAVE_FAILED, {});
gEditorStep = EditorStep::ObjectiveSelection;
InvokeCallback(MODAL_RESULT_FAIL, pathBuffer);
}
break;
}
case (LOADSAVETYPE_LOAD | LOADSAVETYPE_TRACK):
{
SetAndSaveConfigPath(gConfigGeneral.LastSaveTrackDirectory, pathBuffer);
auto intent = Intent(WindowClass::InstallTrack);
2023-01-17 20:46:55 +01:00
intent.PutExtra(INTENT_EXTRA_PATH, std::string{ pathBuffer });
2022-10-02 19:17:36 +02:00
ContextOpenIntent(&intent);
WindowCloseByClass(WindowClass::Loadsave);
2022-10-02 19:17:36 +02:00
InvokeCallback(MODAL_RESULT_OK, pathBuffer);
break;
}
case (LOADSAVETYPE_SAVE | LOADSAVETYPE_TRACK):
{
SetAndSaveConfigPath(gConfigGeneral.LastSaveTrackDirectory, pathBuffer);
const auto withExtension = Path::WithExtension(pathBuffer, "td6");
String::Set(pathBuffer, sizeof(pathBuffer), withExtension.c_str());
RCT2::T6Exporter t6Export{ _trackDesign };
auto success = t6Export.SaveTrack(pathBuffer);
if (success)
{
WindowCloseByClass(WindowClass::Loadsave);
2022-10-02 19:17:36 +02:00
WindowRideMeasurementsDesignCancel();
InvokeCallback(MODAL_RESULT_OK, path);
}
else
{
ContextShowError(STR_FILE_DIALOG_TITLE_SAVE_TRACK, STR_TRACK_SAVE_FAILED, {});
InvokeCallback(MODAL_RESULT_FAIL, path);
}
break;
}
case (LOADSAVETYPE_LOAD | LOADSAVETYPE_HEIGHTMAP):
WindowCloseByClass(WindowClass::Loadsave);
2022-10-02 19:17:36 +02:00
InvokeCallback(MODAL_RESULT_OK, pathBuffer);
break;
2018-01-07 00:30:57 +01:00
}
}
2022-10-04 23:59:46 +02:00
static u8string OpenSystemFileBrowser(bool isSave)
2016-07-12 10:18:46 +02:00
{
2022-01-28 23:50:24 +01:00
OpenRCT2::Ui::FileDialogDesc desc = {};
2022-02-12 21:42:55 +01:00
u8string extension{};
2022-01-27 12:14:19 +01:00
auto fileType = FileExtension::Unknown;
2022-07-31 14:22:58 +02:00
StringId title = STR_NONE;
2018-01-08 15:20:59 +01:00
switch (_type & 0x0E)
{
2018-06-22 23:21:44 +02:00
case LOADSAVETYPE_GAME:
2022-02-12 21:42:55 +01:00
extension = u8".park";
2022-01-27 12:14:19 +01:00
fileType = FileExtension::PARK;
2018-06-22 23:21:44 +02:00
title = isSave ? STR_FILE_DIALOG_TITLE_SAVE_GAME : STR_FILE_DIALOG_TITLE_LOAD_GAME;
desc.Filters.emplace_back(LanguageGetString(STR_OPENRCT2_SAVED_GAME), GetFilterPatternByType(_type, isSave));
2018-06-22 23:21:44 +02:00
break;
2018-01-08 15:20:59 +01:00
2018-06-22 23:21:44 +02:00
case LOADSAVETYPE_LANDSCAPE:
2022-02-12 21:42:55 +01:00
extension = u8".park";
2022-01-27 12:14:19 +01:00
fileType = FileExtension::PARK;
2018-06-22 23:21:44 +02:00
title = isSave ? STR_FILE_DIALOG_TITLE_SAVE_LANDSCAPE : STR_FILE_DIALOG_TITLE_LOAD_LANDSCAPE;
desc.Filters.emplace_back(LanguageGetString(STR_OPENRCT2_LANDSCAPE_FILE), GetFilterPatternByType(_type, isSave));
2018-06-22 23:21:44 +02:00
break;
2018-01-08 15:20:59 +01:00
2018-06-22 23:21:44 +02:00
case LOADSAVETYPE_SCENARIO:
2022-02-12 21:42:55 +01:00
extension = u8".park";
2022-01-27 12:14:19 +01:00
fileType = FileExtension::PARK;
2018-06-22 23:21:44 +02:00
title = STR_FILE_DIALOG_TITLE_SAVE_SCENARIO;
desc.Filters.emplace_back(LanguageGetString(STR_OPENRCT2_SCENARIO_FILE), GetFilterPatternByType(_type, isSave));
2018-06-22 23:21:44 +02:00
break;
2018-01-08 15:20:59 +01:00
2018-06-22 23:21:44 +02:00
case LOADSAVETYPE_TRACK:
2022-02-12 21:42:55 +01:00
extension = u8".td6";
2022-01-27 12:14:19 +01:00
fileType = FileExtension::TD6;
2018-06-22 23:21:44 +02:00
title = isSave ? STR_FILE_DIALOG_TITLE_SAVE_TRACK : STR_FILE_DIALOG_TITLE_INSTALL_NEW_TRACK_DESIGN;
desc.Filters.emplace_back(LanguageGetString(STR_OPENRCT2_TRACK_DESIGN_FILE), GetFilterPatternByType(_type, isSave));
2018-06-22 23:21:44 +02:00
break;
2018-01-08 15:20:59 +01:00
case LOADSAVETYPE_HEIGHTMAP:
2018-06-22 23:21:44 +02:00
title = STR_FILE_DIALOG_TITLE_LOAD_HEIGHTMAP;
desc.Filters.emplace_back(LanguageGetString(STR_OPENRCT2_HEIGHTMAP_FILE), GetFilterPatternByType(_type, isSave));
2018-06-22 23:21:44 +02:00
break;
2017-06-06 23:24:18 +02:00
}
2016-07-14 22:11:50 +02:00
u8string path = _directory;
if (isSave)
{
// The file browser requires a file path instead of just a directory
if (!_defaultPath.empty())
{
path = Path::Combine(path, _defaultPath);
}
else
{
2019-07-21 10:33:15 +01:00
auto& park = OpenRCT2::GetContext()->GetGameState()->GetPark();
auto buffer = park.Name;
if (buffer.empty())
{
// Use localised "Unnamed Park" if park name was empty.
buffer = FormatStringID(STR_UNNAMED_PARK, nullptr);
2019-07-21 10:33:15 +01:00
}
path = Path::Combine(path, buffer);
}
}
2022-01-28 23:50:24 +01:00
desc.InitialDirectory = _directory;
desc.Type = isSave ? OpenRCT2::Ui::FileDialogType::Save : OpenRCT2::Ui::FileDialogType::Open;
2022-01-29 14:09:13 +01:00
desc.DefaultFilename = isSave ? path : u8string();
2017-06-06 23:24:18 +02:00
// Add 'all files' filter. If the number of filters is increased, this code will need to be adjusted.
desc.Filters.emplace_back(LanguageGetString(STR_ALL_FILES), "*");
2017-05-28 23:46:25 +01:00
desc.Title = LanguageGetString(title);
2022-03-10 00:12:05 +01:00
u8string outPath = ContextOpenCommonFileDialog(desc);
if (!outPath.empty())
{
2022-10-02 19:17:36 +02:00
// When the given save type was given, Windows still interprets a filename with a dot in its name as a custom
// extension, meaning files like "My Coaster v1.2" will not get the .td6 extension by default.
if (isSave && GetFileExtensionType(outPath) != fileType)
2022-03-10 00:12:05 +01:00
outPath = Path::WithExtension(outPath, extension);
}
2022-03-10 00:12:05 +01:00
return outPath;
2016-07-12 10:18:46 +02:00
}
2022-10-02 19:17:36 +02:00
class LoadSaveWindow final : public Window
2015-02-14 02:16:03 +00:00
{
2022-10-02 19:17:36 +02:00
public:
2022-10-04 23:59:46 +02:00
LoadSaveWindow(int32_t type)
2018-01-08 15:20:59 +01:00
{
2022-10-02 19:17:36 +02:00
widgets = window_loadsave_widgets;
2022-10-04 23:59:46 +02:00
const auto uiContext = OpenRCT2::GetContext()->GetUiContext();
if (!uiContext->HasFilePicker())
{
disabled_widgets |= (1uLL << WIDX_BROWSE);
window_loadsave_widgets[WIDX_BROWSE].type = WindowWidgetType::Empty;
}
2022-11-20 21:47:56 +01:00
// TODO: Split LOADSAVETYPE_* into two proper enum classes (one for load/save, the other for the type)
2022-10-04 23:59:46 +02:00
const bool isSave = (type & 0x01) == LOADSAVETYPE_SAVE;
const auto path = GetDir(type);
const char* pattern = GetFilterPatternByType(type, isSave);
PopulateList(isSave, path, pattern);
no_list_items = static_cast<uint16_t>(_listItems.size());
selected_list_item = -1;
InitScrollWidgets();
ComputeMaxDateWidth();
2022-10-02 19:17:36 +02:00
}
2018-01-08 15:20:59 +01:00
2022-10-02 19:17:36 +02:00
private:
int32_t maxDateWidth{ 0 };
int32_t maxTimeWidth{ 0 };
2016-07-14 22:11:50 +02:00
2022-10-02 19:17:36 +02:00
public:
void PopulateList(int32_t includeNewItem, const u8string& directory, std::string_view extensionPattern)
{
const auto absoluteDirectory = Path::GetAbsolute(directory);
SafeStrCpy(_directory, absoluteDirectory.c_str(), std::size(_directory));
2022-10-02 19:17:36 +02:00
// Note: This compares the pointers, not values
_extensionPattern = extensionPattern;
_shortenedDirectory[0] = '\0';
2018-06-22 23:21:44 +02:00
2022-10-02 19:17:36 +02:00
_listItems.clear();
2018-06-22 23:21:44 +02:00
2022-10-02 19:17:36 +02:00
// Show "new" buttons when saving
window_loadsave_widgets[WIDX_NEW_FILE].type = includeNewItem ? WindowWidgetType::Button : WindowWidgetType::Empty;
window_loadsave_widgets[WIDX_NEW_FOLDER].type = includeNewItem ? WindowWidgetType::Button : WindowWidgetType::Empty;
int32_t drives = Platform::GetDrives();
if (directory.empty() && drives)
{
2022-10-02 19:17:36 +02:00
// List Windows drives
disabled_widgets |= (1uLL << WIDX_NEW_FILE) | (1uLL << WIDX_NEW_FOLDER) | (1uLL << WIDX_UP);
static constexpr auto NumDriveLetters = 26;
for (int32_t x = 0; x < NumDriveLetters; x++)
2018-06-22 23:21:44 +02:00
{
2022-10-02 19:17:36 +02:00
if (drives & (1 << x))
{
// If the drive exists, list it
LoadSaveListItem newListItem;
newListItem.path = std::string(1, 'A' + x) + ":" PATH_SEPARATOR;
newListItem.name = newListItem.path;
newListItem.type = TYPE_DIRECTORY;
_listItems.push_back(std::move(newListItem));
}
2018-06-22 23:21:44 +02:00
}
}
2017-06-06 23:24:18 +02:00
else
2022-03-10 00:12:05 +01:00
{
2022-10-02 19:17:36 +02:00
// Remove the separator at the end of the path, if present
SafeStrCpy(_parentDirectory, absoluteDirectory.c_str(), std::size(_parentDirectory));
2022-10-02 19:17:36 +02:00
if (_parentDirectory[strlen(_parentDirectory) - 1] == *PATH_SEPARATOR
|| _parentDirectory[strlen(_parentDirectory) - 1] == '/')
_parentDirectory[strlen(_parentDirectory) - 1] = '\0';
// Remove everything past the now last separator
char* ch = strrchr(_parentDirectory, *PATH_SEPARATOR);
char* posix_ch = strrchr(_parentDirectory, '/');
ch = ch < posix_ch ? posix_ch : ch;
if (ch != nullptr)
2018-01-08 15:20:59 +01:00
{
2022-10-02 19:17:36 +02:00
*(ch + 1) = '\0';
}
else if (drives)
{
// If on Windows, clear the entire path to show the drives
_parentDirectory[0] = '\0';
}
else
{
// Else, go to the root directory
snprintf(_parentDirectory, MAX_PATH, "%c", *PATH_SEPARATOR);
2017-06-06 23:24:18 +02:00
}
2018-01-08 15:20:59 +01:00
2022-10-02 19:17:36 +02:00
// Disable the Up button if the current directory is the root directory
if (String::IsNullOrEmpty(_parentDirectory) && !drives)
2022-10-02 19:17:36 +02:00
disabled_widgets |= (1uLL << WIDX_UP);
2017-06-06 23:24:18 +02:00
else
2022-10-02 19:17:36 +02:00
disabled_widgets &= ~(1uLL << WIDX_UP);
2015-02-14 02:16:03 +00:00
2022-10-02 19:17:36 +02:00
// Re-enable the "new" buttons if these were disabled
disabled_widgets &= ~(1uLL << WIDX_NEW_FILE);
disabled_widgets &= ~(1uLL << WIDX_NEW_FOLDER);
2020-03-30 21:09:07 +02:00
2022-10-02 19:17:36 +02:00
// List all directories
auto subDirectories = Path::GetDirectories(absoluteDirectory);
for (const auto& sdName : subDirectories)
2018-01-08 15:20:59 +01:00
{
2022-10-02 19:17:36 +02:00
auto subDir = sdName + PATH_SEPARATOR;
LoadSaveListItem newListItem;
2022-10-02 19:17:36 +02:00
newListItem.path = Path::Combine(absoluteDirectory, subDir);
newListItem.name = std::move(subDir);
newListItem.type = TYPE_DIRECTORY;
2022-10-02 19:17:36 +02:00
newListItem.loaded = false;
2016-07-14 22:11:50 +02:00
_listItems.push_back(std::move(newListItem));
2017-06-06 23:24:18 +02:00
}
2022-10-02 19:17:36 +02:00
// List all files with the wanted extensions
bool showExtension = false;
for (const u8string& extToken : String::Split(extensionPattern, ";"))
{
const u8string filter = Path::Combine(directory, extToken);
auto scanner = Path::ScanDirectory(filter, false);
while (scanner->Next())
{
LoadSaveListItem newListItem;
newListItem.path = scanner->GetPath();
newListItem.type = TYPE_FILE;
newListItem.date_modified = Platform::FileGetModifiedTime(newListItem.path.c_str());
// Cache a human-readable version of the modified date.
newListItem.date_formatted = Platform::FormatShortDate(newListItem.date_modified);
newListItem.time_formatted = Platform::FormatTime(newListItem.date_modified);
// Mark if file is the currently loaded game
newListItem.loaded = newListItem.path.compare(gCurrentLoadedPath) == 0;
// Remove the extension (but only the first extension token)
if (!showExtension)
{
newListItem.name = Path::GetFileNameWithoutExtension(newListItem.path);
}
else
{
newListItem.name = Path::GetFileName(newListItem.path);
}
_listItems.push_back(std::move(newListItem));
}
showExtension = true; // Show any extension after the first iteration
}
SortList();
}
Invalidate();
}
void ComputeMaxDateWidth()
{
// Generate a time object for a relatively wide time: 2000-02-20 00:00:00
std::tm tm;
tm.tm_sec = 0;
tm.tm_min = 0;
tm.tm_hour = 0;
tm.tm_mday = 20;
tm.tm_mon = 2;
tm.tm_year = 100;
tm.tm_wday = 5;
tm.tm_yday = 51;
tm.tm_isdst = -1;
std::time_t long_time = mktime(&tm);
// Check how this date is represented (e.g. 2000-02-20, or 00/02/20)
std::string date = Platform::FormatShortDate(long_time);
maxDateWidth = GfxGetStringWidth(date.c_str(), FontStyle::Medium) + DATE_TIME_GAP;
2022-10-02 19:17:36 +02:00
// Some locales do not use leading zeros for months and days, so let's try October, too.
tm.tm_mon = 10;
tm.tm_yday = 294;
long_time = mktime(&tm);
// Again, check how this date is represented (e.g. 2000-10-20, or 00/10/20)
date = Platform::FormatShortDate(long_time);
maxDateWidth = std::max(maxDateWidth, GfxGetStringWidth(date.c_str(), FontStyle::Medium) + DATE_TIME_GAP);
2022-10-02 19:17:36 +02:00
// Time appears to be universally represented with two digits for minutes, so 12:00 or 00:00 should be representable.
std::string time = Platform::FormatTime(long_time);
maxTimeWidth = GfxGetStringWidth(time.c_str(), FontStyle::Medium) + DATE_TIME_GAP;
2022-10-02 19:17:36 +02:00
}
void SortList()
{
std::sort(_listItems.begin(), _listItems.end(), ListItemSort);
}
#pragma region Events
public:
2022-11-20 21:47:56 +01:00
void OnOpen() override
2022-10-02 19:17:36 +02:00
{
min_width = WW;
min_height = WH / 2;
max_width = WW * 2;
max_height = WH * 2;
2022-10-02 19:17:36 +02:00
}
2022-11-20 21:47:56 +01:00
void OnClose() override
2022-10-02 19:17:36 +02:00
{
_listItems.clear();
WindowCloseByClass(WindowClass::LoadsaveOverwritePrompt);
2022-10-02 19:17:36 +02:00
}
2022-11-20 21:47:56 +01:00
void OnResize() override
2022-10-02 19:17:36 +02:00
{
if (width < min_width)
{
Invalidate();
width = min_width;
}
if (height < min_height)
{
Invalidate();
height = min_height;
2017-06-06 23:24:18 +02:00
}
}
2022-10-02 19:17:36 +02:00
2022-11-20 21:47:56 +01:00
void OnPrepareDraw() override
2018-01-08 15:20:59 +01:00
{
2022-10-02 19:17:36 +02:00
ResizeFrameWithPage();
2016-07-14 22:11:50 +02:00
2022-12-24 16:50:29 +01:00
Widget* date_widget = &window_loadsave_widgets[WIDX_SORT_DATE];
2022-10-02 19:17:36 +02:00
date_widget->right = width - 5;
date_widget->left = date_widget->right - (maxDateWidth + maxTimeWidth + (4 * DATE_TIME_GAP) + (SCROLLBAR_WIDTH + 1));
window_loadsave_widgets[WIDX_SORT_NAME].left = 4;
window_loadsave_widgets[WIDX_SORT_NAME].right = window_loadsave_widgets[WIDX_SORT_DATE].left - 1;
window_loadsave_widgets[WIDX_SCROLL].right = width - 5;
2022-10-02 19:17:36 +02:00
window_loadsave_widgets[WIDX_SCROLL].bottom = height - 30;
window_loadsave_widgets[WIDX_BROWSE].top = height - 24;
window_loadsave_widgets[WIDX_BROWSE].bottom = height - 6;
}
void OnDraw(DrawPixelInfo& dpi) override
2022-10-02 19:17:36 +02:00
{
DrawWidgets(dpi);
if (_shortenedDirectory[0] == '\0')
2018-01-08 15:20:59 +01:00
{
ShortenPath(_shortenedDirectory, sizeof(_shortenedDirectory), _directory, width - 8, FontStyle::Medium);
2018-01-08 15:20:59 +01:00
}
2022-10-02 19:17:36 +02:00
// Format text
thread_local std::string _buffer;
_buffer.assign("{BLACK}");
_buffer += _shortenedDirectory;
// Draw path text
const auto normalisedPath = Platform::StrDecompToPrecomp(_buffer.data());
const auto* normalisedPathC = normalisedPath.c_str();
auto ft = Formatter();
ft.Add<const char*>(normalisedPathC);
2023-02-24 22:05:07 +01:00
DrawTextEllipsised(dpi, windowPos + ScreenCoordsXY{ 4, 20 }, width - 8, STR_STRING, ft);
2022-10-02 19:17:36 +02:00
// Name button text
StringId id = STR_NONE;
if (gConfigGeneral.LoadSaveSort == Sort::NameAscending)
id = STR_UP;
else if (gConfigGeneral.LoadSaveSort == Sort::NameDescending)
id = STR_DOWN;
// Draw name button indicator.
2022-12-24 16:50:29 +01:00
Widget sort_name_widget = window_loadsave_widgets[WIDX_SORT_NAME];
2022-10-02 19:17:36 +02:00
ft = Formatter();
ft.Add<StringId>(id);
DrawTextBasic(
2023-02-24 22:05:07 +01:00
dpi, windowPos + ScreenCoordsXY{ sort_name_widget.left + 11, sort_name_widget.top + 1 }, STR_NAME, ft,
2022-10-02 19:17:36 +02:00
{ COLOUR_GREY });
// Date button text
if (gConfigGeneral.LoadSaveSort == Sort::DateAscending)
id = STR_UP;
else if (gConfigGeneral.LoadSaveSort == Sort::DateDescending)
id = STR_DOWN;
2018-01-08 15:20:59 +01:00
else
2022-10-02 19:17:36 +02:00
id = STR_NONE;
2022-12-24 16:50:29 +01:00
Widget sort_date_widget = window_loadsave_widgets[WIDX_SORT_DATE];
2022-10-02 19:17:36 +02:00
ft = Formatter();
ft.Add<StringId>(id);
DrawTextBasic(
2023-02-24 22:05:07 +01:00
dpi, windowPos + ScreenCoordsXY{ sort_date_widget.left + 5, sort_date_widget.top + 1 }, STR_DATE, ft,
2022-10-02 19:17:36 +02:00
{ COLOUR_GREY });
}
2022-11-20 21:47:56 +01:00
void OnMouseUp(WidgetIndex widgetIndex) override
2022-10-02 19:17:36 +02:00
{
bool isSave = (_type & 0x01) == LOADSAVETYPE_SAVE;
switch (widgetIndex)
2018-01-08 15:20:59 +01:00
{
2022-10-02 19:17:36 +02:00
case WIDX_CLOSE:
Close();
break;
2016-07-14 22:11:50 +02:00
2022-10-02 19:17:36 +02:00
case WIDX_UP:
PopulateList(isSave, _parentDirectory, _extensionPattern);
InitScrollWidgets();
no_list_items = static_cast<uint16_t>(_listItems.size());
break;
2016-07-14 22:11:50 +02:00
2022-10-02 19:17:36 +02:00
case WIDX_NEW_FILE:
WindowTextInputOpen(
this, WIDX_NEW_FILE, STR_NONE, STR_FILEBROWSER_FILE_NAME_PROMPT, {}, STR_STRING,
reinterpret_cast<uintptr_t>(_defaultPath.c_str()), 64);
break;
2016-07-12 12:17:07 +02:00
2022-10-02 19:17:36 +02:00
case WIDX_NEW_FOLDER:
WindowTextInputRawOpen(this, WIDX_NEW_FOLDER, STR_NONE, STR_FILEBROWSER_FOLDER_NAME_PROMPT, {}, "", 64);
break;
2022-10-02 19:17:36 +02:00
case WIDX_BROWSE:
2018-01-08 15:20:59 +01:00
{
2022-10-04 23:59:46 +02:00
u8string path = OpenSystemFileBrowser(isSave);
2022-10-02 19:17:36 +02:00
if (!path.empty())
{
2022-10-02 19:17:36 +02:00
Select(path.c_str());
}
else
{
2022-10-02 19:17:36 +02:00
// If user cancels file dialog, refresh list
PopulateList(isSave, _directory, _extensionPattern);
InitScrollWidgets();
no_list_items = static_cast<uint16_t>(_listItems.size());
}
}
break;
case WIDX_SORT_NAME:
if (gConfigGeneral.LoadSaveSort == Sort::NameAscending)
{
gConfigGeneral.LoadSaveSort = Sort::NameDescending;
}
else
{
gConfigGeneral.LoadSaveSort = Sort::NameAscending;
}
ConfigSaveDefault();
SortList();
Invalidate();
break;
case WIDX_SORT_DATE:
if (gConfigGeneral.LoadSaveSort == Sort::DateDescending)
{
gConfigGeneral.LoadSaveSort = Sort::DateAscending;
}
else
{
gConfigGeneral.LoadSaveSort = Sort::DateDescending;
}
ConfigSaveDefault();
SortList();
Invalidate();
break;
case WIDX_DEFAULT:
PopulateList(isSave, GetInitialDirectoryByType(_type).c_str(), _extensionPattern);
InitScrollWidgets();
no_list_items = static_cast<uint16_t>(_listItems.size());
break;
}
}
2022-11-20 21:47:56 +01:00
void OnTextInput(WidgetIndex widgetIndex, std::string_view text) override
2022-10-02 19:17:36 +02:00
{
if (text.empty())
return;
if (!Platform::IsFilenameValid(text))
{
ContextShowError(STR_ERROR_INVALID_CHARACTERS, STR_NONE, {});
return;
}
switch (widgetIndex)
{
case WIDX_NEW_FOLDER:
{
const u8string path = Path::Combine(_directory, text);
2023-05-09 21:08:46 +02:00
if (!Path::CreateDirectory(path))
2022-10-02 19:17:36 +02:00
{
ContextShowError(STR_UNABLE_TO_CREATE_FOLDER, STR_NONE, {});
return;
}
2022-10-02 19:17:36 +02:00
no_list_items = 0;
selected_list_item = -1;
PopulateList((_type & 1) == LOADSAVETYPE_SAVE, path, _extensionPattern);
InitScrollWidgets();
no_list_items = static_cast<uint16_t>(_listItems.size());
Invalidate();
break;
2017-06-06 23:24:18 +02:00
}
2015-06-22 15:55:57 +02:00
2022-10-02 19:17:36 +02:00
case WIDX_NEW_FILE:
{
const u8string path = Path::WithExtension(
Path::Combine(_directory, text), RemovePatternWildcard(_extensionPattern));
2016-07-14 22:11:50 +02:00
2022-10-02 19:17:36 +02:00
if (File::Exists(path))
2023-06-21 21:15:13 +01:00
WindowOverwritePromptOpen(text, path);
2022-10-02 19:17:36 +02:00
else
Select(path.c_str());
break;
}
}
2017-06-06 23:24:18 +02:00
}
2018-03-13 22:18:59 +01:00
2022-11-20 21:47:56 +01:00
ScreenSize OnScrollGetSize(int32_t scrollIndex) override
2018-01-08 15:20:59 +01:00
{
2022-10-02 19:17:36 +02:00
return { 0, no_list_items * SCROLLABLE_ROW_HEIGHT };
2017-06-06 23:24:18 +02:00
}
2015-11-02 20:43:55 +00:00
2022-11-20 21:47:56 +01:00
void OnScrollMouseOver(int32_t scrollIndex, const ScreenCoordsXY& screenCoords) override
2018-01-08 15:20:59 +01:00
{
2022-10-02 19:17:36 +02:00
int32_t selectedItem;
selectedItem = screenCoords.y / SCROLLABLE_ROW_HEIGHT;
if (selectedItem >= no_list_items)
return;
selected_list_item = selectedItem;
Invalidate();
2017-06-06 23:24:18 +02:00
}
2022-11-20 21:47:56 +01:00
void OnScrollMouseDown(int32_t scrollIndex, const ScreenCoordsXY& screenCoords) override
2018-06-22 23:21:44 +02:00
{
2022-10-02 19:17:36 +02:00
int32_t selectedItem;
2015-11-02 20:43:55 +00:00
2022-10-02 19:17:36 +02:00
selectedItem = screenCoords.y / SCROLLABLE_ROW_HEIGHT;
if (selectedItem >= no_list_items)
return;
2018-01-08 15:20:59 +01:00
2022-10-02 19:17:36 +02:00
if (_listItems[selectedItem].type == TYPE_DIRECTORY)
2018-01-08 15:20:59 +01:00
{
2022-10-02 19:17:36 +02:00
// The selected item is a folder
int32_t includeNewItem;
2018-06-22 23:21:44 +02:00
2022-10-02 19:17:36 +02:00
no_list_items = 0;
selected_list_item = -1;
includeNewItem = (_type & 1) == LOADSAVETYPE_SAVE;
char directory[MAX_PATH];
SafeStrCpy(directory, _listItems[selectedItem].path.c_str(), sizeof(directory));
2022-10-02 19:17:36 +02:00
PopulateList(includeNewItem, directory, _extensionPattern);
InitScrollWidgets();
no_list_items = static_cast<uint16_t>(_listItems.size());
2018-06-22 23:21:44 +02:00
}
2022-10-02 19:17:36 +02:00
else
2018-06-22 23:21:44 +02:00
{
2022-10-02 19:17:36 +02:00
// TYPE_FILE
// Load or overwrite
if ((_type & 0x01) == LOADSAVETYPE_SAVE)
2023-06-21 21:15:13 +01:00
WindowOverwritePromptOpen(_listItems[selectedItem].name, _listItems[selectedItem].path);
2018-06-22 23:21:44 +02:00
else
2022-10-02 19:17:36 +02:00
Select(_listItems[selectedItem].path.c_str());
2017-06-06 23:24:18 +02:00
}
}
2022-10-02 19:17:36 +02:00
void OnScrollDraw(int32_t scrollIndex, DrawPixelInfo& dpi) override
2022-10-02 19:17:36 +02:00
{
GfxFillRect(
dpi, { { dpi.x, dpi.y }, { dpi.x + dpi.width - 1, dpi.y + dpi.height - 1 } }, ColourMapA[colours[1]].mid_light);
2022-10-02 19:17:36 +02:00
const int32_t listWidth = widgets[WIDX_SCROLL].width();
const int32_t dateAnchor = widgets[WIDX_SORT_DATE].left + maxDateWidth + DATE_TIME_GAP;
for (int32_t i = 0; i < no_list_items; i++)
{
int32_t y = i * SCROLLABLE_ROW_HEIGHT;
if (y > dpi.y + dpi.height)
break;
if (y + SCROLLABLE_ROW_HEIGHT < dpi.y)
continue;
StringId stringId = STR_BLACK_STRING;
// If hovering over item, change the color and fill the backdrop.
if (i == selected_list_item)
{
stringId = STR_WINDOW_COLOUR_2_STRINGID;
GfxFilterRect(dpi, { 0, y, listWidth, y + SCROLLABLE_ROW_HEIGHT }, FilterPaletteID::PaletteDarken1);
2022-10-02 19:17:36 +02:00
}
// display a marker next to the currently loaded game file
if (_listItems[i].loaded)
{
auto ft = Formatter();
ft.Add<StringId>(STR_RIGHTGUILLEMET);
2023-02-24 22:05:07 +01:00
DrawTextBasic(dpi, { 0, y }, stringId, ft);
2022-10-02 19:17:36 +02:00
}
// Print filename
auto ft = Formatter();
ft.Add<StringId>(STR_STRING);
ft.Add<char*>(_listItems[i].name.c_str());
int32_t max_file_width = widgets[WIDX_SORT_NAME].width() - 10;
2023-02-24 22:05:07 +01:00
DrawTextEllipsised(dpi, { 10, y }, max_file_width, stringId, ft);
2022-10-02 19:17:36 +02:00
// Print formatted modified date, if this is a file
if (_listItems[i].type == TYPE_FILE)
{
ft = Formatter();
ft.Add<StringId>(STR_STRING);
ft.Add<char*>(_listItems[i].date_formatted.c_str());
DrawTextEllipsised(
2023-02-24 22:05:07 +01:00
dpi, { dateAnchor - DATE_TIME_GAP, y }, maxDateWidth, stringId, ft, { TextAlignment::RIGHT });
2022-10-02 19:17:36 +02:00
ft = Formatter();
ft.Add<StringId>(STR_STRING);
ft.Add<char*>(_listItems[i].time_formatted.c_str());
2023-02-24 22:05:07 +01:00
DrawTextEllipsised(dpi, { dateAnchor + DATE_TIME_GAP, y }, maxTimeWidth, stringId, ft);
2022-10-02 19:17:36 +02:00
}
}
}
#pragma endregion
};
WindowBase* WindowLoadsaveOpen(
2022-10-02 19:17:36 +02:00
int32_t type, std::string_view defaultPath, std::function<void(int32_t result, std::string_view)> callback,
TrackDesign* trackDesign)
{
_loadSaveCallback = callback;
_trackDesign = trackDesign;
_type = type;
_defaultPath = defaultPath;
bool isSave = (type & 0x01) == LOADSAVETYPE_SAVE;
// Bypass the lot?
auto hasFilePicker = OpenRCT2::GetContext()->GetUiContext()->HasFilePicker();
if (gConfigGeneral.UseNativeBrowseDialog && hasFilePicker)
{
2022-10-04 23:59:46 +02:00
const u8string path = OpenSystemFileBrowser(isSave);
2022-10-02 19:17:36 +02:00
if (!path.empty())
{
Select(path.c_str());
}
return nullptr;
}
const u8string path = GetDir(type);
auto* w = static_cast<LoadSaveWindow*>(WindowBringToFrontByClass(WindowClass::Loadsave));
2022-10-02 19:17:36 +02:00
if (w == nullptr)
{
w = WindowCreate<LoadSaveWindow>(
2022-10-04 23:59:46 +02:00
WindowClass::Loadsave, WW, WH, WF_STICK_TO_FRONT | WF_RESIZABLE | WF_AUTO_POSITION | WF_CENTRE_SCREEN, type);
2022-10-02 19:17:36 +02:00
}
switch (type & 0x0E)
{
case LOADSAVETYPE_GAME:
w->widgets[WIDX_TITLE].text = isSave ? STR_FILE_DIALOG_TITLE_SAVE_GAME : STR_FILE_DIALOG_TITLE_LOAD_GAME;
break;
case LOADSAVETYPE_LANDSCAPE:
w->widgets[WIDX_TITLE].text = isSave ? STR_FILE_DIALOG_TITLE_SAVE_LANDSCAPE : STR_FILE_DIALOG_TITLE_LOAD_LANDSCAPE;
break;
case LOADSAVETYPE_SCENARIO:
w->widgets[WIDX_TITLE].text = STR_FILE_DIALOG_TITLE_SAVE_SCENARIO;
break;
case LOADSAVETYPE_TRACK:
w->widgets[WIDX_TITLE].text = isSave ? STR_FILE_DIALOG_TITLE_SAVE_TRACK
: STR_FILE_DIALOG_TITLE_INSTALL_NEW_TRACK_DESIGN;
break;
case LOADSAVETYPE_HEIGHTMAP:
Guard::Assert(!isSave, "Cannot save images through loadsave window");
2022-10-02 19:17:36 +02:00
w->widgets[WIDX_TITLE].text = STR_FILE_DIALOG_TITLE_LOAD_HEIGHTMAP;
break;
default:
Guard::Fail("Unsupported load/save type: %d", type & 0x0F);
2022-10-04 23:59:46 +02:00
break;
2022-10-02 19:17:36 +02:00
}
return w;
2015-02-14 02:16:03 +00:00
}
#pragma region Overwrite prompt
2019-10-24 00:38:13 -03:00
constexpr int32_t OVERWRITE_WW = 200;
constexpr int32_t OVERWRITE_WH = 100;
2015-02-14 02:16:03 +00:00
2018-06-22 23:21:44 +02:00
enum
{
2017-06-06 23:24:18 +02:00
WIDX_OVERWRITE_BACKGROUND,
WIDX_OVERWRITE_TITLE,
WIDX_OVERWRITE_CLOSE,
WIDX_OVERWRITE_OVERWRITE,
WIDX_OVERWRITE_CANCEL
2015-02-14 02:16:03 +00:00
};
2022-12-24 16:50:29 +01:00
static Widget window_overwrite_prompt_widgets[] = {
2020-05-09 18:02:16 +02:00
WINDOW_SHIM_WHITE(STR_FILEBROWSER_OVERWRITE_TITLE, OVERWRITE_WW, OVERWRITE_WH),
{ WindowWidgetType::Button, 0, 10, 94, OVERWRITE_WH - 20, OVERWRITE_WH - 9, STR_FILEBROWSER_OVERWRITE_TITLE, STR_NONE },
{ WindowWidgetType::Button, 0, OVERWRITE_WW - 95, OVERWRITE_WW - 11, OVERWRITE_WH - 20, OVERWRITE_WH - 9,
STR_SAVE_PROMPT_CANCEL, STR_NONE },
2021-09-26 11:11:42 +02:00
WIDGETS_END,
};
2015-02-14 02:16:03 +00:00
2023-06-21 21:15:13 +01:00
class OverwritePromptWindow final : public Window
2015-02-14 02:16:03 +00:00
{
2023-06-21 21:15:13 +01:00
std::string _name;
std::string _path;
2016-07-11 16:38:44 +02:00
2023-06-21 21:15:13 +01:00
public:
OverwritePromptWindow(const std::string_view name, const std::string_view path)
: _name(name)
, _path(path)
{
widgets = window_overwrite_prompt_widgets;
colours[0] = TRANSLUCENT(COLOUR_BORDEAUX_RED);
}
void OnMouseUp(WidgetIndex widgetIndex) override
{
switch (widgetIndex)
{
case WIDX_OVERWRITE_OVERWRITE:
{
Select(_path.c_str());
// As the LoadSaveWindow::Select function can change the order of the
// windows we can't use WindowClose(w).
WindowCloseByClass(WindowClass::LoadsaveOverwritePrompt);
break;
}
case WIDX_OVERWRITE_CANCEL:
case WIDX_OVERWRITE_CLOSE:
Close();
break;
}
}
void OnDraw(DrawPixelInfo& dpi) override
{
DrawWidgets(dpi);
auto ft = Formatter();
ft.Add<StringId>(STR_STRING);
ft.Add<char*>(_name.c_str());
ScreenCoordsXY stringCoords(windowPos.x + width / 2, windowPos.y + (height / 2) - 3);
DrawTextWrapped(dpi, stringCoords, width - 4, STR_FILEBROWSER_OVERWRITE_PROMPT, ft, { TextAlignment::CENTRE });
}
};
static WindowBase* WindowOverwritePromptOpen(const std::string_view name, const std::string_view path)
{
WindowCloseByClass(WindowClass::LoadsaveOverwritePrompt);
2015-02-14 02:16:03 +00:00
2023-06-21 21:15:13 +01:00
return WindowCreate<OverwritePromptWindow>(
WindowClass::LoadsaveOverwritePrompt, OVERWRITE_WW, OVERWRITE_WH, WF_TRANSPARENT | WF_STICK_TO_FRONT | WF_CENTRE_SCREEN,
name, path);
2015-02-14 02:16:03 +00:00
}
#pragma endregion