# HG changeset patch # User Teemu Piippo # Date 1626733321 -10800 # Node ID 128efb9d148be5961d23bbd2b48d4085574230d1 # Parent 6ca6e8c647d4f1e06c345f3e1ffcdd77d6632b10 work on draw preview diff -r 6ca6e8c647d4 -r 128efb9d148b src/functional.h --- a/src/functional.h Mon Jul 19 23:41:52 2021 +0300 +++ b/src/functional.h Tue Jul 20 01:22:01 2021 +0300 @@ -157,3 +157,29 @@ return result; } } + +template +bool any(T&& container, Fn&& f) +{ + for (auto&& x : container) + { + if (f(x)) + { + return true; + } + } + return false; +} + +template +bool all(T&& container, Fn&& f) +{ + for (auto&& x : container) + { + if (not f(x)) + { + return false; + } + } + return true; +} diff -r 6ca6e8c647d4 -r 128efb9d148b src/geometry.h --- a/src/geometry.h Mon Jul 19 23:41:52 2021 +0300 +++ b/src/geometry.h Tue Jul 20 01:22:01 2021 +0300 @@ -102,4 +102,11 @@ { std::vector points; }; + + inline bool isclose(const glm::vec3& a, const glm::vec3& b) + { + return qFuzzyCompare(a.x, b.x) + and qFuzzyCompare(a.y, b.y) + and qFuzzyCompare(a.z, b.z); + } } diff -r 6ca6e8c647d4 -r 128efb9d148b src/mainwindow.cpp --- a/src/mainwindow.cpp Mon Jul 19 23:41:52 2021 +0300 +++ b/src/mainwindow.cpp Tue Jul 20 01:22:01 2021 +0300 @@ -399,6 +399,10 @@ { if (tool != nullptr && tool != this->selectedTool) { + if (this->selectedTool != nullptr) + { + this->selectedTool->reset(); + } this->selectedTool = tool; for (auto&& pair : items(this->toolActions)) { diff -r 6ca6e8c647d4 -r 128efb9d148b src/tools/basetool.h --- a/src/tools/basetool.h Mon Jul 19 23:41:52 2021 +0300 +++ b/src/tools/basetool.h Tue Jul 20 01:22:01 2021 +0300 @@ -17,5 +17,6 @@ virtual bool mouseDoubleClicked(QMouseEvent*) { return false; } virtual bool mouseMoved(QMouseEvent*) { return false; } virtual bool keyReleased(QKeyEvent*) { return false; } + virtual void reset() {} }; diff -r 6ca6e8c647d4 -r 128efb9d148b src/tools/drawtool.cpp --- a/src/tools/drawtool.cpp Mon Jul 19 23:41:52 2021 +0300 +++ b/src/tools/drawtool.cpp Tue Jul 20 01:22:01 2021 +0300 @@ -18,7 +18,30 @@ bool DrawTool::mouseClick(const Canvas::MouseClickInfo& info) { - static_cast(info); - QMessageBox::information(nullptr, "hleelo", "it works"); + if (info.worldPosition.has_value()) + { + const glm::vec3& pos = info.worldPosition.value(); + const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);}; + if (any(this->polygon, isCloseToPos)) + { + QMessageBox::information(nullptr, "test", "close the polygon"); + } + else + { + this->polygon.push_back(pos); + auto& previewLayer = info.invoker->modifyPreviewLayer(Canvas::DrawToolPreview).polygons; + previewLayer.clear(); + previewLayer.push_back({this->polygon}); + if (this->polygon.size() == 4) + { + QMessageBox::information(nullptr, "test", "close the polygon"); + } + } + } return true; } + +void DrawTool::reset() +{ + this->polygon.clear(); +} diff -r 6ca6e8c647d4 -r 128efb9d148b src/tools/drawtool.h --- a/src/tools/drawtool.h Mon Jul 19 23:41:52 2021 +0300 +++ b/src/tools/drawtool.h Tue Jul 20 01:22:01 2021 +0300 @@ -11,4 +11,7 @@ QString name() const override; QString toolTip() const override; bool mouseClick(const Canvas::MouseClickInfo& info) override; + void reset() override; +private: + std::vector polygon; }; diff -r 6ca6e8c647d4 -r 128efb9d148b src/ui/canvas.cpp --- a/src/ui/canvas.cpp Mon Jul 19 23:41:52 2021 +0300 +++ b/src/ui/canvas.cpp Tue Jul 20 01:22:01 2021 +0300 @@ -89,9 +89,6 @@ void Canvas::mouseReleaseEvent(QMouseEvent* event) { - MouseClickInfo info; - info.click = this->totalMouseMove < (2.0 / sqrt(2)) * 5.0; - emit mouseClick(info); if (this->totalMouseMove < (2.0 / sqrt(2)) * 5.0) { if (this->highlighted == ldraw::NULL_ID) @@ -105,6 +102,10 @@ this->compiler->setSelectedObjects(this->selection); emit selectionChanged(this->selection); this->update(); + MouseClickInfo info; + info.worldPosition = this->worldPosition; + info.invoker = this; + emit mouseClick(info); } PartRenderer::mouseReleaseEvent(event); } diff -r 6ca6e8c647d4 -r 128efb9d148b src/ui/canvas.h --- a/src/ui/canvas.h Mon Jul 19 23:41:52 2021 +0300 +++ b/src/ui/canvas.h Tue Jul 20 01:22:01 2021 +0300 @@ -14,7 +14,7 @@ }; struct PreviewLayer { - QSet polygons; + QVector polygons; QColor color{64, 255, 128}; }; static constexpr int NUM_PREVIEW_LAYERS = 1; @@ -54,5 +54,6 @@ struct Canvas::MouseClickInfo { - bool click; + std::optional worldPosition; + Canvas* invoker; };