src/ui/canvas.cpp

changeset 170
9b655f6fe5a1
parent 169
6da096930534
child 172
50f055543ff6
--- a/src/ui/canvas.cpp	Sat Mar 05 17:18:44 2022 +0200
+++ b/src/ui/canvas.cpp	Sat Mar 05 18:26:18 2022 +0200
@@ -120,6 +120,7 @@
 	if (this->renderPreferences.style != gl::RenderStyle::PickScene)
 	{
 		// Render axes
+		if (this->renderPreferences.drawAxes)
 		{
 			glLineWidth(5);
 			glEnable(GL_LINE_SMOOTH);
@@ -161,56 +162,9 @@
 		{
 			QPainter painter{this};
 			painter.setRenderHint(QPainter::Antialiasing);
-			QFont font;
-			//font.setStyle(QFont::StyleItalic);
-			painter.setFont(font);
-			QFontMetrics fontMetrics{font};
-			const auto renderText = [&](const QString& text, const geom::PointOnRectagle& intersection)
+			if (this->renderPreferences.drawAxes)
 			{
-				QPointF position = toQPointF(intersection.position);
-				const geom::RectangleSide side = intersection.side;
-				switch (side)
-				{
-				case geom::RectangleSide::Top:
-					position += QPointF{0, static_cast<qreal>(fontMetrics.ascent())};
-					break;
-				case geom::RectangleSide::Left:
-					break;
-				case geom::RectangleSide::Bottom:
-					position += QPointF{0, static_cast<qreal>(-fontMetrics.descent())};
-					break;
-				case geom::RectangleSide::Right:
-					position += QPointF{static_cast<qreal>(-fontMetrics.horizontalAdvance(text)), 0};
-					break;
-				}
-				painter.drawText(position, text);
-			};
-			const QRectF box {QPointF{0, 0}, sizeToSizeF(this->size())};
-			const QPointF p1 = this->modelToScreenCoordinates(glm::vec3{0, 0, 0});
-
-			static const struct
-			{
-				QString text;
-				glm::vec3 direction;
-			} directions[] =
-			{
-				{"+𝑥", {1, 0, 0}},
-				{"-𝑥", {-1, 0, 0}},
-				{"+𝑦", {0, 1, 0}},
-				{"-𝑦", {0, -1, 0}},
-				{"+𝑧", {0, 0, 1}},
-				{"-𝑧", {0, 0, -1}},
-			};
-			for (const auto& axis : directions)
-			{
-				const QPointF x_p = this->modelToScreenCoordinates(axis.direction);
-				const auto intersection = geom::rayRectangleIntersection(
-					geom::rayFromPoints(toVec2(p1), toVec2(x_p)),
-					box);
-				if (intersection.has_value())
-				{
-					renderText(axis.text, *intersection);
-				}
+				this->renderAxesLabels(painter);
 			}
 			if (this->overpaintCallback != nullptr)
 			{
@@ -221,6 +175,64 @@
 }
 
 /**
+ * @brief Renders labels such as +x at the ends of axes at the screen
+ * @param painter
+ */
+void Canvas::renderAxesLabels(QPainter& painter)
+{
+	QFont font;
+	//font.setStyle(QFont::StyleItalic);
+	painter.setFont(font);
+	QFontMetrics fontMetrics{font};
+	const auto renderText = [&](const QString& text, const geom::PointOnRectagle& intersection)
+	{
+		QPointF position = toQPointF(intersection.position);
+		const geom::RectangleSide side = intersection.side;
+		switch (side)
+		{
+		case geom::RectangleSide::Top:
+			position += QPointF{0, static_cast<qreal>(fontMetrics.ascent())};
+			break;
+		case geom::RectangleSide::Left:
+			break;
+		case geom::RectangleSide::Bottom:
+			position += QPointF{0, static_cast<qreal>(-fontMetrics.descent())};
+			break;
+		case geom::RectangleSide::Right:
+			position += QPointF{static_cast<qreal>(-fontMetrics.horizontalAdvance(text)), 0};
+			break;
+		}
+		painter.drawText(position, text);
+	};
+	const QRectF box {QPointF{0, 0}, sizeToSizeF(this->size())};
+	const QPointF p1 = this->modelToScreenCoordinates(glm::vec3{0, 0, 0});
+	static const struct
+	{
+		QString text;
+		glm::vec3 direction;
+	} directions[] =
+	{
+		{"+𝑥", {1, 0, 0}},
+		{"-𝑥", {-1, 0, 0}},
+		{"+𝑦", {0, 1, 0}},
+		{"-𝑦", {0, -1, 0}},
+		{"+𝑧", {0, 0, 1}},
+		{"-𝑧", {0, 0, -1}},
+	};
+	for (const auto& axis : directions)
+	{
+		const QPointF x_p = this->modelToScreenCoordinates(axis.direction);
+		const auto intersection = geom::rayRectangleIntersection(
+			geom::rayFromPoints(toVec2(p1), toVec2(x_p)),
+			box);
+		if (intersection.has_value())
+		{
+			renderText(axis.text, *intersection);
+		}
+	}
+}
+
+/**
  * @brief Draws a polyline to where the specified vector of 3D points would appear on the screen.
  * @param painter Painter to use to draw with
  * @param points 3D points to render

mercurial