1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-10-16 06:00:45 +00:00
msfs-popout-panel-manager/WindowsAgent/GameRefocusManager.cs

96 lines
3.5 KiB
C#
Raw Normal View History

2023-07-12 22:41:31 +00:00
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
{
2024-02-28 02:44:21 +00:00
private static int _winEventClickLock;
private static readonly object HookLock = new();
private static bool _isHookMouseDown;
2023-07-12 22:41:31 +00:00
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...");
2024-02-28 02:44:21 +00:00
lock (HookLock)
2023-07-12 22:41:31 +00:00
{
2024-02-28 02:44:21 +00:00
PInvoke.GetCursorPos(out var point);
2023-07-12 22:41:31 +00:00
// 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);
2024-02-28 02:44:21 +00:00
lock (HookLock)
2023-07-12 22:41:31 +00:00
{
_isHookMouseDown = false;
2024-02-28 02:44:21 +00:00
PInvoke.GetCursorPos(out var point);
2023-07-12 22:41:31 +00:00
// 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));
2024-02-28 02:44:21 +00:00
if (prevWinEventClickLock != _winEventClickLock)
return;
if (_isHookMouseDown)
return;
2024-03-15 12:55:55 +00:00
PInvoke.SetForegroundWindow(WindowProcessManager.SimulatorProcess.Handle);
2024-02-28 02:44:21 +00:00
var rect = WindowActionManager.GetWindowRectangle(WindowProcessManager.SimulatorProcess.Handle);
PInvoke.SetCursorPos(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
2023-07-12 22:41:31 +00:00
}
}
}