src/tools/drawtool.cpp

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 106
128efb9d148b
child 109
40a1cf2f38f5
permissions
-rw-r--r--

work on editing tools

#include <QMessageBox>
#include "drawtool.h"

static const QBrush brush = {Qt::white};
static const QPen pen = {Qt::black};
static const QBrush polygonBrush = {QColor{64, 255, 128}};

DrawTool::DrawTool(QObject* parent) :
	BaseTool{parent} {}

QString DrawTool::name() const
{
	static const QString result = tr("Draw");
	return result;
}

QString DrawTool::toolTip() const
{
	static const QString result = tr("Draw new elements into the model.");
	return result;
}

bool DrawTool::mouseClick(const Canvas::MouseClickInfo& info)
{
	if (info.worldPosition.has_value())
	{
		const glm::vec3& pos = info.worldPosition.value();
		const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);};
		if (any(this->polygon, isCloseToPos))
		{
			QMessageBox::information(nullptr, "test", "close the polygon");
		}
		else
		{
			this->polygon.push_back(pos);
			auto& previewLayer = info.invoker->modifyPreviewLayer(Canvas::DrawToolPreview);
			previewLayer.points.resize(this->polygon.size());
			previewLayer.points.back() = {pos, brush, pen};
			previewLayer.polygons.clear();
			previewLayer.polygons.push_back({geom::NPolygon{this->polygon}, polygonBrush, pen});
			if (this->polygon.size() == 4)
			{
				QMessageBox::information(nullptr, "test", "close the polygon");
			}
		}
	}
	return true;
}

bool DrawTool::mouseMove(const Canvas::MouseMoveInfo& info)
{
	if (this->polygon.size() < 4 and info.worldPosition.has_value())
	{
		auto& previewLayer = info.invoker->modifyPreviewLayer(Canvas::DrawToolPreview);
		previewLayer.points.resize(this->polygon.size() + 1);
		previewLayer.points.back() = {info.worldPosition.value(), brush, pen};
		if (previewLayer.polygons.size() > 0)
		{
			auto& polygon = previewLayer.polygons.back();
			polygon.geometry.points.resize(this->polygon.size() + 1);
			polygon.geometry.points.back() = info.worldPosition.value();
		}
		return true;
	}
	else
	{
		return false;
	}
}

void DrawTool::reset()
{
	this->polygon.clear();
}

mercurial