src/linetypes/polygonobject.h

changeset 200
ca23936b455b
parent 199
6988973515d2
child 201
5d201ee4a9c3
equal deleted inserted replaced
199:6988973515d2 200:ca23936b455b
1 #pragma once
2 #include "object.h"
3 #include "widgets/vec3editor.h"
4 #include "model.h"
5 #include "modeleditor.h"
6
7 namespace ldraw
8 {
9 template<int N, typename>
10 class PolygonObject;
11 }
12
13 template<int N, typename = std::enable_if_t<(N > 0 and N <= 4)>>
14 class ldraw::PolygonObject : public ColoredObject
15 {
16 public:
17 using BaseClass = ColoredObject;
18 PolygonObject(const std::array<glm::vec3, N>& points, const Color color) :
19 ColoredObject{color},
20 points{points} {}
21 int numPoints() const override
22 {
23 return N;
24 }
25 const glm::vec3& getPoint(int index) const override
26 {
27 Q_ASSERT(index >= 0 and index < N);
28 return this->points[index];
29 }
30 QVariant getProperty(const Property id) const override
31 {
32 switch (id)
33 {
34 case Property::Point0:
35 return QVariant::fromValue(points[0]);
36 case Property::Point1:
37 return QVariant::fromValue(points[1]);
38 case Property::Point2:
39 if (N >= 3)
40 {
41 return QVariant::fromValue(points[2]);
42 }
43 break;
44 case Property::Point3:
45 if (N >= 4)
46 {
47 return QVariant::fromValue(points[3]);
48 }
49 break;
50 default:
51 break;
52 }
53 return BaseClass::getProperty(id);
54 }
55 void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) override
56 {
57 LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point0, {points[0] = value;})
58 LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point1, {points[1] = value;})
59 if constexpr (N >= 3)
60 {
61 LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point2, {points[2] = value;})
62 }
63 if constexpr (N >= 4)
64 {
65 LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point3, {points[3] = value;})
66 }
67 ColoredObject::setProperty(result, pair);
68 }
69 QDataStream &serialize(QDataStream& stream) const override
70 {
71 ColoredObject::serialize(stream);
72 for (const glm::vec3& point : this->points)
73 {
74 stream << point;
75 }
76 return stream;
77 }
78 QDataStream& deserialize(QDataStream& stream) override
79 {
80 ColoredObject::deserialize(stream);
81 for (glm::vec3& point : this->points)
82 {
83 stream >> point;
84 }
85 return stream;
86 }
87 std::array<glm::vec3, N> points;
88 };

mercurial