--- a/src/tools/drawtool.cpp Sun Jul 25 16:29:08 2021 +0300 +++ b/src/tools/drawtool.cpp Sun Jul 25 20:29:14 2021 +0300 @@ -1,5 +1,10 @@ #include <QMessageBox> +#include <document.h> +#include "linetypes/edge.h" +#include "linetypes/triangle.h" +#include "linetypes/quadrilateral.h" #include "drawtool.h" +#include "modeleditcontext.h" static const QBrush pointBrush = {Qt::white}; static const QPen polygonPen = {QBrush{Qt::black}, 2.0, Qt::DashLine}; @@ -21,22 +26,23 @@ return result; } -bool DrawTool::mouseClick(const Canvas::MouseClickInfo& info) +bool DrawTool::mouseClick(Document* document, Canvas* canvas) { - if (info.worldPosition.has_value()) + const auto& worldPosition = canvas->getWorldPosition(); + if (worldPosition.has_value()) { - const glm::vec3& pos = info.worldPosition.value(); + const glm::vec3& pos = worldPosition.value(); const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);}; if (any(this->polygon, isCloseToPos)) { - this->closeShape(); + this->closeShape(document); } else { this->polygon.push_back(pos); if (this->polygon.size() == 4) { - this->closeShape(); + this->closeShape(document); } } } @@ -44,11 +50,13 @@ return true; } -bool DrawTool::mouseMove(const Canvas::MouseMoveInfo& info) +bool DrawTool::mouseMove(Document* document, Canvas* canvas) { - if (info.worldPosition.has_value()) + static_cast<void>(document); + const auto& worldPosition = canvas->getWorldPosition(); + if (worldPosition.has_value()) { - this->previewPoint = info.worldPosition.value(); + this->previewPoint = worldPosition.value(); if (this->polygon.size() < 4) { this->previewPolygon.resize(this->polygon.size() + 1); @@ -80,7 +88,34 @@ } } -void DrawTool::closeShape() +template<std::size_t N, typename T> +std::array<T, N> vectorToArray(const std::vector<T>& x) +{ + std::array<T, N> result; + for (std::size_t i = 0; i < x.size() and i < N; i += 1) + { + result[i] = x[i]; + } + return result; +} + +void DrawTool::closeShape(Document* document) { - QMessageBox::information(nullptr, "test", "close the polygon"); + if (this->polygon.size() >= 2 and this->polygon.size() <= 4) + { + Model::EditContext edit = document->editModel(); + switch (this->polygon.size()) + { + case 2: + edit.append<ldraw::Edge>(vectorToArray<2>(this->polygon), ldraw::edgeColor); + break; + case 3: + edit.append<ldraw::Triangle>(vectorToArray<3>(this->polygon), ldraw::mainColor); + break; + case 4: + edit.append<ldraw::Quadrilateral>(vectorToArray<4>(this->polygon), ldraw::mainColor); + break; + } + } + this->polygon.clear(); }