all repos — mgba @ 451225169dc18686768a5cb58ed70ec0ab5b9914

mGBA Game Boy Advance Emulator

src/platform/qt/Swatch.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 "Swatch.h"
 7
 8#include <QMouseEvent>
 9#include <QPainter>
10
11extern "C" {
12#include "gba/video.h"
13}
14
15using namespace QGBA;
16
17Swatch::Swatch(QWidget* parent)
18	: QWidget(parent)
19{
20	m_size = 10;
21}
22
23void Swatch::setSize(int size) {
24	m_size = size;
25	setDimensions(m_dims);
26}
27
28void Swatch::setDimensions(const QSize& size) {
29	m_dims = size;
30	m_backing = QPixmap(size * (m_size + 1) - QSize(1, 1));
31	m_backing.fill(Qt::transparent);
32	int elem = size.width() * size.height();
33	m_colors.resize(elem);
34	for (int i = 0; i < elem; ++i) {
35		updateFill(i);
36	}
37	setMinimumSize(size * (m_size + 1) - QSize(1, 1));
38}
39
40void Swatch::setColor(int index, uint16_t color) {
41	m_colors[index].setRgb(
42		GBA_R8(color),
43		GBA_G8(color),
44		GBA_B8(color));
45	updateFill(index);
46}
47
48void Swatch::paintEvent(QPaintEvent* event) {
49	QPainter painter(this);
50	painter.drawPixmap(QPoint(), m_backing);
51}
52
53void Swatch::mousePressEvent(QMouseEvent* event) {
54	int x = event->x() / (m_size + 1);
55	int y = event->y() / (m_size + 1);
56	emit indexPressed(y * m_dims.width() + x);
57}
58
59void Swatch::updateFill(int index) {
60	QPainter painter(&m_backing);
61	int x = index % m_dims.width();
62	int y = index / m_dims.width();
63	QRect r(x * (m_size + 1), y * (m_size + 1), m_size, m_size);
64	painter.fillRect(r, m_colors[index]);
65	update(r);
66}