all repos — mgba @ 3c65ac986e8d2bbf64e1f55371c03f06e0342ce7

mGBA Game Boy Advance Emulator

src/platform/qt/PaletteView.cpp (view raw)

 1/* Copyright (c) 2013-2015 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 "PaletteView.h"
 7
 8#include <QFileDialog>
 9#include <QFontDatabase>
10
11extern "C" {
12#include "gba/supervisor/export.h"
13#include "util/vfs.h"
14}
15
16using namespace QGBA;
17
18PaletteView::PaletteView(GameController* controller, QWidget* parent)
19	: QWidget(parent)
20	, m_controller(controller)
21{
22	m_ui.setupUi(this);
23
24	connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(updatePalette()));
25	m_ui.bgGrid->setDimensions(QSize(16, 16));
26	m_ui.objGrid->setDimensions(QSize(16, 16));
27	m_ui.selected->setSize(64);
28	m_ui.selected->setDimensions(QSize(1, 1));
29	updatePalette();
30
31	const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
32
33	m_ui.hexcode->setFont(font);
34	m_ui.value->setFont(font);
35	m_ui.index->setFont(font);
36	m_ui.r->setFont(font);
37	m_ui.g->setFont(font);
38	m_ui.b->setFont(font);
39
40	connect(m_ui.bgGrid, SIGNAL(indexPressed(int)), this, SLOT(selectIndex(int)));
41	connect(m_ui.objGrid, &Swatch::indexPressed, [this] (int index) { selectIndex(index + 256); });
42	connect(m_ui.exportBG, &QAbstractButton::clicked, [this] () { exportPalette(0, 256); });
43	connect(m_ui.exportOBJ, &QAbstractButton::clicked, [this] () { exportPalette(256, 256); });
44
45	connect(controller, SIGNAL(gameStopped(GBAThread*)), this, SLOT(close()));
46}
47
48void PaletteView::updatePalette() {
49	if (!m_controller->thread() || !m_controller->thread()->gba) {
50		return;
51	}
52	const uint16_t* palette = m_controller->thread()->gba->video.palette;
53	for (int i = 0; i < 256; ++i) {
54		m_ui.bgGrid->setColor(i, palette[i]);
55		m_ui.objGrid->setColor(i, palette[i + 256]);
56	}
57	m_ui.bgGrid->update();
58	m_ui.objGrid->update();
59}
60
61void PaletteView::selectIndex(int index) {
62	uint16_t color = m_controller->thread()->gba->video.palette[index];
63	m_ui.selected->setColor(0, color);
64	uint32_t r = GBA_R5(color);
65	uint32_t g = GBA_G5(color);
66	uint32_t b = GBA_B5(color);
67	uint32_t hexcode = (r << 19) | (g << 11) | (b << 3);
68	m_ui.hexcode->setText(tr("#%0").arg(hexcode, 6, 16, QChar('0')));
69	m_ui.value->setText(tr("0x%0").arg(color, 4, 16, QChar('0')));
70	m_ui.index->setText(tr("%0").arg(index, 3, 10, QChar('0')));
71	m_ui.r->setText(tr("0x%0 (%1)").arg(r, 2, 16, QChar('0')).arg(r, 2, 10, QChar('0')));
72	m_ui.g->setText(tr("0x%0 (%1)").arg(g, 2, 16, QChar('0')).arg(g, 2, 10, QChar('0')));
73	m_ui.b->setText(tr("0x%0 (%1)").arg(b, 2, 16, QChar('0')).arg(b, 2, 10, QChar('0')));
74}
75
76void PaletteView::exportPalette(int start, int length) {
77	if (start >= 512) {
78		return;
79	}
80	if (start + length > 512) {
81		length = 512 - start;
82	}
83	m_controller->threadInterrupt();
84	QString filename = QFileDialog::getSaveFileName(this, tr("Export palette"), QString(), tr("Windows PAL (*.pal)"));
85	if (filename.isNull()) {
86		m_controller->threadContinue();
87		return;
88	}
89	VFile* vf = VFileOpen(filename.toLocal8Bit().constData(), O_WRONLY | O_CREAT | O_TRUNC);
90	if (!vf) {
91		m_controller->threadContinue();
92		return;
93	}
94	GBAExportPaletteRIFF(vf, length, &m_controller->thread()->gba->video.palette[start]);
95	vf->close(vf);
96	m_controller->threadContinue();
97}