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