1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-22 05:40:11 +00:00
msfs-popout-panel-manager/WindowsAgent/GameRefocusManager.cs
2024-03-15 08:55:55 -04:00

95 lines
3.5 KiB
C#

using MSFSPopoutPanelManager.DomainModel.Profile;
using MSFSPopoutPanelManager.DomainModel.Setting;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace MSFSPopoutPanelManager.WindowsAgent
{
public class GameRefocusManager
{
private static int _winEventClickLock;
private static readonly object HookLock = new();
private static bool _isHookMouseDown;
public static ApplicationSetting ApplicationSetting { get; set; }
public static void HandleMouseDownEvent(PanelConfig panelConfig)
{
Debug.WriteLine("Handling touch down event...");
if (!_isHookMouseDown)
{
Debug.WriteLine("Executing touch down event...");
lock (HookLock)
{
PInvoke.GetCursorPos(out var point);
// Disable left clicking if user is touching the title bar area or the borders (with 5 extra pixels for margin of error)
// Title bar
if (point.Y - panelConfig.Top < (panelConfig.HideTitlebar ? 5 : 50))
return;
// Bottom border
if (panelConfig.Top + panelConfig.Height - point.Y < 15)
return;
// Left border
if (point.X - panelConfig.Left < 15)
return;
// Right border
if (panelConfig.Left + panelConfig.Width - point.X < 15)
return;
_isHookMouseDown = true;
}
}
}
public static void HandleMouseUpEvent(PanelConfig panelConfig)
{
Debug.WriteLine("Handling touch up event...");
if (_isHookMouseDown)
{
Debug.WriteLine("Executing touch up event...");
Thread.Sleep(ApplicationSetting.TouchSetting.TouchDownUpDelay);
lock (HookLock)
{
_isHookMouseDown = false;
PInvoke.GetCursorPos(out var point);
// Disable left clicking if user is touching the title bar area
if (point.Y - panelConfig.Top > (panelConfig.HideTitlebar ? 5 : 31))
{
var prevWinEventClickLock = ++_winEventClickLock;
// Use click event refocus only if panel is not a touch panel
if (prevWinEventClickLock == _winEventClickLock && ApplicationSetting.RefocusSetting.RefocusGameWindow.IsEnabled && panelConfig.AutoGameRefocus && !panelConfig.TouchEnabled)
{
Task.Run(() => RefocusMsfs(prevWinEventClickLock));
}
}
}
}
}
private static void RefocusMsfs(int prevWinEventClickLock)
{
Thread.Sleep(Convert.ToInt32(ApplicationSetting.RefocusSetting.RefocusGameWindow.Delay * 1000));
if (prevWinEventClickLock != _winEventClickLock)
return;
if (_isHookMouseDown)
return;
PInvoke.SetForegroundWindow(WindowProcessManager.SimulatorProcess.Handle);
var rect = WindowActionManager.GetWindowRectangle(WindowProcessManager.SimulatorProcess.Handle);
PInvoke.SetCursorPos(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
}
}
}