all repos — mgba @ 686380b6c4d8acf219a704a8932668b11efc47c2

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