diff --git a/forms/mainwindow.ui b/forms/mainwindow.ui index 52772b99..46e0117a 100644 --- a/forms/mainwindow.ui +++ b/forms/mainwindow.ui @@ -14,7 +14,7 @@ porymap - + 9 @@ -813,8 +813,8 @@ 0 0 - 423 - 74 + 428 + 77 @@ -1001,10 +1001,10 @@ - 8 + 0 0 411 - 413 + 449 @@ -1155,7 +1155,7 @@ 0 0 428 - 696 + 704 @@ -1258,6 +1258,199 @@ + + + + 0 + 0 + + + + Stamps + + + + 3 + + + 3 + + + 3 + + + 3 + + + 3 + + + + + Edit Stamps + + + + + + + Selection + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 0 + 0 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + 0 + 0 + + + + Qt::ScrollBarAlwaysOn + + + true + + + + + 0 + 0 + 411 + 468 + + + + + 0 + 0 + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + 0 + 0 + + + + + + + + + + @@ -1615,8 +1808,8 @@ 0 0 - 434 - 581 + 83 + 16 @@ -1709,8 +1902,8 @@ 0 0 - 434 - 581 + 83 + 16 @@ -1803,8 +1996,8 @@ 0 0 - 434 - 581 + 83 + 16 @@ -1903,8 +2096,8 @@ 0 0 - 434 - 581 + 83 + 16 @@ -1997,8 +2190,8 @@ 0 0 - 434 - 581 + 83 + 16 @@ -2051,8 +2244,8 @@ 0 0 - 434 - 625 + 100 + 30 @@ -2388,7 +2581,7 @@ Custom fields will be added to the map.json file for the current map. - true + false false @@ -2947,7 +3140,7 @@ 0 0 1287 - 22 + 21 diff --git a/include/core/stamp.h b/include/core/stamp.h new file mode 100644 index 00000000..2e74a347 --- /dev/null +++ b/include/core/stamp.h @@ -0,0 +1,11 @@ +#ifndef STAMP_H +#define STAMP_H + + +class Stamp +{ +public: + Stamp(); +}; + +#endif // STAMP_H diff --git a/include/editor.h b/include/editor.h index e28d9120..82e1b51b 100644 --- a/include/editor.h +++ b/include/editor.h @@ -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; diff --git a/include/ui/collisionpixmapitem.h b/include/ui/collisionpixmapitem.h index 2e3e74e3..7190dee5 100644 --- a/include/ui/collisionpixmapitem.h +++ b/include/ui/collisionpixmapitem.h @@ -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 getActivePaintType) + : MapPixmapItem(map, metatileSelector, stampSelector, settings, getActivePaintType){ this->movementPermissionsSelector = movementPermissionsSelector; this->opacity = opacity; map->setCollisionItem(this); diff --git a/include/ui/mappixmapitem.h b/include/ui/mappixmapitem.h index cd2d335c..ac2d71fe 100644 --- a/include/ui/mappixmapitem.h +++ b/include/ui/mappixmapitem.h @@ -4,8 +4,14 @@ #include "map.h" #include "settings.h" #include "metatileselector.h" +#include "stampselector.h" #include +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 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 getActivePaintType; Settings *settings; bool active; bool has_mouse = false; diff --git a/include/ui/metatileselector.h b/include/ui/metatileselector.h index 0c0b779f..69ad1502 100644 --- a/include/ui/metatileselector.h +++ b/include/ui/metatileselector.h @@ -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 metatileItems; - QList 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); diff --git a/include/ui/paintselection.h b/include/ui/paintselection.h new file mode 100644 index 00000000..d09fb5d4 --- /dev/null +++ b/include/ui/paintselection.h @@ -0,0 +1,48 @@ +#pragma once +#ifndef PAINTSELECTION_H +#define PAINTSELECTION_H + +#include "block.h" + +#include +#include + +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 metatileItems; + QList collisionItems; +}; + +class StampSelection: public PaintSelection +{ +public: + StampSelection() {} + bool paintNormal(int index, Block *block) override; + QList stampIds; +}; + +#endif // PAINTSELECTION_H diff --git a/include/ui/stampselector.h b/include/ui/stampselector.h new file mode 100644 index 00000000..c3480287 --- /dev/null +++ b/include/ui/stampselector.h @@ -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 diff --git a/include/ui/tileseteditor.h b/include/ui/tileseteditor.h index b03964b5..a083e420 100644 --- a/include/ui/tileseteditor.h +++ b/include/ui/tileseteditor.h @@ -9,6 +9,7 @@ #include "tileseteditortileselector.h" #include "metatilelayersitem.h" #include "map.h" +#include "metatile.h" namespace Ui { class TilesetEditor; diff --git a/porymap.pro b/porymap.pro index 8d2d0ba4..d28a0caf 100644 --- a/porymap.pro +++ b/porymap.pro @@ -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 \ diff --git a/src/core/stamp.cpp b/src/core/stamp.cpp new file mode 100644 index 00000000..7bbd17de --- /dev/null +++ b/src/core/stamp.cpp @@ -0,0 +1,6 @@ +#include "stamp.h" + +Stamp::Stamp() +{ + +} diff --git a/src/editor.cpp b/src/editor.cpp index 76124324..c24f9dd8 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -12,6 +12,7 @@ #include "config.h" #include "scripting.h" #include "customattributestable.h" +#include "mappixmapitem.h" #include #include #include @@ -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); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 519ec32b..22bd775b 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -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); diff --git a/src/ui/mappixmapitem.cpp b/src/ui/mappixmapitem.cpp index f44922a4..d4a5a318 100644 --- a/src/ui/mappixmapitem.cpp +++ b/src/ui/mappixmapitem.cpp @@ -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); } } diff --git a/src/ui/metatileselector.cpp b/src/ui/metatileselector.cpp index 3ce9cf55..e4c86796 100644 --- a/src/ui/metatileselector.cpp +++ b/src/ui/metatileselector.cpp @@ -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{true, metatileId}}), - QList(), - }; + this->selection = MetatileSelection(); + this->selection.dimensions = QPoint(1, 1); + this->selection.hasCollision = false; + this->selection.metatileItems = QList({MetatileSelectionItem{true, metatileId}}); + this->selection.collisionItems = QList(); QPoint coords = this->getMetatileIdCoords(metatileId); SelectablePixmapItem::select(coords.x(), coords.y(), 0, 0); this->updateSelectedMetatiles(); diff --git a/src/ui/paintselection.cpp b/src/ui/paintselection.cpp new file mode 100644 index 00000000..66ce7582 --- /dev/null +++ b/src/ui/paintselection.cpp @@ -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; +} diff --git a/src/ui/stampselector.cpp b/src/ui/stampselector.cpp new file mode 100644 index 00000000..4a8f8144 --- /dev/null +++ b/src/ui/stampselector.cpp @@ -0,0 +1,132 @@ +#include "stampselector.h" +#include "imageproviders.h" +#include "project.h" + +#include + +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(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; +}