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