src/tools/circletool.cpp

Tue, 24 May 2022 16:11:10 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 24 May 2022 16:11:10 +0300
changeset 188
64ea7282611e
parent 187
30204975694a
permissions
-rw-r--r--

more work on circle tool + cleanup

186
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 CircleTool::CircleTool(Document *document) :
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 AbstractDrawTool{document}
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 {
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 }
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 QString CircleTool::name() const
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 {
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 return tr("Circle");
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 }
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 QString CircleTool::toolTip() const
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 {
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 return tr("Draw circular primitives like circles or discs");
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 }
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
187
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
19 std::vector<glm::vec3> circle(int divisions)
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
20 {
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
21 std::vector<glm::vec3> points;
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
22 points.reserve(divisions + 1);
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
23 for (int i = 0; i <= divisions; ++i) {
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
24 float ang = i * 2.0f * glm::pi<float>() / divisions;
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
25 points.push_back({std::sin(ang), std::cos(ang), 0.0f});
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
26 }
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
27 return points;
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
28 }
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
29
186
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 void CircleTool::overpaint(Canvas *canvas, QPainter *painter) const
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 {
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 if (this->previewPolygon.size() >= 2)
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 {
187
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
34 for (int i : {0, 1}) {
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
35 canvas->drawWorldPoint(painter, this->previewPolygon[i]);
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
36 }
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
37 painter->setPen(QPen{Qt::green, 2, Qt::DashLine, Qt::RoundCap, Qt::MiterJoin});
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
38 canvas->drawWorldPolyline(painter, {this->previewPolygon[0], this->previewPolygon[1]});
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
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
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
41 matrix[3] = {this->previewPolygon[0], 1};
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
42 std::vector<glm::vec3> points = circle(16);
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
43 for (std::size_t i = 0; i < points.size(); ++i) {
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
44 points[i] = matrix * glm::vec4{points[i], 1.0f};
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
45 }
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
46 painter->setPen(QPen{Qt::black, 2, Qt::DashLine, Qt::RoundCap, Qt::MiterJoin});
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
47 canvas->drawWorldPolyline(painter, points);
186
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 }
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 QString CircleTool::iconName() const
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 {
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 return ":/icons/linetype-circularprimitive.png";
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 }
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 void CircleTool::closeShape()
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 {
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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 }

mercurial