From 215fa141b643e3ac0a5d9f4e94f2aca0f914ae0f Mon Sep 17 00:00:00 2001 From: maelstrom Date: Fri, 30 May 2025 01:20:08 +0200 Subject: [PATCH] refactor(selection): made selection shared_ptr rather than weak_ptr --- core/src/common.cpp | 6 +++--- core/src/common.h | 6 +++--- core/src/partassembly.cpp | 2 +- core/src/partassembly.h | 4 ++-- core/src/rendering/renderer.cpp | 4 +++- editor/mainglwidget.cpp | 18 +++++++++--------- editor/mainwindow.cpp | 12 ++++++------ editor/panes/explorerview.cpp | 8 ++++---- 8 files changed, 31 insertions(+), 29 deletions(-) diff --git a/core/src/common.cpp b/core/src/common.cpp index 30e330c..c5ad1ba 100644 --- a/core/src/common.cpp +++ b/core/src/common.cpp @@ -14,11 +14,11 @@ std::optional hierarchyPostUpdateHandler; Handles editorToolHandles; -std::vector currentSelection; +std::vector> currentSelection; std::vector selectionUpdateListeners; std::vector propertyUpdatelisteners; -void setSelection(std::vector newSelection, bool fromExplorer) { +void setSelection(std::vector> newSelection, bool fromExplorer) { for (SelectionUpdateHandler handler : selectionUpdateListeners) { handler(currentSelection, newSelection, fromExplorer); } @@ -26,7 +26,7 @@ void setSelection(std::vector newSelection, bool fromExplorer) currentSelection = newSelection; } -const std::vector getSelection() { +const std::vector> getSelection() { return currentSelection; } diff --git a/core/src/common.h b/core/src/common.h index 57b7456..b9344f5 100644 --- a/core/src/common.h +++ b/core/src/common.h @@ -11,7 +11,7 @@ class Instance; // typedef std::function element, std::optional> newParent)> HierarchyUpdateHandler; typedef std::function oldParent, std::optional newParent)> HierarchyPreUpdateHandler; typedef std::function oldParent, std::optional newParent)> HierarchyPostUpdateHandler; -typedef std::function oldSelection, std::vector newSelection, bool fromExplorer)> SelectionUpdateHandler; +typedef std::function> oldSelection, std::vector> newSelection, bool fromExplorer)> SelectionUpdateHandler; typedef std::function PropertyUpdateHandler; // TEMPORARY COMMON DATA FOR VARIOUS INTERNAL COMPONENTS @@ -24,8 +24,8 @@ extern std::optional hierarchyPreUpdateHandler; extern std::optional hierarchyPostUpdateHandler; extern Handles editorToolHandles; -void setSelection(std::vector newSelection, bool fromExplorer = false); -const std::vector getSelection(); +void setSelection(std::vector> newSelection, bool fromExplorer = false); +const std::vector> getSelection(); void addSelectionListener(SelectionUpdateHandler handler); void sendPropertyUpdatedSignal(InstanceRef instance, std::string property, Data::Variant newValue); diff --git a/core/src/partassembly.cpp b/core/src/partassembly.cpp index 484879f..3da0322 100644 --- a/core/src/partassembly.cpp +++ b/core/src/partassembly.cpp @@ -32,7 +32,7 @@ PartAssembly::PartAssembly(std::vector> parts, bool worldM _bounds = size; } -PartAssembly PartAssembly::FromSelection(std::vector> newSelection) { +PartAssembly PartAssembly::FromSelection(std::vector> newSelection) { std::vector> selection; for (std::weak_ptr obj : newSelection) { diff --git a/core/src/partassembly.h b/core/src/partassembly.h index a857312..c37e80e 100644 --- a/core/src/partassembly.h +++ b/core/src/partassembly.h @@ -7,7 +7,7 @@ class Part; class Instance; -const std::vector> getSelection(); +const std::vector> getSelection(); class PartAssembly { CFrame _assemblyOrigin; @@ -17,7 +17,7 @@ class PartAssembly { public: PartAssembly(std::vector>, bool worldMode = false); - static PartAssembly FromSelection(std::vector> selection = getSelection()); + static PartAssembly FromSelection(std::vector> selection = getSelection()); inline CFrame assemblyOrigin() { return _assemblyOrigin; }; inline Vector3 bounds() { return _bounds; }; diff --git a/core/src/rendering/renderer.cpp b/core/src/rendering/renderer.cpp index 49a6a69..4daf506 100644 --- a/core/src/rendering/renderer.cpp +++ b/core/src/rendering/renderer.cpp @@ -556,7 +556,9 @@ void renderRotationArcs() { // Pass in the camera position handleShader->set("viewPos", camera.cameraPos); - std::shared_ptr part = std::dynamic_pointer_cast(getSelection()[0].lock()); + // TODO: This won't do... Doesn't support rotating multiple objects, and expects the first selected item to be a part, + // which isn't always the case + std::shared_ptr part = std::dynamic_pointer_cast(getSelection()[0]); for (HandleFace face : HandleFace::Faces) { if (glm::any(glm::lessThan(face.normal, glm::vec3(0)))) continue; diff --git a/editor/mainglwidget.cpp b/editor/mainglwidget.cpp index cf662e6..fe805ce 100755 --- a/editor/mainglwidget.cpp +++ b/editor/mainglwidget.cpp @@ -417,10 +417,10 @@ void MainGLWidget::mousePressEvent(QMouseEvent* evt) { isMouseDragging = true; draggingObject = part; if (evt->modifiers() & Qt::ControlModifier) { - std::vector currentSelection = getSelection(); + std::vector> currentSelection = getSelection(); for (int i = 0; i < currentSelection.size(); i++) { - InstanceRefWeak inst = currentSelection[i]; - if (!inst.expired() && inst.lock() == part) { + std::shared_ptr inst = currentSelection[i]; + if (inst == part) { currentSelection.erase(currentSelection.begin() + i); goto skipAddPart; } @@ -428,8 +428,8 @@ void MainGLWidget::mousePressEvent(QMouseEvent* evt) { currentSelection.push_back(part); skipAddPart: setSelection(currentSelection); - }else - setSelection(std::vector { part }); + } else + setSelection({ part }); // Disable bit so that we can ignore the part while raycasting // part->rigidBody->getCollider(0)->setCollisionCategoryBits(0b10); @@ -488,11 +488,11 @@ void MainGLWidget::keyPressEvent(QKeyEvent* evt) { if (evt->key() == Qt::Key_O) Logger::error("error message"); - if (evt->key() == Qt::Key_C && getSelection().size() > 0 && !getSelection()[0].expired()) - getSelection()[0].lock()->Clone().value()->SetParent(gWorkspace()); + if (evt->key() == Qt::Key_C && getSelection().size() > 0) + getSelection()[0]->Clone().value()->SetParent(gWorkspace()); - if (evt->key() == Qt::Key_H && getSelection().size() > 0 && !getSelection()[0].expired()) - Logger::infof("Object at: 0x%x\n", getSelection()[0].lock().get()); + if (evt->key() == Qt::Key_H && getSelection().size() > 0) + Logger::infof("Object at: 0x%x\n", getSelection()[0].get()); } void MainGLWidget::keyReleaseEvent(QKeyEvent* evt) { diff --git a/editor/mainwindow.cpp b/editor/mainwindow.cpp index 31730ee..48f6dbf 100644 --- a/editor/mainwindow.cpp +++ b/editor/mainwindow.cpp @@ -90,7 +90,7 @@ MainWindow::MainWindow(QWidget *parent) if (newSelection.size() == 0) return; if (newSelection.size() > 1) ui->propertiesView->setSelected(std::nullopt); - ui->propertiesView->setSelected(newSelection[0].lock()); + ui->propertiesView->setSelected(newSelection[0]); }); addSelectionListener([&](auto oldSelection, auto newSelection, bool __) { @@ -295,7 +295,7 @@ void MainWindow::connectActionHandlers() { if (inst.expired()) continue; inst.lock()->SetParent(std::nullopt); } - setSelection(std::vector {}); + setSelection({}); }); connect(ui->actionCopy, &QAction::triggered, this, [&]() { @@ -345,9 +345,9 @@ void MainWindow::connectActionHandlers() { }); connect(ui->actionPasteInto, &QAction::triggered, this, [&]() { - if (getSelection().size() != 1 || getSelection()[0].expired()) return; + if (getSelection().size() != 1) return; - InstanceRef selectedParent = getSelection()[0].lock(); + InstanceRef selectedParent = getSelection()[0]; const QMimeData* mimeData = QApplication::clipboard()->mimeData(); if (!mimeData || !mimeData->hasFormat("application/xml")) return; @@ -382,8 +382,8 @@ void MainWindow::connectActionHandlers() { }); connect(ui->actionInsertModel, &QAction::triggered, this, [&]() { - if (getSelection().size() != 1 || getSelection()[0].expired()) return; - InstanceRef selectedParent = getSelection()[0].lock(); + if (getSelection().size() != 1) return; + InstanceRef selectedParent = getSelection()[0]; std::optional path = openFileDialog("Openblocks Model (*.obm)", ".obm", QFileDialog::AcceptOpen); if (!path) return; diff --git a/editor/panes/explorerview.cpp b/editor/panes/explorerview.cpp index 4c62138..388c6a8 100644 --- a/editor/panes/explorerview.cpp +++ b/editor/panes/explorerview.cpp @@ -38,11 +38,11 @@ ExplorerView::ExplorerView(QWidget* parent): }); connect(selectionModel(), &QItemSelectionModel::selectionChanged, this, [&](const QItemSelection &selected, const QItemSelection &deselected) { - std::vector selectedInstances; + std::vector> selectedInstances; selectedInstances.reserve(selectedIndexes().count()); // This doesn't reserve everything, but should enhance things anyway for (auto index : selectedIndexes()) { - selectedInstances.push_back(reinterpret_cast(index.internalPointer())->weak_from_this()); + selectedInstances.push_back(reinterpret_cast(index.internalPointer())->shared_from_this()); } ::setSelection(selectedInstances, true); @@ -104,8 +104,8 @@ void ExplorerView::buildContextMenu() { QAction* instAction = new QAction(model.iconOf(type), QString::fromStdString(type->className)); insertObjectMenu->addAction(instAction); connect(instAction, &QAction::triggered, this, [&]() { - if (getSelection().size() == 0 || getSelection()[0].expired()) return; - std::shared_ptr instParent = getSelection()[0].lock(); + if (getSelection().size() == 0) return; + std::shared_ptr instParent = getSelection()[0]; std::shared_ptr newInst = type->constructor(); newInst->SetParent(instParent); });