diff --git a/src/platform/qt/Display.cpp b/src/platform/qt/Display.cpp index 211585f79..64ba9c96e 100644 --- a/src/platform/qt/Display.cpp +++ b/src/platform/qt/Display.cpp @@ -68,6 +68,19 @@ Display::Display(QWidget* parent) setMouseTracking(true); } +QSize Display::viewportSize() { + QSize s = size(); + QSize ds = s; + if (isAspectRatioLocked()) { + if (s.width() * m_coreHeight > s.height() * m_coreWidth) { + ds.setWidth(s.height() * m_coreWidth / m_coreHeight); + } else if (s.width() * m_coreHeight < s.height() * m_coreWidth) { + ds.setHeight(s.width() * m_coreHeight / m_coreWidth); + } + } + return ds; +} + void Display::resizeEvent(QResizeEvent*) { m_messagePainter.resize(size(), m_lockAspectRatio, devicePixelRatio()); } @@ -94,3 +107,8 @@ void Display::mouseMoveEvent(QMouseEvent* event) { m_mouseTimer.start(); event->ignore(); } + +void Display::setSystemDimensions(int width, int height) { + m_coreWidth = width; + m_coreHeight = height; +} diff --git a/src/platform/qt/Display.h b/src/platform/qt/Display.h index a78810fe8..54d728b9d 100644 --- a/src/platform/qt/Display.h +++ b/src/platform/qt/Display.h @@ -44,6 +44,8 @@ public: virtual bool supportsShaders() const = 0; virtual VideoShader* shaders() = 0; + QSize viewportSize(); + signals: void showCursor(); void hideCursor(); @@ -68,6 +70,8 @@ protected: MessagePainter* messagePainter() { return &m_messagePainter; } + void setSystemDimensions(int width, int height); + private: static Driver s_driver; static const int MOUSE_DISAPPEAR_TIMER = 1000; @@ -76,6 +80,8 @@ private: bool m_lockAspectRatio; bool m_filter; QTimer m_mouseTimer; + int m_coreWidth; + int m_coreHeight; }; } diff --git a/src/platform/qt/DisplayGL.cpp b/src/platform/qt/DisplayGL.cpp index 5b50ea6fd..726124188 100644 --- a/src/platform/qt/DisplayGL.cpp +++ b/src/platform/qt/DisplayGL.cpp @@ -73,6 +73,10 @@ void DisplayGL::startDrawing(mCoreThread* thread) { mCoreSyncSetVideoSync(&m_context->sync, false); lockAspectRatio(isAspectRatioLocked()); + unsigned width, height; + thread->core->desiredVideoDimensions(thread->core, &width, &height); + setSystemDimensions(width, height); + filter(isFiltered()); #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) messagePainter()->resize(size(), isAspectRatioLocked(), devicePixelRatioF()); diff --git a/src/platform/qt/DisplayQt.cpp b/src/platform/qt/DisplayQt.cpp index 284347d58..0ca443e8b 100644 --- a/src/platform/qt/DisplayQt.cpp +++ b/src/platform/qt/DisplayQt.cpp @@ -21,6 +21,7 @@ DisplayQt::DisplayQt(QWidget* parent) void DisplayQt::startDrawing(mCoreThread* context) { context->core->desiredVideoDimensions(context->core, &m_width, &m_height); + setSystemDimensions(m_width, m_height); m_backing = std::move(QImage()); m_isDrawing = true; } @@ -58,14 +59,7 @@ void DisplayQt::paintEvent(QPaintEvent*) { painter.setRenderHint(QPainter::SmoothPixmapTransform); } QSize s = size(); - QSize ds = s; - if (isAspectRatioLocked()) { - if (s.width() * m_height > s.height() * m_width) { - ds.setWidth(s.height() * m_width / m_height); - } else if (s.width() * m_height < s.height() * m_width) { - ds.setHeight(s.width() * m_height / m_width); - } - } + QSize ds = viewportSize(); QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2); QRect full(origin, ds); diff --git a/src/platform/qt/Window.cpp b/src/platform/qt/Window.cpp index a0630f79e..d54201169 100644 --- a/src/platform/qt/Window.cpp +++ b/src/platform/qt/Window.cpp @@ -686,9 +686,10 @@ void Window::dropEvent(QDropEvent* event) { void Window::mouseMoveEvent(QMouseEvent* event) { QSize dimensions = m_controller->screenDimensions(); + QSize viewportDimensions = m_display->viewportSize(); QSize screenDimensions = m_screenWidget->size(); - int x = dimensions.width() * event->x() / screenDimensions.width(); - int y = dimensions.height() * event->y() / screenDimensions.height(); + int x = dimensions.width() * (event->x() - (screenDimensions.width() - viewportDimensions.width()) / 2) / viewportDimensions.width(); + int y = dimensions.height() * (event->y() - (screenDimensions.height() - viewportDimensions.height()) / 2) / viewportDimensions.height(); m_controller->cursorLocation(x, y); event->accept(); } @@ -698,9 +699,10 @@ void Window::mousePressEvent(QMouseEvent* event) { return; } QSize dimensions = m_controller->screenDimensions(); + QSize viewportDimensions = m_display->viewportSize(); QSize screenDimensions = m_screenWidget->size(); - int x = dimensions.width() * event->x() / screenDimensions.width(); - int y = dimensions.height() * event->y() / screenDimensions.height(); + int x = dimensions.width() * (event->x() - (screenDimensions.width() - viewportDimensions.width()) / 2) / viewportDimensions.width(); + int y = dimensions.height() * (event->y() - (screenDimensions.height() - viewportDimensions.height()) / 2) / viewportDimensions.height(); m_controller->cursorLocation(x, y); m_controller->cursorDown(true); } @@ -710,9 +712,10 @@ void Window::mouseReleaseEvent(QMouseEvent* event) { return; } QSize dimensions = m_controller->screenDimensions(); + QSize viewportDimensions = m_display->viewportSize(); QSize screenDimensions = m_screenWidget->size(); - int x = dimensions.width() * event->x() / screenDimensions.width(); - int y = dimensions.height() * event->y() / screenDimensions.height(); + int x = dimensions.width() * (event->x() - (screenDimensions.width() - viewportDimensions.width()) / 2) / viewportDimensions.width(); + int y = dimensions.height() * (event->y() - (screenDimensions.height() - viewportDimensions.height()) / 2) / viewportDimensions.height(); m_controller->cursorLocation(x, y); m_controller->cursorDown(false); }