Sat, 05 Mar 2022 13:55:12 +0200
fixed the grid not being black on startup if settings has bright background color
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" |
153
2f79053c2e9a
Renamed modeleditcontext.cpp -> modeleditor.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
152
diff
changeset
|
7 | #include "modeleditor.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); |
164
8305e2f968fb
Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents:
163
diff
changeset
|
119 | if (this->previewPolygon.size() > 2) |
8305e2f968fb
Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents:
163
diff
changeset
|
120 | { |
8305e2f968fb
Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents:
163
diff
changeset
|
121 | canvas->drawWorldPolygon(painter, this->previewPolygon); |
8305e2f968fb
Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents:
163
diff
changeset
|
122 | } |
8305e2f968fb
Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents:
163
diff
changeset
|
123 | else |
8305e2f968fb
Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents:
163
diff
changeset
|
124 | { |
8305e2f968fb
Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents:
163
diff
changeset
|
125 | canvas->drawWorldPolyline(painter, this->previewPolygon); |
8305e2f968fb
Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents:
163
diff
changeset
|
126 | } |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
127 | painter->setBrush(::pointBrush); |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
128 | painter->setPen(::pointPen); |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
129 | 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
|
130 | { |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
131 | canvas->drawWorldPoint(painter, point); |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
132 | } |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
133 | if (this->polygon.size() < 4) |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
134 | { |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
135 | canvas->drawWorldPoint(painter, this->previewPoint); |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
136 | } |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
137 | } |
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
138 | |
163 | 139 | QString DrawTool::iconName() const |
140 | { | |
141 | return ":/icons/pencil-outline.png"; | |
142 | } | |
143 | ||
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
144 | template<std::size_t N, typename T> |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
145 | 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
|
146 | { |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
147 | std::array<T, N> result; |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
148 | 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
|
149 | { |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
150 | result[i] = x[i]; |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
151 | } |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
152 | return result; |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
153 | } |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
154 | |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
155 | void DrawTool::closeShape(Document* document) |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
156 | { |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
157 | 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
|
158 | { |
152 | 159 | std::unique_ptr<ModelEditor> modelEditor = document->editModel(); |
111
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
160 | switch (this->polygon.size()) |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
161 | { |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
162 | case 2: |
152 | 163 | 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
|
164 | break; |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
165 | case 3: |
152 | 166 | 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
|
167 | break; |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
168 | case 4: |
152 | 169 | 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
|
170 | break; |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
171 | } |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
172 | } |
1f42c03fafca
Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents:
109
diff
changeset
|
173 | this->polygon.clear(); |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
174 | } |