Sun, 29 Aug 2021 20:05:24 +0300
esc with draw mode now clears the polygon
24 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2020 Teemu Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
3 | 19 | #include "modeleditcontext.h" |
89 | 20 | #include "linetypes/triangle.h" |
21 | #include "linetypes/quadrilateral.h" | |
3 | 22 | |
23 | Model::EditContext::EditContext(Model& model) : | |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
24 | storedModel{model} |
3 | 25 | { |
26 | } | |
27 | ||
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
28 | Model::EditContext::~EditContext() |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
29 | { |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
30 | for (ldraw::id_t id : this->modifiedObjects) |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
31 | { |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
32 | const QModelIndex index = this->model().lookup(id); |
112 | 33 | Q_EMIT this->model().dataChanged(index, index); |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
34 | } |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
35 | } |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
36 | |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
37 | ldraw::id_t Model::EditContext::append(std::unique_ptr<ldraw::Object>&& object) |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
38 | { |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
39 | const ldraw::id_t id = object->id; |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
40 | this->model().objectsById[id] = object.get(); |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
41 | this->model().append(std::move(object)); |
26 | 42 | return id; |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
43 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
44 | |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
45 | void Model::EditContext::remove(int position) |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
46 | { |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
47 | this->model().remove(position); |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
48 | } |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
49 | |
89 | 50 | auto Model::EditContext::setObjectProperty( |
51 | const ldraw::id_t id, | |
52 | const ldraw::Property property, | |
53 | const QVariant& value) | |
54 | -> ldraw::Object::SetPropertyResult | |
55 | { | |
56 | ldraw::Object* const object = this->model().objectAt(id); | |
57 | if (object != nullptr) | |
58 | { | |
59 | const ldraw::Object::SetPropertyResult result = object->setProperty(ldraw::PropertyKeyValue{property, value}); | |
60 | modifiedObjects.insert(id); | |
61 | return result; | |
62 | } | |
63 | else | |
64 | { | |
65 | return ldraw::Object::SetPropertyResult::PropertyNotHandled; | |
66 | } | |
67 | } | |
68 | ||
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
69 | void Model::EditContext::setObjectPoint(ldraw::id_t id, int pointId, const glm::vec3& value) |
3 | 70 | { |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
71 | ldraw::Object* object = this->model().objectAt(id); |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
72 | if (object != nullptr) |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
73 | { |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
74 | object->setProperty(ldraw::PropertyKeyValue{ldraw::pointProperty(pointId), QVariant::fromValue(value)}); |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
77
diff
changeset
|
75 | modifiedObjects.insert(id); |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
76 | } |
3 | 77 | } |
26 | 78 | |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
79 | void Model::EditContext::invertObject(ldraw::id_t id) |
26 | 80 | { |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
81 | auto it = this->model().objectsById.find(id); |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
82 | if (it != this->model().objectsById.end()) |
26 | 83 | { |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
84 | ldraw::Object* object = it->second; |
26 | 85 | object->invert(); |
86 | } | |
87 | } | |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
88 | |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
89 | Model& Model::EditContext::model() |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
90 | { |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
91 | return this->storedModel; |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
92 | } |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
93 | |
87
93ec4d630346
added PolygonObject and refactored away a lot of boilerplate
Teemu Piippo <teemu@hecknology.net>
parents:
86
diff
changeset
|
94 | static std::array<geom::Triangle, 2> splitTriangles(ldraw::Diagonal diagonal, const std::array<glm::vec3, 4>& points) |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
95 | { |
77
028798a72591
added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
96 | std::array<geom::Triangle, 2> result; |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
97 | switch (diagonal) |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
98 | { |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
99 | case ldraw::Diagonal::Diagonal_13: |
77
028798a72591
added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
100 | result = {geom::Triangle{points[0], points[1], points[2]}, {points[0], points[2], points[3]}}; |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
101 | break; |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
102 | case ldraw::Diagonal::Diagonal_24: |
77
028798a72591
added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
103 | result = {geom::Triangle{points[0], points[1], points[3]}, {points[1], points[2], points[3]}}; |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
104 | break; |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
105 | } |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
106 | return result; |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
107 | } |
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
108 | |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
109 | auto ldraw::splitQuadrilateral( |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
110 | Model::EditContext& editor, |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
111 | ldraw::quadrilateralid_t quadrilateral_id, |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
112 | ldraw::Diagonal splitType |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
113 | ) -> std::optional<std::pair<ldraw::triangleid_t, ldraw::triangleid_t>> |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
114 | { |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
115 | std::optional<std::pair<ldraw::triangleid_t, ldraw::triangleid_t>> result; |
116 | 116 | const auto resolved = editor.model().get2(quadrilateral_id); |
117 | if (resolved.object != nullptr) | |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
118 | { |
116 | 119 | const ldraw::Color color = resolved.object->colorIndex; |
120 | const std::array<geom::Triangle, 2> split = splitTriangles(splitType, resolved.object->points); | |
121 | const int position = resolved.index.row(); | |
76
7c4a63a02632
finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
122 | editor.remove(position); |
77
028798a72591
added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
123 | result = std::make_pair( |
028798a72591
added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
124 | editor.insert<ldraw::Triangle>(position, split[0].points, color), |
028798a72591
added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
125 | editor.insert<ldraw::Triangle>(position, split[1].points, color)); |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
126 | } |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
127 | return result; |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
128 | } |