Allow creating projects in non-empty folders with a confirmation popup

This commit is contained in:
Aaron Franke
2020-10-03 01:49:19 -04:00
parent efc4d217d6
commit 46fdca4fae
2 changed files with 28 additions and 9 deletions

View File

@ -61,6 +61,7 @@ class ProjectDialog : public ConfirmationDialog {
GDCLASS(ProjectDialog, ConfirmationDialog); GDCLASS(ProjectDialog, ConfirmationDialog);
public: public:
bool is_folder_empty = true;
enum Mode { enum Mode {
MODE_NEW, MODE_NEW,
MODE_IMPORT, MODE_IMPORT,
@ -218,7 +219,7 @@ private:
// check if the specified install folder is empty, even though this is not an error, it is good to check here // check if the specified install folder is empty, even though this is not an error, it is good to check here
d->list_dir_begin(); d->list_dir_begin();
bool is_empty = true; is_folder_empty = true;
String n = d->get_next(); String n = d->get_next();
while (n != String()) { while (n != String()) {
if (!n.begins_with(".")) { if (!n.begins_with(".")) {
@ -226,14 +227,14 @@ private:
// and hidden files/folders to be present. // and hidden files/folders to be present.
// For instance, this lets users initialize a Git repository // For instance, this lets users initialize a Git repository
// and still be able to create a project in the directory afterwards. // and still be able to create a project in the directory afterwards.
is_empty = false; is_folder_empty = false;
break; break;
} }
n = d->get_next(); n = d->get_next();
} }
d->list_dir_end(); d->list_dir_end();
if (!is_empty) { if (!is_folder_empty) {
set_message(TTR("Please choose an empty folder."), MESSAGE_WARNING, INSTALL_PATH); set_message(TTR("Please choose an empty folder."), MESSAGE_WARNING, INSTALL_PATH);
memdelete(d); memdelete(d);
get_ok()->set_disabled(true); get_ok()->set_disabled(true);
@ -258,7 +259,7 @@ private:
} else { } else {
// check if the specified folder is empty, even though this is not an error, it is good to check here // check if the specified folder is empty, even though this is not an error, it is good to check here
d->list_dir_begin(); d->list_dir_begin();
bool is_empty = true; is_folder_empty = true;
String n = d->get_next(); String n = d->get_next();
while (n != String()) { while (n != String()) {
if (!n.begins_with(".")) { if (!n.begins_with(".")) {
@ -266,18 +267,18 @@ private:
// and hidden files/folders to be present. // and hidden files/folders to be present.
// For instance, this lets users initialize a Git repository // For instance, this lets users initialize a Git repository
// and still be able to create a project in the directory afterwards. // and still be able to create a project in the directory afterwards.
is_empty = false; is_folder_empty = false;
break; break;
} }
n = d->get_next(); n = d->get_next();
} }
d->list_dir_end(); d->list_dir_end();
if (!is_empty) { if (!is_folder_empty) {
set_message(TTR("Please choose an empty folder."), MESSAGE_ERROR); set_message(TTR("The selected path is not empty. Choosing an empty folder is highly recommended."), MESSAGE_WARNING);
memdelete(d); memdelete(d);
get_ok()->set_disabled(true); get_ok()->set_disabled(false);
return ""; return valid_path;
} }
} }
@ -416,6 +417,11 @@ private:
} }
} }
void _nonempty_confirmation_ok_pressed() {
is_folder_empty = true;
ok_pressed();
}
void ok_pressed() override { void ok_pressed() override {
String dir = project_path->get_text(); String dir = project_path->get_text();
@ -454,6 +460,18 @@ private:
} else { } else {
if (mode == MODE_NEW) { if (mode == MODE_NEW) {
// Before we create a project, check that the target folder is empty.
// If not, we need to ask the user if they're sure they want to do this.
if (!is_folder_empty) {
ConfirmationDialog *cd = memnew(ConfirmationDialog);
cd->set_title(TTR("Warning: This folder is not empty"));
cd->set_text(TTR("You are about to create a Godot project in a non-empty folder.\nThe entire contents of this folder will be imported as project resources!\n\nAre you sure you wish to continue?"));
cd->get_ok()->connect("pressed", callable_mp(this, &ProjectDialog::_nonempty_confirmation_ok_pressed));
get_parent()->add_child(cd);
cd->popup_centered();
cd->grab_focus();
return;
}
ProjectSettings::CustomMap initial_settings; ProjectSettings::CustomMap initial_settings;
if (rasterizer_button_group->get_pressed_button()->get_meta("driver_name") == "Vulkan") { if (rasterizer_button_group->get_pressed_button()->get_meta("driver_name") == "Vulkan") {
initial_settings["rendering/quality/driver/driver_name"] = "Vulkan"; initial_settings["rendering/quality/driver/driver_name"] = "Vulkan";

View File

@ -98,6 +98,7 @@ class ProjectManager : public Control {
void _restart_confirm(); void _restart_confirm();
void _exit_dialog(); void _exit_dialog();
void _confirm_update_settings(); void _confirm_update_settings();
void _nonempty_confirmation_ok_pressed();
void _load_recent_projects(); void _load_recent_projects();
void _on_project_created(const String &dir); void _on_project_created(const String &dir);