src/platform/qt/MessagePainter.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 "MessagePainter.h"
7
8#include <QPainter>
9
10#include <QDebug>
11
12#include <mgba/gba/interface.h>
13
14using namespace QGBA;
15
16MessagePainter::MessagePainter(QObject* parent)
17 : QObject(parent)
18{
19 m_messageFont.setFamily("Source Code Pro");
20 m_messageFont.setStyleHint(QFont::Monospace);
21 m_messageFont.setPixelSize(13);
22 connect(&m_messageTimer, &QTimer::timeout, this, &MessagePainter::clearMessage);
23 m_messageTimer.setSingleShot(true);
24 m_messageTimer.setInterval(5000);
25
26 clearMessage();
27}
28
29void MessagePainter::resize(const QSize& size, bool lockAspectRatio, qreal scaleFactor) {
30 int w = size.width();
31 int h = size.height();
32 int drawW = w;
33 int drawH = h;
34 if (lockAspectRatio) {
35 if (w * 2 > h * 3) {
36 drawW = h * 3 / 2;
37 } else if (w * 2 < h * 3) {
38 drawH = w * 2 / 3;
39 }
40 }
41 m_world.reset();
42 m_world.scale(qreal(drawW) / GBA_VIDEO_HORIZONTAL_PIXELS, qreal(drawH) / GBA_VIDEO_VERTICAL_PIXELS);
43 m_scaleFactor = scaleFactor;
44 m_local = QPoint(1, GBA_VIDEO_VERTICAL_PIXELS - m_messageFont.pixelSize() - 1);
45 m_local = m_world.map(m_local);
46 m_local += QPoint((w - drawW) / 2, (h - drawH) / 2);
47 m_pixmapBuffer = QPixmap(drawW * m_scaleFactor,
48 (m_messageFont.pixelSize() + 2) * m_world.m22() * m_scaleFactor);
49 m_pixmapBuffer.setDevicePixelRatio(m_scaleFactor);
50 m_mutex.lock();
51 m_message.prepare(m_world, m_messageFont);
52 redraw();
53 m_mutex.unlock();
54}
55
56void MessagePainter::redraw() {
57 m_pixmapBuffer.fill(Qt::transparent);
58 if (m_message.text().isEmpty()) {
59 m_pixmap = m_pixmapBuffer;
60 m_pixmap.setDevicePixelRatio(m_scaleFactor);
61 return;
62 }
63 QPainter painter(&m_pixmapBuffer);
64 painter.setWorldTransform(m_world);
65 painter.setRenderHint(QPainter::Antialiasing);
66 painter.setFont(m_messageFont);
67 painter.setPen(Qt::black);
68 const static int ITERATIONS = 11;
69 for (int i = 0; i < ITERATIONS; ++i) {
70 painter.save();
71 painter.translate(cos(i * 2.0 * M_PI / ITERATIONS) * 0.8, sin(i * 2.0 * M_PI / ITERATIONS) * 0.8);
72 painter.drawStaticText(0, 0, m_message);
73 painter.restore();
74 }
75 painter.setPen(Qt::white);
76 painter.drawStaticText(0, 0, m_message);
77 m_pixmap = m_pixmapBuffer;
78 m_pixmap.setDevicePixelRatio(m_scaleFactor);
79}
80
81void MessagePainter::paint(QPainter* painter) {
82 painter->drawPixmap(m_local, m_pixmap);
83}
84
85
86void MessagePainter::showMessage(const QString& message) {
87 m_mutex.lock();
88 m_message.setText(message);
89 redraw();
90 m_mutex.unlock();
91 m_messageTimer.stop();
92 m_messageTimer.start();
93}
94
95void MessagePainter::clearMessage() {
96 m_mutex.lock();
97 m_message.setText(QString());
98 redraw();
99 m_mutex.unlock();
100 m_messageTimer.stop();
101}