work on draw preview

Tue, 20 Jul 2021 01:22:01 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 20 Jul 2021 01:22:01 +0300
changeset 106
128efb9d148b
parent 105
6ca6e8c647d4
child 107
02f142b399b1

work on draw preview

src/functional.h file | annotate | diff | comparison | revisions
src/geometry.h file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
src/tools/basetool.h file | annotate | diff | comparison | revisions
src/tools/drawtool.cpp file | annotate | diff | comparison | revisions
src/tools/drawtool.h file | annotate | diff | comparison | revisions
src/ui/canvas.cpp file | annotate | diff | comparison | revisions
src/ui/canvas.h file | annotate | diff | comparison | revisions
--- a/src/functional.h	Mon Jul 19 23:41:52 2021 +0300
+++ b/src/functional.h	Tue Jul 20 01:22:01 2021 +0300
@@ -157,3 +157,29 @@
 		return result;
 	}
 }
+
+template<typename T, typename Fn>
+bool any(T&& container, Fn&& f)
+{
+	for (auto&& x : container)
+	{
+		if (f(x))
+		{
+			return true;
+		}
+	}
+	return false;
+}
+
+template<typename T, typename Fn>
+bool all(T&& container, Fn&& f)
+{
+	for (auto&& x : container)
+	{
+		if (not f(x))
+		{
+			return false;
+		}
+	}
+	return true;
+}
--- a/src/geometry.h	Mon Jul 19 23:41:52 2021 +0300
+++ b/src/geometry.h	Tue Jul 20 01:22:01 2021 +0300
@@ -102,4 +102,11 @@
 	{
 		std::vector<glm::vec3> points;
 	};
+
+	inline bool isclose(const glm::vec3& a, const glm::vec3& b)
+	{
+		return qFuzzyCompare(a.x, b.x)
+			and qFuzzyCompare(a.y, b.y)
+			and qFuzzyCompare(a.z, b.z);
+	}
 }
--- a/src/mainwindow.cpp	Mon Jul 19 23:41:52 2021 +0300
+++ b/src/mainwindow.cpp	Tue Jul 20 01:22:01 2021 +0300
@@ -399,6 +399,10 @@
 {
 	if (tool != nullptr && tool != this->selectedTool)
 	{
+		if (this->selectedTool != nullptr)
+		{
+			this->selectedTool->reset();
+		}
 		this->selectedTool = tool;
 		for (auto&& pair : items(this->toolActions))
 		{
--- a/src/tools/basetool.h	Mon Jul 19 23:41:52 2021 +0300
+++ b/src/tools/basetool.h	Tue Jul 20 01:22:01 2021 +0300
@@ -17,5 +17,6 @@
 	virtual bool mouseDoubleClicked(QMouseEvent*) { return false; }
 	virtual bool mouseMoved(QMouseEvent*) { return false; }
 	virtual bool keyReleased(QKeyEvent*) { return false; }
+	virtual void reset() {}
 };
 
--- a/src/tools/drawtool.cpp	Mon Jul 19 23:41:52 2021 +0300
+++ b/src/tools/drawtool.cpp	Tue Jul 20 01:22:01 2021 +0300
@@ -18,7 +18,30 @@
 
 bool DrawTool::mouseClick(const Canvas::MouseClickInfo& info)
 {
-	static_cast<void>(info);
-	QMessageBox::information(nullptr, "hleelo", "it works");
+	if (info.worldPosition.has_value())
+	{
+		const glm::vec3& pos = info.worldPosition.value();
+		const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);};
+		if (any(this->polygon, isCloseToPos))
+		{
+			QMessageBox::information(nullptr, "test", "close the polygon");
+		}
+		else
+		{
+			this->polygon.push_back(pos);
+			auto& previewLayer = info.invoker->modifyPreviewLayer(Canvas::DrawToolPreview).polygons;
+			previewLayer.clear();
+			previewLayer.push_back({this->polygon});
+			if (this->polygon.size() == 4)
+			{
+				QMessageBox::information(nullptr, "test", "close the polygon");
+			}
+		}
+	}
 	return true;
 }
+
+void DrawTool::reset()
+{
+	this->polygon.clear();
+}
--- a/src/tools/drawtool.h	Mon Jul 19 23:41:52 2021 +0300
+++ b/src/tools/drawtool.h	Tue Jul 20 01:22:01 2021 +0300
@@ -11,4 +11,7 @@
 	QString name() const override;
 	QString toolTip() const override;
 	bool mouseClick(const Canvas::MouseClickInfo& info) override;
+	void reset() override;
+private:
+	std::vector<glm::vec3> polygon;
 };
--- a/src/ui/canvas.cpp	Mon Jul 19 23:41:52 2021 +0300
+++ b/src/ui/canvas.cpp	Tue Jul 20 01:22:01 2021 +0300
@@ -89,9 +89,6 @@
 
 void Canvas::mouseReleaseEvent(QMouseEvent* event)
 {
-	MouseClickInfo info;
-	info.click = this->totalMouseMove < (2.0 / sqrt(2)) * 5.0;
-	emit mouseClick(info);
 	if (this->totalMouseMove < (2.0 / sqrt(2)) * 5.0)
 	{
 		if (this->highlighted == ldraw::NULL_ID)
@@ -105,6 +102,10 @@
 		this->compiler->setSelectedObjects(this->selection);
 		emit selectionChanged(this->selection);
 		this->update();
+		MouseClickInfo info;
+		info.worldPosition = this->worldPosition;
+		info.invoker = this;
+		emit mouseClick(info);
 	}
 	PartRenderer::mouseReleaseEvent(event);
 }
--- a/src/ui/canvas.h	Mon Jul 19 23:41:52 2021 +0300
+++ b/src/ui/canvas.h	Tue Jul 20 01:22:01 2021 +0300
@@ -14,7 +14,7 @@
 	};
 	struct PreviewLayer
 	{
-		QSet<geom::NPolygon> polygons;
+		QVector<geom::NPolygon> polygons;
 		QColor color{64, 255, 128};
 	};
 	static constexpr int NUM_PREVIEW_LAYERS = 1;
@@ -54,5 +54,6 @@
 
 struct Canvas::MouseClickInfo
 {
-	bool click;
+	std::optional<glm::vec3> worldPosition;
+	Canvas* invoker;
 };

mercurial