Tue, 24 May 2022 16:11:10 +0300
more work on circle tool + cleanup
186 | 1 | #include "circletool.h" |
188
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
2 | #include "document.h" |
186 | 3 | |
4 | CircleTool::CircleTool(Document *document) : | |
5 | AbstractDrawTool{document} | |
6 | { | |
7 | } | |
8 | ||
9 | QString CircleTool::name() const | |
10 | { | |
11 | return tr("Circle"); | |
12 | } | |
13 | ||
14 | QString CircleTool::toolTip() const | |
15 | { | |
16 | return tr("Draw circular primitives like circles or discs"); | |
17 | } | |
18 | ||
187 | 19 | std::vector<glm::vec3> circle(int divisions) |
20 | { | |
21 | std::vector<glm::vec3> points; | |
22 | points.reserve(divisions + 1); | |
23 | for (int i = 0; i <= divisions; ++i) { | |
24 | float ang = i * 2.0f * glm::pi<float>() / divisions; | |
25 | points.push_back({std::sin(ang), std::cos(ang), 0.0f}); | |
26 | } | |
27 | return points; | |
28 | } | |
29 | ||
186 | 30 | void CircleTool::overpaint(Canvas *canvas, QPainter *painter) const |
31 | { | |
32 | if (this->previewPolygon.size() >= 2) | |
33 | { | |
187 | 34 | for (int i : {0, 1}) { |
35 | canvas->drawWorldPoint(painter, this->previewPolygon[i]); | |
36 | } | |
37 | painter->setPen(QPen{Qt::green, 2, Qt::DashLine, Qt::RoundCap, Qt::MiterJoin}); | |
38 | canvas->drawWorldPolyline(painter, {this->previewPolygon[0], this->previewPolygon[1]}); | |
39 | const float size = glm::distance(this->previewPolygon[1], this->previewPolygon[0]); | |
188
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
40 | glm::mat4 matrix = size * this->baseGridMatrix; |
187 | 41 | matrix[3] = {this->previewPolygon[0], 1}; |
42 | std::vector<glm::vec3> points = circle(16); | |
43 | for (std::size_t i = 0; i < points.size(); ++i) { | |
44 | points[i] = matrix * glm::vec4{points[i], 1.0f}; | |
45 | } | |
46 | painter->setPen(QPen{Qt::black, 2, Qt::DashLine, Qt::RoundCap, Qt::MiterJoin}); | |
47 | canvas->drawWorldPolyline(painter, points); | |
186 | 48 | } |
188
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
49 | if (this->previewPolygon.size() >= 3) |
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
50 | { |
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
51 | |
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
52 | } |
186 | 53 | } |
54 | ||
55 | QString CircleTool::iconName() const | |
56 | { | |
57 | return ":/icons/linetype-circularprimitive.png"; | |
58 | } | |
59 | ||
188
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
60 | void CircleTool::addPoint(const glm::vec3 &pos) |
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
61 | { |
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
62 | AbstractDrawTool::addPoint(pos); |
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
63 | if (this->polygon.size() >= 2) |
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
64 | { |
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
65 | const glm::mat4& grid = this->document->currentGrid(); |
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
66 | const glm::mat4 newGrid = {grid[1], grid[2], grid[0], {this->polygon[0], 1}}; |
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
67 | Q_EMIT this->desiredGridChange(newGrid); |
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
68 | } |
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
69 | } |
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
70 | |
186 | 71 | void CircleTool::closeShape() |
72 | { | |
73 | ||
74 | } | |
188
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
75 | |
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
76 | void CircleTool::reset() |
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
77 | { |
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
78 | this->baseGridMatrix = this->document->currentGrid(); |
64ea7282611e
more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents:
187
diff
changeset
|
79 | } |