src/linetypes/propertygenerics.h

Sun, 25 Jul 2021 20:29:14 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 25 Jul 2021 20:29:14 +0300
changeset 111
1f42c03fafca
parent 89
7abaf1d64719
child 117
121a40d5e34c
permissions
-rw-r--r--

Draw tool actually adds objects now

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

mercurial