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
This commit is contained in:
Bojidar Marinov 2016-03-09 18:59:35 +02:00
parent e46e43d2aa
commit 0e8a8d2cb1
3 changed files with 32 additions and 13 deletions

View file

@ -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">

View file

@ -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()
{
}

View file

@ -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;