all repos — mgba @ 1002dfd0dbd86950145493c82ff1af7744e60c81

mGBA Game Boy Advance Emulator

src/platform/qt/TilePainter.cpp (view raw)

 1/* Copyright (c) 2013-2016 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 "TilePainter.h"
 7
 8#include <QImage>
 9#include <QMouseEvent>
10#include <QPainter>
11
12using namespace QGBA;
13
14TilePainter::TilePainter(QWidget* parent)
15	: QWidget(parent)
16{
17	m_backing.fill(Qt::transparent);
18	resize(256, 768);
19	setTileCount(3072);
20}
21
22void TilePainter::paintEvent(QPaintEvent* event) {
23	QPainter painter(this);
24	painter.drawPixmap(QPoint(), m_backing);
25}
26
27void TilePainter::resizeEvent(QResizeEvent* event) {
28	int w = width() / m_size;
29	int calculatedHeight = (m_tileCount + w - 1) * m_size / w;
30	calculatedHeight -= calculatedHeight % m_size;
31	if (width() / m_size != m_backing.width() / m_size || m_backing.height() != calculatedHeight) {
32		m_backing = QPixmap(width(), calculatedHeight);
33		m_backing.fill(Qt::transparent);
34		emit needsRedraw();
35	}
36}
37
38void TilePainter::mousePressEvent(QMouseEvent* event) {
39	int x = event->x() / m_size;
40	int y = event->y() / m_size;
41	emit indexPressed(y * (width() / m_size) + x);
42}
43
44void TilePainter::setTile(int index, const color_t* data) {
45	QPainter painter(&m_backing);
46	int w = width() / m_size;
47	int x = index % w;
48	int y = index / w;
49	QRect r(x * m_size, y * m_size, m_size, m_size);
50	QImage tile(reinterpret_cast<const uchar*>(data), 8, 8, QImage::Format_ARGB32);
51	tile = tile.convertToFormat(QImage::Format_RGB32).rgbSwapped();
52	painter.drawImage(r, tile);
53	update(r);
54}
55
56void TilePainter::setTileCount(int tiles) {
57	m_tileCount = tiles;
58	if (sizePolicy().horizontalPolicy() != QSizePolicy::Fixed) {
59		// Only manage the size ourselves if we don't appear to have something else managing it
60		int w = width() / m_size;
61		int h = (tiles + w - 1) * m_size / w;
62		setMinimumSize(m_size, h - (h % m_size));
63	} else {		
64		int w = minimumSize().width() / m_size;
65		if (!w) {
66			w = 1;
67		}
68		int h = (tiles + w - 1) * m_size / w;
69		setMinimumSize(w * m_size, h - (h % m_size));
70	}
71	resizeEvent(nullptr);
72}
73
74void TilePainter::setTileMagnification(int mag) {
75	m_size = mag * 8;
76	setTileCount(m_tileCount);
77}