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 , m_size(8)
17{
18 m_backing = QPixmap(256, 768);
19 m_backing.fill(Qt::transparent);
20 resize(256, 768);
21 setTileCount(3072);
22}
23
24void TilePainter::paintEvent(QPaintEvent* event) {
25 QPainter painter(this);
26 painter.drawPixmap(QPoint(), m_backing);
27}
28
29void TilePainter::resizeEvent(QResizeEvent* event) {
30 int w = width() / m_size;
31 int calculatedHeight = (m_tileCount + w - 1) * m_size / w;
32 calculatedHeight -= calculatedHeight % m_size;
33 if (width() / m_size != m_backing.width() / m_size || m_backing.height() != calculatedHeight) {
34 m_backing = QPixmap(width(), calculatedHeight);
35 m_backing.fill(Qt::transparent);
36 }
37}
38
39void TilePainter::mousePressEvent(QMouseEvent* event) {
40 int x = event->x() / m_size;
41 int y = event->y() / m_size;
42 emit indexPressed(y * (width() / m_size) + x);
43}
44
45void TilePainter::setTile(int index, const uint16_t* data) {
46 QPainter painter(&m_backing);
47 int w = width() / m_size;
48 int x = index % w;
49 int y = index / w;
50 QRect r(x * m_size, y * m_size, m_size, m_size);
51 QImage tile(reinterpret_cast<const uchar*>(data), 8, 8, QImage::Format_RGB555);
52 painter.drawImage(r, tile.rgbSwapped());
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 resizeEvent(nullptr);
64 }
65}
66
67void TilePainter::setTileMagnification(int mag) {
68 m_size = mag * 8;
69 setTileCount(m_tileCount);
70}