Skip to content

Commit

Permalink
Merge pull request godotengine#17345 from AlexHolly/expose-itemlist-m…
Browse files Browse the repository at this point in the history
…ove-item

expose Itemlist.move_item and optimize functionality
  • Loading branch information
akien-mga authored Mar 13, 2018
2 parents b64d4e3 + 73146af commit fe93459
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
34 changes: 11 additions & 23 deletions scene/gui/item_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,35 +295,21 @@ int ItemList::get_current() const {
return current;
}

void ItemList::move_item(int p_item, int p_to_pos) {
void ItemList::move_item(int p_from_idx, int p_to_idx) {

ERR_FAIL_INDEX(p_item, items.size());
ERR_FAIL_INDEX(p_to_pos, items.size() + 1);
ERR_FAIL_INDEX(p_from_idx, items.size());
ERR_FAIL_INDEX(p_to_idx, items.size());

Item it = items[p_item];
items.remove(p_item);

if (p_to_pos > p_item) {
p_to_pos--;
if (is_anything_selected() && get_selected_items()[0] == p_from_idx) {
current = p_to_idx;
}

if (p_to_pos >= items.size()) {
items.push_back(it);
} else {
items.insert(p_to_pos, it);
}

if (current < 0) {
//do none
} else if (p_item == current) {
current = p_to_pos;
} else if (p_to_pos > p_item && current > p_item && current < p_to_pos) {
current--;
} else if (p_to_pos < p_item && current < p_item && current > p_to_pos) {
current++;
}
Item item = items[p_from_idx];
items.remove(p_from_idx);
items.insert(p_to_idx, item);

update();
shape_changed = true;
}

int ItemList::get_item_count() const {
Expand Down Expand Up @@ -1428,6 +1414,8 @@ void ItemList::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_selected", "idx"), &ItemList::is_selected);
ClassDB::bind_method(D_METHOD("get_selected_items"), &ItemList::get_selected_items);

ClassDB::bind_method(D_METHOD("move_item", "p_from_idx", "p_to_idx"), &ItemList::move_item);

ClassDB::bind_method(D_METHOD("get_item_count"), &ItemList::get_item_count);
ClassDB::bind_method(D_METHOD("remove_item", "idx"), &ItemList::remove_item);

Expand Down
2 changes: 1 addition & 1 deletion scene/gui/item_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class ItemList : public Control {
void set_current(int p_current);
int get_current() const;

void move_item(int p_item, int p_to_pos);
void move_item(int p_from_idx, int p_to_idx);

int get_item_count() const;
void remove_item(int p_idx);
Expand Down

0 comments on commit fe93459

Please sign in to comment.