Merge pull request #40500 from KoBeWi/inspector_magic

Convert some resource types in the inspector
This commit is contained in:
Rémi Verschelde 2020-07-20 14:50:06 +02:00 committed by GitHub
commit 0edb49ad16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 52 additions and 18 deletions

View File

@ -2980,7 +2980,16 @@ Variant EditorPropertyResource::get_drag_data_fw(const Point2 &p_point, Control
} }
bool EditorPropertyResource::_is_drop_valid(const Dictionary &p_drag_data) const { bool EditorPropertyResource::_is_drop_valid(const Dictionary &p_drag_data) const {
String allowed_type = base_type; Vector<String> allowed_types = base_type.split(",");
int size = allowed_types.size();
for (int i = 0; i < size; i++) {
String at = allowed_types[i].strip_edges();
if (at == "StandardMaterial3D") {
allowed_types.append("Texture2D");
} else if (at == "ShaderMaterial") {
allowed_types.append("Shader");
}
}
Dictionary drag_data = p_drag_data; Dictionary drag_data = p_drag_data;
@ -2993,9 +3002,9 @@ bool EditorPropertyResource::_is_drop_valid(const Dictionary &p_drag_data) const
} }
if (res.is_valid()) { if (res.is_valid()) {
for (int i = 0; i < allowed_type.get_slice_count(","); i++) { for (int i = 0; i < allowed_types.size(); i++) {
String at = allowed_type.get_slice(",", i).strip_edges(); String at = allowed_types[i].strip_edges();
if (res.is_valid() && ClassDB::is_parent_class(res->get_class(), at)) { if (ClassDB::is_parent_class(res->get_class(), at)) {
return true; return true;
} }
} }
@ -3009,8 +3018,8 @@ bool EditorPropertyResource::_is_drop_valid(const Dictionary &p_drag_data) const
String ftype = EditorFileSystem::get_singleton()->get_file_type(file); String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
if (ftype != "") { if (ftype != "") {
for (int i = 0; i < allowed_type.get_slice_count(","); i++) { for (int i = 0; i < allowed_types.size(); i++) {
String at = allowed_type.get_slice(",", i).strip_edges(); String at = allowed_types[i].strip_edges();
if (ClassDB::is_parent_class(ftype, at)) { if (ClassDB::is_parent_class(ftype, at)) {
return true; return true;
} }
@ -3039,24 +3048,49 @@ void EditorPropertyResource::drop_data_fw(const Point2 &p_point, const Variant &
res = drag_data["resource"]; res = drag_data["resource"];
} }
if (res.is_valid()) { if (!res.is_valid() && drag_data.has("type") && String(drag_data["type"]) == "files") {
emit_changed(get_edited_property(), res);
update_property();
return;
}
if (drag_data.has("type") && String(drag_data["type"]) == "files") {
Vector<String> files = drag_data["files"]; Vector<String> files = drag_data["files"];
if (files.size() == 1) { if (files.size() == 1) {
String file = files[0]; String file = files[0];
RES file_res = ResourceLoader::load(file); res = ResourceLoader::load(file);
if (file_res.is_valid()) { }
emit_changed(get_edited_property(), file_res); }
update_property();
return; if (res.is_valid()) {
bool need_convert = true;
Vector<String> allowed_types = base_type.split(",");
for (int i = 0; i < allowed_types.size(); i++) {
String at = allowed_types[i].strip_edges();
if (ClassDB::is_parent_class(res->get_class(), at)) {
need_convert = false;
break;
} }
} }
if (need_convert) {
for (int i = 0; i < allowed_types.size(); i++) {
String at = allowed_types[i].strip_edges();
if (at == "StandardMaterial3D" && ClassDB::is_parent_class(res->get_class(), "Texture2D")) {
Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D);
mat->set_texture(StandardMaterial3D::TextureParam::TEXTURE_ALBEDO, res);
res = mat;
break;
}
if (at == "ShaderMaterial" && ClassDB::is_parent_class(res->get_class(), "Shader")) {
Ref<ShaderMaterial> mat = memnew(ShaderMaterial);
mat->set_shader(res);
res = mat;
break;
}
}
}
emit_changed(get_edited_property(), res);
update_property();
return;
} }
} }