Load and display prefabs in UI

This commit is contained in:
Marcus Huderle 2022-09-05 20:51:31 -05:00
parent a943b6b260
commit 3046a4d3ae
11 changed files with 316 additions and 3 deletions

95
forms/prefabframe.ui Normal file
View file

@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>PrefabFrame</class>
<widget class="QFrame" name="PrefabFrame">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>149</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="windowTitle">
<string>Frame</string>
</property>
<property name="frameShape">
<enum>QFrame::Panel</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
<item>
<widget class="QLabel" name="label_Name">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QGraphicsView" name="graphicsView_Prefab">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../resources/images.qrc">
<normaloff>:/icons/delete.ico</normaloff>:/icons/delete.ico</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources>
<include location="../resources/images.qrc"/>
</resources>
<connections/>
</ui>

View file

@ -157,6 +157,7 @@ public:
this->enableFloorNumber = false;
this->createMapTextFile = false;
this->enableTripleLayerMetatiles = false;
this->prefabFilepath = QString();
this->customScripts.clear();
this->readKeys.clear();
}
@ -193,6 +194,8 @@ public:
bool getTripleLayerMetatilesEnabled();
void setCustomScripts(QList<QString> scripts);
QList<QString> getCustomScripts();
void setPrefabFilepath(QString filepath);
QString getPrefabFilepath();
protected:
virtual QString getConfigFilepath() override;
virtual void parseConfigKeyValue(QString key, QString value) override;
@ -217,6 +220,7 @@ private:
bool enableTripleLayerMetatiles;
QList<QString> customScripts;
QStringList readKeys;
QString prefabFilepath;
};
extern ProjectConfig projectConfig;

View file

@ -18,4 +18,6 @@ public:
void setMap(Map *map) { this->map = map; }
};
QPixmap drawMetatileSelection(MetatileSelection selection, Map *map);
#endif // CURRENTSELECTEDMETATILESPIXMAPITEM_H

31
include/ui/prefab.h Normal file
View file

@ -0,0 +1,31 @@
#ifndef PREFAB_H
#define PREFAB_H
#include "ui/metatileselector.h"
#include "map.h"
#include <QString>
#include <QLabel>
struct PrefabItem
{
QString name;
QString primaryTileset;
QString secondaryTileset;
MetatileSelection selection;
};
class Prefab
{
public:
void initPrefabUI(QWidget *prefabWidget, QLabel *emptyPrefabLabel, QString primaryTileset, QString secondaryTileset, Map *map);
private:
QList<PrefabItem> items;
void loadPrefabs();
QList<PrefabItem> getPrefabsForTilesets(QString primaryTileset, QString secondaryTileset);
};
extern Prefab prefab;
#endif // PREFAB_H

22
include/ui/prefabframe.h Normal file
View file

@ -0,0 +1,22 @@
#ifndef PREFABFRAME_H
#define PREFABFRAME_H
#include <QFrame>
namespace Ui {
class PrefabFrame;
}
class PrefabFrame : public QFrame
{
Q_OBJECT
public:
explicit PrefabFrame(QWidget *parent = nullptr);
~PrefabFrame();
public:
Ui::PrefabFrame *ui;
};
#endif // PREFABFRAME_H

View file

@ -45,6 +45,7 @@ SOURCES += src/core/block.cpp \
src/ui/connectionpixmapitem.cpp \
src/ui/currentselectedmetatilespixmapitem.cpp \
src/ui/overlay.cpp \
src/ui/prefab.cpp \
src/ui/regionmaplayoutpixmapitem.cpp \
src/ui/regionmapentriespixmapitem.cpp \
src/ui/cursortilerect.cpp \
@ -80,6 +81,7 @@ SOURCES += src/core/block.cpp \
src/ui/shortcut.cpp \
src/ui/shortcutseditor.cpp \
src/ui/multikeyedit.cpp \
src/ui/prefabframe.cpp \
src/ui/preferenceeditor.cpp \
src/ui/regionmappropertiesdialog.cpp \
src/ui/colorpicker.cpp \
@ -126,6 +128,7 @@ HEADERS += include/core/block.h \
include/ui/collisionpixmapitem.h \
include/ui/connectionpixmapitem.h \
include/ui/currentselectedmetatilespixmapitem.h \
include/ui/prefabframe.h \
include/ui/regionmaplayoutpixmapitem.h \
include/ui/regionmapentriespixmapitem.h \
include/ui/cursortilerect.h \
@ -163,6 +166,7 @@ HEADERS += include/core/block.h \
include/ui/shortcut.h \
include/ui/shortcutseditor.h \
include/ui/multikeyedit.h \
include/ui/prefab.h \
include/ui/preferenceeditor.h \
include/ui/regionmappropertiesdialog.h \
include/ui/colorpicker.h \
@ -176,6 +180,7 @@ HEADERS += include/core/block.h \
FORMS += forms/mainwindow.ui \
forms/eventpropertiesframe.ui \
forms/prefabframe.ui \
forms/tileseteditor.ui \
forms/paletteeditor.ui \
forms/regionmapeditor.ui \

View file

@ -503,6 +503,8 @@ void ProjectConfig::parseConfigKeyValue(QString key, QString value) {
this->customScripts.append(script);
}
}
} else if (key == "prefabs_filepath") {
this->prefabFilepath = value;
} else {
logWarn(QString("Invalid config key found in config file %1: '%2'").arg(this->getConfigFilepath()).arg(key));
}
@ -540,6 +542,7 @@ QMap<QString, QString> ProjectConfig::getKeyValueMap() {
map.insert("create_map_text_file", QString::number(this->createMapTextFile));
map.insert("enable_triple_layer_metatiles", QString::number(this->enableTripleLayerMetatiles));
map.insert("custom_scripts", this->customScripts.join(","));
map.insert("prefabs_filepath", this->prefabFilepath);
return map;
}
@ -732,6 +735,15 @@ QList<QString> ProjectConfig::getCustomScripts() {
return this->customScripts;
}
void ProjectConfig::setPrefabFilepath(QString filepath) {
this->prefabFilepath = filepath;
this->save();
}
QString ProjectConfig::getPrefabFilepath() {
return this->prefabFilepath;
}
ShortcutsConfig shortcutsConfig;
QString ShortcutsConfig::getConfigFilepath() {

View file

@ -16,6 +16,7 @@
#include "flowlayout.h"
#include "shortcut.h"
#include "mapparser.h"
#include "prefab.h"
#include <QFileDialog>
#include <QClipboard>
@ -552,6 +553,12 @@ bool MainWindow::openProject(QString dir) {
}
if (success) {
prefab.initPrefabUI(
ui->scrollAreaWidgetContents_Prefabs,
ui->label_prefabHelp,
editor->ui->comboBox_PrimaryTileset->currentText(),
editor->ui->comboBox_SecondaryTileset->currentText(),
editor->map);
for (auto action : this->registeredActions) {
this->ui->menuTools->removeAction(action);
}

View file

@ -2,8 +2,7 @@
#include "imageproviders.h"
#include <QPainter>
void CurrentSelectedMetatilesPixmapItem::draw() {
MetatileSelection selection = metatileSelector->getMetatileSelection();
QPixmap drawMetatileSelection(MetatileSelection selection, Map *map) {
int width = selection.dimensions.x() * 16;
int height = selection.dimensions.y() * 16;
QImage image(width, height, QImage::Format_RGBA8888);
@ -27,5 +26,10 @@ void CurrentSelectedMetatilesPixmapItem::draw() {
}
painter.end();
setPixmap(QPixmap::fromImage(image));
return QPixmap::fromImage(image);
}
void CurrentSelectedMetatilesPixmapItem::draw() {
MetatileSelection selection = metatileSelector->getMetatileSelection();
setPixmap(drawMetatileSelection(selection, this->map));
}

116
src/ui/prefab.cpp Normal file
View file

@ -0,0 +1,116 @@
#include "prefab.h"
#include "prefabframe.h"
#include "ui_prefabframe.h"
#include "parseutil.h"
#include "currentselectedmetatilespixmapitem.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QGraphicsPixmapItem>
#include <QWidget>
#include <QDir>
#include <QSpacerItem>
void Prefab::loadPrefabs() {
this->items.clear();
QString filepath = projectConfig.getPrefabFilepath();
if (filepath.isEmpty()) return;
ParseUtil parser;
QJsonDocument prefabDoc;
if (!QFile::exists(filepath) || !parser.tryParseJsonFile(&prefabDoc, filepath)) {
QString relativePath = QDir::cleanPath(projectConfig.getProjectDir() + QDir::separator() + filepath);
if (!parser.tryParseJsonFile(&prefabDoc, relativePath)) {
logError(QString("Failed to prefab data from %1").arg(filepath));
return;
}
}
QJsonArray prefabs = prefabDoc.array();
if (prefabs.size() == 0) {
logWarn(QString("Prefabs array is empty or missing in %1.").arg(filepath));
return;
}
for (int i = 0; i < prefabs.size(); i++) {
QJsonObject prefabObj = prefabs[i].toObject();
if (prefabObj.isEmpty())
continue;
int width = prefabObj["width"].toInt();
int height = prefabObj["height"].toInt();
if (width <= 0 || height <= 0)
continue;
QString name = prefabObj["name"].toString();
QString primaryTileset = prefabObj["primary_tileset"].toString();
QString secondaryTileset = prefabObj["secondary_tileset"].toString();
MetatileSelection selection;
selection.dimensions = QPoint(width, height);
selection.hasCollision = true;
for (int j = 0; j < width * height; j++) {
selection.metatileItems.append(MetatileSelectionItem{false, 0});
selection.collisionItems.append(CollisionSelectionItem{false, 0, 0});
}
QJsonArray metatiles = prefabObj["metatiles"].toArray();
for (int j = 0; j < metatiles.size(); j++) {
QJsonObject metatileObj = metatiles[j].toObject();
int x = metatileObj["x"].toInt();
int y = metatileObj["y"].toInt();
if (x < 0 || x >= width || y < 0 || y >= height)
continue;
int index = y * width + x;
selection.metatileItems[index].enabled = true;
selection.metatileItems[index].metatileId = metatileObj["metatile_id"].toInt();
selection.collisionItems[index].enabled = true;
selection.collisionItems[index].collision = metatileObj["collision"].toInt();
selection.collisionItems[index].elevation = metatileObj["elevation"].toInt();
}
this->items.append(PrefabItem{name, primaryTileset, secondaryTileset, selection});
}
}
QList<PrefabItem> Prefab::getPrefabsForTilesets(QString primaryTileset, QString secondaryTileset) {
QList<PrefabItem> filteredPrefabs;
for (auto item : this->items) {
if ((item.primaryTileset.isEmpty() || item.primaryTileset == primaryTileset) &&
(item.secondaryTileset.isEmpty() || item.secondaryTileset == secondaryTileset)) {
filteredPrefabs.append(item);
}
}
return filteredPrefabs;
}
void Prefab::initPrefabUI(QWidget *prefabWidget, QLabel *emptyPrefabLabel, QString primaryTileset, QString secondaryTileset, Map *map) {
this->loadPrefabs();
QList<PrefabItem> prefabs = this->getPrefabsForTilesets(primaryTileset, secondaryTileset);
if (prefabs.isEmpty()) {
emptyPrefabLabel->setVisible(true);
return;
}
emptyPrefabLabel->setVisible(false);
for (auto item : this->items) {
PrefabFrame *frame = new PrefabFrame();
frame->ui->label_Name->setText(item.name);
auto scene = new QGraphicsScene;
scene->addPixmap(drawMetatileSelection(item.selection, map));
scene->setSceneRect(scene->itemsBoundingRect());
frame->ui->graphicsView_Prefab->setScene(scene);
frame->ui->graphicsView_Prefab->setFixedSize(scene->itemsBoundingRect().width() + 2,
scene->itemsBoundingRect().height() + 2);
prefabWidget->layout()->addWidget(frame);
}
auto spacer = new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::Expanding);
prefabWidget->layout()->addItem(spacer);
}
Prefab prefab;

15
src/ui/prefabframe.cpp Normal file
View file

@ -0,0 +1,15 @@
#include "prefabframe.h"
#include "ui_prefabframe.h"
PrefabFrame::PrefabFrame(QWidget *parent) :
QFrame(parent),
ui(new Ui::PrefabFrame)
{
ui->setupUi(this);
}
PrefabFrame::~PrefabFrame()
{
delete ui;
}