Sat, 05 Mar 2022 18:26:18 +0200
Added a toggle for setting whether axes are drawn
89 | 1 | #pragma once |
2 | #include "main.h" | |
3 | #include "colors.h" | |
4 | ||
5 | class Vec3Editor; | |
6 | class MatrixEditor; | |
7 | ||
8 | namespace ldraw | |
9 | { | |
10 | enum class Property; | |
11 | struct PropertyKeyValue; | |
12 | template<Property property> | |
13 | struct PropertyTraits | |
14 | { | |
15 | static constexpr bool defined = false; | |
16 | }; | |
17 | } | |
18 | ||
19 | /** | |
20 | * Different properties | |
21 | */ | |
22 | enum class ldraw::Property | |
23 | { | |
24 | Color, // Color of the object | |
25 | Text, // Text contained in a comment | |
26 | Point0, // First vertex in a polygon or edge line | |
27 | Point1, // Second vertex in a polygon or edge line | |
28 | Point2, // Third vertex in a polygon | |
29 | Point3, // Fourth vertex in a quadrilateral | |
30 | Transformation, // 4x4 transformation matrix of a subfile reference | |
31 | ReferenceName, // Subfile reference name | |
32 | IsInverted, // Whether or not the object has been inverted with BFC INVERTNEXT | |
33 | ErrorMessage // For error lines, why parsing failed | |
34 | }; | |
35 | ||
36 | Q_DECLARE_METATYPE(ldraw::Property) | |
37 | ||
38 | // Mapping of properties to types | |
39 | #define LDFORGE_DEFINE_PROPERTY_TYPE(PROPERTY, TYPE) \ | |
40 | namespace ldraw \ | |
41 | { \ | |
42 | template<> struct PropertyTraits<ldraw::Property::PROPERTY> \ | |
43 | { \ | |
44 | using type = TYPE; \ | |
45 | static constexpr std::array<char, 256> name{#PROPERTY}; \ | |
46 | static constexpr bool defined = true; \ | |
47 | }; \ | |
48 | } | |
49 | ||
50 | LDFORGE_DEFINE_PROPERTY_TYPE(Color, ldraw::Color) | |
51 | LDFORGE_DEFINE_PROPERTY_TYPE(Text, QString) | |
52 | LDFORGE_DEFINE_PROPERTY_TYPE(Point0, glm::vec3) | |
53 | LDFORGE_DEFINE_PROPERTY_TYPE(Point1, glm::vec3) | |
54 | LDFORGE_DEFINE_PROPERTY_TYPE(Point2, glm::vec3) | |
55 | LDFORGE_DEFINE_PROPERTY_TYPE(Point3, glm::vec3) | |
56 | LDFORGE_DEFINE_PROPERTY_TYPE(Transformation, glm::mat4) | |
57 | LDFORGE_DEFINE_PROPERTY_TYPE(ReferenceName, QString) | |
58 | LDFORGE_DEFINE_PROPERTY_TYPE(IsInverted, bool) | |
59 | LDFORGE_DEFINE_PROPERTY_TYPE(ErrorMessage, QString) | |
60 | ||
61 | #define LDRAW_OBJECT_HANDLE_SET_PROPERTY(PROPERTY, HANDLER) \ | |
62 | {this->handle<ldraw::Property::PROPERTY>(result, pair, \ | |
63 | [&](const ldraw::PropertyType<ldraw::Property::PROPERTY>& value) HANDLER);} | |
64 | ||
65 | // Generics | |
66 | namespace ldraw | |
67 | { | |
68 | template<ldraw::Property property> | |
69 | using PropertyType = typename PropertyTraits<property>::type; | |
70 | ||
71 | template<ldraw::Property property> | |
72 | inline const char* PROPERTY_NAME = PropertyTraits<property>::name; | |
73 | ||
117 | 74 | constexpr int MAX_POINTS = 4; |
75 | ||
89 | 76 | struct PropertyKeyValue |
77 | { | |
78 | Property key; | |
79 | QVariant value; | |
80 | }; | |
81 | ||
82 | constexpr Property pointProperty(int n) | |
83 | { | |
117 | 84 | Q_ASSERT(n >= 0 and n < MAX_POINTS); |
89 | 85 | return static_cast<Property>(static_cast<int>(Property::Point0) + n); |
86 | } | |
87 | ||
88 | struct PropertyTrait | |
89 | { | |
90 | ldraw::Property property; | |
91 | std::array<char, 256> name; | |
92 | int type; | |
93 | }; | |
94 | ||
95 | namespace detail | |
96 | { | |
97 | template<int N> | |
98 | constexpr int propertyCountHelper() | |
99 | { | |
100 | if constexpr (ldraw::PropertyTraits<static_cast<Property>(N)>::defined) | |
101 | { | |
102 | return propertyCountHelper<N + 1>(); | |
103 | } | |
104 | else | |
105 | { | |
106 | return N; | |
107 | } | |
108 | } | |
109 | ||
110 | template<int k> | |
111 | constexpr PropertyTrait getPropertyTrait() | |
112 | { | |
113 | constexpr auto property = static_cast<ldraw::Property>(k); | |
114 | using trait = ldraw::PropertyTraits<property>; | |
115 | return PropertyTrait{ | |
116 | property, | |
117 | trait::name, | |
118 | qMetaTypeId<typename trait::type>() | |
119 | }; | |
120 | } | |
121 | ||
122 | template<int... Ints> | |
123 | auto getPropertyTraits(std::integer_sequence<int, Ints...>) | |
124 | { | |
125 | return std::array<PropertyTrait, sizeof...(Ints)>{getPropertyTrait<Ints>()...}; | |
126 | } | |
127 | } | |
128 | ||
129 | constexpr int NUM_PROPERTIES = detail::propertyCountHelper<0>(); | |
130 | inline const auto& traits() | |
131 | { | |
132 | static std::array<PropertyTrait, NUM_PROPERTIES> result = | |
133 | detail::getPropertyTraits(std::make_integer_sequence<int, NUM_PROPERTIES>()); | |
134 | return result; | |
135 | } | |
136 | ||
137 | inline const auto& traits(ldraw::Property property) | |
138 | { | |
139 | return traits()[static_cast<int>(property)]; | |
140 | } | |
141 | ||
142 | template<typename T, std::size_t... Ints> | |
143 | constexpr auto makeIndexArray(std::index_sequence<Ints...>) | |
144 | { | |
145 | return std::array{static_cast<T>(Ints)...}; | |
146 | } | |
147 | ||
148 | constexpr auto ALL_PROPERTIES = makeIndexArray<Property>(std::make_index_sequence<NUM_PROPERTIES>{}); | |
149 | ||
150 | template<typename T> | |
151 | bool testPropertyType(ldraw::Property property) | |
152 | { | |
153 | return qMetaTypeId<T>() == ldraw::traits(property).type; | |
154 | } | |
155 | } |