summaryrefslogtreecommitdiff
path: root/scene/gui/tree.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/gui/tree.cpp')
-rw-r--r--scene/gui/tree.cpp133
1 files changed, 90 insertions, 43 deletions
diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cpp
index 08f1bdff3d..e12044fca2 100644
--- a/scene/gui/tree.cpp
+++ b/scene/gui/tree.cpp
@@ -27,6 +27,7 @@
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
+
#include "tree.h"
#include <limits.h>
@@ -44,12 +45,12 @@
void TreeItem::move_to_top() {
- if (!parent || parent->childs == this)
+ if (!parent || parent->children == this)
return; //already on top
TreeItem *prev = get_prev();
prev->next = next;
- next = parent->childs;
- parent->childs = this;
+ next = parent->children;
+ parent->children = this;
}
void TreeItem::move_to_bottom() {
@@ -64,7 +65,7 @@ void TreeItem::move_to_bottom() {
if (prev) {
prev->next = next;
} else {
- parent->childs = next;
+ parent->children = next;
}
last->next = this;
next = NULL;
@@ -367,10 +368,10 @@ TreeItem *TreeItem::get_next() {
TreeItem *TreeItem::get_prev() {
- if (!parent || parent->childs == this)
+ if (!parent || parent->children == this)
return NULL;
- TreeItem *prev = parent->childs;
+ TreeItem *prev = parent->children;
while (prev && prev->next != this)
prev = prev->next;
@@ -384,7 +385,7 @@ TreeItem *TreeItem::get_parent() {
TreeItem *TreeItem::get_children() {
- return childs;
+ return children;
}
TreeItem *TreeItem::get_prev_visible() {
@@ -401,10 +402,10 @@ TreeItem *TreeItem::get_prev_visible() {
} else {
current = prev;
- while (!current->collapsed && current->childs) {
+ while (!current->collapsed && current->children) {
//go to the very end
- current = current->childs;
+ current = current->children;
while (current->next)
current = current->next;
}
@@ -417,9 +418,9 @@ TreeItem *TreeItem::get_next_visible() {
TreeItem *current = this;
- if (!current->collapsed && current->childs) {
+ if (!current->collapsed && current->children) {
- current = current->childs;
+ current = current->children;
} else if (current->next) {
@@ -443,7 +444,7 @@ TreeItem *TreeItem::get_next_visible() {
void TreeItem::remove_child(TreeItem *p_item) {
ERR_FAIL_NULL(p_item);
- TreeItem **c = &childs;
+ TreeItem **c = &children;
while (*c) {
@@ -783,6 +784,10 @@ void TreeItem::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_disable_folding", "disable"), &TreeItem::set_disable_folding);
ClassDB::bind_method(D_METHOD("is_folding_disabled"), &TreeItem::is_folding_disabled);
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed"), "set_collapsed", "is_collapsed");
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_folding"), "set_disable_folding", "is_folding_disabled");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "custom_minimum_height", PROPERTY_HINT_RANGE, "0,1000,1"), "set_custom_minimum_height", "get_custom_minimum_height");
+
BIND_ENUM_CONSTANT(CELL_MODE_STRING);
BIND_ENUM_CONSTANT(CELL_MODE_CHECK);
BIND_ENUM_CONSTANT(CELL_MODE_RANGE);
@@ -797,16 +802,16 @@ void TreeItem::_bind_methods() {
void TreeItem::clear_children() {
- TreeItem *c = childs;
+ TreeItem *c = children;
while (c) {
TreeItem *aux = c;
c = c->get_next();
- aux->parent = 0; // so it wont try to recursively autoremove from me in here
+ aux->parent = 0; // so it won't try to recursively autoremove from me in here
memdelete(aux);
}
- childs = 0;
+ children = 0;
};
TreeItem::TreeItem(Tree *p_tree) {
@@ -818,7 +823,7 @@ TreeItem::TreeItem(Tree *p_tree) {
parent = 0; // parent item
next = 0; // next in list
- childs = 0; //child items
+ children = 0; //child items
}
TreeItem::~TreeItem() {
@@ -915,6 +920,7 @@ int Tree::compute_item_height(TreeItem *p_item) const {
if (p_item == root && hide_root)
return 0;
+ ERR_FAIL_COND_V(cache.font.is_null(), 0);
int height = cache.font->get_height();
for (int i = 0; i < columns.size(); i++) {
@@ -971,9 +977,9 @@ int Tree::get_item_height(TreeItem *p_item) const {
int height = compute_item_height(p_item);
height += cache.vseparation;
- if (!p_item->collapsed) { /* if not collapsed, check the childs */
+ if (!p_item->collapsed) { /* if not collapsed, check the children */
- TreeItem *c = p_item->childs;
+ TreeItem *c = p_item->children;
while (c) {
@@ -988,6 +994,8 @@ int Tree::get_item_height(TreeItem *p_item) const {
void Tree::draw_item_rect(const TreeItem::Cell &p_cell, const Rect2i &p_rect, const Color &p_color, const Color &p_icon_color) {
+ ERR_FAIL_COND(cache.font.is_null());
+
Rect2i rect = p_rect;
Ref<Font> font = cache.font;
String text = p_cell.text;
@@ -1057,6 +1065,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
//draw separation.
//if (p_item->get_parent()!=root || !hide_root)
+ ERR_FAIL_COND_V(cache.font.is_null(), -1);
Ref<Font> font = cache.font;
int font_ascent = font->get_ascent();
@@ -1377,7 +1386,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
}
}
- if (!p_item->disable_folding && !hide_folding && p_item->childs) { //has childs, draw the guide box
+ if (!p_item->disable_folding && !hide_folding && p_item->children) { //has children, draw the guide box
Ref<Texture> arrow;
@@ -1404,9 +1413,9 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
children_pos.y += htotal;
}
- if (!p_item->collapsed) { /* if not collapsed, check the childs */
+ if (!p_item->collapsed) { /* if not collapsed, check the children */
- TreeItem *c = p_item->childs;
+ TreeItem *c = p_item->children;
while (c) {
@@ -1423,17 +1432,33 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
#endif
Point2i parent_pos = Point2i(parent_ofs - cache.arrow->get_width() / 2, p_pos.y + label_h / 2 + cache.arrow->get_height() / 2) - cache.offset + p_draw_ofs;
- VisualServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x - Math::floor(line_width / 2), root_pos.y), cache.relationship_line_color, line_width);
- VisualServer::get_singleton()->canvas_item_add_line(ci, Point2i(parent_pos.x, root_pos.y), parent_pos, cache.relationship_line_color, line_width);
+
+ if (root_pos.y + line_width >= 0) {
+ VisualServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x - Math::floor(line_width / 2), root_pos.y), cache.relationship_line_color, line_width);
+ VisualServer::get_singleton()->canvas_item_add_line(ci, Point2i(parent_pos.x, root_pos.y), parent_pos, cache.relationship_line_color, line_width);
+ }
+
+ if (htotal < 0) {
+ return -1;
+ }
}
- int child_h = draw_item(children_pos, p_draw_ofs, p_draw_size, c);
+ if (htotal >= 0) {
+ int child_h = draw_item(children_pos, p_draw_ofs, p_draw_size, c);
- if (child_h < 0 && cache.draw_relationship_lines == 0)
- return -1; // break, stop drawing, no need to anymore
+ if (child_h < 0) {
+ if (cache.draw_relationship_lines == 0) {
+ return -1; // break, stop drawing, no need to anymore
+ } else {
+ htotal = -1;
+ children_pos.y = cache.offset.y + p_draw_size.height;
+ }
+ } else {
+ htotal += child_h;
+ children_pos.y += child_h;
+ }
+ }
- htotal += child_h;
- children_pos.y += child_h;
c = c->next;
}
}
@@ -1544,7 +1569,7 @@ void Tree::select_single_item(TreeItem *p_selected, TreeItem *p_current, int p_c
*r_in_range = false;
}
- TreeItem *c = p_current->childs;
+ TreeItem *c = p_current->children;
while (c) {
@@ -1610,7 +1635,7 @@ int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, bool
if (!p_item->disable_folding && !hide_folding && (p_pos.x >= x_ofs && p_pos.x < (x_ofs + cache.item_margin))) {
- if (p_item->childs)
+ if (p_item->children)
p_item->set_collapsed(!p_item->is_collapsed());
return -1; //handled!
@@ -1917,9 +1942,9 @@ int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, bool
new_pos.y -= item_h;
}
- if (!p_item->collapsed) { /* if not collapsed, check the childs */
+ if (!p_item->collapsed) { /* if not collapsed, check the children */
- TreeItem *c = p_item->childs;
+ TreeItem *c = p_item->children;
while (c) {
@@ -2352,8 +2377,6 @@ void Tree::_gui_input(Ref<InputEvent> p_event) {
last_keypress = 0;
}
} break;
-
- last_keypress = 0;
}
}
@@ -2779,6 +2802,7 @@ void Tree::update_scrollbars() {
int Tree::_get_title_button_height() const {
+ ERR_FAIL_COND_V(cache.font.is_null() || cache.title_button.is_null(), 0);
return show_column_titles ? cache.font->get_height() + cache.title_button->get_minimum_size().height : 0;
}
@@ -2964,7 +2988,7 @@ TreeItem *Tree::create_item(TreeItem *p_parent, int p_idx) {
ti->cells.resize(columns.size());
TreeItem *prev = NULL;
- TreeItem *c = p_parent->childs;
+ TreeItem *c = p_parent->children;
int idx = 0;
while (c) {
@@ -2979,7 +3003,7 @@ TreeItem *Tree::create_item(TreeItem *p_parent, int p_idx) {
if (prev)
prev->next = ti;
else
- p_parent->childs = ti;
+ p_parent->children = ti;
ti->parent = p_parent;
} else {
@@ -3012,8 +3036,8 @@ TreeItem *Tree::get_last_item() {
if (last->next)
last = last->next;
- else if (last->childs)
- last = last->childs;
+ else if (last->children)
+ last = last->children;
else
break;
}
@@ -3046,6 +3070,7 @@ void Tree::item_selected(int p_column, TreeItem *p_item) {
p_item->cells[p_column].selected = true;
//emit_signal("multi_selected",p_item,p_column,true); - NO this is for TreeItem::select
+ selected_col = p_column;
} else {
select_single_item(p_item, root, p_column);
@@ -3066,12 +3091,19 @@ void Tree::set_select_mode(SelectMode p_mode) {
select_mode = p_mode;
}
+Tree::SelectMode Tree::get_select_mode() const {
+
+ return select_mode;
+}
+
void Tree::deselect_all() {
TreeItem *item = get_next_selected(get_root());
while (item) {
item->deselect(selected_col);
+ TreeItem *prev_item = item;
item = get_next_selected(get_root());
+ ERR_FAIL_COND(item == prev_item);
}
selected_item = NULL;
@@ -3119,6 +3151,11 @@ void Tree::set_hide_root(bool p_enabled) {
update();
}
+bool Tree::is_root_hidden() const {
+
+ return hide_root;
+}
+
void Tree::set_column_min_width(int p_column, int p_min_width) {
ERR_FAIL_INDEX(p_column, columns.size());
@@ -3171,9 +3208,9 @@ TreeItem *Tree::get_next_selected(TreeItem *p_item) {
p_item = root;
} else {
- if (p_item->childs) {
+ if (p_item->children) {
- p_item = p_item->childs;
+ p_item = p_item->children;
} else if (p_item->next) {
@@ -3288,9 +3325,9 @@ int Tree::get_item_offset(TreeItem *p_item) const {
ofs += compute_item_height(it) + cache.vseparation;
- if (it->childs && !it->collapsed) {
+ if (it->children && !it->collapsed) {
- it = it->childs;
+ it = it->children;
} else if (it->next) {
@@ -3511,7 +3548,7 @@ TreeItem *Tree::_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_
}
if (p_item->is_collapsed())
- return NULL; // do not try childs, it's collapsed
+ return NULL; // do not try children, it's collapsed
TreeItem *n = p_item->get_children();
while (n) {
@@ -3739,11 +3776,13 @@ void Tree::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_column_width", "column"), &Tree::get_column_width);
ClassDB::bind_method(D_METHOD("set_hide_root", "enable"), &Tree::set_hide_root);
+ ClassDB::bind_method(D_METHOD("is_root_hidden"), &Tree::is_root_hidden);
ClassDB::bind_method(D_METHOD("get_next_selected", "from"), &Tree::_get_next_selected);
ClassDB::bind_method(D_METHOD("get_selected"), &Tree::get_selected);
ClassDB::bind_method(D_METHOD("get_selected_column"), &Tree::get_selected_column);
ClassDB::bind_method(D_METHOD("get_pressed_button"), &Tree::get_pressed_button);
ClassDB::bind_method(D_METHOD("set_select_mode", "mode"), &Tree::set_select_mode);
+ ClassDB::bind_method(D_METHOD("get_select_mode"), &Tree::get_select_mode);
ClassDB::bind_method(D_METHOD("set_columns", "amount"), &Tree::set_columns);
ClassDB::bind_method(D_METHOD("get_columns"), &Tree::get_columns);
@@ -3777,6 +3816,14 @@ void Tree::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &Tree::set_allow_reselect);
ClassDB::bind_method(D_METHOD("get_allow_reselect"), &Tree::get_allow_reselect);
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "columns"), "set_columns", "get_columns");
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect");
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_rmb_select"), "set_allow_rmb_select", "get_allow_rmb_select");
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_folding"), "set_hide_folding", "is_folding_hidden");
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_root"), "set_hide_root", "is_root_hidden");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "drop_mode_flags", PROPERTY_HINT_FLAGS, "On Item,In between"), "set_drop_mode_flags", "get_drop_mode_flags");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "select_mode", PROPERTY_HINT_ENUM, "Single,Row,Multi"), "set_select_mode", "get_select_mode");
+
ADD_SIGNAL(MethodInfo("item_selected"));
ADD_SIGNAL(MethodInfo("cell_selected"));
ADD_SIGNAL(MethodInfo("multi_selected", PropertyInfo(Variant::OBJECT, "item"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::BOOL, "selected")));