feat: move all objects and workspace into datamodel
This commit is contained in:
parent
dc6e9180ff
commit
d49005295f
10 changed files with 21 additions and 20 deletions
|
@ -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> part = std::dynamic_pointer_cast<Part>(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),
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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<Instance*>(sourceParentIdx.internalPointer()) : workspace.get();
|
||||
Instance* destinationParent = destinationParentIdx.isValid() ? static_cast<Instance*>(destinationParentIdx.internalPointer()) : workspace.get();
|
||||
Instance* sourceParent = sourceParentIdx.isValid() ? static_cast<Instance*>(sourceParentIdx.internalPointer()) : rootItem.get();
|
||||
Instance* destinationParent = destinationParentIdx.isValid() ? static_cast<Instance*>(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<Instance*>(parentIdx.internalPointer()) : workspace.get();
|
||||
Instance* parent = parentIdx.isValid() ? static_cast<Instance*>(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<Instance*>(index.internalPointer())->shared_from_this();
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Instance>(workspace))) {
|
||||
model(ExplorerModel(std::dynamic_pointer_cast<Instance>(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));
|
||||
|
|
|
@ -6,8 +6,6 @@
|
|||
Camera camera(glm::vec3(0.0, 0.0, 3.0));
|
||||
//std::vector<Part> parts;
|
||||
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<HierarchyPostUpdateHandler> hierarchyPostUpdateHandler;
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ typedef std::function<void(std::vector<InstanceRefWeak> oldSelection, std::vecto
|
|||
|
||||
extern Camera camera;
|
||||
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<HierarchyPostUpdateHandler> hierarchyPostUpdateHandler;
|
||||
|
||||
|
|
|
@ -18,4 +18,5 @@ DataModel::DataModel()
|
|||
|
||||
void DataModel::Init() {
|
||||
this->workspace = std::make_shared<Workspace>(shared<DataModel>());
|
||||
this->AddChild(this->workspace);
|
||||
}
|
|
@ -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> part = std::dynamic_pointer_cast<Part>(obj);
|
||||
const rp::Transform& transform = part->rigidBody->getTransform();
|
||||
|
|
|
@ -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> part = std::dynamic_pointer_cast<Part>(inst);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue