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.
This commit is contained in:
Bevan Weiss 2020-08-30 15:12:36 +10:00 committed by Megamouse
parent a86a3d2fee
commit ca3ee019cc
6 changed files with 60 additions and 4 deletions

View file

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

View file

@ -24,4 +24,5 @@ public:
bool eventFilter(QObject* obj, QEvent* ev) override;
private:
QWindow* m_target = nullptr;
bool get_mouse_lock_state();
};

View file

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

View file

@ -101,6 +101,8 @@ protected:
private:
QWindow* m_target = nullptr;
bool get_mouse_lock_state();
std::vector<std::shared_ptr<Pad>> bindings;
// Button Movements

View file

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

View file

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