Validate Resource type when pasting property

This commit is contained in:
kobewi 2025-11-04 16:10:16 +01:00
parent 76cd830ba9
commit c23a224e24
4 changed files with 25 additions and 6 deletions

View file

@ -43,6 +43,7 @@
#include "editor/inspector/add_metadata_dialog.h"
#include "editor/inspector/editor_properties.h"
#include "editor/inspector/editor_property_name_processor.h"
#include "editor/inspector/editor_resource_picker.h"
#include "editor/inspector/multi_node_edit.h"
#include "editor/script/script_editor_plugin.h"
#include "editor/settings/editor_feature_profile.h"
@ -1417,6 +1418,13 @@ void EditorProperty::menu_option(int p_option) {
EditorInspector::set_property_clipboard(object->get(property));
} break;
case MENU_PASTE_VALUE: {
EditorPropertyResource *epr = Object::cast_to<EditorPropertyResource>(this);
if (epr) {
const Ref<Resource> res = InspectorDock::get_inspector_singleton()->get_property_clipboard();
if (res.is_valid() && !epr->get_resource_picker()->is_resource_allowed(res)) {
return;
}
}
emit_changed(property, EditorInspector::get_property_clipboard());
} break;
case MENU_COPY_PROPERTY_PATH: {

View file

@ -764,6 +764,7 @@ protected:
public:
virtual void update_property() override;
void setup(Object *p_object, const String &p_path, const String &p_base_type);
EditorResourcePicker *get_resource_picker() const { return resource_picker; }
void collapse_all_folding() override;
void expand_all_folding() override;

View file

@ -1156,11 +1156,9 @@ Vector<String> EditorResourcePicker::get_allowed_types() const {
return types;
}
void EditorResourcePicker::set_edited_resource(Ref<Resource> p_resource) {
bool EditorResourcePicker::is_resource_allowed(const Ref<Resource> &p_resource) {
if (p_resource.is_null()) {
edited_resource = Ref<Resource>();
_update_resource();
return;
return true;
}
if (!base_type.is_empty()) {
@ -1175,10 +1173,21 @@ void EditorResourcePicker::set_edited_resource(Ref<Resource> p_resource) {
}
if (!is_custom && !_is_type_valid(p_resource->get_class(), allowed_types)) {
String class_str = (custom_class == StringName() ? p_resource->get_class() : vformat("%s (%s)", custom_class, p_resource->get_class()));
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));
return false;
}
}
return true;
}
void EditorResourcePicker::set_edited_resource(Ref<Resource> p_resource) {
if (!is_resource_allowed(p_resource)) {
StringName custom_class;
if (p_resource->get_script()) {
custom_class = EditorNode::get_singleton()->get_object_custom_type_name(p_resource->get_script());
}
const String class_str = (custom_class.is_empty() ? p_resource->get_class() : vformat("%s (%s)", custom_class, p_resource->get_class()));
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));
}
set_edited_resource_no_check(p_resource);
}

View file

@ -140,6 +140,7 @@ public:
String get_base_type() const;
Vector<String> get_allowed_types() const;
bool is_resource_allowed(const Ref<Resource> &p_resource);
void set_edited_resource(Ref<Resource> p_resource);
void set_edited_resource_no_check(Ref<Resource> p_resource);
Ref<Resource> get_edited_resource();