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 "CoreController.h"
9#include "GBAApp.h"
10
11#include <QFontDatabase>
12#include <QTimer>
13
14#include "LogController.h"
15#include "VFileDevice.h"
16
17#ifdef M_CORE_GBA
18#include <mgba/internal/gba/gba.h>
19#endif
20#ifdef M_CORE_GB
21#include <mgba/internal/gb/gb.h>
22#include <mgba/internal/gb/io.h>
23#endif
24#include <mgba-util/png-io.h>
25#include <mgba-util/vfs.h>
26
27using namespace QGBA;
28
29ObjView::ObjView(std::shared_ptr<CoreController> controller, QWidget* parent)
30 : AssetView(controller, parent)
31 , m_controller(controller)
32{
33 m_ui.setupUi(this);
34 m_ui.tile->setController(controller);
35
36 const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
37
38 m_ui.x->setFont(font);
39 m_ui.y->setFont(font);
40 m_ui.w->setFont(font);
41 m_ui.h->setFont(font);
42 m_ui.address->setFont(font);
43 m_ui.priority->setFont(font);
44 m_ui.palette->setFont(font);
45 m_ui.transform->setFont(font);
46 m_ui.mode->setFont(font);
47
48 connect(m_ui.tiles, &TilePainter::indexPressed, this, &ObjView::translateIndex);
49 connect(m_ui.objId, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &ObjView::selectObj);
50 connect(m_ui.magnification, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this]() {
51 updateTiles(true);
52 });
53#ifdef USE_PNG
54 connect(m_ui.exportButton, &QAbstractButton::clicked, this, &ObjView::exportObj);
55#else
56 m_ui.exportButton->setVisible(false);
57#endif
58}
59
60void ObjView::selectObj(int obj) {
61 m_objId = obj;
62 updateTiles(true);
63}
64
65void ObjView::translateIndex(int index) {
66 unsigned x = index % m_objInfo.width;
67 unsigned y = index / m_objInfo.width;
68 m_ui.tile->selectIndex(x + y * m_objInfo.stride + m_tileOffset + m_boundary);
69}
70
71#ifdef M_CORE_GBA
72void ObjView::updateTilesGBA(bool force) {
73 m_ui.objId->setMaximum(127);
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 = 3;
92 m_ui.tile->setBoundary(1024, 1, 3);
93 m_ui.tile->setPalette(0);
94 m_boundary = 1024;
95 palette = 0;
96 tile /= 2;
97 bits = 8;
98 } else {
99 m_ui.palette->setText(QString::number(palette));
100 paletteSet = 2;
101 m_ui.tile->setBoundary(2048, 0, 2);
102 m_ui.tile->setPalette(palette);
103 m_boundary = 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 mTileCache* tileCache = mTileCacheSetGetPointer(&m_cacheSet->tiles, 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 color_t* data = mTileCacheGetTileIfDirty(tileCache, &m_tileStatus[16 * tileBase], tile, palette);
130 if (data) {
131 m_ui.tiles->setTile(i, data);
132 } else if (force) {
133 m_ui.tiles->setTile(i, mTileCacheGetTile(tileCache, 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 m_ui.objId->setMaximum(39);
179 const GB* gb = static_cast<const GB*>(m_controller->thread()->core->board);
180 const GBObj* obj = &gb->video.oam.obj[m_objId];
181
182 mTileCache* tileCache = mTileCacheSetGetPointer(&m_cacheSet->tiles, 0);
183 unsigned width = 8;
184 unsigned height = 8;
185 GBRegisterLCDC lcdc = gb->memory.io[REG_LCDC];
186 if (GBRegisterLCDCIsObjSize(lcdc)) {
187 height = 16;
188 }
189 unsigned tile = obj->tile;
190 m_ui.tiles->setTileCount(width * height / 64);
191 m_ui.tile->setBoundary(1024, 0, 0);
192 m_ui.tiles->setMinimumSize(QSize(width, height) * m_ui.magnification->value());
193 m_ui.tiles->resize(QSize(width, height) * m_ui.magnification->value());
194 unsigned palette = 0;
195 if (gb->model >= GB_MODEL_CGB) {
196 if (GBObjAttributesIsBank(obj->attr)) {
197 tile += 512;
198 }
199 palette = GBObjAttributesGetCGBPalette(obj->attr);
200 } else {
201 palette = GBObjAttributesGetPalette(obj->attr);
202 }
203 m_ui.palette->setText(QString::number(palette));
204 palette += 8;
205
206 ObjInfo newInfo{
207 tile,
208 1,
209 height / 8,
210 1,
211 palette,
212 0,
213 2
214 };
215 if (newInfo != m_objInfo) {
216 force = true;
217 }
218 m_objInfo = newInfo;
219 m_tileOffset = tile;
220 m_boundary = 1024;
221
222 int i = 0;
223 m_ui.tile->setPalette(palette);
224 for (int y = 0; y < height / 8; ++y, ++i) {
225 unsigned t = tile + i;
226 const color_t* data = mTileCacheGetTileIfDirty(tileCache, &m_tileStatus[8 * t], t, palette);
227 if (data) {
228 m_ui.tiles->setTile(i, data);
229 } else if (force) {
230 m_ui.tiles->setTile(i, mTileCacheGetTile(tileCache, t, palette));
231 }
232 }
233
234 m_ui.x->setText(QString::number(obj->x));
235 m_ui.y->setText(QString::number(obj->y));
236 m_ui.w->setText(QString::number(width));
237 m_ui.h->setText(QString::number(height));
238
239 m_ui.address->setText(tr("0x%0").arg(GB_BASE_OAM + m_objId * sizeof(*obj), 4, 16, QChar('0')));
240 m_ui.priority->setText(QString::number(GBObjAttributesGetPriority(obj->attr)));
241 m_ui.flippedH->setChecked(GBObjAttributesIsXFlip(obj->attr));
242 m_ui.flippedV->setChecked(GBObjAttributesIsYFlip(obj->attr));
243 m_ui.enabled->setChecked(obj->y != 0 && obj->y < 160);
244 m_ui.doubleSize->setChecked(false);
245 m_ui.mosaic->setChecked(false);
246 m_ui.transform->setText(tr("N/A"));
247 m_ui.mode->setText(tr("N/A"));
248}
249#endif
250
251#ifdef USE_PNG
252void ObjView::exportObj() {
253 QString filename = GBAApp::app()->getSaveFileName(this, tr("Export sprite"),
254 tr("Portable Network Graphics (*.png)"));
255 VFile* vf = VFileDevice::open(filename, O_WRONLY | O_CREAT | O_TRUNC);
256 if (!vf) {
257 LOG(QT, ERROR) << tr("Failed to open output PNG file: %1").arg(filename);
258 return;
259 }
260
261 CoreController::Interrupter interrupter(m_controller);
262 png_structp png = PNGWriteOpen(vf);
263 png_infop info = PNGWriteHeader8(png, m_objInfo.width * 8, m_objInfo.height * 8);
264
265 mTileCache* tileCache = mTileCacheSetGetPointer(&m_cacheSet->tiles, m_objInfo.paletteSet);
266 const color_t* rawPalette = mTileCacheGetPalette(tileCache, m_objInfo.paletteId);
267 unsigned colors = 1 << m_objInfo.bits;
268 uint32_t palette[256];
269
270 palette[0] = rawPalette[0];
271 for (unsigned c = 1; c < colors && c < 256; ++c) {
272 palette[c] = rawPalette[c] | 0xFF000000;
273 }
274 PNGWritePalette(png, info, palette, colors);
275
276 uint8_t* buffer = new uint8_t[m_objInfo.width * m_objInfo.height * 8 * 8];
277 unsigned t = m_objInfo.tile;
278 for (int y = 0; y < m_objInfo.height; ++y) {
279 for (int x = 0; x < m_objInfo.width; ++x, ++t) {
280 compositeTile(static_cast<const void*>(mTileCacheGetVRAM(tileCache, t)), reinterpret_cast<color_t*>(buffer), m_objInfo.width * 8, x * 8, y * 8, m_objInfo.bits);
281 }
282 t += m_objInfo.stride - m_objInfo.width;
283 }
284 PNGWritePixels8(png, m_objInfo.width * 8, m_objInfo.height * 8, m_objInfo.width * 8, static_cast<void*>(buffer));
285 PNGWriteClose(png, info);
286 delete[] buffer;
287 vf->close(vf);
288}
289#endif
290
291bool ObjView::ObjInfo::operator!=(const ObjInfo& other) {
292 return other.tile != tile ||
293 other.width != width ||
294 other.height != height ||
295 other.stride != stride ||
296 other.paletteId != paletteId ||
297 other.paletteSet != paletteSet;
298}