1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-23 22:30:09 +00:00
msfs-popout-panel-manager/Orchestration/PanelConfigurationOrchestrator.cs

251 lines
10 KiB
C#
Raw Normal View History

2023-07-12 22:41:31 +00:00
using MSFSPopoutPanelManager.DomainModel.Profile;
2022-07-23 19:23:32 +00:00
using MSFSPopoutPanelManager.WindowsAgent;
2022-01-27 13:40:04 +00:00
using System;
2023-07-12 22:41:31 +00:00
using System.Diagnostics;
2022-01-27 13:40:04 +00:00
using System.Linq;
2022-09-25 14:49:49 +00:00
using System.Threading;
2023-07-12 22:41:31 +00:00
using System.Windows;
2022-01-27 13:40:04 +00:00
2022-07-23 19:23:32 +00:00
namespace MSFSPopoutPanelManager.Orchestration
2022-01-27 13:40:04 +00:00
{
2024-02-28 02:44:21 +00:00
public class PanelConfigurationOrchestrator : BaseOrchestrator
2022-01-27 13:40:04 +00:00
{
2024-03-01 11:28:09 +00:00
private UserProfile ActiveProfile => ProfileData?.ActiveProfile;
private readonly KeyboardOrchestrator _keyboardOrchestrator;
public PanelConfigurationOrchestrator(SharedStorage sharedStorage, FlightSimOrchestrator flightSimOrchestrator, KeyboardOrchestrator keyboardOrchestrator) : base(sharedStorage)
2022-01-27 13:40:04 +00:00
{
2024-03-01 11:28:09 +00:00
_keyboardOrchestrator = keyboardOrchestrator;
2024-02-28 02:44:21 +00:00
AppSettingData.OnEnablePanelResetWhenLockedChanged += (_, _) =>
2023-07-12 22:41:31 +00:00
{
2024-02-28 02:44:21 +00:00
if (FlightSimData.IsInCockpit)
2023-07-12 22:41:31 +00:00
StartConfiguration();
};
2024-02-28 02:44:21 +00:00
ProfileData.OnActiveProfileChanged += (_, _) => { EndConfiguration(); EndTouchHook(); };
flightSimOrchestrator.OnFlightStopped += (_, _) =>
{
EndConfiguration();
EndTouchHook();
};
2022-07-23 19:23:32 +00:00
2024-03-01 11:28:09 +00:00
_keyboardOrchestrator.OnKeystrokeDetected += (_, e) =>
{
2024-03-03 05:02:21 +00:00
if (ActiveProfile == null)
return;
2024-03-01 11:28:09 +00:00
var panel = ActiveProfile.PanelConfigs.FirstOrDefault(p => p.Id == e.PanelId);
if (panel != null && panel.FloatingPanel.IsDetectingKeystroke)
{
2024-03-03 05:02:21 +00:00
panel.FloatingPanel.KeyboardBinding = e.KeyBinding;
2024-03-01 11:28:09 +00:00
panel.FloatingPanel.IsDetectingKeystroke = false;
2024-03-03 05:02:21 +00:00
StopDetectKeystroke();
_keyboardOrchestrator.StartGlobalKeyboardHook(KeyboardHookClientType.FloatingPanel);
2024-03-01 11:28:09 +00:00
}
else
{
ToggleFloatPanel(e.KeyBinding);
}
};
ProfileData.OnUseFloatingPanelChanged += (_, e) =>
{
if (ActiveProfile == null)
return;
if (e)
2024-03-03 05:02:21 +00:00
_keyboardOrchestrator.StartGlobalKeyboardHook(KeyboardHookClientType.FloatingPanel);
2024-03-01 11:28:09 +00:00
else
2024-03-03 05:02:21 +00:00
_keyboardOrchestrator.EndGlobalKeyboardHook(KeyboardHookClientType.FloatingPanel);
2024-03-01 11:28:09 +00:00
};
ProfileData.OnActiveProfileChanged += (_, _) =>
{
if (ActiveProfile == null)
return;
if (ActiveProfile.PanelConfigs.Any(x => x.FloatingPanel.IsEnabled))
2024-03-03 05:02:21 +00:00
_keyboardOrchestrator.StartGlobalKeyboardHook(KeyboardHookClientType.FloatingPanel);
2024-03-01 11:28:09 +00:00
};
}
2022-07-23 19:23:32 +00:00
public void StartConfiguration()
2022-01-27 13:40:04 +00:00
{
2023-07-12 22:41:31 +00:00
if (!ActiveProfile.IsPoppedOut)
return;
2022-10-11 13:37:49 +00:00
2023-07-12 22:41:31 +00:00
Debug.WriteLine("Starting Panel Configuration...");
2022-07-25 12:40:21 +00:00
2024-02-28 02:44:21 +00:00
WindowEventManager.ActiveProfile = ProfileData.ActiveProfile;
WindowEventManager.ApplicationSetting = AppSettingData.ApplicationSetting;
TouchEventManager.ActiveProfile = ProfileData.ActiveProfile;
TouchEventManager.ApplicationSetting = AppSettingData.ApplicationSetting;
GameRefocusManager.ApplicationSetting = AppSettingData.ApplicationSetting;
2022-07-25 12:40:21 +00:00
2023-07-12 22:41:31 +00:00
// Must use application dispatcher to dispatch UI events (winEventHook)
2024-02-28 02:44:21 +00:00
Application.Current.Dispatcher.Invoke(WindowEventManager.HookWinEvent);
2022-01-27 13:40:04 +00:00
}
2022-07-23 19:23:32 +00:00
public void EndConfiguration()
2022-01-27 13:40:04 +00:00
{
2023-07-12 22:41:31 +00:00
Debug.WriteLine("Ending Panel Configuration...");
2024-02-28 02:44:21 +00:00
Application.Current.Dispatcher.Invoke(WindowEventManager.UnhookWinEvent);
2022-01-27 13:40:04 +00:00
}
2023-07-12 22:41:31 +00:00
public void StartTouchHook()
2022-01-27 13:40:04 +00:00
{
2023-07-12 22:41:31 +00:00
Application.Current.Dispatcher.Invoke(() =>
{
TouchEventManager.UnHook();
if (!ActiveProfile.IsPoppedOut)
return;
var hasTouchEnabledPanel = ActiveProfile.PanelConfigs.Any(p => p.TouchEnabled && p.IsPopOutSuccess != null && (bool)p.IsPopOutSuccess);
2023-07-23 05:13:23 +00:00
var hasRefocusDisplays = ActiveProfile.PanelConfigs.Any(p => p.PanelType == PanelType.RefocusDisplay);
2023-07-12 22:41:31 +00:00
2023-07-23 05:13:23 +00:00
if (hasRefocusDisplays || (hasTouchEnabledPanel && !TouchEventManager.IsHooked))
2023-07-12 22:41:31 +00:00
TouchEventManager.Hook();
});
}
public void EndTouchHook()
{
2024-02-28 02:44:21 +00:00
Application.Current.Dispatcher.Invoke(TouchEventManager.UnHook);
2022-01-27 13:40:04 +00:00
}
2022-09-11 13:50:04 +00:00
public void PanelConfigPropertyUpdated(IntPtr panelHandle, PanelConfigPropertyName configPropertyName)
2022-01-27 13:40:04 +00:00
{
2023-07-12 22:41:31 +00:00
if (panelHandle == IntPtr.Zero || ActiveProfile.IsLocked || !ActiveProfile.IsPoppedOut)
2022-01-27 13:40:04 +00:00
return;
2022-09-11 13:50:04 +00:00
var panelConfig = ActiveProfile.PanelConfigs.FirstOrDefault(p => p.PanelHandle == panelHandle);
2022-01-27 13:40:04 +00:00
if (panelConfig != null)
{
2022-07-23 19:23:32 +00:00
if (configPropertyName == PanelConfigPropertyName.FullScreen)
2022-01-27 13:40:04 +00:00
{
2022-04-18 19:52:39 +00:00
InputEmulationManager.ToggleFullScreenPanel(panelConfig.PanelHandle);
2022-09-11 13:50:04 +00:00
2023-07-19 13:14:37 +00:00
if (!panelConfig.FullScreen)
2023-07-12 22:41:31 +00:00
WindowActionManager.MoveWindow(panelConfig.PanelHandle, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height);
2022-04-18 19:52:39 +00:00
}
2022-07-23 19:23:32 +00:00
else if (configPropertyName == PanelConfigPropertyName.PanelName)
2022-04-18 19:52:39 +00:00
{
var name = panelConfig.PanelName;
2022-07-23 19:23:32 +00:00
2024-02-28 02:44:21 +00:00
if (panelConfig.PanelType == PanelType.CustomPopout && name.IndexOf("(Custom)", StringComparison.Ordinal) == -1)
2022-07-23 19:23:32 +00:00
{
2022-04-18 19:52:39 +00:00
name = name + " (Custom)";
2023-07-12 22:41:31 +00:00
WindowActionManager.SetWindowCaption(panelConfig.PanelHandle, name);
2022-07-23 19:23:32 +00:00
}
2022-04-18 19:52:39 +00:00
}
2022-06-30 23:53:08 +00:00
else if (!panelConfig.FullScreen)
2022-04-18 19:52:39 +00:00
{
2022-07-23 19:23:32 +00:00
switch (configPropertyName)
2022-04-18 19:52:39 +00:00
{
case PanelConfigPropertyName.Left:
case PanelConfigPropertyName.Top:
2022-08-13 06:14:49 +00:00
WindowActionManager.MoveWindow(panelConfig.PanelHandle, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height);
2022-04-18 19:52:39 +00:00
break;
case PanelConfigPropertyName.Width:
case PanelConfigPropertyName.Height:
if (panelConfig.PanelType == PanelType.HudBarWindow)
return;
2022-08-13 06:14:49 +00:00
2022-04-18 19:52:39 +00:00
if (panelConfig.HideTitlebar)
2023-07-12 22:41:31 +00:00
{
2022-07-23 19:23:32 +00:00
WindowActionManager.ApplyHidePanelTitleBar(panelConfig.PanelHandle, false);
2023-07-12 22:41:31 +00:00
Thread.Sleep(100);
}
2022-04-18 19:52:39 +00:00
2023-07-12 22:41:31 +00:00
WindowActionManager.MoveWindow(panelConfig.PanelHandle, panelConfig.Left, panelConfig.Top, panelConfig.Width, panelConfig.Height);
2022-04-18 19:52:39 +00:00
if (panelConfig.HideTitlebar)
2023-07-12 22:41:31 +00:00
{
Thread.Sleep(100);
2022-07-23 19:23:32 +00:00
WindowActionManager.ApplyHidePanelTitleBar(panelConfig.PanelHandle, true);
2023-07-12 22:41:31 +00:00
Thread.Sleep(100);
}
2022-04-18 19:52:39 +00:00
break;
case PanelConfigPropertyName.AlwaysOnTop:
2023-07-12 22:41:31 +00:00
WindowActionManager.ApplyAlwaysOnTop(panelConfig.PanelHandle, panelConfig.PanelType, panelConfig.AlwaysOnTop);
2022-04-18 19:52:39 +00:00
break;
case PanelConfigPropertyName.HideTitlebar:
2022-07-23 19:23:32 +00:00
WindowActionManager.ApplyHidePanelTitleBar(panelConfig.PanelHandle, panelConfig.HideTitlebar);
2022-04-18 19:52:39 +00:00
break;
2022-07-25 12:40:21 +00:00
case PanelConfigPropertyName.TouchEnabled:
2023-07-12 22:41:31 +00:00
if (ActiveProfile.IsPoppedOut)
WindowEventManager.HookWinEvent();
2022-06-13 03:49:56 +00:00
2023-07-12 22:41:31 +00:00
if (ActiveProfile.PanelConfigs.Any(p => p.TouchEnabled) && !TouchEventManager.IsHooked) // only start hook if it has not been started
StartTouchHook();
else if (ActiveProfile.PanelConfigs.All(p => !p.TouchEnabled) && TouchEventManager.IsHooked) // only disable hook if no more panel is using touch
EndTouchHook();
2022-07-23 19:23:32 +00:00
2022-09-25 14:49:49 +00:00
break;
2023-07-12 22:41:31 +00:00
case PanelConfigPropertyName.AutoGameRefocus:
if (ActiveProfile.IsPoppedOut)
WindowEventManager.HookWinEvent();
2022-09-25 14:49:49 +00:00
break;
}
}
2024-02-28 02:44:21 +00:00
ProfileData.WriteProfiles();
2022-06-13 03:49:56 +00:00
}
}
2024-02-29 06:11:56 +00:00
public void ToggleFloatPanel(string keyBinding)
{
2024-03-03 05:02:21 +00:00
var panels = ActiveProfile.PanelConfigs.ToList().FindAll(x => string.Equals(x.FloatingPanel.KeyboardBinding, keyBinding, StringComparison.Ordinal));
2024-02-29 06:11:56 +00:00
2024-03-01 11:28:09 +00:00
if (!panels.Any())
2024-02-29 06:11:56 +00:00
return;
2024-03-01 11:28:09 +00:00
foreach (var panel in panels)
{
2024-02-29 06:11:56 +00:00
2024-03-01 11:28:09 +00:00
if (!panel.FloatingPanel.IsEnabled || panel.FullScreen)
return;
2024-02-29 06:11:56 +00:00
2024-03-01 11:28:09 +00:00
if (panel.PanelType is not (PanelType.CustomPopout or PanelType.BuiltInPopout))
return;
2024-02-29 06:11:56 +00:00
2024-03-01 11:28:09 +00:00
if (panel.IsPopOutSuccess == null || !(bool)panel.IsPopOutSuccess)
return;
if (!panel.IsFloating)
{
panel.IsFloating = true;
WindowActionManager.RestoreWindow(panel.PanelHandle);
WindowActionManager.ApplyAlwaysOnTop(panel.PanelHandle, panel.PanelType, true);
}
else
{
panel.IsFloating = false;
WindowActionManager.MinimizeWindow(panel.PanelHandle);
}
2024-02-29 06:11:56 +00:00
}
}
2024-03-01 11:28:09 +00:00
public void StartDetectKeystroke(Guid panelId)
{
var panel = ActiveProfile.PanelConfigs.FirstOrDefault(p => p.Id == panelId);
if (panel != null)
panel.FloatingPanel.IsDetectingKeystroke = true;
2024-03-03 05:02:21 +00:00
_keyboardOrchestrator.StartGlobalKeyboardHook(KeyboardHookClientType.FloatingPanelDetection, panelId);
2024-03-01 11:28:09 +00:00
}
2024-03-03 05:02:21 +00:00
public void StopDetectKeystroke()
2024-03-01 11:28:09 +00:00
{
2024-03-03 05:02:21 +00:00
_keyboardOrchestrator.EndGlobalKeyboardHook(KeyboardHookClientType.FloatingPanelDetection);
2024-03-01 11:28:09 +00:00
}
2022-01-27 13:40:04 +00:00
}
}