src/linetypes/polygonobject.h

Sun, 13 Mar 2022 18:46:10 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 13 Mar 2022 18:46:10 +0200
changeset 180
5b7a8f2270ff
parent 153
2f79053c2e9a
permissions
-rw-r--r--

Handle properties in a generic manner in the object editor

#pragma once
#include "object.h"
#include "widgets/vec3editor.h"
#include "model.h"
#include "modeleditor.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) override
	{
		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);
	}
	QDataStream &serialize(QDataStream& stream) const override
	{
		ColoredObject::serialize(stream);
		for (const glm::vec3& point : this->points)
		{
			stream << point;
		}
		return stream;
	}
	QDataStream& deserialize(QDataStream& stream) override
	{
		ColoredObject::deserialize(stream);
		for (glm::vec3& point : this->points)
		{
			stream >> point;
		}
		return stream;
	}
	std::array<glm::vec3, N> points;
};

mercurial