diff --git a/client/src/main.cpp b/client/src/main.cpp index 1857d26..53ef4a7 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -42,12 +42,11 @@ int main() { glewInit(); dataModel->Init(); - workspace = dataModel->workspace; simulationInit(); renderInit(window, 1200, 900); // Baseplate - workspace->AddChild(Part::New({ + workspace()->AddChild(Part::New({ .position = glm::vec3(0, -5, 0), .rotation = glm::vec3(0), .scale = glm::vec3(512, 1.2, 512), @@ -59,7 +58,7 @@ int main() { .anchored = true, })); - workspace->AddChild(lastPart = Part::New({ + workspace()->AddChild(lastPart = Part::New({ .position = glm::vec3(0), .rotation = glm::vec3(0), .scale = glm::vec3(4, 1.2, 2), @@ -70,7 +69,7 @@ int main() { } })); - for (InstanceRef inst : workspace->GetChildren()) { + for (InstanceRef inst : workspace()->GetChildren()) { if (inst->GetClass()->className != "Part") continue; std::shared_ptr part = std::dynamic_pointer_cast(inst); syncPartPhysics(part); @@ -163,7 +162,7 @@ void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods) { void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) { if (key == GLFW_KEY_F && action == GLFW_PRESS) { - workspace->AddChild(lastPart = Part::New({ + workspace()->AddChild(lastPart = Part::New({ .position = camera.cameraPos + camera.cameraFront * glm::vec3(3), .rotation = glm::vec3(0), .scale = glm::vec3(1, 1, 1), diff --git a/editor/mainglwidget.cpp b/editor/mainglwidget.cpp index 439ada7..d90efc2 100644 --- a/editor/mainglwidget.cpp +++ b/editor/mainglwidget.cpp @@ -153,7 +153,7 @@ void MainGLWidget::keyPressEvent(QKeyEvent* evt) { else if (evt->key() == Qt::Key_D) moveX = -1; if (evt->key() == Qt::Key_F) { - workspace->AddChild(lastPart = Part::New({ + workspace()->AddChild(lastPart = Part::New({ .position = camera.cameraPos + camera.cameraFront * glm::vec3(3), .rotation = glm::vec3(0), .scale = glm::vec3(1, 1, 1), diff --git a/editor/mainwindow.cpp b/editor/mainwindow.cpp index 7b271e7..7e9e016 100644 --- a/editor/mainwindow.cpp +++ b/editor/mainwindow.cpp @@ -27,7 +27,6 @@ MainWindow::MainWindow(QWidget *parent) , ui(new Ui::MainWindow) { dataModel->Init(); - workspace = dataModel->workspace; ui->setupUi(this); timer.start(33, this); @@ -53,7 +52,7 @@ MainWindow::MainWindow(QWidget *parent) simulationInit(); // Baseplate - workspace->AddChild(ui->mainWidget->lastPart = Part::New({ + workspace()->AddChild(ui->mainWidget->lastPart = Part::New({ .position = glm::vec3(0, -5, 0), .rotation = glm::vec3(0), .scale = glm::vec3(512, 1.2, 512), @@ -67,7 +66,7 @@ MainWindow::MainWindow(QWidget *parent) ui->mainWidget->lastPart->name = "Baseplate"; syncPartPhysics(ui->mainWidget->lastPart); - workspace->AddChild(ui->mainWidget->lastPart = Part::New({ + workspace()->AddChild(ui->mainWidget->lastPart = Part::New({ .position = glm::vec3(0), .rotation = glm::vec3(0), .scale = glm::vec3(4, 1.2, 2), diff --git a/editor/panes/explorermodel.cpp b/editor/panes/explorermodel.cpp index b1bed69..8dbadd5 100644 --- a/editor/panes/explorermodel.cpp +++ b/editor/panes/explorermodel.cpp @@ -60,7 +60,7 @@ QModelIndex ExplorerModel::index(int row, int column, const QModelIndex &parent) } QModelIndex ExplorerModel::toIndex(InstanceRef item) { - if (item == rootItem) + if (item == rootItem || !item->GetParent().has_value()) return {}; InstanceRef parentItem = item->GetParent().value(); @@ -160,8 +160,8 @@ QImage ExplorerModel::iconOf(const InstanceType* type) const { } bool ExplorerModel::moveRows(const QModelIndex &sourceParentIdx, int sourceRow, int count, const QModelIndex &destinationParentIdx, int destinationChild) { - Instance* sourceParent = sourceParentIdx.isValid() ? static_cast(sourceParentIdx.internalPointer()) : workspace.get(); - Instance* destinationParent = destinationParentIdx.isValid() ? static_cast(destinationParentIdx.internalPointer()) : workspace.get(); + Instance* sourceParent = sourceParentIdx.isValid() ? static_cast(sourceParentIdx.internalPointer()) : rootItem.get(); + Instance* destinationParent = destinationParentIdx.isValid() ? static_cast(destinationParentIdx.internalPointer()) : rootItem.get(); printf("Moved %d from %s\n", count, sourceParent->name.c_str()); @@ -178,7 +178,7 @@ bool ExplorerModel::moveRows(const QModelIndex &sourceParentIdx, int sourceRow, } bool ExplorerModel::removeRows(int row, int count, const QModelIndex& parentIdx) { - Instance* parent = parentIdx.isValid() ? static_cast(parentIdx.internalPointer()) : workspace.get(); + Instance* parent = parentIdx.isValid() ? static_cast(parentIdx.internalPointer()) : rootItem.get(); for (int i = row; i < (row + count); i++) { //parent->GetChildren()[i]->SetParent(nullptr); @@ -206,7 +206,7 @@ Qt::DropActions ExplorerModel::supportedDropActions() const { InstanceRef ExplorerModel::fromIndex(const QModelIndex index) const { - if (!index.isValid()) return workspace; + if (!index.isValid()) return rootItem; return static_cast(index.internalPointer())->shared_from_this(); } diff --git a/editor/panes/explorerview.cpp b/editor/panes/explorerview.cpp index 9573151..699a7b6 100644 --- a/editor/panes/explorerview.cpp +++ b/editor/panes/explorerview.cpp @@ -2,13 +2,14 @@ #include "explorermodel.h" #include "common.h" #include "objects/base/instance.h" +#include "objects/workspace.h" #include "qabstractitemmodel.h" #include "qaction.h" #include "qnamespace.h" ExplorerView::ExplorerView(QWidget* parent): QTreeView(parent), - model(ExplorerModel(std::dynamic_pointer_cast(workspace))) { + model(ExplorerModel(std::dynamic_pointer_cast(dataModel))) { this->setModel(&model); // Disabling the root decoration will cause the expand/collapse chevrons to be hidden too, we don't want that @@ -23,6 +24,9 @@ ExplorerView::ExplorerView(QWidget* parent): this->setDropIndicatorShown(true); this->setContextMenuPolicy(Qt::CustomContextMenu); + // Expand workspace + this->expand(model.ObjectToIndex(workspace())); + connect(this, &QTreeView::customContextMenuRequested, this, [&](const QPoint& point) { QModelIndex index = this->indexAt(point); contextMenu.exec(this->viewport()->mapToGlobal(point)); diff --git a/src/common.cpp b/src/common.cpp index 3816179..61c4be9 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -6,8 +6,6 @@ Camera camera(glm::vec3(0.0, 0.0, 3.0)); //std::vector parts; std::shared_ptr dataModel = DataModel::New(); -std::shared_ptr workspace = dataModel->workspace; -// std::shared_ptr workspace = Workspace::New(); std::optional hierarchyPreUpdateHandler; std::optional hierarchyPostUpdateHandler; diff --git a/src/common.h b/src/common.h index d1ea23c..74fc8f9 100644 --- a/src/common.h +++ b/src/common.h @@ -15,7 +15,7 @@ typedef std::function oldSelection, std::vecto extern Camera camera; extern std::shared_ptr dataModel; -extern std::shared_ptr workspace; +inline std::shared_ptr workspace() { return dataModel->workspace; } extern std::optional hierarchyPreUpdateHandler; extern std::optional hierarchyPostUpdateHandler; diff --git a/src/objects/datamodel.cpp b/src/objects/datamodel.cpp index ea09ec9..08ace9f 100644 --- a/src/objects/datamodel.cpp +++ b/src/objects/datamodel.cpp @@ -18,4 +18,5 @@ DataModel::DataModel() void DataModel::Init() { this->workspace = std::make_shared(shared()); + this->AddChild(this->workspace); } \ No newline at end of file diff --git a/src/physics/simulation.cpp b/src/physics/simulation.cpp index 6e0c439..3ce7f9d 100644 --- a/src/physics/simulation.cpp +++ b/src/physics/simulation.cpp @@ -72,7 +72,7 @@ void 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 (InstanceRef obj : workspace->GetChildren()) { + for (InstanceRef obj : workspace()->GetChildren()) { 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/src/rendering/renderer.cpp b/src/rendering/renderer.cpp index c59b32e..9b6f190 100644 --- a/src/rendering/renderer.cpp +++ b/src/rendering/renderer.cpp @@ -105,7 +105,7 @@ void renderParts() { shader->set("viewPos", camera.cameraPos); // TODO: Same as todo in src/physics/simulation.cpp - for (InstanceRef inst : workspace->GetChildren()) { + for (InstanceRef inst : workspace()->GetChildren()) { if (inst->GetClass()->className != "Part") continue; std::shared_ptr part = std::dynamic_pointer_cast(inst);