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#include <QHBoxLayout>
13
14#include <mgba/core/interface.h>
15#ifdef M_CORE_GBA
16#include <mgba/internal/gba/memory.h>
17#endif
18#ifdef M_CORE_GB
19#include <mgba/internal/gb/memory.h>
20#endif
21
22using namespace QGBA;
23
24AssetTile::AssetTile(QWidget* parent)
25 : AssetInfo(parent)
26{
27 m_ui.setupUi(this);
28
29 m_ui.preview->setDimensions(QSize(8, 8));
30 m_ui.color->setDimensions(QSize(1, 1));
31 m_ui.color->setSize(50);
32
33 connect(m_ui.preview, &Swatch::indexPressed, this, &AssetTile::selectColor);
34
35 const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
36
37 m_ui.tileId->setFont(font);
38 m_ui.paletteId->setFont(font);
39 m_ui.address->setFont(font);
40 m_ui.r->setFont(font);
41 m_ui.g->setFont(font);
42 m_ui.b->setFont(font);
43}
44
45int AssetTile::customLocation(const QString&) {
46 return layout()->indexOf(m_ui.line);
47}
48
49void AssetTile::setController(std::shared_ptr<CoreController> controller) {
50 m_cacheSet = controller->graphicCaches();
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::setBoundary(int boundary, int set0, int set1) {
80 m_boundary = boundary;
81 m_tileCaches[0] = mTileCacheSetGetPointer(&m_cacheSet->tiles, set0);
82 m_tileCaches[1] = mTileCacheSetGetPointer(&m_cacheSet->tiles, set1);
83}
84
85void AssetTile::selectIndex(int index) {
86 m_index = index;
87 const color_t* data;
88 mTileCache* tileCache = m_tileCaches[index >= m_boundary];
89
90 unsigned bpp = 8 << tileCache->bpp;
91 int paletteId = m_paletteId;
92 int base = m_addressBase;
93 if (index >= m_boundary) {
94 base = m_boundaryBase;
95 index -= m_boundary;
96 }
97 int dispIndex = index;
98 if (m_addressWidth == 4 && index >= m_boundary / 2) {
99 dispIndex -= m_boundary / 2;
100 }
101 data = mTileCacheGetTile(tileCache, index, paletteId);
102 m_ui.tileId->setText(QString::number(dispIndex));
103 m_ui.paletteId->setText(QString::number(paletteId));
104 m_ui.address->setText(tr("%0%1%2")
105 .arg(m_addressWidth == 4 ? index >= m_boundary / 2 : 0)
106 .arg(m_addressWidth == 4 ? ":" : "x")
107 .arg(dispIndex * bpp | base, m_addressWidth, 16, QChar('0')));
108 int flip = 0;
109 if (m_flipH) {
110 flip |= 007;
111 }
112 if (m_flipV) {
113 flip |= 070;
114 }
115 for (int i = 0; i < 64; ++i) {
116 m_ui.preview->setColor(i ^ flip, data[i]);
117 }
118 m_ui.preview->update();
119
120 QImage tile(reinterpret_cast<const uchar*>(data), 8, 8, QImage::Format_ARGB32);
121 m_activeTile = tile.rgbSwapped();
122}
123
124void AssetTile::setFlip(bool h, bool v) {
125 m_flipH = h;
126 m_flipV = v;
127 selectIndex(m_index);
128}
129
130void AssetTile::selectColor(int index) {
131 const color_t* data;
132 mTileCache* tileCache = m_tileCaches[m_index >= m_boundary];
133 unsigned bpp = 8 << tileCache->bpp;
134 int paletteId = m_paletteId;
135 data = mTileCacheGetTile(tileCache, m_index >= m_boundary ? m_index - m_boundary : m_index, m_paletteId);
136 color_t color = data[index];
137 m_ui.color->setColor(0, color);
138 m_ui.color->update();
139
140 uint32_t r = ((color & 0xF8) * 0x21) >> 5;
141 uint32_t g = (((color >> 8) & 0xF8) * 0x21) >> 5;
142 uint32_t b = (((color >> 16) & 0xF8) * 0x21) >> 5;
143 m_ui.r->setText(tr("0x%0 (%1)").arg(r, 2, 16, QChar('0')).arg(r, 2, 10, QChar('0')));
144 m_ui.g->setText(tr("0x%0 (%1)").arg(g, 2, 16, QChar('0')).arg(g, 2, 10, QChar('0')));
145 m_ui.b->setText(tr("0x%0 (%1)").arg(b, 2, 16, QChar('0')).arg(b, 2, 10, QChar('0')));
146}