Refactor window_loadsave_open

This commit is contained in:
Sijmen Schoon 2016-07-11 23:48:24 +02:00
parent 6fc36cbabb
commit 0100ae7cd3
1 changed files with 45 additions and 81 deletions

View File

@ -132,7 +132,6 @@ char _shortenedDirectory[MAX_PATH];
static char _parentDirectory[MAX_PATH]; static char _parentDirectory[MAX_PATH];
char _extension[32]; char _extension[32];
char _defaultName[MAX_PATH]; char _defaultName[MAX_PATH];
int _loadsaveType;
int _type; int _type;
static void window_loadsave_populate_list(rct_window *w, int includeNewItem, const char *directory, const char *extension); static void window_loadsave_populate_list(rct_window *w, int includeNewItem, const char *directory, const char *extension);
@ -143,13 +142,24 @@ static int has_extension(char *path, char *extension);
static rct_window *window_overwrite_prompt_open(const char *name, const char *path); static rct_window *window_overwrite_prompt_open(const char *name, const char *path);
static int window_loadsave_get_dir(utf8 *last_save, char *path, const char *subdir)
{
if (last_save && platform_ensure_directory_exists(last_save))
safe_strcpy(path, last_save, MAX_PATH);
else
platform_get_user_directory(path, subdir);
if (!platform_ensure_directory_exists(path)) {
log_error("Unable to create save directory.");
return 0;
}
return 1;
}
rct_window *window_loadsave_open(int type, char *defaultName) rct_window *window_loadsave_open(int type, char *defaultName)
{ {
gLoadSaveCallback = NULL; gLoadSaveCallback = NULL;
gLoadSaveTitleSequenceSave = false; gLoadSaveTitleSequenceSave = false;
char path[MAX_PATH];
int includeNewItem;
rct_window* w;
_type = type; _type = type;
_defaultName[0] = '\0'; _defaultName[0] = '\0';
@ -157,7 +167,7 @@ rct_window *window_loadsave_open(int type, char *defaultName)
safe_strcpy(_defaultName, defaultName, sizeof(_defaultName)); safe_strcpy(_defaultName, defaultName, sizeof(_defaultName));
} }
w = window_bring_to_front_by_class(WC_LOADSAVE); rct_window *w = window_bring_to_front_by_class(WC_LOADSAVE);
if (w == NULL) { if (w == NULL) {
w = window_create_centred(WW, WH, &window_loadsave_events, WC_LOADSAVE, WF_STICK_TO_FRONT); w = window_create_centred(WW, WH, &window_loadsave_events, WC_LOADSAVE, WF_STICK_TO_FRONT);
w->widgets = window_loadsave_widgets; w->widgets = window_loadsave_widgets;
@ -166,97 +176,51 @@ rct_window *window_loadsave_open(int type, char *defaultName)
w->colours[1] = 7; w->colours[1] = 7;
w->colours[2] = 7; w->colours[2] = 7;
} }
_loadsaveType = type;
switch (type & 0x0F) {
case (LOADSAVETYPE_LOAD | LOADSAVETYPE_GAME):
w->widgets[WIDX_TITLE].text = STR_FILE_DIALOG_TITLE_LOAD_GAME;
break;
case (LOADSAVETYPE_SAVE | LOADSAVETYPE_GAME) :
w->widgets[WIDX_TITLE].text = STR_FILE_DIALOG_TITLE_SAVE_GAME;
break;
case (LOADSAVETYPE_LOAD | LOADSAVETYPE_LANDSCAPE) :
w->widgets[WIDX_TITLE].text = STR_FILE_DIALOG_TITLE_LOAD_LANDSCAPE;
break;
case (LOADSAVETYPE_SAVE | LOADSAVETYPE_LANDSCAPE) :
w->widgets[WIDX_TITLE].text = STR_FILE_DIALOG_TITLE_SAVE_LANDSCAPE;
break;
case (LOADSAVETYPE_SAVE | LOADSAVETYPE_SCENARIO) :
w->widgets[WIDX_TITLE].text = STR_FILE_DIALOG_TITLE_SAVE_SCENARIO;
break;
case (LOADSAVETYPE_LOAD | LOADSAVETYPE_TRACK) :
w->widgets[WIDX_TITLE].text = STR_FILE_DIALOG_TITLE_INSTALL_NEW_TRACK_DESIGN;
break;
case (LOADSAVETYPE_SAVE | LOADSAVETYPE_TRACK) :
w->widgets[WIDX_TITLE].image = STR_FILE_DIALOG_TITLE_SAVE_TRACK;
break;
default:
log_error("Unsupported load / save type: %d", type & 0x0F);
return NULL;
}
w->no_list_items = 0; w->no_list_items = 0;
w->selected_list_item = -1; w->selected_list_item = -1;
includeNewItem = (type & 0x01) == LOADSAVETYPE_SAVE; bool isSave = (type & 0x01) == LOADSAVETYPE_SAVE;
bool success = false;
char path[MAX_PATH];
switch (type & 0x0E) { switch (type & 0x0E) {
case LOADSAVETYPE_GAME: case LOADSAVETYPE_GAME:
if (gConfigGeneral.last_save_game_directory && platform_ensure_directory_exists(gConfigGeneral.last_save_game_directory)) w->widgets[WIDX_TITLE].image = isSave ? STR_FILE_DIALOG_TITLE_SAVE_GAME : STR_FILE_DIALOG_TITLE_LOAD_GAME;
safe_strcpy(path, gConfigGeneral.last_save_game_directory, MAX_PATH); if (window_loadsave_get_dir(gConfigGeneral.last_save_game_directory, path, "save")) {
else window_loadsave_populate_list(w, isSave, path, ".sv6");
platform_get_user_directory(path, "save"); success = true;
if (!platform_ensure_directory_exists(path)) {
log_error("Unable to create save directory.");
window_close(w);
return NULL;
} }
window_loadsave_populate_list(w, includeNewItem, path, ".sv6");
break; break;
case LOADSAVETYPE_LANDSCAPE: case LOADSAVETYPE_LANDSCAPE:
if (gConfigGeneral.last_save_landscape_directory && platform_ensure_directory_exists(gConfigGeneral.last_save_landscape_directory)) w->widgets[WIDX_TITLE].image = isSave ? STR_FILE_DIALOG_TITLE_SAVE_LANDSCAPE : STR_FILE_DIALOG_TITLE_LOAD_LANDSCAPE;
safe_strcpy(path, gConfigGeneral.last_save_landscape_directory, MAX_PATH); if (window_loadsave_get_dir(gConfigGeneral.last_save_game_directory, path, "landscape")) {
else window_loadsave_populate_list(w, isSave, path, ".sc6");
platform_get_user_directory(path, "landscape"); success = true;
if (!platform_ensure_directory_exists(path)) {
log_error("Unable to create landscapes directory.");
window_close(w);
return NULL;
} }
window_loadsave_populate_list(w, includeNewItem, path, ".sc6");
break; break;
case LOADSAVETYPE_SCENARIO: case LOADSAVETYPE_SCENARIO:
if (gConfigGeneral.last_save_scenario_directory && platform_ensure_directory_exists(gConfigGeneral.last_save_scenario_directory)) w->widgets[WIDX_TITLE].image = STR_FILE_DIALOG_TITLE_SAVE_SCENARIO;
safe_strcpy(path, gConfigGeneral.last_save_scenario_directory, MAX_PATH); if (window_loadsave_get_dir(gConfigGeneral.last_save_game_directory, path, "scenario")) {
else window_loadsave_populate_list(w, isSave, path, ".sc6");
platform_get_user_directory(path, "scenario"); success = true;
if (!platform_ensure_directory_exists(path)) {
log_error("Unable to create scenarios directory.");
window_close(w);
return NULL;
} }
window_loadsave_populate_list(w, includeNewItem, path, ".sc6");
break; break;
case LOADSAVETYPE_TRACK: case LOADSAVETYPE_TRACK:
if (gConfigGeneral.last_save_track_directory && platform_ensure_directory_exists(gConfigGeneral.last_save_track_directory)) w->widgets[WIDX_TITLE].image = isSave ? STR_FILE_DIALOG_TITLE_SAVE_TRACK : STR_FILE_DIALOG_TITLE_INSTALL_NEW_TRACK_DESIGN;
safe_strcpy(path, gConfigGeneral.last_save_track_directory, MAX_PATH); if (window_loadsave_get_dir(gConfigGeneral.last_save_game_directory, path, "track")) {
else window_loadsave_populate_list(w, isSave, path, ".td?");
platform_get_user_directory(path, "track"); success = true;
if (!platform_ensure_directory_exists(path)) {
log_error("Unable to create tracks directory.");
window_close(w);
return NULL;
} }
window_loadsave_populate_list(w, includeNewItem, path, ".td?");
break; break;
default:
log_error("Unsupported load/save type: %d", type & 0x0F);
} }
if (!success) {
window_close(w);
return NULL;
}
w->no_list_items = _listItemsCount; w->no_list_items = _listItemsCount;
window_init_scroll_widgets(w); window_init_scroll_widgets(w);
return w; return w;
@ -463,7 +427,7 @@ static void window_loadsave_scrollmousedown(rct_window *w, int scrollIndex, int
} else { } else {
// TYPE_FILE // TYPE_FILE
// Load or overwrite // Load or overwrite
if ((_loadsaveType & 0x01) == LOADSAVETYPE_SAVE) if ((_type & 0x01) == LOADSAVETYPE_SAVE)
window_overwrite_prompt_open(_listItems[selectedItem].name, _listItems[selectedItem].path); window_overwrite_prompt_open(_listItems[selectedItem].name, _listItems[selectedItem].path);
else else
window_loadsave_select(w, _listItems[selectedItem].path); window_loadsave_select(w, _listItems[selectedItem].path);
@ -764,7 +728,7 @@ static void save_path(utf8 **config_str, const char *path)
static void window_loadsave_select(rct_window *w, const char *path) static void window_loadsave_select(rct_window *w, const char *path)
{ {
SDL_RWops* rw; SDL_RWops* rw;
switch (_loadsaveType & 0x0F) { switch (_type & 0x0F) {
case (LOADSAVETYPE_LOAD | LOADSAVETYPE_GAME) : case (LOADSAVETYPE_LOAD | LOADSAVETYPE_GAME) :
save_path(&gConfigGeneral.last_save_game_directory, path); save_path(&gConfigGeneral.last_save_game_directory, path);
if (gLoadSaveTitleSequenceSave) { if (gLoadSaveTitleSequenceSave) {