From bc4cbbabfad7279fbbd27615cbadac0382088bae Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Tue, 6 Sep 2022 20:42:19 -0500 Subject: [PATCH] Clicking on a prefab will make it the current selection for painting --- forms/mainwindow.ui | 31 +++++- forms/prefabframe.ui | 98 +++++++++++++++---- include/ui/graphicsview.h | 17 ++++ include/ui/metatileselector.h | 1 + include/ui/prefab.h | 2 +- src/mainwindow.cpp | 1 + src/ui/currentselectedmetatilespixmapitem.cpp | 19 ++-- src/ui/mappixmapitem.cpp | 2 + src/ui/metatileselector.cpp | 8 ++ src/ui/prefab.cpp | 5 +- 10 files changed, 153 insertions(+), 31 deletions(-) diff --git a/forms/mainwindow.ui b/forms/mainwindow.ui index ee20e661..7ed791e1 100644 --- a/forms/mainwindow.ui +++ b/forms/mainwindow.ui @@ -1271,8 +1271,18 @@ + + + 0 + 0 + + - Create New Prefab from Current Selection + Create from Selection + + + + :/icons/add.ico:/icons/add.ico false @@ -1293,6 +1303,9 @@ + + QFrame::NoFrame + true @@ -1301,11 +1314,23 @@ 0 0 - 396 - 630 + 398 + 631 + + 0 + + + 0 + + + 0 + + + 0 + diff --git a/forms/prefabframe.ui b/forms/prefabframe.ui index 9753dd43..d7c57821 100644 --- a/forms/prefabframe.ui +++ b/forms/prefabframe.ui @@ -7,7 +7,7 @@ 0 0 400 - 149 + 262 @@ -26,7 +26,7 @@ Frame - QFrame::Panel + QFrame::Box @@ -34,45 +34,100 @@ + + 0 + QLayout::SetMinimumSize - + - + 0 0 - - + + QFrame::NoFrame + + QFrame::Raised + + + + 6 + + + 0 + + + 6 + + + 9 + + + 6 + + + + + + + + + :/icons/delete.ico:/icons/delete.ico + + + + + + + + 0 + 0 + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + - + 0 0 + + false + + + background-color: transparent + - - - - - - - - :/icons/delete.ico:/icons/delete.ico - - - @@ -88,6 +143,13 @@ + + + ClickableGraphicsView + QGraphicsView +
graphicsview.h
+
+
diff --git a/include/ui/graphicsview.h b/include/ui/graphicsview.h index 76dac180..b7ea81bf 100644 --- a/include/ui/graphicsview.h +++ b/include/ui/graphicsview.h @@ -4,6 +4,23 @@ #include #include +class ClickableGraphicsView : public QGraphicsView +{ + Q_OBJECT +public: + ClickableGraphicsView() : QGraphicsView() {} + ClickableGraphicsView(QWidget *parent) : QGraphicsView(parent) {} + +public: + void mousePressEvent(QMouseEvent *event) { + QGraphicsView::mousePressEvent(event); + emit this->clicked(); + } + +signals: + void clicked(); +}; + class Editor; class GraphicsView : public QGraphicsView diff --git a/include/ui/metatileselector.h b/include/ui/metatileselector.h index 9cb75def..f5b28d3a 100644 --- a/include/ui/metatileselector.h +++ b/include/ui/metatileselector.h @@ -46,6 +46,7 @@ public: bool selectFromMap(uint16_t metatileId, uint16_t collision, uint16_t elevation); void setTilesets(Tileset*, Tileset*); MetatileSelection getMetatileSelection(); + void setDirectSelection(MetatileSelection selection); void setExternalSelection(int, int, QList, QList>); QPoint getMetatileIdCoordsOnWidget(uint16_t); void setMap(Map*); diff --git a/include/ui/prefab.h b/include/ui/prefab.h index a33a6947..b529615b 100644 --- a/include/ui/prefab.h +++ b/include/ui/prefab.h @@ -18,7 +18,7 @@ struct PrefabItem class Prefab { public: - void initPrefabUI(QWidget *prefabWidget, QLabel *emptyPrefabLabel, QString primaryTileset, QString secondaryTileset, Map *map); + void initPrefabUI(MetatileSelector *selector, QWidget *prefabWidget, QLabel *emptyPrefabLabel, QString primaryTileset, QString secondaryTileset, Map *map); private: QList items; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index bdf4a72e..00467203 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -554,6 +554,7 @@ bool MainWindow::openProject(QString dir) { if (success) { prefab.initPrefabUI( + editor->metatile_selector_item, ui->scrollAreaWidgetContents_Prefabs, ui->label_prefabHelp, editor->ui->comboBox_PrimaryTileset->currentText(), diff --git a/src/ui/currentselectedmetatilespixmapitem.cpp b/src/ui/currentselectedmetatilespixmapitem.cpp index 35e52037..0967e558 100644 --- a/src/ui/currentselectedmetatilespixmapitem.cpp +++ b/src/ui/currentselectedmetatilespixmapitem.cpp @@ -6,22 +6,25 @@ 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); + image.fill(QColor(0, 0, 0, 0)); QPainter painter(&image); for (int i = 0; i < selection.dimensions.x(); i++) { for (int j = 0; j < selection.dimensions.y(); j++) { int x = i * 16; int y = j * 16; + QPoint metatile_origin = QPoint(x, y); int index = j * selection.dimensions.x() + i; MetatileSelectionItem item = selection.metatileItems.at(index); - QImage metatile_image = getMetatileImage( - item.metatileId, - map->layout->tileset_primary, - map->layout->tileset_secondary, - map->metatileLayerOrder, - map->metatileLayerOpacity); - QPoint metatile_origin = QPoint(x, y); - painter.drawImage(metatile_origin, metatile_image); + if (item.enabled) { + QImage metatile_image = getMetatileImage( + item.metatileId, + map->layout->tileset_primary, + map->layout->tileset_secondary, + map->metatileLayerOrder, + map->metatileLayerOpacity); + painter.drawImage(metatile_origin, metatile_image); + } } } diff --git a/src/ui/mappixmapitem.cpp b/src/ui/mappixmapitem.cpp index d591418c..c245f4cb 100644 --- a/src/ui/mappixmapitem.cpp +++ b/src/ui/mappixmapitem.cpp @@ -127,6 +127,8 @@ void MapPixmapItem::paintNormal(int x, int y, bool fromScriptCall) { if (map->getBlock(actualX, actualY, &block)) { int index = j * selection.dimensions.x() + i; MetatileSelectionItem item = selection.metatileItems.at(index); + if (!item.enabled) + continue; block.metatileId = item.metatileId; if (selection.hasCollision && selection.collisionItems.length() == selection.metatileItems.length()) { CollisionSelectionItem collisionItem = selection.collisionItems.at(index); diff --git a/src/ui/metatileselector.cpp b/src/ui/metatileselector.cpp index 0edde7ca..f18f0fb2 100644 --- a/src/ui/metatileselector.cpp +++ b/src/ui/metatileselector.cpp @@ -95,6 +95,14 @@ void MetatileSelector::setExternalSelection(int width, int height, QListexternalSelection = false; + this->externalSelectedMetatiles.clear(); + this->selection = selection; + this->draw(); + emit selectedMetatilesChanged(); +} + bool MetatileSelector::shouldAcceptEvent(QGraphicsSceneMouseEvent *event) { QPoint pos = this->getCellPos(event->pos()); return Tileset::metatileIsValid(getMetatileId(pos.x(), pos.y()), this->primaryTileset, this->secondaryTileset); diff --git a/src/ui/prefab.cpp b/src/ui/prefab.cpp index 56463fdb..da6d11dd 100644 --- a/src/ui/prefab.cpp +++ b/src/ui/prefab.cpp @@ -86,7 +86,7 @@ QList Prefab::getPrefabsForTilesets(QString primaryTileset, QString return filteredPrefabs; } -void Prefab::initPrefabUI(QWidget *prefabWidget, QLabel *emptyPrefabLabel, QString primaryTileset, QString secondaryTileset, Map *map) { +void Prefab::initPrefabUI(MetatileSelector *selector, QWidget *prefabWidget, QLabel *emptyPrefabLabel, QString primaryTileset, QString secondaryTileset, Map *map) { this->loadPrefabs(); QList prefabs = this->getPrefabsForTilesets(primaryTileset, secondaryTileset); if (prefabs.isEmpty()) { @@ -107,6 +107,9 @@ void Prefab::initPrefabUI(QWidget *prefabWidget, QLabel *emptyPrefabLabel, QStri scene->itemsBoundingRect().height() + 2); prefabWidget->layout()->addWidget(frame); + QObject::connect(frame->ui->graphicsView_Prefab, &ClickableGraphicsView::clicked, [=](){ + selector->setDirectSelection(item.selection); + }); } auto spacer = new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::Expanding); prefabWidget->layout()->addItem(spacer);