1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-15 11:03:00 +01:00

Merge pull request #16531 from Gymnasiast/refactor/file_dialog_desc

Merge old and new FileDialogDesc
This commit is contained in:
Michael Steenbeek
2022-01-29 14:32:19 +01:00
committed by GitHub
8 changed files with 53 additions and 83 deletions

View File

@@ -138,7 +138,7 @@ namespace OpenRCT2::Ui
{
case DIALOG_TYPE::KDIALOG:
{
std::string action = (desc.Type == FILE_DIALOG_TYPE::OPEN) ? "--getopenfilename" : "--getsavefilename";
std::string action = (desc.Type == FileDialogType::Open) ? "--getopenfilename" : "--getsavefilename";
std::string filter = GetKDialogFilterString(desc.Filters);
std::string cmd = String::StdFormat(
"%s --title '%s' %s '%s' '%s'", executablePath.c_str(), desc.Title.c_str(), action.c_str(),
@@ -154,7 +154,7 @@ namespace OpenRCT2::Ui
{
std::string action = "--file-selection";
std::string flags;
if (desc.Type == FILE_DIALOG_TYPE::SAVE)
if (desc.Type == FileDialogType::Save)
{
flags = "--confirm-overwrite --save";
}
@@ -165,7 +165,7 @@ namespace OpenRCT2::Ui
std::string output;
if (Platform::Execute(cmd, &output) == 0)
{
if (desc.Type == FILE_DIALOG_TYPE::SAVE)
if (desc.Type == FileDialogType::Save)
{
// The default file extension is taken from the **first** available filter, since
// we cannot obtain it from zenity's output. This means that the FileDialogDesc::Filters
@@ -197,14 +197,14 @@ namespace OpenRCT2::Ui
if (!result.empty())
{
if (desc.Type == FILE_DIALOG_TYPE::OPEN && access(result.c_str(), F_OK) == -1)
if (desc.Type == FileDialogType::Open && access(result.c_str(), F_OK) == -1)
{
std::string msg = String::StdFormat(
"\"%s\" not found: %s, please choose another file\n", result.c_str(), strerror(errno));
ShowMessageBox(window, msg);
return ShowFileDialog(window, desc);
}
if (desc.Type == FILE_DIALOG_TYPE::SAVE && access(result.c_str(), F_OK) != -1 && dtype == DIALOG_TYPE::KDIALOG)
if (desc.Type == FileDialogType::Save && access(result.c_str(), F_OK) != -1 && dtype == DIALOG_TYPE::KDIALOG)
{
std::string cmd = String::StdFormat("%s --yesno \"Overwrite %s?\"", executablePath.c_str(), result.c_str());
if (Platform::Execute(cmd) != 0)

View File

@@ -139,12 +139,12 @@ namespace OpenRCT2::Ui
// Open dialog
BOOL dialogResult = FALSE;
DWORD commonFlags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR;
if (desc.Type == FILE_DIALOG_TYPE::OPEN)
if (desc.Type == FileDialogType::Open)
{
openFileName.Flags = commonFlags | OFN_NONETWORKBUTTON | OFN_FILEMUSTEXIST;
dialogResult = GetOpenFileNameW(&openFileName);
}
else if (desc.Type == FILE_DIALOG_TYPE::SAVE)
else if (desc.Type == FileDialogType::Save)
{
openFileName.Flags = commonFlags | OFN_CREATEPROMPT | OFN_OVERWRITEPROMPT;
dialogResult = GetSaveFileNameW(&openFileName);

View File

@@ -110,7 +110,7 @@ namespace OpenRCT2::Ui
NSString* directory;
NSSavePanel* panel;
if (desc.Type == FILE_DIALOG_TYPE::SAVE)
if (desc.Type == FileDialogType::Save)
{
NSString* filePath = [NSString stringWithUTF8String:desc.DefaultFilename.c_str()];
directory = filePath.stringByDeletingLastPathComponent;
@@ -118,7 +118,7 @@ namespace OpenRCT2::Ui
panel = [NSSavePanel savePanel];
panel.nameFieldStringValue = [NSString stringWithFormat:@"%@.%@", basename, extensions.firstObject];
}
else if (desc.Type == FILE_DIALOG_TYPE::OPEN)
else if (desc.Type == FileDialogType::Open)
{
directory = [NSString stringWithUTF8String:desc.InitialDirectory.c_str()];
NSOpenPanel* open = [NSOpenPanel openPanel];

View File

@@ -354,8 +354,8 @@ static void WindowLoadsaveResize(rct_window* w)
static bool Browse(bool isSave, char* path, size_t pathSize)
{
file_dialog_desc desc = {};
const utf8* extension = "";
OpenRCT2::Ui::FileDialogDesc desc = {};
u8string extension = "";
auto fileType = FileExtension::Unknown;
rct_string_id title = STR_NONE;
switch (_type & 0x0E)
@@ -364,38 +364,34 @@ static bool Browse(bool isSave, char* path, size_t pathSize)
extension = ".park";
fileType = FileExtension::PARK;
title = isSave ? STR_FILE_DIALOG_TITLE_SAVE_GAME : STR_FILE_DIALOG_TITLE_LOAD_GAME;
desc.filters[0].name = language_get_string(STR_OPENRCT2_SAVED_GAME);
desc.filters[0].pattern = GetFilterPatternByType(_type, isSave);
desc.Filters.emplace_back(language_get_string(STR_OPENRCT2_SAVED_GAME), GetFilterPatternByType(_type, isSave));
break;
case LOADSAVETYPE_LANDSCAPE:
extension = ".park";
fileType = FileExtension::PARK;
title = isSave ? STR_FILE_DIALOG_TITLE_SAVE_LANDSCAPE : STR_FILE_DIALOG_TITLE_LOAD_LANDSCAPE;
desc.filters[0].name = language_get_string(STR_OPENRCT2_LANDSCAPE_FILE);
desc.filters[0].pattern = GetFilterPatternByType(_type, isSave);
desc.Filters.emplace_back(language_get_string(STR_OPENRCT2_LANDSCAPE_FILE), GetFilterPatternByType(_type, isSave));
break;
case LOADSAVETYPE_SCENARIO:
extension = ".park";
fileType = FileExtension::PARK;
title = STR_FILE_DIALOG_TITLE_SAVE_SCENARIO;
desc.filters[0].name = language_get_string(STR_OPENRCT2_SCENARIO_FILE);
desc.filters[0].pattern = GetFilterPatternByType(_type, isSave);
desc.Filters.emplace_back(language_get_string(STR_OPENRCT2_SCENARIO_FILE), GetFilterPatternByType(_type, isSave));
break;
case LOADSAVETYPE_TRACK:
extension = ".td6";
fileType = FileExtension::TD6;
title = isSave ? STR_FILE_DIALOG_TITLE_SAVE_TRACK : STR_FILE_DIALOG_TITLE_INSTALL_NEW_TRACK_DESIGN;
desc.filters[0].name = language_get_string(STR_OPENRCT2_TRACK_DESIGN_FILE);
desc.filters[0].pattern = GetFilterPatternByType(_type, isSave);
desc.Filters.emplace_back(
language_get_string(STR_OPENRCT2_TRACK_DESIGN_FILE), GetFilterPatternByType(_type, isSave));
break;
case LOADSAVETYPE_HEIGHTMAP:
title = STR_FILE_DIALOG_TITLE_LOAD_HEIGHTMAP;
desc.filters[0].name = language_get_string(STR_OPENRCT2_HEIGHTMAP_FILE);
desc.filters[0].pattern = GetFilterPatternByType(_type, isSave);
desc.Filters.emplace_back(language_get_string(STR_OPENRCT2_HEIGHTMAP_FILE), GetFilterPatternByType(_type, isSave));
break;
}
@@ -420,21 +416,20 @@ static bool Browse(bool isSave, char* path, size_t pathSize)
}
}
desc.initial_directory = _directory;
desc.type = isSave ? FileDialogType::Save : FileDialogType::Open;
desc.default_filename = isSave ? path : nullptr;
desc.InitialDirectory = _directory;
desc.Type = isSave ? OpenRCT2::Ui::FileDialogType::Save : OpenRCT2::Ui::FileDialogType::Open;
desc.DefaultFilename = isSave ? path : u8string();
// Add 'all files' filter. If the number of filters is increased, this code will need to be adjusted.
desc.filters[1].name = language_get_string(STR_ALL_FILES);
desc.filters[1].pattern = "*";
desc.Filters.emplace_back(language_get_string(STR_ALL_FILES), "*");
desc.title = language_get_string(title);
if (platform_open_common_file_dialog(path, &desc, pathSize))
desc.Title = language_get_string(title);
if (platform_open_common_file_dialog(path, desc, pathSize))
{
// 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 && get_file_extension_type(path) != fileType)
path_append_extension(path, extension, pathSize);
path_append_extension(path, extension.c_str(), pathSize);
return true;
}

View File

@@ -1516,23 +1516,11 @@ const utf8* context_get_path_legacy(int32_t pathId)
return result;
}
bool platform_open_common_file_dialog(utf8* outFilename, file_dialog_desc* desc, size_t outSize)
bool platform_open_common_file_dialog(utf8* outFilename, OpenRCT2::Ui::FileDialogDesc& desc, size_t outSize)
{
try
{
FileDialogDesc desc2;
desc2.Type = static_cast<FILE_DIALOG_TYPE>(desc->type);
desc2.Title = String::ToStd(desc->title);
desc2.InitialDirectory = String::ToStd(desc->initial_directory);
desc2.DefaultFilename = String::ToStd(desc->default_filename);
for (const auto& filter : desc->filters)
{
if (filter.name != nullptr)
{
desc2.Filters.push_back({ String::ToStd(filter.name), String::ToStd(filter.pattern) });
}
}
std::string result = GetContext()->GetUiContext()->ShowFileDialog(desc2);
std::string result = GetContext()->GetUiContext()->ShowFileDialog(desc);
String::Set(outFilename, outSize, result.c_str());
return !result.empty();
}

View File

@@ -716,20 +716,16 @@ namespace Config
static bool SelectGogInstaller(utf8* installerPath)
{
file_dialog_desc desc;
memset(&desc, 0, sizeof(desc));
desc.type = FileDialogType::Open;
desc.title = language_get_string(STR_SELECT_GOG_INSTALLER);
desc.filters[0].name = language_get_string(STR_GOG_INSTALLER);
desc.filters[0].pattern = "*.exe";
desc.filters[1].name = language_get_string(STR_ALL_FILES);
desc.filters[1].pattern = "*";
desc.filters[2].name = nullptr;
FileDialogDesc desc{};
desc.Type = FileDialogType::Open;
desc.Title = language_get_string(STR_SELECT_GOG_INSTALLER);
desc.Filters.emplace_back(language_get_string(STR_GOG_INSTALLER), "*.exe");
desc.Filters.emplace_back(language_get_string(STR_ALL_FILES), "*");
const auto userHomePath = Platform::GetFolderPath(SPECIAL_FOLDER::USER_HOME);
desc.initial_directory = userHomePath.c_str();
desc.InitialDirectory = userHomePath.c_str();
return platform_open_common_file_dialog(installerPath, &desc, 4096);
return platform_open_common_file_dialog(installerPath, desc, 4096);
}
static bool ExtractGogInstaller(u8string_view installerPath, u8string_view targetPath)

View File

@@ -20,6 +20,10 @@
#endif // __ANDROID__
struct TTFFontDescriptor;
namespace OpenRCT2::Ui
{
struct FileDialogDesc;
}
#ifndef MAX_PATH
# define MAX_PATH 260
@@ -62,25 +66,6 @@ struct rct2_time
uint8_t second;
};
enum class FileDialogType : uint8_t
{
Open,
Save
};
struct file_dialog_desc
{
FileDialogType type;
const utf8* title;
const utf8* initial_directory;
const utf8* default_filename;
struct
{
const utf8* name; // E.g. "Image Files"
const utf8* pattern; // E.g. "*.png;*.jpg;*.gif"
} filters[8];
};
// Platform shared definitions
void platform_toggle_windowed_mode();
void platform_refresh_video(bool recreate_window);
@@ -94,7 +79,7 @@ bool platform_lock_single_instance();
int32_t platform_get_drives();
uint32_t platform_get_ticks();
void platform_sleep(uint32_t ms);
bool platform_open_common_file_dialog(utf8* outFilename, file_dialog_desc* desc, size_t outSize);
bool platform_open_common_file_dialog(utf8* outFilename, OpenRCT2::Ui::FileDialogDesc& desc, size_t outSize);
std::string platform_get_rct1_steam_dir();
std::string platform_get_rct2_steam_dir();

View File

@@ -65,24 +65,30 @@ namespace OpenRCT2
return !(lhs == rhs);
}
enum class FILE_DIALOG_TYPE
enum class FileDialogType : uint8_t
{
OPEN,
SAVE,
Open,
Save
};
struct FileDialogDesc
{
struct Filter
{
std::string Name; // E.g. "Image Files"
std::string Pattern; // E.g. "*.png;*.jpg;*.gif"
u8string Name; // E.g. "Image Files"
u8string Pattern; // E.g. "*.png;*.jpg;*.gif"
Filter(u8string_view name, u8string_view pattern)
: Name(name)
, Pattern(pattern)
{
}
};
FILE_DIALOG_TYPE Type = FILE_DIALOG_TYPE::OPEN;
std::string Title;
std::string InitialDirectory;
std::string DefaultFilename;
FileDialogType Type = FileDialogType::Open;
u8string Title;
u8string InitialDirectory;
u8string DefaultFilename;
std::vector<Filter> Filters;
};