| 1 | /**************************************************************************/ |
| 2 | /* editor_resource_picker.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "editor_resource_picker.h" |
| 32 | |
| 33 | #include "editor/audio/audio_stream_preview.h" |
| 34 | #include "editor/doc/editor_help.h" |
| 35 | #include "editor/docks/filesystem_dock.h" |
| 36 | #include "editor/docks/scene_tree_dock.h" |
| 37 | #include "editor/editor_node.h" |
| 38 | #include "editor/editor_string_names.h" |
| 39 | #include "editor/gui/editor_file_dialog.h" |
| 40 | #include "editor/gui/editor_quick_open_dialog.h" |
| 41 | #include "editor/inspector/editor_inspector.h" |
| 42 | #include "editor/inspector/editor_resource_preview.h" |
| 43 | #include "editor/plugins/editor_resource_conversion_plugin.h" |
| 44 | #include "editor/script/script_editor_plugin.h" |
| 45 | #include "editor/settings/editor_settings.h" |
| 46 | #include "editor/themes/editor_scale.h" |
| 47 | #include "scene/gui/button.h" |
| 48 | #include "scene/gui/texture_rect.h" |
| 49 | #include "scene/property_utils.h" |
| 50 | #include "scene/resources/gradient_texture.h" |
| 51 | #include "scene/resources/image_texture.h" |
| 52 | |
| 53 | static bool _has_sub_resources(const Ref<Resource> &p_res) { |
| 54 | List<PropertyInfo> property_list; |
| 55 | p_res->get_property_list(&property_list); |
| 56 | for (const PropertyInfo &p : property_list) { |
| 57 | Variant value = p_res->get(p.name); |
| 58 | if (p.type == Variant::OBJECT && p.hint == PROPERTY_HINT_RESOURCE_TYPE && !(p.usage & PROPERTY_USAGE_NEVER_DUPLICATE) && p_res->get(p.name).get_validated_object()) { |
| 59 | return true; |
| 60 | } else if (p.type == Variant::ARRAY) { |
| 61 | Array arr = value; |
| 62 | for (Variant &var : arr) { |
| 63 | Ref<Resource> res = var; |
| 64 | if (res.is_valid()) { |
| 65 | return true; |
| 66 | } |
| 67 | } |
| 68 | } else if (p.type == Variant::DICTIONARY) { |
| 69 | Dictionary dict = value; |
| 70 | for (const KeyValue<Variant, Variant> &kv : dict) { |
| 71 | Ref<Resource> resk = kv.key; |
| 72 | Ref<Resource> resv = kv.value; |
| 73 | if (resk.is_valid() || resv.is_valid()) { |
| 74 | return true; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | void EditorResourcePicker::_update_resource() { |
| 83 | String resource_path; |
| 84 | if (edited_resource.is_valid() && edited_resource->get_path().is_resource_file()) { |
| 85 | resource_path = edited_resource->get_path() + "\n"; |
| 86 | } |
| 87 | String class_name = _get_resource_type(edited_resource); |
| 88 | |
| 89 | if (preview_rect) { |
| 90 | preview_rect->set_texture(Ref<Texture2D>()); |
| 91 | |
| 92 | assign_button->set_custom_minimum_size(assign_button_min_size); |
| 93 | if (edited_resource == Ref<Resource>()) { |
| 94 | assign_button->set_button_icon(Ref<Texture2D>()); |
| 95 | assign_button->set_text(TTR("<empty>")); |
| 96 | assign_button->set_tooltip_text(""); |
| 97 | } else { |
| 98 | assign_button->set_button_icon(EditorNode::get_singleton()->get_object_icon(edited_resource.operator->())); |
| 99 | |
| 100 | if (!edited_resource->get_name().is_empty()) { |
| 101 | assign_button->set_text(edited_resource->get_name()); |
| 102 | } else if (edited_resource->get_path().is_resource_file()) { |
| 103 | assign_button->set_text(edited_resource->get_path().get_file()); |
| 104 | } else { |
| 105 | assign_button->set_text(class_name); |
| 106 | } |
| 107 | |
| 108 | if (edited_resource->get_path().is_resource_file()) { |
| 109 | resource_path = edited_resource->get_path() + "\n"; |
| 110 | } |
| 111 | assign_button->set_tooltip_text(resource_path + TTR("Type:") + " " + class_name); |
| 112 | |
| 113 | // Preview will override the above, so called at the end. |
| 114 | EditorResourcePreview::get_singleton()->queue_edited_resource_preview(edited_resource, callable_mp(this, &EditorResourcePicker::_update_resource_preview).bind(edited_resource->get_instance_id())); |
| 115 | } |
| 116 | } else if (edited_resource.is_valid()) { |
| 117 | assign_button->set_tooltip_text(resource_path + TTR("Type:") + " " + edited_resource->get_class()); |
| 118 | } |
| 119 | |
| 120 | if (edited_resource.is_null()) { |
| 121 | make_unique_button->set_visible(false); |
| 122 | } else { |
| 123 | Ref<Resource> parent_res = _has_parent_resource(); |
| 124 | bool unique_enable = _is_uniqueness_enabled(); |
| 125 | bool unique_recursive_enabled = _is_uniqueness_enabled(true); |
| 126 | bool is_internal = EditorNode::get_singleton()->is_resource_internal_to_scene(edited_resource); |
| 127 | int num_of_copies = EditorNode::get_singleton()->get_resource_count(edited_resource); |
| 128 | make_unique_button->set_button_icon(get_editor_theme_icon(SNAME("Instance"))); |
| 129 | make_unique_button->set_visible(num_of_copies > 1 || (!is_internal && !Object::cast_to<Script>(edited_resource.ptr()))); |
| 130 | make_unique_button->set_disabled((!unique_enable && !unique_recursive_enabled) || !editable); |
| 131 | |
| 132 | String tooltip; |
| 133 | |
| 134 | String resource_name = "resource"; |
| 135 | if (edited_resource.is_valid()) { |
| 136 | resource_name = edited_resource->get_class(); |
| 137 | |
| 138 | if (edited_resource->has_meta(SceneStringName(_custom_type_script))) { |
| 139 | const Ref<Script> custom_script = PropertyUtils::get_custom_type_script(edited_resource.ptr()); |
| 140 | if (custom_script.is_valid()) { |
| 141 | const String global_name = custom_script->get_global_name(); |
| 142 | if (!global_name.is_empty()) { |
| 143 | resource_name = global_name; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (num_of_copies > 1) { |
| 150 | tooltip = vformat(TTRN("This %s is used in %d place.", "This %s is used in %d places.", num_of_copies), resource_name, num_of_copies); |
| 151 | } else if (!is_internal) { |
| 152 | tooltip = vformat(TTR("This %s is external to scene."), resource_name); |
| 153 | } |
| 154 | |
| 155 | if (!editable) { |
| 156 | tooltip += "\n" + vformat(TTR("The %s cannot be edited in the inspector and can't be made unique directly."), resource_name) + "\n"; |
| 157 | } else { |
| 158 | if (unique_enable) { |
| 159 | tooltip += "\n" + TTR("Left-click to make it unique.") + "\n"; |
| 160 | } |
| 161 | |
| 162 | if (unique_recursive_enabled) { |
| 163 | tooltip += TTR("It is possible to make its subresources unique.") + "\n" + TTR("Right-click to make them unique."); |
| 164 | } |
| 165 | |
| 166 | if (!unique_enable && EditorNode::get_singleton()->get_editor_selection()->get_full_selected_node_list().size() == 1) { |
| 167 | tooltip += TTR("In order to duplicate it, make its parent Resource unique.") + "\n"; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | make_unique_button->set_tooltip_text(tooltip); |
| 172 | } |
| 173 | |
| 174 | assign_button->set_disabled(!editable && edited_resource.is_null()); |
| 175 | quick_load_button->set_visible(editable && edited_resource.is_null()); |
| 176 | } |
| 177 | |
| 178 | void EditorResourcePicker::_update_resource_preview(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, ObjectID p_obj) { |
| 179 | if (edited_resource.is_null() || edited_resource->get_instance_id() != p_obj) { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | if (preview_rect) { |
| 184 | Ref<Script> scr = edited_resource; |
| 185 | if (scr.is_valid()) { |
| 186 | assign_button->set_text(scr->get_path().get_file()); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | if (p_preview.is_valid()) { |
| 191 | int thumbnail_size = (int)EDITOR_GET("filesystem/file_dialog/thumbnail_size") * EDSCALE; |
| 192 | if (assign_button->get_button_icon().is_valid()) { |
| 193 | preview_rect->set_offset(SIDE_LEFT, assign_button->get_button_icon()->get_width() + assign_button->get_theme_stylebox(CoreStringName(normal))->get_content_margin(SIDE_LEFT) + get_theme_constant(SNAME("h_separation"), SNAME("Button"))); |
| 194 | } |
| 195 | |
| 196 | // Resource-specific stretching. |
| 197 | if (Ref<GradientTexture1D>(edited_resource).is_valid() || Ref<Gradient>(edited_resource).is_valid()) { |
| 198 | preview_rect->set_stretch_mode(TextureRect::STRETCH_SCALE); |
| 199 | assign_button->set_custom_minimum_size(assign_button_min_size); |
| 200 | } else { |
| 201 | preview_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED); |
| 202 | assign_button->set_custom_minimum_size(assign_button_min_size.max(Size2(1, thumbnail_size))); |
| 203 | } |
| 204 | preview_rect->set_texture(p_preview); |
| 205 | assign_button->set_text(""); |
| 206 | |
| 207 | if (preview_rect->get_size().x <= thumbnail_size) { |
| 208 | assign_button->set_button_icon(Ref<Texture2D>()); |
| 209 | preview_rect->set_offset(SIDE_LEFT, 0); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void EditorResourcePicker::_resource_selected() { |
| 216 | if (edited_resource.is_null()) { |
| 217 | edit_button->set_pressed(true); |
| 218 | _update_menu(); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | emit_signal(SNAME("resource_selected"), edited_resource, false); |
| 223 | } |
| 224 | |
| 225 | void EditorResourcePicker::_resource_changed() { |
| 226 | emit_signal(SNAME("resource_changed"), edited_resource); |
| 227 | _update_resource(); |
| 228 | } |
| 229 | |
| 230 | void EditorResourcePicker::_file_selected(const String &p_path) { |
| 231 | Ref<Resource> loaded_resource = ResourceLoader::load(p_path); |
| 232 | ERR_FAIL_COND_MSG(loaded_resource.is_null(), "Cannot load resource from path '" + p_path + "'."); |
| 233 | |
| 234 | if (!base_type.is_empty()) { |
| 235 | bool any_type_matches = false; |
| 236 | |
| 237 | String res_type = loaded_resource->get_class(); |
| 238 | Ref<Script> res_script = loaded_resource->get_script(); |
| 239 | bool is_global_class = false; |
| 240 | if (res_script.is_valid()) { |
| 241 | String script_type = EditorNode::get_editor_data().script_class_get_name(res_script->get_path()); |
| 242 | if (!script_type.is_empty()) { |
| 243 | is_global_class = true; |
| 244 | res_type = script_type; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | for (int i = 0; i < base_type.get_slice_count(","); i++) { |
| 249 | String base = base_type.get_slicec(',', i); |
| 250 | |
| 251 | any_type_matches = is_global_class ? EditorNode::get_editor_data().script_class_is_parent(res_type, base) : loaded_resource->is_class(base); |
| 252 | |
| 253 | if (any_type_matches) { |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if (!any_type_matches) { |
| 259 | EditorNode::get_singleton()->show_warning(vformat(TTR("The selected resource (%s) does not match any type expected for this property (%s)."), res_type, base_type)); |
| 260 | return; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | edited_resource = loaded_resource; |
| 265 | _resource_changed(); |
| 266 | } |
| 267 | |
| 268 | void EditorResourcePicker::_resource_saved(Object *p_resource) { |
| 269 | if (edited_resource.is_valid() && p_resource == edited_resource.ptr()) { |
| 270 | emit_signal(SNAME("resource_changed"), edited_resource); |
| 271 | _update_resource(); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void EditorResourcePicker::_update_menu() { |
| 276 | if (edit_menu && edit_menu->is_visible()) { |
| 277 | edit_button->set_pressed(false); |
| 278 | edit_menu->hide(); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | _update_menu_items(); |
| 283 | |
| 284 | Rect2 gt = edit_button->get_screen_rect(); |
| 285 | edit_menu->reset_size(); |
| 286 | int ms = edit_menu->get_contents_minimum_size().width; |
| 287 | Vector2 popup_pos = gt.get_end() - Vector2(ms, 0); |
| 288 | edit_menu->set_position(popup_pos); |
| 289 | edit_menu->popup(); |
| 290 | } |
| 291 | |
| 292 | void EditorResourcePicker::_update_menu_items() { |
| 293 | _ensure_resource_menu(); |
| 294 | edit_menu->clear(); |
| 295 | |
| 296 | // Add options for creating specific subtypes of the base resource type. |
| 297 | if (is_editable()) { |
| 298 | set_create_options(edit_menu); |
| 299 | |
| 300 | // Add an option to load a resource from a file using the QuickOpen dialog. |
| 301 | edit_menu->add_icon_item(get_editor_theme_icon(SNAME("LoadQuick")), TTR("Quick Load..."), OBJ_MENU_QUICKLOAD); |
| 302 | edit_menu->set_item_tooltip(-1, TTR("Opens a quick menu to select from a list of allowed Resource files.")); |
| 303 | |
| 304 | // Add an option to load a resource from a file using the regular file dialog. |
| 305 | edit_menu->add_icon_item(get_editor_theme_icon(SNAME("Load")), TTR("Load..."), OBJ_MENU_LOAD); |
| 306 | } |
| 307 | |
| 308 | // Add options for changing existing value of the resource. |
| 309 | if (edited_resource.is_valid()) { |
| 310 | // Determine if the edited resource is part of another scene (foreign) which was imported |
| 311 | bool is_edited_resource_foreign_import = EditorNode::get_singleton()->is_resource_read_only(edited_resource, true); |
| 312 | |
| 313 | // If the resource is determined to be foreign and imported, change the menu entry's description to 'inspect' rather than 'edit' |
| 314 | // since will only be able to view its properties in read-only mode. |
| 315 | if (is_edited_resource_foreign_import) { |
| 316 | // The 'Search' icon is a magnifying glass, which seems appropriate, but maybe a bespoke icon is preferred here. |
| 317 | edit_menu->add_icon_item(get_editor_theme_icon(SNAME("Search")), TTR("Inspect"), OBJ_MENU_INSPECT); |
| 318 | } else { |
| 319 | edit_menu->add_icon_item(get_editor_theme_icon(SNAME("Edit")), TTR("Edit"), OBJ_MENU_INSPECT); |
| 320 | } |
| 321 | |
| 322 | if (is_editable()) { |
| 323 | if (!_is_custom_type_script()) { |
| 324 | edit_menu->add_icon_item(get_editor_theme_icon(SNAME("Clear")), TTR("Clear"), OBJ_MENU_CLEAR); |
| 325 | } |
| 326 | bool unique_enabled = _is_uniqueness_enabled(); |
| 327 | edit_menu->add_icon_item(get_editor_theme_icon(SNAME("Duplicate")), TTR("Make Unique"), OBJ_MENU_MAKE_UNIQUE); |
| 328 | edit_menu->set_item_disabled(-1, !unique_enabled); |
| 329 | |
| 330 | String modifier = "Ctrl"; |
| 331 | if (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) { |
| 332 | modifier = "Cmd"; |
| 333 | } |
| 334 | const String drag_and_drop_text = vformat(TTR("Hold %s while drag-and-dropping from the FileSystem dock or another resource picker to automatically make a dropped resource unique."), modifier); |
| 335 | |
| 336 | if (!unique_enabled) { |
| 337 | if (EditorNode::get_singleton()->is_resource_internal_to_scene(edited_resource) && EditorNode::get_singleton()->get_resource_count(edited_resource) == 1) { |
| 338 | edit_menu->set_item_tooltip(-1, TTR("This Resource is already unique.") + "\n" + drag_and_drop_text); |
| 339 | } else if (_has_parent_resource().is_valid()) { |
| 340 | edit_menu->set_item_tooltip(-1, TTR("In order to duplicate it, make its parent Resource unique.") + "\n" + drag_and_drop_text); |
| 341 | } |
| 342 | } else { |
| 343 | edit_menu->set_item_tooltip(-1, drag_and_drop_text); |
| 344 | } |
| 345 | |
| 346 | if (_has_sub_resources(edited_resource)) { |
| 347 | unique_enabled = _is_uniqueness_enabled(true); |
| 348 | edit_menu->add_icon_item(get_editor_theme_icon(SNAME("Duplicate")), TTR("Make Unique (Recursive)"), OBJ_MENU_MAKE_UNIQUE_RECURSIVE); |
| 349 | edit_menu->set_item_disabled(-1, !unique_enabled); |
| 350 | if (!unique_enabled) { |
| 351 | Ref<Resource> parent_res = _has_parent_resource(); |
| 352 | if (EditorNode::get_singleton()->get_editor_selection()->get_full_selected_node_list().size() == 1) { |
| 353 | edit_menu->set_item_tooltip(-1, (parent_res.is_valid() && EditorNode::get_singleton()->get_resource_count(parent_res) > 1) ? TTRC("In order to duplicate recursively, make its parent Resource unique.") : TTRC("Subresources have already been made unique.")); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | edit_menu->add_icon_item(get_editor_theme_icon(SNAME("Save")), TTR("Save"), OBJ_MENU_SAVE); |
| 359 | edit_menu->add_icon_item(get_editor_theme_icon(SNAME("Save")), TTR("Save As..."), OBJ_MENU_SAVE_AS); |
| 360 | } |
| 361 | |
| 362 | if (edited_resource->get_path().is_resource_file()) { |
| 363 | edit_menu->add_separator(); |
| 364 | edit_menu->add_icon_item(get_editor_theme_icon(SNAME("ShowInFileSystem")), TTR("Show in FileSystem"), OBJ_MENU_SHOW_IN_FILE_SYSTEM); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | // Add options to copy/paste resource. |
| 369 | Ref<Resource> cb = EditorSettings::get_singleton()->get_resource_clipboard(); |
| 370 | bool paste_valid = false; |
| 371 | if (is_editable() && cb.is_valid()) { |
| 372 | if (base_type.is_empty()) { |
| 373 | paste_valid = true; |
| 374 | } else { |
| 375 | String res_type = _get_resource_type(cb); |
| 376 | |
| 377 | for (int i = 0; i < base_type.get_slice_count(","); i++) { |
| 378 | String base = base_type.get_slicec(',', i); |
| 379 | |
| 380 | paste_valid = ClassDB::is_parent_class(res_type, base) || EditorNode::get_editor_data().script_class_is_parent(res_type, base); |
| 381 | |
| 382 | if (paste_valid) { |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | if (edited_resource.is_valid() || paste_valid) { |
| 390 | edit_menu->add_separator(); |
| 391 | |
| 392 | if (edited_resource.is_valid()) { |
| 393 | edit_menu->add_item(TTRC("Copy"), OBJ_MENU_COPY); |
| 394 | } |
| 395 | |
| 396 | if (paste_valid) { |
| 397 | edit_menu->add_item(TTRC("Paste"), OBJ_MENU_PASTE); |
| 398 | edit_menu->add_item(TTRC("Paste as Unique"), OBJ_MENU_PASTE_AS_UNIQUE); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | // Add options to convert existing resource to another type of resource. |
| 403 | if (is_editable() && edited_resource.is_valid()) { |
| 404 | Vector<Ref<EditorResourceConversionPlugin>> conversions = EditorNode::get_singleton()->find_resource_conversion_plugin_for_resource(edited_resource); |
| 405 | if (!conversions.is_empty()) { |
| 406 | edit_menu->add_separator(); |
| 407 | } |
| 408 | int relative_id = 0; |
| 409 | for (const Ref<EditorResourceConversionPlugin> &conversion : conversions) { |
| 410 | String what = conversion->converts_to(); |
| 411 | Ref<Texture2D> icon; |
| 412 | if (has_theme_icon(what, EditorStringName(EditorIcons))) { |
| 413 | icon = get_editor_theme_icon(what); |
| 414 | } else { |
| 415 | icon = get_editor_theme_icon(SNAME("Object")); |
| 416 | } |
| 417 | |
| 418 | edit_menu->add_icon_item(icon, vformat(TTR("Convert to %s"), what), CONVERT_BASE_ID + relative_id); |
| 419 | relative_id++; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void EditorResourcePicker::_edit_menu_cbk(int p_which) { |
| 425 | switch (p_which) { |
| 426 | case OBJ_MENU_LOAD: { |
| 427 | List<String> extensions; |
| 428 | for (int i = 0; i < base_type.get_slice_count(","); i++) { |
| 429 | String base = base_type.get_slicec(',', i); |
| 430 | if (base == "Resource") { |
| 431 | base = ""; |
| 432 | } |
| 433 | ResourceLoader::get_recognized_extensions_for_type(base, &extensions); |
| 434 | if (ScriptServer::is_global_class(base)) { |
| 435 | ResourceLoader::get_recognized_extensions_for_type(ScriptServer::get_global_class_native_base(base), &extensions); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | HashSet<String> valid_extensions; |
| 440 | for (const String &E : extensions) { |
| 441 | valid_extensions.insert(E); |
| 442 | } |
| 443 | |
| 444 | if (!file_dialog) { |
| 445 | file_dialog = memnew(EditorFileDialog); |
| 446 | file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE); |
| 447 | add_child(file_dialog); |
| 448 | file_dialog->connect("file_selected", callable_mp(this, &EditorResourcePicker::_file_selected)); |
| 449 | } |
| 450 | |
| 451 | file_dialog->clear_filters(); |
| 452 | for (const String &E : valid_extensions) { |
| 453 | file_dialog->add_filter("*." + E, E.to_upper()); |
| 454 | } |
| 455 | |
| 456 | file_dialog->popup_file_dialog(); |
| 457 | } break; |
| 458 | |
| 459 | case OBJ_MENU_QUICKLOAD: { |
| 460 | const Vector<String> &base_types_string = base_type.split(","); |
| 461 | |
| 462 | Vector<StringName> base_types; |
| 463 | for (const String &type : base_types_string) { |
| 464 | base_types.push_back(type); |
| 465 | } |
| 466 | |
| 467 | EditorQuickOpenDialog *quick_open = EditorNode::get_singleton()->get_quick_open_dialog(); |
| 468 | if (resource_owner) { |
| 469 | quick_open->popup_dialog_for_property(base_types, resource_owner, property_path, callable_mp(this, &EditorResourcePicker::_file_selected)); |
| 470 | } else { |
| 471 | quick_open->popup_dialog(base_types, callable_mp(this, &EditorResourcePicker::_file_selected)); |
| 472 | } |
| 473 | |
| 474 | } break; |
| 475 | |
| 476 | case OBJ_MENU_INSPECT: { |
| 477 | if (edited_resource.is_valid()) { |
| 478 | emit_signal(SNAME("resource_selected"), edited_resource, true); |
| 479 | } |
| 480 | } break; |
| 481 | |
| 482 | case OBJ_MENU_CLEAR: { |
| 483 | edited_resource = Ref<Resource>(); |
| 484 | _resource_changed(); |
| 485 | } break; |
| 486 | |
| 487 | case OBJ_MENU_MAKE_UNIQUE: { |
| 488 | if (edited_resource.is_null()) { |
| 489 | return; |
| 490 | } |
| 491 | Ref<Resource> unique_resource = edited_resource->duplicate(); |
| 492 | ERR_FAIL_COND(unique_resource.is_null()); // duplicate() may fail. |
| 493 | |
| 494 | edited_resource = unique_resource; |
| 495 | _resource_changed(); |
| 496 | } break; |
| 497 | |
| 498 | case OBJ_MENU_MAKE_UNIQUE_RECURSIVE: { |
| 499 | if (edited_resource.is_null()) { |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | if (!duplicate_resources_dialog) { |
| 504 | duplicate_resources_dialog = memnew(ConfirmationDialog); |
| 505 | add_child(duplicate_resources_dialog); |
| 506 | duplicate_resources_dialog->set_title(TTR("Make Unique (Recursive)")); |
| 507 | duplicate_resources_dialog->connect(SceneStringName(confirmed), callable_mp(this, &EditorResourcePicker::_duplicate_selected_resources)); |
| 508 | |
| 509 | VBoxContainer *vb = memnew(VBoxContainer); |
| 510 | duplicate_resources_dialog->add_child(vb); |
| 511 | |
| 512 | Label *label = memnew(Label(TTR("Select resources to make unique:"))); |
| 513 | vb->add_child(label); |
| 514 | |
| 515 | duplicate_resources_tree = memnew(Tree); |
| 516 | duplicate_resources_tree->set_accessibility_name(TTRC("Duplicate resources")); |
| 517 | duplicate_resources_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 518 | vb->add_child(duplicate_resources_tree); |
| 519 | duplicate_resources_tree->set_columns(2); |
| 520 | duplicate_resources_tree->set_v_size_flags(SIZE_EXPAND_FILL); |
| 521 | } |
| 522 | |
| 523 | duplicate_resources_tree->clear(); |
| 524 | TreeItem *root = duplicate_resources_tree->create_item(); |
| 525 | _gather_resources_to_duplicate(edited_resource, root); |
| 526 | |
| 527 | duplicate_resources_dialog->reset_size(); |
| 528 | duplicate_resources_dialog->popup_centered(Vector2(500, 400) * EDSCALE); |
| 529 | } break; |
| 530 | |
| 531 | case OBJ_MENU_SAVE: { |
| 532 | if (edited_resource.is_null()) { |
| 533 | return; |
| 534 | } |
| 535 | EditorNode::get_singleton()->save_resource(edited_resource); |
| 536 | } break; |
| 537 | |
| 538 | case OBJ_MENU_SAVE_AS: { |
| 539 | if (edited_resource.is_null()) { |
| 540 | return; |
| 541 | } |
| 542 | Callable resource_saved = callable_mp(this, &EditorResourcePicker::_resource_saved); |
| 543 | if (!EditorNode::get_singleton()->is_connected("resource_saved", resource_saved)) { |
| 544 | EditorNode::get_singleton()->connect("resource_saved", resource_saved); |
| 545 | } |
| 546 | EditorNode::get_singleton()->save_resource_as(edited_resource); |
| 547 | } break; |
| 548 | |
| 549 | case OBJ_MENU_COPY: { |
| 550 | EditorSettings::get_singleton()->set_resource_clipboard(edited_resource); |
| 551 | } break; |
| 552 | |
| 553 | case OBJ_MENU_PASTE: { |
| 554 | edited_resource = EditorSettings::get_singleton()->get_resource_clipboard(); |
| 555 | bool make_unique = true; |
| 556 | |
| 557 | // Automatically make resource unique if it belongs to another scene or resource. |
| 558 | if (!EditorNode::get_singleton()->get_edited_scene() || !edited_resource->is_built_in() || edited_resource->get_path().get_slice("::", 0) == EditorNode::get_singleton()->get_edited_scene()->get_scene_file_path()) { |
| 559 | make_unique = false; |
| 560 | } else if (resource_owner) { |
| 561 | Resource *res = Object::cast_to<Resource>(resource_owner); |
| 562 | if (res && edited_resource->get_path().get_slice("::", 0) == res->get_path().get_slice("::", 0)) { |
| 563 | make_unique = false; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | if (make_unique) { |
| 568 | _edit_menu_cbk(OBJ_MENU_MAKE_UNIQUE); |
| 569 | } else { |
| 570 | _resource_changed(); |
| 571 | } |
| 572 | } break; |
| 573 | |
| 574 | case OBJ_MENU_PASTE_AS_UNIQUE: { |
| 575 | edited_resource = EditorSettings::get_singleton()->get_resource_clipboard(); |
| 576 | if (_has_sub_resources(edited_resource)) { |
| 577 | // Use the recursive version when the Resource has sub-resources. |
| 578 | // This will show up a dialog to select which resources to make unique. |
| 579 | _edit_menu_cbk(OBJ_MENU_MAKE_UNIQUE_RECURSIVE); |
| 580 | } else { |
| 581 | _edit_menu_cbk(OBJ_MENU_MAKE_UNIQUE); |
| 582 | } |
| 583 | } break; |
| 584 | |
| 585 | case OBJ_MENU_SHOW_IN_FILE_SYSTEM: { |
| 586 | FileSystemDock::get_singleton()->navigate_to_path(edited_resource->get_path()); |
| 587 | } break; |
| 588 | |
| 589 | default: { |
| 590 | // Allow subclasses to handle their own options first, only then fallback on the default branch logic. |
| 591 | if (handle_menu_selected(p_which)) { |
| 592 | break; |
| 593 | } |
| 594 | |
| 595 | if (p_which >= CONVERT_BASE_ID) { |
| 596 | int to_type = p_which - CONVERT_BASE_ID; |
| 597 | Vector<Ref<EditorResourceConversionPlugin>> conversions = EditorNode::get_singleton()->find_resource_conversion_plugin_for_resource(edited_resource); |
| 598 | ERR_FAIL_INDEX(to_type, conversions.size()); |
| 599 | |
| 600 | Ref<Resource> converted_resource = conversions[to_type]->convert(edited_resource); |
| 601 | if (converted_resource.is_valid()) { |
| 602 | edited_resource = converted_resource; |
| 603 | _resource_changed(); |
| 604 | } |
| 605 | break; |
| 606 | } |
| 607 | |
| 608 | ERR_FAIL_COND(inheritors_array.is_empty()); |
| 609 | |
| 610 | String intype = inheritors_array[p_which - TYPE_BASE_ID]; |
| 611 | Variant obj; |
| 612 | |
| 613 | if (ScriptServer::is_global_class(intype)) { |
| 614 | obj = EditorNode::get_editor_data().script_class_instance(intype); |
| 615 | } else { |
| 616 | obj = ClassDB::instantiate(intype); |
| 617 | } |
| 618 | |
| 619 | if (!obj) { |
| 620 | obj = EditorNode::get_editor_data().instantiate_custom_type(intype, "Resource"); |
| 621 | } |
| 622 | |
| 623 | Resource *resp = Object::cast_to<Resource>(obj); |
| 624 | ERR_BREAK(!resp); |
| 625 | resp->set_path(_get_owner_path() + "::" + resp->generate_scene_unique_id()); // Assign a base path for built-in Resources. |
| 626 | |
| 627 | EditorNode::get_editor_data().instantiate_object_properties(obj); |
| 628 | |
| 629 | // Prevent freeing of the object until the end of the update of the resource (GH-88286). |
| 630 | Ref<Resource> old_edited_resource = edited_resource; |
| 631 | edited_resource = Ref<Resource>(resp); |
| 632 | _resource_changed(); |
| 633 | } break; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | void EditorResourcePicker::set_create_options(Object *p_menu_node) { |
| 638 | _ensure_resource_menu(); |
| 639 | // If a subclass implements this method, use it to replace all create items. |
| 640 | if (GDVIRTUAL_CALL(_set_create_options, p_menu_node)) { |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | // By default provide generic "New ..." options. |
| 645 | if (!base_type.is_empty()) { |
| 646 | int idx = 0; |
| 647 | |
| 648 | _ensure_allowed_types(); |
| 649 | HashSet<StringName> allowed_types = allowed_types_without_convert; |
| 650 | if (!allowed_types.is_empty()) { |
| 651 | edit_menu->add_separator(TTRC("New")); |
| 652 | } |
| 653 | |
| 654 | for (const StringName &E : allowed_types) { |
| 655 | const String &t = E; |
| 656 | |
| 657 | if (!ClassDB::can_instantiate(t)) { |
| 658 | continue; |
| 659 | } |
| 660 | |
| 661 | inheritors_array.push_back(t); |
| 662 | |
| 663 | Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(t); |
| 664 | int id = TYPE_BASE_ID + idx; |
| 665 | edit_menu->add_icon_item(icon, t, id); |
| 666 | edit_menu->set_item_auto_translate_mode(-1, AUTO_TRANSLATE_MODE_DISABLED); |
| 667 | |
| 668 | HashMap<String, DocData::ClassDoc>::Iterator class_doc = EditorHelp::get_doc_data()->class_list.find(t); |
| 669 | if (class_doc) { |
| 670 | edit_menu->set_item_tooltip(-1, DTR(class_doc->value.brief_description)); |
| 671 | } |
| 672 | |
| 673 | idx++; |
| 674 | } |
| 675 | |
| 676 | if (edit_menu->get_item_count()) { |
| 677 | edit_menu->add_separator(); |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | bool EditorResourcePicker::handle_menu_selected(int p_which) { |
| 683 | bool success = false; |
| 684 | GDVIRTUAL_CALL(_handle_menu_selected, p_which, success); |
| 685 | return success; |
| 686 | } |
| 687 | |
| 688 | void EditorResourcePicker::_button_draw() { |
| 689 | if (dropping) { |
| 690 | Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor)); |
| 691 | assign_button->draw_rect(Rect2(Point2(), assign_button->get_size()), color, false); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | void EditorResourcePicker::_button_input(const Ref<InputEvent> &p_event) { |
| 696 | Ref<InputEventMouseButton> mb = p_event; |
| 697 | |
| 698 | if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) { |
| 699 | // Only attempt to update and show the menu if we have |
| 700 | // a valid resource or the Picker is editable, as |
| 701 | // there will otherwise be nothing to display. |
| 702 | if (edited_resource.is_valid() || is_editable()) { |
| 703 | if (edit_menu && edit_menu->is_visible()) { |
| 704 | edit_button->set_pressed(false); |
| 705 | edit_menu->hide(); |
| 706 | return; |
| 707 | } |
| 708 | |
| 709 | _update_menu_items(); |
| 710 | |
| 711 | Vector2 pos = get_screen_position() + mb->get_position(); |
| 712 | edit_menu->reset_size(); |
| 713 | edit_menu->set_position(pos); |
| 714 | edit_menu->popup(); |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | void EditorResourcePicker::_on_unique_button_pressed() { |
| 720 | if (Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) { |
| 721 | _edit_menu_cbk(OBJ_MENU_MAKE_UNIQUE); |
| 722 | } else if (Input::get_singleton()->is_mouse_button_pressed(MouseButton::RIGHT) && _is_uniqueness_enabled(true)) { |
| 723 | _edit_menu_cbk(OBJ_MENU_MAKE_UNIQUE_RECURSIVE); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | String EditorResourcePicker::_get_owner_path() const { |
| 728 | EditorProperty *property = Object::cast_to<EditorProperty>(get_parent()); |
| 729 | if (!property) { |
| 730 | return String(); |
| 731 | } |
| 732 | Object *obj = property->get_edited_object(); |
| 733 | |
| 734 | Node *node = Object::cast_to<Node>(obj); |
| 735 | if (node) { |
| 736 | Node *p_edited_scene_root = EditorNode::get_singleton()->get_editor_data().get_edited_scene_root(); |
| 737 | if (node->get_scene_file_path().is_empty()) { |
| 738 | node = node->get_owner(); |
| 739 | } else if (p_edited_scene_root != nullptr && p_edited_scene_root->get_scene_file_path() != node->get_scene_file_path()) { |
| 740 | // PackedScene should use root scene path. |
| 741 | return p_edited_scene_root->get_scene_file_path(); |
| 742 | } |
| 743 | if (node) { |
| 744 | return node->get_scene_file_path(); |
| 745 | } |
| 746 | return String(); |
| 747 | } |
| 748 | |
| 749 | Resource *res = Object::cast_to<Resource>(obj); |
| 750 | if (res && !res->is_built_in()) { |
| 751 | return res->get_path(); |
| 752 | } |
| 753 | // TODO: It would be nice to handle deeper Resource nesting. |
| 754 | return String(); |
| 755 | } |
| 756 | |
| 757 | String EditorResourcePicker::_get_resource_type(const Ref<Resource> &p_resource) const { |
| 758 | if (p_resource.is_null()) { |
| 759 | return String(); |
| 760 | } |
| 761 | String res_type = p_resource->get_class(); |
| 762 | |
| 763 | Ref<Script> res_script = p_resource->get_script(); |
| 764 | if (res_script.is_null()) { |
| 765 | return res_type; |
| 766 | } |
| 767 | |
| 768 | // TODO: Replace with EditorFileSystem when PR #60606 is merged to use cached resource type. |
| 769 | String script_type = EditorNode::get_editor_data().script_class_get_name(res_script->get_path()); |
| 770 | if (!script_type.is_empty()) { |
| 771 | res_type = script_type; |
| 772 | } |
| 773 | return res_type; |
| 774 | } |
| 775 | |
| 776 | static bool _should_hide_type(const StringName &p_type) { |
| 777 | if (ClassDB::is_virtual(p_type)) { |
| 778 | return true; |
| 779 | } |
| 780 | |
| 781 | if (p_type == SNAME("MissingResource")) { |
| 782 | return true; |
| 783 | } |
| 784 | |
| 785 | return false; |
| 786 | } |
| 787 | |
| 788 | static void _add_allowed_type(const StringName &p_type, List<StringName> *p_vector) { |
| 789 | if (p_vector->find(p_type)) { |
| 790 | // Already added. |
| 791 | return; |
| 792 | } |
| 793 | |
| 794 | if (ClassDB::class_exists(p_type)) { |
| 795 | // Engine class. |
| 796 | |
| 797 | if (!_should_hide_type(p_type)) { |
| 798 | p_vector->push_back(p_type); |
| 799 | } |
| 800 | |
| 801 | LocalVector<StringName> inheriters; |
| 802 | ClassDB::get_inheriters_from_class(p_type, inheriters); |
| 803 | for (const StringName &S : inheriters) { |
| 804 | _add_allowed_type(S, p_vector); |
| 805 | } |
| 806 | } else { |
| 807 | // Script class. |
| 808 | p_vector->push_back(p_type); |
| 809 | } |
| 810 | |
| 811 | List<StringName> inheriters; |
| 812 | ScriptServer::get_inheriters_list(p_type, &inheriters); |
| 813 | for (const StringName &S : inheriters) { |
| 814 | _add_allowed_type(S, p_vector); |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | void EditorResourcePicker::_ensure_allowed_types() const { |
| 819 | if (!allowed_types_without_convert.is_empty()) { |
| 820 | return; |
| 821 | } |
| 822 | |
| 823 | List<StringName> final_allowed; |
| 824 | |
| 825 | Vector<String> allowed_types = base_type.split(","); |
| 826 | int size = allowed_types.size(); |
| 827 | |
| 828 | for (const String &S : allowed_types) { |
| 829 | const String base = S.strip_edges(); |
| 830 | if (base.begins_with("-")) { |
| 831 | final_allowed.erase(base.right(-1)); |
| 832 | continue; |
| 833 | } |
| 834 | _add_allowed_type(base, &final_allowed); |
| 835 | } |
| 836 | |
| 837 | for (const StringName &SN : final_allowed) { |
| 838 | allowed_types_without_convert.insert(SN); |
| 839 | } |
| 840 | |
| 841 | allowed_types_with_convert = HashSet<StringName>(allowed_types_without_convert); |
| 842 | |
| 843 | for (int i = 0; i < size; i++) { |
| 844 | const String base = allowed_types[i].strip_edges(); |
| 845 | if (base == "BaseMaterial3D") { |
| 846 | allowed_types_with_convert.insert("Texture2D"); |
| 847 | } else if (ClassDB::is_parent_class("ShaderMaterial", base)) { |
| 848 | allowed_types_with_convert.insert("Shader"); |
| 849 | } else if (ClassDB::is_parent_class("ImageTexture", base)) { |
| 850 | allowed_types_with_convert.insert("Image"); |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | bool EditorResourcePicker::_is_drop_valid(const Dictionary &p_drag_data) const { |
| 856 | { |
| 857 | const ObjectID source_picker = p_drag_data.get("source_picker", ObjectID()); |
| 858 | if (source_picker == get_instance_id()) { |
| 859 | return false; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | if (base_type.is_empty()) { |
| 864 | return true; |
| 865 | } |
| 866 | |
| 867 | Dictionary drag_data = p_drag_data; |
| 868 | |
| 869 | Ref<Resource> res; |
| 870 | if (drag_data.has("type") && String(drag_data["type"]) == "script_list_element") { |
| 871 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(drag_data["script_list_element"]); |
| 872 | if (se) { |
| 873 | res = se->get_edited_resource(); |
| 874 | } |
| 875 | } else if (drag_data.has("type") && String(drag_data["type"]) == "resource") { |
| 876 | res = drag_data["resource"]; |
| 877 | } else if (drag_data.has("type") && String(drag_data["type"]) == "files") { |
| 878 | Vector<String> files = drag_data["files"]; |
| 879 | |
| 880 | if (files.size() == 1) { |
| 881 | if (ResourceLoader::exists(files[0])) { |
| 882 | // TODO: Extract the typename of the dropped filepath's resource in a more performant way, without fully loading it. |
| 883 | res = ResourceLoader::load(files[0]); |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | _ensure_allowed_types(); |
| 889 | HashSet<StringName> allowed_types = allowed_types_with_convert; |
| 890 | |
| 891 | if (res.is_valid()) { |
| 892 | String res_type = _get_resource_type(res); |
| 893 | |
| 894 | if (_is_type_valid(res_type, allowed_types)) { |
| 895 | return true; |
| 896 | } |
| 897 | |
| 898 | if (res->get_script()) { |
| 899 | StringName custom_class = EditorNode::get_singleton()->get_object_custom_type_name(res->get_script()); |
| 900 | if (_is_type_valid(custom_class, allowed_types)) { |
| 901 | return true; |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | return false; |
| 907 | } |
| 908 | |
| 909 | bool EditorResourcePicker::_is_type_valid(const String &p_type_name, const HashSet<StringName> &p_allowed_types) const { |
| 910 | for (const StringName &E : p_allowed_types) { |
| 911 | String at = E; |
| 912 | if (p_type_name == at || ClassDB::is_parent_class(p_type_name, at) || EditorNode::get_editor_data().script_class_is_parent(p_type_name, at)) { |
| 913 | return true; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | return false; |
| 918 | } |
| 919 | |
| 920 | bool EditorResourcePicker::_is_custom_type_script() const { |
| 921 | EditorProperty *editor_property = Object::cast_to<EditorProperty>(get_parent()); |
| 922 | if (!editor_property) { |
| 923 | return false; |
| 924 | } |
| 925 | |
| 926 | // Check if the property being edited is 'script'. |
| 927 | if (editor_property->get_edited_property() == CoreStringName(script)) { |
| 928 | // If there's currently a valid script assigned and the owning Node/Resource also has a custom type script assigned, then |
| 929 | // the currently assigned script is either the custom type script itself or an extension of it. |
| 930 | Ref<Script> resource_as_script = edited_resource; |
| 931 | if (resource_as_script.is_valid() && resource_owner && resource_owner->has_meta(SceneStringName(_custom_type_script))) { |
| 932 | return true; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | return false; |
| 937 | } |
| 938 | |
| 939 | Variant EditorResourcePicker::get_drag_data_fw(const Point2 &p_point, Control *p_from) { |
| 940 | if (edited_resource.is_valid()) { |
| 941 | Dictionary drag_data = EditorNode::get_singleton()->drag_resource(edited_resource, p_from); |
| 942 | drag_data["source_picker"] = get_instance_id(); |
| 943 | return drag_data; |
| 944 | } |
| 945 | |
| 946 | return Variant(); |
| 947 | } |
| 948 | |
| 949 | bool EditorResourcePicker::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const { |
| 950 | return editable && _is_drop_valid(p_data); |
| 951 | } |
| 952 | |
| 953 | void EditorResourcePicker::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { |
| 954 | ERR_FAIL_COND(!_is_drop_valid(p_data)); |
| 955 | |
| 956 | Dictionary drag_data = p_data; |
| 957 | |
| 958 | Ref<Resource> dropped_resource; |
| 959 | if (drag_data.has("type") && String(drag_data["type"]) == "script_list_element") { |
| 960 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(drag_data["script_list_element"]); |
| 961 | if (se) { |
| 962 | dropped_resource = se->get_edited_resource(); |
| 963 | } |
| 964 | } else if (drag_data.has("type") && String(drag_data["type"]) == "resource") { |
| 965 | dropped_resource = drag_data["resource"]; |
| 966 | } |
| 967 | |
| 968 | if (dropped_resource.is_null() && drag_data.has("type") && String(drag_data["type"]) == "files") { |
| 969 | Vector<String> files = drag_data["files"]; |
| 970 | |
| 971 | if (files.size() == 1) { |
| 972 | dropped_resource = ResourceLoader::load(files[0]); |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | if (dropped_resource.is_valid()) { |
| 977 | _ensure_allowed_types(); |
| 978 | HashSet<StringName> allowed_types = allowed_types_without_convert; |
| 979 | |
| 980 | String res_type = _get_resource_type(dropped_resource); |
| 981 | |
| 982 | // If the accepted dropped resource is from the extended list, it requires conversion. |
| 983 | if (!_is_type_valid(res_type, allowed_types)) { |
| 984 | for (const StringName &E : allowed_types) { |
| 985 | String at = E; |
| 986 | |
| 987 | if (at == "BaseMaterial3D" && Ref<Texture2D>(dropped_resource).is_valid()) { |
| 988 | // Use existing resource if possible and only replace its data. |
| 989 | Ref<StandardMaterial3D> mat = edited_resource; |
| 990 | if (mat.is_null()) { |
| 991 | mat.instantiate(); |
| 992 | } |
| 993 | mat->set_texture(StandardMaterial3D::TextureParam::TEXTURE_ALBEDO, dropped_resource); |
| 994 | dropped_resource = mat; |
| 995 | break; |
| 996 | } |
| 997 | |
| 998 | if (at == "ShaderMaterial" && Ref<Shader>(dropped_resource).is_valid()) { |
| 999 | Ref<ShaderMaterial> mat = edited_resource; |
| 1000 | if (mat.is_null()) { |
| 1001 | mat.instantiate(); |
| 1002 | } |
| 1003 | mat->set_shader(dropped_resource); |
| 1004 | dropped_resource = mat; |
| 1005 | break; |
| 1006 | } |
| 1007 | |
| 1008 | if (at == "ImageTexture" && Ref<Image>(dropped_resource).is_valid()) { |
| 1009 | Ref<ImageTexture> texture = edited_resource; |
| 1010 | if (texture.is_null()) { |
| 1011 | texture.instantiate(); |
| 1012 | } |
| 1013 | texture->set_image(dropped_resource); |
| 1014 | dropped_resource = texture; |
| 1015 | break; |
| 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | edited_resource = dropped_resource; |
| 1021 | |
| 1022 | if (Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) { |
| 1023 | // `_edit_menu_cbk(OBJ_MENU_MAKE_UNIQUE)` already calls `_resource_changed()`, |
| 1024 | // so we don't need to manually call it in this case. |
| 1025 | _edit_menu_cbk(OBJ_MENU_MAKE_UNIQUE); |
| 1026 | } else { |
| 1027 | _resource_changed(); |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | void EditorResourcePicker::_bind_methods() { |
| 1033 | ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &EditorResourcePicker::set_base_type); |
| 1034 | ClassDB::bind_method(D_METHOD("get_base_type"), &EditorResourcePicker::get_base_type); |
| 1035 | ClassDB::bind_method(D_METHOD("get_allowed_types"), &EditorResourcePicker::get_allowed_types); |
| 1036 | ClassDB::bind_method(D_METHOD("set_edited_resource", "resource"), &EditorResourcePicker::set_edited_resource); |
| 1037 | ClassDB::bind_method(D_METHOD("get_edited_resource"), &EditorResourcePicker::get_edited_resource); |
| 1038 | ClassDB::bind_method(D_METHOD("set_toggle_mode", "enable"), &EditorResourcePicker::set_toggle_mode); |
| 1039 | ClassDB::bind_method(D_METHOD("is_toggle_mode"), &EditorResourcePicker::is_toggle_mode); |
| 1040 | ClassDB::bind_method(D_METHOD("set_toggle_pressed", "pressed"), &EditorResourcePicker::set_toggle_pressed); |
| 1041 | ClassDB::bind_method(D_METHOD("set_editable", "enable"), &EditorResourcePicker::set_editable); |
| 1042 | ClassDB::bind_method(D_METHOD("is_editable"), &EditorResourcePicker::is_editable); |
| 1043 | |
| 1044 | GDVIRTUAL_BIND(_set_create_options, "menu_node"); |
| 1045 | GDVIRTUAL_BIND(_handle_menu_selected, "id"); |
| 1046 | |
| 1047 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type"), "set_base_type", "get_base_type"); |
| 1048 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "edited_resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource", PROPERTY_USAGE_NONE), "set_edited_resource", "get_edited_resource"); |
| 1049 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable"); |
| 1050 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode"); |
| 1051 | |
| 1052 | ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), PropertyInfo(Variant::BOOL, "inspect"))); |
| 1053 | ADD_SIGNAL(MethodInfo("resource_changed", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"))); |
| 1054 | } |
| 1055 | |
| 1056 | void EditorResourcePicker::_notification(int p_what) { |
| 1057 | switch (p_what) { |
| 1058 | case NOTIFICATION_ENTER_TREE: { |
| 1059 | EditorNode::get_singleton()->connect("resource_counter_changed", callable_mp(this, &EditorResourcePicker::_update_resource)); |
| 1060 | _update_resource(); |
| 1061 | [[fallthrough]]; |
| 1062 | } |
| 1063 | case NOTIFICATION_THEME_CHANGED: { |
| 1064 | const int icon_width = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)); |
| 1065 | assign_button->add_theme_constant_override("icon_max_width", icon_width); |
| 1066 | make_unique_button->add_theme_constant_override("icon_max_width", icon_width); |
| 1067 | if (edit_menu) { |
| 1068 | edit_menu->add_theme_constant_override("icon_max_width", icon_width); |
| 1069 | } |
| 1070 | |
| 1071 | quick_load_button->set_button_icon(get_editor_theme_icon(SNAME("LoadQuick"))); |
| 1072 | edit_button->set_button_icon(get_theme_icon(SNAME("select_arrow"), SNAME("Tree"))); |
| 1073 | } break; |
| 1074 | |
| 1075 | case NOTIFICATION_DRAW: { |
| 1076 | draw_style_box(get_theme_stylebox(SceneStringName(panel), SNAME("Tree")), Rect2(Point2(), get_size())); |
| 1077 | } break; |
| 1078 | |
| 1079 | case NOTIFICATION_SORT_CHILDREN: { |
| 1080 | _update_resource(); |
| 1081 | } break; |
| 1082 | |
| 1083 | case NOTIFICATION_DRAG_BEGIN: { |
| 1084 | if (editable && _is_drop_valid(get_viewport()->gui_get_drag_data())) { |
| 1085 | dropping = true; |
| 1086 | assign_button->queue_redraw(); |
| 1087 | } |
| 1088 | } break; |
| 1089 | |
| 1090 | case NOTIFICATION_DRAG_END: { |
| 1091 | if (dropping) { |
| 1092 | dropping = false; |
| 1093 | assign_button->queue_redraw(); |
| 1094 | } |
| 1095 | } break; |
| 1096 | |
| 1097 | case NOTIFICATION_EXIT_TREE: { |
| 1098 | Callable resource_saved = callable_mp(this, &EditorResourcePicker::_resource_saved); |
| 1099 | Callable resource_counter_changed = callable_mp(this, &EditorResourcePicker::_update_resource); |
| 1100 | if (EditorNode::get_singleton()->is_connected("resource_saved", resource_saved)) { |
| 1101 | EditorNode::get_singleton()->disconnect("resource_saved", resource_saved); |
| 1102 | } |
| 1103 | if (EditorNode::get_singleton()->is_connected("resource_counter_changed", resource_counter_changed)) { |
| 1104 | EditorNode::get_singleton()->disconnect("resource_counter_changed", resource_counter_changed); |
| 1105 | } |
| 1106 | } break; |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | void EditorResourcePicker::set_assign_button_min_size(const Size2i &p_size) { |
| 1111 | assign_button_min_size = p_size; |
| 1112 | assign_button->set_custom_minimum_size(assign_button_min_size); |
| 1113 | } |
| 1114 | |
| 1115 | void EditorResourcePicker::set_base_type(const String &p_base_type) { |
| 1116 | base_type = p_base_type; |
| 1117 | |
| 1118 | // There is a possibility that the new base type is conflicting with the existing value. |
| 1119 | // Keep the value, but warn the user that there is a potential mistake. |
| 1120 | if (!base_type.is_empty() && edited_resource.is_valid()) { |
| 1121 | _ensure_allowed_types(); |
| 1122 | HashSet<StringName> allowed_types = allowed_types_with_convert; |
| 1123 | |
| 1124 | StringName custom_class; |
| 1125 | bool is_custom = false; |
| 1126 | if (edited_resource->get_script()) { |
| 1127 | custom_class = EditorNode::get_singleton()->get_object_custom_type_name(edited_resource->get_script()); |
| 1128 | is_custom = _is_type_valid(custom_class, allowed_types); |
| 1129 | } |
| 1130 | |
| 1131 | if (!is_custom && !_is_type_valid(edited_resource->get_class(), allowed_types)) { |
| 1132 | String class_str = (custom_class == StringName() ? edited_resource->get_class() : vformat("%s (%s)", custom_class, edited_resource->get_class())); |
| 1133 | WARN_PRINT(vformat("Value mismatch between the new base type of this EditorResourcePicker, '%s', and the type of the value it already has, '%s'.", base_type, class_str)); |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | String EditorResourcePicker::get_base_type() const { |
| 1139 | return base_type; |
| 1140 | } |
| 1141 | |
| 1142 | Vector<String> EditorResourcePicker::get_allowed_types() const { |
| 1143 | _ensure_allowed_types(); |
| 1144 | HashSet<StringName> allowed_types = allowed_types_without_convert; |
| 1145 | |
| 1146 | Vector<String> types; |
| 1147 | types.resize(allowed_types.size()); |
| 1148 | |
| 1149 | int i = 0; |
| 1150 | String *w = types.ptrw(); |
| 1151 | for (const StringName &E : allowed_types) { |
| 1152 | w[i] = E; |
| 1153 | i++; |
| 1154 | } |
| 1155 | |
| 1156 | return types; |
| 1157 | } |
| 1158 | |
| 1159 | bool EditorResourcePicker::is_resource_allowed(const Ref<Resource> &p_resource) { |
| 1160 | if (p_resource.is_null()) { |
| 1161 | return true; |
| 1162 | } |
| 1163 | |
| 1164 | if (!base_type.is_empty()) { |
| 1165 | _ensure_allowed_types(); |
| 1166 | HashSet<StringName> allowed_types = allowed_types_with_convert; |
| 1167 | |
| 1168 | StringName custom_class; |
| 1169 | bool is_custom = false; |
| 1170 | if (p_resource->get_script()) { |
| 1171 | custom_class = EditorNode::get_singleton()->get_object_custom_type_name(p_resource->get_script()); |
| 1172 | is_custom = _is_type_valid(custom_class, allowed_types); |
| 1173 | } |
| 1174 | |
| 1175 | if (!is_custom && !_is_type_valid(p_resource->get_class(), allowed_types)) { |
| 1176 | return false; |
| 1177 | } |
| 1178 | } |
| 1179 | return true; |
| 1180 | } |
| 1181 | |
| 1182 | void EditorResourcePicker::set_edited_resource(Ref<Resource> p_resource) { |
| 1183 | if (!is_resource_allowed(p_resource)) { |
| 1184 | StringName custom_class; |
| 1185 | if (p_resource->get_script()) { |
| 1186 | custom_class = EditorNode::get_singleton()->get_object_custom_type_name(p_resource->get_script()); |
| 1187 | } |
| 1188 | const String class_str = (custom_class.is_empty() ? p_resource->get_class() : vformat("%s (%s)", custom_class, p_resource->get_class())); |
| 1189 | ERR_FAIL_MSG(vformat("Failed to set a resource of the type '%s' because this EditorResourcePicker only accepts '%s' and its derivatives.", class_str, base_type)); |
| 1190 | } |
| 1191 | set_edited_resource_no_check(p_resource); |
| 1192 | } |
| 1193 | |
| 1194 | void EditorResourcePicker::set_edited_resource_no_check(Ref<Resource> p_resource) { |
| 1195 | edited_resource = p_resource; |
| 1196 | _update_resource(); |
| 1197 | } |
| 1198 | |
| 1199 | Ref<Resource> EditorResourcePicker::get_edited_resource() { |
| 1200 | return edited_resource; |
| 1201 | } |
| 1202 | |
| 1203 | void EditorResourcePicker::set_toggle_mode(bool p_enable) { |
| 1204 | assign_button->set_toggle_mode(p_enable); |
| 1205 | } |
| 1206 | |
| 1207 | bool EditorResourcePicker::is_toggle_mode() const { |
| 1208 | return assign_button->is_toggle_mode(); |
| 1209 | } |
| 1210 | |
| 1211 | void EditorResourcePicker::set_toggle_pressed(bool p_pressed) { |
| 1212 | if (!is_toggle_mode()) { |
| 1213 | return; |
| 1214 | } |
| 1215 | |
| 1216 | assign_button->set_pressed(p_pressed); |
| 1217 | } |
| 1218 | |
| 1219 | bool EditorResourcePicker::is_toggle_pressed() const { |
| 1220 | return assign_button->is_pressed(); |
| 1221 | } |
| 1222 | |
| 1223 | void EditorResourcePicker::set_resource_owner(Object *p_object) { |
| 1224 | resource_owner = p_object; |
| 1225 | } |
| 1226 | |
| 1227 | void EditorResourcePicker::set_editable(bool p_editable) { |
| 1228 | editable = p_editable; |
| 1229 | assign_button->set_disabled(!editable && edited_resource.is_null()); |
| 1230 | quick_load_button->set_visible(editable && edited_resource.is_null()); |
| 1231 | edit_button->set_visible(editable); |
| 1232 | } |
| 1233 | |
| 1234 | bool EditorResourcePicker::is_editable() const { |
| 1235 | return editable; |
| 1236 | } |
| 1237 | |
| 1238 | void EditorResourcePicker::_ensure_resource_menu() { |
| 1239 | if (edit_menu) { |
| 1240 | return; |
| 1241 | } |
| 1242 | edit_menu = memnew(PopupMenu); |
| 1243 | edit_menu->add_theme_constant_override("icon_max_width", get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor))); |
| 1244 | add_child(edit_menu); |
| 1245 | edit_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorResourcePicker::_edit_menu_cbk)); |
| 1246 | edit_menu->connect("popup_hide", callable_mp((BaseButton *)edit_button, &BaseButton::set_pressed).bind(false)); |
| 1247 | } |
| 1248 | |
| 1249 | void EditorResourcePicker::_gather_resources_to_duplicate(const Ref<Resource> p_resource, TreeItem *p_item, const String &p_property_name) const { |
| 1250 | p_item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK); |
| 1251 | |
| 1252 | String res_name = p_resource->get_name(); |
| 1253 | if (res_name.is_empty() && !p_resource->is_built_in()) { |
| 1254 | res_name = p_resource->get_path().get_file(); |
| 1255 | } |
| 1256 | |
| 1257 | if (res_name.is_empty()) { |
| 1258 | p_item->set_text(0, _get_resource_type(p_resource)); |
| 1259 | } else { |
| 1260 | p_item->set_text(0, vformat("%s (%s)", _get_resource_type(p_resource), res_name)); |
| 1261 | } |
| 1262 | |
| 1263 | p_item->set_icon(0, EditorNode::get_singleton()->get_object_icon(p_resource.ptr())); |
| 1264 | p_item->set_editable(0, true); |
| 1265 | |
| 1266 | Array meta = { p_resource }; |
| 1267 | p_item->set_metadata(0, meta); |
| 1268 | |
| 1269 | if (!p_property_name.is_empty()) { |
| 1270 | p_item->set_text(1, p_property_name); |
| 1271 | } |
| 1272 | |
| 1273 | static Vector<String> unique_exceptions = { "Image", "Shader", "Mesh", "FontFile" }; |
| 1274 | if (!unique_exceptions.has(p_resource->get_class())) { |
| 1275 | // Automatically select resource, unless it's something that shouldn't be duplicated. |
| 1276 | p_item->set_checked(0, true); |
| 1277 | } |
| 1278 | |
| 1279 | List<PropertyInfo> plist; |
| 1280 | p_resource->get_property_list(&plist); |
| 1281 | |
| 1282 | for (const PropertyInfo &E : plist) { |
| 1283 | if (!(E.usage & PROPERTY_USAGE_STORAGE) || (E.type != Variant::OBJECT && E.type != Variant::ARRAY && E.type != Variant::DICTIONARY)) { |
| 1284 | continue; |
| 1285 | } |
| 1286 | |
| 1287 | Variant value = p_resource->get(E.name); |
| 1288 | TreeItem *child = nullptr; |
| 1289 | |
| 1290 | if (E.type == Variant::ARRAY) { |
| 1291 | Array arr = value; |
| 1292 | for (int i = 0; i < arr.size(); i++) { |
| 1293 | Ref<Resource> res = arr[i]; |
| 1294 | if (res.is_valid()) { |
| 1295 | child = p_item->create_child(); |
| 1296 | _gather_resources_to_duplicate(res, child, E.name); |
| 1297 | meta = child->get_metadata(0); |
| 1298 | meta.push_back(E.name); |
| 1299 | meta.push_back(i); // Remember index. |
| 1300 | } |
| 1301 | } |
| 1302 | continue; |
| 1303 | } else if (E.type == Variant::DICTIONARY) { |
| 1304 | Dictionary dict = value; |
| 1305 | for (const KeyValue<Variant, Variant> &kv : dict) { |
| 1306 | Ref<Resource> key_res = kv.key; |
| 1307 | Ref<Resource> value_res = kv.value; |
| 1308 | if (key_res.is_valid()) { |
| 1309 | child = p_item->create_child(); |
| 1310 | _gather_resources_to_duplicate(key_res, child, E.name); |
| 1311 | meta = child->get_metadata(0); |
| 1312 | meta.push_back(E.name); |
| 1313 | meta.push_back(key_res); |
| 1314 | } |
| 1315 | if (value_res.is_valid()) { |
| 1316 | child = p_item->create_child(); |
| 1317 | _gather_resources_to_duplicate(value_res, child, E.name); |
| 1318 | meta = child->get_metadata(0); |
| 1319 | meta.push_back(E.name); |
| 1320 | meta.push_back(value_res); |
| 1321 | meta.push_back(kv.key); |
| 1322 | } |
| 1323 | } |
| 1324 | continue; |
| 1325 | } |
| 1326 | |
| 1327 | Ref<Resource> res = p_resource->get(E.name); |
| 1328 | if (res.is_null()) { |
| 1329 | continue; |
| 1330 | } |
| 1331 | child = p_item->create_child(); |
| 1332 | _gather_resources_to_duplicate(res, child, E.name); |
| 1333 | meta = child->get_metadata(0); |
| 1334 | // Remember property name. |
| 1335 | meta.push_back(E.name); |
| 1336 | |
| 1337 | if ((E.usage & PROPERTY_USAGE_NEVER_DUPLICATE)) { |
| 1338 | // The resource can't be duplicated, but make it appear on the list anyway. |
| 1339 | child->set_checked(0, false); |
| 1340 | child->set_editable(0, false); |
| 1341 | } |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | void EditorResourcePicker::_duplicate_selected_resources() { |
| 1346 | for (TreeItem *item = duplicate_resources_tree->get_root(); item; item = item->get_next_in_tree()) { |
| 1347 | if (!item->is_checked(0)) { |
| 1348 | continue; |
| 1349 | } |
| 1350 | |
| 1351 | Array meta = item->get_metadata(0); |
| 1352 | Ref<Resource> res = meta[0]; |
| 1353 | Ref<Resource> unique_resource = res->duplicate(); |
| 1354 | ERR_FAIL_COND(unique_resource.is_null()); // duplicate() may fail. |
| 1355 | meta[0] = unique_resource; |
| 1356 | |
| 1357 | if (meta.size() == 1) { // Root. |
| 1358 | edited_resource = unique_resource; |
| 1359 | continue; |
| 1360 | } |
| 1361 | Array parent_meta = item->get_parent()->get_metadata(0); |
| 1362 | Ref<Resource> parent = parent_meta[0]; |
| 1363 | Variant::Type property_type = parent->get(meta[1]).get_type(); |
| 1364 | |
| 1365 | if (property_type == Variant::OBJECT) { |
| 1366 | parent->set(meta[1], unique_resource); |
| 1367 | continue; |
| 1368 | } |
| 1369 | |
| 1370 | Variant property = parent->get(meta[1]); |
| 1371 | |
| 1372 | if (!parent_meta.has(property)) { |
| 1373 | property = property.duplicate(); |
| 1374 | parent->set(meta[1], property); |
| 1375 | parent_meta.push_back(property); // Append Duplicated Type so we can check if it's already been duplicated. |
| 1376 | } |
| 1377 | |
| 1378 | if (property_type == Variant::ARRAY) { |
| 1379 | Array arr = property; |
| 1380 | arr[meta[2]] = unique_resource; |
| 1381 | continue; |
| 1382 | } |
| 1383 | |
| 1384 | Dictionary dict = property; |
| 1385 | LocalVector<Variant> keys = dict.get_key_list(); |
| 1386 | |
| 1387 | if (meta[2].get_type() == Variant::OBJECT) { |
| 1388 | if (keys.has(meta[2])) { |
| 1389 | //It's a key. |
| 1390 | dict[unique_resource] = dict[meta[2]]; |
| 1391 | dict.erase(meta[2]); |
| 1392 | parent_meta.push_back(unique_resource); |
| 1393 | } else { |
| 1394 | // If key has been erased, use last appended Resource key instead. |
| 1395 | Variant key = keys.has(meta[3]) ? meta[3] : parent_meta.back(); |
| 1396 | dict[key] = unique_resource; |
| 1397 | } |
| 1398 | } else { |
| 1399 | dict[meta[2]] = unique_resource; |
| 1400 | } |
| 1401 | } |
| 1402 | _resource_changed(); |
| 1403 | } |
| 1404 | |
| 1405 | bool EditorResourcePicker::_is_uniqueness_enabled(bool p_check_recursive) { |
| 1406 | if (force_allow_unique) { |
| 1407 | return true; |
| 1408 | } |
| 1409 | Ref<Resource> parent_resource = _has_parent_resource(); |
| 1410 | EditorNode *en = EditorNode::get_singleton(); |
| 1411 | bool internal_to_scene = en->is_resource_internal_to_scene(edited_resource); |
| 1412 | List<Node *> node_list = en->get_editor_selection()->get_full_selected_node_list(); |
| 1413 | |
| 1414 | // Todo: Implement a more elegant solution for multiple selected Nodes. This should suffice for the time being. |
| 1415 | if (node_list.size() > 1 && !p_check_recursive) { |
| 1416 | return node_list.size() != en->get_resource_count(edited_resource) || !internal_to_scene; |
| 1417 | } |
| 1418 | |
| 1419 | if (!internal_to_scene) { |
| 1420 | if (parent_resource.is_valid() && (!en->is_resource_internal_to_scene(parent_resource) || en->get_resource_count(parent_resource) > 1)) { |
| 1421 | return !parent_resource->is_built_in(); |
| 1422 | } else if (!p_check_recursive) { |
| 1423 | return true; |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | int parent_counter = en->get_resource_count(parent_resource); |
| 1428 | bool above_threshold = parent_resource.is_valid() ? (en->get_resource_count(parent_resource) <= 1 && en->get_resource_count(edited_resource) > 1) : en->get_resource_count(edited_resource) > 1; |
| 1429 | if (!p_check_recursive) { |
| 1430 | return above_threshold; |
| 1431 | } |
| 1432 | |
| 1433 | if (p_check_recursive && parent_counter <= 1) { |
| 1434 | List<Ref<Resource>> nested_resources; |
| 1435 | HashSet<Object *> scanned_objects; |
| 1436 | en->gather_resources(edited_resource, nested_resources, scanned_objects, true, true); |
| 1437 | |
| 1438 | for (Ref<Resource> R : nested_resources) { |
| 1439 | // Take into account Nested External Resources. |
| 1440 | if (en->get_resource_count(R) > 1 || !EditorNode::get_singleton()->is_resource_internal_to_scene(R)) { |
| 1441 | return true; |
| 1442 | } |
| 1443 | } |
| 1444 | return false; |
| 1445 | } |
| 1446 | return false; |
| 1447 | } |
| 1448 | |
| 1449 | Ref<Resource> EditorResourcePicker::_has_parent_resource() { |
| 1450 | Node *current_node = this->get_parent(); |
| 1451 | while (current_node != nullptr) { |
| 1452 | EditorProperty *ep = Object::cast_to<EditorProperty>(current_node); |
| 1453 | if (ep && Object::cast_to<Resource>(ep->get_edited_object())) { |
| 1454 | return Object::cast_to<Resource>(ep->get_edited_object()); |
| 1455 | } |
| 1456 | current_node = current_node->get_parent(); |
| 1457 | } |
| 1458 | return nullptr; |
| 1459 | } |
| 1460 | |
| 1461 | EditorResourcePicker::EditorResourcePicker(bool p_hide_assign_button_controls) { |
| 1462 | make_unique_button = memnew(Button); |
| 1463 | make_unique_button->set_flat(true); |
| 1464 | make_unique_button->set_accessibility_name(TTRC("Number of Linked Resources.")); |
| 1465 | make_unique_button->set_tooltip_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 1466 | make_unique_button->set_button_mask(MouseButtonMask::LEFT | MouseButtonMask::RIGHT); |
| 1467 | make_unique_button->set_action_mode(BaseButton::ACTION_MODE_BUTTON_PRESS); |
| 1468 | make_unique_button->set_theme_type_variation(SNAME("EditorInspectorButton")); |
| 1469 | add_child(make_unique_button); |
| 1470 | make_unique_button->connect(SceneStringName(pressed), callable_mp(this, &EditorResourcePicker::_on_unique_button_pressed)); |
| 1471 | |
| 1472 | assign_button = memnew(Button); |
| 1473 | assign_button->set_flat(true); |
| 1474 | assign_button->set_h_size_flags(SIZE_EXPAND_FILL); |
| 1475 | assign_button->set_accessibility_name(TTRC("Assign Resource")); |
| 1476 | assign_button->set_expand_icon(true); |
| 1477 | assign_button->set_clip_text(true); |
| 1478 | assign_button->set_theme_type_variation(SNAME("EditorInspectorButton")); |
| 1479 | assign_button->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 1480 | SET_DRAG_FORWARDING_GCD(assign_button, EditorResourcePicker); |
| 1481 | add_child(assign_button); |
| 1482 | assign_button->connect(SceneStringName(pressed), callable_mp(this, &EditorResourcePicker::_resource_selected)); |
| 1483 | assign_button->connect(SceneStringName(draw), callable_mp(this, &EditorResourcePicker::_button_draw)); |
| 1484 | assign_button->connect(SceneStringName(gui_input), callable_mp(this, &EditorResourcePicker::_button_input)); |
| 1485 | |
| 1486 | if (!p_hide_assign_button_controls) { |
| 1487 | preview_rect = memnew(TextureRect); |
| 1488 | preview_rect->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE); |
| 1489 | preview_rect->set_anchors_and_offsets_preset(PRESET_FULL_RECT); |
| 1490 | preview_rect->set_offset(SIDE_TOP, 1); |
| 1491 | preview_rect->set_offset(SIDE_BOTTOM, -1); |
| 1492 | preview_rect->set_offset(SIDE_RIGHT, -1); |
| 1493 | preview_rect->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS); |
| 1494 | assign_button->add_child(preview_rect); |
| 1495 | } |
| 1496 | |
| 1497 | quick_load_button = memnew(Button); |
| 1498 | quick_load_button->set_theme_type_variation(SNAME("EditorInspectorFlatButton")); |
| 1499 | quick_load_button->set_tooltip_text(TTRC("Quick Load")); |
| 1500 | add_child(quick_load_button); |
| 1501 | quick_load_button->connect(SceneStringName(pressed), callable_mp(this, &EditorResourcePicker::_edit_menu_cbk).bind(OBJ_MENU_QUICKLOAD)); |
| 1502 | |
| 1503 | edit_button = memnew(Button); |
| 1504 | edit_button->set_theme_type_variation(SNAME("EditorInspectorFlatButton")); |
| 1505 | edit_button->set_toggle_mode(true); |
| 1506 | edit_button->set_action_mode(BaseButton::ACTION_MODE_BUTTON_PRESS); |
| 1507 | edit_button->set_accessibility_name(TTRC("Edit")); |
| 1508 | add_child(edit_button); |
| 1509 | edit_button->connect(SceneStringName(pressed), callable_mp(this, &EditorResourcePicker::_update_menu)); |
| 1510 | edit_button->connect(SceneStringName(gui_input), callable_mp(this, &EditorResourcePicker::_button_input)); |
| 1511 | |
| 1512 | add_theme_constant_override("separation", 0); |
| 1513 | } |
| 1514 | |
| 1515 | // EditorScriptPicker |
| 1516 | |
| 1517 | void EditorScriptPicker::set_create_options(Object *p_menu_node) { |
| 1518 | PopupMenu *menu_node = Object::cast_to<PopupMenu>(p_menu_node); |
| 1519 | if (!menu_node) { |
| 1520 | return; |
| 1521 | } |
| 1522 | |
| 1523 | if (!(script_owner && script_owner->has_meta(SceneStringName(_custom_type_script)))) { |
| 1524 | menu_node->add_icon_item(get_editor_theme_icon(SNAME("ScriptCreate")), TTR("New Script..."), OBJ_MENU_NEW_SCRIPT); |
| 1525 | } |
| 1526 | |
| 1527 | if (script_owner) { |
| 1528 | Ref<Script> scr = script_owner->get_script(); |
| 1529 | if (scr.is_valid()) { |
| 1530 | menu_node->add_icon_item(get_editor_theme_icon(SNAME("ScriptExtend")), TTR("Extend Script..."), OBJ_MENU_EXTEND_SCRIPT); |
| 1531 | } |
| 1532 | } |
| 1533 | menu_node->add_separator(); |
| 1534 | } |
| 1535 | |
| 1536 | bool EditorScriptPicker::handle_menu_selected(int p_which) { |
| 1537 | switch (p_which) { |
| 1538 | case OBJ_MENU_NEW_SCRIPT: { |
| 1539 | if (script_owner) { |
| 1540 | SceneTreeDock::get_singleton()->open_script_dialog(script_owner, false); |
| 1541 | } |
| 1542 | return true; |
| 1543 | } |
| 1544 | |
| 1545 | case OBJ_MENU_EXTEND_SCRIPT: { |
| 1546 | if (script_owner) { |
| 1547 | SceneTreeDock::get_singleton()->open_script_dialog(script_owner, true); |
| 1548 | } |
| 1549 | return true; |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | return false; |
| 1554 | } |
| 1555 | |
| 1556 | void EditorScriptPicker::set_script_owner(Node *p_owner) { |
| 1557 | script_owner = p_owner; |
| 1558 | } |
| 1559 | |
| 1560 | Node *EditorScriptPicker::get_script_owner() const { |
| 1561 | return script_owner; |
| 1562 | } |
| 1563 | |
| 1564 | void EditorScriptPicker::_bind_methods() { |
| 1565 | ClassDB::bind_method(D_METHOD("set_script_owner", "owner_node"), &EditorScriptPicker::set_script_owner); |
| 1566 | ClassDB::bind_method(D_METHOD("get_script_owner"), &EditorScriptPicker::get_script_owner); |
| 1567 | |
| 1568 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "script_owner", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "set_script_owner", "get_script_owner"); |
| 1569 | } |
| 1570 | |
| 1571 | // EditorShaderPicker |
| 1572 | |
| 1573 | void EditorShaderPicker::set_create_options(Object *p_menu_node) { |
| 1574 | PopupMenu *menu_node = Object::cast_to<PopupMenu>(p_menu_node); |
| 1575 | if (!menu_node) { |
| 1576 | return; |
| 1577 | } |
| 1578 | |
| 1579 | menu_node->add_icon_item(get_editor_theme_icon(SNAME("Shader")), TTR("New Shader..."), OBJ_MENU_NEW_SHADER); |
| 1580 | menu_node->add_separator(); |
| 1581 | } |
| 1582 | |
| 1583 | bool EditorShaderPicker::handle_menu_selected(int p_which) { |
| 1584 | Ref<ShaderMaterial> ed_material = Ref<ShaderMaterial>(get_edited_material()); |
| 1585 | |
| 1586 | switch (p_which) { |
| 1587 | case OBJ_MENU_NEW_SHADER: { |
| 1588 | if (ed_material.is_valid()) { |
| 1589 | SceneTreeDock::get_singleton()->open_shader_dialog(ed_material, preferred_mode); |
| 1590 | return true; |
| 1591 | } |
| 1592 | } break; |
| 1593 | default: |
| 1594 | break; |
| 1595 | } |
| 1596 | return false; |
| 1597 | } |
| 1598 | |
| 1599 | void EditorShaderPicker::set_edited_material(ShaderMaterial *p_material) { |
| 1600 | edited_material = p_material; |
| 1601 | } |
| 1602 | |
| 1603 | ShaderMaterial *EditorShaderPicker::get_edited_material() const { |
| 1604 | return edited_material; |
| 1605 | } |
| 1606 | |
| 1607 | void EditorShaderPicker::set_preferred_mode(int p_mode) { |
| 1608 | preferred_mode = p_mode; |
| 1609 | } |
| 1610 | |
| 1611 | ////////////// |
| 1612 | |
| 1613 | void EditorAudioStreamPicker::_notification(int p_what) { |
| 1614 | switch (p_what) { |
| 1615 | case NOTIFICATION_READY: |
| 1616 | case NOTIFICATION_THEME_CHANGED: { |
| 1617 | _update_resource(); |
| 1618 | } break; |
| 1619 | case NOTIFICATION_INTERNAL_PROCESS: { |
| 1620 | Ref<AudioStream> audio_stream = get_edited_resource(); |
| 1621 | if (audio_stream.is_valid()) { |
| 1622 | if (audio_stream->get_length() > 0) { |
| 1623 | Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(audio_stream); |
| 1624 | if (preview.is_valid()) { |
| 1625 | if (preview->get_version() != last_preview_version) { |
| 1626 | stream_preview_rect->queue_redraw(); |
| 1627 | last_preview_version = preview->get_version(); |
| 1628 | } |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | uint64_t tagged_frame = audio_stream->get_tagged_frame(); |
| 1633 | uint64_t diff_frames = AudioServer::get_singleton()->get_mixed_frames() - tagged_frame; |
| 1634 | uint64_t diff_msec = diff_frames * 1000 / AudioServer::get_singleton()->get_mix_rate(); |
| 1635 | |
| 1636 | if (diff_msec < 300) { |
| 1637 | uint32_t count = audio_stream->get_tagged_frame_count(); |
| 1638 | |
| 1639 | bool differ = false; |
| 1640 | |
| 1641 | if (count != tagged_frame_offset_count) { |
| 1642 | differ = true; |
| 1643 | } |
| 1644 | float offsets[MAX_TAGGED_FRAMES]; |
| 1645 | |
| 1646 | for (uint32_t i = 0; i < MIN(count, uint32_t(MAX_TAGGED_FRAMES)); i++) { |
| 1647 | offsets[i] = audio_stream->get_tagged_frame_offset(i); |
| 1648 | if (offsets[i] != tagged_frame_offsets[i]) { |
| 1649 | differ = true; |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | if (differ) { |
| 1654 | tagged_frame_offset_count = count; |
| 1655 | for (uint32_t i = 0; i < count; i++) { |
| 1656 | tagged_frame_offsets[i] = offsets[i]; |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | stream_preview_rect->queue_redraw(); |
| 1661 | } else { |
| 1662 | if (tagged_frame_offset_count != 0) { |
| 1663 | stream_preview_rect->queue_redraw(); |
| 1664 | } |
| 1665 | tagged_frame_offset_count = 0; |
| 1666 | } |
| 1667 | } |
| 1668 | } break; |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | void EditorAudioStreamPicker::_update_resource() { |
| 1673 | EditorResourcePicker::_update_resource(); |
| 1674 | |
| 1675 | Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label")); |
| 1676 | int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label")); |
| 1677 | Ref<AudioStream> audio_stream = get_edited_resource(); |
| 1678 | if (audio_stream.is_valid() && audio_stream->get_length() > 0.0) { |
| 1679 | set_assign_button_min_size(Size2(1, font->get_height(font_size) * 3)); |
| 1680 | } else { |
| 1681 | set_assign_button_min_size(Size2(1, font->get_height(font_size) * 1.5)); |
| 1682 | } |
| 1683 | |
| 1684 | stream_preview_rect->queue_redraw(); |
| 1685 | } |
| 1686 | |
| 1687 | void EditorAudioStreamPicker::_preview_draw() { |
| 1688 | Ref<AudioStream> audio_stream = get_edited_resource(); |
| 1689 | if (audio_stream.is_null()) { |
| 1690 | get_assign_button()->set_text(TTR("<empty>")); |
| 1691 | return; |
| 1692 | } |
| 1693 | |
| 1694 | int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label")); |
| 1695 | |
| 1696 | get_assign_button()->set_text(""); |
| 1697 | |
| 1698 | Size2i size = stream_preview_rect->get_size(); |
| 1699 | Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label")); |
| 1700 | |
| 1701 | Rect2 rect(Point2(), size); |
| 1702 | |
| 1703 | if (audio_stream->get_length() > 0 && size.width > 0) { |
| 1704 | rect.size.height *= 0.5; |
| 1705 | |
| 1706 | Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(audio_stream); |
| 1707 | float preview_len = preview->get_length(); |
| 1708 | |
| 1709 | Vector<Vector2> points; |
| 1710 | points.resize(size.width * 2); |
| 1711 | |
| 1712 | for (int i = 0; i < size.width; i++) { |
| 1713 | float ofs = i * preview_len / size.width; |
| 1714 | float ofs_n = (i + 1) * preview_len / size.width; |
| 1715 | float max = preview->get_max(ofs, ofs_n) * 0.5 + 0.5; |
| 1716 | float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5; |
| 1717 | |
| 1718 | int idx = i; |
| 1719 | points.write[idx * 2 + 0] = Vector2(i + 1, rect.position.y + min * rect.size.y); |
| 1720 | points.write[idx * 2 + 1] = Vector2(i + 1, rect.position.y + max * rect.size.y); |
| 1721 | } |
| 1722 | |
| 1723 | Vector<Color> colors = { get_theme_color(SNAME("contrast_color_2"), EditorStringName(Editor)) }; |
| 1724 | |
| 1725 | RS::get_singleton()->canvas_item_add_multiline(stream_preview_rect->get_canvas_item(), points, colors); |
| 1726 | |
| 1727 | if (tagged_frame_offset_count) { |
| 1728 | Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor)); |
| 1729 | |
| 1730 | for (uint32_t i = 0; i < tagged_frame_offset_count; i++) { |
| 1731 | int x = CLAMP(tagged_frame_offsets[i] * size.width / preview_len, 0, size.width); |
| 1732 | if (x == 0) { |
| 1733 | continue; // Because some may always return 0, ignore offset 0. |
| 1734 | } |
| 1735 | stream_preview_rect->draw_rect(Rect2i(x, 0, 2, rect.size.height), accent); |
| 1736 | } |
| 1737 | } |
| 1738 | rect.position.y += rect.size.height; |
| 1739 | } |
| 1740 | |
| 1741 | Ref<Texture2D> icon; |
| 1742 | Color icon_modulate(1, 1, 1, 1); |
| 1743 | |
| 1744 | if (tagged_frame_offset_count > 0) { |
| 1745 | icon = get_editor_theme_icon(SNAME("Play")); |
| 1746 | if ((OS::get_singleton()->get_ticks_msec() % 500) > 250) { |
| 1747 | icon_modulate = Color(1, 0.5, 0.5, 1); // get_theme_color(SNAME("accent_color"), EditorStringName(Editor)); |
| 1748 | } |
| 1749 | } else { |
| 1750 | icon = EditorNode::get_singleton()->get_object_icon(audio_stream.operator->()); |
| 1751 | } |
| 1752 | String text; |
| 1753 | if (!audio_stream->get_name().is_empty()) { |
| 1754 | text = audio_stream->get_name(); |
| 1755 | } else if (audio_stream->get_path().is_resource_file()) { |
| 1756 | text = audio_stream->get_path().get_file(); |
| 1757 | } else { |
| 1758 | text = audio_stream->get_class().replace_first("AudioStream", ""); |
| 1759 | } |
| 1760 | |
| 1761 | stream_preview_rect->draw_texture(icon, Point2i(EDSCALE * 4, rect.position.y + (rect.size.height - icon->get_height()) / 2), icon_modulate); |
| 1762 | stream_preview_rect->draw_string(font, Point2i(EDSCALE * 4 + icon->get_width(), rect.position.y + font->get_ascent(font_size) + (rect.size.height - font->get_height(font_size)) / 2), text, HORIZONTAL_ALIGNMENT_CENTER, size.width - 4 * EDSCALE - icon->get_width(), font_size, get_theme_color(SceneStringName(font_color), EditorStringName(Editor))); |
| 1763 | } |
| 1764 | |
| 1765 | EditorAudioStreamPicker::EditorAudioStreamPicker() : |
| 1766 | EditorResourcePicker(true) { |
| 1767 | stream_preview_rect = memnew(Control); |
| 1768 | |
| 1769 | stream_preview_rect->set_anchors_and_offsets_preset(PRESET_FULL_RECT); |
| 1770 | stream_preview_rect->set_offset(SIDE_TOP, 1); |
| 1771 | stream_preview_rect->set_offset(SIDE_BOTTOM, -1); |
| 1772 | stream_preview_rect->set_offset(SIDE_RIGHT, -1); |
| 1773 | stream_preview_rect->set_mouse_filter(MOUSE_FILTER_IGNORE); |
| 1774 | stream_preview_rect->connect(SceneStringName(draw), callable_mp(this, &EditorAudioStreamPicker::_preview_draw)); |
| 1775 | |
| 1776 | get_assign_button()->add_child(stream_preview_rect); |
| 1777 | get_assign_button()->move_child(stream_preview_rect, 0); |
| 1778 | set_process_internal(true); |
| 1779 | } |