From eebade47002f96056200b4611abbb200bd0a6e5f Mon Sep 17 00:00:00 2001 From: morro Date: Thu, 6 Dec 2018 21:10:36 +0100 Subject: [PATCH] rotation snapping every 45 degrees --- src/control/tools/EditSelection.cpp | 41 +++++++++++++++++++++++++++++ src/control/tools/EditSelection.h | 2 ++ 2 files changed, 43 insertions(+) diff --git a/src/control/tools/EditSelection.cpp b/src/control/tools/EditSelection.cpp index e297e415..a7383326 100644 --- a/src/control/tools/EditSelection.cpp +++ b/src/control/tools/EditSelection.cpp @@ -319,6 +319,8 @@ void EditSelection::mouseUp() PageRef page = this->view->getPage(); Layer* layer = page->getSelectedLayer(); + snapRotation(); + this->contents->updateContent(this->x, this->y, this->rotation, this->width, this->height, this->aspectRatio, layer, page, this->view, this->undo, this->mouseDownType); @@ -639,6 +641,43 @@ CursorSelectionType EditSelection::getSelectionTypeForPos(double x, double y, do return CURSOR_SELECTION_NONE; } +void EditSelection::snapRotation() +{ + double epsilon = 0.1; + if(std::abs(this->rotation) < epsilon) + { + this->rotation = 0; + } + else if (std::abs(this->rotation - M_PI / 2.0) < epsilon) + { + this->rotation = M_PI / 2.0; + } + else if (std::abs(this->rotation - M_PI) < epsilon) + { + this->rotation = M_PI; + } + else if (std::abs(this->rotation - M_PI / 4.0) < epsilon) + { + this->rotation = M_PI / 4.0; + } + else if (std::abs(this->rotation - 3.0 * M_PI / 4.0) < epsilon) + { + this->rotation = 3.0 * M_PI / 4.0; + } + else if (std::abs(this->rotation + M_PI / 4.0) < epsilon) + { + this->rotation = - (M_PI / 4.0); + } + else if (std::abs(this->rotation + 3.0 * M_PI / 4.0) < epsilon) + { + this->rotation = - (3.0 * M_PI / 4.0); + } + else if (std::abs(this->rotation + M_PI / 2.0) < epsilon) + { + this->rotation = - (M_PI / 2.0); + } +} + /** * Paints the selection to cr, with the given zoom factor. The coordinates of cr * should be relative to the provideded view by getView() (use translateEvent()) @@ -654,6 +693,8 @@ void EditSelection::paint(cairo_t* cr, double zoom) if (abs(this->rotation) > __DBL_EPSILON__) { + snapRotation(); + double rx = (x + width / 2) * zoom; double ry = (y + height / 2) * zoom; diff --git a/src/control/tools/EditSelection.h b/src/control/tools/EditSelection.h index c5c9adeb..84448dd2 100644 --- a/src/control/tools/EditSelection.h +++ b/src/control/tools/EditSelection.h @@ -44,6 +44,8 @@ private: */ void contstruct(UndoRedoHandler* undo, XojPageView* view, PageRef sourcePage); + void snapRotation(); + public: /** * get the X coordinate relative to the provided view (getView())