Merge pull request #14801 from RayKoopa/insert_tree_item

Tree: Insert new item, fix behavior when no parent given
This commit is contained in:
Noshyaar 2017-12-25 08:35:31 +07:00 committed by GitHub
commit aab4434d76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 22 deletions

View File

@ -2950,43 +2950,51 @@ Size2 Tree::get_minimum_size() const {
return Size2(1, 1); return Size2(1, 1);
} }
TreeItem *Tree::create_item(TreeItem *p_parent) { TreeItem *Tree::create_item(TreeItem *p_parent, int p_idx) {
ERR_FAIL_COND_V(blocked > 0, NULL); ERR_FAIL_COND_V(blocked > 0, NULL);
TreeItem *ti = memnew(TreeItem(this)); TreeItem *ti = NULL;
ERR_FAIL_COND_V(!ti, NULL);
ti->cells.resize(columns.size());
if (p_parent) { if (p_parent) {
/* Always append at the end */ // Append or insert a new item to the given parent.
ti = memnew(TreeItem(this));
ERR_FAIL_COND_V(!ti, NULL);
ti->cells.resize(columns.size());
TreeItem *last = 0; TreeItem *prev = NULL;
TreeItem *c = p_parent->childs; TreeItem *c = p_parent->childs;
int idx = 0;
while (c) { while (c) {
if (idx++ == p_idx) {
last = c; ti->next = c;
break;
}
prev = c;
c = c->next; c = c->next;
} }
if (last) { if (prev)
prev->next = ti;
last->next = ti; else
} else {
p_parent->childs = ti; p_parent->childs = ti;
}
ti->parent = p_parent; ti->parent = p_parent;
} else { } else {
if (root) if (!root) {
ti->childs = root; // No root exists, make the given item the new root.
ti = memnew(TreeItem(this));
ERR_FAIL_COND_V(!ti, NULL);
ti->cells.resize(columns.size());
root = ti; root = ti;
} else {
// Root exists, append or insert to root.
ti = create_item(root, p_idx);
}
} }
return ti; return ti;
@ -3723,7 +3731,7 @@ void Tree::_bind_methods() {
ClassDB::bind_method(D_METHOD("_scroll_moved"), &Tree::_scroll_moved); ClassDB::bind_method(D_METHOD("_scroll_moved"), &Tree::_scroll_moved);
ClassDB::bind_method(D_METHOD("clear"), &Tree::clear); ClassDB::bind_method(D_METHOD("clear"), &Tree::clear);
ClassDB::bind_method(D_METHOD("create_item", "parent"), &Tree::_create_item, DEFVAL(Variant())); ClassDB::bind_method(D_METHOD("create_item", "parent", "idx"), &Tree::_create_item, DEFVAL(Variant()), DEFVAL(-1));
ClassDB::bind_method(D_METHOD("get_root"), &Tree::get_root); ClassDB::bind_method(D_METHOD("get_root"), &Tree::get_root);
ClassDB::bind_method(D_METHOD("set_column_min_width", "column", "min_width"), &Tree::set_column_min_width); ClassDB::bind_method(D_METHOD("set_column_min_width", "column", "min_width"), &Tree::set_column_min_width);

View File

@ -511,8 +511,8 @@ protected:
static void _bind_methods(); static void _bind_methods();
//bind helpers //bind helpers
Object *_create_item(Object *p_parent) { Object *_create_item(Object *p_parent, int p_idx = -1) {
return create_item(Object::cast_to<TreeItem>(p_parent)); return create_item(Object::cast_to<TreeItem>(p_parent), p_idx);
} }
TreeItem *_get_next_selected(Object *p_item) { TreeItem *_get_next_selected(Object *p_item) {
@ -532,7 +532,7 @@ public:
void clear(); void clear();
TreeItem *create_item(TreeItem *p_parent = 0); TreeItem *create_item(TreeItem *p_parent = 0, int p_idx = -1);
TreeItem *get_root(); TreeItem *get_root();
TreeItem *get_last_item(); TreeItem *get_last_item();