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">
<item>
<widget class="QPushButton" name="pushButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<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 name="autoDefault">
<bool>false</bool>
@ -1293,6 +1303,9 @@
<layout class="QVBoxLayout" name="verticalLayout_15">
<item>
<widget class="QScrollArea" name="scrollArea">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
@ -1301,11 +1314,23 @@
<rect>
<x>0</x>
<y>0</y>
<width>396</width>
<height>630</height>
<width>398</width>
<height>631</height>
</rect>
</property>
<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>
<widget class="QLabel" name="label_prefabHelp">
<property name="text">

View file

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

View file

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

View file

@ -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<uint16_t>, QList<QPair<uint16_t, uint16_t>>);
QPoint getMetatileIdCoordsOnWidget(uint16_t);
void setMap(Map*);

View file

@ -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<PrefabItem> items;

View file

@ -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(),

View file

@ -6,24 +6,27 @@ 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);
if (item.enabled) {
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);
}
}
}
painter.end();
return QPixmap::fromImage(image);

View file

@ -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);

View file

@ -95,6 +95,14 @@ void MetatileSelector::setExternalSelection(int width, int height, QList<uint16_
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) {
QPoint pos = this->getCellPos(event->pos());
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;
}
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<PrefabItem> 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);