diff options
Diffstat (limited to 'tools/editor/script_create_dialog.cpp')
-rw-r--r-- | tools/editor/script_create_dialog.cpp | 151 |
1 files changed, 100 insertions, 51 deletions
diff --git a/tools/editor/script_create_dialog.cpp b/tools/editor/script_create_dialog.cpp index 62d5c7cd84..da01cdefe5 100644 --- a/tools/editor/script_create_dialog.cpp +++ b/tools/editor/script_create_dialog.cpp @@ -5,7 +5,7 @@ /* GODOT ENGINE */ /* http://www.godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -27,6 +27,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "script_create_dialog.h" + #include "script_language.h" #include "globals.h" #include "io/resource_saver.h" @@ -38,7 +39,7 @@ void ScriptCreateDialog::config(const String& p_base_name,const String&p_base_pa class_name->set_text(""); parent_name->set_text(p_base_name); if (p_base_path!="") { - initial_bp=p_base_path.basename(); + initial_bp=p_base_path.get_basename(); file_path->set_text(initial_bp+"."+ScriptServer::get_language( language_menu->get_selected() )->get_extension()); } else { initial_bp=""; @@ -96,8 +97,20 @@ void ScriptCreateDialog::_class_name_changed(const String& p_name) { void ScriptCreateDialog::ok_pressed() { - if (class_name->is_editable() && !_validate(class_name->get_text())) { + if (create_new){ + _create_new(); + } else { + _load_exist(); + } + + create_new=true; + _update_controls(); + +} + +void ScriptCreateDialog::_create_new() { + if (class_name->is_editable() && !_validate(class_name->get_text())) { alert->set_text(TTR("Class name is invalid!")); alert->popup_centered_minsize(); return; @@ -105,21 +118,14 @@ void ScriptCreateDialog::ok_pressed() { if (!_validate(parent_name->get_text())) { alert->set_text(TTR("Parent class name is invalid!")); alert->popup_centered_minsize(); - return; - } - String cname; if (class_name->is_editable()) cname=class_name->get_text(); - - - Ref<Script> scr = ScriptServer::get_language( language_menu->get_selected() )->get_template(cname,parent_name->get_text()); - //scr->set_source_code(text); String selected_language = language_menu->get_item_text(language_menu->get_selected()); editor_settings->set_last_selected_language(selected_language); @@ -127,34 +133,40 @@ void ScriptCreateDialog::ok_pressed() { if (cname!="") scr->set_name(cname); - if (!internal->is_pressed()) { - - - String lpath = Globals::get_singleton()->localize_path(file_path->get_text()); + String lpath = GlobalConfig::get_singleton()->localize_path(file_path->get_text()); scr->set_path(lpath); if (!path_valid) { - alert->set_text(TTR("Invalid path!")); alert->popup_centered_minsize(); return; - } Error err = ResourceSaver::save(lpath,scr,ResourceSaver::FLAG_CHANGE_PATH); if (err!=OK) { - alert->set_text(TTR("Could not create script in filesystem.")); alert->popup_centered_minsize(); return; } - //scr->set_path(lpath); - //EditorFileSystem::get_singleton()->update_file(lpath,scr->get_type()); + } + hide(); + emit_signal("script_created",scr); + +} +void ScriptCreateDialog::_load_exist() { + + String path=file_path->get_text(); + RES p_script = ResourceLoader::load(path, "Script"); + if (p_script.is_null()) { + alert->get_ok()->set_text(TTR("Ugh")); + alert->set_text(vformat(TTR("Error loading script from %s"), path)); + alert->popup_centered_minsize(); + return; } hide(); - emit_signal("script_created",scr); + emit_signal("script_created",p_script.get_ref_ptr()); } @@ -166,10 +178,35 @@ void ScriptCreateDialog::_lang_changed(int l) { } else { class_name->set_editable(false); } - if (file_path->get_text().basename()==initial_bp) { - file_path->set_text(initial_bp+"."+ScriptServer::get_language( l )->get_extension()); - _path_changed(file_path->get_text()); + + String selected_ext="."+ScriptServer::get_language( l )->get_extension(); + String path=file_path->get_text(); + String extension=""; + if (path.find(".")>=0) { + extension=path.get_extension(); + } + + if (extension.length()==0) { + // add extension if none + path+=selected_ext; + _path_changed(path); + } else { + // change extension by selected language + List<String> extensions; + // get all possible extensions for script + for (int l=0;l<language_menu->get_item_count();l++) { + ScriptServer::get_language( l )->get_recognized_extensions(&extensions); + } + + for(List<String>::Element *E=extensions.front();E;E=E->next()) { + if (E->get().nocasecmp_to(extension)==0) { + path=path.get_basename()+selected_ext; + _path_changed(path); + break; + } + } } + file_path->set_text(path); _class_name_changed(class_name->get_text()); } @@ -191,8 +228,10 @@ void ScriptCreateDialog::_browse_path() { file_browse->clear_filters(); List<String> extensions; - int l=language_menu->get_selected(); - ScriptServer::get_language( l )->get_recognized_extensions(&extensions); + // get all possible extensions for script + for (int l=0;l<language_menu->get_item_count();l++) { + ScriptServer::get_language( l )->get_recognized_extensions(&extensions); + } for(List<String>::Element *E=extensions.front();E;E=E->next()) { file_browse->add_filter("*."+E->get()); @@ -205,7 +244,7 @@ void ScriptCreateDialog::_browse_path() { void ScriptCreateDialog::_file_selected(const String& p_file) { - String p = Globals::get_singleton()->localize_path(p_file); + String p = GlobalConfig::get_singleton()->localize_path(p_file); file_path->set_text(p); _path_changed(p); @@ -224,7 +263,7 @@ void ScriptCreateDialog::_path_changed(const String& p_path) { } - p = Globals::get_singleton()->localize_path(p); + p = GlobalConfig::get_singleton()->localize_path(p); if (!p.begins_with("res://")) { path_error_label->set_text(TTR("Path is not local")); @@ -246,58 +285,66 @@ void ScriptCreateDialog::_path_changed(const String& p_path) { memdelete(d); } - - FileAccess *f = FileAccess::create(FileAccess::ACCESS_RESOURCES); - - if (f->file_exists(p)) { - - path_error_label->set_text(TTR("File exists")); - path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8)); - memdelete(f); - return; - } - + create_new=!f->file_exists(p); memdelete(f); - String extension=p.extension(); + String extension=p.get_extension(); List<String> extensions; - int l=language_menu->get_selected(); - ScriptServer::get_language( l )->get_recognized_extensions(&extensions); + // get all possible extensions for script + for (int l=0;l<language_menu->get_item_count();l++) { + ScriptServer::get_language( l )->get_recognized_extensions(&extensions); + } bool found=false; + int index=0; for(List<String>::Element *E=extensions.front();E;E=E->next()) { if (E->get().nocasecmp_to(extension)==0) { + language_menu->select(index); // change Language option by extension found=true; break; } + index++; } if (!found) { - path_error_label->set_text(TTR("Invalid extension")); path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8)); return; } + _update_controls(); - path_error_label->set_text(TTR("Valid path")); path_error_label->add_color_override("font_color",Color(0,1.0,0.8,0.8)); path_valid=true; } +void ScriptCreateDialog::_update_controls() { + + if (create_new) { + path_error_label->set_text(TTR("Create new script")); + get_ok()->set_text(TTR("Create")); + } else { + path_error_label->set_text(TTR("Load existing script")); + get_ok()->set_text(TTR("Load")); + } + parent_name->set_editable(create_new); + internal->set_disabled(!create_new); + +} + void ScriptCreateDialog::_bind_methods() { - ObjectTypeDB::bind_method("_class_name_changed",&ScriptCreateDialog::_class_name_changed); - ObjectTypeDB::bind_method("_lang_changed",&ScriptCreateDialog::_lang_changed); - ObjectTypeDB::bind_method("_built_in_pressed",&ScriptCreateDialog::_built_in_pressed); - ObjectTypeDB::bind_method("_browse_path",&ScriptCreateDialog::_browse_path); - ObjectTypeDB::bind_method("_file_selected",&ScriptCreateDialog::_file_selected); - ObjectTypeDB::bind_method("_path_changed",&ScriptCreateDialog::_path_changed); + ClassDB::bind_method("_class_name_changed",&ScriptCreateDialog::_class_name_changed); + ClassDB::bind_method("_lang_changed",&ScriptCreateDialog::_lang_changed); + ClassDB::bind_method("_built_in_pressed",&ScriptCreateDialog::_built_in_pressed); + ClassDB::bind_method("_browse_path",&ScriptCreateDialog::_browse_path); + ClassDB::bind_method("_file_selected",&ScriptCreateDialog::_file_selected); + ClassDB::bind_method("_path_changed",&ScriptCreateDialog::_path_changed); ADD_SIGNAL(MethodInfo("script_created",PropertyInfo(Variant::OBJECT,"script",PROPERTY_HINT_RESOURCE_TYPE,"Script"))); } @@ -307,7 +354,7 @@ ScriptCreateDialog::ScriptCreateDialog() { VBoxContainer *vb = memnew( VBoxContainer ); add_child(vb); - set_child_rect(vb); + //set_child_rect(vb); class_name = memnew( LineEdit ); @@ -376,7 +423,7 @@ ScriptCreateDialog::ScriptCreateDialog() { set_size(Size2(200,150)); set_hide_on_ok(false); - set_title(TTR("Create Node Script")); + set_title(TTR("Attach Node Script")); file_browse = memnew( EditorFileDialog ); file_browse->connect("file_selected",this,"_file_selected"); @@ -385,4 +432,6 @@ ScriptCreateDialog::ScriptCreateDialog() { alert = memnew( AcceptDialog ); add_child(alert); _lang_changed(0); + + create_new=true; } |