2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* script_create_dialog.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 20:16:55 +08:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
2017-01-02 05:01:57 +08:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 06:11:42 +08:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 09:10:30 +08:00
|
|
|
/* */
|
|
|
|
/* 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"
|
2017-01-16 15:04:19 +08:00
|
|
|
|
2017-08-01 02:19:45 +08:00
|
|
|
#include "editor/editor_node.h"
|
2017-05-04 02:41:02 +08:00
|
|
|
#include "editor/editor_scale.h"
|
2017-03-05 23:44:50 +08:00
|
|
|
#include "editor_file_system.h"
|
2014-02-10 09:10:30 +08:00
|
|
|
#include "io/resource_saver.h"
|
|
|
|
#include "os/file_access.h"
|
2017-08-01 02:19:45 +08:00
|
|
|
#include "project_settings.h"
|
2017-03-05 23:44:50 +08:00
|
|
|
#include "script_language.h"
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
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"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void ScriptCreateDialog::config(const String &p_base_name, const String &p_base_path) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
class_name->set_text("");
|
|
|
|
parent_name->set_text(p_base_name);
|
2017-03-05 23:44:50 +08:00
|
|
|
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());
|
2014-02-10 09:10:30 +08:00
|
|
|
} else {
|
2017-03-05 23:44:50 +08:00
|
|
|
initial_bp = "";
|
2014-02-10 09:10:30 +08:00
|
|
|
file_path->set_text("");
|
|
|
|
}
|
2017-05-04 02:41:02 +08:00
|
|
|
_lang_changed(current_language);
|
|
|
|
_parent_name_changed(parent_name->get_text());
|
2014-02-10 09:10:30 +08:00
|
|
|
_class_name_changed("");
|
|
|
|
_path_changed(file_path->get_text());
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
bool ScriptCreateDialog::_validate(const String &p_string) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_string.length() == 0)
|
2014-02-10 09:10:30 +08:00
|
|
|
return false;
|
|
|
|
|
2017-04-15 23:35:06 +08:00
|
|
|
String path_chars = "\"res://";
|
|
|
|
bool is_val_path = ScriptServer::get_language(language_menu->get_selected())->can_inherit_from_file();
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < p_string.length(); i++) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (i == 0) {
|
|
|
|
if (p_string[0] >= '0' && p_string[0] <= '9')
|
2014-02-10 09:10:30 +08:00
|
|
|
return false; // no start with number plz
|
|
|
|
}
|
|
|
|
|
2017-04-15 23:35:06 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-21 23:28:53 +08:00
|
|
|
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] == '.'));
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
if (!valid_char)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void ScriptCreateDialog::_class_name_changed(const String &p_name) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
if (_validate(class_name->get_text())) {
|
|
|
|
is_class_name_valid = true;
|
2014-02-10 09:10:30 +08:00
|
|
|
} else {
|
2017-05-04 02:41:02 +08:00
|
|
|
is_class_name_valid = false;
|
|
|
|
}
|
|
|
|
_update_dialog();
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
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;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2017-05-04 02:41:02 +08:00
|
|
|
_update_dialog();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-06-14 04:03:08 +08:00
|
|
|
void ScriptCreateDialog::_template_changed(int p_template) {
|
|
|
|
|
2017-07-07 16:22:23 +08:00
|
|
|
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);
|
2017-06-14 04:03:08 +08:00
|
|
|
if (p_template == 0) {
|
|
|
|
//default
|
|
|
|
script_template = "";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
String ext = ScriptServer::get_language(language_menu->get_selected())->get_extension();
|
2017-07-07 16:22:23 +08:00
|
|
|
String name = template_list[p_template - 1] + "." + ext;
|
2017-06-14 04:03:08 +08:00
|
|
|
script_template = EditorSettings::get_singleton()->get_settings_path() + "/script_templates/" + name;
|
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
void ScriptCreateDialog::ok_pressed() {
|
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
if (is_new_script_created) {
|
2016-11-10 00:29:15 +08:00
|
|
|
_create_new();
|
|
|
|
} else {
|
|
|
|
_load_exist();
|
|
|
|
}
|
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
is_new_script_created = true;
|
|
|
|
_update_dialog();
|
2016-11-10 00:29:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptCreateDialog::_create_new() {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
String cname;
|
2017-05-04 02:41:02 +08:00
|
|
|
if (has_named_classes)
|
2017-03-05 23:44:50 +08:00
|
|
|
cname = class_name->get_text();
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-06-14 04:03:08 +08:00
|
|
|
Ref<Script> scr;
|
|
|
|
if (script_template != "") {
|
2017-07-07 16:22:23 +08:00
|
|
|
scr = ResourceLoader::load(script_template);
|
|
|
|
if (scr.is_null()) {
|
|
|
|
alert->get_ok()->set_text(TTR("OK"));
|
|
|
|
alert->set_text(vformat(TTR("Error loading template '%s'"), script_template));
|
|
|
|
alert->popup_centered();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
scr = scr->duplicate();
|
2017-06-14 04:03:08 +08:00
|
|
|
ScriptServer::get_language(language_menu->get_selected())->make_template(cname, parent_name->get_text(), scr);
|
|
|
|
} else {
|
|
|
|
scr = ScriptServer::get_language(language_menu->get_selected())->get_template(cname, parent_name->get_text());
|
|
|
|
}
|
2016-08-06 09:46:45 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (cname != "")
|
2016-08-06 09:46:45 +08:00
|
|
|
scr->set_name(cname);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
if (!is_built_in) {
|
2017-07-20 04:00:46 +08:00
|
|
|
String lpath = ProjectSettings::get_singleton()->localize_path(file_path->get_text());
|
2016-08-06 09:46:45 +08:00
|
|
|
scr->set_path(lpath);
|
2017-03-05 23:44:50 +08:00
|
|
|
Error err = ResourceSaver::save(lpath, scr, ResourceSaver::FLAG_CHANGE_PATH);
|
|
|
|
if (err != OK) {
|
2017-05-04 02:41:02 +08:00
|
|
|
alert->set_text(TTR("Error - Could not create script in filesystem."));
|
|
|
|
alert->popup_centered();
|
2014-02-10 09:10:30 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-11-10 00:29:15 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2016-11-10 00:29:15 +08:00
|
|
|
hide();
|
2017-03-05 23:44:50 +08:00
|
|
|
emit_signal("script_created", scr);
|
2016-11-10 00:29:15 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2016-11-10 00:29:15 +08:00
|
|
|
void ScriptCreateDialog::_load_exist() {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
String path = file_path->get_text();
|
2016-11-10 00:29:15 +08:00
|
|
|
RES p_script = ResourceLoader::load(path, "Script");
|
|
|
|
if (p_script.is_null()) {
|
2017-05-04 02:41:02 +08:00
|
|
|
alert->get_ok()->set_text(TTR("OK"));
|
2016-11-10 00:29:15 +08:00
|
|
|
alert->set_text(vformat(TTR("Error loading script from %s"), path));
|
2017-05-04 02:41:02 +08:00
|
|
|
alert->popup_centered();
|
2016-11-10 00:29:15 +08:00
|
|
|
return;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
hide();
|
2017-03-05 23:44:50 +08:00
|
|
|
emit_signal("script_created", p_script.get_ref_ptr());
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptCreateDialog::_lang_changed(int l) {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
l = language_menu->get_selected();
|
2017-06-14 04:03:08 +08:00
|
|
|
ScriptLanguage *language = ScriptServer::get_language(l);
|
|
|
|
if (language->has_named_classes()) {
|
2017-05-04 02:41:02 +08:00
|
|
|
has_named_classes = true;
|
2014-02-10 09:10:30 +08:00
|
|
|
} else {
|
2017-05-04 02:41:02 +08:00
|
|
|
has_named_classes = false;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2016-11-10 00:29:15 +08:00
|
|
|
|
2017-04-15 23:35:06 +08:00
|
|
|
if (ScriptServer::get_language(l)->can_inherit_from_file()) {
|
2017-05-04 02:41:02 +08:00
|
|
|
can_inherit_from_file = true;
|
2017-04-15 23:35:06 +08:00
|
|
|
} else {
|
2017-05-04 02:41:02 +08:00
|
|
|
can_inherit_from_file = false;
|
2017-04-15 23:35:06 +08:00
|
|
|
}
|
|
|
|
|
2017-06-14 04:03:08 +08:00
|
|
|
String selected_ext = "." + language->get_extension();
|
2017-03-05 23:44:50 +08:00
|
|
|
String path = file_path->get_text();
|
|
|
|
String extension = "";
|
2017-05-04 02:41:02 +08:00
|
|
|
if (path != "") {
|
|
|
|
if (path.find(".") >= 0) {
|
|
|
|
extension = path.get_extension();
|
2016-11-10 00:29:15 +08:00
|
|
|
}
|
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
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++) {
|
2017-08-01 02:19:45 +08:00
|
|
|
ScriptServer::get_language(l)->get_recognized_extensions(&extensions);
|
2017-05-04 02:41:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2016-11-10 00:29:15 +08:00
|
|
|
}
|
|
|
|
}
|
2017-08-01 02:19:45 +08:00
|
|
|
} else {
|
|
|
|
path = "class" + selected_ext;
|
|
|
|
_path_changed(path);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2017-08-01 02:19:45 +08:00
|
|
|
file_path->set_text(path);
|
2017-05-04 02:41:02 +08:00
|
|
|
|
2017-06-14 04:03:08 +08:00
|
|
|
bool use_templates = language->is_using_templates();
|
|
|
|
template_menu->set_disabled(!use_templates);
|
2017-07-08 19:33:25 +08:00
|
|
|
template_menu->clear();
|
2017-06-14 04:03:08 +08:00
|
|
|
if (use_templates) {
|
2017-07-08 19:33:25 +08:00
|
|
|
|
2017-07-07 16:22:23 +08:00
|
|
|
template_list = EditorSettings::get_singleton()->get_script_templates(language->get_extension());
|
|
|
|
|
|
|
|
String last_lang = EditorSettings::get_singleton()->get_project_metadata("script_setup", "last_selected_language", "");
|
|
|
|
String last_template = EditorSettings::get_singleton()->get_project_metadata("script_setup", "last_selected_template", "");
|
2017-06-14 04:03:08 +08:00
|
|
|
|
|
|
|
template_menu->add_item(TTR("Default"));
|
|
|
|
for (int i = 0; i < template_list.size(); i++) {
|
2017-07-07 16:22:23 +08:00
|
|
|
String s = template_list[i].capitalize();
|
|
|
|
template_menu->add_item(s);
|
2017-07-08 19:33:25 +08:00
|
|
|
if (language_menu->get_item_text(language_menu->get_selected()) == last_lang && last_template == s) {
|
2017-07-07 16:22:23 +08:00
|
|
|
template_menu->select(i + 1);
|
|
|
|
}
|
2017-06-14 04:03:08 +08:00
|
|
|
}
|
2017-07-08 19:33:25 +08:00
|
|
|
} else {
|
|
|
|
|
|
|
|
template_menu->add_item(TTR("N/A"));
|
|
|
|
script_template = "";
|
2017-06-14 04:03:08 +08:00
|
|
|
}
|
|
|
|
|
2017-07-08 19:33:25 +08:00
|
|
|
_template_changed(template_menu->get_selected());
|
|
|
|
EditorSettings::get_singleton()->set_project_metadata("script_setup", "last_selected_language", language_menu->get_item_text(language_menu->get_selected()));
|
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
_update_dialog();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptCreateDialog::_built_in_pressed() {
|
|
|
|
|
|
|
|
if (internal->is_pressed()) {
|
2017-05-04 02:41:02 +08:00
|
|
|
is_built_in = true;
|
2014-02-10 09:10:30 +08:00
|
|
|
} else {
|
2017-05-04 02:41:02 +08:00
|
|
|
is_built_in = false;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2017-05-04 02:41:02 +08:00
|
|
|
_update_dialog();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-04-15 23:35:06 +08:00
|
|
|
void ScriptCreateDialog::_browse_path(bool browse_parent) {
|
|
|
|
|
|
|
|
is_browsing_parent = browse_parent;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2016-06-19 06:30:21 +08:00
|
|
|
file_browse->set_mode(EditorFileDialog::MODE_SAVE_FILE);
|
2016-06-20 11:38:13 +08:00
|
|
|
file_browse->set_disable_overwrite_warning(true);
|
2014-02-10 09:10:30 +08:00
|
|
|
file_browse->clear_filters();
|
|
|
|
List<String> extensions;
|
|
|
|
|
2017-07-01 21:18:55 +08:00
|
|
|
int lang = language_menu->get_selected();
|
|
|
|
ScriptServer::get_language(lang)->get_recognized_extensions(&extensions);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
|
|
|
|
file_browse->add_filter("*." + E->get());
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2016-03-09 07:00:52 +08:00
|
|
|
file_browse->set_current_path(file_path->get_text());
|
2014-02-10 09:10:30 +08:00
|
|
|
file_browse->popup_centered_ratio();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void ScriptCreateDialog::_file_selected(const String &p_file) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-07-20 04:00:46 +08:00
|
|
|
String p = ProjectSettings::get_singleton()->localize_path(p_file);
|
2017-04-15 23:35:06 +08:00
|
|
|
if (is_browsing_parent) {
|
|
|
|
parent_name->set_text("\"" + p + "\"");
|
|
|
|
_class_name_changed("\"" + p + "\"");
|
|
|
|
} else {
|
|
|
|
file_path->set_text(p);
|
|
|
|
_path_changed(p);
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void ScriptCreateDialog::_path_changed(const String &p_path) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
is_path_valid = false;
|
|
|
|
is_new_script_created = true;
|
2017-03-05 23:44:50 +08:00
|
|
|
String p = p_path;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p == "") {
|
2017-05-04 02:41:02 +08:00
|
|
|
_msg_path_valid(false, TTR("Path is empty"));
|
|
|
|
_update_dialog();
|
2014-02-10 09:10:30 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-20 04:00:46 +08:00
|
|
|
p = ProjectSettings::get_singleton()->localize_path(p);
|
2014-02-10 09:10:30 +08:00
|
|
|
if (!p.begins_with("res://")) {
|
2017-05-04 02:41:02 +08:00
|
|
|
_msg_path_valid(false, TTR("Path is not local"));
|
|
|
|
_update_dialog();
|
2014-02-10 09:10:30 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p.find("/") || p.find("\\")) {
|
|
|
|
DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
|
2017-03-05 23:44:50 +08:00
|
|
|
if (d->change_dir(p.get_base_dir()) != OK) {
|
2017-05-04 02:41:02 +08:00
|
|
|
_msg_path_valid(false, TTR("Invalid base path"));
|
2014-02-10 09:10:30 +08:00
|
|
|
memdelete(d);
|
2017-05-04 02:41:02 +08:00
|
|
|
_update_dialog();
|
2014-02-10 09:10:30 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
memdelete(d);
|
|
|
|
}
|
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
/* Does file already exist */
|
|
|
|
|
|
|
|
DirAccess *f = DirAccess::create(DirAccess::ACCESS_RESOURCES);
|
2017-09-02 01:25:01 +08:00
|
|
|
if (f->dir_exists(p)) {
|
|
|
|
is_new_script_created = false;
|
|
|
|
is_path_valid = false;
|
|
|
|
_msg_path_valid(false, TTR("Directory of the same name exists"));
|
|
|
|
} else if (f->file_exists(p)) {
|
2017-05-04 02:41:02 +08:00
|
|
|
is_new_script_created = false;
|
|
|
|
is_path_valid = true;
|
2017-09-02 01:25:01 +08:00
|
|
|
_msg_path_valid(true, TTR("File exists, will be reused"));
|
|
|
|
} else {
|
|
|
|
path_error_label->set_text("");
|
2017-05-04 02:41:02 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
memdelete(f);
|
2017-05-04 02:41:02 +08:00
|
|
|
_update_dialog();
|
|
|
|
|
|
|
|
/* Check file extension */
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
String extension = p.get_extension();
|
2014-02-10 09:10:30 +08:00
|
|
|
List<String> extensions;
|
|
|
|
|
2016-11-10 00:29:15 +08:00
|
|
|
// get all possible extensions for script
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int l = 0; l < language_menu->get_item_count(); l++) {
|
|
|
|
ScriptServer::get_language(l)->get_recognized_extensions(&extensions);
|
2016-11-10 00:29:15 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
bool found = false;
|
2017-05-04 02:41:02 +08:00
|
|
|
bool match = false;
|
2017-03-05 23:44:50 +08:00
|
|
|
int index = 0;
|
|
|
|
for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
|
|
|
|
if (E->get().nocasecmp_to(extension) == 0) {
|
2017-05-04 02:41:02 +08:00
|
|
|
//FIXME (?) - changing language this way doesn't update controls, needs rework
|
|
|
|
//language_menu->select(index); // change Language option by extension
|
2017-03-05 23:44:50 +08:00
|
|
|
found = true;
|
2017-05-04 02:41:02 +08:00
|
|
|
if (E->get() == ScriptServer::get_language(language_menu->get_selected())->get_extension()) {
|
|
|
|
match = true;
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-11-10 00:29:15 +08:00
|
|
|
index++;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
2017-05-04 02:41:02 +08:00
|
|
|
_msg_path_valid(false, TTR("Invalid extension"));
|
|
|
|
_update_dialog();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!match) {
|
|
|
|
_msg_path_valid(false, TTR("Wrong extension chosen"));
|
|
|
|
_update_dialog();
|
2014-02-10 09:10:30 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
/* All checks passed */
|
|
|
|
|
|
|
|
is_path_valid = true;
|
|
|
|
_update_dialog();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptCreateDialog::_msg_script_valid(bool valid, const String &p_msg) {
|
|
|
|
|
|
|
|
error_label->set_text(TTR(p_msg));
|
|
|
|
if (valid) {
|
2017-08-01 02:19:45 +08:00
|
|
|
error_label->add_color_override("font_color", get_color("success_color", "Editor"));
|
2017-05-04 02:41:02 +08:00
|
|
|
} else {
|
2017-08-01 02:19:45 +08:00
|
|
|
error_label->add_color_override("font_color", get_color("error_color", "Editor"));
|
2017-05-04 02:41:02 +08:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
void ScriptCreateDialog::_msg_path_valid(bool valid, const String &p_msg) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
path_error_label->set_text(TTR(p_msg));
|
|
|
|
if (valid) {
|
2017-08-01 02:19:45 +08:00
|
|
|
path_error_label->add_color_override("font_color", get_color("success_color", "Editor"));
|
2017-05-04 02:41:02 +08:00
|
|
|
} else {
|
2017-08-01 02:19:45 +08:00
|
|
|
path_error_label->add_color_override("font_color", get_color("error_color", "Editor"));
|
2017-05-04 02:41:02 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
void ScriptCreateDialog::_update_dialog() {
|
|
|
|
|
|
|
|
bool script_ok = true;
|
|
|
|
|
|
|
|
/* "Add Script Dialog" gui logic and script checks */
|
|
|
|
|
|
|
|
// Is Script Valid (order from top to bottom)
|
|
|
|
get_ok()->set_disabled(true);
|
|
|
|
if (!is_built_in) {
|
|
|
|
if (!is_path_valid) {
|
|
|
|
_msg_script_valid(false, TTR("Invalid Path"));
|
|
|
|
script_ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (has_named_classes && (!is_class_name_valid)) {
|
|
|
|
_msg_script_valid(false, TTR("Invalid class name"));
|
|
|
|
script_ok = false;
|
|
|
|
}
|
|
|
|
if (!is_parent_name_valid) {
|
|
|
|
_msg_script_valid(false, TTR("Invalid inherited parent name or path"));
|
|
|
|
script_ok = false;
|
|
|
|
}
|
|
|
|
if (script_ok) {
|
|
|
|
_msg_script_valid(true, TTR("Script valid"));
|
|
|
|
get_ok()->set_disabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does script have named classes */
|
2016-11-10 00:29:15 +08:00
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
if (has_named_classes) {
|
|
|
|
if (is_new_script_created) {
|
|
|
|
class_name->set_editable(true);
|
|
|
|
class_name->set_placeholder(TTR("Allowed: a-z, A-Z, 0-9 and _"));
|
|
|
|
class_name->set_placeholder_alpha(0.3);
|
|
|
|
} else {
|
|
|
|
class_name->set_editable(false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
class_name->set_editable(false);
|
|
|
|
class_name->set_placeholder(TTR("N/A"));
|
|
|
|
class_name->set_placeholder_alpha(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Can script inherit from a file */
|
|
|
|
|
|
|
|
if (can_inherit_from_file) {
|
|
|
|
parent_browse_button->set_disabled(false);
|
|
|
|
} else {
|
|
|
|
parent_browse_button->set_disabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is script Built-in */
|
|
|
|
|
|
|
|
if (is_built_in) {
|
|
|
|
file_path->set_editable(false);
|
|
|
|
path_button->set_disabled(true);
|
|
|
|
re_check_path = true;
|
|
|
|
} else {
|
|
|
|
file_path->set_editable(true);
|
|
|
|
path_button->set_disabled(false);
|
|
|
|
if (re_check_path) {
|
|
|
|
re_check_path = false;
|
|
|
|
_path_changed(file_path->get_text());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is Script created or loaded from existing file */
|
|
|
|
|
|
|
|
if (is_new_script_created) {
|
|
|
|
// New Script Created
|
2016-11-10 00:29:15 +08:00
|
|
|
get_ok()->set_text(TTR("Create"));
|
2017-05-04 02:41:02 +08:00
|
|
|
parent_name->set_editable(true);
|
|
|
|
parent_browse_button->set_disabled(false);
|
|
|
|
internal->set_disabled(false);
|
|
|
|
if (is_built_in) {
|
|
|
|
_msg_path_valid(true, TTR("Built-in script (into scene file)"));
|
|
|
|
} else {
|
|
|
|
if (script_ok) {
|
|
|
|
_msg_path_valid(true, TTR("Create new script file"));
|
|
|
|
}
|
|
|
|
}
|
2016-11-10 00:29:15 +08:00
|
|
|
} else {
|
2017-05-04 02:41:02 +08:00
|
|
|
// Script Loaded
|
2016-11-10 00:29:15 +08:00
|
|
|
get_ok()->set_text(TTR("Load"));
|
2017-05-04 02:41:02 +08:00
|
|
|
parent_name->set_editable(false);
|
|
|
|
parent_browse_button->set_disabled(true);
|
|
|
|
internal->set_disabled(true);
|
|
|
|
if (script_ok) {
|
|
|
|
_msg_path_valid(true, TTR("Load existing script file"));
|
|
|
|
}
|
2016-11-10 00:29:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
void ScriptCreateDialog::_bind_methods() {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method("_class_name_changed", &ScriptCreateDialog::_class_name_changed);
|
2017-05-04 02:41:02 +08:00
|
|
|
ClassDB::bind_method("_parent_name_changed", &ScriptCreateDialog::_parent_name_changed);
|
2017-03-05 23:44:50 +08:00
|
|
|
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);
|
2017-06-14 04:03:08 +08:00
|
|
|
ClassDB::bind_method("_template_changed", &ScriptCreateDialog::_template_changed);
|
2017-03-05 23:44:50 +08:00
|
|
|
ADD_SIGNAL(MethodInfo("script_created", PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script")));
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ScriptCreateDialog::ScriptCreateDialog() {
|
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
/* DIALOG */
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
/* Main Controls */
|
|
|
|
|
2017-08-22 03:15:36 +08:00
|
|
|
GridContainer *gc = memnew(GridContainer);
|
2017-05-04 02:41:02 +08:00
|
|
|
gc->set_columns(2);
|
|
|
|
|
|
|
|
/* Error Messages Field */
|
|
|
|
|
2017-08-22 03:15:36 +08:00
|
|
|
VBoxContainer *vb = memnew(VBoxContainer);
|
2017-05-04 02:41:02 +08:00
|
|
|
|
2017-08-22 03:15:36 +08:00
|
|
|
HBoxContainer *hb = memnew(HBoxContainer);
|
|
|
|
Label *l = memnew(Label);
|
2017-05-04 02:41:02 +08:00
|
|
|
l->set_text(" - ");
|
|
|
|
hb->add_child(l);
|
|
|
|
error_label = memnew(Label);
|
|
|
|
error_label->set_text(TTR("Error!"));
|
|
|
|
error_label->set_align(Label::ALIGN_LEFT);
|
|
|
|
hb->add_child(error_label);
|
|
|
|
vb->add_child(hb);
|
|
|
|
|
|
|
|
hb = memnew(HBoxContainer);
|
|
|
|
l = memnew(Label);
|
|
|
|
l->set_text(" - ");
|
|
|
|
hb->add_child(l);
|
|
|
|
path_error_label = memnew(Label);
|
|
|
|
path_error_label->set_text(TTR("Error!"));
|
|
|
|
path_error_label->set_align(Label::ALIGN_LEFT);
|
|
|
|
hb->add_child(path_error_label);
|
|
|
|
vb->add_child(hb);
|
|
|
|
|
2017-08-22 03:15:36 +08:00
|
|
|
PanelContainer *pc = memnew(PanelContainer);
|
2017-05-04 02:41:02 +08:00
|
|
|
pc->set_h_size_flags(Control::SIZE_FILL);
|
2017-08-01 02:19:45 +08:00
|
|
|
pc->add_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_stylebox("bg", "Tree"));
|
2017-05-04 02:41:02 +08:00
|
|
|
pc->add_child(vb);
|
|
|
|
|
|
|
|
/* Margins */
|
|
|
|
|
2017-08-22 03:15:36 +08:00
|
|
|
Control *empty_h = memnew(Control);
|
2017-05-21 04:49:34 +08:00
|
|
|
empty_h->set_name("empty_h"); //duplicate() doesn't like nodes without a name
|
2017-05-04 02:41:02 +08:00
|
|
|
empty_h->set_h_size_flags(Control::SIZE_EXPAND_FILL);
|
|
|
|
empty_h->set_v_size_flags(Control::SIZE_EXPAND_FILL);
|
|
|
|
empty_h->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
|
2017-08-22 03:15:36 +08:00
|
|
|
Control *empty_v = memnew(Control);
|
2017-05-21 04:49:34 +08:00
|
|
|
empty_v->set_name("empty_v");
|
2017-05-04 02:41:02 +08:00
|
|
|
empty_v->set_h_size_flags(Control::SIZE_EXPAND_FILL);
|
|
|
|
empty_v->set_v_size_flags(Control::SIZE_EXPAND_FILL);
|
|
|
|
empty_v->set_custom_minimum_size(Size2(10, 0 * EDSCALE));
|
|
|
|
|
|
|
|
vb = memnew(VBoxContainer);
|
|
|
|
vb->add_child(empty_h->duplicate());
|
|
|
|
vb->add_child(gc);
|
|
|
|
vb->add_child(empty_h->duplicate());
|
|
|
|
vb->add_child(pc);
|
|
|
|
vb->add_child(empty_h->duplicate());
|
|
|
|
hb = memnew(HBoxContainer);
|
|
|
|
hb->add_child(empty_v->duplicate());
|
|
|
|
hb->add_child(vb);
|
|
|
|
hb->add_child(empty_v->duplicate());
|
|
|
|
|
2017-08-25 00:51:28 +08:00
|
|
|
memdelete(empty_h);
|
|
|
|
memdelete(empty_v);
|
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
add_child(hb);
|
|
|
|
|
|
|
|
/* Language */
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
language_menu = memnew(OptionButton);
|
2017-05-04 02:41:02 +08:00
|
|
|
language_menu->set_custom_minimum_size(Size2(250, 0) * EDSCALE);
|
|
|
|
language_menu->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
l = memnew(Label);
|
|
|
|
l->set_text(TTR("Language"));
|
|
|
|
l->set_align(Label::ALIGN_RIGHT);
|
|
|
|
gc->add_child(l);
|
|
|
|
gc->add_child(language_menu);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-04-06 19:40:00 +08:00
|
|
|
int default_lang = 0;
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-04-06 19:40:00 +08:00
|
|
|
String lang = ScriptServer::get_language(i)->get_name();
|
|
|
|
language_menu->add_item(lang);
|
|
|
|
if (lang == "GDScript") {
|
|
|
|
default_lang = i;
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:22:23 +08:00
|
|
|
String last_selected_language = EditorSettings::get_singleton()->get_project_metadata("script_setup", "last_selected_language", "");
|
2017-04-06 19:40:00 +08:00
|
|
|
if (last_selected_language != "") {
|
|
|
|
for (int i = 0; i < language_menu->get_item_count(); i++) {
|
2017-03-05 23:44:50 +08:00
|
|
|
if (language_menu->get_item_text(i) == last_selected_language) {
|
2016-10-13 04:23:48 +08:00
|
|
|
language_menu->select(i);
|
2017-05-04 02:41:02 +08:00
|
|
|
current_language = i;
|
2016-10-13 04:23:48 +08:00
|
|
|
break;
|
2017-04-06 19:40:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
language_menu->select(default_lang);
|
2017-05-04 02:41:02 +08:00
|
|
|
current_language = default_lang;
|
2017-04-06 19:40:00 +08:00
|
|
|
}
|
2016-10-13 04:23:48 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
language_menu->connect("item_selected", this, "_lang_changed");
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
/* Inherits */
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
hb = memnew(HBoxContainer);
|
|
|
|
hb->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
parent_name = memnew(LineEdit);
|
|
|
|
parent_name->connect("text_changed", this, "_parent_name_changed");
|
|
|
|
parent_name->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
hb->add_child(parent_name);
|
|
|
|
parent_browse_button = memnew(Button);
|
|
|
|
parent_browse_button->set_flat(true);
|
|
|
|
parent_browse_button->connect("pressed", this, "_browse_path", varray(true));
|
|
|
|
hb->add_child(parent_browse_button);
|
|
|
|
l = memnew(Label);
|
|
|
|
l->set_text(TTR("Inherits"));
|
|
|
|
l->set_align(Label::ALIGN_RIGHT);
|
|
|
|
gc->add_child(l);
|
|
|
|
gc->add_child(hb);
|
|
|
|
is_browsing_parent = false;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
/* Class Name */
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
class_name = memnew(LineEdit);
|
|
|
|
class_name->connect("text_changed", this, "_class_name_changed");
|
|
|
|
class_name->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
l = memnew(Label);
|
|
|
|
l->set_text(TTR("Class Name"));
|
|
|
|
l->set_align(Label::ALIGN_RIGHT);
|
|
|
|
gc->add_child(l);
|
|
|
|
gc->add_child(class_name);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-06-14 04:03:08 +08:00
|
|
|
/* Templates */
|
|
|
|
|
|
|
|
template_menu = memnew(OptionButton);
|
|
|
|
l = memnew(Label);
|
|
|
|
l->set_text(TTR("Template"));
|
|
|
|
l->set_align(Label::ALIGN_RIGHT);
|
|
|
|
gc->add_child(l);
|
|
|
|
gc->add_child(template_menu);
|
|
|
|
template_menu->connect("item_selected", this, "_template_changed");
|
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
/* Built-in Script */
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
internal = memnew(CheckButton);
|
|
|
|
internal->connect("pressed", this, "_built_in_pressed");
|
|
|
|
hb = memnew(HBoxContainer);
|
2017-08-22 03:15:36 +08:00
|
|
|
Control *empty = memnew(Control);
|
2017-05-04 02:41:02 +08:00
|
|
|
hb->add_child(internal);
|
|
|
|
hb->add_child(empty);
|
|
|
|
l = memnew(Label);
|
|
|
|
l->set_text(TTR("Built-in Script"));
|
|
|
|
l->set_align(Label::ALIGN_RIGHT);
|
|
|
|
gc->add_child(l);
|
|
|
|
gc->add_child(hb);
|
|
|
|
|
|
|
|
/* Path */
|
|
|
|
|
|
|
|
hb = memnew(HBoxContainer);
|
|
|
|
file_path = memnew(LineEdit);
|
|
|
|
file_path->connect("text_changed", this, "_path_changed");
|
|
|
|
file_path->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
hb->add_child(file_path);
|
|
|
|
path_button = memnew(Button);
|
|
|
|
path_button->set_flat(true);
|
|
|
|
path_button->connect("pressed", this, "_browse_path", varray(false));
|
|
|
|
hb->add_child(path_button);
|
|
|
|
l = memnew(Label);
|
|
|
|
l->set_text(TTR("Path"));
|
|
|
|
l->set_align(Label::ALIGN_RIGHT);
|
|
|
|
gc->add_child(l);
|
|
|
|
gc->add_child(hb);
|
|
|
|
|
|
|
|
/* Dialog Setup */
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
file_browse = memnew(EditorFileDialog);
|
|
|
|
file_browse->connect("file_selected", this, "_file_selected");
|
2014-02-10 09:10:30 +08:00
|
|
|
add_child(file_browse);
|
2016-05-04 09:25:37 +08:00
|
|
|
get_ok()->set_text(TTR("Create"));
|
2017-03-05 23:44:50 +08:00
|
|
|
alert = memnew(AcceptDialog);
|
2017-05-04 02:41:02 +08:00
|
|
|
alert->set_as_minsize();
|
|
|
|
alert->get_label()->set_autowrap(true);
|
|
|
|
alert->get_label()->set_align(Label::ALIGN_CENTER);
|
|
|
|
alert->get_label()->set_valign(Label::VALIGN_CENTER);
|
|
|
|
alert->get_label()->set_custom_minimum_size(Size2(325, 60) * EDSCALE);
|
2014-02-10 09:10:30 +08:00
|
|
|
add_child(alert);
|
2016-11-10 00:29:15 +08:00
|
|
|
|
2017-05-04 02:41:02 +08:00
|
|
|
set_as_minsize();
|
|
|
|
set_hide_on_ok(false);
|
|
|
|
set_title(TTR("Attach Node Script"));
|
|
|
|
|
|
|
|
is_parent_name_valid = false;
|
|
|
|
is_class_name_valid = false;
|
|
|
|
is_path_valid = false;
|
|
|
|
|
|
|
|
has_named_classes = false;
|
|
|
|
can_inherit_from_file = false;
|
|
|
|
is_built_in = false;
|
|
|
|
|
|
|
|
is_new_script_created = true;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|