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