|      1 #include <QMessageBox> | 
     1 #include <QMessageBox> | 
|      2 #include "drawtool.h" | 
     2 #include "drawtool.h" | 
|      3  | 
     3  | 
|      4 static const QBrush brush = {Qt::white}; | 
     4 static const QBrush pointBrush = {Qt::white}; | 
|      5 static const QPen pen = {Qt::black}; | 
     5 static const QPen polygonPen = {QBrush{Qt::black}, 2.0, Qt::DashLine}; | 
|      6 static const QBrush polygonBrush = {QColor{64, 255, 128}}; | 
     6 static const QPen pointPen = {QBrush{Qt::black}, 2.0}; | 
|         | 
     7 static const QBrush polygonBrush = {QColor{64, 255, 128, 192}}; | 
|      7  | 
     8  | 
|      8 DrawTool::DrawTool(QObject* parent) : | 
     9 DrawTool::DrawTool(QObject* parent) : | 
|      9 	BaseTool{parent} {} | 
    10 	BaseTool{parent} {} | 
|     10  | 
    11  | 
|     11 QString DrawTool::name() const | 
    12 QString DrawTool::name() const | 
|     26 	{ | 
    27 	{ | 
|     27 		const glm::vec3& pos = info.worldPosition.value(); | 
    28 		const glm::vec3& pos = info.worldPosition.value(); | 
|     28 		const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);}; | 
    29 		const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);}; | 
|     29 		if (any(this->polygon, isCloseToPos)) | 
    30 		if (any(this->polygon, isCloseToPos)) | 
|     30 		{ | 
    31 		{ | 
|     31 			QMessageBox::information(nullptr, "test", "close the polygon"); | 
    32 			this->closeShape(); | 
|     32 		} | 
    33 		} | 
|     33 		else | 
    34 		else | 
|     34 		{ | 
    35 		{ | 
|     35 			this->polygon.push_back(pos); | 
    36 			this->polygon.push_back(pos); | 
|     36 			auto& previewLayer = info.invoker->modifyPreviewLayer(Canvas::DrawToolPreview); | 
        | 
|     37 			previewLayer.points.resize(this->polygon.size()); | 
        | 
|     38 			previewLayer.points.back() = {pos, brush, pen}; | 
        | 
|     39 			previewLayer.polygons.clear(); | 
        | 
|     40 			previewLayer.polygons.push_back({geom::NPolygon{this->polygon}, polygonBrush, pen}); | 
        | 
|     41 			if (this->polygon.size() == 4) | 
    37 			if (this->polygon.size() == 4) | 
|     42 			{ | 
    38 			{ | 
|     43 				QMessageBox::information(nullptr, "test", "close the polygon"); | 
    39 				this->closeShape(); | 
|     44 			} | 
    40 			} | 
|     45 		} | 
    41 		} | 
|     46 	} | 
    42 	} | 
|         | 
    43 	this->previewPolygon = this->polygon; | 
|     47 	return true; | 
    44 	return true; | 
|     48 } | 
    45 } | 
|     49  | 
    46  | 
|     50 bool DrawTool::mouseMove(const Canvas::MouseMoveInfo& info) | 
    47 bool DrawTool::mouseMove(const Canvas::MouseMoveInfo& info) | 
|     51 { | 
    48 { | 
|     52 	if (this->polygon.size() < 4 and info.worldPosition.has_value()) | 
    49 	if (info.worldPosition.has_value()) | 
|     53 	{ | 
    50 	{ | 
|     54 		auto& previewLayer = info.invoker->modifyPreviewLayer(Canvas::DrawToolPreview); | 
    51 		this->previewPoint = info.worldPosition.value(); | 
|     55 		previewLayer.points.resize(this->polygon.size() + 1); | 
    52 		if (this->polygon.size() < 4) | 
|     56 		previewLayer.points.back() = {info.worldPosition.value(), brush, pen}; | 
        | 
|     57 		if (previewLayer.polygons.size() > 0) | 
        | 
|     58 		{ | 
    53 		{ | 
|     59 			auto& polygon = previewLayer.polygons.back(); | 
    54 			this->previewPolygon.resize(this->polygon.size() + 1); | 
|     60 			polygon.geometry.points.resize(this->polygon.size() + 1); | 
    55 			this->previewPolygon.back() = this->previewPoint; | 
|     61 			polygon.geometry.points.back() = info.worldPosition.value(); | 
        | 
|     62 		} | 
    56 		} | 
|     63 		return true; | 
        | 
|     64 	} | 
    57 	} | 
|     65 	else | 
    58 	return false; | 
|     66 	{ | 
        | 
|     67 		return false; | 
        | 
|     68 	} | 
        | 
|     69 } | 
    59 } | 
|     70  | 
    60  | 
|     71 void DrawTool::reset() | 
    61 void DrawTool::reset() | 
|     72 { | 
    62 { | 
|     73 	this->polygon.clear(); | 
    63 	this->polygon.clear(); | 
|     74 } | 
    64 } | 
|         | 
    65  | 
|         | 
    66 void DrawTool::overpaint(Canvas* canvas, QPainter* painter) const | 
|         | 
    67 { | 
|         | 
    68 	painter->setBrush(::polygonBrush); | 
|         | 
    69 	painter->setPen(::polygonPen); | 
|         | 
    70 	canvas->drawWorldPolygon(painter, this->previewPolygon); | 
|         | 
    71 	painter->setBrush(::pointBrush); | 
|         | 
    72 	painter->setPen(::pointPen); | 
|         | 
    73 	for (const glm::vec3& point : this->polygon) | 
|         | 
    74 	{ | 
|         | 
    75 		canvas->drawWorldPoint(painter, point); | 
|         | 
    76 	} | 
|         | 
    77 	if (this->polygon.size() < 4) | 
|         | 
    78 	{ | 
|         | 
    79 		canvas->drawWorldPoint(painter, this->previewPoint); | 
|         | 
    80 	} | 
|         | 
    81 } | 
|         | 
    82  | 
|         | 
    83 void DrawTool::closeShape() | 
|         | 
    84 { | 
|         | 
    85 	QMessageBox::information(nullptr, "test", "close the polygon"); | 
|         | 
    86 } |