From 1000cb30a92b2fea79042e22b95e7a2f1831f7bd Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 26 Apr 2020 17:36:49 +1000 Subject: [PATCH] HostDisplay: Track mouse position --- src/core/host_display.h | 12 ++++++++++++ src/duckstation-qt/qtdisplaywidget.cpp | 14 ++++++++++++-- src/duckstation-qt/qtdisplaywidget.h | 3 ++- src/duckstation-qt/qthostinterface.cpp | 11 +++++++++-- src/duckstation-qt/qthostinterface.h | 3 ++- src/duckstation-sdl/sdl_host_interface.cpp | 8 +++++++- 6 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/core/host_display.h b/src/core/host_display.h index 013ad3988..8e7faef07 100644 --- a/src/core/host_display.h +++ b/src/core/host_display.h @@ -33,6 +33,15 @@ public: ALWAYS_INLINE s32 GetWindowWidth() const { return m_window_width; } ALWAYS_INLINE s32 GetWindowHeight() const { return m_window_height; } + // Position is relative to the top-left corner of the window. + ALWAYS_INLINE s32 GetMousePositionX() const { return m_mouse_position_x; } + ALWAYS_INLINE s32 GetMousePositionY() const { return m_mouse_position_y; } + ALWAYS_INLINE void SetMousePosition(s32 x, s32 y) + { + m_mouse_position_x = x; + m_mouse_position_y = y; + } + virtual RenderAPI GetRenderAPI() const = 0; virtual void* GetRenderDevice() const = 0; virtual void* GetRenderContext() const = 0; @@ -122,6 +131,9 @@ protected: s32 m_window_width = 0; s32 m_window_height = 0; + s32 m_mouse_position_x = 0; + s32 m_mouse_position_y = 0; + s32 m_display_width = 0; s32 m_display_height = 0; s32 m_display_active_left = 0; diff --git a/src/duckstation-qt/qtdisplaywidget.cpp b/src/duckstation-qt/qtdisplaywidget.cpp index 69e3ca37c..e41387153 100644 --- a/src/duckstation-qt/qtdisplaywidget.cpp +++ b/src/duckstation-qt/qtdisplaywidget.cpp @@ -18,6 +18,7 @@ QtDisplayWidget::QtDisplayWidget(QWidget* parent) : QWidget(parent) setAttribute(Qt::WA_NoSystemBackground, true); setAttribute(Qt::WA_PaintOnScreen, true); setFocusPolicy(Qt::StrongFocus); + setMouseTracking(true); } QtDisplayWidget::~QtDisplayWidget() = default; @@ -58,18 +59,27 @@ bool QtDisplayWidget::event(QEvent* event) case QEvent::KeyPress: case QEvent::KeyRelease: { - QKeyEvent* key_event = static_cast(event); + const QKeyEvent* key_event = static_cast(event); if (!key_event->isAutoRepeat()) emit windowKeyEvent(QtUtils::KeyEventToInt(key_event), event->type() == QEvent::KeyPress); return true; } + case QEvent::MouseMove: + { + const qreal dpr = devicePixelRatioFromScreen(); + const QMouseEvent* mouse_event = static_cast(event); + emit windowMouseMoveEvent(static_cast(static_cast(mouse_event->x()) * dpr), + static_cast(static_cast(mouse_event->y()) * dpr)); + return true; + } + case QEvent::MouseButtonPress: case QEvent::MouseButtonRelease: { const u32 button_index = CountTrailingZeros(static_cast(static_cast(event)->button())); - emit windowMouseEvent(static_cast(button_index + 1u), event->type() == QEvent::MouseButtonPress); + emit windowMouseButtonEvent(static_cast(button_index + 1u), event->type() == QEvent::MouseButtonPress); return true; } diff --git a/src/duckstation-qt/qtdisplaywidget.h b/src/duckstation-qt/qtdisplaywidget.h index a53d4422f..9be4289a7 100644 --- a/src/duckstation-qt/qtdisplaywidget.h +++ b/src/duckstation-qt/qtdisplaywidget.h @@ -21,7 +21,8 @@ Q_SIGNALS: void windowRestoredEvent(); void windowClosedEvent(); void windowKeyEvent(int key_code, bool pressed); - void windowMouseEvent(int button, bool pressed); + void windowMouseMoveEvent(int x, int y); + void windowMouseButtonEvent(int button, bool pressed); protected: bool event(QEvent* event) override; diff --git a/src/duckstation-qt/qthostinterface.cpp b/src/duckstation-qt/qthostinterface.cpp index 162216cc6..1e5f5bf4d 100644 --- a/src/duckstation-qt/qthostinterface.cpp +++ b/src/duckstation-qt/qthostinterface.cpp @@ -220,7 +220,13 @@ void QtHostInterface::onDisplayWindowKeyEvent(int key, bool pressed) HandleHostKeyEvent(key, pressed); } -void QtHostInterface::onDisplayWindowMouseEvent(int button, bool pressed) +void QtHostInterface::onDisplayWindowMouseMoveEvent(int x, int y) +{ + DebugAssert(isOnWorkerThread()); + m_display->SetMousePosition(x, y); +} + +void QtHostInterface::onDisplayWindowMouseButtonEvent(int button, bool pressed) { DebugAssert(isOnWorkerThread()); HandleHostMouseEvent(button, pressed); @@ -322,7 +328,8 @@ void QtHostInterface::connectDisplaySignals() connect(widget, &QtDisplayWidget::windowClosedEvent, this, &QtHostInterface::powerOffSystem, Qt::BlockingQueuedConnection); connect(widget, &QtDisplayWidget::windowKeyEvent, this, &QtHostInterface::onDisplayWindowKeyEvent); - connect(widget, &QtDisplayWidget::windowMouseEvent, this, &QtHostInterface::onDisplayWindowMouseEvent); + connect(widget, &QtDisplayWidget::windowMouseMoveEvent, this, &QtHostInterface::onDisplayWindowMouseMoveEvent); + connect(widget, &QtDisplayWidget::windowMouseButtonEvent, this, &QtHostInterface::onDisplayWindowMouseButtonEvent); } void QtHostInterface::disconnectDisplaySignals() diff --git a/src/duckstation-qt/qthostinterface.h b/src/duckstation-qt/qthostinterface.h index 4e63e7d1f..e28fb763c 100644 --- a/src/duckstation-qt/qthostinterface.h +++ b/src/duckstation-qt/qthostinterface.h @@ -104,7 +104,8 @@ public Q_SLOTS: void updateInputMap(); void applyInputProfile(const QString& profile_path); void onDisplayWindowKeyEvent(int key, bool pressed); - void onDisplayWindowMouseEvent(int button, bool pressed); + void onDisplayWindowMouseMoveEvent(int x, int y); + void onDisplayWindowMouseButtonEvent(int button, bool pressed); void bootSystem(const SystemBootParameters& params); void resumeSystemFromState(const QString& filename, bool boot_on_failure); void powerOffSystem(); diff --git a/src/duckstation-sdl/sdl_host_interface.cpp b/src/duckstation-sdl/sdl_host_interface.cpp index 73c3c8a25..9386de110 100644 --- a/src/duckstation-sdl/sdl_host_interface.cpp +++ b/src/duckstation-sdl/sdl_host_interface.cpp @@ -15,8 +15,8 @@ #include "frontend-common/sdl_controller_interface.h" #include "imgui_impl_sdl.h" #include "opengl_host_display.h" -#include "sdl_key_names.h" #include "scmversion/scmversion.h" +#include "sdl_key_names.h" #include #include #include @@ -481,6 +481,12 @@ void SDLHostInterface::HandleSDLEvent(const SDL_Event* event) } break; + case SDL_MOUSEMOTION: + { + m_display->SetMousePosition(event->motion.x, event->motion.y); + } + break; + case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: {