From a346b1d9a83055a3714f13e3c43fe63d27ffcd71 Mon Sep 17 00:00:00 2001 From: maelstrom Date: Sun, 23 Feb 2025 16:45:16 +0100 Subject: [PATCH] refactor: handle inactive when select tool is selected --- core/src/objects/handles.h | 1 + core/src/rendering/renderer.cpp | 2 +- editor/editorcommon.h | 11 +---------- editor/mainglwidget.cpp | 8 ++++---- editor/mainwindow.cpp | 20 +++++++------------- editor/mainwindow.h | 8 ++++++++ 6 files changed, 22 insertions(+), 28 deletions(-) diff --git a/core/src/objects/handles.h b/core/src/objects/handles.h index e4c7885..327c51f 100644 --- a/core/src/objects/handles.h +++ b/core/src/objects/handles.h @@ -28,6 +28,7 @@ class Handles : public Instance { public: const static InstanceType TYPE; + bool active; std::optional> adornee; // inline std::optional> GetAdornee() { return adornee; } // inline void SetAdornee(std::optional> newAdornee) { this->adornee = newAdornee; updateAdornee(); }; diff --git a/core/src/rendering/renderer.cpp b/core/src/rendering/renderer.cpp index f89cdd0..2972858 100644 --- a/core/src/rendering/renderer.cpp +++ b/core/src/rendering/renderer.cpp @@ -152,7 +152,7 @@ void renderSkyBox() { void renderHandles() { // if (getSelection().size() == 0) return; // if (getSelection()[0].lock()->GetClass() != &Part::TYPE) return; - if (!editorToolHandles->adornee.has_value()) return; + if (!editorToolHandles->adornee.has_value() || !editorToolHandles->active) return; glDepthMask(GL_TRUE); diff --git a/editor/editorcommon.h b/editor/editorcommon.h index 2d5d08d..7b9637e 100644 --- a/editor/editorcommon.h +++ b/editor/editorcommon.h @@ -1,10 +1 @@ -#pragma once - -enum SelectedTool { - SELECT, - MOVE, - SCALE, - ROTATE, -}; - -extern SelectedTool selectedTool; \ No newline at end of file +#pragma once \ No newline at end of file diff --git a/editor/mainglwidget.cpp b/editor/mainglwidget.cpp index bbe2898..336638b 100644 --- a/editor/mainglwidget.cpp +++ b/editor/mainglwidget.cpp @@ -146,7 +146,7 @@ void MainGLWidget::handleHandleDrag(QMouseEvent* evt) { QPoint cLastPoint = lastPoint; lastPoint = evt->pos(); - if (!isMouseDragging || !draggingHandle || !editorToolHandles->adornee) return; + if (!isMouseDragging || !draggingHandle || !editorToolHandles->adornee || !editorToolHandles->active) return; QPoint position = evt->pos(); @@ -169,7 +169,7 @@ void MainGLWidget::handleHandleDrag(QMouseEvent* evt) { glm::vec3 handlePoint, rb; get_closest_points_between_segments(axisSegment0, axisSegment1, mouseSegment0, mouseSegment1, handlePoint, rb); - if (selectedTool == SelectedTool::MOVE) { + if (mainWindow()->selectedTool == SelectedTool::MOVE) { glm::vec3 newPos = editorToolHandles->PartCFrameFromHandlePos(draggingHandle.value(), handlePoint).Position(); glm::vec3 oldPos = editorToolHandles->adornee->lock()->cframe.Position(); glm::vec3 diff = newPos - oldPos; @@ -179,7 +179,7 @@ void MainGLWidget::handleHandleDrag(QMouseEvent* evt) { newPos = diff + oldPos; editorToolHandles->adornee->lock()->cframe = editorToolHandles->adornee->lock()->cframe.Rotation() + newPos; - } else if (selectedTool == SelectedTool::SCALE) { + } else if (mainWindow()->selectedTool == SelectedTool::SCALE) { glm::vec3 handlePos = editorToolHandles->PartCFrameFromHandlePos(draggingHandle.value(), handlePoint).Position(); // Find change in handles, and negate difference in sign between axes @@ -199,7 +199,7 @@ void MainGLWidget::handleHandleDrag(QMouseEvent* evt) { } std::optional MainGLWidget::raycastHandle(glm::vec3 pointDir) { - if (!editorToolHandles->adornee.has_value()) return std::nullopt; + if (!editorToolHandles->adornee.has_value() || !editorToolHandles->active) return std::nullopt; return editorToolHandles->RaycastHandle(rp3d::Ray(glmToRp(camera.cameraPos), glmToRp(glm::normalize(pointDir)) * 50000)); } diff --git a/editor/mainwindow.cpp b/editor/mainwindow.cpp index 8a53f19..522bb3a 100644 --- a/editor/mainwindow.cpp +++ b/editor/mainwindow.cpp @@ -24,8 +24,6 @@ #include "qobject.h" #include "qsysinfo.h" -SelectedTool selectedTool; - bool simulationPlaying = false; MainWindow::MainWindow(QWidget *parent) @@ -87,7 +85,12 @@ MainWindow::MainWindow(QWidget *parent) }); addSelectionListener([&](auto oldSelection, auto newSelection, bool fromExplorer) { - updateToolbars(); + editorToolHandles->adornee = std::nullopt; + if (newSelection.size() == 0) return; + InstanceRef inst = newSelection[0].lock(); + if (inst->GetClass() != &Part::TYPE) return; + + editorToolHandles->adornee = std::dynamic_pointer_cast(inst); }); // ui->explorerView->Init(ui); @@ -153,16 +156,7 @@ void MainWindow::updateToolbars() { if (selectedTool == SelectedTool::MOVE) editorToolHandles->worldMode = true; if (selectedTool == SelectedTool::SCALE) editorToolHandles->worldMode = false; - - // This code sucks. A lot - if (selectedTool == SelectedTool::SELECT) return; - if (getSelection().size() == 0) { editorToolHandles->adornee = std::nullopt; return; }; - InstanceRef inst = getSelection()[0].lock(); - if (inst->GetClass() != &Part::TYPE) { editorToolHandles->adornee = std::nullopt; return; }; - - editorToolHandles->adornee = std::dynamic_pointer_cast(inst); - - // editorToolHandles->adornee = std::nullopt; + editorToolHandles->active = selectedTool != SelectedTool::SELECT; } MainWindow::~MainWindow() diff --git a/editor/mainwindow.h b/editor/mainwindow.h index 4dd279e..0bad39e 100644 --- a/editor/mainwindow.h +++ b/editor/mainwindow.h @@ -8,6 +8,13 @@ #include #include +enum SelectedTool { + SELECT, + MOVE, + SCALE, + ROTATE, +}; + enum GridSnappingMode { SNAP_1_STUD, SNAP_05_STUDS, @@ -28,6 +35,7 @@ public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); + SelectedTool selectedTool; GridSnappingMode snappingMode; Ui::MainWindow *ui;