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// TODO: Move color macros elsewhere
14#include "gba/video.h"
15#ifdef M_CORE_GBA
16#include "gba/memory.h"
17#endif
18#ifdef M_CORE_GB
19#include "gb/memory.h"
20#endif
21}
22
23using namespace QGBA;
24
25AssetTile::AssetTile(QWidget* parent)
26 : QGroupBox(parent)
27 , m_tileCache(nullptr)
28 , m_paletteId(0)
29 , m_paletteSet(0)
30 , m_index(0)
31{
32 m_ui.setupUi(this);
33
34 m_ui.preview->setDimensions(QSize(8, 8));
35 m_ui.color->setDimensions(QSize(1, 1));
36 m_ui.color->setSize(50);
37
38 connect(m_ui.preview, SIGNAL(indexPressed(int)), this, SLOT(selectColor(int)));
39
40 const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
41
42 m_ui.tileId->setFont(font);
43 m_ui.address->setFont(font);
44 m_ui.r->setFont(font);
45 m_ui.g->setFont(font);
46 m_ui.b->setFont(font);
47}
48
49void AssetTile::setController(GameController* controller) {
50 m_tileCache = controller->tileCache();
51 switch (controller->platform()) {
52#ifdef M_CORE_GBA
53 case PLATFORM_GBA:
54 m_addressWidth = 8;
55 m_addressBase = BASE_VRAM;
56 m_boundaryBase = BASE_VRAM | 0x10000;
57 break;
58#endif
59#ifdef M_CORE_GB
60 case PLATFORM_GB:
61 m_addressWidth = 4;
62 m_addressBase = GB_BASE_VRAM;
63 m_boundaryBase = GB_BASE_VRAM;
64 break;
65#endif
66 default:
67 m_addressWidth = 0;
68 m_addressBase = 0;
69 m_boundaryBase = 0;
70 break;
71 }
72}
73
74void AssetTile::setPalette(int palette) {
75 m_paletteId = palette;
76 selectIndex(m_index);
77}
78
79void AssetTile::setPaletteSet(int palette, int boundary, int max) {
80 m_index = m_index * (1 + m_paletteSet) / (1 + palette);
81 if (m_index >= max) {
82 m_index = max - 1;
83 }
84 m_boundary = boundary;
85 m_paletteSet = palette;
86 selectIndex(m_index);
87}
88
89void AssetTile::selectIndex(int index) {
90 m_index = index;
91 const uint16_t* data;
92
93 mTileCacheSetPalette(m_tileCache.get(), m_paletteSet);
94 unsigned bpp = 8 << m_tileCache->bpp;
95 int dispIndex = index;
96 int paletteId = m_paletteId;
97 int base = m_addressBase;
98 if (index >= m_boundary) {
99 base = m_boundaryBase;
100 // XXX: Do this better
101#ifdef M_CORE_GBA
102 if (m_boundaryBase == (BASE_VRAM | 0x10000)) {
103 paletteId += m_tileCache->count / 2;
104 }
105#endif
106 dispIndex -= m_boundary;
107 }
108 data = mTileCacheGetTile(m_tileCache.get(), index, paletteId);
109 m_ui.tileId->setText(QString::number(dispIndex * (1 + m_paletteSet)));
110 m_ui.address->setText(tr("%0%1%2")
111 .arg(m_addressWidth == 4 ? index >= m_boundary : 0)
112 .arg(m_addressWidth == 4 ? ":" : "x")
113 .arg(dispIndex * bpp | base, m_addressWidth, 16, QChar('0')));
114 for (int i = 0; i < 64; ++i) {
115 m_ui.preview->setColor(i, data[i]);
116 }
117 m_ui.preview->update();
118}
119
120void AssetTile::selectColor(int index) {
121 const uint16_t* data;
122 mTileCacheSetPalette(m_tileCache.get(), m_paletteSet);
123 unsigned bpp = 8 << m_tileCache->bpp;
124 int paletteId = m_paletteId;
125 // XXX: Do this better
126#ifdef M_CORE_GBA
127 if (m_index >= m_boundary && m_boundaryBase == (BASE_VRAM | 0x10000)) {
128 paletteId += m_tileCache->count / 2;
129 }
130#endif
131 data = mTileCacheGetTile(m_tileCache.get(), m_index, m_paletteId);
132 uint16_t color = data[index];
133 m_ui.color->setColor(0, color);
134 m_ui.color->update();
135
136 uint32_t r = GBA_R5(color);
137 uint32_t g = GBA_G5(color);
138 uint32_t b = GBA_B5(color);
139 m_ui.r->setText(tr("0x%0 (%1)").arg(r, 2, 16, QChar('0')).arg(r, 2, 10, QChar('0')));
140 m_ui.g->setText(tr("0x%0 (%1)").arg(g, 2, 16, QChar('0')).arg(g, 2, 10, QChar('0')));
141 m_ui.b->setText(tr("0x%0 (%1)").arg(b, 2, 16, QChar('0')).arg(b, 2, 10, QChar('0')));
142}
143