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_tileCache = controller->tileCache();
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::setPaletteSet(int palette, int boundary, int max) {
74 m_index = m_index * (1 + m_paletteSet) / (1 + palette);
75 if (m_index >= max) {
76 m_index = max - 1;
77 }
78 m_boundary = boundary;
79 m_paletteSet = palette;
80 selectIndex(m_index);
81}
82
83void AssetTile::selectIndex(int index) {
84 m_index = index;
85 const uint16_t* data;
86
87 mTileCacheSetPalette(m_tileCache, m_paletteSet);
88 unsigned bpp = 8 << m_tileCache->bpp;
89 int dispIndex = index;
90 int paletteId = m_paletteId;
91 int base = m_addressBase;
92 if (index >= m_boundary) {
93 base = m_boundaryBase;
94 // XXX: Do this better
95#ifdef M_CORE_GBA
96 if (m_boundaryBase == (BASE_VRAM | 0x10000)) {
97 paletteId += m_tileCache->count / 2;
98 }
99#endif
100 dispIndex -= m_boundary;
101 }
102 data = mTileCacheGetTile(m_tileCache, index, paletteId);
103 m_ui.tileId->setText(QString::number(dispIndex * (1 + m_paletteSet)));
104 m_ui.address->setText(tr("%0%1%2")
105 .arg(m_addressWidth == 4 ? index >= m_boundary : 0)
106 .arg(m_addressWidth == 4 ? ":" : "x")
107 .arg(dispIndex * bpp | base, m_addressWidth, 16, QChar('0')));
108 for (int i = 0; i < 64; ++i) {
109 m_ui.preview->setColor(i, data[i]);
110 }
111 m_ui.preview->update();
112}
113
114void AssetTile::selectColor(int index) {
115 const uint16_t* data;
116 mTileCacheSetPalette(m_tileCache, m_paletteSet);
117 unsigned bpp = 8 << m_tileCache->bpp;
118 int paletteId = m_paletteId;
119 // XXX: Do this better
120#ifdef M_CORE_GBA
121 if (m_index >= m_boundary && m_boundaryBase == (BASE_VRAM | 0x10000)) {
122 paletteId += m_tileCache->count / 2;
123 }
124#endif
125 data = mTileCacheGetTile(m_tileCache, m_index, m_paletteId);
126 uint16_t color = data[index];
127 m_ui.color->setColor(0, color);
128 m_ui.color->update();
129
130 uint32_t r = M_R5(color);
131 uint32_t g = M_G5(color);
132 uint32_t b = M_B5(color);
133 m_ui.r->setText(tr("0x%0 (%1)").arg(r, 2, 16, QChar('0')).arg(r, 2, 10, QChar('0')));
134 m_ui.g->setText(tr("0x%0 (%1)").arg(g, 2, 16, QChar('0')).arg(g, 2, 10, QChar('0')));
135 m_ui.b->setText(tr("0x%0 (%1)").arg(b, 2, 16, QChar('0')).arg(b, 2, 10, QChar('0')));
136}
137