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 "GBAApp.h"
9
10#include <QFontDatabase>
11
12extern "C" {
13#ifdef M_CORE_GBA
14#include "gba/memory.h"
15#endif
16#ifdef M_CORE_GB
17#include "gb/memory.h"
18#endif
19}
20
21using namespace QGBA;
22
23AssetTile::AssetTile(QWidget* parent)
24 : QGroupBox(parent)
25 , m_tileCache(nullptr)
26 , m_paletteId(0)
27 , m_paletteSet(0)
28 , m_index(0)
29{
30 m_ui.setupUi(this);
31
32 m_ui.preview->setDimensions(QSize(8, 8));
33
34 const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
35
36 m_ui.tileId->setFont(font);
37 m_ui.address->setFont(font);
38}
39
40void AssetTile::setController(GameController* controller) {
41 m_tileCache = controller->tileCache();
42 switch (controller->platform()) {
43#ifdef M_CORE_GBA
44 case PLATFORM_GBA:
45 m_addressWidth = 8;
46 m_addressBase = BASE_VRAM;
47 break;
48#endif
49#ifdef M_CORE_GB
50 case PLATFORM_GB:
51 m_addressWidth = 4;
52 m_addressBase = GB_BASE_VRAM;
53 break;
54#endif
55 default:
56 m_addressWidth = 0;
57 m_addressBase = 0;
58 break;
59 }
60}
61
62void AssetTile::setPalette(int palette) {
63 m_paletteId = palette;
64 selectIndex(m_index);
65}
66
67void AssetTile::setPaletteSet(int palette, int boundary, int max) {
68 m_index = m_index * (1 + m_paletteSet) / (1 + palette);
69 if (m_index >= max) {
70 m_index = max - 1;
71 }
72 m_boundary = boundary;
73 m_paletteSet = palette;
74 selectIndex(m_index);
75}
76
77void AssetTile::selectIndex(int index) {
78 m_index = index;
79 const uint16_t* data;
80 m_ui.tileId->setText(QString::number(index * (1 + m_paletteSet)));
81
82 mTileCacheSetPalette(m_tileCache.get(), m_paletteSet);
83 unsigned bpp = 8 << m_tileCache->bpp;
84 m_ui.address->setText(tr("0x%0").arg(index * bpp | m_addressBase, m_addressWidth, 16, QChar('0')));
85 if (index < m_boundary) {
86 data = mTileCacheGetTile(m_tileCache.get(), index, m_paletteId);
87 } else {
88 data = mTileCacheGetTile(m_tileCache.get(), index, m_paletteId + m_tileCache->count / 2);
89 }
90 for (int i = 0; i < 64; ++i) {
91 m_ui.preview->setColor(i, data[i]);
92 }
93 m_ui.preview->update();
94}
95