# HG changeset patch # User Teemu Piippo # Date 1646480302 -7200 # Node ID 8305e2f968fb045eb022843ea55280d51460edb6 # Parent 36ea1a8aee33ba917bf6526b9b3f35b640505eb3 Render draw tool preview as a line when we only have 2 points diff -r 36ea1a8aee33 -r 8305e2f968fb src/tools/drawtool.cpp --- 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) diff -r 36ea1a8aee33 -r 8305e2f968fb src/ui/canvas.cpp --- 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 &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 &points) { - QVector 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 Canvas::convertWorldPointsToScreenPoints(const std::vector &worldPoints) +{ + QVector points2d; + points2d.reserve(worldPoints.size()); + for (const glm::vec3& point : worldPoints) + { + points2d.push_back(this->modelToScreenCoordinates(point)); + } + return points2d; +} + /** * @brief Clears the selection. */ diff -r 36ea1a8aee33 -r 8305e2f968fb src/ui/canvas.h --- 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& points); void drawWorldPolygon(QPainter* painter, const std::vector& points); const std::optional& getWorldPosition() const; void adjustGridToView(); @@ -42,6 +43,7 @@ void setGridMatrix(const glm::mat4 &newMatrix); glm::vec3 cameraVector() const; bool isGridPerpendicularToScreen(float threshold) const; + QVector convertWorldPointsToScreenPoints(const std::vector& worldPoints); std::optional gridProgram; std::optional axesProgram; std::optional vertexProgram;