src/layers/edittools.cpp

changeset 305
d891da20abca
parent 301
8ccd6fdb30dc
child 306
6ad27b7d2697
--- a/src/layers/edittools.cpp	Tue Jun 28 22:18:11 2022 +0300
+++ b/src/layers/edittools.cpp	Wed Jun 29 14:11:58 2022 +0300
@@ -141,7 +141,19 @@
 	painter->drawPolygon(QPolygonF{convertWorldPointsToScreenPoints(points, renderer)});
 }
 
-static std::vector<std::vector<glm::vec3>> modelActionPoints(const ModelAction& action)
+//! \brief Conversion function from PlainPolygonElement to ModelElement
+ModelElement elementFromPolygonAndColor(const PlainPolygonElement& poly, ColorIndex color)
+{
+	// use std::visit with a templated lambda to resolve the type of poly.
+	return std::visit([color](const auto& resolvedPoly) -> ModelElement {
+		// unlike with normal templates we need to pry out the type out manually
+		using PolygonType = std::decay_t<decltype(resolvedPoly)>;
+		// add color and return as a model element.
+		return Colored<PolygonType>{resolvedPoly, color};
+	}, poly);
+}
+
+static std::vector<std::vector<glm::vec3>> polygonsToBeInserted(const ModelAction& action)
 {
 	std::vector<std::vector<glm::vec3>> result;
 	if (const AppendToModel* append = std::get_if<AppendToModel>(&action)) {
@@ -156,8 +168,10 @@
 			result.push_back({quad->p1, quad->p2, quad->p3, quad->p4});
 		}
 		else if (const CircularPrimitive* circ = std::get_if<Colored<CircularPrimitive>>(&newElement)) {
-			rasterize(*circ, [&](const ModelElement& element){
-				const auto& subpoints = modelActionPoints(AppendToModel{element});
+			// rasterize the circle down to polygons, and append them to the result.
+			rasterize(*circ, [&](const PlainPolygonElement& poly, const ColorIndex color){
+				AppendToModel append{elementFromPolygonAndColor(poly, color)};
+				const auto& subpoints = polygonsToBeInserted(append);
 				std::copy(subpoints.begin(), subpoints.end(), std::back_inserter(result));
 			});
 		}
@@ -232,7 +246,7 @@
 	const Pens& pens = *reinterpret_cast<const Pens*>(pensptr);
 	painter->setPen(pens.polygonPen);
 	for (const ModelAction& action : this->modelActions()) {
-		for (const std::vector<glm::vec3>& points : modelActionPoints(action)) {
+		for (const std::vector<glm::vec3>& points : polygonsToBeInserted(action)) {
 			if (points.size() == 2) {
 				drawWorldPolyline(painter, points, renderer);
 			}

mercurial