121 lines
No EOL
3.6 KiB
C++
121 lines
No EOL
3.6 KiB
C++
#include "hierarchy.h"
|
|
|
|
#include <QMessageBox>
|
|
#include <engine.hpp>
|
|
#include <gameinstance.hpp>
|
|
|
|
#include "global.h"
|
|
|
|
Hierarchy::Hierarchy(QWidget* parent) : QFrame(parent)
|
|
{
|
|
setWindowTitle("Hierarchy");
|
|
|
|
QVBoxLayout* layout = new QVBoxLayout(this);
|
|
|
|
m_view = new QListView();
|
|
layout->addWidget(m_view);
|
|
m_view->setModel(new EntityItemModel);
|
|
m_view->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
connect(m_view, &QListView::customContextMenuRequested, this, &Hierarchy::viewCustomContextMenu);
|
|
|
|
GetEntPool().onEntityListChange.Register(&Hierarchy::OnChange, this);
|
|
|
|
connect(m_view, &QListView::clicked, this, &Hierarchy::OnSelect);
|
|
|
|
m_entityMenu = new QMenu();
|
|
|
|
QAction* deleteEntityAction = new QAction("Delete");
|
|
connect(deleteEntityAction, &QAction::triggered, [=]()
|
|
{
|
|
if(GetState().engine->GetGame()->GetWorld() == nullptr)
|
|
return;
|
|
|
|
if(m_contextEntity != nullptr && m_contextEntity->tag != "editor")
|
|
{
|
|
if(QMessageBox::critical(this,
|
|
"Entity Deletion Confirmation", "Are you sure you want to delete " + QString(m_contextEntity->name.c_str()),
|
|
QMessageBox::Yes,
|
|
QMessageBox::Cancel) == QMessageBox::Yes)
|
|
{
|
|
GetEntPool().Delete(m_contextEntity);
|
|
m_contextEntity = nullptr;
|
|
}
|
|
}
|
|
});
|
|
m_entityMenu->addAction(deleteEntityAction);
|
|
|
|
QAction* duplicateEntityAction = new QAction("Duplicate");
|
|
connect(duplicateEntityAction, &QAction::triggered, [=]()
|
|
{
|
|
if(GetState().engine->GetGame()->GetWorld() == nullptr)
|
|
return;
|
|
|
|
if(m_contextEntity != nullptr && m_contextEntity->tag != "editor")
|
|
{
|
|
GetEntPool().Duplicate(m_contextEntity, GetState().engine->GetGame()->GetWorld());
|
|
}
|
|
});
|
|
m_entityMenu->addAction(duplicateEntityAction);
|
|
|
|
m_otherMenu = new QMenu();
|
|
|
|
QAction* newEntityAction = new QAction("New Entity");
|
|
connect(newEntityAction, &QAction::triggered, [=]()
|
|
{
|
|
if(GetState().engine->GetGame()->GetWorld() == nullptr)
|
|
return;
|
|
|
|
GetEntPool().CreateEntity(GetState().engine->GetGame()->GetWorld());
|
|
});
|
|
m_otherMenu->addAction(newEntityAction);
|
|
|
|
GetEntPool().onEntityListChange(); //force refresh
|
|
}
|
|
|
|
Hierarchy::~Hierarchy()
|
|
{
|
|
GetEntPool().onEntityListChange.Unregister(this);
|
|
|
|
delete m_otherMenu;
|
|
delete m_entityMenu;
|
|
delete m_view;
|
|
|
|
m_otherMenu = nullptr;
|
|
m_entityMenu = nullptr;
|
|
m_view = nullptr;
|
|
}
|
|
|
|
void Hierarchy::OnChange()
|
|
{
|
|
auto entities = GetEntPool().GetAllEntities();
|
|
for(unsigned int i = 0; i < entities.size(); i++)
|
|
if(entities[i]->tag == "editor")
|
|
entities.erase(entities.begin() + i);
|
|
|
|
if(static_cast<unsigned>(m_view->currentIndex().row()) > entities.size())
|
|
m_view->setCurrentIndex(static_cast<EntityItemModel*>(m_view->model())->CreateIndex(entities.size()));
|
|
|
|
static_cast<EntityItemModel*>(m_view->model())->SetData(entities);
|
|
}
|
|
|
|
void Hierarchy::OnSelect(const QModelIndex &index)
|
|
{
|
|
GetState().selectedID = static_cast<EntityItemModel*>(m_view->model())->GetData()[index.row()]->GetID();
|
|
GetState().isSelectionEntity = true;
|
|
}
|
|
|
|
void Hierarchy::viewCustomContextMenu(const QPoint &point)
|
|
{
|
|
QModelIndex index = m_view->indexAt(point);
|
|
|
|
if(index.isValid())
|
|
{
|
|
m_contextEntity = static_cast<EntityItemModel*>(m_view->model())->GetData()[index.row()];
|
|
m_entityMenu->exec(m_view->mapToGlobal(point));
|
|
}
|
|
else
|
|
{
|
|
m_otherMenu->exec(m_view->mapToGlobal(point));
|
|
}
|
|
} |