Wed, 25 May 2022 18:29:49 +0300
move drawState to Document
154 | 1 | #include "ldrawalgorithm.h" |
2 | #include "linetypes/quadrilateral.h" | |
3 | #include "linetypes/triangle.h" | |
4 | ||
183
97b591813c8b
- Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
154
diff
changeset
|
5 | void ldraw::invert(ModelEditor& editor, ldraw::id_t id, GetPolygonsContext* context) |
154 | 6 | { |
183
97b591813c8b
- Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
154
diff
changeset
|
7 | editor.modifyObject(id, [context](ldraw::Object* object){ |
97b591813c8b
- Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
154
diff
changeset
|
8 | object->invert(context); |
154 | 9 | }); |
10 | } | |
11 | ||
12 | static std::array<geom::Triangle, 2> splitTriangles(ldraw::Diagonal diagonal, const std::array<glm::vec3, 4>& points) | |
13 | { | |
14 | std::array<geom::Triangle, 2> result; | |
15 | switch (diagonal) | |
16 | { | |
17 | case ldraw::Diagonal::Diagonal_13: | |
18 | result = {geom::Triangle{points[0], points[1], points[2]}, {points[0], points[2], points[3]}}; | |
19 | break; | |
20 | case ldraw::Diagonal::Diagonal_24: | |
21 | result = {geom::Triangle{points[0], points[1], points[3]}, {points[1], points[2], points[3]}}; | |
22 | break; | |
23 | } | |
24 | return result; | |
25 | } | |
26 | ||
27 | auto ldraw::splitQuadrilateral( | |
28 | ModelEditor& editor, | |
29 | ldraw::quadrilateralid_t quadrilateral_id, | |
30 | ldraw::Diagonal splitType | |
31 | ) -> std::optional<std::pair<ldraw::triangleid_t, ldraw::triangleid_t>> | |
32 | { | |
33 | std::optional<std::pair<ldraw::triangleid_t, ldraw::triangleid_t>> result; | |
34 | const auto resolved = editor.model().get2(quadrilateral_id); | |
35 | if (resolved.object != nullptr) | |
36 | { | |
37 | const ldraw::Color color = resolved.object->colorIndex; | |
38 | const std::array<geom::Triangle, 2> split = splitTriangles(splitType, resolved.object->points); | |
39 | const int position = resolved.index.row(); | |
40 | editor.remove(position); | |
41 | result = std::make_pair( | |
42 | editor.insert<ldraw::Triangle>(position, split[0].points, color), | |
43 | editor.insert<ldraw::Triangle>(position, split[1].points, color)); | |
44 | } | |
45 | return result; | |
46 | } | |
47 | ||
48 | /** | |
49 | * @brief Modifies the !LDRAW_ORG line so that it becomes unofficial | |
50 | */ | |
51 | void ldraw::makeUnofficial(ModelEditor& editor) | |
52 | { | |
53 | if (editor.model().size() >= 4) | |
54 | { | |
55 | const ldraw::Object* ldrawOrgLine = editor.model()[3]; | |
56 | if (isA<ldraw::MetaCommand>(ldrawOrgLine)) | |
57 | { | |
58 | const QString& body = ldrawOrgLine->getProperty<ldraw::Property::Text>(); | |
59 | if (body.startsWith("!LDRAW_ORG ") and not body.startsWith("!LDRAW_ORG Unofficial_")) | |
60 | { | |
61 | // Add Unofficial_ to part type | |
62 | QStringList tokens = body.split(" "); | |
63 | tokens[1] = "Unofficial_" + tokens[1]; | |
64 | // Remove the UPDATE tag if it's there | |
65 | if (tokens.size() >= 4 && tokens[2] == "UPDATE") | |
66 | { | |
67 | tokens.removeAt(3); | |
68 | tokens.removeAt(2); | |
69 | } | |
70 | editor.setObjectProperty<ldraw::Property::Text>(ldrawOrgLine->id, tokens.join(" ")); | |
71 | } | |
72 | } | |
73 | } | |
74 | } |