| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  resource_preloader_editor_plugin.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
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2022-01-03 21:27:34 +01:00
										 |  |  | /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2022 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 "resource_preloader_editor_plugin.h"
 | 
					
						
							| 
									
										
										
										
											2017-01-16 08:04:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-07 19:33:38 -03:00
										 |  |  | #include "core/config/project_settings.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | #include "core/io/resource_loader.h"
 | 
					
						
							| 
									
										
										
										
											2022-02-12 02:46:22 +01:00
										 |  |  | #include "editor/editor_file_dialog.h"
 | 
					
						
							|  |  |  | #include "editor/editor_node.h"
 | 
					
						
							| 
									
										
										
										
											2020-01-12 15:24:15 +08:00
										 |  |  | #include "editor/editor_scale.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-05 14:21:25 +01:00
										 |  |  | #include "editor/editor_settings.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | void ResourcePreloaderEditor::_notification(int p_what) { | 
					
						
							| 
									
										
										
										
											2022-02-15 21:44:22 -05:00
										 |  |  | 	switch (p_what) { | 
					
						
							| 
									
										
										
										
											2022-03-15 23:05:12 +08:00
										 |  |  | 		case NOTIFICATION_ENTER_TREE: | 
					
						
							|  |  |  | 		case NOTIFICATION_THEME_CHANGED: { | 
					
						
							| 
									
										
										
										
											2022-02-15 21:44:22 -05:00
										 |  |  | 			load->set_icon(get_theme_icon(SNAME("Folder"), SNAME("EditorIcons"))); | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void ResourcePreloaderEditor::_files_load_request(const Vector<String> &p_paths) { | 
					
						
							|  |  |  | 	for (int i = 0; i < p_paths.size(); i++) { | 
					
						
							| 
									
										
										
										
											2016-03-18 18:26:02 +08:00
										 |  |  | 		String path = p_paths[i]; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-17 12:30:13 +08:00
										 |  |  | 		RES resource; | 
					
						
							|  |  |  | 		resource = ResourceLoader::load(path); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-17 12:30:13 +08:00
										 |  |  | 		if (resource.is_null()) { | 
					
						
							| 
									
										
										
										
											2016-05-03 22:25:37 -03:00
										 |  |  | 			dialog->set_text(TTR("ERROR: Couldn't load resource!")); | 
					
						
							|  |  |  | 			dialog->set_title(TTR("Error!")); | 
					
						
							| 
									
										
										
										
											2016-05-21 01:18:35 +02:00
										 |  |  | 			//dialog->get_cancel()->set_text("Close");
 | 
					
						
							| 
									
										
										
										
											2020-12-14 18:37:30 +00:00
										 |  |  | 			dialog->get_ok_button()->set_text(TTR("Close")); | 
					
						
							| 
									
										
										
										
											2020-03-06 14:00:16 -03:00
										 |  |  | 			dialog->popup_centered(); | 
					
						
							| 
									
										
										
										
											2016-03-17 12:30:13 +08:00
										 |  |  | 			return; ///beh should show an error i guess
 | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 00:51:09 -03:00
										 |  |  | 		String basename = path.get_file().get_basename(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		String name = basename; | 
					
						
							|  |  |  | 		int counter = 1; | 
					
						
							|  |  |  | 		while (preloader->has_resource(name)) { | 
					
						
							| 
									
										
										
										
											2016-03-17 12:30:13 +08:00
										 |  |  | 			counter++; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			name = basename + " " + itos(counter); | 
					
						
							| 
									
										
										
										
											2016-03-17 12:30:13 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 22:25:37 -03:00
										 |  |  | 		undo_redo->create_action(TTR("Add Resource")); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		undo_redo->add_do_method(preloader, "add_resource", name, resource); | 
					
						
							|  |  |  | 		undo_redo->add_undo_method(preloader, "remove_resource", name); | 
					
						
							|  |  |  | 		undo_redo->add_do_method(this, "_update_library"); | 
					
						
							|  |  |  | 		undo_redo->add_undo_method(this, "_update_library"); | 
					
						
							| 
									
										
										
										
											2016-03-17 12:30:13 +08:00
										 |  |  | 		undo_redo->commit_action(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ResourcePreloaderEditor::_load_pressed() { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	loading_scene = false; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	file->clear_filters(); | 
					
						
							|  |  |  | 	List<String> extensions; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ResourceLoader::get_recognized_extensions_for_type("", &extensions); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	for (int i = 0; i < extensions.size(); i++) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		file->add_filter("*." + extensions[i]); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-06 14:00:16 -03:00
										 |  |  | 	file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES); | 
					
						
							| 
									
										
										
										
											2020-07-11 18:45:19 +02:00
										 |  |  | 	file->popup_file_dialog(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ResourcePreloaderEditor::_item_edited() { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (!tree->get_selected()) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	TreeItem *s = tree->get_selected(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (tree->get_selected_column() == 0) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		// renamed
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		String old_name = s->get_metadata(0); | 
					
						
							|  |  |  | 		String new_name = s->get_text(0); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		if (old_name == new_name) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-03 21:48:38 +05:45
										 |  |  | 		if (new_name.is_empty() || new_name.contains("\\") || new_name.contains("/") || preloader->has_resource(new_name)) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			s->set_text(0, old_name); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		RES samp = preloader->get_resource(old_name); | 
					
						
							| 
									
										
										
										
											2016-05-03 22:25:37 -03:00
										 |  |  | 		undo_redo->create_action(TTR("Rename Resource")); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		undo_redo->add_do_method(preloader, "remove_resource", old_name); | 
					
						
							|  |  |  | 		undo_redo->add_do_method(preloader, "add_resource", new_name, samp); | 
					
						
							|  |  |  | 		undo_redo->add_undo_method(preloader, "remove_resource", new_name); | 
					
						
							|  |  |  | 		undo_redo->add_undo_method(preloader, "add_resource", old_name, samp); | 
					
						
							|  |  |  | 		undo_redo->add_do_method(this, "_update_library"); | 
					
						
							|  |  |  | 		undo_redo->add_undo_method(this, "_update_library"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		undo_redo->commit_action(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-04 21:59:14 -03:00
										 |  |  | void ResourcePreloaderEditor::_remove_resource(const String &p_to_remove) { | 
					
						
							| 
									
										
										
										
											2016-05-03 22:25:37 -03:00
										 |  |  | 	undo_redo->create_action(TTR("Delete Resource")); | 
					
						
							| 
									
										
										
										
											2018-10-04 21:59:14 -03:00
										 |  |  | 	undo_redo->add_do_method(preloader, "remove_resource", p_to_remove); | 
					
						
							|  |  |  | 	undo_redo->add_undo_method(preloader, "add_resource", p_to_remove, preloader->get_resource(p_to_remove)); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	undo_redo->add_do_method(this, "_update_library"); | 
					
						
							|  |  |  | 	undo_redo->add_undo_method(this, "_update_library"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	undo_redo->commit_action(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ResourcePreloaderEditor::_paste_pressed() { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	RES r = EditorSettings::get_singleton()->get_resource_clipboard(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	if (!r.is_valid()) { | 
					
						
							| 
									
										
										
										
											2016-05-03 22:25:37 -03:00
										 |  |  | 		dialog->set_text(TTR("Resource clipboard is empty!")); | 
					
						
							|  |  |  | 		dialog->set_title(TTR("Error!")); | 
					
						
							| 
									
										
										
										
											2020-12-14 18:37:30 +00:00
										 |  |  | 		dialog->get_ok_button()->set_text(TTR("Close")); | 
					
						
							| 
									
										
										
										
											2020-03-06 14:00:16 -03:00
										 |  |  | 		dialog->popup_centered(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return; ///beh should show an error i guess
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	String name = r->get_name(); | 
					
						
							| 
									
										
										
										
											2021-12-09 03:42:46 -06:00
										 |  |  | 	if (name.is_empty()) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		name = r->get_path().get_file(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-12-09 03:42:46 -06:00
										 |  |  | 	if (name.is_empty()) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		name = r->get_class(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	String basename = name; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	int counter = 1; | 
					
						
							|  |  |  | 	while (preloader->has_resource(name)) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		counter++; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		name = basename + " " + itos(counter); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 22:25:37 -03:00
										 |  |  | 	undo_redo->create_action(TTR("Paste Resource")); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	undo_redo->add_do_method(preloader, "add_resource", name, r); | 
					
						
							|  |  |  | 	undo_redo->add_undo_method(preloader, "remove_resource", name); | 
					
						
							|  |  |  | 	undo_redo->add_do_method(this, "_update_library"); | 
					
						
							|  |  |  | 	undo_redo->add_undo_method(this, "_update_library"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	undo_redo->commit_action(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ResourcePreloaderEditor::_update_library() { | 
					
						
							|  |  |  | 	tree->clear(); | 
					
						
							|  |  |  | 	tree->set_hide_root(true); | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	TreeItem *root = tree->create_item(nullptr); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	List<StringName> rnames; | 
					
						
							|  |  |  | 	preloader->get_resource_list(&rnames); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	List<String> names; | 
					
						
							| 
									
										
										
										
											2021-07-24 15:46:25 +02:00
										 |  |  | 	for (const StringName &E : rnames) { | 
					
						
							| 
									
										
										
										
											2021-07-15 23:45:57 -04:00
										 |  |  | 		names.push_back(E); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	names.sort(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-24 15:46:25 +02:00
										 |  |  | 	for (const String &E : names) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		TreeItem *ti = tree->create_item(root); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		ti->set_cell_mode(0, TreeItem::CELL_MODE_STRING); | 
					
						
							|  |  |  | 		ti->set_editable(0, true); | 
					
						
							|  |  |  | 		ti->set_selectable(0, true); | 
					
						
							| 
									
										
										
										
											2021-07-15 23:45:57 -04:00
										 |  |  | 		ti->set_text(0, E); | 
					
						
							|  |  |  | 		ti->set_metadata(0, E); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-15 23:45:57 -04:00
										 |  |  | 		RES r = preloader->get_resource(E); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		ERR_CONTINUE(r.is_null()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-04 21:59:14 -03:00
										 |  |  | 		String type = r->get_class(); | 
					
						
							|  |  |  | 		ti->set_icon(0, EditorNode::get_singleton()->get_class_icon(type, "Object")); | 
					
						
							|  |  |  | 		ti->set_tooltip(0, TTR("Instance:") + " " + r->get_path() + "\n" + TTR("Type:") + " " + type); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-14 18:02:23 +01:00
										 |  |  | 		ti->set_text(1, r->get_path()); | 
					
						
							|  |  |  | 		ti->set_editable(1, false); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		ti->set_selectable(1, false); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-04 21:59:14 -03:00
										 |  |  | 		if (type == "PackedScene") { | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 			ti->add_button(1, get_theme_icon(SNAME("InstanceOptions"), SNAME("EditorIcons")), BUTTON_OPEN_SCENE, false, TTR("Open in Editor")); | 
					
						
							| 
									
										
										
										
											2018-10-04 21:59:14 -03:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 			ti->add_button(1, get_theme_icon(SNAME("Load"), SNAME("EditorIcons")), BUTTON_EDIT_RESOURCE, false, TTR("Open in Editor")); | 
					
						
							| 
									
										
										
										
											2018-10-04 21:59:14 -03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 		ti->add_button(1, get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), BUTTON_REMOVE, false, TTR("Remove")); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//player->add_resource("default",resource);
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-14 18:02:23 +01:00
										 |  |  | void ResourcePreloaderEditor::_cell_button_pressed(Object *p_item, int p_column, int p_id) { | 
					
						
							|  |  |  | 	TreeItem *item = Object::cast_to<TreeItem>(p_item); | 
					
						
							|  |  |  | 	ERR_FAIL_COND(!item); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-04 21:59:14 -03:00
										 |  |  | 	if (p_id == BUTTON_OPEN_SCENE) { | 
					
						
							|  |  |  | 		String rpath = item->get_text(p_column); | 
					
						
							| 
									
										
										
										
											2018-01-14 18:02:23 +01:00
										 |  |  | 		EditorInterface::get_singleton()->open_scene_from_path(rpath); | 
					
						
							| 
									
										
										
										
											2018-10-04 21:59:14 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	} else if (p_id == BUTTON_EDIT_RESOURCE) { | 
					
						
							|  |  |  | 		RES r = preloader->get_resource(item->get_text(0)); | 
					
						
							|  |  |  | 		EditorInterface::get_singleton()->edit_resource(r); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} else if (p_id == BUTTON_REMOVE) { | 
					
						
							|  |  |  | 		_remove_resource(item->get_text(0)); | 
					
						
							| 
									
										
										
										
											2018-01-14 18:02:23 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void ResourcePreloaderEditor::edit(ResourcePreloader *p_preloader) { | 
					
						
							|  |  |  | 	preloader = p_preloader; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (p_preloader) { | 
					
						
							|  |  |  | 		_update_library(); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		hide(); | 
					
						
							| 
									
										
										
										
											2017-09-30 16:19:07 +02:00
										 |  |  | 		set_physics_process(false); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | Variant ResourcePreloaderEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) { | 
					
						
							| 
									
										
										
										
											2017-09-10 15:37:49 +02:00
										 |  |  | 	TreeItem *ti = tree->get_item_at_position(p_point); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (!ti) { | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 		return Variant(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	String name = ti->get_metadata(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	RES res = preloader->get_resource(name); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (!res.is_valid()) { | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 		return Variant(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	return EditorNode::get_singleton()->drag_resource(res, p_from); | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | bool ResourcePreloaderEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const { | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 	Dictionary d = p_data; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (!d.has("type")) { | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (d.has("from") && (Object *)(d["from"]) == tree) { | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (String(d["type"]) == "resource" && d.has("resource")) { | 
					
						
							|  |  |  | 		RES r = d["resource"]; | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return r.is_valid(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (String(d["type"]) == "files") { | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 		Vector<String> files = d["files"]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 15:08:25 +02:00
										 |  |  | 		return files.size() != 0; | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void ResourcePreloaderEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (!can_drop_data_fw(p_point, p_data, p_from)) { | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Dictionary d = p_data; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (!d.has("type")) { | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (String(d["type"]) == "resource" && d.has("resource")) { | 
					
						
							|  |  |  | 		RES r = d["resource"]; | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (r.is_valid()) { | 
					
						
							|  |  |  | 			String basename; | 
					
						
							| 
									
										
										
										
											2021-12-09 03:42:46 -06:00
										 |  |  | 			if (!r->get_name().is_empty()) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				basename = r->get_name(); | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 			} else if (r->get_path().is_resource_file()) { | 
					
						
							| 
									
										
										
										
											2017-01-14 00:51:09 -03:00
										 |  |  | 				basename = r->get_path().get_basename(); | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				basename = "Resource"; | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			String name = basename; | 
					
						
							|  |  |  | 			int counter = 0; | 
					
						
							|  |  |  | 			while (preloader->has_resource(name)) { | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 				counter++; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 				name = basename + "_" + itos(counter); | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			undo_redo->create_action(TTR("Add Resource")); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			undo_redo->add_do_method(preloader, "add_resource", name, r); | 
					
						
							|  |  |  | 			undo_redo->add_undo_method(preloader, "remove_resource", name); | 
					
						
							|  |  |  | 			undo_redo->add_do_method(this, "_update_library"); | 
					
						
							|  |  |  | 			undo_redo->add_undo_method(this, "_update_library"); | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 			undo_redo->commit_action(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (String(d["type"]) == "files") { | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 		Vector<String> files = d["files"]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		_files_load_request(files); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | void ResourcePreloaderEditor::_bind_methods() { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("_update_library"), &ResourcePreloaderEditor::_update_library); | 
					
						
							| 
									
										
										
										
											2018-10-04 21:59:14 -03:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("_remove_resource", "to_remove"), &ResourcePreloaderEditor::_remove_resource); | 
					
						
							| 
									
										
										
										
											2017-02-13 12:47:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-15 23:48:59 +02:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("_get_drag_data_fw"), &ResourcePreloaderEditor::get_drag_data_fw); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("_can_drop_data_fw"), &ResourcePreloaderEditor::can_drop_data_fw); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("_drop_data_fw"), &ResourcePreloaderEditor::drop_data_fw); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ResourcePreloaderEditor::ResourcePreloaderEditor() { | 
					
						
							| 
									
										
										
										
											2022-02-08 10:14:58 +01:00
										 |  |  | 	//add_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_stylebox("panel","Panel"));
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	VBoxContainer *vbc = memnew(VBoxContainer); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	add_child(vbc); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	HBoxContainer *hbc = memnew(HBoxContainer); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	vbc->add_child(hbc); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	load = memnew(Button); | 
					
						
							| 
									
										
										
										
											2016-05-03 22:25:37 -03:00
										 |  |  | 	load->set_tooltip(TTR("Load Resource")); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	hbc->add_child(load); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	paste = memnew(Button); | 
					
						
							| 
									
										
										
										
											2016-05-03 22:25:37 -03:00
										 |  |  | 	paste->set_text(TTR("Paste")); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	hbc->add_child(paste); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	file = memnew(EditorFileDialog); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	add_child(file); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	tree = memnew(Tree); | 
					
						
							| 
									
										
										
										
											2020-02-21 18:28:45 +01:00
										 |  |  | 	tree->connect("button_pressed", callable_mp(this, &ResourcePreloaderEditor::_cell_button_pressed)); | 
					
						
							| 
									
										
										
										
											2018-10-04 21:59:14 -03:00
										 |  |  | 	tree->set_columns(2); | 
					
						
							| 
									
										
										
										
											2021-07-04 00:13:28 -03:00
										 |  |  | 	tree->set_column_expand_ratio(0, 2); | 
					
						
							|  |  |  | 	tree->set_column_clip_content(0, true); | 
					
						
							|  |  |  | 	tree->set_column_expand_ratio(1, 3); | 
					
						
							|  |  |  | 	tree->set_column_clip_content(1, true); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	tree->set_column_expand(0, true); | 
					
						
							|  |  |  | 	tree->set_column_expand(1, true); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	tree->set_v_size_flags(SIZE_EXPAND_FILL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-14 23:48:23 -03:00
										 |  |  | 	tree->set_drag_forwarding(this); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	vbc->add_child(tree); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	dialog = memnew(AcceptDialog); | 
					
						
							|  |  |  | 	add_child(dialog); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-21 18:28:45 +01:00
										 |  |  | 	load->connect("pressed", callable_mp(this, &ResourcePreloaderEditor::_load_pressed)); | 
					
						
							|  |  |  | 	paste->connect("pressed", callable_mp(this, &ResourcePreloaderEditor::_paste_pressed)); | 
					
						
							|  |  |  | 	file->connect("files_selected", callable_mp(this, &ResourcePreloaderEditor::_files_load_request)); | 
					
						
							|  |  |  | 	tree->connect("item_edited", callable_mp(this, &ResourcePreloaderEditor::_item_edited)); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	loading_scene = false; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ResourcePreloaderEditorPlugin::edit(Object *p_object) { | 
					
						
							|  |  |  | 	preloader_editor->set_undo_redo(&get_undo_redo()); | 
					
						
							| 
									
										
										
										
											2017-08-24 22:58:51 +02:00
										 |  |  | 	ResourcePreloader *s = Object::cast_to<ResourcePreloader>(p_object); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (!s) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	preloader_editor->edit(s); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool ResourcePreloaderEditorPlugin::handles(Object *p_object) const { | 
					
						
							| 
									
										
										
										
											2017-01-02 23:03:46 -03:00
										 |  |  | 	return p_object->is_class("ResourcePreloader"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ResourcePreloaderEditorPlugin::make_visible(bool p_visible) { | 
					
						
							|  |  |  | 	if (p_visible) { | 
					
						
							| 
									
										
										
										
											2016-01-17 20:03:57 -03:00
										 |  |  | 		//preloader_editor->show();
 | 
					
						
							|  |  |  | 		button->show(); | 
					
						
							| 
									
										
										
										
											2022-01-27 10:36:51 +01:00
										 |  |  | 		EditorNode::get_singleton()->make_bottom_panel_item_visible(preloader_editor); | 
					
						
							| 
									
										
										
										
											2017-01-14 12:26:56 +01:00
										 |  |  | 		//preloader_editor->set_process(true);
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		if (preloader_editor->is_visible_in_tree()) { | 
					
						
							| 
									
										
										
										
											2022-01-27 10:36:51 +01:00
										 |  |  | 			EditorNode::get_singleton()->hide_bottom_panel(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-01-17 20:03:57 -03:00
										 |  |  | 		button->hide(); | 
					
						
							|  |  |  | 		//preloader_editor->hide();
 | 
					
						
							| 
									
										
										
										
											2017-01-14 12:26:56 +01:00
										 |  |  | 		//preloader_editor->set_process(false);
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-27 10:36:51 +01:00
										 |  |  | ResourcePreloaderEditorPlugin::ResourcePreloaderEditorPlugin() { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	preloader_editor = memnew(ResourcePreloaderEditor); | 
					
						
							| 
									
										
										
										
											2020-01-12 15:24:15 +08:00
										 |  |  | 	preloader_editor->set_custom_minimum_size(Size2(0, 250) * EDSCALE); | 
					
						
							| 
									
										
										
										
											2016-01-17 20:03:57 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-27 10:36:51 +01:00
										 |  |  | 	button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("ResourcePreloader"), preloader_editor); | 
					
						
							| 
									
										
										
										
											2016-01-17 20:03:57 -03:00
										 |  |  | 	button->hide(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-14 12:26:56 +01:00
										 |  |  | 	//preloader_editor->set_anchor( MARGIN_TOP, Control::ANCHOR_END);
 | 
					
						
							|  |  |  | 	//preloader_editor->set_margin( MARGIN_TOP, 120 );
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | ResourcePreloaderEditorPlugin::~ResourcePreloaderEditorPlugin() { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } |