diff --git a/src/control/tools/ArrowHandler.cpp b/src/control/tools/ArrowHandler.cpp index c0ec2939..4129fc3d 100644 --- a/src/control/tools/ArrowHandler.cpp +++ b/src/control/tools/ArrowHandler.cpp @@ -5,6 +5,60 @@ #include "undo/InsertUndoAction.h" #include +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; diff --git a/src/control/tools/ArrowHandler.h b/src/control/tools/ArrowHandler.h index d1a06911..5eaf9d51 100644 --- a/src/control/tools/ArrowHandler.h +++ b/src/control/tools/ArrowHandler.h @@ -23,6 +23,7 @@ private: virtual void drawShape(Point& currentPoint, bool shiftDown); private: + void snapToGrid(double& x, double& y); XOJ_TYPE_ATTRIB; }; diff --git a/src/control/tools/RulerHandler.cpp b/src/control/tools/RulerHandler.cpp index 1c6c97e7..c34594e3 100644 --- a/src/control/tools/RulerHandler.cpp +++ b/src/control/tools/RulerHandler.cpp @@ -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);