You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-11 13:10:58 +00:00
Make anchors snap to each other
This commit is contained in:
@@ -775,25 +775,30 @@ CanvasItemEditor::DragType CanvasItemEditor::_get_resize_handle_drag_type(const
|
|||||||
return DRAG_NONE;
|
return DRAG_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
float CanvasItemEditor::_anchor_snap(const float anchor, bool *snapped) {
|
float CanvasItemEditor::_anchor_snap(float p_anchor, bool *p_snapped, float p_opposite_anchor) {
|
||||||
float result = anchor;
|
bool snapped = false;
|
||||||
if (snapped)
|
float dist, dist_min = 0.0;
|
||||||
*snapped = false;
|
|
||||||
float radius = 0.05 / zoom;
|
float radius = 0.05 / zoom;
|
||||||
if (fabs(result - ANCHOR_BEGIN) < radius) {
|
float basic_anchors[3] = { ANCHOR_BEGIN, ANCHOR_CENTER, ANCHOR_END };
|
||||||
result = ANCHOR_BEGIN;
|
for (int i = 0; i < 3; i++) {
|
||||||
if (snapped)
|
if ((dist = fabs(p_anchor - basic_anchors[i])) < radius) {
|
||||||
*snapped = true;
|
if (!snapped || dist <= dist_min) {
|
||||||
} else if (fabs(result - ANCHOR_CENTER) < radius) {
|
p_anchor = basic_anchors[i];
|
||||||
result = ANCHOR_CENTER;
|
dist_min = dist;
|
||||||
if (snapped)
|
snapped = true;
|
||||||
*snapped = true;
|
}
|
||||||
} else if (fabs(result - ANCHOR_END) < radius) {
|
}
|
||||||
result = ANCHOR_END;
|
|
||||||
if (snapped)
|
|
||||||
*snapped = true;
|
|
||||||
}
|
}
|
||||||
return result;
|
if (p_opposite_anchor >= 0 && (dist = fabs(p_anchor - p_opposite_anchor)) < radius) {
|
||||||
|
if (!snapped || dist <= dist_min) {
|
||||||
|
p_anchor = p_opposite_anchor;
|
||||||
|
dist_min = dist;
|
||||||
|
snapped = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (p_snapped)
|
||||||
|
*p_snapped = snapped;
|
||||||
|
return p_anchor;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector2 CanvasItemEditor::_anchor_to_position(Control *p_control, Vector2 anchor) {
|
Vector2 CanvasItemEditor::_anchor_to_position(Control *p_control, Vector2 anchor) {
|
||||||
@@ -1590,23 +1595,23 @@ void CanvasItemEditor::_viewport_gui_input(const Ref<InputEvent> &p_event) {
|
|||||||
|
|
||||||
switch (drag) {
|
switch (drag) {
|
||||||
case DRAG_ANCHOR_TOP_LEFT:
|
case DRAG_ANCHOR_TOP_LEFT:
|
||||||
control->set_anchor(MARGIN_LEFT, _anchor_snap(anchor.x));
|
control->set_anchor(MARGIN_LEFT, _anchor_snap(anchor.x, NULL, control->get_anchor(MARGIN_RIGHT)));
|
||||||
control->set_anchor(MARGIN_TOP, _anchor_snap(anchor.y));
|
control->set_anchor(MARGIN_TOP, _anchor_snap(anchor.y, NULL, control->get_anchor(MARGIN_BOTTOM)));
|
||||||
continue;
|
continue;
|
||||||
break;
|
break;
|
||||||
case DRAG_ANCHOR_TOP_RIGHT:
|
case DRAG_ANCHOR_TOP_RIGHT:
|
||||||
control->set_anchor(MARGIN_RIGHT, _anchor_snap(anchor.x));
|
control->set_anchor(MARGIN_RIGHT, _anchor_snap(anchor.x, NULL, control->get_anchor(MARGIN_LEFT)));
|
||||||
control->set_anchor(MARGIN_TOP, _anchor_snap(anchor.y));
|
control->set_anchor(MARGIN_TOP, _anchor_snap(anchor.y, NULL, control->get_anchor(MARGIN_BOTTOM)));
|
||||||
continue;
|
continue;
|
||||||
break;
|
break;
|
||||||
case DRAG_ANCHOR_BOTTOM_RIGHT:
|
case DRAG_ANCHOR_BOTTOM_RIGHT:
|
||||||
control->set_anchor(MARGIN_RIGHT, _anchor_snap(anchor.x));
|
control->set_anchor(MARGIN_RIGHT, _anchor_snap(anchor.x, NULL, control->get_anchor(MARGIN_LEFT)));
|
||||||
control->set_anchor(MARGIN_BOTTOM, _anchor_snap(anchor.y));
|
control->set_anchor(MARGIN_BOTTOM, _anchor_snap(anchor.y, NULL, control->get_anchor(MARGIN_TOP)));
|
||||||
continue;
|
continue;
|
||||||
break;
|
break;
|
||||||
case DRAG_ANCHOR_BOTTOM_LEFT:
|
case DRAG_ANCHOR_BOTTOM_LEFT:
|
||||||
control->set_anchor(MARGIN_LEFT, _anchor_snap(anchor.x));
|
control->set_anchor(MARGIN_LEFT, _anchor_snap(anchor.x, NULL, control->get_anchor(MARGIN_RIGHT)));
|
||||||
control->set_anchor(MARGIN_BOTTOM, _anchor_snap(anchor.y));
|
control->set_anchor(MARGIN_BOTTOM, _anchor_snap(anchor.y, NULL, control->get_anchor(MARGIN_TOP)));
|
||||||
continue;
|
continue;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -334,7 +334,7 @@ class CanvasItemEditor : public VBoxContainer {
|
|||||||
void _prepare_drag(const Point2 &p_click_pos);
|
void _prepare_drag(const Point2 &p_click_pos);
|
||||||
DragType _get_anchor_handle_drag_type(const Point2 &p_click, Vector2 &r_point);
|
DragType _get_anchor_handle_drag_type(const Point2 &p_click, Vector2 &r_point);
|
||||||
|
|
||||||
float _anchor_snap(const float anchor, bool *snapped = NULL);
|
float _anchor_snap(float anchor, bool *snapped = NULL, float p_opposite_anchor = -1);
|
||||||
Vector2 _anchor_to_position(Control *p_control, Vector2 anchor);
|
Vector2 _anchor_to_position(Control *p_control, Vector2 anchor);
|
||||||
Vector2 _position_to_anchor(Control *p_control, Vector2 position);
|
Vector2 _position_to_anchor(Control *p_control, Vector2 position);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user