Fix arrow keys to move a rotated selection (fix #775)

This commit is contained in:
David Capello 2016-05-18 14:26:25 -03:00
parent 591b3b19ac
commit b21f5df2d6
10 changed files with 82 additions and 72 deletions

View File

@ -1,5 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2001-2015 David Capello // Copyright (C) 2001-2016 David Capello
// //
// This program is free software; you can redistribute it and/or modify // This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as // it under the terms of the GNU General Public License version 2 as
@ -264,7 +264,7 @@ void Document::setTransformation(const gfx::Transformation& transform)
void Document::resetTransformation() void Document::resetTransformation()
{ {
if (m_mask) if (m_mask)
m_transformation = gfx::Transformation(m_mask->bounds()); m_transformation = gfx::Transformation(gfx::RectF(m_mask->bounds()));
else else
m_transformation = gfx::Transformation(); m_transformation = gfx::Transformation();
} }

View File

@ -364,7 +364,8 @@ public:
m_mask.unfreeze(); m_mask.unfreeze();
loop->setMask(&m_mask); loop->setMask(&m_mask);
loop->getDocument()->setTransformation(Transformation(m_mask.bounds())); loop->getDocument()->setTransformation(
Transformation(RectF(m_mask.bounds())));
m_mask.clear(); m_mask.clear();
} }

View File

@ -1,5 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2001-2015 David Capello // Copyright (C) 2001-2016 David Capello
// //
// This program is free software; you can redistribute it and/or modify // This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as // it under the terms of the GNU General Public License version 2 as
@ -54,7 +54,7 @@ void set_pivot_from_preferences(gfx::Transformation& t)
break; break;
} }
t.displacePivotTo(gfx::Point(pivotPos)); t.displacePivotTo(gfx::PointF(pivotPos));
} }
} // namespace app } // namespace app

View File

@ -235,7 +235,7 @@ void PixelsMovement::moveImage(const gfx::Point& pos, MoveModifier moveModifier)
m_currentData.transformBox(oldCorners); m_currentData.transformBox(oldCorners);
ContextWriter writer(m_reader, 1000); ContextWriter writer(m_reader, 1000);
gfx::Rect bounds = m_initialData.bounds(); gfx::RectF bounds = m_initialData.bounds();
bool updateBounds = false; bool updateBounds = false;
double dx, dy; double dx, dy;
@ -254,16 +254,18 @@ void PixelsMovement::moveImage(const gfx::Point& pos, MoveModifier moveModifier)
dy = 0.0; dy = 0.0;
} }
bounds.offset(int(dx), int(dy)); bounds.offset(dx, dy);
updateBounds = true; updateBounds = true;
if ((moveModifier & SnapToGridMovement) == SnapToGridMovement) { if ((moveModifier & SnapToGridMovement) == SnapToGridMovement) {
// Snap the x1,y1 point to the grid. // Snap the x1,y1 point to the grid.
gfx::Rect gridBounds = App::instance() gfx::Rect gridBounds = App::instance()
->preferences().document(m_document).grid.bounds(); ->preferences().document(m_document).grid.bounds();
gfx::Point gridOffset(bounds.origin()); gfx::PointF gridOffset(
gridOffset = snap_to_grid(gridBounds, gridOffset, snap_to_grid(
PreferSnapTo::ClosestGridVertex); gridBounds,
gfx::Point(bounds.origin()),
PreferSnapTo::ClosestGridVertex));
// Now we calculate the difference from x1,y1 point and we can // Now we calculate the difference from x1,y1 point and we can
// use it to adjust all coordinates (x1, y1, x2, y2). // use it to adjust all coordinates (x1, y1, x2, y2).
@ -285,8 +287,8 @@ void PixelsMovement::moveImage(const gfx::Point& pos, MoveModifier moveModifier)
{ 0.0, 0.5 }, { 1.0, 0.5 }, { 0.0, 0.5 }, { 1.0, 0.5 },
{ 0.0, 1.0 }, { 0.5, 1.0 }, { 1.0, 1.0 } { 0.0, 1.0 }, { 0.5, 1.0 }, { 1.0, 1.0 }
}; };
gfx::PointT<double> pivot; gfx::PointF pivot;
gfx::PointT<double> handle( gfx::PointF handle(
handles[m_handle-ScaleNWHandle][0], handles[m_handle-ScaleNWHandle][0],
handles[m_handle-ScaleNWHandle][1]); handles[m_handle-ScaleNWHandle][1]);
@ -301,12 +303,12 @@ void PixelsMovement::moveImage(const gfx::Point& pos, MoveModifier moveModifier)
pivot.y = bounds.y + bounds.h*pivot.y; pivot.y = bounds.y + bounds.h*pivot.y;
} }
gfx::Point a = bounds.origin(); gfx::PointF a = bounds.origin();
gfx::Point b = bounds.point2(); gfx::PointF b = bounds.point2();
if ((moveModifier & MaintainAspectRatioMovement) == MaintainAspectRatioMovement) { if ((moveModifier & MaintainAspectRatioMovement) == MaintainAspectRatioMovement) {
auto u = point2Vector(gfx::PointT<double>(m_catchPos) - pivot); auto u = point2Vector(gfx::PointF(m_catchPos) - pivot);
auto v = point2Vector(gfx::PointT<double>(pos) - pivot); auto v = point2Vector(gfx::PointF(pos) - pivot);
auto w = v.projectOn(u); auto w = v.projectOn(u);
double scale = u.magnitude(); double scale = u.magnitude();
if (scale != 0.0) if (scale != 0.0)
@ -362,8 +364,8 @@ void PixelsMovement::moveImage(const gfx::Point& pos, MoveModifier moveModifier)
case RotateSHandle: case RotateSHandle:
case RotateSEHandle: case RotateSEHandle:
{ {
gfx::Point abs_initial_pivot = m_initialData.pivot(); gfx::PointF abs_initial_pivot = m_initialData.pivot();
gfx::Point abs_pivot = m_currentData.pivot(); gfx::PointF abs_pivot = m_currentData.pivot();
double newAngle = double newAngle =
m_initialData.angle() m_initialData.angle()
@ -405,8 +407,8 @@ void PixelsMovement::moveImage(const gfx::Point& pos, MoveModifier moveModifier)
case PivotHandle: case PivotHandle:
{ {
// Calculate the new position of the pivot // Calculate the new position of the pivot
gfx::Point newPivot(m_initialData.pivot().x + (pos.x - m_catchPos.x), gfx::PointF newPivot(m_initialData.pivot().x + (pos.x - m_catchPos.x),
m_initialData.pivot().y + (pos.y - m_catchPos.y)); m_initialData.pivot().y + (pos.y - m_catchPos.y));
m_currentData = m_initialData; m_currentData = m_initialData;
m_currentData.displacePivotTo(newPivot); m_currentData.displacePivotTo(newPivot);
@ -527,7 +529,7 @@ void PixelsMovement::dropImageTemporarily()
// Get the a factor for the X/Y position of the initial pivot // Get the a factor for the X/Y position of the initial pivot
// position inside the initial non-rotated bounds. // position inside the initial non-rotated bounds.
gfx::PointT<double> pivotPosFactor(m_initialData.pivot() - m_initialData.bounds().origin()); gfx::PointF pivotPosFactor(m_initialData.pivot() - m_initialData.bounds().origin());
pivotPosFactor.x /= m_initialData.bounds().w; pivotPosFactor.x /= m_initialData.bounds().w;
pivotPosFactor.y /= m_initialData.bounds().h; pivotPosFactor.y /= m_initialData.bounds().h;
@ -544,7 +546,7 @@ void PixelsMovement::dropImageTemporarily()
newPivot += pivotPosFactor.x * point2Vector(corners.rightTop() - corners.leftTop()); newPivot += pivotPosFactor.x * point2Vector(corners.rightTop() - corners.leftTop());
newPivot += pivotPosFactor.y * point2Vector(corners.leftBottom() - corners.leftTop()); newPivot += pivotPosFactor.y * point2Vector(corners.leftBottom() - corners.leftTop());
m_currentData.displacePivotTo(gfx::Point((int)newPivot.x, (int)newPivot.y)); m_currentData.displacePivotTo(gfx::PointF(newPivot.x, newPivot.y));
} }
redrawCurrentMask(); redrawCurrentMask();
@ -707,7 +709,7 @@ void PixelsMovement::drawParallelogram(
// fast rotation algorithm, as it's pixel-perfect match with the // fast rotation algorithm, as it's pixel-perfect match with the
// original selection when just a translation is applied. // original selection when just a translation is applied.
if (m_currentData.angle() == 0.0 && if (m_currentData.angle() == 0.0 &&
m_currentData.bounds().size() == src->size()) { gfx::Rect(m_currentData.bounds()).size() == src->size()) {
rotAlgo = tools::RotationAlgorithm::FAST; rotAlgo = tools::RotationAlgorithm::FAST;
} }

View File

@ -1,5 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2001-2015 David Capello // Copyright (C) 2001-2016 David Capello
// //
// This program is free software; you can redistribute it and/or modify // This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as // it under the terms of the GNU General Public License version 2 as
@ -177,7 +177,7 @@ gfx::Rect TransformHandles::getPivotHandleBounds(Editor* editor,
{ {
SkinTheme* theme = static_cast<SkinTheme*>(CurrentTheme::get()); SkinTheme* theme = static_cast<SkinTheme*>(CurrentTheme::get());
gfx::Size partSize = theme->parts.pivotHandle()->size(); gfx::Size partSize = theme->parts.pivotHandle()->size();
gfx::Point screenPivotPos = editor->editorToScreen(transform.pivot()); gfx::Point screenPivotPos = editor->editorToScreen(gfx::Point(transform.pivot()));
screenPivotPos.x += editor->zoom().apply(1) / 2; screenPivotPos.x += editor->zoom().apply(1) / 2;
screenPivotPos.y += editor->zoom().apply(1) / 2; screenPivotPos.y += editor->zoom().apply(1) / 2;

View File

@ -1,5 +1,5 @@
// Aseprite Gfx Library // Aseprite Gfx Library
// Copyright (C) 2001-2013 David Capello // Copyright (C) 2001-2016 David Capello
// //
// This file is released under the terms of the MIT license. // This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information. // Read LICENSE.txt for more information.
@ -118,6 +118,7 @@ public:
}; };
typedef PointT<int> Point; typedef PointT<int> Point;
typedef PointT<double> PointF;
} // namespace gfx } // namespace gfx

View File

@ -1,5 +1,5 @@
// Aseprite Gfx Library // Aseprite Gfx Library
// Copyright (C) 2001-2013, 2015 David Capello // Copyright (C) 2001-2016 David Capello
// //
// This file is released under the terms of the MIT license. // This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information. // Read LICENSE.txt for more information.
@ -45,6 +45,12 @@ public:
w(rect.w), h(rect.h) { w(rect.w), h(rect.h) {
} }
template<typename T2>
RectT(const RectT<T2>& rect) :
x(static_cast<T>(rect.x)), y(static_cast<T>(rect.y)),
w(static_cast<T>(rect.w)), h(static_cast<T>(rect.h)) {
}
RectT(const PointT<T>& point, const SizeT<T>& size) : RectT(const PointT<T>& point, const SizeT<T>& size) :
x(point.x), y(point.y), x(point.x), y(point.y),
w(size.w), h(size.h) { w(size.w), h(size.h) {
@ -298,6 +304,7 @@ public:
}; };
typedef RectT<int> Rect; typedef RectT<int> Rect;
typedef RectT<double> RectF;
} // namespace gfx } // namespace gfx

View File

@ -130,6 +130,7 @@ public:
}; };
typedef SizeT<int> Size; typedef SizeT<int> Size;
typedef SizeT<double> SizeF;
} // namespace gfx } // namespace gfx

View File

@ -1,5 +1,5 @@
// Aseprite Gfx Library // Aseprite Gfx Library
// Copyright (C) 2001-2013, 2015 David Capello // Copyright (C) 2001-2016 David Capello
// //
// This file is released under the terms of the MIT license. // This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information. // Read LICENSE.txt for more information.
@ -28,7 +28,7 @@ Transformation::Transformation()
m_pivot.y = 0; m_pivot.y = 0;
} }
Transformation::Transformation(const Rect& bounds) Transformation::Transformation(const RectF& bounds)
: m_bounds(bounds) : m_bounds(bounds)
{ {
m_angle = 0.0; m_angle = 0.0;
@ -38,8 +38,6 @@ Transformation::Transformation(const Rect& bounds)
void Transformation::transformBox(Corners& corners) const void Transformation::transformBox(Corners& corners) const
{ {
PointT<double> pivot_f(m_pivot);
// TODO We could create a composed 4x4 matrix with all // TODO We could create a composed 4x4 matrix with all
// transformation and apply the same matrix to avoid calling // transformation and apply the same matrix to avoid calling
// rotatePoint/cos/sin functions 4 times, anyway, it's not // rotatePoint/cos/sin functions 4 times, anyway, it's not
@ -47,10 +45,10 @@ void Transformation::transformBox(Corners& corners) const
corners = m_bounds; corners = m_bounds;
for (std::size_t c=0; c<corners.size(); ++c) for (std::size_t c=0; c<corners.size(); ++c)
corners[c] = Transformation::rotatePoint(corners[c], pivot_f, m_angle); corners[c] = Transformation::rotatePoint(corners[c], m_pivot, m_angle);
} }
void Transformation::displacePivotTo(const Point& newPivot) void Transformation::displacePivotTo(const PointF& newPivot)
{ {
// Calculate the rotated corners // Calculate the rotated corners
Corners corners; Corners corners;
@ -58,20 +56,20 @@ void Transformation::displacePivotTo(const Point& newPivot)
// Rotate-back (-angle) the position of the rotated origin (corners[0]) // Rotate-back (-angle) the position of the rotated origin (corners[0])
// using the new pivot. // using the new pivot.
PointT<double> newBoundsOrigin = PointF newBoundsOrigin =
rotatePoint(corners.leftTop(), rotatePoint(corners.leftTop(),
PointT<double>(newPivot.x, newPivot.y), newPivot,
-m_angle); -m_angle);
// Change the new pivot. // Change the new pivot.
m_pivot = newPivot; m_pivot = newPivot;
m_bounds = Rect(Point(newBoundsOrigin), m_bounds.size()); m_bounds = RectF(newBoundsOrigin, m_bounds.size());
} }
PointT<double> Transformation::rotatePoint( PointF Transformation::rotatePoint(
const PointT<double>& point, const PointF& point,
const PointT<double>& pivot, const PointF& pivot,
double angle) double angle)
{ {
using namespace fixmath; using namespace fixmath;
@ -82,21 +80,21 @@ PointT<double> Transformation::rotatePoint(
fixed sin = fixsin(fixangle); fixed sin = fixsin(fixangle);
fixed dx = fixsub(ftofix(point.x), ftofix(pivot.x)); fixed dx = fixsub(ftofix(point.x), ftofix(pivot.x));
fixed dy = fixsub(ftofix(point.y), ftofix(pivot.y)); fixed dy = fixsub(ftofix(point.y), ftofix(pivot.y));
return PointT<double>( return PointF(
fixtof(fixadd(ftofix(pivot.x), fixsub(fixmul(dx, cos), fixmul(dy, sin)))), fixtof(fixadd(ftofix(pivot.x), fixsub(fixmul(dx, cos), fixmul(dy, sin)))),
fixtof(fixadd(ftofix(pivot.y), fixadd(fixmul(dy, cos), fixmul(dx, sin))))); fixtof(fixadd(ftofix(pivot.y), fixadd(fixmul(dy, cos), fixmul(dx, sin)))));
} }
Rect Transformation::transformedBounds() const RectF Transformation::transformedBounds() const
{ {
// Get transformed corners // Get transformed corners
Corners corners; Corners corners;
transformBox(corners); transformBox(corners);
// Create a union of all corners // Create a union of all corners
Rect bounds; RectF bounds;
for (int i=0; i<Corners::NUM_OF_CORNERS; ++i) for (int i=0; i<Corners::NUM_OF_CORNERS; ++i)
bounds = bounds.createUnion(gfx::Rect((int)corners[i].x, (int)corners[i].y, 1, 1)); bounds = bounds.createUnion(RectF(corners[i].x, corners[i].y, 1, 1));
return bounds; return bounds;
} }

View File

@ -1,5 +1,5 @@
// Aseprite Gfx Library // Aseprite Gfx Library
// Copyright (C) 2001-2013, 2015 David Capello // Copyright (C) 2001-2016 David Capello
// //
// This file is released under the terms of the MIT license. // This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information. // Read LICENSE.txt for more information.
@ -33,20 +33,20 @@ public:
std::size_t size() const { return m_corners.size(); } std::size_t size() const { return m_corners.size(); }
PointT<double>& operator[](int index) { return m_corners[index]; } PointF& operator[](int index) { return m_corners[index]; }
const PointT<double>& operator[](int index) const { return m_corners[index]; } const PointF& operator[](int index) const { return m_corners[index]; }
const PointT<double>& leftTop() const { return m_corners[LEFT_TOP]; } const PointF& leftTop() const { return m_corners[LEFT_TOP]; }
const PointT<double>& rightTop() const { return m_corners[RIGHT_TOP]; } const PointF& rightTop() const { return m_corners[RIGHT_TOP]; }
const PointT<double>& rightBottom() const { return m_corners[RIGHT_BOTTOM]; } const PointF& rightBottom() const { return m_corners[RIGHT_BOTTOM]; }
const PointT<double>& leftBottom() const { return m_corners[LEFT_BOTTOM]; } const PointF& leftBottom() const { return m_corners[LEFT_BOTTOM]; }
void leftTop(const PointT<double>& pt) { m_corners[LEFT_TOP] = pt; } void leftTop(const PointF& pt) { m_corners[LEFT_TOP] = pt; }
void rightTop(const PointT<double>& pt) { m_corners[RIGHT_TOP] = pt; } void rightTop(const PointF& pt) { m_corners[RIGHT_TOP] = pt; }
void rightBottom(const PointT<double>& pt) { m_corners[RIGHT_BOTTOM] = pt; } void rightBottom(const PointF& pt) { m_corners[RIGHT_BOTTOM] = pt; }
void leftBottom(const PointT<double>& pt) { m_corners[LEFT_BOTTOM] = pt; } void leftBottom(const PointF& pt) { m_corners[LEFT_BOTTOM] = pt; }
Corners& operator=(const gfx::Rect& bounds) { Corners& operator=(const RectF bounds) {
m_corners[LEFT_TOP].x = bounds.x; m_corners[LEFT_TOP].x = bounds.x;
m_corners[LEFT_TOP].y = bounds.y; m_corners[LEFT_TOP].y = bounds.y;
m_corners[RIGHT_TOP].x = bounds.x + bounds.w; m_corners[RIGHT_TOP].x = bounds.x + bounds.w;
@ -58,29 +58,28 @@ public:
return *this; return *this;
} }
gfx::Rect bounds() const { RectF bounds() const {
Rect bounds; RectF bounds;
for (int i=0; i<Corners::NUM_OF_CORNERS; ++i) for (int i=0; i<Corners::NUM_OF_CORNERS; ++i)
bounds = bounds.createUnion(gfx::Rect((int)m_corners[i].x, bounds |= RectF(m_corners[i].x, m_corners[i].y, 1, 1);
(int)m_corners[i].y, 1, 1));
return bounds; return bounds;
} }
private: private:
std::vector<PointT<double> > m_corners; std::vector<PointF> m_corners;
}; };
Transformation(); Transformation();
Transformation(const Rect& bounds); Transformation(const RectF& bounds);
// Simple getters and setters. The angle is in radians. // Simple getters and setters. The angle is in radians.
const Rect& bounds() const { return m_bounds; } const RectF& bounds() const { return m_bounds; }
const Point& pivot() const { return m_pivot; } const PointF& pivot() const { return m_pivot; }
double angle() const { return m_angle; } double angle() const { return m_angle; }
void bounds(const Rect& bounds) { m_bounds = bounds; } void bounds(const RectF& bounds) { m_bounds = bounds; }
void pivot(const Point& pivot) { m_pivot = pivot; } void pivot(const PointF& pivot) { m_pivot = pivot; }
void angle(double angle) { m_angle = angle; } void angle(double angle) { m_angle = angle; }
// Applies the transformation (rotation with angle/pivot) to the // Applies the transformation (rotation with angle/pivot) to the
@ -89,17 +88,18 @@ public:
// Changes the pivot to another location, adjusting the bounds to // Changes the pivot to another location, adjusting the bounds to
// keep the current rotated-corners in the same location. // keep the current rotated-corners in the same location.
void displacePivotTo(const Point& newPivot); void displacePivotTo(const PointF& newPivot);
Rect transformedBounds() const; RectF transformedBounds() const;
// Static helper method to rotate points. // Static helper method to rotate points.
static PointT<double> rotatePoint(const PointT<double>& point, static PointF rotatePoint(const PointF& point,
const PointT<double>& pivot, double angle); const PointF& pivot,
double angle);
private: private:
Rect m_bounds; RectF m_bounds;
Point m_pivot; PointF m_pivot;
double m_angle; double m_angle;
}; };