all repos — mgba @ 56e876f3629db5924424788befdf9df5170bb1ea

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	setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
22}
23
24void Swatch::setSize(int size) {
25	m_size = size;
26	setDimensions(m_dims);
27}
28
29void Swatch::setDimensions(const QSize& size) {
30	m_dims = size;
31	m_backing = QPixmap(size * (m_size + 1) - QSize(1, 1));
32	m_backing.fill(Qt::transparent);
33	int elem = size.width() * size.height();
34	m_colors.resize(elem);
35	for (int i = 0; i < elem; ++i) {
36		updateFill(i);
37	}
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}