--- a/src/tools/drawtool.cpp Sun Jul 25 13:49:37 2021 +0300 +++ b/src/tools/drawtool.cpp Sun Jul 25 16:26:38 2021 +0300 @@ -1,9 +1,10 @@ #include <QMessageBox> #include "drawtool.h" -static const QBrush brush = {Qt::white}; -static const QPen pen = {Qt::black}; -static const QBrush polygonBrush = {QColor{64, 255, 128}}; +static const QBrush pointBrush = {Qt::white}; +static const QPen polygonPen = {QBrush{Qt::black}, 2.0, Qt::DashLine}; +static const QPen pointPen = {QBrush{Qt::black}, 2.0}; +static const QBrush polygonBrush = {QColor{64, 255, 128, 192}}; DrawTool::DrawTool(QObject* parent) : BaseTool{parent} {} @@ -28,47 +29,58 @@ const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);}; if (any(this->polygon, isCloseToPos)) { - QMessageBox::information(nullptr, "test", "close the polygon"); + this->closeShape(); } else { this->polygon.push_back(pos); - auto& previewLayer = info.invoker->modifyPreviewLayer(Canvas::DrawToolPreview); - previewLayer.points.resize(this->polygon.size()); - previewLayer.points.back() = {pos, brush, pen}; - previewLayer.polygons.clear(); - previewLayer.polygons.push_back({geom::NPolygon{this->polygon}, polygonBrush, pen}); if (this->polygon.size() == 4) { - QMessageBox::information(nullptr, "test", "close the polygon"); + this->closeShape(); } } } + this->previewPolygon = this->polygon; return true; } bool DrawTool::mouseMove(const Canvas::MouseMoveInfo& info) { - if (this->polygon.size() < 4 and info.worldPosition.has_value()) + if (info.worldPosition.has_value()) { - auto& previewLayer = info.invoker->modifyPreviewLayer(Canvas::DrawToolPreview); - previewLayer.points.resize(this->polygon.size() + 1); - previewLayer.points.back() = {info.worldPosition.value(), brush, pen}; - if (previewLayer.polygons.size() > 0) + this->previewPoint = info.worldPosition.value(); + if (this->polygon.size() < 4) { - auto& polygon = previewLayer.polygons.back(); - polygon.geometry.points.resize(this->polygon.size() + 1); - polygon.geometry.points.back() = info.worldPosition.value(); + this->previewPolygon.resize(this->polygon.size() + 1); + this->previewPolygon.back() = this->previewPoint; } - return true; } - else - { - return false; - } + return false; } void DrawTool::reset() { this->polygon.clear(); } + +void DrawTool::overpaint(Canvas* canvas, QPainter* painter) const +{ + painter->setBrush(::polygonBrush); + painter->setPen(::polygonPen); + canvas->drawWorldPolygon(painter, this->previewPolygon); + painter->setBrush(::pointBrush); + painter->setPen(::pointPen); + for (const glm::vec3& point : this->polygon) + { + canvas->drawWorldPoint(painter, point); + } + if (this->polygon.size() < 4) + { + canvas->drawWorldPoint(painter, this->previewPoint); + } +} + +void DrawTool::closeShape() +{ + QMessageBox::information(nullptr, "test", "close the polygon"); +}