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 = QPixmap(256, 768);
18 m_backing.fill(Qt::transparent);
19 resize(256, 768);
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 if (width() / 8 != m_backing.width() / 8) {
29 m_backing = QPixmap(width(), (3072 * 8) / (width() / 8));
30 m_backing.fill(Qt::transparent);
31 }
32}
33
34void TilePainter::mousePressEvent(QMouseEvent* event) {
35 int x = event->x() / 8;
36 int y = event->y() / 8;
37 emit indexPressed(y * (width() / 8) + x);
38}
39
40void TilePainter::setTile(int index, const uint16_t* data) {
41 QPainter painter(&m_backing);
42 int w = width() / 8;
43 int x = index % w;
44 int y = index / w;
45 QRect r(x * 8, y * 8, 8, 8);
46 QImage tile(reinterpret_cast<const uchar*>(data), 8, 8, QImage::Format_RGB555);
47 painter.fillRect(r, tile.rgbSwapped());
48 update(r);
49}
50
51void TilePainter::setTileCount(int tiles) {
52 setMinimumSize(16, (tiles * 8) / (width() / 8));
53 resizeEvent(nullptr);
54}