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
12extern "C" {
13#include "gba/video.h"
14}
15
16using namespace QGBA;
17
18MessagePainter::MessagePainter(QObject* parent)
19 : QObject(parent)
20 , m_messageTimer(this)
21{
22 m_messageFont.setFamily("Source Code Pro");
23 m_messageFont.setStyleHint(QFont::Monospace);
24 m_messageFont.setPixelSize(13);
25 connect(&m_messageTimer, SIGNAL(timeout()), this, SLOT(clearMessage()));
26 m_messageTimer.setSingleShot(true);
27 m_messageTimer.setInterval(5000);
28
29 clearMessage();
30}
31
32void MessagePainter::resize(const QSize& size, bool lockAspectRatio) {
33 int w = size.width();
34 int h = size.height();
35 int drawW = w;
36 int drawH = h;
37 if (lockAspectRatio) {
38 if (w * 2 > h * 3) {
39 drawW = h * 3 / 2;
40 } else if (w * 2 < h * 3) {
41 drawH = w * 2 / 3;
42 }
43 }
44 m_world.reset();
45 m_world.translate((w - drawW) / 2, (h - drawH) / 2);
46 m_world.scale(qreal(drawW) / VIDEO_HORIZONTAL_PIXELS, qreal(drawH) / VIDEO_VERTICAL_PIXELS);
47 m_message.prepare(m_world, m_messageFont);
48}
49
50void MessagePainter::paint(QPainter* painter) {
51 painter->setWorldTransform(m_world);
52 painter->setRenderHint(QPainter::Antialiasing);
53 painter->setFont(m_messageFont);
54 painter->setPen(Qt::black);
55 painter->translate(1, VIDEO_VERTICAL_PIXELS - m_messageFont.pixelSize() - 1);
56 const static int ITERATIONS = 11;
57 for (int i = 0; i < ITERATIONS; ++i) {
58 painter->save();
59 painter->translate(cos(i * 2.0 * M_PI / ITERATIONS) * 0.8, sin(i * 2.0 * M_PI / ITERATIONS) * 0.8);
60 painter->drawStaticText(0, 0, m_message);
61 painter->restore();
62 }
63 painter->setPen(Qt::white);
64 painter->drawStaticText(0, 0, m_message);
65}
66
67void MessagePainter::showMessage(const QString& message) {
68 m_message.setText(message);
69 m_message.prepare(m_world, m_messageFont);
70 m_messageTimer.stop();
71 m_messageTimer.start();
72}
73
74void MessagePainter::clearMessage() {
75 m_message.setText(QString());
76 m_messageTimer.stop();
77}