/*************************************************************************/ /* script_create_dialog.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2019 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 "core/io/resource_saver.h" #include "core/os/file_access.h" #include "core/project_settings.h" #include "core/script_language.h" #include "editor/create_dialog.h" #include "editor/editor_node.h" #include "editor/editor_scale.h" #include "editor_file_system.h" void ScriptCreateDialog::_notification(int p_what) { switch (p_what) { case NOTIFICATION_THEME_CHANGED: case NOTIFICATION_ENTER_TREE: { path_button->set_icon(get_icon("Folder", "EditorIcons")); parent_browse_button->set_icon(get_icon("Folder", "EditorIcons")); parent_search_button->set_icon(get_icon("ClassList", "EditorIcons")); status_panel->add_style_override("panel", get_stylebox("bg", "Tree")); } break; } } void ScriptCreateDialog::_path_hbox_sorted() { if (is_visible()) { int filename_start_pos = initial_bp.find_last("/") + 1; int filename_end_pos = initial_bp.length(); file_path->select(filename_start_pos, filename_end_pos); // First set cursor to the end of line to scroll LineEdit view // to the right and then set the actual cursor position. file_path->set_cursor_position(file_path->get_text().length()); file_path->set_cursor_position(filename_start_pos); file_path->grab_focus(); } } bool ScriptCreateDialog::_can_be_built_in() { return (supports_built_in && built_in_enabled); } void ScriptCreateDialog::config(const String &p_base_name, const String &p_base_path, bool p_built_in_enabled) { class_name->set_text(""); class_name->deselect(); parent_name->set_text(p_base_name); parent_name->deselect(); 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(""); } file_path->deselect(); built_in_enabled = p_built_in_enabled; _lang_changed(current_language); _class_name_changed(""); _path_changed(file_path->get_text()); } void ScriptCreateDialog::set_inheritance_base_type(const String &p_base) { base_type = p_base; } 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] == '_' || 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_script_templates_dir().plus_file(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_param; if (has_named_classes) { cname_param = class_name->get_text(); } else { cname_param = ProjectSettings::get_singleton()->localize_path(file_path->get_text()).get_file().get_basename(); } Ref