Add both X and Y scale in the scale editor

This commit is contained in:
groud 2018-09-20 15:22:35 +02:00
parent 394e5de6bb
commit bad5ec61b1
2 changed files with 46 additions and 19 deletions

View file

@ -1654,6 +1654,8 @@ bool CanvasItemEditor::_gui_input_scale(const Ref<InputEvent> &p_event) {
Transform2D unscaled_transform = (xform * canvas_item->get_transform().affine_inverse() * Transform2D(canvas_item->_edit_get_rotation(), canvas_item->_edit_get_position())).orthonormalized(); Transform2D unscaled_transform = (xform * canvas_item->get_transform().affine_inverse() * Transform2D(canvas_item->_edit_get_rotation(), canvas_item->_edit_get_position())).orthonormalized();
Transform2D simple_xform = viewport->get_transform() * unscaled_transform; Transform2D simple_xform = viewport->get_transform() * unscaled_transform;
drag_type = DRAG_SCALE_BOTH;
Size2 scale_factor = Size2(SCALE_HANDLE_DISTANCE, SCALE_HANDLE_DISTANCE); Size2 scale_factor = Size2(SCALE_HANDLE_DISTANCE, SCALE_HANDLE_DISTANCE);
Rect2 x_handle_rect = Rect2(scale_factor.x * EDSCALE, -5 * EDSCALE, 10 * EDSCALE, 10 * EDSCALE); Rect2 x_handle_rect = Rect2(scale_factor.x * EDSCALE, -5 * EDSCALE, 10 * EDSCALE, 10 * EDSCALE);
if (x_handle_rect.has_point(simple_xform.affine_inverse().xform(b->get_position()))) { if (x_handle_rect.has_point(simple_xform.affine_inverse().xform(b->get_position()))) {
@ -1663,7 +1665,7 @@ bool CanvasItemEditor::_gui_input_scale(const Ref<InputEvent> &p_event) {
if (y_handle_rect.has_point(simple_xform.affine_inverse().xform(b->get_position()))) { if (y_handle_rect.has_point(simple_xform.affine_inverse().xform(b->get_position()))) {
drag_type = DRAG_SCALE_Y; drag_type = DRAG_SCALE_Y;
} }
if (drag_type == DRAG_SCALE_X || drag_type == DRAG_SCALE_Y) {
drag_from = transform.affine_inverse().xform(b->get_position()); drag_from = transform.affine_inverse().xform(b->get_position());
drag_selection = List<CanvasItem *>(); drag_selection = List<CanvasItem *>();
drag_selection.push_back(canvas_item); drag_selection.push_back(canvas_item);
@ -1672,9 +1674,8 @@ bool CanvasItemEditor::_gui_input_scale(const Ref<InputEvent> &p_event) {
} }
} }
} }
}
if (drag_type == DRAG_SCALE_X || drag_type == DRAG_SCALE_Y) { if (drag_type == DRAG_SCALE_BOTH || drag_type == DRAG_SCALE_X || drag_type == DRAG_SCALE_Y) {
// Resize the node // Resize the node
if (m.is_valid()) { if (m.is_valid()) {
_restore_canvas_item_state(drag_selection); _restore_canvas_item_state(drag_selection);
@ -1682,25 +1683,50 @@ bool CanvasItemEditor::_gui_input_scale(const Ref<InputEvent> &p_event) {
drag_to = transform.affine_inverse().xform(m->get_position()); drag_to = transform.affine_inverse().xform(m->get_position());
Transform2D parent_xform = canvas_item->get_global_transform_with_canvas() * canvas_item->get_transform().affine_inverse();
Transform2D unscaled_transform = (transform * parent_xform * Transform2D(canvas_item->_edit_get_rotation(), canvas_item->_edit_get_position())).orthonormalized();
Transform2D simple_xform = (viewport->get_transform() * unscaled_transform).affine_inverse() * transform;
bool uniform = m->get_shift(); bool uniform = m->get_shift();
Point2 offset = drag_to - drag_from;
Point2 drag_from_local = simple_xform.xform(drag_from);
Point2 drag_to_local = simple_xform.xform(drag_to);
Point2 offset = drag_to_local - drag_from_local;
Size2 scale = canvas_item->call("get_scale"); Size2 scale = canvas_item->call("get_scale");
float ratio = scale.y / scale.x; float ratio = scale.y / scale.x;
if (drag_type == DRAG_SCALE_BOTH) {
Size2 scale_factor = drag_to_local / drag_from_local;
if (uniform) {
if (ABS(offset.x) > ABS(offset.y)) {
scale.x *= scale_factor.x;
scale.y = scale.x * ratio;
} else {
scale.y *= scale_factor.y;
scale.x = scale.y / ratio;
}
} else {
scale *= scale_factor;
}
} else {
Size2 scale_factor = Vector2(offset.x, -offset.y) / SCALE_HANDLE_DISTANCE;
Size2 parent_scale = parent_xform.get_scale();
scale_factor *= Vector2(1.0 / parent_scale.x, 1.0 / parent_scale.y);
if (drag_type == DRAG_SCALE_X) { if (drag_type == DRAG_SCALE_X) {
scale.x += offset.x / SCALE_HANDLE_DISTANCE; scale.x += scale_factor.x;
if (uniform) { if (uniform) {
scale.y = scale.x * ratio; scale.y = scale.x * ratio;
} }
canvas_item->call("set_scale", scale);
} else if (drag_type == DRAG_SCALE_Y) { } else if (drag_type == DRAG_SCALE_Y) {
scale.y -= offset.y / SCALE_HANDLE_DISTANCE; scale.y += scale_factor.y;
if (uniform) { if (uniform) {
scale.x = scale.y / ratio; scale.x = scale.y / ratio;
} }
canvas_item->call("set_scale", scale);
} }
} }
canvas_item->call("set_scale", scale);
return true;
}
// Confirm resize // Confirm resize
if (b.is_valid() && b->get_button_index() == BUTTON_LEFT && !b->is_pressed()) { if (b.is_valid() && b->get_button_index() == BUTTON_LEFT && !b->is_pressed()) {

View file

@ -195,6 +195,7 @@ private:
DRAG_MOVE, DRAG_MOVE,
DRAG_SCALE_X, DRAG_SCALE_X,
DRAG_SCALE_Y, DRAG_SCALE_Y,
DRAG_SCALE_BOTH,
DRAG_ROTATE, DRAG_ROTATE,
DRAG_PIVOT, DRAG_PIVOT,
DRAG_V_GUIDE, DRAG_V_GUIDE,