all repos — mgba @ d25ba2ec59b940940950aa1330fe563bced278bd

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 "gba/supervisor/export.h"
 17#include "util/vfs.h"
 18}
 19
 20using namespace QGBA;
 21
 22PaletteView::PaletteView(GameController* controller, QWidget* parent)
 23	: QWidget(parent)
 24	, m_controller(controller)
 25{
 26	m_ui.setupUi(this);
 27
 28	connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(updatePalette()));
 29	m_ui.bgGrid->setDimensions(QSize(16, 16));
 30	m_ui.objGrid->setDimensions(QSize(16, 16));
 31	m_ui.selected->setSize(64);
 32	m_ui.selected->setDimensions(QSize(1, 1));
 33	updatePalette();
 34
 35	const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
 36
 37	m_ui.hexcode->setFont(font);
 38	m_ui.value->setFont(font);
 39	m_ui.index->setFont(font);
 40	m_ui.r->setFont(font);
 41	m_ui.g->setFont(font);
 42	m_ui.b->setFont(font);
 43
 44	connect(m_ui.bgGrid, SIGNAL(indexPressed(int)), this, SLOT(selectIndex(int)));
 45	connect(m_ui.objGrid, &Swatch::indexPressed, [this] (int index) { selectIndex(index + 256); });
 46	connect(m_ui.exportBG, &QAbstractButton::clicked, [this] () { exportPalette(0, 256); });
 47	connect(m_ui.exportOBJ, &QAbstractButton::clicked, [this] () { exportPalette(256, 256); });
 48
 49	connect(controller, SIGNAL(gameStopped(mCoreThread*)), this, SLOT(close()));
 50}
 51
 52void PaletteView::updatePalette() {
 53	if (!m_controller->thread() || !m_controller->thread()->core) {
 54		return;
 55	}
 56	const uint16_t* palette = static_cast<GBA*>(m_controller->thread()->core->board)->video.palette;
 57	for (int i = 0; i < 256; ++i) {
 58		m_ui.bgGrid->setColor(i, palette[i]);
 59		m_ui.objGrid->setColor(i, palette[i + 256]);
 60	}
 61	m_ui.bgGrid->update();
 62	m_ui.objGrid->update();
 63}
 64
 65void PaletteView::selectIndex(int index) {
 66	uint16_t color = static_cast<GBA*>(m_controller->thread()->core->board)->video.palette[index];
 67	m_ui.selected->setColor(0, color);
 68	uint32_t r = GBA_R5(color);
 69	uint32_t g = GBA_G5(color);
 70	uint32_t b = GBA_B5(color);
 71	uint32_t hexcode = (r << 19) | (g << 11) | (b << 3);
 72	m_ui.hexcode->setText(tr("#%0").arg(hexcode, 6, 16, QChar('0')));
 73	m_ui.value->setText(tr("0x%0").arg(color, 4, 16, QChar('0')));
 74	m_ui.index->setText(tr("%0").arg(index, 3, 10, QChar('0')));
 75	m_ui.r->setText(tr("0x%0 (%1)").arg(r, 2, 16, QChar('0')).arg(r, 2, 10, QChar('0')));
 76	m_ui.g->setText(tr("0x%0 (%1)").arg(g, 2, 16, QChar('0')).arg(g, 2, 10, QChar('0')));
 77	m_ui.b->setText(tr("0x%0 (%1)").arg(b, 2, 16, QChar('0')).arg(b, 2, 10, QChar('0')));
 78}
 79
 80void PaletteView::exportPalette(int start, int length) {
 81	if (start >= 512) {
 82		return;
 83	}
 84	if (start + length > 512) {
 85		length = 512 - start;
 86	}
 87	m_controller->threadInterrupt();
 88	QFileDialog* dialog = GBAApp::app()->getSaveFileDialog(this, tr("Export palette"),
 89	                                                       tr("Windows PAL (*.pal);;Adobe Color Table (*.act)"));
 90	if (!dialog->exec()) {
 91		m_controller->threadContinue();
 92		return;
 93	}
 94	QString filename = dialog->selectedFiles()[0];
 95	VFile* vf = VFileDevice::open(filename, O_WRONLY | O_CREAT | O_TRUNC);
 96	if (!vf) {
 97		LOG(QT, ERROR) << tr("Failed to open output palette file: %1").arg(filename);
 98		m_controller->threadContinue();
 99		return;
100	}
101	QString filter = dialog->selectedNameFilter();
102	if (filter.contains("*.pal")) {
103		GBAExportPaletteRIFF(vf, length, &static_cast<GBA*>(m_controller->thread()->core->board)->video.palette[start]);
104	} else if (filter.contains("*.act")) {
105		GBAExportPaletteACT(vf, length, &static_cast<GBA*>(m_controller->thread()->core->board)->video.palette[start]);
106	}
107	vf->close(vf);
108	m_controller->threadContinue();
109}