Skip to content

Commit

Permalink
Merge pull request #88322 from EmrysMyrddin/fix/dict_editor_focus_on_…
Browse files Browse the repository at this point in the history
…type_change

Editor: Focus value editor on type change in Dictionary and Array editors
  • Loading branch information
akien-mga committed Jun 7, 2024
2 parents bef8df0 + c31111f commit 0c9531c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
13 changes: 13 additions & 0 deletions editor/editor_inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,19 @@ void EditorProperty::add_focusable(Control *p_control) {
focusables.push_back(p_control);
}

void EditorProperty::grab_focus(int p_focusable) {
if (focusables.is_empty()) {
return;
}

if (p_focusable >= 0) {
ERR_FAIL_INDEX(p_focusable, focusables.size());
focusables[p_focusable]->grab_focus();
} else {
focusables[0]->grab_focus();
}
}

void EditorProperty::select(int p_focusable) {
bool already_selected = selected;
if (!selectable) {
Expand Down
1 change: 1 addition & 0 deletions editor/editor_inspector.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ class EditorProperty : public Container {
void set_deletable(bool p_enable);
bool is_deletable() const;
void add_focusable(Control *p_control);
void grab_focus(int p_focusable = -1);
void select(int p_focusable = -1);
void deselect();
bool is_selected() const;
Expand Down
20 changes: 20 additions & 0 deletions editor/editor_properties_array_dict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ void EditorPropertyArray::_change_type_menu(int p_index) {
return;
}

ERR_FAIL_COND_MSG(
changing_type_index == EditorPropertyArrayObject::NOT_CHANGING_TYPE,
"Tried to change type of an array item, but no item was selected.");

Variant value;
VariantInternal::initialize(&value, Variant::Type(p_index));

Expand Down Expand Up @@ -444,6 +448,10 @@ void EditorPropertyArray::update_property() {
slot.prop = new_prop;
slot.set_index(idx);
}
if (slot.index == changing_type_index) {
callable_mp(slot.prop, &EditorProperty::grab_focus).call_deferred(0);
changing_type_index = EditorPropertyArrayObject::NOT_CHANGING_TYPE;
}
slot.prop->update_property();
}

Expand Down Expand Up @@ -921,6 +929,10 @@ void EditorPropertyDictionary::_create_new_property_slot(int p_idx) {
}

void EditorPropertyDictionary::_change_type_menu(int p_index) {
ERR_FAIL_COND_MSG(
changing_type_index == EditorPropertyDictionaryObject::NOT_CHANGING_TYPE,
"Tried to change the type of a dict key or value, but nothing was selected.");

Variant value;
switch (changing_type_index) {
case EditorPropertyDictionaryObject::NEW_KEY_INDEX:
Expand Down Expand Up @@ -1062,6 +1074,14 @@ void EditorPropertyDictionary::update_property() {
new_prop->set_read_only(is_read_only());
slot.set_prop(new_prop);
}

// We need to grab the focus of the property that is being changed, even if the type didn't actually changed.
// Otherwise, focus will stay on the change type button, which is not very user friendly.
if (changing_type_index == slot.index) {
callable_mp(slot.prop, &EditorProperty::grab_focus).call_deferred(0);
changing_type_index = EditorPropertyDictionaryObject::NOT_CHANGING_TYPE; // Reset to avoid grabbing focus again.
}

slot.prop->update_property();
}
updating = false;
Expand Down
11 changes: 8 additions & 3 deletions editor/editor_properties_array_dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class EditorPropertyArrayObject : public RefCounted {
bool _get(const StringName &p_name, Variant &r_ret) const;

public:
enum {
NOT_CHANGING_TYPE = -1,
};

void set_array(const Variant &p_array);
Variant get_array();

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

public:
enum {
NEW_KEY_INDEX = -2,
NOT_CHANGING_TYPE = -3,
NEW_KEY_INDEX,
NEW_VALUE_INDEX,
};

Expand Down Expand Up @@ -111,7 +116,7 @@ class EditorPropertyArray : public EditorProperty {

int page_length = 20;
int page_index = 0;
int changing_type_index;
int changing_type_index = EditorPropertyArrayObject::NOT_CHANGING_TYPE;
Button *edit = nullptr;
PanelContainer *container = nullptr;
VBoxContainer *property_vbox = nullptr;
Expand Down Expand Up @@ -206,7 +211,7 @@ class EditorPropertyDictionary : public EditorProperty {
Ref<EditorPropertyDictionaryObject> object;
int page_length = 20;
int page_index = 0;
int changing_type_index;
int changing_type_index = EditorPropertyDictionaryObject::NOT_CHANGING_TYPE;
Button *edit = nullptr;
PanelContainer *container = nullptr;
VBoxContainer *property_vbox = nullptr;
Expand Down

0 comments on commit 0c9531c

Please sign in to comment.