UI: Hide mouse cursor when running

This commit is contained in:
Sour 2019-03-15 13:46:46 -04:00
parent 92d915b585
commit 9c82005a5c
8 changed files with 215 additions and 14 deletions

View file

@ -8,6 +8,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Mesen.GUI.Config;
using Mesen.GUI.Emulation;
namespace Mesen.GUI.Controls
{
@ -43,12 +44,12 @@ namespace Mesen.GUI.Controls
private void ctrlRenderer_MouseMove(object sender, MouseEventArgs e)
{
/*CursorManager.OnMouseMove(this);
CursorManager.OnMouseMove(this);
if(CursorManager.NeedMouseIcon) {
this.Cursor = Cursors.Cross;
}
*/
double xPos = (double)e.X / this.Width;
double yPos = (double)e.Y / this.Height;
@ -60,7 +61,7 @@ namespace Mesen.GUI.Controls
private void ctrlRenderer_MouseLeave(object sender, EventArgs e)
{
//CursorManager.OnMouseLeave();
CursorManager.OnMouseLeave();
InputApi.SetMousePosition(-1, -1);
}
}

View file

@ -0,0 +1,194 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Mesen.GUI.Config;
using Mesen.GUI.Forms;
namespace Mesen.GUI.Emulation
{
public class CursorManager
{
private static bool _cursorHidden = false;
private static Point _lastPosition;
private static Timer _tmrHideMouse = new Timer();
private static Timer _tmrCheckMouseMove = new Timer();
private static bool _mouseCaptured = false;
static CursorManager()
{
_tmrHideMouse.Interval = 3000;
_tmrHideMouse.Tick += tmrHideMouse_Tick;
_tmrCheckMouseMove.Interval = 500;
_tmrCheckMouseMove.Tick += tmrCheckMouseMove_Tick;
_tmrCheckMouseMove.Start();
}
public static void StopTimers()
{
_tmrCheckMouseMove.Stop();
_tmrHideMouse.Stop();
}
private static void tmrCheckMouseMove_Tick(object sender, EventArgs e)
{
//Rarely the cursor becomes hidden despite leaving the window or moving
//Have not been able to find a reliable way to reproduce it yet
//This is a patch to prevent that bug from having any negative impact
if(!_mouseCaptured && _lastPosition != Cursor.Position) {
_lastPosition = Cursor.Position;
//TODO
/*bool running = EmuRunner.IsRunning() && !EmuApi.IsPaused();
if(running && ConfigManager.Config.Input.HideMousePointerForZapper && CursorManager.IsLightGun) {
//Keep mouse hidden when using zapper if option to hide mouse is enabled
return;
}*/
ShowMouse();
}
}
private static void tmrHideMouse_Tick(object sender, EventArgs e)
{
bool running = EmuRunner.IsRunning() && !EmuApi.IsPaused();
if(running) {
HideMouse();
_tmrHideMouse.Stop();
} else {
ShowMouse();
_tmrHideMouse.Stop();
}
}
private static void ShowMouse()
{
if(_cursorHidden) {
Cursor.Show();
_cursorHidden = false;
}
}
private static void HideMouse()
{
if(!_cursorHidden) {
Cursor.Hide();
_cursorHidden = true;
}
}
private static bool IsLightGun
{
get
{
for(int i = 0; i < 4; i++) {
switch(ConfigManager.Config.Input.Controllers[i].Type) {
case ControllerType.SuperScope:
return true;
}
}
return false;
}
}
public static bool NeedMouseIcon
{
get { return false; }
}
public static void OnMouseMove(Control ctrl)
{
if(_mouseCaptured && AllowMouseCapture) {
HideMouse();
_tmrHideMouse.Stop();
Form frm = Application.OpenForms[0];
Point centerPos = frm.PointToScreen(new Point(frm.Width / 2, frm.Height / 2));
Point diff = new Point(Cursor.Position.X - centerPos.X, Cursor.Position.Y - centerPos.Y);
if(diff.X != 0 || diff.Y != 0) {
InputApi.SetMouseMovement((Int16)diff.X, (Int16)diff.Y);
Cursor.Position = centerPos;
}
} else {
_mouseCaptured = false;
if(!EmuRunner.IsRunning() || EmuApi.IsPaused()) {
ShowMouse();
} /* TODO else if(ConfigManager.Config.Input.HideMousePointerForZapper && CursorManager.IsLightGun) {
//Keep mouse hidden when using zapper if option to hide mouse is enabled
HideMouse();
return;
}*/
_tmrHideMouse.Stop();
if(!CursorManager.NeedMouseIcon) {
//Only hide mouse if no zapper (otherwise this could be pretty annoying)
ctrl.Cursor = Cursors.Default;
if(EmuRunner.IsRunning() && !EmuApi.IsPaused()) {
_tmrHideMouse.Start();
}
}
}
}
public static void OnMouseLeave()
{
_tmrHideMouse.Stop();
ShowMouse();
}
public static bool AllowMouseCapture
{
get
{
if(!EmuRunner.IsRunning()) {
return false;
}
if(EmuApi.IsPaused()) {
return false;
}
//TODO
/*
if(EmuApi.CheckFlag(EmulationFlags.InBackground)) {
return false;
}*/
for(int i = 0; i < 4; i++) {
switch(ConfigManager.Config.Input.Controllers[i].Type) {
case ControllerType.SnesMouse:
return true;
}
}
return false;
}
}
public static void ReleaseMouse()
{
_mouseCaptured = false;
ShowMouse();
}
public static void CaptureMouse()
{
if(AllowMouseCapture) {
if(!_mouseCaptured) {
EmuApi.DisplayMessage("Input", ResourceHelper.GetMessage("MouseModeEnabled"));
}
_mouseCaptured = true;
HideMouse();
Form frm = Application.OpenForms[0];
Point centerPos = frm.PointToScreen(new Point(frm.Width / 2, frm.Height / 2));
Cursor.Position = centerPos;
}
}
}
}

View file

@ -68,7 +68,7 @@ namespace Mesen.GUI.Emulation
private void ctrlRenderer_MouseMove(object sender, MouseEventArgs e)
{
if(sender != _recentGames) {
//CursorManager.OnMouseMove((Control)sender);
CursorManager.OnMouseMove((Control)sender);
}
if(this.HideMenuStrip && !_menu.ContainsFocus) {
@ -85,7 +85,7 @@ namespace Mesen.GUI.Emulation
if(this.HideMenuStrip) {
_menu.Visible = false;
}
//CursorManager.CaptureMouse();
CursorManager.CaptureMouse();
}
private void panelRenderer_Click(object sender, EventArgs e)
@ -93,7 +93,7 @@ namespace Mesen.GUI.Emulation
if(this.HideMenuStrip) {
_menu.Visible = false;
}
//CursorManager.CaptureMouse();
CursorManager.CaptureMouse();
_renderer.Focus();
}

View file

@ -1,4 +1,5 @@
using System;
using Mesen.GUI.Emulation;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
@ -26,7 +27,7 @@ namespace Mesen.GUI.Forms
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if(keyData == Keys.Escape) {
//CursorManager.ReleaseMouse();
CursorManager.ReleaseMouse();
}
return base.ProcessCmdKey(ref msg, keyData);

View file

@ -52,6 +52,8 @@ namespace Mesen.GUI.Forms.Config
AddBinding(nameof(VideoConfig.OverscanRight), nudOverscanRight);
AddBinding(nameof(VideoConfig.OverscanTop), nudOverscanTop);
AddBinding(nameof(VideoConfig.OverscanBottom), nudOverscanBottom);
UpdateOverscanImage(picOverscan, 0, 0, 0, 0);
}
protected override bool ValidateInput()

View file

@ -1,4 +1,5 @@
using System;
using Mesen.GUI.Emulation;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
@ -43,7 +44,7 @@ namespace Mesen.GUI.Forms
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
//CursorManager.CaptureMouse();
CursorManager.CaptureMouse();
SetMouseButtonState(Control.MouseButtons);
}
@ -68,11 +69,11 @@ namespace Mesen.GUI.Forms
int leftMargin = (this.Width - size.Width) / 2;
int topMargin = (this.Height - size.Height) / 2;
//CursorManager.OnMouseMove(this);
CursorManager.OnMouseMove(this);
/*if(CursorManager.NeedMouseIcon) {
if(CursorManager.NeedMouseIcon) {
this.Cursor = Cursors.Cross;
}*/
}
double xPos = (double)(e.X - leftMargin) / size.Width;
double yPos = (double)(e.Y - topMargin) / size.Height;
@ -87,7 +88,7 @@ namespace Mesen.GUI.Forms
{
base.OnMouseLeave(e);
//CursorManager.OnMouseLeave();
CursorManager.OnMouseLeave();
InputApi.SetMousePosition(-1, -1);
}
}

View file

@ -11,6 +11,7 @@ namespace Mesen.GUI
[DllImport(DllPath)] public static extern void SetKeyState(Int32 scanCode, [MarshalAs(UnmanagedType.I1)]bool pressed);
[DllImport(DllPath)] public static extern void ResetKeyState();
[DllImport(DllPath)] public static extern void SetMouseMovement(Int16 x, Int16 y);
[DllImport(DllPath)] public static extern void SetMousePosition(double x, double y);
[DllImport(DllPath)] public static extern void DisableAllKeys([MarshalAs(UnmanagedType.I1)]bool disabled);
[DllImport(DllPath)] public static extern void UpdateInputDevices();

View file

@ -467,6 +467,7 @@
<SubType>Form</SubType>
</Compile>
<Compile Include="Debugger\WatchManager.cs" />
<Compile Include="Emulation\CursorManager.cs" />
<Compile Include="Emulation\DisplayManager.cs" />
<Compile Include="Emulation\SaveStateManager.cs" />
<Compile Include="Forms\BaseConfigForm.Designer.cs">