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 : QGroupBox(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
45void AssetTile::addCustomProperty(const QString& id, const QString& visibleName) {
46 QHBoxLayout* newLayout = new QHBoxLayout;
47 newLayout->addWidget(new QLabel(visibleName));
48 QLabel* value = new QLabel;
49 value->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
50 value->setAlignment(Qt::AlignRight);
51 newLayout->addWidget(value);
52 m_customProperties[id] = value;
53 int index = layout()->indexOf(m_ui.line);
54 static_cast<QBoxLayout*>(layout())->insertLayout(index, newLayout);
55}
56
57void AssetTile::setController(std::shared_ptr<CoreController> controller) {
58 m_cacheSet = controller->graphicCaches();
59 switch (controller->platform()) {
60#ifdef M_CORE_GBA
61 case PLATFORM_GBA:
62 m_addressWidth = 8;
63 m_addressBase = BASE_VRAM;
64 m_boundaryBase = BASE_VRAM | 0x10000;
65 break;
66#endif
67#ifdef M_CORE_GB
68 case PLATFORM_GB:
69 m_addressWidth = 4;
70 m_addressBase = GB_BASE_VRAM;
71 m_boundaryBase = GB_BASE_VRAM;
72 break;
73#endif
74 default:
75 m_addressWidth = 0;
76 m_addressBase = 0;
77 m_boundaryBase = 0;
78 break;
79 }
80}
81
82void AssetTile::setPalette(int palette) {
83 m_paletteId = palette;
84 selectIndex(m_index);
85}
86
87void AssetTile::setBoundary(int boundary, int set0, int set1) {
88 m_boundary = boundary;
89 m_tileCaches[0] = mTileCacheSetGetPointer(&m_cacheSet->tiles, set0);
90 m_tileCaches[1] = mTileCacheSetGetPointer(&m_cacheSet->tiles, set1);
91}
92
93void AssetTile::selectIndex(int index) {
94 m_index = index;
95 const color_t* data;
96 mTileCache* tileCache = m_tileCaches[index >= m_boundary];
97
98 unsigned bpp = 8 << tileCache->bpp;
99 int paletteId = m_paletteId;
100 int base = m_addressBase;
101 if (index >= m_boundary) {
102 base = m_boundaryBase;
103 index -= m_boundary;
104 }
105 int dispIndex = index;
106 if (m_addressWidth == 4 && index >= m_boundary / 2) {
107 dispIndex -= m_boundary / 2;
108 }
109 data = mTileCacheGetTile(tileCache, index, paletteId);
110 m_ui.tileId->setText(QString::number(dispIndex));
111 m_ui.paletteId->setText(QString::number(paletteId));
112 m_ui.address->setText(tr("%0%1%2")
113 .arg(m_addressWidth == 4 ? index >= m_boundary / 2 : 0)
114 .arg(m_addressWidth == 4 ? ":" : "x")
115 .arg(dispIndex * bpp | base, m_addressWidth, 16, QChar('0')));
116 int flip = 0;
117 if (m_flipH) {
118 flip |= 007;
119 }
120 if (m_flipV) {
121 flip |= 070;
122 }
123 for (int i = 0; i < 64; ++i) {
124 m_ui.preview->setColor(i ^ flip, data[i]);
125 }
126 m_ui.preview->update();
127}
128
129void AssetTile::setFlip(bool h, bool v) {
130 m_flipH = h;
131 m_flipV = v;
132 selectIndex(m_index);
133}
134
135void AssetTile::selectColor(int index) {
136 const color_t* data;
137 mTileCache* tileCache = m_tileCaches[m_index >= m_boundary];
138 unsigned bpp = 8 << tileCache->bpp;
139 int paletteId = m_paletteId;
140 data = mTileCacheGetTile(tileCache, m_index >= m_boundary ? m_index - m_boundary : m_index, m_paletteId);
141 color_t color = data[index];
142 m_ui.color->setColor(0, color);
143 m_ui.color->update();
144
145 uint32_t r = color & 0xF8;
146 uint32_t g = (color >> 8) & 0xF8;
147 uint32_t b = (color >> 16) & 0xF8;
148 m_ui.r->setText(tr("0x%0 (%1)").arg(r, 2, 16, QChar('0')).arg(r, 2, 10, QChar('0')));
149 m_ui.g->setText(tr("0x%0 (%1)").arg(g, 2, 16, QChar('0')).arg(g, 2, 10, QChar('0')));
150 m_ui.b->setText(tr("0x%0 (%1)").arg(b, 2, 16, QChar('0')).arg(b, 2, 10, QChar('0')));
151}
152
153void AssetTile::setCustomProperty(const QString& id, const QVariant& value) {
154 QLabel* label = m_customProperties[id];
155 if (!label) {
156 return;
157 }
158 label->setText(value.toString());
159}