src/ui/canvas.cpp

Fri, 28 Feb 2020 19:24:33 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 28 Feb 2020 19:24:33 +0200
changeset 63
f7dd937667a5
parent 61
4585d8d7a7ec
child 64
f99d52b1646b
permissions
-rw-r--r--

omg functional programming

#include <QMouseEvent>
#include <QPainter>
#include "canvas.h"

Canvas::Canvas(
	Model* model,
	DocumentManager* documents,
	const ldraw::ColorTable& colorTable,
	QWidget* parent) :
	PartRenderer{model, documents, colorTable, parent},
	gridProgram{this}
{
	this->setMouseTracking(true);
}

void Canvas::handleSelectionChange(const QSet<ldraw::Id>& selectedIds, const QSet<ldraw::Id>& deselectedIds)
{
	Q_ASSERT(not selectedIds.contains(ldraw::NULL_ID));
	this->selection.subtract(deselectedIds);
	this->selection.unite(selectedIds);
	this->compiler->setSelectedObjects(this->selection);
	this->update();
}

QString vectorToString(const glm::vec3& vec)
{
	return "(%1, %2, %3)"_q
		.arg(toDouble(vec.x))
		.arg(toDouble(vec.y))
		.arg(toDouble(vec.z));
}

void Canvas::mouseMoveEvent(QMouseEvent* event)
{
	const ldraw::Id id = this->pick(event->pos());
	this->highlighted = id;
	this->totalMouseMove += (event->pos() - this->lastMousePosition).manhattanLength();
	this->worldPosition = this->screenToModelCoordinates(event->pos(), geom::XY);
	if (this->worldPosition.has_value())
	{
		this->worldPosition = glm::round(*this->worldPosition);
	}
	if (this->worldPosition.has_value())
	{
		this->newStatusText("Position: (%1, %2, %3)"_q
			.arg(toDouble(this->worldPosition->x))
			.arg(toDouble(this->worldPosition->y))
			.arg(toDouble(this->worldPosition->z)));
	}
	else
	{
		this->newStatusText("Position: <none>"_q);
	}
	PartRenderer::mouseMoveEvent(event);
}

void Canvas::mousePressEvent(QMouseEvent* event)
{
	this->totalMouseMove = 0;
	this->lastMousePosition = event->pos();
	PartRenderer::mousePressEvent(event);
}

void Canvas::mouseReleaseEvent(QMouseEvent* event)
{
	if (this->totalMouseMove < (2.0 / sqrt(2)) * 5.0)
	{
		if (this->highlighted == ldraw::NULL_ID)
		{
			this->selection = {};
		}
		else
		{
			this->selection = {this->highlighted};
		}
		this->compiler->setSelectedObjects(this->selection);
		emit selectionChanged(this->selection);
		this->update();
	}
	PartRenderer::mouseReleaseEvent(event);
}

void Canvas::initializeGL()
{
	// We first create the grid program and connect everything and only then call the part renderer's initialization
	// functions so that when initialization sets up, the signals also set up the matrices on our side.
	this->gridProgram.emplace(this);
	this->gridProgram->initialize();
	connect(this, &PartRenderer::projectionMatrixChanged, [&]()
	{
		this->gridProgram->setProjectionMatrix(this->projectionMatrix);
	});
	connect(this, &PartRenderer::modelMatrixChanged, [&]()
	{
		this->gridProgram->setModelMatrix(this->modelMatrix);
	});
	connect(this, &PartRenderer::viewMatrixChanged, [&]()
	{
		this->gridProgram->setViewMatrix(this->viewMatrix);
	});
	connect(this, &PartRenderer::renderPreferencesChanged, [&]()
	{
		if (this->gridProgram.has_value())
		{
			const bool isDark = luma(this->renderPreferences.backgroundColor) < 0.25;
			this->gridProgram->setGridColor(isDark ? Qt::white : Qt::black);
		}
	});
	PartRenderer::initializeGL();
}

void Canvas::paintGL()
{
	PartRenderer::paintGL();
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	this->gridProgram->draw();
	glDisable(GL_BLEND);
	if (this->worldPosition.has_value())
	{
		QPainter painter{this};
		painter.setRenderHint(QPainter::Antialiasing);
		painter.setPen(Qt::black);
		painter.setBrush(Qt::green);
		const QPointF pos = this->modelToScreenCoordinates(*this->worldPosition);
		painter.drawEllipse(pos, 5, 5);
		painter.setPen(Qt::white);
		painter.drawText(pos + QPointF{5, 5}, vectorToString(*this->worldPosition));
	}
}

mercurial