Sun, 25 Jul 2021 13:49:37 +0300
work on editing tools
103 | 1 | #include <QMessageBox> |
96 | 2 | #include "drawtool.h" |
3 | ||
108 | 4 | static const QBrush brush = {Qt::white}; |
5 | static const QPen pen = {Qt::black}; | |
6 | static const QBrush polygonBrush = {QColor{64, 255, 128}}; | |
7 | ||
96 | 8 | DrawTool::DrawTool(QObject* parent) : |
9 | BaseTool{parent} {} | |
10 | ||
11 | QString DrawTool::name() const | |
12 | { | |
13 | static const QString result = tr("Draw"); | |
14 | return result; | |
15 | } | |
16 | ||
17 | QString DrawTool::toolTip() const | |
18 | { | |
19 | static const QString result = tr("Draw new elements into the model."); | |
20 | return result; | |
21 | } | |
103 | 22 | |
104 | 23 | bool DrawTool::mouseClick(const Canvas::MouseClickInfo& info) |
103 | 24 | { |
106 | 25 | if (info.worldPosition.has_value()) |
26 | { | |
27 | const glm::vec3& pos = info.worldPosition.value(); | |
28 | const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);}; | |
29 | if (any(this->polygon, isCloseToPos)) | |
30 | { | |
31 | QMessageBox::information(nullptr, "test", "close the polygon"); | |
32 | } | |
33 | else | |
34 | { | |
35 | this->polygon.push_back(pos); | |
108 | 36 | auto& previewLayer = info.invoker->modifyPreviewLayer(Canvas::DrawToolPreview); |
37 | previewLayer.points.resize(this->polygon.size()); | |
38 | previewLayer.points.back() = {pos, brush, pen}; | |
39 | previewLayer.polygons.clear(); | |
40 | previewLayer.polygons.push_back({geom::NPolygon{this->polygon}, polygonBrush, pen}); | |
106 | 41 | if (this->polygon.size() == 4) |
42 | { | |
43 | QMessageBox::information(nullptr, "test", "close the polygon"); | |
44 | } | |
45 | } | |
46 | } | |
104 | 47 | return true; |
103 | 48 | } |
106 | 49 | |
108 | 50 | bool DrawTool::mouseMove(const Canvas::MouseMoveInfo& info) |
51 | { | |
52 | if (this->polygon.size() < 4 and info.worldPosition.has_value()) | |
53 | { | |
54 | auto& previewLayer = info.invoker->modifyPreviewLayer(Canvas::DrawToolPreview); | |
55 | previewLayer.points.resize(this->polygon.size() + 1); | |
56 | previewLayer.points.back() = {info.worldPosition.value(), brush, pen}; | |
57 | if (previewLayer.polygons.size() > 0) | |
58 | { | |
59 | auto& polygon = previewLayer.polygons.back(); | |
60 | polygon.geometry.points.resize(this->polygon.size() + 1); | |
61 | polygon.geometry.points.back() = info.worldPosition.value(); | |
62 | } | |
63 | return true; | |
64 | } | |
65 | else | |
66 | { | |
67 | return false; | |
68 | } | |
69 | } | |
70 | ||
106 | 71 | void DrawTool::reset() |
72 | { | |
73 | this->polygon.clear(); | |
74 | } |