src/ui/canvas.h

Sun, 25 Jul 2021 13:49:37 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 25 Jul 2021 13:49:37 +0300
changeset 108
94c92c923713
parent 107
02f142b399b1
child 109
40a1cf2f38f5
permissions
-rw-r--r--

work on editing tools

#pragma once
#include <QBrush>
#include <QPen>
#include "gl/partrenderer.h"
#include "gl/gridprogram.h"
#include "gl/axesprogram.h"

class Canvas : public PartRenderer
{
	Q_OBJECT
public:
	struct MouseClickInfo;
	struct MouseMoveInfo;
	enum PreviewLayerName : std::int8_t
	{
		DrawToolPreview
	};
	struct PreviewLayer
	{
		struct Point
		{
			glm::vec3 location;
			QBrush brush = {};
			QPen pen = {};
		};
		struct Polygon
		{
			geom::NPolygon geometry;
			QBrush brush = {};
			QPen pen = {};
		};
		QVector<Point> points;
		QVector<Polygon> polygons;
	};
	static constexpr int NUM_PREVIEW_LAYERS = 1;

	Canvas(
		Model* model,
		DocumentManager* documents,
		const ldraw::ColorTable& colorTable,
		QWidget* parent = nullptr);
	const PreviewLayer& getPreviewLayer(PreviewLayerName name) const;
	PreviewLayer& modifyPreviewLayer(PreviewLayerName name);
	void clearSelection();
	void addToSelection(ldraw::id_t id);
public slots:
	void handleSelectionChange(const QSet<ldraw::id_t>& selectedIds, const QSet<ldraw::id_t>& deselectedIds);
protected:
	void mouseMoveEvent(QMouseEvent* event) override;
	void mousePressEvent(QMouseEvent* event) override;
	void mouseReleaseEvent(QMouseEvent* event) override;
	void initializeGL() override;
	void paintGL() override;
signals:
	void newStatusText(const QString& newStatusText);
	void selectionChanged(const QSet<ldraw::id_t>& newSelection);
	void mouseClick(const MouseClickInfo& info);
	void mouseMove(const MouseMoveInfo& info);
private:
	void updateGridMatrix();
	glm::vec3 cameraVector() const;
	bool isGridPerpendicularToScreen(float threshold) const;
	std::optional<GridProgram> gridProgram;
	std::optional<AxesProgram> axesProgram;
	std::optional<glm::vec3> worldPosition;
	glm::mat4 gridMatrix;
	geom::Plane gridPlane;
	int totalMouseMove = 0;
	QSet<ldraw::id_t> selection;
	PreviewLayer previewLayers[NUM_PREVIEW_LAYERS];
};

struct Canvas::MouseClickInfo
{
	std::optional<glm::vec3> worldPosition;
	Canvas* invoker;
};

struct Canvas::MouseMoveInfo
{
	std::optional<glm::vec3> worldPosition;
	Canvas* invoker;
};

mercurial