src/tools/drawtool.cpp

Mon, 16 May 2022 01:40:49 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Mon, 16 May 2022 01:40:49 +0300
changeset 187
30204975694a
parent 185
a38a0eb007b0
permissions
-rw-r--r--

work on circle tool

103
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
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
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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};
168
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
11 static const QPen badPolygonPen = {QBrush{Qt::red}, 2.0, Qt::DashLine};
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
12 static const QPen pointPen = {QBrush{Qt::black}, 2.0};
168
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
13 static const QBrush greenPolygonBrush = {QColor{64, 255, 128, 192}};
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
14 static const QBrush redPolygonBrush = {QColor{255, 96, 96, 192}};
108
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
15
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
16 AbstractDrawTool::AbstractDrawTool(Document *document) :
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
17 BaseTool{document}
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
18 {
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
19 }
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
20
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
21 DrawTool::DrawTool(Document* document) :
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
22 AbstractDrawTool{document}
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
23 {
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
24 }
96
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 QString DrawTool::name() const
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 {
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 static const QString result = tr("Draw");
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 return result;
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 }
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 QString DrawTool::toolTip() const
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 {
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 static const QString result = tr("Draw new elements into the model.");
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 return result;
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 }
103
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
37
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
38 bool AbstractDrawTool::mouseClick(Canvas* canvas, QMouseEvent* event)
103
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
39 {
121
000781318c36 added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
40 if (event->button() == Qt::LeftButton)
106
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
41 {
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
42 this->addCurrentPoint(canvas);
121
000781318c36 added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
43 return true;
106
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
44 }
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
45 else if (event->button() == Qt::RightButton)
121
000781318c36 added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
46 {
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
47 this->removeLastPoint();
121
000781318c36 added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
48 return true;
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 else
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 return false;
000781318c36 added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
53 }
103
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
54 }
106
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
55
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
56 bool AbstractDrawTool::mouseMove(Document* document, Canvas* canvas, QMouseEvent *event)
108
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
57 {
111
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
58 static_cast<void>(document);
121
000781318c36 added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
59 static_cast<void>(event);
111
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
60 const auto& worldPosition = canvas->getWorldPosition();
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
61 if (worldPosition.has_value())
108
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
62 {
111
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
63 this->previewPoint = worldPosition.value();
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
64 this->updatePreviewPolygon();
108
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
65 }
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
66 return false;
108
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
67 }
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
68
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
69 bool AbstractDrawTool::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
70 {
f9f308c8e0c5 esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents: 123
diff changeset
71 if (event->key() == Qt::Key_Escape)
f9f308c8e0c5 esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents: 123
diff changeset
72 {
f9f308c8e0c5 esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents: 123
diff changeset
73 this->polygon.clear();
f9f308c8e0c5 esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents: 123
diff changeset
74 this->updatePreviewPolygon();
125
f127982d3412 Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents: 124
diff changeset
75 canvas->update();
124
f9f308c8e0c5 esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents: 123
diff changeset
76 return true;
f9f308c8e0c5 esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents: 123
diff changeset
77 }
f9f308c8e0c5 esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents: 123
diff changeset
78 else
f9f308c8e0c5 esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents: 123
diff changeset
79 {
f9f308c8e0c5 esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents: 123
diff changeset
80 return false;
f9f308c8e0c5 esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents: 123
diff changeset
81 }
f9f308c8e0c5 esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents: 123
diff changeset
82 }
f9f308c8e0c5 esc with draw mode now clears the polygon
Teemu Piippo <teemu@hecknology.net>
parents: 123
diff changeset
83
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
84 void AbstractDrawTool::addCurrentPoint(Canvas* canvas)
121
000781318c36 added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
85 {
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
86 const auto& worldPosition = canvas->getWorldPosition();
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
87 if (worldPosition.has_value())
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
88 {
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
89 const glm::vec3& pos = worldPosition.value();
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
90 if (this->isCloseToExistingPoints(pos))
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
91 {
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
92 this->closeShape();
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
93 }
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
94 else
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
95 {
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
96 this->addPoint(pos);
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
97 }
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
98 }
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
99 }
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
100
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
101 void AbstractDrawTool::updatePreviewPolygon()
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
102 {
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
103 this->previewPolygon = this->polygon;
121
000781318c36 added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
104 this->previewPolygon.resize(this->polygon.size() + 1);
000781318c36 added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
105 this->previewPolygon.back() = this->previewPoint;
122
b54b350dff5d Show concave polygons as red while drawing
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
106 if (this->previewPolygon.size() > 2)
b54b350dff5d Show concave polygons as red while drawing
Teemu Piippo <teemu@hecknology.net>
parents: 121
diff changeset
107 {
123
e3fe3617b631 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 122
diff changeset
108 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
109 }
121
000781318c36 added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
110 }
000781318c36 added right click support for draw tool
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
111
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
112 void AbstractDrawTool::reset()
106
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
113 {
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
114 this->polygon.clear();
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
115 }
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
116
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
117 void AbstractDrawTool::overpaint(Canvas* canvas, QPainter* painter) const
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
118 {
168
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
119 painter->setPen(this->isconcave ? ::badPolygonPen : ::polygonPen);
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
120 if (this->previewPolygon.size() > 2 and not this->isconcave)
164
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 163
diff changeset
121 {
168
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
122 if (canvas->worldPolygonWinding(this->previewPolygon) == Winding::Clockwise)
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
123 {
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
124 painter->setBrush(::greenPolygonBrush);
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
125 }
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
126 else
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
127 {
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
128 painter->setBrush(::redPolygonBrush);
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
129 }
164
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 163
diff changeset
130 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
131 }
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 163
diff changeset
132 else
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 163
diff changeset
133 {
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 163
diff changeset
134 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
135 }
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
136 painter->setBrush(::pointBrush);
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
137 painter->setPen(::pointPen);
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
138 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
139 {
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
140 canvas->drawWorldPoint(painter, point);
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
141 }
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
142 canvas->drawWorldPoint(painter, this->previewPoint);
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
143 }
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
144
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
145 void AbstractDrawTool::addPoint(const glm::vec3 &pos)
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
146 {
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
147 this->polygon.push_back(pos);
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
148 this->updatePreviewPolygon();
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
149 }
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
150
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
151 void AbstractDrawTool::removeLastPoint()
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
152 {
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
153 if (this->polygon.size() > 0)
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
154 {
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
155 this->polygon.erase(this->polygon.end() - 1);
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
156 this->updatePreviewPolygon();
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
157 }
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
158 }
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
159
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
160 void AbstractDrawTool::clearPoints()
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
161 {
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
162 this->polygon.clear();
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
163 this->updatePreviewPolygon();
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
164 }
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
165
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
166 bool AbstractDrawTool::isCloseToExistingPoints(const glm::vec3 &pos) const
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
167 {
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
168 const auto isCloseToPos = [&](const glm::vec3& x)
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
169 {
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
170 return geom::isclose(x, pos);
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
171 };
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
172 return any(this->polygon, isCloseToPos);
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
173 }
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
174
163
36ea1a8aee33 Add icons for tools
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
175 QString DrawTool::iconName() const
36ea1a8aee33 Add icons for tools
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
176 {
36ea1a8aee33 Add icons for tools
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
177 return ":/icons/pencil-outline.png";
36ea1a8aee33 Add icons for tools
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
178 }
36ea1a8aee33 Add icons for tools
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
179
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
180 void DrawTool::addPoint(const glm::vec3 &pos)
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
181 {
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
182 AbstractDrawTool::addPoint(pos);
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
183 if (this->polygon.size() == 4)
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
184 {
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
185 this->closeShape();
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
186 }
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
187 }
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
188
111
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
189 template<std::size_t N, typename T>
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
190 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
191 {
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
192 std::array<T, N> result;
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
193 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
194 {
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
195 result[i] = x[i];
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
196 }
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
197 return result;
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
198 }
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
199
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
200 void DrawTool::closeShape()
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
201 {
111
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
202 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
203 {
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
204 std::unique_ptr<ModelEditor> modelEditor = this->document->editModel();
111
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
205 switch (this->polygon.size())
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
206 {
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
207 case 2:
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
208 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
209 break;
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
210 case 3:
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
211 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
212 break;
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
213 case 4:
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 143
diff changeset
214 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
215 break;
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
216 }
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 109
diff changeset
217 }
185
a38a0eb007b0 Added line path tool
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
218 this->clearPoints();
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
219 }

mercurial