src/tools/drawtool.cpp

changeset 121
000781318c36
parent 111
1f42c03fafca
child 122
b54b350dff5d
--- a/src/tools/drawtool.cpp	Wed Jul 28 13:22:51 2021 +0300
+++ b/src/tools/drawtool.cpp	Fri Jul 30 01:28:39 2021 +0300
@@ -26,46 +26,65 @@
 	return result;
 }
 
-bool DrawTool::mouseClick(Document* document, Canvas* canvas)
+bool DrawTool::mouseClick(Document* document, Canvas* canvas, QMouseEvent* event)
 {
-	const auto& worldPosition = canvas->getWorldPosition();
-	if (worldPosition.has_value())
+	if (event->button() == Qt::LeftButton)
 	{
-		const glm::vec3& pos = worldPosition.value();
-		const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);};
-		if (any(this->polygon, isCloseToPos))
+		const auto& worldPosition = canvas->getWorldPosition();
+		if (worldPosition.has_value())
 		{
-			this->closeShape(document);
-		}
-		else
-		{
-			this->polygon.push_back(pos);
-			if (this->polygon.size() == 4)
+			const glm::vec3& pos = worldPosition.value();
+			const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);};
+			if (any(this->polygon, isCloseToPos))
 			{
 				this->closeShape(document);
 			}
+			else
+			{
+				this->polygon.push_back(pos);
+				if (this->polygon.size() == 4)
+				{
+					this->closeShape(document);
+				}
+			}
 		}
+		this->previewPolygon = this->polygon;
+		return true;
 	}
-	this->previewPolygon = this->polygon;
-	return true;
+	else if (event->button() == Qt::RightButton and this->polygon.size() > 0)
+	{
+		this->polygon.erase(this->polygon.end() - 1);
+		this->updatePreviewPolygon();
+		return true;
+	}
+	else
+	{
+		return false;
+	}
 }
 
-bool DrawTool::mouseMove(Document* document, Canvas* canvas)
+bool DrawTool::mouseMove(Document* document, Canvas* canvas, QMouseEvent *event)
 {
 	static_cast<void>(document);
+	static_cast<void>(event);
 	const auto& worldPosition = canvas->getWorldPosition();
 	if (worldPosition.has_value())
 	{
 		this->previewPoint = worldPosition.value();
 		if (this->polygon.size() < 4)
 		{
-			this->previewPolygon.resize(this->polygon.size() + 1);
-			this->previewPolygon.back() = this->previewPoint;
+			this->updatePreviewPolygon();
 		}
 	}
 	return false;
 }
 
+void DrawTool::updatePreviewPolygon()
+{
+	this->previewPolygon.resize(this->polygon.size() + 1);
+	this->previewPolygon.back() = this->previewPoint;
+}
+
 void DrawTool::reset()
 {
 	this->polygon.clear();

mercurial