src/linetypes/propertygenerics.h

Wed, 25 May 2022 12:01:58 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 25 May 2022 12:01:58 +0300
changeset 189
815fbaae9cb2
parent 186
922662adb72a
permissions
-rw-r--r--

cleanup, gl::Compiler changed to gl::ModelShaders

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

mercurial