diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2017-03-05 16:44:50 +0100 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2017-03-05 16:44:50 +0100 |
commit | 5dbf1809c6e3e905b94b8764e99491e608122261 (patch) | |
tree | 5e5a5360db15d86d59ec8c6e4f7eb511388c5a9a /scene/gui | |
parent | 45438e9918d421b244bfd7776a30e67dc7f2d3e3 (diff) |
A Whole New World (clang-format edition)
I can show you the code
Pretty, with proper whitespace
Tell me, coder, now when did
You last write readable code?
I can open your eyes
Make you see your bad indent
Force you to respect the style
The core devs agreed upon
A whole new world
A new fantastic code format
A de facto standard
With some sugar
Enforced with clang-format
A whole new world
A dazzling style we all dreamed of
And when we read it through
It's crystal clear
That now we're in a whole new world of code
Diffstat (limited to 'scene/gui')
99 files changed, 10149 insertions, 11992 deletions
diff --git a/scene/gui/base_button.cpp b/scene/gui/base_button.cpp index 79b607e2e6..eceee8e317 100644 --- a/scene/gui/base_button.cpp +++ b/scene/gui/base_button.cpp @@ -29,17 +29,16 @@ #include "base_button.h" #include "os/keyboard.h" #include "print_string.h" -#include "scene/scene_string_names.h" #include "scene/main/viewport.h" - +#include "scene/scene_string_names.h" void BaseButton::_unpress_group() { if (!button_group.is_valid()) return; - for (Set<BaseButton*>::Element *E=button_group->buttons.front();E;E=E->next()) { - if (E->get()==this) + for (Set<BaseButton *>::Element *E = button_group->buttons.front(); E; E = E->next()) { + if (E->get() == this) continue; E->get()->set_pressed(false); @@ -48,23 +47,22 @@ void BaseButton::_unpress_group() { void BaseButton::_gui_input(InputEvent p_event) { - if (status.disabled) // no interaction with disabled button return; - switch(p_event.type) { + switch (p_event.type) { case InputEvent::MOUSE_BUTTON: { - const InputEventMouseButton &b=p_event.mouse_button; + const InputEventMouseButton &b = p_event.mouse_button; - if ( status.disabled || b.button_index!=1 ) + if (status.disabled || b.button_index != 1) return; if (status.pressing_button) break; - if (action_mode==ACTION_MODE_BUTTON_PRESS) { + if (action_mode == ACTION_MODE_BUTTON_PRESS) { if (b.pressed) { @@ -72,47 +70,43 @@ void BaseButton::_gui_input(InputEvent p_event) { if (!toggle_mode) { //mouse press attempt - status.press_attempt=true; - status.pressing_inside=true; + status.press_attempt = true; + status.pressing_inside = true; pressed(); if (get_script_instance()) { Variant::CallError ce; - get_script_instance()->call(SceneStringNames::get_singleton()->_pressed,NULL,0,ce); + get_script_instance()->call(SceneStringNames::get_singleton()->_pressed, NULL, 0, ce); } emit_signal("pressed"); _unpress_group(); - } else { - status.pressed=!status.pressed; + status.pressed = !status.pressed; pressed(); if (get_script_instance()) { Variant::CallError ce; - get_script_instance()->call(SceneStringNames::get_singleton()->_pressed,NULL,0,ce); + get_script_instance()->call(SceneStringNames::get_singleton()->_pressed, NULL, 0, ce); } emit_signal("pressed"); _unpress_group(); - toggled(status.pressed); - emit_signal("toggled",status.pressed); - + emit_signal("toggled", status.pressed); } - } else { emit_signal("button_up"); -/* this is pointless if (status.press_attempt && status.pressing_inside) { + /* this is pointless if (status.press_attempt && status.pressing_inside) { //released(); emit_signal("released"); } */ - status.press_attempt=false; + status.press_attempt = false; } update(); break; @@ -120,59 +114,54 @@ void BaseButton::_gui_input(InputEvent p_event) { if (b.pressed) { - status.press_attempt=true; - status.pressing_inside=true; + status.press_attempt = true; + status.pressing_inside = true; emit_signal("button_down"); } else { emit_signal("button_up"); - if (status.press_attempt &&status.pressing_inside) { + if (status.press_attempt && status.pressing_inside) { if (!toggle_mode) { //mouse press attempt pressed(); if (get_script_instance()) { Variant::CallError ce; - get_script_instance()->call(SceneStringNames::get_singleton()->_pressed,NULL,0,ce); + get_script_instance()->call(SceneStringNames::get_singleton()->_pressed, NULL, 0, ce); } emit_signal("pressed"); } else { - status.pressed=!status.pressed; + status.pressed = !status.pressed; pressed(); emit_signal("pressed"); toggled(status.pressed); - emit_signal("toggled",status.pressed); + emit_signal("toggled", status.pressed); if (get_script_instance()) { - get_script_instance()->call(SceneStringNames::get_singleton()->_toggled,status.pressed); + get_script_instance()->call(SceneStringNames::get_singleton()->_toggled, status.pressed); } - - } _unpress_group(); - - } - status.press_attempt=false; - + status.press_attempt = false; } update(); } break; case InputEvent::MOUSE_MOTION: { - if (status.press_attempt && status.pressing_button==0) { - bool last_press_inside=status.pressing_inside; - status.pressing_inside=has_point(Point2(p_event.mouse_motion.x,p_event.mouse_motion.y)); - if (last_press_inside!=status.pressing_inside) + if (status.press_attempt && status.pressing_button == 0) { + bool last_press_inside = status.pressing_inside; + status.pressing_inside = has_point(Point2(p_event.mouse_motion.x, p_event.mouse_motion.y)); + if (last_press_inside != status.pressing_inside) update(); } } break; @@ -180,7 +169,6 @@ void BaseButton::_gui_input(InputEvent p_event) { case InputEvent::JOYPAD_BUTTON: case InputEvent::KEY: { - if (p_event.is_echo()) { break; } @@ -189,7 +177,7 @@ void BaseButton::_gui_input(InputEvent p_event) { break; } - if (status.press_attempt && status.pressing_button==0) { + if (status.press_attempt && status.pressing_button == 0) { break; } @@ -198,8 +186,8 @@ void BaseButton::_gui_input(InputEvent p_event) { if (p_event.is_pressed()) { status.pressing_button++; - status.press_attempt=true; - status.pressing_inside=true; + status.press_attempt = true; + status.pressing_inside = true; emit_signal("button_down"); } else if (status.press_attempt) { @@ -210,8 +198,8 @@ void BaseButton::_gui_input(InputEvent p_event) { if (status.pressing_button) break; - status.press_attempt=false; - status.pressing_inside=false; + status.press_attempt = false; + status.pressing_inside = false; emit_signal("button_up"); @@ -221,83 +209,75 @@ void BaseButton::_gui_input(InputEvent p_event) { emit_signal("pressed"); } else { - status.pressed=!status.pressed; + status.pressed = !status.pressed; pressed(); emit_signal("pressed"); toggled(status.pressed); if (get_script_instance()) { - get_script_instance()->call(SceneStringNames::get_singleton()->_toggled,status.pressed); + get_script_instance()->call(SceneStringNames::get_singleton()->_toggled, status.pressed); } - emit_signal("toggled",status.pressed); + emit_signal("toggled", status.pressed); } _unpress_group(); - } accept_event(); update(); - } } - } } void BaseButton::_notification(int p_what) { + if (p_what == NOTIFICATION_MOUSE_ENTER) { - if (p_what==NOTIFICATION_MOUSE_ENTER) { - - status.hovering=true; + status.hovering = true; update(); } - if (p_what==NOTIFICATION_MOUSE_EXIT) { - status.hovering=false; + if (p_what == NOTIFICATION_MOUSE_EXIT) { + status.hovering = false; update(); } - if (p_what==NOTIFICATION_DRAG_BEGIN) { + if (p_what == NOTIFICATION_DRAG_BEGIN) { if (status.press_attempt) { - status.press_attempt=false; - status.pressing_button=0; + status.press_attempt = false; + status.pressing_button = 0; update(); } } - - if (p_what==NOTIFICATION_FOCUS_ENTER) { - - status.hovering=true; + + if (p_what == NOTIFICATION_FOCUS_ENTER) { + + status.hovering = true; update(); } - if (p_what==NOTIFICATION_FOCUS_EXIT) { + if (p_what == NOTIFICATION_FOCUS_EXIT) { if (status.pressing_button && status.press_attempt) { - status.press_attempt=false; - status.pressing_button=0; - status.hovering=false; + status.press_attempt = false; + status.pressing_button = 0; + status.hovering = false; update(); } else if (status.hovering) { - status.hovering=false; + status.hovering = false; update(); } } - if (p_what==NOTIFICATION_ENTER_TREE) { - - + if (p_what == NOTIFICATION_ENTER_TREE) { } - if (p_what==NOTIFICATION_EXIT_TREE) { - - + if (p_what == NOTIFICATION_EXIT_TREE) { } - if (p_what==NOTIFICATION_VISIBILITY_CHANGED && !is_visible_in_tree()) { + if (p_what == NOTIFICATION_VISIBILITY_CHANGED && !is_visible_in_tree()) { if (!toggle_mode) { status.pressed = false; @@ -318,12 +298,10 @@ void BaseButton::pressed() { void BaseButton::toggled(bool p_pressed) { if (get_script_instance()) { - get_script_instance()->call("toggled",p_pressed); + get_script_instance()->call("toggled", p_pressed); } - } - void BaseButton::set_disabled(bool p_disabled) { status.disabled = p_disabled; @@ -344,26 +322,25 @@ void BaseButton::set_pressed(bool p_pressed) { if (!toggle_mode) return; - if (status.pressed==p_pressed) + if (status.pressed == p_pressed) return; _change_notify("pressed"); - status.pressed=p_pressed; + status.pressed = p_pressed; if (p_pressed) { _unpress_group(); - } update(); } -bool BaseButton::is_pressing() const{ +bool BaseButton::is_pressing() const { return status.press_attempt; } bool BaseButton::is_pressed() const { - return toggle_mode?status.pressed:status.press_attempt; + return toggle_mode ? status.pressed : status.press_attempt; } bool BaseButton::is_hovered() const { @@ -378,8 +355,7 @@ BaseButton::DrawMode BaseButton::get_draw_mode() const { }; //print_line("press attempt: "+itos(status.press_attempt)+" hover: "+itos(status.hovering)+" pressed: "+itos(status.pressed)); - if (status.press_attempt==false && status.hovering && !status.pressed) { - + if (status.press_attempt == false && status.hovering && !status.pressed) { return DRAW_HOVER; } else { @@ -388,12 +364,12 @@ BaseButton::DrawMode BaseButton::get_draw_mode() const { bool pressing; if (status.press_attempt) { - pressing=status.pressing_inside; + pressing = status.pressing_inside; if (status.pressed) - pressing=!pressing; + pressing = !pressing; } else { - pressing=status.pressed; + pressing = status.pressed; } if (pressing) @@ -407,7 +383,7 @@ BaseButton::DrawMode BaseButton::get_draw_mode() const { void BaseButton::set_toggle_mode(bool p_on) { - toggle_mode=p_on; + toggle_mode = p_on; } bool BaseButton::is_toggle_mode() const { @@ -417,7 +393,7 @@ bool BaseButton::is_toggle_mode() const { void BaseButton::set_action_mode(ActionMode p_mode) { - action_mode=p_mode; + action_mode = p_mode; } BaseButton::ActionMode BaseButton::get_action_mode() const { @@ -429,7 +405,7 @@ void BaseButton::set_enabled_focus_mode(FocusMode p_mode) { enabled_focus_mode = p_mode; if (!status.disabled) { - set_focus_mode( p_mode ); + set_focus_mode(p_mode); } } @@ -438,16 +414,16 @@ Control::FocusMode BaseButton::get_enabled_focus_mode() const { return enabled_focus_mode; } -void BaseButton::set_shortcut(const Ref<ShortCut>& p_shortcut) { +void BaseButton::set_shortcut(const Ref<ShortCut> &p_shortcut) { if (shortcut.is_null() == p_shortcut.is_null()) return; - shortcut=p_shortcut; + shortcut = p_shortcut; set_process_unhandled_input(shortcut.is_valid()); } -Ref<ShortCut> BaseButton:: get_shortcut() const { +Ref<ShortCut> BaseButton::get_shortcut() const { return shortcut; } @@ -460,41 +436,39 @@ void BaseButton::_unhandled_input(InputEvent p_event) { if (is_toggle_mode()) { set_pressed(!is_pressed()); - emit_signal("toggled",is_pressed()); + emit_signal("toggled", is_pressed()); } emit_signal("pressed"); } } -String BaseButton::get_tooltip(const Point2& p_pos) const { +String BaseButton::get_tooltip(const Point2 &p_pos) const { - String tooltip=Control::get_tooltip(p_pos); + String tooltip = Control::get_tooltip(p_pos); if (shortcut.is_valid() && shortcut->is_valid()) { - if (tooltip.find("$sc")!=-1) { - tooltip=tooltip.replace_first("$sc","("+shortcut->get_as_text()+")"); + if (tooltip.find("$sc") != -1) { + tooltip = tooltip.replace_first("$sc", "(" + shortcut->get_as_text() + ")"); } else { - tooltip+=" ("+shortcut->get_as_text()+")"; + tooltip += " (" + shortcut->get_as_text() + ")"; } } return tooltip; } - -void BaseButton::set_button_group(const Ref<ButtonGroup>& p_group) { +void BaseButton::set_button_group(const Ref<ButtonGroup> &p_group) { if (button_group.is_valid()) { button_group->buttons.erase(this); } - button_group=p_group; + button_group = p_group; if (button_group.is_valid()) { button_group->buttons.insert(this); } update(); //checkbox changes to radio if set a buttongroup - } Ref<ButtonGroup> BaseButton::get_button_group() const { @@ -502,103 +476,94 @@ Ref<ButtonGroup> BaseButton::get_button_group() const { return button_group; } - void BaseButton::_bind_methods() { - ClassDB::bind_method(D_METHOD("_gui_input"),&BaseButton::_gui_input); - ClassDB::bind_method(D_METHOD("_unhandled_input"),&BaseButton::_unhandled_input); - ClassDB::bind_method(D_METHOD("set_pressed","pressed"),&BaseButton::set_pressed); - ClassDB::bind_method(D_METHOD("is_pressed"),&BaseButton::is_pressed); - ClassDB::bind_method(D_METHOD("is_hovered"),&BaseButton::is_hovered); - ClassDB::bind_method(D_METHOD("set_toggle_mode","enabled"),&BaseButton::set_toggle_mode); - ClassDB::bind_method(D_METHOD("is_toggle_mode"),&BaseButton::is_toggle_mode); - ClassDB::bind_method(D_METHOD("set_disabled","disabled"),&BaseButton::set_disabled); - ClassDB::bind_method(D_METHOD("is_disabled"),&BaseButton::is_disabled); - ClassDB::bind_method(D_METHOD("set_action_mode","mode"),&BaseButton::set_action_mode); - ClassDB::bind_method(D_METHOD("get_action_mode"),&BaseButton::get_action_mode); - ClassDB::bind_method(D_METHOD("get_draw_mode"),&BaseButton::get_draw_mode); - ClassDB::bind_method(D_METHOD("set_enabled_focus_mode","mode"),&BaseButton::set_enabled_focus_mode); - ClassDB::bind_method(D_METHOD("get_enabled_focus_mode"),&BaseButton::get_enabled_focus_mode); - - ClassDB::bind_method(D_METHOD("set_shortcut","shortcut"),&BaseButton::set_shortcut); - ClassDB::bind_method(D_METHOD("get_shortcut"),&BaseButton::get_shortcut); - - ClassDB::bind_method(D_METHOD("set_button_group","button_group"),&BaseButton::set_button_group); - ClassDB::bind_method(D_METHOD("get_button_group"),&BaseButton::get_button_group); + ClassDB::bind_method(D_METHOD("_gui_input"), &BaseButton::_gui_input); + ClassDB::bind_method(D_METHOD("_unhandled_input"), &BaseButton::_unhandled_input); + ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed); + ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed); + ClassDB::bind_method(D_METHOD("is_hovered"), &BaseButton::is_hovered); + ClassDB::bind_method(D_METHOD("set_toggle_mode", "enabled"), &BaseButton::set_toggle_mode); + ClassDB::bind_method(D_METHOD("is_toggle_mode"), &BaseButton::is_toggle_mode); + ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &BaseButton::set_disabled); + ClassDB::bind_method(D_METHOD("is_disabled"), &BaseButton::is_disabled); + ClassDB::bind_method(D_METHOD("set_action_mode", "mode"), &BaseButton::set_action_mode); + ClassDB::bind_method(D_METHOD("get_action_mode"), &BaseButton::get_action_mode); + ClassDB::bind_method(D_METHOD("get_draw_mode"), &BaseButton::get_draw_mode); + ClassDB::bind_method(D_METHOD("set_enabled_focus_mode", "mode"), &BaseButton::set_enabled_focus_mode); + ClassDB::bind_method(D_METHOD("get_enabled_focus_mode"), &BaseButton::get_enabled_focus_mode); + + ClassDB::bind_method(D_METHOD("set_shortcut", "shortcut"), &BaseButton::set_shortcut); + ClassDB::bind_method(D_METHOD("get_shortcut"), &BaseButton::get_shortcut); + + ClassDB::bind_method(D_METHOD("set_button_group", "button_group"), &BaseButton::set_button_group); + ClassDB::bind_method(D_METHOD("get_button_group"), &BaseButton::get_button_group); BIND_VMETHOD(MethodInfo("_pressed")); - BIND_VMETHOD(MethodInfo("_toggled",PropertyInfo(Variant::BOOL,"pressed"))); - - ADD_SIGNAL( MethodInfo("pressed" ) ); - ADD_SIGNAL( MethodInfo("button_up") ); - ADD_SIGNAL( MethodInfo("button_down") ); - ADD_SIGNAL( MethodInfo("toggled", PropertyInfo( Variant::BOOL,"pressed") ) ); - ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "disabled"), "set_disabled", "is_disabled"); - ADD_PROPERTY( PropertyInfo( Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode"); - ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "is_pressed"), "set_pressed", "is_pressed"); - ADD_PROPERTYNO( PropertyInfo( Variant::INT, "action_mode",PROPERTY_HINT_ENUM,"Button Press,Button Release"), "set_action_mode", "get_action_mode"); - ADD_PROPERTY( PropertyInfo( Variant::INT,"enabled_focus_mode", PROPERTY_HINT_ENUM, "None,Click,All" ), "set_enabled_focus_mode", "get_enabled_focus_mode") ; - ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "shortcut",PROPERTY_HINT_RESOURCE_TYPE,"ShortCut"), "set_shortcut", "get_shortcut"); - ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "group",PROPERTY_HINT_RESOURCE_TYPE,"ButtonGroup"), "set_button_group", "get_button_group"); - - - BIND_CONSTANT( DRAW_NORMAL ); - BIND_CONSTANT( DRAW_PRESSED ); - BIND_CONSTANT( DRAW_HOVER ); - BIND_CONSTANT( DRAW_DISABLED ); - - BIND_CONSTANT( ACTION_MODE_BUTTON_PRESS ); - BIND_CONSTANT( ACTION_MODE_BUTTON_RELEASE ); - - + BIND_VMETHOD(MethodInfo("_toggled", PropertyInfo(Variant::BOOL, "pressed"))); + + ADD_SIGNAL(MethodInfo("pressed")); + ADD_SIGNAL(MethodInfo("button_up")); + ADD_SIGNAL(MethodInfo("button_down")); + ADD_SIGNAL(MethodInfo("toggled", PropertyInfo(Variant::BOOL, "pressed"))); + ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode"); + ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "is_pressed"), "set_pressed", "is_pressed"); + ADD_PROPERTYNO(PropertyInfo(Variant::INT, "action_mode", PROPERTY_HINT_ENUM, "Button Press,Button Release"), "set_action_mode", "get_action_mode"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "enabled_focus_mode", PROPERTY_HINT_ENUM, "None,Click,All"), "set_enabled_focus_mode", "get_enabled_focus_mode"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "ShortCut"), "set_shortcut", "get_shortcut"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "group", PROPERTY_HINT_RESOURCE_TYPE, "ButtonGroup"), "set_button_group", "get_button_group"); + + BIND_CONSTANT(DRAW_NORMAL); + BIND_CONSTANT(DRAW_PRESSED); + BIND_CONSTANT(DRAW_HOVER); + BIND_CONSTANT(DRAW_DISABLED); + + BIND_CONSTANT(ACTION_MODE_BUTTON_PRESS); + BIND_CONSTANT(ACTION_MODE_BUTTON_RELEASE); } BaseButton::BaseButton() { - toggle_mode=false; - status.pressed=false; - status.press_attempt=false; - status.hovering=false; - status.pressing_inside=false; + toggle_mode = false; + status.pressed = false; + status.press_attempt = false; + status.hovering = false; + status.pressing_inside = false; status.disabled = false; - status.pressing_button=0; - set_focus_mode( FOCUS_ALL ); + status.pressing_button = 0; + set_focus_mode(FOCUS_ALL); enabled_focus_mode = FOCUS_ALL; - action_mode=ACTION_MODE_BUTTON_RELEASE; - + action_mode = ACTION_MODE_BUTTON_RELEASE; if (button_group.is_valid()) { button_group->buttons.erase(this); } - - } -BaseButton::~BaseButton() -{ +BaseButton::~BaseButton() { } -void ButtonGroup::get_buttons(List<BaseButton*> *r_buttons) { +void ButtonGroup::get_buttons(List<BaseButton *> *r_buttons) { - for (Set<BaseButton*>::Element *E=buttons.front();E;E=E->next()) { + for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) { r_buttons->push_back(E->get()); } } -BaseButton* ButtonGroup::get_pressed_button() { +BaseButton *ButtonGroup::get_pressed_button() { - for (Set<BaseButton*>::Element *E=buttons.front();E;E=E->next()) { + for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) { if (E->get()->is_pressed()) return E->get(); } return NULL; - } void ButtonGroup::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_pressed_button:BaseButton"),&ButtonGroup::get_pressed_button); + ClassDB::bind_method(D_METHOD("get_pressed_button:BaseButton"), &ButtonGroup::get_pressed_button); } ButtonGroup::ButtonGroup() { diff --git a/scene/gui/base_button.h b/scene/gui/base_button.h index def4ff7536..e382df61c9 100644 --- a/scene/gui/base_button.h +++ b/scene/gui/base_button.h @@ -34,21 +34,19 @@ @author Juan Linietsky <reduzio@gmail.com> */ - class ButtonGroup; class BaseButton : public Control { - GDCLASS( BaseButton, Control ); -public: + GDCLASS(BaseButton, Control); +public: enum ActionMode { ACTION_MODE_BUTTON_PRESS, ACTION_MODE_BUTTON_RELEASE, }; private: - bool toggle_mode; FocusMode enabled_focus_mode; Ref<ShortCut> shortcut; @@ -66,17 +64,11 @@ private: } status; - Ref<ButtonGroup> button_group; - void _unpress_group(); protected: - - - - virtual void pressed(); virtual void toggled(bool p_pressed); static void _bind_methods(); @@ -85,7 +77,6 @@ protected: void _notification(int p_what); public: - enum DrawMode { DRAW_NORMAL, DRAW_PRESSED, @@ -114,35 +105,33 @@ public: void set_enabled_focus_mode(FocusMode p_mode); FocusMode get_enabled_focus_mode() const; - void set_shortcut(const Ref<ShortCut>& p_shortcut); + void set_shortcut(const Ref<ShortCut> &p_shortcut); Ref<ShortCut> get_shortcut() const; - virtual String get_tooltip(const Point2& p_pos) const; + virtual String get_tooltip(const Point2 &p_pos) const; - void set_button_group(const Ref<ButtonGroup>& p_group); + void set_button_group(const Ref<ButtonGroup> &p_group); Ref<ButtonGroup> get_button_group() const; BaseButton(); ~BaseButton(); - }; -VARIANT_ENUM_CAST( BaseButton::DrawMode ) -VARIANT_ENUM_CAST( BaseButton::ActionMode ) - - +VARIANT_ENUM_CAST(BaseButton::DrawMode) +VARIANT_ENUM_CAST(BaseButton::ActionMode) class ButtonGroup : public Resource { - GDCLASS(ButtonGroup,Resource) -friend class BaseButton; - Set<BaseButton*> buttons; + GDCLASS(ButtonGroup, Resource) + friend class BaseButton; + Set<BaseButton *> buttons; + protected: static void _bind_methods(); -public: - BaseButton* get_pressed_button(); - void get_buttons(List<BaseButton*> *r_buttons); +public: + BaseButton *get_pressed_button(); + void get_buttons(List<BaseButton *> *r_buttons); ButtonGroup(); }; diff --git a/scene/gui/box_container.cpp b/scene/gui/box_container.cpp index 1f6e0392d8..7c1487c42f 100644 --- a/scene/gui/box_container.cpp +++ b/scene/gui/box_container.cpp @@ -27,8 +27,8 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "box_container.h" -#include "margin_container.h" #include "label.h" +#include "margin_container.h" struct _MinSizeCache { @@ -41,110 +41,105 @@ void BoxContainer::_resort() { /** First pass, determine minimum size AND amount of stretchable elements */ + Size2i new_size = get_size(); - Size2i new_size=get_size(); + int sep = get_constant("separation"); //,vertical?"VBoxContainer":"HBoxContainer"); - int sep=get_constant("separation");//,vertical?"VBoxContainer":"HBoxContainer"); + bool first = true; + int children_count = 0; + int stretch_min = 0; + int stretch_avail = 0; + float stretch_ratio_total = 0; + Map<Control *, _MinSizeCache> min_size_cache; - bool first=true; - int children_count=0; - int stretch_min=0; - int stretch_avail=0; - float stretch_ratio_total=0; - Map<Control*,_MinSizeCache> min_size_cache; - - for(int i=0;i<get_child_count();i++) { - Control *c=get_child(i)->cast_to<Control>(); + for (int i = 0; i < get_child_count(); i++) { + Control *c = get_child(i)->cast_to<Control>(); if (!c || !c->is_visible_in_tree()) continue; if (c->is_set_as_toplevel()) continue; - Size2i size=c->get_combined_minimum_size(); + Size2i size = c->get_combined_minimum_size(); _MinSizeCache msc; if (vertical) { /* VERTICAL */ - stretch_min+=size.height; - msc.min_size=size.height; - msc.will_stretch=c->get_v_size_flags() & SIZE_EXPAND; + stretch_min += size.height; + msc.min_size = size.height; + msc.will_stretch = c->get_v_size_flags() & SIZE_EXPAND; } else { /* HORIZONTAL */ - stretch_min+=size.width; - msc.min_size=size.width; - msc.will_stretch=c->get_h_size_flags() & SIZE_EXPAND; + stretch_min += size.width; + msc.min_size = size.width; + msc.will_stretch = c->get_h_size_flags() & SIZE_EXPAND; } if (msc.will_stretch) { - stretch_avail+=msc.min_size; - stretch_ratio_total+=c->get_stretch_ratio(); + stretch_avail += msc.min_size; + stretch_ratio_total += c->get_stretch_ratio(); } - msc.final_size=msc.min_size; - min_size_cache[c]=msc; + msc.final_size = msc.min_size; + min_size_cache[c] = msc; children_count++; } - if (children_count==0) + if (children_count == 0) return; - int stretch_max = (vertical? new_size.height : new_size.width ) - (children_count-1) * sep; + int stretch_max = (vertical ? new_size.height : new_size.width) - (children_count - 1) * sep; int stretch_diff = stretch_max - stretch_min; - if (stretch_diff<0) { + if (stretch_diff < 0) { //avoid negative stretch space - stretch_max=stretch_min; - stretch_diff=0; + stretch_max = stretch_min; + stretch_diff = 0; } - stretch_avail+=stretch_diff; //available stretch space. + stretch_avail += stretch_diff; //available stretch space. /** Second, pass sucessively to discard elements that can't be stretched, this will run while stretchable elements exist */ - bool has_stretched = false; - while(stretch_ratio_total>0) { // first of all, dont even be here if no stretchable objects exist + while (stretch_ratio_total > 0) { // first of all, dont even be here if no stretchable objects exist has_stretched = true; - bool refit_successful=true; //assume refit-test will go well + bool refit_successful = true; //assume refit-test will go well - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { - Control *c=get_child(i)->cast_to<Control>(); + Control *c = get_child(i)->cast_to<Control>(); if (!c || !c->is_visible_in_tree()) continue; if (c->is_set_as_toplevel()) continue; ERR_FAIL_COND(!min_size_cache.has(c)); - _MinSizeCache &msc=min_size_cache[c]; + _MinSizeCache &msc = min_size_cache[c]; if (msc.will_stretch) { //wants to stretch //let's see if it can really stretch - int final_pixel_size=stretch_avail * c->get_stretch_ratio() / stretch_ratio_total; - if (final_pixel_size<msc.min_size) { + int final_pixel_size = stretch_avail * c->get_stretch_ratio() / stretch_ratio_total; + if (final_pixel_size < msc.min_size) { //if available stretching area is too small for widget, //then remove it from stretching area - msc.will_stretch=false; - stretch_ratio_total-=c->get_stretch_ratio(); - refit_successful=false; - stretch_avail-=msc.min_size; - msc.final_size=msc.min_size; + msc.will_stretch = false; + stretch_ratio_total -= c->get_stretch_ratio(); + refit_successful = false; + stretch_avail -= msc.min_size; + msc.final_size = msc.min_size; break; } else { - msc.final_size=final_pixel_size; + msc.final_size = final_pixel_size; } } } if (refit_successful) //uf refit went well, break break; - } - /** Final pass, draw and stretch elements **/ - - int ofs=0; + int ofs = 0; if (!has_stretched) { switch (align) { case ALIGN_BEGIN: @@ -158,70 +153,64 @@ void BoxContainer::_resort() { } } - first=true; - int idx=0; + first = true; + int idx = 0; - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { - Control *c=get_child(i)->cast_to<Control>(); + Control *c = get_child(i)->cast_to<Control>(); if (!c || !c->is_visible_in_tree()) continue; if (c->is_set_as_toplevel()) continue; - _MinSizeCache &msc=min_size_cache[c]; - + _MinSizeCache &msc = min_size_cache[c]; if (first) - first=false; + first = false; else - ofs+=sep; + ofs += sep; - int from=ofs; - int to=ofs+msc.final_size; + int from = ofs; + int to = ofs + msc.final_size; - - if (msc.will_stretch && idx==children_count-1) { + if (msc.will_stretch && idx == children_count - 1) { //adjust so the last one always fits perfect //compensating for numerical imprecision - to=vertical?new_size.height:new_size.width; - + to = vertical ? new_size.height : new_size.width; } - int size=to-from; + int size = to - from; Rect2 rect; if (vertical) { - rect=Rect2(0,from,new_size.width,size); + rect = Rect2(0, from, new_size.width, size); } else { - rect=Rect2(from,0,size,new_size.height); - + rect = Rect2(from, 0, size, new_size.height); } - fit_child_in_rect(c,rect); + fit_child_in_rect(c, rect); - ofs=to; + ofs = to; idx++; } - } Size2 BoxContainer::get_minimum_size() const { - /* Calculate MINIMUM SIZE */ Size2i minimum; - int sep=get_constant("separation");//,vertical?"VBoxContainer":"HBoxContainer"); + int sep = get_constant("separation"); //,vertical?"VBoxContainer":"HBoxContainer"); - bool first=true; + bool first = true; - for(int i=0;i<get_child_count();i++) { - Control *c=get_child(i)->cast_to<Control>(); + for (int i = 0; i < get_child_count(); i++) { + Control *c = get_child(i)->cast_to<Control>(); if (!c) continue; if (c->is_set_as_toplevel()) @@ -231,27 +220,26 @@ Size2 BoxContainer::get_minimum_size() const { continue; } - Size2i size=c->get_combined_minimum_size(); + Size2i size = c->get_combined_minimum_size(); if (vertical) { /* VERTICAL */ - if ( size.width > minimum.width ) { - minimum.width=size.width; + if (size.width > minimum.width) { + minimum.width = size.width; } - minimum.height+=size.height+(first?0:sep); + minimum.height += size.height + (first ? 0 : sep); } else { /* HORIZONTAL */ - if ( size.height > minimum.height ) { - minimum.height=size.height; + if (size.height > minimum.height) { + minimum.height = size.height; } - minimum.width+=size.width+(first?0:sep); - + minimum.width += size.width + (first ? 0 : sep); } - first=false; + first = false; } return minimum; @@ -259,7 +247,7 @@ Size2 BoxContainer::get_minimum_size() const { void BoxContainer::_notification(int p_what) { - switch(p_what) { + switch (p_what) { case NOTIFICATION_SORT_CHILDREN: { @@ -279,7 +267,7 @@ BoxContainer::AlignMode BoxContainer::get_alignment() const { void BoxContainer::add_spacer(bool p_begin) { - Control *c = memnew( Control ); + Control *c = memnew(Control); c->set_mouse_filter(MOUSE_FILTER_PASS); //allow spacer to pass mouse events if (vertical) @@ -289,12 +277,12 @@ void BoxContainer::add_spacer(bool p_begin) { add_child(c); if (p_begin) - move_child(c,0); + move_child(c, 0); } BoxContainer::BoxContainer(bool p_vertical) { - vertical=p_vertical; + vertical = p_vertical; align = ALIGN_BEGIN; //set_ignore_mouse(true); set_mouse_filter(MOUSE_FILTER_PASS); @@ -302,24 +290,23 @@ BoxContainer::BoxContainer(bool p_vertical) { void BoxContainer::_bind_methods() { - ClassDB::bind_method(D_METHOD("add_spacer","begin"),&BoxContainer::add_spacer); - ClassDB::bind_method(D_METHOD("get_alignment"),&BoxContainer::get_alignment); - ClassDB::bind_method(D_METHOD("set_alignment","alignment"),&BoxContainer::set_alignment); - - BIND_CONSTANT( ALIGN_BEGIN ); - BIND_CONSTANT( ALIGN_CENTER ); - BIND_CONSTANT( ALIGN_END ); + ClassDB::bind_method(D_METHOD("add_spacer", "begin"), &BoxContainer::add_spacer); + ClassDB::bind_method(D_METHOD("get_alignment"), &BoxContainer::get_alignment); + ClassDB::bind_method(D_METHOD("set_alignment", "alignment"), &BoxContainer::set_alignment); - ADD_PROPERTY( PropertyInfo(Variant::INT,"alignment", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment","get_alignment") ; + BIND_CONSTANT(ALIGN_BEGIN); + BIND_CONSTANT(ALIGN_CENTER); + BIND_CONSTANT(ALIGN_END); + ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment", "get_alignment"); } -MarginContainer* VBoxContainer::add_margin_child(const String& p_label,Control *p_control,bool p_expand) { +MarginContainer *VBoxContainer::add_margin_child(const String &p_label, Control *p_control, bool p_expand) { - Label *l = memnew( Label ); + Label *l = memnew(Label); l->set_text(p_label); add_child(l); - MarginContainer *mc = memnew( MarginContainer ); + MarginContainer *mc = memnew(MarginContainer); mc->add_child(p_control); add_child(mc); if (p_expand) diff --git a/scene/gui/box_container.h b/scene/gui/box_container.h index c428ec132c..04b074896b 100644 --- a/scene/gui/box_container.h +++ b/scene/gui/box_container.h @@ -33,10 +33,9 @@ class BoxContainer : public Container { - GDCLASS(BoxContainer,Container); + GDCLASS(BoxContainer, Container); public: - enum AlignMode { ALIGN_BEGIN, ALIGN_CENTER, @@ -48,44 +47,42 @@ private: AlignMode align; void _resort(); -protected: +protected: void _notification(int p_what); static void _bind_methods(); -public: - void add_spacer(bool p_begin=false); +public: + void add_spacer(bool p_begin = false); void set_alignment(AlignMode p_align); AlignMode get_alignment() const; virtual Size2 get_minimum_size() const; - BoxContainer(bool p_vertical=false); + BoxContainer(bool p_vertical = false); }; - class HBoxContainer : public BoxContainer { - GDCLASS(HBoxContainer,BoxContainer); + GDCLASS(HBoxContainer, BoxContainer); public: - - HBoxContainer() : BoxContainer(false) {} + HBoxContainer() + : BoxContainer(false) {} }; - class MarginContainer; class VBoxContainer : public BoxContainer { - GDCLASS(VBoxContainer,BoxContainer); + GDCLASS(VBoxContainer, BoxContainer); public: + MarginContainer *add_margin_child(const String &p_label, Control *p_control, bool p_expand = false); - MarginContainer* add_margin_child(const String& p_label,Control *p_control,bool p_expand=false); - - VBoxContainer() : BoxContainer(true) {} + VBoxContainer() + : BoxContainer(true) {} }; VARIANT_ENUM_CAST(BoxContainer::AlignMode); diff --git a/scene/gui/button.cpp b/scene/gui/button.cpp index de2ccfb0b5..23915c51a8 100644 --- a/scene/gui/button.cpp +++ b/scene/gui/button.cpp @@ -27,86 +27,83 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "button.h" -#include "servers/visual_server.h" #include "print_string.h" +#include "servers/visual_server.h" #include "translation.h" - Size2 Button::get_minimum_size() const { - Size2 minsize=get_font("font")->get_string_size( xl_text ); + Size2 minsize = get_font("font")->get_string_size(xl_text); if (clip_text) - minsize.width=0; + minsize.width = 0; Ref<Texture> _icon; if (icon.is_null() && has_icon("icon")) - _icon=Control::get_icon("icon"); + _icon = Control::get_icon("icon"); else - _icon=icon; + _icon = icon; if (!_icon.is_null()) { - minsize.height=MAX( minsize.height, _icon->get_height() ); - minsize.width+=_icon->get_width(); - if (xl_text!="") - minsize.width+=get_constant("hseparation"); + minsize.height = MAX(minsize.height, _icon->get_height()); + minsize.width += _icon->get_width(); + if (xl_text != "") + minsize.width += get_constant("hseparation"); } - return get_stylebox("normal" )->get_minimum_size() + minsize; - + return get_stylebox("normal")->get_minimum_size() + minsize; } - void Button::_notification(int p_what) { - if (p_what==NOTIFICATION_TRANSLATION_CHANGED) { + if (p_what == NOTIFICATION_TRANSLATION_CHANGED) { - xl_text=XL_MESSAGE(text); + xl_text = XL_MESSAGE(text); minimum_size_changed(); update(); } - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { RID ci = get_canvas_item(); - Size2 size=get_size(); + Size2 size = get_size(); Color color; //print_line(get_text()+": "+itos(is_flat())+" hover "+itos(get_draw_mode())); Ref<StyleBox> style = get_stylebox("normal"); - switch( get_draw_mode() ) { + switch (get_draw_mode()) { case DRAW_NORMAL: { style = get_stylebox("normal"); if (!flat) - style->draw( ci, Rect2(Point2(0,0), size) ); - color=get_color("font_color"); + style->draw(ci, Rect2(Point2(0, 0), size)); + color = get_color("font_color"); } break; case DRAW_PRESSED: { style = get_stylebox("pressed"); - style->draw( ci, Rect2(Point2(0,0), size) ); + style->draw(ci, Rect2(Point2(0, 0), size)); if (has_color("font_color_pressed")) - color=get_color("font_color_pressed"); + color = get_color("font_color_pressed"); else - color=get_color("font_color"); + color = get_color("font_color"); } break; case DRAW_HOVER: { style = get_stylebox("hover"); - style->draw( ci, Rect2(Point2(0,0), size) ); - color=get_color("font_color_hover"); + style->draw(ci, Rect2(Point2(0, 0), size)); + color = get_color("font_color_hover"); } break; case DRAW_DISABLED: { style = get_stylebox("disabled"); - style->draw( ci, Rect2(Point2(0,0), size) ); - color=get_color("font_color_disabled"); + style->draw(ci, Rect2(Point2(0, 0), size)); + color = get_color("font_color_disabled"); } break; } @@ -114,58 +111,54 @@ void Button::_notification(int p_what) { if (has_focus()) { Ref<StyleBox> style = get_stylebox("focus"); - style->draw(ci,Rect2(Point2(),size)); + style->draw(ci, Rect2(Point2(), size)); } - Ref<Font> font=get_font("font"); + Ref<Font> font = get_font("font"); Ref<Texture> _icon; if (icon.is_null() && has_icon("icon")) - _icon=Control::get_icon("icon"); + _icon = Control::get_icon("icon"); else - _icon=icon; + _icon = icon; - Point2 icon_ofs = (!_icon.is_null())?Point2( _icon->get_width() + get_constant("hseparation"), 0):Point2(); - int text_clip=size.width - style->get_minimum_size().width - icon_ofs.width; - Point2 text_ofs = (size - style->get_minimum_size() - icon_ofs - font->get_string_size( xl_text ) )/2.0; + Point2 icon_ofs = (!_icon.is_null()) ? Point2(_icon->get_width() + get_constant("hseparation"), 0) : Point2(); + int text_clip = size.width - style->get_minimum_size().width - icon_ofs.width; + Point2 text_ofs = (size - style->get_minimum_size() - icon_ofs - font->get_string_size(xl_text)) / 2.0; - switch(align) { + switch (align) { case ALIGN_LEFT: { text_ofs.x = style->get_margin(MARGIN_LEFT) + icon_ofs.x; - text_ofs.y+=style->get_offset().y; + text_ofs.y += style->get_offset().y; } break; case ALIGN_CENTER: { - if (text_ofs.x<0) - text_ofs.x=0; - text_ofs+=icon_ofs; - text_ofs+=style->get_offset(); + if (text_ofs.x < 0) + text_ofs.x = 0; + text_ofs += icon_ofs; + text_ofs += style->get_offset(); } break; case ALIGN_RIGHT: { - text_ofs.x=size.x - style->get_margin(MARGIN_RIGHT) - font->get_string_size( xl_text ).x; - text_ofs.y+=style->get_offset().y; + text_ofs.x = size.x - style->get_margin(MARGIN_RIGHT) - font->get_string_size(xl_text).x; + text_ofs.y += style->get_offset().y; } break; } - - text_ofs.y+=font->get_ascent(); - font->draw( ci, text_ofs.floor(), xl_text, color,clip_text?text_clip:-1); + text_ofs.y += font->get_ascent(); + font->draw(ci, text_ofs.floor(), xl_text, color, clip_text ? text_clip : -1); if (!_icon.is_null()) { - int valign = size.height-style->get_minimum_size().y; + int valign = size.height - style->get_minimum_size().y; - _icon->draw(ci,style->get_offset()+Point2(0, Math::floor( (valign-_icon->get_height())/2.0 ) ),is_disabled()?Color(1,1,1,0.4):Color(1,1,1) ); + _icon->draw(ci, style->get_offset() + Point2(0, Math::floor((valign - _icon->get_height()) / 2.0)), is_disabled() ? Color(1, 1, 1, 0.4) : Color(1, 1, 1)); } - - - } } -void Button::set_text(const String& p_text) { +void Button::set_text(const String &p_text) { - if (text==p_text) + if (text == p_text) return; - text=p_text; - xl_text=XL_MESSAGE(p_text); + text = p_text; + xl_text = XL_MESSAGE(p_text); update(); _change_notify("text"); minimum_size_changed(); @@ -175,12 +168,11 @@ String Button::get_text() const { return text; } +void Button::set_icon(const Ref<Texture> &p_icon) { -void Button::set_icon(const Ref<Texture>& p_icon) { - - if (icon==p_icon) + if (icon == p_icon) return; - icon=p_icon; + icon = p_icon; update(); _change_notify("icon"); minimum_size_changed(); @@ -193,7 +185,7 @@ Ref<Texture> Button::get_icon() const { void Button::set_flat(bool p_flat) { - flat=p_flat; + flat = p_flat; update(); _change_notify("flat"); } @@ -205,7 +197,7 @@ bool Button::is_flat() const { void Button::set_clip_text(bool p_clip_text) { - clip_text=p_clip_text; + clip_text = p_clip_text; update(); minimum_size_changed(); } @@ -217,7 +209,7 @@ bool Button::get_clip_text() const { void Button::set_text_align(TextAlign p_align) { - align=p_align; + align = p_align; update(); } @@ -228,41 +220,36 @@ Button::TextAlign Button::get_text_align() const { void Button::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_text","text"),&Button::set_text); - ClassDB::bind_method(D_METHOD("get_text"),&Button::get_text); - ClassDB::bind_method(D_METHOD("set_button_icon","texture:Texture"),&Button::set_icon); - ClassDB::bind_method(D_METHOD("get_button_icon:Texture"),&Button::get_icon); - ClassDB::bind_method(D_METHOD("set_flat","enabled"),&Button::set_flat); - ClassDB::bind_method(D_METHOD("set_clip_text","enabled"),&Button::set_clip_text); - ClassDB::bind_method(D_METHOD("get_clip_text"),&Button::get_clip_text); - ClassDB::bind_method(D_METHOD("set_text_align","align"),&Button::set_text_align); - ClassDB::bind_method(D_METHOD("get_text_align"),&Button::get_text_align); - ClassDB::bind_method(D_METHOD("is_flat"),&Button::is_flat); - - BIND_CONSTANT( ALIGN_LEFT ); - BIND_CONSTANT( ALIGN_CENTER ); - BIND_CONSTANT( ALIGN_RIGHT ); - - ADD_PROPERTYNZ( PropertyInfo( Variant::STRING, "text", PROPERTY_HINT_NONE,"",PROPERTY_USAGE_DEFAULT_INTL ), "set_text","get_text") ; - ADD_PROPERTYNZ( PropertyInfo( Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture" ), "set_button_icon","get_button_icon") ; - ADD_PROPERTY( PropertyInfo( Variant::BOOL, "flat" ), "set_flat","is_flat") ; - ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "clip_text" ), "set_clip_text","get_clip_text") ; - ADD_PROPERTYNO( PropertyInfo( Variant::INT, "align",PROPERTY_HINT_ENUM,"Left,Center,Right" ), "set_text_align","get_text_align") ; - + ClassDB::bind_method(D_METHOD("set_text", "text"), &Button::set_text); + ClassDB::bind_method(D_METHOD("get_text"), &Button::get_text); + ClassDB::bind_method(D_METHOD("set_button_icon", "texture:Texture"), &Button::set_icon); + ClassDB::bind_method(D_METHOD("get_button_icon:Texture"), &Button::get_icon); + ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &Button::set_flat); + ClassDB::bind_method(D_METHOD("set_clip_text", "enabled"), &Button::set_clip_text); + ClassDB::bind_method(D_METHOD("get_clip_text"), &Button::get_clip_text); + ClassDB::bind_method(D_METHOD("set_text_align", "align"), &Button::set_text_align); + ClassDB::bind_method(D_METHOD("get_text_align"), &Button::get_text_align); + ClassDB::bind_method(D_METHOD("is_flat"), &Button::is_flat); + + BIND_CONSTANT(ALIGN_LEFT); + BIND_CONSTANT(ALIGN_CENTER); + BIND_CONSTANT(ALIGN_RIGHT); + + ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text"); + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_button_icon", "get_button_icon"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat"); + ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "get_clip_text"); + ADD_PROPERTYNO(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_text_align", "get_text_align"); } Button::Button(const String &p_text) { - flat=false; - clip_text=false; + flat = false; + clip_text = false; set_mouse_filter(MOUSE_FILTER_STOP); set_text(p_text); - align=ALIGN_CENTER; + align = ALIGN_CENTER; } - -Button::~Button() -{ +Button::~Button() { } - - diff --git a/scene/gui/button.h b/scene/gui/button.h index 2fd3a0cace..38605bd3ef 100644 --- a/scene/gui/button.h +++ b/scene/gui/button.h @@ -34,14 +34,11 @@ @author Juan Linietsky <reduzio@gmail.com> */ - - class Button : public BaseButton { - GDCLASS( Button, BaseButton ); -public: - + GDCLASS(Button, BaseButton); +public: enum TextAlign { ALIGN_LEFT, ALIGN_CENTER, @@ -49,7 +46,6 @@ public: }; private: - bool flat; String text; String xl_text; @@ -57,20 +53,18 @@ private: bool clip_text; TextAlign align; - protected: - virtual Size2 get_minimum_size() const; void _notification(int p_what); static void _bind_methods(); -public: -// +public: + // - void set_text(const String& p_text); + void set_text(const String &p_text); String get_text() const; - void set_icon(const Ref<Texture>& p_icon); + void set_icon(const Ref<Texture> &p_icon); Ref<Texture> get_icon() const; void set_flat(bool p_flat); @@ -82,13 +76,10 @@ public: void set_text_align(TextAlign p_align); TextAlign get_text_align() const; - - Button(const String& p_text=String()); + Button(const String &p_text = String()); ~Button(); - }; - VARIANT_ENUM_CAST(Button::TextAlign); #endif diff --git a/scene/gui/button_array.cpp b/scene/gui/button_array.cpp index 2fd5f0df83..c1cb9ad855 100644 --- a/scene/gui/button_array.cpp +++ b/scene/gui/button_array.cpp @@ -28,46 +28,44 @@ /*************************************************************************/ #include "button_array.h" +bool ButtonArray::_set(const StringName &p_name, const Variant &p_value) { -bool ButtonArray::_set(const StringName& p_name, const Variant& p_value) { - - String n=String(p_name); + String n = String(p_name); if (n.begins_with("button/")) { - String what = n.get_slicec('/',1); - if (what=="count") { - int new_size=p_value; - if (new_size>0 && buttons.size()==0) { - selected=0; + String what = n.get_slicec('/', 1); + if (what == "count") { + int new_size = p_value; + if (new_size > 0 && buttons.size() == 0) { + selected = 0; } if (new_size < buttons.size()) { - if (selected>=new_size) - selected=new_size-1; + if (selected >= new_size) + selected = new_size - 1; } buttons.resize(new_size); _change_notify(); minimum_size_changed(); - } else if (what=="align") { + } else if (what == "align") { set_align(Align(p_value.operator int())); - } else if (what=="selected") { + } else if (what == "selected") { set_selected(p_value); } else if (what == "min_button_size") { min_button_size = p_value; } else { - int idx=what.to_int(); - ERR_FAIL_INDEX_V(idx,buttons.size(),false); - String f = n.get_slicec('/',2); - if (f=="text") { - buttons[idx].text=p_value; - buttons[idx].xl_text=XL_MESSAGE(p_value); - } else if (f=="tooltip") - buttons[idx].tooltip=p_value; - else if (f=="icon") - buttons[idx].icon=p_value; + int idx = what.to_int(); + ERR_FAIL_INDEX_V(idx, buttons.size(), false); + String f = n.get_slicec('/', 2); + if (f == "text") { + buttons[idx].text = p_value; + buttons[idx].xl_text = XL_MESSAGE(p_value); + } else if (f == "tooltip") + buttons[idx].tooltip = p_value; + else if (f == "icon") + buttons[idx].icon = p_value; else return false; - } update(); @@ -75,36 +73,34 @@ bool ButtonArray::_set(const StringName& p_name, const Variant& p_value) { } return false; - } -bool ButtonArray::_get(const StringName& p_name,Variant &r_ret) const { +bool ButtonArray::_get(const StringName &p_name, Variant &r_ret) const { - String n=String(p_name); + String n = String(p_name); if (n.begins_with("button/")) { - String what = n.get_slicec('/',1); - if (what=="count") { - r_ret=buttons.size(); - } else if (what=="align") { - r_ret=get_align(); - } else if (what=="selected") { - r_ret=get_selected(); - } else if (what == "min_button_size"){ + String what = n.get_slicec('/', 1); + if (what == "count") { + r_ret = buttons.size(); + } else if (what == "align") { + r_ret = get_align(); + } else if (what == "selected") { + r_ret = get_selected(); + } else if (what == "min_button_size") { r_ret = min_button_size; } else { - int idx=what.to_int(); - ERR_FAIL_INDEX_V(idx,buttons.size(),false); - String f = n.get_slicec('/',2); - if (f=="text") - r_ret=buttons[idx].text; - else if (f=="tooltip") - r_ret=buttons[idx].tooltip; - else if (f=="icon") - r_ret=buttons[idx].icon; + int idx = what.to_int(); + ERR_FAIL_INDEX_V(idx, buttons.size(), false); + String f = n.get_slicec('/', 2); + if (f == "text") + r_ret = buttons[idx].text; + else if (f == "tooltip") + r_ret = buttons[idx].tooltip; + else if (f == "icon") + r_ret = buttons[idx].icon; else return false; - } return true; @@ -112,24 +108,22 @@ bool ButtonArray::_get(const StringName& p_name,Variant &r_ret) const { return false; } -void ButtonArray::_get_property_list( List<PropertyInfo> *p_list) const { - - p_list->push_back( PropertyInfo( Variant::INT, "button/count",PROPERTY_HINT_RANGE,"0,512,1")); - p_list->push_back( PropertyInfo( Variant::INT, "button/min_button_size",PROPERTY_HINT_RANGE,"0,1024,1")); - p_list->push_back( PropertyInfo( Variant::INT, "button/align",PROPERTY_HINT_ENUM,"Begin,Center,End,Fill,Expand")); - for(int i=0;i<buttons.size();i++) { - String base="button/"+itos(i)+"/"; - p_list->push_back( PropertyInfo( Variant::STRING, base+"text")); - p_list->push_back( PropertyInfo( Variant::STRING, base+"tooltip")); - p_list->push_back( PropertyInfo( Variant::OBJECT, base+"icon",PROPERTY_HINT_RESOURCE_TYPE,"Texture")); +void ButtonArray::_get_property_list(List<PropertyInfo> *p_list) const { + + p_list->push_back(PropertyInfo(Variant::INT, "button/count", PROPERTY_HINT_RANGE, "0,512,1")); + p_list->push_back(PropertyInfo(Variant::INT, "button/min_button_size", PROPERTY_HINT_RANGE, "0,1024,1")); + p_list->push_back(PropertyInfo(Variant::INT, "button/align", PROPERTY_HINT_ENUM, "Begin,Center,End,Fill,Expand")); + for (int i = 0; i < buttons.size(); i++) { + String base = "button/" + itos(i) + "/"; + p_list->push_back(PropertyInfo(Variant::STRING, base + "text")); + p_list->push_back(PropertyInfo(Variant::STRING, base + "tooltip")); + p_list->push_back(PropertyInfo(Variant::OBJECT, base + "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture")); } - if (buttons.size()>0) { - p_list->push_back( PropertyInfo( Variant::INT, "button/selected",PROPERTY_HINT_RANGE,"0,"+itos(buttons.size()-1)+",1")); + if (buttons.size() > 0) { + p_list->push_back(PropertyInfo(Variant::INT, "button/selected", PROPERTY_HINT_RANGE, "0," + itos(buttons.size() - 1) + ",1")); } - } - Size2 ButtonArray::get_minimum_size() const { Ref<StyleBox> style_normal = get_stylebox("normal"); @@ -141,54 +135,51 @@ Size2 ButtonArray::get_minimum_size() const { Size2 minsize; - for(int i=0;i<buttons.size();i++) { + for (int i = 0; i < buttons.size(); i++) { - Ref<StyleBox> sb = i==selected ? style_selected : style_normal; - Ref<Font> f = i==selected ? font_selected : font_normal; + Ref<StyleBox> sb = i == selected ? style_selected : style_normal; + Ref<Font> f = i == selected ? font_selected : font_normal; Size2 ms; ms = f->get_string_size(buttons[i].xl_text); if (buttons[i].icon.is_valid()) { Size2 bs = buttons[i].icon->get_size(); - ms.height = MAX(ms.height,bs.height); - ms.width+=bs.width+icon_sep; + ms.height = MAX(ms.height, bs.height); + ms.width += bs.width + icon_sep; } - ms+=sb->get_minimum_size(); + ms += sb->get_minimum_size(); - buttons[i]._ms_cache=ms[orientation]; - - minsize[orientation]+=ms[orientation]; - if (i>0) - minsize[orientation]+=button_sep; - minsize[!orientation] = MAX(minsize[!orientation],ms[!orientation]); + buttons[i]._ms_cache = ms[orientation]; + minsize[orientation] += ms[orientation]; + if (i > 0) + minsize[orientation] += button_sep; + minsize[!orientation] = MAX(minsize[!orientation], ms[!orientation]); } return minsize; - - } void ButtonArray::_notification(int p_what) { - switch(p_what) { - case NOTIFICATION_MOUSE_EXIT:{ - hover=-1; + switch (p_what) { + case NOTIFICATION_MOUSE_EXIT: { + hover = -1; update(); - }break; - case NOTIFICATION_READY:{ + } break; + case NOTIFICATION_READY: { MethodInfo mi; - mi.name="mouse_sub_enter"; + mi.name = "mouse_sub_enter"; add_user_signal(mi); - }break; + } break; case NOTIFICATION_DRAW: { - Size2 size=get_size(); - Size2 minsize=get_combined_minimum_size(); + Size2 size = get_size(); + Size2 minsize = get_combined_minimum_size(); Ref<StyleBox> style_normal = get_stylebox("normal"); Ref<StyleBox> style_selected = get_stylebox("selected"); Ref<StyleBox> style_focus = get_stylebox("focus"); @@ -200,188 +191,174 @@ void ButtonArray::_notification(int p_what) { Color color_normal = get_color("font_color"); Color color_selected = get_color("font_color_selected"); - int sep=button_sep; - int ofs=0; - int expand=0; + int sep = button_sep; + int ofs = 0; + int expand = 0; - switch(align) { + switch (align) { case ALIGN_BEGIN: { - ofs=0; + ofs = 0; } break; case ALIGN_CENTER: { - ofs=Math::floor((size[orientation] - minsize[orientation])/2); + ofs = Math::floor((size[orientation] - minsize[orientation]) / 2); } break; case ALIGN_END: { - ofs=Math::floor((size[orientation] - minsize[orientation])); + ofs = Math::floor((size[orientation] - minsize[orientation])); } break; case ALIGN_FILL: { - if (buttons.size()>1) - sep+=Math::floor((size[orientation]- minsize[orientation])/(buttons.size()-1.0)); - ofs=0; + if (buttons.size() > 1) + sep += Math::floor((size[orientation] - minsize[orientation]) / (buttons.size() - 1.0)); + ofs = 0; } break; case ALIGN_EXPAND_FILL: { - ofs=0; - expand=size[orientation] - minsize[orientation]; + ofs = 0; + expand = size[orientation] - minsize[orientation]; } break; - - - } - int op_size = orientation==VERTICAL ? size.width : size.height; - + int op_size = orientation == VERTICAL ? size.width : size.height; - for(int i=0;i<buttons.size();i++) { + for (int i = 0; i < buttons.size(); i++) { int ms = buttons[i]._ms_cache; - int s=ms; - if (expand>0) { - s+=expand/buttons.size(); + int s = ms; + if (expand > 0) { + s += expand / buttons.size(); } - if(min_button_size != -1 && s < min_button_size){ + if (min_button_size != -1 && s < min_button_size) { s = min_button_size; } Rect2 r; - r.pos[orientation]=ofs; - r.pos[!orientation]=0; - r.size[orientation]=s; - r.size[!orientation]=op_size; + r.pos[orientation] = ofs; + r.pos[!orientation] = 0; + r.size[orientation] = s; + r.size[!orientation] = op_size; Ref<Font> f; Color c; Point2 sbsize; Point2 sbofs; - if (i==selected) { - draw_style_box(style_selected,r); - sbsize=style_selected->get_minimum_size(); - sbofs=style_selected->get_offset(); - f=font_selected; - c=color_selected; + if (i == selected) { + draw_style_box(style_selected, r); + sbsize = style_selected->get_minimum_size(); + sbofs = style_selected->get_offset(); + f = font_selected; + c = color_selected; if (has_focus()) - draw_style_box(style_focus,r); + draw_style_box(style_focus, r); } else { - if (hover==i) - draw_style_box(style_hover,r); + if (hover == i) + draw_style_box(style_hover, r); else if (!flat) - draw_style_box(style_normal,r); - sbsize=style_normal->get_minimum_size(); - sbofs=style_normal->get_offset(); - f=font_normal; - c=color_normal; + draw_style_box(style_normal, r); + sbsize = style_normal->get_minimum_size(); + sbofs = style_normal->get_offset(); + f = font_normal; + c = color_normal; } Size2 ssize = f->get_string_size(buttons[i].xl_text); if (buttons[i].icon.is_valid()) { - ssize.x+=buttons[i].icon->get_width(); + ssize.x += buttons[i].icon->get_width(); } - Point2 text_ofs=((r.size-ssize-sbsize)/2.0+Point2(0,f->get_ascent())).floor()+sbofs; + Point2 text_ofs = ((r.size - ssize - sbsize) / 2.0 + Point2(0, f->get_ascent())).floor() + sbofs; if (buttons[i].icon.is_valid()) { - draw_texture(buttons[i].icon,r.pos+Point2(text_ofs.x,Math::floor((r.size.height-buttons[i].icon->get_height())/2.0))); - text_ofs.x+=buttons[i].icon->get_width()+icon_sep; - + draw_texture(buttons[i].icon, r.pos + Point2(text_ofs.x, Math::floor((r.size.height - buttons[i].icon->get_height()) / 2.0))); + text_ofs.x += buttons[i].icon->get_width() + icon_sep; } - draw_string(f,text_ofs+r.pos,buttons[i].xl_text,c); - buttons[i]._pos_cache=ofs; - buttons[i]._size_cache=s; + draw_string(f, text_ofs + r.pos, buttons[i].xl_text, c); + buttons[i]._pos_cache = ofs; + buttons[i]._size_cache = s; - ofs+=s; - ofs+=sep; + ofs += s; + ofs += sep; } } break; } } - -void ButtonArray::_gui_input(const InputEvent& p_event) { +void ButtonArray::_gui_input(const InputEvent &p_event) { if ( - ( (orientation==HORIZONTAL && p_event.is_action("ui_left") ) || - (orientation==VERTICAL && p_event.is_action("ui_up") ) ) - && p_event.is_pressed() && selected>0) { - set_selected(selected-1); - accept_event(); - emit_signal("button_selected",selected); - return; - + ((orientation == HORIZONTAL && p_event.is_action("ui_left")) || + (orientation == VERTICAL && p_event.is_action("ui_up"))) && + p_event.is_pressed() && selected > 0) { + set_selected(selected - 1); + accept_event(); + emit_signal("button_selected", selected); + return; } if ( - ( (orientation==HORIZONTAL && p_event.is_action("ui_right") ) || - (orientation==VERTICAL && p_event.is_action("ui_down") ) ) - && p_event.is_pressed() && selected<(buttons.size()-1)) { - set_selected(selected+1); - accept_event(); - emit_signal("button_selected",selected); - return; - + ((orientation == HORIZONTAL && p_event.is_action("ui_right")) || + (orientation == VERTICAL && p_event.is_action("ui_down"))) && + p_event.is_pressed() && selected < (buttons.size() - 1)) { + set_selected(selected + 1); + accept_event(); + emit_signal("button_selected", selected); + return; } - if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed && p_event.mouse_button.button_index==BUTTON_LEFT) { + if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed && p_event.mouse_button.button_index == BUTTON_LEFT) { - int ofs = orientation==HORIZONTAL ? p_event.mouse_button.x: p_event.mouse_button.y; + int ofs = orientation == HORIZONTAL ? p_event.mouse_button.x : p_event.mouse_button.y; - for(int i=0;i<buttons.size();i++) { + for (int i = 0; i < buttons.size(); i++) { - if (ofs>=buttons[i]._pos_cache && ofs<buttons[i]._pos_cache+buttons[i]._size_cache) { + if (ofs >= buttons[i]._pos_cache && ofs < buttons[i]._pos_cache + buttons[i]._size_cache) { set_selected(i); - emit_signal("button_selected",i); + emit_signal("button_selected", i); return; } - } } - if (p_event.type==InputEvent::MOUSE_MOTION) { + if (p_event.type == InputEvent::MOUSE_MOTION) { - int ofs = orientation==HORIZONTAL ? p_event.mouse_motion.x: p_event.mouse_motion.y; - int new_hover=-1; - for(int i=0;i<buttons.size();i++) { + int ofs = orientation == HORIZONTAL ? p_event.mouse_motion.x : p_event.mouse_motion.y; + int new_hover = -1; + for (int i = 0; i < buttons.size(); i++) { - if (ofs>=buttons[i]._pos_cache && ofs<buttons[i]._pos_cache+buttons[i]._size_cache) { + if (ofs >= buttons[i]._pos_cache && ofs < buttons[i]._pos_cache + buttons[i]._size_cache) { - new_hover=i; + new_hover = i; break; } - } - if (new_hover!=hover) { - hover=new_hover; + if (new_hover != hover) { + hover = new_hover; emit_signal("mouse_sub_enter"); update(); } } - - } -String ButtonArray::get_tooltip(const Point2& p_pos) const { +String ButtonArray::get_tooltip(const Point2 &p_pos) const { - int ofs = orientation==HORIZONTAL ? p_pos.x: p_pos.y; - for(int i=0;i<buttons.size();i++) { + int ofs = orientation == HORIZONTAL ? p_pos.x : p_pos.y; + for (int i = 0; i < buttons.size(); i++) { - if (ofs>=buttons[i]._pos_cache && ofs<buttons[i]._pos_cache+buttons[i]._size_cache) + if (ofs >= buttons[i]._pos_cache && ofs < buttons[i]._pos_cache + buttons[i]._size_cache) return buttons[i].tooltip; - } return Control::get_tooltip(p_pos); } void ButtonArray::set_align(Align p_align) { - align=p_align; + align = p_align; update(); - } ButtonArray::Align ButtonArray::get_align() const { @@ -391,7 +368,7 @@ ButtonArray::Align ButtonArray::get_align() const { void ButtonArray::set_flat(bool p_flat) { - flat=p_flat; + flat = p_flat; update(); } @@ -400,79 +377,74 @@ bool ButtonArray::is_flat() const { return flat; } - -void ButtonArray::add_button(const String& p_text,const String& p_tooltip) { +void ButtonArray::add_button(const String &p_text, const String &p_tooltip) { Button button; - button.text=p_text; - button.xl_text=XL_MESSAGE(p_text); - button.tooltip=p_tooltip; + button.text = p_text; + button.xl_text = XL_MESSAGE(p_text); + button.tooltip = p_tooltip; buttons.push_back(button); update(); - if (selected==-1) - selected=0; + if (selected == -1) + selected = 0; minimum_size_changed(); } -void ButtonArray::add_icon_button(const Ref<Texture>& p_icon,const String& p_text,const String& p_tooltip) { +void ButtonArray::add_icon_button(const Ref<Texture> &p_icon, const String &p_text, const String &p_tooltip) { Button button; - button.text=p_text; - button.xl_text=XL_MESSAGE(p_text); - button.icon=p_icon; - button.tooltip=p_tooltip; + button.text = p_text; + button.xl_text = XL_MESSAGE(p_text); + button.icon = p_icon; + button.tooltip = p_tooltip; buttons.push_back(button); - if (selected==-1) - selected=0; + if (selected == -1) + selected = 0; update(); - } -void ButtonArray::set_button_text(int p_button, const String& p_text) { +void ButtonArray::set_button_text(int p_button, const String &p_text) { - ERR_FAIL_INDEX(p_button,buttons.size()); - buttons[p_button].text=p_text; - buttons[p_button].xl_text=XL_MESSAGE(p_text); + ERR_FAIL_INDEX(p_button, buttons.size()); + buttons[p_button].text = p_text; + buttons[p_button].xl_text = XL_MESSAGE(p_text); update(); minimum_size_changed(); - } -void ButtonArray::set_button_tooltip(int p_button, const String& p_text) { - - ERR_FAIL_INDEX(p_button,buttons.size()); - buttons[p_button].tooltip=p_text; +void ButtonArray::set_button_tooltip(int p_button, const String &p_text) { + ERR_FAIL_INDEX(p_button, buttons.size()); + buttons[p_button].tooltip = p_text; } -void ButtonArray::set_button_icon(int p_button, const Ref<Texture>& p_icon) { +void ButtonArray::set_button_icon(int p_button, const Ref<Texture> &p_icon) { - ERR_FAIL_INDEX(p_button,buttons.size()); - buttons[p_button].icon=p_icon; + ERR_FAIL_INDEX(p_button, buttons.size()); + buttons[p_button].icon = p_icon; update(); minimum_size_changed(); } String ButtonArray::get_button_text(int p_button) const { - ERR_FAIL_INDEX_V(p_button,buttons.size(),""); + ERR_FAIL_INDEX_V(p_button, buttons.size(), ""); return buttons[p_button].text; } String ButtonArray::get_button_tooltip(int p_button) const { - ERR_FAIL_INDEX_V(p_button,buttons.size(),""); + ERR_FAIL_INDEX_V(p_button, buttons.size(), ""); return buttons[p_button].tooltip; } Ref<Texture> ButtonArray::get_button_icon(int p_button) const { - ERR_FAIL_INDEX_V(p_button,buttons.size(),Ref<Texture>()); + ERR_FAIL_INDEX_V(p_button, buttons.size(), Ref<Texture>()); return buttons[p_button].icon; - } int ButtonArray::get_selected() const { @@ -487,30 +459,29 @@ int ButtonArray::get_hovered() const { void ButtonArray::set_selected(int p_selected) { - ERR_FAIL_INDEX(p_selected,buttons.size()); - selected=p_selected; + ERR_FAIL_INDEX(p_selected, buttons.size()); + selected = p_selected; update(); - } void ButtonArray::erase_button(int p_button) { - ERR_FAIL_INDEX(p_button,buttons.size()); + ERR_FAIL_INDEX(p_button, buttons.size()); buttons.remove(p_button); - if (p_button>=selected) + if (p_button >= selected) selected--; - if (selected<0) - selected=0; - if (selected>=buttons.size()) - selected=buttons.size()-1; + if (selected < 0) + selected = 0; + if (selected >= buttons.size()) + selected = buttons.size() - 1; update(); } -void ButtonArray::clear(){ +void ButtonArray::clear() { buttons.clear(); - selected=-1; + selected = -1; update(); } @@ -521,53 +492,50 @@ int ButtonArray::get_button_count() const { void ButtonArray::get_translatable_strings(List<String> *p_strings) const { - - for(int i=0;i<buttons.size();i++) { + for (int i = 0; i < buttons.size(); i++) { p_strings->push_back(buttons[i].text); p_strings->push_back(buttons[i].tooltip); } } - void ButtonArray::_bind_methods() { - ClassDB::bind_method(D_METHOD("add_button","text","tooltip"),&ButtonArray::add_button,DEFVAL("")); - ClassDB::bind_method(D_METHOD("add_icon_button","icon:Texture","text","tooltip"),&ButtonArray::add_icon_button,DEFVAL(""),DEFVAL("")); - ClassDB::bind_method(D_METHOD("set_button_text","button_idx","text"),&ButtonArray::set_button_text); - ClassDB::bind_method(D_METHOD("set_button_tooltip","button_idx","text"),&ButtonArray::set_button_tooltip); - ClassDB::bind_method(D_METHOD("set_button_icon","button_idx","icon:Texture"),&ButtonArray::set_button_icon); - ClassDB::bind_method(D_METHOD("get_button_text","button_idx"),&ButtonArray::get_button_text); - ClassDB::bind_method(D_METHOD("get_button_tooltip","button_idx"),&ButtonArray::get_button_tooltip); - ClassDB::bind_method(D_METHOD("get_button_icon:Texture","button_idx"),&ButtonArray::get_button_icon); - ClassDB::bind_method(D_METHOD("get_button_count"),&ButtonArray::get_button_count); - ClassDB::bind_method(D_METHOD("set_flat","enabled"),&ButtonArray::set_flat); - ClassDB::bind_method(D_METHOD("is_flat"),&ButtonArray::is_flat); - ClassDB::bind_method(D_METHOD("get_selected"),&ButtonArray::get_selected); - ClassDB::bind_method(D_METHOD("get_hovered"),&ButtonArray::get_hovered); - ClassDB::bind_method(D_METHOD("set_selected","button_idx"),&ButtonArray::set_selected); - ClassDB::bind_method(D_METHOD("erase_button","button_idx"),&ButtonArray::erase_button); - ClassDB::bind_method(D_METHOD("clear"),&ButtonArray::clear); - - ClassDB::bind_method(D_METHOD("_gui_input"),&ButtonArray::_gui_input); - - BIND_CONSTANT( ALIGN_BEGIN ); - BIND_CONSTANT( ALIGN_CENTER ); - BIND_CONSTANT( ALIGN_END ); - BIND_CONSTANT( ALIGN_FILL ); - BIND_CONSTANT( ALIGN_EXPAND_FILL ); - - ADD_PROPERTY( PropertyInfo( Variant::BOOL, "flat" ), "set_flat","is_flat") ; - - ADD_SIGNAL( MethodInfo("button_selected",PropertyInfo(Variant::INT,"button_idx"))); - + ClassDB::bind_method(D_METHOD("add_button", "text", "tooltip"), &ButtonArray::add_button, DEFVAL("")); + ClassDB::bind_method(D_METHOD("add_icon_button", "icon:Texture", "text", "tooltip"), &ButtonArray::add_icon_button, DEFVAL(""), DEFVAL("")); + ClassDB::bind_method(D_METHOD("set_button_text", "button_idx", "text"), &ButtonArray::set_button_text); + ClassDB::bind_method(D_METHOD("set_button_tooltip", "button_idx", "text"), &ButtonArray::set_button_tooltip); + ClassDB::bind_method(D_METHOD("set_button_icon", "button_idx", "icon:Texture"), &ButtonArray::set_button_icon); + ClassDB::bind_method(D_METHOD("get_button_text", "button_idx"), &ButtonArray::get_button_text); + ClassDB::bind_method(D_METHOD("get_button_tooltip", "button_idx"), &ButtonArray::get_button_tooltip); + ClassDB::bind_method(D_METHOD("get_button_icon:Texture", "button_idx"), &ButtonArray::get_button_icon); + ClassDB::bind_method(D_METHOD("get_button_count"), &ButtonArray::get_button_count); + ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &ButtonArray::set_flat); + ClassDB::bind_method(D_METHOD("is_flat"), &ButtonArray::is_flat); + ClassDB::bind_method(D_METHOD("get_selected"), &ButtonArray::get_selected); + ClassDB::bind_method(D_METHOD("get_hovered"), &ButtonArray::get_hovered); + ClassDB::bind_method(D_METHOD("set_selected", "button_idx"), &ButtonArray::set_selected); + ClassDB::bind_method(D_METHOD("erase_button", "button_idx"), &ButtonArray::erase_button); + ClassDB::bind_method(D_METHOD("clear"), &ButtonArray::clear); + + ClassDB::bind_method(D_METHOD("_gui_input"), &ButtonArray::_gui_input); + + BIND_CONSTANT(ALIGN_BEGIN); + BIND_CONSTANT(ALIGN_CENTER); + BIND_CONSTANT(ALIGN_END); + BIND_CONSTANT(ALIGN_FILL); + BIND_CONSTANT(ALIGN_EXPAND_FILL); + + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat"); + + ADD_SIGNAL(MethodInfo("button_selected", PropertyInfo(Variant::INT, "button_idx"))); } ButtonArray::ButtonArray(Orientation p_orientation) { - orientation=p_orientation; - selected=-1; + orientation = p_orientation; + selected = -1; set_focus_mode(FOCUS_ALL); - hover=-1; - flat=false; + hover = -1; + flat = false; min_button_size = -1; } diff --git a/scene/gui/button_array.h b/scene/gui/button_array.h index 4865ef0373..dac130e79a 100644 --- a/scene/gui/button_array.h +++ b/scene/gui/button_array.h @@ -34,6 +34,7 @@ class ButtonArray : public Control { GDCLASS(ButtonArray, Control); + public: enum Align { ALIGN_BEGIN, @@ -42,8 +43,8 @@ public: ALIGN_FILL, ALIGN_EXPAND_FILL }; -private: +private: Orientation orientation; Align align; @@ -64,19 +65,17 @@ private: double min_button_size; Vector<Button> buttons; -protected: - bool _set(const StringName& p_name, const Variant& p_value); - bool _get(const StringName& p_name,Variant &r_ret) const; - void _get_property_list( List<PropertyInfo> *p_list) const; +protected: + bool _set(const StringName &p_name, const Variant &p_value); + bool _get(const StringName &p_name, Variant &r_ret) const; + void _get_property_list(List<PropertyInfo> *p_list) const; void _notification(int p_what); static void _bind_methods(); public: - - void _gui_input(const InputEvent& p_event); - + void _gui_input(const InputEvent &p_event); void set_align(Align p_align); Align get_align() const; @@ -84,13 +83,12 @@ public: void set_flat(bool p_flat); bool is_flat() const; - void add_button(const String& p_button,const String& p_tooltip=""); - void add_icon_button(const Ref<Texture>& p_icon,const String& p_button="",const String& p_tooltip=""); - - void set_button_text(int p_button, const String& p_text); - void set_button_tooltip(int p_button, const String& p_text); - void set_button_icon(int p_button, const Ref<Texture>& p_icon); + void add_button(const String &p_button, const String &p_tooltip = ""); + void add_icon_button(const Ref<Texture> &p_icon, const String &p_button = "", const String &p_tooltip = ""); + void set_button_text(int p_button, const String &p_text); + void set_button_tooltip(int p_button, const String &p_text); + void set_button_icon(int p_button, const Ref<Texture> &p_icon); String get_button_text(int p_button) const; String get_button_tooltip(int p_button) const; @@ -108,25 +106,25 @@ public: virtual Size2 get_minimum_size() const; virtual void get_translatable_strings(List<String> *p_strings) const; - virtual String get_tooltip(const Point2& p_pos) const; - + virtual String get_tooltip(const Point2 &p_pos) const; - ButtonArray(Orientation p_orientation=HORIZONTAL); + ButtonArray(Orientation p_orientation = HORIZONTAL); }; class HButtonArray : public ButtonArray { - GDCLASS(HButtonArray,ButtonArray); -public: + GDCLASS(HButtonArray, ButtonArray); - HButtonArray() : ButtonArray(HORIZONTAL) {}; +public: + HButtonArray() + : ButtonArray(HORIZONTAL){}; }; class VButtonArray : public ButtonArray { - GDCLASS(VButtonArray,ButtonArray); -public: + GDCLASS(VButtonArray, ButtonArray); - VButtonArray() : ButtonArray(VERTICAL) {}; +public: + VButtonArray() + : ButtonArray(VERTICAL){}; }; - #endif // BUTTON_ARRAY_H diff --git a/scene/gui/center_container.cpp b/scene/gui/center_container.cpp index c1c04ced84..9cee8c0cc1 100644 --- a/scene/gui/center_container.cpp +++ b/scene/gui/center_container.cpp @@ -28,14 +28,12 @@ /*************************************************************************/ #include "center_container.h" - Size2 CenterContainer::get_minimum_size() const { - if (use_top_left) return Size2(); Size2 ms; - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { Control *c = get_child(i)->cast_to<Control>(); if (!c) @@ -45,22 +43,17 @@ Size2 CenterContainer::get_minimum_size() const { if (!c->is_visible()) continue; Size2 minsize = c->get_combined_minimum_size(); - ms.width = MAX(ms.width , minsize.width); - ms.height = MAX(ms.height , minsize.height); - - + ms.width = MAX(ms.width, minsize.width); + ms.height = MAX(ms.height, minsize.height); } return ms; - } - void CenterContainer::set_use_top_left(bool p_enable) { - use_top_left=p_enable; + use_top_left = p_enable; queue_sort(); - } bool CenterContainer::is_using_top_left() const { @@ -68,13 +61,12 @@ bool CenterContainer::is_using_top_left() const { return use_top_left; } - void CenterContainer::_notification(int p_what) { - if (p_what==NOTIFICATION_SORT_CHILDREN) { + if (p_what == NOTIFICATION_SORT_CHILDREN) { Size2 size = get_size(); - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { Control *c = get_child(i)->cast_to<Control>(); if (!c) @@ -83,22 +75,21 @@ void CenterContainer::_notification(int p_what) { continue; Size2 minsize = c->get_combined_minimum_size(); - Point2 ofs = use_top_left ? (-minsize*0.5).floor() : ((size - minsize)/2.0).floor(); - fit_child_in_rect(c,Rect2(ofs,minsize)); - + Point2 ofs = use_top_left ? (-minsize * 0.5).floor() : ((size - minsize) / 2.0).floor(); + fit_child_in_rect(c, Rect2(ofs, minsize)); } } } void CenterContainer::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_use_top_left","enable"),&CenterContainer::set_use_top_left); - ClassDB::bind_method(D_METHOD("is_using_top_left"),&CenterContainer::is_using_top_left); + ClassDB::bind_method(D_METHOD("set_use_top_left", "enable"), &CenterContainer::set_use_top_left); + ClassDB::bind_method(D_METHOD("is_using_top_left"), &CenterContainer::is_using_top_left); - ADD_PROPERTY( PropertyInfo(Variant::BOOL,"use_top_left"),"set_use_top_left","is_using_top_left"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_top_left"), "set_use_top_left", "is_using_top_left"); } CenterContainer::CenterContainer() { - use_top_left=false; + use_top_left = false; } diff --git a/scene/gui/center_container.h b/scene/gui/center_container.h index 7acc14de19..c2deb823f1 100644 --- a/scene/gui/center_container.h +++ b/scene/gui/center_container.h @@ -29,20 +29,19 @@ #ifndef CENTER_CONTAINER_H #define CENTER_CONTAINER_H - #include "scene/gui/container.h" class CenterContainer : public Container { - GDCLASS( CenterContainer, Container ); + GDCLASS(CenterContainer, Container); bool use_top_left; -protected: +protected: void _notification(int p_what); static void _bind_methods(); -public: +public: void set_use_top_left(bool p_enable); bool is_using_top_left() const; diff --git a/scene/gui/check_box.cpp b/scene/gui/check_box.cpp index c9803bc654..c7e2f4e7f9 100644 --- a/scene/gui/check_box.cpp +++ b/scene/gui/check_box.cpp @@ -28,46 +28,39 @@ /*************************************************************************/ #include "check_box.h" -#include "servers/visual_server.h" #include "button_group.h" - +#include "servers/visual_server.h" void CheckBox::_notification(int p_what) { - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { - RID ci = get_canvas_item(); + RID ci = get_canvas_item(); - Ref<Texture> on=Control::get_icon(is_radio() ? "radio_checked" : "checked"); - Ref<Texture> off=Control::get_icon(is_radio() ? "radio_unchecked" : "unchecked"); + Ref<Texture> on = Control::get_icon(is_radio() ? "radio_checked" : "checked"); + Ref<Texture> off = Control::get_icon(is_radio() ? "radio_unchecked" : "unchecked"); - Vector2 ofs; - ofs.x = 0; - ofs.y = int((get_size().height - on->get_height())/2); + Vector2 ofs; + ofs.x = 0; + ofs.y = int((get_size().height - on->get_height()) / 2); - if (is_pressed()) - on->draw(ci,ofs); - else - off->draw(ci,ofs); - - - } + if (is_pressed()) + on->draw(ci, ofs); + else + off->draw(ci, ofs); + } } -bool CheckBox::is_radio() -{ +bool CheckBox::is_radio() { return get_button_group().is_valid(); } -CheckBox::CheckBox(const String &p_text): - Button(p_text) -{ - set_toggle_mode(true); - set_text_align(ALIGN_LEFT); - +CheckBox::CheckBox(const String &p_text) + : Button(p_text) { + set_toggle_mode(true); + set_text_align(ALIGN_LEFT); } -CheckBox::~CheckBox() -{ +CheckBox::~CheckBox() { } diff --git a/scene/gui/check_box.h b/scene/gui/check_box.h index 6a4893936f..9b682de177 100644 --- a/scene/gui/check_box.h +++ b/scene/gui/check_box.h @@ -29,27 +29,22 @@ #ifndef CHECK_BOX_H #define CHECK_BOX_H - #include "scene/gui/button.h" /** @author Mariano Suligoy <marianognu.esyrpg@gmail.com> */ class CheckBox : public Button { - GDCLASS( CheckBox, Button ); - + GDCLASS(CheckBox, Button); protected: - void _notification(int p_what); - - bool is_radio(); + void _notification(int p_what); + bool is_radio(); public: - - CheckBox(const String& p_text=String()); - ~CheckBox(); - + CheckBox(const String &p_text = String()); + ~CheckBox(); }; #endif diff --git a/scene/gui/check_button.cpp b/scene/gui/check_button.cpp index 6404f066e8..6b24e0889a 100644 --- a/scene/gui/check_button.cpp +++ b/scene/gui/check_button.cpp @@ -28,45 +28,34 @@ /*************************************************************************/ #include "check_button.h" -#include "servers/visual_server.h" #include "print_string.h" - +#include "servers/visual_server.h" void CheckButton::_notification(int p_what) { - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { RID ci = get_canvas_item(); - Ref<Texture> on=Control::get_icon("on"); - Ref<Texture> off=Control::get_icon("off"); + Ref<Texture> on = Control::get_icon("on"); + Ref<Texture> off = Control::get_icon("off"); Vector2 ofs; ofs.x = get_size().width - on->get_width(); - ofs.y = int((get_size().height - on->get_height())/2); + ofs.y = int((get_size().height - on->get_height()) / 2); if (is_pressed()) - on->draw(ci,ofs); + on->draw(ci, ofs); else - off->draw(ci,ofs); - - + off->draw(ci, ofs); } } - CheckButton::CheckButton() { set_toggle_mode(true); set_text_align(ALIGN_LEFT); - - } - -CheckButton::~CheckButton() -{ +CheckButton::~CheckButton() { } - - - diff --git a/scene/gui/check_button.h b/scene/gui/check_button.h index 1c5440a25d..adf210a1e1 100644 --- a/scene/gui/check_button.h +++ b/scene/gui/check_button.h @@ -29,24 +29,20 @@ #ifndef CHECK_BUTTON_H #define CHECK_BUTTON_H - #include "scene/gui/button.h" /** @author Juan Linietsky <reduzio@gmail.com> */ class CheckButton : public Button { - GDCLASS( CheckButton, Button ); - + GDCLASS(CheckButton, Button); protected: void _notification(int p_what); public: - CheckButton(); ~CheckButton(); - }; #endif diff --git a/scene/gui/color_picker.cpp b/scene/gui/color_picker.cpp index d9da583bf5..0f982167bf 100644 --- a/scene/gui/color_picker.cpp +++ b/scene/gui/color_picker.cpp @@ -28,17 +28,15 @@ /*************************************************************************/ #include "color_picker.h" -#include "scene/gui/separator.h" -#include "scene/main/viewport.h" -#include "os/os.h" #include "os/input.h" #include "os/keyboard.h" - +#include "os/os.h" +#include "scene/gui/separator.h" +#include "scene/main/viewport.h" void ColorPicker::_notification(int p_what) { - - switch(p_what) { + switch (p_what) { case NOTIFICATION_THEME_CHANGED: { //sample->set_texture(get_icon("color_sample")); @@ -50,8 +48,6 @@ void ColorPicker::_notification(int p_what) { _update_color(); } - - } } @@ -72,16 +68,15 @@ void ColorPicker::_update_controls() { scroll[3]->hide(); labels[3]->hide(); } - } -void ColorPicker::set_pick_color(const Color& p_color) { +void ColorPicker::set_pick_color(const Color &p_color) { - color=p_color; + color = p_color; if (color != last_hsv) { - h=color.get_h(); - s=color.get_s(); - v=color.get_v(); + h = color.get_h(); + s = color.get_s(); + v = color.get_v(); last_hsv = color; } @@ -92,12 +87,11 @@ void ColorPicker::set_pick_color(const Color& p_color) { uv_edit->get_child(0)->cast_to<Control>()->update(); w_edit->get_child(0)->cast_to<Control>()->update(); _update_color(); - } void ColorPicker::set_edit_alpha(bool p_show) { - edit_alpha=p_show; + edit_alpha = p_show; _update_controls(); if (!is_inside_tree()) @@ -117,19 +111,18 @@ void ColorPicker::_value_changed(double) { if (updating) return; - for(int i=0;i<4;i++) { - color.components[i] = scroll[i]->get_value()/(raw_mode_enabled?1.0:255.0); + for (int i = 0; i < 4; i++) { + color.components[i] = scroll[i]->get_value() / (raw_mode_enabled ? 1.0 : 255.0); } set_pick_color(color); _update_text_value(); - emit_signal("color_changed",color); - + emit_signal("color_changed", color); } -void ColorPicker::_html_entered(const String& p_html) { +void ColorPicker::_html_entered(const String &p_html) { if (updating) return; @@ -140,14 +133,14 @@ void ColorPicker::_html_entered(const String& p_html) { return; set_pick_color(color); - emit_signal("color_changed",color); + emit_signal("color_changed", color); } void ColorPicker::_update_color() { - updating=true; + updating = true; - for(int i=0;i<4;i++) { + for (int i = 0; i < 4; i++) { scroll[i]->set_max(255); scroll[i]->set_step(0.01); if (raw_mode_enabled) { @@ -162,30 +155,29 @@ void ColorPicker::_update_color() { _update_text_value(); sample->update(); - updating=false; + updating = false; } -void ColorPicker::_update_presets() -{ - Size2 size=bt_add_preset->get_size(); - preset->set_custom_minimum_size(Size2(size.width*presets.size(),size.height)); +void ColorPicker::_update_presets() { + Size2 size = bt_add_preset->get_size(); + preset->set_custom_minimum_size(Size2(size.width * presets.size(), size.height)); PoolVector<uint8_t> img; - img.resize(size.x*presets.size()*size.y*3); + img.resize(size.x * presets.size() * size.y * 3); { - PoolVector<uint8_t>::Write w=img.write(); - for (int y=0;y<size.y;y++) { - for (int x=0;x<size.x*presets.size();x++) { - int ofs = (y*(size.x*presets.size())+x)*3; - w[ofs+0]=uint8_t(CLAMP(presets[(int)x/size.x].r*255.0,0,255)); - w[ofs+1]=uint8_t(CLAMP(presets[(int)x/size.x].g*255.0,0,255)); - w[ofs+2]=uint8_t(CLAMP(presets[(int)x/size.x].b*255.0,0,255)); + PoolVector<uint8_t>::Write w = img.write(); + for (int y = 0; y < size.y; y++) { + for (int x = 0; x < size.x * presets.size(); x++) { + int ofs = (y * (size.x * presets.size()) + x) * 3; + w[ofs + 0] = uint8_t(CLAMP(presets[(int)x / size.x].r * 255.0, 0, 255)); + w[ofs + 1] = uint8_t(CLAMP(presets[(int)x / size.x].g * 255.0, 0, 255)); + w[ofs + 2] = uint8_t(CLAMP(presets[(int)x / size.x].b * 255.0, 0, 255)); } } } - Image i(size.x*presets.size(),size.y, false, Image::FORMAT_RGB8,img); + Image i(size.x * presets.size(), size.y, false, Image::FORMAT_RGB8, img); Ref<ImageTexture> t; t.instance(); @@ -193,8 +185,7 @@ void ColorPicker::_update_presets() preset->set_texture(t); } -void ColorPicker::_text_type_toggled() -{ +void ColorPicker::_text_type_toggled() { if (!get_tree()->is_editor_hint()) return; text_is_constructor = !text_is_constructor; @@ -213,25 +204,23 @@ Color ColorPicker::get_pick_color() const { return color; } -void ColorPicker::add_preset(const Color &p_color) -{ +void ColorPicker::add_preset(const Color &p_color) { if (presets.find(p_color)) { presets.move_to_back(presets.find(p_color)); } else { presets.push_back(p_color); } _update_presets(); - if (presets.size()==10) + if (presets.size() == 10) bt_add_preset->hide(); } - void ColorPicker::set_raw_mode(bool p_enabled) { - if (raw_mode_enabled==p_enabled) + if (raw_mode_enabled == p_enabled) return; - raw_mode_enabled=p_enabled; - if (btn_mode->is_pressed()!=p_enabled) + raw_mode_enabled = p_enabled; + if (btn_mode->is_pressed() != p_enabled) btn_mode->set_pressed(p_enabled); if (!is_inside_tree()) @@ -246,79 +235,77 @@ bool ColorPicker::is_raw_mode() const { return raw_mode_enabled; } - void ColorPicker::_update_text_value() { if (text_is_constructor) { - String t = "Color("+String::num(color.r)+","+String::num(color.g)+","+String::num(color.b); - if (edit_alpha && color.a<1) - t+=(","+String::num(color.a)+")") ; + String t = "Color(" + String::num(color.r) + "," + String::num(color.g) + "," + String::num(color.b); + if (edit_alpha && color.a < 1) + t += ("," + String::num(color.a) + ")"); else - t+=")"; + t += ")"; c_text->set_text(t); } else { - c_text->set_text(color.to_html(edit_alpha && color.a<1)); + c_text->set_text(color.to_html(edit_alpha && color.a < 1)); } } void ColorPicker::_sample_draw() { - sample->draw_rect(Rect2(Point2(),Size2(256,20)),color); + sample->draw_rect(Rect2(Point2(), Size2(256, 20)), color); } -void ColorPicker::_hsv_draw(int p_wich,Control* c) -{ +void ColorPicker::_hsv_draw(int p_wich, Control *c) { if (!c) return; - if (p_wich==0) { + if (p_wich == 0) { Vector<Point2> points; points.push_back(Vector2()); - points.push_back(Vector2(c->get_size().x,0)); + points.push_back(Vector2(c->get_size().x, 0)); points.push_back(c->get_size()); - points.push_back(Vector2(0,c->get_size().y)); + points.push_back(Vector2(0, c->get_size().y)); Vector<Color> colors; - colors.push_back(Color(1,1,1)); - colors.push_back(Color(1,1,1)); + colors.push_back(Color(1, 1, 1)); + colors.push_back(Color(1, 1, 1)); colors.push_back(Color()); colors.push_back(Color()); - c->draw_polygon(points,colors); + c->draw_polygon(points, colors); Vector<Color> colors2; Color col = color; - col.set_hsv(color.get_h(),1,1); + col.set_hsv(color.get_h(), 1, 1); col.a = 0; colors2.push_back(col); col.a = 1; colors2.push_back(col); - col.set_hsv(color.get_h(),1,0); + col.set_hsv(color.get_h(), 1, 0); colors2.push_back(col); col.a = 0; colors2.push_back(col); - c->draw_polygon(points,colors); + c->draw_polygon(points, colors); int x = CLAMP(c->get_size().x * s, 0, c->get_size().x); - int y = CLAMP(c->get_size().y-c->get_size().y * v, 0, c->get_size().y); + int y = CLAMP(c->get_size().y - c->get_size().y * v, 0, c->get_size().y); col = color; - col.a=1; - c->draw_line(Point2(x,0),Point2(x,c->get_size().y),col.inverted()); - c->draw_line(Point2(0, y),Point2(c->get_size().x, y),col.inverted()); - c->draw_line(Point2(x,y),Point2(x,y),Color(1,1,1),2); - } else if (p_wich==1) { - Ref<Texture> hue = get_icon("color_hue","ColorPicker"); - c->draw_texture_rect(hue,Rect2(Point2(),c->get_size())); - int y=c->get_size().y-c->get_size().y*h; - Color col=Color(); - col.set_hsv(h,1,1); - c->draw_line(Point2(0,y),Point2(c->get_size().x,y),col.inverted()); + col.a = 1; + c->draw_line(Point2(x, 0), Point2(x, c->get_size().y), col.inverted()); + c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted()); + c->draw_line(Point2(x, y), Point2(x, y), Color(1, 1, 1), 2); + } else if (p_wich == 1) { + Ref<Texture> hue = get_icon("color_hue", "ColorPicker"); + c->draw_texture_rect(hue, Rect2(Point2(), c->get_size())); + int y = c->get_size().y - c->get_size().y * h; + Color col = Color(); + col.set_hsv(h, 1, 1); + c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted()); } } void ColorPicker::_uv_input(const InputEvent &ev) { if (ev.type == InputEvent::MOUSE_BUTTON) { const InputEventMouseButton &bev = ev.mouse_button; - if (bev.pressed && bev.button_index==BUTTON_LEFT) { + if (bev.pressed && bev.button_index == BUTTON_LEFT) { changing_color = true; - float x = CLAMP((float)bev.x,0,256); - float y = CLAMP((float)bev.y,0,256); - s=x/256; - v=1.0-y/256.0; - color.set_hsv(h,s,v,color.a); + float x = CLAMP((float)bev.x, 0, 256); + float y = CLAMP((float)bev.y, 0, 256); + s = x / 256; + v = 1.0 - y / 256.0; + color.set_hsv(h, s, v, color.a); last_hsv = color; set_pick_color(color); _update_color(); @@ -330,11 +317,11 @@ void ColorPicker::_uv_input(const InputEvent &ev) { const InputEventMouse &bev = ev.mouse_motion; if (!changing_color) return; - float x = CLAMP((float)bev.x,0,256); - float y = CLAMP((float)bev.y,0,256); - s=x/256; - v=1.0-y/256.0; - color.set_hsv(h,s,v,color.a); + float x = CLAMP((float)bev.x, 0, 256); + float y = CLAMP((float)bev.y, 0, 256); + s = x / 256; + v = 1.0 - y / 256.0; + color.set_hsv(h, s, v, color.a); last_hsv = color; set_pick_color(color); _update_color(); @@ -345,14 +332,14 @@ void ColorPicker::_uv_input(const InputEvent &ev) { void ColorPicker::_w_input(const InputEvent &ev) { if (ev.type == InputEvent::MOUSE_BUTTON) { const InputEventMouseButton &bev = ev.mouse_button; - if (bev.pressed && bev.button_index==BUTTON_LEFT) { + if (bev.pressed && bev.button_index == BUTTON_LEFT) { changing_color = true; - h=1-((float)bev.y)/256.0; + h = 1 - ((float)bev.y) / 256.0; } else { changing_color = false; } - color.set_hsv(h,s,v,color.a); + color.set_hsv(h, s, v, color.a); last_hsv = color; set_pick_color(color); _update_color(); @@ -361,9 +348,9 @@ void ColorPicker::_w_input(const InputEvent &ev) { const InputEventMouse &bev = ev.mouse_motion; if (!changing_color) return; - float y = CLAMP((float)bev.y,0,256); - h=1.0-y/256.0; - color.set_hsv(h,s,v,color.a); + float y = CLAMP((float)bev.y, 0, 256); + h = 1.0 - y / 256.0; + color.set_hsv(h, s, v, color.a); last_hsv = color; set_pick_color(color); _update_color(); @@ -374,11 +361,11 @@ void ColorPicker::_w_input(const InputEvent &ev) { void ColorPicker::_preset_input(const InputEvent &ev) { if (ev.type == InputEvent::MOUSE_BUTTON) { const InputEventMouseButton &bev = ev.mouse_button; - if (bev.pressed && bev.button_index==BUTTON_LEFT) { - int index = bev.x/(preset->get_size().x/presets.size()); + if (bev.pressed && bev.button_index == BUTTON_LEFT) { + int index = bev.x / (preset->get_size().x / presets.size()); set_pick_color(presets[index]); - } else if (bev.pressed && bev.button_index==BUTTON_RIGHT) { - int index = bev.x/(preset->get_size().x/presets.size()); + } else if (bev.pressed && bev.button_index == BUTTON_RIGHT) { + int index = bev.x / (preset->get_size().x / presets.size()); presets.erase(presets[index]); _update_presets(); bt_add_preset->show(); @@ -388,42 +375,41 @@ void ColorPicker::_preset_input(const InputEvent &ev) { } else if (ev.type == InputEvent::MOUSE_MOTION) { const InputEventMouse &mev = ev.mouse_motion; int index = mev.x * presets.size(); - if( preset->get_size().x != 0 ) { + if (preset->get_size().x != 0) { index /= preset->get_size().x; } - if (index<0 || index >= presets.size()) + if (index < 0 || index >= presets.size()) return; - preset->set_tooltip("Color: #"+presets[index].to_html(presets[index].a<1)+"\n" - "LMB: Set color\n" - "RMB: Remove preset"); + preset->set_tooltip("Color: #" + presets[index].to_html(presets[index].a < 1) + "\n" + "LMB: Set color\n" + "RMB: Remove preset"); } } -void ColorPicker::_screen_input(const InputEvent &ev) -{ - if (ev.type==InputEvent::MOUSE_BUTTON) { +void ColorPicker::_screen_input(const InputEvent &ev) { + if (ev.type == InputEvent::MOUSE_BUTTON) { const InputEventMouseButton &bev = ev.mouse_button; - if (bev.button_index==BUTTON_LEFT&&!bev.pressed) { + if (bev.button_index == BUTTON_LEFT && !bev.pressed) { emit_signal("color_changed", color); screen->hide(); } - } else if (ev.type==InputEvent::MOUSE_MOTION) { + } else if (ev.type == InputEvent::MOUSE_MOTION) { const InputEventMouse &mev = ev.mouse_motion; - Viewport *r=get_tree()->get_root(); - if (!r->get_visible_rect().has_point(Point2(mev.global_x,mev.global_y))) + Viewport *r = get_tree()->get_root(); + if (!r->get_visible_rect().has_point(Point2(mev.global_x, mev.global_y))) return; - Image img =r->get_screen_capture(); + Image img = r->get_screen_capture(); if (!img.empty()) { - last_capture=img; + last_capture = img; r->queue_screen_capture(); } if (!last_capture.empty()) { - int pw = last_capture.get_format()==Image::FORMAT_RGBA8?4:3; - int ofs = (mev.global_y*last_capture.get_width()+mev.global_x)*pw; + int pw = last_capture.get_format() == Image::FORMAT_RGBA8 ? 4 : 3; + int ofs = (mev.global_y * last_capture.get_width() + mev.global_x) * pw; PoolVector<uint8_t>::Read r = last_capture.get_data().read(); - Color c( r[ofs+0]/255.0, r[ofs+1]/255.0, r[ofs+2]/255.0 ); + Color c(r[ofs + 0] / 255.0, r[ofs + 1] / 255.0, r[ofs + 2] / 255.0); set_pick_color(c); } @@ -434,15 +420,14 @@ void ColorPicker::_add_preset_pressed() { add_preset(color); } -void ColorPicker::_screen_pick_pressed() -{ - Viewport *r=get_tree()->get_root(); +void ColorPicker::_screen_pick_pressed() { + Viewport *r = get_tree()->get_root(); if (!screen) { - screen=memnew( Control ); + screen = memnew(Control); r->add_child(screen); screen->set_as_toplevel(true); screen->set_area_as_parent_rect(); - screen->connect("gui_input",this,"_screen_input"); + screen->connect("gui_input", this, "_screen_input"); } screen->raise(); screen->show_modal(); @@ -451,199 +436,182 @@ void ColorPicker::_screen_pick_pressed() void ColorPicker::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_pick_color","color"),&ColorPicker::set_pick_color); - ClassDB::bind_method(D_METHOD("get_pick_color"),&ColorPicker::get_pick_color); - ClassDB::bind_method(D_METHOD("set_raw_mode","mode"),&ColorPicker::set_raw_mode); - ClassDB::bind_method(D_METHOD("is_raw_mode"),&ColorPicker::is_raw_mode); - ClassDB::bind_method(D_METHOD("set_edit_alpha","show"),&ColorPicker::set_edit_alpha); - ClassDB::bind_method(D_METHOD("is_editing_alpha"),&ColorPicker::is_editing_alpha); + ClassDB::bind_method(D_METHOD("set_pick_color", "color"), &ColorPicker::set_pick_color); + ClassDB::bind_method(D_METHOD("get_pick_color"), &ColorPicker::get_pick_color); + ClassDB::bind_method(D_METHOD("set_raw_mode", "mode"), &ColorPicker::set_raw_mode); + ClassDB::bind_method(D_METHOD("is_raw_mode"), &ColorPicker::is_raw_mode); + ClassDB::bind_method(D_METHOD("set_edit_alpha", "show"), &ColorPicker::set_edit_alpha); + ClassDB::bind_method(D_METHOD("is_editing_alpha"), &ColorPicker::is_editing_alpha); ClassDB::bind_method(D_METHOD("add_preset"), &ColorPicker::add_preset); - ClassDB::bind_method(D_METHOD("_value_changed"),&ColorPicker::_value_changed); - ClassDB::bind_method(D_METHOD("_html_entered"),&ColorPicker::_html_entered); - ClassDB::bind_method(D_METHOD("_text_type_toggled"),&ColorPicker::_text_type_toggled); + ClassDB::bind_method(D_METHOD("_value_changed"), &ColorPicker::_value_changed); + ClassDB::bind_method(D_METHOD("_html_entered"), &ColorPicker::_html_entered); + ClassDB::bind_method(D_METHOD("_text_type_toggled"), &ColorPicker::_text_type_toggled); ClassDB::bind_method(D_METHOD("_add_preset_pressed"), &ColorPicker::_add_preset_pressed); ClassDB::bind_method(D_METHOD("_screen_pick_pressed"), &ColorPicker::_screen_pick_pressed); - ClassDB::bind_method(D_METHOD("_sample_draw"),&ColorPicker::_sample_draw); - ClassDB::bind_method(D_METHOD("_hsv_draw"),&ColorPicker::_hsv_draw); - ClassDB::bind_method(D_METHOD("_uv_input"),&ColorPicker::_uv_input); - ClassDB::bind_method(D_METHOD("_w_input"),&ColorPicker::_w_input); - ClassDB::bind_method(D_METHOD("_preset_input"),&ColorPicker::_preset_input); - ClassDB::bind_method(D_METHOD("_screen_input"),&ColorPicker::_screen_input); + ClassDB::bind_method(D_METHOD("_sample_draw"), &ColorPicker::_sample_draw); + ClassDB::bind_method(D_METHOD("_hsv_draw"), &ColorPicker::_hsv_draw); + ClassDB::bind_method(D_METHOD("_uv_input"), &ColorPicker::_uv_input); + ClassDB::bind_method(D_METHOD("_w_input"), &ColorPicker::_w_input); + ClassDB::bind_method(D_METHOD("_preset_input"), &ColorPicker::_preset_input); + ClassDB::bind_method(D_METHOD("_screen_input"), &ColorPicker::_screen_input); - ADD_SIGNAL( MethodInfo("color_changed",PropertyInfo(Variant::COLOR,"color"))); + ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color"))); } -ColorPicker::ColorPicker() : - BoxContainer(true) { +ColorPicker::ColorPicker() + : BoxContainer(true) { - updating=true; - edit_alpha=true; + updating = true; + edit_alpha = true; text_is_constructor = false; - raw_mode_enabled=false; - changing_color=false; - screen=NULL; + raw_mode_enabled = false; + changing_color = false; + screen = NULL; - HBoxContainer *hb_smpl = memnew( HBoxContainer ); - btn_pick = memnew( ToolButton ); - btn_pick->connect("pressed",this,"_screen_pick_pressed"); + HBoxContainer *hb_smpl = memnew(HBoxContainer); + btn_pick = memnew(ToolButton); + btn_pick->connect("pressed", this, "_screen_pick_pressed"); - sample = memnew( TextureRect ); + sample = memnew(TextureRect); sample->set_h_size_flags(SIZE_EXPAND_FILL); - sample->connect("draw",this,"_sample_draw"); + sample->connect("draw", this, "_sample_draw"); hb_smpl->add_child(sample); hb_smpl->add_child(btn_pick); add_child(hb_smpl); - HBoxContainer *hb_edit = memnew( HBoxContainer ); - - uv_edit= memnew ( Control ); - - + HBoxContainer *hb_edit = memnew(HBoxContainer); + uv_edit = memnew(Control); uv_edit->connect("gui_input", this, "_uv_input"); uv_edit->set_mouse_filter(MOUSE_FILTER_PASS); - uv_edit->set_custom_minimum_size(Size2 (256,256)); - Vector<Variant> args=Vector<Variant>(); + uv_edit->set_custom_minimum_size(Size2(256, 256)); + Vector<Variant> args = Vector<Variant>(); args.push_back(0); args.push_back(uv_edit); - uv_edit->connect("draw",this,"_hsv_draw",args); + uv_edit->connect("draw", this, "_hsv_draw", args); add_child(hb_edit); - w_edit= memnew( Control ); + w_edit = memnew(Control); //w_edit->set_ignore_mouse(false); - w_edit->set_custom_minimum_size(Size2(30,256)); + w_edit->set_custom_minimum_size(Size2(30, 256)); w_edit->connect("gui_input", this, "_w_input"); args.clear(); args.push_back(1); args.push_back(w_edit); - w_edit->connect("draw",this,"_hsv_draw",args); - + w_edit->connect("draw", this, "_hsv_draw", args); hb_edit->add_child(uv_edit); - hb_edit->add_child(memnew( VSeparator )); + hb_edit->add_child(memnew(VSeparator)); hb_edit->add_child(w_edit); - VBoxContainer *vbl = memnew( VBoxContainer ); + VBoxContainer *vbl = memnew(VBoxContainer); add_child(vbl); - add_child(memnew( HSeparator )); + add_child(memnew(HSeparator)); - VBoxContainer *vbr = memnew( VBoxContainer ); + VBoxContainer *vbr = memnew(VBoxContainer); add_child(vbr); vbr->set_h_size_flags(SIZE_EXPAND_FILL); - const char* lt[4] = {"R","G","B","A"}; + const char *lt[4] = { "R", "G", "B", "A" }; - for(int i=0;i<4;i++) { + for (int i = 0; i < 4; i++) { - HBoxContainer *hbc = memnew( HBoxContainer ); + HBoxContainer *hbc = memnew(HBoxContainer); - labels[i]=memnew( Label(lt[i]) ); + labels[i] = memnew(Label(lt[i])); hbc->add_child(labels[i]); - scroll[i]=memnew( HSlider ); + scroll[i] = memnew(HSlider); hbc->add_child(scroll[i]); - values[i]=memnew( SpinBox ); + values[i] = memnew(SpinBox); scroll[i]->share(values[i]); hbc->add_child(values[i]); - scroll[i]->set_min(0); scroll[i]->set_page(0); scroll[i]->set_h_size_flags(SIZE_EXPAND_FILL); - scroll[i]->connect("value_changed",this,"_value_changed"); + scroll[i]->connect("value_changed", this, "_value_changed"); vbr->add_child(hbc); - } - HBoxContainer *hhb = memnew( HBoxContainer ); + HBoxContainer *hhb = memnew(HBoxContainer); - btn_mode = memnew( CheckButton ); + btn_mode = memnew(CheckButton); btn_mode->set_text("RAW Mode"); btn_mode->connect("toggled", this, "set_raw_mode"); hhb->add_child(btn_mode); vbr->add_child(hhb); - text_type = memnew( Button ); + text_type = memnew(Button); text_type->set_flat(true); text_type->connect("pressed", this, "_text_type_toggled"); hhb->add_child(text_type); - c_text = memnew( LineEdit ); + c_text = memnew(LineEdit); hhb->add_child(c_text); - c_text->connect("text_entered",this,"_html_entered"); + c_text->connect("text_entered", this, "_html_entered"); text_type->set_text("#"); c_text->set_h_size_flags(SIZE_EXPAND_FILL); - _update_controls(); //_update_color(); - updating=false; - - set_pick_color(Color(1,1,1)); + updating = false; + set_pick_color(Color(1, 1, 1)); - HBoxContainer *bbc = memnew( HBoxContainer ); + HBoxContainer *bbc = memnew(HBoxContainer); add_child(bbc); - preset = memnew( TextureRect ); + preset = memnew(TextureRect); bbc->add_child(preset); //preset->set_ignore_mouse(false); preset->connect("gui_input", this, "_preset_input"); - bt_add_preset = memnew ( Button ); + bt_add_preset = memnew(Button); bt_add_preset->set_icon(get_icon("add_preset")); bt_add_preset->set_tooltip("Add current color as a preset"); bt_add_preset->connect("pressed", this, "_add_preset_pressed"); bbc->add_child(bt_add_preset); } - - - ///////////////// - -void ColorPickerButton::_color_changed(const Color& p_color) { +void ColorPickerButton::_color_changed(const Color &p_color) { update(); - emit_signal("color_changed",p_color); + emit_signal("color_changed", p_color); } - void ColorPickerButton::pressed() { - Size2 ms = Size2(300, picker->get_combined_minimum_size().height+10); - popup->set_pos(get_global_pos()-Size2(0,ms.height)); + Size2 ms = Size2(300, picker->get_combined_minimum_size().height + 10); + popup->set_pos(get_global_pos() - Size2(0, ms.height)); popup->set_size(ms); popup->popup(); picker->set_focus_on_line_edit(); - - } void ColorPickerButton::_notification(int p_what) { + if (p_what == NOTIFICATION_DRAW) { - if (p_what==NOTIFICATION_DRAW) { - - Ref<StyleBox> normal = get_stylebox("normal" ); - draw_rect(Rect2(normal->get_offset(),get_size()-normal->get_minimum_size()),picker->get_pick_color()); + Ref<StyleBox> normal = get_stylebox("normal"); + draw_rect(Rect2(normal->get_offset(), get_size() - normal->get_minimum_size()), picker->get_pick_color()); } } -void ColorPickerButton::set_pick_color(const Color& p_color){ - +void ColorPickerButton::set_pick_color(const Color &p_color) { picker->set_pick_color(p_color); update(); - emit_signal("color_changed",p_color); + emit_signal("color_changed", p_color); } -Color ColorPickerButton::get_pick_color() const{ +Color ColorPickerButton::get_pick_color() const { return picker->get_pick_color(); } @@ -653,37 +621,35 @@ void ColorPickerButton::set_edit_alpha(bool p_show) { picker->set_edit_alpha(p_show); } -bool ColorPickerButton::is_editing_alpha() const{ +bool ColorPickerButton::is_editing_alpha() const { return picker->is_editing_alpha(); - } ColorPicker *ColorPickerButton::get_picker() { return picker; } -void ColorPickerButton::_bind_methods(){ - - ClassDB::bind_method(D_METHOD("set_pick_color","color"),&ColorPickerButton::set_pick_color); - ClassDB::bind_method(D_METHOD("get_pick_color"),&ColorPickerButton::get_pick_color); - ClassDB::bind_method(D_METHOD("get_picker:ColorPicker"),&ColorPickerButton::get_picker); - ClassDB::bind_method(D_METHOD("set_edit_alpha","show"),&ColorPickerButton::set_edit_alpha); - ClassDB::bind_method(D_METHOD("is_editing_alpha"),&ColorPickerButton::is_editing_alpha); - ClassDB::bind_method(D_METHOD("_color_changed"),&ColorPickerButton::_color_changed); +void ColorPickerButton::_bind_methods() { - ADD_SIGNAL( MethodInfo("color_changed",PropertyInfo(Variant::COLOR,"color"))); - ADD_PROPERTY( PropertyInfo(Variant::COLOR,"color"),"set_pick_color","get_pick_color") ; - ADD_PROPERTY( PropertyInfo(Variant::BOOL,"edit_alpha"),"set_edit_alpha","is_editing_alpha") ; + ClassDB::bind_method(D_METHOD("set_pick_color", "color"), &ColorPickerButton::set_pick_color); + ClassDB::bind_method(D_METHOD("get_pick_color"), &ColorPickerButton::get_pick_color); + ClassDB::bind_method(D_METHOD("get_picker:ColorPicker"), &ColorPickerButton::get_picker); + ClassDB::bind_method(D_METHOD("set_edit_alpha", "show"), &ColorPickerButton::set_edit_alpha); + ClassDB::bind_method(D_METHOD("is_editing_alpha"), &ColorPickerButton::is_editing_alpha); + ClassDB::bind_method(D_METHOD("_color_changed"), &ColorPickerButton::_color_changed); + ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color"))); + ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_pick_color", "get_pick_color"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_alpha"), "set_edit_alpha", "is_editing_alpha"); } ColorPickerButton::ColorPickerButton() { - popup = memnew( PopupPanel ); - picker = memnew( ColorPicker ); + popup = memnew(PopupPanel); + picker = memnew(ColorPicker); popup->add_child(picker); - picker->connect("color_changed",this,"_color_changed"); + picker->connect("color_changed", this, "_color_changed"); add_child(popup); } diff --git a/scene/gui/color_picker.h b/scene/gui/color_picker.h index d9db9c89f7..bd1abeecbe 100644 --- a/scene/gui/color_picker.h +++ b/scene/gui/color_picker.h @@ -29,23 +29,22 @@ #ifndef COLOR_PICKER_H #define COLOR_PICKER_H -#include "scene/gui/slider.h" -#include "scene/gui/line_edit.h" -#include "scene/gui/spin_box.h" -#include "scene/gui/label.h" +#include "scene/gui/box_container.h" #include "scene/gui/button.h" +#include "scene/gui/check_button.h" +#include "scene/gui/label.h" +#include "scene/gui/line_edit.h" #include "scene/gui/popup.h" -#include "scene/gui/box_container.h" +#include "scene/gui/slider.h" +#include "scene/gui/spin_box.h" #include "scene/gui/texture_rect.h" #include "scene/gui/tool_button.h" -#include "scene/gui/check_button.h" class ColorPicker : public BoxContainer { - GDCLASS(ColorPicker,BoxContainer); + GDCLASS(ColorPicker, BoxContainer); private: - Control *screen; Image last_capture; Control *uv_edit; @@ -69,10 +68,10 @@ private: bool raw_mode_enabled; bool updating; bool changing_color; - float h,s,v; + float h, s, v; Color last_hsv; - void _html_entered(const String& p_html); + void _html_entered(const String &p_html); void _value_changed(double); void _update_controls(); void _update_color(); @@ -80,28 +79,27 @@ private: void _update_text_value(); void _text_type_toggled(); void _sample_draw(); - void _hsv_draw(int p_wich,Control *c); + void _hsv_draw(int p_wich, Control *c); - void _uv_input(const InputEvent& p_input); - void _w_input(const InputEvent& p_input); - void _preset_input(const InputEvent& p_input); - void _screen_input(const InputEvent& p_input); + void _uv_input(const InputEvent &p_input); + void _w_input(const InputEvent &p_input); + void _preset_input(const InputEvent &p_input); + void _screen_input(const InputEvent &p_input); void _add_preset_pressed(); void _screen_pick_pressed(); protected: - void _notification(int); static void _bind_methods(); -public: +public: void set_edit_alpha(bool p_show); bool is_editing_alpha() const; - void set_pick_color(const Color& p_color); + void set_pick_color(const Color &p_color); Color get_pick_color() const; - void add_preset(const Color& p_color); + void add_preset(const Color &p_color); void set_raw_mode(bool p_enabled); bool is_raw_mode() const; @@ -112,21 +110,20 @@ public: class ColorPickerButton : public Button { - GDCLASS(ColorPickerButton,Button); + GDCLASS(ColorPickerButton, Button); PopupPanel *popup; ColorPicker *picker; - void _color_changed(const Color& p_color); + void _color_changed(const Color &p_color); virtual void pressed(); protected: - void _notification(int); static void _bind_methods(); -public: - void set_pick_color(const Color& p_color); +public: + void set_pick_color(const Color &p_color); Color get_pick_color() const; void set_edit_alpha(bool p_show); diff --git a/scene/gui/color_ramp_edit.cpp b/scene/gui/color_ramp_edit.cpp index 81bc5ca9bb..131638bb70 100644 --- a/scene/gui/color_ramp_edit.cpp +++ b/scene/gui/color_ramp_edit.cpp @@ -29,74 +29,72 @@ #include "color_ramp_edit.h" #include "os/keyboard.h" -ColorRampEdit::ColorRampEdit(){ - grabbed=-1; - grabbing=false; +ColorRampEdit::ColorRampEdit() { + grabbed = -1; + grabbing = false; set_focus_mode(FOCUS_ALL); - popup = memnew( PopupPanel ); - picker = memnew( ColorPicker ); + popup = memnew(PopupPanel); + picker = memnew(ColorPicker); popup->add_child(picker); add_child(popup); - checker = Ref<ImageTexture>(memnew( ImageTexture )); - checker->create_from_image( Image(checker_bg_png),ImageTexture::FLAG_REPEAT ); + checker = Ref<ImageTexture>(memnew(ImageTexture)); + checker->create_from_image(Image(checker_bg_png), ImageTexture::FLAG_REPEAT); } int ColorRampEdit::_get_point_from_pos(int x) { int result = -1; - int total_w = get_size().width-get_size().height-3; - for(int i=0;i<points.size();i++) { + int total_w = get_size().width - get_size().height - 3; + for (int i = 0; i < points.size(); i++) { //Check if we clicked at point - if (ABS(x-points[i].offset*total_w+1)<(POINT_WIDTH/2+1)) { - result=i; + if (ABS(x - points[i].offset * total_w + 1) < (POINT_WIDTH / 2 + 1)) { + result = i; } } return result; } void ColorRampEdit::_show_color_picker() { - if (grabbed==-1) + if (grabbed == -1) return; - Size2 ms = Size2(350, picker->get_combined_minimum_size().height+10); + Size2 ms = Size2(350, picker->get_combined_minimum_size().height + 10); picker->set_pick_color(points[grabbed].color); - popup->set_pos(get_global_pos()-Vector2(ms.width-get_size().width,ms.height)); + popup->set_pos(get_global_pos() - Vector2(ms.width - get_size().width, ms.height)); popup->set_size(ms); popup->popup(); } ColorRampEdit::~ColorRampEdit() { - } -void ColorRampEdit::_gui_input(const InputEvent& p_event) { +void ColorRampEdit::_gui_input(const InputEvent &p_event) { - if (p_event.type==InputEvent::KEY && p_event.key.pressed && p_event.key.scancode==KEY_DELETE && grabbed!=-1) { + if (p_event.type == InputEvent::KEY && p_event.key.pressed && p_event.key.scancode == KEY_DELETE && grabbed != -1) { points.remove(grabbed); - grabbed=-1; - grabbing=false; + grabbed = -1; + grabbing = false; update(); emit_signal("ramp_changed"); accept_event(); } //Show color picker on double click. - if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==1 && p_event.mouse_button.doubleclick && p_event.mouse_button.pressed) { - grabbed=_get_point_from_pos(p_event.mouse_button.x); + if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index == 1 && p_event.mouse_button.doubleclick && p_event.mouse_button.pressed) { + grabbed = _get_point_from_pos(p_event.mouse_button.x); _show_color_picker(); accept_event(); } //Delete point on right click - if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==2 && p_event.mouse_button.pressed) { - grabbed=_get_point_from_pos(p_event.mouse_button.x); - if(grabbed != -1) - { + if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index == 2 && p_event.mouse_button.pressed) { + grabbed = _get_point_from_pos(p_event.mouse_button.x); + if (grabbed != -1) { points.remove(grabbed); - grabbed=-1; - grabbing=false; + grabbed = -1; + grabbing = false; update(); emit_signal("ramp_changed"); accept_event(); @@ -104,21 +102,21 @@ void ColorRampEdit::_gui_input(const InputEvent& p_event) { } //Hold alt key to duplicate selected color - if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==1 && p_event.mouse_button.pressed && p_event.key.mod.alt ) { + if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index == 1 && p_event.mouse_button.pressed && p_event.key.mod.alt) { int x = p_event.mouse_button.x; - grabbed=_get_point_from_pos(x); + grabbed = _get_point_from_pos(x); - if( grabbed != -1 ) { - int total_w = get_size().width-get_size().height-3; + if (grabbed != -1) { + int total_w = get_size().width - get_size().height - 3; ColorRamp::Point newPoint = points[grabbed]; - newPoint.offset=CLAMP(x/float(total_w),0,1); + newPoint.offset = CLAMP(x / float(total_w), 0, 1); points.push_back(newPoint); points.sort(); - for(int i=0;i<points.size();++i) { - if (points[i].offset==newPoint.offset) { - grabbed=i; + for (int i = 0; i < points.size(); ++i) { + if (points[i].offset == newPoint.offset) { + grabbed = i; break; } } @@ -128,91 +126,89 @@ void ColorRampEdit::_gui_input(const InputEvent& p_event) { } } - if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==1 && p_event.mouse_button.pressed) { + if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index == 1 && p_event.mouse_button.pressed) { update(); int x = p_event.mouse_button.x; - int total_w = get_size().width-get_size().height-3; + int total_w = get_size().width - get_size().height - 3; //Check if color selector was clicked. - if (x>total_w+3) { + if (x > total_w + 3) { _show_color_picker(); return; } - grabbing=true; + grabbing = true; - grabbed=_get_point_from_pos(x); + grabbed = _get_point_from_pos(x); //grab or select - if (grabbed!=-1) { + if (grabbed != -1) { return; } //insert ColorRamp::Point newPoint; - newPoint.offset=CLAMP(x/float(total_w),0,1); + newPoint.offset = CLAMP(x / float(total_w), 0, 1); ColorRamp::Point prev; ColorRamp::Point next; - int pos=-1; - for(int i=0;i<points.size();i++) { - if (points[i].offset<newPoint.offset) - pos=i; + int pos = -1; + for (int i = 0; i < points.size(); i++) { + if (points[i].offset < newPoint.offset) + pos = i; } - if (pos==-1) { + if (pos == -1) { - prev.color=Color(0,0,0); - prev.offset=0; + prev.color = Color(0, 0, 0); + prev.offset = 0; if (points.size()) { - next=points[0]; + next = points[0]; } else { - next.color=Color(1,1,1); - next.offset=1.0; + next.color = Color(1, 1, 1); + next.offset = 1.0; } - } else { + } else { - if (pos==points.size()-1) { - next.color=Color(1,1,1); - next.offset=1.0; + if (pos == points.size() - 1) { + next.color = Color(1, 1, 1); + next.offset = 1.0; } else { - next=points[pos+1]; + next = points[pos + 1]; } - prev=points[pos]; - + prev = points[pos]; } - newPoint.color=prev.color.linear_interpolate(next.color,(newPoint.offset-prev.offset)/(next.offset-prev.offset)); + newPoint.color = prev.color.linear_interpolate(next.color, (newPoint.offset - prev.offset) / (next.offset - prev.offset)); points.push_back(newPoint); points.sort(); - for(int i=0;i<points.size();i++) { - if (points[i].offset==newPoint.offset) { - grabbed=i; + for (int i = 0; i < points.size(); i++) { + if (points[i].offset == newPoint.offset) { + grabbed = i; break; } } emit_signal("ramp_changed"); - } - if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==1 && !p_event.mouse_button.pressed) { + if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index == 1 && !p_event.mouse_button.pressed) { if (grabbing) { - grabbing=false; + grabbing = false; emit_signal("ramp_changed"); } update(); } - if (p_event.type==InputEvent::MOUSE_MOTION && grabbing) { + if (p_event.type == InputEvent::MOUSE_MOTION && grabbing) { - int total_w = get_size().width-get_size().height-3; + int total_w = get_size().width - get_size().height - 3; int x = p_event.mouse_motion.x; - float newofs = CLAMP(x/float(total_w),0,1); + float newofs = CLAMP(x / float(total_w), 0, 1); //Snap to nearest point if holding shift if (p_event.key.mod.shift) { @@ -220,7 +216,7 @@ void ColorRampEdit::_gui_input(const InputEvent& p_event) { float smallest_ofs = snap_treshhold; bool founded = false; int nearest_point; - for(int i=0;i<points.size();++i) { + for (int i = 0; i < points.size(); ++i) { if (i != grabbed) { float temp_ofs = ABS(points[i].offset - newofs); if (temp_ofs < smallest_ofs) { @@ -234,30 +230,30 @@ void ColorRampEdit::_gui_input(const InputEvent& p_event) { } if (founded) { if (points[nearest_point].offset < newofs) - newofs = points[nearest_point].offset+0.00001; + newofs = points[nearest_point].offset + 0.00001; else - newofs = points[nearest_point].offset-0.00001; - newofs = CLAMP(newofs,0,1); + newofs = points[nearest_point].offset - 0.00001; + newofs = CLAMP(newofs, 0, 1); } } - bool valid=true; - for(int i=0;i<points.size();i++) { + bool valid = true; + for (int i = 0; i < points.size(); i++) { - if (points[i].offset==newofs && i!=grabbed) { - valid=false; + if (points[i].offset == newofs && i != grabbed) { + valid = false; } } if (!valid) return; - points[grabbed].offset=newofs; + points[grabbed].offset = newofs; points.sort(); - for(int i=0;i<points.size();i++) { - if (points[i].offset==newofs) { - grabbed=i; + for (int i = 0; i < points.size(); i++) { + if (points[i].offset == newofs) { + grabbed = i; break; } } @@ -270,12 +266,12 @@ void ColorRampEdit::_gui_input(const InputEvent& p_event) { void ColorRampEdit::_notification(int p_what) { - if (p_what==NOTIFICATION_ENTER_TREE) { - if (!picker->is_connected("color_changed",this,"_color_changed")) { - picker->connect("color_changed",this,"_color_changed"); + if (p_what == NOTIFICATION_ENTER_TREE) { + if (!picker->is_connected("color_changed", this, "_color_changed")) { + picker->connect("color_changed", this, "_color_changed"); } } - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { int w = get_size().x; int h = get_size().y; @@ -283,89 +279,86 @@ void ColorRampEdit::_notification(int p_what) { if (w == 0 || h == 0) return; //Safety check. We have division by 'h'. And in any case there is nothing to draw with such size - int total_w = get_size().width-get_size().height-3; + int total_w = get_size().width - get_size().height - 3; //Draw checker pattern for ramp - _draw_checker(0,0, total_w, h); + _draw_checker(0, 0, total_w, h); //Draw color ramp ColorRamp::Point prev; - prev.offset=0; - if(points.size() == 0) - prev.color=Color(0,0,0); //Draw black rectangle if we have no points + prev.offset = 0; + if (points.size() == 0) + prev.color = Color(0, 0, 0); //Draw black rectangle if we have no points else - prev.color = points[0].color; //Extend color of first point to the beginning. + prev.color = points[0].color; //Extend color of first point to the beginning. - for(int i=-1;i<points.size();i++) { + for (int i = -1; i < points.size(); i++) { ColorRamp::Point next; //If there is no next point - if (i+1 == points.size()) { - if(points.size() == 0) - next.color=Color(0,0,0); //Draw black rectangle if we have no points + if (i + 1 == points.size()) { + if (points.size() == 0) + next.color = Color(0, 0, 0); //Draw black rectangle if we have no points else - next.color=points[i].color; //Extend color of last point to the end. - next.offset=1; + next.color = points[i].color; //Extend color of last point to the end. + next.offset = 1; } else { - next=points[i+1]; + next = points[i + 1]; } - if (prev.offset==next.offset) { - prev=next; + if (prev.offset == next.offset) { + prev = next; continue; } Vector<Vector2> points; Vector<Color> colors; - points.push_back(Vector2(prev.offset*total_w,h)); - points.push_back(Vector2(prev.offset*total_w,0)); - points.push_back(Vector2(next.offset*total_w,0)); - points.push_back(Vector2(next.offset*total_w,h)); + points.push_back(Vector2(prev.offset * total_w, h)); + points.push_back(Vector2(prev.offset * total_w, 0)); + points.push_back(Vector2(next.offset * total_w, 0)); + points.push_back(Vector2(next.offset * total_w, h)); colors.push_back(prev.color); colors.push_back(prev.color); colors.push_back(next.color); colors.push_back(next.color); - draw_primitive(points,colors,Vector<Point2>()); - prev=next; + draw_primitive(points, colors, Vector<Point2>()); + prev = next; } //Draw point markers - for(int i=0;i<points.size();i++) { + for (int i = 0; i < points.size(); i++) { - Color col = i==grabbed?Color(1,0.0,0.0,0.9):points[i].color.contrasted(); + Color col = i == grabbed ? Color(1, 0.0, 0.0, 0.9) : points[i].color.contrasted(); col.a = 0.9; - draw_line(Vector2(points[i].offset*total_w,0),Vector2(points[i].offset*total_w,h/2),col); - draw_rect(Rect2(points[i].offset*total_w-POINT_WIDTH/2, h/2, POINT_WIDTH, h/2), Color(0.6, 0.6, 0.6, i==grabbed?0.9:0.4)); - draw_line(Vector2(points[i].offset*total_w-POINT_WIDTH/2,h/2),Vector2(points[i].offset*total_w-POINT_WIDTH/2,h-1),col); - draw_line(Vector2(points[i].offset*total_w+POINT_WIDTH/2,h/2),Vector2(points[i].offset*total_w+POINT_WIDTH/2,h-1),col); - draw_line(Vector2(points[i].offset*total_w-POINT_WIDTH/2,h/2),Vector2(points[i].offset*total_w+POINT_WIDTH/2,h/2),col); - draw_line(Vector2(points[i].offset*total_w-POINT_WIDTH/2,h-1),Vector2(points[i].offset*total_w+POINT_WIDTH/2,h-1),col); - + draw_line(Vector2(points[i].offset * total_w, 0), Vector2(points[i].offset * total_w, h / 2), col); + draw_rect(Rect2(points[i].offset * total_w - POINT_WIDTH / 2, h / 2, POINT_WIDTH, h / 2), Color(0.6, 0.6, 0.6, i == grabbed ? 0.9 : 0.4)); + draw_line(Vector2(points[i].offset * total_w - POINT_WIDTH / 2, h / 2), Vector2(points[i].offset * total_w - POINT_WIDTH / 2, h - 1), col); + draw_line(Vector2(points[i].offset * total_w + POINT_WIDTH / 2, h / 2), Vector2(points[i].offset * total_w + POINT_WIDTH / 2, h - 1), col); + draw_line(Vector2(points[i].offset * total_w - POINT_WIDTH / 2, h / 2), Vector2(points[i].offset * total_w + POINT_WIDTH / 2, h / 2), col); + draw_line(Vector2(points[i].offset * total_w - POINT_WIDTH / 2, h - 1), Vector2(points[i].offset * total_w + POINT_WIDTH / 2, h - 1), col); } - //Draw "button" for color selector - _draw_checker(total_w+3,0, h, h); - if (grabbed!=-1) { + _draw_checker(total_w + 3, 0, h, h); + if (grabbed != -1) { //Draw with selection color - draw_rect(Rect2(total_w+3,0,h,h),points[grabbed].color); + draw_rect(Rect2(total_w + 3, 0, h, h), points[grabbed].color); } else { //if no color selected draw grey color with 'X' on top. - draw_rect(Rect2(total_w+3,0,h,h), Color(0.5, 0.5, 0.5, 1)); - draw_line(Vector2(total_w+3,0),Vector2(total_w+3+h,h),Color(1,1,1,0.6)); - draw_line(Vector2(total_w+3,h),Vector2(total_w+3+h,0),Color(1,1,1,0.6)); + draw_rect(Rect2(total_w + 3, 0, h, h), Color(0.5, 0.5, 0.5, 1)); + draw_line(Vector2(total_w + 3, 0), Vector2(total_w + 3 + h, h), Color(1, 1, 1, 0.6)); + draw_line(Vector2(total_w + 3, h), Vector2(total_w + 3 + h, 0), Color(1, 1, 1, 0.6)); } //Draw borders around color ramp if in focus if (has_focus()) { - draw_line(Vector2(-1,-1),Vector2(total_w+1,-1),Color(1,1,1,0.6)); - draw_line(Vector2(total_w+1,-1),Vector2(total_w+1,h+1),Color(1,1,1,0.6)); - draw_line(Vector2(total_w+1,h+1),Vector2(-1,h+1),Color(1,1,1,0.6)); - draw_line(Vector2(-1,-1),Vector2(-1,h+1),Color(1,1,1,0.6)); + draw_line(Vector2(-1, -1), Vector2(total_w + 1, -1), Color(1, 1, 1, 0.6)); + draw_line(Vector2(total_w + 1, -1), Vector2(total_w + 1, h + 1), Color(1, 1, 1, 0.6)); + draw_line(Vector2(total_w + 1, h + 1), Vector2(-1, h + 1), Color(1, 1, 1, 0.6)); + draw_line(Vector2(-1, -1), Vector2(-1, h + 1), Color(1, 1, 1, 0.6)); } - } } @@ -373,9 +366,9 @@ void ColorRampEdit::_draw_checker(int x, int y, int w, int h) { //Draw it with polygon to insert UVs for scale Vector<Vector2> backPoints; backPoints.push_back(Vector2(x, y)); - backPoints.push_back(Vector2(x, y+h)); - backPoints.push_back(Vector2(x+w, y+h)); - backPoints.push_back(Vector2(x+w, y)); + backPoints.push_back(Vector2(x, y + h)); + backPoints.push_back(Vector2(x + w, y + h)); + backPoints.push_back(Vector2(x + w, y)); Vector<Color> colorPoints; colorPoints.push_back(Color(1, 1, 1, 1)); colorPoints.push_back(Color(1, 1, 1, 1)); @@ -384,35 +377,34 @@ void ColorRampEdit::_draw_checker(int x, int y, int w, int h) { Vector<Vector2> uvPoints; //Draw checker pattern pixel-perfect and scale it by 2. uvPoints.push_back(Vector2(x, y)); - uvPoints.push_back(Vector2(x, y+h*.5f/checker->get_height())); - uvPoints.push_back(Vector2(x+w*.5f/checker->get_width(), y+h*.5f/checker->get_height())); - uvPoints.push_back(Vector2(x+w*.5f/checker->get_width(), y)); + uvPoints.push_back(Vector2(x, y + h * .5f / checker->get_height())); + uvPoints.push_back(Vector2(x + w * .5f / checker->get_width(), y + h * .5f / checker->get_height())); + uvPoints.push_back(Vector2(x + w * .5f / checker->get_width(), y)); draw_polygon(backPoints, colorPoints, uvPoints, checker); } Size2 ColorRampEdit::get_minimum_size() const { - return Vector2(0,16); + return Vector2(0, 16); } -void ColorRampEdit::_color_changed(const Color& p_color) { +void ColorRampEdit::_color_changed(const Color &p_color) { - if (grabbed==-1) + if (grabbed == -1) return; - points[grabbed].color=p_color; + points[grabbed].color = p_color; update(); emit_signal("ramp_changed"); - } -void ColorRampEdit::set_ramp(const Vector<float>& p_offsets,const Vector<Color>& p_colors) { +void ColorRampEdit::set_ramp(const Vector<float> &p_offsets, const Vector<Color> &p_colors) { - ERR_FAIL_COND(p_offsets.size()!=p_colors.size()); + ERR_FAIL_COND(p_offsets.size() != p_colors.size()); points.clear(); - for(int i=0;i<p_offsets.size();i++) { + for (int i = 0; i < p_offsets.size(); i++) { ColorRamp::Point p; - p.offset=p_offsets[i]; - p.color=p_colors[i]; + p.offset = p_offsets[i]; + p.color = p_colors[i]; points.push_back(p); } @@ -422,31 +414,31 @@ void ColorRampEdit::set_ramp(const Vector<float>& p_offsets,const Vector<Color>& Vector<float> ColorRampEdit::get_offsets() const { Vector<float> ret; - for(int i=0;i<points.size();i++) + for (int i = 0; i < points.size(); i++) ret.push_back(points[i].offset); return ret; } Vector<Color> ColorRampEdit::get_colors() const { Vector<Color> ret; - for(int i=0;i<points.size();i++) + for (int i = 0; i < points.size(); i++) ret.push_back(points[i].color); return ret; } -void ColorRampEdit::set_points(Vector<ColorRamp::Point>& p_points) { - if(points.size() != p_points.size()) +void ColorRampEdit::set_points(Vector<ColorRamp::Point> &p_points) { + if (points.size() != p_points.size()) grabbed = -1; points.clear(); points = p_points; } -Vector<ColorRamp::Point>& ColorRampEdit::get_points() { +Vector<ColorRamp::Point> &ColorRampEdit::get_points() { return points; } void ColorRampEdit::_bind_methods() { - ClassDB::bind_method(D_METHOD("_gui_input"),&ColorRampEdit::_gui_input); - ClassDB::bind_method(D_METHOD("_color_changed"),&ColorRampEdit::_color_changed); + ClassDB::bind_method(D_METHOD("_gui_input"), &ColorRampEdit::_gui_input); + ClassDB::bind_method(D_METHOD("_color_changed"), &ColorRampEdit::_color_changed); ADD_SIGNAL(MethodInfo("ramp_changed")); } diff --git a/scene/gui/color_ramp_edit.h b/scene/gui/color_ramp_edit.h index c6a20a539d..bb12064ddf 100644 --- a/scene/gui/color_ramp_edit.h +++ b/scene/gui/color_ramp_edit.h @@ -29,8 +29,8 @@ #ifndef SCENE_GUI_COLOR_RAMP_EDIT_H_ #define SCENE_GUI_COLOR_RAMP_EDIT_H_ -#include "scene/gui/popup.h" #include "scene/gui/color_picker.h" +#include "scene/gui/popup.h" #include "scene/resources/color_ramp.h" #include "scene/resources/default_theme/theme_data.h" @@ -38,7 +38,7 @@ class ColorRampEdit : public Control { - GDCLASS(ColorRampEdit,Control); + GDCLASS(ColorRampEdit, Control); PopupPanel *popup; ColorPicker *picker; @@ -50,21 +50,21 @@ class ColorRampEdit : public Control { Vector<ColorRamp::Point> points; void _draw_checker(int x, int y, int w, int h); - void _color_changed(const Color& p_color); + void _color_changed(const Color &p_color); int _get_point_from_pos(int x); void _show_color_picker(); protected: - void _gui_input(const InputEvent& p_event); + void _gui_input(const InputEvent &p_event); void _notification(int p_what); static void _bind_methods(); public: - void set_ramp(const Vector<float>& p_offsets,const Vector<Color>& p_colors); + void set_ramp(const Vector<float> &p_offsets, const Vector<Color> &p_colors); Vector<float> get_offsets() const; Vector<Color> get_colors() const; - void set_points(Vector<ColorRamp::Point>& p_points); - Vector<ColorRamp::Point>& get_points(); + void set_points(Vector<ColorRamp::Point> &p_points); + Vector<ColorRamp::Point> &get_points(); virtual Size2 get_minimum_size() const; ColorRampEdit(); @@ -76,5 +76,4 @@ public: GDCLASS(ColorRampEditPanel, Panel ); };*/ - #endif /* SCENE_GUI_COLOR_RAMP_EDIT_H_ */ diff --git a/scene/gui/color_rect.cpp b/scene/gui/color_rect.cpp index d72dffcae3..7febecc0b9 100644 --- a/scene/gui/color_rect.cpp +++ b/scene/gui/color_rect.cpp @@ -28,34 +28,33 @@ /*************************************************************************/ #include "color_rect.h" +void ColorRect::set_frame_color(const Color &p_color) { -void ColorRect::set_frame_color(const Color& p_color) { - - color=p_color; + color = p_color; update(); } -Color ColorRect::get_frame_color() const{ +Color ColorRect::get_frame_color() const { return color; } void ColorRect::_notification(int p_what) { - if (p_what==NOTIFICATION_DRAW) { - draw_rect(Rect2(Point2(),get_size()),color); + if (p_what == NOTIFICATION_DRAW) { + draw_rect(Rect2(Point2(), get_size()), color); } } void ColorRect::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_frame_color","color"),&ColorRect::set_frame_color); - ClassDB::bind_method(D_METHOD("get_frame_color"),&ColorRect::get_frame_color); + ClassDB::bind_method(D_METHOD("set_frame_color", "color"), &ColorRect::set_frame_color); + ClassDB::bind_method(D_METHOD("get_frame_color"), &ColorRect::get_frame_color); - ADD_PROPERTY(PropertyInfo(Variant::COLOR,"color"),"set_frame_color","get_frame_color") ; + ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_frame_color", "get_frame_color"); } ColorRect::ColorRect() { - color=Color(1,1,1); + color = Color(1, 1, 1); } diff --git a/scene/gui/color_rect.h b/scene/gui/color_rect.h index 55e413ce27..c6d0164ac9 100644 --- a/scene/gui/color_rect.h +++ b/scene/gui/color_rect.h @@ -31,17 +31,17 @@ #include "scene/gui/control.h" -class ColorRect : public Control { - GDCLASS(ColorRect,Control) +class ColorRect : public Control { + GDCLASS(ColorRect, Control) Color color; -protected: +protected: void _notification(int p_what); static void _bind_methods(); -public: - void set_frame_color(const Color& p_color); +public: + void set_frame_color(const Color &p_color); Color get_frame_color() const; ColorRect(); diff --git a/scene/gui/container.cpp b/scene/gui/container.cpp index 655c6e1bbf..811b41863c 100644 --- a/scene/gui/container.cpp +++ b/scene/gui/container.cpp @@ -27,9 +27,8 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "container.h" -#include "scene/scene_string_names.h" #include "message_queue.h" - +#include "scene/scene_string_names.h" void Container::_child_minsize_changed() { @@ -47,12 +46,10 @@ void Container::add_child_notify(Node *p_child) { if (!control) return; - control->connect("size_flags_changed",this,"queue_sort"); - control->connect("minimum_size_changed",this,"_child_minsize_changed"); - control->connect("visibility_changed",this,"_child_minsize_changed"); + control->connect("size_flags_changed", this, "queue_sort"); + control->connect("minimum_size_changed", this, "_child_minsize_changed"); + control->connect("visibility_changed", this, "_child_minsize_changed"); queue_sort(); - - } void Container::move_child_notify(Node *p_child) { @@ -63,8 +60,6 @@ void Container::move_child_notify(Node *p_child) { return; queue_sort(); - - } void Container::remove_child_notify(Node *p_child) { @@ -75,9 +70,9 @@ void Container::remove_child_notify(Node *p_child) { if (!control) return; - control->disconnect("size_flags_changed",this,"queue_sort"); - control->disconnect("minimum_size_changed",this,"_child_minsize_changed"); - control->disconnect("visibility_changed",this,"_child_minsize_changed"); + control->disconnect("size_flags_changed", this, "queue_sort"); + control->disconnect("minimum_size_changed", this, "_child_minsize_changed"); + control->disconnect("visibility_changed", this, "_child_minsize_changed"); queue_sort(); } @@ -88,33 +83,33 @@ void Container::_sort_children() { notification(NOTIFICATION_SORT_CHILDREN); emit_signal(SceneStringNames::get_singleton()->sort_children); - pending_sort=false; + pending_sort = false; } -void Container::fit_child_in_rect(Control *p_child,const Rect2& p_rect) { +void Container::fit_child_in_rect(Control *p_child, const Rect2 &p_rect) { - ERR_FAIL_COND(p_child->get_parent()!=this); + ERR_FAIL_COND(p_child->get_parent() != this); Size2 minsize = p_child->get_combined_minimum_size(); - Rect2 r=p_rect; + Rect2 r = p_rect; - if (!(p_child->get_h_size_flags()&SIZE_FILL)) { - r.size.x=minsize.x; - r.pos.x += Math::floor((p_rect.size.x - minsize.x)/2); + if (!(p_child->get_h_size_flags() & SIZE_FILL)) { + r.size.x = minsize.x; + r.pos.x += Math::floor((p_rect.size.x - minsize.x) / 2); } - if (!(p_child->get_v_size_flags()&SIZE_FILL)) { - r.size.y=minsize.y; - r.pos.y += Math::floor((p_rect.size.y - minsize.y)/2); + if (!(p_child->get_v_size_flags() & SIZE_FILL)) { + r.size.y = minsize.y; + r.pos.y += Math::floor((p_rect.size.y - minsize.y) / 2); } - for(int i=0;i<4;i++) - p_child->set_anchor(Margin(i),ANCHOR_BEGIN); + for (int i = 0; i < 4; i++) + p_child->set_anchor(Margin(i), ANCHOR_BEGIN); p_child->set_pos(r.pos); p_child->set_size(r.size); p_child->set_rotation(0); - p_child->set_scale(Vector2(1,1)); + p_child->set_scale(Vector2(1, 1)); } void Container::queue_sort() { @@ -125,16 +120,16 @@ void Container::queue_sort() { if (pending_sort) return; - MessageQueue::get_singleton()->push_call(this,"_sort_children"); - pending_sort=true; + MessageQueue::get_singleton()->push_call(this, "_sort_children"); + pending_sort = true; } void Container::_notification(int p_what) { - switch(p_what) { + switch (p_what) { case NOTIFICATION_ENTER_TREE: { - pending_sort=false; + pending_sort = false; queue_sort(); } break; case NOTIFICATION_RESIZED: { @@ -156,17 +151,17 @@ void Container::_notification(int p_what) { void Container::_bind_methods() { - ClassDB::bind_method(D_METHOD("_sort_children"),&Container::_sort_children); - ClassDB::bind_method(D_METHOD("_child_minsize_changed"),&Container::_child_minsize_changed); + ClassDB::bind_method(D_METHOD("_sort_children"), &Container::_sort_children); + ClassDB::bind_method(D_METHOD("_child_minsize_changed"), &Container::_child_minsize_changed); - ClassDB::bind_method(D_METHOD("queue_sort"),&Container::queue_sort); - ClassDB::bind_method(D_METHOD("fit_child_in_rect","child:Control","rect"),&Container::fit_child_in_rect); + ClassDB::bind_method(D_METHOD("queue_sort"), &Container::queue_sort); + ClassDB::bind_method(D_METHOD("fit_child_in_rect", "child:Control", "rect"), &Container::fit_child_in_rect); - BIND_CONSTANT( NOTIFICATION_SORT_CHILDREN ); + BIND_CONSTANT(NOTIFICATION_SORT_CHILDREN); ADD_SIGNAL(MethodInfo("sort_children")); } Container::Container() { - pending_sort=false; + pending_sort = false; } diff --git a/scene/gui/container.h b/scene/gui/container.h index bb47524972..78f647f59d 100644 --- a/scene/gui/container.h +++ b/scene/gui/container.h @@ -33,13 +33,13 @@ class Container : public Control { - GDCLASS(Container,Control); + GDCLASS(Container, Control); bool pending_sort; void _sort_children(); void _child_minsize_changed(); -protected: +protected: void queue_sort(); virtual void add_child_notify(Node *p_child); virtual void move_child_notify(Node *p_child); @@ -47,12 +47,13 @@ protected: void _notification(int p_what); static void _bind_methods(); + public: enum { - NOTIFICATION_SORT_CHILDREN=50 + NOTIFICATION_SORT_CHILDREN = 50 }; - void fit_child_in_rect(Control *p_child,const Rect2& p_rect); + void fit_child_in_rect(Control *p_child, const Rect2 &p_rect); Container(); }; diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index 496b1e03cf..7779391bae 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -27,55 +27,51 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "control.h" -#include "servers/visual_server.h" -#include "scene/main/viewport.h" -#include "scene/main/canvas_layer.h" #include "global_config.h" +#include "scene/main/canvas_layer.h" +#include "scene/main/viewport.h" +#include "servers/visual_server.h" -#include "print_string.h" +#include "message_queue.h" #include "os/keyboard.h" #include "os/os.h" -#include "message_queue.h" -#include "scene/scene_string_names.h" -#include "scene/gui/panel.h" +#include "print_string.h" #include "scene/gui/label.h" +#include "scene/gui/panel.h" +#include "scene/scene_string_names.h" #ifdef TOOLS_ENABLED #include "editor/editor_settings.h" #endif #include <stdio.h> - - - Variant Control::edit_get_state() const { Dictionary s; - s["rect"]=get_rect(); - s["rot"]=get_rotation(); - s["scale"]=get_scale(); + s["rect"] = get_rect(); + s["rot"] = get_rotation(); + s["scale"] = get_scale(); return s; - } -void Control::edit_set_state(const Variant& p_state) { +void Control::edit_set_state(const Variant &p_state) { - Dictionary s=p_state; + Dictionary s = p_state; - Rect2 state=s["rect"]; + Rect2 state = s["rect"]; set_pos(state.pos); set_size(state.size); set_rotation(s["rot"]); set_scale(s["scale"]); } -void Control::set_custom_minimum_size(const Size2& p_custom) { +void Control::set_custom_minimum_size(const Size2 &p_custom) { - if (p_custom==data.custom_minimum_size) + if (p_custom == data.custom_minimum_size) return; - data.custom_minimum_size=p_custom; + data.custom_minimum_size = p_custom; minimum_size_changed(); } -Size2 Control::get_custom_minimum_size() const{ +Size2 Control::get_custom_minimum_size() const { return data.custom_minimum_size; } @@ -83,8 +79,8 @@ Size2 Control::get_custom_minimum_size() const{ Size2 Control::get_combined_minimum_size() const { Size2 minsize = get_minimum_size(); - minsize.x = MAX(minsize.x,data.custom_minimum_size.x); - minsize.y = MAX(minsize.y,data.custom_minimum_size.y); + minsize.x = MAX(minsize.x, data.custom_minimum_size.x); + minsize.y = MAX(minsize.y, data.custom_minimum_size.y); return minsize; } @@ -93,51 +89,48 @@ Size2 Control::edit_get_minimum_size() const { return get_combined_minimum_size(); } -void Control::edit_set_rect(const Rect2& p_edit_rect) { - +void Control::edit_set_rect(const Rect2 &p_edit_rect) { Transform2D postxf; - postxf.set_rotation_and_scale(data.rotation,data.scale); + postxf.set_rotation_and_scale(data.rotation, data.scale); Vector2 new_pos = postxf.xform(p_edit_rect.pos); - Vector2 pos = get_pos()+new_pos; + Vector2 pos = get_pos() + new_pos; - Rect2 new_rect=get_rect(); - new_rect.pos=pos.snapped(Vector2(1,1)); - new_rect.size=p_edit_rect.size.snapped(Vector2(1,1)); + Rect2 new_rect = get_rect(); + new_rect.pos = pos.snapped(Vector2(1, 1)); + new_rect.size = p_edit_rect.size.snapped(Vector2(1, 1)); set_pos(new_rect.pos); set_size(new_rect.size); - } -bool Control::_set(const StringName& p_name, const Variant& p_value) { +bool Control::_set(const StringName &p_name, const Variant &p_value) { - - String name= p_name; + String name = p_name; if (!name.begins_with("custom")) { return false; } - if (p_value.get_type()==Variant::NIL) { + if (p_value.get_type() == Variant::NIL) { if (name.begins_with("custom_icons/")) { - String dname = name.get_slicec('/',1); + String dname = name.get_slicec('/', 1); data.icon_override.erase(dname); notification(NOTIFICATION_THEME_CHANGED); update(); } else if (name.begins_with("custom_shaders/")) { - String dname = name.get_slicec('/',1); + String dname = name.get_slicec('/', 1); data.shader_override.erase(dname); notification(NOTIFICATION_THEME_CHANGED); update(); } else if (name.begins_with("custom_styles/")) { - String dname = name.get_slicec('/',1); + String dname = name.get_slicec('/', 1); data.style_override.erase(dname); notification(NOTIFICATION_THEME_CHANGED); update(); } else if (name.begins_with("custom_fonts/")) { - String dname = name.get_slicec('/',1); + String dname = name.get_slicec('/', 1); if (data.font_override.has(dname)) { _unref_font(data.font_override[dname]); } @@ -145,12 +138,12 @@ bool Control::_set(const StringName& p_name, const Variant& p_value) { notification(NOTIFICATION_THEME_CHANGED); update(); } else if (name.begins_with("custom_colors/")) { - String dname = name.get_slicec('/',1); + String dname = name.get_slicec('/', 1); data.color_override.erase(dname); notification(NOTIFICATION_THEME_CHANGED); update(); } else if (name.begins_with("custom_constants/")) { - String dname = name.get_slicec('/',1); + String dname = name.get_slicec('/', 1); data.constant_override.erase(dname); notification(NOTIFICATION_THEME_CHANGED); update(); @@ -159,34 +152,33 @@ bool Control::_set(const StringName& p_name, const Variant& p_value) { } else { if (name.begins_with("custom_icons/")) { - String dname = name.get_slicec('/',1); + String dname = name.get_slicec('/', 1); notification(NOTIFICATION_THEME_CHANGED); - add_icon_override(dname,p_value); + add_icon_override(dname, p_value); } else if (name.begins_with("custom_shaders/")) { - String dname = name.get_slicec('/',1); - add_shader_override(dname,p_value); + String dname = name.get_slicec('/', 1); + add_shader_override(dname, p_value); notification(NOTIFICATION_THEME_CHANGED); } else if (name.begins_with("custom_styles/")) { - String dname = name.get_slicec('/',1); - add_style_override(dname,p_value); + String dname = name.get_slicec('/', 1); + add_style_override(dname, p_value); notification(NOTIFICATION_THEME_CHANGED); } else if (name.begins_with("custom_fonts/")) { - String dname = name.get_slicec('/',1); - add_font_override(dname,p_value); + String dname = name.get_slicec('/', 1); + add_font_override(dname, p_value); notification(NOTIFICATION_THEME_CHANGED); } else if (name.begins_with("custom_colors/")) { - String dname = name.get_slicec('/',1); - add_color_override(dname,p_value); + String dname = name.get_slicec('/', 1); + add_color_override(dname, p_value); notification(NOTIFICATION_THEME_CHANGED); } else if (name.begins_with("custom_constants/")) { - String dname = name.get_slicec('/',1); - add_constant_override(dname,p_value); + String dname = name.get_slicec('/', 1); + add_constant_override(dname, p_value); notification(NOTIFICATION_THEME_CHANGED); } else return false; } return true; - } void Control::_update_minimum_size() { @@ -194,155 +186,142 @@ void Control::_update_minimum_size() { if (!is_inside_tree()) return; - data.pending_min_size_update=false; + data.pending_min_size_update = false; Size2 minsize = get_combined_minimum_size(); if (minsize.x > data.size_cache.x || - minsize.y > data.size_cache.y - ) { + minsize.y > data.size_cache.y) { _size_changed(); } emit_signal(SceneStringNames::get_singleton()->minimum_size_changed); - } -bool Control::_get(const StringName& p_name,Variant &r_ret) const { +bool Control::_get(const StringName &p_name, Variant &r_ret) const { - - String sname=p_name; + String sname = p_name; if (!sname.begins_with("custom")) { return false; - } if (sname.begins_with("custom_icons/")) { - String name = sname.get_slicec('/',1); + String name = sname.get_slicec('/', 1); - r_ret= data.icon_override.has(name)?Variant(data.icon_override[name]):Variant(); + r_ret = data.icon_override.has(name) ? Variant(data.icon_override[name]) : Variant(); } else if (sname.begins_with("custom_shaders/")) { - String name = sname.get_slicec('/',1); + String name = sname.get_slicec('/', 1); - r_ret= data.shader_override.has(name)?Variant(data.shader_override[name]):Variant(); + r_ret = data.shader_override.has(name) ? Variant(data.shader_override[name]) : Variant(); } else if (sname.begins_with("custom_styles/")) { - String name = sname.get_slicec('/',1); + String name = sname.get_slicec('/', 1); - r_ret= data.style_override.has(name)?Variant(data.style_override[name]):Variant(); + r_ret = data.style_override.has(name) ? Variant(data.style_override[name]) : Variant(); } else if (sname.begins_with("custom_fonts/")) { - String name = sname.get_slicec('/',1); + String name = sname.get_slicec('/', 1); - r_ret= data.font_override.has(name)?Variant(data.font_override[name]):Variant(); + r_ret = data.font_override.has(name) ? Variant(data.font_override[name]) : Variant(); } else if (sname.begins_with("custom_colors/")) { - String name = sname.get_slicec('/',1); - r_ret= data.color_override.has(name)?Variant(data.color_override[name]):Variant(); + String name = sname.get_slicec('/', 1); + r_ret = data.color_override.has(name) ? Variant(data.color_override[name]) : Variant(); } else if (sname.begins_with("custom_constants/")) { - String name = sname.get_slicec('/',1); + String name = sname.get_slicec('/', 1); - r_ret= data.constant_override.has(name)?Variant(data.constant_override[name]):Variant(); + r_ret = data.constant_override.has(name) ? Variant(data.constant_override[name]) : Variant(); } else return false; - - return true; - - } -void Control::_get_property_list( List<PropertyInfo> *p_list) const { +void Control::_get_property_list(List<PropertyInfo> *p_list) const { Ref<Theme> theme; if (data.theme.is_valid()) { - theme=data.theme; + theme = data.theme; } else { - theme=Theme::get_default(); + theme = Theme::get_default(); } - { List<StringName> names; - theme->get_icon_list(get_class_name(),&names); - for(List<StringName>::Element *E=names.front();E;E=E->next()) { + theme->get_icon_list(get_class_name(), &names); + for (List<StringName>::Element *E = names.front(); E; E = E->next()) { - uint32_t hint= PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CHECKABLE; + uint32_t hint = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE; if (data.icon_override.has(E->get())) - hint|=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_CHECKED; + hint |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED; - p_list->push_back( PropertyInfo(Variant::OBJECT,"custom_icons/"+E->get(),PROPERTY_HINT_RESOURCE_TYPE, "Texture",hint) ); + p_list->push_back(PropertyInfo(Variant::OBJECT, "custom_icons/" + E->get(), PROPERTY_HINT_RESOURCE_TYPE, "Texture", hint)); } } { List<StringName> names; - theme->get_shader_list(get_class_name(),&names); - for(List<StringName>::Element *E=names.front();E;E=E->next()) { + theme->get_shader_list(get_class_name(), &names); + for (List<StringName>::Element *E = names.front(); E; E = E->next()) { - uint32_t hint= PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CHECKABLE; + uint32_t hint = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE; if (data.shader_override.has(E->get())) - hint|=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_CHECKED; + hint |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED; - p_list->push_back( PropertyInfo(Variant::OBJECT,"custom_shaders/"+E->get(),PROPERTY_HINT_RESOURCE_TYPE, "CanvasItemShader,CanvasItemShaderGraph",hint) ); + p_list->push_back(PropertyInfo(Variant::OBJECT, "custom_shaders/" + E->get(), PROPERTY_HINT_RESOURCE_TYPE, "CanvasItemShader,CanvasItemShaderGraph", hint)); } } { List<StringName> names; - theme->get_stylebox_list(get_class_name(),&names); - for(List<StringName>::Element *E=names.front();E;E=E->next()) { + theme->get_stylebox_list(get_class_name(), &names); + for (List<StringName>::Element *E = names.front(); E; E = E->next()) { - uint32_t hint= PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CHECKABLE; + uint32_t hint = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE; if (data.style_override.has(E->get())) - hint|=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_CHECKED; + hint |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED; - p_list->push_back( PropertyInfo(Variant::OBJECT,"custom_styles/"+E->get(),PROPERTY_HINT_RESOURCE_TYPE, "StyleBox",hint) ); + p_list->push_back(PropertyInfo(Variant::OBJECT, "custom_styles/" + E->get(), PROPERTY_HINT_RESOURCE_TYPE, "StyleBox", hint)); } } { List<StringName> names; - theme->get_font_list(get_class_name(),&names); - for(List<StringName>::Element *E=names.front();E;E=E->next()) { + theme->get_font_list(get_class_name(), &names); + for (List<StringName>::Element *E = names.front(); E; E = E->next()) { - uint32_t hint= PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CHECKABLE; + uint32_t hint = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE; if (data.font_override.has(E->get())) - hint|=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_CHECKED; + hint |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED; - p_list->push_back( PropertyInfo(Variant::OBJECT,"custom_fonts/"+E->get(),PROPERTY_HINT_RESOURCE_TYPE, "Font",hint) ); + p_list->push_back(PropertyInfo(Variant::OBJECT, "custom_fonts/" + E->get(), PROPERTY_HINT_RESOURCE_TYPE, "Font", hint)); } } { List<StringName> names; - theme->get_color_list(get_class_name(),&names); - for(List<StringName>::Element *E=names.front();E;E=E->next()) { + theme->get_color_list(get_class_name(), &names); + for (List<StringName>::Element *E = names.front(); E; E = E->next()) { - uint32_t hint= PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CHECKABLE; + uint32_t hint = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE; if (data.color_override.has(E->get())) - hint|=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_CHECKED; + hint |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED; - p_list->push_back( PropertyInfo(Variant::COLOR,"custom_colors/"+E->get(),PROPERTY_HINT_NONE, "",hint) ); + p_list->push_back(PropertyInfo(Variant::COLOR, "custom_colors/" + E->get(), PROPERTY_HINT_NONE, "", hint)); } } { List<StringName> names; - theme->get_constant_list(get_class_name(),&names); - for(List<StringName>::Element *E=names.front();E;E=E->next()) { + theme->get_constant_list(get_class_name(), &names); + for (List<StringName>::Element *E = names.front(); E; E = E->next()) { - uint32_t hint= PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CHECKABLE; + uint32_t hint = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE; if (data.constant_override.has(E->get())) - hint|=PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_CHECKED; + hint |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED; - p_list->push_back( PropertyInfo(Variant::INT,"custom_constants/"+E->get(),PROPERTY_HINT_RANGE, "-16384,16384",hint) ); + p_list->push_back(PropertyInfo(Variant::INT, "custom_constants/" + E->get(), PROPERTY_HINT_RANGE, "-16384,16384", hint)); } } - - } - Control *Control::get_parent_control() const { return data.parent; } - -void Control::_resize(const Size2& p_size) { +void Control::_resize(const Size2 &p_size) { _size_changed(); } @@ -351,39 +330,36 @@ void Control::_resize(const Size2& p_size) { void Control::add_child_notify(Node *p_child) { - Control *child_c=p_child->cast_to<Control>(); + Control *child_c = p_child->cast_to<Control>(); if (!child_c) return; if (child_c->data.theme.is_null() && data.theme_owner) { - _propagate_theme_changed(child_c,data.theme_owner); //need to propagate here, since many controls may require setting up stuff + _propagate_theme_changed(child_c, data.theme_owner); //need to propagate here, since many controls may require setting up stuff } } void Control::remove_child_notify(Node *p_child) { - Control *child_c=p_child->cast_to<Control>(); + Control *child_c = p_child->cast_to<Control>(); if (!child_c) return; if (child_c->data.theme_owner && child_c->data.theme.is_null()) { - _propagate_theme_changed(child_c,NULL); + _propagate_theme_changed(child_c, NULL); } - } void Control::_update_canvas_item_transform() { - Transform2D xform=Transform2D(data.rotation,get_pos()); + Transform2D xform = Transform2D(data.rotation, get_pos()); xform.scale_basis(data.scale); - VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(),xform); - + VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), xform); } void Control::_notification(int p_notification) { - - switch(p_notification) { + switch (p_notification) { case NOTIFICATION_ENTER_TREE: { @@ -394,43 +370,40 @@ void Control::_notification(int p_notification) { get_viewport()->_gui_remove_control(this); - } break; - case NOTIFICATION_ENTER_CANVAS: { - data.parent=get_parent()->cast_to<Control>(); + data.parent = get_parent()->cast_to<Control>(); if (is_set_as_toplevel()) { - data.SI=get_viewport()->_gui_add_subwindow_control(this); + data.SI = get_viewport()->_gui_add_subwindow_control(this); if (data.theme.is_null() && data.parent && data.parent->data.theme_owner) { - data.theme_owner=data.parent->data.theme_owner; + data.theme_owner = data.parent->data.theme_owner; notification(NOTIFICATION_THEME_CHANGED); } } else { + Node *parent = this; //meh + Control *parent_control = NULL; + bool subwindow = false; - Node *parent=this; //meh - Control *parent_control=NULL; - bool subwindow=false; + while (parent) { - while(parent) { - - parent=parent->get_parent(); + parent = parent->get_parent(); if (!parent) break; - CanvasItem *ci =parent->cast_to<CanvasItem>(); + CanvasItem *ci = parent->cast_to<CanvasItem>(); if (ci && ci->is_set_as_toplevel()) { - subwindow=true; + subwindow = true; break; } - parent_control=parent->cast_to<Control>(); + parent_control = parent->cast_to<Control>(); if (parent_control) { break; @@ -441,33 +414,31 @@ void Control::_notification(int p_notification) { } } - if (parent_control) { //do nothing, has a parent control if (data.theme.is_null() && parent_control->data.theme_owner) { - data.theme_owner=parent_control->data.theme_owner; + data.theme_owner = parent_control->data.theme_owner; notification(NOTIFICATION_THEME_CHANGED); } } else if (subwindow) { //is a subwindow (process input before other controls for that canvas) - data.SI=get_viewport()->_gui_add_subwindow_control(this); + data.SI = get_viewport()->_gui_add_subwindow_control(this); } else { //is a regular root control - data.RI=get_viewport()->_gui_add_root_control(this); + data.RI = get_viewport()->_gui_add_root_control(this); } - data.parent_canvas_item=get_parent_item(); + data.parent_canvas_item = get_parent_item(); if (data.parent_canvas_item) { - data.parent_canvas_item->connect("item_rect_changed",this,"_size_changed"); - } else { + data.parent_canvas_item->connect("item_rect_changed", this, "_size_changed"); + } else { //connect viewport - get_viewport()->connect("size_changed",this,"_size_changed"); + get_viewport()->connect("size_changed", this, "_size_changed"); } } - /* if (data.theme.is_null() && data.parent && data.parent->data.theme_owner) { data.theme_owner=data.parent->data.theme_owner; @@ -480,31 +451,30 @@ void Control::_notification(int p_notification) { if (data.parent_canvas_item) { - data.parent_canvas_item->disconnect("item_rect_changed",this,"_size_changed"); - data.parent_canvas_item=NULL; + data.parent_canvas_item->disconnect("item_rect_changed", this, "_size_changed"); + data.parent_canvas_item = NULL; } else if (!is_set_as_toplevel()) { //disconnect viewport - get_viewport()->disconnect("size_changed",this,"_size_changed"); - + get_viewport()->disconnect("size_changed", this, "_size_changed"); } if (data.MI) { get_viewport()->_gui_remove_modal_control(data.MI); - data.MI=NULL; + data.MI = NULL; } if (data.SI) { get_viewport()->_gui_remove_subwindow_control(data.SI); - data.SI=NULL; + data.SI = NULL; } if (data.RI) { get_viewport()->_gui_remove_root_control(data.RI); - data.RI=NULL; + data.RI = NULL; } - data.parent=NULL; - data.parent_canvas_item=NULL; + data.parent = NULL; + data.parent_canvas_item = NULL; /* if (data.theme_owner && data.theme.is_null()) { data.theme_owner=NULL; @@ -513,21 +483,21 @@ void Control::_notification(int p_notification) { */ } break; - case NOTIFICATION_MOVED_IN_PARENT: { - // some parents need to know the order of the childrens to draw (like TabContainer) - // update if necesary - if (data.parent) - data.parent->update(); - update(); - - if (data.SI) { - get_viewport()->_gui_set_subwindow_order_dirty(); - } - if (data.RI) { - get_viewport()->_gui_set_root_order_dirty(); - } - - } break; + case NOTIFICATION_MOVED_IN_PARENT: { + // some parents need to know the order of the childrens to draw (like TabContainer) + // update if necesary + if (data.parent) + data.parent->update(); + update(); + + if (data.SI) { + get_viewport()->_gui_set_subwindow_order_dirty(); + } + if (data.RI) { + get_viewport()->_gui_set_root_order_dirty(); + } + + } break; case NOTIFICATION_RESIZED: { emit_signal(SceneStringNames::get_singleton()->resized); @@ -535,8 +505,8 @@ void Control::_notification(int p_notification) { case NOTIFICATION_DRAW: { _update_canvas_item_transform(); - VisualServer::get_singleton()->canvas_item_set_custom_rect( get_canvas_item(),!data.disable_visibility_clip, Rect2(Point2(),get_size())); - VisualServer::get_singleton()->canvas_item_set_clip( get_canvas_item(), data.clip_contents ); + VisualServer::get_singleton()->canvas_item_set_custom_rect(get_canvas_item(), !data.disable_visibility_clip, Rect2(Point2(), get_size())); + VisualServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), data.clip_contents); //emit_signal(SceneStringNames::get_singleton()->draw); } break; @@ -571,10 +541,10 @@ void Control::_notification(int p_notification) { if (!is_visible_in_tree()) { - if(get_viewport() != NULL) + if (get_viewport() != NULL) get_viewport()->_gui_hid_control(this); - if(is_inside_tree()) { + if (is_inside_tree()) { _modal_stack_remove(); minimum_size_changed(); } @@ -592,25 +562,21 @@ void Control::_notification(int p_notification) { get_viewport()->_gui_unfocus_control(this); } break; - - - } } - bool Control::clips_input() const { return false; } -bool Control::has_point(const Point2& p_point) const { +bool Control::has_point(const Point2 &p_point) const { if (get_script_instance()) { - Variant v=p_point; - const Variant *p=&v; + Variant v = p_point; + const Variant *p = &v; Variant::CallError ce; - Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->has_point,&p,1,ce); - if (ce.error==Variant::CallError::CALL_OK) { + Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->has_point, &p, 1, ce); + if (ce.error == Variant::CallError::CALL_OK) { return ret; } } @@ -618,101 +584,95 @@ bool Control::has_point(const Point2& p_point) const { Ref<StyleBox> mask = get_stylebox("mask"); return mask->test_mask(p_point,Rect2(Point2(),get_size())); }*/ - return Rect2( Point2(), get_size() ).has_point(p_point); + return Rect2(Point2(), get_size()).has_point(p_point); } -void Control::set_drag_forwarding(Control* p_target) { +void Control::set_drag_forwarding(Control *p_target) { if (p_target) - data.drag_owner=p_target->get_instance_ID(); + data.drag_owner = p_target->get_instance_ID(); else - data.drag_owner=0; + data.drag_owner = 0; } -Variant Control::get_drag_data(const Point2& p_point) { +Variant Control::get_drag_data(const Point2 &p_point) { if (data.drag_owner) { Object *obj = ObjectDB::get_instance(data.drag_owner); if (obj) { Control *c = obj->cast_to<Control>(); - return c->call("get_drag_data_fw",p_point,this); + return c->call("get_drag_data_fw", p_point, this); } } if (get_script_instance()) { - Variant v=p_point; - const Variant *p=&v; + Variant v = p_point; + const Variant *p = &v; Variant::CallError ce; - Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->get_drag_data,&p,1,ce); - if (ce.error==Variant::CallError::CALL_OK) + Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->get_drag_data, &p, 1, ce); + if (ce.error == Variant::CallError::CALL_OK) return ret; } return Variant(); } - -bool Control::can_drop_data(const Point2& p_point,const Variant& p_data) const { +bool Control::can_drop_data(const Point2 &p_point, const Variant &p_data) const { if (data.drag_owner) { Object *obj = ObjectDB::get_instance(data.drag_owner); if (obj) { Control *c = obj->cast_to<Control>(); - return c->call("can_drop_data_fw",p_point,p_data,this); + return c->call("can_drop_data_fw", p_point, p_data, this); } } if (get_script_instance()) { - Variant v=p_point; - const Variant *p[2]={&v,&p_data}; + Variant v = p_point; + const Variant *p[2] = { &v, &p_data }; Variant::CallError ce; - Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->can_drop_data,p,2,ce); - if (ce.error==Variant::CallError::CALL_OK) + Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->can_drop_data, p, 2, ce); + if (ce.error == Variant::CallError::CALL_OK) return ret; } return Variant(); - } -void Control::drop_data(const Point2& p_point,const Variant& p_data){ +void Control::drop_data(const Point2 &p_point, const Variant &p_data) { if (data.drag_owner) { Object *obj = ObjectDB::get_instance(data.drag_owner); if (obj) { Control *c = obj->cast_to<Control>(); - c->call("drop_data_fw",p_point,p_data,this); + c->call("drop_data_fw", p_point, p_data, this); return; } } if (get_script_instance()) { - Variant v=p_point; - const Variant *p[2]={&v,&p_data}; + Variant v = p_point; + const Variant *p[2] = { &v, &p_data }; Variant::CallError ce; - Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->drop_data,p,2,ce); - if (ce.error==Variant::CallError::CALL_OK) + Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->drop_data, p, 2, ce); + if (ce.error == Variant::CallError::CALL_OK) return; } } -void Control::force_drag(const Variant& p_data,Control *p_control) { +void Control::force_drag(const Variant &p_data, Control *p_control) { ERR_FAIL_COND(!is_inside_tree()); - ERR_FAIL_COND(p_data.get_type()==Variant::NIL); - - get_viewport()->_gui_force_drag(this,p_data,p_control); + ERR_FAIL_COND(p_data.get_type() == Variant::NIL); + get_viewport()->_gui_force_drag(this, p_data, p_control); } void Control::set_drag_preview(Control *p_control) { ERR_FAIL_COND(!is_inside_tree()); - get_viewport()->_gui_set_drag_preview(this,p_control); + get_viewport()->_gui_set_drag_preview(this, p_control); } - - - bool Control::is_window_modal_on_top() const { if (!is_inside_tree()) @@ -726,154 +686,147 @@ uint64_t Control::get_modal_frame() const { return data.modal_frame; } - - Size2 Control::get_minimum_size() const { - ScriptInstance *si = const_cast<Control*>(this)->get_script_instance(); + ScriptInstance *si = const_cast<Control *>(this)->get_script_instance(); if (si) { Variant::CallError ce; - Variant s = si->call(SceneStringNames::get_singleton()->get_minimum_size,NULL,0,ce); - if (ce.error==Variant::CallError::CALL_OK) + Variant s = si->call(SceneStringNames::get_singleton()->get_minimum_size, NULL, 0, ce); + if (ce.error == Variant::CallError::CALL_OK) return s; } return Size2(); } +Ref<Texture> Control::get_icon(const StringName &p_name, const StringName &p_type) const { -Ref<Texture> Control::get_icon(const StringName& p_name,const StringName& p_type) const { - - if (p_type==StringName() || p_type=="") { + if (p_type == StringName() || p_type == "") { - const Ref<Texture>* tex = data.icon_override.getptr(p_name); + const Ref<Texture> *tex = data.icon_override.getptr(p_name); if (tex) return *tex; } - StringName type = p_type?p_type:get_class_name(); + StringName type = p_type ? p_type : get_class_name(); // try with custom themes Control *theme_owner = data.theme_owner; - while(theme_owner) { + while (theme_owner) { StringName class_name = type; - while(class_name!=StringName()) { - if (theme_owner->data.theme->has_icon(p_name, class_name ) ) { - return theme_owner->data.theme->get_icon(p_name, class_name ); + while (class_name != StringName()) { + if (theme_owner->data.theme->has_icon(p_name, class_name)) { + return theme_owner->data.theme->get_icon(p_name, class_name); } class_name = ClassDB::get_parent_class_nocheck(class_name); } - Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL; + Control *parent = theme_owner->get_parent() ? theme_owner->get_parent()->cast_to<Control>() : NULL; if (parent) - theme_owner=parent->data.theme_owner; + theme_owner = parent->data.theme_owner; else - theme_owner=NULL; - + theme_owner = NULL; } - return Theme::get_default()->get_icon( p_name, type ); - + return Theme::get_default()->get_icon(p_name, type); } -Ref<Shader> Control::get_shader(const StringName& p_name,const StringName& p_type) const { - if (p_type==StringName() || p_type=="") { +Ref<Shader> Control::get_shader(const StringName &p_name, const StringName &p_type) const { + if (p_type == StringName() || p_type == "") { - const Ref<Shader>* sdr = data.shader_override.getptr(p_name); + const Ref<Shader> *sdr = data.shader_override.getptr(p_name); if (sdr) return *sdr; } - StringName type = p_type?p_type:get_class_name(); + StringName type = p_type ? p_type : get_class_name(); // try with custom themes Control *theme_owner = data.theme_owner; - while(theme_owner) { + while (theme_owner) { StringName class_name = type; - while(class_name!=StringName()) { - if (theme_owner->data.theme->has_shader(p_name, class_name ) ) { - return theme_owner->data.theme->get_shader(p_name, class_name ); + while (class_name != StringName()) { + if (theme_owner->data.theme->has_shader(p_name, class_name)) { + return theme_owner->data.theme->get_shader(p_name, class_name); } class_name = ClassDB::get_parent_class_nocheck(class_name); } - Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL; + Control *parent = theme_owner->get_parent() ? theme_owner->get_parent()->cast_to<Control>() : NULL; if (parent) - theme_owner=parent->data.theme_owner; + theme_owner = parent->data.theme_owner; else - theme_owner=NULL; - + theme_owner = NULL; } - return Theme::get_default()->get_shader( p_name, type ); + return Theme::get_default()->get_shader(p_name, type); } -Ref<StyleBox> Control::get_stylebox(const StringName& p_name,const StringName& p_type) const { +Ref<StyleBox> Control::get_stylebox(const StringName &p_name, const StringName &p_type) const { - if (p_type==StringName() || p_type=="") { - const Ref<StyleBox>* style = data.style_override.getptr(p_name); + if (p_type == StringName() || p_type == "") { + const Ref<StyleBox> *style = data.style_override.getptr(p_name); if (style) return *style; } - StringName type = p_type?p_type:get_class_name(); + StringName type = p_type ? p_type : get_class_name(); // try with custom themes Control *theme_owner = data.theme_owner; - while(theme_owner) { + while (theme_owner) { StringName class_name = type; - while(class_name!=StringName()) { - if (theme_owner->data.theme->has_stylebox(p_name, class_name ) ) { - return theme_owner->data.theme->get_stylebox(p_name, class_name ); + while (class_name != StringName()) { + if (theme_owner->data.theme->has_stylebox(p_name, class_name)) { + return theme_owner->data.theme->get_stylebox(p_name, class_name); } class_name = ClassDB::get_parent_class_nocheck(class_name); } - Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL; + Control *parent = theme_owner->get_parent() ? theme_owner->get_parent()->cast_to<Control>() : NULL; if (parent) - theme_owner=parent->data.theme_owner; + theme_owner = parent->data.theme_owner; else - theme_owner=NULL; + theme_owner = NULL; } - return Theme::get_default()->get_stylebox( p_name, type ); - + return Theme::get_default()->get_stylebox(p_name, type); } -Ref<Font> Control::get_font(const StringName& p_name,const StringName& p_type) const { +Ref<Font> Control::get_font(const StringName &p_name, const StringName &p_type) const { - if (p_type==StringName() || p_type=="") { - const Ref<Font>* font = data.font_override.getptr(p_name); + if (p_type == StringName() || p_type == "") { + const Ref<Font> *font = data.font_override.getptr(p_name); if (font) return *font; } - StringName type = p_type?p_type:get_class_name(); + StringName type = p_type ? p_type : get_class_name(); // try with custom themes Control *theme_owner = data.theme_owner; - while(theme_owner) { + while (theme_owner) { StringName class_name = type; - while(class_name!=StringName()) { - if (theme_owner->data.theme->has_font(p_name, class_name ) ) { - return theme_owner->data.theme->get_font(p_name, class_name ); + while (class_name != StringName()) { + if (theme_owner->data.theme->has_font(p_name, class_name)) { + return theme_owner->data.theme->get_font(p_name, class_name); } class_name = ClassDB::get_parent_class_nocheck(class_name); @@ -881,96 +834,89 @@ Ref<Font> Control::get_font(const StringName& p_name,const StringName& p_type) c if (theme_owner->data.theme->get_default_theme_font().is_valid()) return theme_owner->data.theme->get_default_theme_font(); - Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL; + Control *parent = theme_owner->get_parent() ? theme_owner->get_parent()->cast_to<Control>() : NULL; if (parent) - theme_owner=parent->data.theme_owner; + theme_owner = parent->data.theme_owner; else - theme_owner=NULL; - + theme_owner = NULL; } - return Theme::get_default()->get_font( p_name, type ); - + return Theme::get_default()->get_font(p_name, type); } -Color Control::get_color(const StringName& p_name,const StringName& p_type) const { +Color Control::get_color(const StringName &p_name, const StringName &p_type) const { - if (p_type==StringName() || p_type=="") { - const Color* color = data.color_override.getptr(p_name); + if (p_type == StringName() || p_type == "") { + const Color *color = data.color_override.getptr(p_name); if (color) return *color; } - StringName type = p_type?p_type:get_class_name(); + StringName type = p_type ? p_type : get_class_name(); // try with custom themes Control *theme_owner = data.theme_owner; - while(theme_owner) { + while (theme_owner) { StringName class_name = type; - while(class_name!=StringName()) { - if (theme_owner->data.theme->has_color(p_name, class_name ) ) { - return theme_owner->data.theme->get_color(p_name, class_name ); + while (class_name != StringName()) { + if (theme_owner->data.theme->has_color(p_name, class_name)) { + return theme_owner->data.theme->get_color(p_name, class_name); } class_name = ClassDB::get_parent_class_nocheck(class_name); } - Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL; + Control *parent = theme_owner->get_parent() ? theme_owner->get_parent()->cast_to<Control>() : NULL; if (parent) - theme_owner=parent->data.theme_owner; + theme_owner = parent->data.theme_owner; else - theme_owner=NULL; - + theme_owner = NULL; } - return Theme::get_default()->get_color( p_name, type ); - + return Theme::get_default()->get_color(p_name, type); } -int Control::get_constant(const StringName& p_name,const StringName& p_type) const { +int Control::get_constant(const StringName &p_name, const StringName &p_type) const { - if (p_type==StringName() || p_type=="") { - const int* constant = data.constant_override.getptr(p_name); + if (p_type == StringName() || p_type == "") { + const int *constant = data.constant_override.getptr(p_name); if (constant) return *constant; } - StringName type = p_type?p_type:get_class_name(); - // try with custom themes + StringName type = p_type ? p_type : get_class_name(); + // try with custom themes Control *theme_owner = data.theme_owner; - while(theme_owner) { + while (theme_owner) { StringName class_name = type; - while(class_name!=StringName()) { - if (theme_owner->data.theme->has_constant(p_name, class_name ) ) { - return theme_owner->data.theme->get_constant(p_name, class_name ); + while (class_name != StringName()) { + if (theme_owner->data.theme->has_constant(p_name, class_name)) { + return theme_owner->data.theme->get_constant(p_name, class_name); } class_name = ClassDB::get_parent_class_nocheck(class_name); } - Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL; + Control *parent = theme_owner->get_parent() ? theme_owner->get_parent()->cast_to<Control>() : NULL; if (parent) - theme_owner=parent->data.theme_owner; + theme_owner = parent->data.theme_owner; else - theme_owner=NULL; - + theme_owner = NULL; } - return Theme::get_default()->get_constant( p_name, type ); - - + return Theme::get_default()->get_constant(p_name, type); } -bool Control::has_icon_override(const StringName& p_name) const { +bool Control::has_icon_override(const StringName &p_name) const { - const Ref<Texture>* tex = data.icon_override.getptr(p_name); + const Ref<Texture> *tex = data.icon_override.getptr(p_name); if (tex) return true; else @@ -979,279 +925,265 @@ bool Control::has_icon_override(const StringName& p_name) const { bool Control::has_shader_override(const StringName &p_name) const { - const Ref<Shader>* sdr = data.shader_override.getptr(p_name); + const Ref<Shader> *sdr = data.shader_override.getptr(p_name); if (sdr) return true; else return false; } -bool Control::has_stylebox_override(const StringName& p_name) const { +bool Control::has_stylebox_override(const StringName &p_name) const { - const Ref<StyleBox>* style = data.style_override.getptr(p_name); + const Ref<StyleBox> *style = data.style_override.getptr(p_name); if (style) return true; else return false; } -bool Control::has_font_override(const StringName& p_name) const { +bool Control::has_font_override(const StringName &p_name) const { - const Ref<Font>* font = data.font_override.getptr(p_name); + const Ref<Font> *font = data.font_override.getptr(p_name); if (font) return true; else return false; } -bool Control::has_color_override(const StringName& p_name) const { +bool Control::has_color_override(const StringName &p_name) const { - const Color* color = data.color_override.getptr(p_name); + const Color *color = data.color_override.getptr(p_name); if (color) return true; else return false; } -bool Control::has_constant_override(const StringName& p_name) const { +bool Control::has_constant_override(const StringName &p_name) const { - const int* constant = data.constant_override.getptr(p_name); + const int *constant = data.constant_override.getptr(p_name); if (constant) return true; else return false; } -bool Control::has_icon(const StringName& p_name,const StringName& p_type) const { +bool Control::has_icon(const StringName &p_name, const StringName &p_type) const { - if (p_type==StringName() || p_type=="") { + if (p_type == StringName() || p_type == "") { if (has_icon_override(p_name) == true) return true; } - StringName type = p_type?p_type:get_class_name(); + StringName type = p_type ? p_type : get_class_name(); // try with custom themes Control *theme_owner = data.theme_owner; - while(theme_owner) { + while (theme_owner) { StringName class_name = type; - while(class_name!=StringName()) { - if (theme_owner->data.theme->has_icon(p_name, class_name ) ) { + while (class_name != StringName()) { + if (theme_owner->data.theme->has_icon(p_name, class_name)) { return true; } class_name = ClassDB::get_parent_class_nocheck(class_name); } - Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL; + Control *parent = theme_owner->get_parent() ? theme_owner->get_parent()->cast_to<Control>() : NULL; if (parent) - theme_owner=parent->data.theme_owner; + theme_owner = parent->data.theme_owner; else - theme_owner=NULL; - + theme_owner = NULL; } - return Theme::get_default()->has_icon( p_name, type ); - + return Theme::get_default()->has_icon(p_name, type); } bool Control::has_shader(const StringName &p_name, const StringName &p_type) const { - if (p_type==StringName() || p_type=="") { - if (has_shader_override(p_name)==true) + if (p_type == StringName() || p_type == "") { + if (has_shader_override(p_name) == true) return true; } - StringName type = p_type?p_type:get_class_name(); + StringName type = p_type ? p_type : get_class_name(); // try with custom themes Control *theme_owner = data.theme_owner; - while(theme_owner) { + while (theme_owner) { StringName class_name = type; - while(class_name!=StringName()) { - if (theme_owner->data.theme->has_shader(p_name, class_name ) ) { + while (class_name != StringName()) { + if (theme_owner->data.theme->has_shader(p_name, class_name)) { return true; } class_name = ClassDB::get_parent_class_nocheck(class_name); } - Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL; + Control *parent = theme_owner->get_parent() ? theme_owner->get_parent()->cast_to<Control>() : NULL; if (parent) - theme_owner=parent->data.theme_owner; + theme_owner = parent->data.theme_owner; else - theme_owner=NULL; - + theme_owner = NULL; } - return Theme::get_default()->has_shader( p_name, type ); - + return Theme::get_default()->has_shader(p_name, type); } -bool Control::has_stylebox(const StringName& p_name,const StringName& p_type) const { +bool Control::has_stylebox(const StringName &p_name, const StringName &p_type) const { - if (p_type==StringName() || p_type=="") { - if (has_stylebox_override(p_name)==true) + if (p_type == StringName() || p_type == "") { + if (has_stylebox_override(p_name) == true) return true; } - StringName type = p_type?p_type:get_class_name(); + StringName type = p_type ? p_type : get_class_name(); // try with custom themes Control *theme_owner = data.theme_owner; - while(theme_owner) { + while (theme_owner) { StringName class_name = type; - while(class_name!=StringName()) { - if (theme_owner->data.theme->has_stylebox(p_name, class_name ) ) { + while (class_name != StringName()) { + if (theme_owner->data.theme->has_stylebox(p_name, class_name)) { return true; } class_name = ClassDB::get_parent_class_nocheck(class_name); } - Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL; + Control *parent = theme_owner->get_parent() ? theme_owner->get_parent()->cast_to<Control>() : NULL; if (parent) - theme_owner=parent->data.theme_owner; + theme_owner = parent->data.theme_owner; else - theme_owner=NULL; - + theme_owner = NULL; } - return Theme::get_default()->has_stylebox( p_name, type ); - + return Theme::get_default()->has_stylebox(p_name, type); } -bool Control::has_font(const StringName& p_name,const StringName& p_type) const { +bool Control::has_font(const StringName &p_name, const StringName &p_type) const { - if (p_type==StringName() || p_type=="") { - if (has_font_override(p_name)==true) + if (p_type == StringName() || p_type == "") { + if (has_font_override(p_name) == true) return true; } - - StringName type = p_type?p_type:get_class_name(); + StringName type = p_type ? p_type : get_class_name(); // try with custom themes Control *theme_owner = data.theme_owner; - while(theme_owner) { + while (theme_owner) { StringName class_name = type; - while(class_name!=StringName()) { - if (theme_owner->data.theme->has_font(p_name, class_name ) ) { + while (class_name != StringName()) { + if (theme_owner->data.theme->has_font(p_name, class_name)) { return true; } class_name = ClassDB::get_parent_class_nocheck(class_name); } - Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL; + Control *parent = theme_owner->get_parent() ? theme_owner->get_parent()->cast_to<Control>() : NULL; if (parent) - theme_owner=parent->data.theme_owner; + theme_owner = parent->data.theme_owner; else - theme_owner=NULL; - + theme_owner = NULL; } - return Theme::get_default()->has_font( p_name, type ); - + return Theme::get_default()->has_font(p_name, type); } -bool Control::has_color(const StringName& p_name, const StringName& p_type) const { +bool Control::has_color(const StringName &p_name, const StringName &p_type) const { - if (p_type==StringName() || p_type=="") { - if (has_color_override(p_name)==true) + if (p_type == StringName() || p_type == "") { + if (has_color_override(p_name) == true) return true; } - StringName type = p_type?p_type:get_class_name(); + StringName type = p_type ? p_type : get_class_name(); // try with custom themes Control *theme_owner = data.theme_owner; - while(theme_owner) { + while (theme_owner) { StringName class_name = type; - while(class_name!=StringName()) { - if (theme_owner->data.theme->has_color(p_name, class_name ) ) { + while (class_name != StringName()) { + if (theme_owner->data.theme->has_color(p_name, class_name)) { return true; } class_name = ClassDB::get_parent_class_nocheck(class_name); } - Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL; + Control *parent = theme_owner->get_parent() ? theme_owner->get_parent()->cast_to<Control>() : NULL; if (parent) - theme_owner=parent->data.theme_owner; + theme_owner = parent->data.theme_owner; else - theme_owner=NULL; - + theme_owner = NULL; } - return Theme::get_default()->has_color( p_name, type ); - + return Theme::get_default()->has_color(p_name, type); } -bool Control::has_constant(const StringName& p_name,const StringName& p_type) const { +bool Control::has_constant(const StringName &p_name, const StringName &p_type) const { - if (p_type==StringName() || p_type=="") { + if (p_type == StringName() || p_type == "") { if (has_constant_override(p_name) == true) return true; } - - StringName type = p_type?p_type:get_class_name(); + StringName type = p_type ? p_type : get_class_name(); // try with custom themes Control *theme_owner = data.theme_owner; - while(theme_owner) { + while (theme_owner) { StringName class_name = type; - while(class_name!=StringName()) { - if (theme_owner->data.theme->has_constant(p_name, class_name ) ) { + while (class_name != StringName()) { + if (theme_owner->data.theme->has_constant(p_name, class_name)) { return true; } class_name = ClassDB::get_parent_class_nocheck(class_name); } - Control *parent = theme_owner->get_parent()?theme_owner->get_parent()->cast_to<Control>():NULL; + Control *parent = theme_owner->get_parent() ? theme_owner->get_parent()->cast_to<Control>() : NULL; if (parent) - theme_owner=parent->data.theme_owner; + theme_owner = parent->data.theme_owner; else - theme_owner=NULL; - + theme_owner = NULL; } - return Theme::get_default()->has_constant( p_name, type ); + return Theme::get_default()->has_constant(p_name, type); } Size2 Control::get_parent_area_size() const { - ERR_FAIL_COND_V(!is_inside_tree(),Size2()); + ERR_FAIL_COND_V(!is_inside_tree(), Size2()); Size2 parent_size; if (data.parent_canvas_item) { - parent_size=data.parent_canvas_item->get_item_rect().size; + parent_size = data.parent_canvas_item->get_item_rect().size; } else { - parent_size=get_viewport()->get_visible_rect().size; + parent_size = get_viewport()->get_visible_rect().size; } return parent_size; - } void Control::_size_changed() { @@ -1263,38 +1195,38 @@ void Control::_size_changed() { float margin_pos[4]; - for(int i=0;i<4;i++) { + for (int i = 0; i < 4; i++) { - float area = parent_size[i&1]; - switch(data.anchor[i]) { + float area = parent_size[i & 1]; + switch (data.anchor[i]) { case ANCHOR_BEGIN: { - margin_pos[i]=data.margin[i]; + margin_pos[i] = data.margin[i]; } break; case ANCHOR_END: { - margin_pos[i]=area-data.margin[i]; + margin_pos[i] = area - data.margin[i]; } break; case ANCHOR_CENTER: { - margin_pos[i]=(area/2)-data.margin[i]; + margin_pos[i] = (area / 2) - data.margin[i]; } break; } } - Point2 new_pos_cache=Point2(margin_pos[0],margin_pos[1]).floor(); - Size2 new_size_cache=Point2(margin_pos[2],margin_pos[3]).floor()-new_pos_cache; - Size2 minimum_size=get_combined_minimum_size(); + Point2 new_pos_cache = Point2(margin_pos[0], margin_pos[1]).floor(); + Size2 new_size_cache = Point2(margin_pos[2], margin_pos[3]).floor() - new_pos_cache; + Size2 minimum_size = get_combined_minimum_size(); - new_size_cache.x = MAX( minimum_size.x, new_size_cache.x ); - new_size_cache.y = MAX( minimum_size.y, new_size_cache.y ); + new_size_cache.x = MAX(minimum_size.x, new_size_cache.x); + new_size_cache.y = MAX(minimum_size.y, new_size_cache.y); bool pos_changed = new_pos_cache != data.pos_cache; bool size_changed = new_size_cache != data.size_cache; - data.pos_cache=new_pos_cache; - data.size_cache=new_size_cache; + data.pos_cache = new_pos_cache; + data.size_cache = new_size_cache; if (size_changed) { notification(NOTIFICATION_RESIZED); @@ -1315,76 +1247,73 @@ float Control::_get_parent_range(int p_idx) const { if (!is_inside_tree()) { return 1.0; + } + if (data.parent_canvas_item) { - } if (data.parent_canvas_item) { - - return data.parent_canvas_item->get_item_rect().size[p_idx&1]; + return data.parent_canvas_item->get_item_rect().size[p_idx & 1]; } else { - return get_viewport()->get_visible_rect().size[p_idx&1]; + return get_viewport()->get_visible_rect().size[p_idx & 1]; } return 1.0; } - float Control::_get_range(int p_idx) const { - p_idx&=1; + p_idx &= 1; - float parent_range = _get_parent_range( p_idx ); - float from = _a2s( data.margin[p_idx], data.anchor[p_idx], parent_range ); - float to = _a2s( data.margin[p_idx+2], data.anchor[p_idx+2], parent_range ); + float parent_range = _get_parent_range(p_idx); + float from = _a2s(data.margin[p_idx], data.anchor[p_idx], parent_range); + float to = _a2s(data.margin[p_idx + 2], data.anchor[p_idx + 2], parent_range); - return to-from; + return to - from; } -float Control::_s2a(float p_val, AnchorType p_anchor,float p_range) const { +float Control::_s2a(float p_val, AnchorType p_anchor, float p_range) const { - switch(p_anchor) { + switch (p_anchor) { case ANCHOR_BEGIN: { return p_val; } break; case ANCHOR_END: { - return p_range-p_val; + return p_range - p_val; } break; case ANCHOR_CENTER: { - return (p_range/2)-p_val; + return (p_range / 2) - p_val; } break; } return 0; } +float Control::_a2s(float p_val, AnchorType p_anchor, float p_range) const { -float Control::_a2s(float p_val, AnchorType p_anchor,float p_range) const { - - switch(p_anchor) { + switch (p_anchor) { case ANCHOR_BEGIN: { return Math::floor(p_val); } break; case ANCHOR_END: { - return Math::floor(p_range-p_val); + return Math::floor(p_range - p_val); } break; case ANCHOR_CENTER: { - return Math::floor((p_range/2)-p_val); + return Math::floor((p_range / 2) - p_val); } break; } return 0; } - -void Control::set_anchor(Margin p_margin,AnchorType p_anchor, bool p_keep_margin) { +void Control::set_anchor(Margin p_margin, AnchorType p_anchor, bool p_keep_margin) { if (!is_inside_tree()) { data.anchor[p_margin] = p_anchor; - } else if(!p_keep_margin) { + } else if (!p_keep_margin) { float pr = _get_parent_range(p_margin); - float s = _a2s( data.margin[p_margin], data.anchor[p_margin], pr ); + float s = _a2s(data.margin[p_margin], data.anchor[p_margin], pr); data.anchor[p_margin] = p_anchor; - data.margin[p_margin] = _s2a( s, p_anchor, pr ); + data.margin[p_margin] = _s2a(s, p_anchor, pr); } else { data.anchor[p_margin] = p_anchor; _size_changed(); @@ -1392,34 +1321,29 @@ void Control::set_anchor(Margin p_margin,AnchorType p_anchor, bool p_keep_margin _change_notify(); } -void Control::_set_anchor(Margin p_margin,AnchorType p_anchor) { - #ifdef TOOLS_ENABLED +void Control::_set_anchor(Margin p_margin, AnchorType p_anchor) { +#ifdef TOOLS_ENABLED if (is_inside_tree() && get_tree()->is_editor_hint()) { set_anchor(p_margin, p_anchor, EDITOR_DEF("editors/2d/keep_margins_when_changing_anchors", false)); } else { set_anchor(p_margin, p_anchor, false); } - #else +#else set_anchor(p_margin, p_anchor, false); - #endif +#endif } -void Control::set_anchor_and_margin(Margin p_margin,AnchorType p_anchor, float p_pos) { +void Control::set_anchor_and_margin(Margin p_margin, AnchorType p_anchor, float p_pos) { - set_anchor(p_margin,p_anchor); - set_margin(p_margin,p_pos); + set_anchor(p_margin, p_anchor); + set_margin(p_margin, p_pos); } - Control::AnchorType Control::get_anchor(Margin p_margin) const { return data.anchor[p_margin]; } - - - - void Control::_change_notify_margins() { // this avoids sending the whole object data again on a change @@ -1429,28 +1353,25 @@ void Control::_change_notify_margins() { _change_notify("margin/bottom"); _change_notify("rect/pos"); _change_notify("rect/size"); - } +void Control::set_margin(Margin p_margin, float p_value) { -void Control::set_margin(Margin p_margin,float p_value) { - - data.margin[p_margin]=p_value; + data.margin[p_margin] = p_value; _size_changed(); - } -void Control::set_begin(const Size2& p_point) { +void Control::set_begin(const Size2 &p_point) { - data.margin[0]=p_point.x; - data.margin[1]=p_point.y; + data.margin[0] = p_point.x; + data.margin[1] = p_point.y; _size_changed(); } -void Control::set_end(const Size2& p_point) { +void Control::set_end(const Size2 &p_point) { - data.margin[2]=p_point.x; - data.margin[3]=p_point.y; + data.margin[2] = p_point.x; + data.margin[3] = p_point.y; _size_changed(); } @@ -1461,11 +1382,11 @@ float Control::get_margin(Margin p_margin) const { Size2 Control::get_begin() const { - return Size2( data.margin[0], data.margin[1] ); + return Size2(data.margin[0], data.margin[1]); } Size2 Control::get_end() const { - return Size2( data.margin[2], data.margin[3] ); + return Size2(data.margin[2], data.margin[3]); } Point2 Control::get_global_pos() const { @@ -1473,7 +1394,7 @@ Point2 Control::get_global_pos() const { return get_global_transform().get_origin(); } -void Control::set_global_pos(const Point2& p_point) { +void Control::set_global_pos(const Point2 &p_point) { Transform2D inv; @@ -1485,60 +1406,58 @@ void Control::set_global_pos(const Point2& p_point) { set_pos(inv.xform(p_point)); } -void Control::set_pos(const Size2& p_point) { +void Control::set_pos(const Size2 &p_point) { float pw = _get_parent_range(0); float ph = _get_parent_range(1); - float x = _a2s( data.margin[0], data.anchor[0], pw ); - float y = _a2s( data.margin[1], data.anchor[1], ph ); - float x2 = _a2s( data.margin[2], data.anchor[2], pw ); - float y2 = _a2s( data.margin[3], data.anchor[3], ph ); + float x = _a2s(data.margin[0], data.anchor[0], pw); + float y = _a2s(data.margin[1], data.anchor[1], ph); + float x2 = _a2s(data.margin[2], data.anchor[2], pw); + float y2 = _a2s(data.margin[3], data.anchor[3], ph); - Size2 ret = Size2(x2-x,y2-y); + Size2 ret = Size2(x2 - x, y2 - y); Size2 min = get_combined_minimum_size(); - Size2 size = Size2(MAX( min.width, ret.width),MAX( min.height, ret.height)); - float w=size.x; - float h=size.y; + Size2 size = Size2(MAX(min.width, ret.width), MAX(min.height, ret.height)); + float w = size.x; + float h = size.y; - x=p_point.x; - y=p_point.y; + x = p_point.x; + y = p_point.y; - data.margin[0] = _s2a( x, data.anchor[0], pw ); - data.margin[1] = _s2a( y, data.anchor[1], ph ); - data.margin[2] = _s2a( x+w, data.anchor[2], pw ); - data.margin[3] = _s2a( y+h, data.anchor[3], ph ); + data.margin[0] = _s2a(x, data.anchor[0], pw); + data.margin[1] = _s2a(y, data.anchor[1], ph); + data.margin[2] = _s2a(x + w, data.anchor[2], pw); + data.margin[3] = _s2a(y + h, data.anchor[3], ph); _size_changed(); } -void Control::set_size(const Size2& p_size) { +void Control::set_size(const Size2 &p_size) { - Size2 new_size=p_size; - Size2 min=get_combined_minimum_size(); - if (new_size.x<min.x) - new_size.x=min.x; - if (new_size.y<min.y) - new_size.y=min.y; + Size2 new_size = p_size; + Size2 min = get_combined_minimum_size(); + if (new_size.x < min.x) + new_size.x = min.x; + if (new_size.y < min.y) + new_size.y = min.y; float pw = _get_parent_range(0); float ph = _get_parent_range(1); - float x = _a2s( data.margin[0], data.anchor[0], pw ); - float y = _a2s( data.margin[1], data.anchor[1], ph ); + float x = _a2s(data.margin[0], data.anchor[0], pw); + float y = _a2s(data.margin[1], data.anchor[1], ph); - float w=new_size.width; - float h=new_size.height; + float w = new_size.width; + float h = new_size.height; - data.margin[2] = _s2a( x+w, data.anchor[2], pw ); - data.margin[3] = _s2a( y+h, data.anchor[3], ph ); + data.margin[2] = _s2a(x + w, data.anchor[2], pw); + data.margin[3] = _s2a(y + h, data.anchor[3], ph); _size_changed(); - } - Size2 Control::get_pos() const { return data.pos_cache; @@ -1551,71 +1470,67 @@ Size2 Control::get_size() const { Rect2 Control::get_global_rect() const { - return Rect2( get_global_pos(), get_size() ); + return Rect2(get_global_pos(), get_size()); } Rect2 Control::get_window_rect() const { - ERR_FAIL_COND_V(!is_inside_tree(),Rect2()); + ERR_FAIL_COND_V(!is_inside_tree(), Rect2()); Rect2 gr = get_global_rect(); - gr.pos+=get_viewport()->get_visible_rect().pos; + gr.pos += get_viewport()->get_visible_rect().pos; return gr; } - Rect2 Control::get_rect() const { - return Rect2(get_pos(),get_size()); + return Rect2(get_pos(), get_size()); } Rect2 Control::get_item_rect() const { - return Rect2(Point2(),get_size()); + return Rect2(Point2(), get_size()); } void Control::set_area_as_parent_rect(int p_margin) { - data.anchor[MARGIN_LEFT]=ANCHOR_BEGIN; - data.anchor[MARGIN_TOP]=ANCHOR_BEGIN; - data.anchor[MARGIN_RIGHT]=ANCHOR_END; - data.anchor[MARGIN_BOTTOM]=ANCHOR_END; - for(int i=0;i<4;i++) - data.margin[i]=p_margin; + data.anchor[MARGIN_LEFT] = ANCHOR_BEGIN; + data.anchor[MARGIN_TOP] = ANCHOR_BEGIN; + data.anchor[MARGIN_RIGHT] = ANCHOR_END; + data.anchor[MARGIN_BOTTOM] = ANCHOR_END; + for (int i = 0; i < 4; i++) + data.margin[i] = p_margin; _size_changed(); - } -void Control::add_icon_override(const StringName& p_name, const Ref<Texture>& p_icon) { +void Control::add_icon_override(const StringName &p_name, const Ref<Texture> &p_icon) { ERR_FAIL_COND(p_icon.is_null()); - data.icon_override[p_name]=p_icon; + data.icon_override[p_name] = p_icon; notification(NOTIFICATION_THEME_CHANGED); update(); - } void Control::add_shader_override(const StringName &p_name, const Ref<Shader> &p_shader) { ERR_FAIL_COND(p_shader.is_null()); - data.shader_override[p_name]=p_shader; + data.shader_override[p_name] = p_shader; notification(NOTIFICATION_THEME_CHANGED); update(); } -void Control::add_style_override(const StringName& p_name, const Ref<StyleBox>& p_style) { +void Control::add_style_override(const StringName &p_name, const Ref<StyleBox> &p_style) { ERR_FAIL_COND(p_style.is_null()); - data.style_override[p_name]=p_style; + data.style_override[p_name] = p_style; notification(NOTIFICATION_THEME_CHANGED); update(); } - -void Control::add_font_override(const StringName& p_name, const Ref<Font>& p_font) { +void Control::add_font_override(const StringName &p_name, const Ref<Font> &p_font) { ERR_FAIL_COND(p_font.is_null()); if (data.font_override.has(p_name)) { _unref_font(data.font_override[p_name]); } - data.font_override[p_name]=p_font; + data.font_override[p_name] = p_font; if (p_font.is_valid()) { _ref_font(p_font); @@ -1624,26 +1539,25 @@ void Control::add_font_override(const StringName& p_name, const Ref<Font>& p_fon notification(NOTIFICATION_THEME_CHANGED); update(); } -void Control::add_color_override(const StringName& p_name, const Color& p_color) { +void Control::add_color_override(const StringName &p_name, const Color &p_color) { - data.color_override[p_name]=p_color; + data.color_override[p_name] = p_color; notification(NOTIFICATION_THEME_CHANGED); update(); } -void Control::add_constant_override(const StringName& p_name, int p_constant) { +void Control::add_constant_override(const StringName &p_name, int p_constant) { - data.constant_override[p_name]=p_constant; + data.constant_override[p_name] = p_constant; notification(NOTIFICATION_THEME_CHANGED); update(); } void Control::set_focus_mode(FocusMode p_focus_mode) { - if (is_inside_tree() && p_focus_mode == FOCUS_NONE && data.focus_mode!=FOCUS_NONE && has_focus()) + if (is_inside_tree() && p_focus_mode == FOCUS_NONE && data.focus_mode != FOCUS_NONE && has_focus()) release_focus(); - data.focus_mode=p_focus_mode; - + data.focus_mode = p_focus_mode; } static Control *_next_control(Control *p_from) { @@ -1651,17 +1565,16 @@ static Control *_next_control(Control *p_from) { if (p_from->is_set_as_toplevel()) return NULL; // can't go above - Control *parent = p_from->get_parent()?p_from->get_parent()->cast_to<Control>():NULL; + Control *parent = p_from->get_parent() ? p_from->get_parent()->cast_to<Control>() : NULL; if (!parent) { return NULL; } - int next = p_from->get_position_in_parent(); - ERR_FAIL_INDEX_V(next,parent->get_child_count(),NULL); - for(int i=(next+1);i<parent->get_child_count();i++) { + ERR_FAIL_INDEX_V(next, parent->get_child_count(), NULL); + for (int i = (next + 1); i < parent->get_child_count(); i++) { Control *c = parent->get_child(i)->cast_to<Control>(); if (!c || !c->is_visible_in_tree() || c->is_set_as_toplevel()) @@ -1676,24 +1589,22 @@ static Control *_next_control(Control *p_from) { Control *Control::find_next_valid_focus() const { - Control *from = const_cast<Control*>(this); - - while(true) { + Control *from = const_cast<Control *>(this); + while (true) { // find next child - Control *next_child=NULL; + Control *next_child = NULL; - - for(int i=0;i<from->get_child_count();i++) { + for (int i = 0; i < from->get_child_count(); i++) { Control *c = from->get_child(i)->cast_to<Control>(); if (!c || !c->is_visible_in_tree() || c->is_set_as_toplevel()) { continue; } - next_child=c; + next_child = c; break; } @@ -1702,63 +1613,52 @@ Control *Control::find_next_valid_focus() const { from = next_child; } else { - next_child=_next_control(from); + next_child = _next_control(from); if (!next_child) { //nothing else.. go up and find either window or subwindow - next_child=const_cast<Control*>(this); - while(next_child && !next_child->is_set_as_toplevel()) { + next_child = const_cast<Control *>(this); + while (next_child && !next_child->is_set_as_toplevel()) { if (next_child->get_parent()) { - next_child=next_child->get_parent()->cast_to<Control>(); + next_child = next_child->get_parent()->cast_to<Control>(); } else - next_child=NULL; - + next_child = NULL; } if (!next_child) { - next_child=const_cast<Control*>(this); - while(next_child) { + next_child = const_cast<Control *>(this); + while (next_child) { if (next_child->data.SI || next_child->data.RI) break; - next_child=next_child->get_parent_control(); - + next_child = next_child->get_parent_control(); } - - } } - } - - if (next_child==this) // no next control-> - return (get_focus_mode()==FOCUS_ALL)?next_child:NULL; + if (next_child == this) // no next control-> + return (get_focus_mode() == FOCUS_ALL) ? next_child : NULL; if (next_child) { - if (next_child->get_focus_mode()==FOCUS_ALL) + if (next_child->get_focus_mode() == FOCUS_ALL) return next_child; from = next_child; - } else break; + } else + break; } return NULL; - - } - - - static Control *_prev_control(Control *p_from) { - - Control *child=NULL; - for(int i=p_from->get_child_count()-1;i>=0;i--) { + Control *child = NULL; + for (int i = p_from->get_child_count() - 1; i >= 0; i--) { Control *c = p_from->get_child(i)->cast_to<Control>(); if (!c || !c->is_visible_in_tree() || c->is_set_as_toplevel()) continue; - child=c; + child = c; break; } @@ -1770,72 +1670,57 @@ static Control *_prev_control(Control *p_from) { } Control *Control::find_prev_valid_focus() const { - Control *from = const_cast<Control*>(this); - - while(true) { + Control *from = const_cast<Control *>(this); + while (true) { // find prev child + Control *prev_child = NULL; - Control *prev_child = NULL; - + if (from->is_set_as_toplevel() || !from->get_parent() || !from->get_parent()->cast_to<Control>()) { + //find last of the childs - if ( from->is_set_as_toplevel() || !from->get_parent() || !from->get_parent()->cast_to<Control>()) { + prev_child = _prev_control(from); - //find last of the childs - - prev_child=_prev_control(from); - - } else { - - for(int i=(from->get_position_in_parent()-1);i>=0;i--) { - - - Control *c = from->get_parent()->get_child(i)->cast_to<Control>(); + } else { - if (!c || !c->is_visible_in_tree() || c->is_set_as_toplevel()) { - continue; - } + for (int i = (from->get_position_in_parent() - 1); i >= 0; i--) { - prev_child=c; - break; + Control *c = from->get_parent()->get_child(i)->cast_to<Control>(); + if (!c || !c->is_visible_in_tree() || c->is_set_as_toplevel()) { + continue; } - if (!prev_child) { - - - - prev_child = from->get_parent()->cast_to<Control>(); - } else { - - - - prev_child = _prev_control(prev_child); - } + prev_child = c; + break; } + if (!prev_child) { + prev_child = from->get_parent()->cast_to<Control>(); + } else { + prev_child = _prev_control(prev_child); + } + } - if (prev_child==this) // no prev control-> - return (get_focus_mode()==FOCUS_ALL)?prev_child:NULL; - - if (prev_child->get_focus_mode()==FOCUS_ALL) - return prev_child; + if (prev_child == this) // no prev control-> + return (get_focus_mode() == FOCUS_ALL) ? prev_child : NULL; - from = prev_child; + if (prev_child->get_focus_mode() == FOCUS_ALL) + return prev_child; - } + from = prev_child; + } - return NULL; + return NULL; return NULL; } - Control::FocusMode Control::get_focus_mode() const { return data.focus_mode; @@ -1847,14 +1732,13 @@ bool Control::has_focus() const { void Control::grab_focus() { - if (!is_inside_tree()){ + if (!is_inside_tree()) { ERR_FAIL_COND(!is_inside_tree()); } - if (data.focus_mode==FOCUS_NONE) + if (data.focus_mode == FOCUS_NONE) return; get_viewport()->_gui_control_grab_focus(this); - } void Control::release_focus() { @@ -1866,7 +1750,6 @@ void Control::release_focus() { get_viewport()->_gui_remove_focus(); update(); - } bool Control::is_toplevel_control() const { @@ -1882,105 +1765,94 @@ void Control::show_modal(bool p_exclusive) { if (is_visible_in_tree()) hide(); - ERR_FAIL_COND( data.MI!=NULL ); + ERR_FAIL_COND(data.MI != NULL); show(); raise(); - data.modal_exclusive=p_exclusive; - data.MI=get_viewport()->_gui_show_modal(this); - data.modal_frame=Engine::get_singleton()->get_frames_drawn(); - + data.modal_exclusive = p_exclusive; + data.MI = get_viewport()->_gui_show_modal(this); + data.modal_frame = Engine::get_singleton()->get_frames_drawn(); } void Control::_modal_set_prev_focus_owner(ObjectID p_prev) { - data.modal_prev_focus_owner=p_prev; + data.modal_prev_focus_owner = p_prev; } void Control::_modal_stack_remove() { - ERR_FAIL_COND(!is_inside_tree()); if (!data.MI) return; - get_viewport()->_gui_remove_from_modal_stack(data.MI,data.modal_prev_focus_owner); - - data.MI=NULL; - data.modal_prev_focus_owner=0; + get_viewport()->_gui_remove_from_modal_stack(data.MI, data.modal_prev_focus_owner); + data.MI = NULL; + data.modal_prev_focus_owner = 0; } -void Control::_propagate_theme_changed(CanvasItem *p_at,Control *p_owner,bool p_assign) { +void Control::_propagate_theme_changed(CanvasItem *p_at, Control *p_owner, bool p_assign) { Control *c = p_at->cast_to<Control>(); - if (c && c!=p_owner && c->data.theme.is_valid()) // has a theme, this can't be propagated + if (c && c != p_owner && c->data.theme.is_valid()) // has a theme, this can't be propagated return; - for(int i=0;i<p_at->get_child_count();i++) { + for (int i = 0; i < p_at->get_child_count(); i++) { CanvasItem *child = p_at->get_child(i)->cast_to<CanvasItem>(); if (child) { - _propagate_theme_changed(child,p_owner,p_assign); + _propagate_theme_changed(child, p_owner, p_assign); } - } - if (c) { if (p_assign) { - c->data.theme_owner=p_owner; + c->data.theme_owner = p_owner; } c->notification(NOTIFICATION_THEME_CHANGED); c->update(); } } - void Control::_theme_changed() { - _propagate_theme_changed(this,this,false); + _propagate_theme_changed(this, this, false); } -void Control::set_theme(const Ref<Theme>& p_theme) { +void Control::set_theme(const Ref<Theme> &p_theme) { - - if (data.theme==p_theme) + if (data.theme == p_theme) return; if (data.theme.is_valid()) { - data.theme->disconnect("changed",this,"_theme_changed"); + data.theme->disconnect("changed", this, "_theme_changed"); } - data.theme=p_theme; + data.theme = p_theme; if (!p_theme.is_null()) { - _propagate_theme_changed(this,this); + _propagate_theme_changed(this, this); } else { - Control *parent = get_parent()?get_parent()->cast_to<Control>():NULL; + Control *parent = get_parent() ? get_parent()->cast_to<Control>() : NULL; if (parent && parent->data.theme_owner) { - _propagate_theme_changed(this,parent->data.theme_owner); + _propagate_theme_changed(this, parent->data.theme_owner); } else { - _propagate_theme_changed(this,NULL); + _propagate_theme_changed(this, NULL); } - } if (data.theme.is_valid()) { - data.theme->connect("changed",this,"_theme_changed"); + data.theme->connect("changed", this, "_theme_changed"); } - } void Control::accept_event() { if (is_inside_tree()) get_viewport()->_gui_accept_event(); - - } Ref<Theme> Control::get_theme() const { @@ -1988,32 +1860,32 @@ Ref<Theme> Control::get_theme() const { return data.theme; } -void Control::set_tooltip(const String& p_tooltip) { +void Control::set_tooltip(const String &p_tooltip) { - data.tooltip=p_tooltip; + data.tooltip = p_tooltip; } -String Control::get_tooltip(const Point2& p_pos) const { +String Control::get_tooltip(const Point2 &p_pos) const { return data.tooltip; } void Control::set_default_cursor_shape(CursorShape p_shape) { - data.default_cursor=p_shape; + data.default_cursor = p_shape; } Control::CursorShape Control::get_default_cursor_shape() const { return data.default_cursor; } -Control::CursorShape Control::get_cursor_shape(const Point2& p_pos) const { +Control::CursorShape Control::get_cursor_shape(const Point2 &p_pos) const { return data.default_cursor; } Transform2D Control::get_transform() const { - Transform2D xform=Transform2D(data.rotation,get_pos()); + Transform2D xform = Transform2D(data.rotation, get_pos()); xform.scale_basis(data.scale); return xform; } @@ -2025,82 +1897,81 @@ String Control::_get_tooltip() const { void Control::set_focus_neighbour(Margin p_margin, const NodePath &p_neighbour) { - ERR_FAIL_INDEX(p_margin,4); - data.focus_neighbour[p_margin]=p_neighbour; + ERR_FAIL_INDEX(p_margin, 4); + data.focus_neighbour[p_margin] = p_neighbour; } NodePath Control::get_focus_neighbour(Margin p_margin) const { - ERR_FAIL_INDEX_V(p_margin,4,NodePath()); + ERR_FAIL_INDEX_V(p_margin, 4, NodePath()); return data.focus_neighbour[p_margin]; } #define MAX_NEIGHBOUR_SEARCH_COUNT 512 -Control *Control::_get_focus_neighbour(Margin p_margin,int p_count) { +Control *Control::_get_focus_neighbour(Margin p_margin, int p_count) { if (p_count >= MAX_NEIGHBOUR_SEARCH_COUNT) return NULL; if (!data.focus_neighbour[p_margin].is_empty()) { - Control *c=NULL; - Node * n = get_node(data.focus_neighbour[p_margin]); + Control *c = NULL; + Node *n = get_node(data.focus_neighbour[p_margin]); if (n) { - c=n->cast_to<Control>(); + c = n->cast_to<Control>(); if (!c) { - ERR_EXPLAIN("Next focus node is not a control: "+n->get_name()); + ERR_EXPLAIN("Next focus node is not a control: " + n->get_name()); ERR_FAIL_V(NULL); } } else { return NULL; } - bool valid=true; + bool valid = true; if (!c->is_visible()) - valid=false; - if (c->get_focus_mode()==FOCUS_NONE) - valid=false; + valid = false; + if (c->get_focus_mode() == FOCUS_NONE) + valid = false; if (valid) return c; - c=c->_get_focus_neighbour(p_margin,p_count+1); + c = c->_get_focus_neighbour(p_margin, p_count + 1); return c; } - - float dist=1e7; - Control * result=NULL; + float dist = 1e7; + Control *result = NULL; Point2 points[4]; Transform2D xform = get_global_transform(); Rect2 rect = get_item_rect(); - points[0]=xform.xform(rect.pos); - points[1]=xform.xform(rect.pos + Point2(rect.size.x, 0)); - points[2]=xform.xform(rect.pos + rect.size); - points[3]=xform.xform(rect.pos + Point2(0, rect.size.y)); + points[0] = xform.xform(rect.pos); + points[1] = xform.xform(rect.pos + Point2(rect.size.x, 0)); + points[2] = xform.xform(rect.pos + rect.size); + points[3] = xform.xform(rect.pos + Point2(0, rect.size.y)); - const Vector2 dir[4]={ - Vector2(-1,0), - Vector2(0,-1), - Vector2(1,0), - Vector2(0,1) + const Vector2 dir[4] = { + Vector2(-1, 0), + Vector2(0, -1), + Vector2(1, 0), + Vector2(0, 1) }; - Vector2 vdir=dir[p_margin]; + Vector2 vdir = dir[p_margin]; - float maxd=-1e7; + float maxd = -1e7; - for(int i=0;i<4;i++) { + for (int i = 0; i < 4; i++) { float d = vdir.dot(points[i]); - if (d>maxd) - maxd=d; + if (d > maxd) + maxd = d; } - Node *base=this; + Node *base = this; while (base) { @@ -2111,109 +1982,104 @@ Control *Control::_get_focus_neighbour(Margin p_margin,int p_count) { if (c->data.RI) break; } - base=base->get_parent(); + base = base->get_parent(); } if (!base) return NULL; - _window_find_focus_neighbour(vdir,base,points,maxd,dist,&result); + _window_find_focus_neighbour(vdir, base, points, maxd, dist, &result); return result; - } -void Control::_window_find_focus_neighbour(const Vector2& p_dir, Node *p_at,const Point2* p_points,float p_min ,float &r_closest_dist,Control **r_closest) { +void Control::_window_find_focus_neighbour(const Vector2 &p_dir, Node *p_at, const Point2 *p_points, float p_min, float &r_closest_dist, Control **r_closest) { if (p_at->cast_to<Viewport>()) return; //bye Control *c = p_at->cast_to<Control>(); - if (c && c !=this && c->get_focus_mode()==FOCUS_ALL && c->is_visible_in_tree()) { + if (c && c != this && c->get_focus_mode() == FOCUS_ALL && c->is_visible_in_tree()) { Point2 points[4]; Transform2D xform = c->get_global_transform(); Rect2 rect = c->get_item_rect(); - points[0]=xform.xform(rect.pos); - points[1]=xform.xform(rect.pos + Point2(rect.size.x, 0)); - points[2]=xform.xform(rect.pos + rect.size); - points[3]=xform.xform(rect.pos + Point2(0, rect.size.y)); + points[0] = xform.xform(rect.pos); + points[1] = xform.xform(rect.pos + Point2(rect.size.x, 0)); + points[2] = xform.xform(rect.pos + rect.size); + points[3] = xform.xform(rect.pos + Point2(0, rect.size.y)); + float min = 1e7; - float min=1e7; - - for(int i=0;i<4;i++) { + for (int i = 0; i < 4; i++) { float d = p_dir.dot(points[i]); if (d < min) - min =d; + min = d; } + if (min > (p_min - CMP_EPSILON)) { - if (min>(p_min-CMP_EPSILON)) { - - for(int i=0;i<4;i++) { + for (int i = 0; i < 4; i++) { - Vector2 la=p_points[i]; - Vector2 lb=p_points[(i+1)%4]; + Vector2 la = p_points[i]; + Vector2 lb = p_points[(i + 1) % 4]; - for(int j=0;j<4;j++) { + for (int j = 0; j < 4; j++) { - Vector2 fa=points[j]; - Vector2 fb=points[(j+1)%4]; + Vector2 fa = points[j]; + Vector2 fb = points[(j + 1) % 4]; - Vector2 pa,pb; - float d=Geometry::get_closest_points_between_segments(la,lb,fa,fb,pa,pb); + Vector2 pa, pb; + float d = Geometry::get_closest_points_between_segments(la, lb, fa, fb, pa, pb); //float d = Geometry::get_closest_distance_between_segments(Vector3(la.x,la.y,0),Vector3(lb.x,lb.y,0),Vector3(fa.x,fa.y,0),Vector3(fb.x,fb.y,0)); - if (d<r_closest_dist) { - r_closest_dist=d; - *r_closest=c; + if (d < r_closest_dist) { + r_closest_dist = d; + *r_closest = c; } } } } - } - for(int i=0;i<p_at->get_child_count();i++) { + for (int i = 0; i < p_at->get_child_count(); i++) { - Node *child=p_at->get_child(i); + Node *child = p_at->get_child(i); Control *childc = child->cast_to<Control>(); if (childc && childc->data.SI) continue; //subwindow, ignore - _window_find_focus_neighbour(p_dir,p_at->get_child(i),p_points,p_min,r_closest_dist,r_closest); + _window_find_focus_neighbour(p_dir, p_at->get_child(i), p_points, p_min, r_closest_dist, r_closest); } } void Control::set_h_size_flags(int p_flags) { - if (data.h_size_flags==p_flags) + if (data.h_size_flags == p_flags) return; - data.h_size_flags=p_flags; + data.h_size_flags = p_flags; emit_signal(SceneStringNames::get_singleton()->size_flags_changed); } -int Control::get_h_size_flags() const{ +int Control::get_h_size_flags() const { return data.h_size_flags; } void Control::set_v_size_flags(int p_flags) { - if (data.v_size_flags==p_flags) + if (data.v_size_flags == p_flags) return; - data.v_size_flags=p_flags; + data.v_size_flags = p_flags; emit_signal(SceneStringNames::get_singleton()->size_flags_changed); } - void Control::set_stretch_ratio(float p_ratio) { - if (data.expand==p_ratio) + if (data.expand == p_ratio) return; - data.expand=p_ratio; + data.expand = p_ratio; emit_signal(SceneStringNames::get_singleton()->size_flags_changed); } @@ -2222,13 +2088,11 @@ float Control::get_stretch_ratio() const { return data.expand; } - void Control::grab_click_focus() { ERR_FAIL_COND(!is_inside_tree()); get_viewport()->_gui_grab_click_focus(this); - } void Control::minimum_size_changed() { @@ -2239,9 +2103,8 @@ void Control::minimum_size_changed() { if (data.pending_min_size_update) return; - - data.pending_min_size_update=true; - MessageQueue::get_singleton()->push_call(this,"_update_minimum_size"); + data.pending_min_size_update = true; + MessageQueue::get_singleton()->push_call(this, "_update_minimum_size"); if (!is_toplevel_control()) { Control *pc = get_parent_control(); @@ -2250,37 +2113,34 @@ void Control::minimum_size_changed() { } } -int Control::get_v_size_flags() const{ +int Control::get_v_size_flags() const { return data.v_size_flags; } void Control::set_mouse_filter(MouseFilter p_filter) { - ERR_FAIL_INDEX(p_filter,3); - data.mouse_filter=p_filter; + ERR_FAIL_INDEX(p_filter, 3); + data.mouse_filter = p_filter; } -Control::MouseFilter Control::get_mouse_filter() const{ +Control::MouseFilter Control::get_mouse_filter() const { return data.mouse_filter; } - Control *Control::get_focus_owner() const { - ERR_FAIL_COND_V(!is_inside_tree(),NULL); + ERR_FAIL_COND_V(!is_inside_tree(), NULL); return get_viewport()->_gui_get_focus_owner(); } - -void Control::warp_mouse(const Point2& p_to_pos) { +void Control::warp_mouse(const Point2 &p_to_pos) { ERR_FAIL_COND(!is_inside_tree()); get_viewport()->warp_mouse(get_global_transform().xform(p_to_pos)); } - bool Control::is_text_field() const { -/* + /* if (get_script_instance()) { Variant v=p_point; const Variant *p[2]={&v,&p_data}; @@ -2290,19 +2150,18 @@ bool Control::is_text_field() const { return ret; } */ - return false; + return false; } - void Control::set_rotation(float p_radians) { - data.rotation=p_radians; + data.rotation = p_radians; update(); _notify_transform(); _change_notify("rect/rotation"); } -float Control::get_rotation() const{ +float Control::get_rotation() const { return data.rotation; } @@ -2326,13 +2185,13 @@ float Control::_get_rotation_deg() const { return get_rotation_deg(); } //needed to update the control if the font changes.. -void Control::_ref_font( Ref<Font> p_sc) { +void Control::_ref_font(Ref<Font> p_sc) { if (!data.font_refcount.has(p_sc)) { - data.font_refcount[p_sc]=1; - p_sc->connect("changed",this,"_font_changed"); + data.font_refcount[p_sc] = 1; + p_sc->connect("changed", this, "_font_changed"); } else { - data.font_refcount[p_sc]+=1; + data.font_refcount[p_sc] += 1; } } @@ -2340,53 +2199,53 @@ void Control::_unref_font(Ref<Font> p_sc) { ERR_FAIL_COND(!data.font_refcount.has(p_sc)); data.font_refcount[p_sc]--; - if (data.font_refcount[p_sc]==0) { - p_sc->disconnect("changed",this,"_font_changed"); + if (data.font_refcount[p_sc] == 0) { + p_sc->disconnect("changed", this, "_font_changed"); data.font_refcount.erase(p_sc); } } -void Control::_font_changed(){ +void Control::_font_changed() { update(); notification(NOTIFICATION_THEME_CHANGED); minimum_size_changed(); //fonts affect minimum size pretty much almost always } -void Control::set_scale(const Vector2& p_scale){ +void Control::set_scale(const Vector2 &p_scale) { - data.scale=p_scale; + data.scale = p_scale; update(); _notify_transform(); } -Vector2 Control::get_scale() const{ +Vector2 Control::get_scale() const { return data.scale; } Control *Control::get_root_parent_control() const { - const CanvasItem *ci=this; - const Control *root=this; + const CanvasItem *ci = this; + const Control *root = this; - while(ci) { + while (ci) { const Control *c = ci->cast_to<Control>(); if (c) { - root=c; + root = c; if (c->data.RI || c->data.MI || c->is_toplevel_control()) break; } - ci=ci->get_parent_item(); + ci = ci->get_parent_item(); } - return const_cast<Control*>(root); + return const_cast<Control *>(root); } void Control::set_block_minimum_size_adjust(bool p_block) { - data.block_minimum_size_adjust=p_block; + data.block_minimum_size_adjust = p_block; } bool Control::is_minimum_size_adjust_blocked() const { @@ -2394,10 +2253,9 @@ bool Control::is_minimum_size_adjust_blocked() const { return data.block_minimum_size_adjust; } - void Control::set_disable_visibility_clip(bool p_ignore) { - data.disable_visibility_clip=p_ignore; + data.disable_visibility_clip = p_ignore; update(); } @@ -2406,34 +2264,32 @@ bool Control::is_visibility_clip_disabled() const { return data.disable_visibility_clip; } -void Control::get_argument_options(const StringName& p_function,int p_idx,List<String>*r_options) const { +void Control::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const { - Node::get_argument_options(p_function,p_idx,r_options); + Node::get_argument_options(p_function, p_idx, r_options); - if (p_idx==0) { + if (p_idx == 0) { List<StringName> sn; String pf = p_function; - if (pf=="add_color_override" || pf=="has_color" || pf=="has_color_override" || pf=="get_color") { - Theme::get_default()->get_color_list(get_class(),&sn); - } else if (pf=="add_style_override" || pf=="has_style" || pf=="has_style_override" || pf=="get_style") { - Theme::get_default()->get_stylebox_list(get_class(),&sn); - } else if (pf=="add_font_override" || pf=="has_font" || pf=="has_font_override" || pf=="get_font") { - Theme::get_default()->get_font_list(get_class(),&sn); - } else if (pf=="add_constant_override" || pf=="has_constant" || pf=="has_constant_override" || pf=="get_constant") { - Theme::get_default()->get_constant_list(get_class(),&sn); + if (pf == "add_color_override" || pf == "has_color" || pf == "has_color_override" || pf == "get_color") { + Theme::get_default()->get_color_list(get_class(), &sn); + } else if (pf == "add_style_override" || pf == "has_style" || pf == "has_style_override" || pf == "get_style") { + Theme::get_default()->get_stylebox_list(get_class(), &sn); + } else if (pf == "add_font_override" || pf == "has_font" || pf == "has_font_override" || pf == "get_font") { + Theme::get_default()->get_font_list(get_class(), &sn); + } else if (pf == "add_constant_override" || pf == "has_constant" || pf == "has_constant_override" || pf == "get_constant") { + Theme::get_default()->get_constant_list(get_class(), &sn); } sn.sort_custom<StringName::AlphCompare>(); - for (List<StringName>::Element *E=sn.front();E;E=E->next()) { - r_options->push_back("\""+E->get()+"\""); + for (List<StringName>::Element *E = sn.front(); E; E = E->next()) { + r_options->push_back("\"" + E->get() + "\""); } } - - } void Control::set_clip_contents(bool p_clip) { - data.clip_contents=p_clip; + data.clip_contents = p_clip; update(); } @@ -2442,81 +2298,79 @@ bool Control::is_clipping_contents() { return data.clip_contents; } - void Control::_bind_methods() { - //ClassDB::bind_method(D_METHOD("_window_resize_event"),&Control::_window_resize_event); - ClassDB::bind_method(D_METHOD("_size_changed"),&Control::_size_changed); - ClassDB::bind_method(D_METHOD("_update_minimum_size"),&Control::_update_minimum_size); - - ClassDB::bind_method(D_METHOD("accept_event"),&Control::accept_event); - ClassDB::bind_method(D_METHOD("get_minimum_size"),&Control::get_minimum_size); - ClassDB::bind_method(D_METHOD("get_combined_minimum_size"),&Control::get_combined_minimum_size); - ClassDB::bind_method(D_METHOD("set_anchor","margin","anchor_mode","keep_margin"),&Control::set_anchor,DEFVAL(false)); - ClassDB::bind_method(D_METHOD("_set_anchor","margin","anchor_mode"),&Control::_set_anchor); - ClassDB::bind_method(D_METHOD("get_anchor","margin"),&Control::get_anchor); - ClassDB::bind_method(D_METHOD("set_margin","margin","offset"),&Control::set_margin); - ClassDB::bind_method(D_METHOD("set_anchor_and_margin","margin","anchor_mode","offset"),&Control::set_anchor_and_margin); - ClassDB::bind_method(D_METHOD("set_begin","pos"),&Control::set_begin); - ClassDB::bind_method(D_METHOD("set_end","pos"),&Control::set_end); - ClassDB::bind_method(D_METHOD("set_pos","pos"),&Control::set_pos); - ClassDB::bind_method(D_METHOD("set_size","size"),&Control::set_size); - ClassDB::bind_method(D_METHOD("set_custom_minimum_size","size"),&Control::set_custom_minimum_size); - ClassDB::bind_method(D_METHOD("set_global_pos","pos"),&Control::set_global_pos); - ClassDB::bind_method(D_METHOD("set_rotation","radians"),&Control::set_rotation); - ClassDB::bind_method(D_METHOD("set_rotation_deg","degrees"),&Control::set_rotation_deg); + ClassDB::bind_method(D_METHOD("_size_changed"), &Control::_size_changed); + ClassDB::bind_method(D_METHOD("_update_minimum_size"), &Control::_update_minimum_size); + + ClassDB::bind_method(D_METHOD("accept_event"), &Control::accept_event); + ClassDB::bind_method(D_METHOD("get_minimum_size"), &Control::get_minimum_size); + ClassDB::bind_method(D_METHOD("get_combined_minimum_size"), &Control::get_combined_minimum_size); + ClassDB::bind_method(D_METHOD("set_anchor", "margin", "anchor_mode", "keep_margin"), &Control::set_anchor, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("_set_anchor", "margin", "anchor_mode"), &Control::_set_anchor); + ClassDB::bind_method(D_METHOD("get_anchor", "margin"), &Control::get_anchor); + ClassDB::bind_method(D_METHOD("set_margin", "margin", "offset"), &Control::set_margin); + ClassDB::bind_method(D_METHOD("set_anchor_and_margin", "margin", "anchor_mode", "offset"), &Control::set_anchor_and_margin); + ClassDB::bind_method(D_METHOD("set_begin", "pos"), &Control::set_begin); + ClassDB::bind_method(D_METHOD("set_end", "pos"), &Control::set_end); + ClassDB::bind_method(D_METHOD("set_pos", "pos"), &Control::set_pos); + ClassDB::bind_method(D_METHOD("set_size", "size"), &Control::set_size); + ClassDB::bind_method(D_METHOD("set_custom_minimum_size", "size"), &Control::set_custom_minimum_size); + ClassDB::bind_method(D_METHOD("set_global_pos", "pos"), &Control::set_global_pos); + ClassDB::bind_method(D_METHOD("set_rotation", "radians"), &Control::set_rotation); + ClassDB::bind_method(D_METHOD("set_rotation_deg", "degrees"), &Control::set_rotation_deg); // TODO: Obsolete this method (old name) properly (GH-4397) - ClassDB::bind_method(D_METHOD("_set_rotation_deg","degrees"),&Control::_set_rotation_deg); - ClassDB::bind_method(D_METHOD("set_scale","scale"),&Control::set_scale); - ClassDB::bind_method(D_METHOD("get_margin","margin"),&Control::get_margin); - ClassDB::bind_method(D_METHOD("get_begin"),&Control::get_begin); - ClassDB::bind_method(D_METHOD("get_end"),&Control::get_end); - ClassDB::bind_method(D_METHOD("get_pos"),&Control::get_pos); - ClassDB::bind_method(D_METHOD("get_size"),&Control::get_size); - ClassDB::bind_method(D_METHOD("get_rotation"),&Control::get_rotation); - ClassDB::bind_method(D_METHOD("get_rotation_deg"),&Control::get_rotation_deg); + ClassDB::bind_method(D_METHOD("_set_rotation_deg", "degrees"), &Control::_set_rotation_deg); + ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Control::set_scale); + ClassDB::bind_method(D_METHOD("get_margin", "margin"), &Control::get_margin); + ClassDB::bind_method(D_METHOD("get_begin"), &Control::get_begin); + ClassDB::bind_method(D_METHOD("get_end"), &Control::get_end); + ClassDB::bind_method(D_METHOD("get_pos"), &Control::get_pos); + ClassDB::bind_method(D_METHOD("get_size"), &Control::get_size); + ClassDB::bind_method(D_METHOD("get_rotation"), &Control::get_rotation); + ClassDB::bind_method(D_METHOD("get_rotation_deg"), &Control::get_rotation_deg); // TODO: Obsolete this method (old name) properly (GH-4397) - ClassDB::bind_method(D_METHOD("_get_rotation_deg"),&Control::_get_rotation_deg); - ClassDB::bind_method(D_METHOD("get_scale"),&Control::get_scale); - ClassDB::bind_method(D_METHOD("get_custom_minimum_size"),&Control::get_custom_minimum_size); - ClassDB::bind_method(D_METHOD("get_parent_area_size"),&Control::get_size); - ClassDB::bind_method(D_METHOD("get_global_pos"),&Control::get_global_pos); - ClassDB::bind_method(D_METHOD("get_rect"),&Control::get_rect); - ClassDB::bind_method(D_METHOD("get_global_rect"),&Control::get_global_rect); - ClassDB::bind_method(D_METHOD("set_area_as_parent_rect","margin"),&Control::set_area_as_parent_rect,DEFVAL(0)); - ClassDB::bind_method(D_METHOD("show_modal","exclusive"),&Control::show_modal,DEFVAL(false)); - ClassDB::bind_method(D_METHOD("set_focus_mode","mode"),&Control::set_focus_mode); - ClassDB::bind_method(D_METHOD("get_focus_mode"),&Control::get_focus_mode); - ClassDB::bind_method(D_METHOD("has_focus"),&Control::has_focus); - ClassDB::bind_method(D_METHOD("grab_focus"),&Control::grab_focus); - ClassDB::bind_method(D_METHOD("release_focus"),&Control::release_focus); - ClassDB::bind_method(D_METHOD("get_focus_owner:Control"),&Control::get_focus_owner); - - ClassDB::bind_method(D_METHOD("set_h_size_flags","flags"),&Control::set_h_size_flags); - ClassDB::bind_method(D_METHOD("get_h_size_flags"),&Control::get_h_size_flags); - - ClassDB::bind_method(D_METHOD("set_stretch_ratio","ratio"),&Control::set_stretch_ratio); - ClassDB::bind_method(D_METHOD("get_stretch_ratio"),&Control::get_stretch_ratio); - - ClassDB::bind_method(D_METHOD("set_v_size_flags","flags"),&Control::set_v_size_flags); - ClassDB::bind_method(D_METHOD("get_v_size_flags"),&Control::get_v_size_flags); - - ClassDB::bind_method(D_METHOD("set_theme","theme:Theme"),&Control::set_theme); - ClassDB::bind_method(D_METHOD("get_theme:Theme"),&Control::get_theme); - - ClassDB::bind_method(D_METHOD("add_icon_override","name","texture:Texture"),&Control::add_icon_override); - ClassDB::bind_method(D_METHOD("add_shader_override","name","shader:Shader"),&Control::add_shader_override); - ClassDB::bind_method(D_METHOD("add_style_override","name","stylebox:StyleBox"),&Control::add_style_override); - ClassDB::bind_method(D_METHOD("add_font_override","name","font:Font"),&Control::add_font_override); - ClassDB::bind_method(D_METHOD("add_color_override","name","color"),&Control::add_color_override); - ClassDB::bind_method(D_METHOD("add_constant_override","name","constant"),&Control::add_constant_override); - - ClassDB::bind_method(D_METHOD("get_icon:Texture","name","type"),&Control::get_icon,DEFVAL("")); - ClassDB::bind_method(D_METHOD("get_stylebox:StyleBox","name","type"),&Control::get_stylebox,DEFVAL("")); - ClassDB::bind_method(D_METHOD("get_font:Font","name","type"),&Control::get_font,DEFVAL("")); - ClassDB::bind_method(D_METHOD("get_color","name","type"),&Control::get_color,DEFVAL("")); - ClassDB::bind_method(D_METHOD("get_constant","name","type"),&Control::get_constant,DEFVAL("")); + ClassDB::bind_method(D_METHOD("_get_rotation_deg"), &Control::_get_rotation_deg); + ClassDB::bind_method(D_METHOD("get_scale"), &Control::get_scale); + ClassDB::bind_method(D_METHOD("get_custom_minimum_size"), &Control::get_custom_minimum_size); + ClassDB::bind_method(D_METHOD("get_parent_area_size"), &Control::get_size); + ClassDB::bind_method(D_METHOD("get_global_pos"), &Control::get_global_pos); + ClassDB::bind_method(D_METHOD("get_rect"), &Control::get_rect); + ClassDB::bind_method(D_METHOD("get_global_rect"), &Control::get_global_rect); + ClassDB::bind_method(D_METHOD("set_area_as_parent_rect", "margin"), &Control::set_area_as_parent_rect, DEFVAL(0)); + ClassDB::bind_method(D_METHOD("show_modal", "exclusive"), &Control::show_modal, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("set_focus_mode", "mode"), &Control::set_focus_mode); + ClassDB::bind_method(D_METHOD("get_focus_mode"), &Control::get_focus_mode); + ClassDB::bind_method(D_METHOD("has_focus"), &Control::has_focus); + ClassDB::bind_method(D_METHOD("grab_focus"), &Control::grab_focus); + ClassDB::bind_method(D_METHOD("release_focus"), &Control::release_focus); + ClassDB::bind_method(D_METHOD("get_focus_owner:Control"), &Control::get_focus_owner); + + ClassDB::bind_method(D_METHOD("set_h_size_flags", "flags"), &Control::set_h_size_flags); + ClassDB::bind_method(D_METHOD("get_h_size_flags"), &Control::get_h_size_flags); + + ClassDB::bind_method(D_METHOD("set_stretch_ratio", "ratio"), &Control::set_stretch_ratio); + ClassDB::bind_method(D_METHOD("get_stretch_ratio"), &Control::get_stretch_ratio); + + ClassDB::bind_method(D_METHOD("set_v_size_flags", "flags"), &Control::set_v_size_flags); + ClassDB::bind_method(D_METHOD("get_v_size_flags"), &Control::get_v_size_flags); + + ClassDB::bind_method(D_METHOD("set_theme", "theme:Theme"), &Control::set_theme); + ClassDB::bind_method(D_METHOD("get_theme:Theme"), &Control::get_theme); + + ClassDB::bind_method(D_METHOD("add_icon_override", "name", "texture:Texture"), &Control::add_icon_override); + ClassDB::bind_method(D_METHOD("add_shader_override", "name", "shader:Shader"), &Control::add_shader_override); + ClassDB::bind_method(D_METHOD("add_style_override", "name", "stylebox:StyleBox"), &Control::add_style_override); + ClassDB::bind_method(D_METHOD("add_font_override", "name", "font:Font"), &Control::add_font_override); + ClassDB::bind_method(D_METHOD("add_color_override", "name", "color"), &Control::add_color_override); + ClassDB::bind_method(D_METHOD("add_constant_override", "name", "constant"), &Control::add_constant_override); + + ClassDB::bind_method(D_METHOD("get_icon:Texture", "name", "type"), &Control::get_icon, DEFVAL("")); + ClassDB::bind_method(D_METHOD("get_stylebox:StyleBox", "name", "type"), &Control::get_stylebox, DEFVAL("")); + ClassDB::bind_method(D_METHOD("get_font:Font", "name", "type"), &Control::get_font, DEFVAL("")); + ClassDB::bind_method(D_METHOD("get_color", "name", "type"), &Control::get_color, DEFVAL("")); + ClassDB::bind_method(D_METHOD("get_constant", "name", "type"), &Control::get_constant, DEFVAL("")); ClassDB::bind_method(D_METHOD("has_icon_override", "name"), &Control::has_icon_override); ClassDB::bind_method(D_METHOD("has_stylebox_override", "name"), &Control::has_stylebox_override); @@ -2530,183 +2384,170 @@ void Control::_bind_methods() { ClassDB::bind_method(D_METHOD("has_color", "name", "type"), &Control::has_color, DEFVAL("")); ClassDB::bind_method(D_METHOD("has_constant", "name", "type"), &Control::has_constant, DEFVAL("")); - ClassDB::bind_method(D_METHOD("get_parent_control:Control"),&Control::get_parent_control); + ClassDB::bind_method(D_METHOD("get_parent_control:Control"), &Control::get_parent_control); - ClassDB::bind_method(D_METHOD("set_tooltip","tooltip"),&Control::set_tooltip); - ClassDB::bind_method(D_METHOD("get_tooltip","atpos"),&Control::get_tooltip,DEFVAL(Point2())); - ClassDB::bind_method(D_METHOD("_get_tooltip"),&Control::_get_tooltip); + ClassDB::bind_method(D_METHOD("set_tooltip", "tooltip"), &Control::set_tooltip); + ClassDB::bind_method(D_METHOD("get_tooltip", "atpos"), &Control::get_tooltip, DEFVAL(Point2())); + ClassDB::bind_method(D_METHOD("_get_tooltip"), &Control::_get_tooltip); - ClassDB::bind_method(D_METHOD("set_default_cursor_shape","shape"),&Control::set_default_cursor_shape); - ClassDB::bind_method(D_METHOD("get_default_cursor_shape"),&Control::get_default_cursor_shape); - ClassDB::bind_method(D_METHOD("get_cursor_shape","pos"),&Control::get_cursor_shape,DEFVAL(Point2())); + ClassDB::bind_method(D_METHOD("set_default_cursor_shape", "shape"), &Control::set_default_cursor_shape); + ClassDB::bind_method(D_METHOD("get_default_cursor_shape"), &Control::get_default_cursor_shape); + ClassDB::bind_method(D_METHOD("get_cursor_shape", "pos"), &Control::get_cursor_shape, DEFVAL(Point2())); - ClassDB::bind_method(D_METHOD("set_focus_neighbour","margin","neighbour"),&Control::set_focus_neighbour); - ClassDB::bind_method(D_METHOD("get_focus_neighbour","margin"),&Control::get_focus_neighbour); + ClassDB::bind_method(D_METHOD("set_focus_neighbour", "margin", "neighbour"), &Control::set_focus_neighbour); + ClassDB::bind_method(D_METHOD("get_focus_neighbour", "margin"), &Control::get_focus_neighbour); - ClassDB::bind_method(D_METHOD("force_drag","data","preview"),&Control::force_drag); + ClassDB::bind_method(D_METHOD("force_drag", "data", "preview"), &Control::force_drag); - ClassDB::bind_method(D_METHOD("set_mouse_filter","filter"),&Control::set_mouse_filter); - ClassDB::bind_method(D_METHOD("get_mouse_filter"),&Control::get_mouse_filter); + ClassDB::bind_method(D_METHOD("set_mouse_filter", "filter"), &Control::set_mouse_filter); + ClassDB::bind_method(D_METHOD("get_mouse_filter"), &Control::get_mouse_filter); - ClassDB::bind_method(D_METHOD("set_clip_contents","enable"),&Control::set_clip_contents); - ClassDB::bind_method(D_METHOD("is_clipping_contents"),&Control::is_clipping_contents); + ClassDB::bind_method(D_METHOD("set_clip_contents", "enable"), &Control::set_clip_contents); + ClassDB::bind_method(D_METHOD("is_clipping_contents"), &Control::is_clipping_contents); - ClassDB::bind_method(D_METHOD("grab_click_focus"),&Control::grab_click_focus); + ClassDB::bind_method(D_METHOD("grab_click_focus"), &Control::grab_click_focus); - ClassDB::bind_method(D_METHOD("set_drag_forwarding","target:Control"),&Control::set_drag_forwarding); - ClassDB::bind_method(D_METHOD("set_drag_preview","control:Control"),&Control::set_drag_preview); + ClassDB::bind_method(D_METHOD("set_drag_forwarding", "target:Control"), &Control::set_drag_forwarding); + ClassDB::bind_method(D_METHOD("set_drag_preview", "control:Control"), &Control::set_drag_preview); - ClassDB::bind_method(D_METHOD("warp_mouse","to_pos"),&Control::warp_mouse); + ClassDB::bind_method(D_METHOD("warp_mouse", "to_pos"), &Control::warp_mouse); ClassDB::bind_method(D_METHOD("minimum_size_changed"), &Control::minimum_size_changed); ClassDB::bind_method(D_METHOD("_theme_changed"), &Control::_theme_changed); - - ClassDB::bind_method(D_METHOD("_font_changed"), &Control::_font_changed); - BIND_VMETHOD(MethodInfo("_gui_input",PropertyInfo(Variant::INPUT_EVENT,"event"))); - BIND_VMETHOD(MethodInfo(Variant::VECTOR2,"get_minimum_size")); - BIND_VMETHOD(MethodInfo(Variant::OBJECT,"get_drag_data",PropertyInfo(Variant::VECTOR2,"pos"))); - BIND_VMETHOD(MethodInfo(Variant::BOOL,"can_drop_data",PropertyInfo(Variant::VECTOR2,"pos"),PropertyInfo(Variant::NIL,"data"))); - BIND_VMETHOD(MethodInfo("drop_data",PropertyInfo(Variant::VECTOR2,"pos"),PropertyInfo(Variant::NIL,"data"))); - - ADD_GROUP("Anchor","anchor_"); - ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor_left", PROPERTY_HINT_ENUM, "Begin,End,Center"), "_set_anchor","get_anchor", MARGIN_LEFT ); - ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor_top", PROPERTY_HINT_ENUM, "Begin,End,Center"), "_set_anchor","get_anchor", MARGIN_TOP ); - ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor_right", PROPERTY_HINT_ENUM, "Begin,End,Center"), "_set_anchor","get_anchor", MARGIN_RIGHT ); - ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor_bottom", PROPERTY_HINT_ENUM, "Begin,End,Center"), "_set_anchor","get_anchor", MARGIN_BOTTOM ); - - ADD_GROUP("Margin","margin_"); - ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin_left", PROPERTY_HINT_RANGE, "-4096,4096"), "set_margin","get_margin",MARGIN_LEFT ); - ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin_top", PROPERTY_HINT_RANGE, "-4096,4096"), "set_margin","get_margin",MARGIN_TOP ); - ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin_right", PROPERTY_HINT_RANGE, "-4096,4096"), "set_margin","get_margin",MARGIN_RIGHT ); - ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin_bottom", PROPERTY_HINT_RANGE, "-4096,4096"), "set_margin","get_margin",MARGIN_BOTTOM ); - - ADD_GROUP("Rect","rect_"); - ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"rect_pos", PROPERTY_HINT_NONE, "",PROPERTY_USAGE_EDITOR), "set_pos","get_pos") ; - ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"rect_size", PROPERTY_HINT_NONE, "",PROPERTY_USAGE_EDITOR), "set_size","get_size") ; - ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"rect_min_size"), "set_custom_minimum_size","get_custom_minimum_size") ; - ADD_PROPERTYNZ( PropertyInfo(Variant::REAL,"rect_rotation",PROPERTY_HINT_RANGE,"-1080,1080,0.01"), "set_rotation_deg","get_rotation_deg") ; - ADD_PROPERTYNO( PropertyInfo(Variant::VECTOR2,"rect_scale"), "set_scale","get_scale") ; - ADD_PROPERTYNO( PropertyInfo(Variant::BOOL,"rect_clip_content"), "set_clip_contents","is_clipping_contents") ; - - - ADD_GROUP("Hint","hint_"); - ADD_PROPERTYNZ( PropertyInfo(Variant::STRING,"hint_tooltip", PROPERTY_HINT_MULTILINE_TEXT), "set_tooltip","_get_tooltip") ; - - ADD_GROUP("Focus","focus_"); - ADD_PROPERTYINZ( PropertyInfo(Variant::NODE_PATH,"focus_neighbour_left" ), "set_focus_neighbour","get_focus_neighbour",MARGIN_LEFT ); - ADD_PROPERTYINZ( PropertyInfo(Variant::NODE_PATH,"focus_neighbour_top" ), "set_focus_neighbour","get_focus_neighbour",MARGIN_TOP ); - ADD_PROPERTYINZ( PropertyInfo(Variant::NODE_PATH,"focus_neighbour_right" ), "set_focus_neighbour","get_focus_neighbour",MARGIN_RIGHT ); - ADD_PROPERTYINZ( PropertyInfo(Variant::NODE_PATH,"focus_neighbour_bottom" ), "set_focus_neighbour","get_focus_neighbour",MARGIN_BOTTOM ); - - ADD_GROUP("Mouse","mouse_"); - ADD_PROPERTY( PropertyInfo(Variant::INT,"mouse_filter",PROPERTY_HINT_ENUM,"Stop,Pass,Ignore"), "set_mouse_filter","get_mouse_filter") ; - - ADD_GROUP("Size Flags","size_flags_"); - ADD_PROPERTYNO( PropertyInfo(Variant::INT,"size_flags_horizontal", PROPERTY_HINT_FLAGS, "Fill,Expand"), "set_h_size_flags","get_h_size_flags") ; - ADD_PROPERTYNO( PropertyInfo(Variant::INT,"size_flags_vertical", PROPERTY_HINT_FLAGS, "Fill,Expand"), "set_v_size_flags","get_v_size_flags") ; - ADD_PROPERTYNO( PropertyInfo(Variant::INT,"size_flags_stretch_ratio", PROPERTY_HINT_RANGE, "1,128,0.01"), "set_stretch_ratio","get_stretch_ratio") ; - ADD_GROUP("Theme",""); - ADD_PROPERTYNZ( PropertyInfo(Variant::OBJECT,"theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), "set_theme","get_theme") ; - ADD_GROUP("",""); - - BIND_CONSTANT( ANCHOR_BEGIN ); - BIND_CONSTANT( ANCHOR_END ); - BIND_CONSTANT( ANCHOR_CENTER ); - BIND_CONSTANT( FOCUS_NONE ); - BIND_CONSTANT( FOCUS_CLICK ); - BIND_CONSTANT( FOCUS_ALL ); - - - BIND_CONSTANT( NOTIFICATION_RESIZED ); - BIND_CONSTANT( NOTIFICATION_MOUSE_ENTER ); - BIND_CONSTANT( NOTIFICATION_MOUSE_EXIT ); - BIND_CONSTANT( NOTIFICATION_FOCUS_ENTER ); - BIND_CONSTANT( NOTIFICATION_FOCUS_EXIT ); - BIND_CONSTANT( NOTIFICATION_THEME_CHANGED ); - BIND_CONSTANT( NOTIFICATION_MODAL_CLOSE ); - - BIND_CONSTANT( CURSOR_ARROW ); - BIND_CONSTANT( CURSOR_IBEAM ); - BIND_CONSTANT( CURSOR_POINTING_HAND ); - BIND_CONSTANT( CURSOR_CROSS ); - BIND_CONSTANT( CURSOR_WAIT ); - BIND_CONSTANT( CURSOR_BUSY ); - BIND_CONSTANT( CURSOR_DRAG ); - BIND_CONSTANT( CURSOR_CAN_DROP ); - BIND_CONSTANT( CURSOR_FORBIDDEN ); - BIND_CONSTANT( CURSOR_VSIZE ); - BIND_CONSTANT( CURSOR_HSIZE ); - BIND_CONSTANT( CURSOR_BDIAGSIZE ); - BIND_CONSTANT( CURSOR_FDIAGSIZE ); - BIND_CONSTANT( CURSOR_MOVE ); - BIND_CONSTANT( CURSOR_VSPLIT ); - BIND_CONSTANT( CURSOR_HSPLIT ); - BIND_CONSTANT( CURSOR_HELP ); - - BIND_CONSTANT( SIZE_EXPAND ); - BIND_CONSTANT( SIZE_FILL ); - BIND_CONSTANT( SIZE_EXPAND_FILL ); - - BIND_CONSTANT( MOUSE_FILTER_STOP ); - BIND_CONSTANT( MOUSE_FILTER_PASS ); - BIND_CONSTANT( MOUSE_FILTER_IGNORE ); - - ADD_SIGNAL( MethodInfo("resized") ); - ADD_SIGNAL( MethodInfo("gui_input",PropertyInfo(Variant::INPUT_EVENT,"ev")) ); - ADD_SIGNAL( MethodInfo("mouse_entered") ); - ADD_SIGNAL( MethodInfo("mouse_exited") ); - ADD_SIGNAL( MethodInfo("focus_entered") ); - ADD_SIGNAL( MethodInfo("focus_exited") ); - ADD_SIGNAL( MethodInfo("size_flags_changed") ); - ADD_SIGNAL( MethodInfo("minimum_size_changed") ); - ADD_SIGNAL( MethodInfo("modal_closed") ); - - + BIND_VMETHOD(MethodInfo("_gui_input", PropertyInfo(Variant::INPUT_EVENT, "event"))); + BIND_VMETHOD(MethodInfo(Variant::VECTOR2, "get_minimum_size")); + BIND_VMETHOD(MethodInfo(Variant::OBJECT, "get_drag_data", PropertyInfo(Variant::VECTOR2, "pos"))); + BIND_VMETHOD(MethodInfo(Variant::BOOL, "can_drop_data", PropertyInfo(Variant::VECTOR2, "pos"), PropertyInfo(Variant::NIL, "data"))); + BIND_VMETHOD(MethodInfo("drop_data", PropertyInfo(Variant::VECTOR2, "pos"), PropertyInfo(Variant::NIL, "data"))); + + ADD_GROUP("Anchor", "anchor_"); + ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "anchor_left", PROPERTY_HINT_ENUM, "Begin,End,Center"), "_set_anchor", "get_anchor", MARGIN_LEFT); + ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "anchor_top", PROPERTY_HINT_ENUM, "Begin,End,Center"), "_set_anchor", "get_anchor", MARGIN_TOP); + ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "anchor_right", PROPERTY_HINT_ENUM, "Begin,End,Center"), "_set_anchor", "get_anchor", MARGIN_RIGHT); + ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "anchor_bottom", PROPERTY_HINT_ENUM, "Begin,End,Center"), "_set_anchor", "get_anchor", MARGIN_BOTTOM); + + ADD_GROUP("Margin", "margin_"); + ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "margin_left", PROPERTY_HINT_RANGE, "-4096,4096"), "set_margin", "get_margin", MARGIN_LEFT); + ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "margin_top", PROPERTY_HINT_RANGE, "-4096,4096"), "set_margin", "get_margin", MARGIN_TOP); + ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "margin_right", PROPERTY_HINT_RANGE, "-4096,4096"), "set_margin", "get_margin", MARGIN_RIGHT); + ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "margin_bottom", PROPERTY_HINT_RANGE, "-4096,4096"), "set_margin", "get_margin", MARGIN_BOTTOM); + + ADD_GROUP("Rect", "rect_"); + ADD_PROPERTYNZ(PropertyInfo(Variant::VECTOR2, "rect_pos", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_pos", "get_pos"); + ADD_PROPERTYNZ(PropertyInfo(Variant::VECTOR2, "rect_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_size", "get_size"); + ADD_PROPERTYNZ(PropertyInfo(Variant::VECTOR2, "rect_min_size"), "set_custom_minimum_size", "get_custom_minimum_size"); + ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "rect_rotation", PROPERTY_HINT_RANGE, "-1080,1080,0.01"), "set_rotation_deg", "get_rotation_deg"); + ADD_PROPERTYNO(PropertyInfo(Variant::VECTOR2, "rect_scale"), "set_scale", "get_scale"); + ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "rect_clip_content"), "set_clip_contents", "is_clipping_contents"); + + ADD_GROUP("Hint", "hint_"); + ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "hint_tooltip", PROPERTY_HINT_MULTILINE_TEXT), "set_tooltip", "_get_tooltip"); + + ADD_GROUP("Focus", "focus_"); + ADD_PROPERTYINZ(PropertyInfo(Variant::NODE_PATH, "focus_neighbour_left"), "set_focus_neighbour", "get_focus_neighbour", MARGIN_LEFT); + ADD_PROPERTYINZ(PropertyInfo(Variant::NODE_PATH, "focus_neighbour_top"), "set_focus_neighbour", "get_focus_neighbour", MARGIN_TOP); + ADD_PROPERTYINZ(PropertyInfo(Variant::NODE_PATH, "focus_neighbour_right"), "set_focus_neighbour", "get_focus_neighbour", MARGIN_RIGHT); + ADD_PROPERTYINZ(PropertyInfo(Variant::NODE_PATH, "focus_neighbour_bottom"), "set_focus_neighbour", "get_focus_neighbour", MARGIN_BOTTOM); + + ADD_GROUP("Mouse", "mouse_"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_filter", PROPERTY_HINT_ENUM, "Stop,Pass,Ignore"), "set_mouse_filter", "get_mouse_filter"); + + ADD_GROUP("Size Flags", "size_flags_"); + ADD_PROPERTYNO(PropertyInfo(Variant::INT, "size_flags_horizontal", PROPERTY_HINT_FLAGS, "Fill,Expand"), "set_h_size_flags", "get_h_size_flags"); + ADD_PROPERTYNO(PropertyInfo(Variant::INT, "size_flags_vertical", PROPERTY_HINT_FLAGS, "Fill,Expand"), "set_v_size_flags", "get_v_size_flags"); + ADD_PROPERTYNO(PropertyInfo(Variant::INT, "size_flags_stretch_ratio", PROPERTY_HINT_RANGE, "1,128,0.01"), "set_stretch_ratio", "get_stretch_ratio"); + ADD_GROUP("Theme", ""); + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), "set_theme", "get_theme"); + ADD_GROUP("", ""); + + BIND_CONSTANT(ANCHOR_BEGIN); + BIND_CONSTANT(ANCHOR_END); + BIND_CONSTANT(ANCHOR_CENTER); + BIND_CONSTANT(FOCUS_NONE); + BIND_CONSTANT(FOCUS_CLICK); + BIND_CONSTANT(FOCUS_ALL); + + BIND_CONSTANT(NOTIFICATION_RESIZED); + BIND_CONSTANT(NOTIFICATION_MOUSE_ENTER); + BIND_CONSTANT(NOTIFICATION_MOUSE_EXIT); + BIND_CONSTANT(NOTIFICATION_FOCUS_ENTER); + BIND_CONSTANT(NOTIFICATION_FOCUS_EXIT); + BIND_CONSTANT(NOTIFICATION_THEME_CHANGED); + BIND_CONSTANT(NOTIFICATION_MODAL_CLOSE); + + BIND_CONSTANT(CURSOR_ARROW); + BIND_CONSTANT(CURSOR_IBEAM); + BIND_CONSTANT(CURSOR_POINTING_HAND); + BIND_CONSTANT(CURSOR_CROSS); + BIND_CONSTANT(CURSOR_WAIT); + BIND_CONSTANT(CURSOR_BUSY); + BIND_CONSTANT(CURSOR_DRAG); + BIND_CONSTANT(CURSOR_CAN_DROP); + BIND_CONSTANT(CURSOR_FORBIDDEN); + BIND_CONSTANT(CURSOR_VSIZE); + BIND_CONSTANT(CURSOR_HSIZE); + BIND_CONSTANT(CURSOR_BDIAGSIZE); + BIND_CONSTANT(CURSOR_FDIAGSIZE); + BIND_CONSTANT(CURSOR_MOVE); + BIND_CONSTANT(CURSOR_VSPLIT); + BIND_CONSTANT(CURSOR_HSPLIT); + BIND_CONSTANT(CURSOR_HELP); + + BIND_CONSTANT(SIZE_EXPAND); + BIND_CONSTANT(SIZE_FILL); + BIND_CONSTANT(SIZE_EXPAND_FILL); + + BIND_CONSTANT(MOUSE_FILTER_STOP); + BIND_CONSTANT(MOUSE_FILTER_PASS); + BIND_CONSTANT(MOUSE_FILTER_IGNORE); + + ADD_SIGNAL(MethodInfo("resized")); + ADD_SIGNAL(MethodInfo("gui_input", PropertyInfo(Variant::INPUT_EVENT, "ev"))); + ADD_SIGNAL(MethodInfo("mouse_entered")); + ADD_SIGNAL(MethodInfo("mouse_exited")); + ADD_SIGNAL(MethodInfo("focus_entered")); + ADD_SIGNAL(MethodInfo("focus_exited")); + ADD_SIGNAL(MethodInfo("size_flags_changed")); + ADD_SIGNAL(MethodInfo("minimum_size_changed")); + ADD_SIGNAL(MethodInfo("modal_closed")); } Control::Control() { - data.parent=NULL; + data.parent = NULL; - data.mouse_filter=MOUSE_FILTER_STOP; + data.mouse_filter = MOUSE_FILTER_STOP; - data.SI=NULL; - data.MI=NULL; - data.RI=NULL; - data.modal=false; - data.theme_owner=NULL; - data.modal_exclusive=false; + data.SI = NULL; + data.MI = NULL; + data.RI = NULL; + data.modal = false; + data.theme_owner = NULL; + data.modal_exclusive = false; data.default_cursor = CURSOR_ARROW; - data.h_size_flags=SIZE_FILL; - data.v_size_flags=SIZE_FILL; - data.expand=1; - data.pending_min_size_update=false; - data.rotation=0; - data.parent_canvas_item=NULL; - data.scale=Vector2(1,1); - data.drag_owner=0; - data.modal_frame=0; - data.block_minimum_size_adjust=false; - data.disable_visibility_clip=false; - - data.clip_contents=false; - for (int i=0;i<4;i++) { - data.anchor[i]=ANCHOR_BEGIN; - data.margin[i]=0; - } - data.focus_mode=FOCUS_NONE; - data.modal_prev_focus_owner=0; - - - - - -} - - -Control::~Control() -{ + data.h_size_flags = SIZE_FILL; + data.v_size_flags = SIZE_FILL; + data.expand = 1; + data.pending_min_size_update = false; + data.rotation = 0; + data.parent_canvas_item = NULL; + data.scale = Vector2(1, 1); + data.drag_owner = 0; + data.modal_frame = 0; + data.block_minimum_size_adjust = false; + data.disable_visibility_clip = false; + + data.clip_contents = false; + for (int i = 0; i < 4; i++) { + data.anchor[i] = ANCHOR_BEGIN; + data.margin[i] = 0; + } + data.focus_mode = FOCUS_NONE; + data.modal_prev_focus_owner = 0; +} + +Control::~Control() { } diff --git a/scene/gui/control.h b/scene/gui/control.h index 68795b054c..a18567123b 100644 --- a/scene/gui/control.h +++ b/scene/gui/control.h @@ -29,13 +29,13 @@ #ifndef CONTROL_H #define CONTROL_H -#include "scene/main/node.h" -#include "scene/resources/theme.h" -#include "scene/main/timer.h" -#include "scene/2d/canvas_item.h" #include "math_2d.h" #include "rid.h" +#include "scene/2d/canvas_item.h" #include "scene/gui/input_action.h" +#include "scene/main/node.h" +#include "scene/main/timer.h" +#include "scene/resources/theme.h" /** @author Juan Linietsky <reduzio@gmail.com> */ @@ -46,11 +46,10 @@ class Panel; class Control : public CanvasItem { - GDCLASS( Control, CanvasItem ); + GDCLASS(Control, CanvasItem); OBJ_CATEGORY("GUI Nodes"); public: - enum AnchorType { ANCHOR_BEGIN, ANCHOR_END, @@ -65,9 +64,9 @@ public: enum SizeFlags { - SIZE_FILL=1, - SIZE_EXPAND=2, - SIZE_EXPAND_FILL=SIZE_EXPAND|SIZE_FILL + SIZE_FILL = 1, + SIZE_EXPAND = 2, + SIZE_EXPAND_FILL = SIZE_EXPAND | SIZE_FILL }; @@ -99,11 +98,10 @@ public: }; private: - struct CComparator { - bool operator()(const Control* p_a, const Control* p_b) const { - if (p_a->get_canvas_layer()==p_b->get_canvas_layer()) + bool operator()(const Control *p_a, const Control *p_b) const { + if (p_a->get_canvas_layer() == p_b->get_canvas_layer()) return p_b->is_greater_than(p_a); else return p_a->get_canvas_layer() < p_b->get_canvas_layer(); @@ -147,9 +145,9 @@ private: String tooltip; CursorShape default_cursor; - List<Control*>::Element *MI; //modal item - List<Control*>::Element *SI; - List<Control*>::Element *RI; + List<Control *>::Element *MI; //modal item + List<Control *>::Element *SI; + List<Control *>::Element *RI; CanvasItem *parent_canvas_item; @@ -157,39 +155,36 @@ private: NodePath focus_neighbour[4]; - HashMap<StringName, Ref<Texture>, StringNameHasher > icon_override; - HashMap<StringName, Ref<Shader>, StringNameHasher > shader_override; - HashMap<StringName, Ref<StyleBox>, StringNameHasher > style_override; - HashMap<StringName, Ref<Font>, StringNameHasher > font_override; - HashMap<StringName, Color, StringNameHasher > color_override; - HashMap<StringName, int, StringNameHasher > constant_override; - Map< Ref<Font>, int> font_refcount; + HashMap<StringName, Ref<Texture>, StringNameHasher> icon_override; + HashMap<StringName, Ref<Shader>, StringNameHasher> shader_override; + HashMap<StringName, Ref<StyleBox>, StringNameHasher> style_override; + HashMap<StringName, Ref<Font>, StringNameHasher> font_override; + HashMap<StringName, Color, StringNameHasher> color_override; + HashMap<StringName, int, StringNameHasher> constant_override; + Map<Ref<Font>, int> font_refcount; } data; // used internally - Control* _find_control_at_pos(CanvasItem* p_node,const Point2& p_pos,const Transform2D& p_xform,Transform2D& r_inv_xform); - - - void _window_find_focus_neighbour(const Vector2& p_dir, Node *p_at, const Point2* p_points ,float p_min,float &r_closest_dist,Control **r_closest); - Control *_get_focus_neighbour(Margin p_margin,int p_count=0); + Control *_find_control_at_pos(CanvasItem *p_node, const Point2 &p_pos, const Transform2D &p_xform, Transform2D &r_inv_xform); + void _window_find_focus_neighbour(const Vector2 &p_dir, Node *p_at, const Point2 *p_points, float p_min, float &r_closest_dist, Control **r_closest); + Control *_get_focus_neighbour(Margin p_margin, int p_count = 0); - void _set_anchor(Margin p_margin,AnchorType p_anchor); + void _set_anchor(Margin p_margin, AnchorType p_anchor); float _get_parent_range(int p_idx) const; float _get_range(int p_idx) const; - float _s2a(float p_val, AnchorType p_anchor,float p_range) const; - float _a2s(float p_val, AnchorType p_anchor,float p_range) const; - void _propagate_theme_changed(CanvasItem *p_at, Control *p_owner, bool p_assign=true); + float _s2a(float p_val, AnchorType p_anchor, float p_range) const; + float _a2s(float p_val, AnchorType p_anchor, float p_range) const; + void _propagate_theme_changed(CanvasItem *p_at, Control *p_owner, bool p_assign = true); void _theme_changed(); - void _change_notify_margins(); void _update_minimum_size(); void _update_scroll(); - void _resize(const Size2& p_size); + void _resize(const Size2 &p_size); void _size_changed(); String _get_tooltip() const; @@ -199,70 +194,65 @@ private: float _get_rotation_deg() const; void _ref_font(Ref<Font> p_sc); - void _unref_font( Ref<Font> p_sc); + void _unref_font(Ref<Font> p_sc); void _font_changed(); void _update_canvas_item_transform(); - -friend class Viewport; + friend class Viewport; void _modal_stack_remove(); void _modal_set_prev_focus_owner(ObjectID p_prev); protected: - virtual void add_child_notify(Node *p_child); virtual void remove_child_notify(Node *p_child); //virtual void _window_gui_input(InputEvent p_event); - bool _set(const StringName& p_name, const Variant& p_value); - bool _get(const StringName& p_name,Variant &r_ret) const; - void _get_property_list( List<PropertyInfo> *p_list) const; + bool _set(const StringName &p_name, const Variant &p_value); + bool _get(const StringName &p_name, Variant &r_ret) const; + void _get_property_list(List<PropertyInfo> *p_list) const; void _notification(int p_notification); - static void _bind_methods(); //bind helpers public: - enum { -/* NOTIFICATION_DRAW=30, + /* NOTIFICATION_DRAW=30, NOTIFICATION_VISIBILITY_CHANGED=38*/ - NOTIFICATION_RESIZED=40, - NOTIFICATION_MOUSE_ENTER=41, - NOTIFICATION_MOUSE_EXIT=42, - NOTIFICATION_FOCUS_ENTER=43, - NOTIFICATION_FOCUS_EXIT=44, - NOTIFICATION_THEME_CHANGED=45, - NOTIFICATION_MODAL_CLOSE=46, - + NOTIFICATION_RESIZED = 40, + NOTIFICATION_MOUSE_ENTER = 41, + NOTIFICATION_MOUSE_EXIT = 42, + NOTIFICATION_FOCUS_ENTER = 43, + NOTIFICATION_FOCUS_EXIT = 44, + NOTIFICATION_THEME_CHANGED = 45, + NOTIFICATION_MODAL_CLOSE = 46, }; virtual Variant edit_get_state() const; - virtual void edit_set_state(const Variant& p_state); - virtual void edit_set_rect(const Rect2& p_edit_rect); + virtual void edit_set_state(const Variant &p_state); + virtual void edit_set_rect(const Rect2 &p_edit_rect); virtual Size2 edit_get_minimum_size() const; void accept_event(); virtual Size2 get_minimum_size() const; virtual Size2 get_combined_minimum_size() const; - virtual bool has_point(const Point2& p_point) const; + virtual bool has_point(const Point2 &p_point) const; virtual bool clips_input() const; - virtual void set_drag_forwarding(Control* p_target); - virtual Variant get_drag_data(const Point2& p_point); - virtual bool can_drop_data(const Point2& p_point,const Variant& p_data) const; - virtual void drop_data(const Point2& p_point,const Variant& p_data); + virtual void set_drag_forwarding(Control *p_target); + virtual Variant get_drag_data(const Point2 &p_point); + virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const; + virtual void drop_data(const Point2 &p_point, const Variant &p_data); void set_drag_preview(Control *p_control); - void force_drag(const Variant& p_data,Control *p_control); + void force_drag(const Variant &p_data, Control *p_control); - void set_custom_minimum_size(const Size2& p_custom); + void set_custom_minimum_size(const Size2 &p_custom); Size2 get_custom_minimum_size() const; bool is_window_modal_on_top() const; @@ -270,29 +260,25 @@ public: Control *get_parent_control() const; - - /* POSITIONING */ - void set_anchor(Margin p_margin,AnchorType p_anchor, bool p_keep_margin=false); - void set_anchor_and_margin(Margin p_margin,AnchorType p_anchor, float p_pos); + void set_anchor(Margin p_margin, AnchorType p_anchor, bool p_keep_margin = false); + void set_anchor_and_margin(Margin p_margin, AnchorType p_anchor, float p_pos); AnchorType get_anchor(Margin p_margin) const; - void set_margin(Margin p_margin,float p_value); - - void set_begin(const Point2& p_point); // helper - void set_end(const Point2& p_point); // helper - + void set_margin(Margin p_margin, float p_value); + void set_begin(const Point2 &p_point); // helper + void set_end(const Point2 &p_point); // helper float get_margin(Margin p_margin) const; Point2 get_begin() const; Point2 get_end() const; - void set_pos(const Point2& p_point); - void set_size(const Size2& p_size); - void set_global_pos(const Point2& p_point); + void set_pos(const Point2 &p_point); + void set_size(const Size2 &p_size); + void set_global_pos(const Point2 &p_point); Point2 get_pos() const; Point2 get_global_pos() const; @@ -306,15 +292,14 @@ public: float get_rotation() const; float get_rotation_deg() const; - void set_scale(const Vector2& p_scale); + void set_scale(const Vector2 &p_scale); Vector2 get_scale() const; + void set_area_as_parent_rect(int p_margin = 0); - void set_area_as_parent_rect(int p_margin=0); - - void show_modal(bool p_exclusive=false); + void show_modal(bool p_exclusive = false); - void set_theme(const Ref<Theme>& p_theme); + void set_theme(const Ref<Theme> &p_theme); Ref<Theme> get_theme() const; void set_h_size_flags(int p_flags); @@ -349,44 +334,44 @@ public: /* SKINNING */ - void add_icon_override(const StringName& p_name, const Ref<Texture>& p_icon); - void add_shader_override(const StringName& p_name, const Ref<Shader>& p_shader); - void add_style_override(const StringName& p_name, const Ref<StyleBox>& p_style); - void add_font_override(const StringName& p_name, const Ref<Font>& p_font); - void add_color_override(const StringName& p_name, const Color& p_color); - void add_constant_override(const StringName& p_name, int p_constant); - - Ref<Texture> get_icon(const StringName& p_name,const StringName& p_type=StringName()) const; - Ref<Shader> get_shader(const StringName &p_name, const StringName &p_type=StringName()) const; - Ref<StyleBox> get_stylebox(const StringName& p_name,const StringName& p_type=StringName()) const; - Ref<Font> get_font(const StringName& p_name,const StringName& p_type=StringName()) const; - Color get_color(const StringName& p_name,const StringName& p_type=StringName()) const; - int get_constant(const StringName& p_name,const StringName& p_type=StringName()) const; - - bool has_icon_override(const StringName& p_name) const; - bool has_shader_override(const StringName& p_name) const; - bool has_stylebox_override(const StringName& p_name) const; - bool has_font_override(const StringName& p_name) const; - bool has_color_override(const StringName& p_name) const; - bool has_constant_override(const StringName& p_name) const; - - bool has_icon(const StringName& p_name,const StringName& p_type=StringName()) const; - bool has_shader(const StringName& p_name,const StringName& p_type=StringName()) const; - bool has_stylebox(const StringName& p_name,const StringName& p_type=StringName()) const; - bool has_font(const StringName& p_name,const StringName& p_type=StringName()) const; - bool has_color(const StringName& p_name,const StringName& p_type=StringName()) const; - bool has_constant(const StringName& p_name,const StringName& p_type=StringName()) const; + void add_icon_override(const StringName &p_name, const Ref<Texture> &p_icon); + void add_shader_override(const StringName &p_name, const Ref<Shader> &p_shader); + void add_style_override(const StringName &p_name, const Ref<StyleBox> &p_style); + void add_font_override(const StringName &p_name, const Ref<Font> &p_font); + void add_color_override(const StringName &p_name, const Color &p_color); + void add_constant_override(const StringName &p_name, int p_constant); + + Ref<Texture> get_icon(const StringName &p_name, const StringName &p_type = StringName()) const; + Ref<Shader> get_shader(const StringName &p_name, const StringName &p_type = StringName()) const; + Ref<StyleBox> get_stylebox(const StringName &p_name, const StringName &p_type = StringName()) const; + Ref<Font> get_font(const StringName &p_name, const StringName &p_type = StringName()) const; + Color get_color(const StringName &p_name, const StringName &p_type = StringName()) const; + int get_constant(const StringName &p_name, const StringName &p_type = StringName()) const; + + bool has_icon_override(const StringName &p_name) const; + bool has_shader_override(const StringName &p_name) const; + bool has_stylebox_override(const StringName &p_name) const; + bool has_font_override(const StringName &p_name) const; + bool has_color_override(const StringName &p_name) const; + bool has_constant_override(const StringName &p_name) const; + + bool has_icon(const StringName &p_name, const StringName &p_type = StringName()) const; + bool has_shader(const StringName &p_name, const StringName &p_type = StringName()) const; + bool has_stylebox(const StringName &p_name, const StringName &p_type = StringName()) const; + bool has_font(const StringName &p_name, const StringName &p_type = StringName()) const; + bool has_color(const StringName &p_name, const StringName &p_type = StringName()) const; + bool has_constant(const StringName &p_name, const StringName &p_type = StringName()) const; /* TOOLTIP */ - void set_tooltip(const String& p_tooltip); - virtual String get_tooltip(const Point2& p_pos) const; + void set_tooltip(const String &p_tooltip); + virtual String get_tooltip(const Point2 &p_pos) const; /* CURSOR */ void set_default_cursor_shape(CursorShape p_shape); CursorShape get_default_cursor_shape() const; - virtual CursorShape get_cursor_shape(const Point2& p_pos=Point2i()) const; + virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const; virtual Rect2 get_item_rect() const; virtual Transform2D get_transform() const; @@ -397,7 +382,7 @@ public: void grab_click_focus(); - void warp_mouse(const Point2& p_to_pos); + void warp_mouse(const Point2 &p_to_pos); virtual bool is_text_field() const; @@ -412,11 +397,10 @@ public: void set_disable_visibility_clip(bool p_ignore); bool is_visibility_clip_disabled() const; - virtual void get_argument_options(const StringName& p_function,int p_idx,List<String>*r_options) const; + virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const; Control(); ~Control(); - }; VARIANT_ENUM_CAST(Control::AnchorType); diff --git a/scene/gui/dialogs.cpp b/scene/gui/dialogs.cpp index 6d06f8c59c..d081eda86d 100644 --- a/scene/gui/dialogs.cpp +++ b/scene/gui/dialogs.cpp @@ -27,8 +27,8 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "dialogs.h" -#include "print_string.h" #include "line_edit.h" +#include "print_string.h" #include "translation.h" void WindowDialog::_post_popup() { @@ -44,7 +44,7 @@ void WindowDialog::_fix_size() { Point2i pos = get_global_pos(); Size2i size = get_size(); Size2i viewport_size = get_viewport_rect().size; - + // Windows require additional padding to keep the window chrome visible. Ref<StyleBox> panel = get_stylebox("panel", "WindowDialog"); float top = panel->get_margin(MARGIN_TOP); @@ -64,7 +64,7 @@ void WindowDialog::_fix_size() { } } -bool WindowDialog::has_point(const Point2& p_point) const { +bool WindowDialog::has_point(const Point2 &p_point) const { Rect2 r(Point2(), get_size()); @@ -85,7 +85,7 @@ bool WindowDialog::has_point(const Point2& p_point) const { return r.has_point(p_point); } -void WindowDialog::_gui_input(const InputEvent& p_event) { +void WindowDialog::_gui_input(const InputEvent &p_event) { if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index == BUTTON_LEFT) { @@ -94,7 +94,7 @@ void WindowDialog::_gui_input(const InputEvent& p_event) { drag_type = _drag_hit_test(Point2(p_event.mouse_button.x, p_event.mouse_button.y)); if (drag_type != DRAG_NONE) drag_offset = get_global_mouse_pos() - get_pos(); - drag_offset_far = get_pos() + get_size() - get_global_mouse_pos(); + drag_offset_far = get_pos() + get_size() - get_global_mouse_pos(); } else if (drag_type != DRAG_NONE && !p_event.mouse_button.pressed) { // End a dragging operation. drag_type = DRAG_NONE; @@ -208,13 +208,13 @@ void WindowDialog::_closed() { hide(); } -int WindowDialog::_drag_hit_test(const Point2& pos) const { +int WindowDialog::_drag_hit_test(const Point2 &pos) const { int drag_type = DRAG_NONE; if (resizable) { int titlebar_height = get_constant("titlebar_height", "WindowDialog"); int scaleborder_size = get_constant("scaleborder_size", "WindowDialog"); - + Rect2 rect = get_rect(); if (pos.y < (-titlebar_height + scaleborder_size)) @@ -233,9 +233,9 @@ int WindowDialog::_drag_hit_test(const Point2& pos) const { return drag_type; } -void WindowDialog::set_title(const String& p_title) { +void WindowDialog::set_title(const String &p_title) { - title=XL_MESSAGE(p_title); + title = XL_MESSAGE(p_title); update(); } String WindowDialog::get_title() const { @@ -250,36 +250,32 @@ bool WindowDialog::get_resizable() const { return resizable; } - Size2 WindowDialog::get_minimum_size() const { - Ref<Font> font = get_font("title_font","WindowDialog"); - int msx=close_button->get_combined_minimum_size().x; - msx+=font->get_string_size(title).x; + Ref<Font> font = get_font("title_font", "WindowDialog"); + int msx = close_button->get_combined_minimum_size().x; + msx += font->get_string_size(title).x; - return Size2(msx,1); + return Size2(msx, 1); } - - TextureButton *WindowDialog::get_close_button() { - return close_button; } void WindowDialog::_bind_methods() { - ClassDB::bind_method( D_METHOD("_gui_input"),&WindowDialog::_gui_input); - ClassDB::bind_method( D_METHOD("set_title","title"),&WindowDialog::set_title); - ClassDB::bind_method( D_METHOD("get_title"),&WindowDialog::get_title); - ClassDB::bind_method( D_METHOD("set_resizable","resizable"),&WindowDialog::set_resizable); - ClassDB::bind_method( D_METHOD("get_resizable"), &WindowDialog::get_resizable); - ClassDB::bind_method( D_METHOD("_closed"),&WindowDialog::_closed); - ClassDB::bind_method( D_METHOD("get_close_button:TextureButton"),&WindowDialog::get_close_button); + ClassDB::bind_method(D_METHOD("_gui_input"), &WindowDialog::_gui_input); + ClassDB::bind_method(D_METHOD("set_title", "title"), &WindowDialog::set_title); + ClassDB::bind_method(D_METHOD("get_title"), &WindowDialog::get_title); + ClassDB::bind_method(D_METHOD("set_resizable", "resizable"), &WindowDialog::set_resizable); + ClassDB::bind_method(D_METHOD("get_resizable"), &WindowDialog::get_resizable); + ClassDB::bind_method(D_METHOD("_closed"), &WindowDialog::_closed); + ClassDB::bind_method(D_METHOD("get_close_button:TextureButton"), &WindowDialog::get_close_button); - ADD_PROPERTY( PropertyInfo(Variant::STRING,"window_title",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_DEFAULT_INTL),"set_title","get_title"); - ADD_PROPERTY( PropertyInfo(Variant::BOOL,"resizable",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_DEFAULT_INTL),"set_resizable","get_resizable"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "window_title", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT_INTL), "set_title", "get_title"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resizable", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT_INTL), "set_resizable", "get_resizable"); } WindowDialog::WindowDialog() { @@ -290,57 +286,46 @@ WindowDialog::WindowDialog() { close_button = memnew(TextureButton); add_child(close_button); close_button->connect("pressed", this, "_closed"); - } -WindowDialog::~WindowDialog(){ - - +WindowDialog::~WindowDialog() { } - void PopupDialog::_notification(int p_what) { - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { RID ci = get_canvas_item(); - get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size())); + get_stylebox("panel", "PopupMenu")->draw(ci, Rect2(Point2(), get_size())); } } PopupDialog::PopupDialog() { - - } PopupDialog::~PopupDialog() { - - } - // AcceptDialog - void AcceptDialog::_post_popup() { WindowDialog::_post_popup(); get_ok()->grab_focus(); - } void AcceptDialog::_notification(int p_what) { - if (p_what==NOTIFICATION_MODAL_CLOSE) { + if (p_what == NOTIFICATION_MODAL_CLOSE) { cancel_pressed(); - } else if (p_what==NOTIFICATION_RESIZED) { + } else if (p_what == NOTIFICATION_RESIZED) { _update_child_rects(); } } -void AcceptDialog::_builtin_text_entered(const String& p_text) { +void AcceptDialog::_builtin_text_entered(const String &p_text) { _ok_pressed(); } @@ -351,7 +336,6 @@ void AcceptDialog::_ok_pressed() { hide(); ok_pressed(); emit_signal("confirmed"); - } void AcceptDialog::_close_pressed() { @@ -371,7 +355,7 @@ void AcceptDialog::set_text(String p_text) { void AcceptDialog::set_hide_on_ok(bool p_hide) { - hide_on_ok=p_hide; + hide_on_ok = p_hide; } bool AcceptDialog::get_hide_on_ok() const { @@ -379,91 +363,82 @@ bool AcceptDialog::get_hide_on_ok() const { return hide_on_ok; } - void AcceptDialog::register_text_enter(Node *p_line_edit) { ERR_FAIL_NULL(p_line_edit); - p_line_edit->connect("text_entered", this,"_builtin_text_entered"); + p_line_edit->connect("text_entered", this, "_builtin_text_entered"); } void AcceptDialog::_update_child_rects() { - - Size2 label_size=label->get_minimum_size(); + Size2 label_size = label->get_minimum_size(); if (label->get_text().empty()) { label_size.height = 0; } - int margin = get_constant("margin","Dialogs"); + int margin = get_constant("margin", "Dialogs"); Size2 size = get_size(); Size2 hminsize = hbc->get_combined_minimum_size(); - Vector2 cpos(margin,margin+label_size.height); - Vector2 csize(size.x-margin*2,size.y-margin*3-hminsize.y-label_size.height); + Vector2 cpos(margin, margin + label_size.height); + Vector2 csize(size.x - margin * 2, size.y - margin * 3 - hminsize.y - label_size.height); - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { Control *c = get_child(i)->cast_to<Control>(); if (!c) continue; - if (c==hbc || c==label || c==get_close_button() || c->is_set_as_toplevel()) + if (c == hbc || c == label || c == get_close_button() || c->is_set_as_toplevel()) continue; c->set_pos(cpos); c->set_size(csize); - } - cpos.y+=csize.y+margin; - csize.y=hminsize.y; + cpos.y += csize.y + margin; + csize.y = hminsize.y; hbc->set_pos(cpos); hbc->set_size(csize); - } Size2 AcceptDialog::get_minimum_size() const { - int margin = get_constant("margin","Dialogs"); + int margin = get_constant("margin", "Dialogs"); Size2 minsize = label->get_combined_minimum_size(); - - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { Control *c = get_child(i)->cast_to<Control>(); if (!c) continue; - if (c==hbc || c==label || c==const_cast<AcceptDialog*>(this)->get_close_button() || c->is_set_as_toplevel()) + if (c == hbc || c == label || c == const_cast<AcceptDialog *>(this)->get_close_button() || c->is_set_as_toplevel()) continue; Size2 cminsize = c->get_combined_minimum_size(); - minsize.x=MAX(cminsize.x,minsize.x); - minsize.y=MAX(cminsize.y,minsize.y); - + minsize.x = MAX(cminsize.x, minsize.x); + minsize.y = MAX(cminsize.y, minsize.y); } - Size2 hminsize = hbc->get_combined_minimum_size(); - minsize.x = MAX(hminsize.x,minsize.x); - minsize.y+=hminsize.y; - minsize.x+=margin*2; - minsize.y+=margin*3; //one as separation between hbc and child + minsize.x = MAX(hminsize.x, minsize.x); + minsize.y += hminsize.y; + minsize.x += margin * 2; + minsize.y += margin * 3; //one as separation between hbc and child Size2 wmsize = WindowDialog::get_minimum_size(); - minsize.x=MAX(wmsize.x,minsize.x); + minsize.x = MAX(wmsize.x, minsize.x); return minsize; } +void AcceptDialog::_custom_action(const String &p_action) { -void AcceptDialog::_custom_action(const String& p_action) { - - emit_signal("custom_action",p_action); + emit_signal("custom_action", p_action); custom_action(p_action); } -Button* AcceptDialog::add_button(const String& p_text,bool p_right,const String& p_action) { - +Button *AcceptDialog::add_button(const String &p_text, bool p_right, const String &p_action) { - Button *button = memnew( Button ); + Button *button = memnew(Button); button->set_text(p_text); if (p_right) { hbc->add_child(button); @@ -471,98 +446,91 @@ Button* AcceptDialog::add_button(const String& p_text,bool p_right,const String& } else { hbc->add_child(button); - hbc->move_child(button,0); + hbc->move_child(button, 0); hbc->add_spacer(true); } - if (p_action!="") { - button->connect("pressed",this,"_custom_action",varray(p_action)); + if (p_action != "") { + button->connect("pressed", this, "_custom_action", varray(p_action)); } return button; } -Button* AcceptDialog::add_cancel(const String &p_cancel) { +Button *AcceptDialog::add_cancel(const String &p_cancel) { String c = p_cancel; - if (p_cancel=="") - c=RTR("Cancel"); - Button *b = swap_ok_cancel ? add_button(c,true) : add_button(c); - b->connect("pressed",this,"_closed"); + if (p_cancel == "") + c = RTR("Cancel"); + Button *b = swap_ok_cancel ? add_button(c, true) : add_button(c); + b->connect("pressed", this, "_closed"); return b; } void AcceptDialog::_bind_methods() { - ClassDB::bind_method(D_METHOD("_ok"),&AcceptDialog::_ok_pressed); - ClassDB::bind_method(D_METHOD("get_ok"),&AcceptDialog::get_ok); - ClassDB::bind_method(D_METHOD("get_label"),&AcceptDialog::get_label); - ClassDB::bind_method(D_METHOD("set_hide_on_ok","enabled"),&AcceptDialog::set_hide_on_ok); - ClassDB::bind_method(D_METHOD("get_hide_on_ok"),&AcceptDialog::get_hide_on_ok); - ClassDB::bind_method(D_METHOD("add_button:Button","text","right","action"),&AcceptDialog::add_button,DEFVAL(false),DEFVAL("")); - ClassDB::bind_method(D_METHOD("add_cancel:Button","name"),&AcceptDialog::add_cancel); - ClassDB::bind_method(D_METHOD("_builtin_text_entered"),&AcceptDialog::_builtin_text_entered); - ClassDB::bind_method(D_METHOD("register_text_enter:LineEdit","line_edit"),&AcceptDialog::register_text_enter); - ClassDB::bind_method(D_METHOD("_custom_action"),&AcceptDialog::_custom_action); - ClassDB::bind_method(D_METHOD("set_text","text"),&AcceptDialog::set_text); - ClassDB::bind_method(D_METHOD("get_text"),&AcceptDialog::get_text); + ClassDB::bind_method(D_METHOD("_ok"), &AcceptDialog::_ok_pressed); + ClassDB::bind_method(D_METHOD("get_ok"), &AcceptDialog::get_ok); + ClassDB::bind_method(D_METHOD("get_label"), &AcceptDialog::get_label); + ClassDB::bind_method(D_METHOD("set_hide_on_ok", "enabled"), &AcceptDialog::set_hide_on_ok); + ClassDB::bind_method(D_METHOD("get_hide_on_ok"), &AcceptDialog::get_hide_on_ok); + ClassDB::bind_method(D_METHOD("add_button:Button", "text", "right", "action"), &AcceptDialog::add_button, DEFVAL(false), DEFVAL("")); + ClassDB::bind_method(D_METHOD("add_cancel:Button", "name"), &AcceptDialog::add_cancel); + ClassDB::bind_method(D_METHOD("_builtin_text_entered"), &AcceptDialog::_builtin_text_entered); + ClassDB::bind_method(D_METHOD("register_text_enter:LineEdit", "line_edit"), &AcceptDialog::register_text_enter); + ClassDB::bind_method(D_METHOD("_custom_action"), &AcceptDialog::_custom_action); + ClassDB::bind_method(D_METHOD("set_text", "text"), &AcceptDialog::set_text); + ClassDB::bind_method(D_METHOD("get_text"), &AcceptDialog::get_text); - ADD_SIGNAL( MethodInfo("confirmed") ); - ADD_SIGNAL( MethodInfo("custom_action",PropertyInfo(Variant::STRING,"action")) ); - - ADD_GROUP("Dialog","dialog"); - ADD_PROPERTYNZ( PropertyInfo(Variant::STRING,"dialog_text",PROPERTY_HINT_MULTILINE_TEXT,"",PROPERTY_USAGE_DEFAULT_INTL),"set_text","get_text"); - ADD_PROPERTY( PropertyInfo(Variant::BOOL, "dialog_hide_on_ok"),"set_hide_on_ok","get_hide_on_ok") ; + ADD_SIGNAL(MethodInfo("confirmed")); + ADD_SIGNAL(MethodInfo("custom_action", PropertyInfo(Variant::STRING, "action"))); + ADD_GROUP("Dialog", "dialog"); + ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "dialog_text", PROPERTY_HINT_MULTILINE_TEXT, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dialog_hide_on_ok"), "set_hide_on_ok", "get_hide_on_ok"); } - -bool AcceptDialog::swap_ok_cancel=false; +bool AcceptDialog::swap_ok_cancel = false; void AcceptDialog::set_swap_ok_cancel(bool p_swap) { - swap_ok_cancel=p_swap; + swap_ok_cancel = p_swap; } AcceptDialog::AcceptDialog() { - int margin = get_constant("margin","Dialogs"); - int button_margin = get_constant("button_margin","Dialogs"); + int margin = get_constant("margin", "Dialogs"); + int button_margin = get_constant("button_margin", "Dialogs"); - - label = memnew( Label ); - label->set_anchor(MARGIN_RIGHT,ANCHOR_END); - label->set_anchor(MARGIN_BOTTOM,ANCHOR_END); - label->set_begin( Point2( margin, margin) ); - label->set_end( Point2( margin, button_margin+10) ); + label = memnew(Label); + label->set_anchor(MARGIN_RIGHT, ANCHOR_END); + label->set_anchor(MARGIN_BOTTOM, ANCHOR_END); + label->set_begin(Point2(margin, margin)); + label->set_end(Point2(margin, button_margin + 10)); //label->set_autowrap(true); add_child(label); - hbc = memnew( HBoxContainer ); + hbc = memnew(HBoxContainer); add_child(hbc); hbc->add_spacer(); - ok = memnew( Button ); + ok = memnew(Button); ok->set_text(RTR("OK")); hbc->add_child(ok); hbc->add_spacer(); - - ok->connect("pressed", this,"_ok"); + ok->connect("pressed", this, "_ok"); set_as_toplevel(true); - hide_on_ok=true; + hide_on_ok = true; set_title(RTR("Alert!")); } - -AcceptDialog::~AcceptDialog() -{ +AcceptDialog::~AcceptDialog() { } - void ConfirmationDialog::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_cancel:Button"),&ConfirmationDialog::get_cancel); + ClassDB::bind_method(D_METHOD("get_cancel:Button"), &ConfirmationDialog::get_cancel); } Button *ConfirmationDialog::get_cancel() { diff --git a/scene/gui/dialogs.h b/scene/gui/dialogs.h index dd75b76c8e..a729a8f907 100644 --- a/scene/gui/dialogs.h +++ b/scene/gui/dialogs.h @@ -29,20 +29,19 @@ #ifndef DIALOGS_H #define DIALOGS_H -#include "scene/gui/label.h" +#include "box_container.h" #include "scene/gui/button.h" -#include "scene/gui/texture_button.h" +#include "scene/gui/label.h" #include "scene/gui/panel.h" #include "scene/gui/popup.h" -#include "box_container.h" +#include "scene/gui/texture_button.h" /** @author Juan Linietsky <reduzio@gmail.com> */ - class WindowDialog : public Popup { - GDCLASS(WindowDialog,Popup); + GDCLASS(WindowDialog, Popup); enum DRAG_TYPE { DRAG_NONE = 0, @@ -60,22 +59,22 @@ class WindowDialog : public Popup { Point2 drag_offset_far; bool resizable; - void _gui_input(const InputEvent& p_event); + void _gui_input(const InputEvent &p_event); void _closed(); - int _drag_hit_test(const Point2& pos) const; + int _drag_hit_test(const Point2 &pos) const; protected: virtual void _post_popup(); virtual void _fix_size(); - virtual void _close_pressed() {} - virtual bool has_point(const Point2& p_point) const; + virtual void _close_pressed() {} + virtual bool has_point(const Point2 &p_point) const; void _notification(int p_what); static void _bind_methods(); -public: +public: TextureButton *get_close_button(); - void set_title(const String& p_title); + void set_title(const String &p_title); String get_title() const; void set_resizable(bool p_resizable); bool get_resizable() const; @@ -84,28 +83,25 @@ public: WindowDialog(); ~WindowDialog(); - }; class PopupDialog : public Popup { - GDCLASS(PopupDialog,Popup); + GDCLASS(PopupDialog, Popup); protected: void _notification(int p_what); -public: +public: PopupDialog(); ~PopupDialog(); - }; - class LineEdit; class AcceptDialog : public WindowDialog { - GDCLASS(AcceptDialog,WindowDialog); + GDCLASS(AcceptDialog, WindowDialog); HBoxContainer *hbc; Label *label; @@ -113,39 +109,33 @@ class AcceptDialog : public WindowDialog { //Button *cancel; no more cancel (there is X on tht titlebar) bool hide_on_ok; - - void _custom_action(const String& p_action); + void _custom_action(const String &p_action); void _ok_pressed(); void _close_pressed(); - void _builtin_text_entered(const String& p_text); + void _builtin_text_entered(const String &p_text); void _update_child_rects(); static bool swap_ok_cancel; - - protected: - virtual void _post_popup(); void _notification(int p_what); static void _bind_methods(); virtual void ok_pressed() {} virtual void cancel_pressed() {} - virtual void custom_action(const String&) {} -public: + virtual void custom_action(const String &) {} +public: Size2 get_minimum_size() const; Label *get_label() { return label; } static void set_swap_ok_cancel(bool p_swap); - void register_text_enter(Node *p_line_edit); Button *get_ok() { return ok; } - Button* add_button(const String& p_text,bool p_right=false,const String& p_action=""); - Button* add_cancel(const String &p_cancel=""); - + Button *add_button(const String &p_text, bool p_right = false, const String &p_action = ""); + Button *add_cancel(const String &p_cancel = ""); void set_hide_on_ok(bool p_hide); bool get_hide_on_ok() const; @@ -155,21 +145,19 @@ public: AcceptDialog(); ~AcceptDialog(); - }; - class ConfirmationDialog : public AcceptDialog { - GDCLASS(ConfirmationDialog,AcceptDialog); + GDCLASS(ConfirmationDialog, AcceptDialog); Button *cancel; + protected: static void _bind_methods(); -public: +public: Button *get_cancel(); ConfirmationDialog(); - }; #endif diff --git a/scene/gui/file_dialog.cpp b/scene/gui/file_dialog.cpp index 357a70a25b..d4643b399d 100644 --- a/scene/gui/file_dialog.cpp +++ b/scene/gui/file_dialog.cpp @@ -27,52 +27,48 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "file_dialog.h" -#include "scene/gui/label.h" -#include "print_string.h" #include "os/keyboard.h" +#include "print_string.h" +#include "scene/gui/label.h" +FileDialog::GetIconFunc FileDialog::get_icon_func = NULL; +FileDialog::GetIconFunc FileDialog::get_large_icon_func = NULL; - -FileDialog::GetIconFunc FileDialog::get_icon_func=NULL; -FileDialog::GetIconFunc FileDialog::get_large_icon_func=NULL; - -FileDialog::RegisterFunc FileDialog::register_func=NULL; -FileDialog::RegisterFunc FileDialog::unregister_func=NULL; - +FileDialog::RegisterFunc FileDialog::register_func = NULL; +FileDialog::RegisterFunc FileDialog::unregister_func = NULL; VBoxContainer *FileDialog::get_vbox() { return vbox; - } void FileDialog::_notification(int p_what) { - if (p_what==NOTIFICATION_ENTER_TREE) { + if (p_what == NOTIFICATION_ENTER_TREE) { refresh->set_icon(get_icon("reload")); } - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { //RID ci = get_canvas_item(); //get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size())); } - if (p_what==NOTIFICATION_POPUP_HIDE) { + if (p_what == NOTIFICATION_POPUP_HIDE) { set_process_unhandled_input(false); } } -void FileDialog::_unhandled_input(const InputEvent& p_event) { +void FileDialog::_unhandled_input(const InputEvent &p_event) { - if (p_event.type==InputEvent::KEY && is_window_modal_on_top()) { + if (p_event.type == InputEvent::KEY && is_window_modal_on_top()) { - const InputEventKey &k=p_event.key; + const InputEventKey &k = p_event.key; if (k.pressed) { - bool handled=true; + bool handled = true; switch (k.scancode) { @@ -81,7 +77,7 @@ void FileDialog::_unhandled_input(const InputEvent& p_event) { if (k.mod.command) { set_show_hidden_files(!show_hidden_files); } else { - handled=false; + handled = false; } } break; @@ -89,7 +85,7 @@ void FileDialog::_unhandled_input(const InputEvent& p_event) { invalidate(); } break; - default: { handled=false; } + default: { handled = false; } } if (handled) @@ -100,15 +96,15 @@ void FileDialog::_unhandled_input(const InputEvent& p_event) { void FileDialog::set_enable_multiple_selection(bool p_enable) { - tree->set_select_mode(p_enable?Tree::SELECT_MULTI : Tree::SELECT_SINGLE); + tree->set_select_mode(p_enable ? Tree::SELECT_MULTI : Tree::SELECT_SINGLE); }; Vector<String> FileDialog::get_selected_files() const { Vector<String> list; - TreeItem* item = tree->get_root(); - while ( (item = tree->get_next_selected(item)) ) { + TreeItem *item = tree->get_root(); + while ((item = tree->get_next_selected(item))) { list.push_back(dir_access->get_current_dir().plus_file(item->get_text(0))); }; @@ -123,21 +119,20 @@ void FileDialog::update_dir() { void FileDialog::_dir_entered(String p_dir) { - dir_access->change_dir(p_dir); file->set_text(""); invalidate(); update_dir(); } -void FileDialog::_file_entered(const String& p_file) { +void FileDialog::_file_entered(const String &p_file) { _action_pressed(); } void FileDialog::_save_confirm_pressed() { - String f=dir_access->get_current_dir().plus_file(file->get_text()); - emit_signal("file_selected",f); + String f = dir_access->get_current_dir().plus_file(file->get_text()); + emit_signal("file_selected", f); hide(); } @@ -146,77 +141,76 @@ void FileDialog::_post_popup() { ConfirmationDialog::_post_popup(); if (invalidated) { update_file_list(); - invalidated=false; + invalidated = false; } - if (mode==MODE_SAVE_FILE) + if (mode == MODE_SAVE_FILE) file->grab_focus(); else tree->grab_focus(); set_process_unhandled_input(true); - } void FileDialog::_action_pressed() { - if (mode==MODE_OPEN_FILES) { + if (mode == MODE_OPEN_FILES) { - TreeItem *ti=tree->get_next_selected(NULL); - String fbase=dir_access->get_current_dir(); + TreeItem *ti = tree->get_next_selected(NULL); + String fbase = dir_access->get_current_dir(); PoolVector<String> files; - while(ti) { + while (ti) { - files.push_back( fbase.plus_file(ti->get_text(0)) ); - ti=tree->get_next_selected(ti); + files.push_back(fbase.plus_file(ti->get_text(0))); + ti = tree->get_next_selected(ti); } if (files.size()) { - emit_signal("files_selected",files); + emit_signal("files_selected", files); hide(); } return; } - String f=dir_access->get_current_dir().plus_file(file->get_text()); + String f = dir_access->get_current_dir().plus_file(file->get_text()); - if ((mode==MODE_OPEN_ANY || mode==MODE_OPEN_FILE) && dir_access->file_exists(f)) { - emit_signal("file_selected",f); + if ((mode == MODE_OPEN_ANY || mode == MODE_OPEN_FILE) && dir_access->file_exists(f)) { + emit_signal("file_selected", f); hide(); - }else if (mode==MODE_OPEN_ANY || mode==MODE_OPEN_DIR) { + } else if (mode == MODE_OPEN_ANY || mode == MODE_OPEN_DIR) { - String path=dir_access->get_current_dir(); + String path = dir_access->get_current_dir(); - path=path.replace("\\","/"); + path = path.replace("\\", "/"); - if (TreeItem* item = tree->get_selected()) { + if (TreeItem *item = tree->get_selected()) { Dictionary d = item->get_metadata(0); if (d["dir"]) { - path=path.plus_file(d["name"]); + path = path.plus_file(d["name"]); } } - emit_signal("dir_selected",path); + emit_signal("dir_selected", path); hide(); } - if (mode==MODE_SAVE_FILE) { + if (mode == MODE_SAVE_FILE) { - bool valid=false; + bool valid = false; - if (filter->get_selected()==filter->get_item_count()-1) { - valid=true; //match none - } else if (filters.size()>1 && filter->get_selected()==0) { + if (filter->get_selected() == filter->get_item_count() - 1) { + valid = true; //match none + } else if (filters.size() > 1 && filter->get_selected() == 0) { // match all filters - for (int i=0;i<filters.size();i++) { + for (int i = 0; i < filters.size(); i++) { - String flt=filters[i].get_slice(";",0); - for (int j=0;j<flt.get_slice_count(",");j++) { + String flt = filters[i].get_slice(";", 0); + for (int j = 0; j < flt.get_slice_count(","); j++) { - String str = flt.get_slice(",",j).strip_edges(); + String str = flt.get_slice(",", j).strip_edges(); if (f.match(str)) { - valid=true; + valid = true; break; } } @@ -224,48 +218,45 @@ void FileDialog::_action_pressed() { break; } } else { - int idx=filter->get_selected(); - if (filters.size()>1) + int idx = filter->get_selected(); + if (filters.size() > 1) idx--; - if (idx>=0 && idx<filters.size()) { + if (idx >= 0 && idx < filters.size()) { - String flt=filters[idx].get_slice(";",0); - int filterSliceCount=flt.get_slice_count(","); - for (int j=0;j<filterSliceCount;j++) { + String flt = filters[idx].get_slice(";", 0); + int filterSliceCount = flt.get_slice_count(","); + for (int j = 0; j < filterSliceCount; j++) { - String str = (flt.get_slice(",",j).strip_edges()); + String str = (flt.get_slice(",", j).strip_edges()); if (f.match(str)) { - valid=true; + valid = true; break; } } - if (!valid && filterSliceCount>0) { - String str = (flt.get_slice(",",0).strip_edges()); - f+=str.substr(1, str.length()-1); + if (!valid && filterSliceCount > 0) { + String str = (flt.get_slice(",", 0).strip_edges()); + f += str.substr(1, str.length() - 1); file->set_text(f.get_file()); - valid=true; + valid = true; } } else { - valid=true; + valid = true; } } - if (!valid) { - exterr->popup_centered_minsize(Size2(250,80)); + exterr->popup_centered_minsize(Size2(250, 80)); return; - } if (dir_access->file_exists(f)) { confirm_save->set_text(RTR("File Exists, Overwrite?")); - confirm_save->popup_centered(Size2(200,80)); + confirm_save->popup_centered(Size2(200, 80)); } else { - - emit_signal("file_selected",f); + emit_signal("file_selected", f); hide(); } } @@ -280,31 +271,29 @@ void FileDialog::_cancel_pressed() { void FileDialog::_tree_selected() { - TreeItem *ti=tree->get_selected(); + TreeItem *ti = tree->get_selected(); if (!ti) return; - Dictionary d=ti->get_metadata(0); + Dictionary d = ti->get_metadata(0); if (!d["dir"]) { file->set_text(d["name"]); } - } void FileDialog::_tree_dc_selected() { - - TreeItem *ti=tree->get_selected(); + TreeItem *ti = tree->get_selected(); if (!ti) return; - Dictionary d=ti->get_metadata(0); + Dictionary d = ti->get_metadata(0); if (d["dir"]) { dir_access->change_dir(d["name"]); - if (mode==MODE_OPEN_FILE || mode==MODE_OPEN_FILES || mode==MODE_OPEN_DIR || mode==MODE_OPEN_ANY) + if (mode == MODE_OPEN_FILE || mode == MODE_OPEN_FILES || mode == MODE_OPEN_DIR || mode == MODE_OPEN_ANY) file->set_text(""); call_deferred("_update_file_list"); call_deferred("_update_dir"); @@ -329,7 +318,7 @@ void FileDialog::update_file_list() { bool show_hidden = show_hidden_files; String item; - while ((item=dir_access->get_next(&isdir))!="") { + while ((item = dir_access->get_next(&isdir)) != "") { ishidden = dir_access->current_is_hidden(); @@ -341,7 +330,7 @@ void FileDialog::update_file_list() { } } - if (dirs.find("..")==NULL) { + if (dirs.find("..") == NULL) { //may happen if lacking permissions dirs.push_back(".."); } @@ -349,17 +338,17 @@ void FileDialog::update_file_list() { dirs.sort_custom<NoCaseComparator>(); files.sort_custom<NoCaseComparator>(); - while(!dirs.empty()) { - String& dir_name = dirs.front()->get(); - TreeItem *ti=tree->create_item(root); - ti->set_text(0,dir_name+"/"); - ti->set_icon(0,folder); + while (!dirs.empty()) { + String &dir_name = dirs.front()->get(); + TreeItem *ti = tree->create_item(root); + ti->set_text(0, dir_name + "/"); + ti->set_icon(0, folder); Dictionary d; - d["name"]=dir_name; - d["dir"]=true; + d["name"] = dir_name; + d["dir"] = true; - ti->set_metadata(0,d); + ti->set_metadata(0, d); dirs.pop_front(); } @@ -368,83 +357,80 @@ void FileDialog::update_file_list() { List<String> patterns; // build filter - if (filter->get_selected()==filter->get_item_count()-1) { + if (filter->get_selected() == filter->get_item_count() - 1) { // match all - } else if (filters.size()>1 && filter->get_selected()==0) { + } else if (filters.size() > 1 && filter->get_selected() == 0) { // match all filters - for (int i=0;i<filters.size();i++) { + for (int i = 0; i < filters.size(); i++) { - String f=filters[i].get_slice(";",0); - for (int j=0;j<f.get_slice_count(",");j++) { + String f = filters[i].get_slice(";", 0); + for (int j = 0; j < f.get_slice_count(","); j++) { - patterns.push_back(f.get_slice(",",j).strip_edges()); + patterns.push_back(f.get_slice(",", j).strip_edges()); } } } else { - int idx=filter->get_selected(); - if (filters.size()>1) + int idx = filter->get_selected(); + if (filters.size() > 1) idx--; - if (idx>=0 && idx<filters.size()) { + if (idx >= 0 && idx < filters.size()) { - String f=filters[idx].get_slice(";",0); - for (int j=0;j<f.get_slice_count(",");j++) { + String f = filters[idx].get_slice(";", 0); + for (int j = 0; j < f.get_slice_count(","); j++) { - patterns.push_back(f.get_slice(",",j).strip_edges()); + patterns.push_back(f.get_slice(",", j).strip_edges()); } } } - String base_dir = dir_access->get_current_dir(); + while (!files.empty()) { - while(!files.empty()) { - - bool match=patterns.empty(); + bool match = patterns.empty(); String match_str; - for(List<String>::Element *E=patterns.front();E;E=E->next()) { + for (List<String>::Element *E = patterns.front(); E; E = E->next()) { if (files.front()->get().matchn(E->get())) { - match_str=E->get(); - match=true; + match_str = E->get(); + match = true; break; } } if (match) { - TreeItem *ti=tree->create_item(root); - ti->set_text(0,files.front()->get()); + TreeItem *ti = tree->create_item(root); + ti->set_text(0, files.front()->get()); if (get_icon_func) { Ref<Texture> icon = get_icon_func(base_dir.plus_file(files.front()->get())); - ti->set_icon(0,icon); + ti->set_icon(0, icon); } - if (mode==MODE_OPEN_DIR) { - ti->set_custom_color(0,get_color("files_disabled")); - ti->set_selectable(0,false); + if (mode == MODE_OPEN_DIR) { + ti->set_custom_color(0, get_color("files_disabled")); + ti->set_selectable(0, false); } Dictionary d; - d["name"]=files.front()->get(); - d["dir"]=false; - ti->set_metadata(0,d); + d["name"] = files.front()->get(); + d["dir"] = false; + ti->set_metadata(0, d); - if (file->get_text()==files.front()->get() || match_str==files.front()->get()) + if (file->get_text() == files.front()->get() || match_str == files.front()->get()) ti->select(0); } files.pop_front(); } - if (tree->get_root() && tree->get_root()->get_children() && tree->get_selected()==NULL) + if (tree->get_root() && tree->get_root()->get_children() && tree->get_selected() == NULL) tree->get_root()->get_children()->select(0); files.clear(); - } void FileDialog::_filter_selected(int) { @@ -456,35 +442,34 @@ void FileDialog::update_filters() { filter->clear(); - if (filters.size()>1) { + if (filters.size() > 1) { String all_filters; - const int max_filters=5; + const int max_filters = 5; - for(int i=0;i<MIN( max_filters, filters.size()) ;i++) { - String flt=filters[i].get_slice(";",0); - if (i>0) - all_filters+=","; - all_filters+=flt; + for (int i = 0; i < MIN(max_filters, filters.size()); i++) { + String flt = filters[i].get_slice(";", 0); + if (i > 0) + all_filters += ","; + all_filters += flt; } - if (max_filters<filters.size()) - all_filters+=", ..."; + if (max_filters < filters.size()) + all_filters += ", ..."; - filter->add_item(RTR("All Recognized")+" ( "+all_filters+" )"); + filter->add_item(RTR("All Recognized") + " ( " + all_filters + " )"); } - for(int i=0;i<filters.size();i++) { + for (int i = 0; i < filters.size(); i++) { - String flt=filters[i].get_slice(";",0).strip_edges(); - String desc=filters[i].get_slice(";",1).strip_edges(); + String flt = filters[i].get_slice(";", 0).strip_edges(); + String desc = filters[i].get_slice(";", 1).strip_edges(); if (desc.length()) - filter->add_item(String(XL_MESSAGE(desc))+" ( "+flt+" )"); + filter->add_item(String(XL_MESSAGE(desc)) + " ( " + flt + " )"); else - filter->add_item("( "+flt+" )"); + filter->add_item("( " + flt + " )"); } filter->add_item(RTR("All Files (*)")); - } void FileDialog::clear_filters() { @@ -493,21 +478,20 @@ void FileDialog::clear_filters() { update_filters(); invalidate(); } -void FileDialog::add_filter(const String& p_filter) { +void FileDialog::add_filter(const String &p_filter) { filters.push_back(p_filter); update_filters(); invalidate(); - } -void FileDialog::set_filters(const Vector<String>& p_filters){ - filters=p_filters; +void FileDialog::set_filters(const Vector<String> &p_filters) { + filters = p_filters; update_filters(); invalidate(); } -Vector<String> FileDialog::get_filters() const{ +Vector<String> FileDialog::get_filters() const { return filters; } @@ -523,61 +507,76 @@ String FileDialog::get_current_path() const { return dir->get_text().plus_file(file->get_text()); } -void FileDialog::set_current_dir(const String& p_dir) { +void FileDialog::set_current_dir(const String &p_dir) { dir_access->change_dir(p_dir); update_dir(); invalidate(); - } -void FileDialog::set_current_file(const String& p_file) { +void FileDialog::set_current_file(const String &p_file) { file->set_text(p_file); update_dir(); invalidate(); int lp = p_file.find_last("."); - if (lp!=-1) { - file->select(0,lp); + if (lp != -1) { + file->select(0, lp); file->grab_focus(); } - - } -void FileDialog::set_current_path(const String& p_path) { +void FileDialog::set_current_path(const String &p_path) { if (!p_path.size()) return; - int pos=MAX( p_path.find_last("/"), p_path.find_last("\\") ); - if (pos==-1) { + int pos = MAX(p_path.find_last("/"), p_path.find_last("\\")); + if (pos == -1) { set_current_file(p_path); } else { - String dir=p_path.substr(0,pos); - String file=p_path.substr(pos+1,p_path.length()); + String dir = p_path.substr(0, pos); + String file = p_path.substr(pos + 1, p_path.length()); set_current_dir(dir); set_current_file(file); } } - void FileDialog::set_mode(Mode p_mode) { - mode=p_mode; - switch(mode) { - - case MODE_OPEN_FILE: get_ok()->set_text(RTR("Open")); set_title(RTR("Open a File")); makedir->hide(); break; - case MODE_OPEN_FILES: get_ok()->set_text(RTR("Open")); set_title(RTR("Open File(s)")); makedir->hide(); break; - case MODE_OPEN_DIR: get_ok()->set_text(RTR("Open")); set_title(RTR("Open a Directory")); makedir->show(); break; - case MODE_OPEN_ANY: get_ok()->set_text(RTR("Open")); set_title(RTR("Open a File or Directory")); makedir->show(); break; - case MODE_SAVE_FILE: get_ok()->set_text(RTR("Save")); set_title(RTR("Save a File")); makedir->show(); break; + mode = p_mode; + switch (mode) { + + case MODE_OPEN_FILE: + get_ok()->set_text(RTR("Open")); + set_title(RTR("Open a File")); + makedir->hide(); + break; + case MODE_OPEN_FILES: + get_ok()->set_text(RTR("Open")); + set_title(RTR("Open File(s)")); + makedir->hide(); + break; + case MODE_OPEN_DIR: + get_ok()->set_text(RTR("Open")); + set_title(RTR("Open a Directory")); + makedir->show(); + break; + case MODE_OPEN_ANY: + get_ok()->set_text(RTR("Open")); + set_title(RTR("Open a File or Directory")); + makedir->show(); + break; + case MODE_SAVE_FILE: + get_ok()->set_text(RTR("Save")); + set_title(RTR("Save a File")); + makedir->show(); + break; } - if (mode==MODE_OPEN_FILES) { + if (mode == MODE_OPEN_FILES) { tree->set_select_mode(Tree::SELECT_MULTI); } else { tree->set_select_mode(Tree::SELECT_SINGLE); - } } @@ -588,11 +587,11 @@ FileDialog::Mode FileDialog::get_mode() const { void FileDialog::set_access(Access p_access) { - ERR_FAIL_INDEX(p_access,3); - if (access==p_access) + ERR_FAIL_INDEX(p_access, 3); + if (access == p_access) return; - memdelete( dir_access ); - switch(p_access) { + memdelete(dir_access); + switch (p_access) { case ACCESS_FILESYSTEM: { dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); @@ -606,7 +605,7 @@ void FileDialog::set_access(Access p_access) { dir_access = DirAccess::create(DirAccess::ACCESS_USERDATA); } break; } - access=p_access; + access = p_access; _update_drives(); invalidate(); update_filters(); @@ -617,38 +616,35 @@ void FileDialog::invalidate() { if (is_visible_in_tree()) { update_file_list(); - invalidated=false; + invalidated = false; } else { - invalidated=true; + invalidated = true; } - } -FileDialog::Access FileDialog::get_access() const{ +FileDialog::Access FileDialog::get_access() const { return access; } void FileDialog::_make_dir_confirm() { - Error err = dir_access->make_dir( makedirname->get_text() ); - if (err==OK) { + Error err = dir_access->make_dir(makedirname->get_text()); + if (err == OK) { dir_access->change_dir(makedirname->get_text()); invalidate(); update_filters(); update_dir(); } else { - mkdirerr->popup_centered_minsize(Size2(250,50)); + mkdirerr->popup_centered_minsize(Size2(250, 50)); } makedirname->set_text(""); // reset label } - void FileDialog::_make_dir() { - makedialog->popup_centered_minsize(Size2(250,80)); + makedialog->popup_centered_minsize(Size2(250, 80)); makedirname->grab_focus(); - } void FileDialog::_select_drive(int p_idx) { @@ -658,95 +654,89 @@ void FileDialog::_select_drive(int p_idx) { file->set_text(""); invalidate(); update_dir(); - } void FileDialog::_update_drives() { - int dc = dir_access->get_drive_count(); - if (dc==0 || access!=ACCESS_FILESYSTEM) { + if (dc == 0 || access != ACCESS_FILESYSTEM) { drives->hide(); } else { drives->clear(); drives->show(); - for(int i=0;i<dir_access->get_drive_count();i++) { + for (int i = 0; i < dir_access->get_drive_count(); i++) { String d = dir_access->get_drive(i); drives->add_item(dir_access->get_drive(i)); } drives->select(dir_access->get_current_drive()); - } } -bool FileDialog::default_show_hidden_files=false; - +bool FileDialog::default_show_hidden_files = false; void FileDialog::_bind_methods() { - ClassDB::bind_method(D_METHOD("_unhandled_input"),&FileDialog::_unhandled_input); - - ClassDB::bind_method(D_METHOD("_tree_selected"),&FileDialog::_tree_selected); - ClassDB::bind_method(D_METHOD("_tree_db_selected"),&FileDialog::_tree_dc_selected); - ClassDB::bind_method(D_METHOD("_dir_entered"),&FileDialog::_dir_entered); - ClassDB::bind_method(D_METHOD("_file_entered"),&FileDialog::_file_entered); - ClassDB::bind_method(D_METHOD("_action_pressed"),&FileDialog::_action_pressed); - ClassDB::bind_method(D_METHOD("_cancel_pressed"),&FileDialog::_cancel_pressed); - ClassDB::bind_method(D_METHOD("_filter_selected"),&FileDialog::_filter_selected); - ClassDB::bind_method(D_METHOD("_save_confirm_pressed"),&FileDialog::_save_confirm_pressed); - - ClassDB::bind_method(D_METHOD("clear_filters"),&FileDialog::clear_filters); - ClassDB::bind_method(D_METHOD("add_filter","filter"),&FileDialog::add_filter); - ClassDB::bind_method(D_METHOD("set_filters","filters"),&FileDialog::set_filters); - ClassDB::bind_method(D_METHOD("get_filters"),&FileDialog::get_filters); - ClassDB::bind_method(D_METHOD("get_current_dir"),&FileDialog::get_current_dir); - ClassDB::bind_method(D_METHOD("get_current_file"),&FileDialog::get_current_file); - ClassDB::bind_method(D_METHOD("get_current_path"),&FileDialog::get_current_path); - ClassDB::bind_method(D_METHOD("set_current_dir","dir"),&FileDialog::set_current_dir); - ClassDB::bind_method(D_METHOD("set_current_file","file"),&FileDialog::set_current_file); - ClassDB::bind_method(D_METHOD("set_current_path","path"),&FileDialog::set_current_path); - ClassDB::bind_method(D_METHOD("set_mode","mode"),&FileDialog::set_mode); - ClassDB::bind_method(D_METHOD("get_mode"),&FileDialog::get_mode); - ClassDB::bind_method(D_METHOD("get_vbox:VBoxContainer"),&FileDialog::get_vbox); - ClassDB::bind_method(D_METHOD("set_access","access"),&FileDialog::set_access); - ClassDB::bind_method(D_METHOD("get_access"),&FileDialog::get_access); - ClassDB::bind_method(D_METHOD("set_show_hidden_files","show"),&FileDialog::set_show_hidden_files); - ClassDB::bind_method(D_METHOD("is_showing_hidden_files"),&FileDialog::is_showing_hidden_files); - ClassDB::bind_method(D_METHOD("_select_drive"),&FileDialog::_select_drive); - ClassDB::bind_method(D_METHOD("_make_dir"),&FileDialog::_make_dir); - ClassDB::bind_method(D_METHOD("_make_dir_confirm"),&FileDialog::_make_dir_confirm); - ClassDB::bind_method(D_METHOD("_update_file_list"),&FileDialog::update_file_list); - ClassDB::bind_method(D_METHOD("_update_dir"),&FileDialog::update_dir); - - ClassDB::bind_method(D_METHOD("invalidate"),&FileDialog::invalidate); - - ADD_SIGNAL(MethodInfo("file_selected",PropertyInfo( Variant::STRING,"path"))); - ADD_SIGNAL(MethodInfo("files_selected",PropertyInfo( Variant::POOL_STRING_ARRAY,"paths"))); - ADD_SIGNAL(MethodInfo("dir_selected",PropertyInfo( Variant::STRING,"dir"))); - - BIND_CONSTANT( MODE_OPEN_FILE ); - BIND_CONSTANT( MODE_OPEN_FILES ); - BIND_CONSTANT( MODE_OPEN_DIR ); - BIND_CONSTANT( MODE_OPEN_ANY ); - - BIND_CONSTANT( MODE_SAVE_FILE ); - - BIND_CONSTANT( ACCESS_RESOURCES ); - BIND_CONSTANT( ACCESS_USERDATA ); - BIND_CONSTANT( ACCESS_FILESYSTEM ); - - ADD_PROPERTY( PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Open one,Open many,Open folder,Open any,Save"),"set_mode","get_mode") ; - ADD_PROPERTY( PropertyInfo(Variant::INT, "access", PROPERTY_HINT_ENUM, "Resources,User data,File system"),"set_access","get_access") ; - ADD_PROPERTY( PropertyInfo(Variant::POOL_STRING_ARRAY, "filters"),"set_filters","get_filters") ; - ADD_PROPERTY( PropertyInfo(Variant::BOOL, "show_hidden_files"),"set_show_hidden_files","is_showing_hidden_files") ; - + ClassDB::bind_method(D_METHOD("_unhandled_input"), &FileDialog::_unhandled_input); + + ClassDB::bind_method(D_METHOD("_tree_selected"), &FileDialog::_tree_selected); + ClassDB::bind_method(D_METHOD("_tree_db_selected"), &FileDialog::_tree_dc_selected); + ClassDB::bind_method(D_METHOD("_dir_entered"), &FileDialog::_dir_entered); + ClassDB::bind_method(D_METHOD("_file_entered"), &FileDialog::_file_entered); + ClassDB::bind_method(D_METHOD("_action_pressed"), &FileDialog::_action_pressed); + ClassDB::bind_method(D_METHOD("_cancel_pressed"), &FileDialog::_cancel_pressed); + ClassDB::bind_method(D_METHOD("_filter_selected"), &FileDialog::_filter_selected); + ClassDB::bind_method(D_METHOD("_save_confirm_pressed"), &FileDialog::_save_confirm_pressed); + + ClassDB::bind_method(D_METHOD("clear_filters"), &FileDialog::clear_filters); + ClassDB::bind_method(D_METHOD("add_filter", "filter"), &FileDialog::add_filter); + ClassDB::bind_method(D_METHOD("set_filters", "filters"), &FileDialog::set_filters); + ClassDB::bind_method(D_METHOD("get_filters"), &FileDialog::get_filters); + ClassDB::bind_method(D_METHOD("get_current_dir"), &FileDialog::get_current_dir); + ClassDB::bind_method(D_METHOD("get_current_file"), &FileDialog::get_current_file); + ClassDB::bind_method(D_METHOD("get_current_path"), &FileDialog::get_current_path); + ClassDB::bind_method(D_METHOD("set_current_dir", "dir"), &FileDialog::set_current_dir); + ClassDB::bind_method(D_METHOD("set_current_file", "file"), &FileDialog::set_current_file); + ClassDB::bind_method(D_METHOD("set_current_path", "path"), &FileDialog::set_current_path); + ClassDB::bind_method(D_METHOD("set_mode", "mode"), &FileDialog::set_mode); + ClassDB::bind_method(D_METHOD("get_mode"), &FileDialog::get_mode); + ClassDB::bind_method(D_METHOD("get_vbox:VBoxContainer"), &FileDialog::get_vbox); + ClassDB::bind_method(D_METHOD("set_access", "access"), &FileDialog::set_access); + ClassDB::bind_method(D_METHOD("get_access"), &FileDialog::get_access); + ClassDB::bind_method(D_METHOD("set_show_hidden_files", "show"), &FileDialog::set_show_hidden_files); + ClassDB::bind_method(D_METHOD("is_showing_hidden_files"), &FileDialog::is_showing_hidden_files); + ClassDB::bind_method(D_METHOD("_select_drive"), &FileDialog::_select_drive); + ClassDB::bind_method(D_METHOD("_make_dir"), &FileDialog::_make_dir); + ClassDB::bind_method(D_METHOD("_make_dir_confirm"), &FileDialog::_make_dir_confirm); + ClassDB::bind_method(D_METHOD("_update_file_list"), &FileDialog::update_file_list); + ClassDB::bind_method(D_METHOD("_update_dir"), &FileDialog::update_dir); + + ClassDB::bind_method(D_METHOD("invalidate"), &FileDialog::invalidate); + + ADD_SIGNAL(MethodInfo("file_selected", PropertyInfo(Variant::STRING, "path"))); + ADD_SIGNAL(MethodInfo("files_selected", PropertyInfo(Variant::POOL_STRING_ARRAY, "paths"))); + ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir"))); + + BIND_CONSTANT(MODE_OPEN_FILE); + BIND_CONSTANT(MODE_OPEN_FILES); + BIND_CONSTANT(MODE_OPEN_DIR); + BIND_CONSTANT(MODE_OPEN_ANY); + + BIND_CONSTANT(MODE_SAVE_FILE); + + BIND_CONSTANT(ACCESS_RESOURCES); + BIND_CONSTANT(ACCESS_USERDATA); + BIND_CONSTANT(ACCESS_FILESYSTEM); + + ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Open one,Open many,Open folder,Open any,Save"), "set_mode", "get_mode"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "access", PROPERTY_HINT_ENUM, "Resources,User data,File system"), "set_access", "get_access"); + ADD_PROPERTY(PropertyInfo(Variant::POOL_STRING_ARRAY, "filters"), "set_filters", "get_filters"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_hidden_files"), "set_show_hidden_files", "is_showing_hidden_files"); } - void FileDialog::set_show_hidden_files(bool p_show) { - show_hidden_files=p_show; + show_hidden_files = p_show; invalidate(); } @@ -755,109 +745,100 @@ bool FileDialog::is_showing_hidden_files() const { } void FileDialog::set_default_show_hidden_files(bool p_show) { - default_show_hidden_files=p_show; + default_show_hidden_files = p_show; } FileDialog::FileDialog() { - show_hidden_files=default_show_hidden_files; + show_hidden_files = default_show_hidden_files; - VBoxContainer *vbc = memnew( VBoxContainer ); + VBoxContainer *vbc = memnew(VBoxContainer); add_child(vbc); - - mode=MODE_SAVE_FILE; + mode = MODE_SAVE_FILE; set_title(RTR("Save a File")); dir = memnew(LineEdit); - HBoxContainer *pathhb = memnew( HBoxContainer ); + HBoxContainer *pathhb = memnew(HBoxContainer); pathhb->add_child(dir); dir->set_h_size_flags(SIZE_EXPAND_FILL); - refresh = memnew( ToolButton ); - refresh->connect("pressed",this,"_update_file_list"); + refresh = memnew(ToolButton); + refresh->connect("pressed", this, "_update_file_list"); pathhb->add_child(refresh); - drives = memnew( OptionButton ); + drives = memnew(OptionButton); pathhb->add_child(drives); - drives->connect("item_selected",this,"_select_drive"); + drives->connect("item_selected", this, "_select_drive"); - makedir = memnew( Button ); + makedir = memnew(Button); makedir->set_text(RTR("Create Folder")); - makedir->connect("pressed",this,"_make_dir"); + makedir->connect("pressed", this, "_make_dir"); pathhb->add_child(makedir); - vbc->add_margin_child(RTR("Path:"),pathhb); + vbc->add_margin_child(RTR("Path:"), pathhb); tree = memnew(Tree); tree->set_hide_root(true); - vbc->add_margin_child(RTR("Directories & Files:"),tree,true); + vbc->add_margin_child(RTR("Directories & Files:"), tree, true); file = memnew(LineEdit); //add_child(file); - vbc->add_margin_child(RTR("File:"),file); + vbc->add_margin_child(RTR("File:"), file); - - filter = memnew( OptionButton ); + filter = memnew(OptionButton); //add_child(filter); - vbc->add_margin_child(RTR("Filter:"),filter); - filter->set_clip_text(true);//too many extensions overflow it + vbc->add_margin_child(RTR("Filter:"), filter); + filter->set_clip_text(true); //too many extensions overflow it dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES); - access=ACCESS_RESOURCES; + access = ACCESS_RESOURCES; _update_drives(); - - connect("confirmed", this,"_action_pressed"); + connect("confirmed", this, "_action_pressed"); //cancel->connect("pressed", this,"_cancel_pressed"); - tree->connect("cell_selected", this,"_tree_selected",varray(),CONNECT_DEFERRED); - tree->connect("item_activated", this,"_tree_db_selected",varray()); - dir->connect("text_entered", this,"_dir_entered"); - file->connect("text_entered", this,"_file_entered"); - filter->connect("item_selected", this,"_filter_selected"); - + tree->connect("cell_selected", this, "_tree_selected", varray(), CONNECT_DEFERRED); + tree->connect("item_activated", this, "_tree_db_selected", varray()); + dir->connect("text_entered", this, "_dir_entered"); + file->connect("text_entered", this, "_file_entered"); + filter->connect("item_selected", this, "_filter_selected"); - confirm_save = memnew( ConfirmationDialog ); + confirm_save = memnew(ConfirmationDialog); confirm_save->set_as_toplevel(true); add_child(confirm_save); + confirm_save->connect("confirmed", this, "_save_confirm_pressed"); - confirm_save->connect("confirmed", this,"_save_confirm_pressed"); - - makedialog = memnew( ConfirmationDialog ); + makedialog = memnew(ConfirmationDialog); makedialog->set_title(RTR("Create Folder")); - VBoxContainer *makevb= memnew( VBoxContainer ); + VBoxContainer *makevb = memnew(VBoxContainer); makedialog->add_child(makevb); - makedirname = memnew( LineEdit ); - makevb->add_margin_child(RTR("Name:"),makedirname); + makedirname = memnew(LineEdit); + makevb->add_margin_child(RTR("Name:"), makedirname); add_child(makedialog); makedialog->register_text_enter(makedirname); - makedialog->connect("confirmed",this,"_make_dir_confirm"); - mkdirerr = memnew( AcceptDialog ); + makedialog->connect("confirmed", this, "_make_dir_confirm"); + mkdirerr = memnew(AcceptDialog); mkdirerr->set_text(RTR("Could not create folder.")); add_child(mkdirerr); - exterr = memnew( AcceptDialog ); + exterr = memnew(AcceptDialog); exterr->set_text(RTR("Must use a valid extension.")); add_child(exterr); - //update_file_list(); update_filters(); update_dir(); set_hide_on_ok(false); - vbox=vbc; - + vbox = vbc; - invalidated=true; + invalidated = true; if (register_func) register_func(this); - } - FileDialog::~FileDialog() { if (unregister_func) @@ -865,21 +846,19 @@ FileDialog::~FileDialog() { memdelete(dir_access); } - void LineEditFileChooser::_bind_methods() { - ClassDB::bind_method(D_METHOD("_browse"),&LineEditFileChooser::_browse); - ClassDB::bind_method(D_METHOD("_chosen"),&LineEditFileChooser::_chosen); - ClassDB::bind_method(D_METHOD("get_button:Button"),&LineEditFileChooser::get_button); - ClassDB::bind_method(D_METHOD("get_line_edit:LineEdit"),&LineEditFileChooser::get_line_edit); - ClassDB::bind_method(D_METHOD("get_file_dialog:FileDialog"),&LineEditFileChooser::get_file_dialog); - + ClassDB::bind_method(D_METHOD("_browse"), &LineEditFileChooser::_browse); + ClassDB::bind_method(D_METHOD("_chosen"), &LineEditFileChooser::_chosen); + ClassDB::bind_method(D_METHOD("get_button:Button"), &LineEditFileChooser::get_button); + ClassDB::bind_method(D_METHOD("get_line_edit:LineEdit"), &LineEditFileChooser::get_line_edit); + ClassDB::bind_method(D_METHOD("get_file_dialog:FileDialog"), &LineEditFileChooser::get_file_dialog); } -void LineEditFileChooser::_chosen(const String& p_text){ +void LineEditFileChooser::_chosen(const String &p_text) { line_edit->set_text(p_text); - line_edit->emit_signal("text_entered",p_text); + line_edit->emit_signal("text_entered", p_text); } void LineEditFileChooser::_browse() { @@ -889,18 +868,16 @@ void LineEditFileChooser::_browse() { LineEditFileChooser::LineEditFileChooser() { - line_edit = memnew( LineEdit ); + line_edit = memnew(LineEdit); add_child(line_edit); line_edit->set_h_size_flags(SIZE_EXPAND_FILL); - button = memnew( Button ); + button = memnew(Button); button->set_text(" .. "); add_child(button); - button->connect("pressed",this,"_browse"); - dialog = memnew( FileDialog); + button->connect("pressed", this, "_browse"); + dialog = memnew(FileDialog); add_child(dialog); - dialog->connect("file_selected",this,"_chosen"); - dialog->connect("dir_selected",this,"_chosen"); - dialog->connect("files_selected",this,"_chosen"); - + dialog->connect("file_selected", this, "_chosen"); + dialog->connect("dir_selected", this, "_chosen"); + dialog->connect("files_selected", this, "_chosen"); } - diff --git a/scene/gui/file_dialog.h b/scene/gui/file_dialog.h index 653b38e40d..397b0feba2 100644 --- a/scene/gui/file_dialog.h +++ b/scene/gui/file_dialog.h @@ -29,30 +29,28 @@ #ifndef FILE_DIALOG_H #define FILE_DIALOG_H +#include "box_container.h" +#include "os/dir_access.h" +#include "scene/gui/dialogs.h" #include "scene/gui/dialogs.h" -#include "scene/gui/tree.h" #include "scene/gui/line_edit.h" #include "scene/gui/option_button.h" -#include "scene/gui/dialogs.h" #include "scene/gui/tool_button.h" -#include "os/dir_access.h" -#include "box_container.h" +#include "scene/gui/tree.h" /** @author Juan Linietsky <reduzio@gmail.com> */ class FileDialog : public ConfirmationDialog { - GDCLASS( FileDialog, ConfirmationDialog ); + GDCLASS(FileDialog, ConfirmationDialog); public: - enum Access { ACCESS_RESOURCES, ACCESS_USERDATA, ACCESS_FILESYSTEM }; - enum Mode { MODE_OPEN_FILE, MODE_OPEN_FILES, @@ -61,8 +59,8 @@ public: MODE_SAVE_FILE }; - typedef Ref<Texture> (*GetIconFunc)(const String&); - typedef void (*RegisterFunc)(FileDialog*); + typedef Ref<Texture> (*GetIconFunc)(const String &); + typedef void (*RegisterFunc)(FileDialog *); static GetIconFunc get_icon_func; static GetIconFunc get_large_icon_func; @@ -70,7 +68,6 @@ public: static RegisterFunc unregister_func; private: - ConfirmationDialog *makedialog; LineEdit *makedirname; @@ -93,7 +90,6 @@ private: Vector<String> filters; - static bool default_show_hidden_files; bool show_hidden_files; @@ -108,7 +104,7 @@ private: void _select_drive(int p_idx); void _tree_dc_selected(); void _dir_entered(String p_dir); - void _file_entered(const String& p_file); + void _file_entered(const String &p_file); void _action_pressed(); void _save_confirm_pressed(); void _cancel_pressed(); @@ -118,20 +114,18 @@ private: void _update_drives(); - void _unhandled_input(const InputEvent& p_event); + void _unhandled_input(const InputEvent &p_event); virtual void _post_popup(); protected: - void _notification(int p_what); static void _bind_methods(); //bind helpers public: - void clear_filters(); - void add_filter(const String& p_filter); - void set_filters(const Vector<String>& p_filters); + void add_filter(const String &p_filter); + void set_filters(const Vector<String> &p_filters); Vector<String> get_filters() const; void set_enable_multiple_selection(bool p_enable); @@ -140,9 +134,9 @@ public: String get_current_dir() const; String get_current_file() const; String get_current_path() const; - void set_current_dir(const String& p_dir); - void set_current_file(const String& p_file); - void set_current_path(const String& p_path); + void set_current_dir(const String &p_dir); + void set_current_file(const String &p_file); + void set_current_path(const String &p_path); void set_mode(Mode p_mode); Mode get_mode() const; @@ -162,22 +156,22 @@ public: FileDialog(); ~FileDialog(); - }; class LineEditFileChooser : public HBoxContainer { - GDCLASS( LineEditFileChooser, HBoxContainer ); + GDCLASS(LineEditFileChooser, HBoxContainer); Button *button; LineEdit *line_edit; FileDialog *dialog; - void _chosen(const String& p_text); + void _chosen(const String &p_text); void _browse(); + protected: static void _bind_methods(); -public: +public: Button *get_button() { return button; } LineEdit *get_line_edit() { return line_edit; } FileDialog *get_file_dialog() { return dialog; } @@ -185,7 +179,7 @@ public: LineEditFileChooser(); }; -VARIANT_ENUM_CAST( FileDialog::Mode ); -VARIANT_ENUM_CAST( FileDialog::Access ); +VARIANT_ENUM_CAST(FileDialog::Mode); +VARIANT_ENUM_CAST(FileDialog::Access); #endif diff --git a/scene/gui/graph_edit.cpp b/scene/gui/graph_edit.cpp index a6954a2c77..6a3b5e713c 100644 --- a/scene/gui/graph_edit.cpp +++ b/scene/gui/graph_edit.cpp @@ -31,34 +31,30 @@ #include "os/keyboard.h" #include "scene/gui/box_container.h" - #define ZOOM_SCALE 1.2 -#define MIN_ZOOM (((1/ZOOM_SCALE)/ZOOM_SCALE)/ZOOM_SCALE) -#define MAX_ZOOM (1*ZOOM_SCALE*ZOOM_SCALE*ZOOM_SCALE) - +#define MIN_ZOOM (((1 / ZOOM_SCALE) / ZOOM_SCALE) / ZOOM_SCALE) +#define MAX_ZOOM (1 * ZOOM_SCALE * ZOOM_SCALE * ZOOM_SCALE) -bool GraphEditFilter::has_point(const Point2& p_point) const { +bool GraphEditFilter::has_point(const Point2 &p_point) const { return ge->_filter_input(p_point); } - GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) { - ge=p_edit; + ge = p_edit; } +Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) { -Error GraphEdit::connect_node(const StringName& p_from, int p_from_port,const StringName& p_to,int p_to_port) { - - if (is_node_connected(p_from,p_from_port,p_to,p_to_port)) + if (is_node_connected(p_from, p_from_port, p_to, p_to_port)) return OK; Connection c; - c.from=p_from; - c.from_port=p_from_port; - c.to=p_to; - c.to_port=p_to_port; + c.from = p_from; + c.from_port = p_from_port; + c.to = p_to; + c.to_port = p_to_port; connections.push_back(c); top_layer->update(); update(); @@ -67,23 +63,22 @@ Error GraphEdit::connect_node(const StringName& p_from, int p_from_port,const St return OK; } -bool GraphEdit::is_node_connected(const StringName& p_from, int p_from_port,const StringName& p_to,int p_to_port) { +bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) { - for(List<Connection>::Element *E=connections.front();E;E=E->next()) { + for (List<Connection>::Element *E = connections.front(); E; E = E->next()) { - if (E->get().from==p_from && E->get().from_port==p_from_port && E->get().to==p_to && E->get().to_port==p_to_port) + if (E->get().from == p_from && E->get().from_port == p_from_port && E->get().to == p_to && E->get().to_port == p_to_port) return true; } return false; - } -void GraphEdit::disconnect_node(const StringName& p_from, int p_from_port,const StringName& p_to,int p_to_port){ +void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) { - for(List<Connection>::Element *E=connections.front();E;E=E->next()) { + for (List<Connection>::Element *E = connections.front(); E; E = E->next()) { - if (E->get().from==p_from && E->get().from_port==p_from_port && E->get().to==p_to && E->get().to_port==p_to_port) { + if (E->get().from == p_from && E->get().from_port == p_from_port && E->get().to == p_to && E->get().to_port == p_to_port) { connections.erase(E); top_layer->update(); @@ -101,35 +96,34 @@ bool GraphEdit::clips_input() const { void GraphEdit::get_connection_list(List<Connection> *r_connections) const { - *r_connections=connections; + *r_connections = connections; } -void GraphEdit::set_scroll_ofs(const Vector2& p_ofs) { +void GraphEdit::set_scroll_ofs(const Vector2 &p_ofs) { - setting_scroll_ofs=true; + setting_scroll_ofs = true; h_scroll->set_value(p_ofs.x); v_scroll->set_value(p_ofs.y); _update_scroll(); - setting_scroll_ofs=false; + setting_scroll_ofs = false; } -Vector2 GraphEdit::get_scroll_ofs() const{ +Vector2 GraphEdit::get_scroll_ofs() const { - return Vector2(h_scroll->get_value(),v_scroll->get_value()); + return Vector2(h_scroll->get_value(), v_scroll->get_value()); } void GraphEdit::_scroll_moved(double) { - if (!awaiting_scroll_offset_update) { call_deferred("_update_scroll_offset"); - awaiting_scroll_offset_update=true; + awaiting_scroll_offset_update = true; } top_layer->update(); update(); - if (!setting_scroll_ofs) {//in godot, signals on change value are avoided as a convention - emit_signal("scroll_offset_changed",get_scroll_ofs()); + if (!setting_scroll_ofs) { //in godot, signals on change value are avoided as a convention + emit_signal("scroll_offset_changed", get_scroll_ofs()); } } @@ -137,24 +131,23 @@ void GraphEdit::_update_scroll_offset() { set_block_minimum_size_adjust(true); - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { - GraphNode *gn=get_child(i)->cast_to<GraphNode>(); + GraphNode *gn = get_child(i)->cast_to<GraphNode>(); if (!gn) continue; - Point2 pos=gn->get_offset()*zoom; - pos-=Point2(h_scroll->get_value(),v_scroll->get_value()); + Point2 pos = gn->get_offset() * zoom; + pos -= Point2(h_scroll->get_value(), v_scroll->get_value()); gn->set_pos(pos); - if (gn->get_scale()!=Vector2(zoom,zoom)) { - gn->set_scale(Vector2(zoom,zoom)); + if (gn->get_scale() != Vector2(zoom, zoom)) { + gn->set_scale(Vector2(zoom, zoom)); } } - connections_layer->set_pos(-Point2(h_scroll->get_value(),v_scroll->get_value())); + connections_layer->set_pos(-Point2(h_scroll->get_value(), v_scroll->get_value())); set_block_minimum_size_adjust(false); - awaiting_scroll_offset_update=false; - + awaiting_scroll_offset_update = false; } void GraphEdit::_update_scroll() { @@ -162,29 +155,28 @@ void GraphEdit::_update_scroll() { if (updating) return; - updating=true; + updating = true; set_block_minimum_size_adjust(true); Rect2 screen; - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { - GraphNode *gn=get_child(i)->cast_to<GraphNode>(); + GraphNode *gn = get_child(i)->cast_to<GraphNode>(); if (!gn) continue; Rect2 r; - r.pos=gn->get_offset()*zoom; - r.size=gn->get_size()*zoom; + r.pos = gn->get_offset() * zoom; + r.size = gn->get_size() * zoom; screen = screen.merge(r); } - screen.pos-=get_size(); - screen.size+=get_size()*2.0; - + screen.pos -= get_size(); + screen.size += get_size() * 2.0; h_scroll->set_min(screen.pos.x); - h_scroll->set_max(screen.pos.x+screen.size.x); + h_scroll->set_max(screen.pos.x + screen.size.x); h_scroll->set_page(get_size().x); if (h_scroll->get_max() - h_scroll->get_min() <= h_scroll->get_page()) h_scroll->hide(); @@ -192,7 +184,7 @@ void GraphEdit::_update_scroll() { h_scroll->show(); v_scroll->set_min(screen.pos.y); - v_scroll->set_max(screen.pos.y+screen.size.y); + v_scroll->set_max(screen.pos.y + screen.size.y); v_scroll->set_page(get_size().y); if (v_scroll->get_max() - v_scroll->get_min() <= v_scroll->get_page()) @@ -204,40 +196,37 @@ void GraphEdit::_update_scroll() { if (!awaiting_scroll_offset_update) { call_deferred("_update_scroll_offset"); - awaiting_scroll_offset_update=true; + awaiting_scroll_offset_update = true; } - updating=false; + updating = false; } +void GraphEdit::_graph_node_raised(Node *p_gn) { -void GraphEdit::_graph_node_raised(Node* p_gn) { - - GraphNode *gn=p_gn->cast_to<GraphNode>(); + GraphNode *gn = p_gn->cast_to<GraphNode>(); ERR_FAIL_COND(!gn); gn->raise(); if (gn->is_comment()) { - move_child(gn,0); + move_child(gn, 0); } - int first_not_comment=0; - for(int i=0;i<get_child_count();i++) { - GraphNode *gn=get_child(i)->cast_to<GraphNode>(); + int first_not_comment = 0; + for (int i = 0; i < get_child_count(); i++) { + GraphNode *gn = get_child(i)->cast_to<GraphNode>(); if (gn && !gn->is_comment()) { - first_not_comment=i; + first_not_comment = i; break; } } - move_child(connections_layer,first_not_comment); + move_child(connections_layer, first_not_comment); top_layer->raise(); - emit_signal("node_selected",p_gn); - + emit_signal("node_selected", p_gn); } - void GraphEdit::_graph_node_moved(Node *p_gn) { - GraphNode *gn=p_gn->cast_to<GraphNode>(); + GraphNode *gn = p_gn->cast_to<GraphNode>(); ERR_FAIL_COND(!gn); top_layer->update(); update(); @@ -251,15 +240,13 @@ void GraphEdit::add_child_notify(Node *p_child) { top_layer->call_deferred("raise"); //top layer always on top! GraphNode *gn = p_child->cast_to<GraphNode>(); if (gn) { - gn->set_scale(Vector2(zoom,zoom)); - gn->connect("offset_changed",this,"_graph_node_moved",varray(gn)); - gn->connect("raise_request",this,"_graph_node_raised",varray(gn)); - gn->connect("item_rect_changed",connections_layer,"update"); + gn->set_scale(Vector2(zoom, zoom)); + gn->connect("offset_changed", this, "_graph_node_moved", varray(gn)); + gn->connect("raise_request", this, "_graph_node_raised", varray(gn)); + gn->connect("item_rect_changed", connections_layer, "update"); _graph_node_moved(gn); gn->set_mouse_filter(MOUSE_FILTER_PASS); } - - } void GraphEdit::remove_child_notify(Node *p_child) { @@ -269,292 +256,268 @@ void GraphEdit::remove_child_notify(Node *p_child) { top_layer->call_deferred("raise"); //top layer always on top! GraphNode *gn = p_child->cast_to<GraphNode>(); if (gn) { - gn->disconnect("offset_changed",this,"_graph_node_moved"); - gn->disconnect("raise_request",this,"_graph_node_raised"); + gn->disconnect("offset_changed", this, "_graph_node_moved"); + gn->disconnect("raise_request", this, "_graph_node_raised"); } } void GraphEdit::_notification(int p_what) { - if (p_what==NOTIFICATION_READY) { + if (p_what == NOTIFICATION_READY) { Size2 hmin = h_scroll->get_combined_minimum_size(); Size2 vmin = v_scroll->get_combined_minimum_size(); - v_scroll->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_END,vmin.width); - v_scroll->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,0); - v_scroll->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,0); - v_scroll->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,0); - - h_scroll->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_BEGIN,0); - h_scroll->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,0); - h_scroll->set_anchor_and_margin(MARGIN_TOP,ANCHOR_END,hmin.height); - h_scroll->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,0); + v_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, vmin.width); + v_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0); + v_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0); + v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0); + h_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 0); + h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0); + h_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, hmin.height); + h_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0); zoom_minus->set_icon(get_icon("minus")); zoom_reset->set_icon(get_icon("reset")); zoom_plus->set_icon(get_icon("more")); snap_button->set_icon(get_icon("snap")); //zoom_icon->set_texture( get_icon("Zoom", "EditorIcons")); - } - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { - - draw_style_box( get_stylebox("bg"),Rect2(Point2(),get_size()) ); + draw_style_box(get_stylebox("bg"), Rect2(Point2(), get_size())); if (is_using_snap()) { //draw grid int snap = get_snap(); - Vector2 offset = get_scroll_ofs()/zoom; - Size2 size = get_size()/zoom; + Vector2 offset = get_scroll_ofs() / zoom; + Size2 size = get_size() / zoom; - Point2i from = (offset/float(snap)).floor(); - Point2i len = (size/float(snap)).floor()+Vector2(1,1); + Point2i from = (offset / float(snap)).floor(); + Point2i len = (size / float(snap)).floor() + Vector2(1, 1); Color grid_minor = get_color("grid_minor"); Color grid_major = get_color("grid_major"); - for(int i=from.x;i<from.x+len.x;i++) { + for (int i = from.x; i < from.x + len.x; i++) { Color color; - if (ABS(i)%10==0) - color=grid_major; + if (ABS(i) % 10 == 0) + color = grid_major; else - color=grid_minor; + color = grid_minor; - float base_ofs = i*snap*zoom - offset.x*zoom; - draw_line(Vector2(base_ofs,0),Vector2(base_ofs,get_size().height),color); + float base_ofs = i * snap * zoom - offset.x * zoom; + draw_line(Vector2(base_ofs, 0), Vector2(base_ofs, get_size().height), color); } - for(int i=from.y;i<from.y+len.y;i++) { + for (int i = from.y; i < from.y + len.y; i++) { Color color; - if (ABS(i)%10==0) - color=grid_major; + if (ABS(i) % 10 == 0) + color = grid_major; else - color=grid_minor; + color = grid_minor; - float base_ofs = i*snap*zoom - offset.y*zoom; - draw_line(Vector2(0,base_ofs),Vector2(get_size().width,base_ofs),color); + float base_ofs = i * snap * zoom - offset.y * zoom; + draw_line(Vector2(0, base_ofs), Vector2(get_size().width, base_ofs), color); } - } - - - } - if (p_what==NOTIFICATION_RESIZED) { + if (p_what == NOTIFICATION_RESIZED) { _update_scroll(); top_layer->update(); - } } -bool GraphEdit::_filter_input(const Point2& p_point) { +bool GraphEdit::_filter_input(const Point2 &p_point) { - Ref<Texture> port =get_icon("port","GraphNode"); + Ref<Texture> port = get_icon("port", "GraphNode"); float grab_r_extend = 2.0; - float grab_r=port->get_width()*0.5*grab_r_extend; - for(int i=get_child_count()-1;i>=0;i--) { + float grab_r = port->get_width() * 0.5 * grab_r_extend; + for (int i = get_child_count() - 1; i >= 0; i--) { - GraphNode *gn=get_child(i)->cast_to<GraphNode>(); + GraphNode *gn = get_child(i)->cast_to<GraphNode>(); if (!gn) continue; - for(int j=0;j<gn->get_connection_output_count();j++) { + for (int j = 0; j < gn->get_connection_output_count(); j++) { - Vector2 pos = gn->get_connection_output_pos(j)+gn->get_pos(); - if (pos.distance_to(p_point)<grab_r) + Vector2 pos = gn->get_connection_output_pos(j) + gn->get_pos(); + if (pos.distance_to(p_point) < grab_r) return true; - - } - for(int j=0;j<gn->get_connection_input_count();j++) { + for (int j = 0; j < gn->get_connection_input_count(); j++) { - Vector2 pos = gn->get_connection_input_pos(j)+gn->get_pos(); - if (pos.distance_to(p_point)<grab_r) { + Vector2 pos = gn->get_connection_input_pos(j) + gn->get_pos(); + if (pos.distance_to(p_point) < grab_r) { return true; } - - } - } return false; } -void GraphEdit::_top_layer_input(const InputEvent& p_ev) { - +void GraphEdit::_top_layer_input(const InputEvent &p_ev) { float grab_r_extend = 2.0; - if (p_ev.type==InputEvent::MOUSE_BUTTON && p_ev.mouse_button.button_index==BUTTON_LEFT && p_ev.mouse_button.pressed) { + if (p_ev.type == InputEvent::MOUSE_BUTTON && p_ev.mouse_button.button_index == BUTTON_LEFT && p_ev.mouse_button.pressed) { - Ref<Texture> port =get_icon("port","GraphNode"); - Vector2 mpos(p_ev.mouse_button.x,p_ev.mouse_button.y); - float grab_r=port->get_width()*0.5*grab_r_extend; - for(int i=get_child_count()-1;i>=0;i--) { + Ref<Texture> port = get_icon("port", "GraphNode"); + Vector2 mpos(p_ev.mouse_button.x, p_ev.mouse_button.y); + float grab_r = port->get_width() * 0.5 * grab_r_extend; + for (int i = get_child_count() - 1; i >= 0; i--) { - GraphNode *gn=get_child(i)->cast_to<GraphNode>(); + GraphNode *gn = get_child(i)->cast_to<GraphNode>(); if (!gn) continue; - for(int j=0;j<gn->get_connection_output_count();j++) { - - Vector2 pos = gn->get_connection_output_pos(j)+gn->get_pos(); - if (pos.distance_to(mpos)<grab_r) { + for (int j = 0; j < gn->get_connection_output_count(); j++) { + Vector2 pos = gn->get_connection_output_pos(j) + gn->get_pos(); + if (pos.distance_to(mpos) < grab_r) { if (valid_left_disconnect_types.has(gn->get_connection_output_type(j))) { //check disconnect - for (List<Connection>::Element*E=connections.front();E;E=E->next()) { + for (List<Connection>::Element *E = connections.front(); E; E = E->next()) { - if (E->get().from==gn->get_name() && E->get().from_port==j) { + if (E->get().from == gn->get_name() && E->get().from_port == j) { - Node*to = get_node(String(E->get().to)); + Node *to = get_node(String(E->get().to)); if (to && to->cast_to<GraphNode>()) { - connecting_from=E->get().to; - connecting_index=E->get().to_port; - connecting_out=false; - connecting_type=to->cast_to<GraphNode>()->get_connection_input_type(E->get().to_port); - connecting_color=to->cast_to<GraphNode>()->get_connection_input_color(E->get().to_port); - connecting_target=false; - connecting_to=pos; - just_disconected=true; + connecting_from = E->get().to; + connecting_index = E->get().to_port; + connecting_out = false; + connecting_type = to->cast_to<GraphNode>()->get_connection_input_type(E->get().to_port); + connecting_color = to->cast_to<GraphNode>()->get_connection_input_color(E->get().to_port); + connecting_target = false; + connecting_to = pos; + just_disconected = true; - emit_signal("disconnection_request",E->get().from,E->get().from_port,E->get().to,E->get().to_port); + emit_signal("disconnection_request", E->get().from, E->get().from_port, E->get().to, E->get().to_port); to = get_node(String(connecting_from)); //maybe it was erased if (to && to->cast_to<GraphNode>()) { - connecting=true; + connecting = true; } return; } - } } } - connecting=true; - connecting_from=gn->get_name(); - connecting_index=j; - connecting_out=true; - connecting_type=gn->get_connection_output_type(j); - connecting_color=gn->get_connection_output_color(j); - connecting_target=false; - connecting_to=pos; - just_disconected=false; + connecting = true; + connecting_from = gn->get_name(); + connecting_index = j; + connecting_out = true; + connecting_type = gn->get_connection_output_type(j); + connecting_color = gn->get_connection_output_color(j); + connecting_target = false; + connecting_to = pos; + just_disconected = false; return; } - - } - for(int j=0;j<gn->get_connection_input_count();j++) { + for (int j = 0; j < gn->get_connection_input_count(); j++) { - Vector2 pos = gn->get_connection_input_pos(j)+gn->get_pos(); + Vector2 pos = gn->get_connection_input_pos(j) + gn->get_pos(); - if (pos.distance_to(mpos)<grab_r) { + if (pos.distance_to(mpos) < grab_r) { if (right_disconnects || valid_right_disconnect_types.has(gn->get_connection_input_type(j))) { //check disconnect - for (List<Connection>::Element*E=connections.front();E;E=E->next()) { + for (List<Connection>::Element *E = connections.front(); E; E = E->next()) { - if (E->get().to==gn->get_name() && E->get().to_port==j) { + if (E->get().to == gn->get_name() && E->get().to_port == j) { - Node*fr = get_node(String(E->get().from)); + Node *fr = get_node(String(E->get().from)); if (fr && fr->cast_to<GraphNode>()) { - connecting_from=E->get().from; - connecting_index=E->get().from_port; - connecting_out=true; - connecting_type=fr->cast_to<GraphNode>()->get_connection_output_type(E->get().from_port); - connecting_color=fr->cast_to<GraphNode>()->get_connection_output_color(E->get().from_port); - connecting_target=false; - connecting_to=pos; - just_disconected=true; + connecting_from = E->get().from; + connecting_index = E->get().from_port; + connecting_out = true; + connecting_type = fr->cast_to<GraphNode>()->get_connection_output_type(E->get().from_port); + connecting_color = fr->cast_to<GraphNode>()->get_connection_output_color(E->get().from_port); + connecting_target = false; + connecting_to = pos; + just_disconected = true; - emit_signal("disconnection_request",E->get().from,E->get().from_port,E->get().to,E->get().to_port); + emit_signal("disconnection_request", E->get().from, E->get().from_port, E->get().to, E->get().to_port); fr = get_node(String(connecting_from)); //maybe it was erased if (fr && fr->cast_to<GraphNode>()) { - connecting=true; + connecting = true; } return; } - } } } - - connecting=true; - connecting_from=gn->get_name(); - connecting_index=j; - connecting_out=false; - connecting_type=gn->get_connection_input_type(j); - connecting_color=gn->get_connection_input_color(j); - connecting_target=false; - connecting_to=pos; - just_disconected=true; + connecting = true; + connecting_from = gn->get_name(); + connecting_index = j; + connecting_out = false; + connecting_type = gn->get_connection_input_type(j); + connecting_color = gn->get_connection_input_color(j); + connecting_target = false; + connecting_to = pos; + just_disconected = true; return; } - - } } } - if (p_ev.type==InputEvent::MOUSE_MOTION && connecting) { + if (p_ev.type == InputEvent::MOUSE_MOTION && connecting) { - connecting_to=Vector2(p_ev.mouse_motion.x,p_ev.mouse_motion.y); - connecting_target=false; - top_layer->update(); + connecting_to = Vector2(p_ev.mouse_motion.x, p_ev.mouse_motion.y); + connecting_target = false; + top_layer->update(); - Ref<Texture> port =get_icon("port","GraphNode"); - Vector2 mpos(p_ev.mouse_button.x,p_ev.mouse_button.y); - float grab_r=port->get_width()*0.5*grab_r_extend; - for(int i=get_child_count()-1;i>=0;i--) { + Ref<Texture> port = get_icon("port", "GraphNode"); + Vector2 mpos(p_ev.mouse_button.x, p_ev.mouse_button.y); + float grab_r = port->get_width() * 0.5 * grab_r_extend; + for (int i = get_child_count() - 1; i >= 0; i--) { - GraphNode *gn=get_child(i)->cast_to<GraphNode>(); + GraphNode *gn = get_child(i)->cast_to<GraphNode>(); if (!gn) continue; if (!connecting_out) { - for(int j=0;j<gn->get_connection_output_count();j++) { + for (int j = 0; j < gn->get_connection_output_count(); j++) { - Vector2 pos = gn->get_connection_output_pos(j)+gn->get_pos(); - int type =gn->get_connection_output_type(j); - if ((type==connecting_type ||valid_connection_types.has(ConnType(type,connecting_type))) && pos.distance_to(mpos)<grab_r) { + Vector2 pos = gn->get_connection_output_pos(j) + gn->get_pos(); + int type = gn->get_connection_output_type(j); + if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && pos.distance_to(mpos) < grab_r) { - connecting_target=true; - connecting_to=pos; - connecting_target_to=gn->get_name(); - connecting_target_index=j; + connecting_target = true; + connecting_to = pos; + connecting_target_to = gn->get_name(); + connecting_target_index = j; return; } - - } } else { - for(int j=0;j<gn->get_connection_input_count();j++) { + for (int j = 0; j < gn->get_connection_input_count(); j++) { - Vector2 pos = gn->get_connection_input_pos(j)+gn->get_pos(); - int type =gn->get_connection_input_type(j); - if ((type==connecting_type ||valid_connection_types.has(ConnType(type,connecting_type))) && pos.distance_to(mpos)<grab_r) { - connecting_target=true; - connecting_to=pos; - connecting_target_to=gn->get_name(); - connecting_target_index=j; + Vector2 pos = gn->get_connection_input_pos(j) + gn->get_pos(); + int type = gn->get_connection_input_type(j); + if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && pos.distance_to(mpos) < grab_r) { + connecting_target = true; + connecting_to = pos; + connecting_target_to = gn->get_name(); + connecting_target_index = j; return; } } @@ -562,100 +525,88 @@ void GraphEdit::_top_layer_input(const InputEvent& p_ev) { } } - if (p_ev.type==InputEvent::MOUSE_BUTTON && p_ev.mouse_button.button_index==BUTTON_LEFT && !p_ev.mouse_button.pressed) { + if (p_ev.type == InputEvent::MOUSE_BUTTON && p_ev.mouse_button.button_index == BUTTON_LEFT && !p_ev.mouse_button.pressed) { if (connecting && connecting_target) { String from = connecting_from; int from_slot = connecting_index; - String to =connecting_target_to; + String to = connecting_target_to; int to_slot = connecting_target_index; if (!connecting_out) { - SWAP(from,to); - SWAP(from_slot,to_slot); + SWAP(from, to); + SWAP(from_slot, to_slot); } - emit_signal("connection_request",from,from_slot,to,to_slot); + emit_signal("connection_request", from, from_slot, to, to_slot); } else if (!just_disconected) { String from = connecting_from; int from_slot = connecting_index; - Vector2 ofs = Vector2(p_ev.mouse_button.x,p_ev.mouse_button.y); - emit_signal("connection_to_empty",from,from_slot,ofs); + Vector2 ofs = Vector2(p_ev.mouse_button.x, p_ev.mouse_button.y); + emit_signal("connection_to_empty", from, from_slot, ofs); } - connecting=false; + connecting = false; top_layer->update(); update(); connections_layer->update(); - } - } - -template<class Vector2> +template <class Vector2> static _FORCE_INLINE_ Vector2 _bezier_interp(real_t t, Vector2 start, Vector2 control_1, Vector2 control_2, Vector2 end) { - /* Formula from Wikipedia article on Bezier curves. */ + /* Formula from Wikipedia article on Bezier curves. */ real_t omt = (1.0 - t); - real_t omt2 = omt*omt; - real_t omt3 = omt2*omt; - real_t t2 = t*t; - real_t t3 = t2*t; - - return start * omt3 - + control_1 * omt2 * t * 3.0 - + control_2 * omt * t2 * 3.0 - + end * t3; -} + real_t omt2 = omt * omt; + real_t omt3 = omt2 * omt; + real_t t2 = t * t; + real_t t3 = t2 * t; + return start * omt3 + control_1 * omt2 * t * 3.0 + control_2 * omt * t2 * 3.0 + end * t3; +} -void GraphEdit::_bake_segment2d(CanvasItem* p_where,float p_begin, float p_end,const Vector2& p_a,const Vector2& p_out,const Vector2& p_b, const Vector2& p_in,int p_depth,int p_min_depth,int p_max_depth,float p_tol,const Color& p_color,const Color& p_to_color,int &lines) const { +void GraphEdit::_bake_segment2d(CanvasItem *p_where, float p_begin, float p_end, const Vector2 &p_a, const Vector2 &p_out, const Vector2 &p_b, const Vector2 &p_in, int p_depth, int p_min_depth, int p_max_depth, float p_tol, const Color &p_color, const Color &p_to_color, int &lines) const { - float mp = p_begin+(p_end-p_begin)*0.5; - Vector2 beg = _bezier_interp(p_begin,p_a,p_a+p_out,p_b+p_in,p_b); - Vector2 mid = _bezier_interp(mp,p_a,p_a+p_out,p_b+p_in,p_b); - Vector2 end = _bezier_interp(p_end,p_a,p_a+p_out,p_b+p_in,p_b); + float mp = p_begin + (p_end - p_begin) * 0.5; + Vector2 beg = _bezier_interp(p_begin, p_a, p_a + p_out, p_b + p_in, p_b); + Vector2 mid = _bezier_interp(mp, p_a, p_a + p_out, p_b + p_in, p_b); + Vector2 end = _bezier_interp(p_end, p_a, p_a + p_out, p_b + p_in, p_b); - Vector2 na = (mid-beg).normalized(); - Vector2 nb = (end-mid).normalized(); + Vector2 na = (mid - beg).normalized(); + Vector2 nb = (end - mid).normalized(); float dp = Math::rad2deg(Math::acos(na.dot(nb))); - if (p_depth>=p_min_depth && ( dp<p_tol || p_depth>=p_max_depth)) { - - + if (p_depth >= p_min_depth && (dp < p_tol || p_depth >= p_max_depth)) { - p_where->draw_line(beg,end,p_color.linear_interpolate(p_to_color,mp),2,true); + p_where->draw_line(beg, end, p_color.linear_interpolate(p_to_color, mp), 2, true); lines++; } else { - _bake_segment2d(p_where,p_begin,mp,p_a,p_out,p_b,p_in,p_depth+1,p_min_depth,p_max_depth,p_tol,p_color,p_to_color,lines); - _bake_segment2d(p_where,mp,p_end,p_a,p_out,p_b,p_in,p_depth+1,p_min_depth,p_max_depth,p_tol,p_color,p_to_color,lines); + _bake_segment2d(p_where, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_min_depth, p_max_depth, p_tol, p_color, p_to_color, lines); + _bake_segment2d(p_where, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_min_depth, p_max_depth, p_tol, p_color, p_to_color, lines); } } - -void GraphEdit::_draw_cos_line(CanvasItem* p_where,const Vector2& p_from, const Vector2& p_to,const Color& p_color,const Color& p_to_color) { - +void GraphEdit::_draw_cos_line(CanvasItem *p_where, const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, const Color &p_to_color) { #if 1 //cubic bezier code - float diff = p_to.x-p_from.x; + float diff = p_to.x - p_from.x; float cp_offset; int cp_len = get_constant("bezier_len_pos"); int cp_neg_len = get_constant("bezier_len_neg"); - if (diff>0) { - cp_offset=MAX(cp_len,diff*0.5); + if (diff > 0) { + cp_offset = MAX(cp_len, diff * 0.5); } else { - cp_offset=MAX(MIN(cp_len-diff,cp_neg_len),-diff*0.5); + cp_offset = MAX(MIN(cp_len - diff, cp_neg_len), -diff * 0.5); } - Vector2 c1 = Vector2(cp_offset*zoom,0); - Vector2 c2 = Vector2(-cp_offset*zoom,0); - - int lines=0; - _bake_segment2d(p_where,0,1,p_from,c1,p_to,c2,0,3,9,8,p_color,p_to_color,lines); + Vector2 c1 = Vector2(cp_offset * zoom, 0); + Vector2 c2 = Vector2(-cp_offset * zoom, 0); + int lines = 0; + _bake_segment2d(p_where, 0, 1, p_from, c1, p_to, c2, 0, 3, 9, 8, p_color, p_to_color, lines); #else @@ -663,43 +614,40 @@ void GraphEdit::_draw_cos_line(CanvasItem* p_where,const Vector2& p_from, const //old cosine code Rect2 r; - r.pos=p_from; + r.pos = p_from; r.expand_to(p_to); - Vector2 sign=Vector2((p_from.x < p_to.x) ? 1 : -1,(p_from.y < p_to.y) ? 1 : -1); + Vector2 sign = Vector2((p_from.x < p_to.x) ? 1 : -1, (p_from.y < p_to.y) ? 1 : -1); bool flip = sign.x * sign.y < 0; Vector2 prev; - for(int i=0;i<=steps;i++) { - + for (int i = 0; i <= steps; i++) { - float d = i/float(steps); - float c=-Math::cos(d*Math_PI) * 0.5+0.5; + float d = i / float(steps); + float c = -Math::cos(d * Math_PI) * 0.5 + 0.5; if (flip) - c=1.0-c; - Vector2 p = r.pos+Vector2(d*r.size.width,c*r.size.height); + c = 1.0 - c; + Vector2 p = r.pos + Vector2(d * r.size.width, c * r.size.height); - if (i>0) { + if (i > 0) { - p_where->draw_line(prev,p,p_color.linear_interpolate(p_to_color,d),2); + p_where->draw_line(prev, p, p_color.linear_interpolate(p_to_color, d), 2); } - prev=p; + prev = p; } #endif } - void GraphEdit::_connections_layer_draw() { - { //draw connections - List<List<Connection>::Element* > to_erase; - for(List<Connection>::Element *E=connections.front();E;E=E->next()) { + List<List<Connection>::Element *> to_erase; + for (List<Connection>::Element *E = connections.front(); E; E = E->next()) { NodePath fromnp(E->get().from); - Node * from = get_node(fromnp); + Node *from = get_node(fromnp); if (!from) { to_erase.push_back(E); continue; @@ -713,7 +661,7 @@ void GraphEdit::_connections_layer_draw() { } NodePath tonp(E->get().to); - Node * to = get_node(tonp); + Node *to = get_node(tonp); if (!to) { to_erase.push_back(E); continue; @@ -726,15 +674,14 @@ void GraphEdit::_connections_layer_draw() { continue; } - Vector2 frompos=gfrom->get_connection_output_pos(E->get().from_port)+gfrom->get_offset()*zoom; + Vector2 frompos = gfrom->get_connection_output_pos(E->get().from_port) + gfrom->get_offset() * zoom; Color color = gfrom->get_connection_output_color(E->get().from_port); - Vector2 topos=gto->get_connection_input_pos(E->get().to_port)+gto->get_offset()*zoom; + Vector2 topos = gto->get_connection_input_pos(E->get().to_port) + gto->get_offset() * zoom; Color tocolor = gto->get_connection_input_color(E->get().to_port); - _draw_cos_line(connections_layer,frompos,topos,color,tocolor); - + _draw_cos_line(connections_layer, frompos, topos, color, tocolor); } - while(to_erase.size()) { + while (to_erase.size()) { connections.erase(to_erase.front()->get()); to_erase.pop_front(); } @@ -753,66 +700,65 @@ void GraphEdit::_top_layer_draw() { ERR_FAIL_COND(!from); Vector2 pos; if (connecting_out) - pos=from->get_connection_output_pos(connecting_index); + pos = from->get_connection_output_pos(connecting_index); else - pos=from->get_connection_input_pos(connecting_index); - pos+=from->get_pos(); + pos = from->get_connection_input_pos(connecting_index); + pos += from->get_pos(); Vector2 topos; - topos=connecting_to; + topos = connecting_to; - Color col=connecting_color; + Color col = connecting_color; if (connecting_target) { - col.r+=0.4; - col.g+=0.4; - col.b+=0.4; + col.r += 0.4; + col.g += 0.4; + col.b += 0.4; } if (!connecting_out) { - SWAP(pos,topos); + SWAP(pos, topos); } - _draw_cos_line(top_layer,pos,topos,col,col); + _draw_cos_line(top_layer, pos, topos, col, col); } - if (box_selecting) - top_layer->draw_rect(box_selecting_rect,Color(0.7,0.7,1.0,0.3)); + top_layer->draw_rect(box_selecting_rect, Color(0.7, 0.7, 1.0, 0.3)); } -void GraphEdit::set_selected(Node* p_child) { +void GraphEdit::set_selected(Node *p_child) { - for(int i=get_child_count()-1;i>=0;i--) { + for (int i = get_child_count() - 1; i >= 0; i--) { - GraphNode *gn=get_child(i)->cast_to<GraphNode>(); + GraphNode *gn = get_child(i)->cast_to<GraphNode>(); if (!gn) continue; - gn->set_selected(gn==p_child); + gn->set_selected(gn == p_child); } } -void GraphEdit::_gui_input(const InputEvent& p_ev) { +void GraphEdit::_gui_input(const InputEvent &p_ev) { - if (p_ev.type==InputEvent::MOUSE_MOTION && (p_ev.mouse_motion.button_mask&BUTTON_MASK_MIDDLE || (p_ev.mouse_motion.button_mask&BUTTON_MASK_LEFT && Input::get_singleton()->is_key_pressed(KEY_SPACE)))) { - h_scroll->set_value( h_scroll->get_value() - p_ev.mouse_motion.relative_x ); - v_scroll->set_value( v_scroll->get_value() - p_ev.mouse_motion.relative_y ); + if (p_ev.type == InputEvent::MOUSE_MOTION && (p_ev.mouse_motion.button_mask & BUTTON_MASK_MIDDLE || (p_ev.mouse_motion.button_mask & BUTTON_MASK_LEFT && Input::get_singleton()->is_key_pressed(KEY_SPACE)))) { + h_scroll->set_value(h_scroll->get_value() - p_ev.mouse_motion.relative_x); + v_scroll->set_value(v_scroll->get_value() - p_ev.mouse_motion.relative_y); } - if (p_ev.type==InputEvent::MOUSE_MOTION && dragging) { + if (p_ev.type == InputEvent::MOUSE_MOTION && dragging) { - just_selected=true; + just_selected = true; // TODO: Remove local mouse pos hack if/when InputEventMouseMotion is fixed to support floats //drag_accum+=Vector2(p_ev.mouse_motion.relative_x,p_ev.mouse_motion.relative_y); drag_accum = get_local_mouse_pos() - drag_origin; - for(int i=get_child_count()-1;i>=0;i--) { - GraphNode *gn=get_child(i)->cast_to<GraphNode>(); + for (int i = get_child_count() - 1; i >= 0; i--) { + GraphNode *gn = get_child(i)->cast_to<GraphNode>(); if (gn && gn->is_selected()) { - Vector2 pos = (gn->get_drag_from()*zoom+drag_accum)/zoom; + Vector2 pos = (gn->get_drag_from() * zoom + drag_accum) / zoom; if (is_using_snap()) { int snap = get_snap(); - pos = pos.snapped(Vector2(snap,snap)); + pos = pos.snapped(Vector2(snap, snap)); } gn->set_offset(pos); @@ -820,48 +766,47 @@ void GraphEdit::_gui_input(const InputEvent& p_ev) { } } - if (p_ev.type==InputEvent::MOUSE_MOTION && box_selecting) { + if (p_ev.type == InputEvent::MOUSE_MOTION && box_selecting) { box_selecting_to = get_local_mouse_pos(); - box_selecting_rect = Rect2(MIN(box_selecting_from.x,box_selecting_to.x), - MIN(box_selecting_from.y,box_selecting_to.y), - ABS(box_selecting_from.x-box_selecting_to.x), - ABS(box_selecting_from.y-box_selecting_to.y)); + box_selecting_rect = Rect2(MIN(box_selecting_from.x, box_selecting_to.x), + MIN(box_selecting_from.y, box_selecting_to.y), + ABS(box_selecting_from.x - box_selecting_to.x), + ABS(box_selecting_from.y - box_selecting_to.y)); - for(int i=get_child_count()-1;i>=0;i--) { + for (int i = get_child_count() - 1; i >= 0; i--) { - GraphNode *gn=get_child(i)->cast_to<GraphNode>(); + GraphNode *gn = get_child(i)->cast_to<GraphNode>(); if (!gn) continue; Rect2 r = gn->get_rect(); - r.size*=zoom; + r.size *= zoom; bool in_box = r.intersects(box_selecting_rect); if (in_box) gn->set_selected(box_selection_mode_aditive); else - gn->set_selected(previus_selected.find(gn)!=NULL); + gn->set_selected(previus_selected.find(gn) != NULL); } top_layer->update(); } - if (p_ev.type==InputEvent::MOUSE_BUTTON) { + if (p_ev.type == InputEvent::MOUSE_BUTTON) { - const InputEventMouseButton &b=p_ev.mouse_button; + const InputEventMouseButton &b = p_ev.mouse_button; - if (b.button_index==BUTTON_RIGHT && b.pressed) - { + if (b.button_index == BUTTON_RIGHT && b.pressed) { if (box_selecting) { box_selecting = false; - for(int i=get_child_count()-1;i>=0;i--) { + for (int i = get_child_count() - 1; i >= 0; i--) { - GraphNode *gn=get_child(i)->cast_to<GraphNode>(); + GraphNode *gn = get_child(i)->cast_to<GraphNode>(); if (!gn) continue; - gn->set_selected(previus_selected.find(gn)!=NULL); + gn->set_selected(previus_selected.find(gn) != NULL); } top_layer->update(); } else { @@ -874,27 +819,27 @@ void GraphEdit::_gui_input(const InputEvent& p_ev) { } } - if (b.button_index==BUTTON_LEFT && !b.pressed && dragging) { - if (!just_selected && drag_accum==Vector2() && Input::get_singleton()->is_key_pressed(KEY_CONTROL)) { + if (b.button_index == BUTTON_LEFT && !b.pressed && dragging) { + if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(KEY_CONTROL)) { //deselect current node - for(int i=get_child_count()-1;i>=0;i--) { - GraphNode *gn=get_child(i)->cast_to<GraphNode>(); + for (int i = get_child_count() - 1; i >= 0; i--) { + GraphNode *gn = get_child(i)->cast_to<GraphNode>(); if (gn) { Rect2 r = gn->get_rect(); - r.size*=zoom; + r.size *= zoom; if (r.has_point(get_local_mouse_pos())) gn->set_selected(false); } } } - if (drag_accum!=Vector2()) { + if (drag_accum != Vector2()) { emit_signal("_begin_node_move"); - for(int i=get_child_count()-1;i>=0;i--) { - GraphNode *gn=get_child(i)->cast_to<GraphNode>(); + for (int i = get_child_count() - 1; i >= 0; i--) { + GraphNode *gn = get_child(i)->cast_to<GraphNode>(); if (gn && gn->is_selected()) gn->set_drag(false); } @@ -909,13 +854,13 @@ void GraphEdit::_gui_input(const InputEvent& p_ev) { connections_layer->update(); } - if (b.button_index==BUTTON_LEFT && b.pressed) { + if (b.button_index == BUTTON_LEFT && b.pressed) { GraphNode *gn = NULL; GraphNode *gn_selected = NULL; - for(int i=get_child_count()-1;i>=0;i--) { + for (int i = get_child_count() - 1; i >= 0; i--) { - gn_selected=get_child(i)->cast_to<GraphNode>(); + gn_selected = get_child(i)->cast_to<GraphNode>(); if (gn_selected) { @@ -923,23 +868,23 @@ void GraphEdit::_gui_input(const InputEvent& p_ev) { continue; Rect2 r = gn_selected->get_rect(); - r.size*=zoom; + r.size *= zoom; if (r.has_point(get_local_mouse_pos())) gn = gn_selected; - break; + break; } } if (gn) { - if (_filter_input(Vector2(b.x,b.y))) + if (_filter_input(Vector2(b.x, b.y))) return; dragging = true; drag_accum = Vector2(); drag_origin = get_local_mouse_pos(); just_selected = !gn->is_selected(); - if(!gn->is_selected() && !Input::get_singleton()->is_key_pressed(KEY_CONTROL)) { + if (!gn->is_selected() && !Input::get_singleton()->is_key_pressed(KEY_CONTROL)) { for (int i = 0; i < get_child_count(); i++) { GraphNode *o_gn = get_child(i)->cast_to<GraphNode>(); if (o_gn) @@ -957,7 +902,7 @@ void GraphEdit::_gui_input(const InputEvent& p_ev) { } } else { - if (_filter_input(Vector2(b.x,b.y))) + if (_filter_input(Vector2(b.x, b.y))) return; if (Input::get_singleton()->is_key_pressed(KEY_SPACE)) return; @@ -967,9 +912,9 @@ void GraphEdit::_gui_input(const InputEvent& p_ev) { if (b.mod.control) { box_selection_mode_aditive = true; previus_selected.clear(); - for(int i=get_child_count()-1;i>=0;i--) { + for (int i = get_child_count() - 1; i >= 0; i--) { - GraphNode *gn=get_child(i)->cast_to<GraphNode>(); + GraphNode *gn = get_child(i)->cast_to<GraphNode>(); if (!gn || !gn->is_selected()) continue; @@ -978,9 +923,9 @@ void GraphEdit::_gui_input(const InputEvent& p_ev) { } else if (b.mod.shift) { box_selection_mode_aditive = false; previus_selected.clear(); - for(int i=get_child_count()-1;i>=0;i--) { + for (int i = get_child_count() - 1; i >= 0; i--) { - GraphNode *gn=get_child(i)->cast_to<GraphNode>(); + GraphNode *gn = get_child(i)->cast_to<GraphNode>(); if (!gn || !gn->is_selected()) continue; @@ -989,9 +934,9 @@ void GraphEdit::_gui_input(const InputEvent& p_ev) { } else { box_selection_mode_aditive = true; previus_selected.clear(); - for(int i=get_child_count()-1;i>=0;i--) { + for (int i = get_child_count() - 1; i >= 0; i--) { - GraphNode *gn=get_child(i)->cast_to<GraphNode>(); + GraphNode *gn = get_child(i)->cast_to<GraphNode>(); if (!gn) continue; @@ -1001,33 +946,32 @@ void GraphEdit::_gui_input(const InputEvent& p_ev) { } } - if (b.button_index==BUTTON_LEFT && !b.pressed && box_selecting) { + if (b.button_index == BUTTON_LEFT && !b.pressed && box_selecting) { box_selecting = false; previus_selected.clear(); top_layer->update(); } - if (b.button_index==BUTTON_WHEEL_UP && b.pressed) { + if (b.button_index == BUTTON_WHEEL_UP && b.pressed) { //too difficult to get right //set_zoom(zoom*ZOOM_SCALE); } - if (b.button_index==BUTTON_WHEEL_DOWN && b.pressed) { + if (b.button_index == BUTTON_WHEEL_DOWN && b.pressed) { //too difficult to get right //set_zoom(zoom/ZOOM_SCALE); } } - if (p_ev.type==InputEvent::KEY && p_ev.key.scancode==KEY_D && p_ev.key.pressed && p_ev.key.mod.command) { + if (p_ev.type == InputEvent::KEY && p_ev.key.scancode == KEY_D && p_ev.key.pressed && p_ev.key.mod.command) { emit_signal("duplicate_nodes_request"); accept_event(); } - if (p_ev.type==InputEvent::KEY && p_ev.key.scancode==KEY_DELETE && p_ev.key.pressed) { + if (p_ev.type == InputEvent::KEY && p_ev.key.scancode == KEY_DELETE && p_ev.key.pressed) { emit_signal("delete_nodes_request"); accept_event(); } - } void GraphEdit::clear_connections() { @@ -1039,14 +983,14 @@ void GraphEdit::clear_connections() { void GraphEdit::set_zoom(float p_zoom) { - p_zoom=CLAMP(p_zoom,MIN_ZOOM,MAX_ZOOM); + p_zoom = CLAMP(p_zoom, MIN_ZOOM, MAX_ZOOM); if (zoom == p_zoom) return; - zoom_minus->set_disabled(zoom==MIN_ZOOM); - zoom_plus->set_disabled(zoom==MAX_ZOOM); + zoom_minus->set_disabled(zoom == MIN_ZOOM); + zoom_plus->set_disabled(zoom == MAX_ZOOM); - Vector2 sbofs = (Vector2( h_scroll->get_value(), v_scroll->get_value() ) + get_size()/2)/zoom; + Vector2 sbofs = (Vector2(h_scroll->get_value(), v_scroll->get_value()) + get_size() / 2) / zoom; zoom = p_zoom; top_layer->update(); @@ -1056,12 +1000,11 @@ void GraphEdit::set_zoom(float p_zoom) { if (is_visible_in_tree()) { - Vector2 ofs = sbofs*zoom - get_size()/2; - h_scroll->set_value( ofs.x ); - v_scroll->set_value( ofs.y ); + Vector2 ofs = sbofs * zoom - get_size() / 2; + h_scroll->set_value(ofs.x); + v_scroll->set_value(ofs.y); } - update(); } @@ -1071,10 +1014,10 @@ float GraphEdit::get_zoom() const { void GraphEdit::set_right_disconnects(bool p_enable) { - right_disconnects=p_enable; + right_disconnects = p_enable; } -bool GraphEdit::is_right_disconnects_enabled() const{ +bool GraphEdit::is_right_disconnects_enabled() const { return right_disconnects; } @@ -1084,22 +1027,19 @@ void GraphEdit::add_valid_right_disconnect_type(int p_type) { valid_right_disconnect_types.insert(p_type); } -void GraphEdit::remove_valid_right_disconnect_type(int p_type){ +void GraphEdit::remove_valid_right_disconnect_type(int p_type) { valid_right_disconnect_types.erase(p_type); - } -void GraphEdit::add_valid_left_disconnect_type(int p_type){ +void GraphEdit::add_valid_left_disconnect_type(int p_type) { valid_left_disconnect_types.insert(p_type); - } -void GraphEdit::remove_valid_left_disconnect_type(int p_type){ +void GraphEdit::remove_valid_left_disconnect_type(int p_type) { valid_left_disconnect_types.erase(p_type); - } Array GraphEdit::_get_connection_list() const { @@ -1107,85 +1047,77 @@ Array GraphEdit::_get_connection_list() const { List<Connection> conns; get_connection_list(&conns); Array arr; - for(List<Connection>::Element *E=conns.front();E;E=E->next()) { + for (List<Connection>::Element *E = conns.front(); E; E = E->next()) { Dictionary d; - d["from"]=E->get().from; - d["from_port"]=E->get().from_port; - d["to"]=E->get().to; - d["to_port"]=E->get().to_port; + d["from"] = E->get().from; + d["from_port"] = E->get().from_port; + d["to"] = E->get().to; + d["to_port"] = E->get().to_port; arr.push_back(d); } return arr; } - - void GraphEdit::_zoom_minus() { - - set_zoom(zoom/ZOOM_SCALE); + set_zoom(zoom / ZOOM_SCALE); } void GraphEdit::_zoom_reset() { - set_zoom(1); } void GraphEdit::_zoom_plus() { - set_zoom(zoom*ZOOM_SCALE); + set_zoom(zoom * ZOOM_SCALE); } -void GraphEdit::add_valid_connection_type(int p_type,int p_with_type) { +void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) { ConnType ct; - ct.type_a=p_type; - ct.type_b=p_with_type; + ct.type_a = p_type; + ct.type_b = p_with_type; valid_connection_types.insert(ct); } -void GraphEdit::remove_valid_connection_type(int p_type,int p_with_type) { +void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) { ConnType ct; - ct.type_a=p_type; - ct.type_b=p_with_type; + ct.type_a = p_type; + ct.type_b = p_with_type; valid_connection_types.erase(ct); - } -bool GraphEdit::is_valid_connection_type(int p_type,int p_with_type) const { +bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const { ConnType ct; - ct.type_a=p_type; - ct.type_b=p_with_type; + ct.type_a = p_type; + ct.type_b = p_with_type; return valid_connection_types.has(ct); - } void GraphEdit::set_use_snap(bool p_enable) { snap_button->set_pressed(p_enable); update(); - } -bool GraphEdit::is_using_snap() const{ +bool GraphEdit::is_using_snap() const { return snap_button->is_pressed(); - } -int GraphEdit::get_snap() const{ +int GraphEdit::get_snap() const { return snap_amount->get_value(); } void GraphEdit::set_snap(int p_snap) { - ERR_FAIL_COND(p_snap<5); + ERR_FAIL_COND(p_snap < 5); snap_amount->set_value(p_snap); update(); } @@ -1198,78 +1130,73 @@ void GraphEdit::_snap_value_changed(double) { update(); } - void GraphEdit::_bind_methods() { - ClassDB::bind_method(D_METHOD("connect_node:Error","from","from_port","to","to_port"),&GraphEdit::connect_node); - ClassDB::bind_method(D_METHOD("is_node_connected","from","from_port","to","to_port"),&GraphEdit::is_node_connected); - ClassDB::bind_method(D_METHOD("disconnect_node","from","from_port","to","to_port"),&GraphEdit::disconnect_node); - ClassDB::bind_method(D_METHOD("get_connection_list"),&GraphEdit::_get_connection_list); - ClassDB::bind_method(D_METHOD("get_scroll_ofs"),&GraphEdit::get_scroll_ofs); - ClassDB::bind_method(D_METHOD("set_scroll_ofs","ofs"),&GraphEdit::set_scroll_ofs); - - ClassDB::bind_method(D_METHOD("set_zoom","p_zoom"),&GraphEdit::set_zoom); - ClassDB::bind_method(D_METHOD("get_zoom"),&GraphEdit::get_zoom); + ClassDB::bind_method(D_METHOD("connect_node:Error", "from", "from_port", "to", "to_port"), &GraphEdit::connect_node); + ClassDB::bind_method(D_METHOD("is_node_connected", "from", "from_port", "to", "to_port"), &GraphEdit::is_node_connected); + ClassDB::bind_method(D_METHOD("disconnect_node", "from", "from_port", "to", "to_port"), &GraphEdit::disconnect_node); + ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list); + ClassDB::bind_method(D_METHOD("get_scroll_ofs"), &GraphEdit::get_scroll_ofs); + ClassDB::bind_method(D_METHOD("set_scroll_ofs", "ofs"), &GraphEdit::set_scroll_ofs); - ClassDB::bind_method(D_METHOD("set_snap","pixels"),&GraphEdit::set_snap); - ClassDB::bind_method(D_METHOD("get_snap"),&GraphEdit::get_snap); + ClassDB::bind_method(D_METHOD("set_zoom", "p_zoom"), &GraphEdit::set_zoom); + ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom); - ClassDB::bind_method(D_METHOD("set_use_snap","enable"),&GraphEdit::set_use_snap); - ClassDB::bind_method(D_METHOD("is_using_snap"),&GraphEdit::is_using_snap); + ClassDB::bind_method(D_METHOD("set_snap", "pixels"), &GraphEdit::set_snap); + ClassDB::bind_method(D_METHOD("get_snap"), &GraphEdit::get_snap); - ClassDB::bind_method(D_METHOD("set_right_disconnects","enable"),&GraphEdit::set_right_disconnects); - ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"),&GraphEdit::is_right_disconnects_enabled); + ClassDB::bind_method(D_METHOD("set_use_snap", "enable"), &GraphEdit::set_use_snap); + ClassDB::bind_method(D_METHOD("is_using_snap"), &GraphEdit::is_using_snap); - ClassDB::bind_method(D_METHOD("_graph_node_moved"),&GraphEdit::_graph_node_moved); - ClassDB::bind_method(D_METHOD("_graph_node_raised"),&GraphEdit::_graph_node_raised); + ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects); + ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled); - ClassDB::bind_method(D_METHOD("_top_layer_input"),&GraphEdit::_top_layer_input); - ClassDB::bind_method(D_METHOD("_top_layer_draw"),&GraphEdit::_top_layer_draw); - ClassDB::bind_method(D_METHOD("_scroll_moved"),&GraphEdit::_scroll_moved); - ClassDB::bind_method(D_METHOD("_zoom_minus"),&GraphEdit::_zoom_minus); - ClassDB::bind_method(D_METHOD("_zoom_reset"),&GraphEdit::_zoom_reset); - ClassDB::bind_method(D_METHOD("_zoom_plus"),&GraphEdit::_zoom_plus); - ClassDB::bind_method(D_METHOD("_snap_toggled"),&GraphEdit::_snap_toggled); - ClassDB::bind_method(D_METHOD("_snap_value_changed"),&GraphEdit::_snap_value_changed); + ClassDB::bind_method(D_METHOD("_graph_node_moved"), &GraphEdit::_graph_node_moved); + ClassDB::bind_method(D_METHOD("_graph_node_raised"), &GraphEdit::_graph_node_raised); - ClassDB::bind_method(D_METHOD("_gui_input"),&GraphEdit::_gui_input); - ClassDB::bind_method(D_METHOD("_update_scroll_offset"),&GraphEdit::_update_scroll_offset); - ClassDB::bind_method(D_METHOD("_connections_layer_draw"),&GraphEdit::_connections_layer_draw); + ClassDB::bind_method(D_METHOD("_top_layer_input"), &GraphEdit::_top_layer_input); + ClassDB::bind_method(D_METHOD("_top_layer_draw"), &GraphEdit::_top_layer_draw); + ClassDB::bind_method(D_METHOD("_scroll_moved"), &GraphEdit::_scroll_moved); + ClassDB::bind_method(D_METHOD("_zoom_minus"), &GraphEdit::_zoom_minus); + ClassDB::bind_method(D_METHOD("_zoom_reset"), &GraphEdit::_zoom_reset); + ClassDB::bind_method(D_METHOD("_zoom_plus"), &GraphEdit::_zoom_plus); + ClassDB::bind_method(D_METHOD("_snap_toggled"), &GraphEdit::_snap_toggled); + ClassDB::bind_method(D_METHOD("_snap_value_changed"), &GraphEdit::_snap_value_changed); + ClassDB::bind_method(D_METHOD("_gui_input"), &GraphEdit::_gui_input); + ClassDB::bind_method(D_METHOD("_update_scroll_offset"), &GraphEdit::_update_scroll_offset); + ClassDB::bind_method(D_METHOD("_connections_layer_draw"), &GraphEdit::_connections_layer_draw); + ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected); - ClassDB::bind_method(D_METHOD("set_selected","node"),&GraphEdit::set_selected); - - ADD_SIGNAL(MethodInfo("connection_request",PropertyInfo(Variant::STRING,"from"),PropertyInfo(Variant::INT,"from_slot"),PropertyInfo(Variant::STRING,"to"),PropertyInfo(Variant::INT,"to_slot"))); - ADD_SIGNAL(MethodInfo("disconnection_request",PropertyInfo(Variant::STRING,"from"),PropertyInfo(Variant::INT,"from_slot"),PropertyInfo(Variant::STRING,"to"),PropertyInfo(Variant::INT,"to_slot"))); - ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2,"p_position"))); + ADD_SIGNAL(MethodInfo("connection_request", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING, "to"), PropertyInfo(Variant::INT, "to_slot"))); + ADD_SIGNAL(MethodInfo("disconnection_request", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING, "to"), PropertyInfo(Variant::INT, "to_slot"))); + ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "p_position"))); ADD_SIGNAL(MethodInfo("duplicate_nodes_request")); - ADD_SIGNAL(MethodInfo("node_selected",PropertyInfo(Variant::OBJECT,"node"))); - ADD_SIGNAL(MethodInfo("connection_to_empty",PropertyInfo(Variant::STRING,"from"),PropertyInfo(Variant::INT,"from_slot"),PropertyInfo(Variant::VECTOR2,"release_pos"))); + ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node"))); + ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::VECTOR2, "release_pos"))); ADD_SIGNAL(MethodInfo("delete_nodes_request")); ADD_SIGNAL(MethodInfo("_begin_node_move")); ADD_SIGNAL(MethodInfo("_end_node_move")); - ADD_SIGNAL(MethodInfo("scroll_offset_changed",PropertyInfo(Variant::VECTOR2,"ofs"))); + ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "ofs"))); } - - GraphEdit::GraphEdit() { set_focus_mode(FOCUS_ALL); - awaiting_scroll_offset_update=false; - top_layer=NULL; - top_layer=memnew(GraphEditFilter(this)); + awaiting_scroll_offset_update = false; + top_layer = NULL; + top_layer = memnew(GraphEditFilter(this)); add_child(top_layer); top_layer->set_mouse_filter(MOUSE_FILTER_PASS); top_layer->set_area_as_parent_rect(); - top_layer->connect("draw",this,"_top_layer_draw"); + top_layer->connect("draw", this, "_top_layer_draw"); top_layer->set_mouse_filter(MOUSE_FILTER_PASS); - top_layer->connect("gui_input",this,"_top_layer_input"); + top_layer->connect("gui_input", this, "_top_layer_input"); - connections_layer = memnew( Control ); + connections_layer = memnew(Control); add_child(connections_layer); - connections_layer->connect("draw",this,"_connections_layer_draw"); + connections_layer->connect("draw", this, "_connections_layer_draw"); connections_layer->set_name("CLAYER"); connections_layer->set_disable_visibility_clip(true); // so it can draw freely and be offseted @@ -1280,9 +1207,9 @@ GraphEdit::GraphEdit() { v_scroll = memnew(VScrollBar); v_scroll->set_name("_v_scroll"); top_layer->add_child(v_scroll); - updating=false; - connecting=false; - right_disconnects=false; + updating = false; + connecting = false; + right_disconnects = false; box_selecting = false; dragging = false; @@ -1294,49 +1221,46 @@ GraphEdit::GraphEdit() { v_scroll->set_min(-10000); v_scroll->set_max(10000); - h_scroll->connect("value_changed", this,"_scroll_moved"); - v_scroll->connect("value_changed", this,"_scroll_moved"); + h_scroll->connect("value_changed", this, "_scroll_moved"); + v_scroll->connect("value_changed", this, "_scroll_moved"); zoom = 1; - HBoxContainer *zoom_hb = memnew( HBoxContainer ); + HBoxContainer *zoom_hb = memnew(HBoxContainer); top_layer->add_child(zoom_hb); - zoom_hb->set_pos(Vector2(10,10)); - + zoom_hb->set_pos(Vector2(10, 10)); - zoom_minus = memnew( ToolButton ); + zoom_minus = memnew(ToolButton); zoom_hb->add_child(zoom_minus); - zoom_minus->connect("pressed",this,"_zoom_minus"); + zoom_minus->connect("pressed", this, "_zoom_minus"); zoom_minus->set_focus_mode(FOCUS_NONE); - zoom_reset = memnew( ToolButton ); + zoom_reset = memnew(ToolButton); zoom_hb->add_child(zoom_reset); - zoom_reset->connect("pressed",this,"_zoom_reset"); + zoom_reset->connect("pressed", this, "_zoom_reset"); zoom_reset->set_focus_mode(FOCUS_NONE); - zoom_plus = memnew( ToolButton ); + zoom_plus = memnew(ToolButton); zoom_hb->add_child(zoom_plus); - zoom_plus->connect("pressed",this,"_zoom_plus"); + zoom_plus->connect("pressed", this, "_zoom_plus"); zoom_plus->set_focus_mode(FOCUS_NONE); - snap_button = memnew( ToolButton ); + snap_button = memnew(ToolButton); snap_button->set_toggle_mode(true); - snap_button->connect("pressed",this,"_snap_toggled"); + snap_button->connect("pressed", this, "_snap_toggled"); snap_button->set_pressed(true); snap_button->set_focus_mode(FOCUS_NONE); zoom_hb->add_child(snap_button); - snap_amount = memnew( SpinBox ); + snap_amount = memnew(SpinBox); snap_amount->set_min(5); snap_amount->set_max(100); snap_amount->set_step(1); snap_amount->set_value(20); - snap_amount->connect("value_changed",this,"_snap_value_changed"); + snap_amount->connect("value_changed", this, "_snap_value_changed"); zoom_hb->add_child(snap_amount); - setting_scroll_ofs=false; - just_disconected=false; + setting_scroll_ofs = false; + just_disconected = false; set_clip_contents(true); - - } diff --git a/scene/gui/graph_edit.h b/scene/gui/graph_edit.h index 3e9944332f..5166f82310 100644 --- a/scene/gui/graph_edit.h +++ b/scene/gui/graph_edit.h @@ -32,41 +32,37 @@ #include "scene/gui/graph_node.h" #include "scene/gui/scroll_bar.h" #include "scene/gui/slider.h" -#include "scene/gui/tool_button.h" #include "scene/gui/spin_box.h" #include "scene/gui/texture_rect.h" +#include "scene/gui/tool_button.h" class GraphEdit; class GraphEditFilter : public Control { - GDCLASS(GraphEditFilter,Control); + GDCLASS(GraphEditFilter, Control); friend class GraphEdit; GraphEdit *ge; - virtual bool has_point(const Point2& p_point) const; + virtual bool has_point(const Point2 &p_point) const; public: - - GraphEditFilter(GraphEdit *p_edit); }; class GraphEdit : public Control { - GDCLASS(GraphEdit,Control); -public: + GDCLASS(GraphEdit, Control); +public: struct Connection { StringName from; StringName to; int from_port; int to_port; - }; -private: - +private: ToolButton *zoom_minus; ToolButton *zoom_reset; ToolButton *zoom_plus; @@ -78,9 +74,8 @@ private: void _zoom_reset(); void _zoom_plus(); - HScrollBar* h_scroll; - VScrollBar* v_scroll; - + HScrollBar *h_scroll; + VScrollBar *v_scroll; bool connecting; String connecting_from; @@ -106,7 +101,7 @@ private: Point2 box_selecting_from; Point2 box_selecting_to; Rect2 box_selecting_rect; - List<GraphNode*> previus_selected; + List<GraphNode *> previus_selected; bool setting_scroll_ofs; bool right_disconnects; @@ -114,20 +109,20 @@ private: bool awaiting_scroll_offset_update; List<Connection> connections; - void _bake_segment2d(CanvasItem* p_where,float p_begin, float p_end, const Vector2& p_a, const Vector2& p_out, const Vector2& p_b, const Vector2& p_in, int p_depth, int p_min_depth, int p_max_depth, float p_tol, const Color& p_color, const Color& p_to_color, int &lines) const; + void _bake_segment2d(CanvasItem *p_where, float p_begin, float p_end, const Vector2 &p_a, const Vector2 &p_out, const Vector2 &p_b, const Vector2 &p_in, int p_depth, int p_min_depth, int p_max_depth, float p_tol, const Color &p_color, const Color &p_to_color, int &lines) const; - void _draw_cos_line(CanvasItem* p_where,const Vector2& p_from, const Vector2& p_to, const Color& p_color, const Color &p_to_color); + void _draw_cos_line(CanvasItem *p_where, const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, const Color &p_to_color); - void _graph_node_raised(Node* p_gn); + void _graph_node_raised(Node *p_gn); void _graph_node_moved(Node *p_gn); void _update_scroll(); void _scroll_moved(double); - void _gui_input(const InputEvent& p_ev); + void _gui_input(const InputEvent &p_ev); Control *connections_layer; GraphEditFilter *top_layer; - void _top_layer_input(const InputEvent& p_ev); + void _top_layer_input(const InputEvent &p_ev); void _top_layer_draw(); void _connections_layer_draw(); void _update_scroll_offset(); @@ -136,8 +131,6 @@ private: bool lines_on_bg; - - struct ConnType { union { @@ -148,13 +141,13 @@ private: uint64_t key; }; - bool operator<(const ConnType& p_type) const { - return key<p_type.key; + bool operator<(const ConnType &p_type) const { + return key < p_type.key; } - ConnType(uint32_t a=0, uint32_t b=0) { - type_a=a; - type_b=b; + ConnType(uint32_t a = 0, uint32_t b = 0) { + type_a = a; + type_b = b; } }; @@ -163,26 +156,26 @@ private: Set<int> valid_right_disconnect_types; friend class GraphEditFilter; - bool _filter_input(const Point2& p_point); + bool _filter_input(const Point2 &p_point); void _snap_toggled(); void _snap_value_changed(double); -protected: +protected: static void _bind_methods(); virtual void add_child_notify(Node *p_child); virtual void remove_child_notify(Node *p_child); void _notification(int p_what); virtual bool clips_input() const; -public: - Error connect_node(const StringName& p_from, int p_from_port,const StringName& p_to,int p_to_port); - bool is_node_connected(const StringName& p_from, int p_from_port,const StringName& p_to,int p_to_port); - void disconnect_node(const StringName& p_from, int p_from_port,const StringName& p_to,int p_to_port); +public: + Error connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port); + bool is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port); + void disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port); void clear_connections(); - void add_valid_connection_type(int p_type,int p_with_type); - void remove_valid_connection_type(int p_type,int p_with_type); - bool is_valid_connection_type(int p_type,int p_with_type) const; + void add_valid_connection_type(int p_type, int p_with_type); + void remove_valid_connection_type(int p_type, int p_with_type); + bool is_valid_connection_type(int p_type, int p_with_type) const; void set_zoom(float p_zoom); float get_zoom() const; @@ -199,10 +192,10 @@ public: void add_valid_left_disconnect_type(int p_type); void remove_valid_left_disconnect_type(int p_type); - void set_scroll_ofs(const Vector2& p_ofs); + void set_scroll_ofs(const Vector2 &p_ofs); Vector2 get_scroll_ofs() const; - void set_selected(Node* p_child); + void set_selected(Node *p_child); void set_use_snap(bool p_enable); bool is_using_snap() const; @@ -210,7 +203,6 @@ public: int get_snap() const; void set_snap(int p_snap); - GraphEdit(); }; diff --git a/scene/gui/graph_node.cpp b/scene/gui/graph_node.cpp index 58d3f42483..afbb0bbad7 100644 --- a/scene/gui/graph_node.cpp +++ b/scene/gui/graph_node.cpp @@ -29,170 +29,153 @@ #include "graph_node.h" #include "method_bind_ext.inc" - -bool GraphNode::_set(const StringName& p_name, const Variant& p_value) { +bool GraphNode::_set(const StringName &p_name, const Variant &p_value) { if (!p_name.operator String().begins_with("slot/")) return false; - int idx=p_name.operator String().get_slice("/",1).to_int(); - String what = p_name.operator String().get_slice("/",2); - + int idx = p_name.operator String().get_slice("/", 1).to_int(); + String what = p_name.operator String().get_slice("/", 2); Slot si; if (slot_info.has(idx)) - si=slot_info[idx]; - - - if (what=="left_enabled") - si.enable_left=p_value; - else if (what=="left_type") - si.type_left=p_value; - else if (what=="left_color") - si.color_left=p_value; - else if (what=="right_enabled") - si.enable_right=p_value; - else if (what=="right_type") - si.type_right=p_value; - else if (what=="right_color") - si.color_right=p_value; + si = slot_info[idx]; + + if (what == "left_enabled") + si.enable_left = p_value; + else if (what == "left_type") + si.type_left = p_value; + else if (what == "left_color") + si.color_left = p_value; + else if (what == "right_enabled") + si.enable_right = p_value; + else if (what == "right_type") + si.type_right = p_value; + else if (what == "right_color") + si.color_right = p_value; else return false; - set_slot(idx,si.enable_left,si.type_left,si.color_left,si.enable_right,si.type_right,si.color_right); + set_slot(idx, si.enable_left, si.type_left, si.color_left, si.enable_right, si.type_right, si.color_right); update(); return true; } -bool GraphNode::_get(const StringName& p_name,Variant &r_ret) const{ - - +bool GraphNode::_get(const StringName &p_name, Variant &r_ret) const { if (!p_name.operator String().begins_with("slot/")) { return false; } - int idx=p_name.operator String().get_slice("/",1).to_int(); - String what = p_name.operator String().get_slice("/",2); - - + int idx = p_name.operator String().get_slice("/", 1).to_int(); + String what = p_name.operator String().get_slice("/", 2); Slot si; if (slot_info.has(idx)) - si=slot_info[idx]; - - if (what=="left_enabled") - r_ret=si.enable_left; - else if (what=="left_type") - r_ret=si.type_left; - else if (what=="left_color") - r_ret=si.color_left; - else if (what=="right_enabled") - r_ret=si.enable_right; - else if (what=="right_type") - r_ret=si.type_right; - else if (what=="right_color") - r_ret=si.color_right; + si = slot_info[idx]; + + if (what == "left_enabled") + r_ret = si.enable_left; + else if (what == "left_type") + r_ret = si.type_left; + else if (what == "left_color") + r_ret = si.color_left; + else if (what == "right_enabled") + r_ret = si.enable_right; + else if (what == "right_type") + r_ret = si.type_right; + else if (what == "right_color") + r_ret = si.color_right; else return false; return true; } -void GraphNode::_get_property_list( List<PropertyInfo> *p_list) const{ +void GraphNode::_get_property_list(List<PropertyInfo> *p_list) const { - int idx=0; - for(int i=0;i<get_child_count();i++) { - Control *c=get_child(i)->cast_to<Control>(); - if (!c || c->is_set_as_toplevel() ) + int idx = 0; + for (int i = 0; i < get_child_count(); i++) { + Control *c = get_child(i)->cast_to<Control>(); + if (!c || c->is_set_as_toplevel()) continue; - String base="slot/"+itos(idx)+"/"; + String base = "slot/" + itos(idx) + "/"; - p_list->push_back(PropertyInfo(Variant::BOOL,base+"left_enabled")); - p_list->push_back(PropertyInfo(Variant::INT,base+"left_type")); - p_list->push_back(PropertyInfo(Variant::COLOR,base+"left_color")); - p_list->push_back(PropertyInfo(Variant::BOOL,base+"right_enabled")); - p_list->push_back(PropertyInfo(Variant::INT,base+"right_type")); - p_list->push_back(PropertyInfo(Variant::COLOR,base+"right_color")); + p_list->push_back(PropertyInfo(Variant::BOOL, base + "left_enabled")); + p_list->push_back(PropertyInfo(Variant::INT, base + "left_type")); + p_list->push_back(PropertyInfo(Variant::COLOR, base + "left_color")); + p_list->push_back(PropertyInfo(Variant::BOOL, base + "right_enabled")); + p_list->push_back(PropertyInfo(Variant::INT, base + "right_type")); + p_list->push_back(PropertyInfo(Variant::COLOR, base + "right_color")); idx++; } } - void GraphNode::_resort() { - - - int sep=get_constant("separation"); - Ref<StyleBox> sb=get_stylebox("frame"); - bool first=true; + int sep = get_constant("separation"); + Ref<StyleBox> sb = get_stylebox("frame"); + bool first = true; Size2 minsize; - for(int i=0;i<get_child_count();i++) { - Control *c=get_child(i)->cast_to<Control>(); + for (int i = 0; i < get_child_count(); i++) { + Control *c = get_child(i)->cast_to<Control>(); if (!c) continue; if (c->is_set_as_toplevel()) continue; - Size2i size=c->get_combined_minimum_size(); + Size2i size = c->get_combined_minimum_size(); - minsize.y+=size.y; - minsize.x=MAX(minsize.x,size.x); + minsize.y += size.y; + minsize.x = MAX(minsize.x, size.x); if (first) - first=false; + first = false; else - minsize.y+=sep; - + minsize.y += sep; } - - int vofs=0; + int vofs = 0; int w = get_size().x - sb->get_minimum_size().x; - cache_y.clear(); - for(int i=0;i<get_child_count();i++) { - Control *c=get_child(i)->cast_to<Control>(); + for (int i = 0; i < get_child_count(); i++) { + Control *c = get_child(i)->cast_to<Control>(); if (!c) continue; if (c->is_set_as_toplevel()) continue; - Size2i size=c->get_combined_minimum_size(); - - Rect2 r(sb->get_margin(MARGIN_LEFT),sb->get_margin(MARGIN_TOP)+vofs,w,size.y); + Size2i size = c->get_combined_minimum_size(); - fit_child_in_rect(c,r); - cache_y.push_back(vofs+size.y*0.5); - - if (vofs>0) - vofs+=sep; - vofs+=size.y; + Rect2 r(sb->get_margin(MARGIN_LEFT), sb->get_margin(MARGIN_TOP) + vofs, w, size.y); + fit_child_in_rect(c, r); + cache_y.push_back(vofs + size.y * 0.5); + if (vofs > 0) + vofs += sep; + vofs += size.y; } _change_notify(); update(); - connpos_dirty=true; - - + connpos_dirty = true; } -bool GraphNode::has_point(const Point2& p_point) const { +bool GraphNode::has_point(const Point2 &p_point) const { if (comment) { Ref<StyleBox> comment = get_stylebox("comment"); - Ref<Texture> resizer =get_icon("resizer"); + Ref<Texture> resizer = get_icon("resizer"); - if (Rect2(get_size()-resizer->get_size(), resizer->get_size()).has_point(p_point)) { + if (Rect2(get_size() - resizer->get_size(), resizer->get_size()).has_point(p_point)) { return true; } - if (Rect2(0,0,get_size().width,comment->get_margin(MARGIN_TOP)).has_point(p_point)) { + if (Rect2(0, 0, get_size().width, comment->get_margin(MARGIN_TOP)).has_point(p_point)) { return true; } @@ -205,245 +188,226 @@ bool GraphNode::has_point(const Point2& p_point) const { void GraphNode::_notification(int p_what) { - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { Ref<StyleBox> sb; if (comment) { - sb = get_stylebox( selected? "commentfocus" : "comment"); + sb = get_stylebox(selected ? "commentfocus" : "comment"); } else { - sb = get_stylebox( selected ? "selectedframe" : "frame"); + sb = get_stylebox(selected ? "selectedframe" : "frame"); } //sb=sb->duplicate(); //sb->call("set_modulate",modulate); - Ref<Texture> port =get_icon("port"); - Ref<Texture> close =get_icon("close"); - Ref<Texture> resizer =get_icon("resizer"); + Ref<Texture> port = get_icon("port"); + Ref<Texture> close = get_icon("close"); + Ref<Texture> resizer = get_icon("resizer"); int close_offset = get_constant("close_offset"); Ref<Font> title_font = get_font("title_font"); int title_offset = get_constant("title_offset"); Color title_color = get_color("title_color"); - Point2i icofs = -port->get_size()*0.5; - int edgeofs=get_constant("port_offset"); - icofs.y+=sb->get_margin(MARGIN_TOP); - + Point2i icofs = -port->get_size() * 0.5; + int edgeofs = get_constant("port_offset"); + icofs.y += sb->get_margin(MARGIN_TOP); + draw_style_box(sb, Rect2(Point2(), get_size())); - draw_style_box(sb,Rect2(Point2(),get_size())); - - switch(overlay) { + switch (overlay) { case OVERLAY_DISABLED: { } break; case OVERLAY_BREAKPOINT: { - draw_style_box(get_stylebox("breakpoint"),Rect2(Point2(),get_size())); + draw_style_box(get_stylebox("breakpoint"), Rect2(Point2(), get_size())); } break; case OVERLAY_POSITION: { - draw_style_box(get_stylebox("position"),Rect2(Point2(),get_size())); + draw_style_box(get_stylebox("position"), Rect2(Point2(), get_size())); } break; } - int w = get_size().width-sb->get_minimum_size().x; + int w = get_size().width - sb->get_minimum_size().x; if (show_close) - w-=close->get_width(); + w -= close->get_width(); - draw_string(title_font,Point2(sb->get_margin(MARGIN_LEFT),-title_font->get_height()+title_font->get_ascent()+title_offset),title,title_color,w); + draw_string(title_font, Point2(sb->get_margin(MARGIN_LEFT), -title_font->get_height() + title_font->get_ascent() + title_offset), title, title_color, w); if (show_close) { - Vector2 cpos = Point2(w+sb->get_margin(MARGIN_LEFT),-close->get_height()+close_offset); - draw_texture(close,cpos); - close_rect.pos=cpos; - close_rect.size=close->get_size(); + Vector2 cpos = Point2(w + sb->get_margin(MARGIN_LEFT), -close->get_height() + close_offset); + draw_texture(close, cpos); + close_rect.pos = cpos; + close_rect.size = close->get_size(); } else { - close_rect=Rect2(); + close_rect = Rect2(); } - for (Map<int,Slot>::Element *E=slot_info.front();E;E=E->next()) { + for (Map<int, Slot>::Element *E = slot_info.front(); E; E = E->next()) { - if (E->key() < 0 || E->key()>=cache_y.size()) + if (E->key() < 0 || E->key() >= cache_y.size()) continue; if (!slot_info.has(E->key())) continue; - const Slot &s=slot_info[E->key()]; + const Slot &s = slot_info[E->key()]; //left if (s.enable_left) { Ref<Texture> p = port; if (s.custom_slot_left.is_valid()) { - p=s.custom_slot_left; + p = s.custom_slot_left; } - p->draw(get_canvas_item(),icofs+Point2(edgeofs,cache_y[E->key()]),s.color_left); + p->draw(get_canvas_item(), icofs + Point2(edgeofs, cache_y[E->key()]), s.color_left); } if (s.enable_right) { Ref<Texture> p = port; if (s.custom_slot_right.is_valid()) { - p=s.custom_slot_right; + p = s.custom_slot_right; } - p->draw(get_canvas_item(),icofs+Point2(get_size().x-edgeofs,cache_y[E->key()]),s.color_right); + p->draw(get_canvas_item(), icofs + Point2(get_size().x - edgeofs, cache_y[E->key()]), s.color_right); } - } - if (resizeable) { - draw_texture(resizer,get_size()-resizer->get_size()); + draw_texture(resizer, get_size() - resizer->get_size()); } } - if (p_what==NOTIFICATION_SORT_CHILDREN) { + if (p_what == NOTIFICATION_SORT_CHILDREN) { _resort(); } - } +void GraphNode::set_slot(int p_idx, bool p_enable_left, int p_type_left, const Color &p_color_left, bool p_enable_right, int p_type_right, const Color &p_color_right, const Ref<Texture> &p_custom_left, const Ref<Texture> &p_custom_right) { -void GraphNode::set_slot(int p_idx,bool p_enable_left,int p_type_left,const Color& p_color_left, bool p_enable_right,int p_type_right,const Color& p_color_right,const Ref<Texture>& p_custom_left,const Ref<Texture>& p_custom_right) { - - ERR_FAIL_COND(p_idx<0); + ERR_FAIL_COND(p_idx < 0); - if (!p_enable_left && p_type_left==0 && p_color_left==Color(1,1,1,1) && !p_enable_right && p_type_right==0 && p_color_right==Color(1,1,1,1)) { + if (!p_enable_left && p_type_left == 0 && p_color_left == Color(1, 1, 1, 1) && !p_enable_right && p_type_right == 0 && p_color_right == Color(1, 1, 1, 1)) { slot_info.erase(p_idx); return; } Slot s; - s.enable_left=p_enable_left; - s.type_left=p_type_left; - s.color_left=p_color_left; - s.enable_right=p_enable_right; - s.type_right=p_type_right; - s.color_right=p_color_right; - s.custom_slot_left=p_custom_left; - s.custom_slot_right=p_custom_right; - slot_info[p_idx]=s; + s.enable_left = p_enable_left; + s.type_left = p_type_left; + s.color_left = p_color_left; + s.enable_right = p_enable_right; + s.type_right = p_type_right; + s.color_right = p_color_right; + s.custom_slot_left = p_custom_left; + s.custom_slot_right = p_custom_right; + slot_info[p_idx] = s; update(); - connpos_dirty=true; - + connpos_dirty = true; } -void GraphNode::clear_slot(int p_idx){ +void GraphNode::clear_slot(int p_idx) { slot_info.erase(p_idx); update(); - connpos_dirty=true; - + connpos_dirty = true; } -void GraphNode::clear_all_slots(){ +void GraphNode::clear_all_slots() { slot_info.clear(); update(); - connpos_dirty=true; - + connpos_dirty = true; } -bool GraphNode::is_slot_enabled_left(int p_idx) const{ +bool GraphNode::is_slot_enabled_left(int p_idx) const { if (!slot_info.has(p_idx)) return false; return slot_info[p_idx].enable_left; - } -int GraphNode::get_slot_type_left(int p_idx) const{ +int GraphNode::get_slot_type_left(int p_idx) const { if (!slot_info.has(p_idx)) return 0; return slot_info[p_idx].type_left; - } -Color GraphNode::get_slot_color_left(int p_idx) const{ +Color GraphNode::get_slot_color_left(int p_idx) const { if (!slot_info.has(p_idx)) - return Color(1,1,1,1); + return Color(1, 1, 1, 1); return slot_info[p_idx].color_left; - } -bool GraphNode::is_slot_enabled_right(int p_idx) const{ +bool GraphNode::is_slot_enabled_right(int p_idx) const { if (!slot_info.has(p_idx)) return false; return slot_info[p_idx].enable_right; - } - - -int GraphNode::get_slot_type_right(int p_idx) const{ +int GraphNode::get_slot_type_right(int p_idx) const { if (!slot_info.has(p_idx)) return 0; return slot_info[p_idx].type_right; - } -Color GraphNode::get_slot_color_right(int p_idx) const{ +Color GraphNode::get_slot_color_right(int p_idx) const { if (!slot_info.has(p_idx)) - return Color(1,1,1,1); + return Color(1, 1, 1, 1); return slot_info[p_idx].color_right; - } Size2 GraphNode::get_minimum_size() const { Ref<Font> title_font = get_font("title_font"); - int sep=get_constant("separation"); - Ref<StyleBox> sb=get_stylebox("frame"); - bool first=true; + int sep = get_constant("separation"); + Ref<StyleBox> sb = get_stylebox("frame"); + bool first = true; Size2 minsize; - minsize.x=title_font->get_string_size(title).x; + minsize.x = title_font->get_string_size(title).x; if (show_close) { - Ref<Texture> close =get_icon("close"); - minsize.x+=sep+close->get_width(); + Ref<Texture> close = get_icon("close"); + minsize.x += sep + close->get_width(); } + for (int i = 0; i < get_child_count(); i++) { - for(int i=0;i<get_child_count();i++) { - - Control *c=get_child(i)->cast_to<Control>(); + Control *c = get_child(i)->cast_to<Control>(); if (!c) continue; if (c->is_set_as_toplevel()) continue; - Size2i size=c->get_combined_minimum_size(); + Size2i size = c->get_combined_minimum_size(); - minsize.y+=size.y; - minsize.x=MAX(minsize.x,size.x); + minsize.y += size.y; + minsize.x = MAX(minsize.x, size.x); if (first) - first=false; + first = false; else - minsize.y+=sep; + minsize.y += sep; } - return minsize+sb->get_minimum_size(); + return minsize + sb->get_minimum_size(); } -void GraphNode::set_title(const String& p_title) { +void GraphNode::set_title(const String &p_title) { - title=p_title; + title = p_title; minimum_size_changed(); update(); - } -String GraphNode::get_title() const{ +String GraphNode::get_title() const { return title; } -void GraphNode::set_offset(const Vector2& p_offset) { +void GraphNode::set_offset(const Vector2 &p_offset) { - offset=p_offset; + offset = p_offset; emit_signal("offset_changed"); update(); } @@ -453,103 +417,93 @@ Vector2 GraphNode::get_offset() const { return offset; } -void GraphNode::set_selected(bool p_selected) -{ +void GraphNode::set_selected(bool p_selected) { selected = p_selected; update(); } -bool GraphNode::is_selected() -{ +bool GraphNode::is_selected() { return selected; } -void GraphNode::set_drag(bool p_drag) -{ +void GraphNode::set_drag(bool p_drag) { if (p_drag) - drag_from=get_offset(); + drag_from = get_offset(); else - emit_signal("dragged",drag_from,get_offset()); //useful for undo/redo + emit_signal("dragged", drag_from, get_offset()); //useful for undo/redo } -Vector2 GraphNode::get_drag_from() -{ +Vector2 GraphNode::get_drag_from() { return drag_from; } +void GraphNode::set_show_close_button(bool p_enable) { -void GraphNode::set_show_close_button(bool p_enable){ - - show_close=p_enable; + show_close = p_enable; update(); } -bool GraphNode::is_close_button_visible() const{ +bool GraphNode::is_close_button_visible() const { return show_close; } void GraphNode::_connpos_update() { + int edgeofs = get_constant("port_offset"); + int sep = get_constant("separation"); - int edgeofs=get_constant("port_offset"); - int sep=get_constant("separation"); - - Ref<StyleBox> sb=get_stylebox("frame"); + Ref<StyleBox> sb = get_stylebox("frame"); conn_input_cache.clear(); conn_output_cache.clear(); - int vofs=0; + int vofs = 0; - int idx=0; + int idx = 0; - for(int i=0;i<get_child_count();i++) { - Control *c=get_child(i)->cast_to<Control>(); + for (int i = 0; i < get_child_count(); i++) { + Control *c = get_child(i)->cast_to<Control>(); if (!c) continue; if (c->is_set_as_toplevel()) continue; - Size2i size=c->get_combined_minimum_size(); + Size2i size = c->get_combined_minimum_size(); - int y = sb->get_margin(MARGIN_TOP)+vofs; + int y = sb->get_margin(MARGIN_TOP) + vofs; int h = size.y; - if (slot_info.has(idx)) { if (slot_info[idx].enable_left) { ConnCache cc; - cc.pos=Point2i(edgeofs,y+h/2); - cc.type=slot_info[idx].type_left; - cc.color=slot_info[idx].color_left; + cc.pos = Point2i(edgeofs, y + h / 2); + cc.type = slot_info[idx].type_left; + cc.color = slot_info[idx].color_left; conn_input_cache.push_back(cc); } if (slot_info[idx].enable_right) { ConnCache cc; - cc.pos=Point2i(get_size().width-edgeofs,y+h/2); - cc.type=slot_info[idx].type_right; - cc.color=slot_info[idx].color_right; + cc.pos = Point2i(get_size().width - edgeofs, y + h / 2); + cc.type = slot_info[idx].type_right; + cc.color = slot_info[idx].color_right; conn_output_cache.push_back(cc); } } - if (vofs>0) - vofs+=sep; - vofs+=size.y; + if (vofs > 0) + vofs += sep; + vofs += size.y; idx++; - } - - connpos_dirty=false; + connpos_dirty = false; } -int GraphNode::get_connection_input_count() { +int GraphNode::get_connection_input_count() { if (connpos_dirty) _connpos_update(); return conn_input_cache.size(); - } int GraphNode::get_connection_output_count() { @@ -557,16 +511,14 @@ int GraphNode::get_connection_output_count() { _connpos_update(); return conn_output_cache.size(); - } - Vector2 GraphNode::get_connection_input_pos(int p_idx) { if (connpos_dirty) _connpos_update(); - ERR_FAIL_INDEX_V(p_idx,conn_input_cache.size(),Vector2()); + ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Vector2()); Vector2 pos = conn_input_cache[p_idx].pos; pos.x *= get_scale().x; pos.y *= get_scale().y; @@ -578,7 +530,7 @@ int GraphNode::get_connection_input_type(int p_idx) { if (connpos_dirty) _connpos_update(); - ERR_FAIL_INDEX_V(p_idx,conn_input_cache.size(),0); + ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), 0); return conn_input_cache[p_idx].type; } @@ -587,16 +539,16 @@ Color GraphNode::get_connection_input_color(int p_idx) { if (connpos_dirty) _connpos_update(); - ERR_FAIL_INDEX_V(p_idx,conn_input_cache.size(),Color()); + ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Color()); return conn_input_cache[p_idx].color; } -Vector2 GraphNode::get_connection_output_pos(int p_idx){ +Vector2 GraphNode::get_connection_output_pos(int p_idx) { if (connpos_dirty) _connpos_update(); - ERR_FAIL_INDEX_V(p_idx,conn_output_cache.size(),Vector2()); + ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Vector2()); Vector2 pos = conn_output_cache[p_idx].pos; pos.x *= get_scale().x; pos.y *= get_scale().y; @@ -608,7 +560,7 @@ int GraphNode::get_connection_output_type(int p_idx) { if (connpos_dirty) _connpos_update(); - ERR_FAIL_INDEX_V(p_idx,conn_output_cache.size(),0); + ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), 0); return conn_output_cache[p_idx].type; } @@ -617,35 +569,35 @@ Color GraphNode::get_connection_output_color(int p_idx) { if (connpos_dirty) _connpos_update(); - ERR_FAIL_INDEX_V(p_idx,conn_output_cache.size(),Color()); + ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Color()); return conn_output_cache[p_idx].color; } -void GraphNode::_gui_input(const InputEvent& p_ev) { +void GraphNode::_gui_input(const InputEvent &p_ev) { - if (p_ev.type==InputEvent::MOUSE_BUTTON) { + if (p_ev.type == InputEvent::MOUSE_BUTTON) { ERR_EXPLAIN("GraphNode must be the child of a GraphEdit node."); ERR_FAIL_COND(get_parent_control() == NULL); print_line("INPUT EVENT BUTTON"); - if(p_ev.mouse_button.pressed && p_ev.mouse_button.button_index==BUTTON_LEFT) { + if (p_ev.mouse_button.pressed && p_ev.mouse_button.button_index == BUTTON_LEFT) { - Vector2 mpos = Vector2(p_ev.mouse_button.x,p_ev.mouse_button.y); - if (close_rect.size!=Size2() && close_rect.has_point(mpos)) { + Vector2 mpos = Vector2(p_ev.mouse_button.x, p_ev.mouse_button.y); + if (close_rect.size != Size2() && close_rect.has_point(mpos)) { emit_signal("close_request"); accept_event(); return; } - Ref<Texture> resizer =get_icon("resizer"); + Ref<Texture> resizer = get_icon("resizer"); - if (resizeable && mpos.x > get_size().x-resizer->get_width() && mpos.y > get_size().y-resizer->get_height()) { + if (resizeable && mpos.x > get_size().x - resizer->get_width() && mpos.y > get_size().y - resizer->get_height()) { - resizing=true; - resizing_from=mpos; - resizing_from_size=get_size(); + resizing = true; + resizing_from = mpos; + resizing_from_size = get_size(); accept_event(); return; } @@ -653,130 +605,122 @@ void GraphNode::_gui_input(const InputEvent& p_ev) { //send focus to parent emit_signal("raise_request"); get_parent_control()->grab_focus(); - } - if(!p_ev.mouse_button.pressed && p_ev.mouse_button.button_index==BUTTON_LEFT) { - resizing=false; + if (!p_ev.mouse_button.pressed && p_ev.mouse_button.button_index == BUTTON_LEFT) { + resizing = false; } - } - - if (resizing && p_ev.type==InputEvent::MOUSE_MOTION) { - Vector2 mpos = Vector2(p_ev.mouse_motion.x,p_ev.mouse_motion.y); + if (resizing && p_ev.type == InputEvent::MOUSE_MOTION) { + Vector2 mpos = Vector2(p_ev.mouse_motion.x, p_ev.mouse_motion.y); Vector2 diff = mpos - resizing_from; - emit_signal("resize_request",resizing_from_size+diff); - + emit_signal("resize_request", resizing_from_size + diff); } - - } void GraphNode::set_overlay(Overlay p_overlay) { - overlay=p_overlay; + overlay = p_overlay; update(); } -GraphNode::Overlay GraphNode::get_overlay() const{ +GraphNode::Overlay GraphNode::get_overlay() const { return overlay; } void GraphNode::set_comment(bool p_enable) { - comment=p_enable; + comment = p_enable; update(); } -bool GraphNode::is_comment() const{ +bool GraphNode::is_comment() const { return comment; } - void GraphNode::set_resizeable(bool p_enable) { - resizeable=p_enable; + resizeable = p_enable; update(); } -bool GraphNode::is_resizeable() const{ +bool GraphNode::is_resizeable() const { return resizeable; } - void GraphNode::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_title","title"),&GraphNode::set_title); - ClassDB::bind_method(D_METHOD("get_title"),&GraphNode::get_title); - ClassDB::bind_method(D_METHOD("_gui_input"),&GraphNode::_gui_input); + ClassDB::bind_method(D_METHOD("set_title", "title"), &GraphNode::set_title); + ClassDB::bind_method(D_METHOD("get_title"), &GraphNode::get_title); + ClassDB::bind_method(D_METHOD("_gui_input"), &GraphNode::_gui_input); - ClassDB::bind_method(D_METHOD("set_slot","idx","enable_left","type_left","color_left","enable_right","type_right","color_right","custom_left","custom_right"),&GraphNode::set_slot,DEFVAL(Ref<Texture>()),DEFVAL(Ref<Texture>())); - ClassDB::bind_method(D_METHOD("clear_slot","idx"),&GraphNode::clear_slot); - ClassDB::bind_method(D_METHOD("clear_all_slots","idx"),&GraphNode::clear_all_slots); - ClassDB::bind_method(D_METHOD("is_slot_enabled_left","idx"),&GraphNode::is_slot_enabled_left); - ClassDB::bind_method(D_METHOD("get_slot_type_left","idx"),&GraphNode::get_slot_type_left); - ClassDB::bind_method(D_METHOD("get_slot_color_left","idx"),&GraphNode::get_slot_color_left); - ClassDB::bind_method(D_METHOD("is_slot_enabled_right","idx"),&GraphNode::is_slot_enabled_right); - ClassDB::bind_method(D_METHOD("get_slot_type_right","idx"),&GraphNode::get_slot_type_right); - ClassDB::bind_method(D_METHOD("get_slot_color_right","idx"),&GraphNode::get_slot_color_right); + ClassDB::bind_method(D_METHOD("set_slot", "idx", "enable_left", "type_left", "color_left", "enable_right", "type_right", "color_right", "custom_left", "custom_right"), &GraphNode::set_slot, DEFVAL(Ref<Texture>()), DEFVAL(Ref<Texture>())); + ClassDB::bind_method(D_METHOD("clear_slot", "idx"), &GraphNode::clear_slot); + ClassDB::bind_method(D_METHOD("clear_all_slots", "idx"), &GraphNode::clear_all_slots); + ClassDB::bind_method(D_METHOD("is_slot_enabled_left", "idx"), &GraphNode::is_slot_enabled_left); + ClassDB::bind_method(D_METHOD("get_slot_type_left", "idx"), &GraphNode::get_slot_type_left); + ClassDB::bind_method(D_METHOD("get_slot_color_left", "idx"), &GraphNode::get_slot_color_left); + ClassDB::bind_method(D_METHOD("is_slot_enabled_right", "idx"), &GraphNode::is_slot_enabled_right); + ClassDB::bind_method(D_METHOD("get_slot_type_right", "idx"), &GraphNode::get_slot_type_right); + ClassDB::bind_method(D_METHOD("get_slot_color_right", "idx"), &GraphNode::get_slot_color_right); - ClassDB::bind_method(D_METHOD("set_offset","offset"),&GraphNode::set_offset); - ClassDB::bind_method(D_METHOD("get_offset"),&GraphNode::get_offset); + ClassDB::bind_method(D_METHOD("set_offset", "offset"), &GraphNode::set_offset); + ClassDB::bind_method(D_METHOD("get_offset"), &GraphNode::get_offset); - ClassDB::bind_method(D_METHOD("set_comment","comment"),&GraphNode::set_comment); - ClassDB::bind_method(D_METHOD("is_comment"),&GraphNode::is_comment); + ClassDB::bind_method(D_METHOD("set_comment", "comment"), &GraphNode::set_comment); + ClassDB::bind_method(D_METHOD("is_comment"), &GraphNode::is_comment); - ClassDB::bind_method(D_METHOD("set_resizeable","resizeable"),&GraphNode::set_resizeable); - ClassDB::bind_method(D_METHOD("is_resizeable"),&GraphNode::is_resizeable); + ClassDB::bind_method(D_METHOD("set_resizeable", "resizeable"), &GraphNode::set_resizeable); + ClassDB::bind_method(D_METHOD("is_resizeable"), &GraphNode::is_resizeable); - ClassDB::bind_method(D_METHOD("set_selected","selected"),&GraphNode::set_selected); - ClassDB::bind_method(D_METHOD("is_selected"),&GraphNode::is_selected); + ClassDB::bind_method(D_METHOD("set_selected", "selected"), &GraphNode::set_selected); + ClassDB::bind_method(D_METHOD("is_selected"), &GraphNode::is_selected); - ClassDB::bind_method(D_METHOD("get_connection_output_count"),&GraphNode::get_connection_output_count); - ClassDB::bind_method(D_METHOD("get_connection_input_count"),&GraphNode::get_connection_input_count); + ClassDB::bind_method(D_METHOD("get_connection_output_count"), &GraphNode::get_connection_output_count); + ClassDB::bind_method(D_METHOD("get_connection_input_count"), &GraphNode::get_connection_input_count); - ClassDB::bind_method(D_METHOD("get_connection_output_pos","idx"),&GraphNode::get_connection_output_pos); - ClassDB::bind_method(D_METHOD("get_connection_output_type","idx"),&GraphNode::get_connection_output_type); - ClassDB::bind_method(D_METHOD("get_connection_output_color","idx"),&GraphNode::get_connection_output_color); - ClassDB::bind_method(D_METHOD("get_connection_input_pos","idx"),&GraphNode::get_connection_input_pos); - ClassDB::bind_method(D_METHOD("get_connection_input_type","idx"),&GraphNode::get_connection_input_type); - ClassDB::bind_method(D_METHOD("get_connection_input_color","idx"),&GraphNode::get_connection_input_color); + ClassDB::bind_method(D_METHOD("get_connection_output_pos", "idx"), &GraphNode::get_connection_output_pos); + ClassDB::bind_method(D_METHOD("get_connection_output_type", "idx"), &GraphNode::get_connection_output_type); + ClassDB::bind_method(D_METHOD("get_connection_output_color", "idx"), &GraphNode::get_connection_output_color); + ClassDB::bind_method(D_METHOD("get_connection_input_pos", "idx"), &GraphNode::get_connection_input_pos); + ClassDB::bind_method(D_METHOD("get_connection_input_type", "idx"), &GraphNode::get_connection_input_type); + ClassDB::bind_method(D_METHOD("get_connection_input_color", "idx"), &GraphNode::get_connection_input_color); - ClassDB::bind_method(D_METHOD("set_show_close_button","show"),&GraphNode::set_show_close_button); - ClassDB::bind_method(D_METHOD("is_close_button_visible"),&GraphNode::is_close_button_visible); + ClassDB::bind_method(D_METHOD("set_show_close_button", "show"), &GraphNode::set_show_close_button); + ClassDB::bind_method(D_METHOD("is_close_button_visible"), &GraphNode::is_close_button_visible); - ClassDB::bind_method(D_METHOD("set_overlay","overlay"),&GraphNode::set_overlay); - ClassDB::bind_method(D_METHOD("get_overlay"),&GraphNode::get_overlay); + ClassDB::bind_method(D_METHOD("set_overlay", "overlay"), &GraphNode::set_overlay); + ClassDB::bind_method(D_METHOD("get_overlay"), &GraphNode::get_overlay); - ADD_PROPERTY( PropertyInfo(Variant::STRING,"title"),"set_title","get_title"); - ADD_PROPERTY( PropertyInfo(Variant::BOOL,"show_close"),"set_show_close_button","is_close_button_visible"); - ADD_PROPERTY( PropertyInfo(Variant::BOOL,"resizeable"),"set_resizeable","is_resizeable"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_close"), "set_show_close_button", "is_close_button_visible"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resizeable"), "set_resizeable", "is_resizeable"); ADD_SIGNAL(MethodInfo("offset_changed")); - ADD_SIGNAL(MethodInfo("dragged",PropertyInfo(Variant::VECTOR2,"from"),PropertyInfo(Variant::VECTOR2,"to"))); + ADD_SIGNAL(MethodInfo("dragged", PropertyInfo(Variant::VECTOR2, "from"), PropertyInfo(Variant::VECTOR2, "to"))); ADD_SIGNAL(MethodInfo("raise_request")); ADD_SIGNAL(MethodInfo("close_request")); - ADD_SIGNAL(MethodInfo("resize_request",PropertyInfo(Variant::VECTOR2,"new_minsize"))); + ADD_SIGNAL(MethodInfo("resize_request", PropertyInfo(Variant::VECTOR2, "new_minsize"))); - BIND_CONSTANT( OVERLAY_DISABLED ); - BIND_CONSTANT( OVERLAY_BREAKPOINT ); - BIND_CONSTANT( OVERLAY_POSITION ); + BIND_CONSTANT(OVERLAY_DISABLED); + BIND_CONSTANT(OVERLAY_BREAKPOINT); + BIND_CONSTANT(OVERLAY_POSITION); } GraphNode::GraphNode() { - overlay=OVERLAY_DISABLED; - show_close=false; - connpos_dirty=true; + overlay = OVERLAY_DISABLED; + show_close = false; + connpos_dirty = true; set_mouse_filter(MOUSE_FILTER_PASS); - comment=false; - resizeable=false; - resizing=false; - selected=false; + comment = false; + resizeable = false; + resizing = false; + selected = false; } diff --git a/scene/gui/graph_node.h b/scene/gui/graph_node.h index 9cb46fc49c..bab78dedb9 100644 --- a/scene/gui/graph_node.h +++ b/scene/gui/graph_node.h @@ -33,16 +33,16 @@ class GraphNode : public Container { - GDCLASS(GraphNode,Container); -public: + GDCLASS(GraphNode, Container); +public: enum Overlay { OVERLAY_DISABLED, OVERLAY_BREAKPOINT, OVERLAY_POSITION }; -private: +private: struct Slot { bool enable_left; int type_left; @@ -53,8 +53,14 @@ private: Ref<Texture> custom_slot_left; Ref<Texture> custom_slot_right; - - Slot() { enable_left=false; type_left=0; color_left=Color(1,1,1,1); enable_right=false; type_right=0; color_right=Color(1,1,1,1); } + Slot() { + enable_left = false; + type_left = 0; + color_left = Color(1, 1, 1, 1); + enable_right = false; + type_right = 0; + color_right = Color(1, 1, 1, 1); + } }; String title; @@ -80,7 +86,7 @@ private: Vector<ConnCache> conn_input_cache; Vector<ConnCache> conn_output_cache; - Map<int,Slot> slot_info; + Map<int, Slot> slot_info; bool connpos_dirty; @@ -92,25 +98,19 @@ private: Overlay overlay; - bool has_point(const Point2& p_point) const; + bool has_point(const Point2 &p_point) const; protected: - - - void _gui_input(const InputEvent& p_ev); + void _gui_input(const InputEvent &p_ev); void _notification(int p_what); static void _bind_methods(); - bool _set(const StringName& p_name, const Variant& p_value); - bool _get(const StringName& p_name,Variant &r_ret) const; - void _get_property_list( List<PropertyInfo> *p_list) const; + bool _set(const StringName &p_name, const Variant &p_value); + bool _get(const StringName &p_name, Variant &r_ret) const; + void _get_property_list(List<PropertyInfo> *p_list) const; public: - - - - - void set_slot(int p_idx,bool p_enable_left,int p_type_left,const Color& p_color_left, bool p_enable_right,int p_type_right,const Color& p_color_right,const Ref<Texture>& p_custom_left=Ref<Texture>(),const Ref<Texture>& p_custom_right=Ref<Texture>()); + void set_slot(int p_idx, bool p_enable_left, int p_type_left, const Color &p_color_left, bool p_enable_right, int p_type_right, const Color &p_color_right, const Ref<Texture> &p_custom_left = Ref<Texture>(), const Ref<Texture> &p_custom_right = Ref<Texture>()); void clear_slot(int p_idx); void clear_all_slots(); bool is_slot_enabled_left(int p_idx) const; @@ -120,10 +120,10 @@ public: int get_slot_type_right(int p_idx) const; Color get_slot_color_right(int p_idx) const; - void set_title(const String& p_title); + void set_title(const String &p_title); String get_title() const; - void set_offset(const Vector2& p_offset); + void set_offset(const Vector2 &p_offset); Vector2 get_offset() const; void set_selected(bool p_selected); @@ -135,8 +135,8 @@ public: void set_show_close_button(bool p_enable); bool is_close_button_visible() const; - int get_connection_input_count() ; - int get_connection_output_count() ; + int get_connection_input_count(); + int get_connection_output_count(); Vector2 get_connection_input_pos(int p_idx); int get_connection_input_type(int p_idx); Color get_connection_input_color(int p_idx); @@ -144,7 +144,6 @@ public: int get_connection_output_type(int p_idx); Color get_connection_output_color(int p_idx); - void set_overlay(Overlay p_overlay); Overlay get_overlay() const; @@ -161,6 +160,6 @@ public: GraphNode(); }; -VARIANT_ENUM_CAST( GraphNode::Overlay ) +VARIANT_ENUM_CAST(GraphNode::Overlay) #endif // GRAPH_NODE_H diff --git a/scene/gui/grid_container.cpp b/scene/gui/grid_container.cpp index ee1d642110..aae11255ed 100644 --- a/scene/gui/grid_container.cpp +++ b/scene/gui/grid_container.cpp @@ -28,29 +28,27 @@ /*************************************************************************/ #include "grid_container.h" - void GridContainer::_notification(int p_what) { - switch(p_what) { + switch (p_what) { case NOTIFICATION_SORT_CHILDREN: { - Map<int,int> col_minw; - Map<int,int> row_minh; + Map<int, int> col_minw; + Map<int, int> row_minh; Set<int> col_expanded; Set<int> row_expanded; - int hsep=get_constant("hseparation"); - int vsep=get_constant("vseparation"); + int hsep = get_constant("hseparation"); + int vsep = get_constant("vseparation"); - int idx=0; - int max_row=0; - int max_col=0; + int idx = 0; + int max_row = 0; + int max_col = 0; Size2 size = get_size(); - - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { Control *c = get_child(i)->cast_to<Control>(); if (!c || !c->is_visible_in_tree()) @@ -61,55 +59,54 @@ void GridContainer::_notification(int p_what) { Size2i ms = c->get_combined_minimum_size(); if (col_minw.has(col)) - col_minw[col] = MAX(col_minw[col],ms.width); + col_minw[col] = MAX(col_minw[col], ms.width); else - col_minw[col]=ms.width; + col_minw[col] = ms.width; if (row_minh.has(row)) - row_minh[row] = MAX(row_minh[row],ms.height); + row_minh[row] = MAX(row_minh[row], ms.height); else - row_minh[row]=ms.height; + row_minh[row] = ms.height; //print_line("store row "+itos(row)+" mw "+itos(ms.height)); - if (c->get_h_size_flags()&SIZE_EXPAND) + if (c->get_h_size_flags() & SIZE_EXPAND) col_expanded.insert(col); - if (c->get_v_size_flags()&SIZE_EXPAND) + if (c->get_v_size_flags() & SIZE_EXPAND) row_expanded.insert(row); - max_col=MAX(col,max_col); - max_row=MAX(row,max_row); + max_col = MAX(col, max_col); + max_row = MAX(row, max_row); idx++; } Size2 ms; - int expand_rows=0; - int expand_cols=0; + int expand_rows = 0; + int expand_cols = 0; - for (Map<int,int>::Element *E=col_minw.front();E;E=E->next()) { - ms.width+=E->get(); + for (Map<int, int>::Element *E = col_minw.front(); E; E = E->next()) { + ms.width += E->get(); if (col_expanded.has(E->key())) expand_cols++; } - for (Map<int,int>::Element *E=row_minh.front();E;E=E->next()) { - ms.height+=E->get(); + for (Map<int, int>::Element *E = row_minh.front(); E; E = E->next()) { + ms.height += E->get(); if (row_expanded.has(E->key())) expand_rows++; } - ms.height+=vsep*max_row; - ms.width+=hsep*max_col; - - int row_expand = expand_rows?(size.y-ms.y)/expand_rows:0; - int col_expand = expand_cols?(size.x-ms.x)/expand_cols:0; + ms.height += vsep * max_row; + ms.width += hsep * max_col; + int row_expand = expand_rows ? (size.y - ms.y) / expand_rows : 0; + int col_expand = expand_cols ? (size.x - ms.x) / expand_cols : 0; - int col_ofs=0; - int row_ofs=0; - idx=0; + int col_ofs = 0; + int row_ofs = 0; + idx = 0; - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { Control *c = get_child(i)->cast_to<Control>(); if (!c || !c->is_visible_in_tree()) @@ -117,78 +114,74 @@ void GridContainer::_notification(int p_what) { int row = idx / columns; int col = idx % columns; - if (col==0) { - col_ofs=0; - if (row>0 && row_minh.has(row-1)) - row_ofs+=row_minh[row-1]+vsep+(row_expanded.has(row-1)?row_expand:0); + if (col == 0) { + col_ofs = 0; + if (row > 0 && row_minh.has(row - 1)) + row_ofs += row_minh[row - 1] + vsep + (row_expanded.has(row - 1) ? row_expand : 0); } Size2 s; if (col_minw.has(col)) - s.width=col_minw[col]; + s.width = col_minw[col]; if (row_minh.has(row)) - s.height=row_minh[row]; + s.height = row_minh[row]; if (row_expanded.has(row)) - s.height+=row_expand; + s.height += row_expand; if (col_expanded.has(col)) - s.width+=col_expand; + s.width += col_expand; - Point2 p(col_ofs,row_ofs); + Point2 p(col_ofs, row_ofs); //print_line("col: "+itos(col)+" row: "+itos(row)+" col_ofs: "+itos(col_ofs)+" row_ofs: "+itos(row_ofs)); - fit_child_in_rect(c,Rect2(p,s)); + fit_child_in_rect(c, Rect2(p, s)); //print_line("col: "+itos(col)+" row: "+itos(row)+" rect: "+Rect2(p,s)); if (col_minw.has(col)) { - col_ofs+=col_minw[col]+hsep+(col_expanded.has(col)?col_expand:0); + col_ofs += col_minw[col] + hsep + (col_expanded.has(col) ? col_expand : 0); } idx++; } - - } break; } - } void GridContainer::set_columns(int p_columns) { - ERR_FAIL_COND(p_columns<1); - columns=p_columns; + ERR_FAIL_COND(p_columns < 1); + columns = p_columns; queue_sort(); minimum_size_changed(); - } -int GridContainer::get_columns() const{ +int GridContainer::get_columns() const { return columns; } -void GridContainer::_bind_methods(){ +void GridContainer::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_columns","columns"),&GridContainer::set_columns); - ClassDB::bind_method(D_METHOD("get_columns"),&GridContainer::get_columns); + ClassDB::bind_method(D_METHOD("set_columns", "columns"), &GridContainer::set_columns); + ClassDB::bind_method(D_METHOD("get_columns"), &GridContainer::get_columns); - ADD_PROPERTY( PropertyInfo(Variant::INT,"columns",PROPERTY_HINT_RANGE,"1,1024,1"),"set_columns","get_columns"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "columns", PROPERTY_HINT_RANGE, "1,1024,1"), "set_columns", "get_columns"); } Size2 GridContainer::get_minimum_size() const { - Map<int,int> col_minw; - Map<int,int> row_minh; + Map<int, int> col_minw; + Map<int, int> row_minh; - int hsep=get_constant("hseparation"); - int vsep=get_constant("vseparation"); + int hsep = get_constant("hseparation"); + int vsep = get_constant("vseparation"); - int idx=0; - int max_row=0; - int max_col=0; + int idx = 0; + int max_row = 0; + int max_col = 0; - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { Control *c = get_child(i)->cast_to<Control>(); if (!c || !c->is_visible_in_tree()) @@ -197,39 +190,37 @@ Size2 GridContainer::get_minimum_size() const { int col = idx % columns; Size2i ms = c->get_combined_minimum_size(); if (col_minw.has(col)) - col_minw[col] = MAX(col_minw[col],ms.width); + col_minw[col] = MAX(col_minw[col], ms.width); else - col_minw[col]=ms.width; + col_minw[col] = ms.width; if (row_minh.has(row)) - row_minh[row] = MAX(row_minh[row],ms.height); + row_minh[row] = MAX(row_minh[row], ms.height); else - row_minh[row]=ms.height; - max_col=MAX(col,max_col); - max_row=MAX(row,max_row); + row_minh[row] = ms.height; + max_col = MAX(col, max_col); + max_row = MAX(row, max_row); idx++; } Size2 ms; - for (Map<int,int>::Element *E=col_minw.front();E;E=E->next()) { - ms.width+=E->get(); + for (Map<int, int>::Element *E = col_minw.front(); E; E = E->next()) { + ms.width += E->get(); } - for (Map<int,int>::Element *E=row_minh.front();E;E=E->next()) { - ms.height+=E->get(); + for (Map<int, int>::Element *E = row_minh.front(); E; E = E->next()) { + ms.height += E->get(); } - ms.height+=vsep*max_row; - ms.width+=hsep*max_col; + ms.height += vsep * max_row; + ms.width += hsep * max_col; return ms; - } - GridContainer::GridContainer() { set_mouse_filter(MOUSE_FILTER_PASS); - columns=1; + columns = 1; } diff --git a/scene/gui/grid_container.h b/scene/gui/grid_container.h index cc1d04cdb2..61dda4e223 100644 --- a/scene/gui/grid_container.h +++ b/scene/gui/grid_container.h @@ -33,15 +33,15 @@ class GridContainer : public Container { - GDCLASS(GridContainer,Container); + GDCLASS(GridContainer, Container); int columns; -protected: +protected: void _notification(int p_what); static void _bind_methods(); -public: +public: void set_columns(int p_columns); int get_columns() const; virtual Size2 get_minimum_size() const; diff --git a/scene/gui/input_action.cpp b/scene/gui/input_action.cpp index afdffd9a53..9576bb07e4 100644 --- a/scene/gui/input_action.cpp +++ b/scene/gui/input_action.cpp @@ -29,42 +29,41 @@ #include "input_action.h" #include "os/keyboard.h" -void ShortCut::set_shortcut(const InputEvent& p_shortcut){ +void ShortCut::set_shortcut(const InputEvent &p_shortcut) { - shortcut=p_shortcut; + shortcut = p_shortcut; emit_changed(); } -InputEvent ShortCut::get_shortcut() const{ +InputEvent ShortCut::get_shortcut() const { return shortcut; } -bool ShortCut::is_shortcut(const InputEvent& p_event) const { +bool ShortCut::is_shortcut(const InputEvent &p_event) const { - bool same=false; + bool same = false; - - switch(p_event.type) { + switch (p_event.type) { case InputEvent::KEY: { - same=(shortcut.key.scancode==p_event.key.scancode && shortcut.key.mod == p_event.key.mod); + same = (shortcut.key.scancode == p_event.key.scancode && shortcut.key.mod == p_event.key.mod); } break; case InputEvent::JOYPAD_BUTTON: { - same=(shortcut.joy_button.button_index==p_event.joy_button.button_index); + same = (shortcut.joy_button.button_index == p_event.joy_button.button_index); } break; case InputEvent::MOUSE_BUTTON: { - same=(shortcut.mouse_button.button_index==p_event.mouse_button.button_index); + same = (shortcut.mouse_button.button_index == p_event.mouse_button.button_index); } break; case InputEvent::JOYPAD_MOTION: { - same=(shortcut.joy_motion.axis==p_event.joy_motion.axis && (shortcut.joy_motion.axis_value < 0) == (p_event.joy_motion.axis_value < 0)); + same = (shortcut.joy_motion.axis == p_event.joy_motion.axis && (shortcut.joy_motion.axis_value < 0) == (p_event.joy_motion.axis_value < 0)); } break; default: {}; @@ -75,7 +74,7 @@ bool ShortCut::is_shortcut(const InputEvent& p_event) const { String ShortCut::get_as_text() const { - switch(shortcut.type) { + switch (shortcut.type) { case InputEvent::NONE: { @@ -85,35 +84,35 @@ String ShortCut::get_as_text() const { String str; if (shortcut.key.mod.shift) - str+=RTR("Shift+"); + str += RTR("Shift+"); if (shortcut.key.mod.alt) - str+=RTR("Alt+"); + str += RTR("Alt+"); if (shortcut.key.mod.control) - str+=RTR("Ctrl+"); + str += RTR("Ctrl+"); if (shortcut.key.mod.meta) - str+=RTR("Meta+"); + str += RTR("Meta+"); - str+=keycode_get_string(shortcut.key.scancode).capitalize(); + str += keycode_get_string(shortcut.key.scancode).capitalize(); return str; } break; case InputEvent::JOYPAD_BUTTON: { - String str = RTR("Device")+" "+itos(shortcut.device)+", "+RTR("Button")+" "+itos(shortcut.joy_button.button_index); - str+="."; + String str = RTR("Device") + " " + itos(shortcut.device) + ", " + RTR("Button") + " " + itos(shortcut.joy_button.button_index); + str += "."; return str; } break; case InputEvent::MOUSE_BUTTON: { - String str = RTR("Device")+" "+itos(shortcut.device)+", "; + String str = RTR("Device") + " " + itos(shortcut.device) + ", "; switch (shortcut.mouse_button.button_index) { - case BUTTON_LEFT: str+=RTR("Left Button."); break; - case BUTTON_RIGHT: str+=RTR("Right Button."); break; - case BUTTON_MIDDLE: str+=RTR("Middle Button."); break; - case BUTTON_WHEEL_UP: str+=RTR("Wheel Up."); break; - case BUTTON_WHEEL_DOWN: str+=RTR("Wheel Down."); break; - default: str+=RTR("Button")+" "+itos(shortcut.mouse_button.button_index)+"."; + case BUTTON_LEFT: str += RTR("Left Button."); break; + case BUTTON_RIGHT: str += RTR("Right Button."); break; + case BUTTON_MIDDLE: str += RTR("Middle Button."); break; + case BUTTON_WHEEL_UP: str += RTR("Wheel Up."); break; + case BUTTON_WHEEL_DOWN: str += RTR("Wheel Down."); break; + default: str += RTR("Button") + " " + itos(shortcut.mouse_button.button_index) + "."; } return str; @@ -121,7 +120,7 @@ String ShortCut::get_as_text() const { case InputEvent::JOYPAD_MOTION: { int ax = shortcut.joy_motion.axis; - String str = RTR("Device")+" "+itos(shortcut.device)+", "+RTR("Axis")+" "+itos(ax)+"."; + String str = RTR("Device") + " " + itos(shortcut.device) + ", " + RTR("Axis") + " " + itos(ax) + "."; return str; } break; @@ -132,22 +131,21 @@ String ShortCut::get_as_text() const { bool ShortCut::is_valid() const { - return shortcut.type!=InputEvent::NONE; + return shortcut.type != InputEvent::NONE; } void ShortCut::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_shortcut","event"),&ShortCut::set_shortcut); - ClassDB::bind_method(D_METHOD("get_shortcut"),&ShortCut::get_shortcut); + ClassDB::bind_method(D_METHOD("set_shortcut", "event"), &ShortCut::set_shortcut); + ClassDB::bind_method(D_METHOD("get_shortcut"), &ShortCut::get_shortcut); - ClassDB::bind_method(D_METHOD("is_valid"),&ShortCut::is_valid); + ClassDB::bind_method(D_METHOD("is_valid"), &ShortCut::is_valid); - ClassDB::bind_method(D_METHOD("is_shortcut","event"),&ShortCut::is_shortcut); - ClassDB::bind_method(D_METHOD("get_as_text"),&ShortCut::get_as_text); + ClassDB::bind_method(D_METHOD("is_shortcut", "event"), &ShortCut::is_shortcut); + ClassDB::bind_method(D_METHOD("get_as_text"), &ShortCut::get_as_text); - ADD_PROPERTY(PropertyInfo(Variant::INPUT_EVENT,"shortcut"),"set_shortcut","get_shortcut"); + ADD_PROPERTY(PropertyInfo(Variant::INPUT_EVENT, "shortcut"), "set_shortcut", "get_shortcut"); } -ShortCut::ShortCut(){ - +ShortCut::ShortCut() { } diff --git a/scene/gui/input_action.h b/scene/gui/input_action.h index 3cdbc755ef..a0f5cf2c11 100644 --- a/scene/gui/input_action.h +++ b/scene/gui/input_action.h @@ -33,17 +33,17 @@ class ShortCut : public Resource { - GDCLASS(ShortCut,Resource); + GDCLASS(ShortCut, Resource); InputEvent shortcut; -protected: +protected: static void _bind_methods(); -public: - void set_shortcut(const InputEvent& p_shortcut); +public: + void set_shortcut(const InputEvent &p_shortcut); InputEvent get_shortcut() const; - bool is_shortcut(const InputEvent& p_Event) const; + bool is_shortcut(const InputEvent &p_Event) const; bool is_valid() const; String get_as_text() const; diff --git a/scene/gui/item_list.cpp b/scene/gui/item_list.cpp index 91bd16ee0b..1db748a6b1 100644 --- a/scene/gui/item_list.cpp +++ b/scene/gui/item_list.cpp @@ -27,264 +27,235 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "item_list.h" -#include "os/os.h" #include "global_config.h" +#include "os/os.h" - -void ItemList::add_item(const String& p_item,const Ref<Texture>& p_texture,bool p_selectable) { +void ItemList::add_item(const String &p_item, const Ref<Texture> &p_texture, bool p_selectable) { Item item; - item.icon=p_texture; - item.icon_region=Rect2i(); - item.text=p_item; - item.selectable=p_selectable; - item.selected=false; - item.disabled=false; - item.tooltip_enabled=true; - item.custom_bg=Color(0,0,0,0); + item.icon = p_texture; + item.icon_region = Rect2i(); + item.text = p_item; + item.selectable = p_selectable; + item.selected = false; + item.disabled = false; + item.tooltip_enabled = true; + item.custom_bg = Color(0, 0, 0, 0); items.push_back(item); update(); - shape_changed=true; - + shape_changed = true; } -void ItemList::add_icon_item(const Ref<Texture>& p_item,bool p_selectable){ +void ItemList::add_icon_item(const Ref<Texture> &p_item, bool p_selectable) { Item item; - item.icon=p_item; - item.icon_region=Rect2i(); + item.icon = p_item; + item.icon_region = Rect2i(); //item.text=p_item; - item.selectable=p_selectable; - item.selected=false; - item.disabled=false; - item.tooltip_enabled=true; - item.custom_bg=Color(0,0,0,0); + item.selectable = p_selectable; + item.selected = false; + item.disabled = false; + item.tooltip_enabled = true; + item.custom_bg = Color(0, 0, 0, 0); items.push_back(item); update(); - shape_changed=true; - + shape_changed = true; } -void ItemList::set_item_text(int p_idx,const String& p_text){ +void ItemList::set_item_text(int p_idx, const String &p_text) { - ERR_FAIL_INDEX(p_idx,items.size()); + ERR_FAIL_INDEX(p_idx, items.size()); - items[p_idx].text=p_text; + items[p_idx].text = p_text; update(); - shape_changed=true; - + shape_changed = true; } -String ItemList::get_item_text(int p_idx) const{ +String ItemList::get_item_text(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),String()); + ERR_FAIL_INDEX_V(p_idx, items.size(), String()); return items[p_idx].text; - } void ItemList::set_item_tooltip_enabled(int p_idx, const bool p_enabled) { - ERR_FAIL_INDEX(p_idx,items.size()); + ERR_FAIL_INDEX(p_idx, items.size()); items[p_idx].tooltip_enabled = p_enabled; } bool ItemList::is_item_tooltip_enabled(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(), false); + ERR_FAIL_INDEX_V(p_idx, items.size(), false); return items[p_idx].tooltip_enabled; } -void ItemList::set_item_tooltip(int p_idx,const String& p_tooltip){ +void ItemList::set_item_tooltip(int p_idx, const String &p_tooltip) { - ERR_FAIL_INDEX(p_idx,items.size()); + ERR_FAIL_INDEX(p_idx, items.size()); - items[p_idx].tooltip=p_tooltip; + items[p_idx].tooltip = p_tooltip; update(); - shape_changed=true; - + shape_changed = true; } -String ItemList::get_item_tooltip(int p_idx) const{ +String ItemList::get_item_tooltip(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),String()); + ERR_FAIL_INDEX_V(p_idx, items.size(), String()); return items[p_idx].tooltip; - } -void ItemList::set_item_icon(int p_idx,const Ref<Texture>& p_icon){ +void ItemList::set_item_icon(int p_idx, const Ref<Texture> &p_icon) { - ERR_FAIL_INDEX(p_idx,items.size()); + ERR_FAIL_INDEX(p_idx, items.size()); - items[p_idx].icon=p_icon; + items[p_idx].icon = p_icon; update(); - shape_changed=true; - - + shape_changed = true; } -Ref<Texture> ItemList::get_item_icon(int p_idx) const{ +Ref<Texture> ItemList::get_item_icon(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),Ref<Texture>()); + ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture>()); return items[p_idx].icon; - } -void ItemList::set_item_icon_region(int p_idx,const Rect2& p_region) { +void ItemList::set_item_icon_region(int p_idx, const Rect2 &p_region) { - ERR_FAIL_INDEX(p_idx,items.size()); + ERR_FAIL_INDEX(p_idx, items.size()); - items[p_idx].icon_region=p_region; + items[p_idx].icon_region = p_region; update(); - shape_changed=true; + shape_changed = true; } Rect2 ItemList::get_item_icon_region(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),Rect2()); + ERR_FAIL_INDEX_V(p_idx, items.size(), Rect2()); return items[p_idx].icon_region; } -void ItemList::set_item_custom_bg_color(int p_idx,const Color& p_custom_bg_color) { - - ERR_FAIL_INDEX(p_idx,items.size()); +void ItemList::set_item_custom_bg_color(int p_idx, const Color &p_custom_bg_color) { - items[p_idx].custom_bg=p_custom_bg_color; + ERR_FAIL_INDEX(p_idx, items.size()); + items[p_idx].custom_bg = p_custom_bg_color; } Color ItemList::get_item_custom_bg_color(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),Color()); + ERR_FAIL_INDEX_V(p_idx, items.size(), Color()); return items[p_idx].custom_bg; - } +void ItemList::set_item_tag_icon(int p_idx, const Ref<Texture> &p_tag_icon) { + ERR_FAIL_INDEX(p_idx, items.size()); -void ItemList::set_item_tag_icon(int p_idx,const Ref<Texture>& p_tag_icon){ - - ERR_FAIL_INDEX(p_idx,items.size()); - - items[p_idx].tag_icon=p_tag_icon; + items[p_idx].tag_icon = p_tag_icon; update(); - shape_changed=true; - - + shape_changed = true; } -Ref<Texture> ItemList::get_item_tag_icon(int p_idx) const{ +Ref<Texture> ItemList::get_item_tag_icon(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),Ref<Texture>()); + ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture>()); return items[p_idx].tag_icon; - } -void ItemList::set_item_selectable(int p_idx,bool p_selectable){ - - ERR_FAIL_INDEX(p_idx,items.size()); - - items[p_idx].selectable=p_selectable; +void ItemList::set_item_selectable(int p_idx, bool p_selectable) { + ERR_FAIL_INDEX(p_idx, items.size()); + items[p_idx].selectable = p_selectable; } +bool ItemList::is_item_selectable(int p_idx) const { -bool ItemList::is_item_selectable(int p_idx) const{ - - ERR_FAIL_INDEX_V(p_idx,items.size(),false); + ERR_FAIL_INDEX_V(p_idx, items.size(), false); return items[p_idx].selectable; } -void ItemList::set_item_disabled(int p_idx,bool p_disabled){ +void ItemList::set_item_disabled(int p_idx, bool p_disabled) { - ERR_FAIL_INDEX(p_idx,items.size()); + ERR_FAIL_INDEX(p_idx, items.size()); - items[p_idx].disabled=p_disabled; + items[p_idx].disabled = p_disabled; update(); - } +bool ItemList::is_item_disabled(int p_idx) const { -bool ItemList::is_item_disabled(int p_idx) const{ - - ERR_FAIL_INDEX_V(p_idx,items.size(),false); + ERR_FAIL_INDEX_V(p_idx, items.size(), false); return items[p_idx].disabled; } +void ItemList::set_item_metadata(int p_idx, const Variant &p_metadata) { -void ItemList::set_item_metadata(int p_idx,const Variant& p_metadata){ - - ERR_FAIL_INDEX(p_idx,items.size()); + ERR_FAIL_INDEX(p_idx, items.size()); - items[p_idx].metadata=p_metadata; + items[p_idx].metadata = p_metadata; update(); - shape_changed=true; - + shape_changed = true; } -Variant ItemList::get_item_metadata(int p_idx) const{ +Variant ItemList::get_item_metadata(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),Variant()); + ERR_FAIL_INDEX_V(p_idx, items.size(), Variant()); return items[p_idx].metadata; - } -void ItemList::select(int p_idx,bool p_single){ +void ItemList::select(int p_idx, bool p_single) { - ERR_FAIL_INDEX(p_idx,items.size()); + ERR_FAIL_INDEX(p_idx, items.size()); - if (p_single || select_mode==SELECT_SINGLE) { + if (p_single || select_mode == SELECT_SINGLE) { if (!items[p_idx].selectable || items[p_idx].disabled) { return; } - for(int i=0;i<items.size();i++) { - items[i].selected=p_idx==i; + for (int i = 0; i < items.size(); i++) { + items[i].selected = p_idx == i; } - current=p_idx; - ensure_selected_visible=false; + current = p_idx; + ensure_selected_visible = false; } else { if (items[p_idx].selectable && !items[p_idx].disabled) { - items[p_idx].selected=true; + items[p_idx].selected = true; } } update(); - - } -void ItemList::unselect(int p_idx){ +void ItemList::unselect(int p_idx) { - ERR_FAIL_INDEX(p_idx,items.size()); + ERR_FAIL_INDEX(p_idx, items.size()); - if (select_mode!=SELECT_MULTI) { - items[p_idx].selected=false; - current=-1; + if (select_mode != SELECT_MULTI) { + items[p_idx].selected = false; + current = -1; } else { - items[p_idx].selected=false; + items[p_idx].selected = false; } update(); - } -bool ItemList::is_selected(int p_idx) const{ +bool ItemList::is_selected(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),false); + ERR_FAIL_INDEX_V(p_idx, items.size(), false); return items[p_idx].selected; - } void ItemList::set_current(int p_current) { - ERR_FAIL_INDEX(p_current,items.size()); + ERR_FAIL_INDEX(p_current, items.size()); - if (select_mode==SELECT_SINGLE) - select(p_current,true); + if (select_mode == SELECT_SINGLE) + select(p_current, true); else { - current=p_current; + current = p_current; update(); } } @@ -294,117 +265,110 @@ int ItemList::get_current() const { return current; } -void ItemList::move_item(int p_item,int p_to_pos) { +void ItemList::move_item(int p_item, int p_to_pos) { - ERR_FAIL_INDEX(p_item,items.size()); - ERR_FAIL_INDEX(p_to_pos,items.size()+1); + ERR_FAIL_INDEX(p_item, items.size()); + ERR_FAIL_INDEX(p_to_pos, items.size() + 1); - Item it=items[p_item]; + Item it = items[p_item]; items.remove(p_item); - if (p_to_pos>p_item) { + if (p_to_pos > p_item) { p_to_pos--; } - if (p_to_pos>=items.size()) { + if (p_to_pos >= items.size()) { items.push_back(it); } else { - items.insert(p_to_pos,it); + items.insert(p_to_pos, it); } - if (current<0) { + 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) { + } 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) { + } else if (p_to_pos < p_item && current < p_item && current > p_to_pos) { current++; } - update(); } -int ItemList::get_item_count() const{ +int ItemList::get_item_count() const { return items.size(); } -void ItemList::remove_item(int p_idx){ +void ItemList::remove_item(int p_idx) { - ERR_FAIL_INDEX(p_idx,items.size()); + ERR_FAIL_INDEX(p_idx, items.size()); items.remove(p_idx); update(); - shape_changed=true; - defer_select_single=-1; - - + shape_changed = true; + defer_select_single = -1; } -void ItemList::clear(){ +void ItemList::clear() { items.clear(); - current=-1; - ensure_selected_visible=false; + current = -1; + ensure_selected_visible = false; update(); - shape_changed=true; - defer_select_single=-1; - + shape_changed = true; + defer_select_single = -1; } -void ItemList::set_fixed_column_width(int p_size){ +void ItemList::set_fixed_column_width(int p_size) { - ERR_FAIL_COND(p_size<0); - fixed_column_width=p_size; + ERR_FAIL_COND(p_size < 0); + fixed_column_width = p_size; update(); - shape_changed=true; - + shape_changed = true; } -int ItemList::get_fixed_column_width() const{ +int ItemList::get_fixed_column_width() const { return fixed_column_width; } -void ItemList::set_same_column_width(bool p_enable){ +void ItemList::set_same_column_width(bool p_enable) { - same_column_width=p_enable; + same_column_width = p_enable; update(); - shape_changed=true; - + shape_changed = true; } -int ItemList::is_same_column_width() const{ +int ItemList::is_same_column_width() const { return same_column_width; } -void ItemList::set_max_text_lines(int p_lines){ +void ItemList::set_max_text_lines(int p_lines) { - ERR_FAIL_COND(p_lines<1); - max_text_lines=p_lines; + ERR_FAIL_COND(p_lines < 1); + max_text_lines = p_lines; update(); - shape_changed=true; - + shape_changed = true; } -int ItemList::get_max_text_lines() const{ +int ItemList::get_max_text_lines() const { return max_text_lines; } -void ItemList::set_max_columns(int p_amount){ +void ItemList::set_max_columns(int p_amount) { - ERR_FAIL_COND(p_amount<0); - max_columns=p_amount; + ERR_FAIL_COND(p_amount < 0); + max_columns = p_amount; update(); } -int ItemList::get_max_columns() const{ +int ItemList::get_max_columns() const { return max_columns; } void ItemList::set_select_mode(SelectMode p_mode) { - select_mode=p_mode; + select_mode = p_mode; update(); } @@ -413,22 +377,20 @@ ItemList::SelectMode ItemList::get_select_mode() const { return select_mode; } -void ItemList::set_icon_mode(IconMode p_mode){ +void ItemList::set_icon_mode(IconMode p_mode) { - icon_mode=p_mode; + icon_mode = p_mode; update(); - shape_changed=true; - + shape_changed = true; } -ItemList::IconMode ItemList::get_icon_mode() const{ +ItemList::IconMode ItemList::get_icon_mode() const { return icon_mode; } +void ItemList::set_fixed_icon_size(const Size2 &p_size) { -void ItemList::set_fixed_icon_size(const Size2& p_size) { - - fixed_icon_size=p_size; + fixed_icon_size = p_size; update(); } @@ -446,151 +408,142 @@ Size2 ItemList::Item::get_icon_size() const { return icon_region.size; } -void ItemList::_gui_input(const InputEvent& p_event) { +void ItemList::_gui_input(const InputEvent &p_event) { - if (defer_select_single>=0 && p_event.type==InputEvent::MOUSE_MOTION) { - defer_select_single=-1; + if (defer_select_single >= 0 && p_event.type == InputEvent::MOUSE_MOTION) { + defer_select_single = -1; return; } - if (defer_select_single>=0 && p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==BUTTON_LEFT && !p_event.mouse_button.pressed) { - - select(defer_select_single,true); + if (defer_select_single >= 0 && p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index == BUTTON_LEFT && !p_event.mouse_button.pressed) { + select(defer_select_single, true); - emit_signal("multi_selected",defer_select_single,true); - defer_select_single=-1; + emit_signal("multi_selected", defer_select_single, true); + defer_select_single = -1; return; } - if (p_event.type==InputEvent::MOUSE_BUTTON && (p_event.mouse_button.button_index==BUTTON_LEFT || (allow_rmb_select && p_event.mouse_button.button_index==BUTTON_RIGHT)) && p_event.mouse_button.pressed) { + if (p_event.type == InputEvent::MOUSE_BUTTON && (p_event.mouse_button.button_index == BUTTON_LEFT || (allow_rmb_select && p_event.mouse_button.button_index == BUTTON_RIGHT)) && p_event.mouse_button.pressed) { const InputEventMouseButton &mb = p_event.mouse_button; - search_string=""; //any mousepress cancels - Vector2 pos(mb.x,mb.y); + search_string = ""; //any mousepress cancels + Vector2 pos(mb.x, mb.y); Ref<StyleBox> bg = get_stylebox("bg"); - pos-=bg->get_offset(); - pos.y+=scroll_bar->get_value(); + pos -= bg->get_offset(); + pos.y += scroll_bar->get_value(); int closest = -1; - for(int i=0;i<items.size();i++) { + for (int i = 0; i < items.size(); i++) { Rect2 rc = items[i].rect_cache; - if (i%current_columns==current_columns-1) { - rc.size.width=get_size().width; //not right but works + if (i % current_columns == current_columns - 1) { + rc.size.width = get_size().width; //not right but works } if (rc.has_point(pos)) { - closest=i; + closest = i; break; } } - if (closest!=-1) { + if (closest != -1) { int i = closest; - if (select_mode==SELECT_MULTI && items[i].selected && mb.mod.command) { + if (select_mode == SELECT_MULTI && items[i].selected && mb.mod.command) { unselect(i); - emit_signal("multi_selected",i,false); + emit_signal("multi_selected", i, false); - } else if (select_mode==SELECT_MULTI && mb.mod.shift && current>=0 && current<items.size() && current!=i) { + } else if (select_mode == SELECT_MULTI && mb.mod.shift && current >= 0 && current < items.size() && current != i) { int from = current; int to = i; - if (i<current) { - SWAP(from,to); + if (i < current) { + SWAP(from, to); } - for(int j=from;j<=to;j++) { + for (int j = from; j <= to; j++) { bool selected = !items[j].selected; - select(j,false); + select(j, false); if (selected) - emit_signal("multi_selected",i,true); + emit_signal("multi_selected", i, true); } - if (p_event.mouse_button.button_index==BUTTON_RIGHT) { + if (p_event.mouse_button.button_index == BUTTON_RIGHT) { - emit_signal("item_rmb_selected",i,Vector2(mb.x,mb.y)); + emit_signal("item_rmb_selected", i, Vector2(mb.x, mb.y)); } } else { - if (!mb.doubleclick && !mb.mod.command && select_mode==SELECT_MULTI && items[i].selectable && !items[i].disabled && items[i].selected && p_event.mouse_button.button_index==BUTTON_LEFT) { - defer_select_single=i; + if (!mb.doubleclick && !mb.mod.command && select_mode == SELECT_MULTI && items[i].selectable && !items[i].disabled && items[i].selected && p_event.mouse_button.button_index == BUTTON_LEFT) { + defer_select_single = i; return; } - if (items[i].selected && p_event.mouse_button.button_index==BUTTON_RIGHT) { + if (items[i].selected && p_event.mouse_button.button_index == BUTTON_RIGHT) { - emit_signal("item_rmb_selected",i,Vector2(mb.x,mb.y)); + emit_signal("item_rmb_selected", i, Vector2(mb.x, mb.y)); } else { bool selected = !items[i].selected; - - select(i,select_mode==SELECT_SINGLE || !mb.mod.command); + select(i, select_mode == SELECT_SINGLE || !mb.mod.command); if (selected) { - if (select_mode==SELECT_SINGLE) { - emit_signal("item_selected",i); + if (select_mode == SELECT_SINGLE) { + emit_signal("item_selected", i); } else - emit_signal("multi_selected",i,true); - - + emit_signal("multi_selected", i, true); } - if (p_event.mouse_button.button_index==BUTTON_RIGHT) { + if (p_event.mouse_button.button_index == BUTTON_RIGHT) { - emit_signal("item_rmb_selected",i,Vector2(mb.x,mb.y)); + emit_signal("item_rmb_selected", i, Vector2(mb.x, mb.y)); } else if (/*select_mode==SELECT_SINGLE &&*/ mb.doubleclick) { - emit_signal("item_activated",i); - + emit_signal("item_activated", i); } } } - return; } else { Vector<int> sItems = get_selected_items(); - for(int i = 0; i < sItems.size(); i++) { + for (int i = 0; i < sItems.size(); i++) { unselect(sItems[i]); } } } - if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==BUTTON_WHEEL_UP && p_event.mouse_button.pressed) { - - scroll_bar->set_value( scroll_bar->get_value()-scroll_bar->get_page()/8 ); + if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index == BUTTON_WHEEL_UP && p_event.mouse_button.pressed) { + scroll_bar->set_value(scroll_bar->get_value() - scroll_bar->get_page() / 8); } - if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==BUTTON_WHEEL_DOWN && p_event.mouse_button.pressed) { - - scroll_bar->set_value( scroll_bar->get_value()+scroll_bar->get_page()/8 ); + if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index == BUTTON_WHEEL_DOWN && p_event.mouse_button.pressed) { + scroll_bar->set_value(scroll_bar->get_value() + scroll_bar->get_page() / 8); } - if (p_event.is_pressed() && items.size()>0) { + if (p_event.is_pressed() && items.size() > 0) { if (p_event.is_action("ui_up")) { - if (search_string!="") { + if (search_string != "") { uint64_t now = OS::get_singleton()->get_ticks_msec(); - uint64_t diff = now-search_time_msec; + uint64_t diff = now - search_time_msec; - if (diff<int(GlobalConfig::get_singleton()->get("gui/timers/incremental_search_max_interval_msec"))*2) { + if (diff < int(GlobalConfig::get_singleton()->get("gui/timers/incremental_search_max_interval_msec")) * 2) { - for(int i=current-1;i>=0;i--) { + for (int i = current - 1; i >= 0; i--) { if (items[i].text.begins_with(search_string)) { set_current(i); ensure_current_is_visible(); - if (select_mode==SELECT_SINGLE) { - emit_signal("item_selected",current); + if (select_mode == SELECT_SINGLE) { + emit_signal("item_selected", current); } - break; } } @@ -599,31 +552,31 @@ void ItemList::_gui_input(const InputEvent& p_event) { } } - if (current>=current_columns) { - set_current(current-current_columns); + if (current >= current_columns) { + set_current(current - current_columns); ensure_current_is_visible(); - if (select_mode==SELECT_SINGLE) { - emit_signal("item_selected",current); + if (select_mode == SELECT_SINGLE) { + emit_signal("item_selected", current); } accept_event(); } } else if (p_event.is_action("ui_down")) { - if (search_string!="") { + if (search_string != "") { uint64_t now = OS::get_singleton()->get_ticks_msec(); - uint64_t diff = now-search_time_msec; + uint64_t diff = now - search_time_msec; - if (diff<int(GlobalConfig::get_singleton()->get("gui/timers/incremental_search_max_interval_msec"))*2) { + if (diff < int(GlobalConfig::get_singleton()->get("gui/timers/incremental_search_max_interval_msec")) * 2) { - for(int i=current+1;i<items.size();i++) { + for (int i = current + 1; i < items.size(); i++) { if (items[i].text.begins_with(search_string)) { set_current(i); ensure_current_is_visible(); - if (select_mode==SELECT_SINGLE) { - emit_signal("item_selected",current); + if (select_mode == SELECT_SINGLE) { + emit_signal("item_selected", current); } break; } @@ -633,25 +586,24 @@ void ItemList::_gui_input(const InputEvent& p_event) { } } - if (current<items.size()-current_columns) { - set_current(current+current_columns); + if (current < items.size() - current_columns) { + set_current(current + current_columns); ensure_current_is_visible(); - if (select_mode==SELECT_SINGLE) { - emit_signal("item_selected",current); + if (select_mode == SELECT_SINGLE) { + emit_signal("item_selected", current); } accept_event(); - } } else if (p_event.is_action("ui_page_up")) { - search_string=""; //any mousepress cancels + search_string = ""; //any mousepress cancels - for(int i=4;i>0;i--) { - if (current-current_columns*i >=0 ) { - set_current( current- current_columns*i); + for (int i = 4; i > 0; i--) { + if (current - current_columns * i >= 0) { + set_current(current - current_columns * i); ensure_current_is_visible(); - if (select_mode==SELECT_SINGLE) { - emit_signal("item_selected",current); + if (select_mode == SELECT_SINGLE) { + emit_signal("item_selected", current); } accept_event(); break; @@ -659,14 +611,14 @@ void ItemList::_gui_input(const InputEvent& p_event) { } } else if (p_event.is_action("ui_page_down")) { - search_string=""; //any mousepress cancels + search_string = ""; //any mousepress cancels - for(int i=4;i>0;i--) { - if (current+current_columns*i < items.size() ) { - set_current( current+ current_columns*i); + for (int i = 4; i > 0; i--) { + if (current + current_columns * i < items.size()) { + set_current(current + current_columns * i); ensure_current_is_visible(); - if (select_mode==SELECT_SINGLE) { - emit_signal("item_selected",current); + if (select_mode == SELECT_SINGLE) { + emit_signal("item_selected", current); } accept_event(); @@ -675,146 +627,134 @@ void ItemList::_gui_input(const InputEvent& p_event) { } } else if (p_event.is_action("ui_left")) { - search_string=""; //any mousepress cancels + search_string = ""; //any mousepress cancels - if (current%current_columns!=0) { - set_current(current-1); + if (current % current_columns != 0) { + set_current(current - 1); ensure_current_is_visible(); - if (select_mode==SELECT_SINGLE) { - emit_signal("item_selected",current); + if (select_mode == SELECT_SINGLE) { + emit_signal("item_selected", current); } accept_event(); - } } else if (p_event.is_action("ui_right")) { - search_string=""; //any mousepress cancels + search_string = ""; //any mousepress cancels - if (current%current_columns!=(current_columns-1)) { - set_current(current+1); + if (current % current_columns != (current_columns - 1)) { + set_current(current + 1); ensure_current_is_visible(); - if (select_mode==SELECT_SINGLE) { - emit_signal("item_selected",current); + if (select_mode == SELECT_SINGLE) { + emit_signal("item_selected", current); } accept_event(); - } } else if (p_event.is_action("ui_cancel")) { - search_string=""; + search_string = ""; } else if (p_event.is_action("ui_select")) { - - if (select_mode==SELECT_MULTI && current>=0 && current<items.size()) { + if (select_mode == SELECT_MULTI && current >= 0 && current < items.size()) { if (items[current].selectable && !items[current].disabled && !items[current].selected) { - select(current,false); - emit_signal("multi_selected",current,true); + select(current, false); + emit_signal("multi_selected", current, true); } else if (items[current].selected) { unselect(current); - emit_signal("multi_selected",current,false); + emit_signal("multi_selected", current, false); } } } else if (p_event.is_action("ui_accept")) { - search_string=""; //any mousepress cance + search_string = ""; //any mousepress cance - if (current>=0 && current<items.size()) { - emit_signal("item_activated",current); + if (current >= 0 && current < items.size()) { + emit_signal("item_activated", current); } - } else if (p_event.type==InputEvent::KEY) { + } else if (p_event.type == InputEvent::KEY) { if (p_event.key.unicode) { uint64_t now = OS::get_singleton()->get_ticks_msec(); - uint64_t diff = now-search_time_msec; - uint64_t max_interval = uint64_t(GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec",2000)); + uint64_t diff = now - search_time_msec; + uint64_t max_interval = uint64_t(GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec", 2000)); search_time_msec = now; - if (diff>max_interval) { - search_string=""; + if (diff > max_interval) { + search_string = ""; } - search_string+=String::chr(p_event.key.unicode); - for(int i=0;i<items.size();i++) { + search_string += String::chr(p_event.key.unicode); + for (int i = 0; i < items.size(); i++) { if (items[i].text.begins_with(search_string)) { set_current(i); ensure_current_is_visible(); - if (select_mode==SELECT_SINGLE) { - emit_signal("item_selected",current); + if (select_mode == SELECT_SINGLE) { + emit_signal("item_selected", current); } break; } } - } - } } - - - - } void ItemList::ensure_current_is_visible() { - ensure_selected_visible=true; + ensure_selected_visible = true; update(); } static Rect2 _adjust_to_max_size(Size2 p_size, Size2 p_max_size) { - Size2 size=p_max_size; + Size2 size = p_max_size; int tex_width = p_size.width * size.height / p_size.height; int tex_height = size.height; - if (tex_width>size.width) { - tex_width=size.width; - tex_height=p_size.height * tex_width / p_size.width; + if (tex_width > size.width) { + tex_width = size.width; + tex_height = p_size.height * tex_width / p_size.width; } - int ofs_x=(size.width - tex_width)/2; - int ofs_y=(size.height - tex_height)/2; - - return Rect2(ofs_x,ofs_y,tex_width,tex_height); - + int ofs_x = (size.width - tex_width) / 2; + int ofs_y = (size.height - tex_height) / 2; + return Rect2(ofs_x, ofs_y, tex_width, tex_height); } void ItemList::_notification(int p_what) { - if (p_what==NOTIFICATION_RESIZED) { - shape_changed=true; + if (p_what == NOTIFICATION_RESIZED) { + shape_changed = true; update(); } - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { Ref<StyleBox> bg = get_stylebox("bg"); int mw = scroll_bar->get_minimum_size().x; - scroll_bar->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_END,mw+bg->get_margin(MARGIN_RIGHT)); - scroll_bar->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,bg->get_margin(MARGIN_RIGHT)); - scroll_bar->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,bg->get_margin(MARGIN_TOP)); - scroll_bar->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,bg->get_margin(MARGIN_BOTTOM)); - + scroll_bar->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, mw + bg->get_margin(MARGIN_RIGHT)); + scroll_bar->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, bg->get_margin(MARGIN_RIGHT)); + scroll_bar->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, bg->get_margin(MARGIN_TOP)); + scroll_bar->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, bg->get_margin(MARGIN_BOTTOM)); Size2 size = get_size(); - float page = size.height-bg->get_minimum_size().height; - int width = size.width-bg->get_minimum_size().width; - if (scroll_bar->is_visible()){ - width-=mw+bg->get_margin(MARGIN_RIGHT); + float page = size.height - bg->get_minimum_size().height; + int width = size.width - bg->get_minimum_size().width; + if (scroll_bar->is_visible()) { + width -= mw + bg->get_margin(MARGIN_RIGHT); } scroll_bar->set_page(page); - draw_style_box(bg,Rect2(Point2(),size)); + draw_style_box(bg, Rect2(Point2(), size)); int hseparation = get_constant("hseparation"); int vseparation = get_constant("vseparation"); int icon_margin = get_constant("icon_margin"); int line_separation = get_constant("line_separation"); - Ref<StyleBox> sbsel = has_focus()?get_stylebox("selected_focus"):get_stylebox("selected"); - Ref<StyleBox> cursor = has_focus()?get_stylebox("cursor"):get_stylebox("cursor_unfocused"); + Ref<StyleBox> sbsel = has_focus() ? get_stylebox("selected_focus") : get_stylebox("selected"); + Ref<StyleBox> cursor = has_focus() ? get_stylebox("cursor") : get_stylebox("cursor_unfocused"); Ref<Font> font = get_font("font"); Color guide_color = get_color("guide_color"); @@ -830,9 +770,9 @@ void ItemList::_notification(int p_what) { } if (has_focus()) { - VisualServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(),true); - draw_style_box(get_stylebox("bg_focus"),Rect2(Point2(),size)); - VisualServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(),false); + VisualServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(), true); + draw_style_box(get_stylebox("bg_focus"), Rect2(Point2(), size)); + VisualServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(), false); } if (shape_changed) { @@ -840,114 +780,109 @@ void ItemList::_notification(int p_what) { float max_column_width = 0; //1- compute item minimum sizes - for(int i=0;i<items.size();i++) { + for (int i = 0; i < items.size(); i++) { Size2 minsize; if (items[i].icon.is_valid()) { - if (fixed_icon_size.x>0 && fixed_icon_size.y>0) { - minsize=fixed_icon_size* icon_scale; + if (fixed_icon_size.x > 0 && fixed_icon_size.y > 0) { + minsize = fixed_icon_size * icon_scale; } else { - minsize=items[i].get_icon_size() *icon_scale; + minsize = items[i].get_icon_size() * icon_scale; } - if (items[i].text!="") { - if (icon_mode==ICON_MODE_TOP) { - minsize.y+=icon_margin; + if (items[i].text != "") { + if (icon_mode == ICON_MODE_TOP) { + minsize.y += icon_margin; } else { - minsize.x+=icon_margin; + minsize.x += icon_margin; } } } - if (items[i].text!="") { + if (items[i].text != "") { Size2 s = font->get_string_size(items[i].text); //s.width=MIN(s.width,fixed_column_width); - - - if (icon_mode==ICON_MODE_TOP) { - minsize.x=MAX(minsize.x,s.width); - if (max_text_lines>0) { - minsize.y+=(font_height+line_separation)*max_text_lines; + if (icon_mode == ICON_MODE_TOP) { + minsize.x = MAX(minsize.x, s.width); + if (max_text_lines > 0) { + minsize.y += (font_height + line_separation) * max_text_lines; } else { - minsize.y+=s.height; + minsize.y += s.height; } } else { - minsize.y=MAX(minsize.y,s.height); - minsize.x+=s.width; + minsize.y = MAX(minsize.y, s.height); + minsize.x += s.width; } } - - if (fixed_column_width>0) - minsize.x=fixed_column_width; - max_column_width=MAX(max_column_width,minsize.x); - items[i].rect_cache.size=minsize; - items[i].min_rect_cache.size=minsize; - + if (fixed_column_width > 0) + minsize.x = fixed_column_width; + max_column_width = MAX(max_column_width, minsize.x); + items[i].rect_cache.size = minsize; + items[i].min_rect_cache.size = minsize; } int fit_size = size.x - bg->get_minimum_size().width - mw; //2-attempt best fit current_columns = 0x7FFFFFFF; - if (max_columns>0) - current_columns=max_columns; + if (max_columns > 0) + current_columns = max_columns; - - while(true) { + while (true) { //repeat util all fits //print_line("try with "+itos(current_columns)); - bool all_fit=true; + bool all_fit = true; Vector2 ofs; - int col=0; - int max_h=0; + int col = 0; + int max_h = 0; separators.clear(); - for(int i=0;i<items.size();i++) { + for (int i = 0; i < items.size(); i++) { - if (current_columns>1 && items[i].rect_cache.size.width+ofs.x > fit_size) { + if (current_columns > 1 && items[i].rect_cache.size.width + ofs.x > fit_size) { //went past - current_columns=MAX(col,1); - all_fit=false; + current_columns = MAX(col, 1); + all_fit = false; break; } - items[i].rect_cache=items[i].min_rect_cache; - if(same_column_width) - items[i].rect_cache.size.x=max_column_width; - items[i].rect_cache.pos=ofs; - max_h=MAX(max_h,items[i].rect_cache.size.y); - ofs.x+=items[i].rect_cache.size.x + hseparation; + items[i].rect_cache = items[i].min_rect_cache; + if (same_column_width) + items[i].rect_cache.size.x = max_column_width; + items[i].rect_cache.pos = ofs; + max_h = MAX(max_h, items[i].rect_cache.size.y); + ofs.x += items[i].rect_cache.size.x + hseparation; //print_line("item "+itos(i)+" ofs "+rtos(items[i].rect_cache.size.x)); col++; - if (col==current_columns) { + if (col == current_columns) { - if (i<items.size()-1) - separators.push_back(ofs.y+max_h+vseparation/2); + if (i < items.size() - 1) + separators.push_back(ofs.y + max_h + vseparation / 2); - for(int j=i;j>=0 && col>0;j--, col--) { + for (int j = i; j >= 0 && col > 0; j--, col--) { items[j].rect_cache.size.y = max_h; } - ofs.x=0; - ofs.y+=max_h+vseparation; - col=0; - max_h=0; + ofs.x = 0; + ofs.y += max_h + vseparation; + col = 0; + max_h = 0; } } - for(int j=items.size()-1;j>=0 && col>0;j--, col--) { + for (int j = items.size() - 1; j >= 0 && col > 0; j--, col--) { items[j].rect_cache.size.y = max_h; } if (all_fit) { - float max = MAX(page,ofs.y+max_h); + float max = MAX(page, ofs.y + max_h); scroll_bar->set_max(max); //print_line("max: "+rtos(max)+" page "+rtos(page)); - if (max<=page) { + if (max <= page) { scroll_bar->set_value(0); scroll_bar->hide(); } else { @@ -957,12 +892,11 @@ void ItemList::_notification(int p_what) { } } - - shape_changed=false; + shape_changed = false; } //ensure_selected_visible needs to be checked before we draw the list. - if (ensure_selected_visible && current>=0 && current <=items.size()) { + if (ensure_selected_visible && current >= 0 && current <= items.size()) { Rect2 r = items[current].rect_cache; int from = scroll_bar->get_value(); @@ -970,200 +904,188 @@ void ItemList::_notification(int p_what) { if (r.pos.y < from) { scroll_bar->set_value(r.pos.y); - } else if (r.pos.y+r.size.y > to) { - scroll_bar->set_value(r.pos.y+r.size.y - (to-from)); + } else if (r.pos.y + r.size.y > to) { + scroll_bar->set_value(r.pos.y + r.size.y - (to - from)); } - - } - ensure_selected_visible=false; + ensure_selected_visible = false; Vector2 base_ofs = bg->get_offset(); - base_ofs.y-=int(scroll_bar->get_value()); - - Rect2 clip(Point2(),size-bg->get_minimum_size()+Vector2(0,scroll_bar->get_value())); + base_ofs.y -= int(scroll_bar->get_value()); - for(int i=0;i<items.size();i++) { + Rect2 clip(Point2(), size - bg->get_minimum_size() + Vector2(0, scroll_bar->get_value())); + for (int i = 0; i < items.size(); i++) { Rect2 rcache = items[i].rect_cache; if (!clip.intersects(rcache)) continue; - - if (current_columns==1) { - rcache.size.width = width-rcache.pos.x; + if (current_columns == 1) { + rcache.size.width = width - rcache.pos.x; } - Rect2 r=rcache; - r.pos+=base_ofs; + Rect2 r = rcache; + r.pos += base_ofs; // Use stylebox to dimension potential bg color, even if not selected - r.pos.x-=sbsel->get_margin(MARGIN_LEFT); - r.size.x+=sbsel->get_margin(MARGIN_LEFT)+sbsel->get_margin(MARGIN_RIGHT); - r.pos.y-=sbsel->get_margin(MARGIN_TOP); - r.size.y+=sbsel->get_margin(MARGIN_TOP)+sbsel->get_margin(MARGIN_BOTTOM); + r.pos.x -= sbsel->get_margin(MARGIN_LEFT); + r.size.x += sbsel->get_margin(MARGIN_LEFT) + sbsel->get_margin(MARGIN_RIGHT); + r.pos.y -= sbsel->get_margin(MARGIN_TOP); + r.size.y += sbsel->get_margin(MARGIN_TOP) + sbsel->get_margin(MARGIN_BOTTOM); if (items[i].selected) { - draw_style_box(sbsel,r); + draw_style_box(sbsel, r); } - if (items[i].custom_bg.a>0.001) { - r.pos.x+=2; - r.size.x-=4; - r.pos.y+=2; - r.size.y-=4; - draw_rect(r,items[i].custom_bg); + if (items[i].custom_bg.a > 0.001) { + r.pos.x += 2; + r.size.x -= 4; + r.pos.y += 2; + r.size.y -= 4; + draw_rect(r, items[i].custom_bg); } - Vector2 text_ofs; if (items[i].icon.is_valid()) { Size2 icon_size; //= _adjust_to_max_size(items[i].get_icon_size(),fixed_icon_size) * icon_scale; - if (fixed_icon_size.x>0 && fixed_icon_size.y>0) { - icon_size=fixed_icon_size* icon_scale; + if (fixed_icon_size.x > 0 && fixed_icon_size.y > 0) { + icon_size = fixed_icon_size * icon_scale; } else { - icon_size=items[i].get_icon_size() *icon_scale; - + icon_size = items[i].get_icon_size() * icon_scale; } Vector2 icon_ofs; Point2 pos = items[i].rect_cache.pos + icon_ofs + base_ofs; - if (icon_mode==ICON_MODE_TOP) { + if (icon_mode == ICON_MODE_TOP) { - pos.x += Math::floor((items[i].rect_cache.size.width - icon_size.width)/2); + pos.x += Math::floor((items[i].rect_cache.size.width - icon_size.width) / 2); pos.y += MIN( - Math::floor((items[i].rect_cache.size.height - icon_size.height)/2), - items[i].rect_cache.size.height - items[i].min_rect_cache.size.height - ); + Math::floor((items[i].rect_cache.size.height - icon_size.height) / 2), + items[i].rect_cache.size.height - items[i].min_rect_cache.size.height); text_ofs.y = icon_size.height + icon_margin; text_ofs.y += items[i].rect_cache.size.height - items[i].min_rect_cache.size.height; } else { - pos.y += Math::floor((items[i].rect_cache.size.height - icon_size.height)/2); + pos.y += Math::floor((items[i].rect_cache.size.height - icon_size.height) / 2); text_ofs.x = icon_size.width + icon_margin; } - Rect2 draw_rect=Rect2(pos,icon_size); + Rect2 draw_rect = Rect2(pos, icon_size); - if (fixed_icon_size.x>0 && fixed_icon_size.y>0) { - Rect2 adj = _adjust_to_max_size(items[i].get_icon_size() * icon_scale,icon_size); - draw_rect.pos+=adj.pos; - draw_rect.size=adj.size; + if (fixed_icon_size.x > 0 && fixed_icon_size.y > 0) { + Rect2 adj = _adjust_to_max_size(items[i].get_icon_size() * icon_scale, icon_size); + draw_rect.pos += adj.pos; + draw_rect.size = adj.size; } - Color modulate=Color(1,1,1,1); + Color modulate = Color(1, 1, 1, 1); if (items[i].disabled) - modulate.a*=0.5; + modulate.a *= 0.5; if (items[i].icon_region.has_no_area()) - draw_texture_rect(items[i].icon, draw_rect,false,modulate ); + draw_texture_rect(items[i].icon, draw_rect, false, modulate); else - draw_texture_rect_region(items[i].icon, draw_rect, items[i].icon_region,modulate); - + draw_texture_rect_region(items[i].icon, draw_rect, items[i].icon_region, modulate); } if (items[i].tag_icon.is_valid()) { - draw_texture(items[i].tag_icon,items[i].rect_cache.pos+base_ofs); + draw_texture(items[i].tag_icon, items[i].rect_cache.pos + base_ofs); } - if (items[i].text!="") { + if (items[i].text != "") { - int max_len=-1; + int max_len = -1; Vector2 size = font->get_string_size(items[i].text); if (fixed_column_width) - max_len=fixed_column_width; - else if(same_column_width) - max_len=items[i].rect_cache.size.x; + max_len = fixed_column_width; + else if (same_column_width) + max_len = items[i].rect_cache.size.x; else - max_len=size.x; + max_len = size.x; - Color modulate=items[i].selected?font_color_selected:font_color; + Color modulate = items[i].selected ? font_color_selected : font_color; if (items[i].disabled) - modulate.a*=0.5; + modulate.a *= 0.5; - if (icon_mode==ICON_MODE_TOP && max_text_lines>0) { + if (icon_mode == ICON_MODE_TOP && max_text_lines > 0) { int ss = items[i].text.length(); - float ofs=0; - int line=0; - for(int j=0;j<=ss;j++) { - - int cs = j<ss?font->get_char_size(items[i].text[j],items[i].text[j+1]).x:0; - if (ofs+cs>max_len || j==ss) { - line_limit_cache[line]=j; - line_size_cache[line]=ofs; + float ofs = 0; + int line = 0; + for (int j = 0; j <= ss; j++) { + + int cs = j < ss ? font->get_char_size(items[i].text[j], items[i].text[j + 1]).x : 0; + if (ofs + cs > max_len || j == ss) { + line_limit_cache[line] = j; + line_size_cache[line] = ofs; line++; - ofs=0; - if (line>=max_text_lines) + ofs = 0; + if (line >= max_text_lines) break; } else { - ofs+=cs; + ofs += cs; } - } - line=0; - ofs=0; + line = 0; + ofs = 0; - text_ofs.y+=font->get_ascent(); - text_ofs=text_ofs.floor(); - text_ofs+=base_ofs; - text_ofs+=items[i].rect_cache.pos; + text_ofs.y += font->get_ascent(); + text_ofs = text_ofs.floor(); + text_ofs += base_ofs; + text_ofs += items[i].rect_cache.pos; - for(int j=0;j<ss;j++) { + for (int j = 0; j < ss; j++) { - if (j==line_limit_cache[line]) { + if (j == line_limit_cache[line]) { line++; - ofs=0; - if (line>=max_text_lines) + ofs = 0; + if (line >= max_text_lines) break; } - ofs+=font->draw_char(get_canvas_item(),text_ofs+Vector2(ofs+(max_len-line_size_cache[line])/2,line*(font_height+line_separation)).floor(),items[i].text[j],items[i].text[j+1],modulate); + ofs += font->draw_char(get_canvas_item(), text_ofs + Vector2(ofs + (max_len - line_size_cache[line]) / 2, line * (font_height + line_separation)).floor(), items[i].text[j], items[i].text[j + 1], modulate); } //special multiline mode } else { - if (fixed_column_width>0) - size.x=MIN(size.x,fixed_column_width); + if (fixed_column_width > 0) + size.x = MIN(size.x, fixed_column_width); - if (icon_mode==ICON_MODE_TOP) { - text_ofs.x+=(items[i].rect_cache.size.width-size.x)/2; + if (icon_mode == ICON_MODE_TOP) { + text_ofs.x += (items[i].rect_cache.size.width - size.x) / 2; } else { - text_ofs.y+=(items[i].rect_cache.size.height-size.y)/2; + text_ofs.y += (items[i].rect_cache.size.height - size.y) / 2; } - text_ofs.y+=font->get_ascent(); - text_ofs=text_ofs.floor(); - text_ofs+=base_ofs; - text_ofs+=items[i].rect_cache.pos; + text_ofs.y += font->get_ascent(); + text_ofs = text_ofs.floor(); + text_ofs += base_ofs; + text_ofs += items[i].rect_cache.pos; - draw_string(font,text_ofs,items[i].text,modulate,max_len+1); + draw_string(font, text_ofs, items[i].text, modulate, max_len + 1); } - - } - if (select_mode==SELECT_MULTI && i==current) { - - Rect2 r=rcache; - r.pos+=base_ofs; - draw_style_box(cursor,r); + if (select_mode == SELECT_MULTI && i == current) { + Rect2 r = rcache; + r.pos += base_ofs; + draw_style_box(cursor, r); } } - for(int i=0;i<separators.size();i++) { - draw_line(Vector2(bg->get_margin(MARGIN_LEFT),base_ofs.y+separators[i]),Vector2(size.width-bg->get_margin(MARGIN_LEFT),base_ofs.y+separators[i]),guide_color); + for (int i = 0; i < separators.size(); i++) { + draw_line(Vector2(bg->get_margin(MARGIN_LEFT), base_ofs.y + separators[i]), Vector2(size.width - bg->get_margin(MARGIN_LEFT), base_ofs.y + separators[i]), guide_color); } } } @@ -1172,66 +1094,64 @@ void ItemList::_scroll_changed(double) { update(); } -int ItemList::get_item_at_pos(const Point2& p_pos, bool p_exact) const { +int ItemList::get_item_at_pos(const Point2 &p_pos, bool p_exact) const { - Vector2 pos=p_pos; + Vector2 pos = p_pos; Ref<StyleBox> bg = get_stylebox("bg"); - pos-=bg->get_offset(); - pos.y+=scroll_bar->get_value(); + pos -= bg->get_offset(); + pos.y += scroll_bar->get_value(); int closest = -1; - int closest_dist=0x7FFFFFFF; + int closest_dist = 0x7FFFFFFF; - for(int i=0;i<items.size();i++) { + for (int i = 0; i < items.size(); i++) { Rect2 rc = items[i].rect_cache; - if (i%current_columns==current_columns-1) { - rc.size.width=get_size().width; //not right but works + if (i % current_columns == current_columns - 1) { + rc.size.width = get_size().width; //not right but works } if (rc.has_point(pos)) { - closest=i; + closest = i; break; } float dist = rc.distance_to(pos); - if (!p_exact && dist<closest_dist) { - closest=i; - closest_dist=dist; + if (!p_exact && dist < closest_dist) { + closest = i; + closest_dist = dist; } } return closest; } -bool ItemList::is_pos_at_end_of_items(const Point2& p_pos) const { +bool ItemList::is_pos_at_end_of_items(const Point2 &p_pos) const { if (items.empty()) return true; - Vector2 pos=p_pos; + Vector2 pos = p_pos; Ref<StyleBox> bg = get_stylebox("bg"); - pos-=bg->get_offset(); - pos.y+=scroll_bar->get_value(); + pos -= bg->get_offset(); + pos.y += scroll_bar->get_value(); - Rect2 endrect = items[items.size()-1].rect_cache; + Rect2 endrect = items[items.size() - 1].rect_cache; return (pos.y > endrect.pos.y + endrect.size.y); - } - -String ItemList::get_tooltip(const Point2& p_pos) const { +String ItemList::get_tooltip(const Point2 &p_pos) const { int closest = get_item_at_pos(p_pos); - if (closest!=-1) { + if (closest != -1) { if (!items[closest].tooltip_enabled) { return ""; } - if (items[closest].tooltip!="") { + if (items[closest].tooltip != "") { return items[closest].tooltip; } - if (items[closest].text!="") { + if (items[closest].text != "") { return items[closest].text; } } @@ -1243,10 +1163,10 @@ void ItemList::sort_items_by_text() { items.sort(); update(); - shape_changed=true; + shape_changed = true; - if (select_mode==SELECT_SINGLE) { - for(int i=0;i<items.size();i++) { + if (select_mode == SELECT_SINGLE) { + for (int i = 0; i < items.size(); i++) { if (items[i].selected) { select(i); return; @@ -1255,21 +1175,19 @@ void ItemList::sort_items_by_text() { } } -int ItemList::find_metadata(const Variant& p_metadata) const { +int ItemList::find_metadata(const Variant &p_metadata) const { - for(int i=0;i<items.size();i++) { - if (items[i].metadata==p_metadata) { + for (int i = 0; i < items.size(); i++) { + if (items[i].metadata == p_metadata) { return i; } } return -1; - } - void ItemList::set_allow_rmb_select(bool p_allow) { - allow_rmb_select=p_allow; + allow_rmb_select = p_allow; } bool ItemList::get_allow_rmb_select() const { @@ -1298,128 +1216,126 @@ Vector<int> ItemList::get_selected_items() { return selected; } -void ItemList::_bind_methods(){ +void ItemList::_bind_methods() { - ClassDB::bind_method(D_METHOD("add_item","text","icon:Texture","selectable"),&ItemList::add_item,DEFVAL(Variant()),DEFVAL(true)); - ClassDB::bind_method(D_METHOD("add_icon_item","icon:Texture","selectable"),&ItemList::add_icon_item,DEFVAL(true)); + ClassDB::bind_method(D_METHOD("add_item", "text", "icon:Texture", "selectable"), &ItemList::add_item, DEFVAL(Variant()), DEFVAL(true)); + ClassDB::bind_method(D_METHOD("add_icon_item", "icon:Texture", "selectable"), &ItemList::add_icon_item, DEFVAL(true)); - ClassDB::bind_method(D_METHOD("set_item_text","idx","text"),&ItemList::set_item_text); - ClassDB::bind_method(D_METHOD("get_item_text","idx"),&ItemList::get_item_text); + ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &ItemList::set_item_text); + ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &ItemList::get_item_text); - ClassDB::bind_method(D_METHOD("set_item_icon","idx","icon:Texture"),&ItemList::set_item_icon); - ClassDB::bind_method(D_METHOD("get_item_icon:Texture","idx"),&ItemList::get_item_icon); + ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "icon:Texture"), &ItemList::set_item_icon); + ClassDB::bind_method(D_METHOD("get_item_icon:Texture", "idx"), &ItemList::get_item_icon); - ClassDB::bind_method(D_METHOD("set_item_icon_region","idx","rect"),&ItemList::set_item_icon_region); - ClassDB::bind_method(D_METHOD("get_item_icon_region","idx"),&ItemList::get_item_icon_region); + ClassDB::bind_method(D_METHOD("set_item_icon_region", "idx", "rect"), &ItemList::set_item_icon_region); + ClassDB::bind_method(D_METHOD("get_item_icon_region", "idx"), &ItemList::get_item_icon_region); - ClassDB::bind_method(D_METHOD("set_item_selectable","idx","selectable"),&ItemList::set_item_selectable); - ClassDB::bind_method(D_METHOD("is_item_selectable","idx"),&ItemList::is_item_selectable); + ClassDB::bind_method(D_METHOD("set_item_selectable", "idx", "selectable"), &ItemList::set_item_selectable); + ClassDB::bind_method(D_METHOD("is_item_selectable", "idx"), &ItemList::is_item_selectable); - ClassDB::bind_method(D_METHOD("set_item_disabled","idx","disabled"),&ItemList::set_item_disabled); - ClassDB::bind_method(D_METHOD("is_item_disabled","idx"),&ItemList::is_item_disabled); + ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &ItemList::set_item_disabled); + ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &ItemList::is_item_disabled); - ClassDB::bind_method(D_METHOD("set_item_metadata","idx","metadata"),&ItemList::set_item_metadata); - ClassDB::bind_method(D_METHOD("get_item_metadata","idx"),&ItemList::get_item_metadata); + ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &ItemList::set_item_metadata); + ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &ItemList::get_item_metadata); - ClassDB::bind_method(D_METHOD("set_item_custom_bg_color","idx","custom_bg_color"),&ItemList::set_item_custom_bg_color); - ClassDB::bind_method(D_METHOD("get_item_custom_bg_color","idx"),&ItemList::get_item_custom_bg_color); + ClassDB::bind_method(D_METHOD("set_item_custom_bg_color", "idx", "custom_bg_color"), &ItemList::set_item_custom_bg_color); + ClassDB::bind_method(D_METHOD("get_item_custom_bg_color", "idx"), &ItemList::get_item_custom_bg_color); - ClassDB::bind_method(D_METHOD("set_item_tooltip_enabled","idx","enable"),&ItemList::set_item_tooltip_enabled); - ClassDB::bind_method(D_METHOD("is_item_tooltip_enabled","idx"),&ItemList::is_item_tooltip_enabled); + ClassDB::bind_method(D_METHOD("set_item_tooltip_enabled", "idx", "enable"), &ItemList::set_item_tooltip_enabled); + ClassDB::bind_method(D_METHOD("is_item_tooltip_enabled", "idx"), &ItemList::is_item_tooltip_enabled); - ClassDB::bind_method(D_METHOD("set_item_tooltip","idx","tooltip"),&ItemList::set_item_tooltip); - ClassDB::bind_method(D_METHOD("get_item_tooltip","idx"),&ItemList::get_item_tooltip); + ClassDB::bind_method(D_METHOD("set_item_tooltip", "idx", "tooltip"), &ItemList::set_item_tooltip); + ClassDB::bind_method(D_METHOD("get_item_tooltip", "idx"), &ItemList::get_item_tooltip); - ClassDB::bind_method(D_METHOD("select","idx","single"),&ItemList::select,DEFVAL(true)); - ClassDB::bind_method(D_METHOD("unselect","idx"),&ItemList::unselect); - 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("select", "idx", "single"), &ItemList::select, DEFVAL(true)); + ClassDB::bind_method(D_METHOD("unselect", "idx"), &ItemList::unselect); + 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("get_item_count"),&ItemList::get_item_count); - ClassDB::bind_method(D_METHOD("remove_item","idx"),&ItemList::remove_item); + ClassDB::bind_method(D_METHOD("get_item_count"), &ItemList::get_item_count); + ClassDB::bind_method(D_METHOD("remove_item", "idx"), &ItemList::remove_item); - ClassDB::bind_method(D_METHOD("clear"),&ItemList::clear); - ClassDB::bind_method(D_METHOD("sort_items_by_text"),&ItemList::sort_items_by_text); + ClassDB::bind_method(D_METHOD("clear"), &ItemList::clear); + ClassDB::bind_method(D_METHOD("sort_items_by_text"), &ItemList::sort_items_by_text); - ClassDB::bind_method(D_METHOD("set_fixed_column_width","width"),&ItemList::set_fixed_column_width); - ClassDB::bind_method(D_METHOD("get_fixed_column_width"),&ItemList::get_fixed_column_width); + ClassDB::bind_method(D_METHOD("set_fixed_column_width", "width"), &ItemList::set_fixed_column_width); + ClassDB::bind_method(D_METHOD("get_fixed_column_width"), &ItemList::get_fixed_column_width); - ClassDB::bind_method(D_METHOD("set_same_column_width","enable"),&ItemList::set_same_column_width); - ClassDB::bind_method(D_METHOD("is_same_column_width"),&ItemList::is_same_column_width); + ClassDB::bind_method(D_METHOD("set_same_column_width", "enable"), &ItemList::set_same_column_width); + ClassDB::bind_method(D_METHOD("is_same_column_width"), &ItemList::is_same_column_width); - ClassDB::bind_method(D_METHOD("set_max_text_lines","lines"),&ItemList::set_max_text_lines); - ClassDB::bind_method(D_METHOD("get_max_text_lines"),&ItemList::get_max_text_lines); + ClassDB::bind_method(D_METHOD("set_max_text_lines", "lines"), &ItemList::set_max_text_lines); + ClassDB::bind_method(D_METHOD("get_max_text_lines"), &ItemList::get_max_text_lines); - ClassDB::bind_method(D_METHOD("set_max_columns","amount"),&ItemList::set_max_columns); - ClassDB::bind_method(D_METHOD("get_max_columns"),&ItemList::get_max_columns); + ClassDB::bind_method(D_METHOD("set_max_columns", "amount"), &ItemList::set_max_columns); + ClassDB::bind_method(D_METHOD("get_max_columns"), &ItemList::get_max_columns); - ClassDB::bind_method(D_METHOD("set_select_mode","mode"),&ItemList::set_select_mode); - ClassDB::bind_method(D_METHOD("get_select_mode"),&ItemList::get_select_mode); + ClassDB::bind_method(D_METHOD("set_select_mode", "mode"), &ItemList::set_select_mode); + ClassDB::bind_method(D_METHOD("get_select_mode"), &ItemList::get_select_mode); - ClassDB::bind_method(D_METHOD("set_icon_mode","mode"),&ItemList::set_icon_mode); - ClassDB::bind_method(D_METHOD("get_icon_mode"),&ItemList::get_icon_mode); + ClassDB::bind_method(D_METHOD("set_icon_mode", "mode"), &ItemList::set_icon_mode); + ClassDB::bind_method(D_METHOD("get_icon_mode"), &ItemList::get_icon_mode); + ClassDB::bind_method(D_METHOD("set_fixed_icon_size", "size"), &ItemList::set_fixed_icon_size); + ClassDB::bind_method(D_METHOD("get_fixed_icon_size"), &ItemList::get_fixed_icon_size); - ClassDB::bind_method(D_METHOD("set_fixed_icon_size","size"),&ItemList::set_fixed_icon_size); - ClassDB::bind_method(D_METHOD("get_fixed_icon_size"),&ItemList::get_fixed_icon_size); + ClassDB::bind_method(D_METHOD("set_icon_scale", "scale"), &ItemList::set_icon_scale); + ClassDB::bind_method(D_METHOD("get_icon_scale"), &ItemList::get_icon_scale); - ClassDB::bind_method(D_METHOD("set_icon_scale","scale"),&ItemList::set_icon_scale); - ClassDB::bind_method(D_METHOD("get_icon_scale"),&ItemList::get_icon_scale); + ClassDB::bind_method(D_METHOD("set_allow_rmb_select", "allow"), &ItemList::set_allow_rmb_select); + ClassDB::bind_method(D_METHOD("get_allow_rmb_select"), &ItemList::get_allow_rmb_select); - ClassDB::bind_method(D_METHOD("set_allow_rmb_select","allow"),&ItemList::set_allow_rmb_select); - ClassDB::bind_method(D_METHOD("get_allow_rmb_select"),&ItemList::get_allow_rmb_select); + ClassDB::bind_method(D_METHOD("get_item_at_pos", "pos", "exact"), &ItemList::get_item_at_pos, DEFVAL(false)); - ClassDB::bind_method(D_METHOD("get_item_at_pos","pos","exact"),&ItemList::get_item_at_pos,DEFVAL(false)); + ClassDB::bind_method(D_METHOD("ensure_current_is_visible"), &ItemList::ensure_current_is_visible); - ClassDB::bind_method(D_METHOD("ensure_current_is_visible"),&ItemList::ensure_current_is_visible); + ClassDB::bind_method(D_METHOD("get_v_scroll"), &ItemList::get_v_scroll); - ClassDB::bind_method(D_METHOD("get_v_scroll"),&ItemList::get_v_scroll); + ClassDB::bind_method(D_METHOD("_scroll_changed"), &ItemList::_scroll_changed); + ClassDB::bind_method(D_METHOD("_gui_input"), &ItemList::_gui_input); - ClassDB::bind_method(D_METHOD("_scroll_changed"),&ItemList::_scroll_changed); - ClassDB::bind_method(D_METHOD("_gui_input"),&ItemList::_gui_input); + BIND_CONSTANT(ICON_MODE_TOP); + BIND_CONSTANT(ICON_MODE_LEFT); + BIND_CONSTANT(SELECT_SINGLE); + BIND_CONSTANT(SELECT_MULTI); - BIND_CONSTANT( ICON_MODE_TOP ); - BIND_CONSTANT( ICON_MODE_LEFT ); - BIND_CONSTANT( SELECT_SINGLE ); - BIND_CONSTANT( SELECT_MULTI ); + ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "index"))); + ADD_SIGNAL(MethodInfo("item_rmb_selected", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::VECTOR2, "atpos"))); + ADD_SIGNAL(MethodInfo("multi_selected", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::BOOL, "selected"))); + ADD_SIGNAL(MethodInfo("item_activated", PropertyInfo(Variant::INT, "index"))); - ADD_SIGNAL( MethodInfo("item_selected",PropertyInfo(Variant::INT,"index"))); - ADD_SIGNAL( MethodInfo("item_rmb_selected",PropertyInfo(Variant::INT,"index"),PropertyInfo(Variant::VECTOR2,"atpos"))); - ADD_SIGNAL( MethodInfo("multi_selected",PropertyInfo(Variant::INT,"index"),PropertyInfo(Variant::BOOL,"selected"))); - ADD_SIGNAL( MethodInfo("item_activated",PropertyInfo(Variant::INT,"index"))); - - GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec",2000); + GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec", 2000); } ItemList::ItemList() { - current=-1; + current = -1; - select_mode=SELECT_SINGLE; - icon_mode=ICON_MODE_LEFT; + select_mode = SELECT_SINGLE; + icon_mode = ICON_MODE_LEFT; - fixed_column_width=0; + fixed_column_width = 0; same_column_width = false; - max_text_lines=1; - max_columns=1; + max_text_lines = 1; + max_columns = 1; - scroll_bar = memnew( VScrollBar ); + scroll_bar = memnew(VScrollBar); add_child(scroll_bar); - shape_changed=true; - scroll_bar->connect("value_changed",this,"_scroll_changed"); + shape_changed = true; + scroll_bar->connect("value_changed", this, "_scroll_changed"); set_focus_mode(FOCUS_ALL); - current_columns=1; - search_time_msec=0; - ensure_selected_visible=false; - defer_select_single=-1; - allow_rmb_select=false; + current_columns = 1; + search_time_msec = 0; + ensure_selected_visible = false; + defer_select_single = -1; + allow_rmb_select = false; icon_scale = 1.0f; set_clip_contents(true); } ItemList::~ItemList() { - } diff --git a/scene/gui/item_list.h b/scene/gui/item_list.h index 35ffb1be9c..5de58cfde8 100644 --- a/scene/gui/item_list.h +++ b/scene/gui/item_list.h @@ -34,9 +34,9 @@ class ItemList : public Control { - GDCLASS( ItemList, Control ); -public: + GDCLASS(ItemList, Control); +public: enum IconMode { ICON_MODE_TOP, ICON_MODE_LEFT @@ -46,6 +46,7 @@ public: SELECT_SINGLE, SELECT_MULTI }; + private: struct Item { @@ -66,7 +67,7 @@ private: Size2 get_icon_size() const; - bool operator<(const Item& p_another) const { return text<p_another.text; } + bool operator<(const Item &p_another) const { return text < p_another.text; } }; int current; @@ -102,50 +103,47 @@ private: real_t icon_scale; void _scroll_changed(double); - void _gui_input(const InputEvent& p_event); - + void _gui_input(const InputEvent &p_event); protected: - void _notification(int p_what); static void _bind_methods(); -public: - - void add_item(const String& p_item,const Ref<Texture>& p_texture=Ref<Texture>(),bool p_selectable=true); - void add_icon_item(const Ref<Texture>& p_item,bool p_selectable=true); +public: + void add_item(const String &p_item, const Ref<Texture> &p_texture = Ref<Texture>(), bool p_selectable = true); + void add_icon_item(const Ref<Texture> &p_item, bool p_selectable = true); - void set_item_text(int p_idx,const String& p_text); + void set_item_text(int p_idx, const String &p_text); String get_item_text(int p_idx) const; - void set_item_icon(int p_idx,const Ref<Texture>& p_icon); + void set_item_icon(int p_idx, const Ref<Texture> &p_icon); Ref<Texture> get_item_icon(int p_idx) const; - void set_item_icon_region(int p_idx,const Rect2& p_region); + void set_item_icon_region(int p_idx, const Rect2 &p_region); Rect2 get_item_icon_region(int p_idx) const; - void set_item_selectable(int p_idx,bool p_selectable); + void set_item_selectable(int p_idx, bool p_selectable); bool is_item_selectable(int p_idx) const; - void set_item_disabled(int p_idx,bool p_disabled); + void set_item_disabled(int p_idx, bool p_disabled); bool is_item_disabled(int p_idx) const; - void set_item_metadata(int p_idx,const Variant& p_metadata); + void set_item_metadata(int p_idx, const Variant &p_metadata); Variant get_item_metadata(int p_idx) const; - void set_item_tag_icon(int p_idx,const Ref<Texture>& p_tag_icon); + void set_item_tag_icon(int p_idx, const Ref<Texture> &p_tag_icon); Ref<Texture> get_item_tag_icon(int p_idx) const; void set_item_tooltip_enabled(int p_idx, const bool p_enabled); bool is_item_tooltip_enabled(int p_idx) const; - void set_item_tooltip(int p_idx,const String& p_tooltip); + void set_item_tooltip(int p_idx, const String &p_tooltip); String get_item_tooltip(int p_idx) const; - void set_item_custom_bg_color(int p_idx,const Color& p_custom_bg_color); + void set_item_custom_bg_color(int p_idx, const Color &p_custom_bg_color); Color get_item_custom_bg_color(int p_idx) const; - void select(int p_idx,bool p_single=true); + void select(int p_idx, bool p_single = true); void unselect(int p_idx); bool is_selected(int p_idx) const; Vector<int> get_selected_items(); @@ -153,7 +151,7 @@ public: 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_item, int p_to_pos); int get_item_count() const; void remove_item(int p_idx); @@ -178,7 +176,7 @@ public: void set_icon_mode(IconMode p_mode); IconMode get_icon_mode() const; - void set_fixed_icon_size(const Size2& p_size); + void set_fixed_icon_size(const Size2 &p_size); Size2 get_fixed_icon_size() const; void set_allow_rmb_select(bool p_allow); @@ -187,11 +185,11 @@ public: void ensure_current_is_visible(); void sort_items_by_text(); - int find_metadata(const Variant& p_metadata) const; + 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,bool p_exact=false) const; - bool is_pos_at_end_of_items(const Point2& p_pos) const; + virtual String get_tooltip(const Point2 &p_pos) const; + int get_item_at_pos(const Point2 &p_pos, bool p_exact = false) const; + bool is_pos_at_end_of_items(const Point2 &p_pos) const; void set_icon_scale(real_t p_scale); real_t get_icon_scale() const; @@ -205,5 +203,4 @@ public: VARIANT_ENUM_CAST(ItemList::SelectMode); VARIANT_ENUM_CAST(ItemList::IconMode); - #endif // ITEMLIST_H diff --git a/scene/gui/label.cpp b/scene/gui/label.cpp index 915eaa2f45..0dec69d9b3 100644 --- a/scene/gui/label.cpp +++ b/scene/gui/label.cpp @@ -27,29 +27,26 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "label.h" -#include "print_string.h" #include "global_config.h" +#include "print_string.h" #include "translation.h" - void Label::set_autowrap(bool p_autowrap) { - autowrap=p_autowrap; - word_cache_dirty=true; + autowrap = p_autowrap; + word_cache_dirty = true; minimum_size_changed(); update(); - } bool Label::has_autowrap() const { return autowrap; } - void Label::set_uppercase(bool p_uppercase) { - uppercase=p_uppercase; - word_cache_dirty=true; + uppercase = p_uppercase; + word_cache_dirty = true; minimum_size_changed(); update(); } @@ -58,264 +55,247 @@ bool Label::is_uppercase() const { return uppercase; } - int Label::get_line_height() const { return get_font("font")->get_height(); - } - void Label::_notification(int p_what) { - if (p_what==NOTIFICATION_TRANSLATION_CHANGED) { + if (p_what == NOTIFICATION_TRANSLATION_CHANGED) { - xl_text=XL_MESSAGE(text); + xl_text = XL_MESSAGE(text); minimum_size_changed(); update(); } - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { if (clip || autowrap) { - VisualServer::get_singleton()->canvas_item_set_clip(get_canvas_item(),true); + VisualServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), true); } if (word_cache_dirty) regenerate_word_cache(); - RID ci = get_canvas_item(); Size2 string_size; - Size2 size=get_size(); + Size2 size = get_size(); Ref<Font> font = get_font("font"); Color font_color = get_color("font_color"); Color font_color_shadow = get_color("font_color_shadow"); bool use_outline = get_constant("shadow_as_outline"); - Point2 shadow_ofs(get_constant("shadow_offset_x"),get_constant("shadow_offset_y")); + Point2 shadow_ofs(get_constant("shadow_offset_x"), get_constant("shadow_offset_y")); int line_spacing = get_constant("line_spacing"); - VisualServer::get_singleton()->canvas_item_set_distance_field_mode(get_canvas_item(),font.is_valid() && font->is_distance_field_hint()); + VisualServer::get_singleton()->canvas_item_set_distance_field_mode(get_canvas_item(), font.is_valid() && font->is_distance_field_hint()); - int font_h = font->get_height()+line_spacing; + int font_h = font->get_height() + line_spacing; - int lines_visible = (size.y+line_spacing)/font_h; + int lines_visible = (size.y + line_spacing) / font_h; - int space_w=font->get_char_size(' ').width; - int chars_total=0; + int space_w = font->get_char_size(' ').width; + int chars_total = 0; - int vbegin=0,vsep=0; + int vbegin = 0, vsep = 0; if (lines_visible > line_count) { lines_visible = line_count; - } if (max_lines_visible >= 0 && lines_visible > max_lines_visible) { lines_visible = max_lines_visible; - } if (lines_visible > 0) { - switch(valign) { + switch (valign) { case VALIGN_TOP: { //nothing } break; case VALIGN_CENTER: { - vbegin=(size.y - (lines_visible * font_h - line_spacing)) / 2; - vsep=0; + vbegin = (size.y - (lines_visible * font_h - line_spacing)) / 2; + vsep = 0; } break; case VALIGN_BOTTOM: { - vbegin=size.y - (lines_visible * font_h - line_spacing); - vsep=0; + vbegin = size.y - (lines_visible * font_h - line_spacing); + vsep = 0; } break; case VALIGN_FILL: { - vbegin=0; - if (lines_visible>1) { - vsep=(size.y - (lines_visible * font_h - line_spacing)) / (lines_visible - 1); + vbegin = 0; + if (lines_visible > 1) { + vsep = (size.y - (lines_visible * font_h - line_spacing)) / (lines_visible - 1); } else { - vsep=0; + vsep = 0; } } break; } } - WordCache *wc = word_cache; if (!wc) return; - int line=0; - int line_to=lines_skipped + (lines_visible>0?lines_visible:1); - while(wc) { - /* handle lines not meant to be drawn quickly */ - if (line>=line_to) + int line = 0; + int line_to = lines_skipped + (lines_visible > 0 ? lines_visible : 1); + while (wc) { + /* handle lines not meant to be drawn quickly */ + if (line >= line_to) break; - if (line<lines_skipped) { + if (line < lines_skipped) { - while (wc && wc->char_pos>=0) - wc=wc->next; + while (wc && wc->char_pos >= 0) + wc = wc->next; if (wc) - wc=wc->next; + wc = wc->next; line++; continue; } - /* handle lines normally */ + /* handle lines normally */ - if (wc->char_pos<0) { - //empty line - wc=wc->next; + if (wc->char_pos < 0) { + //empty line + wc = wc->next; line++; continue; } - WordCache *from=wc; - WordCache *to=wc; + WordCache *from = wc; + WordCache *to = wc; - int taken=0; - int spaces=0; - while(to && to->char_pos>=0) { + int taken = 0; + int spaces = 0; + while (to && to->char_pos >= 0) { - taken+=to->pixel_width; - if (to!=from && to->space_count) { - spaces+=to->space_count; + taken += to->pixel_width; + if (to != from && to->space_count) { + spaces += to->space_count; } - to=to->next; + to = to->next; } - bool can_fill = to && to->char_pos==WordCache::CHAR_WRAPLINE; + bool can_fill = to && to->char_pos == WordCache::CHAR_WRAPLINE; - float x_ofs=0; + float x_ofs = 0; switch (align) { case ALIGN_FILL: case ALIGN_LEFT: { - x_ofs=0; + x_ofs = 0; } break; case ALIGN_CENTER: { - x_ofs=int(size.width-(taken+spaces*space_w))/2; + x_ofs = int(size.width - (taken + spaces * space_w)) / 2; } break; case ALIGN_RIGHT: { - - x_ofs=int(size.width-(taken+spaces*space_w)); + x_ofs = int(size.width - (taken + spaces * space_w)); } break; - } + } - int y_ofs=(line-lines_skipped)*font_h + font->get_ascent(); - y_ofs+=vbegin + line*vsep; + int y_ofs = (line - lines_skipped) * font_h + font->get_ascent(); + y_ofs += vbegin + line * vsep; - while(from!=to) { + while (from != to) { - // draw a word + // draw a word int pos = from->char_pos; - if (from->char_pos<0) { + if (from->char_pos < 0) { ERR_PRINT("BUG"); return; } if (from->space_count) { - /* spacing */ - x_ofs+=space_w*from->space_count; - if (can_fill && align==ALIGN_FILL && spaces) { + /* spacing */ + x_ofs += space_w * from->space_count; + if (can_fill && align == ALIGN_FILL && spaces) { - x_ofs+=int((size.width-(taken+space_w*spaces))/spaces); + x_ofs += int((size.width - (taken + space_w * spaces)) / spaces); } - - } - - - if (font_color_shadow.a>0) { + if (font_color_shadow.a > 0) { int chars_total_shadow = chars_total; //save chars drawn - float x_ofs_shadow=x_ofs; - for (int i=0;i<from->word_len;i++) { + float x_ofs_shadow = x_ofs; + for (int i = 0; i < from->word_len; i++) { - if (visible_chars < 0 || chars_total_shadow<visible_chars) { - CharType c = xl_text[i+pos]; - CharType n = xl_text[i+pos+1]; + if (visible_chars < 0 || chars_total_shadow < visible_chars) { + CharType c = xl_text[i + pos]; + CharType n = xl_text[i + pos + 1]; if (uppercase) { - c=String::char_uppercase(c); - n=String::char_uppercase(c); + c = String::char_uppercase(c); + n = String::char_uppercase(c); } - float move=font->draw_char(ci, Point2( x_ofs_shadow, y_ofs )+shadow_ofs, c, n,font_color_shadow ); + float move = font->draw_char(ci, Point2(x_ofs_shadow, y_ofs) + shadow_ofs, c, n, font_color_shadow); if (use_outline) { - font->draw_char(ci, Point2( x_ofs_shadow, y_ofs )+Vector2(-shadow_ofs.x,shadow_ofs.y), c, n,font_color_shadow ); - font->draw_char(ci, Point2( x_ofs_shadow, y_ofs )+Vector2(shadow_ofs.x,-shadow_ofs.y), c, n,font_color_shadow ); - font->draw_char(ci, Point2( x_ofs_shadow, y_ofs )+Vector2(-shadow_ofs.x,-shadow_ofs.y), c, n,font_color_shadow ); + font->draw_char(ci, Point2(x_ofs_shadow, y_ofs) + Vector2(-shadow_ofs.x, shadow_ofs.y), c, n, font_color_shadow); + font->draw_char(ci, Point2(x_ofs_shadow, y_ofs) + Vector2(shadow_ofs.x, -shadow_ofs.y), c, n, font_color_shadow); + font->draw_char(ci, Point2(x_ofs_shadow, y_ofs) + Vector2(-shadow_ofs.x, -shadow_ofs.y), c, n, font_color_shadow); } - x_ofs_shadow+=move; + x_ofs_shadow += move; chars_total_shadow++; } } - - } - for (int i=0;i<from->word_len;i++) { + for (int i = 0; i < from->word_len; i++) { - if (visible_chars < 0 || chars_total<visible_chars) { - CharType c = xl_text[i+pos]; - CharType n = xl_text[i+pos+1]; + if (visible_chars < 0 || chars_total < visible_chars) { + CharType c = xl_text[i + pos]; + CharType n = xl_text[i + pos + 1]; if (uppercase) { - c=String::char_uppercase(c); - n=String::char_uppercase(c); + c = String::char_uppercase(c); + n = String::char_uppercase(c); } - x_ofs+=font->draw_char(ci,Point2( x_ofs, y_ofs ), c, n, font_color ); + x_ofs += font->draw_char(ci, Point2(x_ofs, y_ofs), c, n, font_color); chars_total++; } - } - from=from->next; + from = from->next; } - wc=to?to->next:0; + wc = to ? to->next : 0; line++; - } } - if (p_what==NOTIFICATION_THEME_CHANGED) { + if (p_what == NOTIFICATION_THEME_CHANGED) { - word_cache_dirty=true; + word_cache_dirty = true; update(); } - if (p_what==NOTIFICATION_RESIZED) { + if (p_what == NOTIFICATION_RESIZED) { - word_cache_dirty=true; + word_cache_dirty = true; } - } Size2 Label::get_minimum_size() const { if (autowrap) - return Size2(1,1); + return Size2(1, 1); else { // don't want to mutable everything - if(word_cache_dirty) - const_cast<Label*>(this)->regenerate_word_cache(); + if (word_cache_dirty) + const_cast<Label *>(this)->regenerate_word_cache(); - Size2 ms=minsize; + Size2 ms = minsize; if (clip) - ms.width=1; + ms.width = 1; return ms; } } @@ -323,33 +303,32 @@ Size2 Label::get_minimum_size() const { int Label::get_longest_line_width() const { Ref<Font> font = get_font("font"); - int max_line_width=0; - int line_width=0; + int max_line_width = 0; + int line_width = 0; - for (int i=0;i<xl_text.size();i++) { + for (int i = 0; i < xl_text.size(); i++) { - CharType current=xl_text[i]; + CharType current = xl_text[i]; if (uppercase) - current=String::char_uppercase(current); + current = String::char_uppercase(current); - if (current<32) { + if (current < 32) { - if (current=='\n') { + if (current == '\n') { - if (line_width>max_line_width) - max_line_width=line_width; - line_width=0; + if (line_width > max_line_width) + max_line_width = line_width; + line_width = 0; } } else { - int char_width=font->get_char_size(current,xl_text[i+1]).width; - line_width+=char_width; + int char_width = font->get_char_size(current, xl_text[i + 1]).width; + line_width += char_width; } - } - if (line_width>max_line_width) - max_line_width=line_width; + if (line_width > max_line_width) + max_line_width = line_width; return max_line_width; } @@ -359,7 +338,7 @@ int Label::get_line_count() const { if (!is_inside_tree()) return 1; if (word_cache_dirty) - const_cast<Label*>(this)->regenerate_word_cache(); + const_cast<Label *>(this)->regenerate_word_cache(); return line_count; } @@ -367,8 +346,8 @@ int Label::get_line_count() const { int Label::get_visible_line_count() const { int line_spacing = get_constant("line_spacing"); - int font_h = get_font("font")->get_height()+line_spacing; - int lines_visible = (get_size().y+line_spacing)/font_h; + int font_h = get_font("font")->get_height() + line_spacing; + int lines_visible = (get_size().y + line_spacing) / font_h; if (lines_visible > line_count) lines_visible = line_count; @@ -383,189 +362,179 @@ void Label::regenerate_word_cache() { while (word_cache) { - WordCache *current=word_cache; - word_cache=current->next; - memdelete( current ); + WordCache *current = word_cache; + word_cache = current->next; + memdelete(current); } - - int width=autowrap?get_size().width:get_longest_line_width(); + int width = autowrap ? get_size().width : get_longest_line_width(); Ref<Font> font = get_font("font"); - int current_word_size=0; - int word_pos=0; - int line_width=0; - int space_count=0; - int space_width=font->get_char_size(' ').width; + int current_word_size = 0; + int word_pos = 0; + int line_width = 0; + int space_count = 0; + int space_width = font->get_char_size(' ').width; int line_spacing = get_constant("line_spacing"); - line_count=1; - total_char_cache=0; + line_count = 1; + total_char_cache = 0; - WordCache *last=NULL; + WordCache *last = NULL; - for (int i=0;i<xl_text.size()+1;i++) { + for (int i = 0; i < xl_text.size() + 1; i++) { - CharType current=i<xl_text.length()?xl_text[i]:' '; //always a space at the end, so the algo works + CharType current = i < xl_text.length() ? xl_text[i] : ' '; //always a space at the end, so the algo works if (uppercase) - current=String::char_uppercase(current); + current = String::char_uppercase(current); // ranges taken from http://www.unicodemap.org/ // if your language is not well supported, consider helping improve // the unicode support in Godot. - bool separatable = (current>=0x2E08 && current<=0xFAFF) || (current>=0xFE30 && current<=0xFE4F); - //current>=33 && (current < 65||current >90) && (current<97||current>122) && (current<48||current>57); - bool insert_newline=false; + bool separatable = (current >= 0x2E08 && current <= 0xFAFF) || (current >= 0xFE30 && current <= 0xFE4F); + //current>=33 && (current < 65||current >90) && (current<97||current>122) && (current<48||current>57); + bool insert_newline = false; int char_width; - if (current<33) { + if (current < 33) { - if (current_word_size>0) { - WordCache *wc = memnew( WordCache ); + if (current_word_size > 0) { + WordCache *wc = memnew(WordCache); if (word_cache) { - last->next=wc; + last->next = wc; } else { - word_cache=wc; + word_cache = wc; } - last=wc; + last = wc; - wc->pixel_width=current_word_size; - wc->char_pos=word_pos; - wc->word_len=i-word_pos; + wc->pixel_width = current_word_size; + wc->char_pos = word_pos; + wc->word_len = i - word_pos; wc->space_count = space_count; - current_word_size=0; - space_count=0; - + current_word_size = 0; + space_count = 0; } - - if (current=='\n') { - insert_newline=true; + if (current == '\n') { + insert_newline = true; } else { total_char_cache++; } - if (i<xl_text.length() && xl_text[i] == ' ') { - total_char_cache--; // do not count spaces - if (line_width > 0 || last==NULL || last->char_pos!=WordCache::CHAR_WRAPLINE) { + if (i < xl_text.length() && xl_text[i] == ' ') { + total_char_cache--; // do not count spaces + if (line_width > 0 || last == NULL || last->char_pos != WordCache::CHAR_WRAPLINE) { space_count++; - line_width+=space_width; - }else { - space_count=0; + line_width += space_width; + } else { + space_count = 0; } } - } else { // latin characters - if (current_word_size==0) { - word_pos=i; + if (current_word_size == 0) { + word_pos = i; } - char_width=font->get_char_size(current,xl_text[i+1]).width; - current_word_size+=char_width; - line_width+=char_width; + char_width = font->get_char_size(current, xl_text[i + 1]).width; + current_word_size += char_width; + line_width += char_width; total_char_cache++; - } if ((autowrap && (line_width >= width) && ((last && last->char_pos >= 0) || separatable)) || insert_newline) { if (separatable) { - if (current_word_size>0) { - WordCache *wc = memnew( WordCache ); + if (current_word_size > 0) { + WordCache *wc = memnew(WordCache); if (word_cache) { - last->next=wc; + last->next = wc; } else { - word_cache=wc; + word_cache = wc; } - last=wc; + last = wc; - wc->pixel_width=current_word_size-char_width; - wc->char_pos=word_pos; - wc->word_len=i-word_pos; + wc->pixel_width = current_word_size - char_width; + wc->char_pos = word_pos; + wc->word_len = i - word_pos; wc->space_count = space_count; - current_word_size=char_width; - space_count=0; - word_pos=i; + current_word_size = char_width; + space_count = 0; + word_pos = i; } } - WordCache *wc = memnew( WordCache ); + WordCache *wc = memnew(WordCache); if (word_cache) { - last->next=wc; + last->next = wc; } else { - word_cache=wc; + word_cache = wc; } - last=wc; + last = wc; - wc->pixel_width=0; - wc->char_pos=insert_newline?WordCache::CHAR_NEWLINE:WordCache::CHAR_WRAPLINE; + wc->pixel_width = 0; + wc->char_pos = insert_newline ? WordCache::CHAR_NEWLINE : WordCache::CHAR_WRAPLINE; - line_width=current_word_size; + line_width = current_word_size; line_count++; - space_count=0; - + space_count = 0; } - } if (!autowrap) { - minsize.width=width; + minsize.width = width; if (max_lines_visible > 0 && line_count > max_lines_visible) { - minsize.height=(font->get_height() * max_lines_visible) + (line_spacing * (max_lines_visible - 1)); + minsize.height = (font->get_height() * max_lines_visible) + (line_spacing * (max_lines_visible - 1)); } else { - minsize.height=(font->get_height() * line_count)+(line_spacing * (line_count - 1)); + minsize.height = (font->get_height() * line_count) + (line_spacing * (line_count - 1)); } } - word_cache_dirty=false; - + word_cache_dirty = false; } void Label::set_align(Align p_align) { - ERR_FAIL_INDEX(p_align,4); - align=p_align; + ERR_FAIL_INDEX(p_align, 4); + align = p_align; update(); } -Label::Align Label::get_align() const{ +Label::Align Label::get_align() const { return align; } void Label::set_valign(VAlign p_align) { - ERR_FAIL_INDEX(p_align,4); - valign=p_align; + ERR_FAIL_INDEX(p_align, 4); + valign = p_align; update(); } -Label::VAlign Label::get_valign() const{ +Label::VAlign Label::get_valign() const { return valign; } -void Label::set_text(const String& p_string) { - +void Label::set_text(const String &p_string) { - if (text==p_string) + if (text == p_string) return; - text=p_string; - xl_text=XL_MESSAGE(p_string); - word_cache_dirty=true; - if (percent_visible<1) - visible_chars=get_total_character_count()*percent_visible; + text = p_string; + xl_text = XL_MESSAGE(p_string); + word_cache_dirty = true; + if (percent_visible < 1) + visible_chars = get_total_character_count() * percent_visible; update(); if (!autowrap) { minimum_size_changed(); } - } void Label::set_clip_text(bool p_clip) { - clip=p_clip; + clip = p_clip; update(); minimum_size_changed(); } @@ -582,9 +551,9 @@ String Label::get_text() const { void Label::set_visible_characters(int p_amount) { - visible_chars=p_amount; + visible_chars = p_amount; if (get_total_character_count() > 0) { - percent_visible=(float)p_amount/(float)total_char_cache; + percent_visible = (float)p_amount / (float)total_char_cache; } update(); } @@ -596,44 +565,42 @@ int Label::get_visible_characters() const { void Label::set_percent_visible(float p_percent) { - if (p_percent<0 || p_percent>=1) { + if (p_percent < 0 || p_percent >= 1) { - visible_chars=-1; - percent_visible=1; + visible_chars = -1; + percent_visible = 1; } else { - visible_chars=get_total_character_count()*p_percent; - percent_visible=p_percent; - + visible_chars = get_total_character_count() * p_percent; + percent_visible = p_percent; } update(); - } -float Label::get_percent_visible() const{ +float Label::get_percent_visible() const { return percent_visible; } void Label::set_lines_skipped(int p_lines) { - lines_skipped=p_lines; + lines_skipped = p_lines; update(); } -int Label::get_lines_skipped() const{ +int Label::get_lines_skipped() const { return lines_skipped; } void Label::set_max_lines_visible(int p_lines) { - max_lines_visible=p_lines; + max_lines_visible = p_lines; update(); } -int Label::get_max_lines_visible() const{ +int Label::get_max_lines_visible() const { return max_lines_visible; } @@ -641,90 +608,86 @@ int Label::get_max_lines_visible() const{ int Label::get_total_character_count() const { if (word_cache_dirty) - const_cast<Label*>(this)->regenerate_word_cache(); + const_cast<Label *>(this)->regenerate_word_cache(); return total_char_cache; } void Label::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_align","align"),&Label::set_align); - ClassDB::bind_method(D_METHOD("get_align"),&Label::get_align); - ClassDB::bind_method(D_METHOD("set_valign","valign"),&Label::set_valign); - ClassDB::bind_method(D_METHOD("get_valign"),&Label::get_valign); - ClassDB::bind_method(D_METHOD("set_text","text"),&Label::set_text); - ClassDB::bind_method(D_METHOD("get_text"),&Label::get_text); - ClassDB::bind_method(D_METHOD("set_autowrap","enable"),&Label::set_autowrap); - ClassDB::bind_method(D_METHOD("has_autowrap"),&Label::has_autowrap); - ClassDB::bind_method(D_METHOD("set_clip_text","enable"),&Label::set_clip_text); - ClassDB::bind_method(D_METHOD("is_clipping_text"),&Label::is_clipping_text); - ClassDB::bind_method(D_METHOD("set_uppercase","enable"),&Label::set_uppercase); - ClassDB::bind_method(D_METHOD("is_uppercase"),&Label::is_uppercase); - ClassDB::bind_method(D_METHOD("get_line_height"),&Label::get_line_height); - ClassDB::bind_method(D_METHOD("get_line_count"),&Label::get_line_count); - ClassDB::bind_method(D_METHOD("get_visible_line_count"),&Label::get_visible_line_count); - ClassDB::bind_method(D_METHOD("get_total_character_count"),&Label::get_total_character_count); - ClassDB::bind_method(D_METHOD("set_visible_characters","amount"),&Label::set_visible_characters); - ClassDB::bind_method(D_METHOD("get_visible_characters"),&Label::get_visible_characters); - ClassDB::bind_method(D_METHOD("set_percent_visible","percent_visible"),&Label::set_percent_visible); - ClassDB::bind_method(D_METHOD("get_percent_visible"),&Label::get_percent_visible); - ClassDB::bind_method(D_METHOD("set_lines_skipped","lines_skipped"),&Label::set_lines_skipped); - ClassDB::bind_method(D_METHOD("get_lines_skipped"),&Label::get_lines_skipped); - ClassDB::bind_method(D_METHOD("set_max_lines_visible","lines_visible"),&Label::set_max_lines_visible); - ClassDB::bind_method(D_METHOD("get_max_lines_visible"),&Label::get_max_lines_visible); - - BIND_CONSTANT( ALIGN_LEFT ); - BIND_CONSTANT( ALIGN_CENTER ); - BIND_CONSTANT( ALIGN_RIGHT ); - BIND_CONSTANT( ALIGN_FILL ); - - BIND_CONSTANT( VALIGN_TOP ); - BIND_CONSTANT( VALIGN_CENTER ); - BIND_CONSTANT( VALIGN_BOTTOM ); - BIND_CONSTANT( VALIGN_FILL ); - - ADD_PROPERTYNZ( PropertyInfo( Variant::STRING, "text",PROPERTY_HINT_MULTILINE_TEXT,"",PROPERTY_USAGE_DEFAULT_INTL), "set_text","get_text") ; - ADD_PROPERTYNZ( PropertyInfo( Variant::INT, "align", PROPERTY_HINT_ENUM,"Left,Center,Right,Fill" ),"set_align","get_align") ; - ADD_PROPERTYNZ( PropertyInfo( Variant::INT, "valign", PROPERTY_HINT_ENUM,"Top,Center,Bottom,Fill" ),"set_valign","get_valign") ; - ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "autowrap"),"set_autowrap","has_autowrap") ; - ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "clip_text"),"set_clip_text","is_clipping_text") ; - ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "uppercase"),"set_uppercase","is_uppercase") ; - ADD_PROPERTY( PropertyInfo( Variant::REAL, "percent_visible", PROPERTY_HINT_RANGE,"0,1,0.001"),"set_percent_visible","get_percent_visible") ; - ADD_PROPERTY( PropertyInfo( Variant::INT, "lines_skipped", PROPERTY_HINT_RANGE,"0,999,1"),"set_lines_skipped","get_lines_skipped") ; - ADD_PROPERTY( PropertyInfo( Variant::INT, "max_lines_visible", PROPERTY_HINT_RANGE,"-1,999,1"),"set_max_lines_visible","get_max_lines_visible") ; - + ClassDB::bind_method(D_METHOD("set_align", "align"), &Label::set_align); + ClassDB::bind_method(D_METHOD("get_align"), &Label::get_align); + ClassDB::bind_method(D_METHOD("set_valign", "valign"), &Label::set_valign); + ClassDB::bind_method(D_METHOD("get_valign"), &Label::get_valign); + ClassDB::bind_method(D_METHOD("set_text", "text"), &Label::set_text); + ClassDB::bind_method(D_METHOD("get_text"), &Label::get_text); + ClassDB::bind_method(D_METHOD("set_autowrap", "enable"), &Label::set_autowrap); + ClassDB::bind_method(D_METHOD("has_autowrap"), &Label::has_autowrap); + ClassDB::bind_method(D_METHOD("set_clip_text", "enable"), &Label::set_clip_text); + ClassDB::bind_method(D_METHOD("is_clipping_text"), &Label::is_clipping_text); + ClassDB::bind_method(D_METHOD("set_uppercase", "enable"), &Label::set_uppercase); + ClassDB::bind_method(D_METHOD("is_uppercase"), &Label::is_uppercase); + ClassDB::bind_method(D_METHOD("get_line_height"), &Label::get_line_height); + ClassDB::bind_method(D_METHOD("get_line_count"), &Label::get_line_count); + ClassDB::bind_method(D_METHOD("get_visible_line_count"), &Label::get_visible_line_count); + ClassDB::bind_method(D_METHOD("get_total_character_count"), &Label::get_total_character_count); + ClassDB::bind_method(D_METHOD("set_visible_characters", "amount"), &Label::set_visible_characters); + ClassDB::bind_method(D_METHOD("get_visible_characters"), &Label::get_visible_characters); + ClassDB::bind_method(D_METHOD("set_percent_visible", "percent_visible"), &Label::set_percent_visible); + ClassDB::bind_method(D_METHOD("get_percent_visible"), &Label::get_percent_visible); + ClassDB::bind_method(D_METHOD("set_lines_skipped", "lines_skipped"), &Label::set_lines_skipped); + ClassDB::bind_method(D_METHOD("get_lines_skipped"), &Label::get_lines_skipped); + ClassDB::bind_method(D_METHOD("set_max_lines_visible", "lines_visible"), &Label::set_max_lines_visible); + ClassDB::bind_method(D_METHOD("get_max_lines_visible"), &Label::get_max_lines_visible); + + BIND_CONSTANT(ALIGN_LEFT); + BIND_CONSTANT(ALIGN_CENTER); + BIND_CONSTANT(ALIGN_RIGHT); + BIND_CONSTANT(ALIGN_FILL); + + BIND_CONSTANT(VALIGN_TOP); + BIND_CONSTANT(VALIGN_CENTER); + BIND_CONSTANT(VALIGN_BOTTOM); + BIND_CONSTANT(VALIGN_FILL); + + ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text"); + ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_align", "get_align"); + ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "valign", PROPERTY_HINT_ENUM, "Top,Center,Bottom,Fill"), "set_valign", "get_valign"); + ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "autowrap"), "set_autowrap", "has_autowrap"); + ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "is_clipping_text"); + ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "uppercase"), "set_uppercase", "is_uppercase"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "percent_visible", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_percent_visible", "get_percent_visible"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "lines_skipped", PROPERTY_HINT_RANGE, "0,999,1"), "set_lines_skipped", "get_lines_skipped"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "max_lines_visible", PROPERTY_HINT_RANGE, "-1,999,1"), "set_max_lines_visible", "get_max_lines_visible"); } Label::Label(const String &p_text) { - align=ALIGN_LEFT; - valign=VALIGN_TOP; - xl_text=""; - word_cache=NULL; - word_cache_dirty=true; - autowrap=false; - line_count=0; + align = ALIGN_LEFT; + valign = VALIGN_TOP; + xl_text = ""; + word_cache = NULL; + word_cache_dirty = true; + autowrap = false; + line_count = 0; set_v_size_flags(0); - clip=false; + clip = false; set_mouse_filter(MOUSE_FILTER_IGNORE); - total_char_cache=0; - visible_chars=-1; - percent_visible=1; - lines_skipped=0; - max_lines_visible=-1; + total_char_cache = 0; + visible_chars = -1; + percent_visible = 1; + lines_skipped = 0; + max_lines_visible = -1; set_text(p_text); - uppercase=false; + uppercase = false; } - Label::~Label() { while (word_cache) { - WordCache *current=word_cache; - word_cache=current->next; - memdelete( current ); + WordCache *current = word_cache; + word_cache = current->next; + memdelete(current); } } - - diff --git a/scene/gui/label.h b/scene/gui/label.h index 80e4c970f6..769e4b2256 100644 --- a/scene/gui/label.h +++ b/scene/gui/label.h @@ -35,9 +35,9 @@ */ class Label : public Control { - GDCLASS( Label, Control ); -public: + GDCLASS(Label, Control); +public: enum Align { ALIGN_LEFT, @@ -70,15 +70,21 @@ private: struct WordCache { enum { - CHAR_NEWLINE=-1, - CHAR_WRAPLINE=-2 + CHAR_NEWLINE = -1, + CHAR_WRAPLINE = -2 }; int char_pos; // if -1, then newline int word_len; int pixel_width; int space_count; WordCache *next; - WordCache() { char_pos=0; word_len=0; pixel_width=0; next=0; space_count=0;} + WordCache() { + char_pos = 0; + word_len = 0; + pixel_width = 0; + next = 0; + space_count = 0; + } }; bool word_cache_dirty; @@ -91,13 +97,13 @@ private: int visible_chars; int lines_skipped; int max_lines_visible; + protected: void _notification(int p_what); static void _bind_methods(); // bind helpers public: - virtual Size2 get_minimum_size() const; void set_align(Align p_align); @@ -106,7 +112,7 @@ public: void set_valign(VAlign p_align); VAlign get_valign() const; - void set_text(const String& p_string); + void set_text(const String &p_string); String get_text() const; void set_autowrap(bool p_autowrap); @@ -135,13 +141,11 @@ public: int get_line_count() const; int get_visible_line_count() const; - Label(const String& p_text=String()); + Label(const String &p_text = String()); ~Label(); - }; - -VARIANT_ENUM_CAST( Label::Align ); -VARIANT_ENUM_CAST( Label::VAlign ); +VARIANT_ENUM_CAST(Label::Align); +VARIANT_ENUM_CAST(Label::VAlign); #endif diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index 7f61cf80ec..20725194cf 100644 --- a/scene/gui/line_edit.cpp +++ b/scene/gui/line_edit.cpp @@ -27,10 +27,10 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "line_edit.h" +#include "label.h" #include "os/keyboard.h" #include "os/os.h" #include "print_string.h" -#include "label.h" #include "translation.h" #ifdef TOOLS_ENABLED #include "editor/editor_settings.h" @@ -38,27 +38,26 @@ static bool _is_text_char(CharType c) { - return (c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c=='_'; + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'; } void LineEdit::_gui_input(InputEvent p_event) { - - switch(p_event.type) { + switch (p_event.type) { case InputEvent::MOUSE_BUTTON: { const InputEventMouseButton &b = p_event.mouse_button; - if (b.pressed && b.button_index==BUTTON_RIGHT) { + if (b.pressed && b.button_index == BUTTON_RIGHT) { menu->set_pos(get_global_transform().xform(get_local_mouse_pos())); - menu->set_size(Vector2(1,1)); + menu->set_size(Vector2(1, 1)); menu->popup(); grab_focus(); return; } - if (b.button_index!=BUTTON_LEFT) + if (b.button_index != BUTTON_LEFT) break; _reset_caret_blink_timer(); @@ -71,28 +70,28 @@ void LineEdit::_gui_input(InputEvent p_event) { if (b.mod.shift) { selection_fill_at_cursor(); - selection.creating=true; + selection.creating = true; } else { if (b.doubleclick) { - selection.enabled=true; - selection.begin=0; - selection.end=text.length(); - selection.doubleclick=true; + selection.enabled = true; + selection.begin = 0; + selection.end = text.length(); + selection.doubleclick = true; } - selection.drag_attempt=false; + selection.drag_attempt = false; - if ((cursor_pos<selection.begin) || (cursor_pos>selection.end) || !selection.enabled) { + if ((cursor_pos < selection.begin) || (cursor_pos > selection.end) || !selection.enabled) { selection_clear(); - selection.cursor_start=cursor_pos; - selection.creating=true; + selection.cursor_start = cursor_pos; + selection.creating = true; } else if (selection.enabled) { - selection.drag_attempt=true; + selection.drag_attempt = true; } } @@ -100,23 +99,23 @@ void LineEdit::_gui_input(InputEvent p_event) { } else { - if ( (!selection.creating) && (!selection.doubleclick)) { + if ((!selection.creating) && (!selection.doubleclick)) { selection_clear(); } - selection.creating=false; - selection.doubleclick=false; + selection.creating = false; + selection.doubleclick = false; if (OS::get_singleton()->has_virtual_keyboard()) - OS::get_singleton()->show_virtual_keyboard(text,get_global_rect()); + OS::get_singleton()->show_virtual_keyboard(text, get_global_rect()); } update(); } break; case InputEvent::MOUSE_MOTION: { - const InputEventMouseMotion& m=p_event.mouse_motion; + const InputEventMouseMotion &m = p_event.mouse_motion; - if (m.button_mask&BUTTON_LEFT) { + if (m.button_mask & BUTTON_LEFT) { if (selection.creating) { set_cursor_at_pixel_pos(m.x); @@ -127,22 +126,21 @@ void LineEdit::_gui_input(InputEvent p_event) { } break; case InputEvent::KEY: { - const InputEventKey &k =p_event.key; + const InputEventKey &k = p_event.key; if (!k.pressed) return; - unsigned int code = k.scancode; - + unsigned int code = k.scancode; if (k.mod.command) { - bool handled=true; + bool handled = true; switch (code) { case (KEY_X): { // CUT - if(editable) { + if (editable) { cut_text(); } @@ -156,7 +154,7 @@ void LineEdit::_gui_input(InputEvent p_event) { case (KEY_V): { // PASTE - if(editable) { + if (editable) { paste_text(); } @@ -165,22 +163,20 @@ void LineEdit::_gui_input(InputEvent p_event) { case (KEY_Z): { // Simple One level undo - if(editable) { + if (editable) { undo(); - } - } break; case (KEY_U): { // Delete from start to cursor - if(editable) { + if (editable) { selection_clear(); undo_text = text; - text = text.substr(cursor_pos,text.length()-cursor_pos); + text = text.substr(cursor_pos, text.length() - cursor_pos); Ref<Font> font = get_font("font"); @@ -192,15 +188,13 @@ void LineEdit::_gui_input(InputEvent p_event) { set_cursor_pos(0); _text_changed(); - } - } break; case (KEY_Y): { // PASTE (Yank for unix users) - if(editable) { + if (editable) { paste_text(); } @@ -208,11 +202,11 @@ void LineEdit::_gui_input(InputEvent p_event) { } break; case (KEY_K): { // Delete from cursor_pos to end - if(editable) { + if (editable) { selection_clear(); undo_text = text; - text = text.substr(0,cursor_pos); + text = text.substr(0, cursor_pos); _text_changed(); } @@ -220,7 +214,7 @@ void LineEdit::_gui_input(InputEvent p_event) { case (KEY_A): { //Select All select(); } break; - default: { handled=false;} + default: { handled = false; } } if (handled) { @@ -232,13 +226,13 @@ void LineEdit::_gui_input(InputEvent p_event) { _reset_caret_blink_timer(); if (!k.mod.meta) { - bool handled=true; + bool handled = true; switch (code) { case KEY_ENTER: case KEY_RETURN: { - emit_signal( "text_entered",text ); + emit_signal("text_entered", text); if (OS::get_singleton()->has_virtual_keyboard()) OS::get_singleton()->hide_virtual_keyboard(); @@ -251,7 +245,7 @@ void LineEdit::_gui_input(InputEvent p_event) { break; if (selection.enabled) { - undo_text=text; + undo_text = text; selection_delete(); break; } @@ -260,20 +254,20 @@ void LineEdit::_gui_input(InputEvent p_event) { if (k.mod.alt) { #else if (k.mod.alt) { - handled=false; + handled = false; break; } else if (k.mod.command) { #endif - int cc=cursor_pos; - bool prev_char=false; + int cc = cursor_pos; + bool prev_char = false; - while (cc>0) { - bool ischar=_is_text_char(text[cc-1]); + while (cc > 0) { + bool ischar = _is_text_char(text[cc - 1]); if (prev_char && !ischar) break; - prev_char=ischar; + prev_char = ischar; cc--; } @@ -282,7 +276,7 @@ void LineEdit::_gui_input(InputEvent p_event) { set_cursor_pos(cc); } else { - undo_text=text; + undo_text = text; delete_char(); } @@ -299,7 +293,7 @@ void LineEdit::_gui_input(InputEvent p_event) { #ifndef APPLE_STYLE_KEYS if (!k.mod.alt) #endif - shift_selection_check_pre(k.mod.shift); + shift_selection_check_pre(k.mod.shift); #ifdef APPLE_STYLE_KEYS if (k.mod.command) { @@ -308,27 +302,27 @@ void LineEdit::_gui_input(InputEvent p_event) { #else if (k.mod.alt) { - handled=false; + handled = false; break; } else if (k.mod.command) { #endif - bool prev_char=false; - int cc=cursor_pos; + bool prev_char = false; + int cc = cursor_pos; - while (cc>0) { - bool ischar=_is_text_char(text[cc-1]); + while (cc > 0) { + bool ischar = _is_text_char(text[cc - 1]); if (prev_char && !ischar) break; - prev_char=ischar; + prev_char = ischar; cc--; } set_cursor_pos(cc); } else { - set_cursor_pos(get_cursor_pos()-1); + set_cursor_pos(get_cursor_pos() - 1); } shift_selection_check_post(k.mod.shift); @@ -351,27 +345,27 @@ void LineEdit::_gui_input(InputEvent p_event) { } else if (k.mod.alt) { #else if (k.mod.alt) { - handled=false; + handled = false; break; } else if (k.mod.command) { #endif - bool prev_char=false; - int cc=cursor_pos; + bool prev_char = false; + int cc = cursor_pos; - while (cc<text.length()) { - bool ischar=_is_text_char(text[cc]); + while (cc < text.length()) { + bool ischar = _is_text_char(text[cc]); if (prev_char && !ischar) break; - prev_char=ischar; + prev_char = ischar; cc++; } set_cursor_pos(cc); } else { - set_cursor_pos(get_cursor_pos()+1); + set_cursor_pos(get_cursor_pos() + 1); } shift_selection_check_post(k.mod.shift); @@ -388,43 +382,43 @@ void LineEdit::_gui_input(InputEvent p_event) { } if (selection.enabled) { - undo_text=text; + undo_text = text; selection_delete(); break; } int text_len = text.length(); - if (cursor_pos==text_len) + if (cursor_pos == text_len) break; // nothing to do #ifdef APPLE_STYLE_KEYS if (k.mod.alt) { #else if (k.mod.alt) { - handled=false; + handled = false; break; } else if (k.mod.command) { #endif - int cc=cursor_pos; + int cc = cursor_pos; - bool prev_char=false; + bool prev_char = false; - while (cc<text.length()) { + while (cc < text.length()) { - bool ischar=_is_text_char(text[cc]); + bool ischar = _is_text_char(text[cc]); if (prev_char && !ischar) break; - prev_char=ischar; + prev_char = ischar; cc++; } - delete_text(cursor_pos,cc); + delete_text(cursor_pos, cc); } else { - undo_text=text; - set_cursor_pos(cursor_pos+1); + undo_text = text; + set_cursor_pos(cursor_pos + 1); delete_char(); } @@ -456,21 +450,20 @@ void LineEdit::_gui_input(InputEvent p_event) { shift_selection_check_post(k.mod.shift); } break; - default: { - handled=false; + handled = false; } break; } if (handled) { accept_event(); } else if (!k.mod.alt && !k.mod.command) { - if (k.unicode>=32 && k.scancode!=KEY_DELETE) { + if (k.unicode >= 32 && k.scancode != KEY_DELETE) { if (editable) { selection_delete(); - CharType ucodestr[2]={(CharType)k.unicode,0}; + CharType ucodestr[2] = { (CharType)k.unicode, 0 }; append_at_cursor(ucodestr); _text_changed(); accept_event(); @@ -482,14 +475,11 @@ void LineEdit::_gui_input(InputEvent p_event) { } update(); - } - return; } break; - } } @@ -500,31 +490,30 @@ void LineEdit::set_align(Align p_align) { update(); } -LineEdit::Align LineEdit::get_align() const{ +LineEdit::Align LineEdit::get_align() const { return align; } -Variant LineEdit::get_drag_data(const Point2& p_point) { +Variant LineEdit::get_drag_data(const Point2 &p_point) { if (selection.drag_attempt && selection.enabled) { String t = text.substr(selection.begin, selection.end - selection.begin); - Label *l = memnew( Label ); + Label *l = memnew(Label); l->set_text(t); set_drag_preview(l); - return t; + return t; } return Variant(); - } -bool LineEdit::can_drop_data(const Point2& p_point,const Variant& p_data) const{ +bool LineEdit::can_drop_data(const Point2 &p_point, const Variant &p_data) const { - return p_data.get_type()==Variant::STRING; + return p_data.get_type() == Variant::STRING; } -void LineEdit::drop_data(const Point2& p_point,const Variant& p_data){ +void LineEdit::drop_data(const Point2 &p_point, const Variant &p_data) { - if (p_data.get_type()==Variant::STRING) { + if (p_data.get_type() == Variant::STRING) { set_cursor_at_pixel_pos(p_point.x); int selected = selection.end - selection.begin; @@ -537,30 +526,29 @@ void LineEdit::drop_data(const Point2& p_point,const Variant& p_data){ text.erase(selection.begin, selected); append_at_cursor(p_data); - selection.begin = cursor_pos-selected; + selection.begin = cursor_pos - selected; selection.end = cursor_pos; } } - void LineEdit::_notification(int p_what) { - switch(p_what) { + switch (p_what) { #ifdef TOOLS_ENABLED case NOTIFICATION_ENTER_TREE: { if (get_tree()->is_editor_hint()) { cursor_set_blink_enabled(EDITOR_DEF("text_editor/cursor/caret_blink", false)); cursor_set_blink_speed(EDITOR_DEF("text_editor/cursor/caret_blink_speed", 0.65)); - if (!EditorSettings::get_singleton()->is_connected("settings_changed",this,"_editor_settings_changed")) { - EditorSettings::get_singleton()->connect("settings_changed",this,"_editor_settings_changed"); + if (!EditorSettings::get_singleton()->is_connected("settings_changed", this, "_editor_settings_changed")) { + EditorSettings::get_singleton()->connect("settings_changed", this, "_editor_settings_changed"); } } } break; #endif case NOTIFICATION_RESIZED: { - set_cursor_pos( get_cursor_pos() ); + set_cursor_pos(get_cursor_pos()); } break; case MainLoop::NOTIFICATION_WM_FOCUS_IN: { @@ -579,100 +567,100 @@ void LineEdit::_notification(int p_what) { draw_caret = false; } - int width,height; + int width, height; - Size2 size=get_size(); - width=size.width; - height=size.height; + Size2 size = get_size(); + width = size.width; + height = size.height; RID ci = get_canvas_item(); Ref<StyleBox> style = get_stylebox("normal"); if (!is_editable()) - style=get_stylebox("read_only"); + style = get_stylebox("read_only"); - Ref<Font> font=get_font("font"); + Ref<Font> font = get_font("font"); - style->draw( ci, Rect2( Point2(), size ) ); + style->draw(ci, Rect2(Point2(), size)); if (has_focus()) { - get_stylebox("focus")->draw( ci, Rect2( Point2(), size ) ); + get_stylebox("focus")->draw(ci, Rect2(Point2(), size)); } - int x_ofs=0; + int x_ofs = 0; switch (align) { case ALIGN_FILL: case ALIGN_LEFT: { - x_ofs=style->get_offset().x; + x_ofs = style->get_offset().x; } break; case ALIGN_CENTER: { - x_ofs=int(size.width-(cached_width))/2; + x_ofs = int(size.width - (cached_width)) / 2; } break; case ALIGN_RIGHT: { - x_ofs=int(size.width-style->get_offset().x-(cached_width)); + x_ofs = int(size.width - style->get_offset().x - (cached_width)); } break; } - int ofs_max=width-style->get_minimum_size().width; - int char_ofs=window_pos; + int ofs_max = width - style->get_minimum_size().width; + int char_ofs = window_pos; - int y_area=height-style->get_minimum_size().height; - int y_ofs=style->get_offset().y; + int y_area = height - style->get_minimum_size().height; + int y_ofs = style->get_offset().y; - int font_ascent=font->get_ascent(); + int font_ascent = font->get_ascent(); - Color selection_color=get_color("selection_color"); - Color font_color=get_color("font_color"); - Color font_color_selected=get_color("font_color_selected"); - Color cursor_color=get_color("cursor_color"); + Color selection_color = get_color("selection_color"); + Color font_color = get_color("font_color"); + Color font_color_selected = get_color("font_color_selected"); + Color cursor_color = get_color("cursor_color"); - const String& t = text.empty() ? placeholder : text; + const String &t = text.empty() ? placeholder : text; // draw placeholder color - if(text.empty()) + if (text.empty()) font_color.a *= placeholder_alpha; int caret_height = font->get_height() > y_area ? y_area : font->get_height(); - while(true) { + while (true) { - //end of string, break! - if (char_ofs>=t.length()) + //end of string, break! + if (char_ofs >= t.length()) break; - CharType cchar=pass?'*':t[char_ofs]; - CharType next=pass?'*':t[char_ofs+1]; - int char_width=font->get_char_size( cchar,next ).width; + CharType cchar = pass ? '*' : t[char_ofs]; + CharType next = pass ? '*' : t[char_ofs + 1]; + int char_width = font->get_char_size(cchar, next).width; - // end of widget, break! + // end of widget, break! if ((x_ofs + char_width) > ofs_max) break; - - bool selected=selection.enabled && char_ofs>=selection.begin && char_ofs<selection.end; + bool selected = selection.enabled && char_ofs >= selection.begin && char_ofs < selection.end; if (selected) VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs), Size2(char_width, caret_height)), selection_color); - font->draw_char(ci, Point2(x_ofs, y_ofs + font_ascent), cchar, next, selected ? font_color_selected : font_color); - if (char_ofs==cursor_pos && draw_caret) { + if (char_ofs == cursor_pos && draw_caret) { VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2( - Point2( x_ofs , y_ofs ), Size2( 1, caret_height ) ), cursor_color ); + Point2(x_ofs, y_ofs), Size2(1, caret_height)), + cursor_color); } - x_ofs+=char_width; + x_ofs += char_width; char_ofs++; } - if (char_ofs==cursor_pos && draw_caret) {//may be at the end + if (char_ofs == cursor_pos && draw_caret) { //may be at the end VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2( - Point2( x_ofs , y_ofs ), Size2( 1, caret_height ) ), cursor_color ); + Point2(x_ofs, y_ofs), Size2(1, caret_height)), + cursor_color); } } break; case NOTIFICATION_FOCUS_ENTER: { @@ -682,7 +670,7 @@ void LineEdit::_notification(int p_what) { } if (OS::get_singleton()->has_virtual_keyboard()) - OS::get_singleton()->show_virtual_keyboard(text,get_global_rect()); + OS::get_singleton()->show_virtual_keyboard(text, get_global_rect()); } break; case NOTIFICATION_FOCUS_EXIT: { @@ -691,13 +679,12 @@ void LineEdit::_notification(int p_what) { OS::get_singleton()->hide_virtual_keyboard(); } break; - } } void LineEdit::copy_text() { - if(selection.enabled) { + if (selection.enabled) { OS::get_singleton()->set_clipboard(text.substr(selection.begin, selection.end - selection.begin)); } @@ -705,7 +692,7 @@ void LineEdit::copy_text() { void LineEdit::cut_text() { - if(selection.enabled) { + if (selection.enabled) { undo_text = text; OS::get_singleton()->set_clipboard(text.substr(selection.begin, selection.end - selection.begin)); selection_delete(); @@ -716,16 +703,13 @@ void LineEdit::paste_text() { String paste_buffer = OS::get_singleton()->get_clipboard(); - if(paste_buffer != "") { + if (paste_buffer != "") { - if(selection.enabled) selection_delete(); + if (selection.enabled) selection_delete(); append_at_cursor(paste_buffer); _text_changed(); } - - - } void LineEdit::undo() { @@ -736,10 +720,10 @@ void LineEdit::undo() { Ref<Font> font = get_font("font"); cached_width = 0; - for (int i = 0; i<text.length(); i++) + for (int i = 0; i < text.length(); i++) cached_width += font->get_char_size(text[i]).width; - if(old_cursor_pos > text.length()) { + if (old_cursor_pos > text.length()) { set_cursor_pos(text.length()); } else { set_cursor_pos(old_cursor_pos); @@ -750,12 +734,11 @@ void LineEdit::undo() { void LineEdit::shift_selection_check_pre(bool p_shift) { - if (!selection.enabled && p_shift) { - selection.cursor_start=cursor_pos; + if (!selection.enabled && p_shift) { + selection.cursor_start = cursor_pos; } if (!p_shift) selection_clear(); - } void LineEdit::shift_selection_check_post(bool p_shift) { @@ -781,32 +764,30 @@ void LineEdit::set_cursor_at_pixel_pos(int p_x) { } break; case ALIGN_CENTER: { - pixel_ofs=int(size.width-(cached_width))/2; + pixel_ofs = int(size.width - (cached_width)) / 2; } break; case ALIGN_RIGHT: { - pixel_ofs=int(size.width-style->get_offset().x-(cached_width)); + pixel_ofs = int(size.width - style->get_offset().x - (cached_width)); } break; } - - while (ofs<text.length()) { + while (ofs < text.length()) { int char_w = 0; if (font != NULL) { char_w = font->get_char_size(text[ofs]).width; } - pixel_ofs+=char_w; + pixel_ofs += char_w; if (pixel_ofs > p_x) { //found what we look for break; } - ofs++; } - set_cursor_pos( ofs ); + set_cursor_pos(ofs); /* int new_cursor_pos=p_x; @@ -816,7 +797,6 @@ void LineEdit::set_cursor_at_pixel_pos(int p_x) { set_cursor_pos(window_pos+new_cursor_pos); */ } - bool LineEdit::cursor_get_blink_enabled() const { return caret_blink_enabled; } @@ -847,7 +827,7 @@ void LineEdit::_reset_caret_blink_timer() { draw_caret = true; update(); } - } +} void LineEdit::_toggle_draw_caret() { draw_caret = !draw_caret; @@ -858,18 +838,18 @@ void LineEdit::_toggle_draw_caret() { void LineEdit::delete_char() { - if ((text.length()<=0) || (cursor_pos==0)) return; + if ((text.length() <= 0) || (cursor_pos == 0)) return; Ref<Font> font = get_font("font"); if (font != NULL) { cached_width -= font->get_char_size(text[cursor_pos - 1]).width; } - text.erase( cursor_pos-1, 1 ); + text.erase(cursor_pos - 1, 1); - set_cursor_pos(get_cursor_pos()-1); + set_cursor_pos(get_cursor_pos() - 1); - if (cursor_pos==window_pos) { + if (cursor_pos == window_pos) { //set_window_pos(cursor_pos-get_window_length()); } @@ -881,29 +861,26 @@ void LineEdit::delete_text(int p_from_column, int p_to_column) { undo_text = text; - if (text.size() > 0) - { + if (text.size() > 0) { Ref<Font> font = get_font("font"); if (font != NULL) { for (int i = p_from_column; i < p_to_column; i++) cached_width -= font->get_char_size(text[i]).width; } - } - else - { + } else { cached_width = 0; } - text.erase(p_from_column,p_to_column-p_from_column); - cursor_pos-=CLAMP( cursor_pos-p_from_column, 0, p_to_column-p_from_column); + text.erase(p_from_column, p_to_column - p_from_column); + cursor_pos -= CLAMP(cursor_pos - p_from_column, 0, p_to_column - p_from_column); - if (cursor_pos>=text.length()) { + if (cursor_pos >= text.length()) { - cursor_pos=text.length(); + cursor_pos = text.length(); } - if (window_pos>cursor_pos) { + if (window_pos > cursor_pos) { - window_pos=cursor_pos; + window_pos = cursor_pos; } _text_changed(); @@ -914,8 +891,8 @@ void LineEdit::set_text(String p_text) { clear_internal(); append_at_cursor(p_text); update(); - cursor_pos=0; - window_pos=0; + cursor_pos = 0; + window_pos = 0; _text_changed(); } @@ -941,7 +918,6 @@ String LineEdit::get_placeholder() const { return placeholder; } - void LineEdit::set_placeholder_alpha(float p_alpha) { placeholder_alpha = p_alpha; @@ -955,76 +931,72 @@ float LineEdit::get_placeholder_alpha() const { void LineEdit::set_cursor_pos(int p_pos) { - if (p_pos>(int)text.length()) - p_pos=text.length(); - - if(p_pos<0) - p_pos=0; + if (p_pos > (int)text.length()) + p_pos = text.length(); + if (p_pos < 0) + p_pos = 0; - - cursor_pos=p_pos; + cursor_pos = p_pos; if (!is_inside_tree()) { - window_pos=cursor_pos; + window_pos = cursor_pos; return; } Ref<StyleBox> style = get_stylebox("normal"); - Ref<Font> font=get_font("font"); + Ref<Font> font = get_font("font"); - if (cursor_pos<window_pos) { + if (cursor_pos < window_pos) { /* Adjust window if cursor goes too much to the left */ set_window_pos(cursor_pos); - } else if (cursor_pos>window_pos) { + } else if (cursor_pos > window_pos) { /* Adjust window if cursor goes too much to the right */ - int window_width=get_size().width-style->get_minimum_size().width; + int window_width = get_size().width - style->get_minimum_size().width; - if (window_width<0) + if (window_width < 0) return; - int wp=window_pos; + int wp = window_pos; if (font.is_valid()) { - int accum_width=0; + int accum_width = 0; - for(int i=cursor_pos;i>=window_pos;i--) { + for (int i = cursor_pos; i >= window_pos; i--) { - if (i>=text.length()) { - accum_width=font->get_char_size(' ').width; //anything should do + if (i >= text.length()) { + accum_width = font->get_char_size(' ').width; //anything should do } else { - accum_width+=font->get_char_size(text[i],i+1<text.length()?text[i+1]:0).width; //anything should do + accum_width += font->get_char_size(text[i], i + 1 < text.length() ? text[i + 1] : 0).width; //anything should do } - if (accum_width>=window_width) + if (accum_width >= window_width) break; - wp=i; + wp = i; } } - if (wp!=window_pos) - set_window_pos( wp ); - + if (wp != window_pos) + set_window_pos(wp); } update(); } -int LineEdit::get_cursor_pos() const { +int LineEdit::get_cursor_pos() const { return cursor_pos; } void LineEdit::set_window_pos(int p_pos) { - window_pos=p_pos; - if (window_pos<0) window_pos=0; + window_pos = p_pos; + if (window_pos < 0) window_pos = 0; } void LineEdit::append_at_cursor(String p_text) { - - if ( ( max_length <= 0 ) || (text.length()+p_text.length() <= max_length)) { + if ((max_length <= 0) || (text.length() + p_text.length() <= max_length)) { undo_text = text; @@ -1032,45 +1004,44 @@ void LineEdit::append_at_cursor(String p_text) { if (font != NULL) { for (int i = 0; i < p_text.length(); i++) cached_width += font->get_char_size(p_text[i]).width; - } - else { + } else { cached_width = 0; } - String pre = text.substr( 0, cursor_pos ); - String post = text.substr( cursor_pos, text.length()-cursor_pos ); - text=pre+p_text+post; - set_cursor_pos(cursor_pos+p_text.length()); + String pre = text.substr(0, cursor_pos); + String post = text.substr(cursor_pos, text.length() - cursor_pos); + text = pre + p_text + post; + set_cursor_pos(cursor_pos + p_text.length()); } } void LineEdit::clear_internal() { cached_width = 0; - cursor_pos=0; - window_pos=0; - undo_text=""; - text=""; + cursor_pos = 0; + window_pos = 0; + undo_text = ""; + text = ""; update(); } Size2 LineEdit::get_minimum_size() const { Ref<StyleBox> style = get_stylebox("normal"); - Ref<Font> font=get_font("font"); + Ref<Font> font = get_font("font"); - Size2 min=style->get_minimum_size(); - min.height+=font->get_height(); + Size2 min = style->get_minimum_size(); + min.height += font->get_height(); //minimum size of text int space_size = font->get_char_size(' ').x; - int mstext = get_constant("minimum_spaces")*space_size; + int mstext = get_constant("minimum_spaces") * space_size; if (expand_to_text_length) { - mstext=MAX(mstext,font->get_string_size(text).x+space_size); //add a spce because some fonts are too exact + mstext = MAX(mstext, font->get_string_size(text).x + space_size); //add a spce because some fonts are too exact } - min.width+=mstext; + min.width += mstext; return min; } @@ -1079,26 +1050,26 @@ Size2 LineEdit::get_minimum_size() const { void LineEdit::selection_clear() { - selection.begin=0; - selection.end=0; - selection.cursor_start=0; - selection.enabled=false; - selection.creating=false; - selection.doubleclick=false; + selection.begin = 0; + selection.end = 0; + selection.cursor_start = 0; + selection.enabled = false; + selection.creating = false; + selection.doubleclick = false; update(); } void LineEdit::selection_delete() { if (selection.enabled) - delete_text(selection.begin,selection.end); + delete_text(selection.begin, selection.end); selection_clear(); } void LineEdit::set_max_length(int p_max_length) { - ERR_FAIL_COND(p_max_length<0); + ERR_FAIL_COND(p_max_length < 0); max_length = p_max_length; set_text(text); } @@ -1112,16 +1083,16 @@ void LineEdit::selection_fill_at_cursor() { int aux; - selection.begin=cursor_pos; - selection.end=selection.cursor_start; + selection.begin = cursor_pos; + selection.end = selection.cursor_start; - if (selection.end<selection.begin) { - aux=selection.end; - selection.end=selection.begin; - selection.begin=aux; + if (selection.end < selection.begin) { + aux = selection.end; + selection.end = selection.begin; + selection.begin = aux; } - selection.enabled=(selection.begin!=selection.end); + selection.enabled = (selection.begin != selection.end); } void LineEdit::select_all() { @@ -1129,15 +1100,14 @@ void LineEdit::select_all() { if (!text.length()) return; - selection.begin=0; - selection.end=text.length(); - selection.enabled=true; + selection.begin = 0; + selection.end = text.length(); + selection.enabled = true; update(); - } void LineEdit::set_editable(bool p_editable) { - editable=p_editable; + editable = p_editable; update(); } @@ -1148,7 +1118,7 @@ bool LineEdit::is_editable() const { void LineEdit::set_secret(bool p_secret) { - pass=p_secret; + pass = p_secret; update(); } bool LineEdit::is_secret() const { @@ -1158,38 +1128,38 @@ bool LineEdit::is_secret() const { void LineEdit::select(int p_from, int p_to) { - if (p_from==0 && p_to==0) { + if (p_from == 0 && p_to == 0) { selection_clear(); return; } int len = text.length(); - if (p_from<0) - p_from=0; - if (p_from>len) - p_from=len; - if (p_to<0 || p_to>len) - p_to=len; - - if (p_from>=p_to) + if (p_from < 0) + p_from = 0; + if (p_from > len) + p_from = len; + if (p_to < 0 || p_to > len) + p_to = len; + + if (p_from >= p_to) return; - selection.enabled=true; - selection.begin=p_from; - selection.end=p_to; - selection.creating=false; - selection.doubleclick=false; + selection.enabled = true; + selection.begin = p_from; + selection.end = p_to; + selection.creating = false; + selection.doubleclick = false; update(); } bool LineEdit::is_text_field() const { - return true; + return true; } void LineEdit::menu_option(int p_option) { - switch(p_option) { + switch (p_option) { case MENU_CUT: { if (editable) { cut_text(); @@ -1215,9 +1185,7 @@ void LineEdit::menu_option(int p_option) { case MENU_UNDO: { undo(); } break; - } - } PopupMenu *LineEdit::get_menu() const { @@ -1225,148 +1193,140 @@ PopupMenu *LineEdit::get_menu() const { } #ifdef TOOLS_ENABLED - void LineEdit::_editor_settings_changed() { - cursor_set_blink_enabled(EDITOR_DEF("text_editor/cursor/caret_blink", false)); - cursor_set_blink_speed(EDITOR_DEF("text_editor/cursor/caret_blink_speed", 0.65)); - } +void LineEdit::_editor_settings_changed() { + cursor_set_blink_enabled(EDITOR_DEF("text_editor/cursor/caret_blink", false)); + cursor_set_blink_speed(EDITOR_DEF("text_editor/cursor/caret_blink_speed", 0.65)); +} #endif - void LineEdit::set_expand_to_text_length(bool p_enabled) { expand_to_text_length = p_enabled; minimum_size_changed(); } -bool LineEdit::get_expand_to_text_length() const{ +bool LineEdit::get_expand_to_text_length() const { return expand_to_text_length; } - void LineEdit::_text_changed() { if (expand_to_text_length) minimum_size_changed(); - emit_signal("text_changed",text); + emit_signal("text_changed", text); _change_notify("text"); - } void LineEdit::_bind_methods() { - ClassDB::bind_method(D_METHOD("_toggle_draw_caret"),&LineEdit::_toggle_draw_caret); + ClassDB::bind_method(D_METHOD("_toggle_draw_caret"), &LineEdit::_toggle_draw_caret); #ifdef TOOLS_ENABLED - ClassDB::bind_method("_editor_settings_changed",&LineEdit::_editor_settings_changed); + ClassDB::bind_method("_editor_settings_changed", &LineEdit::_editor_settings_changed); #endif ClassDB::bind_method(D_METHOD("set_align", "align"), &LineEdit::set_align); ClassDB::bind_method(D_METHOD("get_align"), &LineEdit::get_align); - ClassDB::bind_method(D_METHOD("_gui_input"),&LineEdit::_gui_input); - ClassDB::bind_method(D_METHOD("clear"),&LineEdit::clear); - ClassDB::bind_method(D_METHOD("select_all"),&LineEdit::select_all); - ClassDB::bind_method(D_METHOD("set_text","text"),&LineEdit::set_text); - ClassDB::bind_method(D_METHOD("get_text"),&LineEdit::get_text); - ClassDB::bind_method(D_METHOD("set_placeholder","text"),&LineEdit::set_placeholder); - ClassDB::bind_method(D_METHOD("get_placeholder"),&LineEdit::get_placeholder); - ClassDB::bind_method(D_METHOD("set_placeholder_alpha","alpha"),&LineEdit::set_placeholder_alpha); - ClassDB::bind_method(D_METHOD("get_placeholder_alpha"),&LineEdit::get_placeholder_alpha); - ClassDB::bind_method(D_METHOD("set_cursor_pos","pos"),&LineEdit::set_cursor_pos); - ClassDB::bind_method(D_METHOD("get_cursor_pos"),&LineEdit::get_cursor_pos); - ClassDB::bind_method(D_METHOD("set_expand_to_text_length","enabled"),&LineEdit::set_expand_to_text_length); - ClassDB::bind_method(D_METHOD("get_expand_to_text_length"),&LineEdit::get_expand_to_text_length); - ClassDB::bind_method(D_METHOD("cursor_set_blink_enabled", "enabled"),&LineEdit::cursor_set_blink_enabled); - ClassDB::bind_method(D_METHOD("cursor_get_blink_enabled"),&LineEdit::cursor_get_blink_enabled); - ClassDB::bind_method(D_METHOD("cursor_set_blink_speed", "blink_speed"),&LineEdit::cursor_set_blink_speed); - ClassDB::bind_method(D_METHOD("cursor_get_blink_speed"),&LineEdit::cursor_get_blink_speed); - ClassDB::bind_method(D_METHOD("set_max_length","chars"),&LineEdit::set_max_length); - ClassDB::bind_method(D_METHOD("get_max_length"),&LineEdit::get_max_length); - ClassDB::bind_method(D_METHOD("append_at_cursor","text"),&LineEdit::append_at_cursor); - ClassDB::bind_method(D_METHOD("set_editable","enabled"),&LineEdit::set_editable); - ClassDB::bind_method(D_METHOD("is_editable"),&LineEdit::is_editable); - ClassDB::bind_method(D_METHOD("set_secret","enabled"),&LineEdit::set_secret); - ClassDB::bind_method(D_METHOD("is_secret"),&LineEdit::is_secret); - ClassDB::bind_method(D_METHOD("select","from","to"),&LineEdit::select,DEFVAL(0),DEFVAL(-1)); - ClassDB::bind_method(D_METHOD("menu_option","option"),&LineEdit::menu_option); - ClassDB::bind_method(D_METHOD("get_menu:PopupMenu"),&LineEdit::get_menu); - - ADD_SIGNAL( MethodInfo("text_changed", PropertyInfo( Variant::STRING, "text" )) ); - ADD_SIGNAL( MethodInfo("text_entered", PropertyInfo( Variant::STRING, "text" )) ); + ClassDB::bind_method(D_METHOD("_gui_input"), &LineEdit::_gui_input); + ClassDB::bind_method(D_METHOD("clear"), &LineEdit::clear); + ClassDB::bind_method(D_METHOD("select_all"), &LineEdit::select_all); + ClassDB::bind_method(D_METHOD("set_text", "text"), &LineEdit::set_text); + ClassDB::bind_method(D_METHOD("get_text"), &LineEdit::get_text); + ClassDB::bind_method(D_METHOD("set_placeholder", "text"), &LineEdit::set_placeholder); + ClassDB::bind_method(D_METHOD("get_placeholder"), &LineEdit::get_placeholder); + ClassDB::bind_method(D_METHOD("set_placeholder_alpha", "alpha"), &LineEdit::set_placeholder_alpha); + ClassDB::bind_method(D_METHOD("get_placeholder_alpha"), &LineEdit::get_placeholder_alpha); + ClassDB::bind_method(D_METHOD("set_cursor_pos", "pos"), &LineEdit::set_cursor_pos); + ClassDB::bind_method(D_METHOD("get_cursor_pos"), &LineEdit::get_cursor_pos); + ClassDB::bind_method(D_METHOD("set_expand_to_text_length", "enabled"), &LineEdit::set_expand_to_text_length); + ClassDB::bind_method(D_METHOD("get_expand_to_text_length"), &LineEdit::get_expand_to_text_length); + ClassDB::bind_method(D_METHOD("cursor_set_blink_enabled", "enabled"), &LineEdit::cursor_set_blink_enabled); + ClassDB::bind_method(D_METHOD("cursor_get_blink_enabled"), &LineEdit::cursor_get_blink_enabled); + ClassDB::bind_method(D_METHOD("cursor_set_blink_speed", "blink_speed"), &LineEdit::cursor_set_blink_speed); + ClassDB::bind_method(D_METHOD("cursor_get_blink_speed"), &LineEdit::cursor_get_blink_speed); + ClassDB::bind_method(D_METHOD("set_max_length", "chars"), &LineEdit::set_max_length); + ClassDB::bind_method(D_METHOD("get_max_length"), &LineEdit::get_max_length); + ClassDB::bind_method(D_METHOD("append_at_cursor", "text"), &LineEdit::append_at_cursor); + ClassDB::bind_method(D_METHOD("set_editable", "enabled"), &LineEdit::set_editable); + ClassDB::bind_method(D_METHOD("is_editable"), &LineEdit::is_editable); + ClassDB::bind_method(D_METHOD("set_secret", "enabled"), &LineEdit::set_secret); + ClassDB::bind_method(D_METHOD("is_secret"), &LineEdit::is_secret); + ClassDB::bind_method(D_METHOD("select", "from", "to"), &LineEdit::select, DEFVAL(0), DEFVAL(-1)); + ClassDB::bind_method(D_METHOD("menu_option", "option"), &LineEdit::menu_option); + ClassDB::bind_method(D_METHOD("get_menu:PopupMenu"), &LineEdit::get_menu); + + ADD_SIGNAL(MethodInfo("text_changed", PropertyInfo(Variant::STRING, "text"))); + ADD_SIGNAL(MethodInfo("text_entered", PropertyInfo(Variant::STRING, "text"))); BIND_CONSTANT(ALIGN_LEFT); BIND_CONSTANT(ALIGN_CENTER); BIND_CONSTANT(ALIGN_RIGHT); BIND_CONSTANT(ALIGN_FILL); - BIND_CONSTANT( MENU_CUT ); - BIND_CONSTANT( MENU_COPY ); - BIND_CONSTANT( MENU_PASTE ); - BIND_CONSTANT( MENU_CLEAR ); - BIND_CONSTANT( MENU_SELECT_ALL ); - BIND_CONSTANT( MENU_UNDO ); - BIND_CONSTANT( MENU_MAX ); + BIND_CONSTANT(MENU_CUT); + BIND_CONSTANT(MENU_COPY); + BIND_CONSTANT(MENU_PASTE); + BIND_CONSTANT(MENU_CLEAR); + BIND_CONSTANT(MENU_SELECT_ALL); + BIND_CONSTANT(MENU_UNDO); + BIND_CONSTANT(MENU_MAX); - ADD_PROPERTYNZ( PropertyInfo( Variant::STRING, "text" ), "set_text","get_text") ; + ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text"); ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_align", "get_align"); - ADD_PROPERTYNZ( PropertyInfo( Variant::INT, "max_length" ), "set_max_length","get_max_length") ; - ADD_PROPERTYNO( PropertyInfo( Variant::BOOL, "editable" ), "set_editable","is_editable") ; - ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "secret" ), "set_secret","is_secret") ; - ADD_PROPERTYNO( PropertyInfo( Variant::BOOL, "expand_to_len" ), "set_expand_to_text_length","get_expand_to_text_length") ; - ADD_PROPERTY( PropertyInfo( Variant::INT,"focus_mode", PROPERTY_HINT_ENUM, "None,Click,All" ), "set_focus_mode", "get_focus_mode") ; - ADD_GROUP("Placeholder","placeholder_"); - ADD_PROPERTYNZ( PropertyInfo( Variant::STRING, "placeholder_text" ), "set_placeholder","get_placeholder") ; - ADD_PROPERTYNZ( PropertyInfo( Variant::REAL, "placeholder_alpha",PROPERTY_HINT_RANGE,"0,1,0.001" ), "set_placeholder_alpha","get_placeholder_alpha") ; - ADD_GROUP("Caret","caret_"); + ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "max_length"), "set_max_length", "get_max_length"); + ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable"); + ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "secret"), "set_secret", "is_secret"); + ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "expand_to_len"), "set_expand_to_text_length", "get_expand_to_text_length"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "focus_mode", PROPERTY_HINT_ENUM, "None,Click,All"), "set_focus_mode", "get_focus_mode"); + ADD_GROUP("Placeholder", "placeholder_"); + ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "placeholder_text"), "set_placeholder", "get_placeholder"); + ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "placeholder_alpha", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_placeholder_alpha", "get_placeholder_alpha"); + ADD_GROUP("Caret", "caret_"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "cursor_set_blink_enabled", "cursor_get_blink_enabled"); - ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "caret_blink_speed",PROPERTY_HINT_RANGE,"0.1,10,0.1"), "cursor_set_blink_speed","cursor_get_blink_speed") ; + ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "caret_blink_speed", PROPERTY_HINT_RANGE, "0.1,10,0.1"), "cursor_set_blink_speed", "cursor_get_blink_speed"); } LineEdit::LineEdit() { align = ALIGN_LEFT; cached_width = 0; - cursor_pos=0; - window_pos=0; - window_has_focus=true; + cursor_pos = 0; + window_pos = 0; + window_has_focus = true; max_length = 0; - pass=false; - placeholder_alpha=0.6; + pass = false; + placeholder_alpha = 0.6; selection_clear(); - set_focus_mode( FOCUS_ALL ); - editable=true; + set_focus_mode(FOCUS_ALL); + editable = true; set_default_cursor_shape(CURSOR_IBEAM); set_mouse_filter(MOUSE_FILTER_STOP); - draw_caret=true; - caret_blink_enabled=false; + draw_caret = true; + 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"); + caret_blink_timer->connect("timeout", this, "_toggle_draw_caret"); cursor_set_blink_enabled(false); - menu = memnew( PopupMenu ); + menu = memnew(PopupMenu); add_child(menu); - menu->add_item(TTR("Cut"),MENU_CUT,KEY_MASK_CMD|KEY_X); - menu->add_item(TTR("Copy"),MENU_COPY,KEY_MASK_CMD|KEY_C); - menu->add_item(TTR("Paste"),MENU_PASTE,KEY_MASK_CMD|KEY_V); + menu->add_item(TTR("Cut"), MENU_CUT, KEY_MASK_CMD | KEY_X); + menu->add_item(TTR("Copy"), MENU_COPY, KEY_MASK_CMD | KEY_C); + menu->add_item(TTR("Paste"), MENU_PASTE, KEY_MASK_CMD | KEY_V); menu->add_separator(); - menu->add_item(TTR("Select All"),MENU_SELECT_ALL,KEY_MASK_CMD|KEY_A); - menu->add_item(TTR("Clear"),MENU_CLEAR); + menu->add_item(TTR("Select All"), MENU_SELECT_ALL, KEY_MASK_CMD | KEY_A); + menu->add_item(TTR("Clear"), MENU_CLEAR); menu->add_separator(); - menu->add_item(TTR("Undo"),MENU_UNDO,KEY_MASK_CMD|KEY_Z); - menu->connect("id_pressed",this,"menu_option"); - expand_to_text_length=false; - - + menu->add_item(TTR("Undo"), MENU_UNDO, KEY_MASK_CMD | KEY_Z); + menu->connect("id_pressed", this, "menu_option"); + expand_to_text_length = false; } LineEdit::~LineEdit() { - - } - diff --git a/scene/gui/line_edit.h b/scene/gui/line_edit.h index 64c37861d0..0746547115 100644 --- a/scene/gui/line_edit.h +++ b/scene/gui/line_edit.h @@ -37,7 +37,7 @@ */ class LineEdit : public Control { - GDCLASS( LineEdit, Control ); + GDCLASS(LineEdit, Control); public: enum Align { @@ -91,7 +91,6 @@ private: Timer *caret_blink_timer; - void _text_changed(); bool expand_to_text_length; @@ -122,16 +121,16 @@ private: void _gui_input(InputEvent p_event); void _notification(int p_what); - protected: static void _bind_methods(); + public: void set_align(Align p_align); Align get_align() const; - virtual Variant get_drag_data(const Point2& p_point); - virtual bool can_drop_data(const Point2& p_point,const Variant& p_data) const; - virtual void drop_data(const Point2& p_point,const Variant& p_data); + virtual Variant get_drag_data(const Point2 &p_point); + virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const; + virtual void drop_data(const Point2 &p_point, const Variant &p_data); void menu_option(int p_option); PopupMenu *get_menu() const; @@ -170,7 +169,7 @@ public: void set_secret(bool p_secret); bool is_secret() const; - void select(int p_from=0, int p_to=-1); + void select(int p_from = 0, int p_to = -1); virtual Size2 get_minimum_size() const; @@ -180,10 +179,8 @@ public: virtual bool is_text_field() const; LineEdit(); ~LineEdit(); - }; - VARIANT_ENUM_CAST(LineEdit::Align); #endif diff --git a/scene/gui/link_button.cpp b/scene/gui/link_button.cpp index 7ea88f35f3..17e549c902 100644 --- a/scene/gui/link_button.cpp +++ b/scene/gui/link_button.cpp @@ -28,10 +28,9 @@ /*************************************************************************/ #include "link_button.h" +void LinkButton::set_text(const String &p_text) { -void LinkButton::set_text(const String& p_text) { - - text=p_text; + text = p_text; update(); minimum_size_changed(); } @@ -42,7 +41,7 @@ String LinkButton::get_text() const { void LinkButton::set_underline_mode(UnderlineMode p_underline_mode) { - underline_mode=p_underline_mode; + underline_mode = p_underline_mode; update(); } @@ -51,55 +50,51 @@ LinkButton::UnderlineMode LinkButton::get_underline_mode() const { return underline_mode; } - Size2 LinkButton::get_minimum_size() const { - return get_font("font")->get_string_size( text ); + return get_font("font")->get_string_size(text); } - - void LinkButton::_notification(int p_what) { - switch( p_what ) { + switch (p_what) { case NOTIFICATION_DRAW: { - RID ci = get_canvas_item(); - Size2 size=get_size(); + Size2 size = get_size(); Color color; - bool do_underline=false; + bool do_underline = false; //print_line(get_text()+": "+itos(is_flat())+" hover "+itos(get_draw_mode())); - switch( get_draw_mode() ) { + switch (get_draw_mode()) { case DRAW_NORMAL: { - color=get_color("font_color"); - do_underline=underline_mode==UNDERLINE_MODE_ALWAYS; + color = get_color("font_color"); + do_underline = underline_mode == UNDERLINE_MODE_ALWAYS; } break; case DRAW_PRESSED: { if (has_color("font_color_pressed")) - color=get_color("font_color_pressed"); + color = get_color("font_color_pressed"); else - color=get_color("font_color"); + color = get_color("font_color"); - do_underline=underline_mode!=UNDERLINE_MODE_NEVER; + do_underline = underline_mode != UNDERLINE_MODE_NEVER; } break; case DRAW_HOVER: { - color=get_color("font_color_hover"); - do_underline=underline_mode!=UNDERLINE_MODE_NEVER; + color = get_color("font_color_hover"); + do_underline = underline_mode != UNDERLINE_MODE_NEVER; } break; case DRAW_DISABLED: { - color=get_color("font_color_disabled"); - do_underline=underline_mode==UNDERLINE_MODE_ALWAYS; + color = get_color("font_color_disabled"); + do_underline = underline_mode == UNDERLINE_MODE_ALWAYS; } break; } @@ -107,21 +102,19 @@ void LinkButton::_notification(int p_what) { if (has_focus()) { Ref<StyleBox> style = get_stylebox("focus"); - style->draw(ci,Rect2(Point2(),size)); + style->draw(ci, Rect2(Point2(), size)); } - Ref<Font> font=get_font("font"); - - draw_string(font,Vector2(0,font->get_ascent()),text,color); - + Ref<Font> font = get_font("font"); + draw_string(font, Vector2(0, font->get_ascent()), text, color); if (do_underline) { int underline_spacing = get_constant("underline_spacing"); int width = font->get_string_size(text).width; - int y = font->get_ascent()+underline_spacing; + int y = font->get_ascent() + underline_spacing; - draw_line(Vector2(0,y),Vector2(width,y),color); + draw_line(Vector2(0, y), Vector2(width, y), color); } } break; @@ -130,24 +123,22 @@ void LinkButton::_notification(int p_what) { void LinkButton::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_text","text"),&LinkButton::set_text); - ClassDB::bind_method(D_METHOD("get_text"),&LinkButton::get_text); - - ClassDB::bind_method(D_METHOD("set_underline_mode","underline_mode"),&LinkButton::set_underline_mode); - ClassDB::bind_method(D_METHOD("get_underline_mode"),&LinkButton::get_underline_mode); - + ClassDB::bind_method(D_METHOD("set_text", "text"), &LinkButton::set_text); + ClassDB::bind_method(D_METHOD("get_text"), &LinkButton::get_text); - BIND_CONSTANT( UNDERLINE_MODE_ALWAYS ); - BIND_CONSTANT( UNDERLINE_MODE_ON_HOVER ); - BIND_CONSTANT( UNDERLINE_MODE_NEVER ); + ClassDB::bind_method(D_METHOD("set_underline_mode", "underline_mode"), &LinkButton::set_underline_mode); + ClassDB::bind_method(D_METHOD("get_underline_mode"), &LinkButton::get_underline_mode); - ADD_PROPERTYNZ(PropertyInfo(Variant::STRING,"text"), "set_text", "get_text"); - ADD_PROPERTYNZ(PropertyInfo(Variant::INT,"underline",PROPERTY_HINT_ENUM,"Always,On Hover,Never"), "set_underline_mode", "get_underline_mode"); + BIND_CONSTANT(UNDERLINE_MODE_ALWAYS); + BIND_CONSTANT(UNDERLINE_MODE_ON_HOVER); + BIND_CONSTANT(UNDERLINE_MODE_NEVER); + ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text"); + ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "underline", PROPERTY_HINT_ENUM, "Always,On Hover,Never"), "set_underline_mode", "get_underline_mode"); } LinkButton::LinkButton() { - underline_mode=UNDERLINE_MODE_ALWAYS; + underline_mode = UNDERLINE_MODE_ALWAYS; set_enabled_focus_mode(FOCUS_NONE); set_default_cursor_shape(CURSOR_POINTING_HAND); } diff --git a/scene/gui/link_button.h b/scene/gui/link_button.h index 42d7c05cff..97c2bfc3c8 100644 --- a/scene/gui/link_button.h +++ b/scene/gui/link_button.h @@ -29,33 +29,31 @@ #ifndef LINKBUTTON_H #define LINKBUTTON_H - #include "scene/gui/base_button.h" #include "scene/resources/bit_mask.h" class LinkButton : public BaseButton { - GDCLASS( LinkButton, BaseButton ); -public: + GDCLASS(LinkButton, BaseButton); +public: enum UnderlineMode { UNDERLINE_MODE_ALWAYS, UNDERLINE_MODE_ON_HOVER, UNDERLINE_MODE_NEVER }; + private: String text; UnderlineMode underline_mode; protected: - virtual Size2 get_minimum_size() const; void _notification(int p_what); static void _bind_methods(); public: - - void set_text(const String& p_text); + void set_text(const String &p_text); String get_text() const; void set_underline_mode(UnderlineMode p_underline_mode); @@ -64,6 +62,6 @@ public: LinkButton(); }; -VARIANT_ENUM_CAST( LinkButton::UnderlineMode ); +VARIANT_ENUM_CAST(LinkButton::UnderlineMode); #endif // LINKBUTTON_H diff --git a/scene/gui/margin_container.cpp b/scene/gui/margin_container.cpp index be27c40117..b7824e9037 100644 --- a/scene/gui/margin_container.cpp +++ b/scene/gui/margin_container.cpp @@ -28,7 +28,6 @@ /*************************************************************************/ #include "margin_container.h" - Size2 MarginContainer::get_minimum_size() const { int margin_left = get_constant("margin_left"); @@ -38,7 +37,7 @@ Size2 MarginContainer::get_minimum_size() const { Size2 max; - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { Control *c = get_child(i)->cast_to<Control>(); if (!c) @@ -59,12 +58,11 @@ Size2 MarginContainer::get_minimum_size() const { max.height += (margin_top + margin_bottom); return max; - } void MarginContainer::_notification(int p_what) { - if (p_what==NOTIFICATION_SORT_CHILDREN) { + if (p_what == NOTIFICATION_SORT_CHILDREN) { int margin_left = get_constant("margin_left"); int margin_top = get_constant("margin_top"); @@ -73,7 +71,7 @@ void MarginContainer::_notification(int p_what) { Size2 s = get_size(); - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { Control *c = get_child(i)->cast_to<Control>(); if (!c) @@ -81,14 +79,12 @@ void MarginContainer::_notification(int p_what) { if (c->is_set_as_toplevel()) continue; - 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)); + 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)); } - } } -MarginContainer::MarginContainer() -{ +MarginContainer::MarginContainer() { } diff --git a/scene/gui/margin_container.h b/scene/gui/margin_container.h index 542578dd01..9f0829a34d 100644 --- a/scene/gui/margin_container.h +++ b/scene/gui/margin_container.h @@ -32,12 +32,12 @@ #include "scene/gui/container.h" class MarginContainer : public Container { - GDCLASS(MarginContainer,Container); + GDCLASS(MarginContainer, Container); protected: void _notification(int p_what); -public: +public: virtual Size2 get_minimum_size() const; MarginContainer(); diff --git a/scene/gui/menu_button.cpp b/scene/gui/menu_button.cpp index f15b864b95..f2e2c9b255 100644 --- a/scene/gui/menu_button.cpp +++ b/scene/gui/menu_button.cpp @@ -30,36 +30,32 @@ #include "os/keyboard.h" #include "scene/main/viewport.h" - void MenuButton::_unhandled_key_input(InputEvent p_event) { - - if (p_event.is_pressed() && !p_event.is_echo() && (p_event.type==InputEvent::KEY || p_event.type==InputEvent::ACTION || p_event.type==InputEvent::JOYPAD_BUTTON)) { + if (p_event.is_pressed() && !p_event.is_echo() && (p_event.type == InputEvent::KEY || p_event.type == InputEvent::ACTION || p_event.type == InputEvent::JOYPAD_BUTTON)) { if (!get_parent() || !is_visible_in_tree() || is_disabled()) return; bool global_only = (get_viewport()->get_modal_stack_top() && !get_viewport()->get_modal_stack_top()->is_a_parent_of(this)); - if (popup->activate_item_by_event(p_event,global_only)) + if (popup->activate_item_by_event(p_event, global_only)) accept_event(); } } - void MenuButton::pressed() { emit_signal("about_to_show"); - Size2 size=get_size(); + Size2 size = get_size(); Point2 gp = get_global_pos(); - popup->set_global_pos( gp + Size2( 0, size.height ) ); - popup->set_size( Size2( size.width, 0) ); - popup->set_parent_rect( Rect2(Point2(gp-popup->get_global_pos()),get_size())); + popup->set_global_pos(gp + Size2(0, size.height)); + popup->set_size(Size2(size.width, 0)); + popup->set_parent_rect(Rect2(Point2(gp - popup->get_global_pos()), get_size())); popup->popup(); popup->call_deferred("grab_click_focus"); popup->set_invalidate_click_until_motion(); - } void MenuButton::_gui_input(InputEvent p_event) { @@ -91,28 +87,27 @@ Array MenuButton::_get_items() const { return popup->get("items"); } -void MenuButton::_set_items(const Array& p_items) { +void MenuButton::_set_items(const Array &p_items) { - popup->set("items",p_items); + popup->set("items", p_items); } void MenuButton::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_popup:PopupMenu"),&MenuButton::get_popup); - ClassDB::bind_method(D_METHOD("_unhandled_key_input"),&MenuButton::_unhandled_key_input); - ClassDB::bind_method(D_METHOD("_set_items"),&MenuButton::_set_items); - ClassDB::bind_method(D_METHOD("_get_items"),&MenuButton::_get_items); + ClassDB::bind_method(D_METHOD("get_popup:PopupMenu"), &MenuButton::get_popup); + ClassDB::bind_method(D_METHOD("_unhandled_key_input"), &MenuButton::_unhandled_key_input); + ClassDB::bind_method(D_METHOD("_set_items"), &MenuButton::_set_items); + ClassDB::bind_method(D_METHOD("_get_items"), &MenuButton::_get_items); - ADD_PROPERTY( PropertyInfo(Variant::ARRAY,"items",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR), "_set_items","_get_items") ; + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "items", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_items", "_get_items"); - ADD_SIGNAL( MethodInfo("about_to_show") ); + ADD_SIGNAL(MethodInfo("about_to_show")); } MenuButton::MenuButton() { - set_flat(true); set_enabled_focus_mode(FOCUS_NONE); - popup = memnew( PopupMenu ); + popup = memnew(PopupMenu); popup->hide(); add_child(popup); popup->set_as_toplevel(true); @@ -120,9 +115,5 @@ MenuButton::MenuButton() { set_action_mode(ACTION_MODE_BUTTON_PRESS); } - MenuButton::~MenuButton() { - } - - diff --git a/scene/gui/menu_button.h b/scene/gui/menu_button.h index 5b5573456f..4acb62cf37 100644 --- a/scene/gui/menu_button.h +++ b/scene/gui/menu_button.h @@ -29,14 +29,14 @@ #ifndef MENU_BUTTON_H #define MENU_BUTTON_H -#include "scene/gui/popup_menu.h" #include "scene/gui/button.h" +#include "scene/gui/popup_menu.h" /** @author Juan Linietsky <reduzio@gmail.com> */ class MenuButton : public Button { - GDCLASS( MenuButton, Button ); + GDCLASS(MenuButton, Button); bool clicked; PopupMenu *popup; @@ -44,15 +44,14 @@ class MenuButton : public Button { void _unhandled_key_input(InputEvent p_event); Array _get_items() const; - void _set_items(const Array& p_items); + void _set_items(const Array &p_items); void _gui_input(InputEvent p_event); -protected: - +protected: static void _bind_methods(); -public: +public: PopupMenu *get_popup(); MenuButton(); ~MenuButton(); diff --git a/scene/gui/option_button.cpp b/scene/gui/option_button.cpp index 9ead79b491..43f68d92ab 100644 --- a/scene/gui/option_button.cpp +++ b/scene/gui/option_button.cpp @@ -29,22 +29,19 @@ #include "option_button.h" #include "print_string.h" - Size2 OptionButton::get_minimum_size() const { - Size2 minsize = Button::get_minimum_size(); if (has_icon("arrow")) - minsize.width+=Control::get_icon("arrow")->get_width(); + minsize.width += Control::get_icon("arrow")->get_width(); return minsize; } void OptionButton::_notification(int p_what) { - - switch(p_what) { + switch (p_what) { case NOTIFICATION_DRAW: { @@ -53,86 +50,82 @@ void OptionButton::_notification(int p_what) { RID ci = get_canvas_item(); Ref<Texture> arrow = Control::get_icon("arrow"); - Ref<StyleBox> normal = get_stylebox("normal" ); + Ref<StyleBox> normal = get_stylebox("normal"); Size2 size = get_size(); - Point2 ofs( size.width - arrow->get_width() - get_constant("arrow_margin"), int(Math::abs((size.height-arrow->get_height())/2))); - arrow->draw(ci,ofs); + Point2 ofs(size.width - arrow->get_width() - get_constant("arrow_margin"), int(Math::abs((size.height - arrow->get_height()) / 2))); + arrow->draw(ci, ofs); } break; } } - void OptionButton::_selected(int p_which) { int selid = -1; - for (int i=0;i<popup->get_item_count();i++) { + for (int i = 0; i < popup->get_item_count(); i++) { - bool is_clicked = popup->get_item_ID(i)==p_which; + bool is_clicked = popup->get_item_ID(i) == p_which; if (is_clicked) { - selid=i; + selid = i; break; } } - if (selid==-1 && p_which>=0 && p_which<popup->get_item_count()) { - _select(p_which,true); + if (selid == -1 && p_which >= 0 && p_which < popup->get_item_count()) { + _select(p_which, true); } else { - ERR_FAIL_COND(selid==-1); + ERR_FAIL_COND(selid == -1); - _select(selid,true); + _select(selid, true); } } - void OptionButton::pressed() { - Size2 size=get_size(); - popup->set_global_pos( get_global_pos() + Size2( 0, size.height ) ); - popup->set_size( Size2( size.width, 0) ); + Size2 size = get_size(); + popup->set_global_pos(get_global_pos() + Size2(0, size.height)); + popup->set_size(Size2(size.width, 0)); popup->popup(); } -void OptionButton::add_icon_item(const Ref<Texture>& p_icon,const String& p_label,int p_ID) { +void OptionButton::add_icon_item(const Ref<Texture> &p_icon, const String &p_label, int p_ID) { - popup->add_icon_check_item( p_icon, p_label, p_ID ); - if (popup->get_item_count()==1) + popup->add_icon_check_item(p_icon, p_label, p_ID); + if (popup->get_item_count() == 1) select(0); } -void OptionButton::add_item(const String& p_label,int p_ID) { +void OptionButton::add_item(const String &p_label, int p_ID) { - popup->add_check_item( p_label, p_ID ); - if (popup->get_item_count()==1) + popup->add_check_item(p_label, p_ID); + if (popup->get_item_count() == 1) select(0); } -void OptionButton::set_item_text(int p_idx,const String& p_text) { - - popup->set_item_text(p_idx,p_text); +void OptionButton::set_item_text(int p_idx, const String &p_text) { + popup->set_item_text(p_idx, p_text); } -void OptionButton::set_item_icon(int p_idx,const Ref<Texture>& p_icon) { - - popup->set_item_icon(p_idx,p_icon); +void OptionButton::set_item_icon(int p_idx, const Ref<Texture> &p_icon) { + popup->set_item_icon(p_idx, p_icon); } -void OptionButton::set_item_ID(int p_idx,int p_ID) { +void OptionButton::set_item_ID(int p_idx, int p_ID) { - popup->set_item_ID(p_idx,p_ID); + popup->set_item_ID(p_idx, p_ID); } -void OptionButton::set_item_metadata(int p_idx,const Variant& p_metadata) { +void OptionButton::set_item_metadata(int p_idx, const Variant &p_metadata) { - popup->set_item_metadata(p_idx,p_metadata); + popup->set_item_metadata(p_idx, p_metadata); } -void OptionButton::set_item_disabled(int p_idx,bool p_disabled) { +void OptionButton::set_item_disabled(int p_idx, bool p_disabled) { - popup->set_item_disabled(p_idx,p_disabled); + popup->set_item_disabled(p_idx, p_disabled); } String OptionButton::get_item_text(int p_idx) const { @@ -159,8 +152,7 @@ bool OptionButton::is_item_disabled(int p_idx) const { return popup->is_item_disabled(p_idx); } - -int OptionButton::get_item_count() const { +int OptionButton::get_item_count() const { return popup->get_item_count(); } @@ -174,44 +166,41 @@ void OptionButton::clear() { popup->clear(); set_text(""); - current=-1; + current = -1; } -void OptionButton::_select(int p_idx,bool p_emit) { +void OptionButton::_select(int p_idx, bool p_emit) { - if (p_idx<0) + if (p_idx < 0) return; - if (p_idx==current) + if (p_idx == current) return; - ERR_FAIL_INDEX( p_idx, popup->get_item_count() ); + ERR_FAIL_INDEX(p_idx, popup->get_item_count()); - for (int i=0;i<popup->get_item_count();i++) { + for (int i = 0; i < popup->get_item_count(); i++) { - popup->set_item_checked(i,i==p_idx); + popup->set_item_checked(i, i == p_idx); } - - - current=p_idx; - set_text( popup->get_item_text( current ) ); - set_icon( popup->get_item_icon( current ) ); + current = p_idx; + set_text(popup->get_item_text(current)); + set_icon(popup->get_item_icon(current)); if (is_inside_tree() && p_emit) - emit_signal("item_selected",current); + emit_signal("item_selected", current); } void OptionButton::_select_int(int p_which) { - if (p_which<0 || p_which>=popup->get_item_count()) + if (p_which < 0 || p_which >= popup->get_item_count()) return; - _select(p_which,false); - + _select(p_which, false); } void OptionButton::select(int p_idx) { - _select(p_idx,false); + _select(p_idx, false); } int OptionButton::get_selected() const { @@ -222,17 +211,16 @@ int OptionButton::get_selected() const { int OptionButton::get_selected_ID() const { int idx = get_selected(); - if (idx<0) + if (idx < 0) return 0; return get_item_ID(current); } Variant OptionButton::get_selected_metadata() const { int idx = get_selected(); - if (idx<0) + if (idx < 0) return Variant(); return get_item_metadata(current); - } void OptionButton::remove_item(int p_idx) { @@ -243,7 +231,7 @@ void OptionButton::remove_item(int p_idx) { Array OptionButton::_get_items() const { Array items; - for(int i=0;i<get_item_count();i++) { + for (int i = 0; i < get_item_count(); i++) { items.push_back(get_item_text(i)); items.push_back(get_item_icon(i)); @@ -253,90 +241,78 @@ Array OptionButton::_get_items() const { } return items; - } -void OptionButton::_set_items(const Array& p_items){ +void OptionButton::_set_items(const Array &p_items) { ERR_FAIL_COND(p_items.size() % 5); clear(); - for(int i=0;i<p_items.size();i+=5) { + for (int i = 0; i < p_items.size(); i += 5) { - String text=p_items[i+0]; - Ref<Texture> icon=p_items[i+1]; - bool disabled=p_items[i+2]; - int id=p_items[i+3]; - Variant meta = p_items[i+4]; + String text = p_items[i + 0]; + Ref<Texture> icon = p_items[i + 1]; + bool disabled = p_items[i + 2]; + int id = p_items[i + 3]; + Variant meta = p_items[i + 4]; - int idx=get_item_count(); - add_item(text,id); - set_item_icon(idx,icon); - set_item_disabled(idx,disabled); - set_item_metadata(idx,meta); + int idx = get_item_count(); + add_item(text, id); + set_item_icon(idx, icon); + set_item_disabled(idx, disabled); + set_item_metadata(idx, meta); } - - } - void OptionButton::get_translatable_strings(List<String> *p_strings) const { return popup->get_translatable_strings(p_strings); } - void OptionButton::_bind_methods() { - ClassDB::bind_method(D_METHOD("_selected"),&OptionButton::_selected); - - ClassDB::bind_method(D_METHOD("add_item","label","id"),&OptionButton::add_item,DEFVAL(-1)); - ClassDB::bind_method(D_METHOD("add_icon_item","texture:Texture","label","id"),&OptionButton::add_icon_item); - ClassDB::bind_method(D_METHOD("set_item_text","idx","text"),&OptionButton::set_item_text); - ClassDB::bind_method(D_METHOD("set_item_icon","idx","texture:Texture"),&OptionButton::set_item_icon); - ClassDB::bind_method(D_METHOD("set_item_disabled","idx","disabled"),&OptionButton::set_item_disabled); - ClassDB::bind_method(D_METHOD("set_item_ID","idx","id"),&OptionButton::set_item_ID); - ClassDB::bind_method(D_METHOD("set_item_metadata","idx","metadata"),&OptionButton::set_item_metadata); - ClassDB::bind_method(D_METHOD("get_item_text","idx"),&OptionButton::get_item_text); - ClassDB::bind_method(D_METHOD("get_item_icon:Texture","idx"),&OptionButton::get_item_icon); - ClassDB::bind_method(D_METHOD("get_item_ID","idx"),&OptionButton::get_item_ID); - ClassDB::bind_method(D_METHOD("get_item_metadata","idx"),&OptionButton::get_item_metadata); - ClassDB::bind_method(D_METHOD("is_item_disabled","idx"),&OptionButton::is_item_disabled); - ClassDB::bind_method(D_METHOD("get_item_count"),&OptionButton::get_item_count); - ClassDB::bind_method(D_METHOD("add_separator"),&OptionButton::add_separator); - ClassDB::bind_method(D_METHOD("clear"),&OptionButton::clear); - ClassDB::bind_method(D_METHOD("select","idx"),&OptionButton::select); - ClassDB::bind_method(D_METHOD("get_selected"),&OptionButton::get_selected); - ClassDB::bind_method(D_METHOD("get_selected_ID"),&OptionButton::get_selected_ID); - ClassDB::bind_method(D_METHOD("get_selected_metadata"),&OptionButton::get_selected_metadata); - ClassDB::bind_method(D_METHOD("remove_item","idx"),&OptionButton::remove_item); - ClassDB::bind_method(D_METHOD("_select_int"),&OptionButton::_select_int); - - ClassDB::bind_method(D_METHOD("_set_items"),&OptionButton::_set_items); - ClassDB::bind_method(D_METHOD("_get_items"),&OptionButton::_get_items); - - ADD_PROPERTY( PropertyInfo(Variant::INT,"selected"), "_select_int","get_selected") ; - ADD_PROPERTY( PropertyInfo(Variant::ARRAY,"items",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR), "_set_items","_get_items") ; - ADD_SIGNAL( MethodInfo("item_selected", PropertyInfo( Variant::INT,"ID") ) ); + ClassDB::bind_method(D_METHOD("_selected"), &OptionButton::_selected); + + ClassDB::bind_method(D_METHOD("add_item", "label", "id"), &OptionButton::add_item, DEFVAL(-1)); + ClassDB::bind_method(D_METHOD("add_icon_item", "texture:Texture", "label", "id"), &OptionButton::add_icon_item); + ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &OptionButton::set_item_text); + ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "texture:Texture"), &OptionButton::set_item_icon); + ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &OptionButton::set_item_disabled); + ClassDB::bind_method(D_METHOD("set_item_ID", "idx", "id"), &OptionButton::set_item_ID); + ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &OptionButton::set_item_metadata); + ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &OptionButton::get_item_text); + ClassDB::bind_method(D_METHOD("get_item_icon:Texture", "idx"), &OptionButton::get_item_icon); + ClassDB::bind_method(D_METHOD("get_item_ID", "idx"), &OptionButton::get_item_ID); + ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &OptionButton::get_item_metadata); + ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &OptionButton::is_item_disabled); + ClassDB::bind_method(D_METHOD("get_item_count"), &OptionButton::get_item_count); + ClassDB::bind_method(D_METHOD("add_separator"), &OptionButton::add_separator); + ClassDB::bind_method(D_METHOD("clear"), &OptionButton::clear); + ClassDB::bind_method(D_METHOD("select", "idx"), &OptionButton::select); + ClassDB::bind_method(D_METHOD("get_selected"), &OptionButton::get_selected); + ClassDB::bind_method(D_METHOD("get_selected_ID"), &OptionButton::get_selected_ID); + ClassDB::bind_method(D_METHOD("get_selected_metadata"), &OptionButton::get_selected_metadata); + ClassDB::bind_method(D_METHOD("remove_item", "idx"), &OptionButton::remove_item); + ClassDB::bind_method(D_METHOD("_select_int"), &OptionButton::_select_int); + + ClassDB::bind_method(D_METHOD("_set_items"), &OptionButton::_set_items); + ClassDB::bind_method(D_METHOD("_get_items"), &OptionButton::_get_items); + + ADD_PROPERTY(PropertyInfo(Variant::INT, "selected"), "_select_int", "get_selected"); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "items", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_items", "_get_items"); + ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "ID"))); } OptionButton::OptionButton() { - - popup = memnew( PopupMenu ); + popup = memnew(PopupMenu); popup->hide(); popup->set_as_toplevel(true); add_child(popup); - popup->connect("id_pressed", this,"_selected"); + popup->connect("id_pressed", this, "_selected"); - current=-1; + current = -1; set_text_align(ALIGN_LEFT); } - OptionButton::~OptionButton() { - - - } - - diff --git a/scene/gui/option_button.h b/scene/gui/option_button.h index 681cb5a088..55a98cb50c 100644 --- a/scene/gui/option_button.h +++ b/scene/gui/option_button.h @@ -36,34 +36,34 @@ */ class OptionButton : public Button { - GDCLASS( OptionButton, Button ); + GDCLASS(OptionButton, Button); PopupMenu *popup; int current; void _selected(int p_which); - void _select(int p_which,bool p_emit=false); + void _select(int p_which, bool p_emit = false); void _select_int(int p_which); Array _get_items() const; - void _set_items(const Array& p_items); + void _set_items(const Array &p_items); virtual void pressed(); -protected: +protected: Size2 get_minimum_size() const; void _notification(int p_what); static void _bind_methods(); -public: - void add_icon_item(const Ref<Texture>& p_icon,const String& p_label,int p_ID=-1); - void add_item(const String& p_label,int p_ID=-1); +public: + void add_icon_item(const Ref<Texture> &p_icon, const String &p_label, int p_ID = -1); + void add_item(const String &p_label, int p_ID = -1); - void set_item_text(int p_idx,const String& p_text); - void set_item_icon(int p_idx,const Ref<Texture>& p_icon); - void set_item_ID(int p_idx,int p_ID); - void set_item_metadata(int p_idx,const Variant& p_metadata); - void set_item_disabled(int p_idx,bool p_disabled); + void set_item_text(int p_idx, const String &p_text); + void set_item_icon(int p_idx, const Ref<Texture> &p_icon); + void set_item_ID(int p_idx, int p_ID); + void set_item_metadata(int p_idx, const Variant &p_metadata); + void set_item_disabled(int p_idx, bool p_disabled); String get_item_text(int p_idx) const; Ref<Texture> get_item_icon(int p_idx) const; @@ -71,7 +71,6 @@ public: Variant get_item_metadata(int p_idx) const; bool is_item_disabled(int p_idx) const; - int get_item_count() const; void add_separator(); @@ -89,7 +88,6 @@ public: OptionButton(); ~OptionButton(); - }; #endif diff --git a/scene/gui/panel.cpp b/scene/gui/panel.cpp index c4b7199c3e..536f0baf73 100644 --- a/scene/gui/panel.cpp +++ b/scene/gui/panel.cpp @@ -31,11 +31,11 @@ void Panel::_notification(int p_what) { - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { RID ci = get_canvas_item(); Ref<StyleBox> style = get_stylebox("panel"); - style->draw( ci, Rect2( Point2(), get_size() ) ); + style->draw(ci, Rect2(Point2(), get_size())); } } @@ -44,9 +44,5 @@ Panel::Panel() { set_mouse_filter(MOUSE_FILTER_STOP); } - -Panel::~Panel() -{ +Panel::~Panel() { } - - diff --git a/scene/gui/panel.h b/scene/gui/panel.h index 34c73960e7..3d2c0a3e98 100644 --- a/scene/gui/panel.h +++ b/scene/gui/panel.h @@ -33,18 +33,16 @@ /** @author Juan Linietsky <reduzio@gmail.com> */ -class Panel : public Control{ +class Panel : public Control { - GDCLASS(Panel,Control); -protected: + GDCLASS(Panel, Control); +protected: void _notification(int p_what); + public: Panel(); ~Panel(); - }; - - #endif diff --git a/scene/gui/panel_container.cpp b/scene/gui/panel_container.cpp index 48270d12c7..9dc7a6b32e 100644 --- a/scene/gui/panel_container.cpp +++ b/scene/gui/panel_container.cpp @@ -28,19 +28,17 @@ /*************************************************************************/ #include "panel_container.h" - Size2 PanelContainer::get_minimum_size() const { Ref<StyleBox> style; if (has_stylebox("panel")) - style=get_stylebox("panel"); + style = get_stylebox("panel"); else - style=get_stylebox("panel","PanelContainer"); - + style = get_stylebox("panel", "PanelContainer"); Size2 ms; - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { Control *c = get_child(i)->cast_to<Control>(); if (!c || !c->is_visible_in_tree()) @@ -49,53 +47,47 @@ Size2 PanelContainer::get_minimum_size() const { continue; Size2 minsize = c->get_combined_minimum_size(); - ms.width = MAX(ms.width , minsize.width); - ms.height = MAX(ms.height , minsize.height); - - + ms.width = MAX(ms.width, minsize.width); + ms.height = MAX(ms.height, minsize.height); } if (style.is_valid()) - ms+=style->get_minimum_size(); + ms += style->get_minimum_size(); return ms; - } void PanelContainer::_notification(int p_what) { - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { RID ci = get_canvas_item(); Ref<StyleBox> style; if (has_stylebox("panel")) - style=get_stylebox("panel"); + style = get_stylebox("panel"); else - style=get_stylebox("panel","PanelContainer"); - - style->draw( ci, Rect2( Point2(), get_size() ) ); + style = get_stylebox("panel", "PanelContainer"); + style->draw(ci, Rect2(Point2(), get_size())); } - if (p_what==NOTIFICATION_SORT_CHILDREN) { + if (p_what == NOTIFICATION_SORT_CHILDREN) { Ref<StyleBox> style; if (has_stylebox("panel")) - style=get_stylebox("panel"); + style = get_stylebox("panel"); else - style=get_stylebox("panel","PanelContainer"); + style = get_stylebox("panel", "PanelContainer"); Size2 size = get_size(); Point2 ofs; if (style.is_valid()) { - size-=style->get_minimum_size(); - ofs+=style->get_offset(); + size -= style->get_minimum_size(); + ofs += style->get_offset(); } - - - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { Control *c = get_child(i)->cast_to<Control>(); if (!c || !c->is_visible_in_tree()) @@ -103,12 +95,10 @@ void PanelContainer::_notification(int p_what) { if (c->is_set_as_toplevel()) continue; - fit_child_in_rect(c,Rect2(ofs,size)); - + fit_child_in_rect(c, Rect2(ofs, size)); } } } -PanelContainer::PanelContainer() -{ +PanelContainer::PanelContainer() { } diff --git a/scene/gui/panel_container.h b/scene/gui/panel_container.h index 86f390fdf3..13ed1c935c 100644 --- a/scene/gui/panel_container.h +++ b/scene/gui/panel_container.h @@ -33,13 +33,12 @@ class PanelContainer : public Container { - GDCLASS( PanelContainer, Container ); + GDCLASS(PanelContainer, Container); protected: - void _notification(int p_what); -public: +public: virtual Size2 get_minimum_size() const; PanelContainer(); diff --git a/scene/gui/patch_9_rect.cpp b/scene/gui/patch_9_rect.cpp index 6fb35f72e5..d0bd45e435 100644 --- a/scene/gui/patch_9_rect.cpp +++ b/scene/gui/patch_9_rect.cpp @@ -32,17 +32,17 @@ void NinePatchRect::_notification(int p_what) { - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { if (texture.is_null()) return; - Size2 s=get_size(); + Size2 s = get_size(); RID ci = get_canvas_item(); - VS::get_singleton()->canvas_item_add_nine_patch(ci,Rect2(Point2(),s),region_rect,texture->get_rid(),Vector2(margin[MARGIN_LEFT],margin[MARGIN_TOP]),Vector2(margin[MARGIN_RIGHT],margin[MARGIN_BOTTOM]),VS::NINE_PATCH_STRETCH,VS::NINE_PATCH_STRETCH,draw_center); + VS::get_singleton()->canvas_item_add_nine_patch(ci, Rect2(Point2(), s), region_rect, texture->get_rid(), Vector2(margin[MARGIN_LEFT], margin[MARGIN_TOP]), Vector2(margin[MARGIN_RIGHT], margin[MARGIN_BOTTOM]), VS::NINE_PATCH_STRETCH, VS::NINE_PATCH_STRETCH, draw_center); //draw_texture_rect(texture,Rect2(Point2(),s),false,modulate); -/* + /* Vector<Point2> points; points.resize(4); points[0]=Point2(0,0); @@ -63,40 +63,37 @@ void NinePatchRect::_notification(int p_what) { Size2 NinePatchRect::get_minimum_size() const { - return Size2(margin[MARGIN_LEFT]+margin[MARGIN_RIGHT],margin[MARGIN_TOP]+margin[MARGIN_BOTTOM]); + return Size2(margin[MARGIN_LEFT] + margin[MARGIN_RIGHT], margin[MARGIN_TOP] + margin[MARGIN_BOTTOM]); } void NinePatchRect::_bind_methods() { - - ClassDB::bind_method(D_METHOD("set_texture","texture"), & NinePatchRect::set_texture ); - ClassDB::bind_method(D_METHOD("get_texture"), & NinePatchRect::get_texture ); - ClassDB::bind_method(D_METHOD("set_patch_margin","margin","value"), & NinePatchRect::set_patch_margin ); - ClassDB::bind_method(D_METHOD("get_patch_margin","margin"), & NinePatchRect::get_patch_margin ); - ClassDB::bind_method(D_METHOD("set_region_rect","rect"),&NinePatchRect::set_region_rect); - ClassDB::bind_method(D_METHOD("get_region_rect"),&NinePatchRect::get_region_rect); - ClassDB::bind_method(D_METHOD("set_draw_center","draw_center"), & NinePatchRect::set_draw_center ); - ClassDB::bind_method(D_METHOD("get_draw_center"), & NinePatchRect::get_draw_center ); + ClassDB::bind_method(D_METHOD("set_texture", "texture"), &NinePatchRect::set_texture); + ClassDB::bind_method(D_METHOD("get_texture"), &NinePatchRect::get_texture); + ClassDB::bind_method(D_METHOD("set_patch_margin", "margin", "value"), &NinePatchRect::set_patch_margin); + ClassDB::bind_method(D_METHOD("get_patch_margin", "margin"), &NinePatchRect::get_patch_margin); + ClassDB::bind_method(D_METHOD("set_region_rect", "rect"), &NinePatchRect::set_region_rect); + ClassDB::bind_method(D_METHOD("get_region_rect"), &NinePatchRect::get_region_rect); + ClassDB::bind_method(D_METHOD("set_draw_center", "draw_center"), &NinePatchRect::set_draw_center); + ClassDB::bind_method(D_METHOD("get_draw_center"), &NinePatchRect::get_draw_center); ADD_SIGNAL(MethodInfo("texture_changed")); - ADD_PROPERTYNZ( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture","get_texture") ; - ADD_PROPERTYNO( PropertyInfo( Variant::BOOL, "draw_center"), "set_draw_center","get_draw_center") ; - ADD_PROPERTYNZ( PropertyInfo( Variant::RECT2, "region_rect"), "set_region_rect","get_region_rect"); - - ADD_GROUP("Patch Margin","patch_margin_"); - ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin_left",PROPERTY_HINT_RANGE,"0,16384,1"), "set_patch_margin","get_patch_margin",MARGIN_LEFT ); - ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin_top",PROPERTY_HINT_RANGE,"0,16384,1"), "set_patch_margin","get_patch_margin",MARGIN_TOP ); - ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin_right",PROPERTY_HINT_RANGE,"0,16384,1"), "set_patch_margin","get_patch_margin",MARGIN_RIGHT ); - ADD_PROPERTYINZ( PropertyInfo( Variant::INT, "patch_margin_bottom",PROPERTY_HINT_RANGE,"0,16384,1"), "set_patch_margin","get_patch_margin",MARGIN_BOTTOM ); + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture"); + ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "get_draw_center"); + ADD_PROPERTYNZ(PropertyInfo(Variant::RECT2, "region_rect"), "set_region_rect", "get_region_rect"); + ADD_GROUP("Patch Margin", "patch_margin_"); + ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "patch_margin_left", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", MARGIN_LEFT); + ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "patch_margin_top", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", MARGIN_TOP); + ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "patch_margin_right", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", MARGIN_RIGHT); + ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "patch_margin_bottom", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", MARGIN_BOTTOM); } +void NinePatchRect::set_texture(const Ref<Texture> &p_tex) { -void NinePatchRect::set_texture(const Ref<Texture>& p_tex) { - - if (texture==p_tex) + if (texture == p_tex) return; - texture=p_tex; + texture = p_tex; update(); /* if (texture.is_valid()) @@ -111,12 +108,10 @@ Ref<Texture> NinePatchRect::get_texture() const { return texture; } +void NinePatchRect::set_patch_margin(Margin p_margin, int p_size) { - -void NinePatchRect::set_patch_margin(Margin p_margin,int p_size) { - - ERR_FAIL_INDEX(p_margin,4); - margin[p_margin]=p_size; + ERR_FAIL_INDEX(p_margin, 4); + margin[p_margin] = p_size; update(); minimum_size_changed(); switch (p_margin) { @@ -135,18 +130,18 @@ void NinePatchRect::set_patch_margin(Margin p_margin,int p_size) { } } -int NinePatchRect::get_patch_margin(Margin p_margin) const{ +int NinePatchRect::get_patch_margin(Margin p_margin) const { - ERR_FAIL_INDEX_V(p_margin,4,0); + ERR_FAIL_INDEX_V(p_margin, 4, 0); return margin[p_margin]; } -void NinePatchRect::set_region_rect(const Rect2& p_region_rect) { +void NinePatchRect::set_region_rect(const Rect2 &p_region_rect) { - if (region_rect==p_region_rect) + if (region_rect == p_region_rect) return; - region_rect=p_region_rect; + region_rect = p_region_rect; item_rect_changed(); _change_notify("region_rect"); @@ -159,28 +154,25 @@ Rect2 NinePatchRect::get_region_rect() const { void NinePatchRect::set_draw_center(bool p_draw) { - draw_center=p_draw; + draw_center = p_draw; update(); } -bool NinePatchRect::get_draw_center() const{ +bool NinePatchRect::get_draw_center() const { return draw_center; } NinePatchRect::NinePatchRect() { - - margin[MARGIN_LEFT]=0; - margin[MARGIN_RIGHT]=0; - margin[MARGIN_BOTTOM]=0; - margin[MARGIN_TOP]=0; + margin[MARGIN_LEFT] = 0; + margin[MARGIN_RIGHT] = 0; + margin[MARGIN_BOTTOM] = 0; + margin[MARGIN_TOP] = 0; set_mouse_filter(MOUSE_FILTER_IGNORE); - draw_center=true; + draw_center = true; } - -NinePatchRect::~NinePatchRect() -{ +NinePatchRect::~NinePatchRect() { } diff --git a/scene/gui/patch_9_rect.h b/scene/gui/patch_9_rect.h index b87f2f64ec..09663e62f6 100644 --- a/scene/gui/patch_9_rect.h +++ b/scene/gui/patch_9_rect.h @@ -35,27 +35,26 @@ */ class NinePatchRect : public Control { - GDCLASS(NinePatchRect,Control); + GDCLASS(NinePatchRect, Control); bool draw_center; int margin[4]; Rect2 region_rect; Ref<Texture> texture; -protected: +protected: void _notification(int p_what); virtual Size2 get_minimum_size() const; static void _bind_methods(); public: - - void set_texture(const Ref<Texture>& p_tex); + void set_texture(const Ref<Texture> &p_tex); Ref<Texture> get_texture() const; - void set_patch_margin(Margin p_margin,int p_size); + void set_patch_margin(Margin p_margin, int p_size); int get_patch_margin(Margin p_margin) const; - void set_region_rect(const Rect2& p_region_rect); + void set_region_rect(const Rect2 &p_region_rect); Rect2 get_region_rect() const; void set_draw_center(bool p_enable); @@ -63,6 +62,5 @@ public: NinePatchRect(); ~NinePatchRect(); - }; #endif // PATCH_9_FRAME_H diff --git a/scene/gui/popup.cpp b/scene/gui/popup.cpp index 1f0daa99ba..52ad37e9ab 100644 --- a/scene/gui/popup.cpp +++ b/scene/gui/popup.cpp @@ -29,18 +29,14 @@ #include "popup.h" #include "os/keyboard.h" - - void Popup::_gui_input(InputEvent p_event) { - - } void Popup::_notification(int p_what) { - if (p_what==NOTIFICATION_VISIBILITY_CHANGED) { + if (p_what == NOTIFICATION_VISIBILITY_CHANGED) { if (popped_up && !is_visible_in_tree()) { - popped_up=false; + popped_up = false; notification(NOTIFICATION_POPUP_HIDE); emit_signal("popup_hide"); } @@ -48,20 +44,18 @@ void Popup::_notification(int p_what) { update_configuration_warning(); } - if (p_what==NOTIFICATION_ENTER_TREE) { - //small helper to make editing of these easier in editor + if (p_what == NOTIFICATION_ENTER_TREE) { +//small helper to make editing of these easier in editor #ifdef TOOLS_ENABLED if (get_tree()->is_editor_hint() && get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root()->is_a_parent_of(this)) { set_as_toplevel(false); } #endif } - } void Popup::_fix_size() { - #if 0 Point2 pos = get_pos(); Size2 size = get_size(); @@ -73,34 +67,32 @@ void Popup::_fix_size() { Point2 window_size = get_viewport_rect().size; #endif - if (pos.x+size.width > window_size.width) - pos.x=window_size.width-size.width; - if (pos.x<0) - pos.x=0; - - if (pos.y+size.height > window_size.height) - pos.y=window_size.height-size.height; - if (pos.y<0) - pos.y=0; + if (pos.x + size.width > window_size.width) + pos.x = window_size.width - size.width; + if (pos.x < 0) + pos.x = 0; + + if (pos.y + size.height > window_size.height) + pos.y = window_size.height - size.height; + if (pos.y < 0) + pos.y = 0; #if 0 if (pos!=get_pos()) set_pos(pos); #else - if (pos!=get_pos()) + if (pos != get_pos()) set_global_pos(pos); #endif - } - void Popup::set_as_minsize() { Size2 total_minsize; - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { - Control *c=get_child(i)->cast_to<Control>(); + Control *c = get_child(i)->cast_to<Control>(); if (!c) continue; if (!c->is_visible()) @@ -108,10 +100,10 @@ void Popup::set_as_minsize() { Size2 minsize = c->get_combined_minimum_size(); - for(int j=0;j<2;j++) { + for (int j = 0; j < 2; j++) { - Margin m_beg = Margin(0+j); - Margin m_end = Margin(2+j); + Margin m_beg = Margin(0 + j); + Margin m_end = Margin(2 + j); float margin_begin = c->get_margin(m_beg); float margin_end = c->get_margin(m_end); @@ -119,29 +111,25 @@ void Popup::set_as_minsize() { AnchorType anchor_end = c->get_anchor(m_end); if (anchor_begin == ANCHOR_BEGIN) - minsize[j]+=margin_begin; + minsize[j] += margin_begin; if (anchor_end == ANCHOR_END) - minsize[j]+=margin_end; - + minsize[j] += margin_end; } - total_minsize.width = MAX( total_minsize.width, minsize.width ); - total_minsize.height = MAX( total_minsize.height, minsize.height ); + total_minsize.width = MAX(total_minsize.width, minsize.width); + total_minsize.height = MAX(total_minsize.height, minsize.height); } set_size(total_minsize); - } +void Popup::popup_centered_minsize(const Size2 &p_minsize) { -void Popup::popup_centered_minsize(const Size2& p_minsize) { - - - Size2 total_minsize=p_minsize; + Size2 total_minsize = p_minsize; - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { - Control *c=get_child(i)->cast_to<Control>(); + Control *c = get_child(i)->cast_to<Control>(); if (!c) continue; if (!c->is_visible()) @@ -149,10 +137,10 @@ void Popup::popup_centered_minsize(const Size2& p_minsize) { Size2 minsize = c->get_combined_minimum_size(); - for(int j=0;j<2;j++) { + for (int j = 0; j < 2; j++) { - Margin m_beg = Margin(0+j); - Margin m_end = Margin(2+j); + Margin m_beg = Margin(0 + j); + Margin m_end = Margin(2 + j); float margin_begin = c->get_margin(m_beg); float margin_end = c->get_margin(m_end); @@ -160,33 +148,30 @@ void Popup::popup_centered_minsize(const Size2& p_minsize) { AnchorType anchor_end = c->get_anchor(m_end); if (anchor_begin == ANCHOR_BEGIN) - minsize[j]+=margin_begin; + minsize[j] += margin_begin; if (anchor_end == ANCHOR_END) - minsize[j]+=margin_end; - + minsize[j] += margin_end; } - total_minsize.width = MAX( total_minsize.width, minsize.width ); - total_minsize.height = MAX( total_minsize.height, minsize.height ); + total_minsize.width = MAX(total_minsize.width, minsize.width); + total_minsize.height = MAX(total_minsize.height, minsize.height); } - - popup_centered( total_minsize ); - popped_up=true; - + popup_centered(total_minsize); + popped_up = true; } -void Popup::popup_centered(const Size2& p_size) { +void Popup::popup_centered(const Size2 &p_size) { Point2 window_size = get_viewport_rect().size; emit_signal("about_to_show"); Rect2 rect; - rect.size = p_size==Size2()?get_size():p_size; + rect.size = p_size == Size2() ? get_size() : p_size; - rect.pos = ((window_size-rect.size)/2.0).floor(); - set_pos( rect.pos ); - set_size( rect.size ); + rect.pos = ((window_size - rect.size) / 2.0).floor(); + set_pos(rect.pos); + set_size(rect.size); show_modal(exclusive); _fix_size(); @@ -197,21 +182,19 @@ void Popup::popup_centered(const Size2& p_size) { _post_popup(); notification(NOTIFICATION_POST_POPUP); - popped_up=true; + popped_up = true; } void Popup::popup_centered_ratio(float p_screen_ratio) { - - emit_signal("about_to_show"); Rect2 rect; Point2 window_size = get_viewport_rect().size; rect.size = (window_size * p_screen_ratio).floor(); - rect.pos = ((window_size-rect.size)/2.0).floor(); - set_pos( rect.pos ); - set_size( rect.size ); + rect.pos = ((window_size - rect.size) / 2.0).floor(); + set_pos(rect.pos); + set_size(rect.size); show_modal(exclusive); _fix_size(); @@ -222,11 +205,10 @@ void Popup::popup_centered_ratio(float p_screen_ratio) { _post_popup(); notification(NOTIFICATION_POST_POPUP); - popped_up=true; - + popped_up = true; } -void Popup::popup(const Rect2& bounds) { +void Popup::popup(const Rect2 &bounds) { emit_signal("about_to_show"); show_modal(exclusive); @@ -245,43 +227,40 @@ void Popup::popup(const Rect2& bounds) { _post_popup(); notification(NOTIFICATION_POST_POPUP); - popped_up=true; + popped_up = true; } void Popup::set_exclusive(bool p_exclusive) { - exclusive=p_exclusive; + exclusive = p_exclusive; } -bool Popup::is_exclusive() const { +bool Popup::is_exclusive() const { return exclusive; } - void Popup::_bind_methods() { - ClassDB::bind_method(D_METHOD("popup_centered","size"),&Popup::popup_centered,DEFVAL(Size2())); - ClassDB::bind_method(D_METHOD("popup_centered_ratio","ratio"),&Popup::popup_centered_ratio,DEFVAL(0.75)); - ClassDB::bind_method(D_METHOD("popup_centered_minsize","minsize"),&Popup::popup_centered_minsize,DEFVAL(Size2())); - ClassDB::bind_method(D_METHOD("popup","bounds"),&Popup::popup,DEFVAL(Rect2())); - ClassDB::bind_method(D_METHOD("set_exclusive","enable"),&Popup::set_exclusive); - ClassDB::bind_method(D_METHOD("is_exclusive"),&Popup::is_exclusive); - ADD_SIGNAL( MethodInfo("about_to_show") ); - ADD_SIGNAL( MethodInfo("popup_hide") ); - ADD_GROUP("Popup","popup_"); - ADD_PROPERTY( PropertyInfo( Variant::BOOL, "popup_exclusive"), "set_exclusive","is_exclusive") ; + ClassDB::bind_method(D_METHOD("popup_centered", "size"), &Popup::popup_centered, DEFVAL(Size2())); + ClassDB::bind_method(D_METHOD("popup_centered_ratio", "ratio"), &Popup::popup_centered_ratio, DEFVAL(0.75)); + ClassDB::bind_method(D_METHOD("popup_centered_minsize", "minsize"), &Popup::popup_centered_minsize, DEFVAL(Size2())); + ClassDB::bind_method(D_METHOD("popup", "bounds"), &Popup::popup, DEFVAL(Rect2())); + ClassDB::bind_method(D_METHOD("set_exclusive", "enable"), &Popup::set_exclusive); + ClassDB::bind_method(D_METHOD("is_exclusive"), &Popup::is_exclusive); + ADD_SIGNAL(MethodInfo("about_to_show")); + ADD_SIGNAL(MethodInfo("popup_hide")); + ADD_GROUP("Popup", "popup_"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "popup_exclusive"), "set_exclusive", "is_exclusive"); BIND_CONSTANT(NOTIFICATION_POST_POPUP); BIND_CONSTANT(NOTIFICATION_POPUP_HIDE); - - } Popup::Popup() { set_as_toplevel(true); - exclusive=false; - popped_up=false; + exclusive = false; + popped_up = false; hide(); } @@ -294,9 +273,7 @@ String Popup::get_configuration_warning() const { return String(); } -Popup::~Popup() -{ - +Popup::~Popup() { } void PopupPanel::set_child_rect(Control *p_child) { @@ -304,21 +281,18 @@ void PopupPanel::set_child_rect(Control *p_child) { Ref<StyleBox> p = get_stylebox("panel"); p_child->set_area_as_parent_rect(); - for(int i=0;i<4;i++) { - p_child->set_margin(Margin(i),p->get_margin(Margin(i))); + for (int i = 0; i < 4; i++) { + p_child->set_margin(Margin(i), p->get_margin(Margin(i))); } } void PopupPanel::_notification(int p_what) { + if (p_what == NOTIFICATION_DRAW) { - if (p_what==NOTIFICATION_DRAW) { - - get_stylebox("panel")->draw(get_canvas_item(),Rect2(Point2(),get_size())); + get_stylebox("panel")->draw(get_canvas_item(), Rect2(Point2(), get_size())); } } PopupPanel::PopupPanel() { - - } diff --git a/scene/gui/popup.h b/scene/gui/popup.h index 4e4c8b0292..d80daaad10 100644 --- a/scene/gui/popup.h +++ b/scene/gui/popup.h @@ -36,56 +36,50 @@ */ class Popup : public Control { - GDCLASS( Popup, Control ); + GDCLASS(Popup, Control); bool exclusive; bool popped_up; protected: - virtual void _post_popup() {} void _gui_input(InputEvent p_event); void _notification(int p_what); virtual void _fix_size(); static void _bind_methods(); -public: +public: enum { - NOTIFICATION_POST_POPUP=80, - NOTIFICATION_POPUP_HIDE=81 + NOTIFICATION_POST_POPUP = 80, + NOTIFICATION_POPUP_HIDE = 81 }; void set_exclusive(bool p_exclusive); bool is_exclusive() const; - void popup_centered_ratio(float p_screen_ratio=0.75); - void popup_centered(const Size2& p_size=Size2()); - void popup_centered_minsize(const Size2& p_minsize=Size2()); + void popup_centered_ratio(float p_screen_ratio = 0.75); + void popup_centered(const Size2 &p_size = Size2()); + void popup_centered_minsize(const Size2 &p_minsize = Size2()); void set_as_minsize(); - virtual void popup(const Rect2& p_bounds=Rect2()); + virtual void popup(const Rect2 &p_bounds = Rect2()); virtual String get_configuration_warning() const; Popup(); ~Popup(); - }; class PopupPanel : public Popup { - GDCLASS(PopupPanel,Popup); - + GDCLASS(PopupPanel, Popup); protected: - void _notification(int p_what); -public: +public: void set_child_rect(Control *p_child); PopupPanel(); - }; - #endif diff --git a/scene/gui/popup_menu.cpp b/scene/gui/popup_menu.cpp index 884cf0312a..a2a670fa1f 100644 --- a/scene/gui/popup_menu.cpp +++ b/scene/gui/popup_menu.cpp @@ -27,14 +27,14 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "popup_menu.h" -#include "print_string.h" +#include "os/input.h" #include "os/keyboard.h" +#include "print_string.h" #include "translation.h" -#include "os/input.h" String PopupMenu::_get_accel_text(int p_item) const { - ERR_FAIL_INDEX_V(p_item,items.size(),String()); + ERR_FAIL_INDEX_V(p_item, items.size(), String()); if (items[p_item].shortcut.is_valid()) return items[p_item].shortcut->get_as_text(); @@ -61,7 +61,6 @@ String PopupMenu::_get_accel_text(int p_item) const { */ } - Size2 PopupMenu::get_minimum_size() const { int vseparation = get_constant("vseparation"); @@ -70,123 +69,114 @@ Size2 PopupMenu::get_minimum_size() const { Size2 minsize = get_stylebox("panel")->get_minimum_size(); Ref<Font> font = get_font("font"); - float max_w=0; + float max_w = 0; int font_h = font->get_height(); int check_w = get_icon("checked")->get_width(); - int accel_max_w=0; + int accel_max_w = 0; - for (int i=0;i<items.size();i++) { + for (int i = 0; i < items.size(); i++) { Size2 size; if (!items[i].icon.is_null()) { Size2 icon_size = items[i].icon->get_size(); - size.height = MAX( icon_size.height, font_h ); - size.width+=icon_size.width; - size.width+=hseparation; + size.height = MAX(icon_size.height, font_h); + size.width += icon_size.width; + size.width += hseparation; } else { - size.height=font_h; + size.height = font_h; } - size.width+=items[i].h_ofs; + size.width += items[i].h_ofs; if (items[i].checkable) { - size.width+=check_w+hseparation; + size.width += check_w + hseparation; } String text = items[i].shortcut.is_valid() ? String(tr(items[i].shortcut->get_name())) : items[i].xl_text; - size.width+=font->get_string_size(text).width; - if (i>0) - size.height+=vseparation; + size.width += font->get_string_size(text).width; + if (i > 0) + size.height += vseparation; if (items[i].accel || (items[i].shortcut.is_valid() && items[i].shortcut->is_valid())) { - int accel_w = hseparation*2; - accel_w+=font->get_string_size(_get_accel_text(i)).width; - accel_max_w = MAX( accel_w, accel_max_w ); + int accel_w = hseparation * 2; + accel_w += font->get_string_size(_get_accel_text(i)).width; + accel_max_w = MAX(accel_w, accel_max_w); } - - minsize.height+=size.height; - max_w = MAX( max_w, size.width ); - + minsize.height += size.height; + max_w = MAX(max_w, size.width); } - minsize.width+=max_w+accel_max_w; + minsize.width += max_w + accel_max_w; return minsize; } -int PopupMenu::_get_mouse_over(const Point2& p_over) const { - +int PopupMenu::_get_mouse_over(const Point2 &p_over) const { - if (p_over.x<0 || p_over.x>=get_size().width) + if (p_over.x < 0 || p_over.x >= get_size().width) return -1; Ref<StyleBox> style = get_stylebox("panel"); - Point2 ofs=style->get_offset(); - + Point2 ofs = style->get_offset(); - if (ofs.y>p_over.y) + if (ofs.y > p_over.y) return -1; - Ref<Font> font = get_font("font"); int vseparation = get_constant("vseparation"); //int hseparation = get_constant("hseparation"); - float font_h=font->get_height(); - + float font_h = font->get_height(); - for (int i=0;i<items.size();i++) { + for (int i = 0; i < items.size(); i++) { - if (i>0) - ofs.y+=vseparation; + if (i > 0) + ofs.y += vseparation; float h; if (!items[i].icon.is_null()) { Size2 icon_size = items[i].icon->get_size(); - h = MAX( icon_size.height, font_h ); + h = MAX(icon_size.height, font_h); } else { - h=font_h; + h = font_h; } - ofs.y+=h; + ofs.y += h; if (p_over.y < ofs.y) { return i; } } return -1; - } - void PopupMenu::_activate_submenu(int over) { - Node* n = get_node(items[over].submenu); - ERR_EXPLAIN("item subnode does not exist: "+items[over].submenu); + Node *n = get_node(items[over].submenu); + ERR_EXPLAIN("item subnode does not exist: " + items[over].submenu); ERR_FAIL_COND(!n); Popup *pm = n->cast_to<Popup>(); - ERR_EXPLAIN("item subnode is not a Popup: "+items[over].submenu); + ERR_EXPLAIN("item subnode is not a Popup: " + items[over].submenu); ERR_FAIL_COND(!pm); if (pm->is_visible_in_tree()) return; //already visible! - Point2 p = get_global_pos(); - Rect2 pr(p,get_size()); + Rect2 pr(p, get_size()); Ref<StyleBox> style = get_stylebox("panel"); - Point2 pos = p+Point2(get_size().width,items[over]._ofs_cache-style->get_offset().y); + Point2 pos = p + Point2(get_size().width, items[over]._ofs_cache - style->get_offset().y); Size2 size = pm->get_size(); // fix pos - if (pos.x+size.width > get_viewport_rect().size.width) - pos.x=p.x-size.width; + if (pos.x + size.width > get_viewport_rect().size.width) + pos.x = p.x - size.width; pm->set_pos(pos); pm->popup(); @@ -194,52 +184,45 @@ void PopupMenu::_activate_submenu(int over) { PopupMenu *pum = pm->cast_to<PopupMenu>(); if (pum) { - pr.pos-=pum->get_global_pos(); + pr.pos -= pum->get_global_pos(); pum->clear_autohide_areas(); - pum->add_autohide_area(Rect2(pr.pos.x,pr.pos.y,pr.size.x,items[over]._ofs_cache)); - if (over<items.size()-1) { - int from = items[over+1]._ofs_cache; - pum->add_autohide_area(Rect2(pr.pos.x,pr.pos.y+from,pr.size.x,pr.size.y-from)); + pum->add_autohide_area(Rect2(pr.pos.x, pr.pos.y, pr.size.x, items[over]._ofs_cache)); + if (over < items.size() - 1) { + int from = items[over + 1]._ofs_cache; + pum->add_autohide_area(Rect2(pr.pos.x, pr.pos.y + from, pr.size.x, pr.size.y - from)); } - } - } void PopupMenu::_submenu_timeout() { - if (mouse_over==submenu_over) { + if (mouse_over == submenu_over) { _activate_submenu(mouse_over); - submenu_over=-1; + submenu_over = -1; } } - void PopupMenu::_gui_input(const InputEvent &p_event) { - switch( p_event.type) { + switch (p_event.type) { case InputEvent::KEY: { - if (!p_event.key.pressed) break; - switch(p_event.key.scancode) { - + switch (p_event.key.scancode) { case KEY_DOWN: { + for (int i = mouse_over + 1; i < items.size(); i++) { - for(int i=mouse_over+1;i<items.size();i++) { - - if (i<0 || i>=items.size()) + if (i < 0 || i >= items.size()) continue; if (!items[i].separator && !items[i].disabled) { - - mouse_over=i; + mouse_over = i; update(); break; } @@ -247,49 +230,38 @@ void PopupMenu::_gui_input(const InputEvent &p_event) { } break; case KEY_UP: { + for (int i = mouse_over - 1; i >= 0; i--) { - for(int i=mouse_over-1;i>=0;i--) { - - if (i<0 || i>=items.size()) + if (i < 0 || i >= items.size()) continue; - if (!items[i].separator && !items[i].disabled) { - - mouse_over=i; - update(); - break; + mouse_over = i; + update(); + break; } } } break; case KEY_RETURN: case KEY_ENTER: { - if (mouse_over>=0 && mouse_over<items.size() && !items[mouse_over].separator) { - + if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator) { activate_item(mouse_over); - } } break; } - - - - } break; case InputEvent::MOUSE_BUTTON: { - - const InputEventMouseButton &b=p_event.mouse_button; + const InputEventMouseButton &b = p_event.mouse_button; if (b.pressed) break; - switch(b.button_index) { - + switch (b.button_index) { case BUTTON_WHEEL_DOWN: { @@ -299,15 +271,15 @@ void PopupMenu::_gui_input(const InputEvent &p_event) { Ref<Font> font = get_font("font"); Point2 pos = get_pos(); - int s = (vseparation+font->get_height())*3; - pos.y-=s; + int s = (vseparation + font->get_height()) * 3; + pos.y -= s; set_pos(pos); //update hover InputEvent ie; - ie.type=InputEvent::MOUSE_MOTION; - ie.mouse_motion.x=b.x; - ie.mouse_motion.y=b.y+s; + ie.type = InputEvent::MOUSE_MOTION; + ie.mouse_motion.x = b.x; + ie.mouse_motion.y = b.y + s; _gui_input(ie); } } break; @@ -319,29 +291,27 @@ void PopupMenu::_gui_input(const InputEvent &p_event) { Ref<Font> font = get_font("font"); Point2 pos = get_pos(); - int s = (vseparation+font->get_height())*3; - pos.y+=s; + int s = (vseparation + font->get_height()) * 3; + pos.y += s; set_pos(pos); //update hover InputEvent ie; - ie.type=InputEvent::MOUSE_MOTION; - ie.mouse_motion.x=b.x; - ie.mouse_motion.y=b.y-s; + ie.type = InputEvent::MOUSE_MOTION; + ie.mouse_motion.x = b.x; + ie.mouse_motion.y = b.y - s; _gui_input(ie); - - } } break; case BUTTON_LEFT: { - int over=_get_mouse_over(Point2(b.x,b.y)); + int over = _get_mouse_over(Point2(b.x, b.y)); if (invalidated_click) { - invalidated_click=false; + invalidated_click = false; break; } - if (over<0) { + if (over < 0) { hide(); break; //non-activable } @@ -349,7 +319,7 @@ void PopupMenu::_gui_input(const InputEvent &p_event) { if (items[over].separator || items[over].disabled) break; - if (items[over].submenu!="") { + if (items[over].submenu != "") { _activate_submenu(over); return; @@ -363,52 +333,48 @@ void PopupMenu::_gui_input(const InputEvent &p_event) { } break; case InputEvent::MOUSE_MOTION: { - if (invalidated_click) { - moved+=Vector2(p_event.mouse_motion.relative_x,p_event.mouse_motion.relative_y); - if (moved.length()>4) - invalidated_click=false; - + moved += Vector2(p_event.mouse_motion.relative_x, p_event.mouse_motion.relative_y); + if (moved.length() > 4) + invalidated_click = false; } - const InputEventMouseMotion &m=p_event.mouse_motion; - for(List<Rect2>::Element *E=autohide_areas.front();E;E=E->next()) { + const InputEventMouseMotion &m = p_event.mouse_motion; + for (List<Rect2>::Element *E = autohide_areas.front(); E; E = E->next()) { - if (!Rect2(Point2(),get_size()).has_point(Point2(m.x,m.y)) && E->get().has_point(Point2(m.x,m.y))) { + if (!Rect2(Point2(), get_size()).has_point(Point2(m.x, m.y)) && E->get().has_point(Point2(m.x, m.y))) { call_deferred("hide"); return; } } - int over=_get_mouse_over(Point2(m.x,m.y)); - int id = (over<0 || items[over].separator || items[over].disabled)?-1:(items[over].ID>=0?items[over].ID:over); + int over = _get_mouse_over(Point2(m.x, m.y)); + int id = (over < 0 || items[over].separator || items[over].disabled) ? -1 : (items[over].ID >= 0 ? items[over].ID : over); - if (id<0) { - mouse_over=-1; + if (id < 0) { + mouse_over = -1; update(); break; } - if (items[over].submenu!="" && submenu_over!=over) { - submenu_over=over; + if (items[over].submenu != "" && submenu_over != over) { + submenu_over = over; submenu_timer->start(); } - if (over!=mouse_over) { - mouse_over=over; + if (over != mouse_over) { + mouse_over = over; update(); } } break; - } } - -bool PopupMenu::has_point(const Point2& p_point) const { +bool PopupMenu::has_point(const Point2 &p_point) const { if (parent_rect.has_point(p_point)) return true; - for(const List<Rect2>::Element *E=autohide_areas.front();E;E=E->next()) { + for (const List<Rect2>::Element *E = autohide_areas.front(); E; E = E->next()) { if (E->get().has_point(p_point)) return true; @@ -419,12 +385,12 @@ bool PopupMenu::has_point(const Point2& p_point) const { void PopupMenu::_notification(int p_what) { - switch(p_what) { + switch (p_what) { case NOTIFICATION_TRANSLATION_CHANGED: { - for(int i=0;i<items.size();i++) { - items[i].xl_text=XL_MESSAGE(items[i].text); + for (int i = 0; i < items.size(); i++) { + items[i].xl_text = XL_MESSAGE(items[i].text); } minimum_size_changed(); @@ -434,95 +400,92 @@ void PopupMenu::_notification(int p_what) { case NOTIFICATION_DRAW: { RID ci = get_canvas_item(); - Size2 size=get_size(); + Size2 size = get_size(); Ref<StyleBox> style = get_stylebox("panel"); Ref<StyleBox> hover = get_stylebox("hover"); Ref<Font> font = get_font("font"); Ref<Texture> check = get_icon("checked"); Ref<Texture> uncheck = get_icon("unchecked"); - Ref<Texture> submenu= get_icon("submenu"); + Ref<Texture> submenu = get_icon("submenu"); Ref<StyleBox> separator = get_stylebox("separator"); - style->draw( ci, Rect2( Point2(), get_size() ) ); - Point2 ofs=style->get_offset(); + style->draw(ci, Rect2(Point2(), get_size())); + Point2 ofs = style->get_offset(); int vseparation = get_constant("vseparation"); int hseparation = get_constant("hseparation"); Color font_color = get_color("font_color"); Color font_color_disabled = get_color("font_color_disabled"); Color font_color_accel = get_color("font_color_accel"); Color font_color_hover = get_color("font_color_hover"); - float font_h=font->get_height(); + float font_h = font->get_height(); - for (int i=0;i<items.size();i++) { + for (int i = 0; i < items.size(); i++) { - if (i>0) - ofs.y+=vseparation; - Point2 item_ofs=ofs; + if (i > 0) + ofs.y += vseparation; + Point2 item_ofs = ofs; float h; Size2 icon_size; - item_ofs.x+=items[i].h_ofs; + item_ofs.x += items[i].h_ofs; if (!items[i].icon.is_null()) { icon_size = items[i].icon->get_size(); - h = MAX( icon_size.height, font_h ); + h = MAX(icon_size.height, font_h); } else { - h=font_h; + h = font_h; } - if (i==mouse_over) { + if (i == mouse_over) { - hover->draw(ci, Rect2( item_ofs+Point2(-hseparation,-vseparation), Size2( get_size().width - style->get_minimum_size().width + hseparation*2, h+vseparation*2 ) )); + hover->draw(ci, Rect2(item_ofs + Point2(-hseparation, -vseparation), Size2(get_size().width - style->get_minimum_size().width + hseparation * 2, h + vseparation * 2))); } if (items[i].separator) { - int sep_h=separator->get_center_size().height+separator->get_minimum_size().height; - separator->draw(ci, Rect2( item_ofs+Point2(0,Math::floor((h-sep_h)/2.0)), Size2( get_size().width - style->get_minimum_size().width , sep_h ) )); - + int sep_h = separator->get_center_size().height + separator->get_minimum_size().height; + separator->draw(ci, Rect2(item_ofs + Point2(0, Math::floor((h - sep_h) / 2.0)), Size2(get_size().width - style->get_minimum_size().width, sep_h))); } if (items[i].checkable) { if (items[i].checked) - check->draw(ci, item_ofs+Point2(0,Math::floor((h-check->get_height())/2.0))); + check->draw(ci, item_ofs + Point2(0, Math::floor((h - check->get_height()) / 2.0))); else - uncheck->draw(ci, item_ofs+Point2(0,Math::floor((h-check->get_height())/2.0))); + uncheck->draw(ci, item_ofs + Point2(0, Math::floor((h - check->get_height()) / 2.0))); - item_ofs.x+=check->get_width()+hseparation; + item_ofs.x += check->get_width() + hseparation; } if (!items[i].icon.is_null()) { - items[i].icon->draw( ci, item_ofs+Point2(0,Math::floor((h-icon_size.height)/2.0))); - item_ofs.x+=items[i].icon->get_width(); - item_ofs.x+=hseparation; + items[i].icon->draw(ci, item_ofs + Point2(0, Math::floor((h - icon_size.height) / 2.0))); + item_ofs.x += items[i].icon->get_width(); + item_ofs.x += hseparation; } - if (items[i].submenu!="") { - submenu->draw( ci, Point2(size.width - style->get_margin(MARGIN_RIGHT) - submenu->get_width(),item_ofs.y+Math::floor(h-submenu->get_height())/2)); + if (items[i].submenu != "") { + submenu->draw(ci, Point2(size.width - style->get_margin(MARGIN_RIGHT) - submenu->get_width(), item_ofs.y + Math::floor(h - submenu->get_height()) / 2)); } - item_ofs.y+=font->get_ascent(); + item_ofs.y += font->get_ascent(); String text = items[i].shortcut.is_valid() ? String(tr(items[i].shortcut->get_name())) : items[i].xl_text; if (!items[i].separator) { - font->draw(ci,item_ofs+Point2(0,Math::floor((h-font_h)/2.0)),text,items[i].disabled?font_color_disabled:(i==mouse_over?font_color_hover:font_color)); + font->draw(ci, item_ofs + Point2(0, Math::floor((h - font_h) / 2.0)), text, items[i].disabled ? font_color_disabled : (i == mouse_over ? font_color_hover : font_color)); } if (items[i].accel || (items[i].shortcut.is_valid() && items[i].shortcut->is_valid())) { //accelerator String text = _get_accel_text(i); - item_ofs.x=size.width-style->get_margin(MARGIN_RIGHT)-font->get_string_size(text).width; - font->draw(ci,item_ofs+Point2(0,Math::floor((h-font_h)/2.0)),text,i==mouse_over?font_color_hover:font_color_accel); - + item_ofs.x = size.width - style->get_margin(MARGIN_RIGHT) - font->get_string_size(text).width; + font->draw(ci, item_ofs + Point2(0, Math::floor((h - font_h) / 2.0)), text, i == mouse_over ? font_color_hover : font_color_accel); } - items[i]._ofs_cache=ofs.y; - - ofs.y+=h; + items[i]._ofs_cache = ofs.y; + ofs.y += h; } } break; @@ -532,255 +495,243 @@ void PopupMenu::_notification(int p_what) { } break; case NOTIFICATION_MOUSE_EXIT: { - if (mouse_over>=0) { - mouse_over=-1; + if (mouse_over >= 0) { + mouse_over = -1; update(); } } break; } } - -void PopupMenu::add_icon_item(const Ref<Texture>& p_icon,const String& p_label,int p_ID,uint32_t p_accel) { +void PopupMenu::add_icon_item(const Ref<Texture> &p_icon, const String &p_label, int p_ID, uint32_t p_accel) { Item item; - item.icon=p_icon; - item.text=p_label; - item.xl_text=XL_MESSAGE(p_label); - item.accel=p_accel; - item.ID=p_ID; + item.icon = p_icon; + item.text = p_label; + item.xl_text = XL_MESSAGE(p_label); + item.accel = p_accel; + item.ID = p_ID; items.push_back(item); update(); } -void PopupMenu::add_item(const String& p_label,int p_ID,uint32_t p_accel) { +void PopupMenu::add_item(const String &p_label, int p_ID, uint32_t p_accel) { Item item; - item.text=p_label; - item.xl_text=XL_MESSAGE(p_label); - item.accel=p_accel; - item.ID=p_ID; + item.text = p_label; + item.xl_text = XL_MESSAGE(p_label); + item.accel = p_accel; + item.ID = p_ID; items.push_back(item); update(); } -void PopupMenu::add_submenu_item(const String& p_label, const String& p_submenu,int p_ID){ +void PopupMenu::add_submenu_item(const String &p_label, const String &p_submenu, int p_ID) { Item item; - item.text=p_label; - item.xl_text=XL_MESSAGE(p_label); - item.ID=p_ID; - item.submenu=p_submenu; + item.text = p_label; + item.xl_text = XL_MESSAGE(p_label); + item.ID = p_ID; + item.submenu = p_submenu; items.push_back(item); update(); } -void PopupMenu::add_icon_check_item(const Ref<Texture>& p_icon,const String& p_label,int p_ID,uint32_t p_accel) { +void PopupMenu::add_icon_check_item(const Ref<Texture> &p_icon, const String &p_label, int p_ID, uint32_t p_accel) { Item item; - item.icon=p_icon; - item.text=p_label; - item.xl_text=XL_MESSAGE(p_label); - item.accel=p_accel; - item.ID=p_ID; - item.checkable=true; + item.icon = p_icon; + item.text = p_label; + item.xl_text = XL_MESSAGE(p_label); + item.accel = p_accel; + item.ID = p_ID; + item.checkable = true; items.push_back(item); update(); } -void PopupMenu::add_check_item(const String& p_label,int p_ID,uint32_t p_accel) { +void PopupMenu::add_check_item(const String &p_label, int p_ID, uint32_t p_accel) { Item item; - item.text=p_label; - item.xl_text=XL_MESSAGE(p_label); - item.accel=p_accel; - item.ID=p_ID; - item.checkable=true; + item.text = p_label; + item.xl_text = XL_MESSAGE(p_label); + item.accel = p_accel; + item.ID = p_ID; + item.checkable = true; items.push_back(item); update(); } - -void PopupMenu::add_icon_shortcut(const Ref<Texture>& p_icon, const Ref<ShortCut>& p_shortcut, int p_ID, bool p_global) { +void PopupMenu::add_icon_shortcut(const Ref<Texture> &p_icon, const Ref<ShortCut> &p_shortcut, int p_ID, bool p_global) { ERR_FAIL_COND(p_shortcut.is_null()); _ref_shortcut(p_shortcut); Item item; - item.ID=p_ID; - item.icon=p_icon; - item.shortcut=p_shortcut; - item.shortcut_is_global=p_global; + item.ID = p_ID; + item.icon = p_icon; + item.shortcut = p_shortcut; + item.shortcut_is_global = p_global; items.push_back(item); update(); - } -void PopupMenu::add_shortcut(const Ref<ShortCut>& p_shortcut, int p_ID, bool p_global){ +void PopupMenu::add_shortcut(const Ref<ShortCut> &p_shortcut, int p_ID, bool p_global) { ERR_FAIL_COND(p_shortcut.is_null()); _ref_shortcut(p_shortcut); Item item; - item.ID=p_ID; - item.shortcut=p_shortcut; - item.shortcut_is_global=p_global; + item.ID = p_ID; + item.shortcut = p_shortcut; + item.shortcut_is_global = p_global; items.push_back(item); update(); - } -void PopupMenu::add_icon_check_shortcut(const Ref<Texture>& p_icon, const Ref<ShortCut>& p_shortcut, int p_ID, bool p_global){ +void PopupMenu::add_icon_check_shortcut(const Ref<Texture> &p_icon, const Ref<ShortCut> &p_shortcut, int p_ID, bool p_global) { ERR_FAIL_COND(p_shortcut.is_null()); _ref_shortcut(p_shortcut); Item item; - item.ID=p_ID; - item.shortcut=p_shortcut; - item.checkable=true; - item.icon=p_icon; - item.shortcut_is_global=p_global; + item.ID = p_ID; + item.shortcut = p_shortcut; + item.checkable = true; + item.icon = p_icon; + item.shortcut_is_global = p_global; items.push_back(item); update(); } -void PopupMenu::add_check_shortcut(const Ref<ShortCut>& p_shortcut, int p_ID, bool p_global){ +void PopupMenu::add_check_shortcut(const Ref<ShortCut> &p_shortcut, int p_ID, bool p_global) { ERR_FAIL_COND(p_shortcut.is_null()); _ref_shortcut(p_shortcut); Item item; - item.ID=p_ID; - item.shortcut=p_shortcut; - item.shortcut_is_global=p_global; - item.checkable=true; + item.ID = p_ID; + item.shortcut = p_shortcut; + item.shortcut_is_global = p_global; + item.checkable = true; items.push_back(item); update(); } -void PopupMenu::set_item_text(int p_idx,const String& p_text) { +void PopupMenu::set_item_text(int p_idx, const String &p_text) { - ERR_FAIL_INDEX(p_idx,items.size()); - items[p_idx].text=p_text; - items[p_idx].xl_text=XL_MESSAGE(p_text); + ERR_FAIL_INDEX(p_idx, items.size()); + items[p_idx].text = p_text; + items[p_idx].xl_text = XL_MESSAGE(p_text); update(); - } -void PopupMenu::set_item_icon(int p_idx,const Ref<Texture>& p_icon) { +void PopupMenu::set_item_icon(int p_idx, const Ref<Texture> &p_icon) { - ERR_FAIL_INDEX(p_idx,items.size()); - items[p_idx].icon=p_icon; + ERR_FAIL_INDEX(p_idx, items.size()); + items[p_idx].icon = p_icon; update(); - } -void PopupMenu::set_item_checked(int p_idx,bool p_checked) { +void PopupMenu::set_item_checked(int p_idx, bool p_checked) { - ERR_FAIL_INDEX(p_idx,items.size()); + ERR_FAIL_INDEX(p_idx, items.size()); - items[p_idx].checked=p_checked; + items[p_idx].checked = p_checked; update(); } -void PopupMenu::set_item_ID(int p_idx,int p_ID) { +void PopupMenu::set_item_ID(int p_idx, int p_ID) { - ERR_FAIL_INDEX(p_idx,items.size()); - items[p_idx].ID=p_ID; + ERR_FAIL_INDEX(p_idx, items.size()); + items[p_idx].ID = p_ID; update(); } -void PopupMenu::set_item_accelerator(int p_idx,uint32_t p_accel) { +void PopupMenu::set_item_accelerator(int p_idx, uint32_t p_accel) { - ERR_FAIL_INDEX(p_idx,items.size()); - items[p_idx].accel=p_accel; + ERR_FAIL_INDEX(p_idx, items.size()); + items[p_idx].accel = p_accel; update(); - } +void PopupMenu::set_item_metadata(int p_idx, const Variant &p_meta) { -void PopupMenu::set_item_metadata(int p_idx,const Variant& p_meta) { - - ERR_FAIL_INDEX(p_idx,items.size()); - items[p_idx].metadata=p_meta; + ERR_FAIL_INDEX(p_idx, items.size()); + items[p_idx].metadata = p_meta; update(); } -void PopupMenu::set_item_disabled(int p_idx,bool p_disabled) { +void PopupMenu::set_item_disabled(int p_idx, bool p_disabled) { - ERR_FAIL_INDEX(p_idx,items.size()); - items[p_idx].disabled=p_disabled; + ERR_FAIL_INDEX(p_idx, items.size()); + items[p_idx].disabled = p_disabled; update(); } -void PopupMenu::set_item_submenu(int p_idx, const String& p_submenu) { +void PopupMenu::set_item_submenu(int p_idx, const String &p_submenu) { - ERR_FAIL_INDEX(p_idx,items.size()); - items[p_idx].submenu=p_submenu; + ERR_FAIL_INDEX(p_idx, items.size()); + items[p_idx].submenu = p_submenu; update(); } void PopupMenu::toggle_item_checked(int p_idx) { - ERR_FAIL_INDEX(p_idx,items.size()); + ERR_FAIL_INDEX(p_idx, items.size()); items[p_idx].checked = !items[p_idx].checked; update(); } String PopupMenu::get_item_text(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),""); + ERR_FAIL_INDEX_V(p_idx, items.size(), ""); return items[p_idx].text; - } Ref<Texture> PopupMenu::get_item_icon(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),Ref<Texture>()); + ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture>()); return items[p_idx].icon; } - uint32_t PopupMenu::get_item_accelerator(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),0); + ERR_FAIL_INDEX_V(p_idx, items.size(), 0); return items[p_idx].accel; - } Variant PopupMenu::get_item_metadata(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),Variant()); + ERR_FAIL_INDEX_V(p_idx, items.size(), Variant()); return items[p_idx].metadata; - } bool PopupMenu::is_item_disabled(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),false); + ERR_FAIL_INDEX_V(p_idx, items.size(), false); return items[p_idx].disabled; } bool PopupMenu::is_item_checked(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),false); + ERR_FAIL_INDEX_V(p_idx, items.size(), false); return items[p_idx].checked; } int PopupMenu::get_item_ID(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),0); + ERR_FAIL_INDEX_V(p_idx, items.size(), 0); return items[p_idx].ID; } int PopupMenu::get_item_index(int p_ID) const { - for(int i=0;i<items.size();i++) { + for (int i = 0; i < items.size(); i++) { - if (items[i].ID==p_ID) + if (items[i].ID == p_ID) return i; } @@ -789,79 +740,72 @@ int PopupMenu::get_item_index(int p_ID) const { String PopupMenu::get_item_submenu(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),""); + ERR_FAIL_INDEX_V(p_idx, items.size(), ""); return items[p_idx].submenu; } String PopupMenu::get_item_tooltip(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),""); + ERR_FAIL_INDEX_V(p_idx, items.size(), ""); return items[p_idx].tooltip; } Ref<ShortCut> PopupMenu::get_item_shortcut(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),Ref<ShortCut>()); + ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<ShortCut>()); return items[p_idx].shortcut; } void PopupMenu::set_item_as_separator(int p_idx, bool p_separator) { - ERR_FAIL_INDEX(p_idx,items.size()); - items[p_idx].separator=p_separator; + ERR_FAIL_INDEX(p_idx, items.size()); + items[p_idx].separator = p_separator; update(); - } bool PopupMenu::is_item_separator(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),false); + ERR_FAIL_INDEX_V(p_idx, items.size(), false); return items[p_idx].separator; } - void PopupMenu::set_item_as_checkable(int p_idx, bool p_checkable) { - ERR_FAIL_INDEX(p_idx,items.size()); - items[p_idx].checkable=p_checkable; + ERR_FAIL_INDEX(p_idx, items.size()); + items[p_idx].checkable = p_checkable; update(); - } -void PopupMenu::set_item_tooltip(int p_idx,const String& p_tooltip) { +void PopupMenu::set_item_tooltip(int p_idx, const String &p_tooltip) { - ERR_FAIL_INDEX(p_idx,items.size()); - items[p_idx].tooltip=p_tooltip; + ERR_FAIL_INDEX(p_idx, items.size()); + items[p_idx].tooltip = p_tooltip; update(); } -void PopupMenu::set_item_shortcut(int p_idx, const Ref<ShortCut>& p_shortcut, bool p_global) { - ERR_FAIL_INDEX(p_idx,items.size()); +void PopupMenu::set_item_shortcut(int p_idx, const Ref<ShortCut> &p_shortcut, bool p_global) { + ERR_FAIL_INDEX(p_idx, items.size()); if (items[p_idx].shortcut.is_valid()) { _unref_shortcut(items[p_idx].shortcut); } - items[p_idx].shortcut=p_shortcut; - items[p_idx].shortcut_is_global=p_global; - + items[p_idx].shortcut = p_shortcut; + items[p_idx].shortcut_is_global = p_global; if (items[p_idx].shortcut.is_valid()) { _ref_shortcut(items[p_idx].shortcut); } - update(); } void PopupMenu::set_item_h_offset(int p_idx, int p_offset) { - ERR_FAIL_INDEX(p_idx,items.size()); - items[p_idx].h_ofs=p_offset; + ERR_FAIL_INDEX(p_idx, items.size()); + items[p_idx].h_ofs = p_offset; update(); - } - bool PopupMenu::is_item_checkable(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,items.size(),false); + ERR_FAIL_INDEX_V(p_idx, items.size(), false); return items[p_idx].checkable; } @@ -870,50 +814,48 @@ int PopupMenu::get_item_count() const { return items.size(); } -bool PopupMenu::activate_item_by_event(const InputEvent& p_event, bool p_for_global_only) { +bool PopupMenu::activate_item_by_event(const InputEvent &p_event, bool p_for_global_only) { - uint32_t code=0; - if (p_event.type==InputEvent::KEY) { - code=p_event.key.scancode; - if (code==0) - code=p_event.key.unicode; + uint32_t code = 0; + if (p_event.type == InputEvent::KEY) { + code = p_event.key.scancode; + if (code == 0) + code = p_event.key.unicode; if (p_event.key.mod.control) - code|=KEY_MASK_CTRL; + code |= KEY_MASK_CTRL; if (p_event.key.mod.alt) - code|=KEY_MASK_ALT; + code |= KEY_MASK_ALT; if (p_event.key.mod.meta) - code|=KEY_MASK_META; + code |= KEY_MASK_META; if (p_event.key.mod.shift) - code|=KEY_MASK_SHIFT; + code |= KEY_MASK_SHIFT; } - - int il=items.size(); - for(int i=0;i<il;i++) { + int il = items.size(); + for (int i = 0; i < il; i++) { if (is_item_disabled(i)) continue; - if (items[i].shortcut.is_valid() && items[i].shortcut->is_shortcut(p_event) && (items[i].shortcut_is_global || !p_for_global_only)) { activate_item(i); return true; } - if (code!=0 && items[i].accel==code) { + if (code != 0 && items[i].accel == code) { activate_item(i); return true; } - if (items[i].submenu!="") { - Node* n = get_node(items[i].submenu); - if(!n) + if (items[i].submenu != "") { + Node *n = get_node(items[i].submenu); + if (!n) continue; - PopupMenu* pm = n->cast_to<PopupMenu>(); - if(!pm) + PopupMenu *pm = n->cast_to<PopupMenu>(); + if (!pm) continue; - if(pm->activate_item_by_event(p_event,p_for_global_only)) { + if (pm->activate_item_by_event(p_event, p_for_global_only)) { return true; } } @@ -923,41 +865,38 @@ bool PopupMenu::activate_item_by_event(const InputEvent& p_event, bool p_for_glo void PopupMenu::activate_item(int p_item) { - - ERR_FAIL_INDEX(p_item,items.size()); + ERR_FAIL_INDEX(p_item, items.size()); ERR_FAIL_COND(items[p_item].separator); - int id = items[p_item].ID>=0?items[p_item].ID:p_item; - emit_signal("id_pressed",id); - emit_signal("index_pressed",p_item); + int id = items[p_item].ID >= 0 ? items[p_item].ID : p_item; + emit_signal("id_pressed", id); + emit_signal("index_pressed", p_item); //hide all parent PopupMenue's Node *next = get_parent(); PopupMenu *pop = next->cast_to<PopupMenu>(); while (pop) { - // We close all parents that are chained together, + // We close all parents that are chained together, // with hide_on_item_selection enabled - if(hide_on_item_selection && pop->is_hide_on_item_selection()) { + if (hide_on_item_selection && pop->is_hide_on_item_selection()) { pop->hide(); next = next->get_parent(); pop = next->cast_to<PopupMenu>(); - } - else { - // Break out of loop when the next parent has + } else { + // Break out of loop when the next parent has // hide_on_item_selection disabled break; } } - // Hides popup by default; unless otherwise specified + // Hides popup by default; unless otherwise specified // by using set_hide_on_item_selection if (hide_on_item_selection) { hide(); } - } void PopupMenu::remove_item(int p_idx) { - ERR_FAIL_INDEX(p_idx,items.size()); + ERR_FAIL_INDEX(p_idx, items.size()); if (items[p_idx].shortcut.is_valid()) { _unref_shortcut(items[p_idx].shortcut); @@ -970,30 +909,28 @@ void PopupMenu::remove_item(int p_idx) { void PopupMenu::add_separator() { Item sep; - sep.separator=true; - sep.ID=-1; + sep.separator = true; + sep.ID = -1; items.push_back(sep); update(); } -void PopupMenu::clear() { +void PopupMenu::clear() { - for(int i=0;i<items.size();i++) { + for (int i = 0; i < items.size(); i++) { if (items[i].shortcut.is_valid()) { _unref_shortcut(items[i].shortcut); } } items.clear(); - mouse_over=-1; + mouse_over = -1; update(); - - } Array PopupMenu::_get_items() const { Array items; - for(int i=0;i<get_item_count();i++) { + for (int i = 0; i < get_item_count(); i++) { items.push_back(get_item_text(i)); items.push_back(get_item_icon(i)); @@ -1009,68 +946,65 @@ Array PopupMenu::_get_items() const { } return items; - } -void PopupMenu::_ref_shortcut( Ref<ShortCut> p_sc) { +void PopupMenu::_ref_shortcut(Ref<ShortCut> p_sc) { if (!shortcut_refcount.has(p_sc)) { - shortcut_refcount[p_sc]=1; - p_sc->connect("changed",this,"update"); + shortcut_refcount[p_sc] = 1; + p_sc->connect("changed", this, "update"); } else { - shortcut_refcount[p_sc]+=1; + shortcut_refcount[p_sc] += 1; } } -void PopupMenu::_unref_shortcut(Ref<ShortCut> p_sc) { +void PopupMenu::_unref_shortcut(Ref<ShortCut> p_sc) { ERR_FAIL_COND(!shortcut_refcount.has(p_sc)); shortcut_refcount[p_sc]--; - if (shortcut_refcount[p_sc]==0) { - p_sc->disconnect("changed",this,"update"); + if (shortcut_refcount[p_sc] == 0) { + p_sc->disconnect("changed", this, "update"); shortcut_refcount.erase(p_sc); } } -void PopupMenu::_set_items(const Array& p_items){ +void PopupMenu::_set_items(const Array &p_items) { ERR_FAIL_COND(p_items.size() % 10); clear(); - for(int i=0;i<p_items.size();i+=10) { - - String text=p_items[i+0]; - Ref<Texture> icon=p_items[i+1]; - bool checkable=p_items[i+2]; - bool checked=p_items[i+3]; - bool disabled=p_items[i+4]; - - int id=p_items[i+5]; - int accel=p_items[i+6]; - Variant meta=p_items[i+7]; - String subm=p_items[i+8]; - bool sep=p_items[i+9]; - - int idx=get_item_count(); - add_item(text,id); - set_item_icon(idx,icon); - set_item_as_checkable(idx,checkable); - set_item_checked(idx,checked); - set_item_disabled(idx,disabled); - set_item_ID(idx,id); - set_item_metadata(idx,meta); - set_item_as_separator(idx,sep); - set_item_accelerator(idx,accel); - set_item_submenu(idx,subm); + for (int i = 0; i < p_items.size(); i += 10) { + + String text = p_items[i + 0]; + Ref<Texture> icon = p_items[i + 1]; + bool checkable = p_items[i + 2]; + bool checked = p_items[i + 3]; + bool disabled = p_items[i + 4]; + + int id = p_items[i + 5]; + int accel = p_items[i + 6]; + Variant meta = p_items[i + 7]; + String subm = p_items[i + 8]; + bool sep = p_items[i + 9]; + + int idx = get_item_count(); + add_item(text, id); + set_item_icon(idx, icon); + set_item_as_checkable(idx, checkable); + set_item_checked(idx, checked); + set_item_disabled(idx, disabled); + set_item_ID(idx, id); + set_item_metadata(idx, meta); + set_item_as_separator(idx, sep); + set_item_accelerator(idx, accel); + set_item_submenu(idx, subm); } - - } // Hide on item selection determines whether or not the popup will close after item selection void PopupMenu::set_hide_on_item_selection(bool p_enabled) { - hide_on_item_selection=p_enabled; + hide_on_item_selection = p_enabled; } bool PopupMenu::is_hide_on_item_selection() { @@ -1078,128 +1012,122 @@ bool PopupMenu::is_hide_on_item_selection() { return hide_on_item_selection; } -String PopupMenu::get_tooltip(const Point2& p_pos) const { - +String PopupMenu::get_tooltip(const Point2 &p_pos) const { - int over=_get_mouse_over(p_pos); - if (over<0 || over>=items.size()) + int over = _get_mouse_over(p_pos); + if (over < 0 || over >= items.size()) return ""; return items[over].tooltip; } +void PopupMenu::set_parent_rect(const Rect2 &p_rect) { -void PopupMenu::set_parent_rect(const Rect2& p_rect) { - - parent_rect=p_rect; + parent_rect = p_rect; } void PopupMenu::get_translatable_strings(List<String> *p_strings) const { - for(int i=0;i<items.size();i++) { + for (int i = 0; i < items.size(); i++) { - if (items[i].xl_text!="") + if (items[i].xl_text != "") p_strings->push_back(items[i].xl_text); } } -void PopupMenu::add_autohide_area(const Rect2& p_area) { +void PopupMenu::add_autohide_area(const Rect2 &p_area) { autohide_areas.push_back(p_area); } -void PopupMenu::clear_autohide_areas(){ +void PopupMenu::clear_autohide_areas() { autohide_areas.clear(); } void PopupMenu::_bind_methods() { - ClassDB::bind_method(D_METHOD("_gui_input"),&PopupMenu::_gui_input); - ClassDB::bind_method(D_METHOD("add_icon_item","texture","label","id","accel"),&PopupMenu::add_icon_item,DEFVAL(-1),DEFVAL(0)); - ClassDB::bind_method(D_METHOD("add_item","label","id","accel"),&PopupMenu::add_item,DEFVAL(-1),DEFVAL(0)); - ClassDB::bind_method(D_METHOD("add_icon_check_item","texture","label","id","accel"),&PopupMenu::add_icon_check_item,DEFVAL(-1),DEFVAL(0)); - ClassDB::bind_method(D_METHOD("add_check_item","label","id","accel"),&PopupMenu::add_check_item,DEFVAL(-1),DEFVAL(0)); - ClassDB::bind_method(D_METHOD("add_submenu_item","label","submenu","id"),&PopupMenu::add_submenu_item,DEFVAL(-1)); - - ClassDB::bind_method(D_METHOD("add_icon_shortcut","texture","shortcut:ShortCut","id","global"),&PopupMenu::add_icon_shortcut,DEFVAL(-1),DEFVAL(false)); - ClassDB::bind_method(D_METHOD("add_shortcut","shortcut:ShortCut","id","global"),&PopupMenu::add_shortcut,DEFVAL(-1),DEFVAL(false)); - ClassDB::bind_method(D_METHOD("add_icon_check_shortcut","texture","shortcut:ShortCut","id","global"),&PopupMenu::add_icon_check_shortcut,DEFVAL(-1),DEFVAL(false)); - ClassDB::bind_method(D_METHOD("add_check_shortcut","shortcut:ShortCut","id","global"),&PopupMenu::add_check_shortcut,DEFVAL(-1),DEFVAL(false)); - - ClassDB::bind_method(D_METHOD("set_item_text","idx","text"),&PopupMenu::set_item_text); - ClassDB::bind_method(D_METHOD("set_item_icon","idx","icon"),&PopupMenu::set_item_icon); - ClassDB::bind_method(D_METHOD("set_item_checked","idx","checked"),&PopupMenu::set_item_checked); - ClassDB::bind_method(D_METHOD("set_item_ID","idx","id"),&PopupMenu::set_item_ID); - ClassDB::bind_method(D_METHOD("set_item_accelerator","idx","accel"),&PopupMenu::set_item_accelerator); - ClassDB::bind_method(D_METHOD("set_item_metadata","idx","metadata"),&PopupMenu::set_item_metadata); - ClassDB::bind_method(D_METHOD("set_item_disabled","idx","disabled"),&PopupMenu::set_item_disabled); - ClassDB::bind_method(D_METHOD("set_item_submenu","idx","submenu"),&PopupMenu::set_item_submenu); - ClassDB::bind_method(D_METHOD("set_item_as_separator","idx","enable"),&PopupMenu::set_item_as_separator); - ClassDB::bind_method(D_METHOD("set_item_as_checkable","idx","enable"),&PopupMenu::set_item_as_checkable); - ClassDB::bind_method(D_METHOD("set_item_tooltip","idx","tooltip"),&PopupMenu::set_item_tooltip); - ClassDB::bind_method(D_METHOD("set_item_shortcut","idx","shortcut:ShortCut","global"),&PopupMenu::set_item_shortcut,DEFVAL(false)); - - ClassDB::bind_method(D_METHOD("toggle_item_checked","idx"), &PopupMenu::toggle_item_checked); - - ClassDB::bind_method(D_METHOD("get_item_text","idx"),&PopupMenu::get_item_text); - ClassDB::bind_method(D_METHOD("get_item_icon","idx"),&PopupMenu::get_item_icon); - ClassDB::bind_method(D_METHOD("is_item_checked","idx"),&PopupMenu::is_item_checked); - ClassDB::bind_method(D_METHOD("get_item_ID","idx"),&PopupMenu::get_item_ID); - ClassDB::bind_method(D_METHOD("get_item_index","id"),&PopupMenu::get_item_index); - ClassDB::bind_method(D_METHOD("get_item_accelerator","idx"),&PopupMenu::get_item_accelerator); - ClassDB::bind_method(D_METHOD("get_item_metadata","idx"),&PopupMenu::get_item_metadata); - ClassDB::bind_method(D_METHOD("is_item_disabled","idx"),&PopupMenu::is_item_disabled); - ClassDB::bind_method(D_METHOD("get_item_submenu","idx"),&PopupMenu::get_item_submenu); - ClassDB::bind_method(D_METHOD("is_item_separator","idx"),&PopupMenu::is_item_separator); - ClassDB::bind_method(D_METHOD("is_item_checkable","idx"),&PopupMenu::is_item_checkable); - ClassDB::bind_method(D_METHOD("get_item_tooltip","idx"),&PopupMenu::get_item_tooltip); - ClassDB::bind_method(D_METHOD("get_item_shortcut:ShortCut","idx"),&PopupMenu::get_item_shortcut); - - ClassDB::bind_method(D_METHOD("get_item_count"),&PopupMenu::get_item_count); - - ClassDB::bind_method(D_METHOD("remove_item","idx"),&PopupMenu::remove_item); - - ClassDB::bind_method(D_METHOD("add_separator"),&PopupMenu::add_separator); - ClassDB::bind_method(D_METHOD("clear"),&PopupMenu::clear); - - ClassDB::bind_method(D_METHOD("_set_items"),&PopupMenu::_set_items); - ClassDB::bind_method(D_METHOD("_get_items"),&PopupMenu::_get_items); - - ClassDB::bind_method(D_METHOD("set_hide_on_item_selection","enable"),&PopupMenu::set_hide_on_item_selection); - ClassDB::bind_method(D_METHOD("is_hide_on_item_selection"),&PopupMenu::is_hide_on_item_selection); - - ClassDB::bind_method(D_METHOD("_submenu_timeout"),&PopupMenu::_submenu_timeout); - - ADD_PROPERTY( PropertyInfo(Variant::ARRAY,"items",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR), "_set_items","_get_items") ; - ADD_PROPERTYNO( PropertyInfo(Variant::BOOL, "hide_on_item_selection" ), "set_hide_on_item_selection", "is_hide_on_item_selection") ; - - ADD_SIGNAL( MethodInfo("id_pressed", PropertyInfo( Variant::INT,"ID") ) ); - ADD_SIGNAL( MethodInfo("index_pressed", PropertyInfo( Variant::INT,"index") ) ); - + ClassDB::bind_method(D_METHOD("_gui_input"), &PopupMenu::_gui_input); + ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_item, DEFVAL(-1), DEFVAL(0)); + ClassDB::bind_method(D_METHOD("add_item", "label", "id", "accel"), &PopupMenu::add_item, DEFVAL(-1), DEFVAL(0)); + ClassDB::bind_method(D_METHOD("add_icon_check_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_check_item, DEFVAL(-1), DEFVAL(0)); + ClassDB::bind_method(D_METHOD("add_check_item", "label", "id", "accel"), &PopupMenu::add_check_item, DEFVAL(-1), DEFVAL(0)); + ClassDB::bind_method(D_METHOD("add_submenu_item", "label", "submenu", "id"), &PopupMenu::add_submenu_item, DEFVAL(-1)); + + ClassDB::bind_method(D_METHOD("add_icon_shortcut", "texture", "shortcut:ShortCut", "id", "global"), &PopupMenu::add_icon_shortcut, DEFVAL(-1), DEFVAL(false)); + ClassDB::bind_method(D_METHOD("add_shortcut", "shortcut:ShortCut", "id", "global"), &PopupMenu::add_shortcut, DEFVAL(-1), DEFVAL(false)); + ClassDB::bind_method(D_METHOD("add_icon_check_shortcut", "texture", "shortcut:ShortCut", "id", "global"), &PopupMenu::add_icon_check_shortcut, DEFVAL(-1), DEFVAL(false)); + ClassDB::bind_method(D_METHOD("add_check_shortcut", "shortcut:ShortCut", "id", "global"), &PopupMenu::add_check_shortcut, DEFVAL(-1), DEFVAL(false)); + + ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &PopupMenu::set_item_text); + ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "icon"), &PopupMenu::set_item_icon); + ClassDB::bind_method(D_METHOD("set_item_checked", "idx", "checked"), &PopupMenu::set_item_checked); + ClassDB::bind_method(D_METHOD("set_item_ID", "idx", "id"), &PopupMenu::set_item_ID); + ClassDB::bind_method(D_METHOD("set_item_accelerator", "idx", "accel"), &PopupMenu::set_item_accelerator); + ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &PopupMenu::set_item_metadata); + ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &PopupMenu::set_item_disabled); + ClassDB::bind_method(D_METHOD("set_item_submenu", "idx", "submenu"), &PopupMenu::set_item_submenu); + ClassDB::bind_method(D_METHOD("set_item_as_separator", "idx", "enable"), &PopupMenu::set_item_as_separator); + ClassDB::bind_method(D_METHOD("set_item_as_checkable", "idx", "enable"), &PopupMenu::set_item_as_checkable); + ClassDB::bind_method(D_METHOD("set_item_tooltip", "idx", "tooltip"), &PopupMenu::set_item_tooltip); + ClassDB::bind_method(D_METHOD("set_item_shortcut", "idx", "shortcut:ShortCut", "global"), &PopupMenu::set_item_shortcut, DEFVAL(false)); + + ClassDB::bind_method(D_METHOD("toggle_item_checked", "idx"), &PopupMenu::toggle_item_checked); + + ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &PopupMenu::get_item_text); + ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &PopupMenu::get_item_icon); + ClassDB::bind_method(D_METHOD("is_item_checked", "idx"), &PopupMenu::is_item_checked); + ClassDB::bind_method(D_METHOD("get_item_ID", "idx"), &PopupMenu::get_item_ID); + ClassDB::bind_method(D_METHOD("get_item_index", "id"), &PopupMenu::get_item_index); + ClassDB::bind_method(D_METHOD("get_item_accelerator", "idx"), &PopupMenu::get_item_accelerator); + ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &PopupMenu::get_item_metadata); + ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &PopupMenu::is_item_disabled); + ClassDB::bind_method(D_METHOD("get_item_submenu", "idx"), &PopupMenu::get_item_submenu); + ClassDB::bind_method(D_METHOD("is_item_separator", "idx"), &PopupMenu::is_item_separator); + ClassDB::bind_method(D_METHOD("is_item_checkable", "idx"), &PopupMenu::is_item_checkable); + ClassDB::bind_method(D_METHOD("get_item_tooltip", "idx"), &PopupMenu::get_item_tooltip); + ClassDB::bind_method(D_METHOD("get_item_shortcut:ShortCut", "idx"), &PopupMenu::get_item_shortcut); + + ClassDB::bind_method(D_METHOD("get_item_count"), &PopupMenu::get_item_count); + + ClassDB::bind_method(D_METHOD("remove_item", "idx"), &PopupMenu::remove_item); + + ClassDB::bind_method(D_METHOD("add_separator"), &PopupMenu::add_separator); + ClassDB::bind_method(D_METHOD("clear"), &PopupMenu::clear); + + ClassDB::bind_method(D_METHOD("_set_items"), &PopupMenu::_set_items); + ClassDB::bind_method(D_METHOD("_get_items"), &PopupMenu::_get_items); + + ClassDB::bind_method(D_METHOD("set_hide_on_item_selection", "enable"), &PopupMenu::set_hide_on_item_selection); + ClassDB::bind_method(D_METHOD("is_hide_on_item_selection"), &PopupMenu::is_hide_on_item_selection); + + ClassDB::bind_method(D_METHOD("_submenu_timeout"), &PopupMenu::_submenu_timeout); + + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "items", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_items", "_get_items"); + ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "hide_on_item_selection"), "set_hide_on_item_selection", "is_hide_on_item_selection"); + + ADD_SIGNAL(MethodInfo("id_pressed", PropertyInfo(Variant::INT, "ID"))); + ADD_SIGNAL(MethodInfo("index_pressed", PropertyInfo(Variant::INT, "index"))); } - void PopupMenu::set_invalidate_click_until_motion() { - moved=Vector2(); - invalidated_click=true; + moved = Vector2(); + invalidated_click = true; } PopupMenu::PopupMenu() { - - mouse_over=-1; + mouse_over = -1; set_focus_mode(FOCUS_ALL); set_as_toplevel(true); set_hide_on_item_selection(true); - submenu_timer = memnew( Timer ); + submenu_timer = memnew(Timer); submenu_timer->set_wait_time(0.3); submenu_timer->set_one_shot(true); - submenu_timer->connect("timeout",this,"_submenu_timeout"); + submenu_timer->connect("timeout", this, "_submenu_timeout"); add_child(submenu_timer); } - PopupMenu::~PopupMenu() { } diff --git a/scene/gui/popup_menu.h b/scene/gui/popup_menu.h index b5fca9a451..186d40f6d3 100644 --- a/scene/gui/popup_menu.h +++ b/scene/gui/popup_menu.h @@ -35,11 +35,9 @@ @author Juan Linietsky <reduzio@gmail.com> */ - - class PopupMenu : public Popup { - GDCLASS(PopupMenu, Popup ); + GDCLASS(PopupMenu, Popup); struct Item { Ref<Texture> icon; @@ -59,10 +57,18 @@ class PopupMenu : public Popup { Ref<ShortCut> shortcut; bool shortcut_is_global; - Item() { checked=false; checkable=false; separator=false; accel=0; disabled=false; _ofs_cache=0; h_ofs=0; shortcut_is_global=false; } + Item() { + checked = false; + checkable = false; + separator = false; + accel = 0; + disabled = false; + _ofs_cache = 0; + h_ofs = 0; + shortcut_is_global = false; + } }; - Timer *submenu_timer; List<Rect2> autohide_areas; Vector<Item> items; @@ -70,7 +76,7 @@ class PopupMenu : public Popup { int submenu_over; Rect2 parent_rect; String _get_accel_text(int p_item) const; - int _get_mouse_over(const Point2& p_over) const; + int _get_mouse_over(const Point2 &p_over) const; virtual Size2 get_minimum_size() const; void _gui_input(const InputEvent &p_event); void _activate_submenu(int over); @@ -81,44 +87,44 @@ class PopupMenu : public Popup { Vector2 moved; Array _get_items() const; - void _set_items(const Array& p_items); + void _set_items(const Array &p_items); - Map< Ref<ShortCut>, int> shortcut_refcount; + Map<Ref<ShortCut>, int> shortcut_refcount; void _ref_shortcut(Ref<ShortCut> p_sc); - void _unref_shortcut( Ref<ShortCut> p_sc); -protected: + void _unref_shortcut(Ref<ShortCut> p_sc); - virtual bool has_point(const Point2& p_point) const; +protected: + virtual bool has_point(const Point2 &p_point) const; -friend class MenuButton; + friend class MenuButton; void _notification(int p_what); static void _bind_methods(); -public: - void add_icon_item(const Ref<Texture>& p_icon,const String& p_label,int p_ID=-1,uint32_t p_accel=0); - void add_item(const String& p_label,int p_ID=-1,uint32_t p_accel=0); - void add_icon_check_item(const Ref<Texture>& p_icon,const String& p_label,int p_ID=-1,uint32_t p_accel=0); - void add_check_item(const String& p_label,int p_ID=-1,uint32_t p_accel=0); - void add_submenu_item(const String& p_label,const String& p_submenu, int p_ID=-1); - - void add_icon_shortcut(const Ref<Texture>& p_icon,const Ref<ShortCut>& p_shortcut,int p_ID=-1,bool p_global=false); - void add_shortcut(const Ref<ShortCut>& p_shortcut,int p_ID=-1,bool p_global=false); - void add_icon_check_shortcut(const Ref<Texture>& p_icon,const Ref<ShortCut>& p_shortcut,int p_ID=-1,bool p_global=false); - void add_check_shortcut(const Ref<ShortCut>& p_shortcut,int p_ID=-1,bool p_global=false); - - void set_item_text(int p_idx,const String& p_text); - void set_item_icon(int p_idx,const Ref<Texture>& p_icon); - void set_item_checked(int p_idx,bool p_checked); - void set_item_ID(int p_idx,int p_ID); - void set_item_accelerator(int p_idx,uint32_t p_accel); - void set_item_metadata(int p_idx,const Variant& p_meta); - void set_item_disabled(int p_idx,bool p_disabled); - void set_item_submenu(int p_idx, const String& p_submenu); +public: + void add_icon_item(const Ref<Texture> &p_icon, const String &p_label, int p_ID = -1, uint32_t p_accel = 0); + void add_item(const String &p_label, int p_ID = -1, uint32_t p_accel = 0); + void add_icon_check_item(const Ref<Texture> &p_icon, const String &p_label, int p_ID = -1, uint32_t p_accel = 0); + void add_check_item(const String &p_label, int p_ID = -1, uint32_t p_accel = 0); + void add_submenu_item(const String &p_label, const String &p_submenu, int p_ID = -1); + + void add_icon_shortcut(const Ref<Texture> &p_icon, const Ref<ShortCut> &p_shortcut, int p_ID = -1, bool p_global = false); + void add_shortcut(const Ref<ShortCut> &p_shortcut, int p_ID = -1, bool p_global = false); + void add_icon_check_shortcut(const Ref<Texture> &p_icon, const Ref<ShortCut> &p_shortcut, int p_ID = -1, bool p_global = false); + void add_check_shortcut(const Ref<ShortCut> &p_shortcut, int p_ID = -1, bool p_global = false); + + void set_item_text(int p_idx, const String &p_text); + void set_item_icon(int p_idx, const Ref<Texture> &p_icon); + void set_item_checked(int p_idx, bool p_checked); + void set_item_ID(int p_idx, int p_ID); + void set_item_accelerator(int p_idx, uint32_t p_accel); + void set_item_metadata(int p_idx, const Variant &p_meta); + void set_item_disabled(int p_idx, bool p_disabled); + void set_item_submenu(int p_idx, const String &p_submenu); void set_item_as_separator(int p_idx, bool p_separator); void set_item_as_checkable(int p_idx, bool p_checkable); - void set_item_tooltip(int p_idx,const String& p_tooltip); - void set_item_shortcut(int p_idx, const Ref<ShortCut>& p_shortcut,bool p_global=false); + void set_item_tooltip(int p_idx, const String &p_tooltip); + void set_item_shortcut(int p_idx, const Ref<ShortCut> &p_shortcut, bool p_global = false); void set_item_h_offset(int p_idx, int p_offset); void toggle_item_checked(int p_idx); @@ -139,7 +145,7 @@ public: int get_item_count() const; - bool activate_item_by_event(const InputEvent& p_event,bool p_for_global_only=false); + bool activate_item_by_event(const InputEvent &p_event, bool p_for_global_only = false); void activate_item(int p_item); void remove_item(int p_idx); @@ -148,13 +154,13 @@ public: void clear(); - void set_parent_rect(const Rect2& p_rect); + void set_parent_rect(const Rect2 &p_rect); - virtual String get_tooltip(const Point2& p_pos) const; + virtual String get_tooltip(const Point2 &p_pos) const; virtual void get_translatable_strings(List<String> *p_strings) const; - void add_autohide_area(const Rect2& p_area); + void add_autohide_area(const Rect2 &p_area); void clear_autohide_areas(); void set_invalidate_click_until_motion(); @@ -163,7 +169,6 @@ public: PopupMenu(); ~PopupMenu(); - }; #endif diff --git a/scene/gui/progress_bar.cpp b/scene/gui/progress_bar.cpp index 61776d3ae6..86dea6cd96 100644 --- a/scene/gui/progress_bar.cpp +++ b/scene/gui/progress_bar.cpp @@ -28,68 +28,64 @@ /*************************************************************************/ #include "progress_bar.h" - Size2 ProgressBar::get_minimum_size() const { Ref<StyleBox> bg = get_stylebox("bg"); Ref<Font> font = get_font("font"); - Size2 ms=bg->get_minimum_size()+bg->get_center_size(); + Size2 ms = bg->get_minimum_size() + bg->get_center_size(); if (percent_visible) { - ms.height=MAX(ms.height,bg->get_minimum_size().height+font->get_height()); + ms.height = MAX(ms.height, bg->get_minimum_size().height + font->get_height()); } return ms; } - void ProgressBar::_notification(int p_what) { - - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { Ref<StyleBox> bg = get_stylebox("bg"); Ref<StyleBox> fg = get_stylebox("fg"); Ref<Font> font = get_font("font"); - Color font_color=get_color("font_color"); + Color font_color = get_color("font_color"); - draw_style_box(bg,Rect2(Point2(),get_size())); + draw_style_box(bg, Rect2(Point2(), get_size())); float r = get_as_ratio(); int mp = fg->get_minimum_size().width; - int p = r*get_size().width-mp; - if (p>0) { + int p = r * get_size().width - mp; + if (p > 0) { - draw_style_box(fg,Rect2(Point2(),Size2(p+fg->get_minimum_size().width,get_size().height))); + draw_style_box(fg, Rect2(Point2(), Size2(p + fg->get_minimum_size().width, get_size().height))); } if (percent_visible) { - String txt=itos(int(get_as_ratio()*100))+"%"; - font->draw_halign(get_canvas_item(),Point2(0,font->get_ascent()+(get_size().height-font->get_height())/2),HALIGN_CENTER,get_size().width,txt,font_color); + String txt = itos(int(get_as_ratio() * 100)) + "%"; + font->draw_halign(get_canvas_item(), Point2(0, font->get_ascent() + (get_size().height - font->get_height()) / 2), HALIGN_CENTER, get_size().width, txt, font_color); } } } - void ProgressBar::set_percent_visible(bool p_visible) { - percent_visible=p_visible; + percent_visible = p_visible; update(); } -bool ProgressBar::is_percent_visible() const{ +bool ProgressBar::is_percent_visible() const { return percent_visible; } void ProgressBar::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_percent_visible","visible"),&ProgressBar::set_percent_visible); - ClassDB::bind_method(D_METHOD("is_percent_visible"),&ProgressBar::is_percent_visible); - ADD_GROUP("Percent","percent_"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL,"percent_visible"),"set_percent_visible","is_percent_visible"); + ClassDB::bind_method(D_METHOD("set_percent_visible", "visible"), &ProgressBar::set_percent_visible); + ClassDB::bind_method(D_METHOD("is_percent_visible"), &ProgressBar::is_percent_visible); + ADD_GROUP("Percent", "percent_"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "percent_visible"), "set_percent_visible", "is_percent_visible"); } ProgressBar::ProgressBar() { set_v_size_flags(0); - percent_visible=true; + percent_visible = true; } diff --git a/scene/gui/progress_bar.h b/scene/gui/progress_bar.h index 01306a2ac4..b5820d1682 100644 --- a/scene/gui/progress_bar.h +++ b/scene/gui/progress_bar.h @@ -33,15 +33,15 @@ class ProgressBar : public Range { - GDCLASS( ProgressBar, Range ); + GDCLASS(ProgressBar, Range); bool percent_visible; -protected: +protected: void _notification(int p_what); static void _bind_methods(); -public: +public: void set_percent_visible(bool p_visible); bool is_percent_visible() const; diff --git a/scene/gui/range.cpp b/scene/gui/range.cpp index f434aff08d..f15f3a6078 100644 --- a/scene/gui/range.cpp +++ b/scene/gui/range.cpp @@ -28,20 +28,18 @@ /*************************************************************************/ #include "range.h" - - void Range::_value_changed_notify() { _value_changed(shared->val); - emit_signal("value_changed",shared->val); + emit_signal("value_changed", shared->val); update(); _change_notify("range/value"); } void Range::Shared::emit_value_changed() { - for (Set<Range*>::Element *E=owners.front();E;E=E->next()) { - Range *r=E->get(); + for (Set<Range *>::Element *E = owners.front(); E; E = E->next()) { + Range *r = E->get(); if (!r->is_inside_tree()) continue; r->_value_changed_notify(); @@ -50,65 +48,62 @@ void Range::Shared::emit_value_changed() { void Range::_changed_notify(const char *p_what) { - emit_signal("changed",shared->val); + emit_signal("changed", shared->val); update(); _change_notify(p_what); } void Range::Shared::emit_changed(const char *p_what) { - for (Set<Range*>::Element *E=owners.front();E;E=E->next()) { - Range *r=E->get(); + for (Set<Range *>::Element *E = owners.front(); E; E = E->next()) { + Range *r = E->get(); if (!r->is_inside_tree()) continue; r->_changed_notify(p_what); } } - void Range::set_value(double p_val) { - if(_rounded_values){ + if (_rounded_values) { p_val = Math::round(p_val); } - if (p_val>shared->max-shared->page) - p_val=shared->max-shared->page; + if (p_val > shared->max - shared->page) + p_val = shared->max - shared->page; - if (p_val<shared->min) - p_val=shared->min; + if (p_val < shared->min) + p_val = shared->min; - if (shared->val==p_val) + if (shared->val == p_val) return; - shared->val=p_val; + shared->val = p_val; shared->emit_value_changed(); } void Range::set_min(double p_min) { - shared->min=p_min; + shared->min = p_min; set_value(shared->val); shared->emit_changed("range/min"); } void Range::set_max(double p_max) { - shared->max=p_max; + shared->max = p_max; set_value(shared->val); shared->emit_changed("range/max"); - } void Range::set_step(double p_step) { - shared->step=p_step; + shared->step = p_step; shared->emit_changed("range/step"); - } void Range::set_page(double p_page) { - shared->page=p_page; + shared->page = p_page; set_value(shared->val); shared->emit_changed("range/page"); @@ -139,11 +134,11 @@ void Range::set_as_ratio(double p_value) { double v; - if (shared->exp_ratio && get_min()>0) { + if (shared->exp_ratio && get_min() > 0) { - double exp_min = Math::log(get_min())/Math::log((double)2); - double exp_max = Math::log(get_max())/Math::log((double)2); - v = Math::pow(2,exp_min+(exp_max-exp_min)*p_value); + double exp_min = Math::log(get_min()) / Math::log((double)2); + double exp_max = Math::log(get_max()) / Math::log((double)2); + v = Math::pow(2, exp_min + (exp_max - exp_min) * p_value); } else { double percent = (get_max() - get_min()) * p_value; @@ -154,15 +149,15 @@ void Range::set_as_ratio(double p_value) { v = percent + get_min(); } } - set_value( v ); + set_value(v); } double Range::get_as_ratio() const { - if (shared->exp_ratio && get_min()>0) { + if (shared->exp_ratio && get_min() > 0) { - double exp_min = Math::log(get_min())/Math::log((double)2); - double exp_max = Math::log(get_max())/Math::log((double)2); - double v = Math::log(get_value())/Math::log((double)2); + double exp_min = Math::log(get_min()) / Math::log((double)2); + double exp_max = Math::log(get_max()) / Math::log((double)2); + double v = Math::log(get_value()) / Math::log((double)2); return (v - exp_min) / (exp_max - exp_min); @@ -174,7 +169,7 @@ double Range::get_as_ratio() const { void Range::_share(Node *p_range) { - Range * r = p_range->cast_to<Range>(); + Range *r = p_range->cast_to<Range>(); ERR_FAIL_COND(!r); share(r); } @@ -190,69 +185,67 @@ void Range::share(Range *p_range) { void Range::unshare() { - Shared * nshared = memnew(Shared); - nshared->min=shared->min; - nshared->max=shared->max; - nshared->val=shared->val; - nshared->step=shared->step; - nshared->page=shared->page; + Shared *nshared = memnew(Shared); + nshared->min = shared->min; + nshared->max = shared->max; + nshared->val = shared->val; + nshared->step = shared->step; + nshared->page = shared->page; _unref_shared(); _ref_shared(nshared); } void Range::_ref_shared(Shared *p_shared) { - if (shared && p_shared==shared) + if (shared && p_shared == shared) return; _unref_shared(); - shared=p_shared; + shared = p_shared; shared->owners.insert(this); } - void Range::_unref_shared() { shared->owners.erase(this); - if (shared->owners.size()==0) { + if (shared->owners.size() == 0) { memdelete(shared); - shared=NULL; + shared = NULL; } } void Range::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_value"),&Range::get_value); - ClassDB::bind_method(D_METHOD("get_min"),&Range::get_min); - ClassDB::bind_method(D_METHOD("get_max"),&Range::get_max); - ClassDB::bind_method(D_METHOD("get_step"),&Range::get_step); - ClassDB::bind_method(D_METHOD("get_page"),&Range::get_page); - ClassDB::bind_method(D_METHOD("get_as_ratio"),&Range::get_as_ratio); - ClassDB::bind_method(D_METHOD("set_value","value"),&Range::set_value); - ClassDB::bind_method(D_METHOD("set_min","minimum"),&Range::set_min); - ClassDB::bind_method(D_METHOD("set_max","maximum"),&Range::set_max); - ClassDB::bind_method(D_METHOD("set_step","step"),&Range::set_step); - ClassDB::bind_method(D_METHOD("set_page","pagesize"),&Range::set_page); - ClassDB::bind_method(D_METHOD("set_as_ratio","value"),&Range::set_as_ratio); - ClassDB::bind_method(D_METHOD("set_use_rounded_values","enabled"),&Range::set_use_rounded_values); - ClassDB::bind_method(D_METHOD("is_using_rounded_values"),&Range::is_using_rounded_values); - ClassDB::bind_method(D_METHOD("set_exp_ratio","enabled"),&Range::set_exp_ratio); - ClassDB::bind_method(D_METHOD("is_ratio_exp"),&Range::is_ratio_exp); - - ClassDB::bind_method(D_METHOD("share","with"),&Range::_share); - ClassDB::bind_method(D_METHOD("unshare"),&Range::unshare); - - ADD_SIGNAL( MethodInfo("value_changed", PropertyInfo(Variant::REAL,"value"))); - ADD_SIGNAL( MethodInfo("changed")); - - ADD_PROPERTY( PropertyInfo( Variant::REAL, "min_value" ), "set_min", "get_min") ; - ADD_PROPERTY( PropertyInfo( Variant::REAL, "max_value" ), "set_max", "get_max") ; - ADD_PROPERTY( PropertyInfo( Variant::REAL, "step" ), "set_step", "get_step") ; - ADD_PROPERTY( PropertyInfo( Variant::REAL, "page" ), "set_page", "get_page") ; - ADD_PROPERTY( PropertyInfo( Variant::REAL, "value" ), "set_value", "get_value") ; - ADD_PROPERTY( PropertyInfo( Variant::BOOL, "exp_edit" ), "set_exp_ratio", "is_ratio_exp") ; - ADD_PROPERTY( PropertyInfo( Variant::BOOL, "rounded" ), "set_use_rounded_values", "is_using_rounded_values") ; - + ClassDB::bind_method(D_METHOD("get_value"), &Range::get_value); + ClassDB::bind_method(D_METHOD("get_min"), &Range::get_min); + ClassDB::bind_method(D_METHOD("get_max"), &Range::get_max); + ClassDB::bind_method(D_METHOD("get_step"), &Range::get_step); + ClassDB::bind_method(D_METHOD("get_page"), &Range::get_page); + ClassDB::bind_method(D_METHOD("get_as_ratio"), &Range::get_as_ratio); + ClassDB::bind_method(D_METHOD("set_value", "value"), &Range::set_value); + ClassDB::bind_method(D_METHOD("set_min", "minimum"), &Range::set_min); + ClassDB::bind_method(D_METHOD("set_max", "maximum"), &Range::set_max); + ClassDB::bind_method(D_METHOD("set_step", "step"), &Range::set_step); + ClassDB::bind_method(D_METHOD("set_page", "pagesize"), &Range::set_page); + ClassDB::bind_method(D_METHOD("set_as_ratio", "value"), &Range::set_as_ratio); + ClassDB::bind_method(D_METHOD("set_use_rounded_values", "enabled"), &Range::set_use_rounded_values); + ClassDB::bind_method(D_METHOD("is_using_rounded_values"), &Range::is_using_rounded_values); + ClassDB::bind_method(D_METHOD("set_exp_ratio", "enabled"), &Range::set_exp_ratio); + ClassDB::bind_method(D_METHOD("is_ratio_exp"), &Range::is_ratio_exp); + + ClassDB::bind_method(D_METHOD("share", "with"), &Range::_share); + ClassDB::bind_method(D_METHOD("unshare"), &Range::unshare); + + ADD_SIGNAL(MethodInfo("value_changed", PropertyInfo(Variant::REAL, "value"))); + ADD_SIGNAL(MethodInfo("changed")); + + ADD_PROPERTY(PropertyInfo(Variant::REAL, "min_value"), "set_min", "get_min"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_value"), "set_max", "get_max"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "step"), "set_step", "get_step"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "page"), "set_page", "get_page"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "value"), "set_value", "get_value"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exp_edit"), "set_exp_ratio", "is_ratio_exp"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rounded"), "set_use_rounded_values", "is_using_rounded_values"); } void Range::set_use_rounded_values(bool p_enable) { @@ -267,7 +260,7 @@ bool Range::is_using_rounded_values() const { void Range::set_exp_ratio(bool p_enable) { - shared->exp_ratio=p_enable; + shared->exp_ratio = p_enable; } bool Range::is_ratio_exp() const { @@ -275,25 +268,20 @@ bool Range::is_ratio_exp() const { return shared->exp_ratio; } - -Range::Range() -{ +Range::Range() { shared = memnew(Shared); - shared->min=0; - shared->max=100; - shared->val= - shared->step=1; - shared->page=0; + shared->min = 0; + shared->max = 100; + shared->val = + shared->step = 1; + shared->page = 0; shared->owners.insert(this); - shared->exp_ratio=false; + shared->exp_ratio = false; _rounded_values = false; } - Range::~Range() { _unref_shared(); } - - diff --git a/scene/gui/range.h b/scene/gui/range.h index 5f274a6901..105bd08a4f 100644 --- a/scene/gui/range.h +++ b/scene/gui/range.h @@ -35,16 +35,15 @@ */ class Range : public Control { - GDCLASS( Range, Control ); - + GDCLASS(Range, Control); struct Shared { - double val,min,max; - double step,page; + double val, min, max; + double step, page; bool exp_ratio; - Set<Range*> owners; + Set<Range *> owners; void emit_value_changed(); - void emit_changed(const char *p_what=""); + void emit_changed(const char *p_what = ""); }; Shared *shared; @@ -55,17 +54,16 @@ class Range : public Control { void _share(Node *p_range); void _value_changed_notify(); - void _changed_notify(const char *p_what=""); + void _changed_notify(const char *p_what = ""); protected: - virtual void _value_changed(double) {} static void _bind_methods(); bool _rounded_values; -public: +public: void set_value(double p_val); void set_min(double p_min); void set_max(double p_max); @@ -91,7 +89,6 @@ public: Range(); ~Range(); - }; #endif diff --git a/scene/gui/reference_rect.cpp b/scene/gui/reference_rect.cpp index ff4cdf04fd..cc38b896d9 100644 --- a/scene/gui/reference_rect.cpp +++ b/scene/gui/reference_rect.cpp @@ -30,15 +30,14 @@ void ReferenceRect::_notification(int p_what) { - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { if (!is_inside_tree()) return; if (get_tree()->is_editor_hint()) - draw_style_box(get_stylebox("border"),Rect2(Point2(),get_size())) ; + draw_style_box(get_stylebox("border"), Rect2(Point2(), get_size())); } } -ReferenceRect::ReferenceRect() -{ +ReferenceRect::ReferenceRect() { } diff --git a/scene/gui/reference_rect.h b/scene/gui/reference_rect.h index be493f346c..b64f479829 100644 --- a/scene/gui/reference_rect.h +++ b/scene/gui/reference_rect.h @@ -33,11 +33,11 @@ class ReferenceRect : public Control { - GDCLASS( ReferenceRect, Control); + GDCLASS(ReferenceRect, Control); protected: - void _notification(int p_what); + public: ReferenceRect(); }; diff --git a/scene/gui/rich_text_label.cpp b/scene/gui/rich_text_label.cpp index 17ec71f4a4..322d9dee20 100644 --- a/scene/gui/rich_text_label.cpp +++ b/scene/gui/rich_text_label.cpp @@ -27,10 +27,10 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "rich_text_label.h" -#include "scene/scene_string_names.h" #include "os/keyboard.h" #include "os/os.h" -RichTextLabel::Item *RichTextLabel::_get_next_item(Item* p_item,bool p_free) { +#include "scene/scene_string_names.h" +RichTextLabel::Item *RichTextLabel::_get_next_item(Item *p_item, bool p_free) { if (p_free) { @@ -45,550 +45,511 @@ RichTextLabel::Item *RichTextLabel::_get_next_item(Item* p_item,bool p_free) { } else { //go up until something with a next is found while (p_item->parent && !p_item->E->next()) { - p_item=p_item->parent; + p_item = p_item->parent; } - if (p_item->parent) return p_item->E->next()->get(); else return NULL; - } } else { - if (p_item->subitems.size() && p_item->type!=ITEM_TABLE) { + if (p_item->subitems.size() && p_item->type != ITEM_TABLE) { return p_item->subitems.front()->get(); - } else if (p_item->type==ITEM_FRAME) { + } else if (p_item->type == ITEM_FRAME) { return NULL; } else if (p_item->E->next()) { return p_item->E->next()->get(); } else { //go up until something with a next is found - while (p_item->type!=ITEM_FRAME && !p_item->E->next()) { - p_item=p_item->parent; + while (p_item->type != ITEM_FRAME && !p_item->E->next()) { + p_item = p_item->parent; } - - if (p_item->type!=ITEM_FRAME) + if (p_item->type != ITEM_FRAME) return p_item->E->next()->get(); else return NULL; - } } return NULL; - } - -void RichTextLabel::_process_line(ItemFrame *p_frame,const Vector2& p_ofs,int &y, int p_width, int p_line, ProcessMode p_mode,const Ref<Font> &p_base_font,const Color &p_base_color,const Point2i& p_click_pos,Item **r_click_item,int *r_click_char,bool *r_outside,int p_char_count) { +void RichTextLabel::_process_line(ItemFrame *p_frame, const Vector2 &p_ofs, int &y, int p_width, int p_line, ProcessMode p_mode, const Ref<Font> &p_base_font, const Color &p_base_color, const Point2i &p_click_pos, Item **r_click_item, int *r_click_char, bool *r_outside, int p_char_count) { RID ci; if (r_outside) - *r_outside=false; - if (p_mode==PROCESS_DRAW) { - ci=get_canvas_item(); + *r_outside = false; + if (p_mode == PROCESS_DRAW) { + ci = get_canvas_item(); if (r_click_item) - *r_click_item=NULL; - + *r_click_item = NULL; } Line &l = p_frame->lines[p_line]; Item *it = l.from; + int line_ofs = 0; + int margin = _find_margin(it, p_base_font); + Align align = _find_align(it); + int line = 0; + int spaces = 0; - int line_ofs=0; - int margin=_find_margin(it,p_base_font); - Align align=_find_align(it); - int line=0; - int spaces=0; + if (p_mode != PROCESS_CACHE) { - - if (p_mode!=PROCESS_CACHE) { - - ERR_FAIL_INDEX(line,l.offset_caches.size()); + ERR_FAIL_INDEX(line, l.offset_caches.size()); line_ofs = l.offset_caches[line]; } - if (p_mode==PROCESS_CACHE) { + if (p_mode == PROCESS_CACHE) { l.offset_caches.clear(); l.height_caches.clear(); - l.char_count=0; - l.minimum_width=0; + l.char_count = 0; + l.minimum_width = 0; } - int wofs=margin; - int spaces_size=0; - int align_ofs=0; - + int wofs = margin; + int spaces_size = 0; + int align_ofs = 0; - if (p_mode!=PROCESS_CACHE && align!=ALIGN_FILL) - wofs+=line_ofs; + if (p_mode != PROCESS_CACHE && align != ALIGN_FILL) + wofs += line_ofs; - int begin=wofs; + int begin = wofs; Ref<Font> cfont = _find_font(it); if (cfont.is_null()) - cfont=p_base_font; + cfont = p_base_font; //line height should be the font height for the first time, this ensures that an empty line will never have zero height and succesive newlines are displayed - int line_height=cfont->get_height(); + int line_height = cfont->get_height(); Variant meta; -#define NEW_LINE \ -{\ - if (p_mode!=PROCESS_CACHE) {\ - line++;\ - if (line < l.offset_caches.size())\ - line_ofs=l.offset_caches[line];\ - wofs=margin;\ - if (align!=ALIGN_FILL)\ - wofs+=line_ofs;\ - } else {\ - int used=wofs-margin;\ - switch(align) {\ - case ALIGN_LEFT: l.offset_caches.push_back(0); break;\ - case ALIGN_CENTER: l.offset_caches.push_back(((p_width-margin)-used)/2); break;\ - case ALIGN_RIGHT: l.offset_caches.push_back(((p_width-margin)-used)); break;\ - case ALIGN_FILL: l.offset_caches.push_back((p_width-margin)-used/*+spaces_size*/); break;\ - }\ - l.height_caches.push_back(line_height);\ - l.space_caches.push_back(spaces);\ - }\ - y+=line_height+get_constant(SceneStringNames::get_singleton()->line_separation);\ - line_height=0;\ - spaces=0;\ - spaces_size=0;\ - wofs=begin;\ - align_ofs=0;\ - if (p_mode!=PROCESS_CACHE) {\ - lh=line<l.height_caches.size()?l.height_caches[line]:1;\ - }\ - if (p_mode==PROCESS_POINTER && r_click_item && p_click_pos.y>=p_ofs.y+y && p_click_pos.y<=p_ofs.y+y+lh && p_click_pos.x<p_ofs.x+wofs) {\ - if (r_outside) *r_outside=true;\ - *r_click_item=it;\ - *r_click_char=rchar;\ - return;\ - }\ -} - - -#define ENSURE_WIDTH(m_width) \ - if (p_mode==PROCESS_CACHE) { \ - l.minimum_width=MAX(l.minimum_width,wofs+m_width);\ - }\ - if (wofs + m_width > p_width) {\ - if (p_mode==PROCESS_CACHE) {\ - if (spaces>0) \ - spaces-=1;\ - }\ - if (p_mode==PROCESS_POINTER && r_click_item && p_click_pos.y>=p_ofs.y+y && p_click_pos.y<=p_ofs.y+y+lh && p_click_pos.x>p_ofs.x+wofs) {\ - if (r_outside) *r_outside=true; \ - *r_click_item=it;\ - *r_click_char=rchar;\ - return;\ - }\ - NEW_LINE\ +#define NEW_LINE \ + { \ + if (p_mode != PROCESS_CACHE) { \ + line++; \ + if (line < l.offset_caches.size()) \ + line_ofs = l.offset_caches[line]; \ + wofs = margin; \ + if (align != ALIGN_FILL) \ + wofs += line_ofs; \ + } else { \ + int used = wofs - margin; \ + switch (align) { \ + case ALIGN_LEFT: l.offset_caches.push_back(0); break; \ + case ALIGN_CENTER: l.offset_caches.push_back(((p_width - margin) - used) / 2); break; \ + case ALIGN_RIGHT: l.offset_caches.push_back(((p_width - margin) - used)); break; \ + case ALIGN_FILL: l.offset_caches.push_back((p_width - margin) - used /*+spaces_size*/); break; \ + } \ + l.height_caches.push_back(line_height); \ + l.space_caches.push_back(spaces); \ + } \ + y += line_height + get_constant(SceneStringNames::get_singleton()->line_separation); \ + line_height = 0; \ + spaces = 0; \ + spaces_size = 0; \ + wofs = begin; \ + align_ofs = 0; \ + if (p_mode != PROCESS_CACHE) { \ + lh = line < l.height_caches.size() ? l.height_caches[line] : 1; \ + } \ + if (p_mode == PROCESS_POINTER && r_click_item && p_click_pos.y >= p_ofs.y + y && p_click_pos.y <= p_ofs.y + y + lh && p_click_pos.x < p_ofs.x + wofs) { \ + if (r_outside) *r_outside = true; \ + *r_click_item = it; \ + *r_click_char = rchar; \ + return; \ + } \ } +#define ENSURE_WIDTH(m_width) \ + if (p_mode == PROCESS_CACHE) { \ + l.minimum_width = MAX(l.minimum_width, wofs + m_width); \ + } \ + if (wofs + m_width > p_width) { \ + if (p_mode == PROCESS_CACHE) { \ + if (spaces > 0) \ + spaces -= 1; \ + } \ + if (p_mode == PROCESS_POINTER && r_click_item && p_click_pos.y >= p_ofs.y + y && p_click_pos.y <= p_ofs.y + y + lh && p_click_pos.x > p_ofs.x + wofs) { \ + if (r_outside) *r_outside = true; \ + *r_click_item = it; \ + *r_click_char = rchar; \ + return; \ + } \ + NEW_LINE \ + } -#define ADVANCE(m_width) \ -{\ - if (p_mode==PROCESS_POINTER && r_click_item && p_click_pos.y>=p_ofs.y+y && p_click_pos.y<=p_ofs.y+y+lh && p_click_pos.x>=p_ofs.x+wofs && p_click_pos.x<p_ofs.x+wofs+m_width) {\ - if (r_outside) *r_outside=false; \ - *r_click_item=it;\ - *r_click_char=rchar;\ - return;\ - }\ - wofs+=m_width;\ -} +#define ADVANCE(m_width) \ + { \ + if (p_mode == PROCESS_POINTER && r_click_item && p_click_pos.y >= p_ofs.y + y && p_click_pos.y <= p_ofs.y + y + lh && p_click_pos.x >= p_ofs.x + wofs && p_click_pos.x < p_ofs.x + wofs + m_width) { \ + if (r_outside) *r_outside = false; \ + *r_click_item = it; \ + *r_click_char = rchar; \ + return; \ + } \ + wofs += m_width; \ + } -#define CHECK_HEIGHT( m_height ) \ -if (m_height > line_height) {\ - line_height=m_height;\ -} +#define CHECK_HEIGHT(m_height) \ + if (m_height > line_height) { \ + line_height = m_height; \ + } Color selection_fg; Color selection_bg; - if (p_mode==PROCESS_DRAW) { - + if (p_mode == PROCESS_DRAW) { selection_fg = get_color("font_color_selected"); selection_bg = get_color("selection_color"); } - int rchar=0; - int lh=0; + int rchar = 0; + int lh = 0; while (it) { - switch(it->type) { + switch (it->type) { case ITEM_TEXT: { - ItemText *text = static_cast<ItemText*>(it); + ItemText *text = static_cast<ItemText *>(it); - Ref<Font> font=_find_font(it); + Ref<Font> font = _find_font(it); if (font.is_null()) - font=p_base_font; + font = p_base_font; const CharType *c = text->text.c_str(); - const CharType *cf=c; - int fh=font->get_height(); + const CharType *cf = c; + int fh = font->get_height(); int ascent = font->get_ascent(); Color color; - bool underline=false; + bool underline = false; - if (p_mode==PROCESS_DRAW) { - color=_find_color(text,p_base_color); - underline=_find_underline(text); - if (_find_meta(text,&meta)) { + if (p_mode == PROCESS_DRAW) { + color = _find_color(text, p_base_color); + underline = _find_underline(text); + if (_find_meta(text, &meta)) { - underline=true; + underline = true; } - - } else if (p_mode==PROCESS_CACHE) { - l.char_count+=text->text.length(); - + } else if (p_mode == PROCESS_CACHE) { + l.char_count += text->text.length(); } - rchar=0; + rchar = 0; - while(*c) { + while (*c) { - int end=0; - int w=0; - int fw=0; + int end = 0; + int w = 0; + int fw = 0; - lh=0; - if (p_mode!=PROCESS_CACHE) { - lh=line<l.height_caches.size()?l.height_caches[line]:1; + lh = 0; + if (p_mode != PROCESS_CACHE) { + lh = line < l.height_caches.size() ? l.height_caches[line] : 1; } - while (c[end]!=0 && !(end && c[end-1]==' ' && c[end]!=' ')) { + while (c[end] != 0 && !(end && c[end - 1] == ' ' && c[end] != ' ')) { - int cw = font->get_char_size(c[end],c[end+1]).width; - if (c[end]=='\t') { - cw=tab_size*font->get_char_size(' ').width; + int cw = font->get_char_size(c[end], c[end + 1]).width; + if (c[end] == '\t') { + cw = tab_size * font->get_char_size(' ').width; } - if (end>0 && w+cw+begin > p_width ) { + if (end > 0 && w + cw + begin > p_width) { break; //don't allow lines longer than assigned width } - w+=cw; - fw+=cw; + w += cw; + fw += cw; end++; } ENSURE_WIDTH(w); - if (end && c[end-1]==' ') { - if (p_mode==PROCESS_CACHE) { - spaces_size+=font->get_char_size(' ').width; - } else if (align==ALIGN_FILL) { - int ln = MIN(l.offset_caches.size()-1,line); + if (end && c[end - 1] == ' ') { + if (p_mode == PROCESS_CACHE) { + spaces_size += font->get_char_size(' ').width; + } else if (align == ALIGN_FILL) { + int ln = MIN(l.offset_caches.size() - 1, line); if (l.space_caches[ln]) { align_ofs = spaces * l.offset_caches[ln] / l.space_caches[ln]; } } spaces++; - } - { - int ofs=0; + int ofs = 0; - for(int i=0;i<end;i++) { - int pofs=wofs+ofs; + for (int i = 0; i < end; i++) { + int pofs = wofs + ofs; - - if (p_mode==PROCESS_POINTER && r_click_char && p_click_pos.y>=p_ofs.y+y && p_click_pos.y<=p_ofs.y+y+lh) { + if (p_mode == PROCESS_POINTER && r_click_char && p_click_pos.y >= p_ofs.y + y && p_click_pos.y <= p_ofs.y + y + lh) { //int o = (wofs+w)-p_click_pos.x; + int cw = font->get_char_size(c[i], c[i + 1]).x; - int cw=font->get_char_size(c[i],c[i+1]).x; - - if (c[i]=='\t') { - cw=tab_size*font->get_char_size(' ').width; + if (c[i] == '\t') { + cw = tab_size * font->get_char_size(' ').width; } + if (p_click_pos.x - cw / 2 > p_ofs.x + align_ofs + pofs) { - if (p_click_pos.x-cw/2>p_ofs.x+align_ofs+pofs) { - - rchar=int((&c[i])-cf); + rchar = int((&c[i]) - cf); } + ofs += cw; + } else if (p_mode == PROCESS_DRAW) { - ofs+=cw; - } else if (p_mode==PROCESS_DRAW) { - - bool selected=false; + bool selected = false; if (selection.active) { - int cofs = (&c[i])-cf; - if ((text->index > selection.from->index || (text->index == selection.from->index && cofs >=selection.from_char)) && (text->index < selection.to->index || (text->index == selection.to->index && cofs <=selection.to_char))) { - selected=true; + int cofs = (&c[i]) - cf; + if ((text->index > selection.from->index || (text->index == selection.from->index && cofs >= selection.from_char)) && (text->index < selection.to->index || (text->index == selection.to->index && cofs <= selection.to_char))) { + selected = true; } } - int cw=0; + int cw = 0; - bool visible = visible_characters<0 || p_char_count<visible_characters; - if (c[i]=='\t') - visible=false; + bool visible = visible_characters < 0 || p_char_count < visible_characters; + if (c[i] == '\t') + visible = false; if (selected) { - cw = font->get_char_size(c[i],c[i+1]).x; - draw_rect(Rect2(p_ofs.x+pofs,p_ofs.y+y,cw,lh),selection_bg); + cw = font->get_char_size(c[i], c[i + 1]).x; + draw_rect(Rect2(p_ofs.x + pofs, p_ofs.y + y, cw, lh), selection_bg); if (visible) - font->draw_char(ci,p_ofs+Point2(align_ofs+pofs,y+lh-(fh-ascent)),c[i],c[i+1],selection_fg); + font->draw_char(ci, p_ofs + Point2(align_ofs + pofs, y + lh - (fh - ascent)), c[i], c[i + 1], selection_fg); } else { if (visible) - cw=font->draw_char(ci,p_ofs+Point2(align_ofs+pofs,y+lh-(fh-ascent)),c[i],c[i+1],color); + cw = font->draw_char(ci, p_ofs + Point2(align_ofs + pofs, y + lh - (fh - ascent)), c[i], c[i + 1], color); } p_char_count++; - if (c[i]=='\t') { - cw=tab_size*font->get_char_size(' ').width; + if (c[i] == '\t') { + cw = tab_size * font->get_char_size(' ').width; } - if (underline) { - Color uc=color; - uc.a*=0.5; - int uy = y+lh-fh+ascent+2; - VS::get_singleton()->canvas_item_add_line(ci,p_ofs+Point2(align_ofs+pofs,uy),p_ofs+Point2(align_ofs+pofs+cw,uy),uc); + Color uc = color; + uc.a *= 0.5; + int uy = y + lh - fh + ascent + 2; + VS::get_singleton()->canvas_item_add_line(ci, p_ofs + Point2(align_ofs + pofs, uy), p_ofs + Point2(align_ofs + pofs + cw, uy), uc); } - ofs+=cw; + ofs += cw; } - } } - ADVANCE(fw); CHECK_HEIGHT(fh); //must be done somewhere - c=&c[end]; + c = &c[end]; } - } break; case ITEM_IMAGE: { - lh=0; - if (p_mode!=PROCESS_CACHE) - lh = line<l.height_caches.size()?l.height_caches[line]:1; + lh = 0; + if (p_mode != PROCESS_CACHE) + lh = line < l.height_caches.size() ? l.height_caches[line] : 1; else - l.char_count+=1; //images count as chars too + l.char_count += 1; //images count as chars too - ItemImage *img = static_cast<ItemImage*>(it); + ItemImage *img = static_cast<ItemImage *>(it); - Ref<Font> font=_find_font(it); + Ref<Font> font = _find_font(it); if (font.is_null()) - font=p_base_font; + font = p_base_font; - if (p_mode==PROCESS_POINTER && r_click_char) - *r_click_char=0; + if (p_mode == PROCESS_POINTER && r_click_char) + *r_click_char = 0; - ENSURE_WIDTH( img->image->get_width() ); + ENSURE_WIDTH(img->image->get_width()); - bool visible = visible_characters<0 || p_char_count<visible_characters; + bool visible = visible_characters < 0 || p_char_count < visible_characters; - if (p_mode==PROCESS_DRAW && visible) { - img->image->draw(ci,p_ofs+Point2(align_ofs+wofs,y+lh-font->get_descent()-img->image->get_height())); + if (p_mode == PROCESS_DRAW && visible) { + img->image->draw(ci, p_ofs + Point2(align_ofs + wofs, y + lh - font->get_descent() - img->image->get_height())); } p_char_count++; - ADVANCE( img->image->get_width() ); - CHECK_HEIGHT( (img->image->get_height()+font->get_descent()) ); + ADVANCE(img->image->get_width()); + CHECK_HEIGHT((img->image->get_height() + font->get_descent())); } break; case ITEM_NEWLINE: { - - lh=0; - if (p_mode!=PROCESS_CACHE) - lh = line<l.height_caches.size()?l.height_caches[line]:1; + lh = 0; + if (p_mode != PROCESS_CACHE) + lh = line < l.height_caches.size() ? l.height_caches[line] : 1; } break; case ITEM_TABLE: { - lh=0; - ItemTable *table = static_cast<ItemTable*>(it); - int hseparation=get_constant("table_hseparation"); - int vseparation=get_constant("table_vseparation"); - Color ccolor = _find_color(table,p_base_color); - Vector2 draw_ofs = Point2(wofs,y); + lh = 0; + ItemTable *table = static_cast<ItemTable *>(it); + int hseparation = get_constant("table_hseparation"); + int vseparation = get_constant("table_vseparation"); + Color ccolor = _find_color(table, p_base_color); + Vector2 draw_ofs = Point2(wofs, y); - if (p_mode==PROCESS_CACHE) { + if (p_mode == PROCESS_CACHE) { - int idx=0; + int idx = 0; //set minimums to zero - for(int i=0;i<table->columns.size();i++) { - table->columns[i].min_width=0; - table->columns[i].width=0; + for (int i = 0; i < table->columns.size(); i++) { + table->columns[i].min_width = 0; + table->columns[i].width = 0; } //compute minimum width for each cell - for (List<Item*>::Element *E=table->subitems.front();E;E=E->next()) { - ERR_CONTINUE(E->get()->type!=ITEM_FRAME); //children should all be frames - ItemFrame *frame = static_cast<ItemFrame*>(E->get()); + for (List<Item *>::Element *E = table->subitems.front(); E; E = E->next()) { + ERR_CONTINUE(E->get()->type != ITEM_FRAME); //children should all be frames + ItemFrame *frame = static_cast<ItemFrame *>(E->get()); int column = idx % table->columns.size(); - int ly=0; - + int ly = 0; - for(int i=0;i<frame->lines.size();i++) { + for (int i = 0; i < frame->lines.size(); i++) { - _process_line(frame,Point2(),ly,p_width,i,PROCESS_CACHE,cfont,Color()); - table->columns[column].min_width=MAX( table->columns[i].min_width, frame->lines[i].minimum_width ); + _process_line(frame, Point2(), ly, p_width, i, PROCESS_CACHE, cfont, Color()); + table->columns[column].min_width = MAX(table->columns[i].min_width, frame->lines[i].minimum_width); } idx++; } //compute available width and total radio (for expanders) + int total_ratio = 0; + int available_width = p_width - hseparation * (table->columns.size() - 1); + table->total_width = hseparation; - int total_ratio=0; - int available_width=p_width - hseparation * (table->columns.size() -1); - table->total_width=hseparation; - - for(int i=0;i<table->columns.size();i++) { - available_width-=table->columns[i].min_width; + for (int i = 0; i < table->columns.size(); i++) { + available_width -= table->columns[i].min_width; if (table->columns[i].expand) - total_ratio+=table->columns[i].expand_ratio; + total_ratio += table->columns[i].expand_ratio; } //assign actual widths - for(int i=0;i<table->columns.size();i++) { + for (int i = 0; i < table->columns.size(); i++) { table->columns[i].width = table->columns[i].min_width; if (table->columns[i].expand) - table->columns[i].width+=table->columns[i].expand_ratio*available_width/total_ratio; - table->total_width+=table->columns[i].width+hseparation; + table->columns[i].width += table->columns[i].expand_ratio * available_width / total_ratio; + table->total_width += table->columns[i].width + hseparation; } //compute caches properly again with the right width - idx=0; - for (List<Item*>::Element *E=table->subitems.front();E;E=E->next()) { - ERR_CONTINUE(E->get()->type!=ITEM_FRAME); //children should all be frames - ItemFrame *frame = static_cast<ItemFrame*>(E->get()); + idx = 0; + for (List<Item *>::Element *E = table->subitems.front(); E; E = E->next()) { + ERR_CONTINUE(E->get()->type != ITEM_FRAME); //children should all be frames + ItemFrame *frame = static_cast<ItemFrame *>(E->get()); int column = idx % table->columns.size(); + for (int i = 0; i < frame->lines.size(); i++) { - for(int i=0;i<frame->lines.size();i++) { - - int ly=0; - _process_line(frame,Point2(),ly,table->columns[column].width,i,PROCESS_CACHE,cfont,Color()); - frame->lines[i].height_cache=ly; //actual height - frame->lines[i].height_accum_cache=ly; //actual height + int ly = 0; + _process_line(frame, Point2(), ly, table->columns[column].width, i, PROCESS_CACHE, cfont, Color()); + frame->lines[i].height_cache = ly; //actual height + frame->lines[i].height_accum_cache = ly; //actual height } idx++; } - } + Point2 offset(align_ofs + hseparation, vseparation); - - Point2 offset(align_ofs+hseparation,vseparation); - - int row_height=0; + int row_height = 0; //draw using computed caches - int idx=0; - for (List<Item*>::Element *E=table->subitems.front();E;E=E->next()) { - ERR_CONTINUE(E->get()->type!=ITEM_FRAME); //children should all be frames - ItemFrame *frame = static_cast<ItemFrame*>(E->get()); + int idx = 0; + for (List<Item *>::Element *E = table->subitems.front(); E; E = E->next()) { + ERR_CONTINUE(E->get()->type != ITEM_FRAME); //children should all be frames + ItemFrame *frame = static_cast<ItemFrame *>(E->get()); int column = idx % table->columns.size(); - int ly=0; - int yofs=0; + int ly = 0; + int yofs = 0; + int lines_h = frame->lines[frame->lines.size() - 1].height_accum_cache - (frame->lines[0].height_accum_cache - frame->lines[0].height_cache); + int lines_ofs = p_ofs.y + offset.y + draw_ofs.y; - int lines_h = frame->lines[frame->lines.size()-1].height_accum_cache - (frame->lines[0].height_accum_cache - frame->lines[0].height_cache); - int lines_ofs = p_ofs.y+offset.y+draw_ofs.y; - - bool visible = lines_ofs < get_size().height && lines_ofs+lines_h >=0; - - for(int i=0;i<frame->lines.size();i++) { + bool visible = lines_ofs < get_size().height && lines_ofs + lines_h >= 0; + for (int i = 0; i < frame->lines.size(); i++) { if (visible) { - if (p_mode==PROCESS_DRAW) { - _process_line(frame,p_ofs+offset+draw_ofs+Vector2(0,yofs),ly,table->columns[column].width,i,PROCESS_DRAW,cfont,ccolor); - } else if (p_mode==PROCESS_POINTER) { - _process_line(frame,p_ofs+offset+draw_ofs+Vector2(0,yofs),ly,table->columns[column].width,i,PROCESS_POINTER,cfont,ccolor,p_click_pos,r_click_item,r_click_char,r_outside); + if (p_mode == PROCESS_DRAW) { + _process_line(frame, p_ofs + offset + draw_ofs + Vector2(0, yofs), ly, table->columns[column].width, i, PROCESS_DRAW, cfont, ccolor); + } else if (p_mode == PROCESS_POINTER) { + _process_line(frame, p_ofs + offset + draw_ofs + Vector2(0, yofs), ly, table->columns[column].width, i, PROCESS_POINTER, cfont, ccolor, p_click_pos, r_click_item, r_click_char, r_outside); } } - yofs+=frame->lines[i].height_cache; - if (p_mode==PROCESS_CACHE) { - frame->lines[i].height_accum_cache=offset.y+draw_ofs.y+frame->lines[i].height_cache; + yofs += frame->lines[i].height_cache; + if (p_mode == PROCESS_CACHE) { + frame->lines[i].height_accum_cache = offset.y + draw_ofs.y + frame->lines[i].height_cache; } - } - row_height=MAX(yofs,row_height); - offset.x+=table->columns[column].width+hseparation; + row_height = MAX(yofs, row_height); + offset.x += table->columns[column].width + hseparation; - if (column==table->columns.size()-1) { + if (column == table->columns.size() - 1) { - offset.y+=row_height+vseparation; - offset.x=hseparation; - row_height=0; + offset.y += row_height + vseparation; + offset.x = hseparation; + row_height = 0; } idx++; } int total_height = offset.y; if (row_height) { - total_height=row_height+vseparation; + total_height = row_height + vseparation; } - - - ADVANCE( table->total_width ); - CHECK_HEIGHT( total_height ); + ADVANCE(table->total_width); + CHECK_HEIGHT(total_height); } break; default: {} - } - Item *itp = it; it = _get_next_item(it); - if (p_mode == PROCESS_POINTER && r_click_item && itp && !it && p_click_pos.y>p_ofs.y+y+lh) { + if (p_mode == PROCESS_POINTER && r_click_item && itp && !it && p_click_pos.y > p_ofs.y + y + lh) { //at the end of all, return this - if (r_outside) *r_outside=true; - *r_click_item=itp; - *r_click_char=rchar; + if (r_outside) *r_outside = true; + *r_click_item = itp; + *r_click_char = rchar; return; } - if (it && (p_line+1 < p_frame->lines.size()) && p_frame->lines[p_line+1].from==it) { + if (it && (p_line + 1 < p_frame->lines.size()) && p_frame->lines[p_line + 1].from == it) { - if (p_mode==PROCESS_POINTER && r_click_item && p_click_pos.y>=p_ofs.y+y && p_click_pos.y<=p_ofs.y+y+lh) { + if (p_mode == PROCESS_POINTER && r_click_item && p_click_pos.y >= p_ofs.y + y && p_click_pos.y <= p_ofs.y + y + lh) { //went to next line, but pointer was on the previous one - if (r_outside) *r_outside=true; - *r_click_item=itp; - *r_click_char=rchar; + if (r_outside) *r_outside = true; + *r_click_item = itp; + *r_click_char = rchar; return; } @@ -602,7 +563,6 @@ if (m_height > line_height) {\ #undef ENSURE_WIDTH #undef ADVANCE #undef CHECK_HEIGHT - } void RichTextLabel::_scroll_changed(double) { @@ -610,44 +570,40 @@ void RichTextLabel::_scroll_changed(double) { if (updating_scroll) return; - if (scroll_follow && vscroll->get_value()>=(vscroll->get_max()-vscroll->get_page())) - scroll_following=true; + if (scroll_follow && vscroll->get_value() >= (vscroll->get_max() - vscroll->get_page())) + scroll_following = true; else - scroll_following=false; + scroll_following = false; update(); - } void RichTextLabel::_update_scroll() { - int total_height=0; + int total_height = 0; if (main->lines.size()) - total_height=main->lines[main->lines.size()-1].height_accum_cache; + total_height = main->lines[main->lines.size() - 1].height_accum_cache; bool exceeds = total_height > get_size().height && scroll_active; - - if (exceeds!=scroll_visible) { + if (exceeds != scroll_visible) { if (exceeds) { - scroll_visible=true; - main->first_invalid_line=0; - scroll_w=vscroll->get_combined_minimum_size().width; + scroll_visible = true; + main->first_invalid_line = 0; + scroll_w = vscroll->get_combined_minimum_size().width; vscroll->show(); - vscroll->set_anchor_and_margin( MARGIN_LEFT, ANCHOR_END,scroll_w); + vscroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, scroll_w); _validate_line_caches(main); } else { - scroll_visible=false; + scroll_visible = false; vscroll->hide(); - scroll_w=0; + scroll_w = 0; _validate_line_caches(main); } - } - } void RichTextLabel::_notification(int p_what) { @@ -656,7 +612,7 @@ void RichTextLabel::_notification(int p_what) { case NOTIFICATION_RESIZED: { - main->first_invalid_line=0; //invalidate ALL + main->first_invalid_line = 0; //invalidate ALL update(); } break; @@ -665,7 +621,7 @@ void RichTextLabel::_notification(int p_what) { if (bbcode != "") set_bbcode(bbcode); - main->first_invalid_line=0; //invalidate ALL + main->first_invalid_line = 0; //invalidate ALL update(); } break; @@ -683,14 +639,13 @@ void RichTextLabel::_notification(int p_what) { _validate_line_caches(main); _update_scroll(); - - RID ci=get_canvas_item(); + RID ci = get_canvas_item(); Size2 size = get_size(); if (has_focus()) { - VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci,true); - draw_style_box(get_stylebox("focus"),Rect2(Point2(),size)); - VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci,false); + VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, true); + draw_style_box(get_stylebox("focus"), Rect2(Point2(), size)); + VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, false); } int ofs = vscroll->get_value(); @@ -699,36 +654,35 @@ void RichTextLabel::_notification(int p_what) { int from_line = 0; int total_chars = 0; - while (from_line<main->lines.size()) { + while (from_line < main->lines.size()) { - if (main->lines[from_line].height_accum_cache>=ofs) + if (main->lines[from_line].height_accum_cache >= ofs) break; from_line++; - total_chars+=main->lines[from_line].char_count; + total_chars += main->lines[from_line].char_count; } - if (from_line>=main->lines.size()) + if (from_line >= main->lines.size()) break; //nothing to draw int y = (main->lines[from_line].height_accum_cache - main->lines[from_line].height_cache) - ofs; - Ref<Font> base_font=get_font("normal_font"); - Color base_color=get_color("default_color"); + Ref<Font> base_font = get_font("normal_font"); + Color base_color = get_color("default_color"); - while (y<size.height && from_line<main->lines.size()) { + while (y < size.height && from_line < main->lines.size()) { - _process_line(main,Point2(),y,size.width-scroll_w,from_line,PROCESS_DRAW,base_font,base_color,Point2i(),NULL,NULL,NULL,total_chars); - total_chars+=main->lines[from_line].char_count; + _process_line(main, Point2(), y, size.width - scroll_w, from_line, PROCESS_DRAW, base_font, base_color, Point2i(), NULL, NULL, NULL, total_chars); + total_chars += main->lines[from_line].char_count; from_line++; } } } } - -void RichTextLabel::_find_click(ItemFrame* p_frame,const Point2i& p_click,Item **r_click_item,int *r_click_char,bool *r_outside) { +void RichTextLabel::_find_click(ItemFrame *p_frame, const Point2i &p_click, Item **r_click_item, int *r_click_char, bool *r_outside) { if (r_click_item) - *r_click_item=NULL; + *r_click_item = NULL; Size2 size = get_size(); @@ -737,148 +691,137 @@ void RichTextLabel::_find_click(ItemFrame* p_frame,const Point2i& p_click,Item * //todo, change to binary search int from_line = 0; - while (from_line<p_frame->lines.size()) { + while (from_line < p_frame->lines.size()) { - if (p_frame->lines[from_line].height_accum_cache>=ofs) + if (p_frame->lines[from_line].height_accum_cache >= ofs) break; from_line++; } - - if (from_line>=p_frame->lines.size()) + if (from_line >= p_frame->lines.size()) return; - int y = (p_frame->lines[from_line].height_accum_cache - p_frame->lines[from_line].height_cache) - ofs; - Ref<Font> base_font=get_font("normal_font"); - Color base_color=get_color("default_color"); - + Ref<Font> base_font = get_font("normal_font"); + Color base_color = get_color("default_color"); - while (y<size.height && from_line<p_frame->lines.size()) { + while (y < size.height && from_line < p_frame->lines.size()) { - _process_line(p_frame,Point2(),y,size.width-scroll_w,from_line,PROCESS_POINTER,base_font,base_color,p_click,r_click_item,r_click_char,r_outside); + _process_line(p_frame, Point2(), y, size.width - scroll_w, from_line, PROCESS_POINTER, base_font, base_color, p_click, r_click_item, r_click_char, r_outside); if (r_click_item && *r_click_item) return; from_line++; } - - } - -Control::CursorShape RichTextLabel::get_cursor_shape(const Point2& p_pos) const { +Control::CursorShape RichTextLabel::get_cursor_shape(const Point2 &p_pos) const { if (!underline_meta || selection.click) return CURSOR_ARROW; - if (main->first_invalid_line<main->lines.size()) + if (main->first_invalid_line < main->lines.size()) return CURSOR_ARROW; //invalid - int line=0; - Item *item=NULL; - - ((RichTextLabel*)(this))->_find_click(main,p_pos,&item,&line); + int line = 0; + Item *item = NULL; + ((RichTextLabel *)(this))->_find_click(main, p_pos, &item, &line); - if (item && ((RichTextLabel*)(this))->_find_meta(item,NULL)) + if (item && ((RichTextLabel *)(this))->_find_meta(item, NULL)) return CURSOR_POINTING_HAND; return CURSOR_ARROW; } - void RichTextLabel::_gui_input(InputEvent p_event) { - switch(p_event.type) { + switch (p_event.type) { case InputEvent::MOUSE_BUTTON: { - if (main->first_invalid_line<main->lines.size()) + if (main->first_invalid_line < main->lines.size()) return; - const InputEventMouseButton& b = p_event.mouse_button; + const InputEventMouseButton &b = p_event.mouse_button; - if (b.button_index==BUTTON_LEFT) { + if (b.button_index == BUTTON_LEFT) { if (true) { - if (b.pressed && !b.doubleclick) { - int line=0; - Item *item=NULL; + int line = 0; + Item *item = NULL; bool outside; - _find_click(main,Point2i(b.x,b.y),&item,&line,&outside); + _find_click(main, Point2i(b.x, b.y), &item, &line, &outside); if (item) { Variant meta; - if (!outside && _find_meta(item,&meta)) { + if (!outside && _find_meta(item, &meta)) { //meta clicked - emit_signal("meta_clicked",meta); + emit_signal("meta_clicked", meta); } else if (selection.enabled) { - selection.click=item; - selection.click_char=line; - + selection.click = item; + selection.click_char = line; } - } } else if (!b.pressed) { - selection.click=NULL; + selection.click = NULL; } } } - if (b.button_index==BUTTON_WHEEL_UP) { + if (b.button_index == BUTTON_WHEEL_UP) { if (scroll_active) - vscroll->set_value( vscroll->get_value()-vscroll->get_page()/8 ); + vscroll->set_value(vscroll->get_value() - vscroll->get_page() / 8); } - if (b.button_index==BUTTON_WHEEL_DOWN) { + if (b.button_index == BUTTON_WHEEL_DOWN) { if (scroll_active) - vscroll->set_value( vscroll->get_value()+vscroll->get_page()/8 ); + vscroll->set_value(vscroll->get_value() + vscroll->get_page() / 8); } } break; case InputEvent::KEY: { - const InputEventKey &k=p_event.key; + const InputEventKey &k = p_event.key; if (k.pressed && !k.mod.alt && !k.mod.shift && !k.mod.meta) { - bool handled=true; - switch(k.scancode) { + bool handled = true; + switch (k.scancode) { case KEY_PAGEUP: { if (vscroll->is_visible_in_tree()) - vscroll->set_value( vscroll->get_value() - vscroll->get_page() ); + vscroll->set_value(vscroll->get_value() - vscroll->get_page()); } break; case KEY_PAGEDOWN: { if (vscroll->is_visible_in_tree()) - vscroll->set_value( vscroll->get_value() + vscroll->get_page() ); + vscroll->set_value(vscroll->get_value() + vscroll->get_page()); } break; case KEY_UP: { if (vscroll->is_visible_in_tree()) - vscroll->set_value( vscroll->get_value() - get_font("normal_font")->get_height() ); + vscroll->set_value(vscroll->get_value() - get_font("normal_font")->get_height()); } break; case KEY_DOWN: { if (vscroll->is_visible_in_tree()) - vscroll->set_value( vscroll->get_value() + get_font("normal_font")->get_height() ); + vscroll->set_value(vscroll->get_value() + get_font("normal_font")->get_height()); } break; case KEY_HOME: { if (vscroll->is_visible_in_tree()) - vscroll->set_value( 0 ); + vscroll->set_value(0); } break; case KEY_END: { if (vscroll->is_visible_in_tree()) - vscroll->set_value( vscroll->get_max() ); + vscroll->set_value(vscroll->get_max()); } break; case KEY_INSERT: case KEY_C: { @@ -886,14 +829,13 @@ void RichTextLabel::_gui_input(InputEvent p_event) { if (k.mod.command) { selection_copy(); } else { - handled=false; + handled = false; } } break; - default: handled=false; + default: handled = false; } - if (handled) accept_event(); } @@ -901,138 +843,131 @@ void RichTextLabel::_gui_input(InputEvent p_event) { } break; case InputEvent::MOUSE_MOTION: { - if (main->first_invalid_line<main->lines.size()) + if (main->first_invalid_line < main->lines.size()) return; - const InputEventMouseMotion& m = p_event.mouse_motion; + const InputEventMouseMotion &m = p_event.mouse_motion; if (selection.click) { - int line=0; - Item *item=NULL; - _find_click(main,Point2i(m.x,m.y),&item,&line); + int line = 0; + Item *item = NULL; + _find_click(main, Point2i(m.x, m.y), &item, &line); if (!item) return; // do not update + selection.from = selection.click; + selection.from_char = selection.click_char; - selection.from=selection.click; - selection.from_char=selection.click_char; - - selection.to=item; - selection.to_char=line; + selection.to = item; + selection.to_char = line; - bool swap=false; - if (selection.from->index > selection.to->index ) - swap=true; + bool swap = false; + if (selection.from->index > selection.to->index) + swap = true; else if (selection.from->index == selection.to->index) { if (selection.from_char > selection.to_char) - swap=true; + swap = true; else if (selection.from_char == selection.to_char) { - selection.active=false; + selection.active = false; return; } } if (swap) { - SWAP( selection.from, selection.to ); - SWAP( selection.from_char, selection.to_char ); + SWAP(selection.from, selection.to); + SWAP(selection.from_char, selection.to_char); } - selection.active=true; + selection.active = true; update(); - } } break; } - } Ref<Font> RichTextLabel::_find_font(Item *p_item) { - Item *fontitem=p_item; + Item *fontitem = p_item; - while(fontitem) { + while (fontitem) { - if (fontitem->type==ITEM_FONT) { + if (fontitem->type == ITEM_FONT) { - ItemFont *fi = static_cast<ItemFont*>(fontitem); + ItemFont *fi = static_cast<ItemFont *>(fontitem); return fi->font; } - fontitem=fontitem->parent; + fontitem = fontitem->parent; } return Ref<Font>(); } -int RichTextLabel::_find_margin(Item *p_item,const Ref<Font>& p_base_font) { +int RichTextLabel::_find_margin(Item *p_item, const Ref<Font> &p_base_font) { - Item *item=p_item; + Item *item = p_item; - int margin=0; + int margin = 0; - while(item) { + while (item) { - if (item->type==ITEM_INDENT) { + if (item->type == ITEM_INDENT) { - Ref<Font> font=_find_font(item); + Ref<Font> font = _find_font(item); if (font.is_null()) - font=p_base_font; + font = p_base_font; - ItemIndent *indent = static_cast<ItemIndent*>(item); + ItemIndent *indent = static_cast<ItemIndent *>(item); - margin+=indent->level*tab_size*font->get_char_size(' ').width; + margin += indent->level * tab_size * font->get_char_size(' ').width; - } else if (item->type==ITEM_LIST) { + } else if (item->type == ITEM_LIST) { - Ref<Font> font=_find_font(item); + Ref<Font> font = _find_font(item); if (font.is_null()) - font=p_base_font; - + font = p_base_font; } - item=item->parent; + item = item->parent; } return margin; } - RichTextLabel::Align RichTextLabel::_find_align(Item *p_item) { - Item *item=p_item; + Item *item = p_item; - while(item) { + while (item) { - if (item->type==ITEM_ALIGN) { + if (item->type == ITEM_ALIGN) { - ItemAlign *align = static_cast<ItemAlign*>(item); + ItemAlign *align = static_cast<ItemAlign *>(item); return align->align; - } - item=item->parent; + item = item->parent; } return default_align; } -Color RichTextLabel::_find_color(Item *p_item,const Color& p_default_color) { +Color RichTextLabel::_find_color(Item *p_item, const Color &p_default_color) { - Item *item=p_item; + Item *item = p_item; - while(item) { + while (item) { - if (item->type==ITEM_COLOR) { + if (item->type == ITEM_COLOR) { - ItemColor *color = static_cast<ItemColor*>(item); + ItemColor *color = static_cast<ItemColor *>(item); return color->color; - } - item=item->parent; + item = item->parent; } return p_default_color; @@ -1040,182 +975,165 @@ Color RichTextLabel::_find_color(Item *p_item,const Color& p_default_color) { bool RichTextLabel::_find_underline(Item *p_item) { - Item *item=p_item; + Item *item = p_item; - while(item) { + while (item) { - if (item->type==ITEM_UNDERLINE) { + if (item->type == ITEM_UNDERLINE) { return true; - } - item=item->parent; + item = item->parent; } return false; } -bool RichTextLabel::_find_meta(Item *p_item,Variant *r_meta) { +bool RichTextLabel::_find_meta(Item *p_item, Variant *r_meta) { - Item *item=p_item; + Item *item = p_item; - while(item) { + while (item) { - if (item->type==ITEM_META) { + if (item->type == ITEM_META) { - ItemMeta *meta = static_cast<ItemMeta*>(item); + ItemMeta *meta = static_cast<ItemMeta *>(item); if (r_meta) - *r_meta=meta->meta; + *r_meta = meta->meta; return true; - } - item=item->parent; + item = item->parent; } return false; - } -void RichTextLabel::_validate_line_caches(ItemFrame* p_frame) { +void RichTextLabel::_validate_line_caches(ItemFrame *p_frame) { - if (p_frame->first_invalid_line==p_frame->lines.size()) + if (p_frame->first_invalid_line == p_frame->lines.size()) return; //validate invalid lines!s Size2 size = get_size(); - Ref<Font> base_font=get_font("normal_font"); - - for(int i=p_frame->first_invalid_line;i<p_frame->lines.size();i++) { - - int y=0; - _process_line(p_frame,Point2(),y,size.width-scroll_w,i,PROCESS_CACHE,base_font,Color()); - p_frame->lines[i].height_cache=y; - p_frame->lines[i].height_accum_cache=y; + Ref<Font> base_font = get_font("normal_font"); - if (i>0) - p_frame->lines[i].height_accum_cache+=p_frame->lines[i-1].height_accum_cache; + for (int i = p_frame->first_invalid_line; i < p_frame->lines.size(); i++) { + int y = 0; + _process_line(p_frame, Point2(), y, size.width - scroll_w, i, PROCESS_CACHE, base_font, Color()); + p_frame->lines[i].height_cache = y; + p_frame->lines[i].height_accum_cache = y; + if (i > 0) + p_frame->lines[i].height_accum_cache += p_frame->lines[i - 1].height_accum_cache; } - int total_height=0; + int total_height = 0; if (p_frame->lines.size()) - total_height=p_frame->lines[p_frame->lines.size()-1].height_accum_cache; + total_height = p_frame->lines[p_frame->lines.size() - 1].height_accum_cache; - main->first_invalid_line=p_frame->lines.size(); + main->first_invalid_line = p_frame->lines.size(); - updating_scroll=true; + updating_scroll = true; vscroll->set_max(total_height); vscroll->set_page(size.height); if (scroll_follow && scroll_following) - vscroll->set_value(total_height-size.height); - - updating_scroll=false; + vscroll->set_value(total_height - size.height); + updating_scroll = false; } +void RichTextLabel::_invalidate_current_line(ItemFrame *p_frame) { -void RichTextLabel::_invalidate_current_line(ItemFrame* p_frame) { - - if (p_frame->lines.size()-1 <= p_frame->first_invalid_line) { + if (p_frame->lines.size() - 1 <= p_frame->first_invalid_line) { - p_frame->first_invalid_line=p_frame->lines.size()-1; + p_frame->first_invalid_line = p_frame->lines.size() - 1; update(); } } -void RichTextLabel::add_text(const String& p_text) { +void RichTextLabel::add_text(const String &p_text) { - if (current->type==ITEM_TABLE) + if (current->type == ITEM_TABLE) return; //can't add anything here - int pos=0; + int pos = 0; - while (pos<p_text.length()) { + while (pos < p_text.length()) { - int end=p_text.find("\n",pos); + int end = p_text.find("\n", pos); String line; - bool eol=false; - if (end==-1) { + bool eol = false; + if (end == -1) { - end=p_text.length(); + end = p_text.length(); } else { - eol=true; + eol = true; } - if (pos==0 && end==p_text.length()) - line=p_text; + if (pos == 0 && end == p_text.length()) + line = p_text; else - line=p_text.substr(pos,end-pos); + line = p_text.substr(pos, end - pos); - if (line.length()>0) { + if (line.length() > 0) { - if (current->subitems.size() && current->subitems.back()->get()->type==ITEM_TEXT) { + if (current->subitems.size() && current->subitems.back()->get()->type == ITEM_TEXT) { //append text condition! - ItemText *ti = static_cast<ItemText*>(current->subitems.back()->get()); - ti->text+=line; + ItemText *ti = static_cast<ItemText *>(current->subitems.back()->get()); + ti->text += line; _invalidate_current_line(main); } else { //append item condition - ItemText *item = memnew( ItemText ); - item->text=line; - _add_item(item,false); - + ItemText *item = memnew(ItemText); + item->text = line; + _add_item(item, false); } - - } if (eol) { - ItemNewline *item = memnew( ItemNewline ); - item->line=current_frame->lines.size(); - _add_item(item,false); - current_frame->lines.resize(current_frame->lines.size()+1); - if (item->type!=ITEM_NEWLINE) - current_frame->lines[current_frame->lines.size()-1].from=item; + ItemNewline *item = memnew(ItemNewline); + item->line = current_frame->lines.size(); + _add_item(item, false); + current_frame->lines.resize(current_frame->lines.size() + 1); + if (item->type != ITEM_NEWLINE) + current_frame->lines[current_frame->lines.size() - 1].from = item; _invalidate_current_line(current_frame); - } - pos=end+1; + pos = end + 1; } } void RichTextLabel::_add_item(Item *p_item, bool p_enter, bool p_ensure_newline) { - - - p_item->parent=current; - p_item->E=current->subitems.push_back(p_item); - p_item->index=current_idx++; - + p_item->parent = current; + p_item->E = current->subitems.push_back(p_item); + p_item->index = current_idx++; if (p_enter) - current=p_item; + current = p_item; - if (p_ensure_newline && current_frame->lines[current_frame->lines.size()-1].from) { + if (p_ensure_newline && current_frame->lines[current_frame->lines.size() - 1].from) { _invalidate_current_line(current_frame); - current_frame->lines.resize( current_frame->lines.size() +1 ); - + current_frame->lines.resize(current_frame->lines.size() + 1); } - if (current_frame->lines[current_frame->lines.size()-1].from==NULL) { - current_frame->lines[current_frame->lines.size()-1].from=p_item; + if (current_frame->lines[current_frame->lines.size() - 1].from == NULL) { + current_frame->lines[current_frame->lines.size() - 1].from = p_item; } - p_item->line=current_frame->lines.size()-1; + p_item->line = current_frame->lines.size() - 1; _invalidate_current_line(current_frame); - } -void RichTextLabel::_remove_item(Item* p_item, const int p_line, const int p_subitem_line) { - +void RichTextLabel::_remove_item(Item *p_item, const int p_line, const int p_subitem_line) { int size = p_item->subitems.size(); if (size == 0) { @@ -1227,37 +1145,33 @@ void RichTextLabel::_remove_item(Item* p_item, const int p_line, const int p_sub current->subitems[i]->line--; } } - } - else { + } else { for (int i = 0; i < size; i++) { _remove_item(p_item->subitems.front()->get(), p_line, p_subitem_line); } } - } -void RichTextLabel::add_image(const Ref<Texture>& p_image) { +void RichTextLabel::add_image(const Ref<Texture> &p_image) { - if (current->type==ITEM_TABLE) + if (current->type == ITEM_TABLE) return; ERR_FAIL_COND(p_image.is_null()); - ItemImage *item = memnew( ItemImage ); - - item->image=p_image; - _add_item(item,false); + ItemImage *item = memnew(ItemImage); + item->image = p_image; + _add_item(item, false); } void RichTextLabel::add_newline() { - if (current->type==ITEM_TABLE) + if (current->type == ITEM_TABLE) return; - ItemNewline *item = memnew( ItemNewline ); - item->line=current_frame->lines.size(); - current_frame->lines.resize(current_frame->lines.size()+1); - _add_item(item,false); - + ItemNewline *item = memnew(ItemNewline); + item->line = current_frame->lines.size(); + current_frame->lines.resize(current_frame->lines.size() + 1); + _add_item(item, false); } bool RichTextLabel::remove_line(const int p_line) { @@ -1280,155 +1194,144 @@ bool RichTextLabel::remove_line(const int p_line) { return true; } -void RichTextLabel::push_font(const Ref<Font>& p_font) { +void RichTextLabel::push_font(const Ref<Font> &p_font) { - ERR_FAIL_COND(current->type==ITEM_TABLE); + ERR_FAIL_COND(current->type == ITEM_TABLE); ERR_FAIL_COND(p_font.is_null()); - ItemFont *item = memnew( ItemFont ); - - item->font=p_font; - _add_item(item,true); + ItemFont *item = memnew(ItemFont); + item->font = p_font; + _add_item(item, true); } -void RichTextLabel::push_color(const Color& p_color) { +void RichTextLabel::push_color(const Color &p_color) { - ERR_FAIL_COND(current->type==ITEM_TABLE); - ItemColor *item = memnew( ItemColor ); - - item->color=p_color; - _add_item(item,true); + ERR_FAIL_COND(current->type == ITEM_TABLE); + ItemColor *item = memnew(ItemColor); + item->color = p_color; + _add_item(item, true); } void RichTextLabel::push_underline() { - ERR_FAIL_COND(current->type==ITEM_TABLE); - ItemUnderline *item = memnew( ItemUnderline ); - - _add_item(item,true); + ERR_FAIL_COND(current->type == ITEM_TABLE); + ItemUnderline *item = memnew(ItemUnderline); + _add_item(item, true); } void RichTextLabel::push_align(Align p_align) { - ERR_FAIL_COND(current->type==ITEM_TABLE); - - ItemAlign *item = memnew( ItemAlign ); - item->align=p_align; - _add_item(item,true,true); + ERR_FAIL_COND(current->type == ITEM_TABLE); + ItemAlign *item = memnew(ItemAlign); + item->align = p_align; + _add_item(item, true, true); } void RichTextLabel::push_indent(int p_level) { - ERR_FAIL_COND(current->type==ITEM_TABLE); - ERR_FAIL_COND(p_level<0); - - ItemIndent *item = memnew( ItemIndent ); - item->level=p_level; - _add_item(item,true,true); + ERR_FAIL_COND(current->type == ITEM_TABLE); + ERR_FAIL_COND(p_level < 0); + ItemIndent *item = memnew(ItemIndent); + item->level = p_level; + _add_item(item, true, true); } void RichTextLabel::push_list(ListType p_list) { - ERR_FAIL_COND(current->type==ITEM_TABLE); - ERR_FAIL_INDEX(p_list,3); + ERR_FAIL_COND(current->type == ITEM_TABLE); + ERR_FAIL_INDEX(p_list, 3); - ItemList *item = memnew( ItemList ); - - item->list_type=p_list; - _add_item(item,true,true); + ItemList *item = memnew(ItemList); + item->list_type = p_list; + _add_item(item, true, true); } -void RichTextLabel::push_meta(const Variant& p_meta) { - - ERR_FAIL_COND(current->type==ITEM_TABLE); - ItemMeta *item = memnew( ItemMeta ); +void RichTextLabel::push_meta(const Variant &p_meta) { - item->meta=p_meta; - _add_item(item,true); + ERR_FAIL_COND(current->type == ITEM_TABLE); + ItemMeta *item = memnew(ItemMeta); + item->meta = p_meta; + _add_item(item, true); } void RichTextLabel::push_table(int p_columns) { - ERR_FAIL_COND( p_columns < 1); - ItemTable *item = memnew( ItemTable ); + ERR_FAIL_COND(p_columns < 1); + ItemTable *item = memnew(ItemTable); item->columns.resize(p_columns); - item->total_width=0; - for(int i=0;i<item->columns.size();i++) { - item->columns[i].expand=false; - item->columns[i].expand_ratio=1; + item->total_width = 0; + for (int i = 0; i < item->columns.size(); i++) { + item->columns[i].expand = false; + item->columns[i].expand_ratio = 1; } - _add_item(item,true,true); - + _add_item(item, true, true); } -void RichTextLabel::set_table_column_expand(int p_column,bool p_expand,int p_ratio) { +void RichTextLabel::set_table_column_expand(int p_column, bool p_expand, int p_ratio) { - ERR_FAIL_COND(current->type!=ITEM_TABLE); - ItemTable *table = static_cast<ItemTable*>(current); - ERR_FAIL_INDEX(p_column,table->columns.size()); - table->columns[p_column].expand=p_expand; - table->columns[p_column].expand_ratio=p_ratio; + ERR_FAIL_COND(current->type != ITEM_TABLE); + ItemTable *table = static_cast<ItemTable *>(current); + ERR_FAIL_INDEX(p_column, table->columns.size()); + table->columns[p_column].expand = p_expand; + table->columns[p_column].expand_ratio = p_ratio; } -void RichTextLabel::push_cell(){ +void RichTextLabel::push_cell() { - ERR_FAIL_COND(current->type!=ITEM_TABLE); + ERR_FAIL_COND(current->type != ITEM_TABLE); - ItemFrame *item = memnew( ItemFrame ); - item->parent_frame=current_frame; - _add_item(item,true); - current_frame=item; - item->cell=true; - item->parent_line=item->parent_frame->lines.size()-1; + ItemFrame *item = memnew(ItemFrame); + item->parent_frame = current_frame; + _add_item(item, true); + current_frame = item; + item->cell = true; + item->parent_line = item->parent_frame->lines.size() - 1; item->lines.resize(1); - item->lines[0].from=NULL; - item->first_invalid_line=0; - + item->lines[0].from = NULL; + item->first_invalid_line = 0; } int RichTextLabel::get_current_table_column() const { - ERR_FAIL_COND_V(current->type!=ITEM_TABLE,-1); + ERR_FAIL_COND_V(current->type != ITEM_TABLE, -1); - ItemTable *table = static_cast<ItemTable*>(current); + ItemTable *table = static_cast<ItemTable *>(current); return table->subitems.size() % table->columns.size(); - } void RichTextLabel::pop() { ERR_FAIL_COND(!current->parent); - if (current->type==ITEM_FRAME) { - current_frame = static_cast<ItemFrame*>(current)->parent_frame; + if (current->type == ITEM_FRAME) { + current_frame = static_cast<ItemFrame *>(current)->parent_frame; } - current=current->parent; + current = current->parent; } void RichTextLabel::clear() { main->_clear_children(); - current=main; - current_frame=main; + current = main; + current_frame = main; main->lines.clear(); main->lines.resize(1); - main->first_invalid_line=0; + main->first_invalid_line = 0; update(); - selection.click=NULL; - selection.active=false; - current_idx=1; - + selection.click = NULL; + selection.active = false; + current_idx = 1; } void RichTextLabel::set_tab_size(int p_spaces) { - tab_size=p_spaces; - main->first_invalid_line=0; + tab_size = p_spaces; + main->first_invalid_line = 0; update(); } @@ -1437,10 +1340,9 @@ int RichTextLabel::get_tab_size() const { return tab_size; } - void RichTextLabel::set_meta_underline(bool p_underline) { - underline_meta=p_underline; + underline_meta = p_underline; update(); } @@ -1456,10 +1358,10 @@ void RichTextLabel::set_offset(int p_pixel) { void RichTextLabel::set_scroll_active(bool p_active) { - if (scroll_active==p_active) + if (scroll_active == p_active) return; - scroll_active=p_active; + scroll_active = p_active; update(); } @@ -1470,9 +1372,9 @@ bool RichTextLabel::is_scroll_active() const { void RichTextLabel::set_scroll_follow(bool p_follow) { - scroll_follow=p_follow; - if (!vscroll->is_visible_in_tree() || vscroll->get_value()>=(vscroll->get_max()-vscroll->get_page())) - scroll_following=true; + scroll_follow = p_follow; + if (!vscroll->is_visible_in_tree() || vscroll->get_value() >= (vscroll->get_max() - vscroll->get_page())) + scroll_following = true; } bool RichTextLabel::is_scroll_following() const { @@ -1480,71 +1382,65 @@ bool RichTextLabel::is_scroll_following() const { return scroll_follow; } -Error RichTextLabel::parse_bbcode(const String& p_bbcode) { +Error RichTextLabel::parse_bbcode(const String &p_bbcode) { clear(); return append_bbcode(p_bbcode); } -Error RichTextLabel::append_bbcode(const String& p_bbcode) { +Error RichTextLabel::append_bbcode(const String &p_bbcode) { int pos = 0; List<String> tag_stack; - Ref<Font> normal_font=get_font("normal_font"); - Ref<Font> bold_font=get_font("bold_font"); - Ref<Font> italics_font=get_font("italics_font"); - Ref<Font> bold_italics_font=get_font("bold_italics_font"); - Ref<Font> mono_font=get_font("mono_font"); + Ref<Font> normal_font = get_font("normal_font"); + Ref<Font> bold_font = get_font("bold_font"); + Ref<Font> italics_font = get_font("italics_font"); + Ref<Font> bold_italics_font = get_font("bold_italics_font"); + Ref<Font> mono_font = get_font("mono_font"); - Color base_color=get_color("default_color"); + Color base_color = get_color("default_color"); - int indent_level=0; + int indent_level = 0; - bool in_bold=false; - bool in_italics=false; + bool in_bold = false; + bool in_italics = false; - while(pos < p_bbcode.length()) { + while (pos < p_bbcode.length()) { + int brk_pos = p_bbcode.find("[", pos); - int brk_pos = p_bbcode.find("[",pos); - - if (brk_pos<0) - brk_pos=p_bbcode.length(); + if (brk_pos < 0) + brk_pos = p_bbcode.length(); if (brk_pos > pos) { - add_text(p_bbcode.substr(pos,brk_pos-pos)); + add_text(p_bbcode.substr(pos, brk_pos - pos)); } - if (brk_pos==p_bbcode.length()) + if (brk_pos == p_bbcode.length()) break; //nothing else o add - int brk_end = p_bbcode.find("]",brk_pos+1); + int brk_end = p_bbcode.find("]", brk_pos + 1); - if (brk_end==-1) { + if (brk_end == -1) { //no close, add the rest - add_text(p_bbcode.substr(brk_pos,p_bbcode.length()-brk_pos)); + add_text(p_bbcode.substr(brk_pos, p_bbcode.length() - brk_pos)); break; } - - String tag = p_bbcode.substr(brk_pos+1,brk_end-brk_pos-1); - + String tag = p_bbcode.substr(brk_pos + 1, brk_end - brk_pos - 1); if (tag.begins_with("/") && tag_stack.size()) { - bool tag_ok = tag_stack.size() && tag_stack.front()->get()==tag.substr(1,tag.length()); - + bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1, tag.length()); - - if (tag_stack.front()->get()=="b") - in_bold=false; - if (tag_stack.front()->get()=="i") - in_italics=false; - if (tag_stack.front()->get()=="indent") + if (tag_stack.front()->get() == "b") + in_bold = false; + if (tag_stack.front()->get() == "i") + in_italics = false; + if (tag_stack.front()->get() == "indent") indent_level--; - if (!tag_ok) { add_text("["); @@ -1553,223 +1449,216 @@ Error RichTextLabel::append_bbcode(const String& p_bbcode) { } tag_stack.pop_front(); - pos=brk_end+1; - if (tag!="/img") + pos = brk_end + 1; + if (tag != "/img") pop(); - } else if (tag=="b") { + } else if (tag == "b") { //use bold font - in_bold=true; + in_bold = true; if (in_italics) push_font(bold_italics_font); else push_font(bold_font); - pos=brk_end+1; + pos = brk_end + 1; tag_stack.push_front(tag); - } else if (tag=="i") { + } else if (tag == "i") { //use italics font - in_italics=true; + in_italics = true; if (in_bold) push_font(bold_italics_font); else push_font(italics_font); - pos=brk_end+1; + pos = brk_end + 1; tag_stack.push_front(tag); - } else if (tag=="code") { + } else if (tag == "code") { //use monospace font push_font(mono_font); - pos=brk_end+1; + pos = brk_end + 1; tag_stack.push_front(tag); } else if (tag.begins_with("table=")) { - int columns = tag.substr(6,tag.length()).to_int(); - if (columns<1) - columns=1; + int columns = tag.substr(6, tag.length()).to_int(); + if (columns < 1) + columns = 1; //use monospace font push_table(columns); - pos=brk_end+1; + pos = brk_end + 1; tag_stack.push_front("table"); - } else if (tag=="cell") { + } else if (tag == "cell") { push_cell(); - pos=brk_end+1; + pos = brk_end + 1; tag_stack.push_front(tag); } else if (tag.begins_with("cell=")) { - int ratio = tag.substr(6,tag.length()).to_int(); - if (ratio<1) - ratio=1; + int ratio = tag.substr(6, tag.length()).to_int(); + if (ratio < 1) + ratio = 1; //use monospace font - set_table_column_expand(get_current_table_column(),true,ratio); + set_table_column_expand(get_current_table_column(), true, ratio); push_cell(); - pos=brk_end+1; + pos = brk_end + 1; tag_stack.push_front("cell"); - } else if (tag=="u") { + } else if (tag == "u") { //use underline push_underline(); - pos=brk_end+1; + pos = brk_end + 1; tag_stack.push_front(tag); - } else if (tag=="s") { + } else if (tag == "s") { //use strikethrough (not supported underline instead) push_underline(); - pos=brk_end+1; + pos = brk_end + 1; tag_stack.push_front(tag); - } else if (tag=="center") { + } else if (tag == "center") { //use underline push_align(ALIGN_CENTER); - pos=brk_end+1; + pos = brk_end + 1; tag_stack.push_front(tag); - } else if (tag=="fill") { + } else if (tag == "fill") { //use underline push_align(ALIGN_FILL); - pos=brk_end+1; + pos = brk_end + 1; tag_stack.push_front(tag); - } else if (tag=="right") { + } else if (tag == "right") { //use underline push_align(ALIGN_RIGHT); - pos=brk_end+1; + pos = brk_end + 1; tag_stack.push_front(tag); - } else if (tag=="ul") { + } else if (tag == "ul") { //use underline push_list(LIST_DOTS); - pos=brk_end+1; + pos = brk_end + 1; tag_stack.push_front(tag); - } else if (tag=="ol") { + } else if (tag == "ol") { //use underline push_list(LIST_NUMBERS); - pos=brk_end+1; + pos = brk_end + 1; tag_stack.push_front(tag); - } else if (tag=="indent") { + } else if (tag == "indent") { //use underline indent_level++; push_indent(indent_level); - pos=brk_end+1; + pos = brk_end + 1; tag_stack.push_front(tag); - } else if (tag=="url") { + } else if (tag == "url") { //use strikethrough (not supported underline instead) - int end=p_bbcode.find("[",brk_end); - if (end==-1) - end=p_bbcode.length(); - String url = p_bbcode.substr(brk_end+1,end-brk_end-1); + int end = p_bbcode.find("[", brk_end); + if (end == -1) + end = p_bbcode.length(); + String url = p_bbcode.substr(brk_end + 1, end - brk_end - 1); push_meta(url); - pos=brk_end+1; + pos = brk_end + 1; tag_stack.push_front(tag); } else if (tag.begins_with("url=")) { - String url = tag.substr(4,tag.length()); + String url = tag.substr(4, tag.length()); push_meta(url); - pos=brk_end+1; + pos = brk_end + 1; tag_stack.push_front("url"); - } else if (tag=="img") { + } else if (tag == "img") { //use strikethrough (not supported underline instead) - int end=p_bbcode.find("[",brk_end); - if (end==-1) - end=p_bbcode.length(); - String image = p_bbcode.substr(brk_end+1,end-brk_end-1); + int end = p_bbcode.find("[", brk_end); + if (end == -1) + end = p_bbcode.length(); + String image = p_bbcode.substr(brk_end + 1, end - brk_end - 1); - Ref<Texture> texture = ResourceLoader::load(image,"Texture"); + Ref<Texture> texture = ResourceLoader::load(image, "Texture"); if (texture.is_valid()) add_image(texture); - pos=end; + pos = end; tag_stack.push_front(tag); } else if (tag.begins_with("color=")) { - String col = tag.substr(6,tag.length()); + String col = tag.substr(6, tag.length()); Color color; if (col.begins_with("#")) - color=Color::html(col); - else if (col=="aqua") - color=Color::html("#00FFFF"); - else if (col=="black") - color=Color::html("#000000"); - else if (col=="blue") - color=Color::html("#0000FF"); - else if (col=="fuchsia") - color=Color::html("#FF00FF"); - else if (col=="gray" || col=="grey") - color=Color::html("#808080"); - else if (col=="green") - color=Color::html("#008000"); - else if (col=="lime") - color=Color::html("#00FF00"); - else if (col=="maroon") - color=Color::html("#800000"); - else if (col=="navy") - color=Color::html("#000080"); - else if (col=="olive") - color=Color::html("#808000"); - else if (col=="purple") - color=Color::html("#800080"); - else if (col=="red") - color=Color::html("#FF0000"); - else if (col=="silver") - color=Color::html("#C0C0C0"); - else if (col=="teal") - color=Color::html("#008008"); - else if (col=="white") - color=Color::html("#FFFFFF"); - else if (col=="yellow") - color=Color::html("#FFFF00"); + color = Color::html(col); + else if (col == "aqua") + color = Color::html("#00FFFF"); + else if (col == "black") + color = Color::html("#000000"); + else if (col == "blue") + color = Color::html("#0000FF"); + else if (col == "fuchsia") + color = Color::html("#FF00FF"); + else if (col == "gray" || col == "grey") + color = Color::html("#808080"); + else if (col == "green") + color = Color::html("#008000"); + else if (col == "lime") + color = Color::html("#00FF00"); + else if (col == "maroon") + color = Color::html("#800000"); + else if (col == "navy") + color = Color::html("#000080"); + else if (col == "olive") + color = Color::html("#808000"); + else if (col == "purple") + color = Color::html("#800080"); + else if (col == "red") + color = Color::html("#FF0000"); + else if (col == "silver") + color = Color::html("#C0C0C0"); + else if (col == "teal") + color = Color::html("#008008"); + else if (col == "white") + color = Color::html("#FFFFFF"); + else if (col == "yellow") + color = Color::html("#FFFF00"); else - color=base_color; - - + color = base_color; push_color(color); - pos=brk_end+1; + pos = brk_end + 1; tag_stack.push_front("color"); } else if (tag.begins_with("font=")) { - String fnt = tag.substr(5,tag.length()); + String fnt = tag.substr(5, tag.length()); - - Ref<Font> font = ResourceLoader::load(fnt,"Font"); + Ref<Font> font = ResourceLoader::load(fnt, "Font"); if (font.is_valid()) push_font(font); else push_font(normal_font); - pos=brk_end+1; + pos = brk_end + 1; tag_stack.push_front("font"); - } else { add_text("["); //ignore - pos=brk_pos+1; - + pos = brk_pos + 1; } } return OK; } - void RichTextLabel::scroll_to_line(int p_line) { - ERR_FAIL_INDEX(p_line,main->lines.size()); + ERR_FAIL_INDEX(p_line, main->lines.size()); _validate_line_caches(main); - vscroll->set_value(main->lines[p_line].height_accum_cache-main->lines[p_line].height_cache); - + vscroll->set_value(main->lines[p_line].height_accum_cache - main->lines[p_line].height_cache); } int RichTextLabel::get_line_count() const { @@ -1779,75 +1668,72 @@ int RichTextLabel::get_line_count() const { void RichTextLabel::set_selection_enabled(bool p_enabled) { - selection.enabled=p_enabled; + selection.enabled = p_enabled; if (!p_enabled) { if (selection.active) { - selection.active=false; + selection.active = false; update(); } set_focus_mode(FOCUS_NONE); } else { set_focus_mode(FOCUS_ALL); } - } -bool RichTextLabel::search(const String& p_string,bool p_from_selection) { +bool RichTextLabel::search(const String &p_string, bool p_from_selection) { - ERR_FAIL_COND_V(!selection.enabled,false); - Item *it=main; - int charidx=0; + ERR_FAIL_COND_V(!selection.enabled, false); + Item *it = main; + int charidx = 0; if (p_from_selection && selection.active && selection.enabled) { - it=selection.to; - charidx=selection.to_char+1; + it = selection.to; + charidx = selection.to_char + 1; } - while(it) { + while (it) { - if (it->type==ITEM_TEXT) { + if (it->type == ITEM_TEXT) { - ItemText *t = static_cast<ItemText*>(it); - int sp = t->text.find(p_string,charidx); - if (sp!=-1) { - selection.from=it; - selection.from_char=sp; - selection.to=it; - selection.to_char=sp+p_string.length()-1; - selection.active=true; + ItemText *t = static_cast<ItemText *>(it); + int sp = t->text.find(p_string, charidx); + if (sp != -1) { + selection.from = it; + selection.from_char = sp; + selection.to = it; + selection.to_char = sp + p_string.length() - 1; + selection.active = true; update(); _validate_line_caches(main); - int fh = _find_font(t).is_valid()?_find_font(t)->get_height():get_font("normal_font")->get_height(); + int fh = _find_font(t).is_valid() ? _find_font(t)->get_height() : get_font("normal_font")->get_height(); - float offset =0; + float offset = 0; int line = t->line; - Item *item =t; - while(item) { - if (item->type==ITEM_FRAME) { - ItemFrame *frame = static_cast<ItemFrame*>(item); - if (line>=0 && line<frame->lines.size()) { - offset+=frame->lines[line].height_accum_cache-frame->lines[line].height_cache; - line=frame->line; + Item *item = t; + while (item) { + if (item->type == ITEM_FRAME) { + ItemFrame *frame = static_cast<ItemFrame *>(item); + if (line >= 0 && line < frame->lines.size()) { + offset += frame->lines[line].height_accum_cache - frame->lines[line].height_cache; + line = frame->line; } } - item=item->parent; + item = item->parent; } - vscroll->set_value(offset-fh); + vscroll->set_value(offset - fh); return true; } } - it=_get_next_item(it,true); - charidx=0; - + it = _get_next_item(it, true); + charidx = 0; } return false; - } void RichTextLabel::selection_copy() { @@ -1857,37 +1743,36 @@ void RichTextLabel::selection_copy() { String text; - RichTextLabel::Item *item=selection.from; + RichTextLabel::Item *item = selection.from; - while(item) { + while (item) { - if (item->type==ITEM_TEXT) { + if (item->type == ITEM_TEXT) { - String itext = static_cast<ItemText*>(item)->text; - if (item==selection.from && item==selection.to) { - text+=itext.substr(selection.from_char,selection.to_char-selection.from_char+1); - } else if (item==selection.from) { - text+=itext.substr(selection.from_char,itext.size()); - } else if (item==selection.to) { - text+=itext.substr(0,selection.to_char+1); + String itext = static_cast<ItemText *>(item)->text; + if (item == selection.from && item == selection.to) { + text += itext.substr(selection.from_char, selection.to_char - selection.from_char + 1); + } else if (item == selection.from) { + text += itext.substr(selection.from_char, itext.size()); + } else if (item == selection.to) { + text += itext.substr(0, selection.to_char + 1); } else { - text+=itext; + text += itext; } - } else if (item->type==ITEM_NEWLINE) { - text+="\n"; + } else if (item->type == ITEM_NEWLINE) { + text += "\n"; } - if (item==selection.to) + if (item == selection.to) break; - item=_get_next_item(item,true); + item = _get_next_item(item, true); } - if (text!="") { + if (text != "") { OS::get_singleton()->set_clipboard(text); //print_line("COPY: "+text); } - } bool RichTextLabel::is_selection_enabled() const { @@ -1895,8 +1780,8 @@ bool RichTextLabel::is_selection_enabled() const { return selection.enabled; } -void RichTextLabel::set_bbcode(const String& p_bbcode) { - bbcode=p_bbcode; +void RichTextLabel::set_bbcode(const String &p_bbcode) { + bbcode = p_bbcode; if (is_inside_tree() && use_bbcode) parse_bbcode(p_bbcode); else { // raw text @@ -1911,9 +1796,9 @@ String RichTextLabel::get_bbcode() const { } void RichTextLabel::set_use_bbcode(bool p_enable) { - if (use_bbcode==p_enable) + if (use_bbcode == p_enable) return; - use_bbcode=p_enable; + use_bbcode = p_enable; set_bbcode(bbcode); } @@ -1927,109 +1812,106 @@ String RichTextLabel::get_text() { Item *it = main; while (it) { if (it->type == ITEM_TEXT) { - ItemText *t = static_cast<ItemText*>(it); + ItemText *t = static_cast<ItemText *>(it); text += t->text; } else if (it->type == ITEM_NEWLINE) { text += "\n"; } else if (it->type == ITEM_INDENT) { text += "\t"; } - it=_get_next_item(it,true); + it = _get_next_item(it, true); } return text; } void RichTextLabel::_bind_methods() { + ClassDB::bind_method(D_METHOD("_gui_input"), &RichTextLabel::_gui_input); + ClassDB::bind_method(D_METHOD("_scroll_changed"), &RichTextLabel::_scroll_changed); + ClassDB::bind_method(D_METHOD("get_text"), &RichTextLabel::get_text); + ClassDB::bind_method(D_METHOD("add_text", "text"), &RichTextLabel::add_text); + ClassDB::bind_method(D_METHOD("add_image", "image:Texture"), &RichTextLabel::add_image); + ClassDB::bind_method(D_METHOD("newline"), &RichTextLabel::add_newline); + ClassDB::bind_method(D_METHOD("remove_line"), &RichTextLabel::remove_line); + ClassDB::bind_method(D_METHOD("push_font", "font"), &RichTextLabel::push_font); + ClassDB::bind_method(D_METHOD("push_color", "color"), &RichTextLabel::push_color); + ClassDB::bind_method(D_METHOD("push_align", "align"), &RichTextLabel::push_align); + ClassDB::bind_method(D_METHOD("push_indent", "level"), &RichTextLabel::push_indent); + ClassDB::bind_method(D_METHOD("push_list", "type"), &RichTextLabel::push_list); + ClassDB::bind_method(D_METHOD("push_meta", "data"), &RichTextLabel::push_meta); + ClassDB::bind_method(D_METHOD("push_underline"), &RichTextLabel::push_underline); + ClassDB::bind_method(D_METHOD("push_table", "columns"), &RichTextLabel::push_table); + ClassDB::bind_method(D_METHOD("set_table_column_expand", "column", "expand", "ratio"), &RichTextLabel::set_table_column_expand); + ClassDB::bind_method(D_METHOD("push_cell"), &RichTextLabel::push_cell); + ClassDB::bind_method(D_METHOD("pop"), &RichTextLabel::pop); - ClassDB::bind_method(D_METHOD("_gui_input"),&RichTextLabel::_gui_input); - ClassDB::bind_method(D_METHOD("_scroll_changed"),&RichTextLabel::_scroll_changed); - ClassDB::bind_method(D_METHOD("get_text"),&RichTextLabel::get_text); - ClassDB::bind_method(D_METHOD("add_text","text"),&RichTextLabel::add_text); - ClassDB::bind_method(D_METHOD("add_image","image:Texture"),&RichTextLabel::add_image); - ClassDB::bind_method(D_METHOD("newline"),&RichTextLabel::add_newline); - ClassDB::bind_method(D_METHOD("remove_line"),&RichTextLabel::remove_line); - ClassDB::bind_method(D_METHOD("push_font","font"),&RichTextLabel::push_font); - ClassDB::bind_method(D_METHOD("push_color","color"),&RichTextLabel::push_color); - ClassDB::bind_method(D_METHOD("push_align","align"),&RichTextLabel::push_align); - ClassDB::bind_method(D_METHOD("push_indent","level"),&RichTextLabel::push_indent); - ClassDB::bind_method(D_METHOD("push_list","type"),&RichTextLabel::push_list); - ClassDB::bind_method(D_METHOD("push_meta","data"),&RichTextLabel::push_meta); - ClassDB::bind_method(D_METHOD("push_underline"),&RichTextLabel::push_underline); - ClassDB::bind_method(D_METHOD("push_table","columns"),&RichTextLabel::push_table); - ClassDB::bind_method(D_METHOD("set_table_column_expand","column","expand","ratio"),&RichTextLabel::set_table_column_expand); - ClassDB::bind_method(D_METHOD("push_cell"),&RichTextLabel::push_cell); - ClassDB::bind_method(D_METHOD("pop"),&RichTextLabel::pop); - - ClassDB::bind_method(D_METHOD("clear"),&RichTextLabel::clear); - - ClassDB::bind_method(D_METHOD("set_meta_underline","enable"),&RichTextLabel::set_meta_underline); - ClassDB::bind_method(D_METHOD("is_meta_underlined"),&RichTextLabel::is_meta_underlined); + ClassDB::bind_method(D_METHOD("clear"), &RichTextLabel::clear); - ClassDB::bind_method(D_METHOD("set_scroll_active","active"),&RichTextLabel::set_scroll_active); - ClassDB::bind_method(D_METHOD("is_scroll_active"),&RichTextLabel::is_scroll_active); + ClassDB::bind_method(D_METHOD("set_meta_underline", "enable"), &RichTextLabel::set_meta_underline); + ClassDB::bind_method(D_METHOD("is_meta_underlined"), &RichTextLabel::is_meta_underlined); - ClassDB::bind_method(D_METHOD("set_scroll_follow","follow"),&RichTextLabel::set_scroll_follow); - ClassDB::bind_method(D_METHOD("is_scroll_following"),&RichTextLabel::is_scroll_following); + ClassDB::bind_method(D_METHOD("set_scroll_active", "active"), &RichTextLabel::set_scroll_active); + ClassDB::bind_method(D_METHOD("is_scroll_active"), &RichTextLabel::is_scroll_active); - ClassDB::bind_method(D_METHOD("get_v_scroll"),&RichTextLabel::get_v_scroll); + ClassDB::bind_method(D_METHOD("set_scroll_follow", "follow"), &RichTextLabel::set_scroll_follow); + ClassDB::bind_method(D_METHOD("is_scroll_following"), &RichTextLabel::is_scroll_following); - ClassDB::bind_method(D_METHOD("scroll_to_line","line"),&RichTextLabel::scroll_to_line); + ClassDB::bind_method(D_METHOD("get_v_scroll"), &RichTextLabel::get_v_scroll); - ClassDB::bind_method(D_METHOD("set_tab_size","spaces"),&RichTextLabel::set_tab_size); - ClassDB::bind_method(D_METHOD("get_tab_size"),&RichTextLabel::get_tab_size); + ClassDB::bind_method(D_METHOD("scroll_to_line", "line"), &RichTextLabel::scroll_to_line); - ClassDB::bind_method(D_METHOD("set_selection_enabled","enabled"),&RichTextLabel::set_selection_enabled); - ClassDB::bind_method(D_METHOD("is_selection_enabled"),&RichTextLabel::is_selection_enabled); + ClassDB::bind_method(D_METHOD("set_tab_size", "spaces"), &RichTextLabel::set_tab_size); + ClassDB::bind_method(D_METHOD("get_tab_size"), &RichTextLabel::get_tab_size); - ClassDB::bind_method(D_METHOD("parse_bbcode", "bbcode"),&RichTextLabel::parse_bbcode); - ClassDB::bind_method(D_METHOD("append_bbcode", "bbcode"),&RichTextLabel::append_bbcode); + ClassDB::bind_method(D_METHOD("set_selection_enabled", "enabled"), &RichTextLabel::set_selection_enabled); + ClassDB::bind_method(D_METHOD("is_selection_enabled"), &RichTextLabel::is_selection_enabled); - ClassDB::bind_method(D_METHOD("set_bbcode","text"),&RichTextLabel::set_bbcode); - ClassDB::bind_method(D_METHOD("get_bbcode"),&RichTextLabel::get_bbcode); + ClassDB::bind_method(D_METHOD("parse_bbcode", "bbcode"), &RichTextLabel::parse_bbcode); + ClassDB::bind_method(D_METHOD("append_bbcode", "bbcode"), &RichTextLabel::append_bbcode); - ClassDB::bind_method(D_METHOD("set_visible_characters","amount"),&RichTextLabel::set_visible_characters); - ClassDB::bind_method(D_METHOD("get_visible_characters"),&RichTextLabel::get_visible_characters); + ClassDB::bind_method(D_METHOD("set_bbcode", "text"), &RichTextLabel::set_bbcode); + ClassDB::bind_method(D_METHOD("get_bbcode"), &RichTextLabel::get_bbcode); - ClassDB::bind_method(D_METHOD("get_total_character_count"),&RichTextLabel::get_total_character_count); + ClassDB::bind_method(D_METHOD("set_visible_characters", "amount"), &RichTextLabel::set_visible_characters); + ClassDB::bind_method(D_METHOD("get_visible_characters"), &RichTextLabel::get_visible_characters); - ClassDB::bind_method(D_METHOD("set_use_bbcode","enable"),&RichTextLabel::set_use_bbcode); - ClassDB::bind_method(D_METHOD("is_using_bbcode"),&RichTextLabel::is_using_bbcode); + ClassDB::bind_method(D_METHOD("get_total_character_count"), &RichTextLabel::get_total_character_count); - ADD_GROUP("BBCode","bbcode_"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL,"bbcode_enabled"),"set_use_bbcode","is_using_bbcode"); - ADD_PROPERTY(PropertyInfo(Variant::STRING,"bbcode_text",PROPERTY_HINT_MULTILINE_TEXT),"set_bbcode","get_bbcode"); - ADD_PROPERTY(PropertyInfo(Variant::INT,"visible_characters",PROPERTY_HINT_RANGE,"-1,128000,1"),"set_visible_characters","get_visible_characters"); + ClassDB::bind_method(D_METHOD("set_use_bbcode", "enable"), &RichTextLabel::set_use_bbcode); + ClassDB::bind_method(D_METHOD("is_using_bbcode"), &RichTextLabel::is_using_bbcode); - ADD_SIGNAL( MethodInfo("meta_clicked",PropertyInfo(Variant::NIL,"meta"))); + ADD_GROUP("BBCode", "bbcode_"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bbcode_enabled"), "set_use_bbcode", "is_using_bbcode"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "bbcode_text", PROPERTY_HINT_MULTILINE_TEXT), "set_bbcode", "get_bbcode"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_characters", PROPERTY_HINT_RANGE, "-1,128000,1"), "set_visible_characters", "get_visible_characters"); - BIND_CONSTANT( ALIGN_LEFT ); - BIND_CONSTANT( ALIGN_CENTER ); - BIND_CONSTANT( ALIGN_RIGHT ); - BIND_CONSTANT( ALIGN_FILL ); + ADD_SIGNAL(MethodInfo("meta_clicked", PropertyInfo(Variant::NIL, "meta"))); - BIND_CONSTANT( LIST_NUMBERS ); - BIND_CONSTANT( LIST_LETTERS ); - BIND_CONSTANT( LIST_DOTS ); + BIND_CONSTANT(ALIGN_LEFT); + BIND_CONSTANT(ALIGN_CENTER); + BIND_CONSTANT(ALIGN_RIGHT); + BIND_CONSTANT(ALIGN_FILL); - BIND_CONSTANT( ITEM_FRAME ); - BIND_CONSTANT( ITEM_TEXT ); - BIND_CONSTANT( ITEM_IMAGE ); - BIND_CONSTANT( ITEM_NEWLINE ); - BIND_CONSTANT( ITEM_FONT ); - BIND_CONSTANT( ITEM_COLOR ); - BIND_CONSTANT( ITEM_UNDERLINE ); - BIND_CONSTANT( ITEM_ALIGN ); - BIND_CONSTANT( ITEM_INDENT ); - BIND_CONSTANT( ITEM_LIST ); - BIND_CONSTANT( ITEM_META ); + BIND_CONSTANT(LIST_NUMBERS); + BIND_CONSTANT(LIST_LETTERS); + BIND_CONSTANT(LIST_DOTS); + BIND_CONSTANT(ITEM_FRAME); + BIND_CONSTANT(ITEM_TEXT); + BIND_CONSTANT(ITEM_IMAGE); + BIND_CONSTANT(ITEM_NEWLINE); + BIND_CONSTANT(ITEM_FONT); + BIND_CONSTANT(ITEM_COLOR); + BIND_CONSTANT(ITEM_UNDERLINE); + BIND_CONSTANT(ITEM_ALIGN); + BIND_CONSTANT(ITEM_INDENT); + BIND_CONSTANT(ITEM_LIST); + BIND_CONSTANT(ITEM_META); } - void RichTextLabel::set_visible_characters(int p_visible) { - visible_characters=p_visible; + visible_characters = p_visible; update(); } @@ -2039,58 +1921,56 @@ int RichTextLabel::get_visible_characters() const { } int RichTextLabel::get_total_character_count() const { - int tc=0; - for(int i=0;i<current_frame->lines.size();i++) - tc+=current_frame->lines[i].char_count; + int tc = 0; + for (int i = 0; i < current_frame->lines.size(); i++) + tc += current_frame->lines[i].char_count; return tc; } - RichTextLabel::RichTextLabel() { - - main = memnew( ItemFrame ); - main->index=0; - current=main; + main = memnew(ItemFrame); + main->index = 0; + current = main; main->lines.resize(1); - main->lines[0].from=main; - main->first_invalid_line=0; - current_frame=main; - tab_size=4; - default_align=ALIGN_LEFT; - underline_meta=true; - - scroll_visible=false; - scroll_follow=false; - scroll_following=false; - updating_scroll=false; - scroll_active=true; - scroll_w=0; - - vscroll = memnew( VScrollBar ); + main->lines[0].from = main; + main->first_invalid_line = 0; + current_frame = main; + tab_size = 4; + default_align = ALIGN_LEFT; + underline_meta = true; + + scroll_visible = false; + scroll_follow = false; + scroll_following = false; + updating_scroll = false; + scroll_active = true; + scroll_w = 0; + + vscroll = memnew(VScrollBar); add_child(vscroll); vscroll->set_drag_slave(String("..")); vscroll->set_step(1); - vscroll->set_anchor_and_margin( MARGIN_TOP, ANCHOR_BEGIN, 0); - vscroll->set_anchor_and_margin( MARGIN_BOTTOM, ANCHOR_END, 0); - vscroll->set_anchor_and_margin( MARGIN_RIGHT, ANCHOR_END, 0); - vscroll->connect("value_changed",this,"_scroll_changed"); + vscroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0); + vscroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0); + vscroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0); + vscroll->connect("value_changed", this, "_scroll_changed"); vscroll->set_step(1); vscroll->hide(); - current_idx=1; - use_bbcode=false; + current_idx = 1; + use_bbcode = false; - selection.click=NULL; - selection.active=false; - selection.enabled=false; + selection.click = NULL; + selection.active = false; + selection.enabled = false; - visible_characters=-1; + visible_characters = -1; set_clip_contents(true); } RichTextLabel::~RichTextLabel() { - memdelete( main ); + memdelete(main); } diff --git a/scene/gui/rich_text_label.h b/scene/gui/rich_text_label.h index 3bdd2cd7e3..a5f23d0ba9 100644 --- a/scene/gui/rich_text_label.h +++ b/scene/gui/rich_text_label.h @@ -29,14 +29,13 @@ #ifndef RICH_TEXT_LABEL_H #define RICH_TEXT_LABEL_H - #include "scene/gui/scroll_bar.h" class RichTextLabel : public Control { - GDCLASS( RichTextLabel, Control ); -public: + GDCLASS(RichTextLabel, Control); +public: enum Align { ALIGN_LEFT, @@ -69,10 +68,9 @@ public: }; protected: - static void _bind_methods(); -private: +private: struct Item; struct Line { @@ -86,27 +84,37 @@ private: int char_count; int minimum_width; - Line() { from=NULL; char_count=0; } + Line() { + from = NULL; + char_count = 0; + } }; - - struct Item { int index; Item *parent; ItemType type; - List<Item*> subitems; - List<Item*>::Element *E; + List<Item *> subitems; + List<Item *>::Element *E; int line; - void _clear_children() { while (subitems.size()) { memdelete(subitems.front()->get()); subitems.pop_front(); } } - - Item() { parent=NULL; E=NULL; line=0;} - virtual ~Item() { _clear_children(); } + void _clear_children() { + while (subitems.size()) { + memdelete(subitems.front()->get()); + subitems.pop_front(); + } + } + + Item() { + parent = NULL; + E = NULL; + line = 0; + } + virtual ~Item() { _clear_children(); } }; - struct ItemFrame : public Item{ + struct ItemFrame : public Item { int parent_line; bool cell; @@ -114,71 +122,74 @@ private: int first_invalid_line; ItemFrame *parent_frame; - ItemFrame() { type=ITEM_FRAME; parent_frame=NULL; cell=false; parent_line=0; } + ItemFrame() { + type = ITEM_FRAME; + parent_frame = NULL; + cell = false; + parent_line = 0; + } }; - struct ItemText : public Item { String text; - ItemText() { type=ITEM_TEXT; } + ItemText() { type = ITEM_TEXT; } }; struct ItemImage : public Item { Ref<Texture> image; - ItemImage() { type=ITEM_IMAGE; } + ItemImage() { type = ITEM_IMAGE; } }; struct ItemFont : public Item { Ref<Font> font; - ItemFont() { type=ITEM_FONT; } + ItemFont() { type = ITEM_FONT; } }; struct ItemColor : public Item { Color color; - ItemColor() { type=ITEM_COLOR; } + ItemColor() { type = ITEM_COLOR; } }; struct ItemUnderline : public Item { - ItemUnderline() { type=ITEM_UNDERLINE; } + ItemUnderline() { type = ITEM_UNDERLINE; } }; struct ItemMeta : public Item { Variant meta; - ItemMeta() { type=ITEM_META; } + ItemMeta() { type = ITEM_META; } }; struct ItemAlign : public Item { Align align; - ItemAlign() { type=ITEM_ALIGN; } + ItemAlign() { type = ITEM_ALIGN; } }; struct ItemIndent : public Item { int level; - ItemIndent() { type=ITEM_INDENT; } + ItemIndent() { type = ITEM_INDENT; } }; struct ItemList : public Item { ListType list_type; - ItemList() { type=ITEM_LIST; } + ItemList() { type = ITEM_LIST; } }; struct ItemNewline : public Item { - int line; // FIXME: Overriding base's line ? - ItemNewline() { type=ITEM_NEWLINE; } + int line; // FIXME: Overriding base's line ? + ItemNewline() { type = ITEM_NEWLINE; } }; - - struct ItemTable : public Item{ + struct ItemTable : public Item { struct Column { bool expand; @@ -189,7 +200,7 @@ private: Vector<Column> columns; int total_width; - ItemTable() { type=ITEM_TABLE; } + ItemTable() { type = ITEM_TABLE; } }; ItemFrame *main; @@ -198,7 +209,6 @@ private: VScrollBar *vscroll; - bool scroll_visible; bool scroll_follow; bool scroll_following; @@ -207,7 +217,6 @@ private: bool updating_scroll; int current_idx; - int tab_size; bool underline_meta; @@ -216,12 +225,9 @@ private: void _invalidate_current_line(ItemFrame *p_frame); void _validate_line_caches(ItemFrame *p_frame); - void _add_item(Item *p_item, bool p_enter=false,bool p_ensure_newline=false); + void _add_item(Item *p_item, bool p_enter = false, bool p_ensure_newline = false); void _remove_item(Item *p_item, const int p_line, const int p_subitem_line); - - - struct ProcessState { int line_width; @@ -250,26 +256,23 @@ private: Selection selection; - int visible_characters; - - void _process_line(ItemFrame *p_frame,const Vector2& p_ofs,int &y, int p_width, int p_line, ProcessMode p_mode,const Ref<Font> &p_base_font,const Color &p_base_color,const Point2i& p_click_pos=Point2i(),Item **r_click_item=NULL,int *r_click_char=NULL,bool *r_outside=NULL,int p_char_count=0); - void _find_click(ItemFrame *p_frame, const Point2i& p_click,Item **r_click_item=NULL,int *r_click_char=NULL,bool *r_outside=NULL); - + void _process_line(ItemFrame *p_frame, const Vector2 &p_ofs, int &y, int p_width, int p_line, ProcessMode p_mode, const Ref<Font> &p_base_font, const Color &p_base_color, const Point2i &p_click_pos = Point2i(), Item **r_click_item = NULL, int *r_click_char = NULL, bool *r_outside = NULL, int p_char_count = 0); + void _find_click(ItemFrame *p_frame, const Point2i &p_click, Item **r_click_item = NULL, int *r_click_char = NULL, bool *r_outside = NULL); Ref<Font> _find_font(Item *p_item); - int _find_margin(Item *p_item,const Ref<Font>& p_base_font); + int _find_margin(Item *p_item, const Ref<Font> &p_base_font); Align _find_align(Item *p_item); - Color _find_color(Item *p_item,const Color& p_default_color); + Color _find_color(Item *p_item, const Color &p_default_color); bool _find_underline(Item *p_item); - bool _find_meta(Item *p_item,Variant *r_meta); + bool _find_meta(Item *p_item, Variant *r_meta); void _update_scroll(); void _scroll_changed(double); void _gui_input(InputEvent p_event); - Item *_get_next_item(Item* p_item, bool p_free=false); + Item *_get_next_item(Item *p_item, bool p_free = false); bool use_bbcode; String bbcode; @@ -280,21 +283,20 @@ protected: void _notification(int p_what); public: - String get_text(); - void add_text(const String& p_text); - void add_image(const Ref<Texture>& p_image); + void add_text(const String &p_text); + void add_image(const Ref<Texture> &p_image); void add_newline(); bool remove_line(const int p_line); - void push_font(const Ref<Font>& p_font); - void push_color(const Color& p_color); + void push_font(const Ref<Font> &p_font); + void push_color(const Color &p_color); void push_underline(); void push_align(Align p_align); void push_indent(int p_level); void push_list(ListType p_list); - void push_meta(const Variant& p_data); + void push_meta(const Variant &p_data); void push_table(int p_columns); - void set_table_column_expand(int p_column, bool p_expand, int p_ratio=1); + void set_table_column_expand(int p_column, bool p_expand, int p_ratio = 1); int get_current_table_column() const; void push_cell(); void pop(); @@ -315,29 +317,26 @@ public: void set_tab_size(int p_spaces); int get_tab_size() const; - - - bool search(const String& p_string,bool p_from_selection=false); + bool search(const String &p_string, bool p_from_selection = false); void scroll_to_line(int p_line); int get_line_count() const; VScrollBar *get_v_scroll() { return vscroll; } - virtual CursorShape get_cursor_shape(const Point2& p_pos) const; + virtual CursorShape get_cursor_shape(const Point2 &p_pos) const; void set_selection_enabled(bool p_enabled); bool is_selection_enabled() const; void selection_copy(); - - Error parse_bbcode(const String& p_bbcode); - Error append_bbcode(const String& p_bbcode); + Error parse_bbcode(const String &p_bbcode); + Error append_bbcode(const String &p_bbcode); void set_use_bbcode(bool p_enable); bool is_using_bbcode() const; - void set_bbcode(const String& p_bbcode); + void set_bbcode(const String &p_bbcode); String get_bbcode() const; void set_visible_characters(int p_visible); @@ -348,8 +347,8 @@ public: ~RichTextLabel(); }; -VARIANT_ENUM_CAST( RichTextLabel::Align ); -VARIANT_ENUM_CAST( RichTextLabel::ListType ); -VARIANT_ENUM_CAST( RichTextLabel::ItemType ); +VARIANT_ENUM_CAST(RichTextLabel::Align); +VARIANT_ENUM_CAST(RichTextLabel::ListType); +VARIANT_ENUM_CAST(RichTextLabel::ItemType); #endif // RICH_TEXT_LABEL_H diff --git a/scene/gui/scroll_bar.cpp b/scene/gui/scroll_bar.cpp index b2ad74eaa8..37f7b46e22 100644 --- a/scene/gui/scroll_bar.cpp +++ b/scene/gui/scroll_bar.cpp @@ -28,166 +28,153 @@ /*************************************************************************/ #include "scroll_bar.h" #include "os/keyboard.h" -#include "print_string.h" #include "os/os.h" -bool ScrollBar::focus_by_default=false; - - +#include "print_string.h" +bool ScrollBar::focus_by_default = false; void ScrollBar::set_can_focus_by_default(bool p_can_focus) { - focus_by_default=p_can_focus; + focus_by_default = p_can_focus; } void ScrollBar::_gui_input(InputEvent p_event) { - - switch(p_event.type) { + switch (p_event.type) { case InputEvent::MOUSE_BUTTON: { - const InputEventMouseButton &b=p_event.mouse_button; + const InputEventMouseButton &b = p_event.mouse_button; accept_event(); - if (b.button_index==5 && b.pressed) { + if (b.button_index == 5 && b.pressed) { /* if (orientation==VERTICAL) set_val( get_val() + get_page() / 4.0 ); else */ - set_value( get_value() + get_page() / 4.0 ); + set_value(get_value() + get_page() / 4.0); accept_event(); - } - if (b.button_index==4 && b.pressed) { + if (b.button_index == 4 && b.pressed) { /* if (orientation==HORIZONTAL) set_val( get_val() - get_page() / 4.0 ); else */ - set_value( get_value() - get_page() / 4.0 ); + set_value(get_value() - get_page() / 4.0); accept_event(); } - if (b.button_index!=1) + if (b.button_index != 1) return; - if (b.pressed) { - - double ofs = orientation==VERTICAL ? b.y : b.x ; + double ofs = orientation == VERTICAL ? b.y : b.x; Ref<Texture> decr = get_icon("decrement"); Ref<Texture> incr = get_icon("increment"); - double decr_size = orientation==VERTICAL ? decr->get_height() : decr->get_width(); - double incr_size = orientation==VERTICAL ? incr->get_height() : incr->get_width(); + double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width(); + double incr_size = orientation == VERTICAL ? incr->get_height() : incr->get_width(); double grabber_ofs = get_grabber_offset(); double grabber_size = get_grabber_size(); - double total = orientation==VERTICAL ? get_size().height : get_size().width; + double total = orientation == VERTICAL ? get_size().height : get_size().width; - if (ofs < decr_size ) { + if (ofs < decr_size) { - set_value( get_value() - (custom_step>=0?custom_step:get_step()) ); + set_value(get_value() - (custom_step >= 0 ? custom_step : get_step())); break; } - if (ofs > total-incr_size ) { + if (ofs > total - incr_size) { - set_value( get_value() + (custom_step>=0?custom_step:get_step()) ); + set_value(get_value() + (custom_step >= 0 ? custom_step : get_step())); break; } - ofs-=decr_size; + ofs -= decr_size; - if ( ofs < grabber_ofs ) { + if (ofs < grabber_ofs) { - set_value( get_value() - get_page() ); + set_value(get_value() - get_page()); break; - } - ofs-=grabber_ofs; + ofs -= grabber_ofs; - if (ofs < grabber_size ) { + if (ofs < grabber_size) { - drag.active=true; - drag.pos_at_click=grabber_ofs+ofs; - drag.value_at_click=get_as_ratio(); + drag.active = true; + drag.pos_at_click = grabber_ofs + ofs; + drag.value_at_click = get_as_ratio(); update(); } else { - - set_value( get_value() + get_page() ); + set_value(get_value() + get_page()); } - } else { - drag.active=false; + drag.active = false; update(); } } break; case InputEvent::MOUSE_MOTION: { - const InputEventMouseMotion &m=p_event.mouse_motion; + const InputEventMouseMotion &m = p_event.mouse_motion; accept_event(); - if (drag.active) { - double ofs = orientation==VERTICAL ? m.y : m.x ; + double ofs = orientation == VERTICAL ? m.y : m.x; Ref<Texture> decr = get_icon("decrement"); - double decr_size = orientation==VERTICAL ? decr->get_height() : decr->get_width(); - ofs-=decr_size; + double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width(); + ofs -= decr_size; - double diff = (ofs-drag.pos_at_click) / get_area_size(); + double diff = (ofs - drag.pos_at_click) / get_area_size(); - set_as_ratio( drag.value_at_click + diff ); + set_as_ratio(drag.value_at_click + diff); } else { - - double ofs = orientation==VERTICAL ? m.y : m.x ; + double ofs = orientation == VERTICAL ? m.y : m.x; Ref<Texture> decr = get_icon("decrement"); Ref<Texture> incr = get_icon("increment"); - double decr_size = orientation==VERTICAL ? decr->get_height() : decr->get_width(); - double incr_size = orientation==VERTICAL ? incr->get_height() : incr->get_width(); - double total = orientation==VERTICAL ? get_size().height : get_size().width; + double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width(); + double incr_size = orientation == VERTICAL ? incr->get_height() : incr->get_width(); + double total = orientation == VERTICAL ? get_size().height : get_size().width; HiliteStatus new_hilite; - if (ofs < decr_size ) { + if (ofs < decr_size) { - new_hilite=HILITE_DECR; + new_hilite = HILITE_DECR; - } else if (ofs > total-incr_size ) { + } else if (ofs > total - incr_size) { - new_hilite=HILITE_INCR; + new_hilite = HILITE_INCR; } else { - new_hilite=HILITE_RANGE; + new_hilite = HILITE_RANGE; } - if (new_hilite!=hilite) { + if (new_hilite != hilite) { - hilite=new_hilite; + hilite = new_hilite; update(); - } - } } break; case InputEvent::KEY: { - const InputEventKey &k=p_event.key; + const InputEventKey &k = p_event.key; if (!k.pressed) return; @@ -196,319 +183,298 @@ void ScrollBar::_gui_input(InputEvent p_event) { case KEY_LEFT: { - if (orientation!=HORIZONTAL) + if (orientation != HORIZONTAL) return; - set_value( get_value() - (custom_step>=0?custom_step:get_step()) ); + set_value(get_value() - (custom_step >= 0 ? custom_step : get_step())); } break; case KEY_RIGHT: { - if (orientation!=HORIZONTAL) + if (orientation != HORIZONTAL) return; - set_value( get_value() + (custom_step>=0?custom_step:get_step()) ); + set_value(get_value() + (custom_step >= 0 ? custom_step : get_step())); } break; case KEY_UP: { - if (orientation!=VERTICAL) + if (orientation != VERTICAL) return; - set_value( get_value() - (custom_step>=0?custom_step:get_step()) ); - + set_value(get_value() - (custom_step >= 0 ? custom_step : get_step())); } break; case KEY_DOWN: { - if (orientation!=VERTICAL) + if (orientation != VERTICAL) return; - set_value( get_value() + (custom_step>=0?custom_step:get_step()) ); + set_value(get_value() + (custom_step >= 0 ? custom_step : get_step())); } break; case KEY_HOME: { - set_value( get_min() ); + set_value(get_min()); } break; case KEY_END: { - set_value( get_max() ); + set_value(get_max()); } break; - - } break; + } + break; } } } void ScrollBar::_notification(int p_what) { - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { RID ci = get_canvas_item(); - Ref<Texture> decr = hilite==HILITE_DECR ? get_icon("decrement_hilite") : get_icon("decrement"); - Ref<Texture> incr = hilite==HILITE_INCR ? get_icon("increment_hilite") : get_icon("increment"); + Ref<Texture> decr = hilite == HILITE_DECR ? get_icon("decrement_hilite") : get_icon("decrement"); + Ref<Texture> incr = hilite == HILITE_INCR ? get_icon("increment_hilite") : get_icon("increment"); Ref<StyleBox> bg = has_focus() ? get_stylebox("scroll_focus") : get_stylebox("scroll"); - Ref<StyleBox> grabber = (drag.active || hilite==HILITE_RANGE) ? get_stylebox("grabber_hilite") : get_stylebox("grabber"); + Ref<StyleBox> grabber = (drag.active || hilite == HILITE_RANGE) ? get_stylebox("grabber_hilite") : get_stylebox("grabber"); Point2 ofs; VisualServer *vs = VisualServer::get_singleton(); - vs->canvas_item_add_texture_rect( ci, Rect2( Point2(), decr->get_size()),decr->get_rid() ); + vs->canvas_item_add_texture_rect(ci, Rect2(Point2(), decr->get_size()), decr->get_rid()); - if (orientation==HORIZONTAL) - ofs.x+=decr->get_width(); + if (orientation == HORIZONTAL) + ofs.x += decr->get_width(); else - ofs.y+=decr->get_height(); + ofs.y += decr->get_height(); - Size2 area=get_size(); + Size2 area = get_size(); - if (orientation==HORIZONTAL) - area.width-=incr->get_width()+decr->get_width(); + if (orientation == HORIZONTAL) + area.width -= incr->get_width() + decr->get_width(); else - area.height-=incr->get_height()+decr->get_height(); + area.height -= incr->get_height() + decr->get_height(); - bg->draw(ci,Rect2(ofs,area)); + bg->draw(ci, Rect2(ofs, area)); - if (orientation==HORIZONTAL) - ofs.width+=area.width; + if (orientation == HORIZONTAL) + ofs.width += area.width; else - ofs.height+=area.height; + ofs.height += area.height; - vs->canvas_item_add_texture_rect( ci, Rect2( ofs, decr->get_size()),incr->get_rid() ); + vs->canvas_item_add_texture_rect(ci, Rect2(ofs, decr->get_size()), incr->get_rid()); Rect2 grabber_rect; - if (orientation==HORIZONTAL) { + if (orientation == HORIZONTAL) { - grabber_rect.size.width=get_grabber_size(); - grabber_rect.size.height=get_size().height; - grabber_rect.pos.y=0; - grabber_rect.pos.x=get_grabber_offset()+decr->get_width()+bg->get_margin( MARGIN_LEFT ); + grabber_rect.size.width = get_grabber_size(); + grabber_rect.size.height = get_size().height; + grabber_rect.pos.y = 0; + grabber_rect.pos.x = get_grabber_offset() + decr->get_width() + bg->get_margin(MARGIN_LEFT); } else { - grabber_rect.size.width=get_size().width; - grabber_rect.size.height=get_grabber_size(); - grabber_rect.pos.y=get_grabber_offset()+decr->get_height()+bg->get_margin( MARGIN_TOP ); - grabber_rect.pos.x=0; + grabber_rect.size.width = get_size().width; + grabber_rect.size.height = get_grabber_size(); + grabber_rect.pos.y = get_grabber_offset() + decr->get_height() + bg->get_margin(MARGIN_TOP); + grabber_rect.pos.x = 0; } - grabber->draw(ci,grabber_rect); - + grabber->draw(ci, grabber_rect); } - if (p_what==NOTIFICATION_ENTER_TREE) { - + if (p_what == NOTIFICATION_ENTER_TREE) { if (has_node(drag_slave_path)) { Node *n = get_node(drag_slave_path); - drag_slave=n->cast_to<Control>(); + drag_slave = n->cast_to<Control>(); } if (drag_slave) { - drag_slave->connect("gui_input",this,"_drag_slave_input"); - drag_slave->connect("tree_exited",this,"_drag_slave_exit",varray(),CONNECT_ONESHOT); + drag_slave->connect("gui_input", this, "_drag_slave_input"); + drag_slave->connect("tree_exited", this, "_drag_slave_exit", varray(), CONNECT_ONESHOT); } - - } - if (p_what==NOTIFICATION_EXIT_TREE) { + if (p_what == NOTIFICATION_EXIT_TREE) { if (drag_slave) { - drag_slave->disconnect("gui_input",this,"_drag_slave_input"); - drag_slave->disconnect("tree_exited",this,"_drag_slave_exit"); + drag_slave->disconnect("gui_input", this, "_drag_slave_input"); + drag_slave->disconnect("tree_exited", this, "_drag_slave_exit"); } - drag_slave=NULL; - + drag_slave = NULL; } - if (p_what==NOTIFICATION_FIXED_PROCESS) { + if (p_what == NOTIFICATION_FIXED_PROCESS) { - if (drag_slave_touching) { + if (drag_slave_touching) { if (drag_slave_touching_deaccel) { - Vector2 pos = Vector2(orientation==HORIZONTAL?get_value():0,orientation==VERTICAL?get_value():0); - pos+=drag_slave_speed*get_fixed_process_delta_time(); + Vector2 pos = Vector2(orientation == HORIZONTAL ? get_value() : 0, orientation == VERTICAL ? get_value() : 0); + pos += drag_slave_speed * get_fixed_process_delta_time(); - bool turnoff=false; + bool turnoff = false; - if (orientation==HORIZONTAL) { + if (orientation == HORIZONTAL) { - if (pos.x<0) { - pos.x=0; - turnoff=true; + if (pos.x < 0) { + pos.x = 0; + turnoff = true; } - if (pos.x > (get_max()-get_page())) { - pos.x=get_max()-get_page(); - turnoff=true; + if (pos.x > (get_max() - get_page())) { + pos.x = get_max() - get_page(); + turnoff = true; } set_value(pos.x); - float sgn_x = drag_slave_speed.x<0? -1 : 1; + float sgn_x = drag_slave_speed.x < 0 ? -1 : 1; float val_x = Math::abs(drag_slave_speed.x); - val_x-=1000*get_fixed_process_delta_time(); + val_x -= 1000 * get_fixed_process_delta_time(); - if (val_x<0) { - turnoff=true; + if (val_x < 0) { + turnoff = true; } - drag_slave_speed.x=sgn_x*val_x; + drag_slave_speed.x = sgn_x * val_x; } else { - - if (pos.y<0) { - pos.y=0; - turnoff=true; + if (pos.y < 0) { + pos.y = 0; + turnoff = true; } - if (pos.y > (get_max()-get_page())) { - pos.y=get_max()-get_page(); - turnoff=true; + if (pos.y > (get_max() - get_page())) { + pos.y = get_max() - get_page(); + turnoff = true; } set_value(pos.y); - float sgn_y = drag_slave_speed.y<0? -1 : 1; + float sgn_y = drag_slave_speed.y < 0 ? -1 : 1; float val_y = Math::abs(drag_slave_speed.y); - val_y-=1000*get_fixed_process_delta_time(); + val_y -= 1000 * get_fixed_process_delta_time(); - if (val_y<0) { - turnoff=true; + if (val_y < 0) { + turnoff = true; } - drag_slave_speed.y=sgn_y*val_y; + drag_slave_speed.y = sgn_y * val_y; } - if (turnoff) { set_fixed_process(false); - drag_slave_touching=false; - drag_slave_touching_deaccel=false; + drag_slave_touching = false; + drag_slave_touching_deaccel = false; } - } else { - - if (time_since_motion==0 || time_since_motion>0.1) { + if (time_since_motion == 0 || time_since_motion > 0.1) { Vector2 diff = drag_slave_accum - last_drag_slave_accum; - last_drag_slave_accum=drag_slave_accum; - drag_slave_speed=diff/get_fixed_process_delta_time(); + last_drag_slave_accum = drag_slave_accum; + drag_slave_speed = diff / get_fixed_process_delta_time(); } - time_since_motion+=get_fixed_process_delta_time(); + time_since_motion += get_fixed_process_delta_time(); } } - - } - if (p_what==NOTIFICATION_MOUSE_EXIT) { + if (p_what == NOTIFICATION_MOUSE_EXIT) { - hilite=HILITE_NONE; + hilite = HILITE_NONE; update(); } } double ScrollBar::get_grabber_min_size() const { - Ref<StyleBox> grabber=get_stylebox("grabber"); - Size2 gminsize=grabber->get_minimum_size()+grabber->get_center_size(); - return (orientation==VERTICAL)?gminsize.height:gminsize.width; + Ref<StyleBox> grabber = get_stylebox("grabber"); + Size2 gminsize = grabber->get_minimum_size() + grabber->get_center_size(); + return (orientation == VERTICAL) ? gminsize.height : gminsize.width; } double ScrollBar::get_grabber_size() const { - float range = get_max()-get_min(); - if (range<=0) + float range = get_max() - get_min(); + if (range <= 0) return 0; - float page = (get_page()>0)? get_page() : 0; + float page = (get_page() > 0) ? get_page() : 0; /* if (grabber_range < get_step()) grabber_range=get_step(); */ - double area_size=get_area_size(); + double area_size = get_area_size(); double grabber_size = page / range * area_size; - return grabber_size+get_grabber_min_size(); - + return grabber_size + get_grabber_min_size(); } double ScrollBar::get_area_size() const { - if (orientation==VERTICAL) { + if (orientation == VERTICAL) { - double area=get_size().height; - area-=get_stylebox("scroll")->get_minimum_size().height; - area-=get_icon("increment")->get_height(); - area-=get_icon("decrement")->get_height(); - area-=get_grabber_min_size(); + double area = get_size().height; + area -= get_stylebox("scroll")->get_minimum_size().height; + area -= get_icon("increment")->get_height(); + area -= get_icon("decrement")->get_height(); + area -= get_grabber_min_size(); return area; - } else if (orientation==HORIZONTAL) { + } else if (orientation == HORIZONTAL) { - double area=get_size().width; - area-=get_stylebox("scroll")->get_minimum_size().width; - area-=get_icon("increment")->get_width(); - area-=get_icon("decrement")->get_width(); - area-=get_grabber_min_size(); + double area = get_size().width; + area -= get_stylebox("scroll")->get_minimum_size().width; + area -= get_icon("increment")->get_width(); + area -= get_icon("decrement")->get_width(); + area -= get_grabber_min_size(); return area; } else { return 0; } - } double ScrollBar::get_area_offset() const { - double ofs=0; + double ofs = 0; - if (orientation==VERTICAL) { - - ofs+=get_stylebox("hscroll")->get_margin( MARGIN_TOP ); - ofs+=get_icon("decrement")->get_height(); + if (orientation == VERTICAL) { + ofs += get_stylebox("hscroll")->get_margin(MARGIN_TOP); + ofs += get_icon("decrement")->get_height(); } - if (orientation==HORIZONTAL) { + if (orientation == HORIZONTAL) { - ofs+=get_stylebox("hscroll")->get_margin( MARGIN_LEFT ); - ofs+=get_icon("decrement")->get_width(); + ofs += get_stylebox("hscroll")->get_margin(MARGIN_LEFT); + ofs += get_icon("decrement")->get_width(); } return ofs; } -double ScrollBar::get_click_pos(const Point2& p_pos) const { - +double ScrollBar::get_click_pos(const Point2 &p_pos) const { - float pos=(orientation==VERTICAL)?p_pos.y:p_pos.x; - pos-=get_area_offset(); + float pos = (orientation == VERTICAL) ? p_pos.y : p_pos.x; + pos -= get_area_offset(); - float area=get_area_size(); - if (area==0) + float area = get_area_size(); + if (area == 0) return 0; else - return pos/area; - + return pos / area; } double ScrollBar::get_grabber_offset() const { - return (get_area_size()) * get_as_ratio(); - } - - Size2 ScrollBar::get_minimum_size() const { Ref<Texture> incr = get_icon("increment"); @@ -516,31 +482,30 @@ Size2 ScrollBar::get_minimum_size() const { Ref<StyleBox> bg = get_stylebox("scroll"); Size2 minsize; - if (orientation==VERTICAL) { + if (orientation == VERTICAL) { - minsize.width=MAX(incr->get_size().width,(bg->get_minimum_size()+bg->get_center_size()).width); - minsize.height+=incr->get_size().height; - minsize.height+=decr->get_size().height; - minsize.height+=bg->get_minimum_size().height; - minsize.height+=get_grabber_min_size(); + minsize.width = MAX(incr->get_size().width, (bg->get_minimum_size() + bg->get_center_size()).width); + minsize.height += incr->get_size().height; + minsize.height += decr->get_size().height; + minsize.height += bg->get_minimum_size().height; + minsize.height += get_grabber_min_size(); } - if (orientation==HORIZONTAL) { + if (orientation == HORIZONTAL) { - minsize.height=MAX(incr->get_size().height,(bg->get_center_size()+bg->get_minimum_size()).height); - minsize.width+=incr->get_size().width; - minsize.width+=decr->get_size().width; - minsize.width+=bg->get_minimum_size().width; - minsize.width+=get_grabber_min_size(); + minsize.height = MAX(incr->get_size().height, (bg->get_center_size() + bg->get_minimum_size()).height); + minsize.width += incr->get_size().width; + minsize.width += decr->get_size().width; + minsize.width += bg->get_minimum_size().width; + minsize.width += get_grabber_min_size(); } return minsize; - } void ScrollBar::set_custom_step(float p_custom_step) { - custom_step=p_custom_step; + custom_step = p_custom_step; } float ScrollBar::get_custom_step() const { @@ -548,53 +513,50 @@ float ScrollBar::get_custom_step() const { return custom_step; } - void ScrollBar::_drag_slave_exit() { if (drag_slave) { - drag_slave->disconnect("gui_input",this,"_drag_slave_input"); + drag_slave->disconnect("gui_input", this, "_drag_slave_input"); } - drag_slave=NULL; + drag_slave = NULL; } +void ScrollBar::_drag_slave_input(const InputEvent &p_input) { -void ScrollBar::_drag_slave_input(const InputEvent& p_input) { - - switch(p_input.type) { + switch (p_input.type) { case InputEvent::MOUSE_BUTTON: { - const InputEventMouseButton &mb=p_input.mouse_button; + const InputEventMouseButton &mb = p_input.mouse_button; - if (mb.button_index!=1) + if (mb.button_index != 1) break; if (mb.pressed) { if (drag_slave_touching) { set_fixed_process(false); - drag_slave_touching_deaccel=false; - drag_slave_touching=false; - drag_slave_speed=Vector2(); - drag_slave_accum=Vector2(); - last_drag_slave_accum=Vector2(); - drag_slave_from=Vector2(); + drag_slave_touching_deaccel = false; + drag_slave_touching = false; + drag_slave_speed = Vector2(); + drag_slave_accum = Vector2(); + last_drag_slave_accum = Vector2(); + drag_slave_from = Vector2(); } if (true) { - drag_slave_speed=Vector2(); - drag_slave_accum=Vector2(); - last_drag_slave_accum=Vector2(); + drag_slave_speed = Vector2(); + drag_slave_accum = Vector2(); + last_drag_slave_accum = Vector2(); //drag_slave_from=Vector2(h_scroll->get_val(),v_scroll->get_val()); - drag_slave_from= Vector2(orientation==HORIZONTAL?get_value():0,orientation==VERTICAL?get_value():0); + drag_slave_from = Vector2(orientation == HORIZONTAL ? get_value() : 0, orientation == VERTICAL ? get_value() : 0); - drag_slave_touching=OS::get_singleton()->has_touchscreen_ui_hint(); - drag_slave_touching_deaccel=false; - time_since_motion=0; + drag_slave_touching = OS::get_singleton()->has_touchscreen_ui_hint(); + drag_slave_touching_deaccel = false; + time_since_motion = 0; if (drag_slave_touching) { set_fixed_process(true); - time_since_motion=0; - + time_since_motion = 0; } } @@ -602,82 +564,79 @@ void ScrollBar::_drag_slave_input(const InputEvent& p_input) { if (drag_slave_touching) { - if (drag_slave_speed==Vector2()) { - drag_slave_touching_deaccel=false; - drag_slave_touching=false; + if (drag_slave_speed == Vector2()) { + drag_slave_touching_deaccel = false; + drag_slave_touching = false; set_fixed_process(false); } else { - drag_slave_touching_deaccel=true; + drag_slave_touching_deaccel = true; } } } } break; case InputEvent::MOUSE_MOTION: { - const InputEventMouseMotion &mm=p_input.mouse_motion; + const InputEventMouseMotion &mm = p_input.mouse_motion; - if (drag_slave_touching && ! drag_slave_touching_deaccel) { + if (drag_slave_touching && !drag_slave_touching_deaccel) { - Vector2 motion = Vector2(mm.relative_x,mm.relative_y); + Vector2 motion = Vector2(mm.relative_x, mm.relative_y); - drag_slave_accum-=motion; - Vector2 diff = drag_slave_from+drag_slave_accum; + drag_slave_accum -= motion; + Vector2 diff = drag_slave_from + drag_slave_accum; - if (orientation==HORIZONTAL) + if (orientation == HORIZONTAL) set_value(diff.x); /* else drag_slave_accum.x=0; */ - if (orientation==VERTICAL) + if (orientation == VERTICAL) set_value(diff.y); /* else drag_slave_accum.y=0; */ - time_since_motion=0; + time_since_motion = 0; } } break; } } -void ScrollBar::set_drag_slave(const NodePath& p_path) { +void ScrollBar::set_drag_slave(const NodePath &p_path) { if (is_inside_tree()) { if (drag_slave) { - drag_slave->disconnect("gui_input",this,"_drag_slave_input"); - drag_slave->disconnect("tree_exited",this,"_drag_slave_exit"); + drag_slave->disconnect("gui_input", this, "_drag_slave_input"); + drag_slave->disconnect("tree_exited", this, "_drag_slave_exit"); } } - drag_slave=NULL; - drag_slave_path=p_path; + drag_slave = NULL; + drag_slave_path = p_path; if (is_inside_tree()) { if (has_node(p_path)) { Node *n = get_node(p_path); - drag_slave=n->cast_to<Control>(); + drag_slave = n->cast_to<Control>(); } if (drag_slave) { - drag_slave->connect("gui_input",this,"_drag_slave_input"); - drag_slave->connect("tree_exited",this,"_drag_slave_exit",varray(),CONNECT_ONESHOT); + drag_slave->connect("gui_input", this, "_drag_slave_input"); + drag_slave->connect("tree_exited", this, "_drag_slave_exit", varray(), CONNECT_ONESHOT); } } } -NodePath ScrollBar::get_drag_slave() const{ - +NodePath ScrollBar::get_drag_slave() const { return drag_slave_path; } - - #if 0 void ScrollBar::mouse_button(const Point2& p_pos, int b.button_index,bool b.pressed,int p_modifier_mask) { @@ -808,48 +767,36 @@ bool ScrollBar::key(unsigned long p_unicode, unsigned long p_scan_code,bool b.pr return true; } - - #endif void ScrollBar::_bind_methods() { - ClassDB::bind_method(D_METHOD("_gui_input"),&ScrollBar::_gui_input); - ClassDB::bind_method(D_METHOD("set_custom_step","step"),&ScrollBar::set_custom_step); - ClassDB::bind_method(D_METHOD("get_custom_step"),&ScrollBar::get_custom_step); - ClassDB::bind_method(D_METHOD("_drag_slave_input"),&ScrollBar::_drag_slave_input); - ClassDB::bind_method(D_METHOD("_drag_slave_exit"),&ScrollBar::_drag_slave_exit); - - ADD_PROPERTY( PropertyInfo(Variant::REAL,"custom_step",PROPERTY_HINT_RANGE,"-1,4096"), "set_custom_step","get_custom_step"); + ClassDB::bind_method(D_METHOD("_gui_input"), &ScrollBar::_gui_input); + ClassDB::bind_method(D_METHOD("set_custom_step", "step"), &ScrollBar::set_custom_step); + ClassDB::bind_method(D_METHOD("get_custom_step"), &ScrollBar::get_custom_step); + ClassDB::bind_method(D_METHOD("_drag_slave_input"), &ScrollBar::_drag_slave_input); + ClassDB::bind_method(D_METHOD("_drag_slave_exit"), &ScrollBar::_drag_slave_exit); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "custom_step", PROPERTY_HINT_RANGE, "-1,4096"), "set_custom_step", "get_custom_step"); } +ScrollBar::ScrollBar(Orientation p_orientation) { -ScrollBar::ScrollBar(Orientation p_orientation) -{ + orientation = p_orientation; + hilite = HILITE_NONE; + custom_step = -1; + drag_slave = NULL; + drag.active = false; - orientation=p_orientation; - hilite=HILITE_NONE; - custom_step=-1; - drag_slave=NULL; - - drag.active=false; - - drag_slave_speed=Vector2(); - drag_slave_touching=false; - drag_slave_touching_deaccel=false; + drag_slave_speed = Vector2(); + drag_slave_touching = false; + drag_slave_touching_deaccel = false; if (focus_by_default) - set_focus_mode( FOCUS_ALL ); + set_focus_mode(FOCUS_ALL); set_step(0); - } - - -ScrollBar::~ScrollBar() -{ +ScrollBar::~ScrollBar() { } - - diff --git a/scene/gui/scroll_bar.h b/scene/gui/scroll_bar.h index be8b394957..5227fa45ac 100644 --- a/scene/gui/scroll_bar.h +++ b/scene/gui/scroll_bar.h @@ -31,13 +31,12 @@ #include "scene/gui/range.h" - /** @author Juan Linietsky <reduzio@gmail.com> */ class ScrollBar : public Range { - GDCLASS( ScrollBar, Range ); + GDCLASS(ScrollBar, Range); enum HiliteStatus { HILITE_NONE, @@ -61,17 +60,16 @@ class ScrollBar : public Range { float value_at_click; } drag; - double get_grabber_size() const; double get_grabber_min_size() const; double get_area_size() const; double get_area_offset() const; - double get_click_pos(const Point2& p_pos) const; + double get_click_pos(const Point2 &p_pos) const; double get_grabber_offset() const; static void set_can_focus_by_default(bool p_can_focus); - Node* drag_slave; + Node *drag_slave; NodePath drag_slave_path; Vector2 drag_slave_speed; @@ -85,43 +83,43 @@ class ScrollBar : public Range { bool click_handled; void _drag_slave_exit(); - void _drag_slave_input(const InputEvent& p_input); + void _drag_slave_input(const InputEvent &p_input); void _gui_input(InputEvent p_event); + protected: void _notification(int p_what); static void _bind_methods(); public: - void set_custom_step(float p_custom_step); float get_custom_step() const; - void set_drag_slave(const NodePath& p_path); + void set_drag_slave(const NodePath &p_path); NodePath get_drag_slave() const; virtual Size2 get_minimum_size() const; - ScrollBar(Orientation p_orientation=VERTICAL); + ScrollBar(Orientation p_orientation = VERTICAL); ~ScrollBar(); - }; class HScrollBar : public ScrollBar { - GDCLASS( HScrollBar, ScrollBar ); -public: + GDCLASS(HScrollBar, ScrollBar); - HScrollBar() : ScrollBar(HORIZONTAL) { set_v_size_flags(0); } +public: + HScrollBar() + : ScrollBar(HORIZONTAL) { set_v_size_flags(0); } }; class VScrollBar : public ScrollBar { - GDCLASS( VScrollBar, ScrollBar ); -public: + GDCLASS(VScrollBar, ScrollBar); - VScrollBar() : ScrollBar(VERTICAL) { set_h_size_flags(0); } +public: + VScrollBar() + : ScrollBar(VERTICAL) { set_h_size_flags(0); } }; - #endif diff --git a/scene/gui/scroll_container.cpp b/scene/gui/scroll_container.cpp index 2296c9742d..c543b783b9 100644 --- a/scene/gui/scroll_container.cpp +++ b/scene/gui/scroll_container.cpp @@ -35,10 +35,9 @@ bool ScrollContainer::clips_input() const { Size2 ScrollContainer::get_minimum_size() const { - Size2 min_size; - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { Control *c = get_child(i)->cast_to<Control>(); if (!c) @@ -49,161 +48,150 @@ Size2 ScrollContainer::get_minimum_size() const { continue; Size2 minsize = c->get_combined_minimum_size(); - if (!scroll_h) { min_size.x = MAX(min_size.x, minsize.x); } if (!scroll_v) { min_size.y = MAX(min_size.y, minsize.y); - } } if (h_scroll->is_visible_in_tree()) { - min_size.y+=h_scroll->get_minimum_size().y; + min_size.y += h_scroll->get_minimum_size().y; } if (v_scroll->is_visible_in_tree()) { - min_size.x+=v_scroll->get_minimum_size().x; + min_size.x += v_scroll->get_minimum_size().x; } return min_size; }; - void ScrollContainer::_cancel_drag() { set_fixed_process(false); - drag_touching_deaccel=false; - drag_touching=false; - drag_speed=Vector2(); - drag_accum=Vector2(); - last_drag_accum=Vector2(); - drag_from=Vector2(); + drag_touching_deaccel = false; + drag_touching = false; + drag_speed = Vector2(); + drag_accum = Vector2(); + last_drag_accum = Vector2(); + drag_from = Vector2(); } -void ScrollContainer::_gui_input(const InputEvent& p_gui_input) { +void ScrollContainer::_gui_input(const InputEvent &p_gui_input) { - - switch(p_gui_input.type) { + switch (p_gui_input.type) { case InputEvent::MOUSE_BUTTON: { - const InputEventMouseButton &mb=p_gui_input.mouse_button; + const InputEventMouseButton &mb = p_gui_input.mouse_button; - if (mb.button_index==BUTTON_WHEEL_UP && mb.pressed) { - if (h_scroll->is_visible_in_tree() && !v_scroll->is_visible_in_tree()){ + if (mb.button_index == BUTTON_WHEEL_UP && mb.pressed) { + if (h_scroll->is_visible_in_tree() && !v_scroll->is_visible_in_tree()) { // only horizontal is enabled, scroll horizontally - h_scroll->set_value( h_scroll->get_value()-h_scroll->get_page()/8 ); + h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() / 8); } else if (v_scroll->is_visible_in_tree()) { - v_scroll->set_value( v_scroll->get_value()-v_scroll->get_page()/8 ); + v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / 8); } } - if (mb.button_index==BUTTON_WHEEL_DOWN && mb.pressed) { - if (h_scroll->is_visible_in_tree() && !v_scroll->is_visible_in_tree()){ + if (mb.button_index == BUTTON_WHEEL_DOWN && mb.pressed) { + if (h_scroll->is_visible_in_tree() && !v_scroll->is_visible_in_tree()) { // only horizontal is enabled, scroll horizontally - h_scroll->set_value( h_scroll->get_value()+h_scroll->get_page()/8 ); + h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() / 8); } else if (v_scroll->is_visible_in_tree()) { - v_scroll->set_value( v_scroll->get_value()+v_scroll->get_page()/8 ); + v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / 8); } } - if(!OS::get_singleton()->has_touchscreen_ui_hint()) + if (!OS::get_singleton()->has_touchscreen_ui_hint()) return; - if (mb.button_index!=BUTTON_LEFT) + if (mb.button_index != BUTTON_LEFT) break; if (mb.pressed) { if (drag_touching) { set_fixed_process(false); - drag_touching_deaccel=false; - drag_touching=false; - drag_speed=Vector2(); - drag_accum=Vector2(); - last_drag_accum=Vector2(); - drag_from=Vector2(); + drag_touching_deaccel = false; + drag_touching = false; + drag_speed = Vector2(); + drag_accum = Vector2(); + last_drag_accum = Vector2(); + drag_from = Vector2(); } if (true) { - drag_speed=Vector2(); - drag_accum=Vector2(); - last_drag_accum=Vector2(); - drag_from=Vector2(h_scroll->get_value(),v_scroll->get_value()); - drag_touching=OS::get_singleton()->has_touchscreen_ui_hint(); - drag_touching_deaccel=false; - time_since_motion=0; + drag_speed = Vector2(); + drag_accum = Vector2(); + last_drag_accum = Vector2(); + drag_from = Vector2(h_scroll->get_value(), v_scroll->get_value()); + drag_touching = OS::get_singleton()->has_touchscreen_ui_hint(); + drag_touching_deaccel = false; + time_since_motion = 0; if (drag_touching) { set_fixed_process(true); - time_since_motion=0; - + time_since_motion = 0; } } - } else { if (drag_touching) { - if (drag_speed==Vector2()) { - drag_touching_deaccel=false; - drag_touching=false; + if (drag_speed == Vector2()) { + drag_touching_deaccel = false; + drag_touching = false; set_fixed_process(false); } else { - drag_touching_deaccel=true; + drag_touching_deaccel = true; } } } - } break; case InputEvent::MOUSE_MOTION: { - const InputEventMouseMotion &mm=p_gui_input.mouse_motion; + const InputEventMouseMotion &mm = p_gui_input.mouse_motion; - if (drag_touching && ! drag_touching_deaccel) { + if (drag_touching && !drag_touching_deaccel) { - Vector2 motion = Vector2(mm.relative_x,mm.relative_y); - drag_accum-=motion; - Vector2 diff = drag_from+drag_accum; + Vector2 motion = Vector2(mm.relative_x, mm.relative_y); + drag_accum -= motion; + Vector2 diff = drag_from + drag_accum; if (scroll_h) h_scroll->set_value(diff.x); else - drag_accum.x=0; + drag_accum.x = 0; if (scroll_v) v_scroll->set_value(diff.y); else - drag_accum.y=0; - time_since_motion=0; + drag_accum.y = 0; + time_since_motion = 0; } } break; } - } - void ScrollContainer::_update_scrollbar_pos() { Size2 hmin = h_scroll->get_combined_minimum_size(); Size2 vmin = v_scroll->get_combined_minimum_size(); - v_scroll->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_END,vmin.width); - v_scroll->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,0); - v_scroll->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,0); - v_scroll->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,0); + v_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, vmin.width); + v_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0); + v_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0); + v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0); - h_scroll->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_BEGIN,0); - h_scroll->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,0); - h_scroll->set_anchor_and_margin(MARGIN_TOP,ANCHOR_END,hmin.height); - h_scroll->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,0); + h_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 0); + h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0); + h_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, hmin.height); + h_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0); h_scroll->raise(); v_scroll->raise(); - } - void ScrollContainer::_notification(int p_what) { if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) { @@ -211,18 +199,17 @@ void ScrollContainer::_notification(int p_what) { call_deferred("_update_scrollbar_pos"); }; - - if (p_what==NOTIFICATION_SORT_CHILDREN) { + if (p_what == NOTIFICATION_SORT_CHILDREN) { child_max_size = Size2(0, 0); Size2 size = get_size(); if (h_scroll->is_visible_in_tree()) - size.y-=h_scroll->get_minimum_size().y; + size.y -= h_scroll->get_minimum_size().y; if (v_scroll->is_visible_in_tree()) - size.x-=h_scroll->get_minimum_size().x; + size.x -= h_scroll->get_minimum_size().x; - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { Control *c = get_child(i)->cast_to<Control>(); if (!c) @@ -235,24 +222,23 @@ void ScrollContainer::_notification(int p_what) { child_max_size.x = MAX(child_max_size.x, minsize.x); child_max_size.y = MAX(child_max_size.y, minsize.y); - Rect2 r = Rect2(-scroll,minsize); - if (!scroll_h || (!h_scroll->is_visible_in_tree() && c->get_h_size_flags()&SIZE_EXPAND)) { - r.pos.x=0; - if (c->get_h_size_flags()&SIZE_EXPAND) - r.size.width=MAX(size.width,minsize.width); + Rect2 r = Rect2(-scroll, minsize); + if (!scroll_h || (!h_scroll->is_visible_in_tree() && c->get_h_size_flags() & SIZE_EXPAND)) { + r.pos.x = 0; + if (c->get_h_size_flags() & SIZE_EXPAND) + r.size.width = MAX(size.width, minsize.width); else - r.size.width=minsize.width; + r.size.width = minsize.width; } - if (!scroll_v || (!v_scroll->is_visible_in_tree() && c->get_v_size_flags()&SIZE_EXPAND)) { - r.pos.y=0; - r.size.height=size.height; - if (c->get_v_size_flags()&SIZE_EXPAND) - r.size.height=MAX(size.height,minsize.height); + if (!scroll_v || (!v_scroll->is_visible_in_tree() && c->get_v_size_flags() & SIZE_EXPAND)) { + r.pos.y = 0; + r.size.height = size.height; + if (c->get_v_size_flags() & SIZE_EXPAND) + r.size.height = MAX(size.height, minsize.height); else - r.size.height=minsize.height; - + r.size.height = minsize.height; } - fit_child_in_rect(c,r); + fit_child_in_rect(c, r); } update(); }; @@ -260,37 +246,36 @@ void ScrollContainer::_notification(int p_what) { if (p_what == NOTIFICATION_DRAW) { update_scrollbars(); - } - if (p_what==NOTIFICATION_FIXED_PROCESS) { + if (p_what == NOTIFICATION_FIXED_PROCESS) { - if (drag_touching) { + if (drag_touching) { if (drag_touching_deaccel) { - Vector2 pos = Vector2(h_scroll->get_value(),v_scroll->get_value()); - pos+=drag_speed*get_fixed_process_delta_time(); + Vector2 pos = Vector2(h_scroll->get_value(), v_scroll->get_value()); + pos += drag_speed * get_fixed_process_delta_time(); - bool turnoff_h=false; - bool turnoff_v=false; + bool turnoff_h = false; + bool turnoff_v = false; - if (pos.x<0) { - pos.x=0; - turnoff_h=true; + if (pos.x < 0) { + pos.x = 0; + turnoff_h = true; } - if (pos.x > (h_scroll->get_max()-h_scroll->get_page())) { - pos.x=h_scroll->get_max()-h_scroll->get_page(); - turnoff_h=true; + if (pos.x > (h_scroll->get_max() - h_scroll->get_page())) { + pos.x = h_scroll->get_max() - h_scroll->get_page(); + turnoff_h = true; } - if (pos.y<0) { - pos.y=0; - turnoff_v=true; + if (pos.y < 0) { + pos.y = 0; + turnoff_v = true; } - if (pos.y > (v_scroll->get_max()-v_scroll->get_page())) { - pos.y=v_scroll->get_max()-v_scroll->get_page(); - turnoff_v=true; + if (pos.y > (v_scroll->get_max() - v_scroll->get_page())) { + pos.y = v_scroll->get_max() - v_scroll->get_page(); + turnoff_v = true; } if (scroll_h) @@ -298,48 +283,43 @@ void ScrollContainer::_notification(int p_what) { if (scroll_v) v_scroll->set_value(pos.y); - float sgn_x = drag_speed.x<0? -1 : 1; + float sgn_x = drag_speed.x < 0 ? -1 : 1; float val_x = Math::abs(drag_speed.x); - val_x-=1000*get_fixed_process_delta_time(); + val_x -= 1000 * get_fixed_process_delta_time(); - if (val_x<0) { - turnoff_h=true; + if (val_x < 0) { + turnoff_h = true; } - - float sgn_y = drag_speed.y<0? -1 : 1; + float sgn_y = drag_speed.y < 0 ? -1 : 1; float val_y = Math::abs(drag_speed.y); - val_y-=1000*get_fixed_process_delta_time(); + val_y -= 1000 * get_fixed_process_delta_time(); - if (val_y<0) { - turnoff_v=true; + if (val_y < 0) { + turnoff_v = true; } - - drag_speed=Vector2(sgn_x*val_x,sgn_y*val_y); + drag_speed = Vector2(sgn_x * val_x, sgn_y * val_y); if (turnoff_h && turnoff_v) { set_fixed_process(false); - drag_touching=false; - drag_touching_deaccel=false; + drag_touching = false; + drag_touching_deaccel = false; } - } else { - - if (time_since_motion==0 || time_since_motion>0.1) { + if (time_since_motion == 0 || time_since_motion > 0.1) { Vector2 diff = drag_accum - last_drag_accum; - last_drag_accum=drag_accum; - drag_speed=diff/get_fixed_process_delta_time(); + last_drag_accum = drag_accum; + drag_speed = diff / get_fixed_process_delta_time(); } - time_since_motion+=get_fixed_process_delta_time(); + time_since_motion += get_fixed_process_delta_time(); } } } - }; void ScrollContainer::update_scrollbars() { @@ -354,66 +334,62 @@ void ScrollContainer::update_scrollbars() { if (!scroll_v || min.height <= size.height - hmin.height) { v_scroll->hide(); - scroll.y=0; + scroll.y = 0; } else { v_scroll->show(); - scroll.y=v_scroll->get_value(); - + scroll.y = v_scroll->get_value(); } v_scroll->set_max(min.height); v_scroll->set_page(size.height - hmin.height); - if (!scroll_h || min.width <= size.width - vmin.width) { h_scroll->hide(); - scroll.x=0; + scroll.x = 0; } else { h_scroll->show(); h_scroll->set_max(min.width); h_scroll->set_page(size.width - vmin.width); - scroll.x=h_scroll->get_value(); + scroll.x = h_scroll->get_value(); } } void ScrollContainer::_scroll_moved(float) { - scroll.x=h_scroll->get_value(); - scroll.y=v_scroll->get_value(); + scroll.x = h_scroll->get_value(); + scroll.y = v_scroll->get_value(); queue_sort(); update(); }; - void ScrollContainer::set_enable_h_scroll(bool p_enable) { - scroll_h=p_enable; + scroll_h = p_enable; queue_sort(); } -bool ScrollContainer::is_h_scroll_enabled() const{ +bool ScrollContainer::is_h_scroll_enabled() const { return scroll_h; } void ScrollContainer::set_enable_v_scroll(bool p_enable) { - scroll_v=p_enable; + scroll_v = p_enable; queue_sort(); } -bool ScrollContainer::is_v_scroll_enabled() const{ +bool ScrollContainer::is_v_scroll_enabled() const { return scroll_v; } int ScrollContainer::get_v_scroll() const { - return v_scroll->get_value(); } void ScrollContainer::set_v_scroll(int p_pos) { @@ -430,14 +406,13 @@ void ScrollContainer::set_h_scroll(int p_pos) { h_scroll->set_value(p_pos); _cancel_drag(); - } String ScrollContainer::get_configuration_warning() const { - int found=0; + int found = 0; - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { Control *c = get_child(i)->cast_to<Control>(); if (!c) @@ -450,31 +425,29 @@ String ScrollContainer::get_configuration_warning() const { found++; } - if (found!=1) + if (found != 1) return TTR("ScrollContainer is intended to work with a single child control.\nUse a container as child (VBox,HBox,etc), or a Control and set the custom minimum size manually."); else return ""; } - void ScrollContainer::_bind_methods() { - ClassDB::bind_method(D_METHOD("_scroll_moved"),&ScrollContainer::_scroll_moved); - ClassDB::bind_method(D_METHOD("_gui_input"),&ScrollContainer::_gui_input); - ClassDB::bind_method(D_METHOD("set_enable_h_scroll","enable"),&ScrollContainer::set_enable_h_scroll); - ClassDB::bind_method(D_METHOD("is_h_scroll_enabled"),&ScrollContainer::is_h_scroll_enabled); - ClassDB::bind_method(D_METHOD("set_enable_v_scroll","enable"),&ScrollContainer::set_enable_v_scroll); - ClassDB::bind_method(D_METHOD("is_v_scroll_enabled"),&ScrollContainer::is_v_scroll_enabled); - ClassDB::bind_method(D_METHOD("_update_scrollbar_pos"),&ScrollContainer::_update_scrollbar_pos); - ClassDB::bind_method(D_METHOD("set_h_scroll","val"),&ScrollContainer::set_h_scroll); - ClassDB::bind_method(D_METHOD("get_h_scroll"),&ScrollContainer::get_h_scroll); - ClassDB::bind_method(D_METHOD("set_v_scroll","val"),&ScrollContainer::set_v_scroll); - ClassDB::bind_method(D_METHOD("get_v_scroll"),&ScrollContainer::get_v_scroll); - - ADD_GROUP("Scroll","scroll_"); - ADD_PROPERTY( PropertyInfo( Variant::BOOL, "scroll_horizontal"), "set_enable_h_scroll","is_h_scroll_enabled"); - ADD_PROPERTY( PropertyInfo( Variant::BOOL, "scroll_vertical"), "set_enable_v_scroll","is_v_scroll_enabled"); - + ClassDB::bind_method(D_METHOD("_scroll_moved"), &ScrollContainer::_scroll_moved); + ClassDB::bind_method(D_METHOD("_gui_input"), &ScrollContainer::_gui_input); + ClassDB::bind_method(D_METHOD("set_enable_h_scroll", "enable"), &ScrollContainer::set_enable_h_scroll); + ClassDB::bind_method(D_METHOD("is_h_scroll_enabled"), &ScrollContainer::is_h_scroll_enabled); + ClassDB::bind_method(D_METHOD("set_enable_v_scroll", "enable"), &ScrollContainer::set_enable_v_scroll); + ClassDB::bind_method(D_METHOD("is_v_scroll_enabled"), &ScrollContainer::is_v_scroll_enabled); + ClassDB::bind_method(D_METHOD("_update_scrollbar_pos"), &ScrollContainer::_update_scrollbar_pos); + ClassDB::bind_method(D_METHOD("set_h_scroll", "val"), &ScrollContainer::set_h_scroll); + ClassDB::bind_method(D_METHOD("get_h_scroll"), &ScrollContainer::get_h_scroll); + ClassDB::bind_method(D_METHOD("set_v_scroll", "val"), &ScrollContainer::set_v_scroll); + ClassDB::bind_method(D_METHOD("get_v_scroll"), &ScrollContainer::get_v_scroll); + + ADD_GROUP("Scroll", "scroll_"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_horizontal"), "set_enable_h_scroll", "is_h_scroll_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_vertical"), "set_enable_v_scroll", "is_v_scroll_enabled"); }; ScrollContainer::ScrollContainer() { @@ -487,15 +460,14 @@ ScrollContainer::ScrollContainer() { v_scroll->set_name("_v_scroll"); add_child(v_scroll); - h_scroll->connect("value_changed", this,"_scroll_moved"); - v_scroll->connect("value_changed", this,"_scroll_moved"); + h_scroll->connect("value_changed", this, "_scroll_moved"); + v_scroll->connect("value_changed", this, "_scroll_moved"); - drag_speed=Vector2(); - drag_touching=false; - drag_touching_deaccel=false; - scroll_h=true; - scroll_v=true; + drag_speed = Vector2(); + drag_touching = false; + drag_touching_deaccel = false; + scroll_h = true; + scroll_v = true; set_clip_contents(true); }; - diff --git a/scene/gui/scroll_container.h b/scene/gui/scroll_container.h index 114cd06306..07b127843a 100644 --- a/scene/gui/scroll_container.h +++ b/scene/gui/scroll_container.h @@ -37,8 +37,8 @@ class ScrollContainer : public Container { GDCLASS(ScrollContainer, Container); - HScrollBar* h_scroll; - VScrollBar* v_scroll; + HScrollBar *h_scroll; + VScrollBar *v_scroll; Size2 child_max_size; Size2 scroll; @@ -63,16 +63,15 @@ class ScrollContainer : public Container { protected: Size2 get_minimum_size() const; - - void _gui_input(const InputEvent& p_gui_input); + void _gui_input(const InputEvent &p_gui_input); void _notification(int p_what); void _scroll_moved(float); static void _bind_methods(); void _update_scrollbar_pos(); -public: +public: int get_v_scroll() const; void set_v_scroll(int p_pos); diff --git a/scene/gui/separator.cpp b/scene/gui/separator.cpp index 32bd2239fc..e256b8f928 100644 --- a/scene/gui/separator.cpp +++ b/scene/gui/separator.cpp @@ -28,54 +28,47 @@ /*************************************************************************/ #include "separator.h" - Size2 Separator::get_minimum_size() const { - Size2 ms(3,3); - ms[orientation]=get_constant("separation"); + Size2 ms(3, 3); + ms[orientation] = get_constant("separation"); return ms; - - } void Separator::_notification(int p_what) { - - switch(p_what) { + switch (p_what) { case NOTIFICATION_DRAW: { Size2i size = get_size(); Ref<StyleBox> style = get_stylebox("separator"); - Size2i ssize=style->get_minimum_size()+style->get_center_size(); + Size2i ssize = style->get_minimum_size() + style->get_center_size(); - if (orientation==VERTICAL) { + if (orientation == VERTICAL) { - style->draw(get_canvas_item(),Rect2( (size.x-ssize.x)/2,0,ssize.x,size.y )); + style->draw(get_canvas_item(), Rect2((size.x - ssize.x) / 2, 0, ssize.x, size.y)); } else { - style->draw(get_canvas_item(),Rect2( 0,(size.y-ssize.y)/2,size.x,ssize.y )); + style->draw(get_canvas_item(), Rect2(0, (size.y - ssize.y) / 2, size.x, ssize.y)); } } break; } } -Separator::Separator() -{ +Separator::Separator() { } - -Separator::~Separator() -{ +Separator::~Separator() { } HSeparator::HSeparator() { - orientation=HORIZONTAL; + orientation = HORIZONTAL; } VSeparator::VSeparator() { - orientation=VERTICAL; + orientation = VERTICAL; } diff --git a/scene/gui/separator.h b/scene/gui/separator.h index 56d8a9724f..7913531748 100644 --- a/scene/gui/separator.h +++ b/scene/gui/separator.h @@ -36,40 +36,33 @@ #include "scene/gui/control.h" class Separator : public Control { - GDCLASS( Separator, Control ); - + GDCLASS(Separator, Control); protected: - Orientation orientation; void _notification(int p_what); -public: +public: virtual Size2 get_minimum_size() const; Separator(); ~Separator(); - }; class VSeparator : public Separator { - GDCLASS( VSeparator, Separator ); + GDCLASS(VSeparator, Separator); public: - VSeparator(); - }; class HSeparator : public Separator { - GDCLASS( HSeparator, Separator ); + GDCLASS(HSeparator, Separator); public: - HSeparator(); - }; #endif diff --git a/scene/gui/slider.cpp b/scene/gui/slider.cpp index a6d6864b16..365d450ed6 100644 --- a/scene/gui/slider.cpp +++ b/scene/gui/slider.cpp @@ -29,91 +29,88 @@ #include "slider.h" #include "os/keyboard.h" - Size2 Slider::get_minimum_size() const { Ref<StyleBox> style = get_stylebox("slider"); - Size2i ms = style->get_minimum_size()+style->get_center_size(); + Size2i ms = style->get_minimum_size() + style->get_center_size(); return ms; } void Slider::_gui_input(InputEvent p_event) { - - - if (p_event.type==InputEvent::MOUSE_BUTTON) { + if (p_event.type == InputEvent::MOUSE_BUTTON) { InputEventMouseButton &mb = p_event.mouse_button; - if (mb.button_index==BUTTON_LEFT) { + if (mb.button_index == BUTTON_LEFT) { - if (mb.pressed) { - Ref<Texture> grabber = get_icon(mouse_inside||has_focus()?"grabber_hilite":"grabber"); - grab.pos=orientation==VERTICAL?mb.y:mb.x; + if (mb.pressed) { + Ref<Texture> grabber = get_icon(mouse_inside || has_focus() ? "grabber_hilite" : "grabber"); + grab.pos = orientation == VERTICAL ? mb.y : mb.x; double grab_width = (double)grabber->get_size().width; double grab_height = (double)grabber->get_size().height; - double max = orientation==VERTICAL ? get_size().height - grab_height : get_size().width - grab_width; - if (orientation==VERTICAL) - set_as_ratio( 1 - (((double)grab.pos - (grab_height / 2.0)) / max) ); + double max = orientation == VERTICAL ? get_size().height - grab_height : get_size().width - grab_width; + if (orientation == VERTICAL) + set_as_ratio(1 - (((double)grab.pos - (grab_height / 2.0)) / max)); else - set_as_ratio(((double)grab.pos - (grab_width/2.0)) / max); - grab.active=true; - grab.uvalue=get_as_ratio(); + set_as_ratio(((double)grab.pos - (grab_width / 2.0)) / max); + grab.active = true; + grab.uvalue = get_as_ratio(); } else { - grab.active=false; + grab.active = false; } - } else if (mb.pressed && mb.button_index==BUTTON_WHEEL_UP) { + } else if (mb.pressed && mb.button_index == BUTTON_WHEEL_UP) { - set_value( get_value() + get_step()); - } else if (mb.pressed && mb.button_index==BUTTON_WHEEL_DOWN) { - set_value( get_value() - get_step()); + set_value(get_value() + get_step()); + } else if (mb.pressed && mb.button_index == BUTTON_WHEEL_DOWN) { + set_value(get_value() - get_step()); } - } else if (p_event.type==InputEvent::MOUSE_MOTION) { + } else if (p_event.type == InputEvent::MOUSE_MOTION) { if (grab.active) { Size2i size = get_size(); Ref<Texture> grabber = get_icon("grabber"); - float motion = (orientation==VERTICAL?p_event.mouse_motion.y:p_event.mouse_motion.x) - grab.pos; - if (orientation==VERTICAL) - motion=-motion; - float areasize = orientation==VERTICAL?size.height - grabber->get_size().height:size.width - grabber->get_size().width; - if (areasize<=0) + float motion = (orientation == VERTICAL ? p_event.mouse_motion.y : p_event.mouse_motion.x) - grab.pos; + if (orientation == VERTICAL) + motion = -motion; + float areasize = orientation == VERTICAL ? size.height - grabber->get_size().height : size.width - grabber->get_size().width; + if (areasize <= 0) return; float umotion = motion / float(areasize); - set_as_ratio( grab.uvalue + umotion ); + set_as_ratio(grab.uvalue + umotion); } } else { if (p_event.is_action("ui_left") && p_event.is_pressed()) { - if (orientation!=HORIZONTAL) + if (orientation != HORIZONTAL) return; - set_value( get_value() - (custom_step>=0?custom_step:get_step()) ); + set_value(get_value() - (custom_step >= 0 ? custom_step : get_step())); accept_event(); } else if (p_event.is_action("ui_right") && p_event.is_pressed()) { - if (orientation!=HORIZONTAL) + if (orientation != HORIZONTAL) return; - set_value( get_value() + (custom_step>=0?custom_step:get_step()) ); + set_value(get_value() + (custom_step >= 0 ? custom_step : get_step())); accept_event(); } else if (p_event.is_action("ui_up") && p_event.is_pressed()) { - if (orientation!=VERTICAL) + if (orientation != VERTICAL) return; - set_value( get_value() + (custom_step>=0?custom_step:get_step()) ); + set_value(get_value() + (custom_step >= 0 ? custom_step : get_step())); accept_event(); } else if (p_event.is_action("ui_down") && p_event.is_pressed()) { - if (orientation!=VERTICAL) + if (orientation != VERTICAL) return; - set_value( get_value() - (custom_step>=0?custom_step:get_step()) ); + set_value(get_value() - (custom_step >= 0 ? custom_step : get_step())); accept_event(); - } else if (p_event.type==InputEvent::KEY) { + } else if (p_event.type == InputEvent::KEY) { - const InputEventKey &k=p_event.key; + const InputEventKey &k = p_event.key; if (!k.pressed) return; @@ -122,35 +119,32 @@ void Slider::_gui_input(InputEvent p_event) { case KEY_HOME: { - set_value( get_min() ); + set_value(get_min()); accept_event(); } break; case KEY_END: { - set_value( get_max() ); + set_value(get_max()); accept_event(); } break; - - } ; + }; } } - } void Slider::_notification(int p_what) { - - switch(p_what) { + switch (p_what) { case NOTIFICATION_MOUSE_ENTER: { - mouse_inside=true; + mouse_inside = true; update(); } break; case NOTIFICATION_MOUSE_EXIT: { - mouse_inside=false; + mouse_inside = false; update(); } break; case NOTIFICATION_DRAW: { @@ -158,45 +152,43 @@ void Slider::_notification(int p_what) { Size2i size = get_size(); Ref<StyleBox> style = get_stylebox("slider"); Ref<StyleBox> focus = get_stylebox("focus"); - Ref<Texture> grabber = get_icon(mouse_inside||has_focus()?"grabber_hilite":"grabber"); + Ref<Texture> grabber = get_icon(mouse_inside || has_focus() ? "grabber_hilite" : "grabber"); Ref<Texture> tick = get_icon("tick"); - if (orientation==VERTICAL) { + if (orientation == VERTICAL) { - style->draw(ci,Rect2i(Point2i(),Size2i(style->get_minimum_size().width+style->get_center_size().width,size.height))); + style->draw(ci, Rect2i(Point2i(), Size2i(style->get_minimum_size().width + style->get_center_size().width, size.height))); /* if (mouse_inside||has_focus()) focus->draw(ci,Rect2i(Point2i(),Size2i(style->get_minimum_size().width+style->get_center_size().width,size.height))); */ float areasize = size.height - grabber->get_size().height; - if (ticks>1) { + if (ticks > 1) { int tickarea = size.height - tick->get_height(); - for(int i=0;i<ticks;i++) { - if( ! ticks_on_borders && (i == 0 || i + 1 == ticks) ) continue; - int ofs = i*tickarea/(ticks-1); - tick->draw(ci,Point2(0,ofs)); + for (int i = 0; i < ticks; i++) { + if (!ticks_on_borders && (i == 0 || i + 1 == ticks)) continue; + int ofs = i * tickarea / (ticks - 1); + tick->draw(ci, Point2(0, ofs)); } - } - grabber->draw(ci,Point2i(size.width/2-grabber->get_size().width/2,size.height - get_as_ratio()*areasize - grabber->get_size().height)); + grabber->draw(ci, Point2i(size.width / 2 - grabber->get_size().width / 2, size.height - get_as_ratio() * areasize - grabber->get_size().height)); } else { - style->draw(ci,Rect2i(Point2i(),Size2i(size.width,style->get_minimum_size().height+style->get_center_size().height))); + style->draw(ci, Rect2i(Point2i(), Size2i(size.width, style->get_minimum_size().height + style->get_center_size().height))); /* if (mouse_inside||has_focus()) focus->draw(ci,Rect2i(Point2i(),Size2i(size.width,style->get_minimum_size().height+style->get_center_size().height))); */ float areasize = size.width - grabber->get_size().width; - if (ticks>1) { + if (ticks > 1) { int tickarea = size.width - tick->get_width(); - for(int i=0;i<ticks;i++) { - if( (! ticks_on_borders) && ( (i == 0) || ((i + 1) == ticks)) ) continue; - int ofs = i*tickarea/(ticks-1); - tick->draw(ci,Point2(ofs,0)); + for (int i = 0; i < ticks; i++) { + if ((!ticks_on_borders) && ((i == 0) || ((i + 1) == ticks))) continue; + int ofs = i * tickarea / (ticks - 1); + tick->draw(ci, Point2(ofs, 0)); } - } - grabber->draw(ci,Point2i(get_as_ratio()*areasize,size.height/2-grabber->get_size().height/2)); + grabber->draw(ci, Point2i(get_as_ratio() * areasize, size.height / 2 - grabber->get_size().height / 2)); } } break; @@ -205,7 +197,7 @@ void Slider::_notification(int p_what) { void Slider::set_custom_step(float p_custom_step) { - custom_step=p_custom_step; + custom_step = p_custom_step; } float Slider::get_custom_step() const { @@ -215,7 +207,7 @@ float Slider::get_custom_step() const { void Slider::set_ticks(int p_count) { - ticks=p_count; + ticks = p_count; update(); } @@ -224,35 +216,34 @@ int Slider::get_ticks() const { return ticks; } -bool Slider::get_ticks_on_borders() const{ +bool Slider::get_ticks_on_borders() const { return ticks_on_borders; } -void Slider::set_ticks_on_borders(bool _tob){ +void Slider::set_ticks_on_borders(bool _tob) { ticks_on_borders = _tob; update(); } void Slider::_bind_methods() { - ClassDB::bind_method(D_METHOD("_gui_input"),&Slider::_gui_input); - ClassDB::bind_method(D_METHOD("set_ticks","count"),&Slider::set_ticks); - ClassDB::bind_method(D_METHOD("get_ticks"),&Slider::get_ticks); - - ClassDB::bind_method(D_METHOD("get_ticks_on_borders"),&Slider::get_ticks_on_borders); - ClassDB::bind_method(D_METHOD("set_ticks_on_borders","ticks_on_border"),&Slider::set_ticks_on_borders); + ClassDB::bind_method(D_METHOD("_gui_input"), &Slider::_gui_input); + ClassDB::bind_method(D_METHOD("set_ticks", "count"), &Slider::set_ticks); + ClassDB::bind_method(D_METHOD("get_ticks"), &Slider::get_ticks); - ADD_PROPERTY( PropertyInfo( Variant::INT, "tick_count", PROPERTY_HINT_RANGE,"0,4096,1"), "set_ticks", "get_ticks") ; - ADD_PROPERTY( PropertyInfo( Variant::BOOL, "ticks_on_borders" ), "set_ticks_on_borders", "get_ticks_on_borders") ; - ADD_PROPERTY( PropertyInfo( Variant::INT,"focus_mode", PROPERTY_HINT_ENUM, "None,Click,All" ), "set_focus_mode", "get_focus_mode") ; + ClassDB::bind_method(D_METHOD("get_ticks_on_borders"), &Slider::get_ticks_on_borders); + ClassDB::bind_method(D_METHOD("set_ticks_on_borders", "ticks_on_border"), &Slider::set_ticks_on_borders); + ADD_PROPERTY(PropertyInfo(Variant::INT, "tick_count", PROPERTY_HINT_RANGE, "0,4096,1"), "set_ticks", "get_ticks"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ticks_on_borders"), "set_ticks_on_borders", "get_ticks_on_borders"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "focus_mode", PROPERTY_HINT_ENUM, "None,Click,All"), "set_focus_mode", "get_focus_mode"); } Slider::Slider(Orientation p_orientation) { - orientation=p_orientation; - mouse_inside=false; - grab.active=false; - ticks=0; - custom_step=-1; + orientation = p_orientation; + mouse_inside = false; + grab.active = false; + ticks = 0; + custom_step = -1; set_focus_mode(FOCUS_ALL); } diff --git a/scene/gui/slider.h b/scene/gui/slider.h index 89eb32737b..f649a4d150 100644 --- a/scene/gui/slider.h +++ b/scene/gui/slider.h @@ -33,7 +33,7 @@ class Slider : public Range { - GDCLASS( Slider, Range ); + GDCLASS(Slider, Range); struct Grab { int pos; @@ -46,16 +46,13 @@ class Slider : public Range { Orientation orientation; float custom_step; - protected: - void _gui_input(InputEvent p_event); void _notification(int p_what); static void _bind_methods(); bool ticks_on_borders; public: - virtual Size2 get_minimum_size() const; void set_custom_step(float p_custom_step); @@ -67,25 +64,25 @@ public: void set_ticks_on_borders(bool); bool get_ticks_on_borders() const; - Slider(Orientation p_orientation=VERTICAL); + Slider(Orientation p_orientation = VERTICAL); }; - - class HSlider : public Slider { - GDCLASS( HSlider, Slider ); -public: + GDCLASS(HSlider, Slider); - HSlider() : Slider(HORIZONTAL) { set_v_size_flags(0);} +public: + HSlider() + : Slider(HORIZONTAL) { set_v_size_flags(0); } }; class VSlider : public Slider { - GDCLASS( VSlider, Slider ); -public: + GDCLASS(VSlider, Slider); - VSlider() : Slider(VERTICAL) { set_h_size_flags(0);} +public: + VSlider() + : Slider(VERTICAL) { set_h_size_flags(0); } }; #endif // SLIDER_H diff --git a/scene/gui/spin_box.cpp b/scene/gui/spin_box.cpp index f2c04d2f76..3f21093fb7 100644 --- a/scene/gui/spin_box.cpp +++ b/scene/gui/spin_box.cpp @@ -32,53 +32,47 @@ Size2 SpinBox::get_minimum_size() const { Size2 ms = line_edit->get_combined_minimum_size(); - ms.width+=last_w; + ms.width += last_w; return ms; } - void SpinBox::_value_changed(double) { - String value = String::num(get_value(),Math::step_decimals(get_step())); - if (prefix!="") - value=prefix+" "+value; - if (suffix!="") - value+=" "+suffix; + String value = String::num(get_value(), Math::step_decimals(get_step())); + if (prefix != "") + value = prefix + " " + value; + if (suffix != "") + value += " " + suffix; line_edit->set_text(value); } -void SpinBox::_text_entered(const String& p_string) { +void SpinBox::_text_entered(const String &p_string) { /* if (!p_string.is_numeric()) return; */ String value = p_string; - if (prefix!="" && p_string.begins_with(prefix)) - value = p_string.substr(prefix.length(), p_string.length()-prefix.length()); - set_value( value.to_double() ); + if (prefix != "" && p_string.begins_with(prefix)) + value = p_string.substr(prefix.length(), p_string.length() - prefix.length()); + set_value(value.to_double()); _value_changed(0); } - LineEdit *SpinBox::get_line_edit() { return line_edit; } - -void SpinBox::_line_edit_input(const InputEvent& p_event) { - - - +void SpinBox::_line_edit_input(const InputEvent &p_event) { } void SpinBox::_range_click_timeout() { if (!drag.enabled && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) { - bool up = get_local_mouse_pos().y < (get_size().height/2); - set_value( get_value() + (up?get_step():-get_step())); + bool up = get_local_mouse_pos().y < (get_size().height / 2); + set_value(get_value() + (up ? get_step() : -get_step())); if (range_click_timer->is_one_shot()) { range_click_timer->set_wait_time(0.075); @@ -91,23 +85,21 @@ void SpinBox::_range_click_timeout() { } } - -void SpinBox::_gui_input(const InputEvent& p_event) { +void SpinBox::_gui_input(const InputEvent &p_event) { if (!is_editable()) { return; } - if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed) { - const InputEventMouseButton &mb=p_event.mouse_button; - + if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed) { + const InputEventMouseButton &mb = p_event.mouse_button; - bool up = mb.y < (get_size().height/2); + bool up = mb.y < (get_size().height / 2); - switch(mb.button_index) { + switch (mb.button_index) { case BUTTON_LEFT: { - set_value( get_value() + (up?get_step():-get_step())); + set_value(get_value() + (up ? get_step() : -get_step())); range_click_timer->set_wait_time(0.6); range_click_timer->set_one_shot(true); @@ -117,70 +109,68 @@ void SpinBox::_gui_input(const InputEvent& p_event) { } break; case BUTTON_RIGHT: { - set_value( (up?get_max():get_min()) ); + set_value((up ? get_max() : get_min())); line_edit->grab_focus(); } break; case BUTTON_WHEEL_UP: { if (line_edit->has_focus()) { - set_value( get_value() + get_step() ); + set_value(get_value() + get_step()); accept_event(); } } break; case BUTTON_WHEEL_DOWN: { if (line_edit->has_focus()) { - set_value( get_value() - get_step() ); + set_value(get_value() - get_step()); accept_event(); } } break; } } - if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed && p_event.mouse_button.button_index==1) { + if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed && p_event.mouse_button.button_index == 1) { //set_default_cursor_shape(CURSOR_VSIZE); - Vector2 cpos = Vector2(p_event.mouse_button.x,p_event.mouse_button.y); - drag.mouse_pos=cpos; + Vector2 cpos = Vector2(p_event.mouse_button.x, p_event.mouse_button.y); + drag.mouse_pos = cpos; } - if (p_event.type==InputEvent::MOUSE_BUTTON && !p_event.mouse_button.pressed && p_event.mouse_button.button_index==1) { + if (p_event.type == InputEvent::MOUSE_BUTTON && !p_event.mouse_button.pressed && p_event.mouse_button.button_index == 1) { //set_default_cursor_shape(CURSOR_ARROW); range_click_timer->stop(); if (drag.enabled) { - drag.enabled=false; + drag.enabled = false; Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE); warp_mouse(drag.capture_pos); } } - if (p_event.type==InputEvent::MOUSE_MOTION && p_event.mouse_button.button_mask&1) { + if (p_event.type == InputEvent::MOUSE_MOTION && p_event.mouse_button.button_mask & 1) { - Vector2 cpos = Vector2(p_event.mouse_motion.x,p_event.mouse_motion.y); + Vector2 cpos = Vector2(p_event.mouse_motion.x, p_event.mouse_motion.y); if (drag.enabled) { float diff_y = drag.mouse_pos.y - cpos.y; - diff_y=Math::pow(ABS(diff_y),1.8f)*SGN(diff_y); - diff_y*=0.1; + diff_y = Math::pow(ABS(diff_y), 1.8f) * SGN(diff_y); + diff_y *= 0.1; - drag.mouse_pos=cpos; - drag.base_val=CLAMP(drag.base_val + get_step() * diff_y, get_min(), get_max()); + drag.mouse_pos = cpos; + drag.base_val = CLAMP(drag.base_val + get_step() * diff_y, get_min(), get_max()); - set_value( drag.base_val); + set_value(drag.base_val); - } else if (drag.mouse_pos.distance_to(cpos)>2) { + } else if (drag.mouse_pos.distance_to(cpos) > 2) { Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED); - drag.enabled=true; - drag.base_val=get_value(); - drag.mouse_pos=cpos; - drag.capture_pos=cpos; - + drag.enabled = true; + drag.base_val = get_value(); + drag.mouse_pos = cpos; + drag.capture_pos = cpos; } } } - void SpinBox::_line_edit_focus_exit() { _text_entered(line_edit->get_text()); @@ -188,54 +178,48 @@ void SpinBox::_line_edit_focus_exit() { void SpinBox::_notification(int p_what) { - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { Ref<Texture> updown = get_icon("updown"); int w = updown->get_width(); - if (w!=last_w) { - line_edit->set_margin(MARGIN_RIGHT,w); - last_w=w; + if (w != last_w) { + line_edit->set_margin(MARGIN_RIGHT, w); + last_w = w; } RID ci = get_canvas_item(); Size2i size = get_size(); - updown->draw(ci,Point2i(size.width-updown->get_width(),(size.height-updown->get_height())/2)); - - } else if (p_what==NOTIFICATION_FOCUS_EXIT) { + updown->draw(ci, Point2i(size.width - updown->get_width(), (size.height - updown->get_height()) / 2)); + } else if (p_what == NOTIFICATION_FOCUS_EXIT) { //_value_changed(0); - } else if (p_what==NOTIFICATION_ENTER_TREE) { + } else if (p_what == NOTIFICATION_ENTER_TREE) { _value_changed(0); } - } +void SpinBox::set_suffix(const String &p_suffix) { -void SpinBox::set_suffix(const String& p_suffix) { - - suffix=p_suffix; + suffix = p_suffix; _value_changed(0); - } -String SpinBox::get_suffix() const{ +String SpinBox::get_suffix() const { return suffix; } +void SpinBox::set_prefix(const String &p_prefix) { -void SpinBox::set_prefix(const String& p_prefix) { - - prefix=p_prefix; + prefix = p_prefix; _value_changed(0); - } -String SpinBox::get_prefix() const{ +String SpinBox::get_prefix() const { return prefix; } @@ -252,41 +236,38 @@ bool SpinBox::is_editable() const { void SpinBox::_bind_methods() { //ClassDB::bind_method(D_METHOD("_value_changed"),&SpinBox::_value_changed); - ClassDB::bind_method(D_METHOD("_gui_input"),&SpinBox::_gui_input); - ClassDB::bind_method(D_METHOD("_text_entered"),&SpinBox::_text_entered); - ClassDB::bind_method(D_METHOD("set_suffix","suffix"),&SpinBox::set_suffix); - ClassDB::bind_method(D_METHOD("get_suffix"),&SpinBox::get_suffix); - ClassDB::bind_method(D_METHOD("set_prefix","prefix"),&SpinBox::set_prefix); - ClassDB::bind_method(D_METHOD("get_prefix"),&SpinBox::get_prefix); - ClassDB::bind_method(D_METHOD("set_editable","editable"),&SpinBox::set_editable); - ClassDB::bind_method(D_METHOD("is_editable"),&SpinBox::is_editable); - ClassDB::bind_method(D_METHOD("_line_edit_focus_exit"),&SpinBox::_line_edit_focus_exit); - ClassDB::bind_method(D_METHOD("get_line_edit"),&SpinBox::get_line_edit); - ClassDB::bind_method(D_METHOD("_line_edit_input"),&SpinBox::_line_edit_input); - ClassDB::bind_method(D_METHOD("_range_click_timeout"),&SpinBox::_range_click_timeout); - - - ADD_PROPERTY(PropertyInfo(Variant::BOOL,"editable"),"set_editable","is_editable"); - ADD_PROPERTY(PropertyInfo(Variant::STRING,"prefix"),"set_prefix","get_prefix"); - ADD_PROPERTY(PropertyInfo(Variant::STRING,"suffix"),"set_suffix","get_suffix"); - - + ClassDB::bind_method(D_METHOD("_gui_input"), &SpinBox::_gui_input); + ClassDB::bind_method(D_METHOD("_text_entered"), &SpinBox::_text_entered); + ClassDB::bind_method(D_METHOD("set_suffix", "suffix"), &SpinBox::set_suffix); + ClassDB::bind_method(D_METHOD("get_suffix"), &SpinBox::get_suffix); + ClassDB::bind_method(D_METHOD("set_prefix", "prefix"), &SpinBox::set_prefix); + ClassDB::bind_method(D_METHOD("get_prefix"), &SpinBox::get_prefix); + ClassDB::bind_method(D_METHOD("set_editable", "editable"), &SpinBox::set_editable); + ClassDB::bind_method(D_METHOD("is_editable"), &SpinBox::is_editable); + ClassDB::bind_method(D_METHOD("_line_edit_focus_exit"), &SpinBox::_line_edit_focus_exit); + ClassDB::bind_method(D_METHOD("get_line_edit"), &SpinBox::get_line_edit); + ClassDB::bind_method(D_METHOD("_line_edit_input"), &SpinBox::_line_edit_input); + ClassDB::bind_method(D_METHOD("_range_click_timeout"), &SpinBox::_range_click_timeout); + + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "prefix"), "set_prefix", "get_prefix"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "suffix"), "set_suffix", "get_suffix"); } SpinBox::SpinBox() { last_w = 0; - line_edit = memnew( LineEdit ); + line_edit = memnew(LineEdit); add_child(line_edit); line_edit->set_area_as_parent_rect(); //connect("value_changed",this,"_value_changed"); - line_edit->connect("text_entered",this,"_text_entered",Vector<Variant>(),CONNECT_DEFERRED); - line_edit->connect("focus_exited",this,"_line_edit_focus_exit",Vector<Variant>(),CONNECT_DEFERRED); - line_edit->connect("gui_input",this,"_line_edit_input"); - drag.enabled=false; + line_edit->connect("text_entered", this, "_text_entered", Vector<Variant>(), CONNECT_DEFERRED); + line_edit->connect("focus_exited", this, "_line_edit_focus_exit", Vector<Variant>(), CONNECT_DEFERRED); + line_edit->connect("gui_input", this, "_line_edit_input"); + drag.enabled = false; - range_click_timer = memnew( Timer ); - range_click_timer->connect("timeout",this,"_range_click_timeout"); + range_click_timer = memnew(Timer); + range_click_timer->connect("timeout", this, "_range_click_timeout"); add_child(range_click_timer); } diff --git a/scene/gui/spin_box.h b/scene/gui/spin_box.h index 9974ec47bc..eba58402fa 100644 --- a/scene/gui/spin_box.h +++ b/scene/gui/spin_box.h @@ -35,7 +35,7 @@ class SpinBox : public Range { - GDCLASS( SpinBox, Range ); + GDCLASS(SpinBox, Range); LineEdit *line_edit; int last_w; @@ -43,35 +43,31 @@ class SpinBox : public Range { Timer *range_click_timer; void _range_click_timeout(); - void _text_entered(const String& p_string); + void _text_entered(const String &p_string); virtual void _value_changed(double); String prefix; String suffix; - void _line_edit_input(const InputEvent& p_event); - + void _line_edit_input(const InputEvent &p_event); struct Drag { float base_val; bool enabled; Vector2 from; - Vector2 mouse_pos; + Vector2 mouse_pos; Vector2 capture_pos; } drag; - void _line_edit_focus_exit(); protected: - - void _gui_input(const InputEvent& p_event); - + void _gui_input(const InputEvent &p_event); void _notification(int p_what); static void _bind_methods(); -public: +public: LineEdit *get_line_edit(); virtual Size2 get_minimum_size() const; @@ -79,10 +75,10 @@ public: void set_editable(bool p_editable); bool is_editable() const; - void set_suffix(const String& p_suffix); + void set_suffix(const String &p_suffix); String get_suffix() const; - void set_prefix(const String& p_prefix); + void set_prefix(const String &p_prefix); String get_prefix() const; SpinBox(); diff --git a/scene/gui/split_container.cpp b/scene/gui/split_container.cpp index 14ea96a4e6..5b6f17e0f7 100644 --- a/scene/gui/split_container.cpp +++ b/scene/gui/split_container.cpp @@ -28,10 +28,8 @@ /*************************************************************************/ #include "split_container.h" -#include "margin_container.h" #include "label.h" - - +#include "margin_container.h" struct _MinSizeCache { @@ -40,235 +38,208 @@ struct _MinSizeCache { int final_size; }; - Control *SplitContainer::_getch(int p_idx) const { - int idx=0; + int idx = 0; - for(int i=0;i<get_child_count();i++) { - Control *c=get_child(i)->cast_to<Control>(); + for (int i = 0; i < get_child_count(); i++) { + Control *c = get_child(i)->cast_to<Control>(); if (!c || !c->is_visible_in_tree()) continue; if (c->is_set_as_toplevel()) continue; - if (idx==p_idx) + if (idx == p_idx) return c; idx++; } return NULL; - } - void SplitContainer::_resort() { /** First pass, determine minimum size AND amount of stretchable elements */ - int axis = vertical?1:0; + int axis = vertical ? 1 : 0; - bool has_first=_getch(0); - bool has_second=_getch(1); + bool has_first = _getch(0); + bool has_second = _getch(1); if (!has_first && !has_second) { return; - } else if (! (has_first && has_second)) { + } else if (!(has_first && has_second)) { if (has_first) - fit_child_in_rect(_getch(0),Rect2(Point2(),get_size())); + fit_child_in_rect(_getch(0), Rect2(Point2(), get_size())); else - fit_child_in_rect(_getch(1),Rect2(Point2(),get_size())); + fit_child_in_rect(_getch(1), Rect2(Point2(), get_size())); return; } + Control *first = _getch(0); + Control *second = _getch(1); - - Control *first=_getch(0); - Control *second=_getch(1); - - - bool ratiomode=false; - bool expand_first_mode=false; - - - + bool ratiomode = false; + bool expand_first_mode = false; if (vertical) { - ratiomode=first->get_v_size_flags()&SIZE_EXPAND && second->get_v_size_flags()&SIZE_EXPAND; - expand_first_mode=first->get_v_size_flags()&SIZE_EXPAND && !(second->get_v_size_flags()&SIZE_EXPAND); + ratiomode = first->get_v_size_flags() & SIZE_EXPAND && second->get_v_size_flags() & SIZE_EXPAND; + expand_first_mode = first->get_v_size_flags() & SIZE_EXPAND && !(second->get_v_size_flags() & SIZE_EXPAND); } else { - ratiomode=first->get_h_size_flags()&SIZE_EXPAND && second->get_h_size_flags()&SIZE_EXPAND; - expand_first_mode=first->get_h_size_flags()&SIZE_EXPAND && !(second->get_h_size_flags()&SIZE_EXPAND); + ratiomode = first->get_h_size_flags() & SIZE_EXPAND && second->get_h_size_flags() & SIZE_EXPAND; + expand_first_mode = first->get_h_size_flags() & SIZE_EXPAND && !(second->get_h_size_flags() & SIZE_EXPAND); } - - int sep=get_constant("separation"); + int sep = get_constant("separation"); Ref<Texture> g = get_icon("grabber"); - if (dragger_visibility==DRAGGER_HIDDEN_COLLAPSED) { - sep=0; + if (dragger_visibility == DRAGGER_HIDDEN_COLLAPSED) { + sep = 0; } else { - sep=MAX(sep,vertical?g->get_height():g->get_width()); + sep = MAX(sep, vertical ? g->get_height() : g->get_width()); } - int total = vertical?get_size().height:get_size().width; + int total = vertical ? get_size().height : get_size().width; - total-=sep; + total -= sep; - int minimum=0; + int minimum = 0; Size2 ms_first = first->get_combined_minimum_size(); Size2 ms_second = second->get_combined_minimum_size(); if (vertical) { - minimum=ms_first.height+ms_second.height; + minimum = ms_first.height + ms_second.height; } else { - minimum=ms_first.width+ms_second.width; + minimum = ms_first.width + ms_second.width; } - int available=total-minimum; - if (available<0) - available=0; - + int available = total - minimum; + if (available < 0) + available = 0; - middle_sep=0; + middle_sep = 0; if (collapsed) { - if (ratiomode) { - middle_sep=ms_first[axis]+available/2; - + middle_sep = ms_first[axis] + available / 2; } else if (expand_first_mode) { - middle_sep=get_size()[axis]-ms_second[axis]-sep; + middle_sep = get_size()[axis] - ms_second[axis] - sep; } else { - middle_sep=ms_first[axis]; + middle_sep = ms_first[axis]; } - } else if (ratiomode) { - if (expand_ofs<-(available/2)) - expand_ofs=-(available/2); - else if (expand_ofs>(available/2)) - expand_ofs=(available/2); - - middle_sep=ms_first[axis]+available/2+expand_ofs; + if (expand_ofs < -(available / 2)) + expand_ofs = -(available / 2); + else if (expand_ofs > (available / 2)) + expand_ofs = (available / 2); + middle_sep = ms_first[axis] + available / 2 + expand_ofs; } else if (expand_first_mode) { - if (expand_ofs>0) - expand_ofs=0; + if (expand_ofs > 0) + expand_ofs = 0; if (expand_ofs < -available) - expand_ofs=-available; + expand_ofs = -available; - middle_sep=get_size()[axis]-ms_second[axis]-sep+expand_ofs; + middle_sep = get_size()[axis] - ms_second[axis] - sep + expand_ofs; } else { - if (expand_ofs<0) - expand_ofs=0; + if (expand_ofs < 0) + expand_ofs = 0; if (expand_ofs > available) - expand_ofs=available; - - middle_sep=ms_first[axis]+expand_ofs; + expand_ofs = available; + middle_sep = ms_first[axis] + expand_ofs; } - - if (vertical) { - fit_child_in_rect(first,Rect2(Point2(0,0),Size2(get_size().width,middle_sep))); - int sofs=middle_sep+sep; - fit_child_in_rect(second,Rect2(Point2(0,sofs),Size2(get_size().width,get_size().height-sofs))); + fit_child_in_rect(first, Rect2(Point2(0, 0), Size2(get_size().width, middle_sep))); + int sofs = middle_sep + sep; + fit_child_in_rect(second, Rect2(Point2(0, sofs), Size2(get_size().width, get_size().height - sofs))); } else { - - fit_child_in_rect(first,Rect2(Point2(0,0),Size2(middle_sep,get_size().height))); - int sofs=middle_sep+sep; - fit_child_in_rect(second,Rect2(Point2(sofs,0),Size2(get_size().width-sofs,get_size().height))); + fit_child_in_rect(first, Rect2(Point2(0, 0), Size2(middle_sep, get_size().height))); + int sofs = middle_sep + sep; + fit_child_in_rect(second, Rect2(Point2(sofs, 0), Size2(get_size().width - sofs, get_size().height))); } update(); _change_notify("split/offset"); - } - - - Size2 SplitContainer::get_minimum_size() const { - /* Calculate MINIMUM SIZE */ Size2i minimum; - int sep=get_constant("separation"); + int sep = get_constant("separation"); Ref<Texture> g = get_icon("grabber"); - sep=(dragger_visibility!=DRAGGER_HIDDEN_COLLAPSED)?MAX(sep,vertical?g->get_height():g->get_width()):0; + sep = (dragger_visibility != DRAGGER_HIDDEN_COLLAPSED) ? MAX(sep, vertical ? g->get_height() : g->get_width()) : 0; - for(int i=0;i<2;i++) { + for (int i = 0; i < 2; i++) { if (!_getch(i)) break; - if (i==1) { + if (i == 1) { if (vertical) - minimum.height+=sep; + minimum.height += sep; else - minimum.width+=sep; + minimum.width += sep; } Size2 ms = _getch(i)->get_combined_minimum_size(); if (vertical) { - minimum.height+=ms.height; - minimum.width=MAX(minimum.width,ms.width); + minimum.height += ms.height; + minimum.width = MAX(minimum.width, ms.width); } else { - minimum.width+=ms.width; - minimum.height=MAX(minimum.height,ms.height); + minimum.width += ms.width; + minimum.height = MAX(minimum.height, ms.height); } - } return minimum; - - } void SplitContainer::_notification(int p_what) { - switch(p_what) { + switch (p_what) { case NOTIFICATION_SORT_CHILDREN: { _resort(); } break; case NOTIFICATION_MOUSE_ENTER: { - mouse_inside=true; + mouse_inside = true; update(); } break; case NOTIFICATION_MOUSE_EXIT: { - mouse_inside=false; + mouse_inside = false; update(); } break; case NOTIFICATION_DRAW: { @@ -278,111 +249,105 @@ void SplitContainer::_notification(int p_what) { if (collapsed || (!mouse_inside && get_constant("autohide"))) return; - int sep=dragger_visibility!=DRAGGER_HIDDEN_COLLAPSED?get_constant("separation"):0; + int sep = dragger_visibility != DRAGGER_HIDDEN_COLLAPSED ? get_constant("separation") : 0; Ref<Texture> tex = get_icon("grabber"); - Size2 size=get_size(); + Size2 size = get_size(); if (vertical) { //draw_style_box( get_stylebox("bg"), Rect2(0,middle_sep,get_size().width,sep)); - if (dragger_visibility==DRAGGER_VISIBLE) - draw_texture(tex,Point2i((size.x-tex->get_width())/2,middle_sep+(sep-tex->get_height())/2)); + if (dragger_visibility == DRAGGER_VISIBLE) + draw_texture(tex, Point2i((size.x - tex->get_width()) / 2, middle_sep + (sep - tex->get_height()) / 2)); } else { //draw_style_box( get_stylebox("bg"), Rect2(middle_sep,0,sep,get_size().height)); - if (dragger_visibility==DRAGGER_VISIBLE) - draw_texture(tex,Point2i(middle_sep+(sep-tex->get_width())/2,(size.y-tex->get_height())/2)); - + if (dragger_visibility == DRAGGER_VISIBLE) + draw_texture(tex, Point2i(middle_sep + (sep - tex->get_width()) / 2, (size.y - tex->get_height()) / 2)); } } break; } } -void SplitContainer::_gui_input(const InputEvent& p_event) { +void SplitContainer::_gui_input(const InputEvent &p_event) { - if (collapsed || !_getch(0) || !_getch(1) || dragger_visibility!=DRAGGER_VISIBLE) + if (collapsed || !_getch(0) || !_getch(1) || dragger_visibility != DRAGGER_VISIBLE) return; - if (p_event.type==InputEvent::MOUSE_BUTTON) { + if (p_event.type == InputEvent::MOUSE_BUTTON) { - const InputEventMouseButton &mb=p_event.mouse_button; + const InputEventMouseButton &mb = p_event.mouse_button; - if (mb.button_index==BUTTON_LEFT) { + if (mb.button_index == BUTTON_LEFT) { if (mb.pressed) { - int sep=get_constant("separation"); + int sep = get_constant("separation"); if (vertical) { - - if (mb.y > middle_sep && mb.y < middle_sep+sep) { - dragging=true; - drag_from=mb.y; - drag_ofs=expand_ofs; + if (mb.y > middle_sep && mb.y < middle_sep + sep) { + dragging = true; + drag_from = mb.y; + drag_ofs = expand_ofs; } } else { - if (mb.x > middle_sep && mb.x < middle_sep+sep) { - dragging=true; - drag_from=mb.x; - drag_ofs=expand_ofs; + if (mb.x > middle_sep && mb.x < middle_sep + sep) { + dragging = true; + drag_from = mb.x; + drag_ofs = expand_ofs; } } } else { - dragging=false; + dragging = false; } - } } - if (p_event.type==InputEvent::MOUSE_MOTION) { + if (p_event.type == InputEvent::MOUSE_MOTION) { - const InputEventMouseMotion &mm=p_event.mouse_motion; + const InputEventMouseMotion &mm = p_event.mouse_motion; if (dragging) { - expand_ofs=drag_ofs+((vertical?mm.y:mm.x)-drag_from); + expand_ofs = drag_ofs + ((vertical ? mm.y : mm.x) - drag_from); queue_sort(); - emit_signal("dragged",get_split_offset()); + emit_signal("dragged", get_split_offset()); } } - } -Control::CursorShape SplitContainer::get_cursor_shape(const Point2& p_pos) const { +Control::CursorShape SplitContainer::get_cursor_shape(const Point2 &p_pos) const { if (collapsed) return Control::get_cursor_shape(p_pos); if (dragging) - return (vertical?CURSOR_VSIZE:CURSOR_HSIZE); + return (vertical ? CURSOR_VSIZE : CURSOR_HSIZE); - int sep=get_constant("separation"); + int sep = get_constant("separation"); if (vertical) { - - if (p_pos.y > middle_sep && p_pos.y < middle_sep+sep) { + if (p_pos.y > middle_sep && p_pos.y < middle_sep + sep) { return CURSOR_VSIZE; } } else { - if (p_pos.x > middle_sep && p_pos.x < middle_sep+sep) { + if (p_pos.x > middle_sep && p_pos.x < middle_sep + sep) { return CURSOR_HSIZE; } } return Control::get_cursor_shape(p_pos); - } void SplitContainer::set_split_offset(int p_offset) { - if (expand_ofs==p_offset) + if (expand_ofs == p_offset) return; - expand_ofs=p_offset; + expand_ofs = p_offset; queue_sort(); } @@ -393,16 +358,15 @@ int SplitContainer::get_split_offset() const { void SplitContainer::set_collapsed(bool p_collapsed) { - if (collapsed==p_collapsed) + if (collapsed == p_collapsed) return; - collapsed=p_collapsed; + collapsed = p_collapsed; queue_sort(); - } void SplitContainer::set_dragger_visibility(DraggerVisibility p_visibility) { - dragger_visibility=p_visibility; + dragger_visibility = p_visibility; queue_sort(); update(); } @@ -412,49 +376,41 @@ SplitContainer::DraggerVisibility SplitContainer::get_dragger_visibility() const return dragger_visibility; } - bool SplitContainer::is_collapsed() const { - return collapsed; } - void SplitContainer::_bind_methods() { - ClassDB::bind_method(D_METHOD("_gui_input"),&SplitContainer::_gui_input); - ClassDB::bind_method(D_METHOD("set_split_offset","offset"),&SplitContainer::set_split_offset); - ClassDB::bind_method(D_METHOD("get_split_offset"),&SplitContainer::get_split_offset); + ClassDB::bind_method(D_METHOD("_gui_input"), &SplitContainer::_gui_input); + ClassDB::bind_method(D_METHOD("set_split_offset", "offset"), &SplitContainer::set_split_offset); + ClassDB::bind_method(D_METHOD("get_split_offset"), &SplitContainer::get_split_offset); - ClassDB::bind_method(D_METHOD("set_collapsed","collapsed"),&SplitContainer::set_collapsed); - ClassDB::bind_method(D_METHOD("is_collapsed"),&SplitContainer::is_collapsed); + ClassDB::bind_method(D_METHOD("set_collapsed", "collapsed"), &SplitContainer::set_collapsed); + ClassDB::bind_method(D_METHOD("is_collapsed"), &SplitContainer::is_collapsed); - ClassDB::bind_method(D_METHOD("set_dragger_visibility","mode"),&SplitContainer::set_dragger_visibility); - ClassDB::bind_method(D_METHOD("get_dragger_visibility"),&SplitContainer::get_dragger_visibility); + ClassDB::bind_method(D_METHOD("set_dragger_visibility", "mode"), &SplitContainer::set_dragger_visibility); + ClassDB::bind_method(D_METHOD("get_dragger_visibility"), &SplitContainer::get_dragger_visibility); - ADD_SIGNAL( MethodInfo("dragged",PropertyInfo(Variant::INT,"offset"))); + ADD_SIGNAL(MethodInfo("dragged", PropertyInfo(Variant::INT, "offset"))); - ADD_PROPERTY( PropertyInfo(Variant::INT,"split_offset"),"set_split_offset","get_split_offset"); - ADD_PROPERTY( PropertyInfo(Variant::BOOL,"collapsed"),"set_collapsed","is_collapsed"); - ADD_PROPERTY( PropertyInfo(Variant::INT,"dragger_visibility",PROPERTY_HINT_ENUM,"Visible,Hidden,Hidden & Collapsed"),"set_dragger_visibility","get_dragger_visibility"); - - BIND_CONSTANT( DRAGGER_VISIBLE ); - BIND_CONSTANT( DRAGGER_HIDDEN ); - BIND_CONSTANT( DRAGGER_HIDDEN_COLLAPSED ); + ADD_PROPERTY(PropertyInfo(Variant::INT, "split_offset"), "set_split_offset", "get_split_offset"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed"), "set_collapsed", "is_collapsed"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "dragger_visibility", PROPERTY_HINT_ENUM, "Visible,Hidden,Hidden & Collapsed"), "set_dragger_visibility", "get_dragger_visibility"); + BIND_CONSTANT(DRAGGER_VISIBLE); + BIND_CONSTANT(DRAGGER_HIDDEN); + BIND_CONSTANT(DRAGGER_HIDDEN_COLLAPSED); } SplitContainer::SplitContainer(bool p_vertical) { - - mouse_inside=false; - expand_ofs=0; - middle_sep=0; - vertical=p_vertical; - dragging=false; - collapsed=false; - dragger_visibility=DRAGGER_VISIBLE; - + mouse_inside = false; + expand_ofs = 0; + middle_sep = 0; + vertical = p_vertical; + dragging = false; + collapsed = false; + dragger_visibility = DRAGGER_VISIBLE; } - - diff --git a/scene/gui/split_container.h b/scene/gui/split_container.h index 03b6b1a167..8db6bfa658 100644 --- a/scene/gui/split_container.h +++ b/scene/gui/split_container.h @@ -31,16 +31,17 @@ #include "scene/gui/container.h" - class SplitContainer : public Container { - GDCLASS(SplitContainer,Container); + GDCLASS(SplitContainer, Container); + public: enum DraggerVisibility { DRAGGER_VISIBLE, DRAGGER_HIDDEN, DRAGGER_HIDDEN_COLLAPSED }; + private: bool vertical; int expand_ofs; @@ -52,20 +53,16 @@ private: DraggerVisibility dragger_visibility; bool mouse_inside; - Control *_getch(int p_idx) const; void _resort(); -protected: - - void _gui_input(const InputEvent& p_event); +protected: + void _gui_input(const InputEvent &p_event); void _notification(int p_what); static void _bind_methods(); -public: - - +public: void set_split_offset(int p_offset); int get_split_offset() const; @@ -75,32 +72,31 @@ public: void set_dragger_visibility(DraggerVisibility p_visibility); DraggerVisibility get_dragger_visibility() const; - virtual CursorShape get_cursor_shape(const Point2& p_pos=Point2i()) const; + virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const; virtual Size2 get_minimum_size() const; - SplitContainer(bool p_vertical=false); + SplitContainer(bool p_vertical = false); }; VARIANT_ENUM_CAST(SplitContainer::DraggerVisibility); class HSplitContainer : public SplitContainer { - GDCLASS(HSplitContainer,SplitContainer); + GDCLASS(HSplitContainer, SplitContainer); public: - - HSplitContainer() : SplitContainer(false) {set_default_cursor_shape(CURSOR_HSPLIT);} + HSplitContainer() + : SplitContainer(false) { set_default_cursor_shape(CURSOR_HSPLIT); } }; - class VSplitContainer : public SplitContainer { - GDCLASS(VSplitContainer,SplitContainer); + GDCLASS(VSplitContainer, SplitContainer); public: - - VSplitContainer() : SplitContainer(true) {set_default_cursor_shape(CURSOR_VSPLIT);} + VSplitContainer() + : SplitContainer(true) { set_default_cursor_shape(CURSOR_VSPLIT); } }; #endif // SPLIT_CONTAINER_H diff --git a/scene/gui/tab_container.cpp b/scene/gui/tab_container.cpp index fc25b68db9..89769937d4 100644 --- a/scene/gui/tab_container.cpp +++ b/scene/gui/tab_container.cpp @@ -30,7 +30,6 @@ #include "message_queue.h" - int TabContainer::_get_top_margin() const { if (!tabs_visible) @@ -47,7 +46,7 @@ int TabContainer::_get_top_margin() const { Ref<Font> font = get_font("font"); int content_height = font->get_height(); - Vector<Control*> tabs = _get_tabs(); + Vector<Control *> tabs = _get_tabs(); for (int i = 0; i < tabs.size(); i++) { Control *c = tabs[i]; @@ -63,13 +62,9 @@ int TabContainer::_get_top_margin() const { return tab_height + content_height; } +void TabContainer::_gui_input(const InputEvent &p_event) { - -void TabContainer::_gui_input(const InputEvent& p_event) { - - if (p_event.type == InputEvent::MOUSE_BUTTON - && p_event.mouse_button.pressed - && p_event.mouse_button.button_index == BUTTON_LEFT) { + if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed && p_event.mouse_button.button_index == BUTTON_LEFT) { Point2 pos(p_event.mouse_button.x, p_event.mouse_button.y); Size2 size = get_size(); @@ -92,7 +87,7 @@ void TabContainer::_gui_input(const InputEvent& p_event) { return; } - Vector<Control*> tabs = _get_tabs(); + Vector<Control *> tabs = _get_tabs(); // Handle navigation buttons. if (buttons_visible_cache) { @@ -144,7 +139,7 @@ void TabContainer::_notification(int p_what) { return; } - Vector<Control*> tabs = _get_tabs(); + Vector<Control *> tabs = _get_tabs(); Ref<StyleBox> tab_bg = get_stylebox("tab_bg"); Ref<StyleBox> tab_fg = get_stylebox("tab_fg"); Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled"); @@ -233,9 +228,7 @@ void TabContainer::_notification(int p_what) { // Draw the tab contents. Control *control = tabs[i + first_tab_cache]->cast_to<Control>(); - String text = control->has_meta("_tab_name") - ? String(XL_MESSAGE(String(control->get_meta("_tab_name")))) - : String(control->get_name()); + String text = control->has_meta("_tab_name") ? String(XL_MESSAGE(String(control->get_meta("_tab_name")))) : String(control->get_name()); int x_content = tab_rect.pos.x + tab_style->get_margin(MARGIN_LEFT); int top_margin = tab_style->get_margin(MARGIN_TOP); @@ -276,13 +269,13 @@ void TabContainer::_notification(int p_what) { x -= increment->get_width(); increment->draw(canvas, - Point2(x, y_center - (increment->get_height() / 2)), - Color(1, 1, 1, last_tab_cache < tabs.size() - 1 ? 1.0 : 0.5)); + Point2(x, y_center - (increment->get_height() / 2)), + Color(1, 1, 1, last_tab_cache < tabs.size() - 1 ? 1.0 : 0.5)); x -= decrement->get_width(); decrement->draw(canvas, - Point2(x, y_center - (decrement->get_height() / 2)), - Color(1, 1, 1, first_tab_cache > 0 ? 1.0 : 0.5)); + Point2(x, y_center - (decrement->get_height() / 2)), + Color(1, 1, 1, first_tab_cache > 0 ? 1.0 : 0.5)); } // Draw the tab area. @@ -303,9 +296,7 @@ int TabContainer::_get_tab_width(int p_index) const { // Get the width of the text displayed on the tab. Ref<Font> font = get_font("font"); - String text = control->has_meta("_tab_name") - ? String(XL_MESSAGE(String(control->get_meta("_tab_name")))) - : String(control->get_name()); + String text = control->has_meta("_tab_name") ? String(XL_MESSAGE(String(control->get_meta("_tab_name")))) : String(control->get_name()); int width = font->get_string_size(text).width; // Add space for a tab icon. @@ -333,9 +324,9 @@ int TabContainer::_get_tab_width(int p_index) const { return width; } -Vector<Control*> TabContainer::_get_tabs() const { +Vector<Control *> TabContainer::_get_tabs() const { - Vector<Control*> controls; + Vector<Control *> controls; for (int i = 0; i < get_child_count(); i++) { Control *control = get_child(i)->cast_to<Control>(); @@ -380,7 +371,6 @@ void TabContainer::add_child_notify(Node *p_child) { for (int i = 0; i < 4; i++) c->set_margin(Margin(i), c->get_margin(Margin(i)) + sb->get_margin(Margin(i))); - update(); p_child->connect("renamed", this, "_child_renamed_callback"); if (first) @@ -392,7 +382,6 @@ int TabContainer::get_tab_count() const { return _get_tabs().size(); } - void TabContainer::set_current_tab(int p_current) { ERR_FAIL_INDEX(p_current, get_tab_count()); @@ -401,7 +390,7 @@ void TabContainer::set_current_tab(int p_current) { current = p_current; Ref<StyleBox> sb = get_stylebox("panel"); - Vector<Control*> tabs = _get_tabs(); + Vector<Control *> tabs = _get_tabs(); for (int i = 0; i < tabs.size(); i++) { Control *c = tabs[i]; @@ -413,9 +402,7 @@ void TabContainer::set_current_tab(int p_current) { for (int i = 0; i < 4; i++) c->set_margin(Margin(i), c->get_margin(Margin(i)) + sb->get_margin(Margin(i))); - - } - else + } else c->hide(); } @@ -438,22 +425,22 @@ int TabContainer::get_current_tab() const { } int TabContainer::get_previous_tab() const { - - return previous; + + return previous; } -Control* TabContainer::get_tab_control(int p_idx) const { +Control *TabContainer::get_tab_control(int p_idx) const { - Vector<Control*> tabs = _get_tabs(); + Vector<Control *> tabs = _get_tabs(); if (p_idx >= 0 && p_idx < tabs.size()) return tabs[p_idx]; else return NULL; } -Control* TabContainer::get_current_tab_control() const { +Control *TabContainer::get_current_tab_control() const { - Vector<Control*> tabs = _get_tabs(); + Vector<Control *> tabs = _get_tabs(); if (current >= 0 && current < tabs.size()) return tabs[current]; else @@ -499,7 +486,7 @@ void TabContainer::set_tabs_visible(bool p_visibe) { tabs_visible = p_visibe; - Vector<Control*> tabs = _get_tabs(); + Vector<Control *> tabs = _get_tabs(); for (int i = 0; i < tabs.size(); i++) { Control *c = tabs[i]; @@ -507,7 +494,6 @@ void TabContainer::set_tabs_visible(bool p_visibe) { c->set_margin(MARGIN_TOP, _get_top_margin()); else c->set_margin(MARGIN_TOP, 0); - } update(); } @@ -515,21 +501,18 @@ void TabContainer::set_tabs_visible(bool p_visibe) { bool TabContainer::are_tabs_visible() const { return tabs_visible; - } Control *TabContainer::_get_tab(int p_idx) const { return get_tab_control(p_idx); - } -void TabContainer::set_tab_title(int p_tab, const String& p_title) { +void TabContainer::set_tab_title(int p_tab, const String &p_title) { Control *child = _get_tab(p_tab); ERR_FAIL_COND(!child); child->set_meta("_tab_name", p_title); - } String TabContainer::get_tab_title(int p_tab) const { @@ -540,15 +523,13 @@ String TabContainer::get_tab_title(int p_tab) const { return child->get_meta("_tab_name"); else return child->get_name(); - } -void TabContainer::set_tab_icon(int p_tab, const Ref<Texture>& p_icon) { +void TabContainer::set_tab_icon(int p_tab, const Ref<Texture> &p_icon) { Control *child = _get_tab(p_tab); ERR_FAIL_COND(!child); child->set_meta("_tab_icon", p_icon); - } Ref<Texture> TabContainer::get_tab_icon(int p_tab) const { @@ -580,7 +561,7 @@ bool TabContainer::get_tab_disabled(int p_tab) const { void TabContainer::get_translatable_strings(List<String> *p_strings) const { - Vector<Control*> tabs = _get_tabs(); + Vector<Control *> tabs = _get_tabs(); for (int i = 0; i < tabs.size(); i++) { Control *c = tabs[i]; @@ -599,7 +580,7 @@ Size2 TabContainer::get_minimum_size() const { Size2 ms; - Vector<Control*> tabs = _get_tabs(); + Vector<Control *> tabs = _get_tabs(); for (int i = 0; i < tabs.size(); i++) { Control *c = tabs[i]; @@ -632,7 +613,7 @@ void TabContainer::set_popup(Node *p_popup) { update(); } -Popup* TabContainer::get_popup() const { +Popup *TabContainer::get_popup() const { return popup; } @@ -667,7 +648,6 @@ void TabContainer::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_align", "get_tab_align"); ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tabs_visible"), "set_tabs_visible", "are_tabs_visible"); - } TabContainer::TabContainer() { @@ -681,5 +661,4 @@ TabContainer::TabContainer() { align = ALIGN_CENTER; tabs_visible = true; popup = NULL; - }
\ No newline at end of file diff --git a/scene/gui/tab_container.h b/scene/gui/tab_container.h index d5a2801bbe..2594611844 100644 --- a/scene/gui/tab_container.h +++ b/scene/gui/tab_container.h @@ -29,22 +29,21 @@ #ifndef TAB_CONTAINER_H #define TAB_CONTAINER_H - #include "scene/gui/control.h" #include "scene/gui/popup.h" class TabContainer : public Control { - GDCLASS( TabContainer, Control ); -public: + GDCLASS(TabContainer, Control); +public: enum TabAlign { ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT }; -private: +private: int mouse_x_cache; int first_tab_cache; int tabs_ofs_cache; @@ -58,13 +57,12 @@ private: int _get_top_margin() const; Popup *popup; - Vector<Control*> _get_tabs() const; + Vector<Control *> _get_tabs() const; int _get_tab_width(int p_index) const; protected: - void _child_renamed_callback(); - void _gui_input(const InputEvent& p_event); + void _gui_input(const InputEvent &p_event); void _notification(int p_what); virtual void add_child_notify(Node *p_child); virtual void remove_child_notify(Node *p_child); @@ -72,18 +70,16 @@ protected: static void _bind_methods(); public: - - void set_tab_align(TabAlign p_align); TabAlign get_tab_align() const; void set_tabs_visible(bool p_visibe); bool are_tabs_visible() const; - void set_tab_title(int p_tab,const String& p_title); + void set_tab_title(int p_tab, const String &p_title); String get_tab_title(int p_tab) const; - void set_tab_icon(int p_tab,const Ref<Texture>& p_icon); + void set_tab_icon(int p_tab, const Ref<Texture> &p_icon); Ref<Texture> get_tab_icon(int p_tab) const; void set_tab_disabled(int p_tab, bool p_disabled); @@ -94,21 +90,19 @@ public: int get_current_tab() const; int get_previous_tab() const; - Control* get_tab_control(int p_idx) const; - Control* get_current_tab_control() const; + Control *get_tab_control(int p_idx) const; + Control *get_current_tab_control() const; virtual Size2 get_minimum_size() const; virtual void get_translatable_strings(List<String> *p_strings) const; void set_popup(Node *p_popup); - Popup* get_popup() const; - + Popup *get_popup() const; TabContainer(); }; - -VARIANT_ENUM_CAST( TabContainer::TabAlign ); +VARIANT_ENUM_CAST(TabContainer::TabAlign); #endif // TAB_CONTAINER_H diff --git a/scene/gui/tabs.cpp b/scene/gui/tabs.cpp index 4e07a495ed..3197553876 100644 --- a/scene/gui/tabs.cpp +++ b/scene/gui/tabs.cpp @@ -37,152 +37,146 @@ Size2 Tabs::get_minimum_size() const { Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled"); Ref<Font> font = get_font("font"); - Size2 ms(0, MAX(MAX(tab_bg->get_minimum_size().height, tab_fg->get_minimum_size().height), tab_disabled->get_minimum_size().height)+font->get_height()); + Size2 ms(0, MAX(MAX(tab_bg->get_minimum_size().height, tab_fg->get_minimum_size().height), tab_disabled->get_minimum_size().height) + font->get_height()); - for(int i=0;i<tabs.size();i++) { + for (int i = 0; i < tabs.size(); i++) { Ref<Texture> tex = tabs[i].icon; if (tex.is_valid()) { ms.height = MAX(ms.height, tex->get_size().height); - if (tabs[i].text!="") - ms.width+=get_constant("hseparation"); + if (tabs[i].text != "") + ms.width += get_constant("hseparation"); } - ms.width+=font->get_string_size(tabs[i].text).width; + ms.width += font->get_string_size(tabs[i].text).width; if (tabs[i].disabled) ms.width += tab_disabled->get_minimum_size().width; - else if (current==i) - ms.width+=tab_fg->get_minimum_size().width; + else if (current == i) + ms.width += tab_fg->get_minimum_size().width; else - ms.width+=tab_bg->get_minimum_size().width; + ms.width += tab_bg->get_minimum_size().width; if (tabs[i].right_button.is_valid()) { - Ref<Texture> rb=tabs[i].right_button; + Ref<Texture> rb = tabs[i].right_button; Size2 bms = rb->get_size(); - bms.width+=get_constant("hseparation"); - ms.width+=bms.width; - ms.height=MAX(bms.height+tab_bg->get_minimum_size().height,ms.height); + bms.width += get_constant("hseparation"); + ms.width += bms.width; + ms.height = MAX(bms.height + tab_bg->get_minimum_size().height, ms.height); } - if (cb_displaypolicy==CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy==CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i==current)) { - Ref<Texture> cb=get_icon("close"); + if (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i == current)) { + Ref<Texture> cb = get_icon("close"); Size2 bms = cb->get_size(); - bms.width+=get_constant("hseparation"); - ms.width+=bms.width; - ms.height=MAX(bms.height+tab_bg->get_minimum_size().height,ms.height); + bms.width += get_constant("hseparation"); + ms.width += bms.width; + ms.height = MAX(bms.height + tab_bg->get_minimum_size().height, ms.height); } } - ms.width=0; //TODO: should make this optional + ms.width = 0; //TODO: should make this optional return ms; } +void Tabs::_gui_input(const InputEvent &p_event) { -void Tabs::_gui_input(const InputEvent& p_event) { + if (p_event.type == InputEvent::MOUSE_MOTION) { - if (p_event.type==InputEvent::MOUSE_MOTION) { + Point2 pos(p_event.mouse_motion.x, p_event.mouse_motion.y); - Point2 pos( p_event.mouse_motion.x, p_event.mouse_motion.y ); - - hilite_arrow=-1; + hilite_arrow = -1; if (buttons_visible) { Ref<Texture> incr = get_icon("increment"); Ref<Texture> decr = get_icon("decrement"); - int limit=get_size().width-incr->get_width()-decr->get_width(); + int limit = get_size().width - incr->get_width() - decr->get_width(); - if (pos.x>limit+decr->get_width()) { - hilite_arrow=1; - } else if (pos.x>limit) { - hilite_arrow=0; + if (pos.x > limit + decr->get_width()) { + hilite_arrow = 1; + } else if (pos.x > limit) { + hilite_arrow = 0; } } // test hovering to display right or close button - int hover_buttons=-1; - hover=-1; - for(int i=0;i<tabs.size();i++) { + int hover_buttons = -1; + hover = -1; + for (int i = 0; i < tabs.size(); i++) { - if (i<offset) + if (i < offset) continue; if (tabs[i].rb_rect.has_point(pos)) { - rb_hover=i; - cb_hover=-1; + rb_hover = i; + cb_hover = -1; hover_buttons = i; break; - } - else if (!tabs[i].disabled && tabs[i].cb_rect.has_point(pos)) { - cb_hover=i; - rb_hover=-1; + } else if (!tabs[i].disabled && tabs[i].cb_rect.has_point(pos)) { + cb_hover = i; + rb_hover = -1; hover_buttons = i; break; } - } if (hover_buttons == -1) { // no hover - rb_hover= hover_buttons; - cb_hover= hover_buttons; + rb_hover = hover_buttons; + cb_hover = hover_buttons; } update(); return; } + if (rb_pressing && p_event.type == InputEvent::MOUSE_BUTTON && + !p_event.mouse_button.pressed && + p_event.mouse_button.button_index == BUTTON_LEFT) { - if (rb_pressing && p_event.type==InputEvent::MOUSE_BUTTON && - !p_event.mouse_button.pressed && - p_event.mouse_button.button_index==BUTTON_LEFT) { - - if (rb_hover!=-1) { + if (rb_hover != -1) { //pressed - emit_signal("right_button_pressed",rb_hover); + emit_signal("right_button_pressed", rb_hover); } - rb_pressing=false; + rb_pressing = false; update(); } + if (cb_pressing && p_event.type == InputEvent::MOUSE_BUTTON && + !p_event.mouse_button.pressed && + p_event.mouse_button.button_index == BUTTON_LEFT) { - if (cb_pressing && p_event.type==InputEvent::MOUSE_BUTTON && - !p_event.mouse_button.pressed && - p_event.mouse_button.button_index==BUTTON_LEFT) { - - if (cb_hover!=-1) { + if (cb_hover != -1) { //pressed - emit_signal("tab_close",cb_hover); + emit_signal("tab_close", cb_hover); } - cb_pressing=false; + cb_pressing = false; update(); } - - if (p_event.type==InputEvent::MOUSE_BUTTON && - p_event.mouse_button.pressed && - p_event.mouse_button.button_index==BUTTON_LEFT) { + if (p_event.type == InputEvent::MOUSE_BUTTON && + p_event.mouse_button.pressed && + p_event.mouse_button.button_index == BUTTON_LEFT) { // clicks - Point2 pos( p_event.mouse_button.x, p_event.mouse_button.y ); + Point2 pos(p_event.mouse_button.x, p_event.mouse_button.y); if (buttons_visible) { Ref<Texture> incr = get_icon("increment"); Ref<Texture> decr = get_icon("decrement"); - int limit=get_size().width-incr->get_width()-decr->get_width(); + int limit = get_size().width - incr->get_width() - decr->get_width(); - if (pos.x>limit+decr->get_width()) { + if (pos.x > limit + decr->get_width()) { if (missing_right) { offset++; update(); } return; - } else if (pos.x>limit) { - if (offset>0) { + } else if (pos.x > limit) { + if (offset > 0) { offset--; update(); } @@ -190,25 +184,25 @@ void Tabs::_gui_input(const InputEvent& p_event) { } } - int found=-1; - for(int i=0;i<tabs.size();i++) { + int found = -1; + for (int i = 0; i < tabs.size(); i++) { - if (i<offset) + if (i < offset) continue; if (tabs[i].rb_rect.has_point(pos)) { - rb_pressing=true; + rb_pressing = true; update(); return; } if (tabs[i].cb_rect.has_point(pos)) { - cb_pressing=true; + cb_pressing = true; update(); return; } - if (pos.x >=tabs[i].ofs_cache && pos.x<tabs[i].ofs_cache+tabs[i].size_cache) { + if (pos.x >= tabs[i].ofs_cache && pos.x < tabs[i].ofs_cache + tabs[i].size_cache) { if (!tabs[i].disabled) { found = i; } @@ -216,25 +210,22 @@ void Tabs::_gui_input(const InputEvent& p_event) { } } - - if (found!=-1) { + if (found != -1) { set_current_tab(found); - emit_signal("tab_changed",found); + emit_signal("tab_changed", found); } } - } - void Tabs::_notification(int p_what) { - switch(p_what) { + switch (p_what) { case NOTIFICATION_MOUSE_EXIT: { - rb_hover=-1; - cb_hover=-1; - hover=-1; + rb_hover = -1; + cb_hover = -1; + hover = -1; update(); } break; case NOTIFICATION_RESIZED: { @@ -252,27 +243,26 @@ void Tabs::_notification(int p_what) { Color color_fg = get_color("font_color_fg"); Color color_bg = get_color("font_color_bg"); Color color_disabled = get_color("font_color_disabled"); - Ref<Texture> close=get_icon("close"); + Ref<Texture> close = get_icon("close"); int h = get_size().height; int w = 0; int mw = 0; - for(int i=0;i<tabs.size();i++) { + for (int i = 0; i < tabs.size(); i++) { tabs[i].ofs_cache = mw; mw += get_tab_width(i); } - - if (tab_align==ALIGN_CENTER) { - w=(get_size().width-mw)/2; - } else if (tab_align==ALIGN_RIGHT) { - w=get_size().width-mw; + if (tab_align == ALIGN_CENTER) { + w = (get_size().width - mw) / 2; + } else if (tab_align == ALIGN_RIGHT) { + w = get_size().width - mw; } - if (w<0) { - w=0; + if (w < 0) { + w = 0; } Ref<Texture> incr = get_icon("increment"); @@ -280,31 +270,30 @@ void Tabs::_notification(int p_what) { Ref<Texture> incr_hl = get_icon("increment_hilite"); Ref<Texture> decr_hl = get_icon("decrement_hilite"); - int limit=get_size().width - incr->get_size().width - decr->get_size().width; + int limit = get_size().width - incr->get_size().width - decr->get_size().width; - missing_right=false; + missing_right = false; - for(int i=0;i<tabs.size();i++) { + for (int i = 0; i < tabs.size(); i++) { - if (i<offset) + if (i < offset) continue; - tabs[i].ofs_cache=w; + tabs[i].ofs_cache = w; int lsize = get_tab_width(i); String text = tabs[i].text; int slen = font->get_string_size(text).width; - if (w+lsize > limit) { - max_drawn_tab=i-1; - missing_right=true; + if (w + lsize > limit) { + max_drawn_tab = i - 1; + missing_right = true; break; } else { - max_drawn_tab=i; + max_drawn_tab = i; } - Ref<StyleBox> sb; Color col; @@ -312,105 +301,101 @@ void Tabs::_notification(int p_what) { sb = tab_disabled; col = color_disabled; } else if (i == current) { - sb=tab_fg; - col=color_fg; + sb = tab_fg; + col = color_fg; } else { - sb=tab_bg; - col=color_bg; + sb = tab_bg; + col = color_bg; } - Rect2 sb_rect = Rect2(w, 0, lsize, h); sb->draw(ci, sb_rect); - w+=sb->get_margin(MARGIN_LEFT); + w += sb->get_margin(MARGIN_LEFT); Size2i sb_ms = sb->get_minimum_size(); Ref<Texture> icon = tabs[i].icon; if (icon.is_valid()) { - icon->draw(ci, Point2i( w, sb->get_margin(MARGIN_TOP)+((sb_rect.size.y-sb_ms.y)-icon->get_height())/2 ) ); - if (text!="") - w+=icon->get_width()+get_constant("hseparation"); - + icon->draw(ci, Point2i(w, sb->get_margin(MARGIN_TOP) + ((sb_rect.size.y - sb_ms.y) - icon->get_height()) / 2)); + if (text != "") + w += icon->get_width() + get_constant("hseparation"); } - font->draw(ci, Point2i( w, sb->get_margin(MARGIN_TOP)+((sb_rect.size.y-sb_ms.y)-font->get_height())/2+font->get_ascent() ), text, col ); + font->draw(ci, Point2i(w, sb->get_margin(MARGIN_TOP) + ((sb_rect.size.y - sb_ms.y) - font->get_height()) / 2 + font->get_ascent()), text, col); - w+=slen; + w += slen; if (tabs[i].right_button.is_valid()) { Ref<StyleBox> style = get_stylebox("button"); - Ref<Texture> rb=tabs[i].right_button; + Ref<Texture> rb = tabs[i].right_button; - w+=get_constant("hseparation"); + w += get_constant("hseparation"); Rect2 rb_rect; - rb_rect.size=style->get_minimum_size()+rb->get_size(); - rb_rect.pos.x=w; - rb_rect.pos.y=sb->get_margin(MARGIN_TOP)+((sb_rect.size.y-sb_ms.y)-(rb_rect.size.y))/2; + rb_rect.size = style->get_minimum_size() + rb->get_size(); + rb_rect.pos.x = w; + rb_rect.pos.y = sb->get_margin(MARGIN_TOP) + ((sb_rect.size.y - sb_ms.y) - (rb_rect.size.y)) / 2; - if (rb_hover==i) { + if (rb_hover == i) { if (rb_pressing) - get_stylebox("button_pressed")->draw(ci,rb_rect); + get_stylebox("button_pressed")->draw(ci, rb_rect); else - style->draw(ci,rb_rect); + style->draw(ci, rb_rect); } - rb->draw(ci,Point2i( w+style->get_margin(MARGIN_LEFT), rb_rect.pos.y+style->get_margin(MARGIN_TOP) )); - w+=rb->get_width(); - tabs[i].rb_rect=rb_rect; - + rb->draw(ci, Point2i(w + style->get_margin(MARGIN_LEFT), rb_rect.pos.y + style->get_margin(MARGIN_TOP))); + w += rb->get_width(); + tabs[i].rb_rect = rb_rect; } - if (cb_displaypolicy==CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy==CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i==current)) { + if (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i == current)) { Ref<StyleBox> style = get_stylebox("button"); - Ref<Texture> cb=close; + Ref<Texture> cb = close; - w+=get_constant("hseparation"); + w += get_constant("hseparation"); Rect2 cb_rect; - cb_rect.size=style->get_minimum_size()+cb->get_size(); - cb_rect.pos.x=w; - cb_rect.pos.y=sb->get_margin(MARGIN_TOP)+((sb_rect.size.y-sb_ms.y)-(cb_rect.size.y))/2; + cb_rect.size = style->get_minimum_size() + cb->get_size(); + cb_rect.pos.x = w; + cb_rect.pos.y = sb->get_margin(MARGIN_TOP) + ((sb_rect.size.y - sb_ms.y) - (cb_rect.size.y)) / 2; if (!tabs[i].disabled && cb_hover == i) { if (cb_pressing) - get_stylebox("button_pressed")->draw(ci,cb_rect); + get_stylebox("button_pressed")->draw(ci, cb_rect); else - style->draw(ci,cb_rect); + style->draw(ci, cb_rect); } - cb->draw(ci,Point2i( w+style->get_margin(MARGIN_LEFT), cb_rect.pos.y+style->get_margin(MARGIN_TOP) )); - w+=cb->get_width(); - tabs[i].cb_rect=cb_rect; + cb->draw(ci, Point2i(w + style->get_margin(MARGIN_LEFT), cb_rect.pos.y + style->get_margin(MARGIN_TOP))); + w += cb->get_width(); + tabs[i].cb_rect = cb_rect; } - w+=sb->get_margin(MARGIN_RIGHT); - - tabs[i].size_cache=w-tabs[i].ofs_cache; + w += sb->get_margin(MARGIN_RIGHT); + tabs[i].size_cache = w - tabs[i].ofs_cache; } - if (offset>0 || missing_right) { + if (offset > 0 || missing_right) { - int vofs = (get_size().height-incr->get_size().height)/2; + int vofs = (get_size().height - incr->get_size().height) / 2; - if (offset>0) - draw_texture(hilite_arrow==0 ? decr_hl : decr, Point2(limit,vofs)); + if (offset > 0) + draw_texture(hilite_arrow == 0 ? decr_hl : decr, Point2(limit, vofs)); else - draw_texture(decr,Point2(limit,vofs), Color(1,1,1,0.5)); + draw_texture(decr, Point2(limit, vofs), Color(1, 1, 1, 0.5)); if (missing_right) - draw_texture(hilite_arrow==1 ? incr_hl : incr, Point2(limit+decr->get_size().width,vofs)); + draw_texture(hilite_arrow == 1 ? incr_hl : incr, Point2(limit + decr->get_size().width, vofs)); else - draw_texture(incr,Point2(limit+decr->get_size().width,vofs), Color(1,1,1,0.5)); + draw_texture(incr, Point2(limit + decr->get_size().width, vofs), Color(1, 1, 1, 0.5)); - buttons_visible=true; + buttons_visible = true; } else { - buttons_visible=false; + buttons_visible = false; } } break; @@ -422,12 +407,11 @@ int Tabs::get_tab_count() const { return tabs.size(); } - void Tabs::set_current_tab(int p_current) { - ERR_FAIL_INDEX( p_current, get_tab_count() ); + ERR_FAIL_INDEX(p_current, get_tab_count()); - current=p_current; + current = p_current; _change_notify("current_tab"); update(); @@ -438,111 +422,98 @@ int Tabs::get_current_tab() const { return current; } +void Tabs::set_tab_title(int p_tab, const String &p_title) { -void Tabs::set_tab_title(int p_tab,const String& p_title) { - - ERR_FAIL_INDEX(p_tab,tabs.size()); - tabs[p_tab].text=p_title; + ERR_FAIL_INDEX(p_tab, tabs.size()); + tabs[p_tab].text = p_title; update(); minimum_size_changed(); - } -String Tabs::get_tab_title(int p_tab) const{ +String Tabs::get_tab_title(int p_tab) const { - ERR_FAIL_INDEX_V(p_tab,tabs.size(),""); + ERR_FAIL_INDEX_V(p_tab, tabs.size(), ""); return tabs[p_tab].text; - } +void Tabs::set_tab_icon(int p_tab, const Ref<Texture> &p_icon) { -void Tabs::set_tab_icon(int p_tab,const Ref<Texture>& p_icon){ - - ERR_FAIL_INDEX(p_tab,tabs.size()); - tabs[p_tab].icon=p_icon; + ERR_FAIL_INDEX(p_tab, tabs.size()); + tabs[p_tab].icon = p_icon; update(); minimum_size_changed(); - } -Ref<Texture> Tabs::get_tab_icon(int p_tab) const{ +Ref<Texture> Tabs::get_tab_icon(int p_tab) const { - ERR_FAIL_INDEX_V(p_tab,tabs.size(),Ref<Texture>()); + ERR_FAIL_INDEX_V(p_tab, tabs.size(), Ref<Texture>()); return tabs[p_tab].icon; - } - void Tabs::set_tab_disabled(int p_tab, bool p_disabled) { - + ERR_FAIL_INDEX(p_tab, tabs.size()); tabs[p_tab].disabled = p_disabled; update(); } bool Tabs::get_tab_disabled(int p_tab) const { - + ERR_FAIL_INDEX_V(p_tab, tabs.size(), false); return tabs[p_tab].disabled; } -void Tabs::set_tab_right_button(int p_tab,const Ref<Texture>& p_right_button){ +void Tabs::set_tab_right_button(int p_tab, const Ref<Texture> &p_right_button) { - ERR_FAIL_INDEX(p_tab,tabs.size()); - tabs[p_tab].right_button=p_right_button; + ERR_FAIL_INDEX(p_tab, tabs.size()); + tabs[p_tab].right_button = p_right_button; update(); minimum_size_changed(); - } -Ref<Texture> Tabs::get_tab_right_button(int p_tab) const{ +Ref<Texture> Tabs::get_tab_right_button(int p_tab) const { - ERR_FAIL_INDEX_V(p_tab,tabs.size(),Ref<Texture>()); + ERR_FAIL_INDEX_V(p_tab, tabs.size(), Ref<Texture>()); return tabs[p_tab].right_button; - } - -void Tabs::add_tab(const String& p_str,const Ref<Texture>& p_icon) { +void Tabs::add_tab(const String &p_str, const Ref<Texture> &p_icon) { Tab t; - t.text=p_str; - t.icon=p_icon; + t.text = p_str; + t.icon = p_icon; t.disabled = false; tabs.push_back(t); update(); minimum_size_changed(); - } void Tabs::clear_tabs() { tabs.clear(); - current=0; + current = 0; update(); } void Tabs::remove_tab(int p_idx) { - ERR_FAIL_INDEX(p_idx,tabs.size()); + ERR_FAIL_INDEX(p_idx, tabs.size()); tabs.remove(p_idx); - if (current>=p_idx) + if (current >= p_idx) current--; update(); minimum_size_changed(); - if (current<0) - current=0; - if (current>=tabs.size()) - current=tabs.size()-1; + if (current < 0) + current = 0; + if (current >= tabs.size()) + current = tabs.size() - 1; _ensure_no_over_offset(); - } - void Tabs::set_tab_align(TabAlign p_align) { - tab_align=p_align; + tab_align = p_align; update(); } @@ -553,49 +524,46 @@ Tabs::TabAlign Tabs::get_tab_align() const { int Tabs::get_tab_width(int p_idx) const { - ERR_FAIL_INDEX_V(p_idx,tabs.size(),0); + ERR_FAIL_INDEX_V(p_idx, tabs.size(), 0); Ref<StyleBox> tab_bg = get_stylebox("tab_bg"); Ref<StyleBox> tab_fg = get_stylebox("tab_fg"); Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled"); Ref<Font> font = get_font("font"); - - int x=0; + int x = 0; Ref<Texture> tex = tabs[p_idx].icon; if (tex.is_valid()) { - x+=tex->get_width(); - if (tabs[p_idx].text!="") - x+=get_constant("hseparation"); - + x += tex->get_width(); + if (tabs[p_idx].text != "") + x += get_constant("hseparation"); } - x+=font->get_string_size(tabs[p_idx].text).width; + x += font->get_string_size(tabs[p_idx].text).width; if (tabs[p_idx].disabled) x += tab_disabled->get_minimum_size().width; - else if (current==p_idx) - x+=tab_fg->get_minimum_size().width; + else if (current == p_idx) + x += tab_fg->get_minimum_size().width; else - x+=tab_bg->get_minimum_size().width; + x += tab_bg->get_minimum_size().width; if (tabs[p_idx].right_button.is_valid()) { - Ref<Texture> rb=tabs[p_idx].right_button; - x+=rb->get_width(); - x+=get_constant("hseparation"); + Ref<Texture> rb = tabs[p_idx].right_button; + x += rb->get_width(); + x += get_constant("hseparation"); } - if (cb_displaypolicy==CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy==CLOSE_BUTTON_SHOW_ACTIVE_ONLY && p_idx==current)) { - Ref<Texture> cb=get_icon("close"); - x+=cb->get_width(); - x+=get_constant("hseparation"); + if (cb_displaypolicy == CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy == CLOSE_BUTTON_SHOW_ACTIVE_ONLY && p_idx == current)) { + Ref<Texture> cb = get_icon("close"); + x += cb->get_width(); + x += get_constant("hseparation"); } return x; } - void Tabs::_ensure_no_over_offset() { if (!is_inside_tree()) @@ -604,17 +572,17 @@ void Tabs::_ensure_no_over_offset() { Ref<Texture> incr = get_icon("increment"); Ref<Texture> decr = get_icon("decrement"); - int limit=get_size().width-incr->get_width()-decr->get_width(); + int limit = get_size().width - incr->get_width() - decr->get_width(); - while(offset>0) { + while (offset > 0) { - int total_w=0; - for(int i=0;i<tabs.size();i++) { + int total_w = 0; + for (int i = 0; i < tabs.size(); i++) { - if (i<offset-1) + if (i < offset - 1) continue; - total_w+=get_tab_width(i); + total_w += get_tab_width(i); } if (total_w < limit) { @@ -624,44 +592,40 @@ void Tabs::_ensure_no_over_offset() { break; } } - } - void Tabs::ensure_tab_visible(int p_idx) { if (!is_inside_tree()) return; - ERR_FAIL_INDEX(p_idx,tabs.size()); + ERR_FAIL_INDEX(p_idx, tabs.size()); _ensure_no_over_offset(); - if (p_idx<=offset) { - offset=p_idx; + if (p_idx <= offset) { + offset = p_idx; update(); return; } Ref<Texture> incr = get_icon("increment"); Ref<Texture> decr = get_icon("decrement"); - int limit=get_size().width-incr->get_width()-decr->get_width(); + int limit = get_size().width - incr->get_width() - decr->get_width(); + int x = 0; + for (int i = 0; i < tabs.size(); i++) { - int x=0; - for(int i=0;i<tabs.size();i++) { - - if (i<offset) + if (i < offset) continue; int sz = get_tab_width(i); - tabs[i].x_cache=x; - tabs[i].x_size_cache=sz; - x+=sz; - + tabs[i].x_cache = x; + tabs[i].x_size_cache = sz; + x += sz; } - while(offset<tabs.size() && ( (tabs[p_idx].x_cache + tabs[p_idx].x_size_cache) - tabs[offset].x_cache) > limit) { + while (offset < tabs.size() && ((tabs[p_idx].x_cache + tabs[p_idx].x_size_cache) - tabs[offset].x_cache) > limit) { offset++; } @@ -669,57 +633,54 @@ void Tabs::ensure_tab_visible(int p_idx) { } void Tabs::set_tab_close_display_policy(CloseButtonDisplayPolicy p_policy) { - cb_displaypolicy=p_policy; + cb_displaypolicy = p_policy; update(); } void Tabs::_bind_methods() { - ClassDB::bind_method(D_METHOD("_gui_input"),&Tabs::_gui_input); - ClassDB::bind_method(D_METHOD("get_tab_count"),&Tabs::get_tab_count); - ClassDB::bind_method(D_METHOD("set_current_tab","tab_idx"),&Tabs::set_current_tab); - ClassDB::bind_method(D_METHOD("get_current_tab"),&Tabs::get_current_tab); - ClassDB::bind_method(D_METHOD("set_tab_title","tab_idx","title"),&Tabs::set_tab_title); - ClassDB::bind_method(D_METHOD("get_tab_title","tab_idx"),&Tabs::get_tab_title); - ClassDB::bind_method(D_METHOD("set_tab_icon","tab_idx","icon:Texture"),&Tabs::set_tab_icon); - ClassDB::bind_method(D_METHOD("get_tab_icon:Texture","tab_idx"),&Tabs::get_tab_icon); + ClassDB::bind_method(D_METHOD("_gui_input"), &Tabs::_gui_input); + ClassDB::bind_method(D_METHOD("get_tab_count"), &Tabs::get_tab_count); + ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &Tabs::set_current_tab); + ClassDB::bind_method(D_METHOD("get_current_tab"), &Tabs::get_current_tab); + ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &Tabs::set_tab_title); + ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &Tabs::get_tab_title); + ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon:Texture"), &Tabs::set_tab_icon); + ClassDB::bind_method(D_METHOD("get_tab_icon:Texture", "tab_idx"), &Tabs::get_tab_icon); ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &Tabs::set_tab_disabled); ClassDB::bind_method(D_METHOD("get_tab_disabled", "tab_idx"), &Tabs::get_tab_disabled); - ClassDB::bind_method(D_METHOD("remove_tab","tab_idx"),&Tabs::remove_tab); - ClassDB::bind_method(D_METHOD("add_tab","title","icon:Texture"),&Tabs::add_tab); - ClassDB::bind_method(D_METHOD("set_tab_align","align"),&Tabs::set_tab_align); - ClassDB::bind_method(D_METHOD("get_tab_align"),&Tabs::get_tab_align); - ClassDB::bind_method(D_METHOD("ensure_tab_visible","idx"),&Tabs::ensure_tab_visible); - - ADD_SIGNAL(MethodInfo("tab_changed",PropertyInfo(Variant::INT,"tab"))); - ADD_SIGNAL(MethodInfo("right_button_pressed",PropertyInfo(Variant::INT,"tab"))); - ADD_SIGNAL(MethodInfo("tab_close",PropertyInfo(Variant::INT,"tab"))); + ClassDB::bind_method(D_METHOD("remove_tab", "tab_idx"), &Tabs::remove_tab); + ClassDB::bind_method(D_METHOD("add_tab", "title", "icon:Texture"), &Tabs::add_tab); + ClassDB::bind_method(D_METHOD("set_tab_align", "align"), &Tabs::set_tab_align); + ClassDB::bind_method(D_METHOD("get_tab_align"), &Tabs::get_tab_align); + ClassDB::bind_method(D_METHOD("ensure_tab_visible", "idx"), &Tabs::ensure_tab_visible); - ADD_PROPERTY( PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE,"-1,4096,1",PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab") ; + ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab"))); + ADD_SIGNAL(MethodInfo("right_button_pressed", PropertyInfo(Variant::INT, "tab"))); + ADD_SIGNAL(MethodInfo("tab_close", PropertyInfo(Variant::INT, "tab"))); - BIND_CONSTANT( ALIGN_LEFT ); - BIND_CONSTANT( ALIGN_CENTER ); - BIND_CONSTANT( ALIGN_RIGHT ); + ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab"); - BIND_CONSTANT( CLOSE_BUTTON_SHOW_ACTIVE_ONLY ); - BIND_CONSTANT( CLOSE_BUTTON_SHOW_ALWAYS ); - BIND_CONSTANT( CLOSE_BUTTON_SHOW_NEVER ); + BIND_CONSTANT(ALIGN_LEFT); + BIND_CONSTANT(ALIGN_CENTER); + BIND_CONSTANT(ALIGN_RIGHT); + BIND_CONSTANT(CLOSE_BUTTON_SHOW_ACTIVE_ONLY); + BIND_CONSTANT(CLOSE_BUTTON_SHOW_ALWAYS); + BIND_CONSTANT(CLOSE_BUTTON_SHOW_NEVER); } - Tabs::Tabs() { - current=0; - tab_align=ALIGN_CENTER; - rb_hover=-1; - rb_pressing=false; - hilite_arrow=-1; + current = 0; + tab_align = ALIGN_CENTER; + rb_hover = -1; + rb_pressing = false; + hilite_arrow = -1; - cb_hover=-1; - cb_pressing=false; + cb_hover = -1; + cb_pressing = false; cb_displaypolicy = CLOSE_BUTTON_SHOW_NEVER; - offset=0; - max_drawn_tab=0; - + offset = 0; + max_drawn_tab = 0; } diff --git a/scene/gui/tabs.h b/scene/gui/tabs.h index 83dcce2613..39811f13c2 100644 --- a/scene/gui/tabs.h +++ b/scene/gui/tabs.h @@ -33,9 +33,9 @@ class Tabs : public Control { - GDCLASS( Tabs, Control ); -public: + GDCLASS(Tabs, Control); +public: enum TabAlign { ALIGN_LEFT, @@ -49,9 +49,8 @@ public: CLOSE_BUTTON_SHOW_ACTIVE_ONLY, CLOSE_BUTTON_SHOW_ALWAYS, }; -private: - +private: struct Tab { String text; @@ -65,10 +64,8 @@ private: Ref<Texture> right_button; Rect2 rb_rect; Rect2 cb_rect; - }; - int offset; int max_drawn_tab; int hilite_arrow; @@ -86,31 +83,29 @@ private: bool cb_pressing; CloseButtonDisplayPolicy cb_displaypolicy; - int hover; // hovered tab + int hover; // hovered tab int get_tab_width(int p_idx) const; void _ensure_no_over_offset(); protected: - - void _gui_input(const InputEvent& p_event); + void _gui_input(const InputEvent &p_event); void _notification(int p_what); static void _bind_methods(); public: + void add_tab(const String &p_str = "", const Ref<Texture> &p_icon = Ref<Texture>()); - void add_tab(const String& p_str="",const Ref<Texture>& p_icon=Ref<Texture>()); - - void set_tab_title(int p_tab,const String& p_title); + void set_tab_title(int p_tab, const String &p_title); String get_tab_title(int p_tab) const; - void set_tab_icon(int p_tab,const Ref<Texture>& p_icon); + void set_tab_icon(int p_tab, const Ref<Texture> &p_icon); Ref<Texture> get_tab_icon(int p_tab) const; - + void set_tab_disabled(int p_tab, bool p_disabled); bool get_tab_disabled(int p_tab) const; - void set_tab_right_button(int p_tab,const Ref<Texture>& p_right_button); + void set_tab_right_button(int p_tab, const Ref<Texture> &p_right_button); Ref<Texture> get_tab_right_button(int p_tab) const; void set_tab_align(TabAlign p_align); diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index f1b061c506..3b7ad910d6 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -28,8 +28,8 @@ /*************************************************************************/ #include "text_edit.h" -#include "os/keyboard.h" #include "os/input.h" +#include "os/keyboard.h" #include "os/os.h" #include "global_config.h" @@ -40,17 +40,17 @@ static bool _is_text_char(CharType c) { - return (c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c=='_'; + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'; } static bool _is_symbol(CharType c) { - return c!='_' && ((c>='!' && c<='/') || (c>=':' && c<='@') || (c>='[' && c<='`') || (c>='{' && c<='~') || c=='\t' || c==' '); + return c != '_' && ((c >= '!' && c <= '/') || (c >= ':' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= '~') || c == '\t' || c == ' '); } static bool _is_char(CharType c) { - return (c>='a' && c<='z') || (c>='A' && c<='Z') || c=='_'; + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'; } static bool _is_number(CharType c) { @@ -62,21 +62,19 @@ static bool _is_hex_symbol(CharType c) { } static bool _is_pair_right_symbol(CharType c) { - return - c == '"' || - c == '\'' || - c == ')' || - c == ']' || - c == '}'; + return c == '"' || + c == '\'' || + c == ')' || + c == ']' || + c == '}'; } static bool _is_pair_left_symbol(CharType c) { - return - c == '"' || - c == '\'' || - c == '(' || - c == '[' || - c == '{'; + return c == '"' || + c == '\'' || + c == '(' || + c == '[' || + c == '{'; } static bool _is_pair_symbol(CharType c) { @@ -84,89 +82,88 @@ static bool _is_pair_symbol(CharType c) { } static CharType _get_right_pair_symbol(CharType c) { - if(c == '"') + if (c == '"') return '"'; - if(c == '\'') + if (c == '\'') return '\''; - if(c == '(') + if (c == '(') return ')'; - if(c == '[') + if (c == '[') return ']'; - if(c == '{') + if (c == '{') return '}'; return 0; } -void TextEdit::Text::set_font(const Ref<Font>& p_font) { +void TextEdit::Text::set_font(const Ref<Font> &p_font) { - font=p_font; + font = p_font; } void TextEdit::Text::set_tab_size(int p_tab_size) { - tab_size=p_tab_size; + tab_size = p_tab_size; } void TextEdit::Text::_update_line_cache(int p_line) const { int w = 0; - int tab_w=font->get_char_size(' ').width*tab_size; + int tab_w = font->get_char_size(' ').width * tab_size; int len = text[p_line].data.length(); const CharType *str = text[p_line].data.c_str(); //update width - for(int i=0;i<len;i++) { - if (str[i]=='\t') { + for (int i = 0; i < len; i++) { + if (str[i] == '\t') { - int left = w%tab_w; - if (left==0) - w+=tab_w; + int left = w % tab_w; + if (left == 0) + w += tab_w; else - w+=tab_w-w%tab_w; // is right... + w += tab_w - w % tab_w; // is right... } else { - w+=font->get_char_size(str[i],str[i+1]).width; + w += font->get_char_size(str[i], str[i + 1]).width; } } - - text[p_line].width_cache=w; + text[p_line].width_cache = w; //update regions text[p_line].region_info.clear(); - for(int i=0;i<len;i++) { + for (int i = 0; i < len; i++) { if (!_is_symbol(str[i])) continue; - if (str[i]=='\\') { + if (str[i] == '\\') { i++; //skip quoted anything continue; } - int left=len-i; + int left = len - i; - for(int j=0;j<color_regions->size();j++) { + for (int j = 0; j < color_regions->size(); j++) { - const ColorRegion& cr=color_regions->operator [](j); + const ColorRegion &cr = color_regions->operator[](j); /* BEGIN */ - int lr=cr.begin_key.length(); - if (lr==0 || lr>left) + int lr = cr.begin_key.length(); + if (lr == 0 || lr > left) continue; - const CharType* kc = cr.begin_key.c_str(); + const CharType *kc = cr.begin_key.c_str(); - bool match=true; + bool match = true; - for(int k=0;k<lr;k++) { - if (kc[k]!=str[i+k]) { - match=false; + for (int k = 0; k < lr; k++) { + if (kc[k] != str[i + k]) { + match = false; break; } } @@ -174,26 +171,26 @@ void TextEdit::Text::_update_line_cache(int p_line) const { if (match) { ColorRegionInfo cri; - cri.end=false; - cri.region=j; - text[p_line].region_info[i]=cri; - i+=lr-1; + cri.end = false; + cri.region = j; + text[p_line].region_info[i] = cri; + i += lr - 1; break; } /* END */ - lr=cr.end_key.length(); - if (lr==0 || lr>left) + lr = cr.end_key.length(); + if (lr == 0 || lr > left) continue; kc = cr.end_key.c_str(); - match=true; + match = true; - for(int k=0;k<lr;k++) { - if (kc[k]!=str[i+k]) { - match=false; + for (int k = 0; k < lr; k++) { + if (kc[k] != str[i + k]) { + match = false; break; } } @@ -201,25 +198,22 @@ void TextEdit::Text::_update_line_cache(int p_line) const { if (match) { ColorRegionInfo cri; - cri.end=true; - cri.region=j; - text[p_line].region_info[i]=cri; - i+=lr-1; + cri.end = true; + cri.region = j; + text[p_line].region_info[i] = cri; + i += lr - 1; break; } - } } - - } -const Map<int,TextEdit::Text::ColorRegionInfo>& TextEdit::Text::get_color_region_info(int p_line) { +const Map<int, TextEdit::Text::ColorRegionInfo> &TextEdit::Text::get_color_region_info(int p_line) { - Map<int,ColorRegionInfo> *cri=NULL; - ERR_FAIL_INDEX_V(p_line,text.size(),*cri); //enjoy your crash + Map<int, ColorRegionInfo> *cri = NULL; + ERR_FAIL_INDEX_V(p_line, text.size(), *cri); //enjoy your crash - if (text[p_line].width_cache==-1) { + if (text[p_line].width_cache == -1) { _update_line_cache(p_line); } @@ -228,9 +222,9 @@ const Map<int,TextEdit::Text::ColorRegionInfo>& TextEdit::Text::get_color_region int TextEdit::Text::get_line_width(int p_line) const { - ERR_FAIL_INDEX_V(p_line,text.size(),-1); + ERR_FAIL_INDEX_V(p_line, text.size(), -1); - if (text[p_line].width_cache==-1) { + if (text[p_line].width_cache == -1) { _update_line_cache(p_line); } @@ -239,16 +233,14 @@ int TextEdit::Text::get_line_width(int p_line) const { void TextEdit::Text::clear_caches() { - for(int i=0;i<text.size();i++) - text[i].width_cache=-1; - + for (int i = 0; i < text.size(); i++) + text[i].width_cache = -1; } void TextEdit::Text::clear() { - text.clear(); - insert(0,""); + insert(0, ""); } int TextEdit::Text::get_max_width() const { @@ -256,29 +248,27 @@ int TextEdit::Text::get_max_width() const { int max = 0; - for(int i=0;i<text.size();i++) - max=MAX(max,get_line_width(i)); + for (int i = 0; i < text.size(); i++) + max = MAX(max, get_line_width(i)); return max; - } -void TextEdit::Text::set(int p_line,const String& p_text) { +void TextEdit::Text::set(int p_line, const String &p_text) { - ERR_FAIL_INDEX(p_line,text.size()); + ERR_FAIL_INDEX(p_line, text.size()); - text[p_line].width_cache=-1; - text[p_line].data=p_text; + text[p_line].width_cache = -1; + text[p_line].data = p_text; } - -void TextEdit::Text::insert(int p_at,const String& p_text) { +void TextEdit::Text::insert(int p_at, const String &p_text) { Line line; - line.marked=false; - line.breakpoint=false; - line.width_cache=-1; - line.data=p_text; - text.insert(p_at,line); + line.marked = false; + line.breakpoint = false; + line.width_cache = -1; + line.data = p_text; + text.insert(p_at, line); } void TextEdit::Text::remove(int p_at) { @@ -287,21 +277,17 @@ void TextEdit::Text::remove(int p_at) { void TextEdit::_update_scrollbars() { - Size2 size = get_size(); Size2 hmin = h_scroll->get_combined_minimum_size(); Size2 vmin = v_scroll->get_combined_minimum_size(); + v_scroll->set_begin(Point2(size.width - vmin.width, cache.style_normal->get_margin(MARGIN_TOP))); + v_scroll->set_end(Point2(size.width, size.height - cache.style_normal->get_margin(MARGIN_TOP) - cache.style_normal->get_margin(MARGIN_BOTTOM))); + h_scroll->set_begin(Point2(0, size.height - hmin.height)); + h_scroll->set_end(Point2(size.width - vmin.width, size.height)); - v_scroll->set_begin( Point2(size.width - vmin.width, cache.style_normal->get_margin(MARGIN_TOP)) ); - v_scroll->set_end( Point2(size.width, size.height - cache.style_normal->get_margin(MARGIN_TOP) - cache.style_normal->get_margin(MARGIN_BOTTOM)) ); - - h_scroll->set_begin( Point2( 0, size.height - hmin.height) ); - h_scroll->set_end( Point2(size.width-vmin.width, size.height) ); - - - int hscroll_rows = ((hmin.height-1)/get_row_height())+1; + int hscroll_rows = ((hmin.height - 1) / get_row_height()) + 1; int visible_rows = get_visible_rows(); int total_rows = text.size(); if (scroll_past_end_of_file_enabled) { @@ -319,28 +305,28 @@ void TextEdit::_update_scrollbars() { total_width += cache.breakpoint_gutter_width; } - bool use_hscroll=true; - bool use_vscroll=true; + bool use_hscroll = true; + bool use_vscroll = true; if (total_rows <= visible_rows && total_width <= visible_width) { //thanks yessopie for this clever bit of logic - use_hscroll=false; - use_vscroll=false; + use_hscroll = false; + use_vscroll = false; } else { if (total_rows > visible_rows && total_width <= visible_width - vscroll_pixels) { //thanks yessopie for this clever bit of logic - use_hscroll=false; + use_hscroll = false; } if (total_rows <= visible_rows - hscroll_rows && total_width > visible_width) { //thanks yessopie for this clever bit of logic - use_vscroll=false; + use_vscroll = false; } } - updating_scrolls=true; + updating_scrolls = true; if (use_vscroll) { @@ -349,7 +335,7 @@ void TextEdit::_update_scrollbars() { v_scroll->set_page(visible_rows); v_scroll->set_value(cursor.line_ofs); - } else { + } else { cursor.line_ofs = 0; v_scroll->hide(); } @@ -366,24 +352,22 @@ void TextEdit::_update_scrollbars() { h_scroll->hide(); } - - - updating_scrolls=false; + updating_scrolls = false; } void TextEdit::_click_selection_held() { - if (Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT) && selection.selecting_mode!=Selection::MODE_NONE) { + if (Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT) && selection.selecting_mode != Selection::MODE_NONE) { - Point2 mp = Input::get_singleton()->get_mouse_pos()-get_global_pos(); + Point2 mp = Input::get_singleton()->get_mouse_pos() - get_global_pos(); - int row,col; - _get_mouse_pos(Point2i(mp.x,mp.y), row,col); + int row, col; + _get_mouse_pos(Point2i(mp.x, mp.y), row, col); - select(selection.selecting_line,selection.selecting_column,row,col); + select(selection.selecting_line, selection.selecting_column, row, col); - cursor_set_line( row ); - cursor_set_column( col ); + cursor_set_line(row); + cursor_set_column(col); update(); click_select_held->start(); @@ -394,25 +378,23 @@ void TextEdit::_click_selection_held() { } } - void TextEdit::_notification(int p_what) { - switch(p_what) { + switch (p_what) { case NOTIFICATION_ENTER_TREE: { _update_caches(); if (cursor_changed_dirty) - MessageQueue::get_singleton()->push_call(this,"_cursor_changed_emit"); + MessageQueue::get_singleton()->push_call(this, "_cursor_changed_emit"); if (text_changed_dirty) - MessageQueue::get_singleton()->push_call(this,"_text_changed_emit"); + MessageQueue::get_singleton()->push_call(this, "_text_changed_emit"); } break; case NOTIFICATION_RESIZED: { - cache.size=get_size(); + cache.size = get_size(); adjust_viewport_to_cursor(); - } break; case NOTIFICATION_THEME_CHANGED: { @@ -441,136 +423,130 @@ void TextEdit::_notification(int p_what) { cache.breakpoint_gutter_width = 0; } - int line_number_char_count=0; + int line_number_char_count = 0; { - int lc=text.size()+1; - cache.line_number_w=0; - while(lc) { - cache.line_number_w+=1; - lc/=10; + int lc = text.size() + 1; + cache.line_number_w = 0; + while (lc) { + cache.line_number_w += 1; + lc /= 10; }; if (line_numbers) { - line_number_char_count=cache.line_number_w; - cache.line_number_w=(cache.line_number_w+1)*cache.font->get_char_size('0').width; + line_number_char_count = cache.line_number_w; + cache.line_number_w = (cache.line_number_w + 1) * cache.font->get_char_size('0').width; } else { - cache.line_number_w=0; + cache.line_number_w = 0; } - - } _update_scrollbars(); - RID ci = get_canvas_item(); - int xmargin_beg=cache.style_normal->get_margin(MARGIN_LEFT)+cache.line_number_w+cache.breakpoint_gutter_width; - int xmargin_end=cache.size.width-cache.style_normal->get_margin(MARGIN_RIGHT); + int xmargin_beg = cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width; + int xmargin_end = cache.size.width - cache.style_normal->get_margin(MARGIN_RIGHT); //let's do it easy for now: - cache.style_normal->draw(ci,Rect2(Point2(),cache.size)); + cache.style_normal->draw(ci, Rect2(Point2(), cache.size)); if (has_focus()) - cache.style_focus->draw(ci,Rect2(Point2(),cache.size)); + cache.style_focus->draw(ci, Rect2(Point2(), cache.size)); - - int ascent=cache.font->get_ascent(); + int ascent = cache.font->get_ascent(); int visible_rows = get_visible_rows(); - int tab_w = cache.font->get_char_size(' ').width*tab_size; + int tab_w = cache.font->get_char_size(' ').width * tab_size; Color color = cache.font_color; - int in_region=-1; + int in_region = -1; if (syntax_coloring) { - if (cache.background_color.a>0.01) { + if (cache.background_color.a > 0.01) { - Point2i ofs = Point2i(cache.style_normal->get_offset())/2.0; - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(ofs, get_size()-cache.style_normal->get_minimum_size()+ofs),cache.background_color); + Point2i ofs = Point2i(cache.style_normal->get_offset()) / 2.0; + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(ofs, get_size() - cache.style_normal->get_minimum_size() + ofs), cache.background_color); } //compute actual region to start (may be inside say, a comment). //slow in very large documments :( but ok for source! - for(int i=0;i<cursor.line_ofs;i++) { + for (int i = 0; i < cursor.line_ofs; i++) { - const Map<int,Text::ColorRegionInfo>& cri_map=text.get_color_region_info(i); + const Map<int, Text::ColorRegionInfo> &cri_map = text.get_color_region_info(i); - if (in_region>=0 && color_regions[in_region].line_only) { - in_region=-1; //reset regions that end at end of line + if (in_region >= 0 && color_regions[in_region].line_only) { + in_region = -1; //reset regions that end at end of line } - for( const Map<int,Text::ColorRegionInfo>::Element* E= cri_map.front();E;E=E->next() ) { + for (const Map<int, Text::ColorRegionInfo>::Element *E = cri_map.front(); E; E = E->next()) { - const Text::ColorRegionInfo &cri=E->get(); + const Text::ColorRegionInfo &cri = E->get(); - if (in_region==-1) { + if (in_region == -1) { if (!cri.end) { - in_region=cri.region; + in_region = cri.region; } - } else if (in_region==cri.region && !color_regions[cri.region].line_only) { //ignore otherwise + } else if (in_region == cri.region && !color_regions[cri.region].line_only) { //ignore otherwise if (cri.end || color_regions[cri.region].eq) { - in_region=-1; + in_region = -1; } } } } } - int brace_open_match_line=-1; - int brace_open_match_column=-1; - bool brace_open_matching=false; - bool brace_open_mismatch=false; - int brace_close_match_line=-1; - int brace_close_match_column=-1; - bool brace_close_matching=false; - bool brace_close_mismatch=false; - + int brace_open_match_line = -1; + int brace_open_match_column = -1; + bool brace_open_matching = false; + bool brace_open_mismatch = false; + int brace_close_match_line = -1; + int brace_close_match_column = -1; + bool brace_close_matching = false; + bool brace_close_mismatch = false; if (brace_matching_enabled) { - if (cursor.column<text[cursor.line].length()) { + if (cursor.column < text[cursor.line].length()) { //check for open CharType c = text[cursor.line][cursor.column]; - CharType closec=0; - - if (c=='[') { - closec=']'; - } else if (c=='{') { - closec='}'; - } else if (c=='(') { - closec=')'; + CharType closec = 0; + + if (c == '[') { + closec = ']'; + } else if (c == '{') { + closec = '}'; + } else if (c == '(') { + closec = ')'; } - if (closec!=0) { + if (closec != 0) { - int stack=1; + int stack = 1; + for (int i = cursor.line; i < text.size(); i++) { - for(int i=cursor.line;i<text.size();i++) { - - int from = i==cursor.line?cursor.column+1:0; - for(int j=from;j<text[i].length();j++) { + int from = i == cursor.line ? cursor.column + 1 : 0; + for (int j = from; j < text[i].length(); j++) { CharType cc = text[i][j]; //ignore any brackets inside a string - if (cc== '"' || cc == '\'') { + if (cc == '"' || cc == '\'') { CharType quotation = cc; do { j++; - if (!(j<text[i].length())) { + if (!(j < text[i].length())) { break; } - cc=text[i][j]; + cc = text[i][j]; //skip over escaped quotation marks inside strings - if (cc=='\\') { + if (cc == '\\') { bool escaped = true; - while (j+1<text[i].length() && text[i][j+1]=='\\') { - escaped=!escaped; + while (j + 1 < text[i].length() && text[i][j + 1] == '\\') { + escaped = !escaped; j++; } if (escaped) { @@ -578,108 +554,98 @@ void TextEdit::_notification(int p_what) { continue; } } - } while (cc!= quotation); - } else if (cc==c) + } while (cc != quotation); + } else if (cc == c) stack++; - else if (cc==closec) + else if (cc == closec) stack--; - if (stack==0) { - brace_open_match_line=i; - brace_open_match_column=j; - brace_open_matching=true; + if (stack == 0) { + brace_open_match_line = i; + brace_open_match_column = j; + brace_open_matching = true; break; } } - if (brace_open_match_line!=-1) + if (brace_open_match_line != -1) break; } if (!brace_open_matching) - brace_open_mismatch=true; - - + brace_open_mismatch = true; } } - if (cursor.column>0) { - CharType c = text[cursor.line][cursor.column-1]; - CharType closec=0; - - + if (cursor.column > 0) { + CharType c = text[cursor.line][cursor.column - 1]; + CharType closec = 0; - if (c==']') { - closec='['; - } else if (c=='}') { - closec='{'; - } else if (c==')') { - closec='('; + if (c == ']') { + closec = '['; + } else if (c == '}') { + closec = '{'; + } else if (c == ')') { + closec = '('; } - if (closec!=0) { + if (closec != 0) { - int stack=1; + int stack = 1; + for (int i = cursor.line; i >= 0; i--) { - for(int i=cursor.line;i>=0;i--) { - - int from = i==cursor.line?cursor.column-2:text[i].length()-1; - for(int j=from;j>=0;j--) { + int from = i == cursor.line ? cursor.column - 2 : text[i].length() - 1; + for (int j = from; j >= 0; j--) { CharType cc = text[i][j]; //ignore any brackets inside a string - if (cc== '"' || cc == '\'') { + if (cc == '"' || cc == '\'') { CharType quotation = cc; do { j--; - if (!(j>=0)) { + if (!(j >= 0)) { break; } - cc=text[i][j]; + cc = text[i][j]; //skip over escaped quotation marks inside strings - if (cc==quotation) { + if (cc == quotation) { bool escaped = false; - while (j-1>=0 && text[i][j-1]=='\\') { - escaped=!escaped; + while (j - 1 >= 0 && text[i][j - 1] == '\\') { + escaped = !escaped; j--; } if (escaped) { j--; - cc='\\'; + cc = '\\'; continue; } } - } while (cc!= quotation); - } else if (cc==c) + } while (cc != quotation); + } else if (cc == c) stack++; - else if (cc==closec) + else if (cc == closec) stack--; - if (stack==0) { - brace_close_match_line=i; - brace_close_match_column=j; - brace_close_matching=true; + if (stack == 0) { + brace_close_match_line = i; + brace_close_match_column = j; + brace_close_matching = true; break; } } - if (brace_close_match_line!=-1) + if (brace_close_match_line != -1) break; } if (!brace_close_matching) - brace_close_mismatch=true; - - + brace_close_mismatch = true; } - - } } - - int deregion=0; //force it to clear inrgion + int deregion = 0; //force it to clear inrgion Point2 cursor_pos; // get the highlighted words @@ -687,22 +653,22 @@ void TextEdit::_notification(int p_what) { String line_num_padding = line_numbers_zero_padded ? "0" : " "; - for (int i=0;i<visible_rows;i++) { + for (int i = 0; i < visible_rows; i++) { - int line=i+cursor.line_ofs; + int line = i + cursor.line_ofs; - if (line<0 || line>=(int)text.size()) + if (line < 0 || line >= (int)text.size()) continue; - const String &str=text[line]; + const String &str = text[line]; - int char_margin=xmargin_beg-cursor.x_ofs; - int char_ofs=0; - int ofs_y=i*get_row_height()+cache.line_spacing/2; - bool prev_is_char=false; + int char_margin = xmargin_beg - cursor.x_ofs; + int char_ofs = 0; + int ofs_y = i * get_row_height() + cache.line_spacing / 2; + bool prev_is_char = false; bool prev_is_number = false; - bool in_keyword=false; - bool underlined=false; + bool in_keyword = false; + bool underlined = false; bool in_word = false; bool in_function_name = false; bool in_member_variable = false; @@ -717,23 +683,22 @@ void TextEdit::_notification(int p_what) { search_text_col = _get_column_pos_of_word(search_text, str, search_flags, 0); if (highlighted_text.length() != 0 && highlighted_text != search_text) - highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE|SEARCH_WHOLE_WORDS, 0); - - const Map<int,Text::ColorRegionInfo>& cri_map=text.get_color_region_info(line); + highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0); + const Map<int, Text::ColorRegionInfo> &cri_map = text.get_color_region_info(line); if (text.is_marked(line)) { - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(xmargin_beg, ofs_y,xmargin_end-xmargin_beg,get_row_height()),cache.mark_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg, ofs_y, xmargin_end - xmargin_beg, get_row_height()), cache.mark_color); } if (text.is_breakpoint(line)) { - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(xmargin_beg, ofs_y,xmargin_end-xmargin_beg,get_row_height()),cache.breakpoint_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg, ofs_y, xmargin_end - xmargin_beg, get_row_height()), cache.breakpoint_color); } - if (line==cursor.line) { - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(0, ofs_y,xmargin_end,get_row_height()),cache.current_line_color); + if (line == cursor.line) { + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(0, ofs_y, xmargin_end, get_row_height()), cache.current_line_color); } // draw breakpoint marker @@ -744,39 +709,37 @@ void TextEdit::_notification(int p_what) { int marker_height = get_row_height() - (vertical_gap * 2); int marker_width = cache.breakpoint_gutter_width - (horizontal_gap * 2); // no transparency on marker - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cache.style_normal->get_margin(MARGIN_LEFT) + horizontal_gap - 2, ofs_y + vertical_gap ,marker_width, marker_height),Color(cache.breakpoint_color.r, cache.breakpoint_color.g, cache.breakpoint_color.b)); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cache.style_normal->get_margin(MARGIN_LEFT) + horizontal_gap - 2, ofs_y + vertical_gap, marker_width, marker_height), Color(cache.breakpoint_color.r, cache.breakpoint_color.g, cache.breakpoint_color.b)); } } - if (cache.line_number_w) { - String fc = String::num(line+1); + String fc = String::num(line + 1); while (fc.length() < line_number_char_count) { - fc=line_num_padding+fc; + fc = line_num_padding + fc; } - cache.font->draw(ci,Point2(cache.style_normal->get_margin(MARGIN_LEFT)+cache.breakpoint_gutter_width,ofs_y+cache.font->get_ascent()),fc,cache.line_number_color); + cache.font->draw(ci, Point2(cache.style_normal->get_margin(MARGIN_LEFT) + cache.breakpoint_gutter_width, ofs_y + cache.font->get_ascent()), fc, cache.line_number_color); } - for (int j=0;j<str.length();j++) { + for (int j = 0; j < str.length(); j++) { //look for keyword - if (deregion>0) { + if (deregion > 0) { deregion--; - if (deregion==0) - in_region=-1; + if (deregion == 0) + in_region = -1; } - if (syntax_coloring && deregion==0) { - + if (syntax_coloring && deregion == 0) { color = cache.font_color; //reset //find keyword - bool is_char = _is_text_char(str[j]); + bool is_char = _is_text_char(str[j]); bool is_symbol = _is_symbol(str[j]); bool is_number = _is_number(str[j]); - if (j==0 && in_region>=0 && color_regions[in_region].line_only) { - in_region=-1; //reset regions that end at end of line + if (j == 0 && in_region >= 0 && color_regions[in_region].line_only) { + in_region = -1; //reset regions that end at end of line } // allow ABCDEF in hex notation @@ -787,11 +750,11 @@ void TextEdit::_notification(int p_what) { } // check for dot or 'x' for hex notation in floating point number - if ((str[j] == '.' || str[j] == 'x') && !in_word && prev_is_number && !is_number) { + if ((str[j] == '.' || str[j] == 'x') && !in_word && prev_is_number && !is_number) { is_number = true; is_symbol = false; - if (str[j] == 'x' && str[j-1] == '0') { + if (str[j] == 'x' && str[j - 1] == '0') { is_hex_notation = true; } } @@ -810,49 +773,48 @@ void TextEdit::_notification(int p_what) { if (is_symbol && cri_map.has(j)) { + const Text::ColorRegionInfo &cri = cri_map[j]; - const Text::ColorRegionInfo &cri=cri_map[j]; - - if (in_region==-1) { + if (in_region == -1) { if (!cri.end) { - in_region=cri.region; + in_region = cri.region; } - } else if (in_region==cri.region && !color_regions[cri.region].line_only) { //ignore otherwise + } else if (in_region == cri.region && !color_regions[cri.region].line_only) { //ignore otherwise if (cri.end || color_regions[cri.region].eq) { - deregion=color_regions[cri.region].eq?color_regions[cri.region].begin_key.length():color_regions[cri.region].end_key.length(); + deregion = color_regions[cri.region].eq ? color_regions[cri.region].begin_key.length() : color_regions[cri.region].end_key.length(); } } } if (!is_char) { - in_keyword=false; - underlined=false; + in_keyword = false; + underlined = false; } - if (in_region==-1 && !in_keyword && is_char && !prev_is_char) { + if (in_region == -1 && !in_keyword && is_char && !prev_is_char) { - int to=j; - while(to<str.length() && _is_text_char(str[to])) + int to = j; + while (to < str.length() && _is_text_char(str[to])) to++; - uint32_t hash = String::hash(&str[j],to-j); - StrRange range(&str[j],to-j); + uint32_t hash = String::hash(&str[j], to - j); + StrRange range(&str[j], to - j); - const Color *col=keywords.custom_getptr(range,hash); + const Color *col = keywords.custom_getptr(range, hash); if (col) { - in_keyword=true; - keyword_color=*col; + in_keyword = true; + keyword_color = *col; } - if (select_identifiers_enabled && hilighted_word!=String()) { - if (hilighted_word==range) { - underlined=true; + if (select_identifiers_enabled && hilighted_word != String()) { + if (hilighted_word == range) { + underlined = true; } } } @@ -860,7 +822,7 @@ void TextEdit::_notification(int p_what) { if (!in_function_name && in_word && !in_keyword) { int k = j; - while(k < str.length() && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') { + while (k < str.length() && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') { k++; } @@ -876,7 +838,7 @@ void TextEdit::_notification(int p_what) { if (!in_function_name && !in_member_variable && !in_keyword && !is_number && in_word) { int k = j; - while(k > 0 && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') { + while (k > 0 && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') { k--; } @@ -890,169 +852,164 @@ void TextEdit::_notification(int p_what) { in_member_variable = false; } - if (in_region>=0) - color=color_regions[in_region].color; + if (in_region >= 0) + color = color_regions[in_region].color; else if (in_keyword) - color=keyword_color; + color = keyword_color; else if (in_member_variable) - color=cache.member_variable_color; + color = cache.member_variable_color; else if (in_function_name) - color=cache.function_color; + color = cache.function_color; else if (is_symbol) - color=cache.symbol_color; + color = cache.symbol_color; else if (is_number) - color=cache.number_color; - - prev_is_char=is_char; - prev_is_number=is_number; + color = cache.number_color; + prev_is_char = is_char; + prev_is_number = is_number; } int char_w; //handle tabulator - - if (str[j]=='\t') { - int left = char_ofs%tab_w; - if (left==0) - char_w=tab_w; + if (str[j] == '\t') { + int left = char_ofs % tab_w; + if (left == 0) + char_w = tab_w; else - char_w=tab_w-char_ofs%tab_w; // is right... + char_w = tab_w - char_ofs % tab_w; // is right... } else { - char_w=cache.font->get_char_size(str[j],str[j+1]).width; + char_w = cache.font->get_char_size(str[j], str[j + 1]).width; } - if ( (char_ofs+char_margin)<xmargin_beg) { - char_ofs+=char_w; + if ((char_ofs + char_margin) < xmargin_beg) { + char_ofs += char_w; continue; } - if ( (char_ofs+char_margin+char_w)>=xmargin_end) { + if ((char_ofs + char_margin + char_w) >= xmargin_end) { if (syntax_coloring) continue; else break; } - bool in_search_result=false; + bool in_search_result = false; if (search_text_col != -1) { // if we are at the end check for new search result on same line - if (j >= search_text_col+search_text.length()) + if (j >= search_text_col + search_text.length()) search_text_col = _get_column_pos_of_word(search_text, str, search_flags, j); - in_search_result = j >= search_text_col && j < search_text_col+search_text.length(); + in_search_result = j >= search_text_col && j < search_text_col + search_text.length(); if (in_search_result) { - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin, ofs_y ), Size2i(char_w, get_row_height())),cache.search_result_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin, ofs_y), Size2i(char_w, get_row_height())), cache.search_result_color); } } - bool in_selection = (selection.active && line>=selection.from_line && line<=selection.to_line && (line>selection.from_line || j>=selection.from_column) && (line<selection.to_line || j<selection.to_column)); + bool in_selection = (selection.active && line >= selection.from_line && line <= selection.to_line && (line > selection.from_line || j >= selection.from_column) && (line < selection.to_line || j < selection.to_column)); if (in_selection) { //inside selection! - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin, ofs_y ), Size2i(char_w,get_row_height())),cache.selection_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin, ofs_y), Size2i(char_w, get_row_height())), cache.selection_color); } if (in_search_result) { - Color border_color=(line==search_result_line && j>=search_result_col && j<search_result_col+search_text.length())?cache.font_color:cache.search_result_border_color; + Color border_color = (line == search_result_line && j >= search_result_col && j < search_result_col + search_text.length()) ? cache.font_color : cache.search_result_border_color; - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin, ofs_y ), Size2i(char_w,1)),border_color); - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin, ofs_y+get_row_height()-1 ), Size2i(char_w,1)),border_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin, ofs_y), Size2i(char_w, 1)), border_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin, ofs_y + get_row_height() - 1), Size2i(char_w, 1)), border_color); - if (j==search_text_col) - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin, ofs_y ), Size2i(1,get_row_height())),border_color); - if (j==search_text_col+search_text.length()-1) - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin+char_w-1, ofs_y ), Size2i(1,get_row_height())),border_color); + if (j == search_text_col) + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin, ofs_y), Size2i(1, get_row_height())), border_color); + if (j == search_text_col + search_text.length() - 1) + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin + char_w - 1, ofs_y), Size2i(1, get_row_height())), border_color); } if (highlight_all_occurrences) { if (highlighted_text_col != -1) { // if we are at the end check for new word on same line - if (j > highlighted_text_col+highlighted_text.length()) { - highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE|SEARCH_WHOLE_WORDS, j); + if (j > highlighted_text_col + highlighted_text.length()) { + highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, j); } - bool in_highlighted_word = (j >= highlighted_text_col && j < highlighted_text_col+highlighted_text.length()); + bool in_highlighted_word = (j >= highlighted_text_col && j < highlighted_text_col + highlighted_text.length()); /* if this is the original highlighted text we don't want to highlight it again */ - if (cursor.line==line && (cursor.column >= highlighted_text_col && cursor.column <= highlighted_text_col+highlighted_text.length())) { + if (cursor.line == line && (cursor.column >= highlighted_text_col && cursor.column <= highlighted_text_col + highlighted_text.length())) { in_highlighted_word = false; } if (in_highlighted_word) { - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2i( char_ofs+char_margin, ofs_y ), Size2i(char_w, get_row_height())),cache.word_highlighted_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin, ofs_y), Size2i(char_w, get_row_height())), cache.word_highlighted_color); } } } if (brace_matching_enabled) { - if ( (brace_open_match_line==line && brace_open_match_column==j) || - (cursor.column==j && cursor.line==line && (brace_open_matching||brace_open_mismatch))) { + if ((brace_open_match_line == line && brace_open_match_column == j) || + (cursor.column == j && cursor.line == line && (brace_open_matching || brace_open_mismatch))) { if (brace_open_mismatch) - color=cache.brace_mismatch_color; - cache.font->draw_char(ci,Point2i( char_ofs+char_margin, ofs_y+ascent),'_',str[j+1],in_selection?cache.font_selected_color:color); - + color = cache.brace_mismatch_color; + cache.font->draw_char(ci, Point2i(char_ofs + char_margin, ofs_y + ascent), '_', str[j + 1], in_selection ? cache.font_selected_color : color); } if ( - (brace_close_match_line==line && brace_close_match_column==j) || - (cursor.column==j+1 && cursor.line==line && (brace_close_matching||brace_close_mismatch))) { - + (brace_close_match_line == line && brace_close_match_column == j) || + (cursor.column == j + 1 && cursor.line == line && (brace_close_matching || brace_close_mismatch))) { if (brace_close_mismatch) - color=cache.brace_mismatch_color; - cache.font->draw_char(ci,Point2i( char_ofs+char_margin, ofs_y+ascent),'_',str[j+1],in_selection?cache.font_selected_color:color); - + color = cache.brace_mismatch_color; + cache.font->draw_char(ci, Point2i(char_ofs + char_margin, ofs_y + ascent), '_', str[j + 1], in_selection ? cache.font_selected_color : color); } } - if (cursor.column==j && cursor.line==line) { + if (cursor.column == j && cursor.line == line) { - cursor_pos = Point2i( char_ofs+char_margin, ofs_y ); + cursor_pos = Point2i(char_ofs + char_margin, ofs_y); if (insert_mode) { cursor_pos.y += (get_row_height() - 3); } - int caret_w = (str[j]=='\t') ? cache.font->get_char_size(' ').width : char_w; + int caret_w = (str[j] == '\t') ? cache.font->get_char_size(' ').width : char_w; if (draw_caret) { if (insert_mode) { int caret_h = (block_caret) ? 4 : 1; - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(caret_w,caret_h)),cache.caret_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cursor_pos, Size2i(caret_w, caret_h)), cache.caret_color); } else { caret_w = (block_caret) ? caret_w : 1; - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(caret_w,get_row_height())),cache.caret_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cursor_pos, Size2i(caret_w, get_row_height())), cache.caret_color); } } } - if (cursor.column==j && cursor.line==line && block_caret && draw_caret && !insert_mode) { + if (cursor.column == j && cursor.line == line && block_caret && draw_caret && !insert_mode) { color = cache.caret_background_color; } - if (str[j]>=32) { - int w = cache.font->draw_char(ci,Point2i( char_ofs+char_margin, ofs_y+ascent),str[j],str[j+1],in_selection?cache.font_selected_color:color); + if (str[j] >= 32) { + int w = cache.font->draw_char(ci, Point2i(char_ofs + char_margin, ofs_y + ascent), str[j], str[j + 1], in_selection ? cache.font_selected_color : color); if (underlined) { - draw_rect(Rect2( char_ofs+char_margin, ofs_y+ascent+2,w,1),in_selection?cache.font_selected_color:color); + draw_rect(Rect2(char_ofs + char_margin, ofs_y + ascent + 2, w, 1), in_selection ? cache.font_selected_color : color); } } - else if (draw_tabs && str[j]=='\t') { - int yofs= (get_row_height() - cache.tab_icon->get_height())/2; - cache.tab_icon->draw(ci, Point2(char_ofs+char_margin,ofs_y+yofs),in_selection?cache.font_selected_color:color); + else if (draw_tabs && str[j] == '\t') { + int yofs = (get_row_height() - cache.tab_icon->get_height()) / 2; + cache.tab_icon->draw(ci, Point2(char_ofs + char_margin, ofs_y + yofs), in_selection ? cache.font_selected_color : color); } - char_ofs+=char_w; + char_ofs += char_w; } - if (cursor.column==str.length() && cursor.line==line && (char_ofs+char_margin)>=xmargin_beg) { + if (cursor.column == str.length() && cursor.line == line && (char_ofs + char_margin) >= xmargin_beg) { - cursor_pos=Point2i( char_ofs+char_margin, ofs_y ); + cursor_pos = Point2i(char_ofs + char_margin, ofs_y); if (insert_mode) { cursor_pos.y += (get_row_height() - 3); @@ -1062,191 +1019,172 @@ void TextEdit::_notification(int p_what) { if (insert_mode) { int char_w = cache.font->get_char_size(' ').width; int caret_h = (block_caret) ? 4 : 1; - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(char_w,caret_h)),cache.caret_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cursor_pos, Size2i(char_w, caret_h)), cache.caret_color); } else { int char_w = cache.font->get_char_size(' ').width; int caret_w = (block_caret) ? char_w : 1; - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor_pos, Size2i(caret_w,get_row_height())),cache.caret_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cursor_pos, Size2i(caret_w, get_row_height())), cache.caret_color); } } } } if (line_length_guideline) { - int x=xmargin_beg+cache.font->get_char_size('0').width*line_length_guideline_col-cursor.x_ofs; - if (x>xmargin_beg && x<xmargin_end) { - VisualServer::get_singleton()->canvas_item_add_line(ci,Point2(x,0),Point2(x,cache.size.height),cache.line_length_guideline_color); + int x = xmargin_beg + cache.font->get_char_size('0').width * line_length_guideline_col - cursor.x_ofs; + if (x > xmargin_beg && x < xmargin_end) { + VisualServer::get_singleton()->canvas_item_add_line(ci, Point2(x, 0), Point2(x, cache.size.height), cache.line_length_guideline_color); } } - bool completion_below = false; if (completion_active) { // code completion box Ref<StyleBox> csb = get_stylebox("completion"); int maxlines = get_constant("completion_lines"); - int cmax_width = get_constant("completion_max_width")*cache.font->get_char_size('x').x; + int cmax_width = get_constant("completion_max_width") * cache.font->get_char_size('x').x; int scrollw = get_constant("completion_scroll_width"); Color scrollc = get_color("completion_scroll_color"); - - - int lines = MIN(completion_options.size(),maxlines); - int w=0; - int h=lines*get_row_height(); + int lines = MIN(completion_options.size(), maxlines); + int w = 0; + int h = lines * get_row_height(); int nofs = cache.font->get_string_size(completion_base).width; - if (completion_options.size() < 50) { - for(int i=0;i<completion_options.size();i++) { - int w2=MIN(cache.font->get_string_size(completion_options[i]).x,cmax_width); - if (w2>w) - w=w2; + for (int i = 0; i < completion_options.size(); i++) { + int w2 = MIN(cache.font->get_string_size(completion_options[i]).x, cmax_width); + if (w2 > w) + w = w2; } } else { - w=cmax_width; + w = cmax_width; } int th = h + csb->get_minimum_size().y; - if (cursor_pos.y+get_row_height()+th > get_size().height) { - completion_rect.pos.y=cursor_pos.y-th; + if (cursor_pos.y + get_row_height() + th > get_size().height) { + completion_rect.pos.y = cursor_pos.y - th; } else { - completion_rect.pos.y=cursor_pos.y+get_row_height()+csb->get_offset().y; + completion_rect.pos.y = cursor_pos.y + get_row_height() + csb->get_offset().y; completion_below = true; } - if (cursor_pos.x-nofs+w+scrollw > get_size().width) { - completion_rect.pos.x=get_size().width-w-scrollw; + if (cursor_pos.x - nofs + w + scrollw > get_size().width) { + completion_rect.pos.x = get_size().width - w - scrollw; } else { - completion_rect.pos.x=cursor_pos.x-nofs; + completion_rect.pos.x = cursor_pos.x - nofs; } - completion_rect.size.width=w+2; - completion_rect.size.height=h; - if (completion_options.size()<=maxlines) - scrollw=0; + completion_rect.size.width = w + 2; + completion_rect.size.height = h; + if (completion_options.size() <= maxlines) + scrollw = 0; - draw_style_box(csb,Rect2(completion_rect.pos-csb->get_offset(),completion_rect.size+csb->get_minimum_size()+Size2(scrollw,0))); + draw_style_box(csb, Rect2(completion_rect.pos - csb->get_offset(), completion_rect.size + csb->get_minimum_size() + Size2(scrollw, 0))); - if (cache.completion_background_color.a>0.01) { - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(completion_rect.pos,completion_rect.size+Size2(scrollw,0)),cache.completion_background_color); + if (cache.completion_background_color.a > 0.01) { + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(completion_rect.pos, completion_rect.size + Size2(scrollw, 0)), cache.completion_background_color); } - int line_from = CLAMP(completion_index - lines/2, 0, completion_options.size() - lines); - VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(Point2(completion_rect.pos.x,completion_rect.pos.y+(completion_index-line_from)*get_row_height()),Size2(completion_rect.size.width,get_row_height())),cache.completion_selected_color); - draw_rect(Rect2(completion_rect.pos,Size2(nofs,completion_rect.size.height)),cache.completion_existing_color); - - + int line_from = CLAMP(completion_index - lines / 2, 0, completion_options.size() - lines); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(completion_rect.pos.x, completion_rect.pos.y + (completion_index - line_from) * get_row_height()), Size2(completion_rect.size.width, get_row_height())), cache.completion_selected_color); + draw_rect(Rect2(completion_rect.pos, Size2(nofs, completion_rect.size.height)), cache.completion_existing_color); - - for(int i=0;i<lines;i++) { + for (int i = 0; i < lines; i++) { int l = line_from + i; - ERR_CONTINUE( l < 0 || l>= completion_options.size()); + ERR_CONTINUE(l < 0 || l >= completion_options.size()); Color text_color = cache.completion_font_color; - for(int j=0;j<color_regions.size();j++) { + for (int j = 0; j < color_regions.size(); j++) { if (completion_options[l].begins_with(color_regions[j].begin_key)) { - text_color=color_regions[j].color; + text_color = color_regions[j].color; } } - draw_string(cache.font,Point2(completion_rect.pos.x,completion_rect.pos.y+i*get_row_height()+cache.font->get_ascent()),completion_options[l],text_color,completion_rect.size.width); + draw_string(cache.font, Point2(completion_rect.pos.x, completion_rect.pos.y + i * get_row_height() + cache.font->get_ascent()), completion_options[l], text_color, completion_rect.size.width); } if (scrollw) { //draw a small scroll rectangle to show a position in the options float r = maxlines / (float)completion_options.size(); float o = line_from / (float)completion_options.size(); - draw_rect(Rect2(completion_rect.pos.x+completion_rect.size.width,completion_rect.pos.y+o*completion_rect.size.y,scrollw,completion_rect.size.y*r),scrollc); + draw_rect(Rect2(completion_rect.pos.x + completion_rect.size.width, completion_rect.pos.y + o * completion_rect.size.y, scrollw, completion_rect.size.y * r), scrollc); } - completion_line_ofs=line_from; - + completion_line_ofs = line_from; } // check to see if the hint should be drawn bool show_hint = false; - if (completion_hint!="") { + if (completion_hint != "") { if (completion_active) { if (completion_below && !callhint_below) { show_hint = true; - } - else if (!completion_below && callhint_below) { + } else if (!completion_below && callhint_below) { show_hint = true; } - } - else { + } else { show_hint = true; } } if (show_hint) { - Ref<StyleBox> sb = get_stylebox("panel","TooltipPanel"); + Ref<StyleBox> sb = get_stylebox("panel", "TooltipPanel"); Ref<Font> font = cache.font; - Color font_color = get_color("font_color","TooltipLabel"); - + Color font_color = get_color("font_color", "TooltipLabel"); - int max_w=0; + int max_w = 0; int sc = completion_hint.get_slice_count("\n"); - int offset=0; - int spacing=0; - for(int i=0;i<sc;i++) { - - String l = completion_hint.get_slice("\n",i); - int len = font->get_string_size(l).x; - max_w = MAX(len,max_w); - if (i==0) { - offset = font->get_string_size(l.substr(0,l.find(String::chr(0xFFFF)))).x; + int offset = 0; + int spacing = 0; + for (int i = 0; i < sc; i++) { + + String l = completion_hint.get_slice("\n", i); + int len = font->get_string_size(l).x; + max_w = MAX(len, max_w); + if (i == 0) { + offset = font->get_string_size(l.substr(0, l.find(String::chr(0xFFFF)))).x; } else { - spacing+=cache.line_spacing; + spacing += cache.line_spacing; } - - } + Size2 size = Size2(max_w, sc * font->get_height() + spacing); + Size2 minsize = size + sb->get_minimum_size(); - - Size2 size = Size2(max_w,sc*font->get_height()+spacing); - Size2 minsize = size+sb->get_minimum_size(); - - - if (completion_hint_offset==-0xFFFF) { - completion_hint_offset=cursor_pos.x-offset; + if (completion_hint_offset == -0xFFFF) { + completion_hint_offset = cursor_pos.x - offset; } - - Point2 hint_ofs = Vector2(completion_hint_offset,cursor_pos.y) + callhint_offset; + Point2 hint_ofs = Vector2(completion_hint_offset, cursor_pos.y) + callhint_offset; if (callhint_below) { hint_ofs.y += get_row_height() + sb->get_offset().y; - } - else { + } else { hint_ofs.y -= minsize.y + sb->get_offset().y; } - draw_style_box(sb,Rect2(hint_ofs,minsize)); + draw_style_box(sb, Rect2(hint_ofs, minsize)); - spacing=0; - for(int i=0;i<sc;i++) { - int begin=0; - int end=0; - String l = completion_hint.get_slice("\n",i); + spacing = 0; + for (int i = 0; i < sc; i++) { + int begin = 0; + int end = 0; + String l = completion_hint.get_slice("\n", i); - if (l.find(String::chr(0xFFFF))!=-1) { - begin = font->get_string_size(l.substr(0,l.find(String::chr(0xFFFF)))).x; - end = font->get_string_size(l.substr(0,l.rfind(String::chr(0xFFFF)))).x; + if (l.find(String::chr(0xFFFF)) != -1) { + begin = font->get_string_size(l.substr(0, l.find(String::chr(0xFFFF)))).x; + end = font->get_string_size(l.substr(0, l.rfind(String::chr(0xFFFF)))).x; } - draw_string(font,hint_ofs+sb->get_offset()+Vector2(0,font->get_ascent()+font->get_height()*i+spacing),l.replace(String::chr(0xFFFF),""),font_color); - if (end>0) { - Vector2 b = hint_ofs+sb->get_offset()+Vector2(begin,font->get_height()+font->get_height()*i+spacing-1); - draw_line(b,b+Vector2(end-begin,0),font_color); + draw_string(font, hint_ofs + sb->get_offset() + Vector2(0, font->get_ascent() + font->get_height() * i + spacing), l.replace(String::chr(0xFFFF), ""), font_color); + if (end > 0) { + Vector2 b = hint_ofs + sb->get_offset() + Vector2(begin, font->get_height() + font->get_height() * i + spacing - 1); + draw_line(b, b + Vector2(end - begin, 0), font_color); } - spacing+=cache.line_spacing; + spacing += cache.line_spacing; } } - } break; case NOTIFICATION_FOCUS_ENTER: { @@ -1254,7 +1192,7 @@ void TextEdit::_notification(int p_what) { draw_caret = true; } if (OS::get_singleton()->has_virtual_keyboard()) - OS::get_singleton()->show_virtual_keyboard(get_text(),get_global_rect()); + OS::get_singleton()->show_virtual_keyboard(get_text(), get_global_rect()); if (raised_from_completion) { VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 1); } @@ -1275,27 +1213,27 @@ void TextEdit::_consume_pair_symbol(CharType ch) { int cursor_position_to_move = cursor_get_column() + 1; - CharType ch_single[2] = {ch, 0}; - CharType ch_single_pair[2] = {_get_right_pair_symbol(ch), 0}; - CharType ch_pair[3] = {ch, _get_right_pair_symbol(ch), 0}; + CharType ch_single[2] = { ch, 0 }; + CharType ch_single_pair[2] = { _get_right_pair_symbol(ch), 0 }; + CharType ch_pair[3] = { ch, _get_right_pair_symbol(ch), 0 }; - if(is_selection_active()) { + if (is_selection_active()) { - int new_column,new_line; + int new_column, new_line; begin_complex_operation(); _insert_text(get_selection_from_line(), get_selection_from_column(), - ch_single, - &new_line, &new_column); + ch_single, + &new_line, &new_column); int to_col_offset = 0; - if(get_selection_from_line() == get_selection_to_line()) + if (get_selection_from_line() == get_selection_to_line()) to_col_offset = 1; _insert_text(get_selection_to_line(), - get_selection_to_column() + to_col_offset, - ch_single_pair, - &new_line,&new_column); + get_selection_to_column() + to_col_offset, + ch_single_pair, + &new_line, &new_column); end_complex_operation(); cursor_set_line(get_selection_to_line()); @@ -1306,78 +1244,71 @@ void TextEdit::_consume_pair_symbol(CharType ch) { return; } - if( (ch == '\'' || ch == '"') && + if ((ch == '\'' || ch == '"') && cursor_get_column() > 0 && - _is_text_char(text[cursor.line][cursor_get_column() - 1]) - ) { + _is_text_char(text[cursor.line][cursor_get_column() - 1])) { insert_text_at_cursor(ch_single); cursor_set_column(cursor_position_to_move); return; } - if(cursor_get_column() < text[cursor.line].length()) { - if(_is_text_char(text[cursor.line][cursor_get_column()])) { + if (cursor_get_column() < text[cursor.line].length()) { + if (_is_text_char(text[cursor.line][cursor_get_column()])) { insert_text_at_cursor(ch_single); cursor_set_column(cursor_position_to_move); return; } - if( _is_pair_right_symbol(ch) && - text[cursor.line][cursor_get_column()] == ch - ) { + if (_is_pair_right_symbol(ch) && + text[cursor.line][cursor_get_column()] == ch) { cursor_set_column(cursor_position_to_move); return; } } - insert_text_at_cursor(ch_pair); cursor_set_column(cursor_position_to_move); return; - } void TextEdit::_consume_backspace_for_pair_symbol(int prev_line, int prev_column) { bool remove_right_symbol = false; - if(cursor.column < text[cursor.line].length() && cursor.column > 0) { + if (cursor.column < text[cursor.line].length() && cursor.column > 0) { CharType left_char = text[cursor.line][cursor.column - 1]; CharType right_char = text[cursor.line][cursor.column]; - if(right_char == _get_right_pair_symbol(left_char)) { + if (right_char == _get_right_pair_symbol(left_char)) { remove_right_symbol = true; } - } - if(remove_right_symbol) { - _remove_text(prev_line,prev_column,cursor.line,cursor.column + 1); + if (remove_right_symbol) { + _remove_text(prev_line, prev_column, cursor.line, cursor.column + 1); } else { - _remove_text(prev_line,prev_column,cursor.line,cursor.column); + _remove_text(prev_line, prev_column, cursor.line, cursor.column); } - } void TextEdit::backspace_at_cursor() { if (readonly) return; - if (cursor.column==0 && cursor.line==0) + if (cursor.column == 0 && cursor.line == 0) return; - int prev_line = cursor.column?cursor.line:cursor.line-1; - int prev_column = cursor.column?(cursor.column-1):(text[cursor.line-1].length()); - if(auto_brace_completion_enabled && + int prev_line = cursor.column ? cursor.line : cursor.line - 1; + int prev_column = cursor.column ? (cursor.column - 1) : (text[cursor.line - 1].length()); + if (auto_brace_completion_enabled && cursor.column > 0 && _is_pair_left_symbol(text[cursor.line][cursor.column - 1])) { _consume_backspace_for_pair_symbol(prev_line, prev_column); } else { - _remove_text(prev_line,prev_column,cursor.line,cursor.column); + _remove_text(prev_line, prev_column, cursor.line, cursor.column); } cursor_set_line(prev_line); cursor_set_column(prev_column); - } void TextEdit::indent_selection_right() { @@ -1441,68 +1372,67 @@ void TextEdit::indent_selection_left() { update(); } -void TextEdit::_get_mouse_pos(const Point2i& p_mouse, int &r_row, int &r_col) const { +void TextEdit::_get_mouse_pos(const Point2i &p_mouse, int &r_row, int &r_col) const { - float rows=p_mouse.y; - rows-=cache.style_normal->get_margin(MARGIN_TOP); - rows/=get_row_height(); - int row=cursor.line_ofs+rows; + float rows = p_mouse.y; + rows -= cache.style_normal->get_margin(MARGIN_TOP); + rows /= get_row_height(); + int row = cursor.line_ofs + rows; - if (row<0) - row=0; + if (row < 0) + row = 0; - int col=0; + int col = 0; - if (row>=text.size()) { + if (row >= text.size()) { - row=text.size()-1; - col=text[row].size(); + row = text.size() - 1; + col = text[row].size(); } else { - col=p_mouse.x-(cache.style_normal->get_margin(MARGIN_LEFT)+cache.line_number_w+cache.breakpoint_gutter_width); - col+=cursor.x_ofs; - col=get_char_pos_for( col, get_line(row) ); + col = p_mouse.x - (cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width); + col += cursor.x_ofs; + col = get_char_pos_for(col, get_line(row)); } - r_row=row; - r_col=col; + r_row = row; + r_col = col; } -void TextEdit::_gui_input(const InputEvent& p_gui_input) { +void TextEdit::_gui_input(const InputEvent &p_gui_input) { - switch(p_gui_input.type) { + switch (p_gui_input.type) { case InputEvent::MOUSE_BUTTON: { - const InputEventMouseButton &mb=p_gui_input.mouse_button; + const InputEventMouseButton &mb = p_gui_input.mouse_button; - if (completion_active && completion_rect.has_point(Point2(mb.x,mb.y))) { + if (completion_active && completion_rect.has_point(Point2(mb.x, mb.y))) { if (!mb.pressed) return; - if (mb.button_index==BUTTON_WHEEL_UP) { - if (completion_index>0) { + if (mb.button_index == BUTTON_WHEEL_UP) { + if (completion_index > 0) { completion_index--; - completion_current=completion_options[completion_index]; + completion_current = completion_options[completion_index]; update(); } - } - if (mb.button_index==BUTTON_WHEEL_DOWN) { + if (mb.button_index == BUTTON_WHEEL_DOWN) { - if (completion_index<completion_options.size()-1) { + if (completion_index < completion_options.size() - 1) { completion_index++; - completion_current=completion_options[completion_index]; + completion_current = completion_options[completion_index]; update(); } } - if (mb.button_index==BUTTON_LEFT) { + if (mb.button_index == BUTTON_LEFT) { - completion_index=CLAMP(completion_line_ofs+(mb.y-completion_rect.pos.y)/get_row_height(),0,completion_options.size()-1); + completion_index = CLAMP(completion_line_ofs + (mb.y - completion_rect.pos.y) / get_row_height(), 0, completion_options.size() - 1); - completion_current=completion_options[completion_index]; + completion_current = completion_options[completion_index]; update(); if (mb.doubleclick) _confirm_completion(); @@ -1514,36 +1444,34 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { } if (mb.pressed) { - if (mb.button_index==BUTTON_WHEEL_UP && !mb.mod.command) { - v_scroll->set_value( v_scroll->get_value() -3 ); + if (mb.button_index == BUTTON_WHEEL_UP && !mb.mod.command) { + v_scroll->set_value(v_scroll->get_value() - 3); } - if (mb.button_index==BUTTON_WHEEL_DOWN && !mb.mod.command) { - v_scroll->set_value( v_scroll->get_value() +3 ); + if (mb.button_index == BUTTON_WHEEL_DOWN && !mb.mod.command) { + v_scroll->set_value(v_scroll->get_value() + 3); } - if (mb.button_index==BUTTON_WHEEL_LEFT) { - h_scroll->set_value( h_scroll->get_value() -3 ); + if (mb.button_index == BUTTON_WHEEL_LEFT) { + h_scroll->set_value(h_scroll->get_value() - 3); } - if (mb.button_index==BUTTON_WHEEL_RIGHT) { - h_scroll->set_value( h_scroll->get_value() +3 ); + if (mb.button_index == BUTTON_WHEEL_RIGHT) { + h_scroll->set_value(h_scroll->get_value() + 3); } - if (mb.button_index==BUTTON_LEFT) { - + if (mb.button_index == BUTTON_LEFT) { _reset_caret_blink_timer(); - int row,col; - _get_mouse_pos(Point2i(mb.x,mb.y), row,col); + int row, col; + _get_mouse_pos(Point2i(mb.x, mb.y), row, col); - if (mb.mod.command && hilighted_word!=String()) { + if (mb.mod.command && hilighted_word != String()) { - emit_signal("symbol_lookup",hilighted_word,row,col); + emit_signal("symbol_lookup", hilighted_word, row, col); return; } - // toggle breakpoint on gutter click if (draw_breakpoint_gutter) { - int gutter=cache.style_normal->get_margin(MARGIN_LEFT); + int gutter = cache.style_normal->get_margin(MARGIN_LEFT); if (mb.x > gutter && mb.x <= gutter + cache.breakpoint_gutter_width + 3) { set_line_as_breakpoint(row, !is_line_set_as_breakpoint(row)); emit_signal("breakpoint_toggled", row); @@ -1551,131 +1479,120 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { } } - int prev_col=cursor.column; - int prev_line=cursor.line; - + int prev_col = cursor.column; + int prev_line = cursor.line; + cursor_set_line(row); + cursor_set_column(col); - cursor_set_line( row ); - cursor_set_column( col ); - - if (mb.mod.shift && (cursor.column!=prev_col || cursor.line!=prev_line)) { + if (mb.mod.shift && (cursor.column != prev_col || cursor.line != prev_line)) { if (!selection.active) { - selection.active=true; - selection.selecting_mode=Selection::MODE_POINTER; - selection.from_column=prev_col; - selection.from_line=prev_line; - selection.to_column=cursor.column; - selection.to_line=cursor.line; - - if (selection.from_line>selection.to_line || (selection.from_line==selection.to_line && selection.from_column>selection.to_column)) { - SWAP(selection.from_column,selection.to_column); - SWAP(selection.from_line,selection.to_line); - selection.shiftclick_left=false; + selection.active = true; + selection.selecting_mode = Selection::MODE_POINTER; + selection.from_column = prev_col; + selection.from_line = prev_line; + selection.to_column = cursor.column; + selection.to_line = cursor.line; + + if (selection.from_line > selection.to_line || (selection.from_line == selection.to_line && selection.from_column > selection.to_column)) { + SWAP(selection.from_column, selection.to_column); + SWAP(selection.from_line, selection.to_line); + selection.shiftclick_left = false; } else { - selection.shiftclick_left=true; + selection.shiftclick_left = true; } - selection.selecting_line=prev_line; - selection.selecting_column=prev_col; + selection.selecting_line = prev_line; + selection.selecting_column = prev_col; update(); } else { - if (cursor.line<selection.selecting_line || (cursor.line==selection.selecting_line && cursor.column<selection.selecting_column)) { + if (cursor.line < selection.selecting_line || (cursor.line == selection.selecting_line && cursor.column < selection.selecting_column)) { if (selection.shiftclick_left) { - SWAP(selection.from_column,selection.to_column); - SWAP(selection.from_line,selection.to_line); + SWAP(selection.from_column, selection.to_column); + SWAP(selection.from_line, selection.to_line); selection.shiftclick_left = !selection.shiftclick_left; } - selection.from_column=cursor.column; - selection.from_line=cursor.line; + selection.from_column = cursor.column; + selection.from_line = cursor.line; - } else if (cursor.line>selection.selecting_line || (cursor.line==selection.selecting_line && cursor.column>selection.selecting_column)) { + } else if (cursor.line > selection.selecting_line || (cursor.line == selection.selecting_line && cursor.column > selection.selecting_column)) { if (!selection.shiftclick_left) { - SWAP(selection.from_column,selection.to_column); - SWAP(selection.from_line,selection.to_line); + SWAP(selection.from_column, selection.to_column); + SWAP(selection.from_line, selection.to_line); selection.shiftclick_left = !selection.shiftclick_left; } - selection.to_column=cursor.column; - selection.to_line=cursor.line; + selection.to_column = cursor.column; + selection.to_line = cursor.line; } else { - selection.active=false; + selection.active = false; } update(); } - - - - - - } else { //if sel active and dblick last time < something //else - selection.active=false; - selection.selecting_mode=Selection::MODE_POINTER; - selection.selecting_line=row; - selection.selecting_column=col; + selection.active = false; + selection.selecting_mode = Selection::MODE_POINTER; + selection.selecting_line = row; + selection.selecting_column = col; } - - if (!mb.doubleclick && (OS::get_singleton()->get_ticks_msec()-last_dblclk)<600 && cursor.line==prev_line) { + if (!mb.doubleclick && (OS::get_singleton()->get_ticks_msec() - last_dblclk) < 600 && cursor.line == prev_line) { //tripleclick select line - select(cursor.line,0,cursor.line,text[cursor.line].length()); - selection.selecting_column=0; - last_dblclk=0; + select(cursor.line, 0, cursor.line, text[cursor.line].length()); + selection.selecting_column = 0; + last_dblclk = 0; } else if (mb.doubleclick && text[cursor.line].length()) { //doubleclick select world String s = text[cursor.line]; - int beg=CLAMP(cursor.column,0,s.length()); - int end=beg; + int beg = CLAMP(cursor.column, 0, s.length()); + int end = beg; - if (s[beg]>32 || beg==s.length()) { + if (s[beg] > 32 || beg == s.length()) { - bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this + bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this - while(beg>0 && s[beg-1]>32 && (symbol==_is_symbol(s[beg-1]))) { + while (beg > 0 && s[beg - 1] > 32 && (symbol == _is_symbol(s[beg - 1]))) { beg--; } - while(end<s.length() && s[end+1]>32 && (symbol==_is_symbol(s[end+1]))) { + while (end < s.length() && s[end + 1] > 32 && (symbol == _is_symbol(s[end + 1]))) { end++; } - if (end<s.length()) - end+=1; + if (end < s.length()) + end += 1; - select(cursor.line,beg,cursor.line,end); + select(cursor.line, beg, cursor.line, end); - selection.selecting_column=beg; + selection.selecting_column = beg; } last_dblclk = OS::get_singleton()->get_ticks_msec(); - } update(); } - if (mb.button_index==BUTTON_RIGHT && context_menu_enabled) { + if (mb.button_index == BUTTON_RIGHT && context_menu_enabled) { menu->set_pos(get_global_transform().xform(get_local_mouse_pos())); - menu->set_size(Vector2(1,1)); + menu->set_size(Vector2(1, 1)); menu->popup(); grab_focus(); - } } else { - if (mb.button_index==BUTTON_LEFT) + if (mb.button_index == BUTTON_LEFT) click_select_held->stop(); // notify to show soft keyboard @@ -1685,56 +1602,53 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { } break; case InputEvent::MOUSE_MOTION: { - const InputEventMouseMotion &mm=p_gui_input.mouse_motion; + const InputEventMouseMotion &mm = p_gui_input.mouse_motion; if (select_identifiers_enabled) { - if (mm.mod.command && mm.button_mask==0) { + if (mm.mod.command && mm.button_mask == 0) { - String new_word = get_word_at_pos(Vector2(mm.x,mm.y)); - if (new_word!=hilighted_word) { - hilighted_word=new_word; + String new_word = get_word_at_pos(Vector2(mm.x, mm.y)); + if (new_word != hilighted_word) { + hilighted_word = new_word; update(); } } else { - if (hilighted_word!=String()) { - hilighted_word=String(); + if (hilighted_word != String()) { + hilighted_word = String(); update(); } } } - if (mm.button_mask&BUTTON_MASK_LEFT && get_viewport()->gui_get_drag_data()==Variant()) { //ignore if dragging + if (mm.button_mask & BUTTON_MASK_LEFT && get_viewport()->gui_get_drag_data() == Variant()) { //ignore if dragging - if (selection.selecting_mode!=Selection::MODE_NONE) { + if (selection.selecting_mode != Selection::MODE_NONE) { _reset_caret_blink_timer(); - int row,col; - _get_mouse_pos(Point2i(mm.x,mm.y), row,col); + int row, col; + _get_mouse_pos(Point2i(mm.x, mm.y), row, col); - select(selection.selecting_line,selection.selecting_column,row,col); + select(selection.selecting_line, selection.selecting_column, row, col); - cursor_set_line( row ); - cursor_set_column( col ); + cursor_set_line(row); + cursor_set_column(col); update(); click_select_held->start(); - } - } } break; case InputEvent::KEY: { - InputEventKey k=p_gui_input.key; - + InputEventKey k = p_gui_input.key; #ifdef OSX_ENABLED - if (k.scancode==KEY_META) { + if (k.scancode == KEY_META) { #else - if (k.scancode==KEY_CONTROL) { + if (k.scancode == KEY_CONTROL) { #endif if (select_identifiers_enabled) { @@ -1745,7 +1659,7 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { update(); } else { - hilighted_word=String(); + hilighted_word = String(); update(); } } @@ -1758,97 +1672,94 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { if (readonly) break; - bool valid=true; + bool valid = true; if (k.mod.command || k.mod.meta) - valid=false; + valid = false; if (valid) { if (!k.mod.alt) { - if (k.scancode==KEY_UP) { + if (k.scancode == KEY_UP) { - if (completion_index>0) { + if (completion_index > 0) { completion_index--; - completion_current=completion_options[completion_index]; + completion_current = completion_options[completion_index]; update(); } accept_event(); return; } + if (k.scancode == KEY_DOWN) { - if (k.scancode==KEY_DOWN) { - - if (completion_index<completion_options.size()-1) { + if (completion_index < completion_options.size() - 1) { completion_index++; - completion_current=completion_options[completion_index]; + completion_current = completion_options[completion_index]; update(); } accept_event(); return; } - if (k.scancode==KEY_PAGEUP) { + if (k.scancode == KEY_PAGEUP) { - completion_index-=get_constant("completion_lines"); - if (completion_index<0) - completion_index=0; - completion_current=completion_options[completion_index]; + completion_index -= get_constant("completion_lines"); + if (completion_index < 0) + completion_index = 0; + completion_current = completion_options[completion_index]; update(); accept_event(); return; } + if (k.scancode == KEY_PAGEDOWN) { - if (k.scancode==KEY_PAGEDOWN) { - - completion_index+=get_constant("completion_lines"); - if (completion_index>=completion_options.size()) - completion_index=completion_options.size()-1; - completion_current=completion_options[completion_index]; + completion_index += get_constant("completion_lines"); + if (completion_index >= completion_options.size()) + completion_index = completion_options.size() - 1; + completion_current = completion_options[completion_index]; update(); accept_event(); return; } - if (k.scancode==KEY_HOME && completion_index>0) { + if (k.scancode == KEY_HOME && completion_index > 0) { - completion_index=0; - completion_current=completion_options[completion_index]; + completion_index = 0; + completion_current = completion_options[completion_index]; update(); accept_event(); return; } - if (k.scancode==KEY_END && completion_index<completion_options.size()-1) { + if (k.scancode == KEY_END && completion_index < completion_options.size() - 1) { - completion_index=completion_options.size()-1; - completion_current=completion_options[completion_index]; + completion_index = completion_options.size() - 1; + completion_current = completion_options[completion_index]; update(); accept_event(); return; } + if (k.scancode == KEY_DOWN) { - if (k.scancode==KEY_DOWN) { - - if (completion_index<completion_options.size()-1) { + if (completion_index < completion_options.size() - 1) { completion_index++; - completion_current=completion_options[completion_index]; + completion_current = completion_options[completion_index]; update(); } accept_event(); return; } - if (k.scancode==KEY_ENTER || k.scancode==KEY_RETURN || k.scancode==KEY_TAB) { + if (k.scancode == KEY_ENTER || k.scancode == KEY_RETURN || k.scancode == KEY_TAB) { _confirm_completion(); accept_event(); return; } - if (k.scancode==KEY_BACKSPACE) { + if (k.scancode == KEY_BACKSPACE) { _reset_caret_blink_timer(); @@ -1858,19 +1769,18 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { return; } - - if (k.scancode==KEY_SHIFT) { + if (k.scancode == KEY_SHIFT) { accept_event(); return; } } - if (k.unicode>32) { + if (k.unicode > 32) { _reset_caret_blink_timer(); - const CharType chr[2] = {(CharType)k.unicode, 0}; - if(auto_brace_completion_enabled && _is_pair_symbol(chr[0])) { + const CharType chr[2] = { (CharType)k.unicode, 0 }; + if (auto_brace_completion_enabled && _is_pair_symbol(chr[0])) { _consume_pair_symbol(chr[0]); } else { @@ -1898,28 +1808,26 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { } _cancel_completion(); - } /* TEST CONTROL FIRST!! */ // some remaps for duplicate functions.. - if (k.mod.command && !k.mod.shift && !k.mod.alt && !k.mod.meta && k.scancode==KEY_INSERT) { + if (k.mod.command && !k.mod.shift && !k.mod.alt && !k.mod.meta && k.scancode == KEY_INSERT) { - k.scancode=KEY_C; + k.scancode = KEY_C; } - if (!k.mod.command && k.mod.shift && !k.mod.alt && !k.mod.meta && k.scancode==KEY_INSERT) { + if (!k.mod.command && k.mod.shift && !k.mod.alt && !k.mod.meta && k.scancode == KEY_INSERT) { - k.scancode=KEY_V; - k.mod.command=true; - k.mod.shift=false; + k.scancode = KEY_V; + k.mod.command = true; + 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; @@ -1929,11 +1837,11 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { if (readonly) break; - bool clear=false; - bool unselect=false; - bool dobreak=false; + bool clear = false; + bool unselect = false; + bool dobreak = false; - switch(k.scancode) { + switch (k.scancode) { case KEY_TAB: { if (k.mod.shift) { @@ -1941,25 +1849,27 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { } else { indent_selection_right(); } - dobreak=true; + dobreak = true; accept_event(); } break; case KEY_X: case KEY_C: //special keys often used with control, wait... - clear=(!k.mod.command || k.mod.shift || k.mod.alt ); + clear = (!k.mod.command || k.mod.shift || k.mod.alt); break; case KEY_DELETE: if (!k.mod.shift) { accept_event(); - clear=true; dobreak=true; + clear = true; + dobreak = true; } else if (k.mod.command || k.mod.alt) { - dobreak=true; + dobreak = true; } break; case KEY_BACKSPACE: accept_event(); - clear=true; dobreak=true; + clear = true; + dobreak = true; break; case KEY_LEFT: case KEY_RIGHT: @@ -1972,19 +1882,19 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { // ignore arrows if any modifiers are held (shift = selecting, others may be used for editor hotkeys) if (k.mod.command || k.mod.shift || k.mod.alt) break; - unselect=true; + unselect = true; break; default: - if (k.unicode>=32 && !k.mod.command && !k.mod.alt && !k.mod.meta) - clear=true; + if (k.unicode >= 32 && !k.mod.command && !k.mod.alt && !k.mod.meta) + clear = true; if (auto_brace_completion_enabled && _is_pair_left_symbol(k.unicode)) - clear=false; + clear = false; } if (unselect) { - selection.active=false; - selection.selecting_mode=Selection::MODE_NONE; + selection.active = false; + selection.selecting_mode = Selection::MODE_NONE; update(); } if (clear) { @@ -1992,9 +1902,9 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { if (!dobreak) { begin_complex_operation(); } - selection.active=false; + selection.active = false; update(); - _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column); + _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column); cursor_set_line(selection.from_line); cursor_set_column(selection.from_column); update(); @@ -2003,9 +1913,9 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { break; } - selection.selecting_text=false; + selection.selecting_text = false; - bool scancode_handled=true; + bool scancode_handled = true; // special scancode test... @@ -2017,20 +1927,20 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { if (readonly) break; - String ins="\n"; + String ins = "\n"; //keep indentation - for(int i=0;i<text[cursor.line].length();i++) { - if (text[cursor.line][i]=='\t') - ins+="\t"; + for (int i = 0; i < text[cursor.line].length(); i++) { + if (text[cursor.line][i] == '\t') + ins += "\t"; else break; } - if(auto_indent){ + if (auto_indent) { // indent once again if previous line will end with ':' // (i.e. colon precedes current cursor position) - if(cursor.column>0 && text[cursor.line][cursor.column-1]==':') { - ins+="\t"; + if (cursor.column > 0 && text[cursor.line][cursor.column - 1] == ':') { + ins += "\t"; } } @@ -2040,13 +1950,11 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { if (cursor.line > 0) { cursor_set_line(cursor.line - 1); cursor_set_column(text[cursor.line].length()); - } - else { + } else { cursor_set_column(0); first_line = true; } - } - else { + } else { cursor_set_column(text[cursor.line].length()); } } @@ -2060,11 +1968,11 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { } break; case KEY_ESCAPE: { - if (completion_hint!="") { - completion_hint=""; + if (completion_hint != "") { + completion_hint = ""; update(); } else { - scancode_handled=false; + scancode_handled = false; } } break; case KEY_TAB: { @@ -2075,12 +1983,11 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { if (selection.active) { - } else { if (k.mod.shift) { int cc = cursor.column; - if (cc>0 && cc<=text[cursor.line].length() && text[cursor.line][cursor.column-1]=='\t') { + if (cc > 0 && cc <= text[cursor.line].length() && text[cursor.line][cursor.column - 1] == '\t') { //simple unindent backspace_at_cursor(); @@ -2100,23 +2007,23 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { if (k.mod.alt) { #else if (k.mod.alt) { - scancode_handled=false; + scancode_handled = false; break; } else if (k.mod.command) { #endif - int line=cursor.line; - int column=cursor.column; + int line = cursor.line; + int column = cursor.column; - bool prev_char=false; - bool only_whitespace=true; + bool prev_char = false; + bool only_whitespace = true; while (only_whitespace && line > 0) { - while (column>0) { - CharType c=text[line][column-1]; + while (column > 0) { + CharType c = text[line][column - 1]; if (c != '\t' && c != ' ') { - only_whitespace=false; + only_whitespace = false; break; } @@ -2125,19 +2032,18 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { if (only_whitespace) { line--; - column=text[line].length(); + column = text[line].length(); } } - while (column>0) { - bool ischar=_is_text_char(text[line][column-1]); + while (column > 0) { + bool ischar = _is_text_char(text[line][column - 1]); if (prev_char && !ischar) break; - prev_char=ischar; + prev_char = ischar; column--; - } _remove_text(line, column, cursor.line, cursor.column); @@ -2175,34 +2081,33 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { #else if (k.mod.alt) { - scancode_handled=false; + scancode_handled = false; break; } else if (k.mod.command) { #endif - bool prev_char=false; - int cc=cursor.column; - while (cc>0) { + bool prev_char = false; + int cc = cursor.column; + while (cc > 0) { - bool ischar=_is_text_char(text[cursor.line][cc-1]); + bool ischar = _is_text_char(text[cursor.line][cc - 1]); if (prev_char && !ischar) break; - prev_char=ischar; + prev_char = ischar; cc--; - } cursor_set_column(cc); - } else if (cursor.column==0) { + } else if (cursor.column == 0) { - if (cursor.line>0) { - cursor_set_line(cursor.line-1); + if (cursor.line > 0) { + cursor_set_line(cursor.line - 1); cursor_set_column(text[cursor.line].length()); } } else { - cursor_set_column(cursor_get_column()-1); + cursor_set_column(cursor_get_column() - 1); } if (k.mod.shift) @@ -2233,32 +2138,32 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { } else if (k.mod.alt) { #else if (k.mod.alt) { - scancode_handled=false; + scancode_handled = false; break; } else if (k.mod.command) { #endif - bool prev_char=false; - int cc=cursor.column; - while (cc<text[cursor.line].length()) { + bool prev_char = false; + int cc = cursor.column; + while (cc < text[cursor.line].length()) { - bool ischar=_is_text_char(text[cursor.line][cc]); + bool ischar = _is_text_char(text[cursor.line][cc]); if (prev_char && !ischar) break; - prev_char=ischar; + prev_char = ischar; cc++; } cursor_set_column(cc); - } else if (cursor.column==text[cursor.line].length()) { + } else if (cursor.column == text[cursor.line].length()) { - if (cursor.line<text.size()-1) { - cursor_set_line(cursor.line+1); + if (cursor.line < text.size() - 1) { + cursor_set_line(cursor.line + 1); cursor_set_column(0); } } else { - cursor_set_column(cursor_get_column()+1); + cursor_set_column(cursor_get_column() + 1); } if (k.mod.shift) @@ -2277,7 +2182,7 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { if (k.mod.shift) _pre_shift_selection(); if (k.mod.alt) { - scancode_handled=false; + scancode_handled = false; break; } #ifndef APPLE_STYLE_KEYS @@ -2295,7 +2200,7 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { cursor_set_line(0); else #endif - cursor_set_line(cursor_get_line()-1); + cursor_set_line(cursor_get_line() - 1); if (k.mod.shift) _post_shift_selection(); @@ -2314,7 +2219,7 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { if (k.mod.shift) _pre_shift_selection(); if (k.mod.alt) { - scancode_handled=false; + scancode_handled = false; break; } #ifndef APPLE_STYLE_KEYS @@ -2329,10 +2234,10 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { } if (k.mod.command) - cursor_set_line(text.size()-1); + cursor_set_line(text.size() - 1); else #endif - cursor_set_line(cursor_get_line()+1); + cursor_set_line(cursor_get_line() + 1); if (k.mod.shift) _post_shift_selection(); @@ -2352,35 +2257,35 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { int curline_len = text[cursor.line].length(); - if (cursor.line==text.size()-1 && cursor.column==curline_len) + if (cursor.line == text.size() - 1 && cursor.column == curline_len) break; //nothing to do - int next_line=cursor.column<curline_len?cursor.line:cursor.line+1; + int next_line = cursor.column < curline_len ? cursor.line : cursor.line + 1; int next_column; #ifdef APPLE_STYLE_KEYS if (k.mod.alt) { #else if (k.mod.alt) { - scancode_handled=false; + scancode_handled = false; break; } else if (k.mod.command) { #endif - int last_line=text.size()-1; + int last_line = text.size() - 1; - int line=cursor.line; - int column=cursor.column; + int line = cursor.line; + int column = cursor.column; - bool prev_char=false; - bool only_whitespace=true; + bool prev_char = false; + bool only_whitespace = true; while (only_whitespace && line < last_line) { - while (column<text[line].length()) { - CharType c=text[line][column]; + while (column < text[line].length()) { + CharType c = text[line][column]; if (c != '\t' && c != ' ') { - only_whitespace=false; + only_whitespace = false; break; } @@ -2389,27 +2294,27 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { if (only_whitespace) { line++; - column=0; + column = 0; } } - while (column<text[line].length()) { + while (column < text[line].length()) { - bool ischar=_is_text_char(text[line][column]); + bool ischar = _is_text_char(text[line][column]); if (prev_char && !ischar) break; - prev_char=ischar; + prev_char = ischar; column++; } - next_line=line; - next_column=column; + next_line = line; + next_column = column; } else { - next_column=cursor.column<curline_len?(cursor.column+1):0; + next_column = cursor.column < curline_len ? (cursor.column + 1) : 0; } - _remove_text(cursor.line,cursor.column,next_line,next_column); + _remove_text(cursor.line, cursor.column, next_line, next_column); update(); } break; @@ -2423,7 +2328,6 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { #ifdef APPLE_STYLE_KEYS case KEY_HOME: { - if (k.mod.shift) _pre_shift_selection(); @@ -2431,43 +2335,41 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { if (k.mod.shift) _post_shift_selection(); - else if(k.mod.command || k.mod.control) + else if (k.mod.command || k.mod.control) deselect(); } break; #else case KEY_HOME: { - if (k.mod.shift) _pre_shift_selection(); if (k.mod.command) { cursor_set_line(0); cursor_set_column(0); - } - else { + } else { // compute whitespace symbols seq length int current_line_whitespace_len = 0; - while( current_line_whitespace_len < text[cursor.line].length() ) { - CharType c = text[cursor.line][current_line_whitespace_len]; - if( c != '\t' && c != ' ' ) - break; - current_line_whitespace_len++; + while (current_line_whitespace_len < text[cursor.line].length()) { + CharType c = text[cursor.line][current_line_whitespace_len]; + if (c != '\t' && c != ' ') + break; + current_line_whitespace_len++; } - if( cursor_get_column() == current_line_whitespace_len ) - cursor_set_column(0); + if (cursor_get_column() == current_line_whitespace_len) + cursor_set_column(0); else - cursor_set_column(current_line_whitespace_len); + cursor_set_column(current_line_whitespace_len); } if (k.mod.shift) _post_shift_selection(); - else if(k.mod.command || k.mod.control) + else if (k.mod.command || k.mod.control) deselect(); _cancel_completion(); - completion_hint=""; + completion_hint = ""; } break; #endif @@ -2484,11 +2386,11 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { if (k.mod.shift) _pre_shift_selection(); - cursor_set_line(text.size()-1); + cursor_set_line(text.size() - 1); if (k.mod.shift) _post_shift_selection(); - else if(k.mod.command || k.mod.control) + else if (k.mod.command || k.mod.control) deselect(); } break; @@ -2499,16 +2401,16 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { _pre_shift_selection(); if (k.mod.command) - cursor_set_line(text.size()-1); + cursor_set_line(text.size() - 1); cursor_set_column(text[cursor.line].length()); if (k.mod.shift) _post_shift_selection(); - else if(k.mod.command || k.mod.control) + else if (k.mod.command || k.mod.control) deselect(); _cancel_completion(); - completion_hint=""; + completion_hint = ""; } break; #endif @@ -2524,14 +2426,13 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { if (k.mod.shift) _pre_shift_selection(); - cursor_set_line(cursor_get_line()-get_visible_rows()); + cursor_set_line(cursor_get_line() - get_visible_rows()); if (k.mod.shift) _post_shift_selection(); _cancel_completion(); - completion_hint=""; - + completion_hint = ""; } break; case KEY_KP_3: { @@ -2546,20 +2447,19 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { if (k.mod.shift) _pre_shift_selection(); - cursor_set_line(cursor_get_line()+get_visible_rows()); + cursor_set_line(cursor_get_line() + get_visible_rows()); if (k.mod.shift) _post_shift_selection(); _cancel_completion(); - completion_hint=""; - + completion_hint = ""; } break; case KEY_A: { if (!k.mod.command || k.mod.shift || k.mod.alt) { - scancode_handled=false; + scancode_handled = false; break; } @@ -2571,7 +2471,7 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { break; } if (!k.mod.command || k.mod.shift || k.mod.alt) { - scancode_handled=false; + scancode_handled = false; break; } @@ -2581,7 +2481,7 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { case KEY_C: { if (!k.mod.command || k.mod.shift || k.mod.alt) { - scancode_handled=false; + scancode_handled = false; break; } @@ -2591,7 +2491,7 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { case KEY_Z: { if (!k.mod.command) { - scancode_handled=false; + scancode_handled = false; break; } @@ -2605,7 +2505,7 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { break; } if (!k.mod.command || k.mod.shift || k.mod.alt) { - scancode_handled=false; + scancode_handled = false; break; } @@ -2620,31 +2520,28 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { #endif query_code_comple(); - scancode_handled=true; + scancode_handled = true; } else { - scancode_handled=false; + scancode_handled = false; } } break; - case KEY_U:{ + case KEY_U: { if (!k.mod.command || k.mod.shift) { - scancode_handled=false; + scancode_handled = false; break; - } - else { + } else { if (selection.active) { int ini = selection.from_line; int end = selection.to_line; - for (int i=ini; i<= end; i++) - { + for (int i = ini; i <= end; i++) { if (text[i][0] == '#') - _remove_text(i,0,i,1); + _remove_text(i, 0, i, 1); } - } - else{ + } else { if (text[cursor.line][0] == '#') - _remove_text(cursor.line,0,cursor.line,1); + _remove_text(cursor.line, 0, cursor.line, 1); } update(); } @@ -2652,9 +2549,8 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { default: { - scancode_handled=false; + scancode_handled = false; } break; - } if (scancode_handled) @@ -2674,7 +2570,7 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { } } */ - if (k.scancode==KEY_INSERT) { + if (k.scancode == KEY_INSERT) { set_insert_mode(!insert_mode); accept_event(); return; @@ -2682,7 +2578,7 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { if (!scancode_handled && !k.mod.command) { //for german kbds - if (k.unicode>=32) { + if (k.unicode >= 32) { if (readonly) break; @@ -2697,12 +2593,12 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { } } - const CharType chr[2] = {(CharType)k.unicode, 0}; + const CharType chr[2] = { (CharType)k.unicode, 0 }; - if (completion_hint!="" && k.unicode==')') { - completion_hint=""; + if (completion_hint != "" && k.unicode == ')') { + completion_hint = ""; } - if(auto_brace_completion_enabled && _is_pair_symbol(chr[0])) { + if (auto_brace_completion_enabled && _is_pair_symbol(chr[0])) { _consume_pair_symbol(chr[0]); } else { _insert_text_at_cursor(chr); @@ -2724,36 +2620,30 @@ void TextEdit::_gui_input(const InputEvent& p_gui_input) { return; } break; - } - } - void TextEdit::_pre_shift_selection() { + if (!selection.active || selection.selecting_mode == Selection::MODE_NONE) { - if (!selection.active || selection.selecting_mode==Selection::MODE_NONE) { - - selection.selecting_line=cursor.line; - selection.selecting_column=cursor.column; - selection.active=true; + selection.selecting_line = cursor.line; + selection.selecting_column = cursor.column; + selection.active = true; } - selection.selecting_mode=Selection::MODE_SHIFT; + selection.selecting_mode = Selection::MODE_SHIFT; } void TextEdit::_post_shift_selection() { + if (selection.active && selection.selecting_mode == Selection::MODE_SHIFT) { - if (selection.active && selection.selecting_mode==Selection::MODE_SHIFT) { - - select(selection.selecting_line,selection.selecting_column,cursor.line,cursor.column); + select(selection.selecting_line, selection.selecting_column, cursor.line, cursor.column); update(); } - - selection.selecting_text=true; + selection.selecting_text = true; } void TextEdit::_scroll_lines_up() { @@ -2788,109 +2678,106 @@ void TextEdit::_scroll_lines_down() { /**** TEXT EDIT CORE API ****/ -void TextEdit::_base_insert_text(int p_line, int p_char,const String& p_text,int &r_end_line,int &r_end_column) { +void TextEdit::_base_insert_text(int p_line, int p_char, const String &p_text, int &r_end_line, int &r_end_column) { //save for undo... - ERR_FAIL_INDEX(p_line,text.size()); - ERR_FAIL_COND(p_char<0); + ERR_FAIL_INDEX(p_line, text.size()); + ERR_FAIL_COND(p_char < 0); /* STEP 1 add spaces if the char is greater than the end of the line */ - while(p_char>text[p_line].length()) { + while (p_char > text[p_line].length()) { - text.set(p_line,text[p_line]+String::chr(' ')); + text.set(p_line, text[p_line] + String::chr(' ')); } /* STEP 2 separate dest string in pre and post text */ - String preinsert_text = text[p_line].substr(0,p_char); - String postinsert_text = text[p_line].substr(p_char,text[p_line].size()); + String preinsert_text = text[p_line].substr(0, p_char); + String postinsert_text = text[p_line].substr(p_char, text[p_line].size()); /* STEP 3 remove \r from source text and separate in substrings */ //buh bye \r and split - Vector<String> substrings = p_text.replace("\r","").split("\n"); + Vector<String> substrings = p_text.replace("\r", "").split("\n"); - - for(int i=0;i<substrings.size();i++) { + for (int i = 0; i < substrings.size(); i++) { //insert the substrings - if (i==0) { + if (i == 0) { - text.set(p_line,preinsert_text+substrings[i]); + text.set(p_line, preinsert_text + substrings[i]); } else { - text.insert(p_line+i,substrings[i]); + text.insert(p_line + i, substrings[i]); } - if (i==substrings.size()-1){ + if (i == substrings.size() - 1) { - text.set(p_line+i,text[p_line+i]+postinsert_text); + text.set(p_line + i, text[p_line + i] + postinsert_text); } } - r_end_line=p_line+substrings.size()-1; - r_end_column=text[r_end_line].length()-postinsert_text.length(); + r_end_line = p_line + substrings.size() - 1; + r_end_column = text[r_end_line].length() - postinsert_text.length(); if (!text_changed_dirty && !setting_text) { if (is_inside_tree()) - MessageQueue::get_singleton()->push_call(this,"_text_changed_emit"); - text_changed_dirty=true; + MessageQueue::get_singleton()->push_call(this, "_text_changed_emit"); + text_changed_dirty = true; } - } -String TextEdit::_base_get_text(int p_from_line, int p_from_column,int p_to_line,int p_to_column) const { +String TextEdit::_base_get_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) const { - ERR_FAIL_INDEX_V(p_from_line,text.size(),String()); - ERR_FAIL_INDEX_V(p_from_column,text[p_from_line].length()+1,String()); - ERR_FAIL_INDEX_V(p_to_line,text.size(),String()); - ERR_FAIL_INDEX_V(p_to_column,text[p_to_line].length()+1,String()); - ERR_FAIL_COND_V(p_to_line < p_from_line ,String()); // from > to - ERR_FAIL_COND_V(p_to_line == p_from_line && p_to_column<p_from_column,String()); // from > to + ERR_FAIL_INDEX_V(p_from_line, text.size(), String()); + ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, String()); + ERR_FAIL_INDEX_V(p_to_line, text.size(), String()); + ERR_FAIL_INDEX_V(p_to_column, text[p_to_line].length() + 1, String()); + ERR_FAIL_COND_V(p_to_line < p_from_line, String()); // from > to + ERR_FAIL_COND_V(p_to_line == p_from_line && p_to_column < p_from_column, String()); // from > to String ret; - for(int i=p_from_line;i<=p_to_line;i++) { + for (int i = p_from_line; i <= p_to_line; i++) { - int begin = (i==p_from_line)?p_from_column:0; - int end = (i==p_to_line)?p_to_column:text[i].length(); + int begin = (i == p_from_line) ? p_from_column : 0; + int end = (i == p_to_line) ? p_to_column : text[i].length(); - if (i>p_from_line) - ret+="\n"; - ret+=text[i].substr(begin,end-begin); + if (i > p_from_line) + ret += "\n"; + ret += text[i].substr(begin, end - begin); } return ret; } -void TextEdit::_base_remove_text(int p_from_line, int p_from_column,int p_to_line,int p_to_column) { - - ERR_FAIL_INDEX(p_from_line,text.size()); - ERR_FAIL_INDEX(p_from_column,text[p_from_line].length()+1); - ERR_FAIL_INDEX(p_to_line,text.size()); - ERR_FAIL_INDEX(p_to_column,text[p_to_line].length()+1); - ERR_FAIL_COND(p_to_line < p_from_line ); // from > to - ERR_FAIL_COND(p_to_line == p_from_line && p_to_column<p_from_column); // from > to +void TextEdit::_base_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) { + ERR_FAIL_INDEX(p_from_line, text.size()); + ERR_FAIL_INDEX(p_from_column, text[p_from_line].length() + 1); + ERR_FAIL_INDEX(p_to_line, text.size()); + ERR_FAIL_INDEX(p_to_column, text[p_to_line].length() + 1); + ERR_FAIL_COND(p_to_line < p_from_line); // from > to + ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column); // from > to - String pre_text = text[p_from_line].substr(0,p_from_column); - String post_text = text[p_to_line].substr(p_to_column,text[p_to_line].length()); + String pre_text = text[p_from_line].substr(0, p_from_column); + String post_text = text[p_to_line].substr(p_to_column, text[p_to_line].length()); - for(int i=p_from_line;i<p_to_line;i++) { + for (int i = p_from_line; i < p_to_line; i++) { - text.remove(p_from_line+1); + text.remove(p_from_line + 1); } - text.set(p_from_line,pre_text+post_text); + text.set(p_from_line, pre_text + post_text); if (!text_changed_dirty && !setting_text) { if (is_inside_tree()) - MessageQueue::get_singleton()->push_call(this,"_text_changed_emit"); - text_changed_dirty=true; + MessageQueue::get_singleton()->push_call(this, "_text_changed_emit"); + text_changed_dirty = true; } } -void TextEdit::_insert_text(int p_line, int p_char,const String& p_text,int *r_end_line,int *r_end_column) { +void TextEdit::_insert_text(int p_line, int p_char, const String &p_text, int *r_end_line, int *r_end_column) { if (!setting_text) idle_detect->start(); @@ -2899,53 +2786,52 @@ void TextEdit::_insert_text(int p_line, int p_char,const String& p_text,int *r_e _clear_redo(); } - int retline,retchar; - _base_insert_text(p_line,p_char,p_text,retline,retchar); + int retline, retchar; + _base_insert_text(p_line, p_char, p_text, retline, retchar); if (r_end_line) - *r_end_line=retline; + *r_end_line = retline; if (r_end_column) - *r_end_column=retchar; + *r_end_column = retchar; if (!undo_enabled) return; /* UNDO!! */ TextOperation op; - op.type=TextOperation::TYPE_INSERT; - op.from_line=p_line; - op.from_column=p_char; - op.to_line=retline; - op.to_column=retchar; - op.text=p_text; - op.version=++version; - op.chain_forward=false; - op.chain_backward=false; + op.type = TextOperation::TYPE_INSERT; + op.from_line = p_line; + op.from_column = p_char; + op.to_line = retline; + op.to_column = retchar; + op.text = p_text; + op.version = ++version; + op.chain_forward = false; + op.chain_backward = false; //see if it shold just be set as current op - if (current_op.type!=op.type) { + if (current_op.type != op.type) { op.prev_version = get_version(); _push_current_op(); - current_op=op; + current_op = op; return; //set as current op, return } //see if it can be merged - if (current_op.to_line!=p_line || current_op.to_column!=p_char) { + if (current_op.to_line != p_line || current_op.to_column != p_char) { op.prev_version = get_version(); _push_current_op(); - current_op=op; + current_op = op; return; //set as current op, return } //merge current op - current_op.text+=p_text; - current_op.to_column=retchar; - current_op.to_line=retline; - current_op.version=op.version; - + current_op.text += p_text; + current_op.to_column = retchar; + current_op.to_line = retline; + current_op.version = op.version; } -void TextEdit::_remove_text(int p_from_line, int p_from_column,int p_to_line,int p_to_column) { +void TextEdit::_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) { if (!setting_text) idle_detect->start(); @@ -2953,42 +2839,42 @@ void TextEdit::_remove_text(int p_from_line, int p_from_column,int p_to_line,int String text; if (undo_enabled) { _clear_redo(); - text=_base_get_text(p_from_line,p_from_column,p_to_line,p_to_column); + text = _base_get_text(p_from_line, p_from_column, p_to_line, p_to_column); } - _base_remove_text(p_from_line,p_from_column,p_to_line,p_to_column); + _base_remove_text(p_from_line, p_from_column, p_to_line, p_to_column); if (!undo_enabled) return; /* UNDO!! */ TextOperation op; - op.type=TextOperation::TYPE_REMOVE; - op.from_line=p_from_line; - op.from_column=p_from_column; - op.to_line=p_to_line; - op.to_column=p_to_column; - op.text=text; - op.version=++version; - op.chain_forward=false; - op.chain_backward=false; + op.type = TextOperation::TYPE_REMOVE; + op.from_line = p_from_line; + op.from_column = p_from_column; + op.to_line = p_to_line; + op.to_column = p_to_column; + op.text = text; + op.version = ++version; + op.chain_forward = false; + op.chain_backward = false; //see if it shold just be set as current op - if (current_op.type!=op.type) { + if (current_op.type != op.type) { op.prev_version = get_version(); _push_current_op(); - current_op=op; + current_op = op; return; //set as current op, return } //see if it can be merged - if (current_op.from_line==p_to_line && current_op.from_column==p_to_column) { + if (current_op.from_line == p_to_line && current_op.from_column == p_to_column) { //basckace or similar - current_op.text=text+current_op.text; - current_op.from_line=p_from_line; - current_op.from_column=p_from_column; + current_op.text = text + current_op.text; + current_op.from_line = p_from_line; + current_op.from_column = p_from_column; return; //update current op } - if (current_op.from_line==p_from_line && current_op.from_column==p_from_column) { + if (current_op.from_line == p_from_line && current_op.from_column == p_from_column) { //current_op.text=text+current_op.text; //current_op.from_line=p_from_line; @@ -2998,33 +2884,28 @@ void TextEdit::_remove_text(int p_from_line, int p_from_column,int p_to_line,int op.prev_version = get_version(); _push_current_op(); - current_op=op; - + current_op = op; } +void TextEdit::_insert_text_at_cursor(const String &p_text) { -void TextEdit::_insert_text_at_cursor(const String& p_text) { - - int new_column,new_line; - _insert_text(cursor.line,cursor.column,p_text,&new_line,&new_column); + int new_column, new_line; + _insert_text(cursor.line, cursor.column, p_text, &new_line, &new_column); cursor_set_line(new_line); cursor_set_column(new_column); update(); } - - - int TextEdit::get_char_count() { - int totalsize=0; + int totalsize = 0; - for (int i=0;i<text.size();i++) { + for (int i = 0; i < text.size(); i++) { - if (i>0) + if (i > 0) totalsize++; // incliude \n - totalsize+=text[i].length(); + totalsize += text[i].length(); } return totalsize; // omit last \n @@ -3036,40 +2917,39 @@ Size2 TextEdit::get_minimum_size() const { } int TextEdit::get_visible_rows() const { - int total=cache.size.height; - total-=cache.style_normal->get_minimum_size().height; - total/=get_row_height(); + int total = cache.size.height; + total -= cache.style_normal->get_minimum_size().height; + total /= get_row_height(); return total; } void TextEdit::adjust_viewport_to_cursor() { - if (cursor.line_ofs>cursor.line) - cursor.line_ofs=cursor.line; + if (cursor.line_ofs > cursor.line) + cursor.line_ofs = cursor.line; - int visible_width=cache.size.width-cache.style_normal->get_minimum_size().width-cache.line_number_w-cache.breakpoint_gutter_width; + int visible_width = cache.size.width - cache.style_normal->get_minimum_size().width - cache.line_number_w - cache.breakpoint_gutter_width; if (v_scroll->is_visible_in_tree()) - visible_width-=v_scroll->get_combined_minimum_size().width; - visible_width-=20; // give it a little more space - + visible_width -= v_scroll->get_combined_minimum_size().width; + visible_width -= 20; // give it a little more space //printf("rowofs %i, visrows %i, cursor.line %i\n",cursor.line_ofs,get_visible_rows(),cursor.line); int visible_rows = get_visible_rows(); if (h_scroll->is_visible_in_tree()) - visible_rows-=((h_scroll->get_combined_minimum_size().height-1)/get_row_height()); + visible_rows -= ((h_scroll->get_combined_minimum_size().height - 1) / get_row_height()); - if (cursor.line>=(cursor.line_ofs+visible_rows)) - cursor.line_ofs=cursor.line-visible_rows+1; - if (cursor.line<cursor.line_ofs) - cursor.line_ofs=cursor.line; + if (cursor.line >= (cursor.line_ofs + visible_rows)) + cursor.line_ofs = cursor.line - visible_rows + 1; + if (cursor.line < cursor.line_ofs) + cursor.line_ofs = cursor.line; - int cursor_x = get_column_x_offset( cursor.column, text[cursor.line] ); + int cursor_x = get_column_x_offset(cursor.column, text[cursor.line]); - if (cursor_x>(cursor.x_ofs+visible_width)) - cursor.x_ofs=cursor_x-visible_width+1; + if (cursor_x > (cursor.x_ofs + visible_width)) + cursor.x_ofs = cursor_x - visible_width + 1; if (cursor_x < cursor.x_ofs) - cursor.x_ofs=cursor_x; + cursor.x_ofs = cursor_x; update(); /* @@ -3079,98 +2959,89 @@ void TextEdit::adjust_viewport_to_cursor() { get_range()->set((int)cursor.line_ofs); */ - - } void TextEdit::center_viewport_to_cursor() { - if (cursor.line_ofs>cursor.line) - cursor.line_ofs=cursor.line; + if (cursor.line_ofs > cursor.line) + cursor.line_ofs = cursor.line; - int visible_width=cache.size.width-cache.style_normal->get_minimum_size().width-cache.line_number_w-cache.breakpoint_gutter_width; + int visible_width = cache.size.width - cache.style_normal->get_minimum_size().width - cache.line_number_w - cache.breakpoint_gutter_width; if (v_scroll->is_visible_in_tree()) - visible_width-=v_scroll->get_combined_minimum_size().width; - visible_width-=20; // give it a little more space + visible_width -= v_scroll->get_combined_minimum_size().width; + visible_width -= 20; // give it a little more space int visible_rows = get_visible_rows(); if (h_scroll->is_visible_in_tree()) - visible_rows-=((h_scroll->get_combined_minimum_size().height-1)/get_row_height()); + visible_rows -= ((h_scroll->get_combined_minimum_size().height - 1) / get_row_height()); - int max_ofs = text.size()-(scroll_past_end_of_file_enabled?1:visible_rows); - cursor.line_ofs=CLAMP(cursor.line-(visible_rows/2),0,max_ofs); + int max_ofs = text.size() - (scroll_past_end_of_file_enabled ? 1 : visible_rows); + cursor.line_ofs = CLAMP(cursor.line - (visible_rows / 2), 0, max_ofs); - int cursor_x = get_column_x_offset( cursor.column, text[cursor.line] ); + int cursor_x = get_column_x_offset(cursor.column, text[cursor.line]); - if (cursor_x>(cursor.x_ofs+visible_width)) - cursor.x_ofs=cursor_x-visible_width+1; + if (cursor_x > (cursor.x_ofs + visible_width)) + cursor.x_ofs = cursor_x - visible_width + 1; if (cursor_x < cursor.x_ofs) - cursor.x_ofs=cursor_x; + cursor.x_ofs = cursor_x; update(); } void TextEdit::cursor_set_column(int p_col, bool p_adjust_viewport) { - if (p_col<0) - p_col=0; + if (p_col < 0) + p_col = 0; - cursor.column=p_col; - if (cursor.column > get_line( cursor.line ).length()) - cursor.column=get_line( cursor.line ).length(); + cursor.column = p_col; + if (cursor.column > get_line(cursor.line).length()) + cursor.column = get_line(cursor.line).length(); - cursor.last_fit_x=get_column_x_offset(cursor.column,get_line(cursor.line)); + cursor.last_fit_x = get_column_x_offset(cursor.column, get_line(cursor.line)); if (p_adjust_viewport) adjust_viewport_to_cursor(); if (!cursor_changed_dirty) { if (is_inside_tree()) - MessageQueue::get_singleton()->push_call(this,"_cursor_changed_emit"); - cursor_changed_dirty=true; + MessageQueue::get_singleton()->push_call(this, "_cursor_changed_emit"); + cursor_changed_dirty = true; } - } - void TextEdit::cursor_set_line(int p_row, bool p_adjust_viewport) { if (setting_row) return; - setting_row=true; - if (p_row<0) - p_row=0; - + setting_row = true; + if (p_row < 0) + p_row = 0; - if (p_row>=(int)text.size()) - p_row=(int)text.size()-1; + if (p_row >= (int)text.size()) + p_row = (int)text.size() - 1; - cursor.line=p_row; - cursor.column=get_char_pos_for( cursor.last_fit_x, get_line( cursor.line) ); + cursor.line = p_row; + cursor.column = get_char_pos_for(cursor.last_fit_x, get_line(cursor.line)); if (p_adjust_viewport) adjust_viewport_to_cursor(); - setting_row=false; - + setting_row = false; if (!cursor_changed_dirty) { if (is_inside_tree()) - MessageQueue::get_singleton()->push_call(this,"_cursor_changed_emit"); - cursor_changed_dirty=true; + MessageQueue::get_singleton()->push_call(this, "_cursor_changed_emit"); + cursor_changed_dirty = true; } - } - int TextEdit::cursor_get_column() const { return cursor.column; } - int TextEdit::cursor_get_line() const { return cursor.line; @@ -3191,7 +3062,6 @@ void TextEdit::cursor_set_blink_enabled(const bool p_enabled) { draw_caret = true; } - float TextEdit::cursor_get_blink_speed() const { return caret_blink_timer->get_wait_time(); } @@ -3201,7 +3071,7 @@ void TextEdit::cursor_set_blink_speed(const float p_speed) { caret_blink_timer->set_wait_time(p_speed); } -void TextEdit::cursor_set_block_mode(const bool p_enable){ +void TextEdit::cursor_set_block_mode(const bool p_enable) { block_caret = p_enable; update(); } @@ -3210,134 +3080,125 @@ bool TextEdit::cursor_is_block_mode() const { return block_caret; } - void TextEdit::_scroll_moved(double p_to_val) { if (updating_scrolls) return; if (h_scroll->is_visible_in_tree()) - cursor.x_ofs=h_scroll->get_value(); + cursor.x_ofs = h_scroll->get_value(); if (v_scroll->is_visible_in_tree()) - cursor.line_ofs=v_scroll->get_value(); + cursor.line_ofs = v_scroll->get_value(); update(); } - - - - int TextEdit::get_row_height() const { - return cache.font->get_height()+cache.line_spacing; + return cache.font->get_height() + cache.line_spacing; } -int TextEdit::get_char_pos_for(int p_px,String p_str) const { +int TextEdit::get_char_pos_for(int p_px, String p_str) const { - int px=0; - int c=0; + int px = 0; + int c = 0; - int tab_w = cache.font->get_char_size(' ').width*tab_size; + int tab_w = cache.font->get_char_size(' ').width * tab_size; - while (c<p_str.length()) { + while (c < p_str.length()) { - int w=0; + int w = 0; - if (p_str[c]=='\t') { + if (p_str[c] == '\t') { - int left = px%tab_w; - if (left==0) - w=tab_w; + int left = px % tab_w; + if (left == 0) + w = tab_w; else - w=tab_w-px%tab_w; // is right... + w = tab_w - px % tab_w; // is right... } else { - w=cache.font->get_char_size(p_str[c],p_str[c+1]).width; + w = cache.font->get_char_size(p_str[c], p_str[c + 1]).width; } - if (p_px<(px+w/2)) + if (p_px < (px + w / 2)) break; - px+=w; + px += w; c++; } return c; } -int TextEdit::get_column_x_offset(int p_char,String p_str) { +int TextEdit::get_column_x_offset(int p_char, String p_str) { - int px=0; + int px = 0; - int tab_w = cache.font->get_char_size(' ').width*tab_size; + int tab_w = cache.font->get_char_size(' ').width * tab_size; - for (int i=0;i<p_char;i++) { + for (int i = 0; i < p_char; i++) { - if (i>=p_str.length()) + if (i >= p_str.length()) break; - if (p_str[i]=='\t') { + if (p_str[i] == '\t') { - int left = px%tab_w; - if (left==0) - px+=tab_w; + int left = px % tab_w; + if (left == 0) + px += tab_w; else - px+=tab_w-px%tab_w; // is right... + px += tab_w - px % tab_w; // is right... } else { - px+=cache.font->get_char_size(p_str[i],p_str[i+1]).width; + px += cache.font->get_char_size(p_str[i], p_str[i + 1]).width; } } return px; - } -void TextEdit::insert_text_at_cursor(const String& p_text) { +void TextEdit::insert_text_at_cursor(const String &p_text) { if (selection.active) { cursor_set_line(selection.from_line); cursor_set_column(selection.from_column); - _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column); - selection.active=false; - selection.selecting_mode=Selection::MODE_NONE; - + _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column); + selection.active = false; + selection.selecting_mode = Selection::MODE_NONE; } _insert_text_at_cursor(p_text); update(); - } -Control::CursorShape TextEdit::get_cursor_shape(const Point2& p_pos) const { - if (hilighted_word!=String()) +Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const { + if (hilighted_word != String()) return CURSOR_POINTING_HAND; - int gutter=cache.style_normal->get_margin(MARGIN_LEFT)+cache.line_number_w+cache.breakpoint_gutter_width; - if((completion_active && completion_rect.has_point(p_pos)) || p_pos.x < gutter) { + int gutter = cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width; + if ((completion_active && completion_rect.has_point(p_pos)) || p_pos.x < gutter) { return CURSOR_ARROW; } return CURSOR_IBEAM; } +void TextEdit::set_text(String p_text) { -void TextEdit::set_text(String p_text){ - - setting_text=true; + setting_text = true; clear(); _insert_text_at_cursor(p_text); clear_undo_history(); - cursor.column=0; - cursor.line=0; - cursor.x_ofs=0; - cursor.line_ofs=0; - cursor.last_fit_x=0; + cursor.column = 0; + cursor.line = 0; + cursor.x_ofs = 0; + cursor.line_ofs = 0; + cursor.last_fit_x = 0; cursor_set_line(0); cursor_set_column(0); update(); - setting_text=false; + setting_text = false; _text_changed_emit(); //get_range()->set(0); }; @@ -3345,117 +3206,102 @@ void TextEdit::set_text(String p_text){ String TextEdit::get_text() { String longthing; int len = text.size(); - for (int i=0;i<len;i++) { - + for (int i = 0; i < len; i++) { - longthing+=text[i]; - if (i!=len-1) - longthing+="\n"; + longthing += text[i]; + if (i != len - 1) + longthing += "\n"; } return longthing; - }; - String TextEdit::get_text_for_lookup_completion() { - - int row,col; - _get_mouse_pos(get_local_mouse_pos(), row,col); - + int row, col; + _get_mouse_pos(get_local_mouse_pos(), row, col); String longthing; int len = text.size(); - for (int i=0;i<len;i++) { + for (int i = 0; i < len; i++) { - if (i==row) { - longthing+=text[i].substr(0,col); - longthing+=String::chr(0xFFFF); //not unicode, represents the cursor - longthing+=text[i].substr(col,text[i].size()); + if (i == row) { + longthing += text[i].substr(0, col); + longthing += String::chr(0xFFFF); //not unicode, represents the cursor + longthing += text[i].substr(col, text[i].size()); } else { - longthing+=text[i]; + longthing += text[i]; } - - if (i!=len-1) - longthing+="\n"; + if (i != len - 1) + longthing += "\n"; } return longthing; - } String TextEdit::get_text_for_completion() { String longthing; int len = text.size(); - for (int i=0;i<len;i++) { + for (int i = 0; i < len; i++) { - if (i==cursor.line) { - longthing+=text[i].substr(0,cursor.column); - longthing+=String::chr(0xFFFF); //not unicode, represents the cursor - longthing+=text[i].substr(cursor.column,text[i].size()); + if (i == cursor.line) { + longthing += text[i].substr(0, cursor.column); + longthing += String::chr(0xFFFF); //not unicode, represents the cursor + longthing += text[i].substr(cursor.column, text[i].size()); } else { - longthing+=text[i]; + longthing += text[i]; } - - if (i!=len-1) - longthing+="\n"; + if (i != len - 1) + longthing += "\n"; } return longthing; - }; - String TextEdit::get_line(int line) const { - if (line<0 || line>=text.size()) + if (line < 0 || line >= text.size()) return ""; - return text[line]; - + return text[line]; }; void TextEdit::_clear() { clear_undo_history(); text.clear(); - cursor.column=0; - cursor.line=0; - cursor.x_ofs=0; - cursor.line_ofs=0; - cursor.last_fit_x=0; + cursor.column = 0; + cursor.line = 0; + cursor.x_ofs = 0; + cursor.line_ofs = 0; + cursor.last_fit_x = 0; } - - void TextEdit::clear() { - setting_text=true; + setting_text = true; _clear(); - setting_text=false; - + setting_text = false; }; void TextEdit::set_readonly(bool p_readonly) { - - readonly=p_readonly; + readonly = p_readonly; } void TextEdit::set_wrap(bool p_wrap) { - wrap=p_wrap; + wrap = p_wrap; } void TextEdit::set_max_chars(int p_max_chars) { - max_chars=p_max_chars; + max_chars = p_max_chars; } void TextEdit::_reset_caret_blink_timer() { @@ -3476,41 +3322,39 @@ void TextEdit::_toggle_draw_caret() { void TextEdit::_update_caches() { - cache.style_normal=get_stylebox("normal"); - cache.style_focus=get_stylebox("focus"); - cache.completion_background_color=get_color("completion_background_color"); - cache.completion_selected_color=get_color("completion_selected_color"); - cache.completion_existing_color=get_color("completion_existing_color"); - cache.completion_font_color=get_color("completion_font_color"); - cache.font=get_font("font"); - cache.caret_color=get_color("caret_color"); - cache.caret_background_color=get_color("caret_background_color"); - cache.line_number_color=get_color("line_number_color"); - cache.font_color=get_color("font_color"); - cache.font_selected_color=get_color("font_selected_color"); - cache.keyword_color=get_color("keyword_color"); - cache.function_color=get_color("function_color"); - cache.member_variable_color=get_color("member_variable_color"); - cache.number_color=get_color("number_color"); - cache.selection_color=get_color("selection_color"); - cache.mark_color=get_color("mark_color"); - cache.current_line_color=get_color("current_line_color"); - cache.line_length_guideline_color=get_color("line_length_guideline_color"); - cache.breakpoint_color=get_color("breakpoint_color"); - cache.brace_mismatch_color=get_color("brace_mismatch_color"); - cache.word_highlighted_color=get_color("word_highlighted_color"); - cache.search_result_color=get_color("search_result_color"); - cache.search_result_border_color=get_color("search_result_border_color"); - cache.symbol_color=get_color("symbol_color"); - cache.background_color=get_color("background_color"); - cache.line_spacing=get_constant("line_spacing"); + cache.style_normal = get_stylebox("normal"); + cache.style_focus = get_stylebox("focus"); + cache.completion_background_color = get_color("completion_background_color"); + cache.completion_selected_color = get_color("completion_selected_color"); + cache.completion_existing_color = get_color("completion_existing_color"); + cache.completion_font_color = get_color("completion_font_color"); + cache.font = get_font("font"); + cache.caret_color = get_color("caret_color"); + cache.caret_background_color = get_color("caret_background_color"); + cache.line_number_color = get_color("line_number_color"); + cache.font_color = get_color("font_color"); + cache.font_selected_color = get_color("font_selected_color"); + cache.keyword_color = get_color("keyword_color"); + cache.function_color = get_color("function_color"); + cache.member_variable_color = get_color("member_variable_color"); + cache.number_color = get_color("number_color"); + cache.selection_color = get_color("selection_color"); + cache.mark_color = get_color("mark_color"); + cache.current_line_color = get_color("current_line_color"); + cache.line_length_guideline_color = get_color("line_length_guideline_color"); + cache.breakpoint_color = get_color("breakpoint_color"); + cache.brace_mismatch_color = get_color("brace_mismatch_color"); + cache.word_highlighted_color = get_color("word_highlighted_color"); + cache.search_result_color = get_color("search_result_color"); + cache.search_result_border_color = get_color("search_result_border_color"); + cache.symbol_color = get_color("symbol_color"); + cache.background_color = get_color("background_color"); + cache.line_spacing = get_constant("line_spacing"); cache.row_height = cache.font->get_height() + cache.line_spacing; - cache.tab_icon=get_icon("tab"); + cache.tab_icon = get_icon("tab"); text.set_font(cache.font); - } - void TextEdit::clear_colors() { keywords.clear(); @@ -3518,24 +3362,22 @@ void TextEdit::clear_colors() { text.clear_caches(); } -void TextEdit::add_keyword_color(const String& p_keyword,const Color& p_color) { +void TextEdit::add_keyword_color(const String &p_keyword, const Color &p_color) { - keywords[p_keyword]=p_color; + keywords[p_keyword] = p_color; update(); - } -void TextEdit::add_color_region(const String& p_begin_key,const String& p_end_key,const Color &p_color,bool p_line_only) { +void TextEdit::add_color_region(const String &p_begin_key, const String &p_end_key, const Color &p_color, bool p_line_only) { - color_regions.push_back(ColorRegion(p_begin_key,p_end_key,p_color,p_line_only)); + color_regions.push_back(ColorRegion(p_begin_key, p_end_key, p_color, p_line_only)); text.clear_caches(); update(); - } void TextEdit::set_syntax_coloring(bool p_enabled) { - syntax_coloring=p_enabled; + syntax_coloring = p_enabled; update(); } @@ -3556,24 +3398,24 @@ void TextEdit::cut() { OS::get_singleton()->set_clipboard(clipboard); cursor_set_line(cursor.line); cursor_set_column(0); - _remove_text(cursor.line,0,cursor.line,text[cursor.line].length()); + _remove_text(cursor.line, 0, cursor.line, text[cursor.line].length()); backspace_at_cursor(); update(); - cursor_set_line(cursor.line+1); + cursor_set_line(cursor.line + 1); cut_copy_line = true; } else { - String clipboard = _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column); + String clipboard = _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column); OS::get_singleton()->set_clipboard(clipboard); cursor_set_line(selection.from_line); cursor_set_column(selection.from_column); - _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column); - selection.active=false; - selection.selecting_mode=Selection::MODE_NONE; + _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column); + selection.active = false; + selection.selecting_mode = Selection::MODE_NONE; update(); cut_copy_line = false; } @@ -3585,11 +3427,11 @@ void TextEdit::copy() { return; if (!selection.active) { - String clipboard = _base_get_text(cursor.line,0,cursor.line,text[cursor.line].length()); + String clipboard = _base_get_text(cursor.line, 0, cursor.line, text[cursor.line].length()); OS::get_singleton()->set_clipboard(clipboard); cut_copy_line = true; } else { - String clipboard = _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column); + String clipboard = _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column); OS::get_singleton()->set_clipboard(clipboard); cut_copy_line = false; } @@ -3601,16 +3443,16 @@ void TextEdit::paste() { if (selection.active) { - selection.active=false; - selection.selecting_mode=Selection::MODE_NONE; - _remove_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column); + selection.active = false; + selection.selecting_mode = Selection::MODE_NONE; + _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column); cursor_set_line(selection.from_line); cursor_set_column(selection.from_column); } else if (cut_copy_line) { cursor_set_column(0); - String ins="\n"; + String ins = "\n"; clipboard += ins; } @@ -3620,74 +3462,71 @@ void TextEdit::paste() { void TextEdit::select_all() { - if (text.size()==1 && text[0].length()==0) + if (text.size() == 1 && text[0].length() == 0) return; - selection.active=true; - selection.from_line=0; - selection.from_column=0; - selection.selecting_line=0; - selection.selecting_column=0; - selection.to_line=text.size()-1; - selection.to_column=text[selection.to_line].length(); - selection.selecting_mode=Selection::MODE_SHIFT; - selection.shiftclick_left=true; - cursor_set_line( selection.to_line, false ); - cursor_set_column( selection.to_column, false ); + selection.active = true; + selection.from_line = 0; + selection.from_column = 0; + selection.selecting_line = 0; + selection.selecting_column = 0; + selection.to_line = text.size() - 1; + selection.to_column = text[selection.to_line].length(); + selection.selecting_mode = Selection::MODE_SHIFT; + selection.shiftclick_left = true; + cursor_set_line(selection.to_line, false); + cursor_set_column(selection.to_column, false); update(); - } - void TextEdit::deselect() { - selection.active=false; + selection.active = false; update(); } -void TextEdit::select(int p_from_line,int p_from_column,int p_to_line,int p_to_column) { +void TextEdit::select(int p_from_line, int p_from_column, int p_to_line, int p_to_column) { - if (p_from_line>=text.size()) - p_from_line=text.size()-1; - if (p_from_column>=text[p_from_line].length()) - p_from_column=text[p_from_line].length(); + if (p_from_line >= text.size()) + p_from_line = text.size() - 1; + if (p_from_column >= text[p_from_line].length()) + p_from_column = text[p_from_line].length(); - if (p_to_line>=text.size()) - p_to_line=text.size()-1; - if (p_to_column>=text[p_to_line].length()) - p_to_column=text[p_to_line].length(); + if (p_to_line >= text.size()) + p_to_line = text.size() - 1; + if (p_to_column >= text[p_to_line].length()) + p_to_column = text[p_to_line].length(); - selection.from_line=p_from_line; - selection.from_column=p_from_column; - selection.to_line=p_to_line; - selection.to_column=p_to_column; + selection.from_line = p_from_line; + selection.from_column = p_from_column; + selection.to_line = p_to_line; + selection.to_column = p_to_column; - selection.active=true; + selection.active = true; - if (selection.from_line==selection.to_line) { + if (selection.from_line == selection.to_line) { - if (selection.from_column==selection.to_column) { + if (selection.from_column == selection.to_column) { - selection.active=false; + selection.active = false; - } else if (selection.from_column>selection.to_column) { + } else if (selection.from_column > selection.to_column) { selection.shiftclick_left = false; - SWAP( selection.from_column, selection.to_column ); + SWAP(selection.from_column, selection.to_column); } else { selection.shiftclick_left = true; } - } else if (selection.from_line>selection.to_line) { + } else if (selection.from_line > selection.to_line) { selection.shiftclick_left = false; - SWAP( selection.from_line, selection.to_line ); - SWAP( selection.from_column, selection.to_column ); + SWAP(selection.from_line, selection.to_line); + SWAP(selection.from_column, selection.to_column); } else { selection.shiftclick_left = true; } - update(); } @@ -3697,27 +3536,23 @@ bool TextEdit::is_selection_active() const { } int TextEdit::get_selection_from_line() const { - ERR_FAIL_COND_V(!selection.active,-1); + ERR_FAIL_COND_V(!selection.active, -1); return selection.from_line; - } int TextEdit::get_selection_from_column() const { - ERR_FAIL_COND_V(!selection.active,-1); + ERR_FAIL_COND_V(!selection.active, -1); return selection.from_column; - } int TextEdit::get_selection_to_line() const { - ERR_FAIL_COND_V(!selection.active,-1); + ERR_FAIL_COND_V(!selection.active, -1); return selection.to_line; - } int TextEdit::get_selection_to_column() const { - ERR_FAIL_COND_V(!selection.active,-1); + ERR_FAIL_COND_V(!selection.active, -1); return selection.to_column; - } String TextEdit::get_selection_text() const { @@ -3725,30 +3560,29 @@ String TextEdit::get_selection_text() const { if (!selection.active) return ""; - return _base_get_text(selection.from_line,selection.from_column,selection.to_line,selection.to_column); - + return _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column); } String TextEdit::get_word_under_cursor() const { int prev_cc = cursor.column; - while(prev_cc >0) { - bool is_char = _is_text_char(text[cursor.line][prev_cc-1]); + while (prev_cc > 0) { + bool is_char = _is_text_char(text[cursor.line][prev_cc - 1]); if (!is_char) break; --prev_cc; } int next_cc = cursor.column; - while(next_cc<text[cursor.line].length()) { + while (next_cc < text[cursor.line].length()) { bool is_char = _is_text_char(text[cursor.line][next_cc]); - if(!is_char) + if (!is_char) break; - ++ next_cc; + ++next_cc; } if (prev_cc == cursor.column || next_cc == cursor.column) return ""; - return text[cursor.line].substr(prev_cc, next_cc-prev_cc); + return text[cursor.line].substr(prev_cc, next_cc - prev_cc); } void TextEdit::set_search_text(const String &p_search_text) { @@ -3783,37 +3617,37 @@ int TextEdit::_get_column_pos_of_word(const String &p_key, const String &p_searc } while (col == -1 && p_from_column <= p_search.length()) { - if (p_search_flags&SEARCH_MATCH_CASE) { - col = p_search.find(p_key,p_from_column); + if (p_search_flags & SEARCH_MATCH_CASE) { + col = p_search.find(p_key, p_from_column); } else { - col = p_search.findn(p_key,p_from_column); + col = p_search.findn(p_key, p_from_column); } // whole words only - if (col != -1 && p_search_flags&SEARCH_WHOLE_WORDS) { - p_from_column=col; + if (col != -1 && p_search_flags & SEARCH_WHOLE_WORDS) { + p_from_column = col; - if (col > 0 && _is_text_char(p_search[col-1])) { + if (col > 0 && _is_text_char(p_search[col - 1])) { col = -1; - } else if ((col + p_key.length()) < p_search.length() && _is_text_char(p_search[col+p_key.length()])) { + } else if ((col + p_key.length()) < p_search.length() && _is_text_char(p_search[col + p_key.length()])) { col = -1; } } - p_from_column+=1; + p_from_column += 1; } } return col; } -PoolVector<int> TextEdit::_search_bind(const String &p_key,uint32_t p_search_flags, int p_from_line,int p_from_column) const { +PoolVector<int> TextEdit::_search_bind(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column) const { - int col,line; - if (search(p_key,p_search_flags,p_from_line,p_from_column,col,line)) { + int col, line; + if (search(p_key, p_search_flags, p_from_line, p_from_column, col, line)) { PoolVector<int> result; result.resize(2); - result.set(0,line); - result.set(1,col); + result.set(0, line); + result.set(1, col); return result; } else { @@ -3822,108 +3656,104 @@ PoolVector<int> TextEdit::_search_bind(const String &p_key,uint32_t p_search_fla } } -bool TextEdit::search(const String &p_key,uint32_t p_search_flags, int p_from_line, int p_from_column,int &r_line,int &r_column) const { +bool TextEdit::search(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column, int &r_line, int &r_column) const { - if (p_key.length()==0) + if (p_key.length() == 0) return false; - ERR_FAIL_INDEX_V(p_from_line,text.size(),false); - ERR_FAIL_INDEX_V(p_from_column,text[p_from_line].length()+1,false); + ERR_FAIL_INDEX_V(p_from_line, text.size(), false); + ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, false); //search through the whole documment, but start by current line - int line=-1; - int pos=-1; + int line = -1; + int pos = -1; - line=p_from_line; + line = p_from_line; - for(int i=0;i<text.size()+1;i++) { + for (int i = 0; i < text.size() + 1; i++) { //backwards is broken... //int idx=(p_search_flags&SEARCH_BACKWARDS)?(text.size()-i):i; //do backwards seearch - - if (line<0) { - line=text.size()-1; + if (line < 0) { + line = text.size() - 1; } - if (line==text.size()) { - line=0; + if (line == text.size()) { + line = 0; } String text_line = text[line]; - int from_column=0; - if (line==p_from_line) { + int from_column = 0; + if (line == p_from_line) { - if (i==text.size()) { + if (i == text.size()) { //wrapped - if (p_search_flags&SEARCH_BACKWARDS) { - from_column=text_line.length(); + if (p_search_flags & SEARCH_BACKWARDS) { + from_column = text_line.length(); } else { - from_column=0; + from_column = 0; } } else { - from_column=p_from_column; + from_column = p_from_column; } - } else { - if (p_search_flags&SEARCH_BACKWARDS) - from_column=text_line.length()-1; + if (p_search_flags & SEARCH_BACKWARDS) + from_column = text_line.length() - 1; else - from_column=0; + from_column = 0; } - pos=-1; + pos = -1; - int pos_from=0; - int last_pos=-1; - while ((last_pos=(p_search_flags&SEARCH_MATCH_CASE)?text_line.find(p_key,pos_from):text_line.findn(p_key,pos_from))!=-1) { + int pos_from = 0; + int last_pos = -1; + while ((last_pos = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.find(p_key, pos_from) : text_line.findn(p_key, pos_from)) != -1) { - if (p_search_flags&SEARCH_BACKWARDS) { + if (p_search_flags & SEARCH_BACKWARDS) { - if (last_pos>from_column) + if (last_pos > from_column) break; - pos=last_pos; + pos = last_pos; } else { - if (last_pos>=from_column) { - pos=last_pos; + if (last_pos >= from_column) { + pos = last_pos; break; } } - pos_from=last_pos+p_key.length(); + pos_from = last_pos + p_key.length(); } - if (pos!=-1 && (p_search_flags&SEARCH_WHOLE_WORDS)) { + if (pos != -1 && (p_search_flags & SEARCH_WHOLE_WORDS)) { //validate for whole words - if (pos>0 && _is_text_char(text_line[pos-1])) - pos=-1; - else if (_is_text_char(text_line[pos+p_key.length()])) - pos=-1; + if (pos > 0 && _is_text_char(text_line[pos - 1])) + pos = -1; + else if (_is_text_char(text_line[pos + p_key.length()])) + pos = -1; } - if (pos!=-1) + if (pos != -1) break; - if (p_search_flags&SEARCH_BACKWARDS) + if (p_search_flags & SEARCH_BACKWARDS) line--; else line++; - } - if (pos==-1) { - r_line=-1; - r_column=-1; + if (pos == -1) { + r_line = -1; + r_column = -1; return false; } - r_line=line; - r_column=pos; - + r_line = line; + r_column = pos; return true; } @@ -3931,40 +3761,38 @@ bool TextEdit::search(const String &p_key,uint32_t p_search_flags, int p_from_li void TextEdit::_cursor_changed_emit() { emit_signal("cursor_changed"); - cursor_changed_dirty=false; + cursor_changed_dirty = false; } void TextEdit::_text_changed_emit() { emit_signal("text_changed"); - text_changed_dirty=false; + text_changed_dirty = false; } -void TextEdit::set_line_as_marked(int p_line,bool p_marked) { +void TextEdit::set_line_as_marked(int p_line, bool p_marked) { - ERR_FAIL_INDEX(p_line,text.size()); - text.set_marked(p_line,p_marked); + ERR_FAIL_INDEX(p_line, text.size()); + text.set_marked(p_line, p_marked); update(); } bool TextEdit::is_line_set_as_breakpoint(int p_line) const { - ERR_FAIL_INDEX_V(p_line,text.size(),false); + ERR_FAIL_INDEX_V(p_line, text.size(), false); return text.is_breakpoint(p_line); - } -void TextEdit::set_line_as_breakpoint(int p_line,bool p_breakpoint) { - +void TextEdit::set_line_as_breakpoint(int p_line, bool p_breakpoint) { - ERR_FAIL_INDEX(p_line,text.size()); - text.set_breakpoint(p_line,p_breakpoint); + ERR_FAIL_INDEX(p_line, text.size()); + text.set_breakpoint(p_line, p_breakpoint); update(); } void TextEdit::get_breakpoints(List<int> *p_breakpoints) const { - for(int i=0;i<text.size();i++) { + for (int i = 0; i < text.size(); i++) { if (text.is_breakpoint(i)) p_breakpoints->push_back(i); } @@ -3975,64 +3803,62 @@ int TextEdit::get_line_count() const { return text.size(); } -void TextEdit::_do_text_op(const TextOperation& p_op, bool p_reverse) { +void TextEdit::_do_text_op(const TextOperation &p_op, bool p_reverse) { - ERR_FAIL_COND(p_op.type==TextOperation::TYPE_NONE); + ERR_FAIL_COND(p_op.type == TextOperation::TYPE_NONE); - bool insert = p_op.type==TextOperation::TYPE_INSERT; + bool insert = p_op.type == TextOperation::TYPE_INSERT; if (p_reverse) - insert=!insert; + insert = !insert; if (insert) { int check_line; int check_column; - _base_insert_text(p_op.from_line,p_op.from_column,p_op.text,check_line,check_column); - ERR_FAIL_COND( check_line != p_op.to_line ); // BUG - ERR_FAIL_COND( check_column != p_op.to_column ); // BUG + _base_insert_text(p_op.from_line, p_op.from_column, p_op.text, check_line, check_column); + ERR_FAIL_COND(check_line != p_op.to_line); // BUG + ERR_FAIL_COND(check_column != p_op.to_column); // BUG } else { - _base_remove_text(p_op.from_line,p_op.from_column,p_op.to_line,p_op.to_column); + _base_remove_text(p_op.from_line, p_op.from_column, p_op.to_line, p_op.to_column); } - } void TextEdit::_clear_redo() { - if (undo_stack_pos==NULL) + if (undo_stack_pos == NULL) return; //nothing to clear _push_current_op(); - while (undo_stack_pos) { + while (undo_stack_pos) { List<TextOperation>::Element *elem = undo_stack_pos; - undo_stack_pos=undo_stack_pos->next(); + undo_stack_pos = undo_stack_pos->next(); undo_stack.erase(elem); } } - void TextEdit::undo() { _push_current_op(); - if (undo_stack_pos==NULL) { + if (undo_stack_pos == NULL) { if (!undo_stack.size()) return; //nothing to undo - undo_stack_pos=undo_stack.back(); + undo_stack_pos = undo_stack.back(); - } else if (undo_stack_pos==undo_stack.front()) + } else if (undo_stack_pos == undo_stack.front()) return; // at the bottom of the undo stack else - undo_stack_pos=undo_stack_pos->prev(); + undo_stack_pos = undo_stack_pos->prev(); TextOperation op = undo_stack_pos->get(); _do_text_op(op, true); - current_op.version=op.prev_version; - if(undo_stack_pos->get().chain_backward) { - while(true) { + current_op.version = op.prev_version; + if (undo_stack_pos->get().chain_backward) { + while (true) { ERR_BREAK(!undo_stack_pos->prev()); undo_stack_pos = undo_stack_pos->prev(); op = undo_stack_pos->get(); @@ -4059,17 +3885,17 @@ void TextEdit::redo() { _push_current_op(); - if (undo_stack_pos==NULL) + if (undo_stack_pos == NULL) return; //nothing to do. TextOperation op = undo_stack_pos->get(); _do_text_op(op, false); current_op.version = op.version; - if(undo_stack_pos->get().chain_forward) { + if (undo_stack_pos->get().chain_forward) { - while(true) { + while (true) { ERR_BREAK(!undo_stack_pos->next()); - undo_stack_pos=undo_stack_pos->next(); + undo_stack_pos = undo_stack_pos->next(); op = undo_stack_pos->get(); _do_text_op(op, false); current_op.version = op.version; @@ -4079,22 +3905,21 @@ void TextEdit::redo() { } cursor_set_line(undo_stack_pos->get().to_line); cursor_set_column(undo_stack_pos->get().to_column); - undo_stack_pos=undo_stack_pos->next(); + undo_stack_pos = undo_stack_pos->next(); update(); } void TextEdit::clear_undo_history() { - saved_version=0; - current_op.type=TextOperation::TYPE_NONE; - undo_stack_pos=NULL; + saved_version = 0; + current_op.type = TextOperation::TYPE_NONE; + undo_stack_pos = NULL; undo_stack.clear(); - } void TextEdit::begin_complex_operation() { _push_current_op(); - next_operation_is_complex=true; + next_operation_is_complex = true; } void TextEdit::end_complex_operation() { @@ -4102,29 +3927,28 @@ void TextEdit::end_complex_operation() { _push_current_op(); ERR_FAIL_COND(undo_stack.size() == 0); - if(undo_stack.back()->get().chain_forward) { - undo_stack.back()->get().chain_forward=false; + if (undo_stack.back()->get().chain_forward) { + undo_stack.back()->get().chain_forward = false; return; } - undo_stack.back()->get().chain_backward=true; + undo_stack.back()->get().chain_backward = true; } void TextEdit::_push_current_op() { - if (current_op.type==TextOperation::TYPE_NONE) + if (current_op.type == TextOperation::TYPE_NONE) return; // do nothing - if(next_operation_is_complex) { - current_op.chain_forward=true; - next_operation_is_complex=false; + if (next_operation_is_complex) { + current_op.chain_forward = true; + next_operation_is_complex = false; } undo_stack.push_back(current_op); - current_op.type=TextOperation::TYPE_NONE; - current_op.text=""; - current_op.chain_forward=false; - + current_op.type = TextOperation::TYPE_NONE; + current_op.text = ""; + current_op.chain_forward = false; } void TextEdit::set_tab_size(const int p_size) { @@ -4136,10 +3960,10 @@ void TextEdit::set_tab_size(const int p_size) { void TextEdit::set_draw_tabs(bool p_draw) { - draw_tabs=p_draw; + draw_tabs = p_draw; } -bool TextEdit::is_drawing_tabs() const{ +bool TextEdit::is_drawing_tabs() const { return draw_tabs; } @@ -4162,7 +3986,7 @@ uint32_t TextEdit::get_saved_version() const { } void TextEdit::tag_saved_version() { - saved_version=get_version(); + saved_version = get_version(); } int TextEdit::get_v_scroll() const { @@ -4172,7 +3996,7 @@ int TextEdit::get_v_scroll() const { void TextEdit::set_v_scroll(int p_scroll) { v_scroll->set_value(p_scroll); - cursor.line_ofs=p_scroll; + cursor.line_ofs = p_scroll; } int TextEdit::get_h_scroll() const { @@ -4184,11 +4008,11 @@ void TextEdit::set_h_scroll(int p_scroll) { h_scroll->set_value(p_scroll); } -void TextEdit::set_completion(bool p_enabled,const Vector<String>& p_prefixes) { +void TextEdit::set_completion(bool p_enabled, const Vector<String> &p_prefixes) { completion_prefixes.clear(); - completion_enabled=p_enabled; - for(int i=0;i<p_prefixes.size();i++) + completion_enabled = p_enabled; + for (int i = 0; i < p_prefixes.size(); i++) completion_prefixes.insert(p_prefixes[i]); } @@ -4210,12 +4034,11 @@ void TextEdit::_confirm_completion() { _cancel_completion(); } - void TextEdit::_cancel_code_hint() { VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 0); raised_from_completion = false; - completion_hint=""; + completion_hint = ""; update(); } @@ -4226,94 +4049,91 @@ void TextEdit::_cancel_completion() { if (!completion_active) return; - completion_active=false; + completion_active = false; update(); - } static bool _is_completable(CharType c) { - return !_is_symbol(c) || c=='"' || c=='\''; + return !_is_symbol(c) || c == '"' || c == '\''; } - void TextEdit::_update_completion_candidates() { String l = text[cursor.line]; - int cofs = CLAMP(cursor.column,0,l.length()); + int cofs = CLAMP(cursor.column, 0, l.length()); String s; //look for keywords first - bool inquote=false; - int first_quote=-1; + bool inquote = false; + int first_quote = -1; - int c=cofs-1; - while(c>=0) { - if (l[c]=='"' || l[c]=='\'') { - inquote=!inquote; - if (first_quote==-1) - first_quote=c; + int c = cofs - 1; + while (c >= 0) { + if (l[c] == '"' || l[c] == '\'') { + inquote = !inquote; + if (first_quote == -1) + first_quote = c; } c--; } - bool pre_keyword=false; - bool cancel=false; + bool pre_keyword = false; + bool cancel = false; //print_line("inquote: "+itos(inquote)+"first quote "+itos(first_quote)+" cofs-1 "+itos(cofs-1)); - if (!inquote && first_quote==cofs-1) { + if (!inquote && first_quote == cofs - 1) { //no completion here //print_line("cancel!"); - cancel=true; - } else if (inquote && first_quote!=-1) { + cancel = true; + } else if (inquote && first_quote != -1) { - s=l.substr(first_quote,cofs-first_quote); + s = l.substr(first_quote, cofs - first_quote); //print_line("s: 1"+s); - } else if (cofs>0 && l[cofs-1]==' ') { - int kofs=cofs-1; + } else if (cofs > 0 && l[cofs - 1] == ' ') { + int kofs = cofs - 1; String kw; - while (kofs>=0 && l[kofs]==' ') + while (kofs >= 0 && l[kofs] == ' ') kofs--; - while(kofs>=0 && l[kofs]>32 && _is_completable(l[kofs])) { - kw=String::chr(l[kofs])+kw; + while (kofs >= 0 && l[kofs] > 32 && _is_completable(l[kofs])) { + kw = String::chr(l[kofs]) + kw; kofs--; } - pre_keyword=keywords.has(kw); + pre_keyword = keywords.has(kw); //print_line("KW "+kw+"? "+itos(pre_keyword)); } else { - - while(cofs>0 && l[cofs-1]>32 && _is_completable(l[cofs-1])) { - s=String::chr(l[cofs-1])+s; - if (l[cofs-1]=='\'' || l[cofs-1]=='"' || l[cofs-1]=='$') + while (cofs > 0 && l[cofs - 1] > 32 && _is_completable(l[cofs - 1])) { + s = String::chr(l[cofs - 1]) + s; + if (l[cofs - 1] == '\'' || l[cofs - 1] == '"' || l[cofs - 1] == '$') break; cofs--; } } - if (cursor.column > 0 && l[cursor.column - 1] == '(' && !pre_keyword && !completion_strings[0].begins_with("\"")) { + if (cursor.column > 0 && l[cursor.column - 1] == '(' && !pre_keyword && !completion_strings[0].begins_with("\"")) { cancel = true; } update(); - if (cancel || (!pre_keyword && s=="" && (cofs==0 || !completion_prefixes.has(String::chr(l[cofs-1]))))) { + if (cancel || (!pre_keyword && s == "" && (cofs == 0 || !completion_prefixes.has(String::chr(l[cofs - 1]))))) { //none to complete, cancel _cancel_completion(); return; } completion_options.clear(); - completion_index=0; - completion_base=s; + completion_index = 0; + completion_base = s; Vector<float> sim_cache; - for(int i=0;i<completion_strings.size();i++) { + for (int i = 0; i < completion_strings.size(); i++) { if (s == completion_strings[i]) { // A perfect match, stop completion _cancel_completion(); @@ -4321,7 +4141,7 @@ void TextEdit::_update_completion_candidates() { } if (s.is_subsequence_ofi(completion_strings[i])) { - // don't remove duplicates if no input is provided + // don't remove duplicates if no input is provided if (s != "" && completion_options.find(completion_strings[i]) != -1) { continue; } @@ -4344,7 +4164,7 @@ void TextEdit::_update_completion_candidates() { int pos = 0; do { comp_sim = sim_cache[pos++]; - } while(pos < comp_size && similarity < comp_sim); + } while (pos < comp_size && similarity < comp_sim); pos = similarity > comp_sim ? pos - 1 : pos; // Pos will be off by one completion_options.insert(pos, completion_strings[i]); sim_cache.insert(pos, similarity); @@ -4352,20 +4172,17 @@ void TextEdit::_update_completion_candidates() { } } - - - if (completion_options.size()==0) { + if (completion_options.size() == 0) { //no options to complete, cancel _cancel_completion(); return; - } // The top of the list is the best match - completion_current=completion_options[0]; + completion_current = completion_options[0]; -#if 0 // even there's only one option, user still get the chance to choose using it or not +#if 0 // even there's only one option, user still get the chance to choose using it or not if (completion_options.size()==1) { //one option to complete, just complete it automagically _confirm_completion(); @@ -4376,37 +4193,33 @@ void TextEdit::_update_completion_candidates() { } #endif - completion_enabled=true; + completion_enabled = true; } - - void TextEdit::query_code_comple() { String l = text[cursor.line]; - int ofs = CLAMP(cursor.column,0,l.length()); + int ofs = CLAMP(cursor.column, 0, l.length()); - bool inquote=false; + bool inquote = false; - int c=ofs-1; - while(c>=0) { - if (l[c]=='"' || l[c]=='\'') - inquote=!inquote; + int c = ofs - 1; + while (c >= 0) { + if (l[c] == '"' || l[c] == '\'') + inquote = !inquote; c--; } - if (ofs>0 && (inquote || _is_completable(l[ofs-1]) || completion_prefixes.has(String::chr(l[ofs-1])))) + if (ofs > 0 && (inquote || _is_completable(l[ofs - 1]) || completion_prefixes.has(String::chr(l[ofs - 1])))) emit_signal("request_completion"); - } - -void TextEdit::set_code_hint(const String& p_hint) { +void TextEdit::set_code_hint(const String &p_hint) { VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 1); raised_from_completion = true; - completion_hint=p_hint; - completion_hint_offset=-0xFFFF; + completion_hint = p_hint; + completion_hint_offset = -0xFFFF; update(); } @@ -4414,119 +4227,112 @@ void TextEdit::code_complete(const Vector<String> &p_strings) { VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 1); raised_from_completion = true; - completion_strings=p_strings; - completion_active=true; - completion_current=""; - completion_index=0; + completion_strings = p_strings; + completion_active = true; + completion_current = ""; + completion_index = 0; _update_completion_candidates(); // } +String TextEdit::get_word_at_pos(const Vector2 &p_pos) const { -String TextEdit::get_word_at_pos(const Vector2& p_pos) const { - - int row,col; + int row, col; _get_mouse_pos(p_pos, row, col); String s = text[row]; - if (s.length()==0) + if (s.length() == 0) return ""; - int beg=CLAMP(col,0,s.length()); - int end=beg; + int beg = CLAMP(col, 0, s.length()); + int end = beg; + if (s[beg] > 32 || beg == s.length()) { - if (s[beg]>32 || beg==s.length()) { + bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this - bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this - - while(beg>0 && s[beg-1]>32 && (symbol==_is_symbol(s[beg-1]))) { + while (beg > 0 && s[beg - 1] > 32 && (symbol == _is_symbol(s[beg - 1]))) { beg--; } - while(end<s.length() && s[end+1]>32 && (symbol==_is_symbol(s[end+1]))) { + while (end < s.length() && s[end + 1] > 32 && (symbol == _is_symbol(s[end + 1]))) { end++; } - if (end<s.length()) - end+=1; + if (end < s.length()) + end += 1; - return s.substr(beg,end-beg); + return s.substr(beg, end - beg); } return String(); } -String TextEdit::get_tooltip(const Point2& p_pos) const { +String TextEdit::get_tooltip(const Point2 &p_pos) const { if (!tooltip_obj) return Control::get_tooltip(p_pos); - int row,col; + int row, col; _get_mouse_pos(p_pos, row, col); String s = text[row]; - if (s.length()==0) + if (s.length() == 0) return Control::get_tooltip(p_pos); - int beg=CLAMP(col,0,s.length()); - int end=beg; - + int beg = CLAMP(col, 0, s.length()); + int end = beg; - if (s[beg]>32 || beg==s.length()) { + if (s[beg] > 32 || beg == s.length()) { - bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this + bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this - while(beg>0 && s[beg-1]>32 && (symbol==_is_symbol(s[beg-1]))) { + while (beg > 0 && s[beg - 1] > 32 && (symbol == _is_symbol(s[beg - 1]))) { beg--; } - while(end<s.length() && s[end+1]>32 && (symbol==_is_symbol(s[end+1]))) { + while (end < s.length() && s[end + 1] > 32 && (symbol == _is_symbol(s[end + 1]))) { end++; } - if (end<s.length()) - end+=1; + if (end < s.length()) + end += 1; - String tt = tooltip_obj->call(tooltip_func,s.substr(beg,end-beg),tooltip_ud); + String tt = tooltip_obj->call(tooltip_func, s.substr(beg, end - beg), tooltip_ud); return tt; - } return Control::get_tooltip(p_pos); - } -void TextEdit::set_tooltip_request_func(Object *p_obj, const StringName& p_function,const Variant& p_udata) { +void TextEdit::set_tooltip_request_func(Object *p_obj, const StringName &p_function, const Variant &p_udata) { - tooltip_obj=p_obj; - tooltip_func=p_function; - tooltip_ud=p_udata; + tooltip_obj = p_obj; + tooltip_func = p_function; + tooltip_ud = p_udata; } -void TextEdit::set_line(int line, String new_text) -{ +void TextEdit::set_line(int line, String new_text) { if (line < 0 || line > text.size()) return; _remove_text(line, 0, line, text[line].length()); _insert_text(line, 0, new_text); - if (cursor.line==line) { - cursor.column=MIN(cursor.column,new_text.length()); + if (cursor.line == line) { + cursor.column = MIN(cursor.column, new_text.length()); } } -void TextEdit::insert_at(const String &p_text, int at) -{ +void TextEdit::insert_at(const String &p_text, int at) { cursor_set_column(0); cursor_set_line(at); - _insert_text(at, 0, p_text+"\n"); + _insert_text(at, 0, p_text + "\n"); } void TextEdit::set_show_line_numbers(bool p_show) { - line_numbers=p_show; + line_numbers = p_show; update(); } void TextEdit::set_line_numbers_zero_padded(bool p_zero_padded) { - line_numbers_zero_padded=p_zero_padded; + line_numbers_zero_padded = p_zero_padded; update(); } @@ -4535,12 +4341,12 @@ bool TextEdit::is_show_line_numbers_enabled() const { } void TextEdit::set_show_line_length_guideline(bool p_show) { - line_length_guideline=p_show; + line_length_guideline = p_show; update(); } void TextEdit::set_line_length_guideline_column(int p_column) { - line_length_guideline_col=p_column; + line_length_guideline_col = p_column; update(); } @@ -4564,12 +4370,12 @@ int TextEdit::get_breakpoint_gutter_width() const { bool TextEdit::is_text_field() const { - return true; + return true; } void TextEdit::menu_option(int p_option) { - switch( p_option ) { + switch (p_option) { case MENU_CUT: { if (!readonly) { cut(); @@ -4594,14 +4400,12 @@ void TextEdit::menu_option(int p_option) { case MENU_UNDO: { undo(); } break; - }; } - void TextEdit::set_select_identifiers_on_hover(bool p_enable) { - select_identifiers_enabled=p_enable; + select_identifiers_enabled = p_enable; } bool TextEdit::is_selecting_identifiers_on_hover_enabled() const { @@ -4619,65 +4423,64 @@ PopupMenu *TextEdit::get_menu() const { void TextEdit::_bind_methods() { + ClassDB::bind_method(D_METHOD("_gui_input"), &TextEdit::_gui_input); + ClassDB::bind_method(D_METHOD("_scroll_moved"), &TextEdit::_scroll_moved); + ClassDB::bind_method(D_METHOD("_cursor_changed_emit"), &TextEdit::_cursor_changed_emit); + ClassDB::bind_method(D_METHOD("_text_changed_emit"), &TextEdit::_text_changed_emit); + ClassDB::bind_method(D_METHOD("_push_current_op"), &TextEdit::_push_current_op); + ClassDB::bind_method(D_METHOD("_click_selection_held"), &TextEdit::_click_selection_held); + ClassDB::bind_method(D_METHOD("_toggle_draw_caret"), &TextEdit::_toggle_draw_caret); - ClassDB::bind_method(D_METHOD("_gui_input"),&TextEdit::_gui_input); - ClassDB::bind_method(D_METHOD("_scroll_moved"),&TextEdit::_scroll_moved); - ClassDB::bind_method(D_METHOD("_cursor_changed_emit"),&TextEdit::_cursor_changed_emit); - ClassDB::bind_method(D_METHOD("_text_changed_emit"),&TextEdit::_text_changed_emit); - ClassDB::bind_method(D_METHOD("_push_current_op"),&TextEdit::_push_current_op); - ClassDB::bind_method(D_METHOD("_click_selection_held"),&TextEdit::_click_selection_held); - ClassDB::bind_method(D_METHOD("_toggle_draw_caret"),&TextEdit::_toggle_draw_caret); - - BIND_CONSTANT( SEARCH_MATCH_CASE ); - BIND_CONSTANT( SEARCH_WHOLE_WORDS ); - BIND_CONSTANT( SEARCH_BACKWARDS ); + BIND_CONSTANT(SEARCH_MATCH_CASE); + BIND_CONSTANT(SEARCH_WHOLE_WORDS); + BIND_CONSTANT(SEARCH_BACKWARDS); /* ClassDB::bind_method(D_METHOD("delete_char"),&TextEdit::delete_char); ClassDB::bind_method(D_METHOD("delete_line"),&TextEdit::delete_line); */ - ClassDB::bind_method(D_METHOD("set_text","text"),&TextEdit::set_text); - ClassDB::bind_method(D_METHOD("insert_text_at_cursor","text"),&TextEdit::insert_text_at_cursor); + ClassDB::bind_method(D_METHOD("set_text", "text"), &TextEdit::set_text); + ClassDB::bind_method(D_METHOD("insert_text_at_cursor", "text"), &TextEdit::insert_text_at_cursor); - ClassDB::bind_method(D_METHOD("get_line_count"),&TextEdit::get_line_count); - ClassDB::bind_method(D_METHOD("get_text"),&TextEdit::get_text); - ClassDB::bind_method(D_METHOD("get_line","line"),&TextEdit::get_line); + ClassDB::bind_method(D_METHOD("get_line_count"), &TextEdit::get_line_count); + ClassDB::bind_method(D_METHOD("get_text"), &TextEdit::get_text); + ClassDB::bind_method(D_METHOD("get_line", "line"), &TextEdit::get_line); - ClassDB::bind_method(D_METHOD("cursor_set_column","column","adjust_viewport"),&TextEdit::cursor_set_column,DEFVAL(false)); - ClassDB::bind_method(D_METHOD("cursor_set_line","line","adjust_viewport"),&TextEdit::cursor_set_line,DEFVAL(false)); + ClassDB::bind_method(D_METHOD("cursor_set_column", "column", "adjust_viewport"), &TextEdit::cursor_set_column, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("cursor_set_line", "line", "adjust_viewport"), &TextEdit::cursor_set_line, DEFVAL(false)); - ClassDB::bind_method(D_METHOD("cursor_get_column"),&TextEdit::cursor_get_column); - ClassDB::bind_method(D_METHOD("cursor_get_line"),&TextEdit::cursor_get_line); - ClassDB::bind_method(D_METHOD("cursor_set_blink_enabled", "enable"),&TextEdit::cursor_set_blink_enabled); - ClassDB::bind_method(D_METHOD("cursor_get_blink_enabled"),&TextEdit::cursor_get_blink_enabled); - ClassDB::bind_method(D_METHOD("cursor_set_blink_speed", "blink_speed"),&TextEdit::cursor_set_blink_speed); - ClassDB::bind_method(D_METHOD("cursor_get_blink_speed"),&TextEdit::cursor_get_blink_speed); + ClassDB::bind_method(D_METHOD("cursor_get_column"), &TextEdit::cursor_get_column); + ClassDB::bind_method(D_METHOD("cursor_get_line"), &TextEdit::cursor_get_line); + ClassDB::bind_method(D_METHOD("cursor_set_blink_enabled", "enable"), &TextEdit::cursor_set_blink_enabled); + ClassDB::bind_method(D_METHOD("cursor_get_blink_enabled"), &TextEdit::cursor_get_blink_enabled); + ClassDB::bind_method(D_METHOD("cursor_set_blink_speed", "blink_speed"), &TextEdit::cursor_set_blink_speed); + ClassDB::bind_method(D_METHOD("cursor_get_blink_speed"), &TextEdit::cursor_get_blink_speed); ClassDB::bind_method(D_METHOD("cursor_set_block_mode", "enable"), &TextEdit::cursor_set_block_mode); ClassDB::bind_method(D_METHOD("cursor_is_block_mode"), &TextEdit::cursor_is_block_mode); - ClassDB::bind_method(D_METHOD("set_readonly","enable"),&TextEdit::set_readonly); - ClassDB::bind_method(D_METHOD("set_wrap","enable"),&TextEdit::set_wrap); - ClassDB::bind_method(D_METHOD("set_max_chars","amount"),&TextEdit::set_max_chars); - - ClassDB::bind_method(D_METHOD("cut"),&TextEdit::cut); - ClassDB::bind_method(D_METHOD("copy"),&TextEdit::copy); - ClassDB::bind_method(D_METHOD("paste"),&TextEdit::paste); - ClassDB::bind_method(D_METHOD("select_all"),&TextEdit::select_all); - ClassDB::bind_method(D_METHOD("select","from_line","from_column","to_line","to_column"),&TextEdit::select); - - ClassDB::bind_method(D_METHOD("is_selection_active"),&TextEdit::is_selection_active); - ClassDB::bind_method(D_METHOD("get_selection_from_line"),&TextEdit::get_selection_from_line); - ClassDB::bind_method(D_METHOD("get_selection_from_column"),&TextEdit::get_selection_from_column); - ClassDB::bind_method(D_METHOD("get_selection_to_line"),&TextEdit::get_selection_to_line); - ClassDB::bind_method(D_METHOD("get_selection_to_column"),&TextEdit::get_selection_to_column); - ClassDB::bind_method(D_METHOD("get_selection_text"),&TextEdit::get_selection_text); - ClassDB::bind_method(D_METHOD("get_word_under_cursor"),&TextEdit::get_word_under_cursor); - ClassDB::bind_method(D_METHOD("search","flags","from_line","from_column","to_line","to_column"),&TextEdit::_search_bind); - - ClassDB::bind_method(D_METHOD("undo"),&TextEdit::undo); - ClassDB::bind_method(D_METHOD("redo"),&TextEdit::redo); - ClassDB::bind_method(D_METHOD("clear_undo_history"),&TextEdit::clear_undo_history); + ClassDB::bind_method(D_METHOD("set_readonly", "enable"), &TextEdit::set_readonly); + ClassDB::bind_method(D_METHOD("set_wrap", "enable"), &TextEdit::set_wrap); + ClassDB::bind_method(D_METHOD("set_max_chars", "amount"), &TextEdit::set_max_chars); + + ClassDB::bind_method(D_METHOD("cut"), &TextEdit::cut); + ClassDB::bind_method(D_METHOD("copy"), &TextEdit::copy); + ClassDB::bind_method(D_METHOD("paste"), &TextEdit::paste); + ClassDB::bind_method(D_METHOD("select_all"), &TextEdit::select_all); + ClassDB::bind_method(D_METHOD("select", "from_line", "from_column", "to_line", "to_column"), &TextEdit::select); + + ClassDB::bind_method(D_METHOD("is_selection_active"), &TextEdit::is_selection_active); + ClassDB::bind_method(D_METHOD("get_selection_from_line"), &TextEdit::get_selection_from_line); + ClassDB::bind_method(D_METHOD("get_selection_from_column"), &TextEdit::get_selection_from_column); + ClassDB::bind_method(D_METHOD("get_selection_to_line"), &TextEdit::get_selection_to_line); + ClassDB::bind_method(D_METHOD("get_selection_to_column"), &TextEdit::get_selection_to_column); + ClassDB::bind_method(D_METHOD("get_selection_text"), &TextEdit::get_selection_text); + ClassDB::bind_method(D_METHOD("get_word_under_cursor"), &TextEdit::get_word_under_cursor); + ClassDB::bind_method(D_METHOD("search", "flags", "from_line", "from_column", "to_line", "to_column"), &TextEdit::_search_bind); + + ClassDB::bind_method(D_METHOD("undo"), &TextEdit::undo); + ClassDB::bind_method(D_METHOD("redo"), &TextEdit::redo); + ClassDB::bind_method(D_METHOD("clear_undo_history"), &TextEdit::clear_undo_history); ClassDB::bind_method(D_METHOD("set_show_line_numbers", "enable"), &TextEdit::set_show_line_numbers); ClassDB::bind_method(D_METHOD("is_show_line_numbers_enabled"), &TextEdit::is_show_line_numbers_enabled); @@ -4685,109 +4488,106 @@ void TextEdit::_bind_methods() { ClassDB::bind_method(D_METHOD("set_highlight_all_occurrences", "enable"), &TextEdit::set_highlight_all_occurrences); ClassDB::bind_method(D_METHOD("is_highlight_all_occurrences_enabled"), &TextEdit::is_highlight_all_occurrences_enabled); - ClassDB::bind_method(D_METHOD("set_syntax_coloring","enable"),&TextEdit::set_syntax_coloring); - ClassDB::bind_method(D_METHOD("is_syntax_coloring_enabled"),&TextEdit::is_syntax_coloring_enabled); - + ClassDB::bind_method(D_METHOD("set_syntax_coloring", "enable"), &TextEdit::set_syntax_coloring); + ClassDB::bind_method(D_METHOD("is_syntax_coloring_enabled"), &TextEdit::is_syntax_coloring_enabled); - ClassDB::bind_method(D_METHOD("add_keyword_color","keyword","color"),&TextEdit::add_keyword_color); - ClassDB::bind_method(D_METHOD("add_color_region","begin_key","end_key","color","line_only"),&TextEdit::add_color_region,DEFVAL(false)); - ClassDB::bind_method(D_METHOD("clear_colors"),&TextEdit::clear_colors); - ClassDB::bind_method(D_METHOD("menu_option"),&TextEdit::menu_option); - ClassDB::bind_method(D_METHOD("get_menu:PopupMenu"),&TextEdit::get_menu); + ClassDB::bind_method(D_METHOD("add_keyword_color", "keyword", "color"), &TextEdit::add_keyword_color); + ClassDB::bind_method(D_METHOD("add_color_region", "begin_key", "end_key", "color", "line_only"), &TextEdit::add_color_region, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("clear_colors"), &TextEdit::clear_colors); + ClassDB::bind_method(D_METHOD("menu_option"), &TextEdit::menu_option); + ClassDB::bind_method(D_METHOD("get_menu:PopupMenu"), &TextEdit::get_menu); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "syntax_highlighting"), "set_syntax_coloring", "is_syntax_coloring_enabled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_line_numbers"), "set_show_line_numbers", "is_show_line_numbers_enabled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_all_occurrences"), "set_highlight_all_occurrences", "is_highlight_all_occurrences_enabled"); - - ADD_GROUP("Caret","caret_"); + ADD_GROUP("Caret", "caret_"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_block_mode"), "cursor_set_block_mode", "cursor_is_block_mode"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "cursor_set_blink_enabled", "cursor_get_blink_enabled"); - ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "caret_blink_speed",PROPERTY_HINT_RANGE,"0.1,10,0.1"), "cursor_set_blink_speed","cursor_get_blink_speed") ; + ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "caret_blink_speed", PROPERTY_HINT_RANGE, "0.1,10,0.1"), "cursor_set_blink_speed", "cursor_get_blink_speed"); ADD_SIGNAL(MethodInfo("cursor_changed")); ADD_SIGNAL(MethodInfo("text_changed")); ADD_SIGNAL(MethodInfo("request_completion")); - ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo( Variant::INT, "row"))); - ADD_SIGNAL(MethodInfo("symbol_lookup", PropertyInfo(Variant::STRING,"symbol"),PropertyInfo( Variant::INT, "row"),PropertyInfo( Variant::INT, "column"))); - - BIND_CONSTANT( MENU_CUT ); - BIND_CONSTANT( MENU_COPY ); - BIND_CONSTANT( MENU_PASTE ); - BIND_CONSTANT( MENU_CLEAR ); - BIND_CONSTANT( MENU_SELECT_ALL ); - BIND_CONSTANT( MENU_UNDO ); - BIND_CONSTANT( MENU_MAX ); + ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::INT, "row"))); + ADD_SIGNAL(MethodInfo("symbol_lookup", PropertyInfo(Variant::STRING, "symbol"), PropertyInfo(Variant::INT, "row"), PropertyInfo(Variant::INT, "column"))); + BIND_CONSTANT(MENU_CUT); + BIND_CONSTANT(MENU_COPY); + BIND_CONSTANT(MENU_PASTE); + BIND_CONSTANT(MENU_CLEAR); + BIND_CONSTANT(MENU_SELECT_ALL); + BIND_CONSTANT(MENU_UNDO); + BIND_CONSTANT(MENU_MAX); - GLOBAL_DEF("gui/timers/text_edit_idle_detect_sec",3); + GLOBAL_DEF("gui/timers/text_edit_idle_detect_sec", 3); } -TextEdit::TextEdit() { +TextEdit::TextEdit() { - readonly=false; - setting_row=false; - draw_tabs=false; - draw_caret=true; - max_chars=0; + readonly = false; + setting_row = false; + draw_tabs = false; + draw_caret = true; + max_chars = 0; clear(); - wrap=false; + wrap = false; set_focus_mode(FOCUS_ALL); _update_caches(); - cache.size=Size2(1,1); - cache.row_height=1; - cache.line_spacing=1; - cache.line_number_w=1; - cache.breakpoint_gutter_width=0; + cache.size = Size2(1, 1); + cache.row_height = 1; + cache.line_spacing = 1; + cache.line_number_w = 1; + cache.breakpoint_gutter_width = 0; breakpoint_gutter_width = 0; - tab_size=4; + tab_size = 4; text.set_tab_size(tab_size); text.clear(); //text.insert(1,"Mongolia.."); //text.insert(2,"PAIS GENEROSO!!"); text.set_color_regions(&color_regions); - h_scroll = memnew( HScrollBar ); - v_scroll = memnew( VScrollBar ); + h_scroll = memnew(HScrollBar); + v_scroll = memnew(VScrollBar); add_child(h_scroll); add_child(v_scroll); - updating_scrolls=false; - selection.active=false; + updating_scrolls = false; + selection.active = false; - h_scroll->connect("value_changed", this,"_scroll_moved"); - v_scroll->connect("value_changed", this,"_scroll_moved"); + h_scroll->connect("value_changed", this, "_scroll_moved"); + v_scroll->connect("value_changed", this, "_scroll_moved"); - cursor_changed_dirty=false; - text_changed_dirty=false; + cursor_changed_dirty = false; + text_changed_dirty = false; - selection.selecting_mode=Selection::MODE_NONE; - selection.selecting_line=0; - selection.selecting_column=0; - selection.selecting_text=false; - selection.active=false; - syntax_coloring=false; + selection.selecting_mode = Selection::MODE_NONE; + selection.selecting_line = 0; + selection.selecting_column = 0; + selection.selecting_text = false; + selection.active = false; + syntax_coloring = false; - block_caret=false; - caret_blink_enabled=false; + block_caret = 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"); + caret_blink_timer->connect("timeout", this, "_toggle_draw_caret"); cursor_set_blink_enabled(false); - idle_detect = memnew( Timer ); + idle_detect = memnew(Timer); add_child(idle_detect); idle_detect->set_one_shot(true); idle_detect->set_wait_time(GLOBAL_GET("gui/timers/text_edit_idle_detect_sec")); - idle_detect->connect("timeout", this,"_push_current_op"); + idle_detect->connect("timeout", this, "_push_current_op"); - click_select_held = memnew( Timer ); + click_select_held = memnew(Timer); add_child(click_select_held); click_select_held->set_wait_time(0.05); - click_select_held->connect("timeout", this,"_click_selection_held"); + click_select_held->connect("timeout", this, "_click_selection_held"); #if 0 syntax_coloring=true; @@ -4807,52 +4607,49 @@ TextEdit::TextEdit() { #endif - current_op.type=TextOperation::TYPE_NONE; - undo_enabled=true; - undo_stack_pos=NULL; - setting_text=false; - last_dblclk=0; - current_op.version=0; - version=0; - saved_version=0; - - completion_enabled=false; - completion_active=false; - completion_line_ofs=0; - tooltip_obj=NULL; - line_numbers=false; - line_numbers_zero_padded=false; - line_length_guideline=false; - line_length_guideline_col=80; - draw_breakpoint_gutter=false; - next_operation_is_complex=false; - scroll_past_end_of_file_enabled=false; - auto_brace_completion_enabled=false; - brace_matching_enabled=false; - highlight_all_occurrences=false; - auto_indent=false; + current_op.type = TextOperation::TYPE_NONE; + undo_enabled = true; + undo_stack_pos = NULL; + setting_text = false; + last_dblclk = 0; + current_op.version = 0; + version = 0; + saved_version = 0; + + completion_enabled = false; + completion_active = false; + completion_line_ofs = 0; + tooltip_obj = NULL; + line_numbers = false; + line_numbers_zero_padded = false; + line_length_guideline = false; + line_length_guideline_col = 80; + draw_breakpoint_gutter = false; + next_operation_is_complex = false; + scroll_past_end_of_file_enabled = false; + auto_brace_completion_enabled = false; + brace_matching_enabled = false; + highlight_all_occurrences = false; + auto_indent = false; insert_mode = false; - window_has_focus=true; - select_identifiers_enabled=false; + window_has_focus = true; + select_identifiers_enabled = false; raised_from_completion = false; - context_menu_enabled=true; - menu = memnew( PopupMenu ); + context_menu_enabled = true; + menu = memnew(PopupMenu); add_child(menu); - menu->add_item(TTR("Cut"),MENU_CUT,KEY_MASK_CMD|KEY_X); - menu->add_item(TTR("Copy"),MENU_COPY,KEY_MASK_CMD|KEY_C); - menu->add_item(TTR("Paste"),MENU_PASTE,KEY_MASK_CMD|KEY_V); + menu->add_item(TTR("Cut"), MENU_CUT, KEY_MASK_CMD | KEY_X); + menu->add_item(TTR("Copy"), MENU_COPY, KEY_MASK_CMD | KEY_C); + menu->add_item(TTR("Paste"), MENU_PASTE, KEY_MASK_CMD | KEY_V); menu->add_separator(); - menu->add_item(TTR("Select All"),MENU_SELECT_ALL,KEY_MASK_CMD|KEY_A); - menu->add_item(TTR("Clear"),MENU_CLEAR); + menu->add_item(TTR("Select All"), MENU_SELECT_ALL, KEY_MASK_CMD | KEY_A); + menu->add_item(TTR("Clear"), MENU_CLEAR); menu->add_separator(); - menu->add_item(TTR("Undo"),MENU_UNDO,KEY_MASK_CMD|KEY_Z); - menu->connect("id_pressed",this,"menu_option"); - - + menu->add_item(TTR("Undo"), MENU_UNDO, KEY_MASK_CMD | KEY_Z); + menu->connect("id_pressed", this, "menu_option"); } -TextEdit::~TextEdit() -{ +TextEdit::~TextEdit() { } diff --git a/scene/gui/text_edit.h b/scene/gui/text_edit.h index 437e22ca40..e38d484b0d 100644 --- a/scene/gui/text_edit.h +++ b/scene/gui/text_edit.h @@ -30,19 +30,18 @@ #define TEXT_EDIT_H #include "scene/gui/control.h" -#include "scene/gui/scroll_bar.h" #include "scene/gui/popup_menu.h" +#include "scene/gui/scroll_bar.h" #include "scene/main/timer.h" +class TextEdit : public Control { -class TextEdit : public Control { - - GDCLASS( TextEdit, Control ); + GDCLASS(TextEdit, Control); struct Cursor { int last_fit_x; - int line,column; ///< cursor - int x_ofs,line_ofs; + int line, column; ///< cursor + int x_ofs, line_ofs; } cursor; struct Selection { @@ -55,14 +54,13 @@ class TextEdit : public Control { }; Mode selecting_mode; - int selecting_line,selecting_column; + int selecting_line, selecting_column; bool selecting_text; - bool active; - int from_line,from_column; - int to_line,to_column; + int from_line, from_column; + int to_line, to_column; bool shiftclick_left; @@ -113,7 +111,13 @@ class TextEdit : public Control { String end_key; bool line_only; bool eq; - ColorRegion(const String& p_begin_key="",const String& p_end_key="",const Color &p_color=Color(),bool p_line_only=false) { begin_key=p_begin_key; end_key=p_end_key; color=p_color; line_only=p_line_only || p_end_key==""; eq=begin_key==end_key; } + ColorRegion(const String &p_begin_key = "", const String &p_end_key = "", const Color &p_color = Color(), bool p_line_only = false) { + begin_key = p_begin_key; + end_key = p_end_key; + color = p_color; + line_only = p_line_only || p_end_key == ""; + eq = begin_key == end_key; + } }; class Text { @@ -124,43 +128,42 @@ class TextEdit : public Control { bool end; }; - struct Line { - int width_cache : 24; - bool marked : 1; - bool breakpoint : 1; - Map<int,ColorRegionInfo> region_info; - String data; - }; + struct Line { + int width_cache : 24; + bool marked : 1; + bool breakpoint : 1; + Map<int, ColorRegionInfo> region_info; + String data; + }; + private: - const Vector<ColorRegion> *color_regions; - mutable Vector<Line> text; - Ref<Font> font; - int tab_size; + const Vector<ColorRegion> *color_regions; + mutable Vector<Line> text; + Ref<Font> font; + int tab_size; - void _update_line_cache(int p_line) const; + void _update_line_cache(int p_line) const; public: - - void set_tab_size(int p_tab_size); - void set_font(const Ref<Font>& p_font); - void set_color_regions(const Vector<ColorRegion>*p_regions) { color_regions=p_regions; } + void set_font(const Ref<Font> &p_font); + void set_color_regions(const Vector<ColorRegion> *p_regions) { color_regions = p_regions; } int get_line_width(int p_line) const; int get_max_width() const; - const Map<int,ColorRegionInfo>& get_color_region_info(int p_line); - void set(int p_line,const String& p_string); - void set_marked(int p_line,bool p_marked) { text[p_line].marked=p_marked; } + const Map<int, ColorRegionInfo> &get_color_region_info(int p_line); + void set(int p_line, const String &p_string); + void set_marked(int p_line, bool p_marked) { text[p_line].marked = p_marked; } bool is_marked(int p_line) const { return text[p_line].marked; } - void set_breakpoint(int p_line,bool p_breakpoint) { text[p_line].breakpoint=p_breakpoint; } + void set_breakpoint(int p_line, bool p_breakpoint) { text[p_line].breakpoint = p_breakpoint; } bool is_breakpoint(int p_line) const { return text[p_line].breakpoint; } - void insert(int p_at,const String& p_text); + void insert(int p_at, const String &p_text); void remove(int p_at); int size() const { return text.size(); } void clear(); void clear_caches(); - _FORCE_INLINE_ const String& operator[](int p_line) const { return text[p_line].data; } - Text() { tab_size=4; } - }; + _FORCE_INLINE_ const String &operator[](int p_line) const { return text[p_line].data; } + Text() { tab_size = 4; } + }; struct TextOperation { @@ -171,7 +174,7 @@ class TextEdit : public Control { }; Type type; - int from_line,from_column; + int from_line, from_column; int to_line, to_column; String text; uint32_t prev_version; @@ -183,14 +186,13 @@ class TextEdit : public Control { TextOperation current_op; List<TextOperation> undo_stack; - List<TextOperation>::Element *undo_stack_pos; + List<TextOperation>::Element *undo_stack_pos; void _clear_redo(); - void _do_text_op(const TextOperation& p_op, bool p_reverse); - + void _do_text_op(const TextOperation &p_op, bool p_reverse); //syntax coloring - HashMap<String,Color> keywords; + HashMap<String, Color> keywords; Vector<ColorRegion> color_regions; @@ -260,7 +262,6 @@ class TextEdit : public Control { VScrollBar *v_scroll; bool updating_scrolls; - Object *tooltip_obj; StringName tooltip_func; Variant tooltip_ud; @@ -281,8 +282,8 @@ class TextEdit : public Control { int get_char_count(); - int get_char_pos_for(int p_px,String p_pos) const; - int get_column_x_offset(int p_column,String p_pos); + int get_char_pos_for(int p_px, String p_pos) const; + int get_column_x_offset(int p_column, String p_pos); void adjust_viewport_to_cursor(); void _scroll_moved(double); @@ -311,13 +312,13 @@ class TextEdit : public Control { /* super internal api, undo/redo builds on it */ - void _base_insert_text(int p_line, int p_column,const String& p_text,int &r_end_line,int &r_end_column); - String _base_get_text(int p_from_line, int p_from_column,int p_to_line,int p_to_column) const; - void _base_remove_text(int p_from_line, int p_from_column,int p_to_line,int p_to_column); + void _base_insert_text(int p_line, int p_column, const String &p_text, int &r_end_line, int &r_end_column); + String _base_get_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) const; + void _base_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column); int _get_column_pos_of_word(const String &p_key, const String &p_search, uint32_t p_search_flags, int p_from_column); - PoolVector<int> _search_bind(const String &p_key,uint32_t p_search_flags, int p_from_line,int p_from_column) const; + PoolVector<int> _search_bind(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column) const; PopupMenu *menu; @@ -328,13 +329,12 @@ class TextEdit : public Control { void _update_completion_candidates(); protected: + virtual String get_tooltip(const Point2 &p_pos) const; - virtual String get_tooltip(const Point2& p_pos) const; - - void _insert_text(int p_line, int p_column,const String& p_text,int *r_end_line=NULL,int *r_end_char=NULL); - void _remove_text(int p_from_line, int p_from_column,int p_to_line,int p_to_column); - void _insert_text_at_cursor(const String& p_text); - void _gui_input(const InputEvent& p_input); + void _insert_text(int p_line, int p_column, const String &p_text, int *r_end_line = NULL, int *r_end_char = NULL); + void _remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column); + void _insert_text_at_cursor(const String &p_text); + void _gui_input(const InputEvent &p_input); void _notification(int p_what); void _consume_pair_symbol(CharType ch); @@ -342,10 +342,7 @@ protected: static void _bind_methods(); - - public: - enum MenuItems { MENU_CUT, MENU_COPY, @@ -359,14 +356,14 @@ public: enum SearchFlags { - SEARCH_MATCH_CASE=1, - SEARCH_WHOLE_WORDS=2, - SEARCH_BACKWARDS=4 + SEARCH_MATCH_CASE = 1, + SEARCH_WHOLE_WORDS = 2, + SEARCH_BACKWARDS = 4 }; - virtual CursorShape get_cursor_shape(const Point2& p_pos=Point2i()) const; + virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const; - void _get_mouse_pos(const Point2i& p_mouse, int &r_row, int &r_col) const; + void _get_mouse_pos(const Point2i &p_mouse, int &r_row, int &r_col) const; //void delete_char(); //void delete_line(); @@ -375,16 +372,16 @@ public: void end_complex_operation(); void set_text(String p_text); - void insert_text_at_cursor(const String& p_text); - void insert_at(const String& p_text, int at); + void insert_text_at_cursor(const String &p_text); + void insert_at(const String &p_text, int at); int get_line_count() const; - void set_line_as_marked(int p_line,bool p_marked); - void set_line_as_breakpoint(int p_line,bool p_breakpoint); + void set_line_as_marked(int p_line, bool p_marked); + void set_line_as_breakpoint(int p_line, bool p_breakpoint); bool is_line_set_as_breakpoint(int p_line) const; void get_breakpoints(List<int> *p_breakpoints) const; String get_text(); String get_line(int line) const; - void set_line(int line, String new_text); + void set_line(int line, String new_text); void backspace_at_cursor(); void indent_selection_left(); @@ -398,7 +395,7 @@ public: auto_brace_completion_enabled = p_enabled; } inline void set_brace_matching(bool p_enabled) { - brace_matching_enabled=p_enabled; + brace_matching_enabled = p_enabled; update(); } inline void set_callhint_settings(bool below, Vector2 offset) { @@ -409,8 +406,8 @@ public: void center_viewport_to_cursor(); - void cursor_set_column(int p_col, bool p_adjust_viewport=true); - void cursor_set_line(int p_row, bool p_adjust_viewport=true); + void cursor_set_column(int p_col, bool p_adjust_viewport = true); + void cursor_set_line(int p_row, bool p_adjust_viewport = true); int cursor_get_column() const; int cursor_get_line() const; @@ -438,10 +435,10 @@ public: void copy(); void paste(); void select_all(); - void select(int p_from_line,int p_from_column,int p_to_line,int p_to_column); + void select(int p_from_line, int p_from_column, int p_to_line, int p_to_column); void deselect(); - void set_search_text(const String& p_search_text); + void set_search_text(const String &p_search_text); void set_search_flags(uint32_t p_flags); void set_current_search_result(int line, int col); @@ -455,9 +452,9 @@ public: String get_selection_text() const; String get_word_under_cursor() const; - String get_word_at_pos(const Vector2& p_pos) const; + String get_word_at_pos(const Vector2 &p_pos) const; - bool search(const String &p_key,uint32_t p_search_flags, int p_from_line, int p_from_column,int &r_line,int &r_column) const; + bool search(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column, int &r_line, int &r_column) const; void undo(); void redo(); @@ -470,8 +467,8 @@ public: void set_insert_mode(bool p_enabled); bool is_insert_mode() const; - void add_keyword_color(const String& p_keyword,const Color& p_color); - void add_color_region(const String& p_begin_key=String(),const String& p_end_key=String(),const Color &p_color=Color(),bool p_line_only=false); + void add_keyword_color(const String &p_keyword, const Color &p_color); + void add_color_region(const String &p_begin_key = String(), const String &p_end_key = String(), const Color &p_color = Color(), bool p_line_only = false); void clear_colors(); int get_v_scroll() const; @@ -500,11 +497,11 @@ public: void set_breakpoint_gutter_width(int p_gutter_width); int get_breakpoint_gutter_width() const; - void set_tooltip_request_func(Object *p_obj, const StringName& p_function, const Variant& p_udata); + void set_tooltip_request_func(Object *p_obj, const StringName &p_function, const Variant &p_udata); - void set_completion(bool p_enabled,const Vector<String>& p_prefixes); + void set_completion(bool p_enabled, const Vector<String> &p_prefixes); void code_complete(const Vector<String> &p_strings); - void set_code_hint(const String& p_hint); + void set_code_hint(const String &p_hint); void query_code_comple(); void set_select_identifiers_on_hover(bool p_enable); @@ -521,5 +518,4 @@ public: ~TextEdit(); }; - #endif // TEXT_EDIT_H diff --git a/scene/gui/texture_button.cpp b/scene/gui/texture_button.cpp index 2f10de054a..54f09cbf68 100644 --- a/scene/gui/texture_button.cpp +++ b/scene/gui/texture_button.cpp @@ -37,27 +37,27 @@ Size2 TextureButton::get_minimum_size() const { if (pressed.is_null()) { if (hover.is_null()) if (click_mask.is_null()) - rscale= Size2(); + rscale = Size2(); else - rscale= click_mask->get_size(); + rscale = click_mask->get_size(); else - rscale= hover->get_size(); + rscale = hover->get_size(); } else - rscale=pressed->get_size(); + rscale = pressed->get_size(); } else - rscale= normal->get_size(); + rscale = normal->get_size(); } return rscale.abs(); } -bool TextureButton::has_point(const Point2& p_point) const { +bool TextureButton::has_point(const Point2 &p_point) const { if (click_mask.is_valid()) { - Point2i p =p_point; - if (p.x<0 || p.x>=click_mask->get_size().width || p.y<0 || p.y>=click_mask->get_size().height) + Point2i p = p_point; + if (p.x < 0 || p.x >= click_mask->get_size().width || p.y < 0 || p.y >= click_mask->get_size().height) return false; return click_mask->get_bit(p); @@ -68,7 +68,7 @@ bool TextureButton::has_point(const Point2& p_point) const { void TextureButton::_notification(int p_what) { - switch( p_what ) { + switch (p_what) { case NOTIFICATION_DRAW: { DrawMode draw_mode = get_draw_mode(); @@ -79,37 +79,37 @@ void TextureButton::_notification(int p_what) { case DRAW_NORMAL: { if (normal.is_valid()) - texdraw=normal; + texdraw = normal; } break; case DRAW_PRESSED: { if (pressed.is_null()) { if (hover.is_null()) { if (normal.is_valid()) - texdraw=normal; + texdraw = normal; } else - texdraw=hover; + texdraw = hover; } else - texdraw=pressed; + texdraw = pressed; } break; case DRAW_HOVER: { if (hover.is_null()) { if (pressed.is_valid() && is_pressed()) - texdraw=pressed; + texdraw = pressed; else if (normal.is_valid()) - texdraw=normal; + texdraw = normal; } else - texdraw=hover; + texdraw = hover; } break; case DRAW_DISABLED: { if (disabled.is_null()) { if (normal.is_valid()) - texdraw=normal; + texdraw = normal; } else - texdraw=disabled; + texdraw = disabled; } break; } @@ -131,12 +131,12 @@ void TextureButton::_notification(int p_what) { tile = true; break; case STRETCH_KEEP_CENTERED: - ofs = (get_size() - texdraw->get_size())/2; + ofs = (get_size() - texdraw->get_size()) / 2; size = texdraw->get_size(); break; case STRETCH_KEEP_ASPECT_CENTERED: case STRETCH_KEEP_ASPECT: { - Size2 _size=get_size(); + Size2 _size = get_size(); float tex_width = texdraw->get_width() * _size.height / texdraw->get_height(); float tex_height = _size.height; @@ -145,34 +145,33 @@ void TextureButton::_notification(int p_what) { tex_height = texdraw->get_height() * tex_width / texdraw->get_width(); } - if (stretch_mode==STRETCH_KEEP_ASPECT_CENTERED) { - ofs.x = (_size.width - tex_width)/2; - ofs.y = (_size.height - tex_height)/2; + if (stretch_mode == STRETCH_KEEP_ASPECT_CENTERED) { + ofs.x = (_size.width - tex_width) / 2; + ofs.y = (_size.height - tex_height) / 2; } size.width = tex_width; size.height = tex_height; } break; - case STRETCH_KEEP_ASPECT_COVERED:{ + case STRETCH_KEEP_ASPECT_COVERED: { size = get_size(); Size2 tex_size = texdraw->get_size(); - Size2 scaleSize(size.width/tex_size.width, size.height/tex_size.height); - float scale = scaleSize.width > scaleSize.height? scaleSize.width : scaleSize.height; + Size2 scaleSize(size.width / tex_size.width, size.height / tex_size.height); + float scale = scaleSize.width > scaleSize.height ? scaleSize.width : scaleSize.height; Size2 scaledTexSize = tex_size * scale; Point2 ofs = ((scaledTexSize - size) / scale).abs() / 2.0f; - tex_regin = Rect2(ofs, size/scale); + tex_regin = Rect2(ofs, size / scale); } break; } } if (tile) - draw_texture_rect(texdraw,Rect2(ofs,size),tile); + draw_texture_rect(texdraw, Rect2(ofs, size), tile); else draw_texture_rect_region(texdraw, Rect2(ofs, size), tex_regin); } if (has_focus() && focused.is_valid()) { Rect2 drect(Point2(), get_size()); - draw_texture_rect(focused,drect,false); - + draw_texture_rect(focused, drect, false); }; } break; } @@ -180,33 +179,33 @@ void TextureButton::_notification(int p_what) { void TextureButton::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_normal_texture","texture:Texture"),&TextureButton::set_normal_texture); - ClassDB::bind_method(D_METHOD("set_pressed_texture","texture:Texture"),&TextureButton::set_pressed_texture); - ClassDB::bind_method(D_METHOD("set_hover_texture","texture:Texture"),&TextureButton::set_hover_texture); - ClassDB::bind_method(D_METHOD("set_disabled_texture","texture:Texture"),&TextureButton::set_disabled_texture); - ClassDB::bind_method(D_METHOD("set_focused_texture","texture:Texture"),&TextureButton::set_focused_texture); - ClassDB::bind_method(D_METHOD("set_click_mask","mask:BitMap"),&TextureButton::set_click_mask); - ClassDB::bind_method(D_METHOD("set_expand","p_expand"),&TextureButton::set_expand); - ClassDB::bind_method(D_METHOD("set_stretch_mode","p_mode"),&TextureButton::set_stretch_mode); - - ClassDB::bind_method(D_METHOD("get_normal_texture:Texture"),&TextureButton::get_normal_texture); - ClassDB::bind_method(D_METHOD("get_pressed_texture:Texture"),&TextureButton::get_pressed_texture); - ClassDB::bind_method(D_METHOD("get_hover_texture:Texture"),&TextureButton::get_hover_texture); - ClassDB::bind_method(D_METHOD("get_disabled_texture:Texture"),&TextureButton::get_disabled_texture); - ClassDB::bind_method(D_METHOD("get_focused_texture:Texture"),&TextureButton::get_focused_texture); - ClassDB::bind_method(D_METHOD("get_click_mask:BitMap"),&TextureButton::get_click_mask); - ClassDB::bind_method(D_METHOD("get_expand"),&TextureButton::get_expand); - ClassDB::bind_method(D_METHOD("get_stretch_mode"),&TextureButton::get_stretch_mode); - - ADD_GROUP("Textures","texture_"); - ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"texture_normal",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), "set_normal_texture", "get_normal_texture"); - ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"texture_pressed",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), "set_pressed_texture", "get_pressed_texture"); - ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"texture_hover",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), "set_hover_texture", "get_hover_texture"); - ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"texture_disabled",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), "set_disabled_texture", "get_disabled_texture"); - ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"texture_focused",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), "set_focused_texture", "get_focused_texture"); - ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"texture_click_mask",PROPERTY_HINT_RESOURCE_TYPE,"BitMap"), "set_click_mask", "get_click_mask") ; - ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL,"expand",PROPERTY_HINT_RESOURCE_TYPE,"bool"), "set_expand", "get_expand") ; - ADD_PROPERTYNO(PropertyInfo(Variant::INT,"stretch_mode",PROPERTY_HINT_ENUM,"Scale,Tile,Keep,Keep Centered,Keep Aspect,Keep Aspect Centered,Keep Aspect Covered"), "set_stretch_mode", "get_stretch_mode"); + ClassDB::bind_method(D_METHOD("set_normal_texture", "texture:Texture"), &TextureButton::set_normal_texture); + ClassDB::bind_method(D_METHOD("set_pressed_texture", "texture:Texture"), &TextureButton::set_pressed_texture); + ClassDB::bind_method(D_METHOD("set_hover_texture", "texture:Texture"), &TextureButton::set_hover_texture); + ClassDB::bind_method(D_METHOD("set_disabled_texture", "texture:Texture"), &TextureButton::set_disabled_texture); + ClassDB::bind_method(D_METHOD("set_focused_texture", "texture:Texture"), &TextureButton::set_focused_texture); + ClassDB::bind_method(D_METHOD("set_click_mask", "mask:BitMap"), &TextureButton::set_click_mask); + ClassDB::bind_method(D_METHOD("set_expand", "p_expand"), &TextureButton::set_expand); + ClassDB::bind_method(D_METHOD("set_stretch_mode", "p_mode"), &TextureButton::set_stretch_mode); + + ClassDB::bind_method(D_METHOD("get_normal_texture:Texture"), &TextureButton::get_normal_texture); + ClassDB::bind_method(D_METHOD("get_pressed_texture:Texture"), &TextureButton::get_pressed_texture); + ClassDB::bind_method(D_METHOD("get_hover_texture:Texture"), &TextureButton::get_hover_texture); + ClassDB::bind_method(D_METHOD("get_disabled_texture:Texture"), &TextureButton::get_disabled_texture); + ClassDB::bind_method(D_METHOD("get_focused_texture:Texture"), &TextureButton::get_focused_texture); + ClassDB::bind_method(D_METHOD("get_click_mask:BitMap"), &TextureButton::get_click_mask); + ClassDB::bind_method(D_METHOD("get_expand"), &TextureButton::get_expand); + ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureButton::get_stretch_mode); + + ADD_GROUP("Textures", "texture_"); + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_normal_texture", "get_normal_texture"); + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture_pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_pressed_texture", "get_pressed_texture"); + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture_hover", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_hover_texture", "get_hover_texture"); + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture_disabled", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_disabled_texture", "get_disabled_texture"); + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture_focused", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_focused_texture", "get_focused_texture"); + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture_click_mask", PROPERTY_HINT_RESOURCE_TYPE, "BitMap"), "set_click_mask", "get_click_mask"); + ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "expand", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_expand", "get_expand"); + ADD_PROPERTYNO(PropertyInfo(Variant::INT, "stretch_mode", PROPERTY_HINT_ENUM, "Scale,Tile,Keep,Keep Centered,Keep Aspect,Keep Aspect Centered,Keep Aspect Covered"), "set_stretch_mode", "get_stretch_mode"); BIND_CONSTANT(STRETCH_SCALE); BIND_CONSTANT(STRETCH_TILE); @@ -215,39 +214,33 @@ void TextureButton::_bind_methods() { BIND_CONSTANT(STRETCH_KEEP_ASPECT); BIND_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED); BIND_CONSTANT(STRETCH_KEEP_ASPECT_COVERED); - } +void TextureButton::set_normal_texture(const Ref<Texture> &p_normal) { -void TextureButton::set_normal_texture(const Ref<Texture>& p_normal) { - - normal=p_normal; + normal = p_normal; update(); minimum_size_changed(); - } -void TextureButton::set_pressed_texture(const Ref<Texture>& p_pressed) { +void TextureButton::set_pressed_texture(const Ref<Texture> &p_pressed) { - pressed=p_pressed; + pressed = p_pressed; update(); - } -void TextureButton::set_hover_texture(const Ref<Texture>& p_hover) { +void TextureButton::set_hover_texture(const Ref<Texture> &p_hover) { - hover=p_hover; + hover = p_hover; update(); - } -void TextureButton::set_disabled_texture(const Ref<Texture>& p_disabled) { +void TextureButton::set_disabled_texture(const Ref<Texture> &p_disabled) { - disabled=p_disabled; + disabled = p_disabled; update(); - } -void TextureButton::set_click_mask(const Ref<BitMap>& p_click_mask) { +void TextureButton::set_click_mask(const Ref<BitMap> &p_click_mask) { - click_mask=p_click_mask; + click_mask = p_click_mask; update(); } @@ -277,7 +270,7 @@ Ref<Texture> TextureButton::get_focused_texture() const { return focused; }; -void TextureButton::set_focused_texture(const Ref<Texture>& p_focused) { +void TextureButton::set_focused_texture(const Ref<Texture> &p_focused) { focused = p_focused; }; diff --git a/scene/gui/texture_button.h b/scene/gui/texture_button.h index 64103860bf..fc5079dbf9 100644 --- a/scene/gui/texture_button.h +++ b/scene/gui/texture_button.h @@ -33,7 +33,7 @@ #include "scene/resources/bit_mask.h" class TextureButton : public BaseButton { - GDCLASS( TextureButton, BaseButton ); + GDCLASS(TextureButton, BaseButton); public: enum StretchMode { @@ -57,20 +57,18 @@ private: StretchMode stretch_mode; protected: - virtual Size2 get_minimum_size() const; - virtual bool has_point(const Point2& p_point) const; + virtual bool has_point(const Point2 &p_point) const; void _notification(int p_what); static void _bind_methods(); public: - - void set_normal_texture(const Ref<Texture>& p_normal); - void set_pressed_texture(const Ref<Texture>& p_pressed); - void set_hover_texture(const Ref<Texture>& p_hover); - void set_disabled_texture(const Ref<Texture>& p_disabled); - void set_focused_texture(const Ref<Texture>& p_focused); - void set_click_mask(const Ref<BitMap>& p_image); + void set_normal_texture(const Ref<Texture> &p_normal); + void set_pressed_texture(const Ref<Texture> &p_pressed); + void set_hover_texture(const Ref<Texture> &p_hover); + void set_disabled_texture(const Ref<Texture> &p_disabled); + void set_focused_texture(const Ref<Texture> &p_focused); + void set_click_mask(const Ref<BitMap> &p_image); Ref<Texture> get_normal_texture() const; Ref<Texture> get_pressed_texture() const; @@ -88,6 +86,5 @@ public: TextureButton(); }; - -VARIANT_ENUM_CAST( TextureButton::StretchMode ); +VARIANT_ENUM_CAST(TextureButton::StretchMode); #endif // TEXTURE_BUTTON_H diff --git a/scene/gui/texture_progress.cpp b/scene/gui/texture_progress.cpp index 3897647502..914d17d2dc 100644 --- a/scene/gui/texture_progress.cpp +++ b/scene/gui/texture_progress.cpp @@ -28,33 +28,30 @@ /*************************************************************************/ #include "texture_progress.h" +void TextureProgress::set_under_texture(const Ref<Texture> &p_texture) { -void TextureProgress::set_under_texture(const Ref<Texture>& p_texture) { - - under=p_texture; + under = p_texture; update(); minimum_size_changed(); } -Ref<Texture> TextureProgress::get_under_texture() const{ +Ref<Texture> TextureProgress::get_under_texture() const { return under; - } -void TextureProgress::set_over_texture(const Ref<Texture>& p_texture) { +void TextureProgress::set_over_texture(const Ref<Texture> &p_texture) { - over=p_texture; + over = p_texture; update(); if (under.is_null()) { minimum_size_changed(); } } -Ref<Texture> TextureProgress::get_over_texture() const{ +Ref<Texture> TextureProgress::get_over_texture() const { return over; - } Size2 TextureProgress::get_minimum_size() const { @@ -66,243 +63,227 @@ Size2 TextureProgress::get_minimum_size() const { else if (progress.is_valid()) return progress->get_size(); - return Size2(1,1); + return Size2(1, 1); } -void TextureProgress::set_progress_texture(const Ref<Texture>& p_texture) { +void TextureProgress::set_progress_texture(const Ref<Texture> &p_texture) { - progress=p_texture; + progress = p_texture; update(); minimum_size_changed(); } -Ref<Texture> TextureProgress::get_progress_texture() const{ +Ref<Texture> TextureProgress::get_progress_texture() const { return progress; - } Point2 TextureProgress::unit_val_to_uv(float val) { if (progress.is_null()) return Point2(); - if (val<0) - val+=1; - if (val>1) - val-=1; - - Point2 p=get_relative_center(); - - if (val<0.125) - return Point2(p.x+(1-p.x)*val*8,0); - if (val<0.25) - return Point2(1,p.y*(val-0.125)*8); - if (val<0.375) - return Point2(1,p.y+(1-p.y)*(val-0.25)*8); - if (val<0.5) - return Point2(1-(1-p.x)*(val-0.375)*8,1); - if (val<0.625) - return Point2(p.x*(1-(val-0.5)*8),1); - if (val<0.75) - return Point2(0,1-((1-p.y)*(val-0.625)*8)); - if (val<0.875) - return Point2(0,p.y-p.y*(val-0.75)*8); + if (val < 0) + val += 1; + if (val > 1) + val -= 1; + + Point2 p = get_relative_center(); + + if (val < 0.125) + return Point2(p.x + (1 - p.x) * val * 8, 0); + if (val < 0.25) + return Point2(1, p.y * (val - 0.125) * 8); + if (val < 0.375) + return Point2(1, p.y + (1 - p.y) * (val - 0.25) * 8); + if (val < 0.5) + return Point2(1 - (1 - p.x) * (val - 0.375) * 8, 1); + if (val < 0.625) + return Point2(p.x * (1 - (val - 0.5) * 8), 1); + if (val < 0.75) + return Point2(0, 1 - ((1 - p.y) * (val - 0.625) * 8)); + if (val < 0.875) + return Point2(0, p.y - p.y * (val - 0.75) * 8); else - return Point2(p.x*(val-0.875)*8,0); + return Point2(p.x * (val - 0.875) * 8, 0); } -Point2 TextureProgress::get_relative_center() -{ +Point2 TextureProgress::get_relative_center() { if (progress.is_null()) return Point2(); - Point2 p = progress->get_size()/2; - p+=rad_center_off; - p.x/=progress->get_width(); - p.y/=progress->get_height(); - p.x=CLAMP(p.x,0,1); - p.y=CLAMP(p.y,0,1); + Point2 p = progress->get_size() / 2; + p += rad_center_off; + p.x /= progress->get_width(); + p.y /= progress->get_height(); + p.x = CLAMP(p.x, 0, 1); + p.y = CLAMP(p.y, 0, 1); return p; } -void TextureProgress::_notification(int p_what){ - const float corners[12]={-0.125,-0.375,-0.625,-0.875,0.125,0.375,0.625,0.875,1.125,1.375,1.625,1.875}; - switch(p_what) { +void TextureProgress::_notification(int p_what) { + const float corners[12] = { -0.125, -0.375, -0.625, -0.875, 0.125, 0.375, 0.625, 0.875, 1.125, 1.375, 1.625, 1.875 }; + switch (p_what) { case NOTIFICATION_DRAW: { - if (under.is_valid()) - draw_texture(under,Point2()); + draw_texture(under, Point2()); if (progress.is_valid()) { Size2 s = progress->get_size(); switch (mode) { - case FILL_LEFT_TO_RIGHT: { - Rect2 region=Rect2(Point2(),Size2(s.x*get_as_ratio(),s.y)); - draw_texture_rect_region(progress,region,region); - } break; - case FILL_RIGHT_TO_LEFT: { - Rect2 region=Rect2(Point2(s.x-s.x*get_as_ratio(),0),Size2(s.x*get_as_ratio(),s.y)); - draw_texture_rect_region(progress,region,region); - } break; - case FILL_TOP_TO_BOTTOM: { - Rect2 region=Rect2(Point2(),Size2(s.x,s.y*get_as_ratio())); - draw_texture_rect_region(progress,region,region); - } break; - case FILL_BOTTOM_TO_TOP: { - Rect2 region=Rect2(Point2(0,s.y-s.y*get_as_ratio()),Size2(s.x,s.y*get_as_ratio())); - draw_texture_rect_region(progress,region,region); - } break; - case FILL_CLOCKWISE: - case FILL_COUNTER_CLOCKWISE: { - float val=get_as_ratio()*rad_max_degrees/360; - if (val==1) { - Rect2 region=Rect2(Point2(),s); - draw_texture_rect_region(progress,region,region); - } else if (val!=0) { - Array pts; - float direction=mode==FILL_CLOCKWISE?1:-1; - float start=rad_init_angle/360; - float end=start+direction*val; - pts.append(start); - pts.append(end); - float from=MIN(start,end); - float to=MAX(start,end); - for (int i=0;i<12;i++) - if (corners[i]>from&&corners[i]<to) - pts.append(corners[i]); - pts.sort(); - Vector<Point2> uvs; - Vector<Point2> points; - uvs.push_back(get_relative_center()); - points.push_back(Point2(s.x*get_relative_center().x,s.y*get_relative_center().y)); - for (int i=0;i<pts.size();i++) { - Point2 uv=unit_val_to_uv(pts[i]); - if (uvs.find(uv)>=0) - continue; - uvs.push_back(uv); - points.push_back(Point2(uv.x*s.x,uv.y*s.y)); + case FILL_LEFT_TO_RIGHT: { + Rect2 region = Rect2(Point2(), Size2(s.x * get_as_ratio(), s.y)); + draw_texture_rect_region(progress, region, region); + } break; + case FILL_RIGHT_TO_LEFT: { + Rect2 region = Rect2(Point2(s.x - s.x * get_as_ratio(), 0), Size2(s.x * get_as_ratio(), s.y)); + draw_texture_rect_region(progress, region, region); + } break; + case FILL_TOP_TO_BOTTOM: { + Rect2 region = Rect2(Point2(), Size2(s.x, s.y * get_as_ratio())); + draw_texture_rect_region(progress, region, region); + } break; + case FILL_BOTTOM_TO_TOP: { + Rect2 region = Rect2(Point2(0, s.y - s.y * get_as_ratio()), Size2(s.x, s.y * get_as_ratio())); + draw_texture_rect_region(progress, region, region); + } break; + case FILL_CLOCKWISE: + case FILL_COUNTER_CLOCKWISE: { + float val = get_as_ratio() * rad_max_degrees / 360; + if (val == 1) { + Rect2 region = Rect2(Point2(), s); + draw_texture_rect_region(progress, region, region); + } else if (val != 0) { + Array pts; + float direction = mode == FILL_CLOCKWISE ? 1 : -1; + float start = rad_init_angle / 360; + float end = start + direction * val; + pts.append(start); + pts.append(end); + float from = MIN(start, end); + float to = MAX(start, end); + for (int i = 0; i < 12; i++) + if (corners[i] > from && corners[i] < to) + pts.append(corners[i]); + pts.sort(); + Vector<Point2> uvs; + Vector<Point2> points; + uvs.push_back(get_relative_center()); + points.push_back(Point2(s.x * get_relative_center().x, s.y * get_relative_center().y)); + for (int i = 0; i < pts.size(); i++) { + Point2 uv = unit_val_to_uv(pts[i]); + if (uvs.find(uv) >= 0) + continue; + uvs.push_back(uv); + points.push_back(Point2(uv.x * s.x, uv.y * s.y)); + } + draw_polygon(points, Vector<Color>(), uvs, progress); } - draw_polygon(points,Vector<Color>(),uvs,progress); - } - if (get_tree()->is_editor_hint()) { - Point2 p=progress->get_size(); - p.x*=get_relative_center().x; - p.y*=get_relative_center().y; - p=p.floor(); - draw_line(p-Point2(8,0),p+Point2(8,0),Color(0.9,0.5,0.5),2); - draw_line(p-Point2(0,8),p+Point2(0,8),Color(0.9,0.5,0.5),2); - } - } break; - default: - draw_texture_rect_region(progress,Rect2(Point2(),Size2(s.x*get_as_ratio(),s.y)),Rect2(Point2(),Size2(s.x*get_as_ratio(),s.y))); + if (get_tree()->is_editor_hint()) { + Point2 p = progress->get_size(); + p.x *= get_relative_center().x; + p.y *= get_relative_center().y; + p = p.floor(); + draw_line(p - Point2(8, 0), p + Point2(8, 0), Color(0.9, 0.5, 0.5), 2); + draw_line(p - Point2(0, 8), p + Point2(0, 8), Color(0.9, 0.5, 0.5), 2); + } + } break; + default: + draw_texture_rect_region(progress, Rect2(Point2(), Size2(s.x * get_as_ratio(), s.y)), Rect2(Point2(), Size2(s.x * get_as_ratio(), s.y))); } - - } if (over.is_valid()) - draw_texture(over,Point2()); + draw_texture(over, Point2()); } break; } } -void TextureProgress::set_fill_mode(int p_fill) -{ - ERR_FAIL_INDEX(p_fill,6); - mode=(FillMode)p_fill; +void TextureProgress::set_fill_mode(int p_fill) { + ERR_FAIL_INDEX(p_fill, 6); + mode = (FillMode)p_fill; update(); } -int TextureProgress::get_fill_mode() -{ +int TextureProgress::get_fill_mode() { return mode; } -void TextureProgress::set_radial_initial_angle(float p_angle) -{ - while(p_angle>360) - p_angle-=360; - while (p_angle<0) - p_angle+=360; - rad_init_angle=p_angle; +void TextureProgress::set_radial_initial_angle(float p_angle) { + while (p_angle > 360) + p_angle -= 360; + while (p_angle < 0) + p_angle += 360; + rad_init_angle = p_angle; update(); } -float TextureProgress::get_radial_initial_angle() -{ +float TextureProgress::get_radial_initial_angle() { return rad_init_angle; } -void TextureProgress::set_fill_degrees(float p_angle) -{ - rad_max_degrees=CLAMP(p_angle,0,360); +void TextureProgress::set_fill_degrees(float p_angle) { + rad_max_degrees = CLAMP(p_angle, 0, 360); update(); } -float TextureProgress::get_fill_degrees() -{ +float TextureProgress::get_fill_degrees() { return rad_max_degrees; } -void TextureProgress::set_radial_center_offset(const Point2 &p_off) -{ - rad_center_off=p_off; +void TextureProgress::set_radial_center_offset(const Point2 &p_off) { + rad_center_off = p_off; update(); } -Point2 TextureProgress::get_radial_center_offset() -{ +Point2 TextureProgress::get_radial_center_offset() { return rad_center_off; } void TextureProgress::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_under_texture","tex"),&TextureProgress::set_under_texture); - ClassDB::bind_method(D_METHOD("get_under_texture"),&TextureProgress::get_under_texture); + ClassDB::bind_method(D_METHOD("set_under_texture", "tex"), &TextureProgress::set_under_texture); + ClassDB::bind_method(D_METHOD("get_under_texture"), &TextureProgress::get_under_texture); - ClassDB::bind_method(D_METHOD("set_progress_texture","tex"),&TextureProgress::set_progress_texture); - ClassDB::bind_method(D_METHOD("get_progress_texture"),&TextureProgress::get_progress_texture); + ClassDB::bind_method(D_METHOD("set_progress_texture", "tex"), &TextureProgress::set_progress_texture); + ClassDB::bind_method(D_METHOD("get_progress_texture"), &TextureProgress::get_progress_texture); - ClassDB::bind_method(D_METHOD("set_over_texture","tex"),&TextureProgress::set_over_texture); - ClassDB::bind_method(D_METHOD("get_over_texture"),&TextureProgress::get_over_texture); + ClassDB::bind_method(D_METHOD("set_over_texture", "tex"), &TextureProgress::set_over_texture); + ClassDB::bind_method(D_METHOD("get_over_texture"), &TextureProgress::get_over_texture); - ClassDB::bind_method(D_METHOD("set_fill_mode","mode"),&TextureProgress::set_fill_mode); + ClassDB::bind_method(D_METHOD("set_fill_mode", "mode"), &TextureProgress::set_fill_mode); ClassDB::bind_method(D_METHOD("get_fill_mode"), &TextureProgress::get_fill_mode); - ClassDB::bind_method(D_METHOD("set_radial_initial_angle","mode"),&TextureProgress::set_radial_initial_angle); + ClassDB::bind_method(D_METHOD("set_radial_initial_angle", "mode"), &TextureProgress::set_radial_initial_angle); ClassDB::bind_method(D_METHOD("get_radial_initial_angle"), &TextureProgress::get_radial_initial_angle); - ClassDB::bind_method(D_METHOD("set_radial_center_offset","mode"),&TextureProgress::set_radial_center_offset); + ClassDB::bind_method(D_METHOD("set_radial_center_offset", "mode"), &TextureProgress::set_radial_center_offset); ClassDB::bind_method(D_METHOD("get_radial_center_offset"), &TextureProgress::get_radial_center_offset); - ClassDB::bind_method(D_METHOD("set_fill_degrees","mode"),&TextureProgress::set_fill_degrees); + ClassDB::bind_method(D_METHOD("set_fill_degrees", "mode"), &TextureProgress::set_fill_degrees); ClassDB::bind_method(D_METHOD("get_fill_degrees"), &TextureProgress::get_fill_degrees); - ADD_GROUP("Textures","texture_"); - ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"texture_under",PROPERTY_HINT_RESOURCE_TYPE,"Texture"),"set_under_texture","get_under_texture"); - ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"texture_over",PROPERTY_HINT_RESOURCE_TYPE,"Texture"),"set_over_texture","get_over_texture"); - ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"texture_progress",PROPERTY_HINT_RESOURCE_TYPE,"Texture"),"set_progress_texture","get_progress_texture"); - ADD_PROPERTYNZ( PropertyInfo(Variant::INT,"fill_mode",PROPERTY_HINT_ENUM,"Left to Right,Right to Left,Top to Bottom,Bottom to Top,Clockwise,Counter Clockwise"),"set_fill_mode","get_fill_mode"); - ADD_GROUP("Radial Fill","radial_"); - ADD_PROPERTYNZ( PropertyInfo(Variant::REAL,"radial_initial_angle",PROPERTY_HINT_RANGE,"0.0,360.0,0.1,slider"),"set_radial_initial_angle","get_radial_initial_angle"); - ADD_PROPERTYNZ( PropertyInfo(Variant::REAL,"radial_fill_degrees",PROPERTY_HINT_RANGE,"0.0,360.0,0.1,slider"),"set_fill_degrees","get_fill_degrees"); - ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"radial_center_offset"),"set_radial_center_offset","get_radial_center_offset"); - - BIND_CONSTANT( FILL_LEFT_TO_RIGHT ); - BIND_CONSTANT( FILL_RIGHT_TO_LEFT ); - BIND_CONSTANT( FILL_TOP_TO_BOTTOM ); - BIND_CONSTANT( FILL_BOTTOM_TO_TOP ); - BIND_CONSTANT( FILL_CLOCKWISE ); - BIND_CONSTANT( FILL_COUNTER_CLOCKWISE ); - + ADD_GROUP("Textures", "texture_"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_under", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_under_texture", "get_under_texture"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_over", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_over_texture", "get_over_texture"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_progress", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_progress_texture", "get_progress_texture"); + ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "fill_mode", PROPERTY_HINT_ENUM, "Left to Right,Right to Left,Top to Bottom,Bottom to Top,Clockwise,Counter Clockwise"), "set_fill_mode", "get_fill_mode"); + ADD_GROUP("Radial Fill", "radial_"); + ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "radial_initial_angle", PROPERTY_HINT_RANGE, "0.0,360.0,0.1,slider"), "set_radial_initial_angle", "get_radial_initial_angle"); + ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "radial_fill_degrees", PROPERTY_HINT_RANGE, "0.0,360.0,0.1,slider"), "set_fill_degrees", "get_fill_degrees"); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "radial_center_offset"), "set_radial_center_offset", "get_radial_center_offset"); + + BIND_CONSTANT(FILL_LEFT_TO_RIGHT); + BIND_CONSTANT(FILL_RIGHT_TO_LEFT); + BIND_CONSTANT(FILL_TOP_TO_BOTTOM); + BIND_CONSTANT(FILL_BOTTOM_TO_TOP); + BIND_CONSTANT(FILL_CLOCKWISE); + BIND_CONSTANT(FILL_COUNTER_CLOCKWISE); } - -TextureProgress::TextureProgress() -{ - mode=FILL_LEFT_TO_RIGHT; - rad_init_angle=0; - rad_center_off=Point2(); - rad_max_degrees=360; +TextureProgress::TextureProgress() { + mode = FILL_LEFT_TO_RIGHT; + rad_init_angle = 0; + rad_center_off = Point2(); + rad_max_degrees = 360; set_mouse_filter(MOUSE_FILTER_PASS); } diff --git a/scene/gui/texture_progress.h b/scene/gui/texture_progress.h index 02794354ef..0d9b5e6b1a 100644 --- a/scene/gui/texture_progress.h +++ b/scene/gui/texture_progress.h @@ -33,20 +33,19 @@ class TextureProgress : public Range { - GDCLASS( TextureProgress, Range ); + GDCLASS(TextureProgress, Range); Ref<Texture> under; Ref<Texture> progress; Ref<Texture> over; protected: - static void _bind_methods(); void _notification(int p_what); -public: +public: enum FillMode { - FILL_LEFT_TO_RIGHT=0, + FILL_LEFT_TO_RIGHT = 0, FILL_RIGHT_TO_LEFT, FILL_TOP_TO_BOTTOM, FILL_BOTTOM_TO_TOP, @@ -66,13 +65,13 @@ public: void set_radial_center_offset(const Point2 &p_off); Point2 get_radial_center_offset(); - void set_under_texture(const Ref<Texture>& p_texture); + void set_under_texture(const Ref<Texture> &p_texture); Ref<Texture> get_under_texture() const; - void set_progress_texture(const Ref<Texture>& p_texture); + void set_progress_texture(const Ref<Texture> &p_texture); Ref<Texture> get_progress_texture() const; - void set_over_texture(const Ref<Texture>& p_texture); + void set_over_texture(const Ref<Texture> &p_texture); Ref<Texture> get_over_texture() const; Size2 get_minimum_size() const; @@ -80,7 +79,6 @@ public: TextureProgress(); private: - FillMode mode; float rad_init_angle; float rad_max_degrees; diff --git a/scene/gui/texture_rect.cpp b/scene/gui/texture_rect.cpp index f2fe3eae12..ea35ee8b52 100644 --- a/scene/gui/texture_rect.cpp +++ b/scene/gui/texture_rect.cpp @@ -31,65 +31,63 @@ void TextureRect::_notification(int p_what) { - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { if (texture.is_null()) return; - - switch(stretch_mode) { + switch (stretch_mode) { case STRETCH_SCALE_ON_EXPAND: { - Size2 s=expand?get_size():texture->get_size(); - draw_texture_rect(texture,Rect2(Point2(),s),false); + Size2 s = expand ? get_size() : texture->get_size(); + draw_texture_rect(texture, Rect2(Point2(), s), false); } break; case STRETCH_SCALE: { - draw_texture_rect(texture,Rect2(Point2(),get_size()),false); + draw_texture_rect(texture, Rect2(Point2(), get_size()), false); } break; case STRETCH_TILE: { - draw_texture_rect(texture,Rect2(Point2(),get_size()),true); + draw_texture_rect(texture, Rect2(Point2(), get_size()), true); } break; case STRETCH_KEEP: { - draw_texture_rect(texture,Rect2(Point2(),texture->get_size()),false); + draw_texture_rect(texture, Rect2(Point2(), texture->get_size()), false); } break; case STRETCH_KEEP_CENTERED: { - Vector2 ofs = (get_size() - texture->get_size())/2; - draw_texture_rect(texture,Rect2(ofs,texture->get_size()),false); + Vector2 ofs = (get_size() - texture->get_size()) / 2; + draw_texture_rect(texture, Rect2(ofs, texture->get_size()), false); } break; case STRETCH_KEEP_ASPECT_CENTERED: case STRETCH_KEEP_ASPECT: { - Size2 size=get_size(); - int tex_width = texture->get_width() * size.height / texture ->get_height(); + Size2 size = get_size(); + int tex_width = texture->get_width() * size.height / texture->get_height(); int tex_height = size.height; - if (tex_width>size.width) { - tex_width=size.width; - tex_height=texture->get_height() * tex_width / texture->get_width(); + if (tex_width > size.width) { + tex_width = size.width; + tex_height = texture->get_height() * tex_width / texture->get_width(); } int ofs_x = 0; int ofs_y = 0; - if (stretch_mode==STRETCH_KEEP_ASPECT_CENTERED) { - ofs_x+=(size.width - tex_width)/2; - ofs_y+=(size.height - tex_height)/2; + if (stretch_mode == STRETCH_KEEP_ASPECT_CENTERED) { + ofs_x += (size.width - tex_width) / 2; + ofs_y += (size.height - tex_height) / 2; } - draw_texture_rect(texture,Rect2(ofs_x,ofs_y,tex_width,tex_height)); + draw_texture_rect(texture, Rect2(ofs_x, ofs_y, tex_width, tex_height)); } break; case STRETCH_KEEP_ASPECT_COVERED: { Size2 size = get_size(); Size2 tex_size = texture->get_size(); - Size2 scaleSize(size.width/tex_size.width, size.height/tex_size.height); - float scale = scaleSize.width > scaleSize.height? scaleSize.width : scaleSize.height; + Size2 scaleSize(size.width / tex_size.width, size.height / tex_size.height); + float scale = scaleSize.width > scaleSize.height ? scaleSize.width : scaleSize.height; Size2 scaledTexSize = tex_size * scale; Point2 ofs = ((scaledTexSize - size) / scale).abs() / 2.0f; - draw_texture_rect_region(texture, Rect2(Point2(), size), Rect2(ofs, size/scale)); + draw_texture_rect_region(texture, Rect2(Point2(), size), Rect2(ofs, size / scale)); } break; } - } } @@ -102,32 +100,30 @@ Size2 TextureRect::get_minimum_size() const { } void TextureRect::_bind_methods() { - - ClassDB::bind_method(D_METHOD("set_texture","texture"), & TextureRect::set_texture ); - ClassDB::bind_method(D_METHOD("get_texture"), & TextureRect::get_texture ); - ClassDB::bind_method(D_METHOD("set_expand","enable"), & TextureRect::set_expand ); - ClassDB::bind_method(D_METHOD("has_expand"), & TextureRect::has_expand ); - ClassDB::bind_method(D_METHOD("set_stretch_mode","stretch_mode"), & TextureRect::set_stretch_mode ); - ClassDB::bind_method(D_METHOD("get_stretch_mode"), & TextureRect::get_stretch_mode ); - - ADD_PROPERTYNZ( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture"); - ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "expand" ), "set_expand","has_expand"); - ADD_PROPERTYNO( PropertyInfo( Variant::INT, "stretch_mode",PROPERTY_HINT_ENUM,"Scale On Expand (Compat),Scale,Tile,Keep,Keep Centered,Keep Aspect,Keep Aspect Centered,Keep Aspect Covered"), "set_stretch_mode", "get_stretch_mode"); - - BIND_CONSTANT( STRETCH_SCALE_ON_EXPAND ); - BIND_CONSTANT( STRETCH_SCALE ); - BIND_CONSTANT( STRETCH_TILE ); - BIND_CONSTANT( STRETCH_KEEP ); - BIND_CONSTANT( STRETCH_KEEP_CENTERED ); - BIND_CONSTANT( STRETCH_KEEP_ASPECT ); - BIND_CONSTANT( STRETCH_KEEP_ASPECT_CENTERED ); - BIND_CONSTANT( STRETCH_KEEP_ASPECT_COVERED ); + ClassDB::bind_method(D_METHOD("set_texture", "texture"), &TextureRect::set_texture); + ClassDB::bind_method(D_METHOD("get_texture"), &TextureRect::get_texture); + ClassDB::bind_method(D_METHOD("set_expand", "enable"), &TextureRect::set_expand); + ClassDB::bind_method(D_METHOD("has_expand"), &TextureRect::has_expand); + ClassDB::bind_method(D_METHOD("set_stretch_mode", "stretch_mode"), &TextureRect::set_stretch_mode); + ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureRect::get_stretch_mode); + + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture"); + ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "expand"), "set_expand", "has_expand"); + ADD_PROPERTYNO(PropertyInfo(Variant::INT, "stretch_mode", PROPERTY_HINT_ENUM, "Scale On Expand (Compat),Scale,Tile,Keep,Keep Centered,Keep Aspect,Keep Aspect Centered,Keep Aspect Covered"), "set_stretch_mode", "get_stretch_mode"); + + BIND_CONSTANT(STRETCH_SCALE_ON_EXPAND); + BIND_CONSTANT(STRETCH_SCALE); + BIND_CONSTANT(STRETCH_TILE); + BIND_CONSTANT(STRETCH_KEEP); + BIND_CONSTANT(STRETCH_KEEP_CENTERED); + BIND_CONSTANT(STRETCH_KEEP_ASPECT); + BIND_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED); + BIND_CONSTANT(STRETCH_KEEP_ASPECT_COVERED); } +void TextureRect::set_texture(const Ref<Texture> &p_tex) { -void TextureRect::set_texture(const Ref<Texture>& p_tex) { - - texture=p_tex; + texture = p_tex; update(); /* if (texture.is_valid()) @@ -141,10 +137,9 @@ Ref<Texture> TextureRect::get_texture() const { return texture; } - void TextureRect::set_expand(bool p_expand) { - expand=p_expand; + expand = p_expand; update(); minimum_size_changed(); } @@ -155,7 +150,7 @@ bool TextureRect::has_expand() const { void TextureRect::set_stretch_mode(StretchMode p_mode) { - stretch_mode=p_mode; + stretch_mode = p_mode; update(); } @@ -166,15 +161,10 @@ TextureRect::StretchMode TextureRect::get_stretch_mode() const { TextureRect::TextureRect() { - - expand=false; + expand = false; set_mouse_filter(MOUSE_FILTER_PASS); - stretch_mode=STRETCH_SCALE_ON_EXPAND; + stretch_mode = STRETCH_SCALE_ON_EXPAND; } - -TextureRect::~TextureRect() -{ +TextureRect::~TextureRect() { } - - diff --git a/scene/gui/texture_rect.h b/scene/gui/texture_rect.h index 0d72458909..2cc6b97b13 100644 --- a/scene/gui/texture_rect.h +++ b/scene/gui/texture_rect.h @@ -35,7 +35,8 @@ */ class TextureRect : public Control { - GDCLASS(TextureRect,Control); + GDCLASS(TextureRect, Control); + public: enum StretchMode { STRETCH_SCALE_ON_EXPAND, //default, for backwards compatibility @@ -47,22 +48,21 @@ public: STRETCH_KEEP_ASPECT_CENTERED, STRETCH_KEEP_ASPECT_COVERED, }; + private: bool expand; Ref<Texture> texture; StretchMode stretch_mode; -protected: +protected: void _notification(int p_what); virtual Size2 get_minimum_size() const; static void _bind_methods(); public: - - void set_texture(const Ref<Texture>& p_tex); + void set_texture(const Ref<Texture> &p_tex); Ref<Texture> get_texture() const; - void set_expand(bool p_expand); bool has_expand() const; @@ -71,8 +71,7 @@ public: TextureRect(); ~TextureRect(); - }; -VARIANT_ENUM_CAST( TextureRect::StretchMode ); +VARIANT_ENUM_CAST(TextureRect::StretchMode); #endif // TEXTURE_FRAME_H diff --git a/scene/gui/tool_button.h b/scene/gui/tool_button.h index ddeb34273b..02d2bfaef1 100644 --- a/scene/gui/tool_button.h +++ b/scene/gui/tool_button.h @@ -32,7 +32,8 @@ #include "scene/gui/button.h" class ToolButton : public Button { - GDCLASS(ToolButton,Button); + GDCLASS(ToolButton, Button); + public: ToolButton(); }; diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cpp index 292efdcc01..d63ef41239 100644 --- a/scene/gui/tree.cpp +++ b/scene/gui/tree.cpp @@ -27,23 +27,21 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "tree.h" -#include "print_string.h" -#include "os/os.h" -#include "os/keyboard.h" #include "global_config.h" #include "os/input.h" +#include "os/keyboard.h" +#include "os/os.h" +#include "print_string.h" #include "scene/main/viewport.h" - - void TreeItem::move_to_top() { - if (!parent || parent->childs==this) + if (!parent || parent->childs == this) return; //already on top TreeItem *prev = get_prev(); - prev->next=next; - next=parent->childs; - parent->childs=this; + prev->next = next; + next = parent->childs; + parent->childs = this; } void TreeItem::move_to_bottom() { @@ -51,276 +49,255 @@ void TreeItem::move_to_bottom() { if (!parent || !next) return; - while(next) { + while (next) { - if (parent->childs==this) - parent->childs=next; - TreeItem *n=next; - next=n->next; - n->next=this; + if (parent->childs == this) + parent->childs = next; + TreeItem *n = next; + next = n->next; + n->next = this; } } - Size2 TreeItem::Cell::get_icon_size() const { if (icon.is_null()) return Size2(); - if (icon_region==Rect2i()) + if (icon_region == Rect2i()) return icon->get_size(); else return icon_region.size; } -void TreeItem::Cell::draw_icon(const RID& p_where, const Point2& p_pos, const Size2& p_size) const{ +void TreeItem::Cell::draw_icon(const RID &p_where, const Point2 &p_pos, const Size2 &p_size) const { if (icon.is_null()) return; - Size2i dsize=(p_size==Size2()) ? icon->get_size() : p_size; + Size2i dsize = (p_size == Size2()) ? icon->get_size() : p_size; - if (icon_region==Rect2i()) { + if (icon_region == Rect2i()) { - icon->draw_rect_region(p_where,Rect2(p_pos,dsize),Rect2(Point2(),icon->get_size())); + icon->draw_rect_region(p_where, Rect2(p_pos, dsize), Rect2(Point2(), icon->get_size())); } else { - icon->draw_rect_region(p_where,Rect2(p_pos,dsize),icon_region); + icon->draw_rect_region(p_where, Rect2(p_pos, dsize), icon_region); } - } - void TreeItem::_changed_notify(int p_cell) { - tree->item_changed(p_cell,this); + tree->item_changed(p_cell, this); } void TreeItem::_changed_notify() { - tree->item_changed(-1,this); + tree->item_changed(-1, this); } void TreeItem::_cell_selected(int p_cell) { - tree->item_selected(p_cell,this); + tree->item_selected(p_cell, this); } void TreeItem::_cell_deselected(int p_cell) { - tree->item_deselected(p_cell,this); + tree->item_deselected(p_cell, this); } /* cell mode */ -void TreeItem::set_cell_mode( int p_column, TreeCellMode p_mode ) { - - ERR_FAIL_INDEX( p_column, cells.size() ); - Cell&c=cells[p_column]; - c.mode=p_mode; - c.min=0; - c.max=100; - c.step=1; - c.val=0; - c.checked=false; - c.icon=Ref<Texture>(); - c.text=""; - c.icon_max_w=0; +void TreeItem::set_cell_mode(int p_column, TreeCellMode p_mode) { + + ERR_FAIL_INDEX(p_column, cells.size()); + Cell &c = cells[p_column]; + c.mode = p_mode; + c.min = 0; + c.max = 100; + c.step = 1; + c.val = 0; + c.checked = false; + c.icon = Ref<Texture>(); + c.text = ""; + c.icon_max_w = 0; _changed_notify(p_column); } -TreeItem::TreeCellMode TreeItem::get_cell_mode( int p_column ) const { +TreeItem::TreeCellMode TreeItem::get_cell_mode(int p_column) const { - ERR_FAIL_INDEX_V( p_column, cells.size(), TreeItem::CELL_MODE_STRING ); + ERR_FAIL_INDEX_V(p_column, cells.size(), TreeItem::CELL_MODE_STRING); return cells[p_column].mode; } - /* check mode */ -void TreeItem::set_checked(int p_column,bool p_checked) { +void TreeItem::set_checked(int p_column, bool p_checked) { - ERR_FAIL_INDEX( p_column, cells.size() ); - cells[p_column].checked=p_checked; + ERR_FAIL_INDEX(p_column, cells.size()); + cells[p_column].checked = p_checked; _changed_notify(p_column); - } bool TreeItem::is_checked(int p_column) const { - ERR_FAIL_INDEX_V( p_column, cells.size(), false ); + ERR_FAIL_INDEX_V(p_column, cells.size(), false); return cells[p_column].checked; } +void TreeItem::set_text(int p_column, String p_text) { -void TreeItem::set_text(int p_column,String p_text) { + ERR_FAIL_INDEX(p_column, cells.size()); + cells[p_column].text = p_text; - ERR_FAIL_INDEX( p_column, cells.size() ); - cells[p_column].text=p_text; + if (cells[p_column].mode == TreeItem::CELL_MODE_RANGE || cells[p_column].mode == TreeItem::CELL_MODE_RANGE_EXPRESSION) { - if (cells[p_column].mode==TreeItem::CELL_MODE_RANGE || cells[p_column].mode==TreeItem::CELL_MODE_RANGE_EXPRESSION) { - - cells[p_column].min=0; - cells[p_column].max=p_text.get_slice_count(","); - cells[p_column].step=0; + cells[p_column].min = 0; + cells[p_column].max = p_text.get_slice_count(","); + cells[p_column].step = 0; } _changed_notify(p_column); - } String TreeItem::get_text(int p_column) const { - ERR_FAIL_INDEX_V( p_column, cells.size(), "" ); + ERR_FAIL_INDEX_V(p_column, cells.size(), ""); return cells[p_column].text; - } -void TreeItem::set_suffix(int p_column,String p_suffix) { +void TreeItem::set_suffix(int p_column, String p_suffix) { - ERR_FAIL_INDEX( p_column, cells.size() ); - cells[p_column].suffix=p_suffix; + ERR_FAIL_INDEX(p_column, cells.size()); + cells[p_column].suffix = p_suffix; _changed_notify(p_column); - } String TreeItem::get_suffix(int p_column) const { - ERR_FAIL_INDEX_V( p_column, cells.size(), "" ); + ERR_FAIL_INDEX_V(p_column, cells.size(), ""); return cells[p_column].suffix; - } -void TreeItem::set_icon(int p_column,const Ref<Texture>& p_icon) { +void TreeItem::set_icon(int p_column, const Ref<Texture> &p_icon) { - ERR_FAIL_INDEX( p_column, cells.size() ); - cells[p_column].icon=p_icon; + ERR_FAIL_INDEX(p_column, cells.size()); + cells[p_column].icon = p_icon; _changed_notify(p_column); - } Ref<Texture> TreeItem::get_icon(int p_column) const { - ERR_FAIL_INDEX_V( p_column, cells.size(), Ref<Texture>() ); + ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture>()); return cells[p_column].icon; - } -void TreeItem::set_icon_region(int p_column,const Rect2& p_icon_region) { +void TreeItem::set_icon_region(int p_column, const Rect2 &p_icon_region) { - ERR_FAIL_INDEX( p_column, cells.size() ); - cells[p_column].icon_region=p_icon_region; + ERR_FAIL_INDEX(p_column, cells.size()); + cells[p_column].icon_region = p_icon_region; _changed_notify(p_column); } Rect2 TreeItem::get_icon_region(int p_column) const { - ERR_FAIL_INDEX_V( p_column, cells.size(), Rect2() ); + ERR_FAIL_INDEX_V(p_column, cells.size(), Rect2()); return cells[p_column].icon_region; } -void TreeItem::set_icon_max_width(int p_column,int p_max) { +void TreeItem::set_icon_max_width(int p_column, int p_max) { - ERR_FAIL_INDEX( p_column, cells.size() ); - cells[p_column].icon_max_w=p_max; + ERR_FAIL_INDEX(p_column, cells.size()); + cells[p_column].icon_max_w = p_max; _changed_notify(p_column); } int TreeItem::get_icon_max_width(int p_column) const { - ERR_FAIL_INDEX_V( p_column, cells.size(), 0); + ERR_FAIL_INDEX_V(p_column, cells.size(), 0); return cells[p_column].icon_max_w; - } - /* range works for mode number or mode combo */ -void TreeItem::set_range(int p_column,double p_value) { +void TreeItem::set_range(int p_column, double p_value) { - ERR_FAIL_INDEX( p_column, cells.size() ); - if (cells[p_column].step>0) - p_value=Math::stepify( p_value, cells[p_column].step ); - if (p_value<cells[p_column].min) - p_value=cells[p_column].min; - if (p_value>cells[p_column].max) - p_value=cells[p_column].max; + ERR_FAIL_INDEX(p_column, cells.size()); + if (cells[p_column].step > 0) + p_value = Math::stepify(p_value, cells[p_column].step); + if (p_value < cells[p_column].min) + p_value = cells[p_column].min; + if (p_value > cells[p_column].max) + p_value = cells[p_column].max; - cells[p_column].val=p_value; + cells[p_column].val = p_value; _changed_notify(p_column); - } double TreeItem::get_range(int p_column) const { - ERR_FAIL_INDEX_V( p_column, cells.size(), 0 ); + ERR_FAIL_INDEX_V(p_column, cells.size(), 0); return cells[p_column].val; } - bool TreeItem::is_range_exponential(int p_column) const { - ERR_FAIL_INDEX_V( p_column, cells.size(), false); + ERR_FAIL_INDEX_V(p_column, cells.size(), false); return cells[p_column].expr; - } -void TreeItem::set_range_config(int p_column,double p_min,double p_max,double p_step,bool p_exp) { +void TreeItem::set_range_config(int p_column, double p_min, double p_max, double p_step, bool p_exp) { - ERR_FAIL_INDEX( p_column, cells.size() ); - cells[p_column].min=p_min; - cells[p_column].max=p_max; - cells[p_column].step=p_step; - cells[p_column].expr=p_exp; + ERR_FAIL_INDEX(p_column, cells.size()); + cells[p_column].min = p_min; + cells[p_column].max = p_max; + cells[p_column].step = p_step; + cells[p_column].expr = p_exp; _changed_notify(p_column); - } -void TreeItem::get_range_config(int p_column,double& r_min,double& r_max,double &r_step) const { - - ERR_FAIL_INDEX( p_column, cells.size() ); - r_min=cells[p_column].min; - r_max=cells[p_column].max; - r_step=cells[p_column].step; +void TreeItem::get_range_config(int p_column, double &r_min, double &r_max, double &r_step) const { + ERR_FAIL_INDEX(p_column, cells.size()); + r_min = cells[p_column].min; + r_max = cells[p_column].max; + r_step = cells[p_column].step; } -void TreeItem::set_metadata(int p_column,const Variant& p_meta) { - - ERR_FAIL_INDEX( p_column, cells.size() ); - cells[p_column].meta=p_meta; +void TreeItem::set_metadata(int p_column, const Variant &p_meta) { + ERR_FAIL_INDEX(p_column, cells.size()); + cells[p_column].meta = p_meta; } Variant TreeItem::get_metadata(int p_column) const { - ERR_FAIL_INDEX_V( p_column, cells.size(), Variant() ); + ERR_FAIL_INDEX_V(p_column, cells.size(), Variant()); return cells[p_column].meta; } -void TreeItem::set_custom_draw(int p_column,Object *p_object,const StringName& p_callback) { +void TreeItem::set_custom_draw(int p_column, Object *p_object, const StringName &p_callback) { - ERR_FAIL_INDEX( p_column, cells.size() ); + ERR_FAIL_INDEX(p_column, cells.size()); ERR_FAIL_NULL(p_object); - cells[p_column].custom_draw_obj=p_object->get_instance_ID(); - cells[p_column].custom_draw_callback=p_callback; - + cells[p_column].custom_draw_obj = p_object->get_instance_ID(); + cells[p_column].custom_draw_callback = p_callback; } void TreeItem::set_collapsed(bool p_collapsed) { - if (collapsed==p_collapsed) + if (collapsed == p_collapsed) return; - collapsed=p_collapsed; + collapsed = p_collapsed; TreeItem *ci = tree->selected_item; if (ci) { - while (ci && ci!=this) { + while (ci && ci != this) { - ci=ci->parent; + ci = ci->parent; } if (ci) { // collapsing cursor/selectd, move it! - if (tree->select_mode==Tree::SELECT_MULTI) { + if (tree->select_mode == Tree::SELECT_MULTI) { - tree->selected_item=this; + tree->selected_item = this; emit_signal("cell_selected"); } else { @@ -329,13 +306,11 @@ void TreeItem::set_collapsed(bool p_collapsed) { tree->update(); } - } _changed_notify(); if (tree) - tree->emit_signal("item_collapsed",this); - + tree->emit_signal("item_collapsed", this); } bool TreeItem::is_collapsed() { @@ -343,7 +318,6 @@ bool TreeItem::is_collapsed() { return collapsed; } - TreeItem *TreeItem::get_next() { return next; @@ -351,12 +325,12 @@ TreeItem *TreeItem::get_next() { TreeItem *TreeItem::get_prev() { - if (!parent || parent->childs==this) + if (!parent || parent->childs == this) return NULL; TreeItem *prev = parent->childs; - while(prev && prev->next!=this) - prev=prev->next; + while (prev && prev->next != this) + prev = prev->next; return prev; } @@ -371,61 +345,54 @@ TreeItem *TreeItem::get_children() { return childs; } - TreeItem *TreeItem::get_prev_visible() { - TreeItem *current=this; + TreeItem *current = this; TreeItem *prev = current->get_prev(); - if (!prev) { - current=current->parent; - if (!current || (current==tree->root && tree->hide_root)) + current = current->parent; + if (!current || (current == tree->root && tree->hide_root)) return NULL; } else { - - current=prev; - while( !current->collapsed && current->childs ) { + current = prev; + while (!current->collapsed && current->childs) { //go to the very end current = current->childs; while (current->next) - current=current->next; + current = current->next; } - - } return current; } - TreeItem *TreeItem::get_next_visible() { - TreeItem *current=this; - + TreeItem *current = this; if (!current->collapsed && current->childs) { - current=current->childs; + current = current->childs; } else if (current->next) { - current=current->next; + current = current->next; } else { - while(current && !current->next) { + while (current && !current->next) { - current=current->parent; + current = current->parent; } - if (current==NULL) + if (current == NULL) return NULL; else - current=current->next; + current = current->next; } return current; @@ -434,117 +401,111 @@ TreeItem *TreeItem::get_next_visible() { void TreeItem::remove_child(TreeItem *p_item) { ERR_FAIL_NULL(p_item); - TreeItem **c=&childs; + TreeItem **c = &childs; while (*c) { - if ( (*c) == p_item ) { + if ((*c) == p_item) { TreeItem *aux = *c; - *c=(*c)->next; + *c = (*c)->next; aux->parent = NULL; return; } - c=&(*c)->next; + c = &(*c)->next; } ERR_FAIL(); } +void TreeItem::set_selectable(int p_column, bool p_selectable) { -void TreeItem::set_selectable(int p_column,bool p_selectable) { - - ERR_FAIL_INDEX( p_column, cells.size() ); - cells[p_column].selectable=p_selectable; + ERR_FAIL_INDEX(p_column, cells.size()); + cells[p_column].selectable = p_selectable; } bool TreeItem::is_selectable(int p_column) const { - ERR_FAIL_INDEX_V( p_column, cells.size(), false ); + ERR_FAIL_INDEX_V(p_column, cells.size(), false); return cells[p_column].selectable; - } bool TreeItem::is_selected(int p_column) { - ERR_FAIL_INDEX_V( p_column, cells.size(), false ); + ERR_FAIL_INDEX_V(p_column, cells.size(), false); return cells[p_column].selectable && cells[p_column].selected; } void TreeItem::set_as_cursor(int p_column) { - ERR_FAIL_INDEX( p_column, cells.size() ); + ERR_FAIL_INDEX(p_column, cells.size()); if (!tree) return; - if (tree->select_mode!=Tree::SELECT_MULTI) + if (tree->select_mode != Tree::SELECT_MULTI) return; - tree->selected_item=this; - tree->selected_col=p_column; + tree->selected_item = this; + tree->selected_col = p_column; tree->update(); } void TreeItem::select(int p_column) { - ERR_FAIL_INDEX( p_column, cells.size() ); + ERR_FAIL_INDEX(p_column, cells.size()); _cell_selected(p_column); } void TreeItem::deselect(int p_column) { - ERR_FAIL_INDEX( p_column, cells.size() ); + ERR_FAIL_INDEX(p_column, cells.size()); _cell_deselected(p_column); } -void TreeItem::add_button(int p_column, const Ref<Texture>& p_button, int p_id, bool p_disabled) { +void TreeItem::add_button(int p_column, const Ref<Texture> &p_button, int p_id, bool p_disabled) { - - ERR_FAIL_INDEX( p_column, cells.size() ); + ERR_FAIL_INDEX(p_column, cells.size()); ERR_FAIL_COND(!p_button.is_valid()); TreeItem::Cell::Button button; - button.texture=p_button; - if (p_id<0) - p_id=cells[p_column].buttons.size(); - button.id=p_id; - button.disabled=p_disabled; + button.texture = p_button; + if (p_id < 0) + p_id = cells[p_column].buttons.size(); + button.id = p_id; + button.disabled = p_disabled; cells[p_column].buttons.push_back(button); _changed_notify(p_column); } int TreeItem::get_button_count(int p_column) const { - ERR_FAIL_INDEX_V( p_column, cells.size(), -1 ); + ERR_FAIL_INDEX_V(p_column, cells.size(), -1); return cells[p_column].buttons.size(); - } -Ref<Texture> TreeItem::get_button(int p_column,int p_idx) const { - ERR_FAIL_INDEX_V( p_column, cells.size(), Ref<Texture>() ); - ERR_FAIL_INDEX_V( p_idx, cells[p_column].buttons.size(), Ref<Texture>() ); +Ref<Texture> TreeItem::get_button(int p_column, int p_idx) const { + ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture>()); + ERR_FAIL_INDEX_V(p_idx, cells[p_column].buttons.size(), Ref<Texture>()); return cells[p_column].buttons[p_idx].texture; - } -int TreeItem::get_button_id(int p_column,int p_idx) const { - ERR_FAIL_INDEX_V( p_column, cells.size(), -1 ); - ERR_FAIL_INDEX_V( p_idx, cells[p_column].buttons.size(), -1 ); +int TreeItem::get_button_id(int p_column, int p_idx) const { + ERR_FAIL_INDEX_V(p_column, cells.size(), -1); + ERR_FAIL_INDEX_V(p_idx, cells[p_column].buttons.size(), -1); return cells[p_column].buttons[p_idx].id; - } -void TreeItem::erase_button(int p_column,int p_idx) { +void TreeItem::erase_button(int p_column, int p_idx) { - ERR_FAIL_INDEX( p_column, cells.size() ); - ERR_FAIL_INDEX( p_idx, cells[p_column].buttons.size() ); + ERR_FAIL_INDEX(p_column, cells.size()); + ERR_FAIL_INDEX(p_idx, cells[p_column].buttons.size()); cells[p_column].buttons.remove(p_idx); _changed_notify(p_column); } -int TreeItem::get_button_by_id(int p_column,int p_id) const { +int TreeItem::get_button_by_id(int p_column, int p_id) const { - ERR_FAIL_INDEX_V( p_column, cells.size(),-1 ); - for(int i=0;i<cells[p_column].buttons.size();i++) { + ERR_FAIL_INDEX_V(p_column, cells.size(), -1); + for (int i = 0; i < cells[p_column].buttons.size(); i++) { - if (cells[p_column].buttons[i].id==p_id) + if (cells[p_column].buttons[i].id == p_id) return i; } @@ -553,200 +514,190 @@ int TreeItem::get_button_by_id(int p_column,int p_id) const { bool TreeItem::is_button_disabled(int p_column, int p_idx) const { - ERR_FAIL_INDEX_V( p_column, cells.size(), false ); - ERR_FAIL_INDEX_V( p_idx, cells[p_column].buttons.size(), false ); + ERR_FAIL_INDEX_V(p_column, cells.size(), false); + ERR_FAIL_INDEX_V(p_idx, cells[p_column].buttons.size(), false); return cells[p_column].buttons[p_idx].disabled; - } -void TreeItem::set_button(int p_column,int p_idx,const Ref<Texture>& p_button){ +void TreeItem::set_button(int p_column, int p_idx, const Ref<Texture> &p_button) { - ERR_FAIL_COND( p_button.is_null() ); - ERR_FAIL_INDEX( p_column, cells.size() ); - ERR_FAIL_INDEX( p_idx, cells[p_column].buttons.size() ); - cells[p_column].buttons[p_idx].texture=p_button; + ERR_FAIL_COND(p_button.is_null()); + ERR_FAIL_INDEX(p_column, cells.size()); + ERR_FAIL_INDEX(p_idx, cells[p_column].buttons.size()); + cells[p_column].buttons[p_idx].texture = p_button; _changed_notify(p_column); - } -void TreeItem::set_button_color(int p_column,int p_idx,const Color& p_color) { +void TreeItem::set_button_color(int p_column, int p_idx, const Color &p_color) { - ERR_FAIL_INDEX( p_column, cells.size() ); - ERR_FAIL_INDEX( p_idx, cells[p_column].buttons.size() ); - cells[p_column].buttons[p_idx].color=p_color; + ERR_FAIL_INDEX(p_column, cells.size()); + ERR_FAIL_INDEX(p_idx, cells[p_column].buttons.size()); + cells[p_column].buttons[p_idx].color = p_color; _changed_notify(p_column); - } -void TreeItem::set_editable(int p_column,bool p_editable) { +void TreeItem::set_editable(int p_column, bool p_editable) { - ERR_FAIL_INDEX( p_column, cells.size() ); - cells[p_column].editable=p_editable; + ERR_FAIL_INDEX(p_column, cells.size()); + cells[p_column].editable = p_editable; _changed_notify(p_column); } bool TreeItem::is_editable(int p_column) { - ERR_FAIL_INDEX_V( p_column, cells.size(), false ); + ERR_FAIL_INDEX_V(p_column, cells.size(), false); return cells[p_column].editable; } +void TreeItem::set_custom_color(int p_column, const Color &p_color) { -void TreeItem::set_custom_color(int p_column,const Color& p_color) { - - ERR_FAIL_INDEX( p_column, cells.size() ); - cells[p_column].custom_color=true; - cells[p_column].color=p_color; + ERR_FAIL_INDEX(p_column, cells.size()); + cells[p_column].custom_color = true; + cells[p_column].color = p_color; _changed_notify(p_column); } Color TreeItem::get_custom_color(int p_column) const { - ERR_FAIL_INDEX_V( p_column, cells.size(), Color() ); + ERR_FAIL_INDEX_V(p_column, cells.size(), Color()); if (!cells[p_column].custom_color) return Color(); return cells[p_column].color; - } void TreeItem::clear_custom_color(int p_column) { - ERR_FAIL_INDEX( p_column, cells.size() ); - cells[p_column].custom_color=false; - cells[p_column].color=Color(); + ERR_FAIL_INDEX(p_column, cells.size()); + cells[p_column].custom_color = false; + cells[p_column].color = Color(); _changed_notify(p_column); } +void TreeItem::set_tooltip(int p_column, const String &p_tooltip) { -void TreeItem::set_tooltip(int p_column, const String& p_tooltip) { - - ERR_FAIL_INDEX( p_column, cells.size() ); - cells[p_column].tooltip=p_tooltip; - + ERR_FAIL_INDEX(p_column, cells.size()); + cells[p_column].tooltip = p_tooltip; } -String TreeItem::get_tooltip(int p_column) const{ +String TreeItem::get_tooltip(int p_column) const { - ERR_FAIL_INDEX_V( p_column, cells.size(), "" ); + ERR_FAIL_INDEX_V(p_column, cells.size(), ""); return cells[p_column].tooltip; } -void TreeItem::set_custom_bg_color(int p_column,const Color& p_color,bool p_bg_outline) { +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; + 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); } void TreeItem::clear_custom_bg_color(int p_column) { - ERR_FAIL_INDEX( p_column, cells.size() ); - cells[p_column].custom_bg_color=false; - cells[p_column].bg_color=Color(); + ERR_FAIL_INDEX(p_column, cells.size()); + cells[p_column].custom_bg_color = false; + cells[p_column].bg_color = Color(); _changed_notify(p_column); } Color TreeItem::get_custom_bg_color(int p_column) const { - ERR_FAIL_INDEX_V( p_column, cells.size(), Color() ); + ERR_FAIL_INDEX_V(p_column, cells.size(), Color()); if (!cells[p_column].custom_bg_color) return Color(); return cells[p_column].bg_color; - } void TreeItem::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_cell_mode","column","mode"),&TreeItem::set_cell_mode); - ClassDB::bind_method(D_METHOD("get_cell_mode","column"),&TreeItem::get_cell_mode); - - ClassDB::bind_method(D_METHOD("set_checked","column","checked"),&TreeItem::set_checked); - ClassDB::bind_method(D_METHOD("is_checked","column"),&TreeItem::is_checked); - - ClassDB::bind_method(D_METHOD("set_text","column","text"),&TreeItem::set_text); - ClassDB::bind_method(D_METHOD("get_text","column"),&TreeItem::get_text); + ClassDB::bind_method(D_METHOD("set_cell_mode", "column", "mode"), &TreeItem::set_cell_mode); + ClassDB::bind_method(D_METHOD("get_cell_mode", "column"), &TreeItem::get_cell_mode); - ClassDB::bind_method(D_METHOD("set_icon","column","texture:Texture"),&TreeItem::set_icon); - ClassDB::bind_method(D_METHOD("get_icon:Texture","column"),&TreeItem::get_icon); + ClassDB::bind_method(D_METHOD("set_checked", "column", "checked"), &TreeItem::set_checked); + ClassDB::bind_method(D_METHOD("is_checked", "column"), &TreeItem::is_checked); - ClassDB::bind_method(D_METHOD("set_icon_region","column","region"),&TreeItem::set_icon_region); - ClassDB::bind_method(D_METHOD("get_icon_region","column"),&TreeItem::get_icon_region); + ClassDB::bind_method(D_METHOD("set_text", "column", "text"), &TreeItem::set_text); + ClassDB::bind_method(D_METHOD("get_text", "column"), &TreeItem::get_text); - ClassDB::bind_method(D_METHOD("set_icon_max_width","column","width"),&TreeItem::set_icon_max_width); - ClassDB::bind_method(D_METHOD("get_icon_max_width","column"),&TreeItem::get_icon_max_width); + ClassDB::bind_method(D_METHOD("set_icon", "column", "texture:Texture"), &TreeItem::set_icon); + ClassDB::bind_method(D_METHOD("get_icon:Texture", "column"), &TreeItem::get_icon); - ClassDB::bind_method(D_METHOD("set_range","column","value"),&TreeItem::set_range); - ClassDB::bind_method(D_METHOD("get_range","column"),&TreeItem::get_range); - ClassDB::bind_method(D_METHOD("set_range_config","column","min","max","step","expr"),&TreeItem::set_range_config,DEFVAL(false)); - ClassDB::bind_method(D_METHOD("get_range_config","column"),&TreeItem::_get_range_config); + ClassDB::bind_method(D_METHOD("set_icon_region", "column", "region"), &TreeItem::set_icon_region); + ClassDB::bind_method(D_METHOD("get_icon_region", "column"), &TreeItem::get_icon_region); - ClassDB::bind_method(D_METHOD("set_metadata","column","meta"),&TreeItem::set_metadata); - ClassDB::bind_method(D_METHOD("get_metadata","column"),&TreeItem::get_metadata); + ClassDB::bind_method(D_METHOD("set_icon_max_width", "column", "width"), &TreeItem::set_icon_max_width); + ClassDB::bind_method(D_METHOD("get_icon_max_width", "column"), &TreeItem::get_icon_max_width); - ClassDB::bind_method(D_METHOD("set_custom_draw","column","object","callback"),&TreeItem::set_custom_draw); + ClassDB::bind_method(D_METHOD("set_range", "column", "value"), &TreeItem::set_range); + ClassDB::bind_method(D_METHOD("get_range", "column"), &TreeItem::get_range); + ClassDB::bind_method(D_METHOD("set_range_config", "column", "min", "max", "step", "expr"), &TreeItem::set_range_config, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("get_range_config", "column"), &TreeItem::_get_range_config); - ClassDB::bind_method(D_METHOD("set_collapsed","enable"),&TreeItem::set_collapsed); - ClassDB::bind_method(D_METHOD("is_collapsed"),&TreeItem::is_collapsed); + ClassDB::bind_method(D_METHOD("set_metadata", "column", "meta"), &TreeItem::set_metadata); + ClassDB::bind_method(D_METHOD("get_metadata", "column"), &TreeItem::get_metadata); - ClassDB::bind_method(D_METHOD("get_next:TreeItem"),&TreeItem::get_next); - ClassDB::bind_method(D_METHOD("get_prev:TreeItem"),&TreeItem::get_prev); - ClassDB::bind_method(D_METHOD("get_parent:TreeItem"),&TreeItem::get_parent); - ClassDB::bind_method(D_METHOD("get_children:TreeItem"),&TreeItem::get_children); + ClassDB::bind_method(D_METHOD("set_custom_draw", "column", "object", "callback"), &TreeItem::set_custom_draw); - ClassDB::bind_method(D_METHOD("get_next_visible:TreeItem"),&TreeItem::get_next_visible); - ClassDB::bind_method(D_METHOD("get_prev_visible:TreeItem"),&TreeItem::get_prev_visible); + ClassDB::bind_method(D_METHOD("set_collapsed", "enable"), &TreeItem::set_collapsed); + ClassDB::bind_method(D_METHOD("is_collapsed"), &TreeItem::is_collapsed); - ClassDB::bind_method(D_METHOD("remove_child:TreeItem","child"),&TreeItem::_remove_child); + ClassDB::bind_method(D_METHOD("get_next:TreeItem"), &TreeItem::get_next); + ClassDB::bind_method(D_METHOD("get_prev:TreeItem"), &TreeItem::get_prev); + ClassDB::bind_method(D_METHOD("get_parent:TreeItem"), &TreeItem::get_parent); + ClassDB::bind_method(D_METHOD("get_children:TreeItem"), &TreeItem::get_children); - ClassDB::bind_method(D_METHOD("set_selectable","column","selectable"),&TreeItem::set_selectable); - ClassDB::bind_method(D_METHOD("is_selectable","column"),&TreeItem::is_selectable); + ClassDB::bind_method(D_METHOD("get_next_visible:TreeItem"), &TreeItem::get_next_visible); + ClassDB::bind_method(D_METHOD("get_prev_visible:TreeItem"), &TreeItem::get_prev_visible); - ClassDB::bind_method(D_METHOD("is_selected","column"),&TreeItem::is_selected); - ClassDB::bind_method(D_METHOD("select","column"),&TreeItem::select); - ClassDB::bind_method(D_METHOD("deselect","column"),&TreeItem::deselect); + ClassDB::bind_method(D_METHOD("remove_child:TreeItem", "child"), &TreeItem::_remove_child); - ClassDB::bind_method(D_METHOD("set_editable","column","enabled"),&TreeItem::set_editable); - ClassDB::bind_method(D_METHOD("is_editable","column"),&TreeItem::is_editable); + ClassDB::bind_method(D_METHOD("set_selectable", "column", "selectable"), &TreeItem::set_selectable); + ClassDB::bind_method(D_METHOD("is_selectable", "column"), &TreeItem::is_selectable); - ClassDB::bind_method(D_METHOD("set_custom_color","column","color"),&TreeItem::set_custom_color); - ClassDB::bind_method(D_METHOD("clear_custom_color","column"),&TreeItem::clear_custom_color); + ClassDB::bind_method(D_METHOD("is_selected", "column"), &TreeItem::is_selected); + ClassDB::bind_method(D_METHOD("select", "column"), &TreeItem::select); + ClassDB::bind_method(D_METHOD("deselect", "column"), &TreeItem::deselect); - ClassDB::bind_method(D_METHOD("set_custom_bg_color","column","color","just_outline"),&TreeItem::set_custom_bg_color,DEFVAL(false)); - ClassDB::bind_method(D_METHOD("clear_custom_bg_color","column"),&TreeItem::clear_custom_bg_color); - ClassDB::bind_method(D_METHOD("get_custom_bg_color","column"),&TreeItem::get_custom_bg_color); + ClassDB::bind_method(D_METHOD("set_editable", "column", "enabled"), &TreeItem::set_editable); + ClassDB::bind_method(D_METHOD("is_editable", "column"), &TreeItem::is_editable); - ClassDB::bind_method(D_METHOD("add_button","column","button:Texture","button_idx","disabled"),&TreeItem::add_button,DEFVAL(-1),DEFVAL(false)); - ClassDB::bind_method(D_METHOD("get_button_count","column"),&TreeItem::get_button_count); - ClassDB::bind_method(D_METHOD("get_button:Texture","column","button_idx"),&TreeItem::get_button); - ClassDB::bind_method(D_METHOD("set_button","column","button_idx","button:Texture"),&TreeItem::set_button); - ClassDB::bind_method(D_METHOD("erase_button","column","button_idx"),&TreeItem::erase_button); - ClassDB::bind_method(D_METHOD("is_button_disabled","column","button_idx"),&TreeItem::is_button_disabled); + ClassDB::bind_method(D_METHOD("set_custom_color", "column", "color"), &TreeItem::set_custom_color); + ClassDB::bind_method(D_METHOD("clear_custom_color", "column"), &TreeItem::clear_custom_color); - ClassDB::bind_method(D_METHOD("set_tooltip","column","tooltip"),&TreeItem::set_tooltip); - ClassDB::bind_method(D_METHOD("get_tooltip","column"),&TreeItem::get_tooltip); + ClassDB::bind_method(D_METHOD("set_custom_bg_color", "column", "color", "just_outline"), &TreeItem::set_custom_bg_color, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("clear_custom_bg_color", "column"), &TreeItem::clear_custom_bg_color); + ClassDB::bind_method(D_METHOD("get_custom_bg_color", "column"), &TreeItem::get_custom_bg_color); - ClassDB::bind_method(D_METHOD("move_to_top"),&TreeItem::move_to_top); - ClassDB::bind_method(D_METHOD("move_to_bottom"),&TreeItem::move_to_bottom); + ClassDB::bind_method(D_METHOD("add_button", "column", "button:Texture", "button_idx", "disabled"), &TreeItem::add_button, DEFVAL(-1), DEFVAL(false)); + ClassDB::bind_method(D_METHOD("get_button_count", "column"), &TreeItem::get_button_count); + ClassDB::bind_method(D_METHOD("get_button:Texture", "column", "button_idx"), &TreeItem::get_button); + ClassDB::bind_method(D_METHOD("set_button", "column", "button_idx", "button:Texture"), &TreeItem::set_button); + ClassDB::bind_method(D_METHOD("erase_button", "column", "button_idx"), &TreeItem::erase_button); + ClassDB::bind_method(D_METHOD("is_button_disabled", "column", "button_idx"), &TreeItem::is_button_disabled); - BIND_CONSTANT( CELL_MODE_STRING ); - BIND_CONSTANT( CELL_MODE_CHECK ); - BIND_CONSTANT( CELL_MODE_RANGE ); - BIND_CONSTANT( CELL_MODE_RANGE_EXPRESSION ); - BIND_CONSTANT( CELL_MODE_ICON ); - BIND_CONSTANT( CELL_MODE_CUSTOM ); + ClassDB::bind_method(D_METHOD("set_tooltip", "column", "tooltip"), &TreeItem::set_tooltip); + ClassDB::bind_method(D_METHOD("get_tooltip", "column"), &TreeItem::get_tooltip); + ClassDB::bind_method(D_METHOD("move_to_top"), &TreeItem::move_to_top); + ClassDB::bind_method(D_METHOD("move_to_bottom"), &TreeItem::move_to_bottom); + BIND_CONSTANT(CELL_MODE_STRING); + BIND_CONSTANT(CELL_MODE_CHECK); + BIND_CONSTANT(CELL_MODE_RANGE); + BIND_CONSTANT(CELL_MODE_RANGE_EXPRESSION); + BIND_CONSTANT(CELL_MODE_ICON); + BIND_CONSTANT(CELL_MODE_CUSTOM); } void TreeItem::clear_children() { - TreeItem *c=childs; + TreeItem *c = childs; while (c) { - TreeItem *aux=c; - c=c->get_next(); - aux->parent=0; // so it wont try to recursively autoremove from me in here - memdelete( aux ); + TreeItem *aux = c; + c = c->get_next(); + aux->parent = 0; // so it wont try to recursively autoremove from me in here + memdelete(aux); } childs = 0; @@ -754,13 +705,12 @@ void TreeItem::clear_children() { TreeItem::TreeItem(Tree *p_tree) { - tree=p_tree; - collapsed=false; - - parent=0; // parent item - next=0; // next in list - childs=0; //child items + tree = p_tree; + collapsed = false; + parent = 0; // parent item + next = 0; // next in list + childs = 0; //child items } TreeItem::~TreeItem() { @@ -770,33 +720,29 @@ TreeItem::~TreeItem() { if (parent) parent->remove_child(this); - if (tree && tree->root==this) { + if (tree && tree->root == this) { - tree->root=0; + tree->root = 0; } - if (tree && tree->popup_edited_item==this) { - tree->popup_edited_item=NULL; - tree->pressing_for_editor=false; - + if (tree && tree->popup_edited_item == this) { + tree->popup_edited_item = NULL; + tree->pressing_for_editor = false; } - if (tree && tree->selected_item==this) - tree->selected_item=NULL; + 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->drop_mode_over==this) - tree->drop_mode_over=NULL; + if (tree && tree->single_select_defer == this) + tree->single_select_defer = 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; + if (tree && tree->edited_item == this) { + tree->edited_item = NULL; + tree->pressing_for_editor = false; } - - } /**********************************************/ @@ -806,41 +752,37 @@ TreeItem::~TreeItem() { /**********************************************/ /**********************************************/ - - - - void Tree::update_cache() { cache.font = get_font("font"); cache.tb_font = get_font("title_button_font"); cache.bg = get_stylebox("bg"); - cache.selected= get_stylebox("selected"); - cache.selected_focus= get_stylebox("selected_focus"); + cache.selected = get_stylebox("selected"); + cache.selected_focus = get_stylebox("selected_focus"); cache.cursor = get_stylebox("cursor"); cache.cursor_unfocus = get_stylebox("cursor_unfocused"); - cache.button_pressed= get_stylebox("button_pressed"); - - cache.checked=get_icon("checked"); - cache.unchecked=get_icon("unchecked"); - cache.arrow_collapsed=get_icon("arrow_collapsed"); - cache.arrow =get_icon("arrow"); - cache.select_arrow =get_icon("select_arrow"); - cache.updown=get_icon("updown"); - - 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"); - cache.draw_relationship_lines=get_constant("draw_relationship_lines"); - cache.relationship_line_color=get_color("relationship_line_color"); - cache.scroll_border=get_constant("scroll_border"); - cache.scroll_speed=get_constant("scroll_speed"); + cache.button_pressed = get_stylebox("button_pressed"); + + cache.checked = get_icon("checked"); + cache.unchecked = get_icon("unchecked"); + cache.arrow_collapsed = get_icon("arrow_collapsed"); + cache.arrow = get_icon("arrow"); + cache.select_arrow = get_icon("select_arrow"); + cache.updown = get_icon("updown"); + + 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"); + cache.draw_relationship_lines = get_constant("draw_relationship_lines"); + cache.relationship_line_color = get_color("relationship_line_color"); + cache.scroll_border = get_constant("scroll_border"); + cache.scroll_speed = get_constant("scroll_speed"); cache.title_button = get_stylebox("title_button_normal"); cache.title_button_pressed = get_stylebox("title_button_pressed"); @@ -848,39 +790,32 @@ void Tree::update_cache() { cache.title_button_color = get_color("title_button_color"); v_scroll->set_custom_step(cache.font->get_height()); - } int Tree::compute_item_height(TreeItem *p_item) const { - if (p_item==root && hide_root) + if (p_item == root && hide_root) return 0; - int height=cache.font->get_height(); - - - for (int i=0;i<columns.size();i++) { + int height = cache.font->get_height(); + for (int i = 0; i < columns.size(); i++) { - for(int j=0;j<p_item->cells[i].buttons.size();j++) { + for (int j = 0; j < p_item->cells[i].buttons.size(); j++) { - - Size2i s;// = cache.button_pressed->get_minimum_size(); - s+= p_item->cells[i].buttons[j].texture->get_size(); - if (s.height>height) - height=s.height; + Size2i s; // = cache.button_pressed->get_minimum_size(); + s += p_item->cells[i].buttons[j].texture->get_size(); + if (s.height > height) + height = s.height; } - switch(p_item->cells[i].mode) { + switch (p_item->cells[i].mode) { case TreeItem::CELL_MODE_CHECK: { int check_icon_h = cache.checked->get_height(); - if (height<check_icon_h) - height=check_icon_h; - - - + if (height < check_icon_h) + height = check_icon_h; } case TreeItem::CELL_MODE_STRING: case TreeItem::CELL_MODE_CUSTOM: @@ -890,11 +825,11 @@ int Tree::compute_item_height(TreeItem *p_item) const { if (!icon.is_null()) { Size2i s = p_item->cells[i].get_icon_size(); - if (p_item->cells[i].icon_max_w>0 && s.width > p_item->cells[i].icon_max_w ) { - s.height=s.height * p_item->cells[i].icon_max_w / s.width; + if (p_item->cells[i].icon_max_w > 0 && s.width > p_item->cells[i].icon_max_w) { + s.height = s.height * p_item->cells[i].icon_max_w / s.width; } - if (s.height > height ) - height=s.height; + if (s.height > height) + height = s.height; } } break; @@ -902,51 +837,47 @@ int Tree::compute_item_height(TreeItem *p_item) const { } } - height += cache.vseparation; return height; - } int Tree::get_item_height(TreeItem *p_item) const { - int height=compute_item_height(p_item); - height+=cache.vseparation; + int height = compute_item_height(p_item); + height += cache.vseparation; if (!p_item->collapsed) { /* if not collapsed, check the childs */ - TreeItem *c=p_item->childs; + TreeItem *c = p_item->childs; while (c) { - height += get_item_height( c ); + height += get_item_height(c); - c=c->next; + c = c->next; } } return height; } +void Tree::draw_item_rect(const TreeItem::Cell &p_cell, const Rect2i &p_rect, const Color &p_color) { -void Tree::draw_item_rect(const TreeItem::Cell& p_cell,const Rect2i& p_rect,const Color& p_color) { - - Rect2i rect=p_rect; + Rect2i rect = p_rect; RID ci = get_canvas_item(); if (!p_cell.icon.is_null()) { Size2i bmsize = p_cell.get_icon_size(); - if (p_cell.icon_max_w>0 && bmsize.width > p_cell.icon_max_w) { + if (p_cell.icon_max_w > 0 && bmsize.width > p_cell.icon_max_w) { bmsize.height = bmsize.height * p_cell.icon_max_w / bmsize.width; - bmsize.width=p_cell.icon_max_w; + bmsize.width = p_cell.icon_max_w; } - p_cell.draw_icon(ci,rect.pos + Size2i(0,Math::floor((real_t)(rect.size.y-bmsize.y)/2)),bmsize); - rect.pos.x+=bmsize.x+cache.hseparation; - rect.size.x-=bmsize.x+cache.hseparation; - + p_cell.draw_icon(ci, rect.pos + Size2i(0, Math::floor((real_t)(rect.size.y - bmsize.y) / 2)), bmsize); + rect.pos.x += bmsize.x + cache.hseparation; + rect.size.x -= bmsize.x + cache.hseparation; } /* @@ -957,12 +888,11 @@ void Tree::draw_item_rect(const TreeItem::Cell& p_cell,const Rect2i& p_rect,cons Ref<Font> font = cache.font; String text = p_cell.text; - if (p_cell.suffix!=String()) - text+=" "+p_cell.suffix; - - rect.pos.y+=Math::floor((rect.size.y-font->get_height())/2.0) +font->get_ascent(); - font->draw(ci,rect.pos,text,p_color,rect.size.x); + if (p_cell.suffix != String()) + text += " " + p_cell.suffix; + rect.pos.y += Math::floor((rect.size.y - font->get_height()) / 2.0) + font->get_ascent(); + font->draw(ci, rect.pos, text, p_color, rect.size.x); } #if 0 @@ -991,27 +921,25 @@ void Tree::draw_item_text(String p_text,const Ref<Texture>& p_icon,int p_icon_ma font->draw(ci,p_rect.pos,p_text,p_color,p_rect.size.x); } #endif -int Tree::draw_item(const Point2i& p_pos,const Point2& p_draw_ofs, const Size2& p_draw_size,TreeItem *p_item) { +int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 &p_draw_size, TreeItem *p_item) { - if (p_pos.y-cache.offset.y > (p_draw_size.height)) + if (p_pos.y - cache.offset.y > (p_draw_size.height)) return -1; //draw no more! RID ci = get_canvas_item(); - int htotal=0; + int htotal = 0; - int label_h=compute_item_height( p_item ); + int label_h = compute_item_height(p_item); /* Calculate height of the label part */ - label_h+=cache.vseparation; + label_h += cache.vseparation; /* Draw label, if height fits */ + bool skip = (p_item == root && hide_root); - bool skip=(p_item==root && hide_root); - - - if (!skip && (p_pos.y+label_h-cache.offset.y)>0) { + if (!skip && (p_pos.y + label_h - cache.offset.y) > 0) { if (!hide_folding && p_item->childs) { //has childs, draw the guide box @@ -1019,15 +947,12 @@ int Tree::draw_item(const Point2i& p_pos,const Point2& p_draw_ofs, const Size2& if (p_item->collapsed) { - arrow=cache.arrow_collapsed; + arrow = cache.arrow_collapsed; } else { - arrow=cache.arrow; - + arrow = cache.arrow; } - arrow->draw( ci , p_pos+p_draw_ofs+Point2i(0,(label_h-arrow->get_height())/2)-cache.offset); - - + arrow->draw(ci, p_pos + p_draw_ofs + Point2i(0, (label_h - arrow->get_height()) / 2) - cache.offset); } //draw separation. @@ -1035,162 +960,156 @@ int Tree::draw_item(const Point2i& p_pos,const Point2& p_draw_ofs, const Size2& Ref<Font> font = cache.font; - int font_ascent=font->get_ascent(); + int font_ascent = font->get_ascent(); - int ofs = p_pos.x + (hide_folding?cache.hseparation:cache.item_margin); - for (int i=0;i<columns.size();i++) { + int ofs = p_pos.x + (hide_folding ? cache.hseparation : cache.item_margin); + for (int i = 0; i < columns.size(); i++) { int w = get_column_width(i); - if (i==0) { + if (i == 0) { - w-=ofs; + w -= ofs; - if (w<=0) { + if (w <= 0) { - ofs=get_column_width(0); + ofs = get_column_width(0); continue; } } else { - ofs+=cache.hseparation; - w-=cache.hseparation; + ofs += cache.hseparation; + w -= cache.hseparation; } - int bw=0; - for(int j=p_item->cells[i].buttons.size()-1;j>=0;j--) { - Ref<Texture> b=p_item->cells[i].buttons[j].texture; + int bw = 0; + for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) { + Ref<Texture> b = p_item->cells[i].buttons[j].texture; Size2 s = b->get_size() + cache.button_pressed->get_minimum_size(); - Point2i o = Point2i( ofs+w-s.width, p_pos.y )-cache.offset+p_draw_ofs; + Point2i o = Point2i(ofs + w - s.width, p_pos.y) - cache.offset + p_draw_ofs; - if (cache.click_type==Cache::CLICK_BUTTON && cache.click_item==p_item && cache.click_column==i && cache.click_index==j && !p_item->cells[i].buttons[j].disabled) { + if (cache.click_type == Cache::CLICK_BUTTON && cache.click_item == p_item && cache.click_column == i && cache.click_index == j && !p_item->cells[i].buttons[j].disabled) { //being pressed - cache.button_pressed->draw(get_canvas_item(),Rect2(o,s)); + cache.button_pressed->draw(get_canvas_item(), Rect2(o, s)); } - o.y+=(label_h-s.height)/2; - o+=cache.button_pressed->get_offset(); + o.y += (label_h - s.height) / 2; + o += cache.button_pressed->get_offset(); - b->draw(ci,o,p_item->cells[i].buttons[j].disabled?Color(1,1,1,0.5):p_item->cells[i].buttons[j].color); - w-=s.width+cache.button_margin; - bw+=s.width+cache.button_margin; + b->draw(ci, o, p_item->cells[i].buttons[j].disabled ? Color(1, 1, 1, 0.5) : p_item->cells[i].buttons[j].color); + w -= s.width + cache.button_margin; + bw += s.width + cache.button_margin; } - Rect2i item_rect = Rect2i( Point2i( ofs, p_pos.y )-cache.offset+p_draw_ofs, Size2i( w, label_h )); - Rect2i cell_rect=item_rect; - if (i!=0) { - cell_rect.pos.x-=cache.hseparation; - cell_rect.size.x+=cache.hseparation; + Rect2i item_rect = Rect2i(Point2i(ofs, p_pos.y) - cache.offset + p_draw_ofs, Size2i(w, label_h)); + Rect2i cell_rect = item_rect; + if (i != 0) { + cell_rect.pos.x -= cache.hseparation; + cell_rect.size.x += cache.hseparation; } + VisualServer::get_singleton()->canvas_item_add_line(ci, Point2i(cell_rect.pos.x, cell_rect.pos.y + cell_rect.size.height), cell_rect.pos + cell_rect.size, cache.guide_color, 1); + if (i == 0) { - VisualServer::get_singleton()->canvas_item_add_line(ci,Point2i(cell_rect.pos.x,cell_rect.pos.y+cell_rect.size.height),cell_rect.pos+cell_rect.size,cache.guide_color,1); - - if (i==0) { - - if (p_item->cells[0].selected && select_mode==SELECT_ROW) { - Rect2i row_rect = Rect2i( Point2i( cache.bg->get_margin(MARGIN_LEFT), item_rect.pos.y), Size2i( get_size().width-cache.bg->get_minimum_size().width, item_rect.size.y )); + if (p_item->cells[0].selected && select_mode == SELECT_ROW) { + Rect2i row_rect = Rect2i(Point2i(cache.bg->get_margin(MARGIN_LEFT), item_rect.pos.y), Size2i(get_size().width - cache.bg->get_minimum_size().width, item_rect.size.y)); //Rect2 r = Rect2i(row_rect.pos,row_rect.size); //r.grow(cache.selected->get_margin(MARGIN_LEFT)); if (has_focus()) - cache.selected_focus->draw(ci,row_rect ); + cache.selected_focus->draw(ci, row_rect); else - cache.selected->draw(ci,row_rect ); + cache.selected->draw(ci, row_rect); } - } - if (p_item->cells[i].selected && select_mode!=SELECT_ROW) { + if (p_item->cells[i].selected && select_mode != SELECT_ROW) { - Rect2i r(item_rect.pos,item_rect.size); - if (p_item->cells[i].text.size() > 0){ + Rect2i r(item_rect.pos, item_rect.size); + if (p_item->cells[i].text.size() > 0) { float icon_width = p_item->cells[i].get_icon_size().width; r.pos.x += icon_width; r.size.x -= icon_width; } //r.grow(cache.selected->get_margin(MARGIN_LEFT)); - if (has_focus()){ - cache.selected_focus->draw(ci,r ); - p_item->set_meta("__focus_rect", Rect2(r.pos,r.size)); + if (has_focus()) { + cache.selected_focus->draw(ci, r); + p_item->set_meta("__focus_rect", Rect2(r.pos, r.size)); } else { - cache.selected->draw(ci,r ); + cache.selected->draw(ci, r); } - if (text_editor->is_visible_in_tree()){ + if (text_editor->is_visible_in_tree()) { text_editor->set_pos(get_global_pos() + r.pos); } } if (p_item->cells[i].custom_bg_color) { - Rect2 r=cell_rect; - r.pos.x-=cache.hseparation; - r.size.x+=cache.hseparation; + Rect2 r = cell_rect; + r.pos.x -= cache.hseparation; + r.size.x += cache.hseparation; 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); + 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); + 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_flags && drop_mode_over == p_item) { - 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); + 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 == 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); + 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"); + 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"); - Point2i text_pos=item_rect.pos; - text_pos.y+=Math::floor((item_rect.size.y-font->get_height())/2) + font_ascent; + Point2i text_pos = item_rect.pos; + text_pos.y += Math::floor((item_rect.size.y - font->get_height()) / 2) + font_ascent; switch (p_item->cells[i].mode) { case TreeItem::CELL_MODE_STRING: { - draw_item_rect(p_item->cells[i],item_rect,col); + draw_item_rect(p_item->cells[i], item_rect, col); } break; case TreeItem::CELL_MODE_CHECK: { Ref<Texture> checked = cache.checked; Ref<Texture> unchecked = cache.unchecked; - Point2i check_ofs=item_rect.pos; - check_ofs.y+=Math::floor((real_t)(item_rect.size.y-checked->get_height())/2); + Point2i check_ofs = item_rect.pos; + check_ofs.y += Math::floor((real_t)(item_rect.size.y - checked->get_height()) / 2); if (p_item->cells[i].checked) { - checked->draw( ci, check_ofs ); + checked->draw(ci, check_ofs); } else { - unchecked->draw( ci, check_ofs ); - + unchecked->draw(ci, check_ofs); } - int check_w = checked->get_width()+cache.hseparation; + int check_w = checked->get_width() + cache.hseparation; - text_pos.x+=check_w; + text_pos.x += check_w; - item_rect.size.x-=check_w; - item_rect.pos.x+=check_w; + item_rect.size.x -= check_w; + item_rect.pos.x += check_w; - draw_item_rect(p_item->cells[i],item_rect,col); + draw_item_rect(p_item->cells[i], item_rect, col); //font->draw( ci, text_pos, p_item->cells[i].text, col,item_rect.size.x-check_w ); @@ -1198,7 +1117,7 @@ int Tree::draw_item(const Point2i& p_pos,const Point2& p_draw_ofs, const Size2& case TreeItem::CELL_MODE_RANGE: case TreeItem::CELL_MODE_RANGE_EXPRESSION: { - if (p_item->cells[i].text!="") { + if (p_item->cells[i].text != "") { if (!p_item->cells[i].editable) break; @@ -1206,41 +1125,41 @@ int Tree::draw_item(const Point2i& p_pos,const Point2& p_draw_ofs, const Size2& int option = (int)p_item->cells[i].val; String s = p_item->cells[i].text; - s=s.get_slicec(',',option); + s = s.get_slicec(',', option); - if (p_item->cells[i].suffix!=String()) - s+=" "+p_item->cells[i].suffix; + if (p_item->cells[i].suffix != String()) + s += " " + p_item->cells[i].suffix; Ref<Texture> downarrow = cache.select_arrow; - font->draw(ci, text_pos, s, col,item_rect.size.x-downarrow->get_width() ); + font->draw(ci, text_pos, s, col, item_rect.size.x - downarrow->get_width()); //? - Point2i arrow_pos=item_rect.pos; - arrow_pos.x+=item_rect.size.x-downarrow->get_width(); - arrow_pos.y+=Math::floor(((item_rect.size.y-downarrow->get_height()))/2.0); + Point2i arrow_pos = item_rect.pos; + arrow_pos.x += item_rect.size.x - downarrow->get_width(); + arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0); - downarrow->draw( ci, arrow_pos ); + downarrow->draw(ci, arrow_pos); } else { Ref<Texture> updown = cache.updown; - String valtext = String::num( p_item->cells[i].val, Math::step_decimals( p_item->cells[i].step ) ); + String valtext = String::num(p_item->cells[i].val, Math::step_decimals(p_item->cells[i].step)); //String valtext = rtos( p_item->cells[i].val ); - if (p_item->cells[i].suffix!=String()) - valtext+=" "+p_item->cells[i].suffix; + if (p_item->cells[i].suffix != String()) + valtext += " " + p_item->cells[i].suffix; - font->draw( ci, text_pos, valtext, col, item_rect.size.x-updown->get_width()); + font->draw(ci, text_pos, valtext, col, item_rect.size.x - updown->get_width()); if (!p_item->cells[i].editable) break; - Point2i updown_pos=item_rect.pos; - updown_pos.x+=item_rect.size.x-updown->get_width(); - updown_pos.y+=Math::floor(((item_rect.size.y-updown->get_height()))/2.0); + Point2i updown_pos = item_rect.pos; + updown_pos.x += item_rect.size.x - updown->get_width(); + updown_pos.y += Math::floor(((item_rect.size.y - updown->get_height())) / 2.0); - updown->draw( ci, updown_pos ); + updown->draw(ci, updown_pos); } } break; @@ -1249,16 +1168,15 @@ int Tree::draw_item(const Point2i& p_pos,const Point2& p_draw_ofs, const Size2& if (p_item->cells[i].icon.is_null()) break; Size2i icon_size = p_item->cells[i].get_icon_size(); - if (p_item->cells[i].icon_max_w>0 && icon_size.width >p_item->cells[i].icon_max_w) { + if (p_item->cells[i].icon_max_w > 0 && icon_size.width > p_item->cells[i].icon_max_w) { icon_size.height = icon_size.height * p_item->cells[i].icon_max_w / icon_size.width; - icon_size.width=p_item->cells[i].icon_max_w; + icon_size.width = p_item->cells[i].icon_max_w; } - Point2i icon_ofs = (item_rect.size-icon_size)/2; - icon_ofs+=item_rect.pos; + Point2i icon_ofs = (item_rect.size - icon_size) / 2; + icon_ofs += item_rect.pos; - - draw_texture_rect(p_item->cells[i].icon,Rect2(icon_ofs,icon_size)); + draw_texture_rect(p_item->cells[i].icon, Rect2(icon_ofs, icon_size)); //p_item->cells[i].icon->draw(ci, icon_ofs); } break; @@ -1266,253 +1184,234 @@ int Tree::draw_item(const Point2i& p_pos,const Point2& p_draw_ofs, const Size2& //int option = (int)p_item->cells[i].val; - - if (p_item->cells[i].custom_draw_obj) { - Object* cdo = ObjectDB::get_instance(p_item->cells[i].custom_draw_obj); + Object *cdo = ObjectDB::get_instance(p_item->cells[i].custom_draw_obj); if (cdo) - cdo->call(p_item->cells[i].custom_draw_callback,p_item,Rect2(item_rect)); + cdo->call(p_item->cells[i].custom_draw_callback, p_item, Rect2(item_rect)); } if (!p_item->cells[i].editable) { - draw_item_rect(p_item->cells[i],item_rect,col); + draw_item_rect(p_item->cells[i], item_rect, col); break; } Ref<Texture> downarrow = cache.select_arrow; - Rect2i ir=item_rect; - ir.size.width-=downarrow->get_width(); - draw_item_rect(p_item->cells[i],ir,col); + Rect2i ir = item_rect; + ir.size.width -= downarrow->get_width(); + draw_item_rect(p_item->cells[i], ir, col); - Point2i arrow_pos=item_rect.pos; - arrow_pos.x+=item_rect.size.x-downarrow->get_width(); - arrow_pos.y+=Math::floor(((item_rect.size.y-downarrow->get_height()))/2.0); + Point2i arrow_pos = item_rect.pos; + arrow_pos.x += item_rect.size.x - downarrow->get_width(); + arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0); - downarrow->draw( ci, arrow_pos ); + downarrow->draw(ci, arrow_pos); } break; } - if (i==0) { + if (i == 0) { - ofs=get_column_width(0); + ofs = get_column_width(0); } else { - ofs+=w+bw; + ofs += w + bw; } - if (select_mode==SELECT_MULTI && selected_item==p_item && selected_col==i) { + if (select_mode == SELECT_MULTI && selected_item == p_item && selected_col == i) { if (has_focus()) - cache.cursor->draw(ci,cell_rect); + cache.cursor->draw(ci, cell_rect); else - cache.cursor_unfocus->draw(ci,cell_rect); + cache.cursor_unfocus->draw(ci, cell_rect); } - } //separator //get_painter()->draw_fill_rect( Point2i(0,pos.y),Size2i(get_size().width,1),color( COLOR_TREE_GRID) ); //pos=p_pos; //reset pos - } - - Point2 children_pos=p_pos; + Point2 children_pos = p_pos; if (!skip) { - children_pos.x+=cache.item_margin; - htotal+=label_h; - children_pos.y+=htotal; - + children_pos.x += cache.item_margin; + htotal += label_h; + children_pos.y += htotal; } - if (!p_item->collapsed) { /* if not collapsed, check the childs */ - TreeItem *c=p_item->childs; + TreeItem *c = p_item->childs; while (c) { - if (cache.draw_relationship_lines == 1){ - int root_ofs = children_pos.x + (hide_folding?cache.hseparation:cache.item_margin); - int parent_ofs = p_pos.x + (hide_folding?cache.hseparation:cache.item_margin); - Point2i root_pos = Point2i(root_ofs, children_pos.y + label_h/2)-cache.offset+p_draw_ofs; + if (cache.draw_relationship_lines == 1) { + int root_ofs = children_pos.x + (hide_folding ? cache.hseparation : cache.item_margin); + int parent_ofs = p_pos.x + (hide_folding ? cache.hseparation : cache.item_margin); + Point2i root_pos = Point2i(root_ofs, children_pos.y + label_h / 2) - cache.offset + p_draw_ofs; if (c->get_children() != NULL) - root_pos -= Point2i(cache.arrow->get_width(),0); + root_pos -= Point2i(cache.arrow->get_width(), 0); - 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; + 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, root_pos.y), cache.relationship_line_color); VisualServer::get_singleton()->canvas_item_add_line(ci, Point2i(parent_pos.x, root_pos.y), parent_pos, cache.relationship_line_color); } - int child_h=draw_item(children_pos, p_draw_ofs, p_draw_size, c ); + int child_h = draw_item(children_pos, p_draw_ofs, p_draw_size, c); - if (child_h<0 && cache.draw_relationship_lines == 0) + if (child_h < 0 && cache.draw_relationship_lines == 0) return -1; // break, stop drawing, no need to anymore - htotal+=child_h; - children_pos.y+=child_h; - c=c->next; + htotal += child_h; + children_pos.y += child_h; + c = c->next; } } - return htotal; - - } -int Tree::_count_selected_items(TreeItem* p_from) const { +int Tree::_count_selected_items(TreeItem *p_from) const { - int count=0; - for(int i=0;i<columns.size();i++) { + 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()); + count += _count_selected_items(p_from->get_children()); } if (p_from->get_next()) { - count+=_count_selected_items(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, bool p_force_deselect) { - TreeItem::Cell &selected_cell=p_selected->cells[p_col]; + TreeItem::Cell &selected_cell = p_selected->cells[p_col]; - bool switched=false; - if (r_in_range && !*r_in_range && (p_current==p_selected || p_current==p_prev)) { - *r_in_range=true; - switched=true; + bool switched = false; + if (r_in_range && !*r_in_range && (p_current == p_selected || p_current == p_prev)) { + *r_in_range = true; + switched = true; } - bool emitted_row=false; + bool emitted_row = false; - for (int i=0;i<columns.size();i++) { + for (int i = 0; i < columns.size(); i++) { - TreeItem::Cell &c=p_current->cells[i]; + TreeItem::Cell &c = p_current->cells[i]; if (!c.selectable) continue; - if (select_mode==SELECT_ROW) { + if (select_mode == SELECT_ROW) { - if (p_selected==p_current && !c.selected) { - c.selected=true; - selected_item=p_selected; - selected_col=0; - selected_item=p_selected; - if (!emitted_row) { - emit_signal("item_selected"); - emitted_row=true; - } - /* + if (p_selected == p_current && !c.selected) { + c.selected = true; + selected_item = p_selected; + selected_col = 0; + selected_item = p_selected; + if (!emitted_row) { + emit_signal("item_selected"); + emitted_row = true; + } + /* if (p_col==i) p_current->selected_signal.call(p_col); */ } else if (c.selected) { - c.selected=false; - //p_current->deselected_signal.call(p_col); + c.selected = false; + //p_current->deselected_signal.call(p_col); } - } else if (select_mode==SELECT_SINGLE || select_mode==SELECT_MULTI) { + } else if (select_mode == SELECT_SINGLE || select_mode == SELECT_MULTI) { - if (!r_in_range && &selected_cell==&c) { + if (!r_in_range && &selected_cell == &c) { if (!selected_cell.selected) { - selected_cell.selected=true; + selected_cell.selected = true; - selected_item=p_selected; - selected_col=i; + selected_item = p_selected; + selected_col = i; emit_signal("cell_selected"); - if (select_mode==SELECT_MULTI) - emit_signal("multi_selected",p_current,i,true); - else if(select_mode == SELECT_SINGLE) + if (select_mode == SELECT_MULTI) + emit_signal("multi_selected", p_current, i, true); + else if (select_mode == SELECT_SINGLE) emit_signal("item_selected"); - } else if (select_mode==SELECT_MULTI && (selected_item!=p_selected || selected_col!=i)) { + } else if (select_mode == SELECT_MULTI && (selected_item != p_selected || selected_col != i)) { - selected_item=p_selected; - selected_col=i; + selected_item = p_selected; + selected_col = i; emit_signal("cell_selected"); - } } else { - if (r_in_range && *r_in_range && !p_force_deselect) { - if (!c.selected && c.selectable) { - c.selected=true; - emit_signal("multi_selected",p_current,i,true); + c.selected = true; + emit_signal("multi_selected", p_current, i, true); } - } else if (!r_in_range || p_force_deselect){ - if (select_mode==SELECT_MULTI && c.selected) - emit_signal("multi_selected",p_current,i,false); - c.selected=false; + } else if (!r_in_range || p_force_deselect) { + if (select_mode == SELECT_MULTI && c.selected) + emit_signal("multi_selected", p_current, i, false); + c.selected = false; } //p_current->deselected_signal.call(p_col); } } - } - if (!switched && r_in_range && *r_in_range && (p_current==p_selected || p_current==p_prev)) { - *r_in_range=false; + if (!switched && r_in_range && *r_in_range && (p_current == p_selected || p_current == p_prev)) { + *r_in_range = false; } - TreeItem *c=p_current->childs; + TreeItem *c = p_current->childs; while (c) { - select_single_item(p_selected,c,p_col,p_prev,r_in_range,p_current->is_collapsed() || p_force_deselect); - c=c->next; + select_single_item(p_selected, c, p_col, p_prev, r_in_range, p_current->is_collapsed() || p_force_deselect); + c = c->next; } - } - Rect2 Tree::search_item_rect(TreeItem *p_from, TreeItem *p_item) { - return Rect2(); } - void Tree::_range_click_timeout() { if (range_item_last && !range_drag_enabled && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) { - Point2 pos = get_local_mouse_pos()-cache.bg->get_offset(); + Point2 pos = get_local_mouse_pos() - cache.bg->get_offset(); if (show_column_titles) { - pos.y-=_get_title_button_height(); + pos.y -= _get_title_button_height(); - if (pos.y<0) { + if (pos.y < 0) { range_click_timer->stop(); return; } } - click_handled=false; + click_handled = false; InputModifierState mod = InputModifierState(); // should be irrelevant.. blocked++; - propagate_mouse_event(pos+cache.offset, 0, 0, false, root, BUTTON_LEFT, mod); + propagate_mouse_event(pos + cache.offset, 0, 0, false, root, BUTTON_LEFT, mod); blocked--; if (range_click_timer->is_one_shot()) { @@ -1529,120 +1428,112 @@ void Tree::_range_click_timeout() { } } +int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, bool p_doubleclick, TreeItem *p_item, int p_button, const InputModifierState &p_mod) { -int Tree::propagate_mouse_event(const Point2i &p_pos,int x_ofs,int y_ofs,bool p_doubleclick,TreeItem *p_item,int p_button,const InputModifierState& p_mod) { + int item_h = compute_item_height(p_item) + cache.vseparation; - int item_h=compute_item_height( p_item )+cache.vseparation; + bool skip = (p_item == root && hide_root); - bool skip=(p_item==root && hide_root); - - if (!skip && p_pos.y<item_h) { + if (!skip && p_pos.y < item_h) { // check event! if (range_click_timer->get_time_left() > 0 && p_item != range_item_last) { return -1; } - if (!hide_folding && (p_pos.x >=x_ofs && p_pos.x < (x_ofs+cache.item_margin) )) { - + if (!hide_folding && (p_pos.x >= x_ofs && p_pos.x < (x_ofs + cache.item_margin))) { if (p_item->childs) - p_item->set_collapsed( ! p_item->is_collapsed() ); + p_item->set_collapsed(!p_item->is_collapsed()); return -1; //handled! } - int x=p_pos.x; + int x = p_pos.x; /* find clicked column */ - int col=-1; - int col_ofs=0; - int col_width=0; - for (int i=0;i<columns.size();i++) { - - col_width=get_column_width(i); - if (x>col_width) { - col_ofs+=col_width; - x-=col_width; + int col = -1; + int col_ofs = 0; + int col_width = 0; + for (int i = 0; i < columns.size(); i++) { + + col_width = get_column_width(i); + if (x > col_width) { + col_ofs += col_width; + x -= col_width; continue; } - col=i; + col = i; break; } - - - if (col==-1) + if (col == -1) return -1; - else if (col==0) { - int margin=x_ofs+cache.item_margin;//-cache.hseparation; + else if (col == 0) { + int margin = x_ofs + cache.item_margin; //-cache.hseparation; //int lm = cache.bg->get_margin(MARGIN_LEFT); - col_width-=margin; - col_ofs+=margin; - x-=margin; + col_width -= margin; + col_ofs += margin; + x -= margin; } else { - col_width-=cache.hseparation; - x-=cache.hseparation; - + col_width -= cache.hseparation; + x -= cache.hseparation; } TreeItem::Cell &c = p_item->cells[col]; + bool already_selected = c.selected; + bool already_cursor = (p_item == selected_item) && col == selected_col; - bool already_selected=c.selected; - bool already_cursor=(p_item==selected_item) && col == selected_col; - - - for(int j=c.buttons.size()-1;j>=0;j--) { - Ref<Texture> b=c.buttons[j].texture; + for (int j = c.buttons.size() - 1; j >= 0; j--) { + Ref<Texture> b = c.buttons[j].texture; int w = b->get_size().width + cache.button_pressed->get_minimum_size().width; - if (x>col_width-w) { + if (x > col_width - w) { if (c.buttons[j].disabled) { - pressed_button=-1; - cache.click_type=Cache::CLICK_NONE; + pressed_button = -1; + cache.click_type = Cache::CLICK_NONE; return -1; } - pressed_button=j; - cache.click_type=Cache::CLICK_BUTTON; - cache.click_index=j; - cache.click_id=c.buttons[j].id; - cache.click_item=p_item; - cache.click_column=col; - cache.click_pos=get_global_mouse_pos()-get_global_pos(); + pressed_button = j; + cache.click_type = Cache::CLICK_BUTTON; + cache.click_index = j; + cache.click_id = c.buttons[j].id; + cache.click_item = p_item; + cache.click_column = col; + cache.click_pos = get_global_mouse_pos() - get_global_pos(); update(); //emit_signal("button_pressed"); return -1; } - col_width-=w+cache.button_margin; + col_width -= w + cache.button_margin; } - if (p_button==BUTTON_LEFT || (p_button==BUTTON_RIGHT && allow_rmb_select)) { + if (p_button == BUTTON_LEFT || (p_button == BUTTON_RIGHT && allow_rmb_select)) { /* process selection */ - if (p_doubleclick && (!c.editable || c.mode==TreeItem::CELL_MODE_CUSTOM || c.mode==TreeItem::CELL_MODE_ICON /*|| c.mode==TreeItem::CELL_MODE_CHECK*/)) { //it' s confusing for check + if (p_doubleclick && (!c.editable || c.mode == TreeItem::CELL_MODE_CUSTOM || c.mode == TreeItem::CELL_MODE_ICON /*|| c.mode==TreeItem::CELL_MODE_CHECK*/)) { //it' s confusing for check emit_signal("item_activated"); return -1; } - if (select_mode==SELECT_MULTI && p_mod.command && c.selectable) { + if (select_mode == SELECT_MULTI && p_mod.command && c.selectable) { - if (!c.selected || p_button==BUTTON_RIGHT) { + if (!c.selected || p_button == BUTTON_RIGHT) { p_item->select(col); - emit_signal("multi_selected",p_item,col,true); - if (p_button==BUTTON_RIGHT) { - emit_signal("item_rmb_selected",get_local_mouse_pos()); + emit_signal("multi_selected", p_item, col, true); + if (p_button == BUTTON_RIGHT) { + emit_signal("item_rmb_selected", get_local_mouse_pos()); } - //p_item->selected_signal.call(col); } else { p_item->deselect(col); - emit_signal("multi_selected",p_item,col,false); + emit_signal("multi_selected", p_item, col, false); //p_item->deselected_signal.call(col); } @@ -1650,29 +1541,29 @@ int Tree::propagate_mouse_event(const Point2i &p_pos,int x_ofs,int y_ofs,bool p_ if (c.selectable) { - if (select_mode==SELECT_MULTI && p_mod.shift && selected_item && selected_item!=p_item) { + if (select_mode == SELECT_MULTI && p_mod.shift && selected_item && selected_item != p_item) { - bool inrange=false; + bool inrange = false; - select_single_item( p_item, root, col,selected_item,&inrange ); - if (p_button==BUTTON_RIGHT) { - emit_signal("item_rmb_selected",get_local_mouse_pos()); + select_single_item(p_item, root, col, selected_item, &inrange); + if (p_button == BUTTON_RIGHT) { + emit_signal("item_rmb_selected", get_local_mouse_pos()); } } else { int icount = _count_selected_items(root); - if (select_mode==SELECT_MULTI && icount>1 && p_button!=BUTTON_RIGHT) { - single_select_defer=p_item; - single_select_defer_column=col; + if (select_mode == SELECT_MULTI && icount > 1 && p_button != BUTTON_RIGHT) { + single_select_defer = p_item; + single_select_defer_column = col; } else { - if (p_button!=BUTTON_RIGHT || !c.selected) { - select_single_item( p_item, root, col ); + if (p_button != BUTTON_RIGHT || !c.selected) { + select_single_item(p_item, root, col); } - if (p_button==BUTTON_RIGHT) { - emit_signal("item_rmb_selected",get_local_mouse_pos()); + if (p_button == BUTTON_RIGHT) { + emit_signal("item_rmb_selected", get_local_mouse_pos()); } } } @@ -1684,34 +1575,30 @@ int Tree::propagate_mouse_event(const Point2i &p_pos,int x_ofs,int y_ofs,bool p_ */ update(); } - - } } - - if (!c.editable) return -1; // if cell is not editable, don't bother /* editing */ - bool bring_up_editor=force_select_on_already_selected ? (c.selected && already_selected) : c.selected; - String editor_text=c.text; + bool bring_up_editor = force_select_on_already_selected ? (c.selected && already_selected) : c.selected; + String editor_text = c.text; switch (c.mode) { case TreeItem::CELL_MODE_STRING: { //nothing in particular - if (select_mode==SELECT_MULTI && (get_tree()->get_last_event_id() == focus_in_id || !already_cursor)) { - bring_up_editor=false; + if (select_mode == SELECT_MULTI && (get_tree()->get_last_event_id() == focus_in_id || !already_cursor)) { + bring_up_editor = false; } } break; case TreeItem::CELL_MODE_CHECK: { - bring_up_editor=false; //checkboxes are not edited with editor + bring_up_editor = false; //checkboxes are not edited with editor if (force_edit_checkbox_only_on_checkbox) { if (x < cache.checked->get_width()) { p_item->set_checked(col, !c.checked); @@ -1728,38 +1615,36 @@ int Tree::propagate_mouse_event(const Point2i &p_pos,int x_ofs,int y_ofs,bool p_ case TreeItem::CELL_MODE_RANGE: case TreeItem::CELL_MODE_RANGE_EXPRESSION: { - - if (c.text!="") { + if (c.text != "") { //if (x >= (get_column_width(col)-item_h/2)) { popup_menu->clear(); - for (int i=0;i<c.text.get_slice_count(",");i++) { - - String s = c.text.get_slicec(',',i); - popup_menu->add_item(s,i); + for (int i = 0; i < c.text.get_slice_count(","); i++) { + String s = c.text.get_slicec(',', i); + popup_menu->add_item(s, i); } - popup_menu->set_size(Size2(col_width,0)); - popup_menu->set_pos( get_global_pos() + Point2i(col_ofs,_get_title_button_height()+y_ofs+item_h)-cache.offset ); + popup_menu->set_size(Size2(col_width, 0)); + popup_menu->set_pos(get_global_pos() + Point2i(col_ofs, _get_title_button_height() + y_ofs + item_h) - cache.offset); popup_menu->popup(); - popup_edited_item=p_item; - popup_edited_item_col=col; + popup_edited_item = p_item; + popup_edited_item_col = col; //} - bring_up_editor=false; + bring_up_editor = false; } else { - if (x >= (col_width-item_h/2)) { + if (x >= (col_width - item_h / 2)) { /* touching the combo */ - bool up=p_pos.y < (item_h /2); + bool up = p_pos.y < (item_h / 2); - if (p_button==BUTTON_LEFT) { + if (p_button == BUTTON_LEFT) { if (range_click_timer->get_time_left() == 0) { - range_item_last=p_item; - range_up_last=up; + range_item_last = p_item; + range_up_last = up; range_click_timer->set_wait_time(0.6); range_click_timer->set_one_shot(true); @@ -1770,115 +1655,103 @@ int Tree::propagate_mouse_event(const Point2i &p_pos,int x_ofs,int y_ofs,bool p_ return -1; // break. avoid changing direction on mouse held } - p_item->set_range( col, c.val + (up?1.0:-1.0) * c.step ); + p_item->set_range(col, c.val + (up ? 1.0 : -1.0) * c.step); - item_edited(col,p_item); + item_edited(col, p_item); - } else if (p_button==BUTTON_RIGHT) { + } else if (p_button == BUTTON_RIGHT) { - p_item->set_range( col, (up?c.max:c.min) ); - item_edited(col,p_item); - } else if (p_button==BUTTON_WHEEL_UP) { + p_item->set_range(col, (up ? c.max : c.min)); + item_edited(col, p_item); + } else if (p_button == BUTTON_WHEEL_UP) { - p_item->set_range( col, c.val + c.step ); - item_edited(col,p_item); - } else if (p_button==BUTTON_WHEEL_DOWN) { + p_item->set_range(col, c.val + c.step); + item_edited(col, p_item); + } else if (p_button == BUTTON_WHEEL_DOWN) { - p_item->set_range( col, c.val - c.step ); - item_edited(col,p_item); + p_item->set_range(col, c.val - c.step); + item_edited(col, p_item); } //p_item->edited_signal.call(col); - bring_up_editor=false; - - - } else { + bring_up_editor = false; - editor_text=String::num( p_item->cells[col].val, Math::step_decimals( p_item->cells[col].step ) ); - if (select_mode==SELECT_MULTI && get_tree()->get_last_event_id() == focus_in_id) - bring_up_editor=false; + } else { + editor_text = String::num(p_item->cells[col].val, Math::step_decimals(p_item->cells[col].step)); + if (select_mode == SELECT_MULTI && get_tree()->get_last_event_id() == focus_in_id) + bring_up_editor = false; } - } - click_handled=true; + click_handled = true; } break; case TreeItem::CELL_MODE_ICON: { - bring_up_editor=false; + bring_up_editor = false; } break; case TreeItem::CELL_MODE_CUSTOM: { - edited_item=p_item; - edited_col=col; - custom_popup_rect=Rect2i(get_global_pos() + Point2i(col_ofs,_get_title_button_height()+y_ofs+item_h-cache.offset.y), Size2(get_column_width(col),item_h)); - emit_signal("custom_popup_edited",((bool)(x >= (col_width-item_h/2)))); - - bring_up_editor=false; - item_edited(col,p_item); - click_handled=true; + edited_item = p_item; + edited_col = col; + custom_popup_rect = Rect2i(get_global_pos() + Point2i(col_ofs, _get_title_button_height() + y_ofs + item_h - cache.offset.y), Size2(get_column_width(col), item_h)); + emit_signal("custom_popup_edited", ((bool)(x >= (col_width - item_h / 2)))); + + bring_up_editor = false; + item_edited(col, p_item); + click_handled = true; return -1; } break; - }; - if (!bring_up_editor || p_button!=BUTTON_LEFT) + if (!bring_up_editor || p_button != BUTTON_LEFT) return -1; + click_handled = true; + popup_edited_item = p_item; + popup_edited_item_col = col; - - click_handled=true; - popup_edited_item=p_item; - popup_edited_item_col=col; - - pressing_item_rect=Rect2(get_global_pos() + Point2i(col_ofs,_get_title_button_height()+y_ofs)-cache.offset,Size2(col_width,item_h)); - pressing_for_editor_text=editor_text; - pressing_for_editor=true; + pressing_item_rect = Rect2(get_global_pos() + Point2i(col_ofs, _get_title_button_height() + y_ofs) - cache.offset, Size2(col_width, item_h)); + pressing_for_editor_text = editor_text; + pressing_for_editor = true; return -1; //select } else { - Point2i new_pos=p_pos; + Point2i new_pos = p_pos; if (!skip) { - x_ofs+=cache.item_margin; + x_ofs += cache.item_margin; //new_pos.x-=cache.item_margin; - y_ofs+=item_h; - new_pos.y-=item_h; + y_ofs += item_h; + new_pos.y -= item_h; } - if (!p_item->collapsed) { /* if not collapsed, check the childs */ - TreeItem *c=p_item->childs; + TreeItem *c = p_item->childs; while (c) { - int child_h=propagate_mouse_event( new_pos,x_ofs,y_ofs,p_doubleclick,c,p_button,p_mod); + int child_h = propagate_mouse_event(new_pos, x_ofs, y_ofs, p_doubleclick, c, p_button, p_mod); - if (child_h<0) + if (child_h < 0) return -1; // break, stop propagating, no need to anymore - new_pos.y-=child_h; - y_ofs+=child_h; - c=c->next; - item_h+=child_h; + new_pos.y -= child_h; + y_ofs += child_h; + c = c->next; + item_h += child_h; } } - - - } return item_h; // nothing found - } - void Tree::_text_editor_modal_close() { - if ( Input::get_singleton()->is_key_pressed(KEY_ESCAPE) || - Input::get_singleton()->is_key_pressed(KEY_ENTER) || - Input::get_singleton()->is_key_pressed(KEY_RETURN) ) { + if (Input::get_singleton()->is_key_pressed(KEY_ESCAPE) || + Input::get_singleton()->is_key_pressed(KEY_ENTER) || + Input::get_singleton()->is_key_pressed(KEY_RETURN)) { return; } @@ -1889,52 +1762,49 @@ void Tree::_text_editor_modal_close() { text_editor_enter(text_editor->get_text()); } - void Tree::text_editor_enter(String p_text) { - text_editor->hide(); value_editor->hide(); if (!popup_edited_item) return; - if (popup_edited_item_col<0 || popup_edited_item_col>columns.size()) + if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size()) return; - TreeItem::Cell &c=popup_edited_item->cells[popup_edited_item_col]; - switch( c.mode ) { + TreeItem::Cell &c = popup_edited_item->cells[popup_edited_item_col]; + switch (c.mode) { case TreeItem::CELL_MODE_STRING: { - c.text=p_text; + c.text = p_text; //popup_edited_item->edited_signal.call( popup_edited_item_col ); } break; case TreeItem::CELL_MODE_RANGE: { - c.val=p_text.to_double(); - if (c.step>0) - c.val=Math::stepify(c.val,c.step); - if (c.val<c.min) - c.val=c.min; - else if (c.val>c.max) - c.val=c.max; + c.val = p_text.to_double(); + if (c.step > 0) + c.val = Math::stepify(c.val, c.step); + if (c.val < c.min) + c.val = c.min; + else if (c.val > c.max) + c.val = c.max; //popup_edited_item->edited_signal.call( popup_edited_item_col ); } break; case TreeItem::CELL_MODE_RANGE_EXPRESSION: { - if(evaluator) - c.val=evaluator->eval(p_text); + if (evaluator) + c.val = evaluator->eval(p_text); else - c.val=p_text.to_double(); + c.val = p_text.to_double(); } break; - default: { ERR_FAIL(); } + default: { ERR_FAIL(); } } - item_edited(popup_edited_item_col,popup_edited_item); + item_edited(popup_edited_item_col, popup_edited_item); update(); - } void Tree::value_editor_changed(double p_value) { @@ -1946,9 +1816,9 @@ void Tree::value_editor_changed(double p_value) { return; } - TreeItem::Cell &c=popup_edited_item->cells[popup_edited_item_col]; - c.val=p_value; - item_edited(popup_edited_item_col,popup_edited_item); + TreeItem::Cell &c = popup_edited_item->cells[popup_edited_item_col]; + c.val = p_value; + item_edited(popup_edited_item_col, popup_edited_item); update(); } @@ -1957,18 +1827,15 @@ void Tree::popup_select(int p_option) { if (!popup_edited_item) return; - if (popup_edited_item_col<0 || popup_edited_item_col>columns.size()) + if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size()) return; - - popup_edited_item->cells[popup_edited_item_col].val=p_option; + popup_edited_item->cells[popup_edited_item_col].val = p_option; //popup_edited_item->edited_signal.call( popup_edited_item_col ); update(); - item_edited(popup_edited_item_col,popup_edited_item); + item_edited(popup_edited_item_col, popup_edited_item); } - - void Tree::_gui_input(InputEvent p_event) { switch (p_event.type) { @@ -1977,7 +1844,7 @@ void Tree::_gui_input(InputEvent p_event) { if (!p_event.key.pressed) break; - if (p_event.key.mod.alt || p_event.key.mod.command || (p_event.key.mod.shift && p_event.key.unicode==0) || p_event.key.mod.meta) + if (p_event.key.mod.alt || p_event.key.mod.command || (p_event.key.mod.shift && p_event.key.unicode == 0) || p_event.key.mod.meta) break; if (!root) return; @@ -1985,23 +1852,27 @@ void Tree::_gui_input(InputEvent p_event) { if (hide_root && !root->get_next_visible()) return; - switch(p_event.key.scancode) { -#define EXIT_BREAK { if (!cursor_can_exit_tree) accept_event(); break; } + switch (p_event.key.scancode) { +#define EXIT_BREAK \ + { \ + if (!cursor_can_exit_tree) accept_event(); \ + break; \ + } case KEY_RIGHT: { //TreeItem *next = NULL; if (!selected_item) break; - if (select_mode==SELECT_ROW) + if (select_mode == SELECT_ROW) EXIT_BREAK; - if (selected_col>=(columns.size()-1)) + if (selected_col >= (columns.size() - 1)) EXIT_BREAK; - if (select_mode==SELECT_MULTI) { + if (select_mode == SELECT_MULTI) { selected_col++; emit_signal("cell_selected"); } else { - selected_item->select(selected_col+1); + selected_item->select(selected_col + 1); } update(); @@ -2014,16 +1885,16 @@ void Tree::_gui_input(InputEvent p_event) { //TreeItem *next = NULL; if (!selected_item) break; - if (select_mode==SELECT_ROW) + if (select_mode == SELECT_ROW) EXIT_BREAK; - if (selected_col<=0) + if (selected_col <= 0) EXIT_BREAK; - if (select_mode==SELECT_MULTI) { + if (select_mode == SELECT_MULTI) { selected_col--; emit_signal("cell_selected"); } else { - selected_item->select(selected_col-1); + selected_item->select(selected_col - 1); } update(); @@ -2035,43 +1906,41 @@ void Tree::_gui_input(InputEvent p_event) { TreeItem *next = NULL; if (!selected_item) { - next=hide_root?root->get_next_visible():root; - selected_item=0; + next = hide_root ? root->get_next_visible() : root; + selected_item = 0; } else { - next=selected_item->get_next_visible(); + next = selected_item->get_next_visible(); //if (diff < uint64_t(GLOBAL_DEF("gui/incr_search_max_interval_msec",2000))) { - if (last_keypress!=0) { + if (last_keypress != 0) { //incr search next int col; - next=_search_item_text(next,incr_search,&col,true); + next = _search_item_text(next, incr_search, &col, true); if (!next) { accept_event(); return; } - } } - if (select_mode==SELECT_MULTI) { + if (select_mode == SELECT_MULTI) { if (!next) EXIT_BREAK; - selected_item=next; + selected_item = next; emit_signal("cell_selected"); update(); } else { - int col=selected_col<0?0:selected_col; + int col = selected_col < 0 ? 0 : selected_col; while (next && !next->cells[col].selectable) - next=next->get_next_visible(); + next = next->get_next_visible(); if (!next) EXIT_BREAK; // do nothing.. next->select(col); - } ensure_cursor_is_visible(); @@ -2080,47 +1949,43 @@ void Tree::_gui_input(InputEvent p_event) { } break; case KEY_UP: { - TreeItem *prev = NULL; - if (!selected_item) { - prev = get_last_item(); - selected_col=0; - } else { - - prev=selected_item->get_prev_visible(); - if (last_keypress!=0) { - //incr search next - int col; - prev=_search_item_text(prev,incr_search,&col,true,true); - if (!prev) { - accept_event(); - return; - } + TreeItem *prev = NULL; + if (!selected_item) { + prev = get_last_item(); + selected_col = 0; + } else { + prev = selected_item->get_prev_visible(); + if (last_keypress != 0) { + //incr search next + int col; + prev = _search_item_text(prev, incr_search, &col, true, true); + if (!prev) { + accept_event(); + return; } - } + } - if (select_mode==SELECT_MULTI) { - + if (select_mode == SELECT_MULTI) { - if (!prev) - break; - selected_item=prev; - emit_signal("cell_selected"); - update(); - } else { - - int col=selected_col<0?0:selected_col; - while (prev && !prev->cells[col].selectable) - prev=prev->get_prev_visible(); - if (!prev) - break; // do nothing.. - prev->select(col); + if (!prev) + break; + selected_item = prev; + emit_signal("cell_selected"); + update(); + } else { - } + int col = selected_col < 0 ? 0 : selected_col; + while (prev && !prev->cells[col].selectable) + prev = prev->get_prev_visible(); + if (!prev) + break; // do nothing.. + prev->select(col); + } - ensure_cursor_is_visible(); - accept_event(); + ensure_cursor_is_visible(); + accept_event(); } break; case KEY_PAGEDOWN: { @@ -2128,73 +1993,69 @@ void Tree::_gui_input(InputEvent p_event) { TreeItem *next = NULL; if (!selected_item) break; - next=selected_item; + next = selected_item; - for(int i=0;i<10;i++) { + for (int i = 0; i < 10; i++) { TreeItem *_n = next->get_next_visible(); if (_n) { - next=_n; + next = _n; } else { break; } } - if (next==selected_item) + if (next == selected_item) break; - if (select_mode==SELECT_MULTI) { - + if (select_mode == SELECT_MULTI) { - selected_item=next; + selected_item = next; emit_signal("cell_selected"); update(); } else { while (next && !next->cells[selected_col].selectable) - next=next->get_next_visible(); + next = next->get_next_visible(); if (!next) EXIT_BREAK; // do nothing.. next->select(selected_col); - } ensure_cursor_is_visible(); - } break; + } break; case KEY_PAGEUP: { TreeItem *prev = NULL; if (!selected_item) break; - prev=selected_item; + prev = selected_item; - for(int i=0;i<10;i++) { + for (int i = 0; i < 10; i++) { TreeItem *_n = prev->get_prev_visible(); if (_n) { - prev=_n; + prev = _n; } else { break; } } - if (prev==selected_item) + if (prev == selected_item) break; - if (select_mode==SELECT_MULTI) { + if (select_mode == SELECT_MULTI) { - - selected_item=prev; + selected_item = prev; emit_signal("cell_selected"); update(); } else { while (prev && !prev->cells[selected_col].selectable) - prev=prev->get_prev_visible(); + prev = prev->get_prev_visible(); if (!prev) EXIT_BREAK; // do nothing.. prev->select(selected_col); - } ensure_cursor_is_visible(); @@ -2209,21 +2070,20 @@ void Tree::_gui_input(InputEvent p_event) { if (!edit_selected()) { emit_signal("item_activated"); } - } accept_event(); } break; case KEY_SPACE: { - if (select_mode==SELECT_MULTI) { + if (select_mode == SELECT_MULTI) { if (!selected_item) break; if (selected_item->is_selected(selected_col)) { selected_item->deselect(selected_col); - emit_signal("multi_selected",selected_item,selected_col,false); + emit_signal("multi_selected", selected_item, selected_col, false); } else if (selected_item->is_selectable(selected_col)) { selected_item->select(selected_col); - emit_signal("multi_selected",selected_item,selected_col,true); + emit_signal("multi_selected", selected_item, selected_col, true); } } accept_event(); @@ -2231,19 +2091,19 @@ void Tree::_gui_input(InputEvent p_event) { } break; default: { - if (p_event.key.unicode>0) { + if (p_event.key.unicode > 0) { _do_incr_search(String::chr(p_event.key.unicode)); accept_event(); return; } else { - if (p_event.key.scancode!=KEY_SHIFT) - last_keypress=0; + if (p_event.key.scancode != KEY_SHIFT) + last_keypress = 0; } } break; - last_keypress=0; + last_keypress = 0; } } break; @@ -2252,119 +2112,107 @@ void Tree::_gui_input(InputEvent p_event) { if (cache.font.is_null()) // avoid a strange case that may fuckup stuff update_cache(); - const InputEventMouseMotion& b=p_event.mouse_motion; + const InputEventMouseMotion &b = p_event.mouse_motion; Ref<StyleBox> bg = cache.bg; - Point2 pos = Point2(b.x,b.y) - bg->get_offset(); + Point2 pos = Point2(b.x, b.y) - bg->get_offset(); Cache::ClickType old_hover = cache.hover_type; int old_index = cache.hover_index; - cache.hover_type=Cache::CLICK_NONE; - cache.hover_index=0; + cache.hover_type = Cache::CLICK_NONE; + cache.hover_index = 0; if (show_column_titles) { - pos.y-=_get_title_button_height(); - if (pos.y<0) { - pos.x+=cache.offset.x; - int len=0; - for(int i=0;i<columns.size();i++) { + pos.y -= _get_title_button_height(); + if (pos.y < 0) { + pos.x += cache.offset.x; + int len = 0; + for (int i = 0; i < columns.size(); i++) { - len+=get_column_width(i); - if (pos.x<len) { + len += get_column_width(i); + if (pos.x < len) { - cache.hover_type=Cache::CLICK_TITLE; - cache.hover_index=i; + cache.hover_type = Cache::CLICK_TITLE; + cache.hover_index = i; update(); break; } } - } - } if (drop_mode_flags && root) { - Point2 mpos=Point2(b.x,b.y); + Point2 mpos = Point2(b.x, b.y); mpos -= cache.bg->get_offset(); - mpos.y-=_get_title_button_height(); - if (mpos.y>=0) { + mpos.y -= _get_title_button_height(); + if (mpos.y >= 0) { if (h_scroll->is_visible_in_tree()) - mpos.x+=h_scroll->get_value(); + mpos.x += h_scroll->get_value(); if (v_scroll->is_visible_in_tree()) - mpos.y+=v_scroll->get_value(); + mpos.y += v_scroll->get_value(); - int col,h,section; - TreeItem *it = _find_item_at_pos(root,mpos,col,h,section); + 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; + 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) { + if (cache.hover_type != old_hover || cache.hover_index != old_index) { update(); } - if (pressing_for_editor && popup_edited_item && (popup_edited_item->get_cell_mode(popup_edited_item_col)==TreeItem::CELL_MODE_RANGE || popup_edited_item->get_cell_mode(popup_edited_item_col)==TreeItem::CELL_MODE_RANGE_EXPRESSION)) { + if (pressing_for_editor && popup_edited_item && (popup_edited_item->get_cell_mode(popup_edited_item_col) == TreeItem::CELL_MODE_RANGE || popup_edited_item->get_cell_mode(popup_edited_item_col) == TreeItem::CELL_MODE_RANGE_EXPRESSION)) { //range drag if (!range_drag_enabled) { - Vector2 cpos = Vector2(b.x,b.y); - if (cpos.distance_to(pressing_pos)>2) { - range_drag_enabled=true; - range_drag_capture_pos=cpos; - range_drag_base=popup_edited_item->get_range(popup_edited_item_col); + Vector2 cpos = Vector2(b.x, b.y); + if (cpos.distance_to(pressing_pos) > 2) { + range_drag_enabled = true; + range_drag_capture_pos = cpos; + range_drag_base = popup_edited_item->get_range(popup_edited_item_col); Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED); } } else { - TreeItem::Cell &c=popup_edited_item->cells[popup_edited_item_col]; + TreeItem::Cell &c = popup_edited_item->cells[popup_edited_item_col]; float diff_y = -b.relative_y; - diff_y=Math::pow(ABS(diff_y),1.8f)*SGN(diff_y); - diff_y*=0.1; - range_drag_base=CLAMP(range_drag_base + c.step * diff_y, c.min, c.max); - popup_edited_item->set_range(popup_edited_item_col,range_drag_base); - item_edited(popup_edited_item_col,popup_edited_item); - + diff_y = Math::pow(ABS(diff_y), 1.8f) * SGN(diff_y); + diff_y *= 0.1; + range_drag_base = CLAMP(range_drag_base + c.step * diff_y, c.min, c.max); + popup_edited_item->set_range(popup_edited_item_col, range_drag_base); + item_edited(popup_edited_item_col, popup_edited_item); } - } - if (drag_touching && ! drag_touching_deaccel) { - - - drag_accum-=b.relative_y; - v_scroll->set_value(drag_from+drag_accum); - drag_speed=-b.speed_y; + if (drag_touching && !drag_touching_deaccel) { + drag_accum -= b.relative_y; + v_scroll->set_value(drag_from + drag_accum); + drag_speed = -b.speed_y; } } break; case InputEvent::MOUSE_BUTTON: { - - if (cache.font.is_null()) // avoid a strange case that may fuckup stuff update_cache(); - const InputEventMouseButton& b=p_event.mouse_button; - + const InputEventMouseButton &b = p_event.mouse_button; if (!b.pressed) { - if (b.button_index==BUTTON_LEFT) { - + 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; + select_single_item(single_select_defer, root, single_select_defer_column); + single_select_defer = NULL; } range_click_timer->stop(); @@ -2373,77 +2221,70 @@ void Tree::_gui_input(InputEvent p_event) { if (range_drag_enabled) { - range_drag_enabled=false; + range_drag_enabled = false; Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE); warp_mouse(range_drag_capture_pos); } else { Rect2 rect = get_selected()->get_meta("__focus_rect"); - if (rect.has_point(Point2(p_event.mouse_button.x,p_event.mouse_button.y))) { + if (rect.has_point(Point2(p_event.mouse_button.x, p_event.mouse_button.y))) { edit_selected(); } else { emit_signal("item_double_clicked"); } } - pressing_for_editor=false; - + pressing_for_editor = false; } - - - if (cache.click_type==Cache::CLICK_BUTTON) { + if (cache.click_type == Cache::CLICK_BUTTON) { // make sure in case of wrong reference after reconstructing whole TreeItems - cache.click_item=get_item_at_pos(cache.click_pos); - emit_signal("button_pressed",cache.click_item,cache.click_column,cache.click_id); - + cache.click_item = get_item_at_pos(cache.click_pos); + emit_signal("button_pressed", cache.click_item, cache.click_column, cache.click_id); } - cache.click_type=Cache::CLICK_NONE; - cache.click_index=-1; - cache.click_id=-1; - cache.click_item=NULL; - cache.click_column=0; + cache.click_type = Cache::CLICK_NONE; + cache.click_index = -1; + cache.click_id = -1; + cache.click_item = NULL; + cache.click_column = 0; if (drag_touching) { - - if (drag_speed==0) { - drag_touching_deaccel=false; - drag_touching=false; + if (drag_speed == 0) { + drag_touching_deaccel = false; + drag_touching = false; set_fixed_process(false); } else { - drag_touching_deaccel=true; + drag_touching_deaccel = true; } - } update(); } break; - } if (range_drag_enabled) break; - switch(b.button_index) { + switch (b.button_index) { case BUTTON_RIGHT: case BUTTON_LEFT: { Ref<StyleBox> bg = cache.bg; - Point2 pos = Point2(b.x,b.y) - bg->get_offset(); - cache.click_type=Cache::CLICK_NONE; - if (show_column_titles && b.button_index==BUTTON_LEFT) { - pos.y-=_get_title_button_height(); + Point2 pos = Point2(b.x, b.y) - bg->get_offset(); + cache.click_type = Cache::CLICK_NONE; + if (show_column_titles && b.button_index == BUTTON_LEFT) { + pos.y -= _get_title_button_height(); - if (pos.y<0) { - pos.x+=cache.offset.x; - int len=0; - for(int i=0;i<columns.size();i++) { + if (pos.y < 0) { + pos.x += cache.offset.x; + int len = 0; + for (int i = 0; i < columns.size(); i++) { - len+=get_column_width(i); - if (pos.x<len) { + len += get_column_width(i); + if (pos.x < len) { - cache.click_type=Cache::CLICK_TITLE; - cache.click_index=i; + cache.click_type = Cache::CLICK_TITLE; + cache.click_index = i; //cache.click_id=; update(); break; @@ -2451,140 +2292,134 @@ void Tree::_gui_input(InputEvent p_event) { } break; } - } if (!root || (!root->get_children() && hide_root)) { - if (b.button_index==BUTTON_RIGHT && allow_rmb_select) { - emit_signal("empty_tree_rmb_selected",get_local_mouse_pos()); + if (b.button_index == BUTTON_RIGHT && allow_rmb_select) { + emit_signal("empty_tree_rmb_selected", get_local_mouse_pos()); } break; } - click_handled=false; - pressing_for_editor=false; + click_handled = false; + pressing_for_editor = false; blocked++; - bool handled = propagate_mouse_event(pos+cache.offset,0,0,b.doubleclick,root,b.button_index,b.mod); + bool handled = propagate_mouse_event(pos + cache.offset, 0, 0, b.doubleclick, root, b.button_index, b.mod); blocked--; if (pressing_for_editor) { - pressing_pos=Point2(b.x,b.y); + pressing_pos = Point2(b.x, b.y); } - - if (b.button_index==BUTTON_RIGHT) + if (b.button_index == BUTTON_RIGHT) break; if (drag_touching) { set_fixed_process(false); - drag_touching_deaccel=false; - drag_touching=false; - drag_speed=0; - drag_from=0; + drag_touching_deaccel = false; + drag_touching = false; + drag_speed = 0; + drag_from = 0; } if (!click_handled) { - drag_speed=0; - drag_accum=0; + drag_speed = 0; + drag_accum = 0; //last_drag_accum=0; - drag_from=v_scroll->get_value(); - drag_touching=OS::get_singleton()->has_touchscreen_ui_hint(); - drag_touching_deaccel=false; + drag_from = v_scroll->get_value(); + drag_touching = OS::get_singleton()->has_touchscreen_ui_hint(); + drag_touching_deaccel = false; if (drag_touching) { set_fixed_process(true); } } - } break; case BUTTON_WHEEL_UP: { - v_scroll->set_value( v_scroll->get_value()-v_scroll->get_page()/8 ); + v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / 8); } break; case BUTTON_WHEEL_DOWN: { - v_scroll->set_value( v_scroll->get_value()+v_scroll->get_page()/8 ); + v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / 8); } break; } } break; } - } - bool Tree::edit_selected() { TreeItem *s = get_selected(); ERR_EXPLAIN("No item selected!"); - ERR_FAIL_COND_V(!s,false); + ERR_FAIL_COND_V(!s, false); ensure_cursor_is_visible(); int col = get_selected_column(); ERR_EXPLAIN("No item column selected!"); - ERR_FAIL_INDEX_V(col,columns.size(),false); + ERR_FAIL_INDEX_V(col, columns.size(), false); if (!s->cells[col].editable) return false; Rect2 rect = s->get_meta("__focus_rect"); - popup_edited_item=s; - popup_edited_item_col=col; + popup_edited_item = s; + popup_edited_item_col = col; TreeItem::Cell &c = s->cells[col]; - if (c.mode==TreeItem::CELL_MODE_CHECK) { + if (c.mode == TreeItem::CELL_MODE_CHECK) { s->set_checked(col, !c.checked); - item_edited(col,s); + item_edited(col, s); return true; - } else if (c.mode==TreeItem::CELL_MODE_CUSTOM) { + } else if (c.mode == TreeItem::CELL_MODE_CUSTOM) { - edited_item=s; - edited_col=col; - custom_popup_rect=Rect2i( get_global_pos() + rect.pos, rect.size ); - emit_signal("custom_popup_edited",false); - item_edited(col,s); + edited_item = s; + edited_col = col; + custom_popup_rect = Rect2i(get_global_pos() + rect.pos, rect.size); + emit_signal("custom_popup_edited", false); + item_edited(col, s); return true; - } else if ((c.mode==TreeItem::CELL_MODE_RANGE||c.mode==TreeItem::CELL_MODE_RANGE_EXPRESSION) && c.text!="") { + } else if ((c.mode == TreeItem::CELL_MODE_RANGE || c.mode == TreeItem::CELL_MODE_RANGE_EXPRESSION) && c.text != "") { popup_menu->clear(); - for (int i=0;i<c.text.get_slice_count(",");i++) { - - String s = c.text.get_slicec(',',i); - popup_menu->add_item(s,i); + for (int i = 0; i < c.text.get_slice_count(","); i++) { + String s = c.text.get_slicec(',', i); + popup_menu->add_item(s, i); } - popup_menu->set_size(Size2(rect.size.width,0)); - popup_menu->set_pos( get_global_pos() + rect.pos + Point2i(0,rect.size.height) ); + popup_menu->set_size(Size2(rect.size.width, 0)); + popup_menu->set_pos(get_global_pos() + rect.pos + Point2i(0, rect.size.height)); popup_menu->popup(); - popup_edited_item=s; - popup_edited_item_col=col; + popup_edited_item = s; + popup_edited_item_col = col; return true; - } else if (c.mode==TreeItem::CELL_MODE_STRING || c.mode==TreeItem::CELL_MODE_RANGE || c.mode==TreeItem::CELL_MODE_RANGE_EXPRESSION ) { + } else if (c.mode == TreeItem::CELL_MODE_STRING || c.mode == TreeItem::CELL_MODE_RANGE || c.mode == TreeItem::CELL_MODE_RANGE_EXPRESSION) { - Point2i textedpos=get_global_pos() + rect.pos; - text_editor->set_pos( textedpos ); - text_editor->set_size( rect.size); + Point2i textedpos = get_global_pos() + rect.pos; + text_editor->set_pos(textedpos); + text_editor->set_size(rect.size); text_editor->clear(); - text_editor->set_text( c.mode==TreeItem::CELL_MODE_STRING?c.text:String::num( c.val, Math::step_decimals( c.step ) ) ); + text_editor->set_text(c.mode == TreeItem::CELL_MODE_STRING ? c.text : String::num(c.val, Math::step_decimals(c.step))); text_editor->select_all(); - if (c.mode==TreeItem::CELL_MODE_RANGE || c.mode==TreeItem::CELL_MODE_RANGE_EXPRESSION ) { + if (c.mode == TreeItem::CELL_MODE_RANGE || c.mode == TreeItem::CELL_MODE_RANGE_EXPRESSION) { - value_editor->set_pos(textedpos + Point2i(0,text_editor->get_size().height) ); - value_editor->set_size( Size2(rect.size.width,1)); + value_editor->set_pos(textedpos + Point2i(0, text_editor->get_size().height)); + value_editor->set_size(Size2(rect.size.width, 1)); value_editor->show_modal(); - updating_value_editor=true; - value_editor->set_min( c.min ); - value_editor->set_max( c.max ); - value_editor->set_step( c.step ); - value_editor->set_value( c.val ); - value_editor->set_exp_ratio( c.expr ); - updating_value_editor=false; + updating_value_editor = true; + value_editor->set_min(c.min); + value_editor->set_max(c.max); + value_editor->set_step(c.step); + value_editor->set_value(c.val); + value_editor->set_exp_ratio(c.expr); + updating_value_editor = false; } text_editor->show_modal(); @@ -2597,12 +2432,12 @@ bool Tree::edit_selected() { Size2 Tree::get_internal_min_size() const { - Size2i size=cache.bg->get_offset(); + Size2i size = cache.bg->get_offset(); if (root) - size.height+=get_item_height(root); - for (int i=0;i<columns.size();i++) { + size.height += get_item_height(root); + for (int i = 0; i < columns.size(); i++) { - size.width+=columns[i].min_width; + size.width += columns[i].min_width; } return size; @@ -2613,140 +2448,133 @@ void Tree::update_scrollbars() { Size2 size = get_size(); int tbh; if (show_column_titles) { - tbh=_get_title_button_height(); + tbh = _get_title_button_height(); } else { - tbh=0; + tbh = 0; } Size2 hmin = h_scroll->get_combined_minimum_size(); Size2 vmin = v_scroll->get_combined_minimum_size(); + v_scroll->set_begin(Point2(size.width - vmin.width, cache.bg->get_margin(MARGIN_TOP))); + v_scroll->set_end(Point2(size.width, size.height - cache.bg->get_margin(MARGIN_TOP) - cache.bg->get_margin(MARGIN_BOTTOM))); - - v_scroll->set_begin( Point2(size.width - vmin.width , cache.bg->get_margin(MARGIN_TOP)) ); - v_scroll->set_end( Point2(size.width, size.height-cache.bg->get_margin(MARGIN_TOP)-cache.bg->get_margin(MARGIN_BOTTOM)) ); - - h_scroll->set_begin( Point2( 0, size.height - hmin.height) ); - h_scroll->set_end( Point2(size.width-vmin.width, size.height) ); - + h_scroll->set_begin(Point2(0, size.height - hmin.height)); + h_scroll->set_end(Point2(size.width - vmin.width, size.height)); Size2 min = get_internal_min_size(); if (min.height < size.height - hmin.height) { v_scroll->hide(); - cache.offset.y=0; + cache.offset.y = 0; } else { v_scroll->show(); v_scroll->set_max(min.height); v_scroll->set_page(size.height - hmin.height - tbh); - cache.offset.y=v_scroll->get_value(); + cache.offset.y = v_scroll->get_value(); } if (min.width < size.width - vmin.width) { h_scroll->hide(); - cache.offset.x=0; + cache.offset.x = 0; } else { h_scroll->show(); h_scroll->set_max(min.width); h_scroll->set_page(size.width - vmin.width); - cache.offset.x=h_scroll->get_value(); + cache.offset.x = h_scroll->get_value(); } } - int Tree::_get_title_button_height() const { - return show_column_titles?cache.font->get_height() + cache.title_button->get_minimum_size().height:0; + return show_column_titles ? cache.font->get_height() + cache.title_button->get_minimum_size().height : 0; } void Tree::_notification(int p_what) { - if (p_what==NOTIFICATION_FOCUS_ENTER) { + if (p_what == NOTIFICATION_FOCUS_ENTER) { - focus_in_id=get_tree()->get_last_event_id(); + focus_in_id = get_tree()->get_last_event_id(); } - if (p_what==NOTIFICATION_MOUSE_EXIT) { + if (p_what == NOTIFICATION_MOUSE_EXIT) { - if (cache.hover_type!=Cache::CLICK_NONE) { - cache.hover_type=Cache::CLICK_NONE; + if (cache.hover_type != Cache::CLICK_NONE) { + cache.hover_type = Cache::CLICK_NONE; update(); } } - if (p_what==NOTIFICATION_VISIBILITY_CHANGED) { + if (p_what == NOTIFICATION_VISIBILITY_CHANGED) { - drag_touching=false; + drag_touching = false; } - if (p_what==NOTIFICATION_ENTER_TREE) { + if (p_what == NOTIFICATION_ENTER_TREE) { update_cache(); } - if (p_what==NOTIFICATION_DRAG_END) { + if (p_what == NOTIFICATION_DRAG_END) { - drop_mode_flags=0; + drop_mode_flags = 0; scrolling = false; set_fixed_process(false); update(); } - if (p_what==NOTIFICATION_DRAG_BEGIN) { + if (p_what == NOTIFICATION_DRAG_BEGIN) { - single_select_defer=NULL; + single_select_defer = NULL; if (cache.scroll_speed > 0 && get_rect().has_point(get_viewport()->get_mouse_pos() - get_global_pos())) { scrolling = true; set_fixed_process(true); } } - if (p_what==NOTIFICATION_FIXED_PROCESS) { + if (p_what == NOTIFICATION_FIXED_PROCESS) { - if (drag_touching) { + if (drag_touching) { if (drag_touching_deaccel) { float pos = v_scroll->get_value(); - pos+=drag_speed*get_fixed_process_delta_time(); + pos += drag_speed * get_fixed_process_delta_time(); - bool turnoff=false; - if (pos<0) { - pos=0; - turnoff=true; + bool turnoff = false; + if (pos < 0) { + pos = 0; + turnoff = true; set_fixed_process(false); - drag_touching=false; - drag_touching_deaccel=false; + drag_touching = false; + drag_touching_deaccel = false; } - if (pos > (v_scroll->get_max()-v_scroll->get_page())) { - pos=v_scroll->get_max()-v_scroll->get_page(); - turnoff=true; - + if (pos > (v_scroll->get_max() - v_scroll->get_page())) { + pos = v_scroll->get_max() - v_scroll->get_page(); + turnoff = true; } v_scroll->set_value(pos); - float sgn = drag_speed<0? -1 : 1; + float sgn = drag_speed < 0 ? -1 : 1; float val = Math::abs(drag_speed); - val-=1000*get_fixed_process_delta_time(); + val -= 1000 * get_fixed_process_delta_time(); - if (val<0) { - turnoff=true; + if (val < 0) { + turnoff = true; } - drag_speed=sgn*val; + drag_speed = sgn * val; if (turnoff) { set_fixed_process(false); - drag_touching=false; - drag_touching_deaccel=false; + drag_touching = false; + drag_touching_deaccel = false; } - } else { - } } - + if (scrolling) { Point2 point = get_viewport()->get_mouse_pos() - get_global_pos(); if (point.x < cache.scroll_border) { @@ -2770,7 +2598,7 @@ void Tree::_notification(int p_what) { } } - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { update_cache(); update_scrollbars(); @@ -2780,125 +2608,117 @@ void Tree::_notification(int p_what) { Ref<StyleBox> bg_focus = get_stylebox("bg_focus"); Point2 draw_ofs; - draw_ofs+=bg->get_offset(); - Size2 draw_size=get_size()-bg->get_minimum_size(); + draw_ofs += bg->get_offset(); + Size2 draw_size = get_size() - bg->get_minimum_size(); - bg->draw( ci, Rect2( Point2(), get_size()) ); + bg->draw(ci, Rect2(Point2(), get_size())); if (has_focus()) { - VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci,true); - bg_focus->draw( ci, Rect2( Point2(), get_size()) ); - VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci,false); + VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, true); + bg_focus->draw(ci, Rect2(Point2(), get_size())); + VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, false); } int tbh = _get_title_button_height(); - draw_ofs.y+=tbh; - draw_size.y-=tbh; + draw_ofs.y += tbh; + draw_size.y -= tbh; if (root) { - - draw_item( Point2(),draw_ofs,draw_size,root); - + draw_item(Point2(), draw_ofs, draw_size, root); } - int ofs=0; + int ofs = 0; - for (int i=0;i<(columns.size()-1-1);i++) { + for (int i = 0; i < (columns.size() - 1 - 1); i++) { - ofs+=get_column_width(i); + ofs += get_column_width(i); } if (show_column_titles) { //title butons - int ofs=cache.bg->get_margin(MARGIN_LEFT); - for(int i=0;i<columns.size();i++) { + int ofs = cache.bg->get_margin(MARGIN_LEFT); + for (int i = 0; i < columns.size(); i++) { - Ref<StyleBox> sb = (cache.click_type==Cache::CLICK_TITLE && cache.click_index==i)?cache.title_button_pressed:((cache.hover_type==Cache::CLICK_TITLE && cache.hover_index==i)?cache.title_button_hover:cache.title_button); + Ref<StyleBox> sb = (cache.click_type == Cache::CLICK_TITLE && cache.click_index == i) ? cache.title_button_pressed : ((cache.hover_type == Cache::CLICK_TITLE && cache.hover_index == i) ? cache.title_button_hover : cache.title_button); Ref<Font> f = cache.tb_font; - Rect2 tbrect = Rect2(ofs - cache.offset.x,bg->get_margin(MARGIN_TOP),get_column_width(i),tbh); - sb->draw(ci,tbrect); - ofs+=tbrect.size.width; + Rect2 tbrect = Rect2(ofs - cache.offset.x, bg->get_margin(MARGIN_TOP), get_column_width(i), tbh); + sb->draw(ci, tbrect); + ofs += tbrect.size.width; //text int clip_w = tbrect.size.width - sb->get_minimum_size().width; - f->draw_halign(ci,tbrect.pos+Point2i(sb->get_offset().x,(tbrect.size.height-f->get_height())/2+f->get_ascent()),HALIGN_CENTER,clip_w,columns[i].title,cache.title_button_color); + f->draw_halign(ci, tbrect.pos + Point2i(sb->get_offset().x, (tbrect.size.height - f->get_height()) / 2 + f->get_ascent()), HALIGN_CENTER, clip_w, columns[i].title, cache.title_button_color); } } } - if (p_what==NOTIFICATION_THEME_CHANGED) { + if (p_what == NOTIFICATION_THEME_CHANGED) { update_cache(); } - } - - - Size2 Tree::get_minimum_size() const { - return Size2(1,1); + return Size2(1, 1); } TreeItem *Tree::create_item(TreeItem *p_parent) { - ERR_FAIL_COND_V(blocked>0,NULL); + ERR_FAIL_COND_V(blocked > 0, NULL); - TreeItem *ti = memnew( TreeItem(this) ); + TreeItem *ti = memnew(TreeItem(this)); - ti->cells.resize( columns.size() ); - ERR_FAIL_COND_V(!ti,NULL); + ti->cells.resize(columns.size()); + ERR_FAIL_COND_V(!ti, NULL); if (p_parent) { /* Always append at the end */ - TreeItem *last=0; - TreeItem *c=p_parent->childs; + TreeItem *last = 0; + TreeItem *c = p_parent->childs; - while(c) { + while (c) { - last=c; - c=c->next; + last = c; + c = c->next; } if (last) { - last->next=ti; + last->next = ti; } else { - p_parent->childs=ti; + p_parent->childs = ti; } - ti->parent=p_parent; + ti->parent = p_parent; } else { if (root) - ti->childs=root; - - root=ti; + ti->childs = root; + root = ti; } - return ti; } -TreeItem* Tree::get_root() { +TreeItem *Tree::get_root() { return root; } -TreeItem* Tree::get_last_item() { +TreeItem *Tree::get_last_item() { - TreeItem *last=root; + TreeItem *last = root; - while(last) { + while (last) { if (last->next) - last=last->next; + last = last->next; else if (last->childs) - last=last->childs; + last = last->childs; else break; } @@ -2906,54 +2726,53 @@ TreeItem* Tree::get_last_item() { return last; } -void Tree::item_edited(int p_column,TreeItem *p_item) { +void Tree::item_edited(int p_column, TreeItem *p_item) { - edited_item=p_item; - edited_col=p_column; + edited_item = p_item; + edited_col = p_column; emit_signal("item_edited"); } -void Tree::item_changed(int p_column,TreeItem *p_item) { +void Tree::item_changed(int p_column, TreeItem *p_item) { update(); } -void Tree::item_selected(int p_column,TreeItem *p_item) { +void Tree::item_selected(int p_column, TreeItem *p_item) { - if (select_mode==SELECT_MULTI) { + if (select_mode == SELECT_MULTI) { if (!p_item->cells[p_column].selectable) return; - p_item->cells[p_column].selected=true; + p_item->cells[p_column].selected = true; //emit_signal("multi_selected",p_item,p_column,true); - NO this is for TreeItem::select } else { - select_single_item(p_item,root,p_column); + select_single_item(p_item, root, p_column); } update(); } -void Tree::item_deselected(int p_column,TreeItem *p_item) { +void Tree::item_deselected(int p_column, TreeItem *p_item) { - if (select_mode==SELECT_MULTI || select_mode == SELECT_SINGLE) { - p_item->cells[p_column].selected=false; + if (select_mode == SELECT_MULTI || select_mode == SELECT_SINGLE) { + p_item->cells[p_column].selected = false; } update(); } - void Tree::set_select_mode(SelectMode p_mode) { - select_mode=p_mode; + select_mode = p_mode; } void Tree::clear() { - if (blocked>0) { + if (blocked > 0) { - ERR_FAIL_COND(blocked>0); + ERR_FAIL_COND(blocked > 0); } if (pressing_for_editor) { @@ -2966,43 +2785,37 @@ void Tree::clear() { } if (root) { - memdelete( root ); + memdelete(root); root = NULL; }; - selected_item=NULL; - edited_item=NULL; - popup_edited_item=NULL; + selected_item = NULL; + edited_item = NULL; + popup_edited_item = NULL; update(); }; - - void Tree::set_hide_root(bool p_enabled) { - - - hide_root=p_enabled; + hide_root = p_enabled; update(); } -void Tree::set_column_min_width(int p_column,int p_min_width) { +void Tree::set_column_min_width(int p_column, int p_min_width) { + ERR_FAIL_INDEX(p_column, columns.size()); - ERR_FAIL_INDEX(p_column,columns.size()); - - if (p_min_width<1) + if (p_min_width < 1) return; - columns[p_column].min_width=p_min_width; + columns[p_column].min_width = p_min_width; update(); - } -void Tree::set_column_expand(int p_column,bool p_expand) { +void Tree::set_column_expand(int p_column, bool p_expand) { - ERR_FAIL_INDEX(p_column,columns.size()); + ERR_FAIL_INDEX(p_column, columns.size()); - columns[p_column].expand=p_expand; + columns[p_column].expand = p_expand; update(); } @@ -3026,7 +2839,7 @@ int Tree::get_edited_column() const { return edited_col; } -TreeItem* Tree::get_next_selected( TreeItem* p_item) { +TreeItem *Tree::get_next_selected(TreeItem *p_item) { /* if (!p_item) @@ -3035,35 +2848,33 @@ TreeItem* Tree::get_next_selected( TreeItem* p_item) { if (!root) return NULL; - while(true) { - + while (true) { if (!p_item) { - p_item=root; + p_item = root; } else { if (p_item->childs) { - p_item=p_item->childs; + p_item = p_item->childs; } else if (p_item->next) { - p_item=p_item->next; + p_item = p_item->next; } else { - while(!p_item->next) { + while (!p_item->next) { - p_item=p_item->parent; - if (p_item==NULL) + p_item = p_item->parent; + if (p_item == NULL) return NULL; } - p_item=p_item->next; + p_item = p_item->next; } - } - for (int i=0;i<columns.size();i++) + for (int i = 0; i < columns.size(); i++) if (p_item->cells[i].selected) return p_item; } @@ -3073,64 +2884,62 @@ TreeItem* Tree::get_next_selected( TreeItem* p_item) { int Tree::get_column_width(int p_column) const { - ERR_FAIL_INDEX_V(p_column,columns.size(),-1); - + ERR_FAIL_INDEX_V(p_column, columns.size(), -1); if (!columns[p_column].expand) return columns[p_column].min_width; Ref<StyleBox> bg = cache.bg; - int expand_area=get_size().width-(bg->get_margin(MARGIN_LEFT)+bg->get_margin(MARGIN_RIGHT)); + int expand_area = get_size().width - (bg->get_margin(MARGIN_LEFT) + bg->get_margin(MARGIN_RIGHT)); if (v_scroll->is_visible_in_tree()) - expand_area-=v_scroll->get_combined_minimum_size().width; + expand_area -= v_scroll->get_combined_minimum_size().width; - int expanding_columns=0; - int expanding_total=0; + int expanding_columns = 0; + int expanding_total = 0; - for (int i=0;i<columns.size();i++) { + for (int i = 0; i < columns.size(); i++) { if (!columns[i].expand) { - expand_area-=columns[i].min_width; + expand_area -= columns[i].min_width; } else { - expanding_total+=columns[i].min_width; + expanding_total += columns[i].min_width; expanding_columns++; } } - if (expand_area<expanding_total) + if (expand_area < expanding_total) return columns[p_column].min_width; - ERR_FAIL_COND_V(expanding_columns==0,-1); // shouldnt happen + ERR_FAIL_COND_V(expanding_columns == 0, -1); // shouldnt happen return expand_area * columns[p_column].min_width / expanding_total; } void Tree::propagate_set_columns(TreeItem *p_item) { - p_item->cells.resize( columns.size() ); + p_item->cells.resize(columns.size()); TreeItem *c = p_item->get_children(); - while(c) { + while (c) { propagate_set_columns(c); - c=c->get_next(); + c = c->get_next(); } } void Tree::set_columns(int p_columns) { - ERR_FAIL_COND(p_columns<1); - ERR_FAIL_COND(blocked>0); + ERR_FAIL_COND(p_columns < 1); + ERR_FAIL_COND(blocked > 0); columns.resize(p_columns); if (root) propagate_set_columns(root); - if (selected_col>=p_columns) - selected_col=p_columns-1; + if (selected_col >= p_columns) + selected_col = p_columns - 1; update(); - } int Tree::get_columns() const { @@ -3150,35 +2959,35 @@ Rect2 Tree::get_custom_popup_rect() const { int Tree::get_item_offset(TreeItem *p_item) const { - TreeItem *it=root; - int ofs=_get_title_button_height(); + TreeItem *it = root; + int ofs = _get_title_button_height(); if (!it) return 0; - while(true) { + while (true) { - if (it==p_item) + if (it == p_item) return ofs; - ofs+=compute_item_height(it)+cache.vseparation; + ofs += compute_item_height(it) + cache.vseparation; if (it->childs && !it->collapsed) { - it=it->childs; + it = it->childs; } else if (it->next) { - it=it->next; + it = it->next; } else { - while(!it->next) { + while (!it->next) { - it=it->parent; - if (it==NULL) + it = it->parent; + if (it == NULL) return 0; } - it=it->next; + it = it->next; } } @@ -3194,13 +3003,13 @@ void Tree::ensure_cursor_is_visible() { if (!selected) return; int ofs = get_item_offset(selected); - if (ofs==-1) + if (ofs == -1) return; - int h = compute_item_height(selected)+cache.vseparation; - int screenh=get_size().height-h_scroll->get_combined_minimum_size().height; + int h = compute_item_height(selected) + cache.vseparation; + int screenh = get_size().height - h_scroll->get_combined_minimum_size().height; - if (ofs+h>v_scroll->get_value()+screenh) - v_scroll->call_deferred("set_val", ofs-screenh+h); + if (ofs + h > v_scroll->get_value() + screenh) + v_scroll->call_deferred("set_val", ofs - screenh + h); else if (ofs < v_scroll->get_value()) v_scroll->set_value(ofs); } @@ -3210,32 +3019,31 @@ int Tree::get_pressed_button() const { return pressed_button; } +Rect2 Tree::get_item_rect(TreeItem *p_item, int p_column) const { -Rect2 Tree::get_item_rect(TreeItem *p_item,int p_column) const { - - ERR_FAIL_NULL_V(p_item,Rect2()); - ERR_FAIL_COND_V(p_item->tree!=this,Rect2()); - if (p_column!=-1) { - ERR_FAIL_INDEX_V(p_column,columns.size(),Rect2()); + ERR_FAIL_NULL_V(p_item, Rect2()); + ERR_FAIL_COND_V(p_item->tree != this, Rect2()); + if (p_column != -1) { + ERR_FAIL_INDEX_V(p_column, columns.size(), Rect2()); } int ofs = get_item_offset(p_item); int height = compute_item_height(p_item); Rect2 r; - r.pos.y=ofs; - r.size.height=height; + r.pos.y = ofs; + r.size.height = height; - if (p_column==-1) { - r.pos.x=0; - r.size.x=get_size().width; + if (p_column == -1) { + r.pos.x = 0; + r.size.x = get_size().width; } else { - int accum=0; - for(int i=0;i<p_column;i++) { - accum+=get_column_width(i); + int accum = 0; + for (int i = 0; i < p_column; i++) { + accum += get_column_width(i); } - r.pos.x=accum; - r.size.x=get_column_width(p_column); + r.pos.x = accum; + r.size.x = get_column_width(p_column); } return r; @@ -3243,7 +3051,7 @@ Rect2 Tree::get_item_rect(TreeItem *p_item,int p_column) const { void Tree::set_column_titles_visible(bool p_show) { - show_column_titles=p_show; + show_column_titles = p_show; update(); } @@ -3252,16 +3060,16 @@ bool Tree::are_column_titles_visible() const { return show_column_titles; } -void Tree::set_column_title(int p_column,const String& p_title) { +void Tree::set_column_title(int p_column, const String &p_title) { - ERR_FAIL_INDEX(p_column,columns.size()); - columns[p_column].title=p_title; + ERR_FAIL_INDEX(p_column, columns.size()); + columns[p_column].title = p_title; update(); } String Tree::get_column_title(int p_column) const { - ERR_FAIL_INDEX_V(p_column,columns.size(),""); + ERR_FAIL_INDEX_V(p_column, columns.size(), ""); return columns[p_column].title; } @@ -3269,153 +3077,137 @@ Point2 Tree::get_scroll() const { Point2 ofs; if (h_scroll->is_visible_in_tree()) - ofs.x=h_scroll->get_value(); + ofs.x = h_scroll->get_value(); if (v_scroll->is_visible_in_tree()) - ofs.y=v_scroll->get_value(); + ofs.y = v_scroll->get_value(); return ofs; - } -TreeItem* Tree::_search_item_text(TreeItem *p_at, const String& p_find,int *r_col,bool p_selectable,bool p_backwards) { - - +TreeItem *Tree::_search_item_text(TreeItem *p_at, const String &p_find, int *r_col, bool p_selectable, bool p_backwards) { - while(p_at) { + while (p_at) { - for(int i=0;i<columns.size();i++) { - if (p_at->get_text(i).findn(p_find)==0 && (!p_selectable || p_at->is_selectable(i))) { + for (int i = 0; i < columns.size(); i++) { + if (p_at->get_text(i).findn(p_find) == 0 && (!p_selectable || p_at->is_selectable(i))) { if (r_col) - *r_col=i; + *r_col = i; return p_at; } } if (p_backwards) - p_at=p_at->get_prev_visible(); + p_at = p_at->get_prev_visible(); else - p_at=p_at->get_next_visible(); + p_at = p_at->get_next_visible(); } return NULL; - } - -TreeItem* Tree::search_item_text(const String& p_find,int *r_col,bool p_selectable) { +TreeItem *Tree::search_item_text(const String &p_find, int *r_col, bool p_selectable) { if (!root) return NULL; - return _search_item_text(root,p_find,r_col,p_selectable); - + return _search_item_text(root, p_find, r_col, p_selectable); } -void Tree::_do_incr_search(const String& p_add) { +void Tree::_do_incr_search(const String &p_add) { uint64_t time = OS::get_singleton()->get_ticks_usec() / 1000; // convert to msec uint64_t diff = time - last_keypress; - if (diff > uint64_t(GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec",2000))) - incr_search=p_add; + if (diff > uint64_t(GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec", 2000))) + incr_search = p_add; else - incr_search+=p_add; - + incr_search += p_add; - last_keypress=time; + last_keypress = time; int col; - TreeItem *item = search_item_text(incr_search,&col,true); + TreeItem *item = search_item_text(incr_search, &col, true); if (!item) return; item->select(col); ensure_cursor_is_visible(); - - } -TreeItem* Tree::_find_item_at_pos(TreeItem*p_item, const Point2& p_pos,int& r_column,int &h,int §ion) 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; + if (root != p_item || !hide_root) { - if (root!=p_item || ! hide_root) { + h = compute_item_height(p_item) + cache.vseparation; + if (pos.y < h) { - 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; + 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; + section = 0; } - for(int i=0;i<columns.size();i++) { + for (int i = 0; i < columns.size(); i++) { int w = get_column_width(i); if (pos.x < w) { - r_column=i; - + r_column = i; return p_item; } - pos.x-=w; + pos.x -= w; } - - - return NULL; } else { - pos.y-=h; + pos.y -= h; } } else { - h=0; + h = 0; } if (p_item->is_collapsed()) return NULL; // do not try childs, it's collapsed TreeItem *n = p_item->get_children(); - while(n) { - + while (n) { int ch; - TreeItem *r = _find_item_at_pos(n,pos,r_column,ch,section); - pos.y-=ch; - h+=ch; + TreeItem *r = _find_item_at_pos(n, pos, r_column, ch, section); + pos.y -= ch; + h += ch; if (r) return r; - n=n->get_next(); + n = n->get_next(); } return NULL; - } -int Tree::get_column_at_pos(const Point2& p_pos) const { +int Tree::get_column_at_pos(const Point2 &p_pos) const { if (root) { - Point2 pos=p_pos; + Point2 pos = p_pos; pos -= cache.bg->get_offset(); - pos.y-=_get_title_button_height(); - if (pos.y<0) + pos.y -= _get_title_button_height(); + if (pos.y < 0) return -1; if (h_scroll->is_visible_in_tree()) - pos.x+=h_scroll->get_value(); + pos.x += h_scroll->get_value(); if (v_scroll->is_visible_in_tree()) - pos.y+=v_scroll->get_value(); + pos.y += v_scroll->get_value(); - int col,h,section; - TreeItem *it = _find_item_at_pos(root,pos,col,h,section); + int col, h, section; + TreeItem *it = _find_item_at_pos(root, pos, col, h, section); if (it) { return col; @@ -3423,26 +3215,25 @@ int Tree::get_column_at_pos(const Point2& p_pos) const { } return -1; - } -int Tree::get_drop_section_at_pos(const Point2& p_pos) const { +int Tree::get_drop_section_at_pos(const Point2 &p_pos) const { if (root) { - Point2 pos=p_pos; + Point2 pos = p_pos; pos -= cache.bg->get_offset(); - pos.y-=_get_title_button_height(); - if (pos.y<0) + pos.y -= _get_title_button_height(); + if (pos.y < 0) return -100; if (h_scroll->is_visible_in_tree()) - pos.x+=h_scroll->get_value(); + pos.x += h_scroll->get_value(); if (v_scroll->is_visible_in_tree()) - pos.y+=v_scroll->get_value(); + pos.y += v_scroll->get_value(); - int col,h,section; - TreeItem *it = _find_item_at_pos(root,pos,col,h,section); + int col, h, section; + TreeItem *it = _find_item_at_pos(root, pos, col, h, section); if (it) { return section; @@ -3450,26 +3241,24 @@ int Tree::get_drop_section_at_pos(const Point2& p_pos) const { } return -100; - } -TreeItem* Tree::get_item_at_pos(const Point2& p_pos) const { - +TreeItem *Tree::get_item_at_pos(const Point2 &p_pos) const { if (root) { - Point2 pos=p_pos; + Point2 pos = p_pos; pos -= cache.bg->get_offset(); - pos.y-=_get_title_button_height(); - if (pos.y<0) + pos.y -= _get_title_button_height(); + if (pos.y < 0) return NULL; if (h_scroll->is_visible_in_tree()) - pos.x+=h_scroll->get_value(); + pos.x += h_scroll->get_value(); if (v_scroll->is_visible_in_tree()) - pos.y+=v_scroll->get_value(); + pos.y += v_scroll->get_value(); - int col,h,section; - TreeItem *it = _find_item_at_pos(root,pos,col,h,section); + int col, h, section; + TreeItem *it = _find_item_at_pos(root, pos, col, h, section); if (it) { @@ -3478,35 +3267,33 @@ TreeItem* Tree::get_item_at_pos(const Point2& p_pos) const { } return NULL; - } -String Tree::get_tooltip(const Point2& p_pos) const { +String Tree::get_tooltip(const Point2 &p_pos) const { if (root) { - Point2 pos=p_pos; + Point2 pos = p_pos; pos -= cache.bg->get_offset(); - pos.y-=_get_title_button_height(); - if (pos.y<0) + pos.y -= _get_title_button_height(); + if (pos.y < 0) return Control::get_tooltip(p_pos); if (h_scroll->is_visible_in_tree()) - pos.x+=h_scroll->get_value(); + pos.x += h_scroll->get_value(); if (v_scroll->is_visible_in_tree()) - pos.y+=v_scroll->get_value(); - - int col,h,section; - TreeItem *it = _find_item_at_pos(root,pos,col,h,section); + pos.y += v_scroll->get_value(); + int col, h, section; + TreeItem *it = _find_item_at_pos(root, pos, col, h, section); if (it) { String ret; - if (it->get_tooltip(col)=="") - ret=it->get_text(col); + if (it->get_tooltip(col) == "") + ret = it->get_text(col); else - ret=it->get_tooltip(col); + ret = it->get_tooltip(col); return ret; } } @@ -3516,7 +3303,7 @@ String Tree::get_tooltip(const Point2& p_pos) const { void Tree::set_cursor_can_exit_tree(bool p_enable) { - cursor_can_exit_tree=p_enable; + cursor_can_exit_tree = p_enable; } bool Tree::can_cursor_exit_tree() const { @@ -3525,7 +3312,7 @@ bool Tree::can_cursor_exit_tree() const { } void Tree::set_hide_folding(bool p_hide) { - hide_folding=p_hide; + hide_folding = p_hide; update(); } @@ -3539,11 +3326,11 @@ void Tree::set_value_evaluator(ValueEvaluator *p_evaluator) { } void Tree::set_drop_mode_flags(int p_flags) { - if (drop_mode_flags==p_flags) + if (drop_mode_flags == p_flags) return; - drop_mode_flags=p_flags; - if (drop_mode_flags==0) { - drop_mode_over=NULL; + drop_mode_flags = p_flags; + if (drop_mode_flags == 0) { + drop_mode_over = NULL; } update(); @@ -3556,7 +3343,7 @@ int Tree::get_drop_mode_flags() const { void Tree::set_single_select_cell_editing_only_when_already_selected(bool p_enable) { - force_select_on_already_selected=p_enable; + force_select_on_already_selected = p_enable; } bool Tree::get_single_select_cell_editing_only_when_already_selected() const { @@ -3566,7 +3353,7 @@ bool Tree::get_single_select_cell_editing_only_when_already_selected() const { void Tree::set_edit_checkbox_cell_only_when_checkbox_is_pressed(bool p_enable) { - force_edit_checkbox_only_on_checkbox=p_enable; + force_edit_checkbox_only_on_checkbox = p_enable; } bool Tree::get_edit_checkbox_cell_only_when_checkbox_is_pressed() const { @@ -3574,194 +3361,188 @@ bool Tree::get_edit_checkbox_cell_only_when_checkbox_is_pressed() const { return force_edit_checkbox_only_on_checkbox; } - void Tree::set_allow_rmb_select(bool p_allow) { - allow_rmb_select=p_allow; + allow_rmb_select = p_allow; } -bool Tree::get_allow_rmb_select() const{ +bool Tree::get_allow_rmb_select() const { return allow_rmb_select; } - void Tree::_bind_methods() { - ClassDB::bind_method(D_METHOD("_range_click_timeout"),&Tree::_range_click_timeout); - ClassDB::bind_method(D_METHOD("_gui_input"),&Tree::_gui_input); - ClassDB::bind_method(D_METHOD("_popup_select"),&Tree::popup_select); - ClassDB::bind_method(D_METHOD("_text_editor_enter"),&Tree::text_editor_enter); - ClassDB::bind_method(D_METHOD("_text_editor_modal_close"),&Tree::_text_editor_modal_close); - ClassDB::bind_method(D_METHOD("_value_editor_changed"),&Tree::value_editor_changed); - ClassDB::bind_method(D_METHOD("_scroll_moved"),&Tree::_scroll_moved); - - ClassDB::bind_method(D_METHOD("clear"),&Tree::clear); - ClassDB::bind_method(D_METHOD("create_item:TreeItem","parent:TreeItem"),&Tree::_create_item,DEFVAL(Variant())); - - ClassDB::bind_method(D_METHOD("get_root:TreeItem"),&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_expand","column","expand"),&Tree::set_column_expand); - 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("get_next_selected:TreeItem","from:TreeItem"),&Tree::_get_next_selected); - ClassDB::bind_method(D_METHOD("get_selected:TreeItem"),&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("set_columns","amount"),&Tree::set_columns); - ClassDB::bind_method(D_METHOD("get_columns"),&Tree::get_columns); - - ClassDB::bind_method(D_METHOD("get_edited:TreeItem"),&Tree::get_edited); - ClassDB::bind_method(D_METHOD("get_edited_column"),&Tree::get_edited_column); - ClassDB::bind_method(D_METHOD("get_custom_popup_rect"),&Tree::get_custom_popup_rect); - ClassDB::bind_method(D_METHOD("get_item_area_rect","item:TreeItem","column"),&Tree::_get_item_rect,DEFVAL(-1)); - ClassDB::bind_method(D_METHOD("get_item_at_pos:TreeItem","pos"),&Tree::get_item_at_pos); - ClassDB::bind_method(D_METHOD("get_column_at_pos","pos"),&Tree::get_column_at_pos); - - ClassDB::bind_method(D_METHOD("ensure_cursor_is_visible"),&Tree::ensure_cursor_is_visible); - - ClassDB::bind_method(D_METHOD("set_column_titles_visible","visible"),&Tree::set_column_titles_visible); - ClassDB::bind_method(D_METHOD("are_column_titles_visible"),&Tree::are_column_titles_visible); - - ClassDB::bind_method(D_METHOD("set_column_title","column","title"),&Tree::set_column_title); - ClassDB::bind_method(D_METHOD("get_column_title","column"),&Tree::get_column_title); - ClassDB::bind_method(D_METHOD("get_scroll"),&Tree::get_scroll); - - ClassDB::bind_method(D_METHOD("set_hide_folding","hide"),&Tree::set_hide_folding); - ClassDB::bind_method(D_METHOD("is_folding_hidden"),&Tree::is_folding_hidden); - - ClassDB::bind_method(D_METHOD("set_drop_mode_flags","flags"),&Tree::set_drop_mode_flags); - ClassDB::bind_method(D_METHOD("get_drop_mode_flags"),&Tree::get_drop_mode_flags); - - ClassDB::bind_method(D_METHOD("set_allow_rmb_select","allow"),&Tree::set_allow_rmb_select); - ClassDB::bind_method(D_METHOD("get_allow_rmb_select"),&Tree::get_allow_rmb_select); - - ClassDB::bind_method(D_METHOD("set_single_select_cell_editing_only_when_already_selected","enable"),&Tree::set_single_select_cell_editing_only_when_already_selected); - ClassDB::bind_method(D_METHOD("get_single_select_cell_editing_only_when_already_selected"),&Tree::get_single_select_cell_editing_only_when_already_selected); - - 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")) ); - ADD_SIGNAL( MethodInfo("item_rmb_selected",PropertyInfo(Variant::VECTOR2,"pos"))); - ADD_SIGNAL( MethodInfo("empty_tree_rmb_selected",PropertyInfo(Variant::VECTOR2,"pos"))); - ADD_SIGNAL( MethodInfo("item_edited")); - ADD_SIGNAL( MethodInfo("item_double_clicked")); - ADD_SIGNAL( MethodInfo("item_collapsed",PropertyInfo(Variant::OBJECT,"item"))); + ClassDB::bind_method(D_METHOD("_range_click_timeout"), &Tree::_range_click_timeout); + ClassDB::bind_method(D_METHOD("_gui_input"), &Tree::_gui_input); + ClassDB::bind_method(D_METHOD("_popup_select"), &Tree::popup_select); + ClassDB::bind_method(D_METHOD("_text_editor_enter"), &Tree::text_editor_enter); + ClassDB::bind_method(D_METHOD("_text_editor_modal_close"), &Tree::_text_editor_modal_close); + ClassDB::bind_method(D_METHOD("_value_editor_changed"), &Tree::value_editor_changed); + ClassDB::bind_method(D_METHOD("_scroll_moved"), &Tree::_scroll_moved); + + ClassDB::bind_method(D_METHOD("clear"), &Tree::clear); + ClassDB::bind_method(D_METHOD("create_item:TreeItem", "parent:TreeItem"), &Tree::_create_item, DEFVAL(Variant())); + + ClassDB::bind_method(D_METHOD("get_root:TreeItem"), &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_expand", "column", "expand"), &Tree::set_column_expand); + 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("get_next_selected:TreeItem", "from:TreeItem"), &Tree::_get_next_selected); + ClassDB::bind_method(D_METHOD("get_selected:TreeItem"), &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("set_columns", "amount"), &Tree::set_columns); + ClassDB::bind_method(D_METHOD("get_columns"), &Tree::get_columns); + + ClassDB::bind_method(D_METHOD("get_edited:TreeItem"), &Tree::get_edited); + ClassDB::bind_method(D_METHOD("get_edited_column"), &Tree::get_edited_column); + ClassDB::bind_method(D_METHOD("get_custom_popup_rect"), &Tree::get_custom_popup_rect); + ClassDB::bind_method(D_METHOD("get_item_area_rect", "item:TreeItem", "column"), &Tree::_get_item_rect, DEFVAL(-1)); + ClassDB::bind_method(D_METHOD("get_item_at_pos:TreeItem", "pos"), &Tree::get_item_at_pos); + ClassDB::bind_method(D_METHOD("get_column_at_pos", "pos"), &Tree::get_column_at_pos); + + ClassDB::bind_method(D_METHOD("ensure_cursor_is_visible"), &Tree::ensure_cursor_is_visible); + + ClassDB::bind_method(D_METHOD("set_column_titles_visible", "visible"), &Tree::set_column_titles_visible); + ClassDB::bind_method(D_METHOD("are_column_titles_visible"), &Tree::are_column_titles_visible); + + ClassDB::bind_method(D_METHOD("set_column_title", "column", "title"), &Tree::set_column_title); + ClassDB::bind_method(D_METHOD("get_column_title", "column"), &Tree::get_column_title); + ClassDB::bind_method(D_METHOD("get_scroll"), &Tree::get_scroll); + + ClassDB::bind_method(D_METHOD("set_hide_folding", "hide"), &Tree::set_hide_folding); + ClassDB::bind_method(D_METHOD("is_folding_hidden"), &Tree::is_folding_hidden); + + ClassDB::bind_method(D_METHOD("set_drop_mode_flags", "flags"), &Tree::set_drop_mode_flags); + ClassDB::bind_method(D_METHOD("get_drop_mode_flags"), &Tree::get_drop_mode_flags); + + ClassDB::bind_method(D_METHOD("set_allow_rmb_select", "allow"), &Tree::set_allow_rmb_select); + ClassDB::bind_method(D_METHOD("get_allow_rmb_select"), &Tree::get_allow_rmb_select); + + ClassDB::bind_method(D_METHOD("set_single_select_cell_editing_only_when_already_selected", "enable"), &Tree::set_single_select_cell_editing_only_when_already_selected); + ClassDB::bind_method(D_METHOD("get_single_select_cell_editing_only_when_already_selected"), &Tree::get_single_select_cell_editing_only_when_already_selected); + + 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"))); + ADD_SIGNAL(MethodInfo("item_rmb_selected", PropertyInfo(Variant::VECTOR2, "pos"))); + ADD_SIGNAL(MethodInfo("empty_tree_rmb_selected", PropertyInfo(Variant::VECTOR2, "pos"))); + ADD_SIGNAL(MethodInfo("item_edited")); + ADD_SIGNAL(MethodInfo("item_double_clicked")); + ADD_SIGNAL(MethodInfo("item_collapsed", PropertyInfo(Variant::OBJECT, "item"))); //ADD_SIGNAL( MethodInfo("item_doubleclicked" ) ); - ADD_SIGNAL( MethodInfo("button_pressed",PropertyInfo(Variant::OBJECT,"item"),PropertyInfo(Variant::INT,"column"),PropertyInfo(Variant::INT,"id"))); - ADD_SIGNAL( MethodInfo("custom_popup_edited",PropertyInfo(Variant::BOOL,"arrow_clicked") ) ); - ADD_SIGNAL( MethodInfo("item_activated")); + ADD_SIGNAL(MethodInfo("button_pressed", PropertyInfo(Variant::OBJECT, "item"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::INT, "id"))); + ADD_SIGNAL(MethodInfo("custom_popup_edited", PropertyInfo(Variant::BOOL, "arrow_clicked"))); + ADD_SIGNAL(MethodInfo("item_activated")); - 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 ); + 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() { - selected_col=0; + selected_col = 0; columns.resize(1); - selected_item=NULL; - edited_item=NULL; - selected_col=-1; - edited_col=-1; - - hide_root=false; - select_mode=SELECT_SINGLE; - root=0; - popup_menu=NULL; - popup_edited_item=NULL; - text_editor=NULL; + selected_item = NULL; + edited_item = NULL; + selected_col = -1; + edited_col = -1; + + hide_root = false; + select_mode = SELECT_SINGLE; + root = 0; + popup_menu = NULL; + popup_edited_item = NULL; + text_editor = NULL; set_focus_mode(FOCUS_ALL); - - popup_menu = memnew( PopupMenu ); + popup_menu = memnew(PopupMenu); popup_menu->hide(); add_child(popup_menu); popup_menu->set_as_toplevel(true); - text_editor = memnew( LineEdit ); + text_editor = memnew(LineEdit); add_child(text_editor); text_editor->set_as_toplevel(true); text_editor->hide(); - value_editor = memnew( HSlider ); + value_editor = memnew(HSlider); add_child(value_editor); value_editor->set_as_toplevel(true); value_editor->hide(); - h_scroll = memnew( HScrollBar ); - v_scroll = memnew( VScrollBar ); + h_scroll = memnew(HScrollBar); + v_scroll = memnew(VScrollBar); add_child(h_scroll); add_child(v_scroll); - range_click_timer = memnew( Timer ); - range_click_timer->connect("timeout",this,"_range_click_timeout"); + range_click_timer = memnew(Timer); + range_click_timer->connect("timeout", this, "_range_click_timeout"); add_child(range_click_timer); - h_scroll->connect("value_changed", this,"_scroll_moved"); - v_scroll->connect("value_changed", this,"_scroll_moved"); - text_editor->connect("text_entered", this,"_text_editor_enter"); - text_editor->connect("modal_closed", this,"_text_editor_modal_close"); - popup_menu->connect("id_pressed", this,"_popup_select"); - value_editor->connect("value_changed", this,"_value_editor_changed"); + h_scroll->connect("value_changed", this, "_scroll_moved"); + v_scroll->connect("value_changed", this, "_scroll_moved"); + text_editor->connect("text_entered", this, "_text_editor_enter"); + text_editor->connect("modal_closed", this, "_text_editor_modal_close"); + popup_menu->connect("id_pressed", this, "_popup_select"); + value_editor->connect("value_changed", this, "_value_editor_changed"); value_editor->set_as_toplevel(true); text_editor->set_as_toplevel(true); - updating_value_editor=false; - pressed_button=-1; - show_column_titles=false; + updating_value_editor = false; + pressed_button = -1; + show_column_titles = false; cache.click_type = Cache::CLICK_NONE; cache.hover_type = Cache::CLICK_NONE; cache.hover_index = -1; - cache.click_index=-1; - cache.click_id=-1; - cache.click_item=NULL; - cache.click_column=0; - last_keypress=0; - focus_in_id=0; + cache.click_index = -1; + cache.click_id = -1; + cache.click_item = NULL; + cache.click_column = 0; + last_keypress = 0; + focus_in_id = 0; - blocked=0; + blocked = 0; - cursor_can_exit_tree=true; + cursor_can_exit_tree = true; set_mouse_filter(MOUSE_FILTER_STOP); - drag_speed=0; - drag_touching=false; - drag_touching_deaccel=false; - pressing_for_editor=false; - range_drag_enabled=false; + drag_speed = 0; + drag_touching = false; + drag_touching_deaccel = false; + pressing_for_editor = false; + range_drag_enabled = false; - hide_folding=false; + hide_folding = false; - evaluator=NULL; + evaluator = NULL; - drop_mode_flags=0; - drop_mode_over=NULL; - drop_mode_section=0; - single_select_defer=NULL; - force_select_on_already_selected=false; + drop_mode_flags = 0; + drop_mode_over = NULL; + drop_mode_section = 0; + single_select_defer = NULL; + force_select_on_already_selected = false; - allow_rmb_select=false; - force_edit_checkbox_only_on_checkbox=false; + allow_rmb_select = false; + force_edit_checkbox_only_on_checkbox = false; set_clip_contents(true); } - Tree::~Tree() { if (root) { - memdelete( root ); + memdelete(root); } - } diff --git a/scene/gui/tree.h b/scene/gui/tree.h index 14bd2efbaa..68d4453156 100644 --- a/scene/gui/tree.h +++ b/scene/gui/tree.h @@ -29,25 +29,24 @@ #ifndef TREE_H #define TREE_H +#include "core/helper/value_evaluator.h" #include "scene/gui/control.h" -#include "scene/gui/popup_menu.h" #include "scene/gui/line_edit.h" +#include "scene/gui/popup_menu.h" #include "scene/gui/scroll_bar.h" #include "scene/gui/slider.h" -#include "core/helper/value_evaluator.h" /** @author Juan Linietsky <reduzio@gmail.com> */ - class Tree; class TreeItem : public Object { - GDCLASS(TreeItem,Object); -public: + GDCLASS(TreeItem, Object); +public: enum TreeCellMode { CELL_MODE_STRING, ///< just a string @@ -59,8 +58,7 @@ public: }; private: -friend class Tree; - + friend class Tree; struct Cell { @@ -70,7 +68,7 @@ friend class Tree; Rect2i icon_region; String text; String suffix; - double min,max,step,val; + double min, max, step, val; int icon_max_w; bool expr; bool checked; @@ -94,33 +92,35 @@ friend class Tree; bool disabled; Ref<Texture> texture; Color color; - Button() { id=0; disabled=false; color=Color(1,1,1,1); } + Button() { + id = 0; + disabled = false; + color = Color(1, 1, 1, 1); + } }; - Vector< Button > buttons; + Vector<Button> buttons; Cell() { - custom_draw_obj=0; - mode=TreeItem::CELL_MODE_STRING; - min=0; - max=100; - step=1; - val=0; - checked=false; - editable=false; - selected=false; - selectable=true; - custom_color=false; - custom_bg_color=false; - expr=false; - icon_max_w=0; + custom_draw_obj = 0; + mode = TreeItem::CELL_MODE_STRING; + min = 0; + max = 100; + step = 1; + val = 0; + checked = false; + editable = false; + selected = false; + selectable = true; + custom_color = false; + custom_bg_color = false; + expr = false; + icon_max_w = 0; } - Size2 get_icon_size() const; - void draw_icon(const RID& p_where, const Point2& p_pos, const Size2& p_size=Size2()) const; - + void draw_icon(const RID &p_where, const Point2 &p_pos, const Size2 &p_size = Size2()) const; }; Vector<Cell> cells; @@ -132,79 +132,76 @@ friend class Tree; TreeItem *childs; //child items Tree *tree; //tree (for reference) - - TreeItem(Tree *p_tree); - void _changed_notify(int p_cell); void _changed_notify(); void _cell_selected(int p_cell); void _cell_deselected(int p_cell); -protected: +protected: static void _bind_methods(); //bind helpers - Dictionary _get_range_config( int p_column ) { + Dictionary _get_range_config(int p_column) { Dictionary d; - double min,max,step; - get_range_config(p_column,min,max,step); - d["min"]=min; - d["max"]=max; - d["step"]=step; - d["expr"]=false; + double min, max, step; + get_range_config(p_column, min, max, step); + d["min"] = min; + d["max"] = max; + d["step"] = step; + d["expr"] = false; return d; } - void _remove_child(Object *p_child) { remove_child( p_child->cast_to<TreeItem>() ); } -public: + void _remove_child(Object *p_child) { remove_child(p_child->cast_to<TreeItem>()); } +public: /* cell mode */ - void set_cell_mode( int p_column, TreeCellMode p_mode ); - TreeCellMode get_cell_mode( int p_column ) const; + void set_cell_mode(int p_column, TreeCellMode p_mode); + TreeCellMode get_cell_mode(int p_column) const; /* check mode */ - void set_checked(int p_column,bool p_checked); + void set_checked(int p_column, bool p_checked); bool is_checked(int p_column) const; - void set_text(int p_column,String p_text); + void set_text(int p_column, String p_text); String get_text(int p_column) const; - void set_suffix(int p_column,String p_suffix); + void set_suffix(int p_column, String p_suffix); String get_suffix(int p_column) const; - void set_icon(int p_column,const Ref<Texture>& p_icon); + void set_icon(int p_column, const Ref<Texture> &p_icon); Ref<Texture> get_icon(int p_column) const; - void set_icon_region(int p_column,const Rect2& p_icon_region); + void set_icon_region(int p_column, const Rect2 &p_icon_region); Rect2 get_icon_region(int p_column) const; - void set_icon_max_width(int p_column,int p_max); + void set_icon_max_width(int p_column, int p_max); int get_icon_max_width(int p_column) const; - void add_button(int p_column,const Ref<Texture>& p_button,int p_id=-1,bool p_disabled=false); + void add_button(int p_column, const Ref<Texture> &p_button, int p_id = -1, bool p_disabled = false); int get_button_count(int p_column) const; - Ref<Texture> get_button(int p_column,int p_idx) const; - int get_button_id(int p_column,int p_idx) const; - void erase_button(int p_column,int p_idx); - int get_button_by_id(int p_column,int p_id) const; - bool is_button_disabled(int p_column,int p_idx) const; - void set_button(int p_column,int p_idx,const Ref<Texture>& p_button); - void set_button_color(int p_column,int p_idx,const Color& p_color); + Ref<Texture> get_button(int p_column, int p_idx) const; + int get_button_id(int p_column, int p_idx) const; + void erase_button(int p_column, int p_idx); + int get_button_by_id(int p_column, int p_id) const; + bool is_button_disabled(int p_column, int p_idx) const; + void set_button(int p_column, int p_idx, const Ref<Texture> &p_button); + void set_button_color(int p_column, int p_idx, const Color &p_color); /* range works for mode number or mode combo */ - void set_range(int p_column,double p_value); + void set_range(int p_column, double p_value); double get_range(int p_column) const; - void set_range_config(int p_column,double p_min,double p_max,double p_step,bool p_exp=false); - void get_range_config(int p_column,double& r_min,double& r_max,double &r_step) const; + void set_range_config(int p_column, double p_min, double p_max, double p_step, bool p_exp = false); + void get_range_config(int p_column, double &r_min, double &r_max, double &r_step) const; bool is_range_exponential(int p_column) const; - void set_metadata(int p_column,const Variant& p_meta); + void set_metadata(int p_column, const Variant &p_meta); Variant get_metadata(int p_column) const; - void set_custom_draw(int p_column,Object *p_object,const StringName& p_callback); + void set_custom_draw(int p_column, Object *p_object, const StringName &p_callback); void set_collapsed(bool p_collapsed); bool is_collapsed(); @@ -219,7 +216,7 @@ public: void remove_child(TreeItem *p_item); - void set_selectable(int p_column,bool p_selectable); + void set_selectable(int p_column, bool p_selectable); bool is_selectable(int p_column) const; bool is_selected(int p_column); @@ -227,52 +224,49 @@ public: void deselect(int p_column); void set_as_cursor(int p_column); - void set_editable(int p_column,bool p_editable); + void set_editable(int p_column, bool p_editable); bool is_editable(int p_column); - void set_custom_color(int p_column,const Color& p_color); + void set_custom_color(int p_column, const Color &p_color); 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, bool p_bg_outline=false); + 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; - void set_tooltip(int p_column, const String& p_tooltip); + void set_tooltip(int p_column, const String &p_tooltip); String get_tooltip(int p_column) const; - void clear_children(); void move_to_top(); void move_to_bottom(); ~TreeItem(); - }; - -VARIANT_ENUM_CAST( TreeItem::TreeCellMode ); - +VARIANT_ENUM_CAST(TreeItem::TreeCellMode); class Tree : public Control { - GDCLASS( Tree, Control ); + GDCLASS(Tree, Control); + public: enum SelectMode { - SELECT_SINGLE, - SELECT_ROW, - SELECT_MULTI + SELECT_SINGLE, + SELECT_ROW, + SELECT_MULTI }; enum DropModeFlags { - DROP_MODE_DISABLED=0, - DROP_MODE_ON_ITEM=1, - DROP_MODE_INBETWEEN=2 + DROP_MODE_DISABLED = 0, + DROP_MODE_ON_ITEM = 1, + DROP_MODE_INBETWEEN = 2 }; private: -friend class TreeItem; + friend class TreeItem; TreeItem *root; TreeItem *popup_edited_item; @@ -295,7 +289,6 @@ friend class TreeItem; bool range_drag_enabled; Vector2 range_drag_capture_pos; - //TreeItem *cursor_item; //int cursor_column; @@ -315,7 +308,10 @@ friend class TreeItem; int min_width; bool expand; String title; - ColumnInfo() { min_width=1; expand=true; } + ColumnInfo() { + min_width = 1; + expand = true; + } }; bool show_column_titles; @@ -335,10 +331,10 @@ friend class TreeItem; int compute_item_height(TreeItem *p_item) const; int get_item_height(TreeItem *p_item) const; //void draw_item_text(String p_text,const Ref<Texture>& p_icon,int p_icon_max_w,bool p_tool,Rect2i p_rect,const Color& p_color); - void draw_item_rect(const TreeItem::Cell& p_cell,const Rect2i& p_rect,const Color& p_color); - int draw_item(const Point2i& p_pos,const Point2& p_draw_ofs, const Size2& p_draw_size,TreeItem *p_item); - void select_single_item(TreeItem *p_selected,TreeItem *p_current,int p_col,TreeItem *p_prev=NULL,bool *r_in_range=NULL,bool p_force_deselect=false); - int propagate_mouse_event(const Point2i &p_pos,int x_ofs,int y_ofs,bool p_doubleclick,TreeItem *p_item,int p_button,const InputModifierState& p_mod); + void draw_item_rect(const TreeItem::Cell &p_cell, const Rect2i &p_rect, const Color &p_color); + int draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 &p_draw_size, TreeItem *p_item); + void select_single_item(TreeItem *p_selected, TreeItem *p_current, int p_col, TreeItem *p_prev = NULL, bool *r_in_range = NULL, bool p_force_deselect = false); + int propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, bool p_doubleclick, TreeItem *p_item, int p_button, const InputModifierState &p_mod); void text_editor_enter(String p_text); void _text_editor_modal_close(); void value_editor_changed(double p_value); @@ -350,10 +346,10 @@ friend class TreeItem; Size2 get_minimum_size() const; - void item_edited(int p_column,TreeItem *p_item); - void item_changed(int p_column,TreeItem *p_item); - void item_selected(int p_column,TreeItem *p_item); - void item_deselected(int p_column,TreeItem *p_item); + void item_edited(int p_column, TreeItem *p_item); + void item_changed(int p_column, TreeItem *p_item); + void item_selected(int p_column, TreeItem *p_item); + void item_deselected(int p_column, TreeItem *p_item); void propagate_set_columns(TreeItem *p_item); @@ -413,7 +409,6 @@ friend class TreeItem; } cache; - int _get_title_button_height() const; void _scroll_moved(float p_value); @@ -429,13 +424,13 @@ friend class TreeItem; uint64_t last_keypress; String incr_search; bool cursor_can_exit_tree; - void _do_incr_search(const String& p_add); + void _do_incr_search(const String &p_add); - TreeItem* _search_item_text(TreeItem *p_at, const String& p_find,int *r_col,bool p_selectable,bool p_backwards=false); + 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, int §ion) 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_speed; float drag_accum; float last_drag_accum; @@ -459,37 +454,35 @@ friend class TreeItem; ValueEvaluator *evaluator; - int _count_selected_items(TreeItem* p_from) const; + int _count_selected_items(TreeItem *p_from) const; protected: static void _bind_methods(); //bind helpers - 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 ); } - + 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; - 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; + 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); - TreeItem* get_root(); - TreeItem* get_last_item(); + TreeItem *create_item(TreeItem *p_parent = 0); + TreeItem *get_root(); + TreeItem *get_last_item(); - void set_column_min_width(int p_column,int p_min_width); - void set_column_expand(int p_column,bool p_expand); + void set_column_min_width(int p_column, int p_min_width); + void set_column_expand(int p_column, bool p_expand); int get_column_width(int p_column) const; void set_hide_root(bool p_eanbled); - TreeItem *get_next_selected( TreeItem* p_item); + TreeItem *get_next_selected(TreeItem *p_item); TreeItem *get_selected() const; int get_selected_column() const; int get_pressed_button() const; @@ -498,7 +491,7 @@ public: void set_columns(int p_columns); int get_columns() const; - void set_column_title(int p_column,const String& p_title); + void set_column_title(int p_column, const String &p_title); String get_column_title(int p_column) const; void set_column_titles_visible(bool p_show); @@ -512,10 +505,10 @@ public: Rect2 get_custom_popup_rect() const; int get_item_offset(TreeItem *p_item) const; - Rect2 get_item_rect(TreeItem *p_item,int p_column=-1) const; + Rect2 get_item_rect(TreeItem *p_item, int p_column = -1) const; bool edit_selected(); - TreeItem* search_item_text(const String& p_find,int *r_col=NULL,bool p_selectable=false); + TreeItem *search_item_text(const String &p_find, int *r_col = NULL, bool p_selectable = false); Point2 get_scroll() const; @@ -536,7 +529,6 @@ public: void set_edit_checkbox_cell_only_when_checkbox_is_pressed(bool p_enable); bool get_edit_checkbox_cell_only_when_checkbox_is_pressed() const; - void set_allow_rmb_select(bool p_allow); bool get_allow_rmb_select() const; @@ -544,8 +536,7 @@ public: Tree(); ~Tree(); - }; -VARIANT_ENUM_CAST( Tree::SelectMode ); +VARIANT_ENUM_CAST(Tree::SelectMode); #endif diff --git a/scene/gui/video_player.cpp b/scene/gui/video_player.cpp index 063ad8c44a..433cc546a6 100644 --- a/scene/gui/video_player.cpp +++ b/scene/gui/video_player.cpp @@ -54,15 +54,15 @@ int VideoPlayer::sp_get_channel_count() const { return playback->get_channels(); } -void VideoPlayer::sp_set_mix_rate(int p_rate){ +void VideoPlayer::sp_set_mix_rate(int p_rate) { - server_mix_rate=p_rate; + server_mix_rate = p_rate; } -bool VideoPlayer::sp_mix(int32_t *p_buffer,int p_frames) { +bool VideoPlayer::sp_mix(int32_t *p_buffer, int p_frames) { if (resampler.is_ready()) { - return resampler.mix(p_buffer,p_frames); + return resampler.mix(p_buffer, p_frames); } return false; @@ -93,24 +93,22 @@ void VideoPlayer::sp_update() { #endif } -int VideoPlayer::_audio_mix_callback(void* p_udata,const int16_t *p_data,int p_frames) { +int VideoPlayer::_audio_mix_callback(void *p_udata, const int16_t *p_data, int p_frames) { - VideoPlayer *vp=(VideoPlayer*)p_udata; + VideoPlayer *vp = (VideoPlayer *)p_udata; - int todo=MIN(vp->resampler.get_todo(),p_frames); + int todo = MIN(vp->resampler.get_todo(), p_frames); int16_t *wb = vp->resampler.get_write_buffer(); int c = vp->resampler.get_channel_count(); - for(int i=0;i<todo*c;i++) { - wb[i]=p_data[i]; + for (int i = 0; i < todo * c; i++) { + wb[i] = p_data[i]; } vp->resampler.write(todo); return todo; } - - void VideoPlayer::_notification(int p_notification) { switch (p_notification) { @@ -133,12 +131,11 @@ void VideoPlayer::_notification(int p_notification) { double audio_time = USEC_TO_SEC(OS::get_singleton()->get_ticks_usec()); //AudioServer::get_singleton()->get_mix_time(); - double delta = last_audio_time==0?0:audio_time-last_audio_time; - last_audio_time=audio_time; - if (delta==0) + double delta = last_audio_time == 0 ? 0 : audio_time - last_audio_time; + last_audio_time = audio_time; + if (delta == 0) return; - playback->update(delta); } break; @@ -150,17 +147,13 @@ void VideoPlayer::_notification(int p_notification) { if (texture->get_width() == 0) return; - Size2 s=expand?get_size():texture->get_size(); - draw_texture_rect(texture,Rect2(Point2(),s),false); + Size2 s = expand ? get_size() : texture->get_size(); + draw_texture_rect(texture, Rect2(Point2(), s), false); } break; }; - }; - - - Size2 VideoPlayer::get_minimum_size() const { if (!expand && !texture.is_null()) @@ -171,7 +164,7 @@ Size2 VideoPlayer::get_minimum_size() const { void VideoPlayer::set_expand(bool p_expand) { - expand=p_expand; + expand = p_expand; update(); minimum_size_changed(); } @@ -181,35 +174,34 @@ bool VideoPlayer::has_expand() const { return expand; } - void VideoPlayer::set_stream(const Ref<VideoStream> &p_stream) { stop(); - stream=p_stream; - if (stream.is_valid()) { - stream->set_audio_track(audio_track); - playback=stream->instance_playback(); - } else { - playback=Ref<VideoStreamPlayback>(); - } + stream = p_stream; + if (stream.is_valid()) { + stream->set_audio_track(audio_track); + playback = stream->instance_playback(); + } else { + playback = Ref<VideoStreamPlayback>(); + } if (!playback.is_null()) { playback->set_loop(loops); playback->set_paused(paused); - texture=playback->get_texture(); + texture = playback->get_texture(); const int channels = playback->get_channels(); AudioServer::get_singleton()->lock(); if (channels > 0) - resampler.setup(channels,playback->get_mix_rate(),server_mix_rate,buffering_ms,0); + resampler.setup(channels, playback->get_mix_rate(), server_mix_rate, buffering_ms, 0); else resampler.clear(); AudioServer::get_singleton()->unlock(); if (channels > 0) - playback->set_mix_callback(_audio_mix_callback,this); + playback->set_mix_callback(_audio_mix_callback, this); } else { texture.unref(); @@ -219,7 +211,6 @@ void VideoPlayer::set_stream(const Ref<VideoStream> &p_stream) { } update(); - }; Ref<VideoStream> VideoPlayer::get_stream() const { @@ -235,9 +226,9 @@ void VideoPlayer::play() { playback->stop(); playback->play(); set_process_internal(true); -// AudioServer::get_singleton()->stream_set_active(stream_rid,true); -// AudioServer::get_singleton()->stream_set_volume_scale(stream_rid,volume); - last_audio_time=0; + // AudioServer::get_singleton()->stream_set_active(stream_rid,true); + // AudioServer::get_singleton()->stream_set_volume_scale(stream_rid,volume); + last_audio_time = 0; }; void VideoPlayer::stop() { @@ -248,10 +239,10 @@ void VideoPlayer::stop() { return; playback->stop(); -// AudioServer::get_singleton()->stream_set_active(stream_rid,false); + // AudioServer::get_singleton()->stream_set_active(stream_rid,false); resampler.flush(); set_process_internal(false); - last_audio_time=0; + last_audio_time = 0; }; bool VideoPlayer::is_playing() const { @@ -264,7 +255,7 @@ bool VideoPlayer::is_playing() const { void VideoPlayer::set_paused(bool p_paused) { - paused=p_paused; + paused = p_paused; if (playback.is_valid()) { playback->set_paused(p_paused); set_process_internal(!p_paused); @@ -279,27 +270,26 @@ bool VideoPlayer::is_paused() const { void VideoPlayer::set_buffering_msec(int p_msec) { - buffering_ms=p_msec; + buffering_ms = p_msec; } -int VideoPlayer::get_buffering_msec() const{ +int VideoPlayer::get_buffering_msec() const { return buffering_ms; } void VideoPlayer::set_audio_track(int p_track) { - audio_track=p_track; + audio_track = p_track; } int VideoPlayer::get_audio_track() const { - return audio_track; + return audio_track; } - void VideoPlayer::set_volume(float p_vol) { - volume=p_vol; + volume = p_vol; }; float VideoPlayer::get_volume() const { @@ -309,7 +299,7 @@ float VideoPlayer::get_volume() const { void VideoPlayer::set_volume_db(float p_db) { - if (p_db<-79) + if (p_db < -79) set_volume(0); else set_volume(Math::db2linear(p_db)); @@ -317,13 +307,12 @@ void VideoPlayer::set_volume_db(float p_db) { float VideoPlayer::get_volume_db() const { - if (volume==0) + if (volume == 0) return -80; else return Math::linear2db(volume); }; - String VideoPlayer::get_stream_name() const { if (stream.is_null()) @@ -343,12 +332,12 @@ Ref<Texture> VideoPlayer::get_video_texture() { if (playback.is_valid()) return playback->get_texture(); - return Ref<Texture> (); + return Ref<Texture>(); } void VideoPlayer::set_autoplay(bool p_enable) { - autoplay=p_enable; + autoplay = p_enable; }; bool VideoPlayer::has_autoplay() const { @@ -358,75 +347,72 @@ bool VideoPlayer::has_autoplay() const { void VideoPlayer::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_stream","stream:VideoStream"),&VideoPlayer::set_stream); - ClassDB::bind_method(D_METHOD("get_stream:VideoStream"),&VideoPlayer::get_stream); + ClassDB::bind_method(D_METHOD("set_stream", "stream:VideoStream"), &VideoPlayer::set_stream); + ClassDB::bind_method(D_METHOD("get_stream:VideoStream"), &VideoPlayer::get_stream); - ClassDB::bind_method(D_METHOD("play"),&VideoPlayer::play); - ClassDB::bind_method(D_METHOD("stop"),&VideoPlayer::stop); + ClassDB::bind_method(D_METHOD("play"), &VideoPlayer::play); + ClassDB::bind_method(D_METHOD("stop"), &VideoPlayer::stop); - ClassDB::bind_method(D_METHOD("is_playing"),&VideoPlayer::is_playing); + ClassDB::bind_method(D_METHOD("is_playing"), &VideoPlayer::is_playing); - ClassDB::bind_method(D_METHOD("set_paused","paused"),&VideoPlayer::set_paused); - ClassDB::bind_method(D_METHOD("is_paused"),&VideoPlayer::is_paused); + ClassDB::bind_method(D_METHOD("set_paused", "paused"), &VideoPlayer::set_paused); + ClassDB::bind_method(D_METHOD("is_paused"), &VideoPlayer::is_paused); - ClassDB::bind_method(D_METHOD("set_volume","volume"),&VideoPlayer::set_volume); - ClassDB::bind_method(D_METHOD("get_volume"),&VideoPlayer::get_volume); + ClassDB::bind_method(D_METHOD("set_volume", "volume"), &VideoPlayer::set_volume); + ClassDB::bind_method(D_METHOD("get_volume"), &VideoPlayer::get_volume); - ClassDB::bind_method(D_METHOD("set_volume_db","db"),&VideoPlayer::set_volume_db); - ClassDB::bind_method(D_METHOD("get_volume_db"),&VideoPlayer::get_volume_db); + ClassDB::bind_method(D_METHOD("set_volume_db", "db"), &VideoPlayer::set_volume_db); + ClassDB::bind_method(D_METHOD("get_volume_db"), &VideoPlayer::get_volume_db); - ClassDB::bind_method(D_METHOD("set_audio_track","track"),&VideoPlayer::set_audio_track); - ClassDB::bind_method(D_METHOD("get_audio_track"),&VideoPlayer::get_audio_track); + ClassDB::bind_method(D_METHOD("set_audio_track", "track"), &VideoPlayer::set_audio_track); + ClassDB::bind_method(D_METHOD("get_audio_track"), &VideoPlayer::get_audio_track); - ClassDB::bind_method(D_METHOD("get_stream_name"),&VideoPlayer::get_stream_name); + ClassDB::bind_method(D_METHOD("get_stream_name"), &VideoPlayer::get_stream_name); - ClassDB::bind_method(D_METHOD("get_stream_pos"),&VideoPlayer::get_stream_pos); + ClassDB::bind_method(D_METHOD("get_stream_pos"), &VideoPlayer::get_stream_pos); - ClassDB::bind_method(D_METHOD("set_autoplay","enabled"),&VideoPlayer::set_autoplay); - ClassDB::bind_method(D_METHOD("has_autoplay"),&VideoPlayer::has_autoplay); + ClassDB::bind_method(D_METHOD("set_autoplay", "enabled"), &VideoPlayer::set_autoplay); + ClassDB::bind_method(D_METHOD("has_autoplay"), &VideoPlayer::has_autoplay); - ClassDB::bind_method(D_METHOD("set_expand","enable"), &VideoPlayer::set_expand ); - ClassDB::bind_method(D_METHOD("has_expand"), &VideoPlayer::has_expand ); + ClassDB::bind_method(D_METHOD("set_expand", "enable"), &VideoPlayer::set_expand); + ClassDB::bind_method(D_METHOD("has_expand"), &VideoPlayer::has_expand); - ClassDB::bind_method(D_METHOD("set_buffering_msec","msec"),&VideoPlayer::set_buffering_msec); - ClassDB::bind_method(D_METHOD("get_buffering_msec"),&VideoPlayer::get_buffering_msec); + ClassDB::bind_method(D_METHOD("set_buffering_msec", "msec"), &VideoPlayer::set_buffering_msec); + ClassDB::bind_method(D_METHOD("get_buffering_msec"), &VideoPlayer::get_buffering_msec); - ClassDB::bind_method(D_METHOD("get_video_texture:Texture"), &VideoPlayer::get_video_texture ); + ClassDB::bind_method(D_METHOD("get_video_texture:Texture"), &VideoPlayer::get_video_texture); - ADD_PROPERTY( PropertyInfo(Variant::INT, "audio_track",PROPERTY_HINT_RANGE,"0,128,1"), "set_audio_track", "get_audio_track") ; - ADD_PROPERTY( PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE,"VideoStream"), "set_stream", "get_stream") ; + ADD_PROPERTY(PropertyInfo(Variant::INT, "audio_track", PROPERTY_HINT_RANGE, "0,128,1"), "set_audio_track", "get_audio_track"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "VideoStream"), "set_stream", "get_stream"); //ADD_PROPERTY( PropertyInfo(Variant::BOOL, "stream/loop"), "set_loop", "has_loop") ; - ADD_PROPERTY( PropertyInfo(Variant::REAL, "volume_db", PROPERTY_HINT_RANGE,"-80,24,0.01"), "set_volume_db", "get_volume_db") ; - ADD_PROPERTY( PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "has_autoplay") ; - ADD_PROPERTY( PropertyInfo(Variant::BOOL, "paused"), "set_paused", "is_paused") ; - ADD_PROPERTY( PropertyInfo( Variant::BOOL, "expand" ), "set_expand","has_expand") ; + ADD_PROPERTY(PropertyInfo(Variant::REAL, "volume_db", PROPERTY_HINT_RANGE, "-80,24,0.01"), "set_volume_db", "get_volume_db"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoplay"), "set_autoplay", "has_autoplay"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused"), "set_paused", "is_paused"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand"), "set_expand", "has_expand"); } - VideoPlayer::VideoPlayer() { - volume=1; + volume = 1; loops = false; paused = false; autoplay = false; expand = true; loops = false; - audio_track=0; - - buffering_ms=500; - server_mix_rate=44100; + audio_track = 0; -// internal_stream.player=this; -// stream_rid=AudioServer::get_singleton()->audio_stream_create(&internal_stream); - last_audio_time=0; + buffering_ms = 500; + server_mix_rate = 44100; + // internal_stream.player=this; + // stream_rid=AudioServer::get_singleton()->audio_stream_create(&internal_stream); + last_audio_time = 0; }; VideoPlayer::~VideoPlayer() { -// if (stream_rid.is_valid()) -// AudioServer::get_singleton()->free(stream_rid); + // if (stream_rid.is_valid()) + // AudioServer::get_singleton()->free(stream_rid); resampler.clear(); //Not necessary here, but make in consistent with other "stream_player" classes }; - diff --git a/scene/gui/video_player.h b/scene/gui/video_player.h index 168ea805b1..4f2894cdc2 100644 --- a/scene/gui/video_player.h +++ b/scene/gui/video_player.h @@ -29,15 +29,15 @@ #ifndef VIDEO_PLAYER_H #define VIDEO_PLAYER_H -#include "scene/resources/video_stream.h" #include "scene/gui/control.h" +#include "scene/resources/video_stream.h" #include "servers/audio/audio_rb_resampler.h" class VideoPlayer : public Control { - GDCLASS(VideoPlayer,Control); + GDCLASS(VideoPlayer, Control); -/* struct InternalStream : public AudioServer::AudioStream { + /* struct InternalStream : public AudioServer::AudioStream { VideoPlayer *player; virtual int get_channel_count() const; virtual void set_mix_rate(int p_rate); //notify the stream of the mix rate @@ -46,16 +46,15 @@ class VideoPlayer : public Control { }; */ -// InternalStream internal_stream; + // InternalStream internal_stream; Ref<VideoStreamPlayback> playback; Ref<VideoStream> stream; int sp_get_channel_count() const; void sp_set_mix_rate(int p_rate); //notify the stream of the mix rate - bool sp_mix(int32_t *p_buffer,int p_frames); + bool sp_mix(int32_t *p_buffer, int p_frames); void sp_update(); - RID stream_rid; Ref<ImageTexture> texture; @@ -70,24 +69,20 @@ class VideoPlayer : public Control { bool expand; bool loops; int buffering_ms; - int server_mix_rate; - int audio_track; - - static int _audio_mix_callback(void* p_udata,const int16_t *p_data,int p_frames); + int server_mix_rate; + int audio_track; + static int _audio_mix_callback(void *p_udata, const int16_t *p_data, int p_frames); protected: - static void _bind_methods(); void _notification(int p_notification); public: - Size2 get_minimum_size() const; void set_expand(bool p_expand); bool has_expand() const; - Ref<Texture> get_video_texture(); void set_stream(const Ref<VideoStream> &p_stream); diff --git a/scene/gui/viewport_container.cpp b/scene/gui/viewport_container.cpp index adc4a14a60..93789d945d 100644 --- a/scene/gui/viewport_container.cpp +++ b/scene/gui/viewport_container.cpp @@ -35,28 +35,25 @@ Size2 ViewportContainer::get_minimum_size() const { if (stretch) return Size2(); Size2 ms; - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { Viewport *c = get_child(i)->cast_to<Viewport>(); if (!c) continue; Size2 minsize = c->get_size(); - ms.width = MAX(ms.width , minsize.width); - ms.height = MAX(ms.height , minsize.height); + ms.width = MAX(ms.width, minsize.width); + ms.height = MAX(ms.height, minsize.height); } return ms; - } - void ViewportContainer::set_stretch(bool p_enable) { - stretch=p_enable; + stretch = p_enable; queue_sort(); update(); - } bool ViewportContainer::is_stretch_enabled() const { @@ -64,16 +61,14 @@ bool ViewportContainer::is_stretch_enabled() const { return stretch; } - void ViewportContainer::_notification(int p_what) { - - if (p_what==NOTIFICATION_RESIZED) { + if (p_what == NOTIFICATION_RESIZED) { if (!stretch) return; - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { Viewport *c = get_child(i)->cast_to<Viewport>(); if (!c) @@ -83,50 +78,46 @@ void ViewportContainer::_notification(int p_what) { } } - if (p_what==NOTIFICATION_ENTER_TREE || p_what==NOTIFICATION_VISIBILITY_CHANGED) { + if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_VISIBILITY_CHANGED) { - for(int i=0;i<get_child_count();i++) { + for (int i = 0; i < get_child_count(); i++) { Viewport *c = get_child(i)->cast_to<Viewport>(); if (!c) continue; - if (is_visible_in_tree()) c->set_update_mode(Viewport::UPDATE_ALWAYS); else c->set_update_mode(Viewport::UPDATE_DISABLED); } - } - if (p_what==NOTIFICATION_DRAW) { - - for(int i=0;i<get_child_count();i++) { + if (p_what == NOTIFICATION_DRAW) { + for (int i = 0; i < get_child_count(); i++) { Viewport *c = get_child(i)->cast_to<Viewport>(); if (!c) continue; if (stretch) - draw_texture_rect(c->get_texture(),Rect2(Vector2(),get_size()*Size2(1,-1))); + draw_texture_rect(c->get_texture(), Rect2(Vector2(), get_size() * Size2(1, -1))); else - draw_texture_rect(c->get_texture(),Rect2(Vector2(),c->get_size()*Size2(1,-1))); + draw_texture_rect(c->get_texture(), Rect2(Vector2(), c->get_size() * Size2(1, -1))); } } - } void ViewportContainer::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_stretch","enable"),&ViewportContainer::set_stretch); - ClassDB::bind_method(D_METHOD("is_stretch_enabled"),&ViewportContainer::is_stretch_enabled); + ClassDB::bind_method(D_METHOD("set_stretch", "enable"), &ViewportContainer::set_stretch); + ClassDB::bind_method(D_METHOD("is_stretch_enabled"), &ViewportContainer::is_stretch_enabled); - ADD_PROPERTY( PropertyInfo(Variant::BOOL,"stretch"),"set_stretch","is_stretch_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stretch"), "set_stretch", "is_stretch_enabled"); } ViewportContainer::ViewportContainer() { - stretch=false; + stretch = false; } diff --git a/scene/gui/viewport_container.h b/scene/gui/viewport_container.h index bcd41fa4a8..96b352b1cc 100644 --- a/scene/gui/viewport_container.h +++ b/scene/gui/viewport_container.h @@ -33,15 +33,15 @@ class ViewportContainer : public Container { - GDCLASS( ViewportContainer, Container ); + GDCLASS(ViewportContainer, Container); bool stretch; -protected: +protected: void _notification(int p_what); static void _bind_methods(); -public: +public: void set_stretch(bool p_enable); bool is_stretch_enabled() const; |