Clicking on a prefab will make it the current selection for painting

This commit is contained in:
Marcus Huderle 2022-09-06 20:42:19 -05:00
parent 3046a4d3ae
commit bc4cbbabfa
10 changed files with 153 additions and 31 deletions

View file

@ -1271,8 +1271,18 @@
<layout class="QVBoxLayout" name="verticalLayout_3"> <layout class="QVBoxLayout" name="verticalLayout_3">
<item> <item>
<widget class="QPushButton" name="pushButton"> <widget class="QPushButton" name="pushButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text"> <property name="text">
<string>Create New Prefab from Current Selection</string> <string>Create from Selection</string>
</property>
<property name="icon">
<iconset resource="../resources/images.qrc">
<normaloff>:/icons/add.ico</normaloff>:/icons/add.ico</iconset>
</property> </property>
<property name="autoDefault"> <property name="autoDefault">
<bool>false</bool> <bool>false</bool>
@ -1293,6 +1303,9 @@
<layout class="QVBoxLayout" name="verticalLayout_15"> <layout class="QVBoxLayout" name="verticalLayout_15">
<item> <item>
<widget class="QScrollArea" name="scrollArea"> <widget class="QScrollArea" name="scrollArea">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="widgetResizable"> <property name="widgetResizable">
<bool>true</bool> <bool>true</bool>
</property> </property>
@ -1301,11 +1314,23 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>396</width> <width>398</width>
<height>630</height> <height>631</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_14"> <layout class="QVBoxLayout" name="verticalLayout_14">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item> <item>
<widget class="QLabel" name="label_prefabHelp"> <widget class="QLabel" name="label_prefabHelp">
<property name="text"> <property name="text">

View file

@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>400</width> <width>400</width>
<height>149</height> <height>262</height>
</rect> </rect>
</property> </property>
<property name="sizePolicy"> <property name="sizePolicy">
@ -26,7 +26,7 @@
<string>Frame</string> <string>Frame</string>
</property> </property>
<property name="frameShape"> <property name="frameShape">
<enum>QFrame::Panel</enum> <enum>QFrame::Box</enum>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">
<property name="sizeConstraint"> <property name="sizeConstraint">
@ -34,45 +34,100 @@
</property> </property>
<item> <item>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="sizeConstraint"> <property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum> <enum>QLayout::SetMinimumSize</enum>
</property> </property>
<item> <item>
<widget class="QLabel" name="label_Name"> <widget class="QFrame" name="frame">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum"> <sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch> <horstretch>0</horstretch>
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<property name="text"> <property name="frameShape">
<string/> <enum>QFrame::NoFrame</enum>
</property> </property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="spacing">
<number>6</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>9</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<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>
<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>
<spacer name="horizontalSpacer_2">
<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> </widget>
</item> </item>
<item> <item>
<widget class="QGraphicsView" name="graphicsView_Prefab"> <widget class="ClickableGraphicsView" name="graphicsView_Prefab">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Minimum"> <sizepolicy hsizetype="Expanding" vsizetype="Minimum">
<horstretch>0</horstretch> <horstretch>0</horstretch>
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="styleSheet">
<string notr="true">background-color: transparent</string>
</property>
</widget> </widget>
</item> </item>
</layout> </layout>
</item> </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> <item>
<spacer name="horizontalSpacer"> <spacer name="horizontalSpacer">
<property name="orientation"> <property name="orientation">
@ -88,6 +143,13 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<customwidgets>
<customwidget>
<class>ClickableGraphicsView</class>
<extends>QGraphicsView</extends>
<header>graphicsview.h</header>
</customwidget>
</customwidgets>
<resources> <resources>
<include location="../resources/images.qrc"/> <include location="../resources/images.qrc"/>
</resources> </resources>

View file

@ -4,6 +4,23 @@
#include <QGraphicsView> #include <QGraphicsView>
#include <QMouseEvent> #include <QMouseEvent>
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 Editor;
class GraphicsView : public QGraphicsView class GraphicsView : public QGraphicsView

View file

@ -46,6 +46,7 @@ public:
bool selectFromMap(uint16_t metatileId, uint16_t collision, uint16_t elevation); bool selectFromMap(uint16_t metatileId, uint16_t collision, uint16_t elevation);
void setTilesets(Tileset*, Tileset*); void setTilesets(Tileset*, Tileset*);
MetatileSelection getMetatileSelection(); MetatileSelection getMetatileSelection();
void setDirectSelection(MetatileSelection selection);
void setExternalSelection(int, int, QList<uint16_t>, QList<QPair<uint16_t, uint16_t>>); void setExternalSelection(int, int, QList<uint16_t>, QList<QPair<uint16_t, uint16_t>>);
QPoint getMetatileIdCoordsOnWidget(uint16_t); QPoint getMetatileIdCoordsOnWidget(uint16_t);
void setMap(Map*); void setMap(Map*);

View file

@ -18,7 +18,7 @@ struct PrefabItem
class Prefab class Prefab
{ {
public: 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: private:
QList<PrefabItem> items; QList<PrefabItem> items;

View file

@ -554,6 +554,7 @@ bool MainWindow::openProject(QString dir) {
if (success) { if (success) {
prefab.initPrefabUI( prefab.initPrefabUI(
editor->metatile_selector_item,
ui->scrollAreaWidgetContents_Prefabs, ui->scrollAreaWidgetContents_Prefabs,
ui->label_prefabHelp, ui->label_prefabHelp,
editor->ui->comboBox_PrimaryTileset->currentText(), editor->ui->comboBox_PrimaryTileset->currentText(),

View file

@ -6,22 +6,25 @@ QPixmap drawMetatileSelection(MetatileSelection selection, Map *map) {
int width = selection.dimensions.x() * 16; int width = selection.dimensions.x() * 16;
int height = selection.dimensions.y() * 16; int height = selection.dimensions.y() * 16;
QImage image(width, height, QImage::Format_RGBA8888); QImage image(width, height, QImage::Format_RGBA8888);
image.fill(QColor(0, 0, 0, 0));
QPainter painter(&image); QPainter painter(&image);
for (int i = 0; i < selection.dimensions.x(); i++) { for (int i = 0; i < selection.dimensions.x(); i++) {
for (int j = 0; j < selection.dimensions.y(); j++) { for (int j = 0; j < selection.dimensions.y(); j++) {
int x = i * 16; int x = i * 16;
int y = j * 16; int y = j * 16;
QPoint metatile_origin = QPoint(x, y);
int index = j * selection.dimensions.x() + i; int index = j * selection.dimensions.x() + i;
MetatileSelectionItem item = selection.metatileItems.at(index); MetatileSelectionItem item = selection.metatileItems.at(index);
QImage metatile_image = getMetatileImage( if (item.enabled) {
item.metatileId, QImage metatile_image = getMetatileImage(
map->layout->tileset_primary, item.metatileId,
map->layout->tileset_secondary, map->layout->tileset_primary,
map->metatileLayerOrder, map->layout->tileset_secondary,
map->metatileLayerOpacity); map->metatileLayerOrder,
QPoint metatile_origin = QPoint(x, y); map->metatileLayerOpacity);
painter.drawImage(metatile_origin, metatile_image); painter.drawImage(metatile_origin, metatile_image);
}
} }
} }

View file

@ -127,6 +127,8 @@ void MapPixmapItem::paintNormal(int x, int y, bool fromScriptCall) {
if (map->getBlock(actualX, actualY, &block)) { if (map->getBlock(actualX, actualY, &block)) {
int index = j * selection.dimensions.x() + i; int index = j * selection.dimensions.x() + i;
MetatileSelectionItem item = selection.metatileItems.at(index); MetatileSelectionItem item = selection.metatileItems.at(index);
if (!item.enabled)
continue;
block.metatileId = item.metatileId; block.metatileId = item.metatileId;
if (selection.hasCollision && selection.collisionItems.length() == selection.metatileItems.length()) { if (selection.hasCollision && selection.collisionItems.length() == selection.metatileItems.length()) {
CollisionSelectionItem collisionItem = selection.collisionItems.at(index); CollisionSelectionItem collisionItem = selection.collisionItems.at(index);

View file

@ -95,6 +95,14 @@ void MetatileSelector::setExternalSelection(int width, int height, QList<uint16_
emit selectedMetatilesChanged(); emit selectedMetatilesChanged();
} }
void MetatileSelector::setDirectSelection(MetatileSelection selection) {
this->externalSelection = false;
this->externalSelectedMetatiles.clear();
this->selection = selection;
this->draw();
emit selectedMetatilesChanged();
}
bool MetatileSelector::shouldAcceptEvent(QGraphicsSceneMouseEvent *event) { bool MetatileSelector::shouldAcceptEvent(QGraphicsSceneMouseEvent *event) {
QPoint pos = this->getCellPos(event->pos()); QPoint pos = this->getCellPos(event->pos());
return Tileset::metatileIsValid(getMetatileId(pos.x(), pos.y()), this->primaryTileset, this->secondaryTileset); return Tileset::metatileIsValid(getMetatileId(pos.x(), pos.y()), this->primaryTileset, this->secondaryTileset);

View file

@ -86,7 +86,7 @@ QList<PrefabItem> Prefab::getPrefabsForTilesets(QString primaryTileset, QString
return filteredPrefabs; 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(); this->loadPrefabs();
QList<PrefabItem> prefabs = this->getPrefabsForTilesets(primaryTileset, secondaryTileset); QList<PrefabItem> prefabs = this->getPrefabsForTilesets(primaryTileset, secondaryTileset);
if (prefabs.isEmpty()) { if (prefabs.isEmpty()) {
@ -107,6 +107,9 @@ void Prefab::initPrefabUI(QWidget *prefabWidget, QLabel *emptyPrefabLabel, QStri
scene->itemsBoundingRect().height() + 2); scene->itemsBoundingRect().height() + 2);
prefabWidget->layout()->addWidget(frame); 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); auto spacer = new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::Expanding);
prefabWidget->layout()->addItem(spacer); prefabWidget->layout()->addItem(spacer);