From b0aab53853e2add8ae76bede06ba961e36ce263c Mon Sep 17 00:00:00 2001 From: Saracen Date: Sun, 20 Mar 2016 05:25:05 +0000 Subject: [PATCH] Dynamic property list for control margins allowing floating point properties to be used with ratio anchors. (cherry picked from commit d8392dd1176f5d133a77304ad591fe9cb311c3ea) --- scene/gui/control.cpp | 88 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 80 insertions(+), 8 deletions(-) diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index 380ea5686f5..06314057847 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -104,8 +104,32 @@ bool Control::_set(const StringName& p_name, const Variant& p_value) { String name= p_name; - if (!name.begins_with("custom")) - return false; + if (!name.begins_with("custom")) { + if (name.begins_with("margin/")) { + String dname = name.get_slicec('/', 1); + if (dname == "left") { + set_margin(MARGIN_LEFT, p_value); + return true; + } + else if (dname == "top") { + set_margin(MARGIN_TOP, p_value); + return true; + } + else if (dname == "right") { + set_margin(MARGIN_RIGHT, p_value); + return true; + } + else if (dname == "bottom") { + set_margin(MARGIN_BOTTOM, p_value); + return true; + } + else { + return false; + } + } else { + return false; + } + } if (p_value.get_type()==Variant::NIL) { @@ -197,7 +221,30 @@ bool Control::_get(const StringName& p_name,Variant &r_ret) const { String sname=p_name; if (!sname.begins_with("custom")) - return false; + if (sname.begins_with("margin/")) { + String dname = sname.get_slicec('/', 1); + if (dname == "left") { + r_ret = get_margin(MARGIN_LEFT); + return true; + } + else if (dname == "top") { + r_ret = get_margin(MARGIN_TOP); + return true; + } + else if (dname == "right") { + r_ret = get_margin(MARGIN_RIGHT); + return true; + } + else if (dname == "bottom") { + r_ret = get_margin(MARGIN_BOTTOM); + return true; + } + else { + return false; + } + } else { + return false; + } if (sname.begins_with("custom_icons/")) { String name = sname.get_slicec('/',1); @@ -233,6 +280,36 @@ bool Control::_get(const StringName& p_name,Variant &r_ret) const { } void Control::_get_property_list( List *p_list) const { + { + if (get_anchor(MARGIN_LEFT) == ANCHOR_RATIO) { + p_list->push_back(PropertyInfo(Variant::REAL, "margin/left", PROPERTY_HINT_RANGE, "-4096,4096,0.001")); + } + else { + p_list->push_back(PropertyInfo(Variant::INT, "margin/left", PROPERTY_HINT_RANGE, "-4096,4096")); + } + + if (get_anchor(MARGIN_TOP) == ANCHOR_RATIO) { + p_list->push_back(PropertyInfo(Variant::REAL, "margin/top", PROPERTY_HINT_RANGE, "-4096,4096,0.001")); + } + else { + p_list->push_back(PropertyInfo(Variant::INT, "margin/top", PROPERTY_HINT_RANGE, "-4096,4096")); + } + + if (get_anchor(MARGIN_RIGHT) == ANCHOR_RATIO) { + p_list->push_back(PropertyInfo(Variant::REAL, "margin/right", PROPERTY_HINT_RANGE, "-4096,4096,0.001")); + } + else { + p_list->push_back(PropertyInfo(Variant::INT, "margin/right", PROPERTY_HINT_RANGE, "-4096,4096")); + } + + if (get_anchor(MARGIN_BOTTOM) == ANCHOR_RATIO) { + p_list->push_back(PropertyInfo(Variant::REAL, "margin/bottom", PROPERTY_HINT_RANGE, "-4096,4096,0.001")); + } + else { + p_list->push_back(PropertyInfo(Variant::INT, "margin/bottom", PROPERTY_HINT_RANGE, "-4096,4096")); + } + } + Ref theme; if (data.theme.is_valid()) { @@ -2195,11 +2272,6 @@ void Control::_bind_methods() { 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 ); - ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin/right", PROPERTY_HINT_RANGE, "-4096,4096"), _SCS("set_margin"),_SCS("get_margin"), MARGIN_RIGHT ); - ADD_PROPERTYINZ( PropertyInfo(Variant::INT,"margin/bottom", PROPERTY_HINT_RANGE, "-4096,4096"), _SCS("set_margin"),_SCS("get_margin"), MARGIN_BOTTOM ); - ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"rect/pos", PROPERTY_HINT_NONE, "",PROPERTY_USAGE_EDITOR), _SCS("set_pos"),_SCS("get_pos") ); ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"rect/size", PROPERTY_HINT_NONE, "",PROPERTY_USAGE_EDITOR), _SCS("set_size"),_SCS("get_size") ); ADD_PROPERTYNZ( PropertyInfo(Variant::VECTOR2,"rect/min_size"), _SCS("set_custom_minimum_size"),_SCS("get_custom_minimum_size") );