diff options
Diffstat (limited to 'scene/gui')
-rw-r--r-- | scene/gui/control.cpp | 52 | ||||
-rw-r--r-- | scene/gui/control.h | 7 | ||||
-rw-r--r-- | scene/gui/item_list.cpp | 16 | ||||
-rw-r--r-- | scene/gui/item_list.h | 1 | ||||
-rw-r--r-- | scene/gui/margin_container.cpp | 24 | ||||
-rw-r--r-- | scene/gui/text_edit.cpp | 126 | ||||
-rw-r--r-- | scene/gui/text_edit.h | 13 | ||||
-rw-r--r-- | scene/gui/tree.cpp | 255 | ||||
-rw-r--r-- | scene/gui/tree.h | 32 |
9 files changed, 446 insertions, 80 deletions
diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index 86ec31a9c2..73e7237058 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -1287,14 +1287,13 @@ void Control::set_anchor(Margin p_margin,AnchorType p_anchor, bool p_keep_margin void Control::_set_anchor(Margin p_margin,AnchorType p_anchor) { #ifdef TOOLS_ENABLED - SceneTree *st=OS::get_singleton()->get_main_loop()->cast_to<SceneTree>(); - if (st && st->is_editor_hint()) { + if (is_inside_tree() && get_tree()->is_editor_hint()) { set_anchor(p_margin, p_anchor, EDITOR_DEF("2d_editor/keep_margins_when_changing_anchors", false)); } else { - set_anchor(p_margin, p_anchor); + set_anchor(p_margin, p_anchor, false); } #else - set_anchor(p_margin, p_anchor); + set_anchor(p_margin, p_anchor, false); #endif } @@ -2157,17 +2156,9 @@ bool Control::is_text_field() const { } -void Control::_set_rotation_deg(float p_rot) { - set_rotation(Math::deg2rad(p_rot)); -} - -float Control::_get_rotation_deg() const { - return Math::rad2deg(get_rotation()); -} +void Control::set_rotation(float p_radians) { -void Control::set_rotation(float p_rotation) { - - data.rotation=p_rotation; + data.rotation=p_radians; update(); _notify_transform(); } @@ -2177,6 +2168,25 @@ float Control::get_rotation() const{ return data.rotation; } +void Control::set_rotation_deg(float p_degrees) { + set_rotation(Math::deg2rad(p_degrees)); +} + +float Control::get_rotation_deg() const { + return Math::rad2deg(get_rotation()); +} + +// Kept for compatibility after rename to {s,g}et_rotation_deg. +// Could be removed after a couple releases. +void Control::_set_rotation_deg(float p_degrees) { + WARN_PRINT("Deprecated method Control._set_rotation_deg(): This method was renamed to set_rotation_deg. Please adapt your code accordingly, as the old method will be obsoleted."); + set_rotation_deg(p_degrees); +} +float Control::_get_rotation_deg() const { + WARN_PRINT("Deprecated method Control._get_rotation_deg(): This method was renamed to get_rotation_deg. Please adapt your code accordingly, as the old method will be obsoleted."); + return get_rotation_deg(); +} + void Control::set_scale(const Vector2& p_scale){ data.scale=p_scale; @@ -2232,8 +2242,10 @@ void Control::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_size","size"),&Control::set_size); ObjectTypeDB::bind_method(_MD("set_custom_minimum_size","size"),&Control::set_custom_minimum_size); ObjectTypeDB::bind_method(_MD("set_global_pos","pos"),&Control::set_global_pos); - ObjectTypeDB::bind_method(_MD("set_rotation","rotation"),&Control::set_rotation); - ObjectTypeDB::bind_method(_MD("_set_rotation_deg","rotation"),&Control::_set_rotation_deg); + ObjectTypeDB::bind_method(_MD("set_rotation","radians"),&Control::set_rotation); + ObjectTypeDB::bind_method(_MD("set_rotation_deg","degrees"),&Control::set_rotation_deg); + // TODO: Obsolete this method (old name) properly (GH-4397) + ObjectTypeDB::bind_method(_MD("_set_rotation_deg","degrees"),&Control::_set_rotation_deg); ObjectTypeDB::bind_method(_MD("set_scale","scale"),&Control::set_scale); ObjectTypeDB::bind_method(_MD("get_margin","margin"),&Control::get_margin); ObjectTypeDB::bind_method(_MD("get_begin"),&Control::get_begin); @@ -2241,12 +2253,14 @@ void Control::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_pos"),&Control::get_pos); ObjectTypeDB::bind_method(_MD("get_size"),&Control::get_size); ObjectTypeDB::bind_method(_MD("get_rotation"),&Control::get_rotation); + ObjectTypeDB::bind_method(_MD("get_rotation_deg"),&Control::get_rotation_deg); + // TODO: Obsolete this method (old name) properly (GH-4397) + ObjectTypeDB::bind_method(_MD("_get_rotation_deg"),&Control::_get_rotation_deg); ObjectTypeDB::bind_method(_MD("get_scale"),&Control::get_scale); ObjectTypeDB::bind_method(_MD("get_custom_minimum_size"),&Control::get_custom_minimum_size); ObjectTypeDB::bind_method(_MD("get_parent_area_size"),&Control::get_size); ObjectTypeDB::bind_method(_MD("get_global_pos"),&Control::get_global_pos); ObjectTypeDB::bind_method(_MD("get_rect"),&Control::get_rect); - ObjectTypeDB::bind_method(_MD("_get_rotation_deg"),&Control::_get_rotation_deg); ObjectTypeDB::bind_method(_MD("get_global_rect"),&Control::get_global_rect); ObjectTypeDB::bind_method(_MD("set_area_as_parent_rect","margin"),&Control::set_area_as_parent_rect,DEFVAL(0)); ObjectTypeDB::bind_method(_MD("show_modal","exclusive"),&Control::show_modal,DEFVAL(false)); @@ -2305,7 +2319,7 @@ void Control::_bind_methods() { ObjectTypeDB::bind_method(_MD("grab_click_focus"),&Control::grab_click_focus); - ObjectTypeDB::bind_method(_MD("set_drag_forwarding;","target:Control"),&Control::set_drag_forwarding); + ObjectTypeDB::bind_method(_MD("set_drag_forwarding","target:Control"),&Control::set_drag_forwarding); ObjectTypeDB::bind_method(_MD("set_drag_preview","control:Control"),&Control::set_drag_preview); ObjectTypeDB::bind_method(_MD("warp_mouse","to_pos"),&Control::warp_mouse); @@ -2325,7 +2339,7 @@ void Control::_bind_methods() { ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"rect/pos", PROPERTY_HINT_NONE, "",PROPERTY_USAGE_EDITOR), _SCS("set_pos"),_SCS("get_pos") ); ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"rect/size", PROPERTY_HINT_NONE, "",PROPERTY_USAGE_EDITOR), _SCS("set_size"),_SCS("get_size") ); ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"rect/min_size"), _SCS("set_custom_minimum_size"),_SCS("get_custom_minimum_size") ); - ADD_PROPERTYNZ( PropertyInfo(Variant::REAL,"rect/rotation",PROPERTY_HINT_RANGE,"-1080,1080,0.01"), _SCS("_set_rotation_deg"),_SCS("_get_rotation_deg") ); + ADD_PROPERTYNZ( PropertyInfo(Variant::REAL,"rect/rotation",PROPERTY_HINT_RANGE,"-1080,1080,0.01"), _SCS("set_rotation_deg"),_SCS("get_rotation_deg") ); ADD_PROPERTYNO( PropertyInfo(Variant::VECTOR2,"rect/scale"), _SCS("set_scale"),_SCS("get_scale") ); ADD_PROPERTYNZ( PropertyInfo(Variant::STRING,"hint/tooltip", PROPERTY_HINT_MULTILINE_TEXT), _SCS("set_tooltip"),_SCS("_get_tooltip") ); ADD_PROPERTYINZ( PropertyInfo(Variant::NODE_PATH,"focus_neighbour/left" ), _SCS("set_focus_neighbour"),_SCS("get_focus_neighbour"),MARGIN_LEFT ); diff --git a/scene/gui/control.h b/scene/gui/control.h index aa9a7612a5..f720185c9d 100644 --- a/scene/gui/control.h +++ b/scene/gui/control.h @@ -180,7 +180,8 @@ private: void _size_changed(); String _get_tooltip() const; - void _set_rotation_deg(float p_rot); + // Deprecated, should be removed in a future version. + void _set_rotation_deg(float p_degrees); float _get_rotation_deg() const; friend class Viewport; @@ -275,8 +276,10 @@ public: Rect2 get_global_rect() const; Rect2 get_window_rect() const; ///< use with care, as it blocks waiting for the visual server - void set_rotation(float p_rotation); + void set_rotation(float p_radians); + void set_rotation_deg(float p_degrees); float get_rotation() const; + float get_rotation_deg() const; void set_scale(const Vector2& p_scale); Vector2 get_scale() const; diff --git a/scene/gui/item_list.cpp b/scene/gui/item_list.cpp index 6a251a5ac5..bf66a770d3 100644 --- a/scene/gui/item_list.cpp +++ b/scene/gui/item_list.cpp @@ -462,7 +462,7 @@ void ItemList::_input_event(const InputEvent& p_event) { } } else { - if (!mb.mod.command && select_mode==SELECT_MULTI && items[i].selectable && items[i].selected) { + if (!mb.doubleclick && !mb.mod.command && select_mode==SELECT_MULTI && items[i].selectable && items[i].selected) { defer_select_single=i; return; } @@ -1044,8 +1044,7 @@ void ItemList::_scroll_changed(double) { update(); } - -String ItemList::get_tooltip(const Point2& p_pos) const { +int ItemList::get_item_at_pos(const Point2& p_pos) const { Vector2 pos=p_pos; Ref<StyleBox> bg = get_stylebox("bg"); @@ -1074,6 +1073,13 @@ String ItemList::get_tooltip(const Point2& p_pos) const { } } + return closest; +} + +String ItemList::get_tooltip(const Point2& p_pos) const { + + int closest = get_item_at_pos(p_pos); + if (closest!=-1) { if (items[closest].tooltip!="") { return items[closest].tooltip; @@ -1084,8 +1090,6 @@ String ItemList::get_tooltip(const Point2& p_pos) const { } return Control::get_tooltip(p_pos); - - } void ItemList::sort_items_by_text() { @@ -1170,6 +1174,8 @@ void ItemList::_bind_methods(){ ObjectTypeDB::bind_method(_MD("set_min_icon_size","size"),&ItemList::set_min_icon_size); ObjectTypeDB::bind_method(_MD("get_min_icon_size"),&ItemList::get_min_icon_size); + ObjectTypeDB::bind_method(_MD("get_item_at_pos","pos"),&ItemList::get_item_at_pos); + ObjectTypeDB::bind_method(_MD("ensure_current_is_visible"),&ItemList::ensure_current_is_visible); ObjectTypeDB::bind_method(_MD("_scroll_changed"),&ItemList::_scroll_changed); diff --git a/scene/gui/item_list.h b/scene/gui/item_list.h index 5aec946686..fcb4dfae5a 100644 --- a/scene/gui/item_list.h +++ b/scene/gui/item_list.h @@ -140,6 +140,7 @@ public: int find_metadata(const Variant& p_metadata) const; virtual String get_tooltip(const Point2& p_pos) const; + int get_item_at_pos(const Point2& p_pos) const; ItemList(); ~ItemList(); diff --git a/scene/gui/margin_container.cpp b/scene/gui/margin_container.cpp index fde5df6b72..5f798f445c 100644 --- a/scene/gui/margin_container.cpp +++ b/scene/gui/margin_container.cpp @@ -31,7 +31,10 @@ Size2 MarginContainer::get_minimum_size() const { - int margin = get_constant("margin"); + int margin_left = get_constant("margin_left"); + int margin_top = get_constant("margin_top"); + int margin_right = get_constant("margin_right"); + int margin_bottom = get_constant("margin_bottom"); Size2 max; @@ -52,9 +55,10 @@ Size2 MarginContainer::get_minimum_size() const { max.height = s.height; } - max.width += margin; - return max; + max.width += (margin_left + margin_right); + max.height += (margin_top + margin_bottom); + return max; } @@ -62,7 +66,10 @@ void MarginContainer::_notification(int p_what) { if (p_what==NOTIFICATION_SORT_CHILDREN) { - int margin = get_constant("margin"); + int margin_left = get_constant("margin_left"); + int margin_top = get_constant("margin_top"); + int margin_right = get_constant("margin_right"); + int margin_bottom = get_constant("margin_bottom"); Size2 s = get_size(); @@ -73,13 +80,10 @@ void MarginContainer::_notification(int p_what) { continue; if (c->is_set_as_toplevel()) continue; - int w; - if (c->get_h_size_flags() & Control::SIZE_EXPAND) - w=s.width-margin; - else - w=c->get_combined_minimum_size().width; - fit_child_in_rect(c,Rect2(margin,0,s.width-margin,s.height)); + int w=s.width-margin_left-margin_right; + int h=s.height-margin_top-margin_bottom; + fit_child_in_rect(c,Rect2(margin_left,margin_top,w,h)); } } diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index 05f7725a92..8df0c6d254 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -921,14 +921,18 @@ void TextEdit::_notification(int p_what) { if (cursor.column==j && cursor.line==line) { cursor_pos = Point2i( char_ofs+char_margin, ofs_y ); + if (insert_mode) { cursor_pos.y += get_row_height(); - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(char_w,1)),cache.caret_color); - } else { - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(1,get_row_height())),cache.caret_color); } - + if (draw_caret) { + if (insert_mode) { + VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(char_w,1)),cache.caret_color); + } else { + VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(1,get_row_height())),cache.caret_color); + } + } } char_ofs+=char_w; @@ -937,12 +941,18 @@ void TextEdit::_notification(int p_what) { if (cursor.column==str.length() && cursor.line==line && (char_ofs+char_margin)>=xmargin_beg) { cursor_pos=Point2i( char_ofs+char_margin, ofs_y ); + if (insert_mode) { cursor_pos.y += get_row_height(); - int char_w = cache.font->get_char_size(' ').width; - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(char_w,1)),cache.caret_color); - } else { - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(1,get_row_height())),cache.caret_color); + } + + if (draw_caret) { + if (insert_mode) { + int char_w = cache.font->get_char_size(' ').width; + VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(char_w,1)),cache.caret_color); + } else { + VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(1,get_row_height())),cache.caret_color); + } } } } @@ -1390,6 +1400,8 @@ void TextEdit::_input_event(const InputEvent& p_input_event) { } if (mb.button_index==BUTTON_LEFT) { + _reset_caret_blink_timer(); + int row,col; _get_mouse_pos(Point2i(mb.x,mb.y), row,col); @@ -1524,6 +1536,8 @@ void TextEdit::_input_event(const InputEvent& p_input_event) { if (selection.selecting_mode!=Selection::MODE_NONE) { + _reset_caret_blink_timer(); + int row,col; _get_mouse_pos(Point2i(mm.x,mm.y), row,col); @@ -1644,6 +1658,8 @@ void TextEdit::_input_event(const InputEvent& p_input_event) { if (k.scancode==KEY_BACKSPACE) { + _reset_caret_blink_timer(); + backspace_at_cursor(); _update_completion_candidates(); accept_event(); @@ -1659,35 +1675,29 @@ void TextEdit::_input_event(const InputEvent& p_input_event) { if (k.unicode>32) { - if (cursor.column<text[cursor.line].length() && text[cursor.line][cursor.column]==k.unicode) { - //same char, move ahead - cursor_set_column(cursor.column+1); + _reset_caret_blink_timer(); + const CharType chr[2] = {(CharType)k.unicode, 0}; + if(auto_brace_completion_enabled && _is_pair_symbol(chr[0])) { + _consume_pair_symbol(chr[0]); } else { - //different char, go back - const CharType chr[2] = {(CharType)k.unicode, 0}; - if(auto_brace_completion_enabled && _is_pair_symbol(chr[0])) { - _consume_pair_symbol(chr[0]); - } else { - // remove the old character if in insert mode - if (insert_mode) { - begin_complex_operation(); + // remove the old character if in insert mode + if (insert_mode) { + begin_complex_operation(); - // make sure we don't try and remove empty space - if (cursor.column < get_line(cursor.line).length()) { - _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1); - } + // make sure we don't try and remove empty space + if (cursor.column < get_line(cursor.line).length()) { + _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1); } + } - _insert_text_at_cursor(chr); + _insert_text_at_cursor(chr); - if (insert_mode) { - end_complex_operation(); - } + if (insert_mode) { + end_complex_operation(); } } - _update_completion_candidates(); accept_event(); @@ -1713,6 +1723,9 @@ void TextEdit::_input_event(const InputEvent& p_input_event) { k.mod.shift=false; } + if (!k.mod.command) { + _reset_caret_blink_timer(); + } // save here for insert mode, just in case it is cleared in the following section bool had_selection = selection.active; @@ -2910,6 +2923,30 @@ int TextEdit::cursor_get_line() const { return cursor.line; } +bool TextEdit::cursor_get_blink_enabled() const { + return caret_blink_enabled; +} + +void TextEdit::cursor_set_blink_enabled(const bool p_enabled) { + caret_blink_enabled = p_enabled; + + if (p_enabled) { + caret_blink_timer->start(); + } else { + caret_blink_timer->stop(); + } + draw_caret = true; +} + + +float TextEdit::cursor_get_blink_speed() const { + return caret_blink_timer->get_wait_time(); +} + +void TextEdit::cursor_set_blink_speed(const float p_speed) { + ERR_FAIL_COND(p_speed <= 0); + caret_blink_timer->set_wait_time(p_speed); +} void TextEdit::_scroll_moved(double p_to_val) { @@ -3125,6 +3162,20 @@ void TextEdit::set_max_chars(int p_max_chars) { max_chars=p_max_chars; } +void TextEdit::_reset_caret_blink_timer() { + if (caret_blink_enabled) { + caret_blink_timer->stop(); + caret_blink_timer->start(); + draw_caret = true; + update(); + } +} + +void TextEdit::_toggle_draw_caret() { + draw_caret = !draw_caret; + update(); +} + void TextEdit::_update_caches() { cache.style_normal=get_stylebox("normal"); @@ -3911,6 +3962,9 @@ void TextEdit::_update_completion_candidates() { } } + if (l[cursor.column - 1] == '(' && !pre_keyword && !completion_strings[0].begins_with("\"")) { + cancel = true; + } update(); @@ -4101,6 +4155,7 @@ void TextEdit::_bind_methods() { ObjectTypeDB::bind_method(_MD("_text_changed_emit"),&TextEdit::_text_changed_emit); ObjectTypeDB::bind_method(_MD("_push_current_op"),&TextEdit::_push_current_op); ObjectTypeDB::bind_method(_MD("_click_selection_held"),&TextEdit::_click_selection_held); + ObjectTypeDB::bind_method(_MD("_toggle_draw_caret"),&TextEdit::_toggle_draw_caret); BIND_CONSTANT( SEARCH_MATCH_CASE ); BIND_CONSTANT( SEARCH_WHOLE_WORDS ); @@ -4123,7 +4178,10 @@ void TextEdit::_bind_methods() { ObjectTypeDB::bind_method(_MD("cursor_get_column"),&TextEdit::cursor_get_column); ObjectTypeDB::bind_method(_MD("cursor_get_line"),&TextEdit::cursor_get_line); - + ObjectTypeDB::bind_method(_MD("cursor_set_blink_enabled", "enable"),&TextEdit::cursor_set_blink_enabled); + ObjectTypeDB::bind_method(_MD("cursor_get_blink_enabled"),&TextEdit::cursor_get_blink_enabled); + ObjectTypeDB::bind_method(_MD("cursor_set_blink_speed", "blink_speed"),&TextEdit::cursor_set_blink_speed); + ObjectTypeDB::bind_method(_MD("cursor_get_blink_speed"),&TextEdit::cursor_get_blink_speed); ObjectTypeDB::bind_method(_MD("set_readonly","enable"),&TextEdit::set_readonly); ObjectTypeDB::bind_method(_MD("set_wrap","enable"),&TextEdit::set_wrap); @@ -4158,6 +4216,8 @@ void TextEdit::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_custom_bg_color","color"),&TextEdit::set_custom_bg_color); ObjectTypeDB::bind_method(_MD("clear_colors"),&TextEdit::clear_colors); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret/caret_blink"), _SCS("cursor_set_blink_enabled"), _SCS("cursor_get_blink_enabled"));; + ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "caret/caret_blink_speed",PROPERTY_HINT_RANGE,"0.1,10,0.1"), _SCS("cursor_set_blink_speed"),_SCS("cursor_get_blink_speed") ); ADD_SIGNAL(MethodInfo("cursor_changed")); ADD_SIGNAL(MethodInfo("text_changed")); @@ -4170,6 +4230,7 @@ TextEdit::TextEdit() { readonly=false; setting_row=false; draw_tabs=false; + draw_caret=true; max_chars=0; clear(); wrap=false; @@ -4209,6 +4270,13 @@ TextEdit::TextEdit() { selection.active=false; syntax_coloring=false; + caret_blink_enabled=false; + caret_blink_timer = memnew(Timer); + add_child(caret_blink_timer); + caret_blink_timer->set_wait_time(0.65); + caret_blink_timer->connect("timeout", this,"_toggle_draw_caret"); + cursor_set_blink_enabled(false); + custom_bg_color=Color(0,0,0,0); idle_detect = memnew( Timer ); add_child(idle_detect); diff --git a/scene/gui/text_edit.h b/scene/gui/text_edit.h index 193dd236d1..ea4f148e91 100644 --- a/scene/gui/text_edit.h +++ b/scene/gui/text_edit.h @@ -210,6 +210,10 @@ class TextEdit : public Control { bool syntax_coloring; int tab_size; + Timer *caret_blink_timer; + bool caret_blink_enabled; + bool draw_caret; + bool setting_row; bool wrap; bool draw_tabs; @@ -267,6 +271,9 @@ class TextEdit : public Control { int get_row_height() const; + void _reset_caret_blink_timer(); + void _toggle_draw_caret(); + void _update_caches(); void _cursor_changed_emit(); void _text_changed_emit(); @@ -364,6 +371,12 @@ public: int cursor_get_column() const; int cursor_get_line() const; + bool cursor_get_blink_enabled() const; + void cursor_set_blink_enabled(const bool p_enabled); + + float cursor_get_blink_speed() const; + void cursor_set_blink_speed(const float p_speed); + void set_readonly(bool p_readonly); void set_max_chars(int p_max_chars); diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cpp index c4efe82eaa..062a377cee 100644 --- a/scene/gui/tree.cpp +++ b/scene/gui/tree.cpp @@ -604,10 +604,11 @@ String TreeItem::get_tooltip(int p_column) const{ return cells[p_column].tooltip; } -void TreeItem::set_custom_bg_color(int p_column,const Color& p_color) { +void TreeItem::set_custom_bg_color(int p_column,const Color& p_color,bool p_bg_outline) { ERR_FAIL_INDEX( p_column, cells.size() ); cells[p_column].custom_bg_color=true; + cells[p_column].custom_bg_outline=p_bg_outline; cells[p_column].bg_color=p_color; _changed_notify(p_column); } @@ -685,7 +686,7 @@ void TreeItem::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_custom_color","column","color"),&TreeItem::set_custom_color); ObjectTypeDB::bind_method(_MD("clear_custom_color","column"),&TreeItem::clear_custom_color); - ObjectTypeDB::bind_method(_MD("set_custom_bg_color","column","color"),&TreeItem::set_custom_bg_color); + ObjectTypeDB::bind_method(_MD("set_custom_bg_color","column","color","just_outline"),&TreeItem::set_custom_bg_color,DEFVAL(false)); ObjectTypeDB::bind_method(_MD("clear_custom_bg_color","column"),&TreeItem::clear_custom_bg_color); ObjectTypeDB::bind_method(_MD("get_custom_bg_color","column"),&TreeItem::get_custom_bg_color); @@ -757,6 +758,13 @@ TreeItem::~TreeItem() { if (tree && tree->selected_item==this) tree->selected_item=NULL; + + if (tree && tree->drop_mode_over==this) + tree->drop_mode_over=NULL; + + if (tree && tree->single_select_defer==this) + tree->single_select_defer=NULL; + if (tree && tree->edited_item==this) { tree->edited_item=NULL; tree->pressing_for_editor=false; @@ -797,17 +805,13 @@ void Tree::update_cache() { cache.font_color=get_color("font_color"); cache.font_color_selected=get_color("font_color_selected"); cache.guide_color=get_color("guide_color"); + cache.drop_position_color=get_color("drop_position_color"); cache.hseparation=get_constant("hseparation"); cache.vseparation=get_constant("vseparation"); cache.item_margin=get_constant("item_margin"); cache.button_margin=get_constant("button_margin"); cache.guide_width=get_constant("guide_width"); - Ref<StyleBox> title_button; - Ref<StyleBox> title_button_hover; - Ref<StyleBox> title_button_pressed; - Color title_button_color; - cache.title_button = get_stylebox("title_button_normal"); cache.title_button_pressed = get_stylebox("title_button_pressed"); cache.title_button_hover = get_stylebox("title_button_hover"); @@ -1087,7 +1091,34 @@ int Tree::draw_item(const Point2i& p_pos,const Point2& p_draw_ofs, const Size2& Rect2 r=cell_rect; r.pos.x-=cache.hseparation; r.size.x+=cache.hseparation; - VisualServer::get_singleton()->canvas_item_add_rect(ci,r,p_item->cells[i].bg_color); + if (p_item->cells[i].custom_bg_outline) { + VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(r.pos.x,r.pos.y,r.size.x,1),p_item->cells[i].bg_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(r.pos.x,r.pos.y+r.size.y-1,r.size.x,1),p_item->cells[i].bg_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(r.pos.x,r.pos.y,1,r.size.y),p_item->cells[i].bg_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(r.pos.x+r.size.x-1,r.pos.y,1,r.size.y),p_item->cells[i].bg_color); + } else { + VisualServer::get_singleton()->canvas_item_add_rect(ci,r,p_item->cells[i].bg_color); + } + } + + if (drop_mode_flags && drop_mode_over==p_item) { + + Rect2 r=cell_rect; + + if (drop_mode_section==-1 || drop_mode_section==0) { + VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(r.pos.x,r.pos.y,r.size.x,1),cache.drop_position_color); + + } + + if (drop_mode_section==0) { + VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(r.pos.x,r.pos.y,1,r.size.y),cache.drop_position_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(r.pos.x+r.size.x-1,r.pos.y,1,r.size.y),cache.drop_position_color); + + } + + if (drop_mode_section==1 || drop_mode_section==0) { + VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(r.pos.x,r.pos.y+r.size.y,r.size.x,1),cache.drop_position_color); + } } Color col=p_item->cells[i].custom_color?p_item->cells[i].color:get_color( p_item->cells[i].selected?"font_color_selected":"font_color"); @@ -1280,6 +1311,25 @@ int Tree::draw_item(const Point2i& p_pos,const Point2& p_draw_ofs, const Size2& } +int Tree::_count_selected_items(TreeItem* p_from) const { + + int count=0; + for(int i=0;i<columns.size();i++) { + if (p_from->is_selected(i)) + count++; + } + + if (p_from->get_children()) { + count+=_count_selected_items(p_from->get_children()); + } + + if (p_from->get_next()) { + count+=_count_selected_items(p_from->get_next()); + } + + return count; + +} void Tree::select_single_item(TreeItem *p_selected,TreeItem *p_current,int p_col,TreeItem *p_prev,bool *r_in_range) { TreeItem::Cell &selected_cell=p_selected->cells[p_col]; @@ -1548,7 +1598,15 @@ int Tree::propagate_mouse_event(const Point2i &p_pos,int x_ofs,int y_ofs,bool p_ select_single_item( p_item, root, col,selected_item,&inrange ); } else { - select_single_item( p_item, root, col ); + + int icount = _count_selected_items(root); + + if (select_mode==SELECT_MULTI && icount>1) { + single_select_defer=p_item; + single_select_defer_column=col; + } else { + select_single_item( p_item, root, col ); + } } //if (!c.selected && select_mode==SELECT_MULTI) { @@ -2159,6 +2217,31 @@ void Tree::_input_event(InputEvent p_event) { } + if (drop_mode_flags && root) { + + Point2 mpos=Point2(b.x,b.y); + mpos -= cache.bg->get_offset(); + mpos.y-=_get_title_button_height(); + if (mpos.y>=0) { + + if (h_scroll->is_visible()) + mpos.x+=h_scroll->get_val(); + if (v_scroll->is_visible()) + mpos.y+=v_scroll->get_val(); + + int col,h,section; + TreeItem *it = _find_item_at_pos(root,mpos,col,h,section); + + if (it!=drop_mode_over || section!=drop_mode_section) { + drop_mode_over=it; + drop_mode_section=section; + update(); + } + } + } + + + if (cache.hover_type!=old_hover || cache.hover_index!=old_index) { update(); } @@ -2211,6 +2294,12 @@ void Tree::_input_event(InputEvent p_event) { if (b.button_index==BUTTON_LEFT) { + + if (single_select_defer) { + select_single_item( single_select_defer, root, single_select_defer_column ); + single_select_defer=NULL; + } + range_click_timer->stop(); if (pressing_for_editor) { @@ -2522,6 +2611,15 @@ void Tree::_notification(int p_what) { update_cache();; } + if (p_what==NOTIFICATION_DRAG_END) { + + drop_mode_flags=0; + update(); + } + if (p_what==NOTIFICATION_DRAG_BEGIN) { + + single_select_defer=NULL; + } if (p_what==NOTIFICATION_FIXED_PROCESS) { if (drag_touching) { @@ -3135,7 +3233,7 @@ void Tree::_do_incr_search(const String& p_add) { } -TreeItem* Tree::_find_item_at_pos(TreeItem*p_item, const Point2& p_pos,int& r_column,int &h) const { +TreeItem* Tree::_find_item_at_pos(TreeItem*p_item, const Point2& p_pos,int& r_column,int &h,int §ion) const { Point2 pos = p_pos; @@ -3145,15 +3243,33 @@ TreeItem* Tree::_find_item_at_pos(TreeItem*p_item, const Point2& p_pos,int& r_co h = compute_item_height(p_item)+cache.vseparation;; if (pos.y<h) { + if (drop_mode_flags==DROP_MODE_ON_ITEM) { + section=0; + } else if (drop_mode_flags==DROP_MODE_INBETWEEN) { + section=pos.y<h/2?-1:1; + } else if (pos.y<h/4) { + section=-1; + } else if (pos.y>=(h*3/4)) { + section=1; + } else { + section=0; + } + for(int i=0;i<columns.size();i++) { int w = get_column_width(i); if (pos.x < w) { r_column=i; + + return p_item; } pos.x-=w; } + + + + return NULL; } else { @@ -3172,7 +3288,7 @@ TreeItem* Tree::_find_item_at_pos(TreeItem*p_item, const Point2& p_pos,int& r_co int ch; - TreeItem *r = _find_item_at_pos(n,pos,r_column,ch); + TreeItem *r = _find_item_at_pos(n,pos,r_column,ch,section); pos.y-=ch; h+=ch; if (r) @@ -3184,6 +3300,88 @@ TreeItem* Tree::_find_item_at_pos(TreeItem*p_item, const Point2& p_pos,int& r_co } +int Tree::get_column_at_pos(const Point2& p_pos) const { + + if (root) { + + Point2 pos=p_pos; + pos -= cache.bg->get_offset(); + pos.y-=_get_title_button_height(); + if (pos.y<0) + return -1; + + if (h_scroll->is_visible()) + pos.x+=h_scroll->get_val(); + if (v_scroll->is_visible()) + pos.y+=v_scroll->get_val(); + + int col,h,section; + TreeItem *it = _find_item_at_pos(root,pos,col,h,section); + + if (it) { + return col; + } + } + + return -1; + +} + +int Tree::get_drop_section_at_pos(const Point2& p_pos) const { + + if (root) { + + Point2 pos=p_pos; + pos -= cache.bg->get_offset(); + pos.y-=_get_title_button_height(); + if (pos.y<0) + return -100; + + if (h_scroll->is_visible()) + pos.x+=h_scroll->get_val(); + if (v_scroll->is_visible()) + pos.y+=v_scroll->get_val(); + + int col,h,section; + TreeItem *it = _find_item_at_pos(root,pos,col,h,section); + + if (it) { + return section; + } + } + + return -100; + +} +TreeItem* Tree::get_item_at_pos(const Point2& p_pos) const { + + + if (root) { + + Point2 pos=p_pos; + pos -= cache.bg->get_offset(); + pos.y-=_get_title_button_height(); + if (pos.y<0) + return NULL; + + if (h_scroll->is_visible()) + pos.x+=h_scroll->get_val(); + if (v_scroll->is_visible()) + pos.y+=v_scroll->get_val(); + + int col,h,section; + TreeItem *it = _find_item_at_pos(root,pos,col,h,section); + + if (it) { + + return it; + } + } + + return NULL; + +} + String Tree::get_tooltip(const Point2& p_pos) const { if (root) { @@ -3199,8 +3397,8 @@ String Tree::get_tooltip(const Point2& p_pos) const { if (v_scroll->is_visible()) pos.y+=v_scroll->get_val(); - int col,h; - TreeItem *it = _find_item_at_pos(root,pos,col,h); + int col,h,section; + TreeItem *it = _find_item_at_pos(root,pos,col,h,section); if (it) { @@ -3241,6 +3439,22 @@ void Tree::set_value_evaluator(ValueEvaluator *p_evaluator) { evaluator = p_evaluator; } +void Tree::set_drop_mode_flags(int p_flags) { + if (drop_mode_flags==p_flags) + return; + drop_mode_flags=p_flags; + if (drop_mode_flags==0) { + drop_mode_over=NULL; + } + + update(); +} + +int Tree::get_drop_mode_flags() const { + + return drop_mode_flags; +} + void Tree::_bind_methods() { ObjectTypeDB::bind_method(_MD("_range_click_timeout"),&Tree::_range_click_timeout); @@ -3273,6 +3487,8 @@ void Tree::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_edited_column"),&Tree::get_edited_column); ObjectTypeDB::bind_method(_MD("get_custom_popup_rect"),&Tree::get_custom_popup_rect); ObjectTypeDB::bind_method(_MD("get_item_area_rect","item:TreeItem","column"),&Tree::_get_item_rect,DEFVAL(-1)); + ObjectTypeDB::bind_method(_MD("get_item_at_pos:TreeItem","pos"),&Tree::get_item_at_pos); + ObjectTypeDB::bind_method(_MD("get_column_at_pos","pos"),&Tree::get_column_at_pos); ObjectTypeDB::bind_method(_MD("ensure_cursor_is_visible"),&Tree::ensure_cursor_is_visible); @@ -3286,6 +3502,9 @@ void Tree::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_hide_folding","hide"),&Tree::set_hide_folding); ObjectTypeDB::bind_method(_MD("is_folding_hidden"),&Tree::is_folding_hidden); + ObjectTypeDB::bind_method(_MD("set_drop_mode_flags","flags"),&Tree::set_drop_mode_flags); + ObjectTypeDB::bind_method(_MD("get_drop_mode_flags"),&Tree::get_drop_mode_flags); + ADD_SIGNAL( MethodInfo("item_selected")); ADD_SIGNAL( MethodInfo("cell_selected")); @@ -3300,6 +3519,11 @@ void Tree::_bind_methods() { BIND_CONSTANT( SELECT_SINGLE ); BIND_CONSTANT( SELECT_ROW ); BIND_CONSTANT( SELECT_MULTI ); + + BIND_CONSTANT( DROP_MODE_DISABLED ); + BIND_CONSTANT( DROP_MODE_ON_ITEM ); + BIND_CONSTANT( DROP_MODE_INBETWEEN ); + } Tree::Tree() { @@ -3381,6 +3605,11 @@ Tree::Tree() { hide_folding=false; evaluator=NULL; + + drop_mode_flags=0; + drop_mode_over=NULL; + drop_mode_section=0; + single_select_defer=NULL; } diff --git a/scene/gui/tree.h b/scene/gui/tree.h index 75bcfd8e33..43a913392e 100644 --- a/scene/gui/tree.h +++ b/scene/gui/tree.h @@ -79,7 +79,9 @@ friend class Tree; bool custom_color; Color color; bool custom_bg_color; + bool custom_bg_outline; Color bg_color; + Variant meta; String tooltip; @@ -226,7 +228,7 @@ public: Color get_custom_color(int p_column) const; void clear_custom_color(int p_column); - void set_custom_bg_color(int p_column,const Color& p_color); + void set_custom_bg_color(int p_column, const Color& p_color, bool p_bg_outline=false); void clear_custom_bg_color(int p_column); Color get_custom_bg_color(int p_column) const; @@ -257,6 +259,12 @@ public: SELECT_MULTI }; + enum DropModeFlags { + DROP_MODE_DISABLED=0, + DROP_MODE_ON_ITEM=1, + DROP_MODE_INBETWEEN=2 + }; + private: friend class TreeItem; @@ -265,6 +273,11 @@ friend class TreeItem; TreeItem *selected_item; TreeItem *edited_item; + TreeItem *drop_mode_over; + int drop_mode_section; + + TreeItem *single_select_defer; + int single_select_defer_column; int pressed_button; bool pressing_for_editor; @@ -289,6 +302,8 @@ friend class TreeItem; int blocked; + int drop_mode_flags; + struct ColumnInfo { int min_width; @@ -361,6 +376,8 @@ friend class TreeItem; Color font_color; Color font_color_selected; Color guide_color; + Color drop_position_color; + int hseparation; int vseparation; int item_margin; @@ -405,7 +422,7 @@ friend class TreeItem; TreeItem* _search_item_text(TreeItem *p_at, const String& p_find,int *r_col,bool p_selectable,bool p_backwards=false); - TreeItem* _find_item_at_pos(TreeItem *p_current, const Point2& p_pos,int& r_column,int &h) const; + TreeItem* _find_item_at_pos(TreeItem *p_current, const Point2& p_pos, int& r_column, int &h, int §ion) const; /* float drag_speed; float drag_accum; @@ -426,6 +443,8 @@ friend class TreeItem; ValueEvaluator *evaluator; + int _count_selected_items(TreeItem* p_from) const; + protected: static void _bind_methods(); @@ -433,10 +452,16 @@ protected: Object* _create_item(Object *p_parent) { return create_item(p_parent->cast_to<TreeItem>() ); } TreeItem *_get_next_selected(Object *p_item) { return get_next_selected(p_item->cast_to<TreeItem>() ); } Rect2 _get_item_rect(Object *p_item,int p_column) const { return get_item_rect(p_item->cast_to<TreeItem>(),p_column ); } + + public: virtual String get_tooltip(const Point2& p_pos) const; + TreeItem* get_item_at_pos(const Point2& p_pos) const; + int get_column_at_pos(const Point2& p_pos) const; + int get_drop_section_at_pos(const Point2& p_pos) const; + void clear(); TreeItem* create_item(TreeItem *p_parent=0); @@ -486,6 +511,9 @@ public: void set_hide_folding(bool p_hide); bool is_folding_hidden() const; + void set_drop_mode_flags(int p_flags); + int get_drop_mode_flags() const; + void set_value_evaluator(ValueEvaluator *p_evaluator); Tree(); |