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