feat(editor): insert objects from context menu
This commit is contained in:
parent
c26c9aaeef
commit
2dc9df4d1f
4 changed files with 29 additions and 5 deletions
|
@ -61,7 +61,7 @@ const InstanceType* Part::GetClass() {
|
|||
return &TYPE;
|
||||
}
|
||||
|
||||
Part::Part(): Part(PartConstructParams { .color = Color3(0.639216f, 0.635294f, 0.647059f) }) {
|
||||
Part::Part(): Part(PartConstructParams { .size = glm::vec3(2, 1.2, 4), .color = Color3(0.639216f, 0.635294f, 0.647059f) }) {
|
||||
}
|
||||
|
||||
Part::Part(PartConstructParams params): Instance(&TYPE), cframe(CFrame::FromEulerAnglesXYZ((Vector3)params.rotation) + params.position),
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
#include "explorermodel.h"
|
||||
#include "common.h"
|
||||
#include <qicon.h>
|
||||
#include <qmimedata.h>
|
||||
#include <QWidget>
|
||||
|
||||
// https://doc.qt.io/qt-6/qtwidgets-itemviews-simpletreemodel-example.html#testing-the-model
|
||||
|
||||
std::map<std::string, QImage> instanceIconCache;
|
||||
std::map<std::string, QIcon> instanceIconCache;
|
||||
|
||||
ExplorerModel::ExplorerModel(InstanceRef dataRoot, QWidget *parent)
|
||||
: QAbstractItemModel(parent)
|
||||
|
@ -136,13 +137,13 @@ Qt::ItemFlags ExplorerModel::flags(const QModelIndex &index) const
|
|||
: Qt::NoItemFlags | Qt::ItemIsDropEnabled;
|
||||
}
|
||||
|
||||
QImage ExplorerModel::iconOf(const InstanceType* type) const {
|
||||
QIcon ExplorerModel::iconOf(const InstanceType* type) const {
|
||||
if (instanceIconCache.count(type->className)) return instanceIconCache[type->className];
|
||||
|
||||
const InstanceType* currentClass = type;
|
||||
while (currentClass->explorerIcon.empty()) currentClass = currentClass->super;
|
||||
|
||||
QImage icon("assets/icons/" + QString::fromStdString(currentClass->explorerIcon));
|
||||
QIcon icon("assets/icons/" + QString::fromStdString(currentClass->explorerIcon));
|
||||
instanceIconCache[type->className] = icon;
|
||||
return icon;
|
||||
}
|
||||
|
|
|
@ -31,12 +31,13 @@ public:
|
|||
Qt::DropActions supportedDropActions() const override;
|
||||
InstanceRef fromIndex(const QModelIndex index) const;
|
||||
QModelIndex ObjectToIndex(InstanceRef item);
|
||||
|
||||
QIcon iconOf(const InstanceType* type) const;
|
||||
|
||||
void updateRoot(InstanceRef newRoot);
|
||||
private:
|
||||
InstanceRef rootItem;
|
||||
QModelIndex toIndex(InstanceRef item);
|
||||
QImage iconOf(const InstanceType* type) const;
|
||||
};
|
||||
|
||||
// #endif
|
|
@ -1,6 +1,10 @@
|
|||
#include "explorerview.h"
|
||||
#include "common.h"
|
||||
#include "../ui_mainwindow.h"
|
||||
#include "objects/base/instance.h"
|
||||
#include "objects/meta.h"
|
||||
#include <memory>
|
||||
#include <qaction.h>
|
||||
|
||||
#define M_mainWindow dynamic_cast<MainWindow*>(window())
|
||||
|
||||
|
@ -75,6 +79,24 @@ void ExplorerView::buildContextMenu() {
|
|||
contextMenu.addSeparator();
|
||||
contextMenu.addAction(M_mainWindow->ui->actionSaveModel);
|
||||
contextMenu.addAction(M_mainWindow->ui->actionInsertModel);
|
||||
|
||||
// Insert Object menu
|
||||
|
||||
QMenu* insertObjectMenu = new QMenu("Insert Object");
|
||||
contextMenu.addMenu(insertObjectMenu);
|
||||
|
||||
for (const auto& [_, type] : INSTANCE_MAP) {
|
||||
if (type->flags & INSTANCE_NOTCREATABLE || !type->constructor) continue;
|
||||
|
||||
QAction* instAction = new QAction(model.iconOf(type), QString::fromStdString(type->className));
|
||||
insertObjectMenu->addAction(instAction);
|
||||
connect(instAction, &QAction::triggered, this, [&]() {
|
||||
if (getSelection().size() == 0 || getSelection()[0].expired()) return;
|
||||
std::shared_ptr<Instance> instParent = getSelection()[0].lock();
|
||||
std::shared_ptr<Instance> newInst = type->constructor();
|
||||
newInst->SetParent(instParent);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void ExplorerView::updateRoot(InstanceRef newRoot) {
|
||||
|
|
Loading…
Add table
Reference in a new issue