Skip to content

Commit

Permalink
Add buttons to reorder inspector array items without dragging
Browse files Browse the repository at this point in the history
  • Loading branch information
jmb462 committed Aug 27, 2023
1 parent 6da4ad1 commit a871679
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
33 changes: 31 additions & 2 deletions editor/editor_inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,10 @@ void EditorInspectorArray::_move_element(int p_element_index, int p_to_pos) {
if (page == max_page && (MAX(0, count - 1) / page_length != max_page)) {
emit_signal(SNAME("page_change_request"), max_page - 1);
}
} else if (p_to_pos == begin_array_index - 1) {
emit_signal(SNAME("page_change_request"), page - 1);
} else if (p_to_pos > end_array_index) {
emit_signal(SNAME("page_change_request"), page + 1);
}
begin_array_index = page * page_length;
end_array_index = MIN(count, (page + 1) * page_length);
Expand Down Expand Up @@ -2112,14 +2116,34 @@ void EditorInspectorArray::_setup() {

// Move button.
if (movable) {
int element_position = begin_array_index + i;
VBoxContainer *move_vbox = memnew(VBoxContainer);
move_vbox->set_v_size_flags(SIZE_EXPAND_FILL);
move_vbox->set_alignment(BoxContainer::ALIGNMENT_CENTER);
ae.hbox->add_child(move_vbox);

if (element_position > 0) {
ae.move_up = memnew(Button);
ae.move_up->set_icon(get_theme_icon(SNAME("MoveUp"), SNAME("EditorIcons")));
ae.move_up->connect("pressed", callable_mp(this, &EditorInspectorArray::_move_element).bind(element_position, element_position - 1));
move_vbox->add_child(ae.move_up);
}

ae.move_texture_rect = memnew(TextureRect);
ae.move_texture_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
ae.move_texture_rect->set_default_cursor_shape(Control::CURSOR_MOVE);

if (is_inside_tree()) {
ae.move_texture_rect->set_texture(get_theme_icon(SNAME("TripleBar"), SNAME("EditorIcons")));
}
ae.hbox->add_child(ae.move_texture_rect);
move_vbox->add_child(ae.move_texture_rect);

if (element_position < _get_array_count() - 1) {
ae.move_down = memnew(Button);
ae.move_down->set_icon(get_theme_icon(SNAME("MoveDown"), SNAME("EditorIcons")));
ae.move_down->connect("pressed", callable_mp(this, &EditorInspectorArray::_move_element).bind(element_position, element_position + 2));
move_vbox->add_child(ae.move_down);
}
}

if (numbered) {
Expand Down Expand Up @@ -2220,7 +2244,12 @@ void EditorInspectorArray::_notification(int p_what) {
if (ae.move_texture_rect) {
ae.move_texture_rect->set_texture(get_theme_icon(SNAME("TripleBar"), SNAME("EditorIcons")));
}

if (ae.move_up) {
ae.move_up->set_icon(get_theme_icon(SNAME("MoveUp"), SNAME("EditorIcons")));
}
if (ae.move_down) {
ae.move_down->set_icon(get_theme_icon(SNAME("MoveDown"), SNAME("EditorIcons")));
}
Size2 min_size = get_theme_stylebox(SNAME("Focus"), SNAME("EditorStyles"))->get_minimum_size();
ae.margin->add_theme_constant_override("margin_left", min_size.x / 2);
ae.margin->add_theme_constant_override("margin_top", min_size.y / 2);
Expand Down
2 changes: 2 additions & 0 deletions editor/editor_inspector.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,9 @@ class EditorInspectorArray : public EditorInspectorSection {
PanelContainer *panel = nullptr;
MarginContainer *margin = nullptr;
HBoxContainer *hbox = nullptr;
Button *move_up = nullptr;
TextureRect *move_texture_rect = nullptr;
Button *move_down = nullptr;
Label *number = nullptr;
VBoxContainer *vbox = nullptr;
Button *erase = nullptr;
Expand Down

0 comments on commit a871679

Please sign in to comment.