more work on circle tool + cleanup

Tue, 24 May 2022 16:11:10 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 24 May 2022 16:11:10 +0300
changeset 188
64ea7282611e
parent 187
30204975694a
child 189
815fbaae9cb2

more work on circle tool + cleanup

src/colors.h file | annotate | diff | comparison | revisions
src/document.cpp file | annotate | diff | comparison | revisions
src/document.h file | annotate | diff | comparison | revisions
src/main.h file | annotate | diff | comparison | revisions
src/tools/basetool.h file | annotate | diff | comparison | revisions
src/tools/circletool.cpp file | annotate | diff | comparison | revisions
src/tools/circletool.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/colors.h	Mon May 16 01:40:49 2022 +0300
+++ b/src/colors.h	Tue May 24 16:11:10 2022 +0300
@@ -55,6 +55,7 @@
 struct ldraw::Color
 {
 	qint32 index = 0;
+	constexpr auto operator<=>(const Color&) const = default;
 };
 
 Q_DECLARE_METATYPE(ldraw::Color)
@@ -63,36 +64,6 @@
 
 namespace ldraw
 {
-	constexpr bool operator==(Color one, Color other)
-	{
-		return one.index == other.index;
-	}
-
-	constexpr bool operator!=(Color one, Color other)
-	{
-		return one.index != other.index;
-	}
-
-	constexpr bool operator<(Color one, Color other)
-	{
-		return one.index < other.index;
-	}
-
-	constexpr bool operator<=(Color one, Color other)
-	{
-		return one.index <= other.index;
-	}
-
-	constexpr bool operator>(Color one, Color other)
-	{
-		return one.index > other.index;
-	}
-
-	constexpr bool operator>=(Color one, Color other)
-	{
-		return one.index >= other.index;
-	}
-
 	static constexpr Color BLACK {0};
 	static constexpr Color BLUE {1};
 	static constexpr Color GREEN {2};
--- a/src/document.cpp	Mon May 16 01:40:49 2022 +0300
+++ b/src/document.cpp	Tue May 24 16:11:10 2022 +0300
@@ -167,7 +167,7 @@
 	this->tools.push_back(new CircleTool{this});
 	this->tools.push_back(new PathTool{this});
 	this->tools.push_back(new TransformTool{this});
-	for (BaseTool* const toolInstance : this->tools)
+	for (BaseTool* toolInstance : this->tools)
 	{
 		QAction* action = new QAction{toolInstance->name(), this};
 		action->setCheckable(true);
@@ -185,6 +185,7 @@
 		{
 			this->ui.toolWidgetStack->addWidget(new QWidget{this});
 		}
+		connect(toolInstance, &BaseTool::desiredGridChange, this->renderer, &Canvas::setGridMatrix);
 	}
 	this->selectTool(this->tools[0]);
 }
@@ -240,6 +241,11 @@
 	this->renderer->adjustGridToView();
 }
 
+const glm::mat4 &Document::currentGrid() const
+{
+	return this->renderer->getGridMatrix();
+}
+
 const Model &Document::getModel() const
 {
 	return *this->model;
--- a/src/document.h	Mon May 16 01:40:49 2022 +0300
+++ b/src/document.h	Tue May 24 16:11:10 2022 +0300
@@ -48,6 +48,7 @@
 	void applyToVertices(VertexMap::ApplyFunction fn) const;
 	void handleKeyPress(QKeyEvent* event);
 	void adjustGridToView();
+	const glm::mat4& currentGrid() const;
 	const Model& getModel() const;
 	const QSet<ldraw::id_t> selectedObjects() const;
 	const ldraw::ColorTable& colorTable;
--- a/src/main.h	Mon May 16 01:40:49 2022 +0300
+++ b/src/main.h	Tue May 24 16:11:10 2022 +0300
@@ -21,6 +21,7 @@
 #include <QVector>
 #include <QSet>
 #include <QDebug>
+#include <compare>
 #include <memory>
 #include "basics.h"
 #include "utility.h"
--- a/src/tools/basetool.h	Mon May 16 01:40:49 2022 +0300
+++ b/src/tools/basetool.h	Tue May 24 16:11:10 2022 +0300
@@ -23,6 +23,8 @@
 	virtual void reset() {}
 	virtual void overpaint(Canvas*, QPainter*) const {}
 	virtual QString iconName() const;
+Q_SIGNALS:
+	void desiredGridChange(const glm::mat4& matrix);
 protected:
 	QWidget* const parentWidget;
 	Document* const document;
--- a/src/tools/circletool.cpp	Mon May 16 01:40:49 2022 +0300
+++ b/src/tools/circletool.cpp	Tue May 24 16:11:10 2022 +0300
@@ -1,4 +1,5 @@
 #include "circletool.h"
+#include "document.h"
 
 CircleTool::CircleTool(Document *document) :
 	AbstractDrawTool{document}
@@ -36,7 +37,7 @@
 		painter->setPen(QPen{Qt::green, 2, Qt::DashLine, Qt::RoundCap, Qt::MiterJoin});
 		canvas->drawWorldPolyline(painter, {this->previewPolygon[0], this->previewPolygon[1]});
 		const float size = glm::distance(this->previewPolygon[1], this->previewPolygon[0]);
-		glm::mat4 matrix = size * canvas->getGridMatrix();
+		glm::mat4 matrix = size * this->baseGridMatrix;
 		matrix[3] = {this->previewPolygon[0], 1};
 		std::vector<glm::vec3> points = circle(16);
 		for (std::size_t i = 0; i < points.size(); ++i) {
@@ -45,6 +46,10 @@
 		painter->setPen(QPen{Qt::black, 2, Qt::DashLine, Qt::RoundCap, Qt::MiterJoin});
 		canvas->drawWorldPolyline(painter, points);
 	}
+	if (this->previewPolygon.size() >= 3)
+	{
+
+	}
 }
 
 QString CircleTool::iconName() const
@@ -52,7 +57,23 @@
 	return ":/icons/linetype-circularprimitive.png";
 }
 
+void CircleTool::addPoint(const glm::vec3 &pos)
+{
+	AbstractDrawTool::addPoint(pos);
+	if (this->polygon.size() >= 2)
+	{
+		const glm::mat4& grid = this->document->currentGrid();
+		const glm::mat4 newGrid = {grid[1], grid[2], grid[0], {this->polygon[0], 1}};
+		Q_EMIT this->desiredGridChange(newGrid);
+	}
+}
+
 void CircleTool::closeShape()
 {
 	
 }
+
+void CircleTool::reset()
+{
+	this->baseGridMatrix = this->document->currentGrid();
+}
--- a/src/tools/circletool.h	Mon May 16 01:40:49 2022 +0300
+++ b/src/tools/circletool.h	Tue May 24 16:11:10 2022 +0300
@@ -10,5 +10,9 @@
 	QString toolTip() const override;
 	void overpaint(Canvas *canvas, QPainter *painter) const override;
 	QString iconName() const override;
+	void addPoint(const glm::vec3& pos) override;
 	void closeShape() override;
+	void reset() override;
+private:
+	glm::mat4 baseGridMatrix;
 };
--- a/src/ui/canvas.cpp	Mon May 16 01:40:49 2022 +0300
+++ b/src/ui/canvas.cpp	Tue May 24 16:11:10 2022 +0300
@@ -320,6 +320,7 @@
 	};
 	this->gridPlane = geom::planeFromTriangle(triangle);
 	this->gridProgram->setGridMatrix(this->gridMatrix);
+	this->update();
 }
 
 /**
--- a/src/ui/canvas.h	Mon May 16 01:40:49 2022 +0300
+++ b/src/ui/canvas.h	Tue May 24 16:11:10 2022 +0300
@@ -24,13 +24,14 @@
 	void drawWorldPolyline(QPainter* painter, const std::vector<glm::vec3>& points);
 	void drawWorldPolygon(QPainter* painter, const std::vector<glm::vec3>& points);
 	Winding worldPolygonWinding(const std::vector<glm::vec3>& points) const;
-	const std::optional<glm::vec3>& getWorldPosition() const;
+        const std::optional<glm::vec3>& getWorldPosition() const;
 	void adjustGridToView();
 	const QSet<ldraw::id_t> selectedObjects() const;
 	const glm::mat4& getGridMatrix() const;
 public Q_SLOTS:
 	void handleSelectionChange(const QSet<ldraw::id_t>& selectedIds, const QSet<ldraw::id_t>& deselectedIds);
 	void rebuildVertices(Document *document);
+        void setGridMatrix(const glm::mat4 &newMatrix);
 protected:
 	void mouseMoveEvent(QMouseEvent* event) override;
 	void mousePressEvent(QMouseEvent* event) override;
@@ -43,7 +44,6 @@
 	void mouseClick(Canvas* canvas, QMouseEvent* event);
 	void mouseMove(Canvas* canvas, QMouseEvent* event);
 private:
-	void setGridMatrix(const glm::mat4 &newMatrix);
 	glm::vec3 cameraVector() const;
 	bool isGridPerpendicularToScreen(float threshold) const;
 	QVector<QPointF> convertWorldPointsToScreenPoints(const std::vector<glm::vec3>& worldPoints) const;

mercurial