Bootstrap stamping

This commit is contained in:
Marcus Huderle 2023-02-28 10:21:08 -06:00
parent 4fbe8bf5ef
commit 64004a68f2
17 changed files with 575 additions and 73 deletions

View file

@ -14,7 +14,7 @@
<string>porymap</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout_15">
<layout class="QGridLayout" name="gridLayout_8">
<property name="leftMargin">
<number>9</number>
</property>
@ -813,8 +813,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>423</width>
<height>74</height>
<width>428</width>
<height>77</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_7">
@ -1001,10 +1001,10 @@
</property>
<property name="geometry">
<rect>
<x>8</x>
<x>0</x>
<y>0</y>
<width>411</width>
<height>413</height>
<height>449</height>
</rect>
</property>
<property name="sizePolicy">
@ -1155,7 +1155,7 @@
<x>0</x>
<y>0</y>
<width>428</width>
<height>696</height>
<height>704</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_7">
@ -1258,6 +1258,199 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_stamps">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<attribute name="title">
<string>Stamps</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_stamps">
<property name="spacing">
<number>3</number>
</property>
<property name="leftMargin">
<number>3</number>
</property>
<property name="topMargin">
<number>3</number>
</property>
<property name="rightMargin">
<number>3</number>
</property>
<property name="bottomMargin">
<number>3</number>
</property>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Edit Stamps</string>
</property>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_4">
<property name="title">
<string>Selection</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_12">
<property name="spacing">
<number>0</number>
</property>
<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>
<spacer name="horizontalSpacer_7">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QGraphicsView" name="graphicsView_stampSelection">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_8">
<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="QScrollArea" name="scrollArea_3">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</enum>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents_3">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>411</width>
<height>468</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QGridLayout" name="gridLayout_10">
<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>
<property name="spacing">
<number>0</number>
</property>
<item row="2" column="0">
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="2">
<spacer name="horizontalSpacer_5">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="1">
<widget class="QGraphicsView" name="graphicsView_Stamps">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_prefabs">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
@ -1615,8 +1808,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>434</width>
<height>581</height>
<width>83</width>
<height>16</height>
</rect>
</property>
<property name="sizePolicy">
@ -1709,8 +1902,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>434</width>
<height>581</height>
<width>83</width>
<height>16</height>
</rect>
</property>
<property name="sizePolicy">
@ -1803,8 +1996,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>434</width>
<height>581</height>
<width>83</width>
<height>16</height>
</rect>
</property>
<property name="sizePolicy">
@ -1903,8 +2096,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>434</width>
<height>581</height>
<width>83</width>
<height>16</height>
</rect>
</property>
<property name="sizePolicy">
@ -1997,8 +2190,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>434</width>
<height>581</height>
<width>83</width>
<height>16</height>
</rect>
</property>
<property name="sizePolicy">
@ -2051,8 +2244,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>434</width>
<height>625</height>
<width>100</width>
<height>30</height>
</rect>
</property>
<property name="sizePolicy">
@ -2388,7 +2581,7 @@
<string>Custom fields will be added to the map.json file for the current map.</string>
</property>
<attribute name="horizontalHeaderVisible">
<bool>true</bool>
<bool>false</bool>
</attribute>
<attribute name="horizontalHeaderCascadingSectionResizes">
<bool>false</bool>
@ -2947,7 +3140,7 @@
<x>0</x>
<y>0</y>
<width>1287</width>
<height>22</height>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">

11
include/core/stamp.h Normal file
View file

@ -0,0 +1,11 @@
#ifndef STAMP_H
#define STAMP_H
class Stamp
{
public:
Stamp();
};
#endif // STAMP_H

View file

@ -13,6 +13,7 @@
#include "mapconnection.h"
#include "metatileselector.h"
#include "stampselector.h"
#include "movementpermissionsselector.h"
#include "project.h"
#include "ui_mainwindow.h"
@ -54,6 +55,7 @@ public:
void saveEncounterTabData();
bool displayMap();
void displayMetatileSelector();
void displayStampSelector();
void displayMapMetatiles();
void displayMapMovementPermissions();
void displayBorderMetatiles();
@ -127,6 +129,9 @@ public:
QGraphicsScene *scene_elevation_metatiles = nullptr;
MetatileSelector *metatile_selector_item = nullptr;
QGraphicsScene *scene_stamps = nullptr;
StampSelector *stamp_selector_item = nullptr;
BorderMetatilesPixmapItem *selected_border_metatiles_item = nullptr;
CurrentSelectedMetatilesPixmapItem *current_metatile_selection_item = nullptr;
MovementPermissionsSelector *movement_permissions_selector_item = nullptr;

View file

@ -2,6 +2,7 @@
#define COLLISIONPIXMAPITEM_H
#include "metatileselector.h"
//#include "stampselector.h"
#include "movementpermissionsselector.h"
#include "mappixmapitem.h"
#include "map.h"
@ -10,8 +11,14 @@
class CollisionPixmapItem : public MapPixmapItem {
Q_OBJECT
public:
CollisionPixmapItem(Map *map, MovementPermissionsSelector *movementPermissionsSelector, MetatileSelector *metatileSelector, Settings *settings, qreal *opacity)
: MapPixmapItem(map, metatileSelector, settings){
CollisionPixmapItem(Map *map,
MovementPermissionsSelector *movementPermissionsSelector,
MetatileSelector *metatileSelector,
StampSelector *stampSelector,
Settings *settings,
qreal *opacity,
std::function<PaintType(void)> getActivePaintType)
: MapPixmapItem(map, metatileSelector, stampSelector, settings, getActivePaintType){
this->movementPermissionsSelector = movementPermissionsSelector;
this->opacity = opacity;
map->setCollisionItem(this);

View file

@ -4,8 +4,14 @@
#include "map.h"
#include "settings.h"
#include "metatileselector.h"
#include "stampselector.h"
#include <QGraphicsPixmapItem>
enum PaintType {
PaintTypeMetatile,
PaintTypeStamp,
};
class MapPixmapItem : public QObject, public QGraphicsPixmapItem {
Q_OBJECT
@ -18,10 +24,12 @@ public:
Metatiles,
EventObjects
};
MapPixmapItem(Map *map_, MetatileSelector *metatileSelector, Settings *settings) {
MapPixmapItem(Map *map_, MetatileSelector *metatileSelector, StampSelector *stampSelector, Settings *settings, std::function<PaintType(void)> getActivePaintType) {
this->map = map_;
this->map->setMapItem(this);
this->metatileSelector = metatileSelector;
this->stampSelector = stampSelector;
this->getActivePaintType = getActivePaintType;
this->settings = settings;
this->paintingMode = PaintMode::Metatiles;
this->lockedAxis = MapPixmapItem::Axis::None;
@ -31,6 +39,8 @@ public:
MapPixmapItem::PaintMode paintingMode;
Map *map;
MetatileSelector *metatileSelector;
StampSelector *stampSelector;
std::function<PaintType(void)> getActivePaintType;
Settings *settings;
bool active;
bool has_mouse = false;

View file

@ -6,27 +6,7 @@
#include "map.h"
#include "tileset.h"
#include "maplayout.h"
struct MetatileSelectionItem
{
bool enabled;
uint16_t metatileId;
};
struct CollisionSelectionItem
{
bool enabled;
uint16_t collision;
uint16_t elevation;
};
struct MetatileSelection
{
QPoint dimensions;
bool hasCollision;
QList<MetatileSelectionItem> metatileItems;
QList<CollisionSelectionItem> collisionItems;
};
#include "paintselection.h"
class MetatileSelector: public SelectablePixmapItem {
Q_OBJECT
@ -38,7 +18,7 @@ public:
this->map = map;
this->primaryTileset = map->layout->tileset_primary;
this->secondaryTileset = map->layout->tileset_secondary;
this->selection = MetatileSelection{};
this->selection = MetatileSelection();
setAcceptHoverEvents(true);
}
QPoint getSelectionDimensions();
@ -74,7 +54,6 @@ private:
uint16_t getMetatileId(int x, int y);
QPoint getMetatileIdCoords(uint16_t);
bool shouldAcceptEvent(QGraphicsSceneMouseEvent*);
bool selectionIsValid();
signals:
void hoveredMetatileSelectionChanged(uint16_t);

View file

@ -0,0 +1,48 @@
#pragma once
#ifndef PAINTSELECTION_H
#define PAINTSELECTION_H
#include "block.h"
#include <QList>
#include <QPoint>
struct MetatileSelectionItem
{
bool enabled;
uint16_t metatileId;
};
struct CollisionSelectionItem
{
bool enabled;
uint16_t collision;
uint16_t elevation;
};
class PaintSelection
{
public:
QPoint dimensions;
virtual bool paintNormal(int, Block*) = 0;
};
class MetatileSelection: public PaintSelection
{
public:
MetatileSelection() {}
bool paintNormal(int index, Block *block) override;
bool hasCollision = false;
QList<MetatileSelectionItem> metatileItems;
QList<CollisionSelectionItem> collisionItems;
};
class StampSelection: public PaintSelection
{
public:
StampSelection() {}
bool paintNormal(int index, Block *block) override;
QList<uint16_t> stampIds;
};
#endif // PAINTSELECTION_H

View file

@ -0,0 +1,47 @@
#ifndef STAMPSELECTOR_H
#define STAMPSELECTOR_H
#include "selectablepixmapitem.h"
#include "map.h"
#include "tileset.h"
#include "maplayout.h"
#include "paintselection.h"
class StampSelector: public SelectablePixmapItem {
Q_OBJECT
public:
StampSelector(int numStampsWide, Map *map): SelectablePixmapItem(16, 16) {
this->numStampsWide = numStampsWide;
this->map = map;
this->primaryTileset = map->layout->tileset_primary;
this->secondaryTileset = map->layout->tileset_secondary;
this->selection = StampSelection{};
}
QPoint getSelectionDimensions();
void draw();
bool select(uint16_t stampId);
void setTilesets(Tileset*, Tileset*);
StampSelection getStampSelection();
QPoint getStampIdCoordsOnWidget(uint16_t);
void setMap(Map*);
Tileset *primaryTileset;
Tileset *secondaryTileset;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent*);
void mouseMoveEvent(QGraphicsSceneMouseEvent*);
void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
private:
int numStampsWide;
Map *map;
StampSelection selection;
void updateSelectedStamps();
uint16_t getStampId(int x, int y);
QPoint getStampIdCoords(uint16_t);
bool shouldAcceptEvent(QGraphicsSceneMouseEvent*);
signals:
void selectedStampsChanged();
};
#endif // STAMPSELECTOR_H

View file

@ -9,6 +9,7 @@
#include "tileseteditortileselector.h"
#include "metatilelayersitem.h"
#include "map.h"
#include "metatile.h"
namespace Ui {
class TilesetEditor;

View file

@ -27,6 +27,7 @@ SOURCES += src/core/block.cpp \
src/core/metatileparser.cpp \
src/core/paletteutil.cpp \
src/core/parseutil.cpp \
src/core/stamp.cpp \
src/core/tile.cpp \
src/core/tileset.cpp \
src/core/regionmap.cpp \
@ -74,6 +75,7 @@ SOURCES += src/core/block.cpp \
src/ui/encountertabledelegates.cpp \
src/ui/paletteeditor.cpp \
src/ui/selectablepixmapitem.cpp \
src/ui/stampselector.cpp \
src/ui/tileseteditor.cpp \
src/ui/tileseteditormetatileselector.cpp \
src/ui/tileseteditortileselector.cpp \
@ -87,6 +89,7 @@ SOURCES += src/core/block.cpp \
src/ui/shortcut.cpp \
src/ui/shortcutseditor.cpp \
src/ui/multikeyedit.cpp \
src/ui/paintselection.cpp \
src/ui/prefabframe.cpp \
src/ui/preferenceeditor.cpp \
src/ui/regionmappropertiesdialog.cpp \
@ -113,6 +116,7 @@ HEADERS += include/core/block.h \
include/core/metatileparser.h \
include/core/paletteutil.h \
include/core/parseutil.h \
include/core/stamp.h \
include/core/tile.h \
include/core/tileset.h \
include/core/regionmap.h \
@ -133,6 +137,7 @@ HEADERS += include/core/block.h \
include/ui/collisionpixmapitem.h \
include/ui/connectionpixmapitem.h \
include/ui/currentselectedmetatilespixmapitem.h \
include/ui/paintselection.h \
include/ui/prefabframe.h \
include/ui/regionmaplayoutpixmapitem.h \
include/ui/regionmapentriespixmapitem.h \
@ -161,6 +166,7 @@ HEADERS += include/core/block.h \
include/ui/adjustingstackedwidget.h \
include/ui/paletteeditor.h \
include/ui/selectablepixmapitem.h \
include/ui/stampselector.h \
include/ui/tileseteditor.h \
include/ui/tileseteditormetatileselector.h \
include/ui/tileseteditortileselector.h \

6
src/core/stamp.cpp Normal file
View file

@ -0,0 +1,6 @@
#include "stamp.h"
Stamp::Stamp()
{
}

View file

@ -12,6 +12,7 @@
#include "config.h"
#include "scripting.h"
#include "customattributestable.h"
#include "mappixmapitem.h"
#include <QCheckBox>
#include <QPainter>
#include <QMouseEvent>
@ -1345,6 +1346,7 @@ bool Editor::displayMap() {
}
displayMetatileSelector();
displayStampSelector();
displayMovementPermissionSelector();
displayMapMetatiles();
displayMapMovementPermissions();
@ -1400,8 +1402,30 @@ void Editor::displayMetatileSelector() {
scene_metatiles->addItem(metatile_selector_item);
}
void Editor::displayStampSelector() {
if (stamp_selector_item && stamp_selector_item->scene()) {
stamp_selector_item->scene()->removeItem(stamp_selector_item);
delete scene_stamps;
}
scene_stamps = new QGraphicsScene;
if (!stamp_selector_item) {
stamp_selector_item = new StampSelector(8, map);
stamp_selector_item->select(0);
} else {
stamp_selector_item->setMap(map);
stamp_selector_item->setTilesets(map->layout->tileset_primary, map->layout->tileset_secondary);
}
scene_stamps->addItem(stamp_selector_item);
}
void Editor::displayMapMetatiles() {
map_item = new MapPixmapItem(map, this->metatile_selector_item, this->settings);
map_item = new MapPixmapItem(map, this->metatile_selector_item, this->stamp_selector_item, this->settings, [=](){
if (this->ui->mapViewTab->currentIndex() == 2) {
return PaintType::PaintTypeStamp;
}
return PaintType::PaintTypeMetatile;
});
connect(map_item, &MapPixmapItem::mouseEvent, this, &Editor::mouseEvent_map);
connect(map_item, &MapPixmapItem::startPaint, this, &Editor::onMapStartPaint);
connect(map_item, &MapPixmapItem::endPaint, this, &Editor::onMapEndPaint);
@ -1426,8 +1450,13 @@ void Editor::displayMapMovementPermissions() {
scene->removeItem(collision_item);
delete collision_item;
}
collision_item = new CollisionPixmapItem(map, this->movement_permissions_selector_item,
this->metatile_selector_item, this->settings, &this->collisionOpacity);
collision_item = new CollisionPixmapItem(map,
this->movement_permissions_selector_item,
this->metatile_selector_item,
this->stamp_selector_item,
this->settings,
&this->collisionOpacity,
[]() {return PaintType::PaintTypeMetatile; });
connect(collision_item, &CollisionPixmapItem::mouseEvent, this, &Editor::mouseEvent_collision);
connect(collision_item, &CollisionPixmapItem::hoveredMapMovementPermissionChanged,
this, &Editor::onHoveredMapMovementPermissionChanged);

View file

@ -696,6 +696,10 @@ void MainWindow::refreshMapScene()
ui->graphicsView_Collision->setFixedSize(editor->movement_permissions_selector_item->pixmap().width() + 2, editor->movement_permissions_selector_item->pixmap().height() + 2);
on_horizontalSlider_MetatileZoom_valueChanged(ui->horizontalSlider_MetatileZoom->value());
ui->graphicsView_Stamps->setScene(editor->scene_stamps);
//ui->graphicsView_Stamps->setSceneRect(editor->scene_stamps->sceneRect());
ui->graphicsView_Stamps->setFixedSize(editor->stamp_selector_item->pixmap().width() + 2, editor->stamp_selector_item->pixmap().height() + 2);
}
void MainWindow::openWarpMap(QString map_name, int event_id, Event::Group event_group) {
@ -1673,6 +1677,8 @@ void MainWindow::on_mapViewTab_tabBarClicked(int index)
editor->setEditingCollision();
} else if (index == 2) {
editor->setEditingMap();
} else if (index == 3) {
editor->setEditingMap();
prefab.tryImportDefaultPrefabs(this->editor->map);
}
editor->setCursorRectVisible(false);
@ -2205,7 +2211,7 @@ void MainWindow::on_toolButton_Paint_clicked()
editor->settings->mapCursor = QCursor(QPixmap(":/icons/pencil_cursor.ico"), 10, 10);
// do not stop single tile mode when editing collision
if (ui->mapViewTab->currentIndex() == 0)
if (ui->mapViewTab->currentIndex() != 1)
editor->cursorMapTileRect->stopSingleTileMode();
ui->graphicsView_Map->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);

View file

@ -102,40 +102,42 @@ void MapPixmapItem::shift(int xDelta, int yDelta, bool fromScriptCall) {
}
void MapPixmapItem::paintNormal(int x, int y, bool fromScriptCall) {
MetatileSelection selection = this->metatileSelector->getMetatileSelection();
PaintSelection *selection = nullptr;
if (this->getActivePaintType() == PaintType::PaintTypeMetatile) {
MetatileSelection metatileSelection = this->metatileSelector->getMetatileSelection();
selection = &metatileSelection;
} else {
StampSelection stampSelection = this->stampSelector->getStampSelection();
selection = &stampSelection;
}
int initialX = fromScriptCall ? x : this->paint_tile_initial_x;
int initialY = fromScriptCall ? y : this->paint_tile_initial_y;
int selectionWidth = selection->dimensions.x();
int selectionHeight = selection->dimensions.y();
// Snap the selected position to the top-left of the block boundary.
// This allows painting via dragging the mouse to tile the painted region.
int xDiff = x - initialX;
int yDiff = y - initialY;
if (xDiff < 0 && xDiff % selection.dimensions.x() != 0) xDiff -= selection.dimensions.x();
if (yDiff < 0 && yDiff % selection.dimensions.y() != 0) yDiff -= selection.dimensions.y();
if (xDiff < 0 && xDiff % selectionWidth != 0) xDiff -= selectionWidth;
if (yDiff < 0 && yDiff % selectionHeight != 0) yDiff -= selectionHeight;
x = initialX + (xDiff / selection.dimensions.x()) * selection.dimensions.x();
y = initialY + (yDiff / selection.dimensions.y()) * selection.dimensions.y();
x = initialX + (xDiff / selectionWidth) * selectionWidth;
y = initialY + (yDiff / selectionHeight) * selectionHeight;
// for edit history
Blockdata oldMetatiles = !fromScriptCall ? map->layout->blockdata : Blockdata();
for (int i = 0; i < selection.dimensions.x() && i + x < map->getWidth(); i++)
for (int j = 0; j < selection.dimensions.y() && j + y < map->getHeight(); j++) {
for (int i = 0; i < selectionWidth && i + x < map->getWidth(); i++)
for (int j = 0; j < selectionHeight && j + y < map->getHeight(); j++) {
int actualX = i + x;
int actualY = j + y;
Block block;
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);
block.collision = collisionItem.collision;
block.elevation = collisionItem.elevation;
int index = j * selectionWidth + i;
if (selection->paintNormal(index, &block)) {
map->setBlock(actualX, actualY, block, !fromScriptCall);
}
map->setBlock(actualX, actualY, block, !fromScriptCall);
}
}

View file

@ -45,12 +45,11 @@ bool MetatileSelector::select(uint16_t metatileId) {
if (!Tileset::metatileIsValid(metatileId, this->primaryTileset, this->secondaryTileset)) return false;
this->externalSelection = false;
this->prefabSelection = false;
this->selection = MetatileSelection{
QPoint(1, 1),
false,
QList<MetatileSelectionItem>({MetatileSelectionItem{true, metatileId}}),
QList<CollisionSelectionItem>(),
};
this->selection = MetatileSelection();
this->selection.dimensions = QPoint(1, 1);
this->selection.hasCollision = false;
this->selection.metatileItems = QList<MetatileSelectionItem>({MetatileSelectionItem{true, metatileId}});
this->selection.collisionItems = QList<CollisionSelectionItem>();
QPoint coords = this->getMetatileIdCoords(metatileId);
SelectablePixmapItem::select(coords.x(), coords.y(), 0, 0);
this->updateSelectedMetatiles();

21
src/ui/paintselection.cpp Normal file
View file

@ -0,0 +1,21 @@
#include "paintselection.h"
#include "log.h"
bool MetatileSelection::paintNormal(int index, Block *block) {
MetatileSelectionItem item = this->metatileItems.at(index);
if (!item.enabled)
return false;
block->metatileId = item.metatileId;
if (this->hasCollision && this->collisionItems.length() == this->metatileItems.length()) {
CollisionSelectionItem collisionItem = this->collisionItems.at(index);
block->collision = collisionItem.collision;
block->elevation = collisionItem.elevation;
}
return true;
}
bool StampSelection::paintNormal(int index, Block *block) {
block->metatileId = 1;
return true;
}

132
src/ui/stampselector.cpp Normal file
View file

@ -0,0 +1,132 @@
#include "stampselector.h"
#include "imageproviders.h"
#include "project.h"
#include <QPainter>
QPoint StampSelector::getSelectionDimensions() {
return selection.dimensions;
}
void StampSelector::draw() {
if (!this->primaryTileset || !this->secondaryTileset) {
this->setPixmap(QPixmap());
}
int primaryLength = this->primaryTileset->metatiles.length();
int length_ = primaryLength + this->secondaryTileset->metatiles.length();
int height_ = length_ / this->numStampsWide;
if (length_ % this->numStampsWide != 0) {
height_++;
}
QImage image(this->numStampsWide * 16, height_ * 16, QImage::Format_RGBA8888);
image.fill(Qt::magenta);
QPainter painter(&image);
for (int i = 0; i < length_; i++) {
int tile = i;
if (i >= primaryLength) {
tile += Project::getNumMetatilesPrimary() - primaryLength;
}
QImage metatile_image = getMetatileImage(tile, this->primaryTileset, this->secondaryTileset, map->metatileLayerOrder, map->metatileLayerOpacity);
int map_y = i / this->numStampsWide;
int map_x = i % this->numStampsWide;
QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
painter.drawImage(metatile_origin, metatile_image);
}
painter.end();
this->setPixmap(QPixmap::fromImage(image));
this->drawSelection();
}
bool StampSelector::select(uint16_t stampId) {
// TODO:
if (stampId > 9999999999999) return false;
this->selection = StampSelection();
this->selection.dimensions = QPoint(1, 1);
this->selection.stampIds = QList<uint16_t>(stampId);
QPoint coords = this->getStampIdCoords(stampId);
SelectablePixmapItem::select(coords.x(), coords.y(), 0, 0);
this->updateSelectedStamps();
return true;
}
void StampSelector::setTilesets(Tileset *primaryTileset, Tileset *secondaryTileset) {
this->primaryTileset = primaryTileset;
this->secondaryTileset = secondaryTileset;
this->updateSelectedStamps();
this->draw();
}
StampSelection StampSelector::getStampSelection() {
return selection;
}
bool StampSelector::shouldAcceptEvent(QGraphicsSceneMouseEvent *event) {
// QPoint pos = this->getCellPos(event->pos());
// return Tileset::metatileIsValid(getStampId(pos.x(), pos.y()), this->primaryTileset, this->secondaryTileset);
// TODO:
return true;
}
void StampSelector::mousePressEvent(QGraphicsSceneMouseEvent *event) {
if (!shouldAcceptEvent(event)) return;
SelectablePixmapItem::mousePressEvent(event);
this->updateSelectedStamps();
}
void StampSelector::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
if (!shouldAcceptEvent(event)) return;
SelectablePixmapItem::mouseMoveEvent(event);
this->updateSelectedStamps();
}
void StampSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
if (!shouldAcceptEvent(event)) return;
SelectablePixmapItem::mouseReleaseEvent(event);
this->updateSelectedStamps();
}
void StampSelector::updateSelectedStamps() {
this->selection.stampIds.clear();
this->selection.dimensions = SelectablePixmapItem::getSelectionDimensions();
QPoint origin = this->getSelectionStart();
for (int j = 0; j < this->selection.dimensions.y(); j++) {
for (int i = 0; i < this->selection.dimensions.x(); i++) {
uint16_t stampId = this->getStampId(origin.x() + i, origin.y() + j);
// TODO: check if stamp id is valid
if (false)
stampId = 0;
this->selection.stampIds.append(stampId);
}
}
emit selectedStampsChanged();
}
uint16_t StampSelector::getStampId(int x, int y) {
return y * this->numStampsWide + x;
}
QPoint StampSelector::getStampIdCoords(uint16_t stampId) {
// TODO: check if stamp id is valid
if (false)
{
// Invalid stamp id.
return QPoint(0, 0);
}
return QPoint(stampId % this->numStampsWide, stampId / this->numStampsWide);
}
QPoint StampSelector::getStampIdCoordsOnWidget(uint16_t stampId) {
QPoint pos = getStampIdCoords(stampId);
pos.rx() = (pos.x() * this->cellWidth) + (this->cellWidth / 2);
pos.ry() = (pos.y() * this->cellHeight) + (this->cellHeight / 2);
return pos;
}
void StampSelector::setMap(Map *map) {
this->map = map;
}