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