Sun, 29 Aug 2021 22:14:42 +0300
added a simple matrix transformation tool
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 | |
126
a7c720aff97c
moved ObjectEditor under SelectTool
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
15 | DrawTool::DrawTool(Model *model, QObject *parent) : |
a7c720aff97c
moved ObjectEditor under SelectTool
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
16 | BaseTool{model, parent} {} |
96 | 17 | |
18 | QString DrawTool::name() const | |
19 | { | |
20 | static const QString result = tr("Draw"); | |
21 | return result; | |
22 | } | |
23 | ||
24 | QString DrawTool::toolTip() const | |
25 | { | |
26 | static const QString result = tr("Draw new elements into the model."); | |
27 | return result; | |
28 | } | |
103 | 29 | |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
30 | bool DrawTool::mouseClick(Document* document, Canvas* canvas, QMouseEvent* event) |
103 | 31 | { |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
32 | if (event->button() == Qt::LeftButton) |
106 | 33 | { |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
34 | const auto& worldPosition = canvas->getWorldPosition(); |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
35 | if (worldPosition.has_value()) |
106 | 36 | { |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
37 | const glm::vec3& pos = worldPosition.value(); |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
38 | 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
|
39 | if (any(this->polygon, isCloseToPos)) |
106 | 40 | { |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
41 | this->closeShape(document); |
106 | 42 | } |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
43 | else |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
44 | { |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
45 | this->polygon.push_back(pos); |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
46 | if (this->polygon.size() == 4) |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
47 | { |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
48 | this->closeShape(document); |
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 | } |
106 | 51 | } |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
52 | this->previewPolygon = this->polygon; |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
53 | return true; |
106 | 54 | } |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
55 | 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
|
56 | { |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
57 | this->polygon.erase(this->polygon.end() - 1); |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
58 | this->updatePreviewPolygon(); |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
59 | return true; |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
60 | } |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
61 | else |
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 | return false; |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
64 | } |
103 | 65 | } |
106 | 66 | |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
67 | bool DrawTool::mouseMove(Document* document, Canvas* canvas, QMouseEvent *event) |
108 | 68 | { |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
69 | static_cast<void>(document); |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
70 | static_cast<void>(event); |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
71 | const auto& worldPosition = canvas->getWorldPosition(); |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
72 | if (worldPosition.has_value()) |
108 | 73 | { |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
74 | this->previewPoint = worldPosition.value(); |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
75 | if (this->polygon.size() < 4) |
108 | 76 | { |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
77 | this->updatePreviewPolygon(); |
108 | 78 | } |
79 | } | |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
80 | return false; |
108 | 81 | } |
82 | ||
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
83 | 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
|
84 | { |
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
85 | if (event->key() == Qt::Key_Escape) |
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 | this->polygon.clear(); |
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
88 | this->updatePreviewPolygon(); |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
89 | canvas->update(); |
124
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
90 | return true; |
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
91 | } |
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
92 | else |
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 | return false; |
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 | } |
f9f308c8e0c5
esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents:
123
diff
changeset
|
97 | |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
98 | void DrawTool::updatePreviewPolygon() |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
99 | { |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
100 | this->previewPolygon.resize(this->polygon.size() + 1); |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
101 | this->previewPolygon.back() = this->previewPoint; |
122
b54b350dff5d
Show concave polygons as red while drawing
Teemu Piippo <teemu@hecknology.net>
parents:
121
diff
changeset
|
102 | if (this->previewPolygon.size() > 2) |
b54b350dff5d
Show concave polygons as red while drawing
Teemu Piippo <teemu@hecknology.net>
parents:
121
diff
changeset
|
103 | { |
123 | 104 | 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
|
105 | } |
121
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
106 | } |
000781318c36
added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents:
111
diff
changeset
|
107 | |
106 | 108 | void DrawTool::reset() |
109 | { | |
110 | this->polygon.clear(); | |
111 | } | |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
112 | |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
113 | 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
|
114 | { |
122
b54b350dff5d
Show concave polygons as red while drawing
Teemu Piippo <teemu@hecknology.net>
parents:
121
diff
changeset
|
115 | 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
|
116 | painter->setPen(::polygonPen); |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
117 | canvas->drawWorldPolygon(painter, this->previewPolygon); |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
118 | painter->setBrush(::pointBrush); |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
119 | painter->setPen(::pointPen); |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
120 | 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
|
121 | { |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
122 | canvas->drawWorldPoint(painter, point); |
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 | if (this->polygon.size() < 4) |
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 | canvas->drawWorldPoint(painter, this->previewPoint); |
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 | } |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
129 | |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
130 | template<std::size_t N, typename T> |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
131 | 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
|
132 | { |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
133 | std::array<T, N> result; |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
134 | 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
|
135 | { |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
136 | result[i] = x[i]; |
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 | return result; |
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 | |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
141 | void DrawTool::closeShape(Document* document) |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
142 | { |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
143 | 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
|
144 | { |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
145 | Model::EditContext edit = document->editModel(); |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
146 | switch (this->polygon.size()) |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
147 | { |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
148 | case 2: |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
149 | edit.append<ldraw::Edge>(vectorToArray<2>(this->polygon), ldraw::edgeColor); |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
150 | break; |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
151 | case 3: |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
152 | edit.append<ldraw::Triangle>(vectorToArray<3>(this->polygon), ldraw::mainColor); |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
153 | break; |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
154 | case 4: |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
155 | edit.append<ldraw::Quadrilateral>(vectorToArray<4>(this->polygon), ldraw::mainColor); |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
156 | break; |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
157 | } |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
158 | } |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
159 | this->polygon.clear(); |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
160 | } |