From 515bbe45be4c41ee8f94b95d8a274f05e705078c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20M=C3=BCller?= <d_muel20@uni-muenster.de>
Date: Sat, 14 Jul 2018 11:35:15 +0200
Subject: [PATCH] * Drawing tools are now more precise when zoomed

---
 Grinder/ui/image/draftitems/PixelsDraftItemNode.cpp |  2 +-
 Grinder/ui/image/tools/FloodFillTool.cpp            |  5 +++--
 Grinder/ui/image/tools/PaintbrushTool.cpp           |  9 +++++----
 Grinder/util/MathUtils.cpp                          | 10 ++++++++++
 Grinder/util/MathUtils.h                            |  3 +++
 5 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/Grinder/ui/image/draftitems/PixelsDraftItemNode.cpp b/Grinder/ui/image/draftitems/PixelsDraftItemNode.cpp
index e5514c9..af4969a 100644
--- a/Grinder/ui/image/draftitems/PixelsDraftItemNode.cpp
+++ b/Grinder/ui/image/draftitems/PixelsDraftItemNode.cpp
@@ -21,7 +21,7 @@ PixelsDraftItemNode::PixelsDraftItemNode(ImageEditorScene* scene, const std::sha
 bool PixelsDraftItemNode::contains(const QPointF& point) const
 {
 	if (auto draftItem = pixelsDraftItem())	// Make sure that the underlying draft item still exists
-		return draftItem->pixelsData().isSet(point.toPoint());
+		return draftItem->pixelsData().isSet(MathUtils::floor(point));
 	else
 		return DraftItemNode::contains(point);
 }
diff --git a/Grinder/ui/image/tools/FloodFillTool.cpp b/Grinder/ui/image/tools/FloodFillTool.cpp
index 788ae38..c0794da 100644
--- a/Grinder/ui/image/tools/FloodFillTool.cpp
+++ b/Grinder/ui/image/tools/FloodFillTool.cpp
@@ -6,6 +6,7 @@
 #include "Grinder.h"
 #include "FloodFillTool.h"
 #include "ui/image/ImageEditor.h"
+#include "util/MathUtils.h"
 #include "res/Resources.h"
 
 const char* FloodFillTool::tool_type = "FloodFillTool";
@@ -34,13 +35,13 @@ void FloodFillTool::createProperties()
 
 ImageEditorTool::InputEventResult FloodFillTool::mousePressed(const QGraphicsSceneMouseEvent* event)
 {
-	floodFill(event->scenePos().toPoint());
+	floodFill(MathUtils::floor(event->scenePos()));
 	return InputEventResult::Process;
 }
 
 VisualSceneInputHandler::InputEventResult FloodFillTool::rightMousePressed(const QGraphicsSceneMouseEvent* event)
 {
-	floodFill(event->scenePos().toPoint(), true);
+	floodFill(MathUtils::floor(event->scenePos()), true);
 	return InputEventResult::Process;
 }
 
diff --git a/Grinder/ui/image/tools/PaintbrushTool.cpp b/Grinder/ui/image/tools/PaintbrushTool.cpp
index 2add57f..ea5040a 100644
--- a/Grinder/ui/image/tools/PaintbrushTool.cpp
+++ b/Grinder/ui/image/tools/PaintbrushTool.cpp
@@ -7,6 +7,7 @@
 #include "PaintbrushTool.h"
 #include "ui/image/ImageEditor.h"
 #include "image/ImageUtils.h"
+#include "util/MathUtils.h"
 #include "res/Resources.h"
 
 const char* PaintbrushTool::tool_type = "PaintbrushTool";
@@ -35,7 +36,7 @@ void PaintbrushTool::createProperties()
 
 ImageEditorTool::InputEventResult PaintbrushTool::mousePressed(const QGraphicsSceneMouseEvent* event)
 {
-	auto pos = event->scenePos().toPoint();
+	auto pos = MathUtils::floor(event->scenePos());
 	_lastPixelPos = pos;
 	paintPixel(pos, _eraseByDefault);
 	return InputEventResult::Process;
@@ -43,7 +44,7 @@ ImageEditorTool::InputEventResult PaintbrushTool::mousePressed(const QGraphicsSc
 
 ImageEditorTool::InputEventResult PaintbrushTool::mouseMoved(const QGraphicsSceneMouseEvent* event)
 {
-	auto pos = event->scenePos().toPoint();
+	auto pos = MathUtils::floor(event->scenePos());
 	paintPixel(pos, _eraseByDefault);
 	_lastPixelPos = pos;
 	return InputEventResult::Process;
@@ -51,7 +52,7 @@ ImageEditorTool::InputEventResult PaintbrushTool::mouseMoved(const QGraphicsScen
 
 VisualSceneInputHandler::InputEventResult PaintbrushTool::rightMousePressed(const QGraphicsSceneMouseEvent* event)
 {
-	auto pos = event->scenePos().toPoint();
+	auto pos = MathUtils::floor(event->scenePos());
 	_lastPixelPos = pos;
 	paintPixel(pos, !_eraseByDefault);
 	return InputEventResult::Process;
@@ -59,7 +60,7 @@ VisualSceneInputHandler::InputEventResult PaintbrushTool::rightMousePressed(cons
 
 VisualSceneInputHandler::InputEventResult PaintbrushTool::rightMouseMoved(const QGraphicsSceneMouseEvent* event)
 {
-	auto pos = event->scenePos().toPoint();
+	auto pos = MathUtils::floor(event->scenePos());
 	paintPixel(pos, !_eraseByDefault);
 	_lastPixelPos = pos;
 	return InputEventResult::Process;
diff --git a/Grinder/util/MathUtils.cpp b/Grinder/util/MathUtils.cpp
index 9c3d550..5ae677a 100644
--- a/Grinder/util/MathUtils.cpp
+++ b/Grinder/util/MathUtils.cpp
@@ -11,3 +11,13 @@ double MathUtils::vectorLength(double x, double y)
 	// Simple L2 norm
 	return std::sqrt(x * x + y * y);
 }
+
+QPoint MathUtils::ceil(QPointF pos)
+{
+	return QPoint{static_cast<int>(std::ceil(pos.x())), static_cast<int>(std::ceil(pos.y()))};
+}
+
+QPoint MathUtils::floor(QPointF pos)
+{
+	return QPoint{static_cast<int>(std::floor(pos.x())), static_cast<int>(std::floor(pos.y()))};
+}
diff --git a/Grinder/util/MathUtils.h b/Grinder/util/MathUtils.h
index 80fa7ff..7382610 100644
--- a/Grinder/util/MathUtils.h
+++ b/Grinder/util/MathUtils.h
@@ -18,6 +18,9 @@ namespace grndr
 		template<typename T>
 		static T clamp(T& val, T min, T max) { if (val < min) val = min; if (val > max) val = max; return val; }
 
+		static QPoint ceil(QPointF pos);
+		static QPoint floor(QPointF pos);
+
 	private:
 		MathUtils() { }
 	};
-- 
GitLab