From 405eafb969ef1ed55cb4792099cac7897c4f7713 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Sun, 24 Sep 2017 12:43:26 -0700 Subject: [PATCH] Qt: Make clicking tiles work --- include/mgba/core/map-cache.h | 2 ++ src/core/map-cache.c | 6 ++-- src/platform/qt/AssetTile.cpp | 37 +++++++++++++++++++++- src/platform/qt/AssetTile.h | 7 +++++ src/platform/qt/AssetTile.ui | 29 +++++++++++++++++ src/platform/qt/MapView.cpp | 59 +++++++++++++++++++++++++++++++++-- src/platform/qt/MapView.h | 7 +++++ src/platform/qt/MapView.ui | 8 ++--- 8 files changed, 145 insertions(+), 10 deletions(-) diff --git a/include/mgba/core/map-cache.h b/include/mgba/core/map-cache.h index 22771459d..0ceeb5d98 100644 --- a/include/mgba/core/map-cache.h +++ b/include/mgba/core/map-cache.h @@ -66,6 +66,8 @@ void mMapCacheConfigureSystem(struct mMapCache* cache, mMapCacheSystemInfo confi void mMapCacheConfigureMap(struct mMapCache* cache, uint32_t mapStart); void mMapCacheWriteVRAM(struct mMapCache* cache, uint32_t address); +uint32_t mMapCacheTileId(struct mMapCache* cache, unsigned x, unsigned y); + bool mMapCacheCheckTile(struct mMapCache* cache, const struct mMapCacheEntry* entry, unsigned x, unsigned y); void mMapCacheCleanTile(struct mMapCache* cache, struct mMapCacheEntry* entry, unsigned x, unsigned y); diff --git a/src/core/map-cache.c b/src/core/map-cache.c index ed18979f6..980f26899 100644 --- a/src/core/map-cache.c +++ b/src/core/map-cache.c @@ -114,7 +114,7 @@ static inline void _cleanTile(struct mMapCache* cache, const color_t* tile, colo } } -static inline size_t _tileId(struct mMapCache* cache, unsigned x, unsigned y) { +uint32_t mMapCacheTileId(struct mMapCache* cache, unsigned x, unsigned y) { int tilesWide = mMapCacheSystemInfoGetTilesWide(cache->sysConfig); int tilesHigh = mMapCacheSystemInfoGetTilesHigh(cache->sysConfig); int stride = 1 << mMapCacheSystemInfoGetMacroTileSize(cache->sysConfig); @@ -130,7 +130,7 @@ static inline size_t _tileId(struct mMapCache* cache, unsigned x, unsigned y) { } void mMapCacheCleanTile(struct mMapCache* cache, struct mMapCacheEntry* entry, unsigned x, unsigned y) { - size_t location = _tileId(cache, x, y); + size_t location = mMapCacheTileId(cache, x, y); struct mMapCacheEntry* status = &cache->status[location]; const color_t* tile = NULL; if (!mMapCacheEntryFlagsIsVramClean(status->flags)) { @@ -156,7 +156,7 @@ void mMapCacheCleanTile(struct mMapCache* cache, struct mMapCacheEntry* entry, u } bool mMapCacheCheckTile(struct mMapCache* cache, const struct mMapCacheEntry* entry, unsigned x, unsigned y) { - size_t location = _tileId(cache, x, y); + size_t location = mMapCacheTileId(cache, x, y); struct mMapCacheEntry* status = &cache->status[location]; int paletteId = mMapCacheEntryFlagsGetPaletteId(status->flags); const color_t* tile = NULL; diff --git a/src/platform/qt/AssetTile.cpp b/src/platform/qt/AssetTile.cpp index 452829f85..9bdb57150 100644 --- a/src/platform/qt/AssetTile.cpp +++ b/src/platform/qt/AssetTile.cpp @@ -9,6 +9,7 @@ #include "GBAApp.h" #include +#include #include #ifdef M_CORE_GBA @@ -34,12 +35,25 @@ AssetTile::AssetTile(QWidget* parent) const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont); m_ui.tileId->setFont(font); + m_ui.paletteId->setFont(font); m_ui.address->setFont(font); m_ui.r->setFont(font); m_ui.g->setFont(font); m_ui.b->setFont(font); } +void AssetTile::addCustomProperty(const QString& id, const QString& visibleName) { + QHBoxLayout* newLayout = new QHBoxLayout; + newLayout->addWidget(new QLabel(visibleName)); + QLabel* value = new QLabel; + value->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); + value->setAlignment(Qt::AlignRight); + newLayout->addWidget(value); + m_customProperties[id] = value; + int index = layout()->indexOf(m_ui.line); + static_cast(layout())->insertLayout(index, newLayout); +} + void AssetTile::setController(std::shared_ptr controller) { m_cacheSet = controller->graphicCaches(); switch (controller->platform()) { @@ -94,16 +108,30 @@ void AssetTile::selectIndex(int index) { } data = mTileCacheGetTile(tileCache, index, paletteId); m_ui.tileId->setText(QString::number(dispIndex)); + m_ui.paletteId->setText(QString::number(paletteId)); m_ui.address->setText(tr("%0%1%2") .arg(m_addressWidth == 4 ? index >= m_boundary / 2 : 0) .arg(m_addressWidth == 4 ? ":" : "x") .arg(dispIndex * bpp | base, m_addressWidth, 16, QChar('0'))); + int flip = 0; + if (m_flipH) { + flip |= 007; + } + if (m_flipV) { + flip |= 070; + } for (int i = 0; i < 64; ++i) { - m_ui.preview->setColor(i, data[i]); + m_ui.preview->setColor(i ^ flip, data[i]); } m_ui.preview->update(); } +void AssetTile::setFlip(bool h, bool v) { + m_flipH = h; + m_flipV = v; + selectIndex(m_index); +} + void AssetTile::selectColor(int index) { const color_t* data; mTileCache* tileCache = m_tileCaches[m_index >= m_boundary]; @@ -122,3 +150,10 @@ void AssetTile::selectColor(int index) { m_ui.b->setText(tr("0x%0 (%1)").arg(b, 2, 16, QChar('0')).arg(b, 2, 10, QChar('0'))); } +void AssetTile::setCustomProperty(const QString& id, const QVariant& value) { + QLabel* label = m_customProperties[id]; + if (!label) { + return; + } + label->setText(value.toString()); +} diff --git a/src/platform/qt/AssetTile.h b/src/platform/qt/AssetTile.h index dbe6acf57..35dd14e5b 100644 --- a/src/platform/qt/AssetTile.h +++ b/src/platform/qt/AssetTile.h @@ -21,12 +21,15 @@ Q_OBJECT public: AssetTile(QWidget* parent = nullptr); void setController(std::shared_ptr); + void addCustomProperty(const QString& id, const QString& visibleName); public slots: void setPalette(int); void setBoundary(int boundary, int set0, int set1); void selectIndex(int); + void setFlip(bool h, bool v); void selectColor(int); + void setCustomProperty(const QString& id, const QVariant& value); private: Ui::AssetTile m_ui; @@ -40,6 +43,10 @@ private: int m_addressBase; int m_boundary; int m_boundaryBase; + bool m_flipH = false; + bool m_flipV = false; + + QMap m_customProperties; }; } diff --git a/src/platform/qt/AssetTile.ui b/src/platform/qt/AssetTile.ui index b9075f24f..92a8623e1 100644 --- a/src/platform/qt/AssetTile.ui +++ b/src/platform/qt/AssetTile.ui @@ -2,6 +2,14 @@ AssetTile + + + 0 + 0 + 171 + 355 + + 0 @@ -46,6 +54,27 @@ + + + + + + Palette # + + + + + + + 0 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + diff --git a/src/platform/qt/MapView.cpp b/src/platform/qt/MapView.cpp index 4db7b1b8b..2481bd9a3 100644 --- a/src/platform/qt/MapView.cpp +++ b/src/platform/qt/MapView.cpp @@ -10,9 +10,16 @@ #include "LogController.h" #include +#ifdef M_CORE_GBA +#include +#endif +#ifdef M_CORE_GB +#include +#endif #include #include +#include #include #include @@ -28,17 +35,22 @@ MapView::MapView(std::shared_ptr controller, QWidget* parent) switch (m_controller->platform()) { #ifdef M_CORE_GBA case PLATFORM_GBA: - m_ui.tile->setBoundary(2048, 0, 2); + m_boundary = 2048; + m_addressBase = BASE_VRAM; + m_addressWidth = 8; break; #endif #ifdef M_CORE_GB case PLATFORM_GB: - m_ui.tile->setBoundary(1024, 0, 0); + m_boundary = 1024; + m_addressBase = GB_BASE_VRAM; + m_addressWidth = 4; break; #endif default: return; } + m_ui.tile->setBoundary(m_boundary, 0, 0); connect(m_ui.magnification, static_cast(&QSpinBox::valueChanged), [this]() { updateTiles(true); @@ -67,6 +79,10 @@ MapView::MapView(std::shared_ptr controller, QWidget* parent) #else m_ui.exportButton->setVisible(false); #endif + m_ui.map->installEventFilter(this); + m_ui.tile->addCustomProperty("mapAddr", tr("Map Addr.")); + m_ui.tile->addCustomProperty("flip", tr("Mirror")); + selectTile(0, 0); } void MapView::selectMap(int map) { @@ -80,6 +96,45 @@ void MapView::selectMap(int map) { updateTiles(true); } +void MapView::selectTile(int x, int y) { + CoreController::Interrupter interrupter(m_controller); + mMapCache* mapCache = mMapCacheSetGetPointer(&m_cacheSet->maps, m_map); + size_t tileCache = mTileCacheSetIndex(&m_cacheSet->tiles, mapCache->tileCache); + m_ui.tile->setBoundary(m_boundary, tileCache, tileCache); + uint32_t location = mMapCacheTileId(mapCache, x, y); + mMapCacheEntry* entry = &m_mapStatus[location]; + m_ui.tile->selectIndex(entry->tileId + mapCache->tileStart); + m_ui.tile->setPalette(mMapCacheEntryFlagsGetPaletteId(entry->flags)); + m_ui.tile->setFlip(mMapCacheEntryFlagsGetHMirror(entry->flags), mMapCacheEntryFlagsGetVMirror(entry->flags)); + location <<= (mMapCacheSystemInfoGetMapAlign(mapCache->sysConfig)); + location += m_addressBase + mapCache->mapStart; + + QString flip(tr("None")); + if (mMapCacheEntryFlagsGetHMirror(entry->flags) && mMapCacheEntryFlagsGetVMirror(entry->flags)) { + flip = tr("Both"); + } else if (mMapCacheEntryFlagsGetHMirror(entry->flags)) { + flip = tr("Horizontal"); + } else if (mMapCacheEntryFlagsGetVMirror(entry->flags)) { + flip = tr("Vertical"); + } + m_ui.tile->setCustomProperty("flip", flip); + m_ui.tile->setCustomProperty("mapAddr", QString("%0%1") + .arg(m_addressWidth == 8 ? "0x" : "") + .arg(location, m_addressWidth, 16, QChar('0'))); +} + +bool MapView::eventFilter(QObject* obj, QEvent* event) { + if (event->type() != QEvent::MouseButtonPress) { + return false; + } + int x = static_cast(event)->x(); + int y = static_cast(event)->y(); + x /= 8 * m_ui.magnification->value(); + y /= 8 * m_ui.magnification->value(); + selectTile(x, y); + return true; +} + void MapView::updateTilesGBA(bool force) { { CoreController::Interrupter interrupter(m_controller); diff --git a/src/platform/qt/MapView.h b/src/platform/qt/MapView.h index 7466cb038..c8aa0867a 100644 --- a/src/platform/qt/MapView.h +++ b/src/platform/qt/MapView.h @@ -28,6 +28,10 @@ public slots: private slots: void selectMap(int); + void selectTile(int x, int y); + +protected: + bool eventFilter(QObject*, QEvent*) override; private: #ifdef M_CORE_GBA @@ -43,6 +47,9 @@ private: mMapCacheEntry m_mapStatus[128 * 128] = {}; // TODO: Correct size int m_map = 0; QImage m_rawMap; + int m_boundary; + int m_addressBase; + int m_addressWidth; }; } diff --git a/src/platform/qt/MapView.ui b/src/platform/qt/MapView.ui index 35c204917..15a1f5f74 100644 --- a/src/platform/qt/MapView.ui +++ b/src/platform/qt/MapView.ui @@ -6,8 +6,8 @@ 0 0 - 498 - 335 + 641 + 489 @@ -30,8 +30,8 @@ 0 0 - 314 - 309 + 457 + 463