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 <QHBoxLayout>
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 : AssetInfo(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 = GBAApp::monospaceFont();
35
36 m_ui.tileId->setFont(font);
37 m_ui.paletteId->setFont(font);
38 m_ui.address->setFont(font);
39 m_ui.r->setFont(font);
40 m_ui.g->setFont(font);
41 m_ui.b->setFont(font);
42}
43
44int AssetTile::customLocation(const QString&) {
45 return layout()->indexOf(m_ui.line);
46}
47
48void AssetTile::setController(std::shared_ptr<CoreController> controller) {
49 m_cacheSet = controller->graphicCaches();
50 switch (controller->platform()) {
51#ifdef M_CORE_GBA
52 case mPLATFORM_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 mPLATFORM_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::setBoundary(int boundary, int set0, int set1) {
79 m_boundary = boundary;
80 m_tileCaches[0] = mTileCacheSetGetPointer(&m_cacheSet->tiles, set0);
81 m_tileCaches[1] = mTileCacheSetGetPointer(&m_cacheSet->tiles, set1);
82}
83
84void AssetTile::selectIndex(int index) {
85 m_index = index;
86 const color_t* data;
87 mTileCache* tileCache = m_tileCaches[index >= m_boundary];
88
89 unsigned bpp = 8 << tileCache->bpp;
90 int paletteId = m_paletteId;
91 int base = m_addressBase;
92 if (index >= m_boundary) {
93 base = m_boundaryBase;
94 index -= m_boundary;
95 }
96 int dispIndex = index;
97 if (m_addressWidth == 4 && index >= m_boundary / 2) {
98 dispIndex -= m_boundary / 2;
99 }
100 data = mTileCacheGetTile(tileCache, index, paletteId);
101 m_ui.tileId->setText(QString::number(dispIndex));
102 m_ui.paletteId->setText(QString::number(paletteId));
103 m_ui.address->setText(tr("%0%1%2")
104 .arg(m_addressWidth == 4 ? index >= m_boundary / 2 : 0)
105 .arg(m_addressWidth == 4 ? ":" : "x")
106 .arg(dispIndex * bpp | base, m_addressWidth, 16, QChar('0')));
107 int flip = 0;
108 if (m_flipH) {
109 flip |= 007;
110 }
111 if (m_flipV) {
112 flip |= 070;
113 }
114 for (int i = 0; i < 64; ++i) {
115 m_ui.preview->setColor(i ^ flip, data[i]);
116 }
117 m_ui.preview->update();
118
119 QImage tile(reinterpret_cast<const uchar*>(data), 8, 8, QImage::Format_ARGB32);
120 m_activeTile = tile.rgbSwapped();
121}
122
123void AssetTile::setFlip(bool h, bool v) {
124 m_flipH = h;
125 m_flipV = v;
126 selectIndex(m_index);
127}
128
129void AssetTile::selectColor(int index) {
130 const color_t* data;
131 mTileCache* tileCache = m_tileCaches[m_index >= m_boundary];
132 data = mTileCacheGetTile(tileCache, m_index >= m_boundary ? m_index - m_boundary : m_index, m_paletteId);
133 color_t color = data[index];
134 m_ui.color->setColor(0, color);
135 m_ui.color->update();
136
137 uint32_t r = ((color & 0xF8) * 0x21) >> 5;
138 uint32_t g = (((color >> 8) & 0xF8) * 0x21) >> 5;
139 uint32_t b = (((color >> 16) & 0xF8) * 0x21) >> 5;
140 m_ui.r->setText(tr("0x%0 (%1)").arg(r, 2, 16, QChar('0')).arg(r, 2, 10, QChar('0')));
141 m_ui.g->setText(tr("0x%0 (%1)").arg(g, 2, 16, QChar('0')).arg(g, 2, 10, QChar('0')));
142 m_ui.b->setText(tr("0x%0 (%1)").arg(b, 2, 16, QChar('0')).arg(b, 2, 10, QChar('0')));
143}