From ca3ee019ccc8968724c847d4125e31aa615b8465 Mon Sep 17 00:00:00 2001 From: Bevan Weiss Date: Sun, 30 Aug 2020 15:12:36 +1000 Subject: [PATCH] GUI: Mouse Hide and Lock Keyboard Shortcut This should address the second point of #4502. A few notes: 1. it changes the current behaviour of the 'Fullscreen cursor'. Currently it defaults to be captive. This changes it so that it defaults to NOT being captive, but can be made captive with the CTRL+L key combination. 2. There are situations when in windowed mode it's possible to escape the captivity (it's like a minigame if you will). This requires the mouse movement to exceed the bounds of the window in a single event scan. It will just show up as a temporary visibility of the cursor when outside of the window bounds. It's not too difficult to 'fix', but might not be a likely enough scenario to warrant either. 3. There currently isn't an ability to change what this keyboard combo maps to, but it's inline with a collection of other similar keyboard mappings. I think adding such a more generic keyboard mapping system (not for just keypad items, but system items.. e.g. so that Emulator stop could be mapped to something other than CTRL+S etc) is a bit out-of-scope of this particular PR. --- rpcs3/Input/basic_mouse_handler.cpp | 14 +++++++++++++- rpcs3/Input/basic_mouse_handler.h | 1 + rpcs3/Input/keyboard_pad_handler.cpp | 14 +++++++++++++- rpcs3/Input/keyboard_pad_handler.h | 2 ++ rpcs3/rpcs3qt/gs_frame.cpp | 29 ++++++++++++++++++++++++++-- rpcs3/rpcs3qt/gs_frame.h | 4 ++++ 6 files changed, 60 insertions(+), 4 deletions(-) diff --git a/rpcs3/Input/basic_mouse_handler.cpp b/rpcs3/Input/basic_mouse_handler.cpp index 7bce34beb4..b1b8f54563 100644 --- a/rpcs3/Input/basic_mouse_handler.cpp +++ b/rpcs3/Input/basic_mouse_handler.cpp @@ -89,11 +89,23 @@ void basic_mouse_handler::MouseScroll(QWheelEvent* event) MouseHandlerBase::Scroll(event->angleDelta().y()); } +bool basic_mouse_handler::get_mouse_lock_state() +{ + if (m_target) + { + auto mouse_locked = m_target->property("mouse_locked"); + if (mouse_locked.isValid()) + return mouse_locked.toBool(); + return false; + } + return false; +} + void basic_mouse_handler::MouseMove(QMouseEvent* event) { if (is_time_for_update()) { - if (m_target && m_target->visibility() == QWindow::Visibility::FullScreen && m_target->isActive()) + if (m_target && m_target->isActive() && get_mouse_lock_state()) { // get the screen dimensions const QSize screen = m_target->size(); diff --git a/rpcs3/Input/basic_mouse_handler.h b/rpcs3/Input/basic_mouse_handler.h index f76e520834..06cea8ab24 100644 --- a/rpcs3/Input/basic_mouse_handler.h +++ b/rpcs3/Input/basic_mouse_handler.h @@ -24,4 +24,5 @@ public: bool eventFilter(QObject* obj, QEvent* ev) override; private: QWindow* m_target = nullptr; + bool get_mouse_lock_state(); }; diff --git a/rpcs3/Input/keyboard_pad_handler.cpp b/rpcs3/Input/keyboard_pad_handler.cpp index 908a2059e4..3601828f4c 100644 --- a/rpcs3/Input/keyboard_pad_handler.cpp +++ b/rpcs3/Input/keyboard_pad_handler.cpp @@ -313,6 +313,18 @@ void keyboard_pad_handler::mouseReleaseEvent(QMouseEvent* event) event->ignore(); } +bool keyboard_pad_handler::get_mouse_lock_state() +{ + if (m_target) + { + auto mouse_locked = m_target->property("mouse_locked"); + if (mouse_locked.isValid()) + return mouse_locked.toBool(); + return false; + } + return false; +} + void keyboard_pad_handler::mouseMoveEvent(QMouseEvent* event) { static int movement_x = 0; @@ -320,7 +332,7 @@ void keyboard_pad_handler::mouseMoveEvent(QMouseEvent* event) static int last_pos_x = 0; static int last_pos_y = 0; - if (m_target && m_target->visibility() == QWindow::Visibility::FullScreen && m_target->isActive()) + if (m_target && m_target->isActive() && get_mouse_lock_state()) { // get the screen dimensions const QSize screen = m_target->size(); diff --git a/rpcs3/Input/keyboard_pad_handler.h b/rpcs3/Input/keyboard_pad_handler.h index 2cd7966eb5..656c4a4ed6 100644 --- a/rpcs3/Input/keyboard_pad_handler.h +++ b/rpcs3/Input/keyboard_pad_handler.h @@ -101,6 +101,8 @@ protected: private: QWindow* m_target = nullptr; + bool get_mouse_lock_state(); + std::vector> bindings; // Button Movements diff --git a/rpcs3/rpcs3qt/gs_frame.cpp b/rpcs3/rpcs3qt/gs_frame.cpp index 3f6c8bd04e..39b91dbad9 100644 --- a/rpcs3/rpcs3qt/gs_frame.cpp +++ b/rpcs3/rpcs3qt/gs_frame.cpp @@ -76,6 +76,11 @@ gs_frame::gs_frame(const QRect& geometry, const QIcon& appIcon, const std::share connect(&m_mousehide_timer, &QTimer::timeout, this, &gs_frame::MouseHideTimeout); m_mousehide_timer.setSingleShot(true); + // We default the mouse lock to being off + m_mouse_hide_and_lock = false; + // and we 'publish' this to the property values of this window + setProperty("mouse_locked", m_mouse_hide_and_lock); + #ifdef _WIN32 m_tb_button = new QWinTaskbarButton(); m_tb_progress = m_tb_button->progress(); @@ -135,6 +140,11 @@ void gs_frame::keyPressEvent(QKeyEvent *keyEvent) screenshot.success("Made forced mark %d in log", ++count); return; } + else if (keyEvent->modifiers() == Qt::ControlModifier) + { + toggle_mouselock(); + return; + } break; case Qt::Key_Return: if (keyEvent->modifiers() == Qt::AltModifier) @@ -209,6 +219,17 @@ void gs_frame::toggle_fullscreen() }); } +void gs_frame::toggle_mouselock() +{ + // first we toggle the value + m_mouse_hide_and_lock = !m_mouse_hide_and_lock; + + // and then we publish it to the window properties + setProperty("mouse_locked", m_mouse_hide_and_lock); + + HandleCursor(this->visibility()); +} + void gs_frame::close() { Emu.Stop(); @@ -465,14 +486,18 @@ void gs_frame::mouseDoubleClickEvent(QMouseEvent* ev) void gs_frame::HandleCursor(QWindow::Visibility visibility) { - if (visibility == QWindow::Visibility::FullScreen && !m_show_mouse_in_fullscreen) + if (m_mouse_hide_and_lock || (visibility == QWindow::Visibility::FullScreen && !m_show_mouse_in_fullscreen)) { setCursor(Qt::BlankCursor); m_mousehide_timer.stop(); } else { - setCursor(Qt::ArrowCursor); + if (!m_mouse_hide_and_lock) + { + setCursor(Qt::ArrowCursor); + } + if (m_hide_mouse_after_idletime) { m_mousehide_timer.start(m_hide_mouse_idletime); diff --git a/rpcs3/rpcs3qt/gs_frame.h b/rpcs3/rpcs3qt/gs_frame.h index aba43e51f4..fd743627a2 100644 --- a/rpcs3/rpcs3qt/gs_frame.h +++ b/rpcs3/rpcs3qt/gs_frame.h @@ -36,6 +36,7 @@ private: QString m_window_title; bool m_disable_mouse = false; bool m_disable_kb_hotkeys = false; + bool m_mouse_hide_and_lock = false; bool m_show_mouse_in_fullscreen = false; bool m_hide_mouse_after_idletime = false; u32 m_hide_mouse_idletime = 2000; // ms @@ -77,6 +78,9 @@ protected: bool event(QEvent* ev) override; +private: + void toggle_mouselock(); + private Q_SLOTS: void HandleCursor(QWindow::Visibility visibility); void MouseHideTimeout();