Fixed the snap to grid and made it work for the first point on arrows

too.  The snapToGrid function needs to be moved up in inheritance
instead of using copy paste as I did here, but this works for now.
presentation
Rob Frohne 7 years ago
parent 672154ac27
commit 2e4d297dd0
  1. 66
      src/control/tools/ArrowHandler.cpp
  2. 1
      src/control/tools/ArrowHandler.h
  3. 2
      src/control/tools/RulerHandler.cpp

@ -5,6 +5,60 @@
#include "undo/InsertUndoAction.h"
#include <cmath>
void ArrowHandler::snapToGrid(double& x, double& y)
{
XOJ_CHECK_TYPE(ArrowHandler);
if (!xournal->getControl()->getSettings()->isSnapGrid())
{
return;
}
/**
* Snap points to a grid:
* If x/y coordinates are under a certain tolerance,
* fix the point to the grid intersection value
*/
double gridSize = 14.17;
double tolerance = gridSize/2.0;
double xRem = fmod(x,gridSize);
double yRem = fmod(y,gridSize);
bool snapX = false;
bool snapY = false;
double tmpX = 0;
double tmpY = 0;
if (xRem < tolerance)
{
tmpX = x - xRem;
snapX = true;
}
if (xRem > gridSize - tolerance )
{
tmpX = x + (gridSize - xRem);
snapX = true;
}
if (yRem < tolerance)
{
tmpY = y - yRem;
snapY = true;
}
if (yRem > gridSize - tolerance )
{
tmpY = y + (gridSize - yRem);
snapY = true;
}
if (snapX && snapY)
{
x = tmpX;
y = tmpY;
}
}
ArrowHandler::ArrowHandler(XournalView* xournal, XojPageView* redrawable, PageRef page)
: BaseStrokeHandler(xournal, redrawable, page)
{
@ -22,6 +76,16 @@ void ArrowHandler::drawShape(Point& c, bool shiftDown)
{
int count = stroke->getPointCount();
/**
* Snap first point to grid (if enabled)
*/
if (!shiftDown && xournal->getControl()->getSettings()->isSnapGrid())
{
Point firstPoint = stroke->getPoint(0);
snapToGrid(firstPoint.x,firstPoint.y);
stroke->setFirstPoint(firstPoint.x,firstPoint.y);
}
if (count < 1)
{
stroke->addPoint(c);
@ -60,7 +124,7 @@ void ArrowHandler::drawShape(Point& c, bool shiftDown)
}
else
{
double epsilon = 0.1;
double epsilon = 0.2;
if (std::abs(angle) < epsilon)
{
angle = 0;

@ -23,6 +23,7 @@ private:
virtual void drawShape(Point& currentPoint, bool shiftDown);
private:
void snapToGrid(double& x, double& y);
XOJ_TYPE_ATTRIB;
};

@ -33,7 +33,7 @@ void RulerHandler::snapToGrid(double& x, double& y)
* fix the point to the grid intersection value
*/
double gridSize = 14.17;
double tolerance = 2.5;
double tolerance = gridSize/2.0;
double xRem = fmod(x,gridSize);
double yRem = fmod(y,gridSize);

Loading…
Cancel
Save