feat: move all objects and workspace into datamodel

This commit is contained in:
maelstrom 2025-02-04 21:44:54 +01:00
parent dc6e9180ff
commit d49005295f
10 changed files with 21 additions and 20 deletions

View file

@ -42,12 +42,11 @@ int main() {
glewInit(); glewInit();
dataModel->Init(); dataModel->Init();
workspace = dataModel->workspace;
simulationInit(); simulationInit();
renderInit(window, 1200, 900); renderInit(window, 1200, 900);
// Baseplate // Baseplate
workspace->AddChild(Part::New({ workspace()->AddChild(Part::New({
.position = glm::vec3(0, -5, 0), .position = glm::vec3(0, -5, 0),
.rotation = glm::vec3(0), .rotation = glm::vec3(0),
.scale = glm::vec3(512, 1.2, 512), .scale = glm::vec3(512, 1.2, 512),
@ -59,7 +58,7 @@ int main() {
.anchored = true, .anchored = true,
})); }));
workspace->AddChild(lastPart = Part::New({ workspace()->AddChild(lastPart = Part::New({
.position = glm::vec3(0), .position = glm::vec3(0),
.rotation = glm::vec3(0), .rotation = glm::vec3(0),
.scale = glm::vec3(4, 1.2, 2), .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; if (inst->GetClass()->className != "Part") continue;
std::shared_ptr<Part> part = std::dynamic_pointer_cast<Part>(inst); std::shared_ptr<Part> part = std::dynamic_pointer_cast<Part>(inst);
syncPartPhysics(part); 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) { void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_F && action == GLFW_PRESS) { 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), .position = camera.cameraPos + camera.cameraFront * glm::vec3(3),
.rotation = glm::vec3(0), .rotation = glm::vec3(0),
.scale = glm::vec3(1, 1, 1), .scale = glm::vec3(1, 1, 1),

View file

@ -153,7 +153,7 @@ void MainGLWidget::keyPressEvent(QKeyEvent* evt) {
else if (evt->key() == Qt::Key_D) moveX = -1; else if (evt->key() == Qt::Key_D) moveX = -1;
if (evt->key() == Qt::Key_F) { if (evt->key() == Qt::Key_F) {
workspace->AddChild(lastPart = Part::New({ workspace()->AddChild(lastPart = Part::New({
.position = camera.cameraPos + camera.cameraFront * glm::vec3(3), .position = camera.cameraPos + camera.cameraFront * glm::vec3(3),
.rotation = glm::vec3(0), .rotation = glm::vec3(0),
.scale = glm::vec3(1, 1, 1), .scale = glm::vec3(1, 1, 1),

View file

@ -27,7 +27,6 @@ MainWindow::MainWindow(QWidget *parent)
, ui(new Ui::MainWindow) , ui(new Ui::MainWindow)
{ {
dataModel->Init(); dataModel->Init();
workspace = dataModel->workspace;
ui->setupUi(this); ui->setupUi(this);
timer.start(33, this); timer.start(33, this);
@ -53,7 +52,7 @@ MainWindow::MainWindow(QWidget *parent)
simulationInit(); simulationInit();
// Baseplate // Baseplate
workspace->AddChild(ui->mainWidget->lastPart = Part::New({ workspace()->AddChild(ui->mainWidget->lastPart = Part::New({
.position = glm::vec3(0, -5, 0), .position = glm::vec3(0, -5, 0),
.rotation = glm::vec3(0), .rotation = glm::vec3(0),
.scale = glm::vec3(512, 1.2, 512), .scale = glm::vec3(512, 1.2, 512),
@ -67,7 +66,7 @@ MainWindow::MainWindow(QWidget *parent)
ui->mainWidget->lastPart->name = "Baseplate"; ui->mainWidget->lastPart->name = "Baseplate";
syncPartPhysics(ui->mainWidget->lastPart); syncPartPhysics(ui->mainWidget->lastPart);
workspace->AddChild(ui->mainWidget->lastPart = Part::New({ workspace()->AddChild(ui->mainWidget->lastPart = Part::New({
.position = glm::vec3(0), .position = glm::vec3(0),
.rotation = glm::vec3(0), .rotation = glm::vec3(0),
.scale = glm::vec3(4, 1.2, 2), .scale = glm::vec3(4, 1.2, 2),

View file

@ -60,7 +60,7 @@ QModelIndex ExplorerModel::index(int row, int column, const QModelIndex &parent)
} }
QModelIndex ExplorerModel::toIndex(InstanceRef item) { QModelIndex ExplorerModel::toIndex(InstanceRef item) {
if (item == rootItem) if (item == rootItem || !item->GetParent().has_value())
return {}; return {};
InstanceRef parentItem = item->GetParent().value(); 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) { bool ExplorerModel::moveRows(const QModelIndex &sourceParentIdx, int sourceRow, int count, const QModelIndex &destinationParentIdx, int destinationChild) {
Instance* sourceParent = sourceParentIdx.isValid() ? static_cast<Instance*>(sourceParentIdx.internalPointer()) : workspace.get(); Instance* sourceParent = sourceParentIdx.isValid() ? static_cast<Instance*>(sourceParentIdx.internalPointer()) : rootItem.get();
Instance* destinationParent = destinationParentIdx.isValid() ? static_cast<Instance*>(destinationParentIdx.internalPointer()) : workspace.get(); Instance* destinationParent = destinationParentIdx.isValid() ? static_cast<Instance*>(destinationParentIdx.internalPointer()) : rootItem.get();
printf("Moved %d from %s\n", count, sourceParent->name.c_str()); 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) { bool ExplorerModel::removeRows(int row, int count, const QModelIndex& parentIdx) {
Instance* parent = parentIdx.isValid() ? static_cast<Instance*>(parentIdx.internalPointer()) : workspace.get(); Instance* parent = parentIdx.isValid() ? static_cast<Instance*>(parentIdx.internalPointer()) : rootItem.get();
for (int i = row; i < (row + count); i++) { for (int i = row; i < (row + count); i++) {
//parent->GetChildren()[i]->SetParent(nullptr); //parent->GetChildren()[i]->SetParent(nullptr);
@ -206,7 +206,7 @@ Qt::DropActions ExplorerModel::supportedDropActions() const {
InstanceRef ExplorerModel::fromIndex(const QModelIndex index) const { InstanceRef ExplorerModel::fromIndex(const QModelIndex index) const {
if (!index.isValid()) return workspace; if (!index.isValid()) return rootItem;
return static_cast<Instance*>(index.internalPointer())->shared_from_this(); return static_cast<Instance*>(index.internalPointer())->shared_from_this();
} }

View file

@ -2,13 +2,14 @@
#include "explorermodel.h" #include "explorermodel.h"
#include "common.h" #include "common.h"
#include "objects/base/instance.h" #include "objects/base/instance.h"
#include "objects/workspace.h"
#include "qabstractitemmodel.h" #include "qabstractitemmodel.h"
#include "qaction.h" #include "qaction.h"
#include "qnamespace.h" #include "qnamespace.h"
ExplorerView::ExplorerView(QWidget* parent): ExplorerView::ExplorerView(QWidget* parent):
QTreeView(parent), QTreeView(parent),
model(ExplorerModel(std::dynamic_pointer_cast<Instance>(workspace))) { model(ExplorerModel(std::dynamic_pointer_cast<Instance>(dataModel))) {
this->setModel(&model); this->setModel(&model);
// Disabling the root decoration will cause the expand/collapse chevrons to be hidden too, we don't want that // 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->setDropIndicatorShown(true);
this->setContextMenuPolicy(Qt::CustomContextMenu); this->setContextMenuPolicy(Qt::CustomContextMenu);
// Expand workspace
this->expand(model.ObjectToIndex(workspace()));
connect(this, &QTreeView::customContextMenuRequested, this, [&](const QPoint& point) { connect(this, &QTreeView::customContextMenuRequested, this, [&](const QPoint& point) {
QModelIndex index = this->indexAt(point); QModelIndex index = this->indexAt(point);
contextMenu.exec(this->viewport()->mapToGlobal(point)); contextMenu.exec(this->viewport()->mapToGlobal(point));

View file

@ -6,8 +6,6 @@
Camera camera(glm::vec3(0.0, 0.0, 3.0)); Camera camera(glm::vec3(0.0, 0.0, 3.0));
//std::vector<Part> parts; //std::vector<Part> parts;
std::shared_ptr<DataModel> dataModel = DataModel::New(); std::shared_ptr<DataModel> dataModel = DataModel::New();
std::shared_ptr<Workspace> workspace = dataModel->workspace;
// std::shared_ptr<Workspace> workspace = Workspace::New();
std::optional<HierarchyPreUpdateHandler> hierarchyPreUpdateHandler; std::optional<HierarchyPreUpdateHandler> hierarchyPreUpdateHandler;
std::optional<HierarchyPostUpdateHandler> hierarchyPostUpdateHandler; std::optional<HierarchyPostUpdateHandler> hierarchyPostUpdateHandler;

View file

@ -15,7 +15,7 @@ typedef std::function<void(std::vector<InstanceRefWeak> oldSelection, std::vecto
extern Camera camera; extern Camera camera;
extern std::shared_ptr<DataModel> dataModel; extern std::shared_ptr<DataModel> dataModel;
extern std::shared_ptr<Workspace> workspace; inline std::shared_ptr<Workspace> workspace() { return dataModel->workspace; }
extern std::optional<HierarchyPreUpdateHandler> hierarchyPreUpdateHandler; extern std::optional<HierarchyPreUpdateHandler> hierarchyPreUpdateHandler;
extern std::optional<HierarchyPostUpdateHandler> hierarchyPostUpdateHandler; extern std::optional<HierarchyPostUpdateHandler> hierarchyPostUpdateHandler;

View file

@ -18,4 +18,5 @@ DataModel::DataModel()
void DataModel::Init() { void DataModel::Init() {
this->workspace = std::make_shared<Workspace>(shared<DataModel>()); this->workspace = std::make_shared<Workspace>(shared<DataModel>());
this->AddChild(this->workspace);
} }

View file

@ -72,7 +72,7 @@ void physicsStep(float deltaTime) {
// Naive implementation. Parts are only considered so if they are just under Workspace // 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 // 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 if (obj->GetClass()->className != "Part") continue; // TODO: Replace this with a .IsA call instead of comparing the class name directly
std::shared_ptr<Part> part = std::dynamic_pointer_cast<Part>(obj); std::shared_ptr<Part> part = std::dynamic_pointer_cast<Part>(obj);
const rp::Transform& transform = part->rigidBody->getTransform(); const rp::Transform& transform = part->rigidBody->getTransform();

View file

@ -105,7 +105,7 @@ void renderParts() {
shader->set("viewPos", camera.cameraPos); shader->set("viewPos", camera.cameraPos);
// TODO: Same as todo in src/physics/simulation.cpp // 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; if (inst->GetClass()->className != "Part") continue;
std::shared_ptr<Part> part = std::dynamic_pointer_cast<Part>(inst); std::shared_ptr<Part> part = std::dynamic_pointer_cast<Part>(inst);