Qt: Refactor tile viewer into its own class

This commit is contained in:
Jeffrey Pfau 2016-10-20 17:36:09 -07:00
parent 8d89fb78ba
commit 524e94edbf
7 changed files with 320 additions and 141 deletions

View File

@ -0,0 +1,94 @@
/* Copyright (c) 2013-2016 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "AssetTile.h"
#include "GBAApp.h"
#include <QFontDatabase>
extern "C" {
#ifdef M_CORE_GBA
#include "gba/memory.h"
#endif
#ifdef M_CORE_GB
#include "gb/memory.h"
#endif
}
using namespace QGBA;
AssetTile::AssetTile(QWidget* parent)
: QGroupBox(parent)
, m_tileCache(nullptr)
, m_paletteId(0)
, m_paletteSet(0)
, m_index(0)
{
m_ui.setupUi(this);
m_ui.preview->setDimensions(QSize(8, 8));
const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
m_ui.tileId->setFont(font);
m_ui.address->setFont(font);
}
void AssetTile::setController(GameController* controller) {
m_tileCache = controller->tileCache();
switch (controller->platform()) {
#ifdef M_CORE_GBA
case PLATFORM_GBA:
m_addressWidth = 8;
m_addressBase = BASE_VRAM;
break;
#endif
#ifdef M_CORE_GB
case PLATFORM_GB:
m_addressWidth = 4;
m_addressBase = GB_BASE_VRAM;
break;
#endif
default:
m_addressWidth = 0;
m_addressBase = 0;
break;
}
}
void AssetTile::setPalette(int palette) {
m_paletteId = palette;
selectIndex(m_index);
}
void AssetTile::setPaletteSet(int palette, int boundary, int max) {
if (m_index >= max) {
m_index = max - 1;
}
m_boundary = boundary;
m_paletteSet = palette;
selectIndex(m_index);
}
void AssetTile::selectIndex(int index) {
m_index = index;
const uint16_t* data;
m_ui.tileId->setText(QString::number(index));
mTileCacheSetPalette(m_tileCache.get(), m_paletteSet);
unsigned bpp = 8 << m_tileCache->bpp;
m_ui.address->setText(tr("0x%0").arg(index * bpp | m_addressBase, m_addressWidth, 16, QChar('0')));
if (index < m_boundary) {
data = mTileCacheGetTile(m_tileCache.get(), index, m_paletteId);
} else {
data = mTileCacheGetTile(m_tileCache.get(), index, m_paletteId + m_tileCache->count / 2);
}
for (int i = 0; i < 64; ++i) {
m_ui.preview->setColor(i, data[i]);
}
m_ui.preview->update();
}

View File

@ -0,0 +1,46 @@
/* Copyright (c) 2013-2016 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef QGBA_ASSET_TILE
#define QGBA_ASSET_TILE
#include "GameController.h"
#include "ui_AssetTile.h"
extern "C" {
#include "core/tile-cache.h"
}
namespace QGBA {
class AssetTile : public QGroupBox {
Q_OBJECT
public:
AssetTile(QWidget* parent = nullptr);
void setController(GameController*);
public slots:
void setPalette(int);
void setPaletteSet(int, int boundary, int max);
void selectIndex(int);
private:
Ui::AssetTile m_ui;
std::shared_ptr<mTileCache> m_tileCache;
int m_paletteId;
int m_paletteSet;
int m_index;
int m_addressWidth;
int m_addressBase;
int m_boundary;
};
}
#endif

View File

@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>AssetTile</class>
<widget class="QGroupBox" name="AssetTile">
<property name="geometry">
<rect>
<x>120</x>
<y>50</y>
<width>160</width>
<height>168</height>
</rect>
</property>
<property name="windowTitle">
<string>AssetTile</string>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>170</width>
<height>192</height>
</size>
</property>
<property name="title">
<string/>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QGBA::Swatch" name="preview" native="true">
<property name="minimumSize">
<size>
<width>87</width>
<height>87</height>
</size>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Tile #</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="tileId">
<property name="text">
<string>0</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Address</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="address">
<property name="text">
<string>0x06000000</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QGBA::Swatch</class>
<extends>QWidget</extends>
<header>Swatch.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@ -69,6 +69,7 @@ endif()
set(SOURCE_FILES
AboutScreen.cpp
ArchiveInspector.cpp
AssetTile.cpp
AudioProcessor.cpp
CheatsModel.cpp
CheatsView.cpp
@ -113,6 +114,7 @@ set(SOURCE_FILES
set(UI_FILES
AboutScreen.ui
ArchiveInspector.ui
AssetTile.ui
CheatsView.ui
GIFView.ui
IOViewer.ui

View File

@ -24,22 +24,59 @@ TileView::TileView(GameController* controller, QWidget* parent)
, m_paletteId(0)
{
m_ui.setupUi(this);
m_ui.tile->setController(controller);
m_ui.preview->setDimensions(QSize(8, 8));
m_updateTimer.setSingleShot(true);
m_updateTimer.setInterval(1);
connect(&m_updateTimer, SIGNAL(timeout()), this, SLOT(updateTiles()));
const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
m_ui.tileId->setFont(font);
m_ui.address->setFont(font);
connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), &m_updateTimer, SLOT(start()));
connect(m_controller, SIGNAL(gameStopped(mCoreThread*)), this, SLOT(close()));
connect(m_ui.tiles, SIGNAL(indexPressed(int)), this, SLOT(selectIndex(int)));
connect(m_ui.tiles, SIGNAL(indexPressed(int)), m_ui.tile, SLOT(selectIndex(int)));
connect(m_ui.paletteId, SIGNAL(valueChanged(int)), this, SLOT(updatePalette(int)));
connect(m_ui.palette256, &QAbstractButton::toggled, [this]() {
int max = 1024;
int boundary = 1024;
switch (m_controller->platform()) {
#ifdef M_CORE_GBA
case PLATFORM_GBA:
max = 3072;
boundary = 2048;
break;
#endif
#ifdef M_CORE_GB
case PLATFORM_GB:
max = 1024;
boundary = 1024;
m_ui.palette256->setEnabled(false);
break;
#endif
default:
return;
}
m_ui.tile->setPaletteSet(0, boundary, max);
connect(m_ui.palette256, &QAbstractButton::toggled, [this](bool selected) {
if (selected) {
m_ui.paletteId->setValue(0);
}
int max = 1024;
int boundary = 1024;
switch (m_controller->platform()) {
#ifdef M_CORE_GBA
case PLATFORM_GBA:
max = 3072 >> selected;
boundary = 2048 >> selected;
break;
#endif
#ifdef M_CORE_GB
case PLATFORM_GB:
return;
#endif
default:
break;
}
m_ui.tile->setPaletteSet(selected, boundary, max);
updateTiles(true);
});
connect(m_ui.magnification, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this]() {
@ -47,30 +84,6 @@ TileView::TileView(GameController* controller, QWidget* parent)
});
}
void TileView::selectIndex(int index) {
const uint16_t* data;
m_ui.tileId->setText(QString::number(index));
if (m_ui.palette256->isChecked()) {
m_ui.address->setText(tr("0x%0").arg(index * 64 | BASE_VRAM, 8, 16, QChar('0')));
if (index < 1024) {
data = mTileCacheGetTile(m_tileCache.get(), index, 0);
} else {
data = mTileCacheGetTile(m_tileCache.get(), index, 1);
}
} else {
m_ui.address->setText(tr("0x%0").arg(index * 32 | BASE_VRAM, 8, 16, QChar('0')));
if (index < 2048) {
data = mTileCacheGetTile(m_tileCache.get(), index, m_paletteId);
} else {
data = mTileCacheGetTile(m_tileCache.get(), index, m_paletteId + 16);
}
}
for (int i = 0; i < 64; ++i) {
m_ui.preview->setColor(i, data[i]);
}
m_ui.preview->update();
}
void TileView::updateTiles(bool force) {
if (!m_controller->thread() || !m_controller->thread()->core) {
return;
@ -153,6 +166,7 @@ void TileView::updateTilesGB(bool force) {
void TileView::updatePalette(int palette) {
m_paletteId = palette;
m_ui.tile->setPalette(palette);
updateTiles(true);
}

View File

@ -28,9 +28,6 @@ public slots:
void updateTiles(bool force = false);
void updatePalette(int);
private slots:
void selectIndex(int);
protected:
void resizeEvent(QResizeEvent*) override;
void showEvent(QShowEvent*) override;

View File

@ -21,79 +21,6 @@
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QGroupBox" name="groupBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>170</width>
<height>192</height>
</size>
</property>
<property name="title">
<string/>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QGBA::Swatch" name="preview" native="true">
<property name="minimumSize">
<size>
<width>87</width>
<height>87</height>
</size>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Tile #</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="tileId">
<property name="text">
<string>0</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Address</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="address">
<property name="text">
<string>0x06000000</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item row="0" column="0">
<widget class="QSlider" name="paletteId">
<property name="maximumSize">
@ -129,6 +56,36 @@
</property>
</spacer>
</item>
<item row="2" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QSpinBox" name="magnification">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="suffix">
<string>×</string>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>4</number>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Magnification</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="1" rowspan="5">
<widget class="QScrollArea" name="scrollArea">
<property name="sizePolicy">
@ -145,7 +102,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>290</width>
<width>286</width>
<height>768</height>
</rect>
</property>
@ -185,35 +142,8 @@
</widget>
</widget>
</item>
<item row="2" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QSpinBox" name="magnification">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="suffix">
<string>×</string>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>4</number>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Magnification</string>
</property>
</widget>
</item>
</layout>
<item row="3" column="0">
<widget class="QGBA::AssetTile" name="tile"/>
</item>
</layout>
</widget>
@ -228,9 +158,9 @@
</slots>
</customwidget>
<customwidget>
<class>QGBA::Swatch</class>
<extends>QWidget</extends>
<header>Swatch.h</header>
<class>QGBA::AssetTile</class>
<extends>QGroupBox</extends>
<header>AssetTile.h</header>
<container>1</container>
</customwidget>
</customwidgets>