| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  script_create_dialog.cpp                                             */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2021-01-01 20:13:46 +01:00
										 |  |  | /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03: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.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #include "script_create_dialog.h"
 | 
					
						
							| 
									
										
										
										
											2017-01-16 08:04:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | #include "core/io/resource_saver.h"
 | 
					
						
							|  |  |  | #include "core/os/file_access.h"
 | 
					
						
							|  |  |  | #include "core/project_settings.h"
 | 
					
						
							|  |  |  | #include "core/script_language.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-22 18:59:43 +03:00
										 |  |  | #include "core/string_builder.h"
 | 
					
						
							| 
									
										
										
										
											2019-02-18 16:45:26 +01:00
										 |  |  | #include "editor/create_dialog.h"
 | 
					
						
							| 
									
										
										
										
											2017-07-31 13:19:45 -05:00
										 |  |  | #include "editor/editor_node.h"
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | #include "editor/editor_scale.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | #include "editor_file_system.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | void ScriptCreateDialog::_notification(int p_what) { | 
					
						
							|  |  |  | 	switch (p_what) { | 
					
						
							| 
									
										
										
										
											2018-02-25 23:04:16 +07:00
										 |  |  | 		case NOTIFICATION_THEME_CHANGED: | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 		case NOTIFICATION_ENTER_TREE: { | 
					
						
							| 
									
										
										
										
											2019-08-12 14:41:24 +03:00
										 |  |  | 			for (int i = 0; i < ScriptServer::get_language_count(); i++) { | 
					
						
							| 
									
										
										
										
											2020-01-03 22:49:22 +01:00
										 |  |  | 				String lang = ScriptServer::get_language(i)->get_type(); | 
					
						
							| 
									
										
										
										
											2019-08-12 14:41:24 +03:00
										 |  |  | 				Ref<Texture> lang_icon = get_icon(lang, "EditorIcons"); | 
					
						
							|  |  |  | 				if (lang_icon.is_valid()) { | 
					
						
							|  |  |  | 					language_menu->set_item_icon(i, lang_icon); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-02-15 00:37:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:41:24 +03:00
										 |  |  | 			String last_lang = EditorSettings::get_singleton()->get_project_metadata("script_setup", "last_selected_language", ""); | 
					
						
							|  |  |  | 			if (!last_lang.empty()) { | 
					
						
							| 
									
										
										
										
											2019-09-03 13:42:34 +03:00
										 |  |  | 				for (int i = 0; i < language_menu->get_item_count(); i++) { | 
					
						
							|  |  |  | 					if (language_menu->get_item_text(i) == last_lang) { | 
					
						
							|  |  |  | 						language_menu->select(i); | 
					
						
							|  |  |  | 						current_language = i; | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-08-12 14:41:24 +03:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2020-02-15 00:37:04 +01:00
										 |  |  | 				language_menu->select(default_language); | 
					
						
							| 
									
										
										
										
											2019-08-12 14:41:24 +03:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-02-15 00:37:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 			path_button->set_icon(get_icon("Folder", "EditorIcons")); | 
					
						
							|  |  |  | 			parent_browse_button->set_icon(get_icon("Folder", "EditorIcons")); | 
					
						
							| 
									
										
										
										
											2019-02-18 16:45:26 +01:00
										 |  |  | 			parent_search_button->set_icon(get_icon("ClassList", "EditorIcons")); | 
					
						
							| 
									
										
										
										
											2018-02-25 23:04:16 +07:00
										 |  |  | 			status_panel->add_style_override("panel", get_stylebox("bg", "Tree")); | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-08 18:42:05 +01:00
										 |  |  | void ScriptCreateDialog::_path_hbox_sorted() { | 
					
						
							|  |  |  | 	if (is_visible()) { | 
					
						
							|  |  |  | 		int filename_start_pos = initial_bp.find_last("/") + 1; | 
					
						
							|  |  |  | 		int filename_end_pos = initial_bp.length(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 14:08:22 -03:00
										 |  |  | 		if (!is_built_in) { | 
					
						
							|  |  |  | 			file_path->select(filename_start_pos, filename_end_pos); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-02-08 18:42:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// 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(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-23 00:29:24 +02:00
										 |  |  | bool ScriptCreateDialog::_can_be_built_in() { | 
					
						
							|  |  |  | 	return (supports_built_in && built_in_enabled); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-08 19:43:55 -03:00
										 |  |  | void ScriptCreateDialog::config(const String &p_base_name, const String &p_base_path, bool p_built_in_enabled, bool p_load_enabled) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	class_name->set_text(""); | 
					
						
							| 
									
										
										
										
											2018-01-10 21:33:12 -02:00
										 |  |  | 	class_name->deselect(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	parent_name->set_text(p_base_name); | 
					
						
							| 
									
										
										
										
											2018-01-10 21:33:12 -02:00
										 |  |  | 	parent_name->deselect(); | 
					
						
							| 
									
										
										
										
											2018-09-12 12:29:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01: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()); | 
					
						
							| 
									
										
										
										
											2020-01-01 01:09:50 +01:00
										 |  |  | 		current_language = language_menu->get_selected(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		initial_bp = ""; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		file_path->set_text(""); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-01-10 21:33:12 -02:00
										 |  |  | 	file_path->deselect(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-23 00:29:24 +02:00
										 |  |  | 	built_in_enabled = p_built_in_enabled; | 
					
						
							| 
									
										
										
										
											2020-01-08 19:43:55 -03:00
										 |  |  | 	load_enabled = p_load_enabled; | 
					
						
							| 
									
										
										
										
											2018-09-23 00:29:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	_lang_changed(current_language); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	_class_name_changed(""); | 
					
						
							|  |  |  | 	_path_changed(file_path->get_text()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 16:45:26 +01:00
										 |  |  | void ScriptCreateDialog::set_inheritance_base_type(const String &p_base) { | 
					
						
							|  |  |  | 	base_type = p_base; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 16:05:24 -05:00
										 |  |  | bool ScriptCreateDialog::_validate_parent(const String &p_string) { | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	if (p_string.length() == 0) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 16:05:24 -05:00
										 |  |  | 	if (can_inherit_from_file && p_string.is_quoted()) { | 
					
						
							| 
									
										
										
										
											2019-04-30 15:58:02 +02:00
										 |  |  | 		String p = p_string.substr(1, p_string.length() - 2); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 		if (_validate_path(p, true) == "") { | 
					
						
							| 
									
										
										
										
											2019-04-30 15:58:02 +02:00
										 |  |  | 			return true; | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-04-30 15:58:02 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 16:05:24 -05:00
										 |  |  | 	return ClassDB::class_exists(p_string) || ScriptServer::is_global_class(p_string); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool ScriptCreateDialog::_validate_class(const String &p_string) { | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	if (p_string.length() == 0) { | 
					
						
							| 
									
										
										
										
											2019-06-11 16:05:24 -05:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-06-11 16:05:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int i = 0; i < p_string.length(); i++) { | 
					
						
							|  |  |  | 		if (i == 0) { | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 			if (p_string[0] >= '0' && p_string[0] <= '9') { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 				return false; // no start with number plz
 | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-10 13:44:52 +01: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] == '.'; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 		if (!valid_char) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			return false; | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-30 15:58:02 +02:00
										 |  |  | String ScriptCreateDialog::_validate_path(const String &p_path, bool p_file_must_exist) { | 
					
						
							|  |  |  | 	String p = p_path.strip_edges(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	if (p == "") { | 
					
						
							| 
									
										
										
										
											2021-05-04 14:28:27 +02:00
										 |  |  | 		return TTR("Path is empty."); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if (p.get_file().get_basename() == "") { | 
					
						
							| 
									
										
										
										
											2021-05-04 14:28:27 +02:00
										 |  |  | 		return TTR("Filename is empty."); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-30 15:58:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	p = ProjectSettings::get_singleton()->localize_path(p); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	if (!p.begins_with("res://")) { | 
					
						
							| 
									
										
										
										
											2021-05-04 14:28:27 +02:00
										 |  |  | 		return TTR("Path is not local."); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-30 15:58:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES); | 
					
						
							|  |  |  | 	if (d->change_dir(p.get_base_dir()) != OK) { | 
					
						
							|  |  |  | 		memdelete(d); | 
					
						
							|  |  |  | 		return TTR("Invalid base path."); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	memdelete(d); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Does file already exist */ | 
					
						
							|  |  |  | 	DirAccess *f = DirAccess::create(DirAccess::ACCESS_RESOURCES); | 
					
						
							|  |  |  | 	if (f->dir_exists(p)) { | 
					
						
							|  |  |  | 		memdelete(f); | 
					
						
							|  |  |  | 		return TTR("A directory with the same name exists."); | 
					
						
							|  |  |  | 	} else if (p_file_must_exist && !f->file_exists(p)) { | 
					
						
							|  |  |  | 		memdelete(f); | 
					
						
							|  |  |  | 		return TTR("File does not exist."); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	memdelete(f); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Check file extension */ | 
					
						
							|  |  |  | 	String extension = p.get_extension(); | 
					
						
							|  |  |  | 	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); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool found = false; | 
					
						
							|  |  |  | 	bool match = false; | 
					
						
							|  |  |  | 	int index = 0; | 
					
						
							|  |  |  | 	for (List<String>::Element *E = extensions.front(); E; E = E->next()) { | 
					
						
							|  |  |  | 		if (E->get().nocasecmp_to(extension) == 0) { | 
					
						
							|  |  |  | 			//FIXME (?) - changing language this way doesn't update controls, needs rework
 | 
					
						
							|  |  |  | 			//language_menu->select(index); // change Language option by extension
 | 
					
						
							|  |  |  | 			found = true; | 
					
						
							|  |  |  | 			if (E->get() == ScriptServer::get_language(language_menu->get_selected())->get_extension()) { | 
					
						
							|  |  |  | 				match = true; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		index++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	if (!found) { | 
					
						
							| 
									
										
										
										
											2021-05-04 14:28:27 +02:00
										 |  |  | 		return TTR("Invalid extension."); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if (!match) { | 
					
						
							| 
									
										
										
										
											2021-05-04 14:28:27 +02:00
										 |  |  | 		return TTR("Wrong extension chosen."); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-30 15:58:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Let ScriptLanguage do custom validation */ | 
					
						
							|  |  |  | 	String path_error = ScriptServer::get_language(language_menu->get_selected())->validate_path(p); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	if (path_error != "") { | 
					
						
							| 
									
										
										
										
											2021-05-04 14:28:27 +02:00
										 |  |  | 		return path_error; | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-30 15:58:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* All checks passed */ | 
					
						
							|  |  |  | 	return ""; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-29 01:31:41 -04:00
										 |  |  | String ScriptCreateDialog::_get_class_name() const { | 
					
						
							|  |  |  | 	if (has_named_classes) { | 
					
						
							|  |  |  | 		return class_name->get_text(); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return ProjectSettings::get_singleton()->localize_path(file_path->get_text()).get_file().get_basename(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void ScriptCreateDialog::_class_name_changed(const String &p_name) { | 
					
						
							| 
									
										
										
										
											2019-06-11 16:05:24 -05:00
										 |  |  | 	if (_validate_class(class_name->get_text())) { | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 		is_class_name_valid = true; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 		is_class_name_valid = false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	_update_dialog(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | void ScriptCreateDialog::_parent_name_changed(const String &p_parent) { | 
					
						
							| 
									
										
										
										
											2019-06-11 16:05:24 -05:00
										 |  |  | 	if (_validate_parent(parent_name->get_text())) { | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 		is_parent_name_valid = true; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		is_parent_name_valid = false; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	_update_dialog(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 20:03:08 +00:00
										 |  |  | void ScriptCreateDialog::_template_changed(int p_template) { | 
					
						
							| 
									
										
										
										
											2019-08-22 18:59:43 +03:00
										 |  |  | 	String selected_template = p_template == 0 ? "" : template_menu->get_item_text(p_template); | 
					
						
							| 
									
										
										
										
											2017-07-07 15:22:23 +07:00
										 |  |  | 	EditorSettings::get_singleton()->set_project_metadata("script_setup", "last_selected_template", selected_template); | 
					
						
							| 
									
										
										
										
											2017-06-13 20:03:08 +00:00
										 |  |  | 	if (p_template == 0) { | 
					
						
							|  |  |  | 		//default
 | 
					
						
							|  |  |  | 		script_template = ""; | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-22 18:59:43 +03:00
										 |  |  | 	int selected_id = template_menu->get_selected_id(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < template_list.size(); i++) { | 
					
						
							|  |  |  | 		const ScriptTemplateInfo &sinfo = template_list[i]; | 
					
						
							|  |  |  | 		if (sinfo.id == selected_id) { | 
					
						
							|  |  |  | 			script_template = sinfo.dir.plus_file(sinfo.name + "." + sinfo.extension); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-06-13 20:03:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | void ScriptCreateDialog::ok_pressed() { | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	if (is_new_script_created) { | 
					
						
							| 
									
										
										
										
											2016-11-10 01:29:15 +09:00
										 |  |  | 		_create_new(); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		_load_exist(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	is_new_script_created = true; | 
					
						
							|  |  |  | 	_update_dialog(); | 
					
						
							| 
									
										
										
										
											2016-11-10 01:29:15 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ScriptCreateDialog::_create_new() { | 
					
						
							| 
									
										
										
										
											2020-09-29 01:31:41 -04:00
										 |  |  | 	String cname_param = _get_class_name(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 20:03:08 +00:00
										 |  |  | 	Ref<Script> scr; | 
					
						
							|  |  |  | 	if (script_template != "") { | 
					
						
							| 
									
										
										
										
											2017-07-07 15:22:23 +07:00
										 |  |  | 		scr = ResourceLoader::load(script_template); | 
					
						
							|  |  |  | 		if (scr.is_null()) { | 
					
						
							|  |  |  | 			alert->set_text(vformat(TTR("Error loading template '%s'"), script_template)); | 
					
						
							|  |  |  | 			alert->popup_centered(); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		scr = scr->duplicate(); | 
					
						
							| 
									
										
										
										
											2017-10-24 01:54:47 +02:00
										 |  |  | 		ScriptServer::get_language(language_menu->get_selected())->make_template(cname_param, parent_name->get_text(), scr); | 
					
						
							| 
									
										
										
										
											2017-06-13 20:03:08 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2017-10-24 01:54:47 +02:00
										 |  |  | 		scr = ScriptServer::get_language(language_menu->get_selected())->get_template(cname_param, parent_name->get_text()); | 
					
						
							| 
									
										
										
										
											2017-06-13 20:03:08 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-05 22:46:45 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-24 01:54:47 +02:00
										 |  |  | 	if (has_named_classes) { | 
					
						
							|  |  |  | 		String cname = class_name->get_text(); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 		if (cname.length()) { | 
					
						
							| 
									
										
										
										
											2017-10-24 01:54:47 +02:00
										 |  |  | 			scr->set_name(cname); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-10-24 01:54:47 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	if (!is_built_in) { | 
					
						
							| 
									
										
										
										
											2017-07-19 17:00:46 -03:00
										 |  |  | 		String lpath = ProjectSettings::get_singleton()->localize_path(file_path->get_text()); | 
					
						
							| 
									
										
										
										
											2016-08-05 22:46:45 -03:00
										 |  |  | 		scr->set_path(lpath); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		Error err = ResourceSaver::save(lpath, scr, ResourceSaver::FLAG_CHANGE_PATH); | 
					
						
							|  |  |  | 		if (err != OK) { | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 			alert->set_text(TTR("Error - Could not create script in filesystem.")); | 
					
						
							|  |  |  | 			alert->popup_centered(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-11-10 01:29:15 +09:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	emit_signal("script_created", scr); | 
					
						
							| 
									
										
										
										
											2019-06-29 17:20:10 -04:00
										 |  |  | 	hide(); | 
					
						
							| 
									
										
										
										
											2016-11-10 01:29:15 +09:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-10 01:29:15 +09:00
										 |  |  | void ScriptCreateDialog::_load_exist() { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	String path = file_path->get_text(); | 
					
						
							| 
									
										
										
										
											2016-11-10 01:29:15 +09:00
										 |  |  | 	RES p_script = ResourceLoader::load(path, "Script"); | 
					
						
							|  |  |  | 	if (p_script.is_null()) { | 
					
						
							|  |  |  | 		alert->set_text(vformat(TTR("Error loading script from %s"), path)); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 		alert->popup_centered(); | 
					
						
							| 
									
										
										
										
											2016-11-10 01:29:15 +09:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	emit_signal("script_created", p_script.get_ref_ptr()); | 
					
						
							| 
									
										
										
										
											2019-06-29 17:20:10 -04:00
										 |  |  | 	hide(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ScriptCreateDialog::_lang_changed(int l) { | 
					
						
							| 
									
										
										
										
											2017-06-13 20:03:08 +00:00
										 |  |  | 	ScriptLanguage *language = ScriptServer::get_language(l); | 
					
						
							| 
									
										
										
										
											2017-10-24 01:54:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 16:05:24 -05:00
										 |  |  | 	has_named_classes = language->has_named_classes(); | 
					
						
							|  |  |  | 	can_inherit_from_file = language->can_inherit_from_file(); | 
					
						
							|  |  |  | 	supports_built_in = language->supports_builtin_mode(); | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	if (!supports_built_in) { | 
					
						
							| 
									
										
										
										
											2017-10-24 01:54:47 +02:00
										 |  |  | 		is_built_in = false; | 
					
						
							| 
									
										
										
										
											2021-05-05 12:44:11 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-15 17:35:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 20:03:08 +00:00
										 |  |  | 	String selected_ext = "." + language->get_extension(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	String path = file_path->get_text(); | 
					
						
							|  |  |  | 	String extension = ""; | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	if (path != "") { | 
					
						
							| 
									
										
										
										
											2018-01-04 01:00:11 +01:00
										 |  |  | 		if (path.find(".") != -1) { | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 			extension = path.get_extension(); | 
					
						
							| 
									
										
										
										
											2016-11-10 01:29:15 +09:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02: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
 | 
					
						
							| 
									
										
										
										
											2019-02-12 21:10:08 +01:00
										 |  |  | 			for (int m = 0; m < language_menu->get_item_count(); m++) { | 
					
						
							|  |  |  | 				ScriptServer::get_language(m)->get_recognized_extensions(&extensions); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02: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 01:29:15 +09:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-07-31 13:19:45 -05:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		path = "class" + selected_ext; | 
					
						
							|  |  |  | 		_path_changed(path); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-07-31 13:19:45 -05:00
										 |  |  | 	file_path->set_text(path); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 20:03:08 +00:00
										 |  |  | 	bool use_templates = language->is_using_templates(); | 
					
						
							|  |  |  | 	template_menu->set_disabled(!use_templates); | 
					
						
							| 
									
										
										
										
											2017-07-08 18:33:25 +07:00
										 |  |  | 	template_menu->clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-22 18:59:43 +03:00
										 |  |  | 	if (use_templates) { | 
					
						
							|  |  |  | 		_update_script_templates(language->get_extension()); | 
					
						
							| 
									
										
										
										
											2017-07-07 15:22:23 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		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-13 20:03:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		template_menu->add_item(TTR("Default")); | 
					
						
							| 
									
										
										
										
											2019-08-22 18:59:43 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		ScriptTemplateInfo *templates = template_list.ptrw(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Vector<String> origin_names; | 
					
						
							|  |  |  | 		origin_names.push_back(TTR("Project")); | 
					
						
							|  |  |  | 		origin_names.push_back(TTR("Editor")); | 
					
						
							|  |  |  | 		int cur_origin = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Populate script template items previously sorted and now grouped by origin
 | 
					
						
							| 
									
										
										
										
											2017-06-13 20:03:08 +00:00
										 |  |  | 		for (int i = 0; i < template_list.size(); i++) { | 
					
						
							| 
									
										
										
										
											2019-08-22 18:59:43 +03:00
										 |  |  | 			if (int(templates[i].origin) != cur_origin) { | 
					
						
							|  |  |  | 				template_menu->add_separator(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				String origin_name = origin_names[templates[i].origin]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				int last_index = template_menu->get_item_count() - 1; | 
					
						
							|  |  |  | 				template_menu->set_item_text(last_index, origin_name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				cur_origin = templates[i].origin; | 
					
						
							| 
									
										
										
										
											2017-07-07 15:22:23 +07:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-08-22 18:59:43 +03:00
										 |  |  | 			String item_name = templates[i].name.capitalize(); | 
					
						
							|  |  |  | 			template_menu->add_item(item_name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			int new_id = template_menu->get_item_count() - 1; | 
					
						
							|  |  |  | 			templates[i].id = new_id; | 
					
						
							| 
									
										
										
										
											2017-06-13 20:03:08 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-08-22 18:59:43 +03:00
										 |  |  | 		// Disable overridden
 | 
					
						
							| 
									
										
										
										
											2021-05-04 14:20:36 +02:00
										 |  |  | 		for (Map<String, Vector<int>>::Element *E = template_overrides.front(); E; E = E->next()) { | 
					
						
							| 
									
										
										
										
											2019-08-22 18:59:43 +03:00
										 |  |  | 			const Vector<int> &overrides = E->get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (overrides.size() == 1) { | 
					
						
							|  |  |  | 				continue; // doesn't override anything
 | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			const ScriptTemplateInfo &extended = template_list[overrides[0]]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			StringBuilder override_info; | 
					
						
							|  |  |  | 			override_info += TTR("Overrides"); | 
					
						
							|  |  |  | 			override_info += ": "; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			for (int i = 1; i < overrides.size(); i++) { | 
					
						
							|  |  |  | 				const ScriptTemplateInfo &overridden = template_list[overrides[i]]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				int disable_index = template_menu->get_item_index(overridden.id); | 
					
						
							|  |  |  | 				template_menu->set_item_disabled(disable_index, true); | 
					
						
							| 
									
										
										
										
											2017-07-08 18:33:25 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-22 18:59:43 +03:00
										 |  |  | 				override_info += origin_names[overridden.origin]; | 
					
						
							|  |  |  | 				if (i < overrides.size() - 1) { | 
					
						
							|  |  |  | 					override_info += ", "; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			template_menu->set_item_icon(extended.id, get_icon("Override", "EditorIcons")); | 
					
						
							|  |  |  | 			template_menu->get_popup()->set_item_tooltip(extended.id, override_info.as_string()); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// Reselect last selected template
 | 
					
						
							|  |  |  | 		for (int i = 0; i < template_menu->get_item_count(); i++) { | 
					
						
							|  |  |  | 			const String &ti = template_menu->get_item_text(i); | 
					
						
							|  |  |  | 			if (language_menu->get_item_text(language_menu->get_selected()) == last_lang && last_template == ti) { | 
					
						
							|  |  |  | 				template_menu->select(i); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2017-07-08 18:33:25 +07:00
										 |  |  | 		template_menu->add_item(TTR("N/A")); | 
					
						
							|  |  |  | 		script_template = ""; | 
					
						
							| 
									
										
										
										
											2017-06-13 20:03:08 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-08 18:33:25 +07: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())); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-10 21:33:12 -02:00
										 |  |  | 	_parent_name_changed(parent_name->get_text()); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	_update_dialog(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-22 18:59:43 +03:00
										 |  |  | void ScriptCreateDialog::_update_script_templates(const String &p_extension) { | 
					
						
							|  |  |  | 	template_list.clear(); | 
					
						
							|  |  |  | 	template_overrides.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Vector<String> dirs; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Ordered from local to global for correct override mechanism
 | 
					
						
							|  |  |  | 	dirs.push_back(EditorSettings::get_singleton()->get_project_script_templates_dir()); | 
					
						
							|  |  |  | 	dirs.push_back(EditorSettings::get_singleton()->get_script_templates_dir()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < dirs.size(); i++) { | 
					
						
							|  |  |  | 		Vector<String> list = EditorSettings::get_singleton()->get_script_templates(p_extension, dirs[i]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (int j = 0; j < list.size(); j++) { | 
					
						
							|  |  |  | 			ScriptTemplateInfo sinfo; | 
					
						
							|  |  |  | 			sinfo.origin = ScriptOrigin(i); | 
					
						
							|  |  |  | 			sinfo.dir = dirs[i]; | 
					
						
							|  |  |  | 			sinfo.name = list[j]; | 
					
						
							|  |  |  | 			sinfo.extension = p_extension; | 
					
						
							|  |  |  | 			template_list.push_back(sinfo); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (!template_overrides.has(sinfo.name)) { | 
					
						
							|  |  |  | 				Vector<int> overrides; | 
					
						
							|  |  |  | 				overrides.push_back(template_list.size() - 1); // first one
 | 
					
						
							|  |  |  | 				template_overrides.insert(sinfo.name, overrides); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				Vector<int> &overrides = template_overrides[sinfo.name]; | 
					
						
							|  |  |  | 				overrides.push_back(template_list.size() - 1); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | void ScriptCreateDialog::_built_in_pressed() { | 
					
						
							|  |  |  | 	if (internal->is_pressed()) { | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 		is_built_in = true; | 
					
						
							| 
									
										
										
										
											2018-12-16 18:31:43 +09:00
										 |  |  | 		is_new_script_created = true; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 		is_built_in = false; | 
					
						
							| 
									
										
										
										
											2018-12-16 18:31:43 +09:00
										 |  |  | 		_path_changed(file_path->get_text()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	_update_dialog(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-10 00:10:55 -02:00
										 |  |  | void ScriptCreateDialog::_browse_path(bool browse_parent, bool p_save) { | 
					
						
							| 
									
										
										
										
											2017-04-15 17:35:06 +02:00
										 |  |  | 	is_browsing_parent = browse_parent; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-10 00:10:55 -02:00
										 |  |  | 	if (p_save) { | 
					
						
							|  |  |  | 		file_browse->set_mode(EditorFileDialog::MODE_SAVE_FILE); | 
					
						
							| 
									
										
										
										
											2019-03-25 01:54:29 +01:00
										 |  |  | 		file_browse->set_title(TTR("Open Script / Choose Location")); | 
					
						
							| 
									
										
										
										
											2018-01-10 00:10:55 -02:00
										 |  |  | 		file_browse->get_ok()->set_text(TTR("Open")); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		file_browse->set_mode(EditorFileDialog::MODE_OPEN_FILE); | 
					
						
							|  |  |  | 		file_browse->set_title(TTR("Open Script")); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-20 00:38:13 -03:00
										 |  |  | 	file_browse->set_disable_overwrite_warning(true); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	file_browse->clear_filters(); | 
					
						
							|  |  |  | 	List<String> extensions; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 15:18:55 +02:00
										 |  |  | 	int lang = language_menu->get_selected(); | 
					
						
							|  |  |  | 	ScriptServer::get_language(lang)->get_recognized_extensions(&extensions); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (List<String>::Element *E = extensions.front(); E; E = E->next()) { | 
					
						
							|  |  |  | 		file_browse->add_filter("*." + E->get()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 	file_browse->set_current_path(file_path->get_text()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	file_browse->popup_centered_ratio(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void ScriptCreateDialog::_file_selected(const String &p_file) { | 
					
						
							| 
									
										
										
										
											2017-07-19 17:00:46 -03:00
										 |  |  | 	String p = ProjectSettings::get_singleton()->localize_path(p_file); | 
					
						
							| 
									
										
										
										
											2017-04-15 17:35:06 +02:00
										 |  |  | 	if (is_browsing_parent) { | 
					
						
							|  |  |  | 		parent_name->set_text("\"" + p + "\""); | 
					
						
							| 
									
										
										
										
											2019-06-11 16:05:24 -05:00
										 |  |  | 		_parent_name_changed(parent_name->get_text()); | 
					
						
							| 
									
										
										
										
											2017-04-15 17:35:06 +02:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		file_path->set_text(p); | 
					
						
							|  |  |  | 		_path_changed(p); | 
					
						
							| 
									
										
										
										
											2017-12-14 11:25:00 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		String filename = p.get_file().get_basename(); | 
					
						
							|  |  |  | 		int select_start = p.find_last(filename); | 
					
						
							|  |  |  | 		file_path->select(select_start, select_start + filename.length()); | 
					
						
							|  |  |  | 		file_path->set_cursor_position(select_start + filename.length()); | 
					
						
							|  |  |  | 		file_path->grab_focus(); | 
					
						
							| 
									
										
										
										
											2017-04-15 17:35:06 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 16:45:26 +01:00
										 |  |  | void ScriptCreateDialog::_create() { | 
					
						
							| 
									
										
										
										
											2019-06-11 16:05:24 -05:00
										 |  |  | 	parent_name->set_text(select_class->get_selected_type().split(" ")[0]); | 
					
						
							|  |  |  | 	_parent_name_changed(parent_name->get_text()); | 
					
						
							| 
									
										
										
										
											2019-02-18 16:45:26 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ScriptCreateDialog::_browse_class_in_tree() { | 
					
						
							|  |  |  | 	select_class->set_base_type(base_type); | 
					
						
							|  |  |  | 	select_class->popup_create(true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void ScriptCreateDialog::_path_changed(const String &p_path) { | 
					
						
							| 
									
										
										
										
											2020-02-02 14:08:22 -03:00
										 |  |  | 	if (is_built_in) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	is_path_valid = false; | 
					
						
							|  |  |  | 	is_new_script_created = true; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-30 15:58:02 +02:00
										 |  |  | 	String path_error = _validate_path(p_path, false); | 
					
						
							|  |  |  | 	if (path_error != "") { | 
					
						
							|  |  |  | 		_msg_path_valid(false, path_error); | 
					
						
							| 
									
										
										
										
											2018-01-04 01:00:11 +01:00
										 |  |  | 		_update_dialog(); | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	/* Does file already exist */ | 
					
						
							|  |  |  | 	DirAccess *f = DirAccess::create(DirAccess::ACCESS_RESOURCES); | 
					
						
							| 
									
										
										
										
											2019-04-30 15:58:02 +02:00
										 |  |  | 	String p = ProjectSettings::get_singleton()->localize_path(p_path.strip_edges()); | 
					
						
							|  |  |  | 	if (f->file_exists(p)) { | 
					
						
							| 
									
										
										
										
											2017-09-01 14:25:01 -03:00
										 |  |  | 		is_new_script_created = false; | 
					
						
							| 
									
										
										
										
											2019-03-25 01:54:29 +01:00
										 |  |  | 		_msg_path_valid(true, TTR("File exists, it will be reused.")); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	memdelete(f); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	is_path_valid = true; | 
					
						
							|  |  |  | 	_update_dialog(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-11 21:19:06 +09:00
										 |  |  | void ScriptCreateDialog::_path_entered(const String &p_path) { | 
					
						
							|  |  |  | 	ok_pressed(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | void ScriptCreateDialog::_msg_script_valid(bool valid, const String &p_msg) { | 
					
						
							| 
									
										
										
										
											2021-07-28 19:47:11 +02:00
										 |  |  | 	error_label->set_text(String::utf8("• ") + TTR(p_msg)); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	if (valid) { | 
					
						
							| 
									
										
										
										
											2017-07-31 13:19:45 -05:00
										 |  |  | 		error_label->add_color_override("font_color", get_color("success_color", "Editor")); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2017-07-31 13:19:45 -05:00
										 |  |  | 		error_label->add_color_override("font_color", get_color("error_color", "Editor")); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | void ScriptCreateDialog::_msg_path_valid(bool valid, const String &p_msg) { | 
					
						
							| 
									
										
										
										
											2021-07-28 19:47:11 +02:00
										 |  |  | 	path_error_label->set_text(String::utf8("• ") + TTR(p_msg)); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	if (valid) { | 
					
						
							| 
									
										
										
										
											2017-07-31 13:19:45 -05:00
										 |  |  | 		path_error_label->add_color_override("font_color", get_color("success_color", "Editor")); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2017-07-31 13:19:45 -05:00
										 |  |  | 		path_error_label->add_color_override("font_color", get_color("error_color", "Editor")); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | void ScriptCreateDialog::_update_dialog() { | 
					
						
							| 
									
										
										
										
											2020-01-08 19:43:55 -03:00
										 |  |  | 	/* "Add Script Dialog" GUI logic and script checks. */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	bool script_ok = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-08 19:43:55 -03:00
										 |  |  | 	// Is script path/name valid (order from top to bottom)?
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 11:35:11 -03:00
										 |  |  | 	if (!is_built_in && !is_path_valid) { | 
					
						
							|  |  |  | 		_msg_script_valid(false, TTR("Invalid path.")); | 
					
						
							|  |  |  | 		script_ok = false; | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-15 19:40:08 +01:00
										 |  |  | 	if (has_named_classes && (is_new_script_created && !is_class_name_valid)) { | 
					
						
							| 
									
										
										
										
											2019-03-25 01:54:29 +01:00
										 |  |  | 		_msg_script_valid(false, TTR("Invalid class name.")); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 		script_ok = false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-05-01 11:35:11 -03:00
										 |  |  | 	if (!is_parent_name_valid && is_new_script_created) { | 
					
						
							| 
									
										
										
										
											2019-03-25 01:54:29 +01:00
										 |  |  | 		_msg_script_valid(false, TTR("Invalid inherited parent name or path.")); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 		script_ok = false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-01-08 19:43:55 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	if (script_ok) { | 
					
						
							| 
									
										
										
										
											2020-01-08 20:25:13 -03:00
										 |  |  | 		_msg_script_valid(true, TTR("Script path/name is valid.")); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-08 19:43:55 -03:00
										 |  |  | 	// Does script have named classes?
 | 
					
						
							| 
									
										
										
										
											2016-11-10 01:29:15 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	if (has_named_classes) { | 
					
						
							|  |  |  | 		if (is_new_script_created) { | 
					
						
							|  |  |  | 			class_name->set_editable(true); | 
					
						
							| 
									
										
										
										
											2019-07-10 13:44:52 +01:00
										 |  |  | 			class_name->set_placeholder(TTR("Allowed: a-z, A-Z, 0-9, _ and .")); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 			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); | 
					
						
							| 
									
										
										
										
											2019-06-11 16:05:24 -05:00
										 |  |  | 		class_name->set_text(""); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-08 19:43:55 -03:00
										 |  |  | 	// Is script Built-in?
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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()); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-08 01:15:05 -03:00
										 |  |  | 	if (!_can_be_built_in()) { | 
					
						
							|  |  |  | 		internal->set_pressed(false); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	internal->set_disabled(!_can_be_built_in()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-08 19:43:55 -03:00
										 |  |  | 	// Is Script created or loaded from existing file?
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 23:56:57 +02:00
										 |  |  | 	builtin_warning_label->set_visible(is_built_in); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-29 01:31:41 -04:00
										 |  |  | 	// Check if the script name is the same as the parent class.
 | 
					
						
							|  |  |  | 	// This warning isn't relevant if the script is built-in.
 | 
					
						
							|  |  |  | 	script_name_warning_label->set_visible(!is_built_in && _get_class_name() == parent_name->get_text()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 14:41:01 +05:30
										 |  |  | 	if (is_built_in) { | 
					
						
							|  |  |  | 		get_ok()->set_text(TTR("Create")); | 
					
						
							|  |  |  | 		parent_name->set_editable(true); | 
					
						
							| 
									
										
										
										
											2019-06-11 16:05:24 -05:00
										 |  |  | 		parent_search_button->set_disabled(false); | 
					
						
							|  |  |  | 		parent_browse_button->set_disabled(!can_inherit_from_file); | 
					
						
							| 
									
										
										
										
											2019-03-25 01:54:29 +01:00
										 |  |  | 		_msg_path_valid(true, TTR("Built-in script (into scene file).")); | 
					
						
							| 
									
										
										
										
											2018-02-21 14:41:01 +05:30
										 |  |  | 	} else if (is_new_script_created) { | 
					
						
							| 
									
										
										
										
											2020-01-08 19:43:55 -03:00
										 |  |  | 		// New script created.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-10 01:29:15 +09:00
										 |  |  | 		get_ok()->set_text(TTR("Create")); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 		parent_name->set_editable(true); | 
					
						
							| 
									
										
										
										
											2019-06-11 16:05:24 -05:00
										 |  |  | 		parent_search_button->set_disabled(false); | 
					
						
							|  |  |  | 		parent_browse_button->set_disabled(!can_inherit_from_file); | 
					
						
							| 
									
										
										
										
											2018-02-21 14:41:01 +05:30
										 |  |  | 		if (is_path_valid) { | 
					
						
							| 
									
										
										
										
											2019-03-25 01:54:29 +01:00
										 |  |  | 			_msg_path_valid(true, TTR("Will create a new script file.")); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-01-08 19:43:55 -03:00
										 |  |  | 	} else if (load_enabled) { | 
					
						
							|  |  |  | 		// Script loaded.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-10 01:29:15 +09:00
										 |  |  | 		get_ok()->set_text(TTR("Load")); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 		parent_name->set_editable(false); | 
					
						
							| 
									
										
										
										
											2019-06-11 16:05:24 -05:00
										 |  |  | 		parent_search_button->set_disabled(true); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 		parent_browse_button->set_disabled(true); | 
					
						
							| 
									
										
										
										
											2018-01-10 21:33:12 -02:00
										 |  |  | 		if (is_path_valid) { | 
					
						
							| 
									
										
										
										
											2019-03-25 01:54:29 +01:00
										 |  |  | 			_msg_path_valid(true, TTR("Will load an existing script file.")); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-01-08 19:43:55 -03:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		get_ok()->set_text(TTR("Create")); | 
					
						
							|  |  |  | 		parent_name->set_editable(true); | 
					
						
							|  |  |  | 		parent_search_button->set_disabled(false); | 
					
						
							|  |  |  | 		parent_browse_button->set_disabled(!can_inherit_from_file); | 
					
						
							|  |  |  | 		_msg_path_valid(false, TTR("Script file already exists.")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		script_ok = false; | 
					
						
							| 
									
										
										
										
											2016-11-10 01:29:15 +09:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-01-08 19:43:55 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	get_ok()->set_disabled(!script_ok); | 
					
						
							| 
									
										
										
										
											2021-05-20 08:07:52 -04:00
										 |  |  | 	set_size(Vector2()); | 
					
						
							| 
									
										
										
										
											2021-05-05 18:10:17 -04:00
										 |  |  | 	minimum_size_changed(); | 
					
						
							| 
									
										
										
										
											2016-11-10 01:29:15 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | void ScriptCreateDialog::_bind_methods() { | 
					
						
							| 
									
										
										
										
											2019-02-08 18:42:05 +01:00
										 |  |  | 	ClassDB::bind_method("_path_hbox_sorted", &ScriptCreateDialog::_path_hbox_sorted); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method("_class_name_changed", &ScriptCreateDialog::_class_name_changed); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	ClassDB::bind_method("_parent_name_changed", &ScriptCreateDialog::_parent_name_changed); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01: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-12-11 21:19:06 +09:00
										 |  |  | 	ClassDB::bind_method("_path_entered", &ScriptCreateDialog::_path_entered); | 
					
						
							| 
									
										
										
										
											2017-06-13 20:03:08 +00:00
										 |  |  | 	ClassDB::bind_method("_template_changed", &ScriptCreateDialog::_template_changed); | 
					
						
							| 
									
										
										
										
											2019-02-18 16:45:26 +01:00
										 |  |  | 	ClassDB::bind_method("_create", &ScriptCreateDialog::_create); | 
					
						
							|  |  |  | 	ClassDB::bind_method("_browse_class_in_tree", &ScriptCreateDialog::_browse_class_in_tree); | 
					
						
							| 
									
										
										
										
											2018-06-29 22:08:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-08 19:43:55 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("config", "inherits", "path", "built_in_enabled", "load_enabled"), &ScriptCreateDialog::config, DEFVAL(true), DEFVAL(true)); | 
					
						
							| 
									
										
										
										
											2018-06-29 22:08:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ADD_SIGNAL(MethodInfo("script_created", PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script"))); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ScriptCreateDialog::ScriptCreateDialog() { | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	/* DIALOG */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	/* Main Controls */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-21 15:15:36 -04:00
										 |  |  | 	GridContainer *gc = memnew(GridContainer); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	gc->set_columns(2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Error Messages Field */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-21 15:15:36 -04:00
										 |  |  | 	VBoxContainer *vb = memnew(VBoxContainer); | 
					
						
							| 
									
										
										
										
											2021-05-20 08:07:52 -04:00
										 |  |  | 	vb->set_custom_minimum_size(Size2(340, 30) * EDSCALE); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	error_label = memnew(Label); | 
					
						
							| 
									
										
										
										
											2019-09-27 00:49:20 -03:00
										 |  |  | 	vb->add_child(error_label); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	path_error_label = memnew(Label); | 
					
						
							| 
									
										
										
										
											2019-09-27 00:49:20 -03:00
										 |  |  | 	vb->add_child(path_error_label); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 23:56:57 +02:00
										 |  |  | 	builtin_warning_label = memnew(Label); | 
					
						
							| 
									
										
										
										
											2021-05-20 08:07:52 -04:00
										 |  |  | 	builtin_warning_label->set_custom_minimum_size(Size2(340, 10) * EDSCALE); | 
					
						
							| 
									
										
										
										
											2020-04-03 23:56:57 +02:00
										 |  |  | 	builtin_warning_label->set_text( | 
					
						
							|  |  |  | 			TTR("Note: Built-in scripts have some limitations and can't be edited using an external editor.")); | 
					
						
							|  |  |  | 	vb->add_child(builtin_warning_label); | 
					
						
							|  |  |  | 	builtin_warning_label->set_autowrap(true); | 
					
						
							|  |  |  | 	builtin_warning_label->hide(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-29 01:31:41 -04:00
										 |  |  | 	script_name_warning_label = memnew(Label); | 
					
						
							| 
									
										
										
										
											2021-05-20 08:07:52 -04:00
										 |  |  | 	script_name_warning_label->set_custom_minimum_size(Size2(340, 10) * EDSCALE); | 
					
						
							| 
									
										
										
										
											2020-09-29 01:31:41 -04:00
										 |  |  | 	script_name_warning_label->set_text( | 
					
						
							|  |  |  | 			TTR("Warning: Having the script name be the same as a built-in type is usually not desired.")); | 
					
						
							|  |  |  | 	vb->add_child(script_name_warning_label); | 
					
						
							|  |  |  | 	script_name_warning_label->add_color_override("font_color", Color(1, 0.85, 0.4)); | 
					
						
							|  |  |  | 	script_name_warning_label->set_autowrap(true); | 
					
						
							|  |  |  | 	script_name_warning_label->hide(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 23:04:16 +07:00
										 |  |  | 	status_panel = memnew(PanelContainer); | 
					
						
							| 
									
										
										
										
											2021-05-20 08:07:52 -04:00
										 |  |  | 	status_panel->set_custom_minimum_size(Size2(350, 40) * EDSCALE); | 
					
						
							| 
									
										
										
										
											2018-02-25 23:04:16 +07:00
										 |  |  | 	status_panel->set_h_size_flags(Control::SIZE_FILL); | 
					
						
							|  |  |  | 	status_panel->add_child(vb); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 14:06:45 -03:00
										 |  |  | 	/* Spacing */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Control *spacing = memnew(Control); | 
					
						
							|  |  |  | 	spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE)); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	vb = memnew(VBoxContainer); | 
					
						
							|  |  |  | 	vb->add_child(gc); | 
					
						
							| 
									
										
										
										
											2019-05-01 14:06:45 -03:00
										 |  |  | 	vb->add_child(spacing); | 
					
						
							| 
									
										
										
										
											2018-02-25 23:04:16 +07:00
										 |  |  | 	vb->add_child(status_panel); | 
					
						
							| 
									
										
										
										
											2019-09-27 00:49:20 -03:00
										 |  |  | 	HBoxContainer *hb = memnew(HBoxContainer); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	hb->add_child(vb); | 
					
						
							| 
									
										
										
										
											2017-08-24 12:51:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	add_child(hb); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Language */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	language_menu = memnew(OptionButton); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	language_menu->set_custom_minimum_size(Size2(250, 0) * EDSCALE); | 
					
						
							|  |  |  | 	language_menu->set_h_size_flags(SIZE_EXPAND_FILL); | 
					
						
							| 
									
										
										
										
											2019-09-27 00:49:20 -03:00
										 |  |  | 	gc->add_child(memnew(Label(TTR("Language:")))); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	gc->add_child(language_menu); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-31 20:31:45 +03:00
										 |  |  | 	default_language = -1; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	for (int i = 0; i < ScriptServer::get_language_count(); i++) { | 
					
						
							| 
									
										
										
										
											2017-04-06 13:40:00 +02:00
										 |  |  | 		String lang = ScriptServer::get_language(i)->get_name(); | 
					
						
							|  |  |  | 		language_menu->add_item(lang); | 
					
						
							|  |  |  | 		if (lang == "GDScript") { | 
					
						
							| 
									
										
										
										
											2019-08-12 14:41:24 +03:00
										 |  |  | 			default_language = i; | 
					
						
							| 
									
										
										
										
											2017-04-06 13:40:00 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-05-31 20:31:45 +03:00
										 |  |  | 	if (default_language >= 0) { | 
					
						
							|  |  |  | 		language_menu->select(default_language); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-09-03 13:42:34 +03:00
										 |  |  | 	current_language = default_language; | 
					
						
							| 
									
										
										
										
											2016-10-12 22:23:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	language_menu->connect("item_selected", this, "_lang_changed"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	/* Inherits */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 16:45:26 +01:00
										 |  |  | 	base_type = "Object"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02: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); | 
					
						
							| 
									
										
										
										
											2019-02-18 16:45:26 +01:00
										 |  |  | 	parent_search_button = memnew(Button); | 
					
						
							|  |  |  | 	parent_search_button->set_flat(true); | 
					
						
							|  |  |  | 	parent_search_button->connect("pressed", this, "_browse_class_in_tree"); | 
					
						
							|  |  |  | 	hb->add_child(parent_search_button); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	parent_browse_button = memnew(Button); | 
					
						
							|  |  |  | 	parent_browse_button->set_flat(true); | 
					
						
							| 
									
										
										
										
											2018-01-10 00:10:55 -02:00
										 |  |  | 	parent_browse_button->connect("pressed", this, "_browse_path", varray(true, false)); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	hb->add_child(parent_browse_button); | 
					
						
							| 
									
										
										
										
											2019-09-27 00:49:20 -03:00
										 |  |  | 	gc->add_child(memnew(Label(TTR("Inherits:")))); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	gc->add_child(hb); | 
					
						
							|  |  |  | 	is_browsing_parent = false; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	/* Class Name */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	class_name = memnew(LineEdit); | 
					
						
							|  |  |  | 	class_name->connect("text_changed", this, "_class_name_changed"); | 
					
						
							|  |  |  | 	class_name->set_h_size_flags(SIZE_EXPAND_FILL); | 
					
						
							| 
									
										
										
										
											2019-09-27 00:49:20 -03:00
										 |  |  | 	gc->add_child(memnew(Label(TTR("Class Name:")))); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	gc->add_child(class_name); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 20:03:08 +00:00
										 |  |  | 	/* Templates */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	template_menu = memnew(OptionButton); | 
					
						
							| 
									
										
										
										
											2019-09-27 00:49:20 -03:00
										 |  |  | 	gc->add_child(memnew(Label(TTR("Template:")))); | 
					
						
							| 
									
										
										
										
											2017-06-13 20:03:08 +00:00
										 |  |  | 	gc->add_child(template_menu); | 
					
						
							|  |  |  | 	template_menu->connect("item_selected", this, "_template_changed"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	/* Built-in Script */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 17:30:44 +02:00
										 |  |  | 	internal = memnew(CheckBox); | 
					
						
							|  |  |  | 	internal->set_text(TTR("On")); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	internal->connect("pressed", this, "_built_in_pressed"); | 
					
						
							| 
									
										
										
										
											2020-01-08 01:15:05 -03:00
										 |  |  | 	gc->add_child(memnew(Label(TTR("Built-in Script:")))); | 
					
						
							| 
									
										
										
										
											2019-04-30 16:18:08 -03:00
										 |  |  | 	gc->add_child(internal); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Path */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	hb = memnew(HBoxContainer); | 
					
						
							| 
									
										
										
										
											2019-02-08 18:42:05 +01:00
										 |  |  | 	hb->connect("sort_children", this, "_path_hbox_sorted"); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	file_path = memnew(LineEdit); | 
					
						
							|  |  |  | 	file_path->connect("text_changed", this, "_path_changed"); | 
					
						
							| 
									
										
										
										
											2017-12-11 21:19:06 +09:00
										 |  |  | 	file_path->connect("text_entered", this, "_path_entered"); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	file_path->set_h_size_flags(SIZE_EXPAND_FILL); | 
					
						
							|  |  |  | 	hb->add_child(file_path); | 
					
						
							|  |  |  | 	path_button = memnew(Button); | 
					
						
							|  |  |  | 	path_button->set_flat(true); | 
					
						
							| 
									
										
										
										
											2018-01-10 00:10:55 -02:00
										 |  |  | 	path_button->connect("pressed", this, "_browse_path", varray(false, true)); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	hb->add_child(path_button); | 
					
						
							| 
									
										
										
										
											2019-09-27 00:49:20 -03:00
										 |  |  | 	gc->add_child(memnew(Label(TTR("Path:")))); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	gc->add_child(hb); | 
					
						
							| 
									
										
										
										
											2019-11-20 16:22:16 +01:00
										 |  |  | 	re_check_path = false; | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Dialog Setup */ | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 16:45:26 +01:00
										 |  |  | 	select_class = memnew(CreateDialog); | 
					
						
							|  |  |  | 	select_class->connect("create", this, "_create"); | 
					
						
							|  |  |  | 	add_child(select_class); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	file_browse = memnew(EditorFileDialog); | 
					
						
							|  |  |  | 	file_browse->connect("file_selected", this, "_file_selected"); | 
					
						
							| 
									
										
										
										
											2018-01-10 00:10:55 -02:00
										 |  |  | 	file_browse->set_mode(EditorFileDialog::MODE_OPEN_FILE); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	add_child(file_browse); | 
					
						
							| 
									
										
										
										
											2016-05-03 22:25:37 -03:00
										 |  |  | 	get_ok()->set_text(TTR("Create")); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	alert = memnew(AcceptDialog); | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02: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-09 22:10:30 -03:00
										 |  |  | 	add_child(alert); | 
					
						
							| 
									
										
										
										
											2016-11-10 01:29:15 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02: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; | 
					
						
							| 
									
										
										
										
											2017-10-24 01:54:47 +02:00
										 |  |  | 	supports_built_in = false; | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 	can_inherit_from_file = false; | 
					
						
							|  |  |  | 	is_built_in = false; | 
					
						
							| 
									
										
										
										
											2020-01-08 19:43:55 -03:00
										 |  |  | 	built_in_enabled = true; | 
					
						
							|  |  |  | 	load_enabled = true; | 
					
						
							| 
									
										
										
										
											2017-05-03 20:41:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	is_new_script_created = true; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } |