refactor: handle inactive when select tool is selected

This commit is contained in:
maelstrom 2025-02-23 16:45:16 +01:00
parent 4bfca68fb0
commit a346b1d9a8
6 changed files with 22 additions and 28 deletions

View file

@ -28,6 +28,7 @@ class Handles : public Instance {
public:
const static InstanceType TYPE;
bool active;
std::optional<std::weak_ptr<Part>> adornee;
// inline std::optional<std::weak_ptr<Part>> GetAdornee() { return adornee; }
// inline void SetAdornee(std::optional<std::weak_ptr<Part>> newAdornee) { this->adornee = newAdornee; updateAdornee(); };

View file

@ -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);

View file

@ -1,10 +1 @@
#pragma once
enum SelectedTool {
SELECT,
MOVE,
SCALE,
ROTATE,
};
extern SelectedTool selectedTool;
#pragma once

View file

@ -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<HandleFace> 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));
}

View file

@ -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<Part>(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<Part>(inst);
// editorToolHandles->adornee = std::nullopt;
editorToolHandles->active = selectedTool != SelectedTool::SELECT;
}
MainWindow::~MainWindow()

View file

@ -8,6 +8,13 @@
#include <QMainWindow>
#include <QLineEdit>
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;