HostDisplay: Track mouse position

This commit is contained in:
Connor McLaughlin 2020-04-26 17:36:49 +10:00
parent 2502afc3f6
commit 1000cb30a9
6 changed files with 44 additions and 7 deletions

View File

@ -33,6 +33,15 @@ public:
ALWAYS_INLINE s32 GetWindowWidth() const { return m_window_width; } ALWAYS_INLINE s32 GetWindowWidth() const { return m_window_width; }
ALWAYS_INLINE s32 GetWindowHeight() const { return m_window_height; } 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 RenderAPI GetRenderAPI() const = 0;
virtual void* GetRenderDevice() const = 0; virtual void* GetRenderDevice() const = 0;
virtual void* GetRenderContext() const = 0; virtual void* GetRenderContext() const = 0;
@ -122,6 +131,9 @@ protected:
s32 m_window_width = 0; s32 m_window_width = 0;
s32 m_window_height = 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_width = 0;
s32 m_display_height = 0; s32 m_display_height = 0;
s32 m_display_active_left = 0; s32 m_display_active_left = 0;

View File

@ -18,6 +18,7 @@ QtDisplayWidget::QtDisplayWidget(QWidget* parent) : QWidget(parent)
setAttribute(Qt::WA_NoSystemBackground, true); setAttribute(Qt::WA_NoSystemBackground, true);
setAttribute(Qt::WA_PaintOnScreen, true); setAttribute(Qt::WA_PaintOnScreen, true);
setFocusPolicy(Qt::StrongFocus); setFocusPolicy(Qt::StrongFocus);
setMouseTracking(true);
} }
QtDisplayWidget::~QtDisplayWidget() = default; QtDisplayWidget::~QtDisplayWidget() = default;
@ -58,18 +59,27 @@ bool QtDisplayWidget::event(QEvent* event)
case QEvent::KeyPress: case QEvent::KeyPress:
case QEvent::KeyRelease: case QEvent::KeyRelease:
{ {
QKeyEvent* key_event = static_cast<QKeyEvent*>(event); const QKeyEvent* key_event = static_cast<QKeyEvent*>(event);
if (!key_event->isAutoRepeat()) if (!key_event->isAutoRepeat())
emit windowKeyEvent(QtUtils::KeyEventToInt(key_event), event->type() == QEvent::KeyPress); emit windowKeyEvent(QtUtils::KeyEventToInt(key_event), event->type() == QEvent::KeyPress);
return true; return true;
} }
case QEvent::MouseMove:
{
const qreal dpr = devicePixelRatioFromScreen();
const QMouseEvent* mouse_event = static_cast<QMouseEvent*>(event);
emit windowMouseMoveEvent(static_cast<int>(static_cast<double>(mouse_event->x()) * dpr),
static_cast<int>(static_cast<double>(mouse_event->y()) * dpr));
return true;
}
case QEvent::MouseButtonPress: case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease: case QEvent::MouseButtonRelease:
{ {
const u32 button_index = CountTrailingZeros(static_cast<u32>(static_cast<const QMouseEvent*>(event)->button())); const u32 button_index = CountTrailingZeros(static_cast<u32>(static_cast<const QMouseEvent*>(event)->button()));
emit windowMouseEvent(static_cast<int>(button_index + 1u), event->type() == QEvent::MouseButtonPress); emit windowMouseButtonEvent(static_cast<int>(button_index + 1u), event->type() == QEvent::MouseButtonPress);
return true; return true;
} }

View File

@ -21,7 +21,8 @@ Q_SIGNALS:
void windowRestoredEvent(); void windowRestoredEvent();
void windowClosedEvent(); void windowClosedEvent();
void windowKeyEvent(int key_code, bool pressed); 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: protected:
bool event(QEvent* event) override; bool event(QEvent* event) override;

View File

@ -220,7 +220,13 @@ void QtHostInterface::onDisplayWindowKeyEvent(int key, bool pressed)
HandleHostKeyEvent(key, 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()); DebugAssert(isOnWorkerThread());
HandleHostMouseEvent(button, pressed); HandleHostMouseEvent(button, pressed);
@ -322,7 +328,8 @@ void QtHostInterface::connectDisplaySignals()
connect(widget, &QtDisplayWidget::windowClosedEvent, this, &QtHostInterface::powerOffSystem, connect(widget, &QtDisplayWidget::windowClosedEvent, this, &QtHostInterface::powerOffSystem,
Qt::BlockingQueuedConnection); Qt::BlockingQueuedConnection);
connect(widget, &QtDisplayWidget::windowKeyEvent, this, &QtHostInterface::onDisplayWindowKeyEvent); 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() void QtHostInterface::disconnectDisplaySignals()

View File

@ -104,7 +104,8 @@ public Q_SLOTS:
void updateInputMap(); void updateInputMap();
void applyInputProfile(const QString& profile_path); void applyInputProfile(const QString& profile_path);
void onDisplayWindowKeyEvent(int key, bool pressed); 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 bootSystem(const SystemBootParameters& params);
void resumeSystemFromState(const QString& filename, bool boot_on_failure); void resumeSystemFromState(const QString& filename, bool boot_on_failure);
void powerOffSystem(); void powerOffSystem();

View File

@ -15,8 +15,8 @@
#include "frontend-common/sdl_controller_interface.h" #include "frontend-common/sdl_controller_interface.h"
#include "imgui_impl_sdl.h" #include "imgui_impl_sdl.h"
#include "opengl_host_display.h" #include "opengl_host_display.h"
#include "sdl_key_names.h"
#include "scmversion/scmversion.h" #include "scmversion/scmversion.h"
#include "sdl_key_names.h"
#include <cinttypes> #include <cinttypes>
#include <cmath> #include <cmath>
#include <imgui.h> #include <imgui.h>
@ -481,6 +481,12 @@ void SDLHostInterface::HandleSDLEvent(const SDL_Event* event)
} }
break; break;
case SDL_MOUSEMOTION:
{
m_display->SetMousePosition(event->motion.x, event->motion.y);
}
break;
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
{ {