diff options
author | Bojidar Marinov <bojidar.marinov.bg@gmail.com> | 2016-03-09 18:59:35 +0200 |
---|---|---|
committer | Bojidar Marinov <bojidar.marinov.bg@gmail.com> | 2016-03-09 21:14:17 +0200 |
commit | 0e8a8d2cb116f06ecd5d420b22b158b917fc7626 (patch) | |
tree | 3ea952147ca6add333b6bcca4b1b5e51fd885c7e | |
parent | e46e43d2aaa6339a1675eb989f41885e745bf5c3 (diff) |
Add option to keep margins when changing anchors, closes #3979
Amend: Fixed an issue for non-tool builds
Amend2: Same, just fixed doing nothing at some times
-rw-r--r-- | doc/base/classes.xml | 6 | ||||
-rw-r--r-- | scene/gui/control.cpp | 35 | ||||
-rw-r--r-- | scene/gui/control.h | 4 |
3 files changed, 32 insertions, 13 deletions
diff --git a/doc/base/classes.xml b/doc/base/classes.xml index d111569a60..04e08f166e 100644 --- a/doc/base/classes.xml +++ b/doc/base/classes.xml @@ -8385,8 +8385,12 @@ </argument> <argument index="1" name="anchor_mode" type="int"> </argument> + <argument index="2" name="keep_margin" type="bool" default="false"> + </argument> <description> - Change the anchor (ANCHOR_BEGIN, ANCHOR_END, ANCHOR_RATIO) type for a margin (MARGIN_LEFT, MARGIN_TOP, MARGIN_RIGHT, MARGIN_BOTTOM). Changing the anchor mode converts the current margin offset from the previous anchor mode to the new one, so margin offsets ([method set_margin]) must be done after setting anchors, or at the same time ([method set_anchor_and_margin]). + Change the anchor (ANCHOR_BEGIN, ANCHOR_END, ANCHOR_RATIO) type for a margin (MARGIN_LEFT, MARGIN_TOP, MARGIN_RIGHT, MARGIN_BOTTOM). Changing the anchor mode converts the current margin offset from the previous anchor mode to the new one, so margin offsets ([method set_margin]) must be done after setting anchors, or at the same time ([method set_anchor_and_margin]) + + Additionally, [code]keep_margin[/code] controls whether margins should be left the same, or changed to keep the same position and size on-screen. </description> </method> <method name="get_anchor" qualifiers="const"> diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index 295ec03702..0364f73275 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -39,6 +39,9 @@ #include "scene/scene_string_names.h" #include "scene/gui/panel.h" #include "scene/gui/label.h" +#ifdef TOOLS_ENABLED +#include "tools/editor/editor_settings.h" +#endif #include <stdio.h> @@ -1153,20 +1156,31 @@ float Control::_a2s(float p_val, AnchorType p_anchor,float p_range) const { } -void Control::set_anchor(Margin p_margin,AnchorType p_anchor) { +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 { + data.anchor[p_margin] = p_anchor; + } else if(!p_keep_margin) { float pr = _get_parent_range(p_margin); float s = _a2s( data.margin[p_margin], data.anchor[p_margin], pr ); - data.anchor[p_margin]=p_anchor; + data.anchor[p_margin] = p_anchor; data.margin[p_margin] = _s2a( s, p_anchor, pr ); + } else { + data.anchor[p_margin] = p_anchor; + _size_changed(); } _change_notify(); } +void Control::_set_anchor(Margin p_margin,AnchorType p_anchor) { + #ifdef TOOLS_ENABLED + set_anchor(p_margin, p_anchor, EDITOR_DEF("2d_editor/keep_margins_when_changing_anchors", false)); + #else + set_anchor(p_margin, p_anchor); + #endif +} + void Control::set_anchor_and_margin(Margin p_margin,AnchorType p_anchor, float p_pos) { set_anchor(p_margin,p_anchor); @@ -2090,7 +2104,8 @@ void Control::_bind_methods() { ObjectTypeDB::bind_method(_MD("accept_event"),&Control::accept_event); ObjectTypeDB::bind_method(_MD("get_minimum_size"),&Control::get_minimum_size); ObjectTypeDB::bind_method(_MD("get_combined_minimum_size"),&Control::get_combined_minimum_size); - ObjectTypeDB::bind_method(_MD("set_anchor","margin","anchor_mode"),&Control::set_anchor); + ObjectTypeDB::bind_method(_MD("set_anchor","margin","anchor_mode","keep_margin"),&Control::set_anchor,DEFVAL(false)); + ObjectTypeDB::bind_method(_MD("_set_anchor","margin","anchor_mode"),&Control::_set_anchor); ObjectTypeDB::bind_method(_MD("get_anchor","margin"),&Control::get_anchor); ObjectTypeDB::bind_method(_MD("set_margin","margin","offset"),&Control::set_margin); ObjectTypeDB::bind_method(_MD("set_anchor_and_margin","margin","anchor_mode","offset"),&Control::set_anchor_and_margin); @@ -2184,10 +2199,10 @@ void Control::_bind_methods() { 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_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/left", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("set_anchor"),_SCS("get_anchor"), MARGIN_LEFT ); - ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/top", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("set_anchor"),_SCS("get_anchor"), MARGIN_TOP ); - ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/right", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("set_anchor"),_SCS("get_anchor"), MARGIN_RIGHT ); - ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/bottom", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("set_anchor"),_SCS("get_anchor"), MARGIN_BOTTOM ); + ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/left", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("_set_anchor"),_SCS("get_anchor"), MARGIN_LEFT ); + ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/top", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("_set_anchor"),_SCS("get_anchor"), MARGIN_TOP ); + ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/right", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("_set_anchor"),_SCS("get_anchor"), MARGIN_RIGHT ); + ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"anchor/bottom", PROPERTY_HINT_ENUM, "Begin,End,Ratio,Center"), _SCS("_set_anchor"),_SCS("get_anchor"), MARGIN_BOTTOM ); ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin/left", PROPERTY_HINT_RANGE, "-4096,4096"), _SCS("set_margin"),_SCS("get_margin"), MARGIN_LEFT ); ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin/top", PROPERTY_HINT_RANGE, "-4096,4096"), _SCS("set_margin"),_SCS("get_margin"), MARGIN_TOP ); @@ -2304,5 +2319,3 @@ Control::Control() { Control::~Control() { } - - diff --git a/scene/gui/control.h b/scene/gui/control.h index 851d8316fb..1e2db7a575 100644 --- a/scene/gui/control.h +++ b/scene/gui/control.h @@ -162,6 +162,8 @@ private: Control *_get_focus_neighbour(Margin p_margin,int p_count=0); + 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; @@ -244,7 +246,7 @@ public: /* POSITIONING */ - void set_anchor(Margin p_margin,AnchorType p_anchor); + 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; |