Fix some bugs in shader creation dialog (continue)

This commit is contained in:
Yuri Roubinsky 2021-08-09 10:41:01 +03:00
parent 6e11017401
commit a1dd51f5e6
2 changed files with 37 additions and 43 deletions

View file

@ -78,26 +78,18 @@ void ShaderCreateDialog::_update_theme() {
void ShaderCreateDialog::_update_language_info() { void ShaderCreateDialog::_update_language_info() {
language_data.clear(); language_data.clear();
List<StringName> classes; for (int i = 0; i < SHADER_TYPE_MAX; i++) {
classes.push_front(SNAME("Shader")); ShaderTypeData data;
classes.push_front(SNAME("VisualShader")); if (i == int(SHADER_TYPE_TEXT)) {
data.use_templates = true;
for (List<StringName>::Element *E = classes.front(); E; E = E->next()) { data.extensions.push_back("gdshader");
language_data.push_back(ShaderTypeData()); data.default_extension = "gdshader";
}
int idx = 0;
for (List<ShaderTypeData>::Element *E = language_data.front(); E; E = E->next()) {
if (idx == int(SHADER_TYPE_TEXT)) {
E->get().use_templates = true;
E->get().extensions.push_back("gdshader");
E->get().default_extension = "gdshader";
} else { } else {
E->get().default_extension = "tres"; data.default_extension = "tres";
} }
E->get().extensions.push_back("res"); data.extensions.push_back("res");
E->get().extensions.push_back("tres"); data.extensions.push_back("tres");
idx++; language_data.push_back(data);
} }
} }
@ -216,6 +208,7 @@ void ShaderCreateDialog::_load_exist() {
} }
void ShaderCreateDialog::_language_changed(int p_language) { void ShaderCreateDialog::_language_changed(int p_language) {
current_language = p_language;
ShaderTypeData data = language_data[p_language]; ShaderTypeData data = language_data[p_language];
String selected_ext = "." + data.default_extension; String selected_ext = "." + data.default_extension;
@ -304,20 +297,19 @@ void ShaderCreateDialog::_path_changed(const String &p_path) {
is_path_valid = false; is_path_valid = false;
is_new_shader_created = true; is_new_shader_created = true;
String path_error = _validate_path(p_path, false); String path_error = _validate_path(p_path);
if (path_error != "") { if (path_error != "") {
_msg_path_valid(false, path_error); _msg_path_valid(false, path_error);
_update_dialog(); _update_dialog();
return; return;
} }
DirAccess *f = DirAccess::create(DirAccess::ACCESS_RESOURCES); DirAccessRef f = DirAccess::create(DirAccess::ACCESS_RESOURCES);
String p = ProjectSettings::get_singleton()->localize_path(p_path.strip_edges()); String p = ProjectSettings::get_singleton()->localize_path(p_path.strip_edges());
if (f->file_exists(p)) { if (f->file_exists(p)) {
is_new_shader_created = false; is_new_shader_created = false;
_msg_path_valid(true, TTR("File exists, it will be reused.")); _msg_path_valid(true, TTR("File exists, it will be reused."));
} }
memdelete(f);
is_path_valid = true; is_path_valid = true;
_update_dialog(); _update_dialog();
@ -345,7 +337,7 @@ void ShaderCreateDialog::config(const String &p_base_path, bool p_built_in_enabl
_path_changed(file_path->get_text()); _path_changed(file_path->get_text());
} }
String ShaderCreateDialog::_validate_path(const String &p_path, bool p_file_must_exist) { String ShaderCreateDialog::_validate_path(const String &p_path) {
String p = p_path.strip_edges(); String p = p_path.strip_edges();
if (p == "") { if (p == "") {
@ -360,46 +352,48 @@ String ShaderCreateDialog::_validate_path(const String &p_path, bool p_file_must
return TTR("Path is not local."); return TTR("Path is not local.");
} }
DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES); DirAccessRef d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
if (d->change_dir(p.get_base_dir()) != OK) { if (d->change_dir(p.get_base_dir()) != OK) {
memdelete(d);
return TTR("Invalid base path."); return TTR("Invalid base path.");
} }
memdelete(d);
DirAccess *f = DirAccess::create(DirAccess::ACCESS_RESOURCES); DirAccessRef f = DirAccess::create(DirAccess::ACCESS_RESOURCES);
if (f->dir_exists(p)) { if (f->dir_exists(p)) {
memdelete(f);
return TTR("A directory with the same name exists."); return TTR("A directory with the same name exists.");
} else if (p_file_must_exist && !f->file_exists(p)) {
memdelete(f);
return TTR("File does not exist.");
} }
memdelete(f);
String extension = p.get_extension(); String extension = p.get_extension();
Set<String> extensions; Set<String> extensions;
for (int l = 0; l < SHADER_TYPE_MAX; l++) { for (int i = 0; i < SHADER_TYPE_MAX; i++) {
for (List<String>::Element *E = language_data[l].extensions.front(); E; E = E->next()) { for (const String &ext : language_data[i].extensions) {
if (!extensions.has(E->get())) { if (!extensions.has(ext)) {
extensions.insert(E->get()); extensions.insert(ext);
} }
} }
} }
ShaderTypeData data = language_data[language_menu->get_selected()]; ShaderTypeData data = language_data[language_menu->get_selected()];
bool found = false;
bool match = false; bool match = false;
int index = 0;
for (Set<String>::Element *E = extensions.front(); E; E = E->next()) { for (const String &ext : extensions) {
if (E->get().nocasecmp_to(extension) == 0) { if (ext.nocasecmp_to(extension) == 0) {
found = true;
for (const String &lang_ext : language_data[current_language].extensions) {
if (lang_ext.nocasecmp_to(extension) == 0) {
match = true; match = true;
break; break;
} }
index++; }
break;
}
} }
if (!found) {
return TTR("Invalid extension.");
}
if (!match) { if (!match) {
return TTR("Wrong extension chosen."); return TTR("Wrong extension chosen.");
} }
@ -575,8 +569,8 @@ ShaderCreateDialog::ShaderCreateDialog() {
// Modes. // Modes.
mode_menu = memnew(OptionButton); mode_menu = memnew(OptionButton);
for (const List<String>::Element *E = ShaderTypes::get_singleton()->get_types_list().front(); E; E = E->next()) { for (const String &type_name : ShaderTypes::get_singleton()->get_types_list()) {
mode_menu->add_item(E->get().capitalize()); mode_menu->add_item(type_name.capitalize());
} }
gc->add_child(memnew(Label(TTR("Mode:")))); gc->add_child(memnew(Label(TTR("Mode:"))));
gc->add_child(mode_menu); gc->add_child(mode_menu);

View file

@ -94,7 +94,7 @@ class ShaderCreateDialog : public ConfirmationDialog {
void _mode_changed(int p_mode = 0); void _mode_changed(int p_mode = 0);
void _browse_path(); void _browse_path();
void _file_selected(const String &p_file); void _file_selected(const String &p_file);
String _validate_path(const String &p_path, bool p_file_must_exist); String _validate_path(const String &p_path);
virtual void ok_pressed() override; virtual void ok_pressed() override;
void _create_new(); void _create_new();
void _load_exist(); void _load_exist();