all repos — mgba @ 6e9507f0822f76bba63f29fba6a78d1f14f34b9f

mGBA Game Boy Advance Emulator

src/platform/qt/AssetTile.cpp (view raw)

  1/* Copyright (c) 2013-2016 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "AssetTile.h"
  7
  8#include "CoreController.h"
  9#include "GBAApp.h"
 10
 11#include <QFontDatabase>
 12
 13#include <mgba/core/interface.h>
 14#ifdef M_CORE_GBA
 15#include <mgba/internal/gba/memory.h>
 16#endif
 17#ifdef M_CORE_GB
 18#include <mgba/internal/gb/memory.h>
 19#endif
 20
 21using namespace QGBA;
 22
 23AssetTile::AssetTile(QWidget* parent)
 24	: QGroupBox(parent)
 25{
 26	m_ui.setupUi(this);
 27
 28	m_ui.preview->setDimensions(QSize(8, 8));
 29	m_ui.color->setDimensions(QSize(1, 1));
 30	m_ui.color->setSize(50);
 31
 32	connect(m_ui.preview, &Swatch::indexPressed, this, &AssetTile::selectColor);
 33
 34	const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
 35
 36	m_ui.tileId->setFont(font);
 37	m_ui.address->setFont(font);
 38	m_ui.r->setFont(font);
 39	m_ui.g->setFont(font);
 40	m_ui.b->setFont(font);
 41}
 42
 43void AssetTile::setController(std::shared_ptr<CoreController> controller) {
 44	m_cacheSet = controller->graphicCaches();
 45	switch (controller->platform()) {
 46#ifdef M_CORE_GBA
 47	case PLATFORM_GBA:
 48		m_addressWidth = 8;
 49		m_addressBase = BASE_VRAM;
 50		m_boundaryBase = BASE_VRAM | 0x10000;
 51		break;
 52#endif
 53#ifdef M_CORE_GB
 54	case PLATFORM_GB:
 55		m_addressWidth = 4;
 56		m_addressBase = GB_BASE_VRAM;
 57		m_boundaryBase = GB_BASE_VRAM;
 58		break;
 59#endif
 60	default:
 61		m_addressWidth = 0;
 62		m_addressBase = 0;
 63		m_boundaryBase = 0;
 64		break;
 65	}
 66}
 67
 68void AssetTile::setPalette(int palette) {
 69	m_paletteId = palette;
 70	selectIndex(m_index);
 71}
 72
 73void AssetTile::setBoundary(int boundary, int set0, int set1) {
 74	m_boundary = boundary;
 75	m_tileCaches[0] = mTileCacheSetGetPointer(&m_cacheSet->tiles, set0);
 76	m_tileCaches[1] = mTileCacheSetGetPointer(&m_cacheSet->tiles, set1);
 77}
 78
 79void AssetTile::selectIndex(int index) {
 80	m_index = index;
 81	const color_t* data;
 82	mTileCache* tileCache = m_tileCaches[index >= m_boundary];
 83
 84	unsigned bpp = 8 << tileCache->bpp;
 85	int paletteId = m_paletteId;
 86	int base = m_addressBase;
 87	if (index >= m_boundary) {
 88		base = m_boundaryBase;
 89		index -= m_boundary;
 90	}
 91	int dispIndex = index;
 92	if (m_addressWidth == 4 && index >= m_boundary / 2) {
 93		dispIndex -= m_boundary / 2;
 94	}
 95	data = mTileCacheGetTile(tileCache, index, paletteId);
 96	m_ui.tileId->setText(QString::number(dispIndex));
 97	m_ui.address->setText(tr("%0%1%2")
 98		.arg(m_addressWidth == 4 ? index >= m_boundary / 2 : 0)
 99		.arg(m_addressWidth == 4 ? ":" : "x")
100		.arg(dispIndex * bpp | base, m_addressWidth, 16, QChar('0')));
101	for (int i = 0; i < 64; ++i) {
102		m_ui.preview->setColor(i, data[i]);
103	}
104	m_ui.preview->update();
105}
106
107void AssetTile::selectColor(int index) {
108	const color_t* data;
109	mTileCache* tileCache = m_tileCaches[m_index >= m_boundary];
110	unsigned bpp = 8 << tileCache->bpp;
111	int paletteId = m_paletteId;
112	data = mTileCacheGetTile(tileCache, m_index, m_paletteId);
113	color_t color = data[index];
114	m_ui.color->setColor(0, color);
115	m_ui.color->update();
116
117	uint32_t r = color & 0xF8;
118	uint32_t g = (color >> 8) & 0xF8;
119	uint32_t b = (color >> 16) & 0xF8;
120	m_ui.r->setText(tr("0x%0 (%1)").arg(r, 2, 16, QChar('0')).arg(r, 2, 10, QChar('0')));
121	m_ui.g->setText(tr("0x%0 (%1)").arg(g, 2, 16, QChar('0')).arg(g, 2, 10, QChar('0')));
122	m_ui.b->setText(tr("0x%0 (%1)").arg(b, 2, 16, QChar('0')).arg(b, 2, 10, QChar('0')));
123}
124