Qt: Fix cursor events for aspect-ratio locked

This commit is contained in:
Vicki Pfau 2017-03-20 14:51:35 -07:00
parent 47070562b8
commit 6215bca45b
5 changed files with 39 additions and 14 deletions

View File

@ -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;
}

View File

@ -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;
};
}

View File

@ -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());

View File

@ -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);

View File

@ -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);
}