all repos — mgba @ a73cfe4496cae2e482422b18d12c566047ff17c1

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 <QFontDatabase>
 9
10using namespace QGBA;
11
12PaletteView::PaletteView(GameController* controller, QWidget* parent)
13	: QWidget(parent)
14	, m_controller(controller)
15{
16	m_ui.setupUi(this);
17
18	connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(updatePalette()));
19	m_ui.bgGrid->setDimensions(QSize(16, 16));
20	m_ui.objGrid->setDimensions(QSize(16, 16));
21	m_ui.selected->setSize(64);
22	m_ui.selected->setDimensions(QSize(1, 1));
23	updatePalette();
24
25	const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
26
27	m_ui.hexcode->setFont(font);
28	m_ui.value->setFont(font);
29	m_ui.index->setFont(font);
30	m_ui.r->setFont(font);
31	m_ui.g->setFont(font);
32	m_ui.b->setFont(font);
33
34	connect(m_ui.bgGrid, SIGNAL(indexPressed(int)), this, SLOT(selectIndex(int)));
35	connect(m_ui.objGrid, &Swatch::indexPressed, [this] (int index) { selectIndex(index + 256); });
36
37	connect(controller, SIGNAL(gameStopped(GBAThread*)), this, SLOT(close()));
38}
39
40void PaletteView::updatePalette() {
41	if (!m_controller->thread() || !m_controller->thread()->gba) {
42		return;
43	}
44	const uint16_t* palette = m_controller->thread()->gba->video.palette;
45	for (int i = 0; i < 256; ++i) {
46		m_ui.bgGrid->setColor(i, palette[i]);
47		m_ui.objGrid->setColor(i, palette[i + 256]);
48	}
49	m_ui.bgGrid->update();
50	m_ui.objGrid->update();
51}
52
53void PaletteView::selectIndex(int index) {
54	uint16_t color = m_controller->thread()->gba->video.palette[index];
55	m_ui.selected->setColor(0, color);
56	uint32_t r = color & 0x1F;
57	uint32_t g = (color >> 5) & 0x1F;
58	uint32_t b = (color >> 10) & 0x1F;
59	uint32_t hexcode = (r << 19) | (g << 11) | (b << 3);
60	m_ui.hexcode->setText(tr("#%0").arg(hexcode, 6, 16, QChar('0')));
61	m_ui.value->setText(tr("0x%0").arg(color, 4, 16, QChar('0')));
62	m_ui.index->setText(tr("%0").arg(index, 3, 10, QChar('0')));
63	m_ui.r->setText(tr("0x%0 (%1)").arg(r, 2, 16, QChar('0')).arg(r, 2, 10, QChar('0')));
64	m_ui.g->setText(tr("0x%0 (%1)").arg(g, 2, 16, QChar('0')).arg(g, 2, 10, QChar('0')));
65	m_ui.b->setText(tr("0x%0 (%1)").arg(b, 2, 16, QChar('0')).arg(b, 2, 10, QChar('0')));
66}