/*************************************************************************/ /* script_create_dialog.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* http://www.godotengine.org */ /*************************************************************************/ /* 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 */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "script_create_dialog.h" #include "editor_file_system.h" #include "globals.h" #include "io/resource_saver.h" #include "os/file_access.h" #include "script_language.h" void ScriptCreateDialog::config(const String &p_base_name, const String &p_base_path) { class_name->set_text(""); parent_name->set_text(p_base_name); if (p_base_path != "") { initial_bp = p_base_path.basename(); file_path->set_text(initial_bp + "." + ScriptServer::get_language(language_menu->get_selected())->get_extension()); } else { initial_bp = ""; file_path->set_text(""); } _class_name_changed(""); _path_changed(file_path->get_text()); } bool ScriptCreateDialog::_validate(const String &p_string) { if (p_string.length() == 0) return false; for (int i = 0; i < p_string.length(); i++) { if (i == 0) { if (p_string[0] >= '0' && p_string[0] <= '9') return false; // no start with number plz } bool valid_char = (p_string[i] >= '0' && p_string[i] <= '9') || (p_string[i] >= 'a' && p_string[i] <= 'z') || (p_string[i] >= 'A' && p_string[i] <= 'Z') || p_string[i] == '_'; if (!valid_char) return false; } return true; } void ScriptCreateDialog::_class_name_changed(const String &p_name) { if (!_validate(parent_name->get_text())) { error_label->set_text(TTR("Invalid parent class name")); error_label->add_color_override("font_color", Color(1, 0.4, 0.0, 0.8)); } else if (class_name->is_editable()) { if (class_name->get_text() == "") { error_label->set_text(TTR("Valid chars:") + " a-z A-Z 0-9 _"); error_label->add_color_override("font_color", Color(1, 1, 1, 0.6)); } else if (!_validate(class_name->get_text())) { error_label->set_text(TTR("Invalid class name")); error_label->add_color_override("font_color", Color(1, 0.2, 0.2, 0.8)); } else { error_label->set_text(TTR("Valid name")); error_label->add_color_override("font_color", Color(0, 1.0, 0.8, 0.8)); } } else { error_label->set_text(TTR("N/A")); error_label->add_color_override("font_color", Color(0, 1.0, 0.8, 0.8)); } } void ScriptCreateDialog::ok_pressed() { 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; } 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(); String text = ScriptServer::get_language(language_menu->get_selected())->get_template(cname, parent_name->get_text()); Script *script = ScriptServer::get_language(language_menu->get_selected())->create_script(); script->set_source_code(text); if (cname != "") script->set_name(cname); Ref