1 #include <QMessageBox> |
1 #include <QMessageBox> |
|
2 #include <document.h> |
|
3 #include "linetypes/edge.h" |
|
4 #include "linetypes/triangle.h" |
|
5 #include "linetypes/quadrilateral.h" |
2 #include "drawtool.h" |
6 #include "drawtool.h" |
|
7 #include "modeleditcontext.h" |
3 |
8 |
4 static const QBrush pointBrush = {Qt::white}; |
9 static const QBrush pointBrush = {Qt::white}; |
5 static const QPen polygonPen = {QBrush{Qt::black}, 2.0, Qt::DashLine}; |
10 static const QPen polygonPen = {QBrush{Qt::black}, 2.0, Qt::DashLine}; |
6 static const QPen pointPen = {QBrush{Qt::black}, 2.0}; |
11 static const QPen pointPen = {QBrush{Qt::black}, 2.0}; |
7 static const QBrush polygonBrush = {QColor{64, 255, 128, 192}}; |
12 static const QBrush polygonBrush = {QColor{64, 255, 128, 192}}; |
19 { |
24 { |
20 static const QString result = tr("Draw new elements into the model."); |
25 static const QString result = tr("Draw new elements into the model."); |
21 return result; |
26 return result; |
22 } |
27 } |
23 |
28 |
24 bool DrawTool::mouseClick(const Canvas::MouseClickInfo& info) |
29 bool DrawTool::mouseClick(Document* document, Canvas* canvas) |
25 { |
30 { |
26 if (info.worldPosition.has_value()) |
31 const auto& worldPosition = canvas->getWorldPosition(); |
|
32 if (worldPosition.has_value()) |
27 { |
33 { |
28 const glm::vec3& pos = info.worldPosition.value(); |
34 const glm::vec3& pos = worldPosition.value(); |
29 const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);}; |
35 const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);}; |
30 if (any(this->polygon, isCloseToPos)) |
36 if (any(this->polygon, isCloseToPos)) |
31 { |
37 { |
32 this->closeShape(); |
38 this->closeShape(document); |
33 } |
39 } |
34 else |
40 else |
35 { |
41 { |
36 this->polygon.push_back(pos); |
42 this->polygon.push_back(pos); |
37 if (this->polygon.size() == 4) |
43 if (this->polygon.size() == 4) |
38 { |
44 { |
39 this->closeShape(); |
45 this->closeShape(document); |
40 } |
46 } |
41 } |
47 } |
42 } |
48 } |
43 this->previewPolygon = this->polygon; |
49 this->previewPolygon = this->polygon; |
44 return true; |
50 return true; |
45 } |
51 } |
46 |
52 |
47 bool DrawTool::mouseMove(const Canvas::MouseMoveInfo& info) |
53 bool DrawTool::mouseMove(Document* document, Canvas* canvas) |
48 { |
54 { |
49 if (info.worldPosition.has_value()) |
55 static_cast<void>(document); |
|
56 const auto& worldPosition = canvas->getWorldPosition(); |
|
57 if (worldPosition.has_value()) |
50 { |
58 { |
51 this->previewPoint = info.worldPosition.value(); |
59 this->previewPoint = worldPosition.value(); |
52 if (this->polygon.size() < 4) |
60 if (this->polygon.size() < 4) |
53 { |
61 { |
54 this->previewPolygon.resize(this->polygon.size() + 1); |
62 this->previewPolygon.resize(this->polygon.size() + 1); |
55 this->previewPolygon.back() = this->previewPoint; |
63 this->previewPolygon.back() = this->previewPoint; |
56 } |
64 } |
78 { |
86 { |
79 canvas->drawWorldPoint(painter, this->previewPoint); |
87 canvas->drawWorldPoint(painter, this->previewPoint); |
80 } |
88 } |
81 } |
89 } |
82 |
90 |
83 void DrawTool::closeShape() |
91 template<std::size_t N, typename T> |
|
92 std::array<T, N> vectorToArray(const std::vector<T>& x) |
84 { |
93 { |
85 QMessageBox::information(nullptr, "test", "close the polygon"); |
94 std::array<T, N> result; |
|
95 for (std::size_t i = 0; i < x.size() and i < N; i += 1) |
|
96 { |
|
97 result[i] = x[i]; |
|
98 } |
|
99 return result; |
86 } |
100 } |
|
101 |
|
102 void DrawTool::closeShape(Document* document) |
|
103 { |
|
104 if (this->polygon.size() >= 2 and this->polygon.size() <= 4) |
|
105 { |
|
106 Model::EditContext edit = document->editModel(); |
|
107 switch (this->polygon.size()) |
|
108 { |
|
109 case 2: |
|
110 edit.append<ldraw::Edge>(vectorToArray<2>(this->polygon), ldraw::edgeColor); |
|
111 break; |
|
112 case 3: |
|
113 edit.append<ldraw::Triangle>(vectorToArray<3>(this->polygon), ldraw::mainColor); |
|
114 break; |
|
115 case 4: |
|
116 edit.append<ldraw::Quadrilateral>(vectorToArray<4>(this->polygon), ldraw::mainColor); |
|
117 break; |
|
118 } |
|
119 } |
|
120 this->polygon.clear(); |
|
121 } |