Moved automatic grid adjusting into a new action

Sun, 12 Sep 2021 12:14:32 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 12 Sep 2021 12:14:32 +0300
changeset 128
7c834fe36b25
parent 127
f64bfb7f5d26
child 129
f35843351601

Moved automatic grid adjusting into a new action

src/document.cpp file | annotate | diff | comparison | revisions
src/document.h file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
src/mainwindow.ui file | annotate | diff | comparison | revisions
src/ui/canvas.cpp file | annotate | diff | comparison | revisions
src/ui/canvas.h file | annotate | diff | comparison | revisions
--- a/src/document.cpp	Sun Aug 29 22:14:42 2021 +0300
+++ b/src/document.cpp	Sun Sep 12 12:14:32 2021 +0300
@@ -234,3 +234,8 @@
 		this->selectedTool->keyReleased(this, this->renderer, event);
 	}
 }
+
+void Document::adjustGridToView()
+{
+	this->renderer->adjustGrid();
+}
--- a/src/document.h	Sun Aug 29 22:14:42 2021 +0300
+++ b/src/document.h	Sun Sep 12 12:14:32 2021 +0300
@@ -46,6 +46,7 @@
 	Model::EditContext editModel();
 	void applyToVertices(VertexMap::ApplyFunction fn) const;
 	void handleKeyPress(QKeyEvent* event);
+	void adjustGridToView();
 Q_SIGNALS:
 	void newStatusText(const QString& newStatusText);
 	void splitterChanged();
--- a/src/mainwindow.cpp	Sun Aug 29 22:14:42 2021 +0300
+++ b/src/mainwindow.cpp	Sun Sep 12 12:14:32 2021 +0300
@@ -65,6 +65,13 @@
 	connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openModel);
 	connect(ui->actionQuit, &QAction::triggered, this, &QMainWindow::close);
 	connect(ui->actionSettingsEditor, &QAction::triggered, this, &MainWindow::runSettingsEditor);
+	connect(ui->actionAdjustGridToView, &QAction::triggered, [&]()
+	{
+		if (this->currentDocument() != nullptr)
+		{
+			this->currentDocument()->adjustGridToView();
+		}
+	});
 	for (auto data : ::renderStyleButtons)
 	{
 		QAction* action = data.memberInstance(this->ui.get());
--- a/src/mainwindow.ui	Sun Aug 29 22:14:42 2021 +0300
+++ b/src/mainwindow.ui	Sun Sep 12 12:14:32 2021 +0300
@@ -26,7 +26,7 @@
      <x>0</x>
      <y>0</y>
      <width>800</width>
-     <height>34</height>
+     <height>32</height>
     </rect>
    </property>
    <widget class="QMenu" name="menuFile">
@@ -54,6 +54,8 @@
     <addaction name="actionRenderStyleBfc"/>
     <addaction name="actionRenderStyleRandom"/>
     <addaction name="actionRenderStylePickScene"/>
+    <addaction name="separator"/>
+    <addaction name="actionAdjustGridToView"/>
    </widget>
    <addaction name="menuFile"/>
    <addaction name="menuView"/>
@@ -117,6 +119,14 @@
     <string>Pick scene colours</string>
    </property>
   </action>
+  <action name="actionAdjustGridToView">
+   <property name="text">
+    <string>Adjust grid to view</string>
+   </property>
+   <property name="shortcut">
+    <string>Ctrl+G</string>
+   </property>
+  </action>
  </widget>
  <resources/>
  <connections/>
--- a/src/ui/canvas.cpp	Sun Aug 29 22:14:42 2021 +0300
+++ b/src/ui/canvas.cpp	Sun Sep 12 12:14:32 2021 +0300
@@ -66,26 +66,6 @@
 		this->newStatusText("Position: <none>"_q);
 	}
 	*/
-	// use a relatively high threshold so that we know when the grid is somewhat perpendicular so we can
-	// automatically change it properly
-	if (isGridPerpendicularToScreen(0.03f))
-	{
-		const glm::vec3 cameraDirection = this->cameraVector();
-		const glm::vec3 vector_x = glm::normalize(this->gridMatrix * glm::vec4{1, 0, 0, 1});
-		const glm::vec3 vector_y = glm::normalize(this->gridMatrix * glm::vec4{0, 1, 0, 1});
-		const float angle_x = std::abs(glm::dot(vector_x, cameraDirection));
-		const float angle_y = std::abs(glm::dot(vector_y, cameraDirection));
-		if (angle_x < angle_y)
-		{
-			this->gridMatrix = glm::rotate(this->gridMatrix, PI<float> / 2, glm::vec3{1, 0, 0});
-		}
-		else
-		{
-			this->gridMatrix = glm::rotate(this->gridMatrix, PI<float> / 2, glm::vec3{0, 1, 0});
-		}
-		this->updateGridMatrix();
-		this->update();
-	}
 	Q_EMIT this->mouseMove(this, event);
 	PartRenderer::mouseMoveEvent(event);
 	this->update();
@@ -269,6 +249,28 @@
 	return this->worldPosition;
 }
 
+/**
+ * @brief Adjusts the grid to be so that it is perpendicular to the camera.
+ */
+void Canvas::adjustGridToView()
+{
+	const glm::vec3 cameraDirection = this->cameraVector();
+	const glm::vec3 vector_x = glm::normalize(this->gridMatrix * glm::vec4{1, 0, 0, 1});
+	const glm::vec3 vector_y = glm::normalize(this->gridMatrix * glm::vec4{0, 1, 0, 1});
+	const float angle_x = std::abs(glm::dot(vector_x, cameraDirection));
+	const float angle_y = std::abs(glm::dot(vector_y, cameraDirection));
+	if (angle_x < angle_y)
+	{
+		this->gridMatrix = glm::rotate(this->gridMatrix, PI<float> / 2, glm::vec3{1, 0, 0});
+	}
+	else
+	{
+		this->gridMatrix = glm::rotate(this->gridMatrix, PI<float> / 2, glm::vec3{0, 1, 0});
+	}
+	this->updateGridMatrix();
+	this->update();
+}
+
 void Canvas::drawWorldPoint(QPainter* painter, const glm::vec3& worldPoint) const
 {
 	const QPointF center = this->modelToScreenCoordinates(worldPoint);
--- a/src/ui/canvas.h	Sun Aug 29 22:14:42 2021 +0300
+++ b/src/ui/canvas.h	Sun Sep 12 12:14:32 2021 +0300
@@ -23,6 +23,7 @@
 	void drawWorldPoint(QPainter* painter, const glm::vec3& worldPoint) const;
 	void drawWorldPolygon(QPainter* painter, const std::vector<glm::vec3>& points);
 	const std::optional<glm::vec3>& getWorldPosition() const;
+	void adjustGrid();
 public Q_SLOTS:
 	void handleSelectionChange(const QSet<ldraw::id_t>& selectedIds, const QSet<ldraw::id_t>& deselectedIds);
 	void rebuildVertices(Document *document);

mercurial