Tue, 28 Sep 2021 00:10:29 +0300
Saving works now
#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}; static const QPen pointPen = {QBrush{Qt::black}, 2.0}; static const QBrush polygonBrush = {QColor{64, 255, 128, 192}}; static const QBrush badPolygonBrush = {QColor{255, 96, 96, 192}}; DrawTool::DrawTool(Model *model, QObject *parent) : BaseTool{model, parent} {} QString DrawTool::name() const { static const QString result = tr("Draw"); return result; } QString DrawTool::toolTip() const { static const QString result = tr("Draw new elements into the model."); return result; } bool DrawTool::mouseClick(Document* document, Canvas* canvas, QMouseEvent* event) { if (event->button() == Qt::LeftButton) { const auto& worldPosition = canvas->getWorldPosition(); if (worldPosition.has_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(document); } else { this->polygon.push_back(pos); if (this->polygon.size() == 4) { this->closeShape(document); } } } this->previewPolygon = this->polygon; return true; } else if (event->button() == Qt::RightButton and this->polygon.size() > 0) { this->polygon.erase(this->polygon.end() - 1); this->updatePreviewPolygon(); return true; } else { return false; } } bool DrawTool::mouseMove(Document* document, Canvas* canvas, QMouseEvent *event) { static_cast<void>(document); static_cast<void>(event); const auto& worldPosition = canvas->getWorldPosition(); if (worldPosition.has_value()) { this->previewPoint = worldPosition.value(); if (this->polygon.size() < 4) { this->updatePreviewPolygon(); } } return false; } bool DrawTool::keyReleased(Document*, Canvas* canvas, QKeyEvent* event) { if (event->key() == Qt::Key_Escape) { this->polygon.clear(); this->updatePreviewPolygon(); canvas->update(); return true; } else { return false; } } void DrawTool::updatePreviewPolygon() { this->previewPolygon.resize(this->polygon.size() + 1); this->previewPolygon.back() = this->previewPoint; if (this->previewPolygon.size() > 2) { this->isconcave = not geom::isConvex(this->previewPolygon); } } void DrawTool::reset() { this->polygon.clear(); } void DrawTool::overpaint(Canvas* canvas, QPainter* painter) const { painter->setBrush(this->isconcave ? ::badPolygonBrush : ::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); } } 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) { 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::EDGE_COLOR); break; case 3: edit.append<ldraw::Triangle>(vectorToArray<3>(this->polygon), ldraw::MAIN_COLOR); break; case 4: edit.append<ldraw::Quadrilateral>(vectorToArray<4>(this->polygon), ldraw::MAIN_COLOR); break; } } this->polygon.clear(); }