Skip to content

Commit 0c9531c

Browse files
committed
Merge pull request #88322 from EmrysMyrddin/fix/dict_editor_focus_on_type_change
Editor: Focus value editor on type change in Dictionary and Array editors
2 parents bef8df0 + c31111f commit 0c9531c

4 files changed

+42
-3
lines changed

editor/editor_inspector.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,19 @@ void EditorProperty::add_focusable(Control *p_control) {
652652
focusables.push_back(p_control);
653653
}
654654

655+
void EditorProperty::grab_focus(int p_focusable) {
656+
if (focusables.is_empty()) {
657+
return;
658+
}
659+
660+
if (p_focusable >= 0) {
661+
ERR_FAIL_INDEX(p_focusable, focusables.size());
662+
focusables[p_focusable]->grab_focus();
663+
} else {
664+
focusables[0]->grab_focus();
665+
}
666+
}
667+
655668
void EditorProperty::select(int p_focusable) {
656669
bool already_selected = selected;
657670
if (!selectable) {

editor/editor_inspector.h

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ class EditorProperty : public Container {
192192
void set_deletable(bool p_enable);
193193
bool is_deletable() const;
194194
void add_focusable(Control *p_control);
195+
void grab_focus(int p_focusable = -1);
195196
void select(int p_focusable = -1);
196197
void deselect();
197198
bool is_selected() const;

editor/editor_properties_array_dict.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ void EditorPropertyArray::_change_type_menu(int p_index) {
255255
return;
256256
}
257257

258+
ERR_FAIL_COND_MSG(
259+
changing_type_index == EditorPropertyArrayObject::NOT_CHANGING_TYPE,
260+
"Tried to change type of an array item, but no item was selected.");
261+
258262
Variant value;
259263
VariantInternal::initialize(&value, Variant::Type(p_index));
260264

@@ -444,6 +448,10 @@ void EditorPropertyArray::update_property() {
444448
slot.prop = new_prop;
445449
slot.set_index(idx);
446450
}
451+
if (slot.index == changing_type_index) {
452+
callable_mp(slot.prop, &EditorProperty::grab_focus).call_deferred(0);
453+
changing_type_index = EditorPropertyArrayObject::NOT_CHANGING_TYPE;
454+
}
447455
slot.prop->update_property();
448456
}
449457

@@ -921,6 +929,10 @@ void EditorPropertyDictionary::_create_new_property_slot(int p_idx) {
921929
}
922930

923931
void EditorPropertyDictionary::_change_type_menu(int p_index) {
932+
ERR_FAIL_COND_MSG(
933+
changing_type_index == EditorPropertyDictionaryObject::NOT_CHANGING_TYPE,
934+
"Tried to change the type of a dict key or value, but nothing was selected.");
935+
924936
Variant value;
925937
switch (changing_type_index) {
926938
case EditorPropertyDictionaryObject::NEW_KEY_INDEX:
@@ -1062,6 +1074,14 @@ void EditorPropertyDictionary::update_property() {
10621074
new_prop->set_read_only(is_read_only());
10631075
slot.set_prop(new_prop);
10641076
}
1077+
1078+
// We need to grab the focus of the property that is being changed, even if the type didn't actually changed.
1079+
// Otherwise, focus will stay on the change type button, which is not very user friendly.
1080+
if (changing_type_index == slot.index) {
1081+
callable_mp(slot.prop, &EditorProperty::grab_focus).call_deferred(0);
1082+
changing_type_index = EditorPropertyDictionaryObject::NOT_CHANGING_TYPE; // Reset to avoid grabbing focus again.
1083+
}
1084+
10651085
slot.prop->update_property();
10661086
}
10671087
updating = false;

editor/editor_properties_array_dict.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class EditorPropertyArrayObject : public RefCounted {
4949
bool _get(const StringName &p_name, Variant &r_ret) const;
5050

5151
public:
52+
enum {
53+
NOT_CHANGING_TYPE = -1,
54+
};
55+
5256
void set_array(const Variant &p_array);
5357
Variant get_array();
5458

@@ -68,7 +72,8 @@ class EditorPropertyDictionaryObject : public RefCounted {
6872

6973
public:
7074
enum {
71-
NEW_KEY_INDEX = -2,
75+
NOT_CHANGING_TYPE = -3,
76+
NEW_KEY_INDEX,
7277
NEW_VALUE_INDEX,
7378
};
7479

@@ -111,7 +116,7 @@ class EditorPropertyArray : public EditorProperty {
111116

112117
int page_length = 20;
113118
int page_index = 0;
114-
int changing_type_index;
119+
int changing_type_index = EditorPropertyArrayObject::NOT_CHANGING_TYPE;
115120
Button *edit = nullptr;
116121
PanelContainer *container = nullptr;
117122
VBoxContainer *property_vbox = nullptr;
@@ -206,7 +211,7 @@ class EditorPropertyDictionary : public EditorProperty {
206211
Ref<EditorPropertyDictionaryObject> object;
207212
int page_length = 20;
208213
int page_index = 0;
209-
int changing_type_index;
214+
int changing_type_index = EditorPropertyDictionaryObject::NOT_CHANGING_TYPE;
210215
Button *edit = nullptr;
211216
PanelContainer *container = nullptr;
212217
VBoxContainer *property_vbox = nullptr;

0 commit comments

Comments
 (0)