Skip to content

Commit

Permalink
Merge pull request #15610 from groud/fix_node_resize_setting_anchors
Browse files Browse the repository at this point in the history
Fixes nodes being resized or moved when changing an anchor
  • Loading branch information
akien-mga authored Jan 11, 2018
2 parents e3f02b1 + 00473e0 commit c1c17b0
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions scene/gui/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1357,24 +1357,32 @@ float Control::_a2s(float p_val, float p_anchor, float p_range) const {
}

void Control::set_anchor(Margin p_margin, float p_anchor, bool p_keep_margin, bool p_push_opposite_anchor) {
bool pushed = false;
float parent_range = _get_parent_range((p_margin == MARGIN_LEFT || p_margin == MARGIN_RIGHT) ? 0 : 1);
float previous_margin_pos = data.margin[p_margin] + data.anchor[p_margin] * parent_range;
float previous_opposite_margin_pos = data.margin[(p_margin + 2) % 4] + data.anchor[(p_margin + 2) % 4] * parent_range;

data.anchor[p_margin] = CLAMP(p_anchor, 0.0, 1.0);

if (((p_margin == MARGIN_LEFT || p_margin == MARGIN_TOP) && data.anchor[p_margin] > data.anchor[(p_margin + 2) % 4]) ||
((p_margin == MARGIN_RIGHT || p_margin == MARGIN_BOTTOM) && data.anchor[p_margin] < data.anchor[(p_margin + 2) % 4])) {
if (p_push_opposite_anchor) {
data.anchor[(p_margin + 2) % 4] = data.anchor[p_margin];
pushed = true;
} else {
data.anchor[p_margin] = data.anchor[(p_margin + 2) % 4];
}
}

if (is_inside_tree()) {
if (p_keep_margin) {
_size_changed();
if (!p_keep_margin) {
data.margin[p_margin] = _s2a(previous_margin_pos, data.anchor[p_margin], parent_range);
if (p_push_opposite_anchor) {
data.margin[(p_margin + 2) % 4] = _s2a(previous_opposite_margin_pos, data.anchor[(p_margin + 2) % 4], parent_range);
}
}

if (is_inside_tree()) {
_size_changed();
}

update();
_change_notify();
}
Expand Down

0 comments on commit c1c17b0

Please sign in to comment.