# HG changeset patch # User Teemu Piippo # Date 1631438072 -10800 # Node ID 7c834fe36b2595ee6b7e71d904dbb4b3a53e8887 # Parent f64bfb7f5d26c47b0ff1665a3a505423c0cffb4e Moved automatic grid adjusting into a new action diff -r f64bfb7f5d26 -r 7c834fe36b25 src/document.cpp --- 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(); +} diff -r f64bfb7f5d26 -r 7c834fe36b25 src/document.h --- 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(); diff -r f64bfb7f5d26 -r 7c834fe36b25 src/mainwindow.cpp --- 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()); diff -r f64bfb7f5d26 -r 7c834fe36b25 src/mainwindow.ui --- 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 @@ 0 0 800 - 34 + 32 @@ -54,6 +54,8 @@ + + @@ -117,6 +119,14 @@ Pick scene colours + + + Adjust grid to view + + + Ctrl+G + + diff -r f64bfb7f5d26 -r 7c834fe36b25 src/ui/canvas.cpp --- 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: "_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 / 2, glm::vec3{1, 0, 0}); - } - else - { - this->gridMatrix = glm::rotate(this->gridMatrix, PI / 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 / 2, glm::vec3{1, 0, 0}); + } + else + { + this->gridMatrix = glm::rotate(this->gridMatrix, PI / 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); diff -r f64bfb7f5d26 -r 7c834fe36b25 src/ui/canvas.h --- 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& points); const std::optional& getWorldPosition() const; + void adjustGrid(); public Q_SLOTS: void handleSelectionChange(const QSet& selectedIds, const QSet& deselectedIds); void rebuildVertices(Document *document);