Moved more editing-related methods to Canvas.

Thu, 09 Feb 2017 13:26:44 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 09 Feb 2017 13:26:44 +0200
changeset 1108
90d6f4b7782e
parent 1107
97279c658e2b
child 1109
a55b2608a395

Moved more editing-related methods to Canvas.

src/canvas.cpp file | annotate | diff | comparison | revisions
src/canvas.h file | annotate | diff | comparison | revisions
src/glRenderer.cpp file | annotate | diff | comparison | revisions
src/glRenderer.h file | annotate | diff | comparison | revisions
--- a/src/canvas.cpp	Thu Feb 09 13:17:22 2017 +0200
+++ b/src/canvas.cpp	Thu Feb 09 13:26:44 2017 +0200
@@ -19,9 +19,11 @@
 #include <QMouseEvent>
 #include "canvas.h"
 #include "documentmanager.h"
+#include "grid.h"
 #include "ldDocument.h"
 #include "mainwindow.h"
 #include "messageLog.h"
+#include "miscallenous.h"
 #include "primitives.h"
 
 Canvas::Canvas(LDDocument* document, QWidget* parent) :
@@ -204,3 +206,107 @@
 	linepen.setColor(luma(backgroundColor()) < 40 ? Qt::white : Qt::black);
 	return linepen;
 }
+
+
+int Canvas::depthNegateFactor() const
+{
+	return cameraInfo(camera()).negatedDepth ? -1 : 1;
+}
+
+// =============================================================================
+//
+void Canvas::getRelativeAxes(Axis& relativeX, Axis& relativeY) const
+{
+	const CameraInfo& camera = cameraInfo(this->camera());
+	relativeX = camera.localX;
+	relativeY = camera.localY;
+}
+
+// =============================================================================
+//
+Axis Canvas::getRelativeZ() const
+{
+	const CameraInfo& camera = cameraInfo(this->camera());
+	return static_cast<Axis>(3 - camera.localX - camera.localY);
+}
+
+// =============================================================================
+//
+void Canvas::setDepthValue (double depth)
+{
+	if (camera() < FreeCamera)
+		m_depthValues[camera()] = depth;
+}
+
+// =============================================================================
+//
+double Canvas::getDepthValue() const
+{
+	if (camera() < FreeCamera)
+		return m_depthValues[camera()];
+	else
+		return 0.0;
+}
+
+/*
+ * This converts a 2D point on the screen to a 3D point in the model. If 'snap' is true, the 3D point will snap to the current grid.
+ */
+Vertex Canvas::convert2dTo3d(const QPoint& position2d, bool snap) const
+{
+	if (camera() == FreeCamera)
+	{
+		return {0, 0, 0};
+	}
+	else
+	{
+		Vertex position3d;
+		const CameraInfo& camera = cameraInfo(this->camera());
+		Axis axisX = camera.localX;
+		Axis axisY = camera.localY;
+		int signX = camera.negatedX ? -1 : 1;
+		int signY = camera.negatedY ? -1 : 1;
+
+		// Calculate cx and cy - these are the LDraw unit coords the cursor is at.
+		double cx = (-virtualWidth() + ((2 * position2d.x() * virtualWidth()) / width()) - panning(X));
+		double cy = (virtualHeight() - ((2 * position2d.y() * virtualHeight()) / height()) - panning(Y));
+
+		if (snap)
+		{
+			cx = grid()->snap(cx, Grid::Coordinate);
+			cy = grid()->snap(cy, Grid::Coordinate);
+		}
+
+		cx *= signX;
+		cy *= signY;
+		roundToDecimals(cx, 4);
+		roundToDecimals(cy, 4);
+
+		// Create the vertex from the coordinates
+		position3d.setCoordinate(axisX, cx);
+		position3d.setCoordinate(axisY, cy);
+		position3d.setCoordinate(static_cast<Axis>(3 - axisX - axisY), getDepthValue());
+		return position3d;
+	}
+}
+
+/*
+ * Inverse operation for the above - convert a 3D position to a 2D screen position.
+ */
+QPoint Canvas::convert3dTo2d(const Vertex& position3d) const
+{
+	if (camera() == FreeCamera)
+	{
+		return {0, 0};
+	}
+	else
+	{
+		const CameraInfo& camera = cameraInfo(this->camera());
+		Axis axisX = camera.localX;
+		Axis axisY = camera.localY;
+		int signX = camera.negatedX ? -1 : 1;
+		int signY = camera.negatedY ? -1 : 1;
+		int rx = (((position3d[axisX] * signX) + virtualWidth() + panning(X)) * width()) / (2 * virtualWidth());
+		int ry = (((position3d[axisY] * signY) - virtualHeight() + panning(Y)) * height()) / (2 * virtualHeight());
+		return {rx, -ry};
+	}
+}
\ No newline at end of file
--- a/src/canvas.h	Thu Feb 09 13:17:22 2017 +0200
+++ b/src/canvas.h	Thu Feb 09 13:26:44 2017 +0200
@@ -26,13 +26,20 @@
 	Canvas(LDDocument* document, QWidget* parent = nullptr);
 	~Canvas();
 
+	Vertex convert2dTo3d(const QPoint& pos2d, bool snap) const;
+	QPoint convert3dTo2d(const Vertex& pos3d) const;
 	EditModeType currentEditModeType() const;
+	int depthNegateFactor() const;
 	LDDocument* document() const;
 	void drawPoint(QPainter& painter, QPointF pos, QColor color = QColor (64, 192, 0)) const;
 	void drawBlipCoordinates(QPainter& painter, const Vertex& pos3d) const;
 	void drawBlipCoordinates(QPainter& painter, const Vertex& pos3d, QPointF pos) const;
+	double getDepthValue() const;
+	void getRelativeAxes(Axis& relX, Axis& relY) const;
+	Axis getRelativeZ() const;
 	QPen linePen() const;
 	const Vertex& position3D() const;
+	void setDepthValue(double depth);
 	void setEditMode(EditModeType type);
 
 protected:
@@ -49,4 +56,5 @@
 	LDDocument& m_document;
 	AbstractEditMode* m_currentEditMode = nullptr;
 	Vertex m_position3D;
+	double m_depthValues[6] = {0};
 };
--- a/src/glRenderer.cpp	Thu Feb 09 13:17:22 2017 +0200
+++ b/src/glRenderer.cpp	Thu Feb 09 13:26:44 2017 +0200
@@ -498,71 +498,6 @@
 	}
 }
 
-// =============================================================================
-//
-// This converts a 2D point on the screen to a 3D point in the model. If 'snap'
-// is true, the 3D point will snap to the current grid.
-//
-Vertex GLRenderer::convert2dTo3d (const QPoint& position2d, bool snap) const
-{
-	if (camera() == FreeCamera)
-	{
-		return {0, 0, 0};
-	}
-	else
-	{
-		Vertex position3d;
-		const CameraInfo* camera = &g_cameraInfo[this->camera()];
-		Axis axisX = camera->localX;
-		Axis axisY = camera->localY;
-		int signX = camera->negatedX ? -1 : 1;
-		int signY = camera->negatedY ? -1 : 1;
-
-		// Calculate cx and cy - these are the LDraw unit coords the cursor is at.
-		double cx = (-m_virtualWidth + ((2 * position2d.x() * m_virtualWidth) / m_width) - panning(X));
-		double cy = (m_virtualHeight - ((2 * position2d.y() * m_virtualHeight) / m_height) - panning(Y));
-
-		if (snap)
-		{
-			cx = grid()->snap(cx, Grid::Coordinate);
-			cy = grid()->snap(cy, Grid::Coordinate);
-		}
-
-		cx *= signX;
-		cy *= signY;
-		roundToDecimals(cx, 4);
-		roundToDecimals(cy, 4);
-
-		// Create the vertex from the coordinates
-		position3d.setCoordinate(axisX, cx);
-		position3d.setCoordinate(axisY, cy);
-		position3d.setCoordinate(static_cast<Axis>(3 - axisX - axisY), getDepthValue());
-		return position3d;
-	}
-}
-
-/*
- * Inverse operation for the above - convert a 3D position to a 2D screen position.
- */
-QPoint GLRenderer::convert3dTo2d(const Vertex& position3d) const
-{
-	if (camera() == FreeCamera)
-	{
-		return {0, 0};
-	}
-	else
-	{
-		const CameraInfo* camera = &g_cameraInfo[this->camera()];
-		Axis axisX = camera->localX;
-		Axis axisY = camera->localY;
-		int signX = camera->negatedX ? -1 : 1;
-		int signY = camera->negatedY ? -1 : 1;
-		int rx = (((position3d[axisX] * signX) + m_virtualWidth + panning(X)) * m_width) / (2 * m_virtualWidth);
-		int ry = (((position3d[axisY] * signY) - m_virtualHeight + panning(Y)) * m_height) / (2 * m_virtualHeight);
-		return {rx, -ry};
-	}
-}
-
 QPen GLRenderer::textPen() const
 {
 	return {m_useDarkBackground ? Qt::white : Qt::black};
@@ -894,23 +829,6 @@
 
 // =============================================================================
 //
-void GLRenderer::getRelativeAxes(Axis& relativeX, Axis& relativeY) const
-{
-	const CameraInfo* camera = &g_cameraInfo[this->camera()];
-	relativeX = camera->localX;
-	relativeY = camera->localY;
-}
-
-// =============================================================================
-//
-Axis GLRenderer::getRelativeZ() const
-{
-	const CameraInfo* camera = &g_cameraInfo[this->camera()];
-	return static_cast<Axis>(3 - camera->localX - camera->localY);
-}
-
-// =============================================================================
-//
 void GLRenderer::compileObject (LDObject* obj)
 {
 	compiler()->stageForCompilation (obj);
@@ -972,24 +890,6 @@
 
 // =============================================================================
 //
-void GLRenderer::setDepthValue (double depth)
-{
-	if (camera() < FreeCamera)
-		m_depthValues[camera()] = depth;
-}
-
-// =============================================================================
-//
-double GLRenderer::getDepthValue() const
-{
-	if (camera() < FreeCamera)
-		return m_depthValues[camera()];
-	else
-		return 0.0;
-}
-
-// =============================================================================
-//
 QString GLRenderer::cameraName (Camera camera) const
 {
 	switch (camera)
@@ -1184,11 +1084,6 @@
 	return m_mousePositionF;
 }
 
-int GLRenderer::depthNegateFactor() const
-{
-	return g_cameraInfo[camera()].negatedDepth ? -1 : 1;
-}
-
 Qt::KeyboardModifiers GLRenderer::keyboardModifiers() const
 {
 	return m_currentKeyboardModifiers;
@@ -1230,3 +1125,13 @@
 {
 	return m_lastButtons;
 }
+
+double GLRenderer::virtualHeight() const
+{
+	return m_virtualHeight;
+}
+
+double GLRenderer::virtualWidth() const
+{
+	return m_virtualWidth;
+}
--- a/src/glRenderer.h	Thu Feb 09 13:17:22 2017 +0200
+++ b/src/glRenderer.h	Thu Feb 09 13:26:44 2017 +0200
@@ -34,6 +34,8 @@
 
 struct CameraInfo
 {
+	CameraInfo(const CameraInfo&) = delete;
+
 	int glrotate[3];
 	Axis localX;
 	Axis localY;
@@ -80,16 +82,10 @@
 	QByteArray capturePixels();
 	void compileObject(LDObject* obj);
 	GLCompiler* compiler() const;
-	Vertex convert2dTo3d(const QPoint& pos2d, bool snap) const;
-	QPoint convert3dTo2d(const Vertex& pos3d) const;
 	QString currentCameraName() const;
-	int depthNegateFactor() const;
 	void drawGLScene();
 	void forgetObject(LDObject* obj);
 	Axis getCameraAxis(bool y, Camera camid = (Camera) -1);
-	double getDepthValue() const;
-	void getRelativeAxes(Axis& relX, Axis& relY) const;
-	Axis getRelativeZ() const;
 	void hardRefresh();
 	void highlightCursorObject();
 	void initGLData();
@@ -109,10 +105,11 @@
 	void resetAngles();
 	void setBackground();
 	void setCamera(Camera cam);
-	void setDepthValue(double depth);
 	void setDrawOnly(bool value);
 	void setPicking(bool a);
 	QPen textPen() const;
+	double virtualHeight() const;
+	double virtualWidth() const;
 	void zoomNotch(bool inward);
 
 	static const QPen thinBorderPen;
@@ -160,7 +157,6 @@
 	double m_panX[7] = {0};
 	double m_panY[7] = {0};
 	double m_zoom[7] = {30};
-	double m_depthValues[6];
 	bool m_useDarkBackground = false;
 	bool m_drawToolTip = false;
 	bool m_takingScreenCapture = false;

mercurial