Add snap to grid in selection movement.

This commit is contained in:
David Capello 2012-01-07 17:23:21 -03:00
parent 99bd0015b9
commit a22b86df65
7 changed files with 56 additions and 16 deletions

View File

@ -21,6 +21,7 @@
#include "app/color.h"
#include "filters/tiled_mode.h"
#include "gfx/point.h"
#include "gfx/rect.h"
#include "raster/pen_type.h"
@ -29,6 +30,11 @@ class IPenSettings;
namespace tools { class Tool; }
enum SnapBehavior {
NormalSnap = 0,
SnapInRightBottom = 1
};
// Settings used in tool <-> drawing <-> editor stuff
class ISettings
{
@ -59,6 +65,8 @@ public:
virtual void setGridBounds(const gfx::Rect& rect) = 0;
virtual void setGridColor(const Color& color) = 0;
virtual void snapToGrid(gfx::Point& point, SnapBehavior snapBehavior) const = 0;
// Pixel grid
virtual bool getPixelGridVisible() = 0;

View File

@ -174,6 +174,23 @@ void UISettingsImpl::setGridColor(const Color& color)
m_gridColor = color;
}
void UISettingsImpl::snapToGrid(gfx::Point& point, SnapBehavior snapBehavior) const
{
register int w = m_gridBounds.w;
register int h = m_gridBounds.h;
int adjust = (snapBehavior & SnapInRightBottom ? 1: 0);
div_t d, dx, dy;
dx = div(m_gridBounds.x, w);
dy = div(m_gridBounds.y, h);
d = div(point.x-dx.rem, w);
point.x = dx.rem + d.quot*w + ((d.rem > w/2)? w-adjust: 0);
d = div(point.y-dy.rem, h);
point.y = dy.rem + d.quot*h + ((d.rem > h/2)? h-adjust: 0);
}
//////////////////////////////////////////////////////////////////////
// Pixel grid

View File

@ -21,6 +21,7 @@
#include <map>
#include <string>
#include "base/compiler_specific.h"
#include "settings/settings.h"
class UISettingsImpl : public ISettings
@ -53,6 +54,8 @@ public:
void setGridBounds(const gfx::Rect& rect);
void setGridColor(const Color& color);
void snapToGrid(gfx::Point& point, SnapBehavior snapBehavior) const OVERRIDE;
// Pixel grid
bool getPixelGridVisible();

View File

@ -197,21 +197,8 @@ void ToolLoopManager::snapToGrid(bool flexible, Point& point)
!m_toolLoop->getContext()->getSettings()->getSnapToGrid())
return;
Rect grid(m_toolLoop->getContext()->getSettings()->getGridBounds());
register int w = grid.w;
register int h = grid.h;
div_t d, dx, dy;
flexible = flexible ? 1: 0;
dx = div(grid.x, w);
dy = div(grid.y, h);
d = div(point.x-dx.rem, w);
point.x = dx.rem + d.quot*w + ((d.rem > w/2)? w-flexible: 0);
d = div(point.y-dy.rem, h);
point.y = dy.rem + d.quot*h + ((d.rem > h/2)? h-flexible: 0);
m_toolLoop->getContext()->getSettings()
->snapToGrid(point, (flexible ? SnapInRightBottom: NormalSnap));
}
void ToolLoopManager::calculateDirtyArea(ToolLoop* loop, const Points& points, Rect& dirty_area)

View File

@ -189,8 +189,11 @@ bool MovingPixelsState::onMouseMove(Editor* editor, Message* msg)
PixelsMovement::MoveModifier moveModifier = PixelsMovement::NormalMovement;
if (editor->getCustomizationDelegate()->isSnapToGridKeyPressed())
moveModifier |= PixelsMovement::SnapToGridMovement;
if (editor->getCustomizationDelegate()->isAngleSnapKeyPressed())
moveModifier = PixelsMovement::AngleSnapMovement;
moveModifier |= PixelsMovement::AngleSnapMovement;
// Drag the image to that position
gfx::Rect bounds = m_pixelsMovement->moveImage(x, y, moveModifier);

View File

@ -29,6 +29,7 @@
#include "raster/mask.h"
#include "raster/rotate.h"
#include "raster/sprite.h"
#include "ui_context.h"
#include "util/expand_cel_canvas.h"
template<typename T>
@ -176,6 +177,21 @@ gfx::Rect PixelsMovement::moveImage(int x, int y, MoveModifier moveModifier)
x2 += dx;
y2 += dy;
updateBounds = true;
if ((moveModifier & SnapToGridMovement) == SnapToGridMovement) {
// Snap the x1,y1 point to the grid.
gfx::Point gridOffset(x1, y1);
UIContext::instance()->getSettings()->snapToGrid(gridOffset, NormalSnap);
// Now we calculate the difference from x1,y1 point and we can
// use it to adjust all coordinates (x1, y1, x2, y2).
gridOffset -= gfx::Point(x1, y1);
x1 += gridOffset.x;
y1 += gridOffset.y;
x2 += gridOffset.x;
y2 += gridOffset.y;
}
break;
case ScaleNWHandle:

View File

@ -89,4 +89,10 @@ private:
Mask* m_currentMask;
};
inline PixelsMovement::MoveModifier& operator|=(PixelsMovement::MoveModifier& a,
const PixelsMovement::MoveModifier& b) {
a = static_cast<PixelsMovement::MoveModifier>(a | b);
return a;
}
#endif