Qt: Redo message painter sizing

This commit is contained in:
Vicki Pfau 2022-01-15 17:48:21 -08:00
parent 9b6b7c7392
commit b512d6d455
4 changed files with 28 additions and 32 deletions

View File

@ -114,12 +114,15 @@ void Display::configure(ConfigController* config) {
}
void Display::resizeEvent(QResizeEvent*) {
m_messagePainter.resize(size(), m_lockAspectRatio, devicePixelRatio());
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
m_messagePainter.resize(size(), devicePixelRatioF());
#else
m_messagePainter.resize(size(), devicePixelRatio());
#endif
}
void Display::lockAspectRatio(bool lock) {
m_lockAspectRatio = lock;
m_messagePainter.resize(size(), m_lockAspectRatio, devicePixelRatio());
}
void Display::lockIntegerScaling(bool lock) {

View File

@ -98,10 +98,11 @@ void DisplayGL::startDrawing(std::shared_ptr<CoreController> controller) {
filter(isFiltered());
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
messagePainter()->resize(size(), isAspectRatioLocked(), devicePixelRatioF());
messagePainter()->resize(size(), devicePixelRatioF());
#else
messagePainter()->resize(size(), isAspectRatioLocked(), devicePixelRatio());
messagePainter()->resize(size(), devicePixelRatio());
#endif
CoreController::Interrupter interrupter(controller);
QMetaObject::invokeMethod(m_painter.get(), "start");
setUpdatesEnabled(false);
@ -401,8 +402,10 @@ void PainterGL::setMessagePainter(MessagePainter* messagePainter) {
}
void PainterGL::resize(const QSize& size) {
qreal r = m_surface->devicePixelRatio();
m_size = size;
m_window->setSize(m_size);
m_window->setSize(m_size * r);
m_window->setDevicePixelRatio(r);
if (m_started && !m_active) {
forceDraw();
}

View File

@ -25,40 +25,32 @@ MessagePainter::MessagePainter(QObject* parent)
clearMessage();
}
void MessagePainter::resize(const QSize& size, bool lockAspectRatio, qreal scaleFactor) {
int w = size.width();
int h = size.height();
int drawW = w;
int drawH = h;
if (lockAspectRatio) {
if (w * 2 > h * 3) {
drawW = h * 3 / 2;
} else if (w * 2 < h * 3) {
drawH = w * 2 / 3;
}
}
m_world.reset();
m_world.scale(qreal(drawW) / GBA_VIDEO_HORIZONTAL_PIXELS, qreal(drawH) / GBA_VIDEO_VERTICAL_PIXELS);
void MessagePainter::resize(const QSize& size, qreal scaleFactor) {
double drawW = size.width();
double drawH = size.height();
double area = pow(drawW * drawW * drawW * drawH * drawH, 0.2);
m_scaleFactor = scaleFactor;
m_local = QPoint(1, GBA_VIDEO_VERTICAL_PIXELS - m_messageFont.pixelSize() - 1);
m_local = m_world.map(m_local);
m_local += QPoint((w - drawW) / 2, (h - drawH) / 2);
m_pixmapBuffer = QPixmap(drawW * m_scaleFactor,
(m_messageFont.pixelSize() + 2) * m_world.m22() * m_scaleFactor);
m_pixmapBuffer.setDevicePixelRatio(m_scaleFactor);
m_world.reset();
m_world.scale(area / 220., area / 220.);
m_local = QPoint(area / 100., drawH - m_messageFont.pixelSize() * m_world.m22() * 1.3);
m_mutex.lock();
m_message.prepare(m_world, m_messageFont);
redraw();
m_mutex.unlock();
}
void MessagePainter::redraw() {
m_pixmapBuffer.fill(Qt::transparent);
if (m_message.text().isEmpty()) {
m_pixmapBuffer.fill(Qt::transparent);
m_pixmap = m_pixmapBuffer;
m_pixmap.setDevicePixelRatio(m_scaleFactor);
return;
}
m_message.prepare(m_world, m_messageFont);
QSizeF sizef = m_message.size() * m_scaleFactor;
m_pixmapBuffer = QPixmap(sizef.width() * m_world.m11(), sizef.height() * m_world.m22());
m_pixmapBuffer.setDevicePixelRatio(m_scaleFactor);
m_pixmapBuffer.fill(Qt::transparent);
QPainter painter(&m_pixmapBuffer);
painter.setWorldTransform(m_world);
painter.setRenderHint(QPainter::Antialiasing);
@ -74,7 +66,6 @@ void MessagePainter::redraw() {
painter.setPen(Qt::white);
painter.drawStaticText(0, 0, m_message);
m_pixmap = m_pixmapBuffer;
m_pixmap.setDevicePixelRatio(m_scaleFactor);
}
void MessagePainter::paint(QPainter* painter) {
@ -83,7 +74,6 @@ void MessagePainter::paint(QPainter* painter) {
}
}
void MessagePainter::showMessage(const QString& message) {
m_mutex.lock();
m_message.setText(message);

View File

@ -19,7 +19,7 @@ Q_OBJECT
public:
MessagePainter(QObject* parent = nullptr);
void resize(const QSize& size, bool lockAspectRatio, qreal scaleFactor);
void resize(const QSize& size, qreal scaleFactor);
void paint(QPainter* painter);
void setScaleFactor(qreal factor);
@ -32,13 +32,13 @@ private:
QMutex m_mutex;
QStaticText m_message;
qreal m_scaleFactor = 1;
QPixmap m_pixmap;
QPixmap m_pixmapBuffer;
QTimer m_messageTimer{this};
QPoint m_local;
QTransform m_world;
QFont m_messageFont;
qreal m_scaleFactor = 1;
};
}