Render draw tool preview as a line when we only have 2 points

Sat, 05 Mar 2022 13:38:22 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 05 Mar 2022 13:38:22 +0200
changeset 164
8305e2f968fb
parent 163
36ea1a8aee33
child 165
f6eab2bd46c2

Render draw tool preview as a line when we only have 2 points

src/tools/drawtool.cpp 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/tools/drawtool.cpp	Sat Mar 05 13:32:58 2022 +0200
+++ b/src/tools/drawtool.cpp	Sat Mar 05 13:38:22 2022 +0200
@@ -116,7 +116,14 @@
 {
 	painter->setBrush(this->isconcave ? ::badPolygonBrush : ::polygonBrush);
 	painter->setPen(::polygonPen);
-	canvas->drawWorldPolygon(painter, this->previewPolygon);
+	if (this->previewPolygon.size() > 2)
+	{
+		canvas->drawWorldPolygon(painter, this->previewPolygon);
+	}
+	else
+	{
+		canvas->drawWorldPolyline(painter, this->previewPolygon);
+	}
 	painter->setBrush(::pointBrush);
 	painter->setPen(::pointPen);
 	for (const glm::vec3& point : this->polygon)
--- a/src/ui/canvas.cpp	Sat Mar 05 13:32:58 2022 +0200
+++ b/src/ui/canvas.cpp	Sat Mar 05 13:38:22 2022 +0200
@@ -228,19 +228,23 @@
 }
 
 /**
+ * @brief Draws a polyline to where the specified vector of 3D points would appear on the screen.
+ * @param painter Painter to use to draw with
+ * @param points 3D points to render
+ */
+void Canvas::drawWorldPolyline(QPainter *painter, const std::vector<glm::vec3> &points)
+{
+	painter->drawPolyline(QPolygonF{this->convertWorldPointsToScreenPoints(points)});
+}
+
+/**
  * @brief Draws a polygon to where the specified vector of 3D points would appear on the screen.
  * @param painter Painter to use to draw with
  * @param points 3D points to render
  */
 void Canvas::drawWorldPolygon(QPainter* painter, const std::vector<glm::vec3> &points)
 {
-	QVector<QPointF> points2d;
-	points2d.reserve(points.size());
-	for (const glm::vec3& point : points)
-	{
-		points2d.push_back(this->modelToScreenCoordinates(point));
-	}
-	painter->drawPolygon(QPolygonF{points2d});
+	painter->drawPolygon(QPolygonF{this->convertWorldPointsToScreenPoints(points)});
 }
 
 /**
@@ -329,6 +333,17 @@
 	return std::abs(dot) < threshold;
 }
 
+QVector<QPointF> Canvas::convertWorldPointsToScreenPoints(const std::vector<glm::vec3> &worldPoints)
+{
+	QVector<QPointF> points2d;
+	points2d.reserve(worldPoints.size());
+	for (const glm::vec3& point : worldPoints)
+	{
+		points2d.push_back(this->modelToScreenCoordinates(point));
+	}
+	return points2d;
+}
+
 /**
  * @brief Clears the selection.
  */
--- a/src/ui/canvas.h	Sat Mar 05 13:32:58 2022 +0200
+++ b/src/ui/canvas.h	Sat Mar 05 13:38:22 2022 +0200
@@ -21,6 +21,7 @@
 	void addToSelection(ldraw::id_t id);
 	void setOverpaintCallback(OverpaintCallback fn);
 	void drawWorldPoint(QPainter* painter, const glm::vec3& worldPoint) const;
+	void drawWorldPolyline(QPainter* painter, const std::vector<glm::vec3>& points);
 	void drawWorldPolygon(QPainter* painter, const std::vector<glm::vec3>& points);
 	const std::optional<glm::vec3>& getWorldPosition() const;
 	void adjustGridToView();
@@ -42,6 +43,7 @@
 	void setGridMatrix(const glm::mat4 &newMatrix);
 	glm::vec3 cameraVector() const;
 	bool isGridPerpendicularToScreen(float threshold) const;
+	QVector<QPointF> convertWorldPointsToScreenPoints(const std::vector<glm::vec3>& worldPoints);
 	std::optional<GridProgram> gridProgram;
 	std::optional<AxesProgram> axesProgram;
 	std::optional<VertexProgram> vertexProgram;

mercurial