Skip to content

Commit 1f675e6

Browse files
committed
Merge pull request #101278 from bruvzg/ed_prop_nt_dict
Fix non-typed Dictionary keys displayed as `<null>` in the inspector.
2 parents d164f05 + b8860f1 commit 1f675e6

File tree

2 files changed

+130
-21
lines changed

2 files changed

+130
-21
lines changed

editor/editor_properties_array_dict.cpp

+115-21
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,15 @@ void EditorPropertyArray::_create_new_property_slot() {
347347
slots.push_back(slot);
348348
}
349349

350+
void EditorPropertyArray::set_preview_value(bool p_preview_value) {
351+
preview_value = p_preview_value;
352+
}
353+
350354
void EditorPropertyArray::update_property() {
351355
Variant array = get_edited_property_value();
352356

353357
String array_type_name = Variant::get_type_name(array_type);
358+
String array_sub_type_name;
354359
if (array_type == Variant::ARRAY && subtype != Variant::NIL) {
355360
String type_name;
356361
if (subtype == Variant::OBJECT && (subtype_hint == PROPERTY_HINT_RESOURCE_TYPE || subtype_hint == PROPERTY_HINT_NODE_TYPE)) {
@@ -359,11 +364,23 @@ void EditorPropertyArray::update_property() {
359364
type_name = Variant::get_type_name(subtype);
360365
}
361366

362-
array_type_name = vformat("%s[%s]", array_type_name, type_name);
367+
if (preview_value) {
368+
array_sub_type_name = vformat("[%s] ", type_name);
369+
} else {
370+
array_type_name = vformat("%s[%s]", array_type_name, type_name);
371+
}
363372
}
364373

365374
if (!array.is_array()) {
366-
edit->set_text(vformat(TTR("(Nil) %s"), array_type_name));
375+
if (preview_value) {
376+
edit->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
377+
edit->set_button_icon(get_editor_theme_icon(SNAME("Nil")));
378+
edit->set_text(array_type_name);
379+
} else {
380+
edit->set_text_alignment(HORIZONTAL_ALIGNMENT_CENTER);
381+
edit->set_button_icon(Ref<Texture2D>());
382+
edit->set_text(vformat(TTR("(Nil) %s"), array_type_name));
383+
}
367384
edit->set_pressed(false);
368385
if (container) {
369386
set_bottom_editor(nullptr);
@@ -383,7 +400,25 @@ void EditorPropertyArray::update_property() {
383400
_page_changed(max_page);
384401
}
385402

386-
edit->set_text(vformat(TTR("%s (size %s)"), array_type_name, itos(size)));
403+
if (preview_value) {
404+
String ctr_str = array.get_construct_string().trim_prefix(array_type_name + "(").trim_suffix(")").replace("\n", "");
405+
if (array_type == Variant::ARRAY && subtype != Variant::NIL) {
406+
int type_end = ctr_str.find("](");
407+
if (type_end > 0) {
408+
ctr_str = ctr_str.substr(type_end + 2);
409+
}
410+
}
411+
412+
edit->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
413+
edit->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
414+
edit->set_button_icon(get_editor_theme_icon(array_type_name));
415+
edit->set_text(vformat("%s%s", array_sub_type_name, ctr_str));
416+
edit->set_tooltip_text(vformat(TTR("%s%s (size %d)"), array_type_name, array_sub_type_name, size));
417+
} else {
418+
edit->set_text_alignment(HORIZONTAL_ALIGNMENT_CENTER);
419+
edit->set_button_icon(Ref<Texture2D>());
420+
edit->set_text(vformat(TTR("%s (size %s)"), array_type_name, itos(size)));
421+
}
387422

388423
bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
389424
if (edit->is_pressed() != unfolded) {
@@ -961,20 +996,7 @@ void EditorPropertyDictionary::_create_new_property_slot(int p_idx) {
961996

962997
EditorProperty *prop_key = nullptr;
963998
if (p_idx != EditorPropertyDictionaryObject::NEW_KEY_INDEX && p_idx != EditorPropertyDictionaryObject::NEW_VALUE_INDEX) {
964-
if (key_subtype == Variant::OBJECT) {
965-
EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
966-
editor->setup("Object");
967-
prop_key = editor;
968-
} else {
969-
prop_key = EditorInspector::instantiate_property_editor(this, key_subtype, "", key_subtype_hint, key_subtype_hint_string, PROPERTY_USAGE_NONE);
970-
}
971-
prop_key->set_read_only(true);
972-
prop_key->set_selectable(false);
973-
prop_key->set_focus_mode(Control::FOCUS_NONE);
974-
prop_key->set_draw_background(false);
975-
prop_key->set_use_folding(is_using_folding());
976-
prop_key->set_h_size_flags(SIZE_EXPAND_FILL);
977-
prop_key->set_draw_label(false);
999+
prop_key = memnew(EditorPropertyNil);
9781000
hbox->add_child(prop_key);
9791001
}
9801002

@@ -1096,10 +1118,15 @@ void EditorPropertyDictionary::setup(PropertyHint p_hint, const String &p_hint_s
10961118
}
10971119
}
10981120

1121+
void EditorPropertyDictionary::set_preview_value(bool p_preview_value) {
1122+
preview_value = p_preview_value;
1123+
}
1124+
10991125
void EditorPropertyDictionary::update_property() {
11001126
Variant updated_val = get_edited_property_value();
11011127

11021128
String dict_type_name = "Dictionary";
1129+
String dict_sub_type_name;
11031130
if (key_subtype != Variant::NIL || value_subtype != Variant::NIL) {
11041131
String key_subtype_name = "Variant";
11051132
if (key_subtype == Variant::OBJECT && (key_subtype_hint == PROPERTY_HINT_RESOURCE_TYPE || key_subtype_hint == PROPERTY_HINT_NODE_TYPE)) {
@@ -1113,11 +1140,23 @@ void EditorPropertyDictionary::update_property() {
11131140
} else if (value_subtype != Variant::NIL) {
11141141
value_subtype_name = Variant::get_type_name(value_subtype);
11151142
}
1116-
dict_type_name += vformat("[%s, %s]", key_subtype_name, value_subtype_name);
1143+
if (preview_value) {
1144+
dict_sub_type_name = vformat("[%s, %s] ", key_subtype_name, value_subtype_name);
1145+
} else {
1146+
dict_type_name += vformat("[%s, %s]", key_subtype_name, value_subtype_name);
1147+
}
11171148
}
11181149

11191150
if (updated_val.get_type() != Variant::DICTIONARY) {
1120-
edit->set_text(vformat(TTR("(Nil) %s"), dict_type_name)); // This provides symmetry with the array property.
1151+
if (preview_value) {
1152+
edit->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
1153+
edit->set_button_icon(get_editor_theme_icon(SNAME("Nil")));
1154+
edit->set_text(dict_type_name);
1155+
} else {
1156+
edit->set_text_alignment(HORIZONTAL_ALIGNMENT_CENTER);
1157+
edit->set_button_icon(Ref<Texture2D>());
1158+
edit->set_text(vformat(TTR("(Nil) %s"), dict_type_name));
1159+
}
11211160
edit->set_pressed(false);
11221161
if (container) {
11231162
set_bottom_editor(nullptr);
@@ -1133,7 +1172,25 @@ void EditorPropertyDictionary::update_property() {
11331172
Dictionary dict = updated_val;
11341173
object->set_dict(updated_val);
11351174

1136-
edit->set_text(vformat(TTR("%s (size %d)"), dict_type_name, dict.size()));
1175+
if (preview_value) {
1176+
String ctr_str = updated_val.get_construct_string().replace("\n", "");
1177+
if (key_subtype != Variant::NIL || value_subtype != Variant::NIL) {
1178+
int type_end = ctr_str.find("](");
1179+
if (type_end > 0) {
1180+
ctr_str = ctr_str.substr(type_end + 2).trim_suffix(")");
1181+
}
1182+
}
1183+
1184+
edit->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
1185+
edit->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
1186+
edit->set_button_icon(get_editor_theme_icon(dict_type_name));
1187+
edit->set_text(vformat("%s%s", dict_sub_type_name, ctr_str));
1188+
edit->set_tooltip_text(vformat(TTR("%s%s (size %d)"), dict_type_name, dict_sub_type_name, dict.size()));
1189+
} else {
1190+
edit->set_text_alignment(HORIZONTAL_ALIGNMENT_CENTER);
1191+
edit->set_button_icon(Ref<Texture2D>());
1192+
edit->set_text(vformat(TTR("%s (size %d)"), dict_type_name, dict.size()));
1193+
}
11371194

11381195
bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
11391196
if (edit->is_pressed() != unfolded) {
@@ -1199,6 +1256,44 @@ void EditorPropertyDictionary::update_property() {
11991256
if (!slot_visible) {
12001257
continue;
12011258
}
1259+
1260+
// Check if the editor property key needs to be updated.
1261+
if (slot.prop_key) {
1262+
Variant key;
1263+
object->get_by_property_name(slot.key_name, key);
1264+
Variant::Type key_type = key.get_type();
1265+
1266+
bool key_as_id = Object::cast_to<EncodedObjectAsID>(key);
1267+
if (key_type != slot.key_type || (key_type == Variant::OBJECT && key_as_id != slot.key_as_id)) {
1268+
slot.key_as_id = key_as_id;
1269+
slot.key_type = key_type;
1270+
EditorProperty *new_prop = nullptr;
1271+
if (key_type == Variant::OBJECT && key_as_id) {
1272+
EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
1273+
editor->setup("Object");
1274+
new_prop = editor;
1275+
} else {
1276+
new_prop = EditorInspector::instantiate_property_editor(this, key_type, "", key_subtype_hint, key_subtype_hint_string, PROPERTY_USAGE_NONE);
1277+
}
1278+
new_prop->set_read_only(true);
1279+
new_prop->set_selectable(false);
1280+
new_prop->set_focus_mode(Control::FOCUS_NONE);
1281+
new_prop->set_draw_background(false);
1282+
new_prop->set_use_folding(is_using_folding());
1283+
new_prop->set_h_size_flags(SIZE_EXPAND_FILL);
1284+
new_prop->set_draw_label(false);
1285+
EditorPropertyArray *arr_prop = Object::cast_to<EditorPropertyArray>(new_prop);
1286+
if (arr_prop) {
1287+
arr_prop->set_preview_value(true);
1288+
}
1289+
EditorPropertyDictionary *dict_prop = Object::cast_to<EditorPropertyDictionary>(new_prop);
1290+
if (dict_prop) {
1291+
dict_prop->set_preview_value(true);
1292+
}
1293+
slot.set_key_prop(new_prop);
1294+
}
1295+
}
1296+
12021297
Variant value;
12031298
object->get_by_property_name(slot.prop_name, value);
12041299
Variant::Type value_type = value.get_type();
@@ -1231,7 +1326,6 @@ void EditorPropertyDictionary::update_property() {
12311326
} else if (slot.index != EditorPropertyDictionaryObject::NEW_KEY_INDEX && slot.index != EditorPropertyDictionaryObject::NEW_VALUE_INDEX) {
12321327
Variant key = dict.get_key_at_index(slot.index);
12331328
String cs = key.get_construct_string();
1234-
slot.prop->set_label(cs);
12351329
slot.prop->set_tooltip_text(cs);
12361330
}
12371331

editor/editor_properties_array_dict.h

+15
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class EditorPropertyArray : public EditorProperty {
115115

116116
PopupMenu *change_type = nullptr;
117117

118+
bool preview_value = false;
118119
int page_length = 20;
119120
int page_index = 0;
120121
int changing_type_index = EditorPropertyArrayObject::NOT_CHANGING_TYPE;
@@ -169,6 +170,7 @@ class EditorPropertyArray : public EditorProperty {
169170

170171
public:
171172
void setup(Variant::Type p_array_type, const String &p_hint_string = "");
173+
void set_preview_value(bool p_preview_value);
172174
virtual void update_property() override;
173175
virtual bool is_colored(ColorationMode p_mode) override;
174176
EditorPropertyArray();
@@ -182,7 +184,9 @@ class EditorPropertyDictionary : public EditorProperty {
182184
HBoxContainer *container = nullptr;
183185
int index = -1;
184186
Variant::Type type = Variant::VARIANT_MAX;
187+
Variant::Type key_type = Variant::VARIANT_MAX;
185188
bool as_id = false;
189+
bool key_as_id = false;
186190
EditorProperty *prop = nullptr;
187191
EditorProperty *prop_key = nullptr;
188192
String prop_name;
@@ -202,6 +206,15 @@ class EditorPropertyDictionary : public EditorProperty {
202206
update_prop_or_index();
203207
}
204208

209+
void set_key_prop(EditorProperty *p_prop) {
210+
if (prop_key) {
211+
prop_key->add_sibling(p_prop);
212+
prop_key->queue_free();
213+
prop_key = p_prop;
214+
update_prop_or_index();
215+
}
216+
}
217+
205218
void update_prop_or_index() {
206219
prop->set_object_and_property(object.ptr(), prop_name);
207220
if (prop_key) {
@@ -215,6 +228,7 @@ class EditorPropertyDictionary : public EditorProperty {
215228
PopupMenu *change_type = nullptr;
216229
bool updating = false;
217230

231+
bool preview_value = false;
218232
Ref<EditorPropertyDictionaryObject> object;
219233
int page_length = 20;
220234
int page_index = 0;
@@ -252,6 +266,7 @@ class EditorPropertyDictionary : public EditorProperty {
252266

253267
public:
254268
void setup(PropertyHint p_hint, const String &p_hint_string = "");
269+
void set_preview_value(bool p_preview_value);
255270
virtual void update_property() override;
256271
virtual bool is_colored(ColorationMode p_mode) override;
257272
EditorPropertyDictionary();

0 commit comments

Comments
 (0)