all repos — mgba @ 0f9c4bbe60d4c30e10ac0d15d5c653297c51e034

mGBA Game Boy Advance Emulator

src/platform/qt/TileView.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 "TileView.h"
  7
  8#include "CoreController.h"
  9#include "GBAApp.h"
 10
 11#include <QFontDatabase>
 12#include <QTimer>
 13
 14#ifdef M_CORE_GB
 15#include <mgba/internal/gb/gb.h>
 16#endif
 17
 18using namespace QGBA;
 19
 20TileView::TileView(std::shared_ptr<CoreController> controller, QWidget* parent)
 21	: AssetView(controller, parent)
 22	, m_controller(controller)
 23{
 24	m_ui.setupUi(this);
 25	m_ui.tile->setController(controller);
 26
 27	connect(m_ui.tiles, &TilePainter::indexPressed, m_ui.tile, &AssetTile::selectIndex);
 28	connect(m_ui.tiles, &TilePainter::needsRedraw, this, [this]() {
 29		updateTiles(true);
 30	});
 31	connect(m_ui.paletteId, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &TileView::updatePalette);
 32
 33	switch (m_controller->platform()) {
 34#ifdef M_CORE_GBA
 35	case PLATFORM_GBA:
 36		m_ui.tile->setBoundary(2048, 0, 2);
 37		break;
 38#endif
 39#ifdef M_CORE_GB
 40	case PLATFORM_GB:
 41		m_ui.palette256->setEnabled(false);
 42		m_ui.tile->setBoundary(1024, 0, 0);
 43		break;
 44#endif
 45	default:
 46		return;
 47	}
 48
 49	connect(m_ui.palette256, &QAbstractButton::toggled, [this](bool selected) {
 50		if (selected) {
 51			m_ui.paletteId->setValue(0);
 52		}
 53		switch (m_controller->platform()) {
 54#ifdef M_CORE_GBA
 55		case PLATFORM_GBA:
 56			m_ui.tile->setBoundary(2048 >> selected, selected, selected + 2);
 57			break;
 58#endif
 59#ifdef M_CORE_GB
 60		case PLATFORM_GB:
 61			return;
 62#endif
 63		default:
 64			break;
 65		}
 66		updateTiles(true);
 67	});
 68	connect(m_ui.magnification, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this]() {
 69		updateTiles(true);
 70	});
 71
 72	connect(m_ui.tilesPerRow, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this](int count) {
 73		m_ui.tiles->setMinimumSize(m_ui.magnification->value() * 8 * count, m_ui.tiles->minimumSize().height());
 74		updateTiles(true);
 75	});
 76
 77	connect(m_ui.tileFit, &QAbstractButton::toggled, [this](bool selected) {
 78		if (!selected) {
 79			m_ui.tiles->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
 80			m_ui.tiles->setMinimumSize(m_ui.magnification->value() * 8 * m_ui.tilesPerRow->value(), m_ui.tiles->minimumSize().height());
 81		} else {
 82			m_ui.tiles->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
 83		}
 84		updateTiles(true);
 85	});
 86}
 87
 88#ifdef M_CORE_GBA
 89void TileView::updateTilesGBA(bool force) {
 90	if (m_ui.palette256->isChecked()) {
 91		m_ui.tiles->setTileCount(1536);
 92		mTileCache* cache = mTileCacheSetGetPointer(&m_cacheSet->tiles, 1);
 93		for (int i = 0; i < 1024; ++i) {
 94			const color_t* data = mTileCacheGetTileIfDirty(cache, &m_tileStatus[16 * i], i, 0);
 95			if (data) {
 96				m_ui.tiles->setTile(i, data);
 97			} else if (force) {
 98				m_ui.tiles->setTile(i, mTileCacheGetTile(cache, i, 0));
 99			}
100		}
101		cache = mTileCacheSetGetPointer(&m_cacheSet->tiles, 3);
102		for (int i = 1024; i < 1536; ++i) {
103			const color_t* data = mTileCacheGetTileIfDirty(cache, &m_tileStatus[16 * i], i - 1024, 0);
104			if (data) {
105				m_ui.tiles->setTile(i, data);
106			} else if (force) {
107				m_ui.tiles->setTile(i, mTileCacheGetTile(cache, i - 1024, 0));
108			}
109		}
110	} else {
111		mTileCache* cache = mTileCacheSetGetPointer(&m_cacheSet->tiles, 0);
112		m_ui.tiles->setTileCount(3072);
113		for (int i = 0; i < 2048; ++i) {
114			const color_t* data = mTileCacheGetTileIfDirty(cache, &m_tileStatus[16 * i], i, m_paletteId);
115			if (data) {
116				m_ui.tiles->setTile(i, data);
117			} else if (force) {
118				m_ui.tiles->setTile(i, mTileCacheGetTile(cache, i, m_paletteId));
119			}
120		}
121		cache = mTileCacheSetGetPointer(&m_cacheSet->tiles, 2);
122		for (int i = 2048; i < 3072; ++i) {
123			const color_t* data = mTileCacheGetTileIfDirty(cache, &m_tileStatus[16 * i], i - 2048, m_paletteId);
124			if (data) {
125				m_ui.tiles->setTile(i, data);
126			} else if (force) {
127				m_ui.tiles->setTile(i, mTileCacheGetTile(cache, i - 2048, m_paletteId));
128			}
129		}
130	}
131}
132#endif
133
134#ifdef M_CORE_GB
135void TileView::updateTilesGB(bool force) {
136	const GB* gb = static_cast<const GB*>(m_controller->thread()->core->board);
137	int count = gb->model >= GB_MODEL_CGB ? 1024 : 512;
138	m_ui.tiles->setTileCount(count);
139	mTileCache* cache = mTileCacheSetGetPointer(&m_cacheSet->tiles, 0);
140	for (int i = 0; i < count; ++i) {
141		const color_t* data = mTileCacheGetTileIfDirty(cache, &m_tileStatus[8 * i], i, m_paletteId);
142		if (data) {
143			m_ui.tiles->setTile(i, data);
144		} else if (force) {
145			m_ui.tiles->setTile(i, mTileCacheGetTile(cache, i, m_paletteId));
146		}
147	}
148}
149#endif
150
151void TileView::updatePalette(int palette) {
152	m_paletteId = palette;
153	m_ui.tile->setPalette(palette);
154	updateTiles(true);
155}