summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGilles Roudiere <gilles.roudiere@gmail.com>2017-09-19 22:39:19 +0200
committerGilles Roudiere <gilles.roudiere@gmail.com>2017-09-22 09:23:07 +0200
commit92f062696adc5efee2355259f42f5462ec42a605 (patch)
tree371d7a264f07ae64cf110a7d70f2ca3edfd72e01
parentb8d7dd22e85941b09c7c76ea565f89d1994e6d3d (diff)
Implements set_margins_preset(...)
-rw-r--r--scene/gui/control.cpp113
-rw-r--r--scene/gui/control.h34
2 files changed, 135 insertions, 12 deletions
diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp
index 2c01a0e2d1..900496c7c9 100644
--- a/scene/gui/control.cpp
+++ b/scene/gui/control.cpp
@@ -1471,6 +1471,110 @@ void Control::set_anchors_preset(LayoutPreset p_preset, bool p_keep_margin) {
}
}
+void Control::set_margins_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode, int p_margin) {
+ if (!is_inside_tree())
+ return;
+
+ Point2 new_pos;
+ Size2 min_size = get_minimum_size();
+ Size2 new_size = get_size();
+ Size2 parent_size = get_parent_area_size();
+
+ // Width
+ switch (p_preset) {
+ case PRESET_TOP_WIDE:
+ case PRESET_BOTTOM_WIDE:
+ case PRESET_HCENTER_WIDE:
+ case PRESET_WIDE:
+ new_size.x = parent_size.x - 2 * p_margin;
+ break;
+ default:
+ if (p_resize_mode == PRESET_MODE_MINSIZE || p_resize_mode == PRESET_MODE_KEEP_HEIGHT) {
+ new_size.x = min_size.x;
+ }
+ break;
+ }
+
+ // Height
+ switch (p_preset) {
+ case PRESET_LEFT_WIDE:
+ case PRESET_RIGHT_WIDE:
+ case PRESET_VCENTER_WIDE:
+ case PRESET_WIDE:
+ new_size.y = parent_size.y - 2 * p_margin;
+ break;
+ default:
+ if (p_resize_mode == PRESET_MODE_MINSIZE || p_resize_mode == PRESET_MODE_KEEP_WIDTH) {
+ new_size.y = min_size.y;
+ }
+ break;
+ }
+
+ // x pos
+ switch (p_preset) {
+ case PRESET_TOP_LEFT:
+ case PRESET_BOTTOM_LEFT:
+ case PRESET_CENTER_LEFT:
+ case PRESET_TOP_WIDE:
+ case PRESET_BOTTOM_WIDE:
+ case PRESET_LEFT_WIDE:
+ case PRESET_HCENTER_WIDE:
+ case PRESET_WIDE:
+ new_pos.x = p_margin;
+ break;
+
+ case PRESET_CENTER_TOP:
+ case PRESET_CENTER_BOTTOM:
+ case PRESET_CENTER:
+ case PRESET_VCENTER_WIDE:
+ new_pos.x = (parent_size.x - new_size.x) / 2.0;
+ break;
+
+ case PRESET_TOP_RIGHT:
+ case PRESET_BOTTOM_RIGHT:
+ case PRESET_CENTER_RIGHT:
+ case PRESET_RIGHT_WIDE:
+ new_pos.x = parent_size.x - new_size.x - p_margin;
+ break;
+ }
+
+ // y pos
+ switch (p_preset) {
+ case PRESET_TOP_LEFT:
+ case PRESET_TOP_RIGHT:
+ case PRESET_CENTER_TOP:
+ case PRESET_LEFT_WIDE:
+ case PRESET_RIGHT_WIDE:
+ case PRESET_TOP_WIDE:
+ case PRESET_VCENTER_WIDE:
+ case PRESET_WIDE:
+ new_pos.y = p_margin;
+ break;
+
+ case PRESET_CENTER_LEFT:
+ case PRESET_CENTER_RIGHT:
+ case PRESET_CENTER:
+ case PRESET_HCENTER_WIDE:
+ new_pos.y = (parent_size.y - new_size.y) / 2.0;
+ break;
+
+ case PRESET_BOTTOM_LEFT:
+ case PRESET_BOTTOM_RIGHT:
+ case PRESET_CENTER_BOTTOM:
+ case PRESET_BOTTOM_WIDE:
+ new_pos.y = parent_size.y - new_size.y - p_margin;
+ break;
+ }
+
+ set_position(new_pos);
+ set_size(new_size);
+}
+
+void Control::set_anchors_and_margins_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode, int p_margin) {
+ set_anchors_preset(p_preset);
+ set_margins_preset(p_preset, p_resize_mode, p_margin);
+}
+
float Control::get_anchor(Margin p_margin) const {
return data.anchor[p_margin];
@@ -2471,9 +2575,11 @@ void Control::_bind_methods() {
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_anchors_preset", "preset", "keep_margin"), &Control::set_anchors_preset, DEFVAL(false));
+ ClassDB::bind_method(D_METHOD("set_margins_preset", "preset", "resize_mode", "margin"), &Control::set_margins_preset, DEFVAL(PRESET_MODE_MINSIZE), DEFVAL(0));
+ ClassDB::bind_method(D_METHOD("set_anchors_and_margins_preset", "preset", "resize_mode", "margin"), &Control::set_anchors_and_margins_preset, DEFVAL(PRESET_MODE_MINSIZE), DEFVAL(0));
ClassDB::bind_method(D_METHOD("set_anchor", "margin", "anchor", "keep_margin", "push_opposite_anchor"), &Control::set_anchor, DEFVAL(false), DEFVAL(true));
ClassDB::bind_method(D_METHOD("_set_anchor", "margin", "anchor"), &Control::_set_anchor);
- ClassDB::bind_method(D_METHOD("set_anchors_preset", "preset", "keep_margin"), &Control::set_anchors_preset, DEFVAL(false));
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", "offset", "push_opposite_anchor"), &Control::set_anchor_and_margin, DEFVAL(false));
@@ -2689,6 +2795,11 @@ void Control::_bind_methods() {
BIND_ENUM_CONSTANT(PRESET_HCENTER_WIDE);
BIND_ENUM_CONSTANT(PRESET_WIDE);
+ BIND_ENUM_CONSTANT(PRESET_MODE_MINSIZE);
+ BIND_ENUM_CONSTANT(PRESET_MODE_KEEP_HEIGHT);
+ BIND_ENUM_CONSTANT(PRESET_MODE_KEEP_WIDTH);
+ BIND_ENUM_CONSTANT(PRESET_MODE_KEEP_SIZE);
+
BIND_ENUM_CONSTANT(SIZE_EXPAND);
BIND_ENUM_CONSTANT(SIZE_FILL);
BIND_ENUM_CONSTANT(SIZE_EXPAND_FILL);
diff --git a/scene/gui/control.h b/scene/gui/control.h
index da5c4d0908..f757a537e5 100644
--- a/scene/gui/control.h
+++ b/scene/gui/control.h
@@ -124,6 +124,13 @@ public:
PRESET_WIDE
};
+ enum LayoutPresetMode {
+ PRESET_MODE_MINSIZE,
+ PRESET_MODE_KEEP_WIDTH,
+ PRESET_MODE_KEEP_HEIGHT,
+ PRESET_MODE_KEEP_SIZE
+ };
+
private:
struct CComparator {
@@ -294,34 +301,32 @@ public:
/* POSITIONING */
- void set_anchor(Margin p_margin, float p_anchor, bool p_keep_margin = false, bool p_push_opposite_anchor = true);
- void set_anchor_and_margin(Margin p_margin, float p_anchor, float p_pos, bool p_push_opposite_anchor = true);
void set_anchors_preset(LayoutPreset p_preset, bool p_keep_margin = false);
+ void set_margins_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode = PRESET_MODE_MINSIZE, int p_margin = 0);
+ void set_anchors_and_margins_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode = PRESET_MODE_MINSIZE, int p_margin = 0);
+ void set_anchor(Margin p_margin, float p_anchor, bool p_keep_margin = false, bool p_push_opposite_anchor = true);
float get_anchor(Margin p_margin) const;
void set_margin(Margin p_margin, float p_value);
+ float get_margin(Margin p_margin) const;
+
+ void set_anchor_and_margin(Margin p_margin, float p_anchor, float p_pos, bool p_push_opposite_anchor = true);
void set_begin(const Point2 &p_point); // helper
void set_end(const Point2 &p_point); // helper
- void set_h_grow_direction(GrowDirection p_direction);
- GrowDirection get_h_grow_direction() const;
-
- void set_v_grow_direction(GrowDirection p_direction);
- GrowDirection get_v_grow_direction() const;
-
- float get_margin(Margin p_margin) const;
Point2 get_begin() const;
Point2 get_end() const;
void set_position(const Point2 &p_point);
- void set_size(const Size2 &p_size);
void set_global_position(const Point2 &p_point);
-
Point2 get_position() const;
Point2 get_global_position() const;
+
+ void set_size(const Size2 &p_size);
Size2 get_size() const;
+
Rect2 get_rect() const;
Rect2 get_global_rect() const;
Rect2 get_window_rect() const; ///< use with care, as it blocks waiting for the visual server
@@ -331,6 +336,12 @@ public:
float get_rotation() const;
float get_rotation_deg() const;
+ void set_h_grow_direction(GrowDirection p_direction);
+ GrowDirection get_h_grow_direction() const;
+
+ void set_v_grow_direction(GrowDirection p_direction);
+ GrowDirection get_v_grow_direction() const;
+
void set_pivot_offset(const Vector2 &p_pivot);
Vector2 get_pivot_offset() const;
@@ -449,6 +460,7 @@ VARIANT_ENUM_CAST(Control::FocusMode);
VARIANT_ENUM_CAST(Control::SizeFlags);
VARIANT_ENUM_CAST(Control::CursorShape);
VARIANT_ENUM_CAST(Control::LayoutPreset);
+VARIANT_ENUM_CAST(Control::LayoutPresetMode);
VARIANT_ENUM_CAST(Control::MouseFilter);
VARIANT_ENUM_CAST(Control::GrowDirection);
VARIANT_ENUM_CAST(Control::Anchor);