src/linetypes/polygonobject.h

changeset 89
7abaf1d64719
child 93
6fe24fd945c0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/linetypes/polygonobject.h	Mon May 11 12:18:59 2020 +0300
@@ -0,0 +1,70 @@
+#pragma once
+#include "object.h"
+#include "widgets/vec3editor.h"
+#include "model.h"
+#include "modeleditcontext.h"
+
+namespace ldraw
+{
+	template<int N, typename>
+	class PolygonObject;
+}
+
+template<int N, typename = std::enable_if_t<(N > 0 and N <= 4)>>
+class ldraw::PolygonObject : public ColoredObject
+{
+public:
+	using BaseClass = ColoredObject;
+	PolygonObject(const std::array<glm::vec3, N>& points, const Color color) :
+		ColoredObject{color},
+		points{points} {}
+	int numPoints() const override
+	{
+		return N;
+	}
+	const glm::vec3& getPoint(int index) const override
+	{
+		Q_ASSERT(index >= 0 and index < N);
+		return this->points[index];
+	}
+	QVariant getProperty(const Property id) const override
+	{
+		switch (id)
+		{
+		case Property::Point0:
+			return QVariant::fromValue(points[0]);
+		case Property::Point1:
+			return QVariant::fromValue(points[1]);
+		case Property::Point2:
+			if (N >= 3)
+			{
+				return QVariant::fromValue(points[2]);
+			}
+			break;
+		case Property::Point3:
+			if (N >= 4)
+			{
+				return QVariant::fromValue(points[3]);
+			}
+			break;
+		default:
+			break;
+		}
+		return BaseClass::getProperty(id);
+	}
+	void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair)
+	{
+		LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point0, {points[0] = value;})
+		LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point1, {points[1] = value;})
+		if constexpr (N >= 3)
+		{
+			LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point2, {points[2] = value;})
+		}
+		if constexpr (N >= 4)
+		{
+			LDRAW_OBJECT_HANDLE_SET_PROPERTY(Point3, {points[3] = value;})
+		}
+		ColoredObject::setProperty(result, pair);
+	}
+	std::array<glm::vec3, N> points;
+};

mercurial