src/platform/qt/ObjView.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 "ObjView.h"
7
8#include "GBAApp.h"
9
10#include <QFontDatabase>
11#include <QTimer>
12
13#include "LogController.h"
14#include "VFileDevice.h"
15
16#ifdef M_CORE_GBA
17#include <mgba/internal/gba/gba.h>
18#endif
19#ifdef M_CORE_GB
20#include <mgba/internal/gb/gb.h>
21#include <mgba/internal/gb/io.h>
22#endif
23#include <mgba-util/png-io.h>
24
25using namespace QGBA;
26
27ObjView::ObjView(GameController* controller, QWidget* parent)
28 : AssetView(controller, parent)
29 , m_controller(controller)
30 , m_tileStatus{}
31 , m_objId(0)
32 , m_objInfo{}
33{
34 m_ui.setupUi(this);
35 m_ui.tile->setController(controller);
36
37 const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
38
39 m_ui.x->setFont(font);
40 m_ui.y->setFont(font);
41 m_ui.w->setFont(font);
42 m_ui.h->setFont(font);
43 m_ui.address->setFont(font);
44 m_ui.priority->setFont(font);
45 m_ui.palette->setFont(font);
46 m_ui.transform->setFont(font);
47 m_ui.mode->setFont(font);
48
49 connect(m_ui.tiles, SIGNAL(indexPressed(int)), this, SLOT(translateIndex(int)));
50 connect(m_ui.objId, SIGNAL(valueChanged(int)), this, SLOT(selectObj(int)));
51 connect(m_ui.magnification, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this]() {
52 updateTiles(true);
53 });
54#ifdef USE_PNG
55 connect(m_ui.exportButton, SIGNAL(clicked()), this, SLOT(exportObj()));
56#else
57 m_ui.exportButton->setVisible(false);
58#endif
59}
60
61void ObjView::selectObj(int obj) {
62 m_objId = obj;
63 updateTiles(true);
64}
65
66void ObjView::translateIndex(int index) {
67 unsigned x = index % m_objInfo.width;
68 unsigned y = index / m_objInfo.width;
69 m_ui.tile->selectIndex(x + y * m_objInfo.stride + m_tileOffset);
70}
71
72#ifdef M_CORE_GBA
73void ObjView::updateTilesGBA(bool force) {
74 const GBA* gba = static_cast<const GBA*>(m_controller->thread()->core->board);
75 const GBAObj* obj = &gba->video.oam.obj[m_objId];
76
77 unsigned shape = GBAObjAttributesAGetShape(obj->a);
78 unsigned size = GBAObjAttributesBGetSize(obj->b);
79 unsigned width = GBAVideoObjSizes[shape * 4 + size][0];
80 unsigned height = GBAVideoObjSizes[shape * 4 + size][1];
81 unsigned tile = GBAObjAttributesCGetTile(obj->c);
82 m_ui.tiles->setTileCount(width * height / 64);
83 m_ui.tiles->setMinimumSize(QSize(width, height) * m_ui.magnification->value());
84 m_ui.tiles->resize(QSize(width, height) * m_ui.magnification->value());
85 unsigned palette = GBAObjAttributesCGetPalette(obj->c);
86 unsigned tileBase = tile;
87 unsigned paletteSet;
88 unsigned bits;
89 if (GBAObjAttributesAIs256Color(obj->a)) {
90 m_ui.palette->setText("256-color");
91 paletteSet = 1;
92 m_ui.tile->setPalette(0);
93 m_ui.tile->setPaletteSet(1, 1024, 1536);
94 palette = 1;
95 tile = tile / 2 + 1024;
96 bits = 8;
97 } else {
98 m_ui.palette->setText(QString::number(palette));
99 paletteSet = 0;
100 m_ui.tile->setPalette(palette);
101 m_ui.tile->setPaletteSet(0, 2048, 3072);
102 palette += 16;
103 tile += 2048;
104 bits = 4;
105 }
106 ObjInfo newInfo{
107 tile,
108 width / 8,
109 height / 8,
110 width / 8,
111 palette,
112 paletteSet,
113 bits
114 };
115 if (newInfo != m_objInfo) {
116 force = true;
117 }
118 GBARegisterDISPCNT dispcnt = gba->memory.io[0]; // FIXME: Register name can't be imported due to namespacing issues
119 if (!GBARegisterDISPCNTIsObjCharacterMapping(dispcnt)) {
120 newInfo.stride = 0x20 >> (GBAObjAttributesAGet256Color(obj->a));
121 };
122 m_objInfo = newInfo;
123 m_tileOffset = tile;
124 mTileCacheSetPalette(m_tileCache.get(), paletteSet);
125
126 int i = 0;
127 for (int y = 0; y < height / 8; ++y) {
128 for (int x = 0; x < width / 8; ++x, ++i, ++tile, ++tileBase) {
129 const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache.get(), &m_tileStatus[32 * tileBase], tile, palette);
130 if (data) {
131 m_ui.tiles->setTile(i, data);
132 } else if (force) {
133 m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache.get(), tile, palette));
134 }
135 }
136 tile += newInfo.stride - width / 8;
137 tileBase += newInfo.stride - width / 8;
138 }
139
140 m_ui.x->setText(QString::number(GBAObjAttributesBGetX(obj->b)));
141 m_ui.y->setText(QString::number(GBAObjAttributesAGetY(obj->a)));
142 m_ui.w->setText(QString::number(width));
143 m_ui.h->setText(QString::number(height));
144
145 m_ui.address->setText(tr("0x%0").arg(BASE_OAM + m_objId * sizeof(*obj), 8, 16, QChar('0')));
146 m_ui.priority->setText(QString::number(GBAObjAttributesCGetPriority(obj->c)));
147 m_ui.flippedH->setChecked(GBAObjAttributesBIsHFlip(obj->b));
148 m_ui.flippedV->setChecked(GBAObjAttributesBIsVFlip(obj->b));
149 m_ui.enabled->setChecked(!GBAObjAttributesAIsDisable(obj->a) || GBAObjAttributesAIsTransformed(obj->a));
150 m_ui.doubleSize->setChecked(GBAObjAttributesAIsDoubleSize(obj->a) && GBAObjAttributesAIsTransformed(obj->a));
151 m_ui.mosaic->setChecked(GBAObjAttributesAIsMosaic(obj->a));
152
153 if (GBAObjAttributesAIsTransformed(obj->a)) {
154 m_ui.transform->setText(QString::number(GBAObjAttributesBGetMatIndex(obj->b)));
155 } else {
156 m_ui.transform->setText(tr("Off"));
157 }
158
159 switch (GBAObjAttributesAGetMode(obj->a)) {
160 case OBJ_MODE_NORMAL:
161 m_ui.mode->setText(tr("Normal"));
162 break;
163 case OBJ_MODE_SEMITRANSPARENT:
164 m_ui.mode->setText(tr("Trans"));
165 break;
166 case OBJ_MODE_OBJWIN:
167 m_ui.mode->setText(tr("OBJWIN"));
168 break;
169 default:
170 m_ui.mode->setText(tr("Invalid"));
171 break;
172 }
173}
174#endif
175
176#ifdef M_CORE_GB
177void ObjView::updateTilesGB(bool force) {
178 const GB* gb = static_cast<const GB*>(m_controller->thread()->core->board);
179 const GBObj* obj = &gb->video.oam.obj[m_objId];
180
181 unsigned width = 8;
182 unsigned height = 8;
183 GBRegisterLCDC lcdc = gb->memory.io[REG_LCDC];
184 if (GBRegisterLCDCIsObjSize(lcdc)) {
185 height = 16;
186 }
187 unsigned tile = obj->tile;
188 m_ui.tiles->setTileCount(width * height / 64);
189 m_ui.tiles->setMinimumSize(QSize(width, height) * m_ui.magnification->value());
190 m_ui.tiles->resize(QSize(width, height) * m_ui.magnification->value());
191 unsigned palette = 0;
192 if (gb->model >= GB_MODEL_CGB) {
193 if (GBObjAttributesIsBank(obj->attr)) {
194 tile += 512;
195 }
196 palette = GBObjAttributesGetCGBPalette(obj->attr);
197 } else {
198 palette = GBObjAttributesGetPalette(obj->attr);
199 }
200 m_ui.palette->setText(QString::number(palette));
201 palette += 8;
202
203 ObjInfo newInfo{
204 tile,
205 1,
206 height / 8,
207 1,
208 palette,
209 0,
210 2
211 };
212 if (newInfo != m_objInfo) {
213 force = true;
214 }
215 m_objInfo = newInfo;
216 m_tileOffset = tile;
217
218 int i = 0;
219 mTileCacheSetPalette(m_tileCache.get(), 0);
220 m_ui.tile->setPalette(palette);
221 m_ui.tile->setPaletteSet(0, 512, 1024);
222 for (int y = 0; y < height / 8; ++y, ++i) {
223 unsigned t = tile + i;
224 const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache.get(), &m_tileStatus[16 * t], t, palette);
225 if (data) {
226 m_ui.tiles->setTile(i, data);
227 } else if (force) {
228 m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache.get(), t, palette));
229 }
230 }
231
232 m_ui.x->setText(QString::number(obj->x));
233 m_ui.y->setText(QString::number(obj->y));
234 m_ui.w->setText(QString::number(width));
235 m_ui.h->setText(QString::number(height));
236
237 m_ui.address->setText(tr("0x%0").arg(GB_BASE_OAM + m_objId * sizeof(*obj), 4, 16, QChar('0')));
238 m_ui.priority->setText(QString::number(GBObjAttributesGetPriority(obj->attr)));
239 m_ui.flippedH->setChecked(GBObjAttributesIsXFlip(obj->attr));
240 m_ui.flippedV->setChecked(GBObjAttributesIsYFlip(obj->attr));
241 m_ui.enabled->setChecked(obj->y != 0 && obj->y < 160);
242 m_ui.doubleSize->setChecked(false);
243 m_ui.mosaic->setChecked(false);
244 m_ui.transform->setText(tr("N/A"));
245 m_ui.mode->setText(tr("N/A"));
246}
247#endif
248
249#ifdef USE_PNG
250void ObjView::exportObj() {
251 GameController::Interrupter interrupter(m_controller);
252 QString filename = GBAApp::app()->getSaveFileName(this, tr("Export sprite"),
253 tr("Portable Network Graphics (*.png)"));
254 VFile* vf = VFileDevice::open(filename, O_WRONLY | O_CREAT | O_TRUNC);
255 if (!vf) {
256 LOG(QT, ERROR) << tr("Failed to open output PNG file: %1").arg(filename);
257 return;
258 }
259
260 mTileCacheSetPalette(m_tileCache.get(), m_objInfo.paletteSet);
261 png_structp png = PNGWriteOpen(vf);
262 png_infop info = PNGWriteHeader8(png, m_objInfo.width * 8, m_objInfo.height * 8);
263
264 const uint16_t* rawPalette = mTileCacheGetPalette(m_tileCache.get(), m_objInfo.paletteId);
265 unsigned colors = 1 << m_objInfo.bits;
266 uint32_t palette[256];
267 for (unsigned c = 0; c < colors && c < 256; ++c) {
268 uint16_t color = rawPalette[c];
269 palette[c] = M_R8(rawPalette[c]);
270 palette[c] |= M_G8(rawPalette[c]) << 8;
271 palette[c] |= M_B8(rawPalette[c]) << 16;
272 if (c) {
273 palette[c] |= 0xFF000000;
274 }
275 }
276 PNGWritePalette(png, info, palette, colors);
277
278 uint8_t* buffer = new uint8_t[m_objInfo.width * m_objInfo.height * 8 * 8];
279 unsigned t = m_objInfo.tile;
280 for (int y = 0; y < m_objInfo.height; ++y) {
281 for (int x = 0; x < m_objInfo.width; ++x, ++t) {
282 compositeTile(t, static_cast<void*>(buffer), m_objInfo.width * 8, x * 8, y * 8, m_objInfo.bits);
283 }
284 t += m_objInfo.stride - m_objInfo.width;
285 }
286 PNGWritePixels8(png, m_objInfo.width * 8, m_objInfo.height * 8, m_objInfo.width * 8, static_cast<void*>(buffer));
287 PNGWriteClose(png, info);
288 delete[] buffer;
289}
290#endif
291
292bool ObjView::ObjInfo::operator!=(const ObjInfo& other) {
293 return other.tile != tile ||
294 other.width != width ||
295 other.height != height ||
296 other.stride != stride ||
297 other.paletteId != paletteId ||
298 other.paletteSet != paletteSet;
299}