1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Inline Array and Dictionary inspector buttons

This commit is contained in:
dugramen
2025-06-23 22:16:04 -04:00
parent 2cb2c1fb5b
commit 7df65de796
4 changed files with 151 additions and 17 deletions

View File

@@ -232,6 +232,12 @@ Size2 EditorProperty::get_minimum_size() const {
if (c == bottom_editor) {
continue;
}
if (c == left_container) {
continue;
}
if (c == right_container) {
continue;
}
Size2 minsize = c->get_combined_minimum_size();
ms = ms.max(minsize);
@@ -249,6 +255,14 @@ Size2 EditorProperty::get_minimum_size() const {
ms.width += theme_cache.checked_icon->get_width() + theme_cache.padding + theme_cache.horizontal_separation;
}
Size2 ls = left_container->get_combined_minimum_size();
ms.width += ls.x;
ms.height = MAX(ms.height, ls.y);
Size2 rs = right_container->get_combined_minimum_size();
ms.width += rs.x;
ms.height = MAX(ms.height, rs.y);
if (bottom_editor != nullptr && bottom_editor->is_visible()) {
ms.height += label.is_empty() ? 0 : _get_v_separation();
Size2 bems = bottom_editor->get_combined_minimum_size();
@@ -318,9 +332,11 @@ void EditorProperty::_notification(int p_what) {
}
Size2 minsize = c->get_combined_minimum_size();
child_room = MAX(child_room, minsize.width);
if (c != left_container && c != right_container) {
child_room = MAX(child_room, minsize.width);
no_children = false;
}
height = MAX(height, minsize.height);
no_children = false;
}
if (no_children) {
@@ -338,6 +354,13 @@ void EditorProperty::_notification(int p_what) {
}
}
if (rect.size.x > 1) {
rect.size.x -= right_container->get_combined_minimum_size().x;
if (is_layout_rtl()) {
rect.position.x += right_container->get_combined_minimum_size().x;
}
}
if (bottom_editor) {
int v_offset = label.is_empty() ? 0 : _get_v_separation();
bottom_rect = Rect2(0, rect.size.height + v_offset, size.width, bottom_editor->get_combined_minimum_size().height);
@@ -396,6 +419,12 @@ void EditorProperty::_notification(int p_what) {
if (c == bottom_editor) {
continue;
}
if (c == left_container) {
continue;
}
if (c == right_container) {
continue;
}
fit_child_in_rect(c, rect);
right_child_rect = rect;
@@ -406,6 +435,21 @@ void EditorProperty::_notification(int p_what) {
bottom_child_rect = bottom_rect;
}
Size2 rs = right_container->get_combined_minimum_size();
if (is_layout_rtl()) {
fit_child_in_rect(right_container, Rect2(0, 0, rs.width, rs.y));
} else {
fit_child_in_rect(right_container, Rect2(size.width - rs.width, 0, rs.width, rs.y));
}
Size2 ls = left_container->get_combined_minimum_size();
real_t right_size = rect.size.x + rs.x;
if (is_layout_rtl()) {
fit_child_in_rect(left_container, Rect2(right_size, 0, size.x - right_size, ls.y));
} else {
fit_child_in_rect(left_container, Rect2(0, 0, size.x - right_size, ls.y));
}
queue_redraw(); //need to redraw text
} break;
@@ -419,6 +463,8 @@ void EditorProperty::_notification(int p_what) {
size.height = bottom_editor->get_offset(SIDE_TOP) - _get_v_separation();
} else if (label_reference) {
size.height = label_reference->get_size().height;
} else if (label_overlayed) {
size.height = left_container->get_size().height;
}
// Only draw the label if it's not empty.
@@ -456,6 +502,10 @@ void EditorProperty::_notification(int p_what) {
int half_padding = EDITOR_GET("interface/theme/base_spacing");
int padding = half_padding * 2;
int left_ofs = left_container->get_combined_minimum_size().x;
ofs += left_ofs;
text_limit -= left_ofs;
if (checkable) {
Ref<Texture2D> checkbox;
if (checked) {
@@ -549,6 +599,7 @@ void EditorProperty::_notification(int p_what) {
}
ofs = size.width;
ofs -= right_container->get_minimum_size().x;
if (keying) {
Ref<Texture2D> key;
@@ -963,6 +1014,22 @@ bool EditorProperty::is_selected() const {
return selected;
}
void EditorProperty::add_inline_control(Control *p_control, InlineControlSide p_side) {
Node *parent = p_control->get_parent();
if (parent != nullptr) {
parent->remove_child(p_control);
}
if (p_side == INLINE_CONTROL_LEFT) {
left_container->add_child(p_control);
} else {
right_container->add_child(p_control);
}
}
void EditorProperty::set_label_overlayed(bool p_overlay) {
label_overlayed = p_overlay;
}
void EditorProperty::gui_input(const Ref<InputEvent> &p_event) {
ERR_FAIL_COND(p_event.is_null());
@@ -1143,6 +1210,14 @@ void EditorProperty::set_label_reference(Control *p_control) {
label_reference = p_control;
}
HBoxContainer *EditorProperty::get_inline_container(InlineControlSide p_side) {
if (p_side == INLINE_CONTROL_LEFT) {
return left_container;
} else {
return right_container;
}
}
void EditorProperty::set_bottom_editor(Control *p_control) {
bottom_editor = p_control;
if (has_borders) {
@@ -1467,6 +1542,15 @@ EditorProperty::EditorProperty() {
label_reference = nullptr;
bottom_editor = nullptr;
menu = nullptr;
left_container = memnew(HBoxContainer);
left_container->add_theme_constant_override(SNAME("separation"), 0);
add_child(left_container);
right_container = memnew(HBoxContainer);
right_container->add_theme_constant_override(SNAME("separation"), 0);
add_child(right_container);
set_process_shortcut_input(true);
}

View File

@@ -136,6 +136,11 @@ public:
COLORATION_EXTERNAL,
};
enum InlineControlSide {
INLINE_CONTROL_LEFT,
INLINE_CONTROL_RIGHT
};
private:
String label;
int text_size;
@@ -158,6 +163,7 @@ private:
bool draw_prop_warning = false;
bool keying = false;
bool deletable = false;
bool label_overlayed = false;
Rect2 right_child_rect;
Rect2 bottom_child_rect;
@@ -198,6 +204,8 @@ private:
Control *label_reference = nullptr;
Control *bottom_editor = nullptr;
PopupMenu *menu = nullptr;
HBoxContainer *left_container = nullptr;
HBoxContainer *right_container = nullptr;
HashMap<StringName, Variant> cache;
@@ -284,6 +292,10 @@ public:
void deselect();
bool is_selected() const;
void add_inline_control(Control *p_control, InlineControlSide p_side);
HBoxContainer *get_inline_container(InlineControlSide p_side);
void set_label_overlayed(bool p_overlay);
void set_label_reference(Control *p_control);
void set_bottom_editor(Control *p_control);

View File

@@ -305,35 +305,38 @@ void EditorPropertyArray::_create_new_property_slot() {
int idx = slots.size();
HBoxContainer *hbox = memnew(HBoxContainer);
EditorProperty *prop = memnew(EditorPropertyNil);
Button *reorder_button = memnew(Button);
reorder_button->set_accessibility_name(TTRC("Reorder"));
reorder_button->set_button_icon(get_editor_theme_icon(SNAME("TripleBar")));
reorder_button->set_default_cursor_shape(Control::CURSOR_MOVE);
reorder_button->set_disabled(is_read_only());
reorder_button->set_theme_type_variation(SceneStringName(FlatButton));
reorder_button->connect(SceneStringName(gui_input), callable_mp(this, &EditorPropertyArray::_reorder_button_gui_input));
reorder_button->connect(SNAME("button_up"), callable_mp(this, &EditorPropertyArray::_reorder_button_up));
reorder_button->connect(SNAME("button_down"), callable_mp(this, &EditorPropertyArray::_reorder_button_down).bind(idx));
hbox->add_child(reorder_button);
EditorProperty *prop = memnew(EditorPropertyNil);
hbox->add_child(prop);
bool is_untyped_array = object->get_array().get_type() == Variant::ARRAY && subtype == Variant::NIL;
Button *edit_btn = nullptr;
Button *remove_btn = nullptr;
if (is_untyped_array) {
Button *edit_btn = memnew(Button);
edit_btn = memnew(Button);
edit_btn->set_accessibility_name(TTRC("Edit"));
edit_btn->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
edit_btn->set_disabled(is_read_only());
edit_btn->set_theme_type_variation(SceneStringName(FlatButton));
edit_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyArray::_change_type).bind(edit_btn, idx));
hbox->add_child(edit_btn);
} else {
Button *remove_btn = memnew(Button);
remove_btn = memnew(Button);
remove_btn->set_accessibility_name(TTRC("Remove"));
remove_btn->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
remove_btn->set_disabled(is_read_only());
remove_btn->set_theme_type_variation(SceneStringName(FlatButton));
remove_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyArray::_remove_pressed).bind(idx));
hbox->add_child(remove_btn);
}
property_vbox->add_child(hbox);
@@ -342,6 +345,8 @@ void EditorPropertyArray::_create_new_property_slot() {
slot.object = object;
slot.container = hbox;
slot.reorder_button = reorder_button;
slot.edit_button = edit_btn;
slot.remove_button = remove_btn;
slot.set_index(idx + page_index * page_length);
slots.push_back(slot);
}
@@ -513,6 +518,17 @@ void EditorPropertyArray::update_property() {
new_prop->connect(SNAME("object_id_selected"), callable_mp(this, &EditorPropertyArray::_object_id_selected));
new_prop->set_h_size_flags(SIZE_EXPAND_FILL);
new_prop->set_read_only(is_read_only());
if (slot.reorder_button) {
new_prop->add_inline_control(slot.reorder_button, INLINE_CONTROL_LEFT);
slot.reorder_button->get_parent()->move_child(slot.reorder_button, 0);
}
if (slot.edit_button) {
new_prop->add_inline_control(slot.edit_button, INLINE_CONTROL_RIGHT);
} else if (slot.remove_button) {
new_prop->add_inline_control(slot.remove_button, INLINE_CONTROL_RIGHT);
}
slot.prop->add_sibling(new_prop, false);
slot.prop->queue_free();
slot.prop = new_prop;
@@ -1047,33 +1063,34 @@ void EditorPropertyDictionary::_create_new_property_slot(int p_idx) {
EditorProperty *prop_key = nullptr;
if (p_idx != EditorPropertyDictionaryObject::NEW_KEY_INDEX && p_idx != EditorPropertyDictionaryObject::NEW_VALUE_INDEX) {
prop_key = memnew(EditorPropertyNil);
hbox->add_child(prop_key);
}
EditorProperty *prop = memnew(EditorPropertyNil);
prop->set_h_size_flags(SIZE_EXPAND_FILL);
if (p_idx != EditorPropertyDictionaryObject::NEW_KEY_INDEX && p_idx != EditorPropertyDictionaryObject::NEW_VALUE_INDEX) {
prop->set_draw_label(false);
}
hbox->add_child(prop);
if (prop_key) {
prop->add_inline_control(prop_key, INLINE_CONTROL_LEFT);
}
bool use_key = p_idx == EditorPropertyDictionaryObject::NEW_KEY_INDEX;
bool is_untyped_dict = (use_key ? key_subtype : value_subtype) == Variant::NIL;
Button *edit_btn = nullptr;
Button *remove_btn = nullptr;
if (is_untyped_dict) {
Button *edit_btn = memnew(Button);
edit_btn = memnew(Button);
edit_btn->set_accessibility_name(TTRC("Edit"));
edit_btn->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
edit_btn->set_disabled(is_read_only());
edit_btn->set_theme_type_variation(SceneStringName(FlatButton));
edit_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyDictionary::_change_type).bind(edit_btn, slots.size()));
hbox->add_child(edit_btn);
} else if (p_idx >= 0) {
Button *remove_btn = memnew(Button);
remove_btn = memnew(Button);
remove_btn->set_accessibility_name(TTRC("Remove"));
remove_btn->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
remove_btn->set_disabled(is_read_only());
remove_btn->set_theme_type_variation(SceneStringName(FlatButton));
remove_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyDictionary::_remove_pressed).bind(slots.size()));
hbox->add_child(remove_btn);
}
if (add_panel) {
@@ -1087,6 +1104,8 @@ void EditorPropertyDictionary::_create_new_property_slot(int p_idx) {
slot.prop_key = prop_key;
slot.object = object;
slot.container = hbox;
slot.edit_button = edit_btn;
slot.remove_button = remove_btn;
int index = p_idx + (p_idx >= 0 ? page_index * page_length : 0);
slot.set_index(index);
slots.push_back(slot);
@@ -1360,6 +1379,9 @@ void EditorPropertyDictionary::update_property() {
dict_prop->set_preview_value(true);
}
slot.set_key_prop(new_prop);
if (slot.prop) {
slot.prop->add_inline_control(new_prop, INLINE_CONTROL_LEFT);
}
}
}
@@ -1395,10 +1417,22 @@ void EditorPropertyDictionary::update_property() {
new_prop->connect(SNAME("object_id_selected"), callable_mp(this, &EditorPropertyDictionary::_object_id_selected));
new_prop->set_h_size_flags(SIZE_EXPAND_FILL);
if (slot.index != EditorPropertyDictionaryObject::NEW_KEY_INDEX && slot.index != EditorPropertyDictionaryObject::NEW_VALUE_INDEX) {
new_prop->set_draw_label(false);
new_prop->set_label(" ");
new_prop->set_label_overlayed(true);
}
new_prop->set_read_only(is_read_only());
if (slot.remove_button) {
new_prop->add_inline_control(slot.remove_button, INLINE_CONTROL_RIGHT);
}
if (slot.edit_button) {
new_prop->add_inline_control(slot.edit_button, INLINE_CONTROL_RIGHT);
}
if (slot.prop_key) {
new_prop->add_inline_control(slot.prop_key, INLINE_CONTROL_LEFT);
}
slot.set_prop(new_prop);
} else if (slot.index != EditorPropertyDictionaryObject::NEW_KEY_INDEX && slot.index != EditorPropertyDictionaryObject::NEW_VALUE_INDEX) {
Variant key = dict.get_key_at_index(slot.index);
String cs = key.get_construct_string();

View File

@@ -100,6 +100,8 @@ class EditorPropertyArray : public EditorProperty {
bool as_id = false;
EditorProperty *prop = nullptr;
Button *reorder_button = nullptr;
Button *edit_button = nullptr;
Button *remove_button = nullptr;
void set_index(int p_idx) {
String prop_name = "indices/" + itos(p_idx);
@@ -186,6 +188,8 @@ class EditorPropertyDictionary : public EditorProperty {
bool key_as_id = false;
EditorProperty *prop = nullptr;
EditorProperty *prop_key = nullptr;
Button *edit_button = nullptr;
Button *remove_button = nullptr;
String prop_name;
String key_name;