src/layers/edittools.cpp

Mon, 04 Jul 2022 15:37:22 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Mon, 04 Jul 2022 15:37:22 +0300
changeset 332
ae7f7fbb9cda
parent 330
edb6c09cdd3c
child 333
07e65a4c6611
permissions
-rw-r--r--

Only enable the text editor when having a model open

24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
1 /*
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
2 * LDForge: LDraw parts authoring CAD
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
4 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
5 * This program is free software: you can redistribute it and/or modify
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
6 * it under the terms of the GNU General Public License as published by
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
8 * (at your option) any later version.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
9 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
10 * This program is distributed in the hope that it will be useful,
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
13 * GNU General Public License for more details.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
14 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
15 * You should have received a copy of the GNU General Public License
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
17 */
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
18
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 39
diff changeset
19 #include <QMouseEvent>
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
20 #include <QPainter>
264
76a025db4948 Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 263
diff changeset
21 #include "src/model.h"
76a025db4948 Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 263
diff changeset
22 #include "src/gl/partrenderer.h"
76a025db4948 Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 263
diff changeset
23 #include "src/circularprimitive.h"
76a025db4948 Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 263
diff changeset
24 #include "src/layers/edittools.h"
306
6ad27b7d2697 Enable drawing clockwise shapes despite mapbox::earcut rewinding them
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 305
diff changeset
25 #include "src/invert.h"
319
9727e545b0bc Extract the triangulation and triangle merging code into a new source file and clean it up somewhat
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 316
diff changeset
26 #include "src/triangulate.h"
223
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
27
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
28 EditTools::EditTools(QObject* parent) :
214
8e1fe64ce4e3 begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
29 QObject{parent},
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
30 RenderLayer{}
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33
214
8e1fe64ce4e3 begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
34 EditTools::~EditTools()
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 {
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
36 }
36
bbb901b97404 added render style storage
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
37
250
2837b549e616 I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 245
diff changeset
38 void EditTools::setEditMode(EditingMode newMode)
125
f127982d3412 Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents: 124
diff changeset
39 {
250
2837b549e616 I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 245
diff changeset
40 this->mode = newMode;
311
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
41 switch (this->mode) {
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
42 case SelectMode:
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
43 Q_EMIT this->suggestCursor(Qt::ArrowCursor);
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
44 break;
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
45 case DrawMode:
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
46 case CircleMode:
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
47 Q_EMIT this->suggestCursor(Qt::CrossCursor);
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
48 break;
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
49 }
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
50 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
51
250
2837b549e616 I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 245
diff changeset
52 void EditTools::setGridMatrix(const glm::mat4& newGridMatrix)
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
53 {
250
2837b549e616 I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 245
diff changeset
54 this->gridMatrix = newGridMatrix;
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
55 this->gridPlane = planeFromTriangle({
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
56 this->gridMatrix * glm::vec4{0, 0, 0, 1},
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
57 this->gridMatrix * glm::vec4{1, 0, 0, 1},
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
58 this->gridMatrix * glm::vec4{0, 1, 0, 1},
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
59 });
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
60 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
61
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
62 void EditTools::setCircleToolOptions(const CircleToolOptions& options)
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
63 {
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
64 this->circleToolOptions = options;
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
65 }
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
66
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
67 void EditTools::mvpMatrixChanged(const glm::mat4& matrix)
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
68 {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
69 this->mvpMatrix = matrix;
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
70 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
71
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
72 void EditTools::mouseMoved(const QMouseEvent* event)
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
73 {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
74 this->worldPosition = this->renderer->screenToModelCoordinates(event->pos(), this->gridPlane);
314
4642ba1218e8 Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 312
diff changeset
75 this->localPosition = event->localPos();
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
76 if (this->worldPosition.has_value())
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
77 {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
78 // Snap the position to grid. This procedure is basically the "change of basis" and almost follows the
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
79 // A⁻¹ × M × A formula which is used to perform a transformation in some other coordinate system, except
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
80 // we actually use the inverted matrix first and the regular one last to perform the transformation of
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
81 // grid coordinates in our XY coordinate system. Also, we're rounding the coordinates which is obviously
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
82 // not a linear transformation, but fits the pattern anyway.
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
83 // First transform the coordinates to the XY plane...
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
84 this->worldPosition = glm::inverse(this->gridMatrix) * glm::vec4{*this->worldPosition, 1};
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
85 // Then round the coordinates to integer precision...
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
86 this->worldPosition = glm::round(*this->worldPosition);
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
87 // And finally transform it back to grid coordinates by transforming it with the
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
88 // grid matrix.
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
89 this->worldPosition = this->gridMatrix * glm::vec4{*this->worldPosition, 1};
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
90 this->inputPolygon.updateCurrentPoint(*this->worldPosition);
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
91 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
92 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
93
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 301
diff changeset
94 //! \brief Conversion function from PlainPolygonElement to ModelElement
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 301
diff changeset
95 ModelElement elementFromPolygonAndColor(const PlainPolygonElement& poly, ColorIndex color)
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 301
diff changeset
96 {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 301
diff changeset
97 // use std::visit with a templated lambda to resolve the type of poly.
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 301
diff changeset
98 return std::visit([color](const auto& resolvedPoly) -> ModelElement {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 301
diff changeset
99 // unlike with normal templates we need to pry out the type out manually
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 301
diff changeset
100 using PolygonType = std::decay_t<decltype(resolvedPoly)>;
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 301
diff changeset
101 // add color and return as a model element.
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 301
diff changeset
102 return Colored<PolygonType>{resolvedPoly, color};
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 301
diff changeset
103 }, poly);
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 301
diff changeset
104 }
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 301
diff changeset
105
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 301
diff changeset
106 static std::vector<std::vector<glm::vec3>> polygonsToBeInserted(const ModelAction& action)
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
107 {
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
108 std::vector<std::vector<glm::vec3>> result;
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
109 if (const AppendToModel* append = std::get_if<AppendToModel>(&action)) {
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
110 const ModelElement& newElement = append->newElement;
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
111 if (const LineSegment* seg = std::get_if<Colored<LineSegment>>(&newElement)) {
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
112 result.push_back({seg->p1, seg->p2});
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
113 }
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
114 else if (const Triangle* tri = std::get_if<Colored<Triangle>>(&newElement)) {
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
115 result.push_back({tri->p1, tri->p2, tri->p3});
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
116 }
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
117 else if (const Quadrilateral* quad = std::get_if<Colored<Quadrilateral>>(&newElement)) {
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
118 result.push_back({quad->p1, quad->p2, quad->p3, quad->p4});
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
119 }
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
120 else if (const CircularPrimitive* circ = std::get_if<Colored<CircularPrimitive>>(&newElement)) {
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 301
diff changeset
121 // rasterize the circle down to polygons, and append them to the result.
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 301
diff changeset
122 rasterize(*circ, [&](const PlainPolygonElement& poly, const ColorIndex color){
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 301
diff changeset
123 AppendToModel append{elementFromPolygonAndColor(poly, color)};
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 301
diff changeset
124 const auto& subpoints = polygonsToBeInserted(append);
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
125 std::copy(subpoints.begin(), subpoints.end(), std::back_inserter(result));
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
126 });
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
127 }
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
128 }
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
129 return result;
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
130 }
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
131
228
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
132 namespace {
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
133 struct Pens
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
134 {
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
135 const QBrush pointBrush;
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
136 const QPen pointPen;
245
a41ccc6924e3 improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 244
diff changeset
137 const QPen textPen;
228
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
138 const QPen polygonPen;
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
139 const QBrush greenPolygonBrush;
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
140 const QBrush redPolygonBrush;
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
141 };
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
142 }
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
143
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
144 static const Pens brightPens{
245
a41ccc6924e3 improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 244
diff changeset
145 .pointBrush = {Qt::black},
228
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
146 .pointPen = {QBrush{Qt::black}, 2.0},
245
a41ccc6924e3 improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 244
diff changeset
147 .textPen = {Qt::black},
228
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
148 .polygonPen = {QBrush{Qt::black}, 2.0, Qt::DashLine},
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
149 .greenPolygonBrush = {QColor{64, 255, 128, 192}},
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
150 .redPolygonBrush = {QColor{255, 96, 96, 192}},
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
151 };
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
152
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
153 static const Pens darkPens{
245
a41ccc6924e3 improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 244
diff changeset
154 .pointBrush = {Qt::white},
228
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
155 .pointPen = {QBrush{Qt::white}, 2.0},
245
a41ccc6924e3 improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 244
diff changeset
156 .textPen = {Qt::white},
228
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
157 .polygonPen = {QBrush{Qt::white}, 2.0, Qt::DashLine},
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
158 .greenPolygonBrush = {QColor{64, 255, 128, 192}},
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
159 .redPolygonBrush = {QColor{255, 96, 96, 192}},
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
160 };
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
161
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
162 void EditTools::overpaint(QPainter* painter)
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
163 {
245
a41ccc6924e3 improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 244
diff changeset
164 painter->save();
311
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
165 if (this->usePolygon()) {
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
166 const Pens& pens = (this->renderer->isDark() ? darkPens : brightPens);
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
167 this->renderPreview(painter, &pens);
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
168 QFont font;
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
169 font.setBold(true);
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
170 if (this->usePolygon() and this->worldPosition.has_value())
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
171 {
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
172 painter->setRenderHint(QPainter::Antialiasing);
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
173 painter->setPen(pens.pointPen);
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
174 painter->setBrush(pens.greenPolygonBrush);
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
175 const QPointF pos = this->renderer->modelToScreenCoordinates(*this->worldPosition);
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
176 painter->drawEllipse(pos, 5, 5);
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
177 drawBorderedText(painter, pos + QPointF{5, 5}, font, vectorToString(*this->worldPosition));
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
178 }
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
179 }
245
a41ccc6924e3 improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 244
diff changeset
180 painter->restore();
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
181 }
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
182
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
183 const std::vector<ModelAction> EditTools::modelActions() const
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
184 {
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
185 switch(this->mode) {
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
186 case SelectMode:
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
187 return {};
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
188 case DrawMode:
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
189 return drawModeActions();
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
190 case CircleMode:
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
191 return circleModeActions();
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
192 }
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
193 }
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
194
228
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
195 void EditTools::renderPreview(QPainter* painter, const void* pensptr)
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
196 {
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
197 const Pens& pens = *reinterpret_cast<const Pens*>(pensptr);
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
198 painter->setPen(pens.polygonPen);
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
199 for (const ModelAction& action : this->modelActions()) {
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 301
diff changeset
200 for (const std::vector<glm::vec3>& points : polygonsToBeInserted(action)) {
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
201 if (points.size() == 2) {
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
202 drawWorldPolyline(painter, points, renderer);
228
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
203 }
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
204 else {
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
205 if (worldPolygonWinding(points, this->renderer) == Winding::Clockwise) {
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
206 painter->setBrush(pens.greenPolygonBrush);
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
207 }
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
208 else {
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
209 painter->setBrush(pens.redPolygonBrush);
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
210 }
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
211 drawWorldPolygon(painter, points, this->renderer);
228
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
212 }
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
213 }
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
214 }
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
215 painter->setBrush(pens.pointBrush);
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
216 painter->setPen(pens.pointPen);
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
217 for (const glm::vec3& point : this->inputPolygon) {
228
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
218 drawWorldPoint(painter, point, this->renderer);
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
219 }
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
220 if (this->mode == CircleMode and this->inputPolygon.polygonSize() >= 2) {
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
221 const glm::vec3 circleOrigin = this->inputPolygon[0];
314
4642ba1218e8 Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 312
diff changeset
222 const QPointF originScreen = this->renderer->modelToScreenCoordinates(circleOrigin);
4642ba1218e8 Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 312
diff changeset
223 const auto extremity = [this, &originScreen](const glm::vec3& p){
4642ba1218e8 Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 312
diff changeset
224 const QPointF s2 = this->renderer->modelToScreenCoordinates(p);
4642ba1218e8 Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 312
diff changeset
225 const auto intersection = rayRectangleIntersection(
4642ba1218e8 Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 312
diff changeset
226 rayFromPoints(toVec2(originScreen), toVec2(s2)),
4642ba1218e8 Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 312
diff changeset
227 this->renderer->rect());
4642ba1218e8 Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 312
diff changeset
228 if (intersection.has_value()) {
4642ba1218e8 Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 312
diff changeset
229 return intersection->position;
4642ba1218e8 Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 312
diff changeset
230 }
4642ba1218e8 Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 312
diff changeset
231 else {
4642ba1218e8 Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 312
diff changeset
232 return glm::vec2{s2.x(), s2.y()};
4642ba1218e8 Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 312
diff changeset
233 }
4642ba1218e8 Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 312
diff changeset
234 };
4642ba1218e8 Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 312
diff changeset
235 const glm::vec3 zvec = this->gridMatrix[2];
324
d9bab5d22c98 Make the extrusion axis only visible when extruding
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 323
diff changeset
236 if (this->inputPolygon.bufferSize() >= 3) {
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
237 const glm::vec2 p1 = extremity(this->inputPolygon[0] + zvec);
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
238 const glm::vec2 p2 = extremity(this->inputPolygon[0] - zvec);
315
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
239 const glm::vec2 lateral = glm::normalize(glm::mat2{{0, 1}, {-1, 0}} * (p2 - p1));
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
240 painter->setPen(QPen{Qt::white, 3});
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
241 painter->drawLine(vecToQPoint(p1), vecToQPoint(p2));
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
242 constexpr float notchsize = 40.0f;
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
243 for (int a = -30; a <= 30; ++a) {
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
244 const glm::vec3 notch = this->inputPolygon[0] + static_cast<float>(a) * zvec;
315
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
245 const QPointF s_notchcenter = this->renderer->modelToScreenCoordinates(notch);
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
246 const QPointF notch_s1 = s_notchcenter + notchsize * 0.5f * vecToQPoint(lateral);
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
247 const QPointF notch_s2 = s_notchcenter - notchsize * 0.5f * vecToQPoint(lateral);
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
248 painter->drawLine(notch_s1, notch_s2);
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
249 }
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
250 const opt<float> height = this->cylinderHeight();
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
251 if (height.has_value()) {
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
252 const glm::vec3 heightvec = height.value_or(0) * zvec;
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
253 const glm::vec3 p = this->inputPolygon[1] + 0.5f * heightvec;
315
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
254 QFont font{};
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
255 font.setBold(true);
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
256 drawBorderedText(painter, this->renderer->modelToScreenCoordinates(p), font, QString::number(*height));
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
257 }
314
4642ba1218e8 Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 312
diff changeset
258 }
4642ba1218e8 Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 312
diff changeset
259 }
228
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
260 }
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
261
315
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
262 opt<float> EditTools::cylinderHeight() const
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
263 {
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
264 if (this->inputPolygon.bufferSize() < 3) {
315
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
265 return {};
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
266 }
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
267 else {
323
3c09c937848c Fix normal of the plane used to find cylinder height
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 322
diff changeset
268 const glm::vec3 cameravec = glm::normalize(this->renderer->cameraVector(this->localPosition));
3c09c937848c Fix normal of the plane used to find cylinder height
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 322
diff changeset
269 const glm::vec3 heightvec = glm::normalize(glm::vec3{gridMatrix[2]});
3c09c937848c Fix normal of the plane used to find cylinder height
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 322
diff changeset
270 const glm::vec3 normal = glm::cross(glm::cross(cameravec, heightvec), heightvec);
315
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
271 const Plane plane{
323
3c09c937848c Fix normal of the plane used to find cylinder height
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 322
diff changeset
272 .normal = normal,
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
273 .anchor = this->inputPolygon[0],
315
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
274 };
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
275 const opt<glm::vec3> p = this->renderer->screenToModelCoordinates(this->localPosition, plane);
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
276 if (p.has_value()) {
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
277 return std::round(glm::dot(*p - this->inputPolygon[0], heightvec));
315
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
278 }
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
279 else {
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
280 return {};
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
281 }
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
282 }
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
283 }
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
284
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
285 EditingMode EditTools::currentEditingMode() const
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
286 {
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
287 return this->mode;
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
288 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
289
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
290 void EditTools::mouseClick(const QMouseEvent* event)
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
291 {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
292 switch(this->mode) {
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
293 case SelectMode:
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
294 if (event->button() == Qt::LeftButton) {
309
d862721d19a3 Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 307
diff changeset
295 const ElementId highlighted = this->renderer->pick(event->pos());
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
296 Q_EMIT this->select({highlighted}, false);
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
297 }
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
298 break;
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
299 case DrawMode:
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
300 if (event->button() == Qt::LeftButton and this->worldPosition.has_value()) {
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
301 if (this->inputPolygon.currentPointOnExistingPoint()) {
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
302 this->closeShape();
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
303 }
214
8e1fe64ce4e3 begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
304 else {
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
305 this->inputPolygon.finishCurrentPoint();
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
306 }
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
307 }
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
308 break;
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
309 case CircleMode:
316
aab8e139a149 Allow make a cylinder even if the world position is not on grid
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 315
diff changeset
310 if (event->button() == Qt::LeftButton) {
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
311 if (this->inputPolygon.bufferSize() == 3) {
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
312 this->closeShape();
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
313 }
316
aab8e139a149 Allow make a cylinder even if the world position is not on grid
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 315
diff changeset
314 else if (this->worldPosition.has_value()) {
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
315 this->inputPolygon.finishCurrentPoint();
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
316 }
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
317 }
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
318 break;
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
319 }
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
320 if (event->button() == Qt::RightButton) {
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
321 this->inputPolygon.removeLastPoint();
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
322 }
223
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
323 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
324
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
325
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
326 const std::vector<ModelAction> EditTools::circleModeActions() const
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
327 {
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
328 std::vector<ModelAction> result;
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
329 if (this->inputPolygon.polygonSize() >= 2) {
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
330 const glm::vec3 x = this->inputPolygon[1] - this->inputPolygon[0];
315
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
331 const opt<float> cyliheight = this->cylinderHeight().value_or(1);
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
332 glm::mat4 transform{
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
333 glm::vec4{x, 0},
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
334 *cyliheight * this->gridMatrix[2],
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
335 glm::vec4{glm::cross(glm::vec3{-this->gridMatrix[2]}, x), 0},
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
336 glm::vec4{this->inputPolygon[0], 1},
315
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
337 };
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
338 Colored<CircularPrimitive> circ{
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
339 CircularPrimitive{
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
340 .type = this->circleToolOptions.type,
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
341 .fraction = this->circleToolOptions.fraction,
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
342 .transformation = transform,
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
343 },
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
344 MAIN_COLOR
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
345 };
23b47902d857 Improve preview of cylinder extrusion
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 314
diff changeset
346 result.push_back(AppendToModel{.newElement = circ});
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
347 }
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
348 return result;
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
349 }
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
350
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
351 const std::vector<ModelAction> EditTools::drawModeActions() const
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
352 {
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
353 std::vector<ModelAction> result;
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
354 if (this->inputPolygon.polygonSize() == 2) {
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
355 result.push_back(AppendToModel{edge(this->inputPolygon[0], this->inputPolygon[1])});
223
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
356 }
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
357 else if (this->inputPolygon.polygonSize() > 2) {
319
9727e545b0bc Extract the triangulation and triangle merging code into a new source file and clean it up somewhat
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 316
diff changeset
358 for (const PlainPolygonElement& poly : polygonize(
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
359 this->inputPolygon.begin(),
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
360 this->inputPolygon.polygonEnd())
319
9727e545b0bc Extract the triangulation and triangle merging code into a new source file and clean it up somewhat
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 316
diff changeset
361 ) {
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
362 result.push_back(AppendToModel{
319
9727e545b0bc Extract the triangulation and triangle merging code into a new source file and clean it up somewhat
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 316
diff changeset
363 .newElement = elementFromPolygonAndColor(poly, MAIN_COLOR),
204
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
364 });
223
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
365 }
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
366 }
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
367 return result;
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
368 }
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
369
311
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
370 bool EditTools::usePolygon() const
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
371 {
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
372 switch (this->mode) {
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
373 case SelectMode:
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
374 return false;
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
375 case DrawMode:
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
376 case CircleMode:
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
377 return true;
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
378 }
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
379 return {};
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
380 }
fab454611f9b Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 310
diff changeset
381
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
382 void EditTools::closeShape()
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
383 {
232
8efa3a33172e Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 228
diff changeset
384 for (const ModelAction& action : this->modelActions()) {
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
385 Q_EMIT this->modelAction(action);
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
386 }
322
a39f454a3d7f Reduce edittools.cpp further
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 320
diff changeset
387 this->inputPolygon.clear();
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
388 }

mercurial