all repos — mgba @ 751523899763b7d5aa46c538c3e306d0c927485e

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 "GBAApp.h"
  9#include "LogController.h"
 10#include "VFileDevice.h"
 11
 12#include <QFileDialog>
 13#include <QFontDatabase>
 14
 15extern "C" {
 16#include "core/core.h"
 17#ifdef M_CORE_GBA
 18#include "gba/extra/export.h"
 19#endif
 20#ifdef M_CORE_GB
 21#include "gb/gb.h"
 22#endif
 23#include "util/vfs.h"
 24}
 25
 26using namespace QGBA;
 27
 28PaletteView::PaletteView(GameController* controller, QWidget* parent)
 29	: QWidget(parent)
 30	, m_controller(controller)
 31{
 32	m_ui.setupUi(this);
 33
 34	connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(updatePalette()));
 35	m_ui.bgGrid->setDimensions(QSize(16, 16));
 36	m_ui.objGrid->setDimensions(QSize(16, 16));
 37	int count = 256;
 38#ifdef M_CORE_GB
 39	if (controller->platform() == PLATFORM_GB) {
 40		m_ui.bgGrid->setDimensions(QSize(4, 8));
 41		m_ui.objGrid->setDimensions(QSize(4, 8));
 42		m_ui.bgGrid->setSize(24);
 43		m_ui.objGrid->setSize(24);
 44		count = 32;
 45	}
 46#endif
 47	m_ui.selected->setSize(64);
 48	m_ui.selected->setDimensions(QSize(1, 1));
 49	updatePalette();
 50
 51	const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
 52
 53	m_ui.hexcode->setFont(font);
 54	m_ui.value->setFont(font);
 55	m_ui.index->setFont(font);
 56	m_ui.r->setFont(font);
 57	m_ui.g->setFont(font);
 58	m_ui.b->setFont(font);
 59
 60	connect(m_ui.bgGrid, SIGNAL(indexPressed(int)), this, SLOT(selectIndex(int)));
 61	connect(m_ui.objGrid, &Swatch::indexPressed, [this, count] (int index) { selectIndex(index + count); });
 62	connect(m_ui.exportBG, &QAbstractButton::clicked, [this, count] () { exportPalette(0, count); });
 63	connect(m_ui.exportOBJ, &QAbstractButton::clicked, [this, count] () { exportPalette(count, count); });
 64
 65	connect(controller, SIGNAL(gameStopped(mCoreThread*)), this, SLOT(close()));
 66}
 67
 68void PaletteView::updatePalette() {
 69	if (!m_controller->thread() || !m_controller->thread()->core) {
 70		return;
 71	}
 72	const uint16_t* palette;
 73	size_t count;
 74	switch (m_controller->platform()) {
 75#ifdef M_CORE_GBA
 76	case PLATFORM_GBA:
 77		palette = static_cast<GBA*>(m_controller->thread()->core->board)->video.palette;
 78		count = 256;
 79		break;
 80#endif
 81#ifdef M_CORE_GB
 82	case PLATFORM_GB:
 83		palette = static_cast<GB*>(m_controller->thread()->core->board)->video.palette;
 84		count = 32;
 85		break;
 86#endif
 87	default:
 88		return;
 89	}
 90	for (int i = 0; i < count; ++i) {
 91		m_ui.bgGrid->setColor(i, palette[i]);
 92		m_ui.objGrid->setColor(i, palette[i + count]);
 93	}
 94	m_ui.bgGrid->update();
 95	m_ui.objGrid->update();
 96}
 97
 98void PaletteView::selectIndex(int index) {
 99	const uint16_t* palette;
100	switch (m_controller->platform()) {
101#ifdef M_CORE_GBA
102	case PLATFORM_GBA:
103		palette = static_cast<GBA*>(m_controller->thread()->core->board)->video.palette;
104		break;
105#endif
106#ifdef M_CORE_GB
107	case PLATFORM_GB:
108		palette = static_cast<GB*>(m_controller->thread()->core->board)->video.palette;
109		break;
110#endif
111	default:
112		return;
113	}
114	uint16_t color = palette[index];
115	m_ui.selected->setColor(0, color);
116	uint32_t r = GBA_R5(color);
117	uint32_t g = GBA_G5(color);
118	uint32_t b = GBA_B5(color);
119	uint32_t hexcode = (r << 19) | (g << 11) | (b << 3);
120	m_ui.hexcode->setText(tr("#%0").arg(hexcode, 6, 16, QChar('0')));
121	m_ui.value->setText(tr("0x%0").arg(color, 4, 16, QChar('0')));
122	m_ui.index->setText(tr("%0").arg(index, 3, 10, QChar('0')));
123	m_ui.r->setText(tr("0x%0 (%1)").arg(r, 2, 16, QChar('0')).arg(r, 2, 10, QChar('0')));
124	m_ui.g->setText(tr("0x%0 (%1)").arg(g, 2, 16, QChar('0')).arg(g, 2, 10, QChar('0')));
125	m_ui.b->setText(tr("0x%0 (%1)").arg(b, 2, 16, QChar('0')).arg(b, 2, 10, QChar('0')));
126}
127
128void PaletteView::exportPalette(int start, int length) {
129	if (start >= 512) {
130		return;
131	}
132	if (start + length > 512) {
133		length = 512 - start;
134	}
135	m_controller->threadInterrupt();
136	QFileDialog* dialog = GBAApp::app()->getSaveFileDialog(this, tr("Export palette"),
137	                                                       tr("Windows PAL (*.pal);;Adobe Color Table (*.act)"));
138	if (!dialog->exec()) {
139		m_controller->threadContinue();
140		return;
141	}
142	QString filename = dialog->selectedFiles()[0];
143	VFile* vf = VFileDevice::open(filename, O_WRONLY | O_CREAT | O_TRUNC);
144	if (!vf) {
145		LOG(QT, ERROR) << tr("Failed to open output palette file: %1").arg(filename);
146		m_controller->threadContinue();
147		return;
148	}
149	QString filter = dialog->selectedNameFilter();
150	if (filter.contains("*.pal")) {
151		GBAExportPaletteRIFF(vf, length, &static_cast<GBA*>(m_controller->thread()->core->board)->video.palette[start]);
152	} else if (filter.contains("*.act")) {
153		GBAExportPaletteACT(vf, length, &static_cast<GBA*>(m_controller->thread()->core->board)->video.palette[start]);
154	}
155	vf->close(vf);
156	m_controller->threadContinue();
157}