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

171 lines
7.1 KiB
C#
Raw Normal View History

2023-07-12 22:41:31 +00:00
using MSFSPopoutPanelManager.DomainModel.Profile;
using MSFSPopoutPanelManager.Shared;
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
{
2022-07-23 19:23:32 +00:00
public class PanelConfigurationOrchestrator : ObservableObject
2022-01-27 13:40:04 +00:00
{
2023-07-12 22:41:31 +00:00
private ProfileData _profileData;
private AppSettingData _appSettingData;
private FlightSimData _flightSimData;
2022-01-27 13:40:04 +00:00
2023-07-12 22:41:31 +00:00
public PanelConfigurationOrchestrator(ProfileData profileData, AppSettingData appSettingData, FlightSimData flightSimData)
2022-01-27 13:40:04 +00:00
{
2023-07-12 22:41:31 +00:00
_profileData = profileData;
_appSettingData = appSettingData;
_flightSimData = flightSimData;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
_appSettingData.EnablePanelResetWhenLockedChanged += (sender, e) =>
{
if (flightSimData.IsInCockpit)
StartConfiguration();
};
_profileData.ActiveProfileChanged += (sender, e) => { EndConfiguration(); EndTouchHook(); };
}
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
private UserProfile ActiveProfile { get { return _profileData == null ? null : _profileData.ActiveProfile; } }
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
2023-07-12 22:41:31 +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)
Application.Current.Dispatcher.Invoke(() =>
2022-07-25 12:40:21 +00:00
{
2023-07-12 22:41:31 +00:00
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...");
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);
if (hasTouchEnabledPanel && !TouchEventManager.IsHooked)
TouchEventManager.Hook();
});
}
public void EndTouchHook()
{
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
if (panelConfig.PanelType == PanelType.CustomPopout && name.IndexOf("(Custom)") == -1)
{
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;
}
}
2023-07-12 22:41:31 +00:00
_profileData.WriteProfiles();
2022-06-13 03:49:56 +00:00
}
}
2022-01-27 13:40:04 +00:00
}
}