Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
graph/tools/leveleditor/src/properties.cpp

162 lines
4.6 KiB
C++
Raw Normal View History

2018-12-25 23:04:32 -05:00
#include "properties.h"
2018-12-25 22:38:26 -05:00
#include <QIcon>
#include <QLabel>
#include <QLineEdit>
2018-12-25 22:51:26 -05:00
#include <QComboBox>
2018-12-26 07:13:03 -05:00
#include <QMenu>
2018-12-25 22:38:26 -05:00
#include "context.h"
#include "ecs.h"
#include "worldmanager.h"
#include "vec3edit.h"
#include "collapsesection.h"
2018-12-25 22:51:26 -05:00
#include "coloredit.h"
2018-12-25 22:38:26 -05:00
2018-12-25 23:04:32 -05:00
Properties::Properties(Context& context, QWidget* parent) : QWidget(parent), context(context) {
setWindowTitle("Properties");
setWindowIcon(QIcon::fromTheme("document-properties"));
2018-12-25 22:38:26 -05:00
layout = new QVBoxLayout();
layout->setAlignment(Qt::AlignTop);
layout->setSpacing(0);
2018-12-26 07:13:03 -05:00
QMenu* addComponentMenu = new QMenu();
QAction* addMeshComponent = new QAction("Mesh");
connect(addMeshComponent, &QAction::triggered, [&context] {
const auto& entity = context.selectedEntities[0];
ECS::addComponent<MeshComponent>(entity);
emit context.entitiesChanged();
});
addComponentMenu->addAction(addMeshComponent);
QAction* addLightComponent = new QAction("Light");
connect(addLightComponent, &QAction::triggered, [&context] {
const auto& entity = context.selectedEntities[0];
ECS::addComponent<LightComponent>(entity);
emit context.entitiesChanged();
});
addComponentMenu->addAction(addLightComponent);
addComponentButton = new QPushButton("Add Component...");
addComponentButton->setMenu(addComponentMenu);
layout->addWidget(addComponentButton);
2018-12-25 22:38:26 -05:00
setLayout(layout);
rebuild();
2018-12-25 23:04:32 -05:00
connect(&context, &Context::selectionChanged, this, &Properties::rebuild);
2018-12-26 07:13:03 -05:00
connect(&context, &Context::entitiesChanged, this, &Properties::rebuild);
2018-12-25 22:38:26 -05:00
}
2018-12-25 23:04:32 -05:00
void Properties::rebuild() {
2018-12-25 22:38:26 -05:00
for(auto section : sections) {
layout->removeWidget(section);
delete section;
}
sections.clear();
2018-12-26 07:13:03 -05:00
addComponentButton->setEnabled(context.selectedEntities.size() != 0);
2018-12-25 22:38:26 -05:00
if(context.selectedEntities.size() == 0)
return;
const auto& entity = context.selectedEntities[0];
InfoComponent* info = ECS::getComponent<InfoComponent>(entity);
if(info)
2018-12-25 23:04:32 -05:00
addInfoSection(info);
2018-12-25 22:38:26 -05:00
TransformComponent* transform = ECS::getComponent<TransformComponent>(entity);
if(transform)
2018-12-25 23:04:32 -05:00
addTransformSection(transform);
2018-12-25 22:38:26 -05:00
MeshComponent* mesh = ECS::getComponent<MeshComponent>(entity);
if(mesh)
2018-12-25 23:04:32 -05:00
addMeshSection(mesh);
2018-12-25 22:51:26 -05:00
LightComponent* light = ECS::getComponent<LightComponent>(entity);
if(light)
2018-12-25 23:04:32 -05:00
addLightSection(light);
2018-12-25 22:38:26 -05:00
}
2018-12-25 23:04:32 -05:00
void Properties::addInfoSection(InfoComponent* info) {
2018-12-25 22:38:26 -05:00
CollapseSection* section = new CollapseSection("Info");
layout->addWidget(section);
sections.push_back(section);
QGridLayout* layout = new QGridLayout();
section->setLayout(layout);
QLabel* nameLabel = new QLabel("Name");
layout->addWidget(nameLabel);
QLineEdit* nameEdit = new QLineEdit();
nameEdit->setText(info->name.c_str());
connect(nameEdit, &QLineEdit::editingFinished, [info, nameEdit] {
info->name = nameEdit->text().toStdString();
});
layout->addWidget(nameEdit, 0, 1);
}
2018-12-25 23:04:32 -05:00
void Properties::addTransformSection(TransformComponent* transform) {
2018-12-25 22:38:26 -05:00
CollapseSection* section = new CollapseSection("Transform");
layout->addWidget(section);
sections.push_back(section);
QGridLayout* layout = new QGridLayout();
section->setLayout(layout);
QLabel* positionLabel = new QLabel("Position");
layout->addWidget(positionLabel);
Vector3Edit* positionEdit = new Vector3Edit(transform->position);
layout->addWidget(positionEdit, 0, 1);
}
2018-12-25 23:04:32 -05:00
void Properties::addMeshSection(MeshComponent* mesh) {
2018-12-25 22:38:26 -05:00
CollapseSection* section = new CollapseSection("Mesh", true);
layout->addWidget(section);
sections.push_back(section);
QGridLayout* layout = new QGridLayout();
section->setLayout(layout);
}
2018-12-25 22:51:26 -05:00
2018-12-25 23:04:32 -05:00
void Properties::addLightSection(LightComponent* light) {
2018-12-25 22:51:26 -05:00
CollapseSection* section = new CollapseSection("Light", true);
layout->addWidget(section);
sections.push_back(section);
QGridLayout* layout = new QGridLayout();
section->setLayout(layout);
QLabel* typeLabel = new QLabel("Type");
layout->addWidget(typeLabel);
QComboBox* typeCombo = new QComboBox();
typeCombo->addItem("Point");
typeCombo->addItem("Directional");
typeCombo->setCurrentIndex((int)light->type);
connect(typeCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), [light](int index) {
light->type = (LightType)index;
});
layout->addWidget(typeCombo, 0, 1);
QLabel* colorLabel = new QLabel("Color");
layout->addWidget(colorLabel, 1, 0);
ColorEdit* colorEdit = new ColorEdit(light->color);
layout->addWidget(colorEdit, 1, 1);
}