all repos — mgba @ a46901b90980c82d4ccc63c8daf2746ec64dd670

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