diff --git a/autogen/src/object/codegen.cpp b/autogen/src/object/codegen.cpp index dc3f117..b15aa22 100644 --- a/autogen/src/object/codegen.cpp +++ b/autogen/src/object/codegen.cpp @@ -53,7 +53,7 @@ static std::string castToVariant(std::string valueStr, std::string fieldType) { return "Data::Int((int)" + valueStr + ")"; } - // InstanceRef + // std::shared_ptr std::string subtype = parseWeakPtr(fieldType); if (!subtype.empty()) { return "Data::Variant(" + valueStr + ".expired() ? Data::InstanceRef() : Data::InstanceRef(std::dynamic_pointer_cast(" + valueStr + ".lock())))"; @@ -73,7 +73,7 @@ static void writePropertySetHandler(std::ofstream& out, ClassAnalysis state) { bool first = true; for (auto& prop : state.properties) { out << (first ? "" : " else ") << "if (name == \"" << prop.name << "\") {"; - // InstanceRef + // std::shared_ptr std::string subtype = parseWeakPtr(prop.backingFieldType); if (prop.flags & PropertyFlag_Readonly) { diff --git a/client/src/main.cpp b/client/src/main.cpp index fe13d52..67da5a0 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -52,7 +52,7 @@ int main() { .color = glm::vec3(0.639216f, 0.635294f, 0.647059f), })); - for (InstanceRef inst : gWorkspace()->GetChildren()) { + for (std::shared_ptr inst : gWorkspace()->GetChildren()) { if (inst->GetClass()->className != "Part") continue; std::shared_ptr part = std::dynamic_pointer_cast(inst); gWorkspace()->SyncPartPhysics(part); diff --git a/core/src/common.cpp b/core/src/common.cpp index c5ad1ba..fc58748 100644 --- a/core/src/common.cpp +++ b/core/src/common.cpp @@ -34,7 +34,7 @@ void addSelectionListener(SelectionUpdateHandler handler) { selectionUpdateListeners.push_back(handler); } -void sendPropertyUpdatedSignal(InstanceRef instance, std::string property, Data::Variant newValue) { +void sendPropertyUpdatedSignal(std::shared_ptr instance, std::string property, Data::Variant newValue) { for (PropertyUpdateHandler handler : propertyUpdatelisteners) { handler(instance, property, newValue); } diff --git a/core/src/common.h b/core/src/common.h index b9344f5..70f68b2 100644 --- a/core/src/common.h +++ b/core/src/common.h @@ -9,10 +9,10 @@ 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 object, std::optional> oldParent, std::optional> newParent)> HierarchyPreUpdateHandler; +typedef std::function object, std::optional> oldParent, std::optional> newParent)> HierarchyPostUpdateHandler; typedef std::function> oldSelection, std::vector> newSelection, bool fromExplorer)> SelectionUpdateHandler; -typedef std::function PropertyUpdateHandler; +typedef std::function instance, std::string property, Data::Variant newValue)> PropertyUpdateHandler; // TEMPORARY COMMON DATA FOR VARIOUS INTERNAL COMPONENTS @@ -28,5 +28,5 @@ void setSelection(std::vector> newSelection, bool from const std::vector> getSelection(); void addSelectionListener(SelectionUpdateHandler handler); -void sendPropertyUpdatedSignal(InstanceRef instance, std::string property, Data::Variant newValue); +void sendPropertyUpdatedSignal(std::shared_ptr instance, std::string property, Data::Variant newValue); void addPropertyUpdateListener(PropertyUpdateHandler handler); \ No newline at end of file diff --git a/core/src/objects/base/instance.cpp b/core/src/objects/base/instance.cpp index 44d15e0..07ce9a2 100644 --- a/core/src/objects/base/instance.cpp +++ b/core/src/objects/base/instance.cpp @@ -109,7 +109,7 @@ void Instance::updateAncestry(std::optional> updatedCh } // Update ancestry in descendants - for (InstanceRef child : children) { + for (std::shared_ptr child : children) { child->updateAncestry(updatedChild, newParent); } } @@ -272,7 +272,7 @@ void Instance::Serialize(pugi::xml_node parent, RefStateSerialize state) { pugi::xml_node propertyNode = propertiesNode.append_child(meta.type->name); propertyNode.append_attribute("name").set_value(name); - // Update InstanceRef properties using map above + // Update std::shared_ptr properties using map above if (meta.type == &Data::InstanceRef::TYPE) { std::weak_ptr refWeak = GetPropertyValue(name).expect("Declared property is missing").get(); if (refWeak.expired()) continue; @@ -306,18 +306,18 @@ void Instance::Serialize(pugi::xml_node parent, RefStateSerialize state) { state->refsAwaitingRemap[shared_from_this()].clear(); // Add children - for (InstanceRef child : this->children) { + for (std::shared_ptr child : this->children) { child->Serialize(node, state); } } -result Instance::Deserialize(pugi::xml_node node, RefStateDeserialize state) { +result, NoSuchInstance> Instance::Deserialize(pugi::xml_node node, RefStateDeserialize state) { std::string className = node.attribute("class").value(); if (INSTANCE_MAP.count(className) == 0) { return NoSuchInstance(className); } // This will error if an abstract instance is used in the file. Oh well, not my prob rn. - InstanceRef object = INSTANCE_MAP[className]->constructor(); + std::shared_ptr object = INSTANCE_MAP[className]->constructor(); object->GetChildren(); // const InstanceType* type = INSTANCE_MAP.at(className); @@ -332,7 +332,7 @@ result Instance::Deserialize(pugi::xml_node node, R continue; } - // Update InstanceRef properties using map above + // Update std::shared_ptr properties using map above if (meta_.expect().type == &Data::InstanceRef::TYPE) { if (propertyNode.text().empty()) continue; @@ -369,7 +369,7 @@ result Instance::Deserialize(pugi::xml_node node, R // Read children for (pugi::xml_node childNode : node.children("Item")) { - result child = Instance::Deserialize(childNode, state); + result, NoSuchInstance> child = Instance::Deserialize(childNode, state); if (child.isError()) { std::get(child.error().value()).logMessage(); continue; @@ -434,7 +434,7 @@ std::optional> Instance::Clone(RefStateClone state) { if (meta.flags & (PROP_READONLY | PROP_NOSAVE)) continue; - // Update InstanceRef properties using map above + // Update std::shared_ptr properties using map above if (meta.type == &Data::InstanceRef::TYPE) { std::weak_ptr refWeak = GetPropertyValue(property).expect().get(); if (refWeak.expired()) continue; diff --git a/core/src/objects/base/instance.h b/core/src/objects/base/instance.h index 744e2fb..cb74c96 100644 --- a/core/src/objects/base/instance.h +++ b/core/src/objects/base/instance.h @@ -138,9 +138,6 @@ public: std::optional> Clone(RefStateClone state = std::make_shared<__RefStateClone>()); }; -typedef std::shared_ptr InstanceRef; -typedef std::weak_ptr InstanceRefWeak; - // https://gist.github.com/jeetsukumaran/307264 class DescendantsIterator { public: diff --git a/core/src/objects/datamodel.cpp b/core/src/objects/datamodel.cpp index 22eff4c..539b913 100644 --- a/core/src/objects/datamodel.cpp +++ b/core/src/objects/datamodel.cpp @@ -49,7 +49,7 @@ void DataModel::SaveToFile(std::optional path) { pugi::xml_document doc; pugi::xml_node root = doc.append_child("openblocks"); - for (InstanceRef child : this->GetChildren()) { + for (std::shared_ptr child : this->GetChildren()) { child->Serialize(root); } @@ -59,50 +59,6 @@ void DataModel::SaveToFile(std::optional path) { Logger::info("Place saved successfully"); } -// void DataModel::DeserializeService(pugi::xml_node node, RefStateDeserialize state) { -// std::string className = node.attribute("class").value(); -// if (INSTANCE_MAP.count(className) == 0) { -// Logger::fatalErrorf("Unknown service: '%s'", className.c_str()); -// return; -// } - -// if (services.count(className) != 0) { -// Logger::fatalErrorf("Service %s defined multiple times in file", className.c_str()); -// return; -// } - -// // This will error if an abstract instance is used in the file. Oh well, not my prob rn. -// InstanceRef object = INSTANCE_MAP[className]->constructor(); -// AddChild(object); - -// // Read properties -// pugi::xml_node propertiesNode = node.child("Properties"); -// for (pugi::xml_node propertyNode : propertiesNode) { -// std::string propertyName = propertyNode.attribute("name").value(); -// auto meta_ = object->GetPropertyMeta(propertyName); -// if (!meta_) { -// Logger::fatalErrorf("Attempt to set unknown property '%s' of %s", propertyName.c_str(), object->GetClass()->className.c_str()); -// continue; -// } -// Data::Variant value = Data::Variant::Deserialize(propertyNode, state); -// object->SetPropertyValue(propertyName, value).expect(); -// } - -// // Add children -// for (pugi::xml_node childNode : node.children("Item")) { -// result child = Instance::Deserialize(childNode, state); -// if (child.isError()) { -// std::get(child.error().value()).logMessage(); -// continue; -// } -// object->AddChild(child.expect()); -// } - -// // We add the service to the list -// // All services get init'd at once in InitServices -// this->services[className] = std::dynamic_pointer_cast(object); -// } - std::shared_ptr DataModel::LoadFromFile(std::string path) { std::ifstream inStream(path); pugi::xml_document doc; @@ -174,7 +130,7 @@ std::shared_ptr DataModel::CloneModel() { if (meta.flags & (PROP_READONLY | PROP_NOSAVE)) continue; - // Update InstanceRef properties using map above + // Update std::shared_ptr properties using map above if (meta.type == &Data::InstanceRef::TYPE) { std::weak_ptr refWeak = GetPropertyValue(property).expect().get(); if (refWeak.expired()) continue; diff --git a/core/src/objects/part.cpp b/core/src/objects/part.cpp index 2f527b3..f7a842c 100644 --- a/core/src/objects/part.cpp +++ b/core/src/objects/part.cpp @@ -224,7 +224,7 @@ void Part::MakeJoints() { // TEMPORARY // TODO: Use more efficient algorithm to *actually* find nearby parts) for (auto it = workspace().value()->GetDescendantsStart(); it != workspace().value()->GetDescendantsEnd(); it++) { - InstanceRef obj = *it; + std::shared_ptr obj = *it; if (obj == shared_from_this()) continue; // Skip ourselves if (obj->GetClass()->className != "Part") continue; // TODO: Replace this with a .IsA call instead of comparing the class name directly std::shared_ptr otherPart = obj->CastTo().expect(); diff --git a/core/src/objects/part.h b/core/src/objects/part.h index 94c0c83..27c7987 100644 --- a/core/src/objects/part.h +++ b/core/src/objects/part.h @@ -107,7 +107,7 @@ public: static inline std::shared_ptr New() { return std::make_shared(); }; static inline std::shared_ptr New(PartConstructParams params) { return std::make_shared(params); }; - static inline InstanceRef Create() { return std::make_shared(); }; + static inline std::shared_ptr Create() { return std::make_shared(); }; inline Vector3 position() { return cframe.Position(); } diff --git a/core/src/objects/workspace.cpp b/core/src/objects/workspace.cpp index a8f9672..694ec66 100644 --- a/core/src/objects/workspace.cpp +++ b/core/src/objects/workspace.cpp @@ -59,7 +59,7 @@ void Workspace::InitService() { // Sync all parts for (auto it = this->GetDescendantsStart(); it != this->GetDescendantsEnd(); it++) { - InstanceRef obj = *it; + std::shared_ptr obj = *it; if (!obj->IsA()) continue; std::shared_ptr part = obj->CastTo().expect(); this->SyncPartPhysics(part); @@ -68,7 +68,7 @@ void Workspace::InitService() { // Activate all joints for (auto it = this->GetDescendantsStart(); it != this->GetDescendantsEnd(); it++) { - InstanceRef obj = *it; + std::shared_ptr obj = *it; if (!obj->IsA()) continue; std::shared_ptr joint = obj->CastTo().expect(); joint->UpdateProperty("Part0"); @@ -130,7 +130,7 @@ void Workspace::PhysicsStep(float deltaTime) { // Naive implementation. Parts are only considered so if they are just under Workspace // TODO: Add list of tracked parts in workspace based on their ancestry using inWorkspace property of Instance for (auto it = this->GetDescendantsStart(); it != this->GetDescendantsEnd(); it++) { - InstanceRef obj = *it; + std::shared_ptr obj = *it; if (obj->GetClass()->className != "Part") continue; // TODO: Replace this with a .IsA call instead of comparing the class name directly std::shared_ptr part = std::dynamic_pointer_cast(obj); const rp::Transform& transform = part->rigidBody->getTransform(); diff --git a/core/src/rendering/renderer.cpp b/core/src/rendering/renderer.cpp index 4daf506..0ac8f43 100644 --- a/core/src/rendering/renderer.cpp +++ b/core/src/rendering/renderer.cpp @@ -133,7 +133,7 @@ void renderParts() { // Sort by nearest std::map> sorted; for (auto it = gWorkspace()->GetDescendantsStart(); it != gWorkspace()->GetDescendantsEnd(); it++) { - InstanceRef inst = *it; + std::shared_ptr inst = *it; if (inst->GetClass()->className != "Part") continue; std::shared_ptr part = std::dynamic_pointer_cast(inst); if (part->transparency > 0.00001) { @@ -227,7 +227,7 @@ void renderSurfaceExtras() { ghostShader->set("viewPos", camera.cameraPos); for (auto it = gWorkspace()->GetDescendantsStart(); it != gWorkspace()->GetDescendantsEnd(); it++) { - InstanceRef inst = *it; + std::shared_ptr inst = *it; if (!inst->IsA("Part")) continue; std::shared_ptr part = std::dynamic_pointer_cast(inst); for (int i = 0; i < 6; i++) { @@ -367,7 +367,7 @@ void renderAABB() { ghostShader->set("color", glm::vec3(1.f, 0.f, 0.f)); // Sort by nearest - for (InstanceRef inst : gWorkspace()->GetChildren()) { + for (std::shared_ptr inst : gWorkspace()->GetChildren()) { if (inst->GetClass()->className != "Part") continue; std::shared_ptr part = std::dynamic_pointer_cast(inst); glm::mat4 model = CFrame::IDENTITY + part->cframe.Position(); @@ -407,7 +407,7 @@ void renderWireframe() { wireframeShader->set("color", glm::vec3(1.f, 0.f, 0.f)); // Sort by nearest - for (InstanceRef inst : gWorkspace()->GetChildren()) { + for (std::shared_ptr inst : gWorkspace()->GetChildren()) { if (inst->GetClass()->className != "Part") continue; std::shared_ptr part = std::dynamic_pointer_cast(inst); glm::mat4 model = part->cframe; @@ -451,7 +451,7 @@ void renderOutlines() { int count = 0; for (auto it = gWorkspace()->GetDescendantsStart(); it != gWorkspace()->GetDescendantsEnd(); it++) { - InstanceRef inst = *it; + std::shared_ptr inst = *it; if (inst->GetClass() != &Part::TYPE) continue; std::shared_ptr part = std::dynamic_pointer_cast(inst); if (!part->selected) continue; diff --git a/editor/mainwindow.cpp b/editor/mainwindow.cpp index 48f6dbf..7d2c3c8 100644 --- a/editor/mainwindow.cpp +++ b/editor/mainwindow.cpp @@ -94,13 +94,13 @@ MainWindow::MainWindow(QWidget *parent) }); addSelectionListener([&](auto oldSelection, auto newSelection, bool __) { - for (InstanceRefWeak inst : oldSelection) { + for (std::weak_ptr inst : oldSelection) { if (inst.expired() || inst.lock()->GetClass() != &Part::TYPE) continue; std::shared_ptr part = std::dynamic_pointer_cast(inst.lock()); part->selected = false; } - for (InstanceRefWeak inst : newSelection) { + for (std::weak_ptr inst : newSelection) { if (inst.expired() || inst.lock()->GetClass() != &Part::TYPE) continue; std::shared_ptr part = std::dynamic_pointer_cast(inst.lock()); part->selected = true; @@ -291,7 +291,7 @@ void MainWindow::connectActionHandlers() { }); connect(ui->actionDelete, &QAction::triggered, this, [&]() { - for (InstanceRefWeak inst : getSelection()) { + for (std::weak_ptr inst : getSelection()) { if (inst.expired()) continue; inst.lock()->SetParent(std::nullopt); } @@ -300,7 +300,7 @@ void MainWindow::connectActionHandlers() { connect(ui->actionCopy, &QAction::triggered, this, [&]() { pugi::xml_document rootDoc; - for (InstanceRefWeak inst : getSelection()) { + for (std::weak_ptr inst : getSelection()) { if (inst.expired()) continue; inst.lock()->Serialize(rootDoc); } @@ -314,7 +314,7 @@ void MainWindow::connectActionHandlers() { }); connect(ui->actionCut, &QAction::triggered, this, [&]() { pugi::xml_document rootDoc; - for (InstanceRefWeak inst : getSelection()) { + for (std::weak_ptr inst : getSelection()) { if (inst.expired()) continue; inst.lock()->Serialize(rootDoc); inst.lock()->SetParent(std::nullopt); @@ -338,7 +338,7 @@ void MainWindow::connectActionHandlers() { rootDoc.load_string(encoded.c_str()); for (pugi::xml_node instNode : rootDoc.children()) { - result inst = Instance::Deserialize(instNode); + result, NoSuchInstance> inst = Instance::Deserialize(instNode); if (!inst) { inst.logError(); continue; } gWorkspace()->AddChild(inst.expect()); } @@ -347,7 +347,7 @@ void MainWindow::connectActionHandlers() { connect(ui->actionPasteInto, &QAction::triggered, this, [&]() { if (getSelection().size() != 1) return; - InstanceRef selectedParent = getSelection()[0]; + std::shared_ptr selectedParent = getSelection()[0]; const QMimeData* mimeData = QApplication::clipboard()->mimeData(); if (!mimeData || !mimeData->hasFormat("application/xml")) return; @@ -358,7 +358,7 @@ void MainWindow::connectActionHandlers() { rootDoc.load_string(encoded.c_str()); for (pugi::xml_node instNode : rootDoc.children()) { - result inst = Instance::Deserialize(instNode); + result, NoSuchInstance> inst = Instance::Deserialize(instNode); if (!inst) { inst.logError(); continue; } selectedParent->AddChild(inst.expect()); } @@ -373,7 +373,7 @@ void MainWindow::connectActionHandlers() { pugi::xml_document modelDoc; pugi::xml_node modelRoot = modelDoc.append_child("openblocks"); - for (InstanceRefWeak inst : getSelection()) { + for (std::weak_ptr inst : getSelection()) { if (inst.expired()) continue; inst.lock()->Serialize(modelRoot); } @@ -383,7 +383,7 @@ void MainWindow::connectActionHandlers() { connect(ui->actionInsertModel, &QAction::triggered, this, [&]() { if (getSelection().size() != 1) return; - InstanceRef selectedParent = getSelection()[0]; + std::shared_ptr selectedParent = getSelection()[0]; std::optional path = openFileDialog("Openblocks Model (*.obm)", ".obm", QFileDialog::AcceptOpen); if (!path) return; @@ -393,7 +393,7 @@ void MainWindow::connectActionHandlers() { modelDoc.load(inStream); for (pugi::xml_node instNode : modelDoc.child("openblocks").children("Item")) { - result inst = Instance::Deserialize(instNode); + result, NoSuchInstance> inst = Instance::Deserialize(instNode); if (!inst) { inst.logError(); continue; } selectedParent->AddChild(inst.expect()); } diff --git a/editor/panes/explorermodel.cpp b/editor/panes/explorermodel.cpp index a02092d..b13f683 100644 --- a/editor/panes/explorermodel.cpp +++ b/editor/panes/explorermodel.cpp @@ -10,11 +10,11 @@ std::map instanceIconCache; -ExplorerModel::ExplorerModel(InstanceRef dataRoot, QWidget *parent) +ExplorerModel::ExplorerModel(std::shared_ptr dataRoot, QWidget *parent) : QAbstractItemModel(parent) , rootItem(dataRoot) { // TODO: Don't use lambdas and handlers like that - hierarchyPreUpdateHandler = [&](InstanceRef object, std::optional oldParent, std::optional newParent) { + hierarchyPreUpdateHandler = [&](std::shared_ptr object, std::optional> oldParent, std::optional> newParent) { if (oldParent.has_value()) { auto children = oldParent.value()->GetChildren(); size_t idx = std::find(children.begin(), children.end(), object) - children.begin(); @@ -29,7 +29,7 @@ ExplorerModel::ExplorerModel(InstanceRef dataRoot, QWidget *parent) } }; - hierarchyPostUpdateHandler = [&](InstanceRef object, std::optional oldParent, std::optional newParent) { + hierarchyPostUpdateHandler = [&](std::shared_ptr object, std::optional> oldParent, std::optional> newParent) { if (newParent.has_value()) endInsertRows(); if (oldParent.has_value()) endRemoveRows(); }; @@ -50,11 +50,11 @@ QModelIndex ExplorerModel::index(int row, int column, const QModelIndex &parent) return {}; } -QModelIndex ExplorerModel::toIndex(InstanceRef item) { +QModelIndex ExplorerModel::toIndex(std::shared_ptr item) { if (item == rootItem || !item->GetParent().has_value()) return {}; - InstanceRef parentItem = item->GetParent().value(); + std::shared_ptr parentItem = item->GetParent().value(); // Check above ensures this item is not root, so value() must be valid for (int i = 0; i < parentItem->GetChildren().size(); i++) if (parentItem->GetChildren()[i] == item) @@ -62,7 +62,7 @@ QModelIndex ExplorerModel::toIndex(InstanceRef item) { return QModelIndex{}; } -QModelIndex ExplorerModel::ObjectToIndex(InstanceRef item) { +QModelIndex ExplorerModel::ObjectToIndex(std::shared_ptr item) { return toIndex(item); } @@ -72,13 +72,13 @@ QModelIndex ExplorerModel::parent(const QModelIndex &index) const { Instance* childItem = static_cast(index.internalPointer()); // NORISK: The parent must exist if the child was obtained from it during this frame - InstanceRef parentItem = childItem->GetParent().value(); + std::shared_ptr parentItem = childItem->GetParent().value(); if (parentItem == rootItem) return {}; // Check above ensures this item is not root, so value() must be valid - InstanceRef parentParent = parentItem->GetParent().value(); + std::shared_ptr parentParent = parentItem->GetParent().value(); for (int i = 0; i < parentParent->GetChildren().size(); i++) if (parentParent->GetChildren()[i] == parentItem) return createIndex(i, 0, parentItem.get()); @@ -196,13 +196,13 @@ Qt::DropActions ExplorerModel::supportedDropActions() const { } -InstanceRef ExplorerModel::fromIndex(const QModelIndex index) const { +std::shared_ptr ExplorerModel::fromIndex(const QModelIndex index) const { if (!index.isValid()) return rootItem; return static_cast(index.internalPointer())->shared_from_this(); } struct DragDropSlot { - std::vector instances; + std::vector> instances; }; bool ExplorerModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) { @@ -216,8 +216,8 @@ bool ExplorerModel::dropMimeData(const QMimeData *data, Qt::DropAction action, i return true; } - InstanceRef parentInst = fromIndex(parent); - for (InstanceRef instance : slot->instances) { + std::shared_ptr parentInst = fromIndex(parent); + for (std::shared_ptr instance : slot->instances) { instance->SetParent(parentInst); } @@ -225,7 +225,7 @@ bool ExplorerModel::dropMimeData(const QMimeData *data, Qt::DropAction action, i return true; } -void ExplorerModel::updateRoot(InstanceRef newRoot) { +void ExplorerModel::updateRoot(std::shared_ptr newRoot) { beginResetModel(); rootItem = newRoot; endResetModel(); diff --git a/editor/panes/explorermodel.h b/editor/panes/explorermodel.h index 4085349..e7fb4d1 100644 --- a/editor/panes/explorermodel.h +++ b/editor/panes/explorermodel.h @@ -8,7 +8,7 @@ class ExplorerModel : public QAbstractItemModel { public: Q_DISABLE_COPY_MOVE(ExplorerModel) - explicit ExplorerModel(InstanceRef dataRoot, QWidget *parent = nullptr); + explicit ExplorerModel(std::shared_ptr dataRoot, QWidget *parent = nullptr); ~ExplorerModel() override; QVariant data(const QModelIndex &index, int role) const override; @@ -29,15 +29,15 @@ public: QStringList mimeTypes() const override; Qt::DropActions supportedDragActions() const override; Qt::DropActions supportedDropActions() const override; - InstanceRef fromIndex(const QModelIndex index) const; - QModelIndex ObjectToIndex(InstanceRef item); + std::shared_ptr fromIndex(const QModelIndex index) const; + QModelIndex ObjectToIndex(std::shared_ptr item); QIcon iconOf(const InstanceType* type) const; - void updateRoot(InstanceRef newRoot); + void updateRoot(std::shared_ptr newRoot); private: - InstanceRef rootItem; - QModelIndex toIndex(InstanceRef item); + std::shared_ptr rootItem; + QModelIndex toIndex(std::shared_ptr item); }; // #endif \ No newline at end of file diff --git a/editor/panes/explorerview.cpp b/editor/panes/explorerview.cpp index 388c6a8..0cd5510 100644 --- a/editor/panes/explorerview.cpp +++ b/editor/panes/explorerview.cpp @@ -53,7 +53,7 @@ ExplorerView::ExplorerView(QWidget* parent): if (fromExplorer) return; this->clearSelection(); - for (InstanceRefWeak inst : newSelection) { + for (std::weak_ptr inst : newSelection) { if (inst.expired()) continue; QModelIndex index = this->model.ObjectToIndex(inst.lock()); this->selectionModel()->select(index, QItemSelectionModel::SelectionFlag::Select); @@ -112,6 +112,6 @@ void ExplorerView::buildContextMenu() { } } -void ExplorerView::updateRoot(InstanceRef newRoot) { +void ExplorerView::updateRoot(std::shared_ptr newRoot) { model.updateRoot(newRoot); } \ No newline at end of file diff --git a/editor/panes/explorerview.h b/editor/panes/explorerview.h index 82da25e..e181840 100644 --- a/editor/panes/explorerview.h +++ b/editor/panes/explorerview.h @@ -16,7 +16,7 @@ public: // void dropEvent(QDropEvent*) override; void buildContextMenu(); - void updateRoot(InstanceRef newRoot); + void updateRoot(std::shared_ptr newRoot); private: ExplorerModel model; QMenu contextMenu; diff --git a/editor/panes/propertiesview.cpp b/editor/panes/propertiesview.cpp index 1637ddf..12ddaf9 100644 --- a/editor/panes/propertiesview.cpp +++ b/editor/panes/propertiesview.cpp @@ -38,7 +38,7 @@ public: if (index.column() == 0) return nullptr; if (!index.parent().isValid() || view->currentInstance.expired()) return nullptr; - InstanceRef inst = view->currentInstance.lock(); + std::shared_ptr inst = view->currentInstance.lock(); // If the property is deeper than 1 layer, then it is considered composite // Handle specially @@ -107,7 +107,7 @@ public: if (index.column() == 0) return; if (!index.parent().isValid() || view->currentInstance.expired()) return; - InstanceRef inst = view->currentInstance.lock(); + std::shared_ptr inst = view->currentInstance.lock(); bool isComposite = index.parent().parent().isValid(); std::string componentName = isComposite ? view->itemFromIndex(index)->data(0, Qt::DisplayRole).toString().toStdString() : ""; @@ -159,7 +159,7 @@ public: if (index.column() == 0) return; if (!index.parent().isValid() || view->currentInstance.expired()) return; - InstanceRef inst = view->currentInstance.lock(); + std::shared_ptr inst = view->currentInstance.lock(); bool isComposite = index.parent().parent().isValid(); std::string componentName = isComposite ? view->itemFromIndex(index)->data(0, Qt::DisplayRole).toString().toStdString() : ""; @@ -268,11 +268,11 @@ void PropertiesView::drawBranches(QPainter *painter, const QRect &rect, const QM QTreeWidget::drawBranches(painter, rect, index); } -void PropertiesView::setSelected(std::optional instance) { +void PropertiesView::setSelected(std::optional> instance) { clear(); currentInstance = {}; if (!instance) return; - InstanceRef inst = instance.value(); + std::shared_ptr inst = instance.value(); currentInstance = inst; std::map propertyCategories; @@ -347,7 +347,7 @@ void PropertiesView::propertyChanged(QTreeWidgetItem *item, int column) { // Necessary because otherwise this will catch setCheckState from onPropertyUpdated if (ignorePropertyUpdates) return; if (!item->parent() || (item->parent() && item->parent()->parent()) || currentInstance.expired()) return; - InstanceRef inst = currentInstance.lock(); + std::shared_ptr inst = currentInstance.lock(); std::string propertyName = item->data(0, Qt::DisplayRole).toString().toStdString(); PropertyMeta meta = inst->GetPropertyMeta(propertyName).expect(); @@ -384,7 +384,7 @@ void PropertiesView::rebuildCompositeProperty(QTreeWidgetItem *item, const Data: } // static auto lastUpdateTime = std::chrono::steady_clock::now(); -void PropertiesView::onPropertyUpdated(InstanceRef inst, std::string property, Data::Variant newValue) { +void PropertiesView::onPropertyUpdated(std::shared_ptr inst, std::string property, Data::Variant newValue) { // if (!currentInstance || currentInstance->expired() || instance != currentInstance->lock()) return; // if (std::chrono::duration_cast(std::chrono::steady_clock::now() - lastUpdateTime).count() < 1000) return; // lastUpdateTime = std::chrono::steady_clock::now(); diff --git a/editor/panes/propertiesview.h b/editor/panes/propertiesview.h index 7334bb3..b9c2882 100644 --- a/editor/panes/propertiesview.h +++ b/editor/panes/propertiesview.h @@ -12,11 +12,11 @@ class PropertiesView : public QTreeWidget { Q_DECLARE_PRIVATE(QTreeView) bool ignorePropertyUpdates = false; - InstanceRefWeak currentInstance; + std::weak_ptr currentInstance; void propertyChanged(QTreeWidgetItem *item, int column); void activateProperty(QTreeWidgetItem *item, int column); void rebuildCompositeProperty(QTreeWidgetItem *item, const Data::TypeInfo*, Data::Variant); - void onPropertyUpdated(InstanceRef instance, std::string property, Data::Variant newValue); + void onPropertyUpdated(std::shared_ptr instance, std::string property, Data::Variant newValue); friend PropertiesItemDelegate; protected: @@ -26,5 +26,5 @@ public: PropertiesView(QWidget* parent = nullptr); ~PropertiesView() override; - void setSelected(std::optional instance); + void setSelected(std::optional> instance); }; \ No newline at end of file