/*************************************************************************/ /* script_create_dialog.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* http://www.godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* 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/editor_scale.h" #include "editor_file_system.h" #include "io/resource_saver.h" #include "os/file_access.h" #include "project_settings.h" #include "script_language.h" void ScriptCreateDialog::_notification(int p_what) { switch (p_what) { case NOTIFICATION_ENTER_TREE: { path_button->set_icon(get_icon("Folder", "EditorIcons")); parent_browse_button->set_icon(get_icon("Folder", "EditorIcons")); } } } 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.get_basename(); file_path->set_text(initial_bp + "." + ScriptServer::get_language(language_menu->get_selected())->get_extension()); } else { initial_bp = ""; file_path->set_text(""); } _lang_changed(current_language); _parent_name_changed(parent_name->get_text()); _class_name_changed(""); _path_changed(file_path->get_text()); } bool ScriptCreateDialog::_validate(const String &p_string) { if (p_string.length() == 0) return false; String path_chars = "\"res://"; bool is_val_path = ScriptServer::get_language(language_menu->get_selected())->can_inherit_from_file(); 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 } if (i == p_string.length() - 1 && is_val_path) return p_string[i] == '\"'; if (is_val_path && i < path_chars.length()) { if (p_string[i] != path_chars[i]) is_val_path = false; else continue; } 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] == '_' || (is_val_path && (p_string[i] == '/' || p_string[i] == '.')); if (!valid_char) return false; } return true; } void ScriptCreateDialog::_class_name_changed(const String &p_name) { if (_validate(class_name->get_text())) { is_class_name_valid = true; } else { is_class_name_valid = false; } _update_dialog(); } void ScriptCreateDialog::_parent_name_changed(const String &p_parent) { if (_validate(parent_name->get_text())) { is_parent_name_valid = true; } else { is_parent_name_valid = false; } _update_dialog(); } void ScriptCreateDialog::_template_changed(int p_template) { String selected_template = p_template == 0 ? "" : template_menu->get_item_text(template_menu->get_selected()); EditorSettings::get_singleton()->set_project_metadata("script_setup", "last_selected_template", selected_template); if (p_template == 0) { //default script_template = ""; return; } String ext = ScriptServer::get_language(language_menu->get_selected())->get_extension(); String name = template_list[p_template - 1] + "." + ext; script_template = EditorSettings::get_singleton()->get_settings_path() + "/script_templates/" + name; } void ScriptCreateDialog::ok_pressed() { if (is_new_script_created) { _create_new(); } else { _load_exist(); } is_new_script_created = true; _update_dialog(); } void ScriptCreateDialog::_create_new() { String cname; if (has_named_classes) cname = class_name->get_text(); Ref