Fri, 04 Mar 2022 11:37:50 +0200
Major refactoring
- Model now just stores objects
- Document contains business logic
- Model::EditContext is now ModelEditor, no longer a nested class
103 | 1 | #include <QMessageBox> |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
2 | #include <document.h> |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
3 | #include "linetypes/edge.h" |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
4 | #include "linetypes/triangle.h" |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
5 | #include "linetypes/quadrilateral.h" |
96 | 6 | #include "drawtool.h" |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
7 | #include "modeleditcontext.h" |
96 | 8 | |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
9 | static const QBrush pointBrush = {Qt::white}; |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
10 | static const QPen polygonPen = {QBrush{Qt::black}, 2.0, Qt::DashLine}; |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
11 | static const QPen pointPen = {QBrush{Qt::black}, 2.0}; |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
12 | static const QBrush polygonBrush = {QColor{64, 255, 128, 192}}; |
122
b54b350dff5d
Show concave polygons as red while drawing
Teemu Piippo <teemu@hecknology.net>
parents:
121
diff
changeset
|
13 | static const QBrush badPolygonBrush = {QColor{255, 96, 96, 192}}; |
108 | 14 | |
152 | 15 | DrawTool::DrawTool(Document* document) : |
16 | BaseTool{document} | |
17 | { | |
18 | } | |
96 | 19 | |
20 | QString DrawTool::name() const | |
21 | { | |
22 | static const QString result = tr("Draw"); | |
23 | return result; | |
24 | } | |
25 | ||
26 | QString DrawTool::toolTip() const | |
27 | { | |
28 | static const QString result = tr("Draw new elements into the model."); | |
29 | return result; | |
30 | } | |
103 | 31 | |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
32 | bool DrawTool::mouseClick(Document* document, Canvas* canvas, QMouseEvent* event) |
103 | 33 | { |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
34 | if (event->button() == Qt::LeftButton) |
106 | 35 | { |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
36 | const auto& worldPosition = canvas->getWorldPosition(); |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
37 | if (worldPosition.has_value()) |
106 | 38 | { |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
39 | const glm::vec3& pos = worldPosition.value(); |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
40 | const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);}; |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
41 | if (any(this->polygon, isCloseToPos)) |
106 | 42 | { |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
43 | this->closeShape(document); |
106 | 44 | } |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
45 | else |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
46 | { |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
47 | this->polygon.push_back(pos); |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
48 | if (this->polygon.size() == 4) |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
49 | { |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
50 | this->closeShape(document); |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
51 | } |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
52 | } |
106 | 53 | } |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
54 | this->previewPolygon = this->polygon; |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
55 | return true; |
106 | 56 | } |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
57 | else if (event->button() == Qt::RightButton and this->polygon.size() > 0) |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
58 | { |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
59 | this->polygon.erase(this->polygon.end() - 1); |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
60 | this->updatePreviewPolygon(); |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
61 | return true; |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
62 | } |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
63 | else |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
64 | { |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
65 | return false; |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
66 | } |
103 | 67 | } |
106 | 68 | |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
69 | bool DrawTool::mouseMove(Document* document, Canvas* canvas, QMouseEvent *event) |
108 | 70 | { |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
71 | static_cast<void>(document); |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
72 | static_cast<void>(event); |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
73 | const auto& worldPosition = canvas->getWorldPosition(); |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
74 | if (worldPosition.has_value()) |
108 | 75 | { |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
76 | this->previewPoint = worldPosition.value(); |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
77 | if (this->polygon.size() < 4) |
108 | 78 | { |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
79 | this->updatePreviewPolygon(); |
108 | 80 | } |
81 | } | |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
82 | return false; |
108 | 83 | } |
84 | ||
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
85 | bool DrawTool::keyReleased(Document*, Canvas* canvas, QKeyEvent* event) |
124
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
86 | { |
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
87 | if (event->key() == Qt::Key_Escape) |
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
88 | { |
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
89 | this->polygon.clear(); |
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
90 | this->updatePreviewPolygon(); |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
91 | canvas->update(); |
124
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
92 | return true; |
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
93 | } |
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
94 | else |
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
95 | { |
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
96 | return false; |
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
97 | } |
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
98 | } |
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
99 | |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
100 | void DrawTool::updatePreviewPolygon() |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
101 | { |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
102 | this->previewPolygon.resize(this->polygon.size() + 1); |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
103 | this->previewPolygon.back() = this->previewPoint; |
122
b54b350dff5d
Show concave polygons as red while drawing
Teemu Piippo <teemu@hecknology.net>
parents:
121
diff
changeset
|
104 | if (this->previewPolygon.size() > 2) |
b54b350dff5d
Show concave polygons as red while drawing
Teemu Piippo <teemu@hecknology.net>
parents:
121
diff
changeset
|
105 | { |
123 | 106 | this->isconcave = not geom::isConvex(this->previewPolygon); |
122
b54b350dff5d
Show concave polygons as red while drawing
Teemu Piippo <teemu@hecknology.net>
parents:
121
diff
changeset
|
107 | } |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
108 | } |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
109 | |
106 | 110 | void DrawTool::reset() |
111 | { | |
112 | this->polygon.clear(); | |
113 | } | |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
114 | |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
115 | void DrawTool::overpaint(Canvas* canvas, QPainter* painter) const |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
116 | { |
122
b54b350dff5d
Show concave polygons as red while drawing
Teemu Piippo <teemu@hecknology.net>
parents:
121
diff
changeset
|
117 | painter->setBrush(this->isconcave ? ::badPolygonBrush : ::polygonBrush); |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
118 | painter->setPen(::polygonPen); |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
119 | canvas->drawWorldPolygon(painter, this->previewPolygon); |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
120 | painter->setBrush(::pointBrush); |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
121 | painter->setPen(::pointPen); |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
122 | for (const glm::vec3& point : this->polygon) |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
123 | { |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
124 | canvas->drawWorldPoint(painter, point); |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
125 | } |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
126 | if (this->polygon.size() < 4) |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
127 | { |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
128 | canvas->drawWorldPoint(painter, this->previewPoint); |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
129 | } |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
130 | } |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
131 | |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
132 | template<std::size_t N, typename T> |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
133 | std::array<T, N> vectorToArray(const std::vector<T>& x) |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
134 | { |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
135 | std::array<T, N> result; |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
136 | for (std::size_t i = 0; i < x.size() and i < N; i += 1) |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
137 | { |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
138 | result[i] = x[i]; |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
139 | } |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
140 | return result; |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
141 | } |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
142 | |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
143 | void DrawTool::closeShape(Document* document) |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
144 | { |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
145 | if (this->polygon.size() >= 2 and this->polygon.size() <= 4) |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
146 | { |
152 | 147 | std::unique_ptr<ModelEditor> modelEditor = document->editModel(); |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
148 | switch (this->polygon.size()) |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
149 | { |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
150 | case 2: |
152 | 151 | modelEditor->append<ldraw::Edge>(vectorToArray<2>(this->polygon), ldraw::EDGE_COLOR); |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
152 | break; |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
153 | case 3: |
152 | 154 | modelEditor->append<ldraw::Triangle>(vectorToArray<3>(this->polygon), ldraw::MAIN_COLOR); |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
155 | break; |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
156 | case 4: |
152 | 157 | modelEditor->append<ldraw::Quadrilateral>(vectorToArray<4>(this->polygon), ldraw::MAIN_COLOR); |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
158 | break; |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
159 | } |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
160 | } |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
161 | this->polygon.clear(); |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
162 | } |