1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-22 13:50:14 +00:00
msfs-popout-panel-manager/Orchestration/PanelSourceOrchestrator.cs

188 lines
7.1 KiB
C#
Raw Normal View History

2023-07-12 22:41:31 +00:00
using MSFSPopoutPanelManager.DomainModel.Profile;
using MSFSPopoutPanelManager.DomainModel.Setting;
using MSFSPopoutPanelManager.Shared;
2022-07-23 19:23:32 +00:00
using MSFSPopoutPanelManager.WindowsAgent;
using System;
2023-07-30 21:30:47 +00:00
using System.Threading;
2023-07-12 22:41:31 +00:00
using System.Threading.Tasks;
using Point = System.Drawing.Point;
2022-07-23 19:23:32 +00:00
namespace MSFSPopoutPanelManager.Orchestration
{
public class PanelSourceOrchestrator : ObservableObject
{
2023-07-12 22:41:31 +00:00
private ProfileData _profileData;
private AppSettingData _appSettingData;
2023-07-30 21:30:47 +00:00
private FlightSimData _flightSimData;
private int _prePanelConfigurationCockpitZoomLevel = 50;
2022-07-23 19:23:32 +00:00
2023-07-30 21:30:47 +00:00
public PanelSourceOrchestrator(ProfileData profileData, AppSettingData appSettingData, FlightSimData flightSimData)
2023-07-12 22:41:31 +00:00
{
_profileData = profileData;
_appSettingData = appSettingData;
2023-07-30 21:30:47 +00:00
_flightSimData = flightSimData;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
_profileData.ActiveProfileChanged += (sender, e) => { CloseAllPanelSource(); };
}
2022-07-23 19:23:32 +00:00
internal FlightSimOrchestrator FlightSimOrchestrator { get; set; }
2023-07-12 22:41:31 +00:00
internal IntPtr ApplicationHandle { get; set; }
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
2023-07-12 22:41:31 +00:00
private ApplicationSetting AppSetting { get { return _appSettingData == null ? null : _appSettingData.ApplicationSetting; } }
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
public event EventHandler<PanelConfig> OnOverlayShowed;
public event EventHandler<PanelConfig> OnOverlayRemoved;
public event EventHandler OnPanelSourceSelectionStarted;
public event EventHandler OnPanelSourceSelectionCompleted;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
public void StartPanelSelectionEvent()
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
OnPanelSourceSelectionStarted?.Invoke(this, null);
}
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
public void StartPanelSelection(PanelConfig panelConfig)
{
_profileData.ActiveProfile.IsEditingPanelSource = true;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
InputHookManager.OnLeftClick += (sender, e) => HandleOnPanelSelectionAdded(panelConfig, e);
InputHookManager.StartMouseHook();
}
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
public async Task StartEditPanelSources()
{
await Task.Run(() =>
{
// Set Windowed Display Mode window's configuration if needed
if (_appSettingData.ApplicationSetting.WindowedModeSetting.AutoResizeMsfsGameWindow)
WindowActionManager.SetMsfsGameWindowLocation(ActiveProfile.MsfsGameWindowConfig);
if (AppSetting.PopOutSetting.AutoPanning.IsEnabled)
{
2023-07-30 21:30:47 +00:00
_prePanelConfigurationCockpitZoomLevel = _flightSimData.CockpitCameraZoom;
2023-07-12 22:41:31 +00:00
InputEmulationManager.LoadCustomView(AppSetting.PopOutSetting.AutoPanning.KeyBinding);
2023-07-30 21:30:47 +00:00
FlightSimOrchestrator.SetCockpitCameraZoomLevel(50);
2023-07-12 22:41:31 +00:00
WindowActionManager.BringWindowToForeground(ApplicationHandle);
}
});
foreach (var panel in _profileData.ActiveProfile.PanelConfigs)
{
if (panel.HasPanelSource)
OnOverlayShowed?.Invoke(this, panel);
}
2022-07-23 19:23:32 +00:00
// Turn off TrackIR if TrackIR is started
2023-07-12 22:41:31 +00:00
FlightSimOrchestrator.TurnOffTrackIR(false);
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
public async Task EndEditPanelSources()
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
foreach (var panel in _profileData.ActiveProfile.PanelConfigs)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
OnOverlayRemoved?.Invoke(this, panel);
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
// Save last auto panning camera angle
if (AppSetting.PopOutSetting.AutoPanning.IsEnabled)
{
InputEmulationManager.SaveCustomView(AppSetting.PopOutSetting.AutoPanning.KeyBinding);
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
// If using windows mode, save MSFS game window configuration
if (_appSettingData.ApplicationSetting.WindowedModeSetting.AutoResizeMsfsGameWindow)
_profileData.SaveMsfsGameWindowConfig();
}
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
await Task.Run(() =>
{
// Recenter game or return to after pop out camera view
if (!AppSetting.PopOutSetting.AfterPopOutCameraView.IsEnabled)
2023-07-30 21:30:47 +00:00
{
FlightSimOrchestrator.SetCockpitCameraZoomLevel(_prePanelConfigurationCockpitZoomLevel);
2023-07-12 22:41:31 +00:00
InputEmulationManager.CenterView();
2023-07-30 21:30:47 +00:00
}
2023-07-12 22:41:31 +00:00
else
{
switch (AppSetting.PopOutSetting.AfterPopOutCameraView.CameraView)
{
case AfterPopOutCameraViewType.CockpitCenterView:
2023-07-30 21:30:47 +00:00
FlightSimOrchestrator.SetCockpitCameraZoomLevel(_prePanelConfigurationCockpitZoomLevel);
2023-07-12 22:41:31 +00:00
InputEmulationManager.CenterView();
break;
case AfterPopOutCameraViewType.CustomCameraView:
2023-07-30 21:30:47 +00:00
FlightSimOrchestrator.SetCockpitCameraZoomLevel(_prePanelConfigurationCockpitZoomLevel);
2023-07-12 22:41:31 +00:00
InputEmulationManager.LoadCustomView(AppSetting.PopOutSetting.AfterPopOutCameraView.KeyBinding);
break;
}
}
WindowActionManager.BringWindowToForeground(ApplicationHandle);
// Turn TrackIR back on
FlightSimOrchestrator.TurnOnTrackIR(false);
});
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
public void ShowPanelSourceNonEdit(PanelConfig panel)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
if (panel.HasPanelSource)
OnOverlayShowed?.Invoke(this, panel);
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
public void ClosePanelSourceNonEdit(PanelConfig panel)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
OnOverlayRemoved?.Invoke(this, panel);
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
public void CloseAllPanelSource()
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
if (ActiveProfile != null)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
ActiveProfile.IsEditingPanelSource = false;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
foreach (var panelConfig in ActiveProfile.PanelConfigs)
OnOverlayRemoved?.Invoke(this, panelConfig);
2022-07-23 19:23:32 +00:00
}
}
2023-07-12 22:41:31 +00:00
public void HandleOnPanelSelectionAdded(PanelConfig panelConfig, Point e)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
OnPanelSourceSelectionCompleted?.Invoke(this, null);
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
InputHookManager.EndMouseHook();
2022-07-23 19:23:32 +00:00
if (ActiveProfile == null)
return;
2023-07-12 22:41:31 +00:00
panelConfig.PanelSource.X = e.X;
panelConfig.PanelSource.Y = e.Y;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
_profileData.WriteProfiles();
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
// Show source circle on screen
OnOverlayShowed?.Invoke(this, panelConfig);
2022-07-23 19:23:32 +00:00
// If using windows mode, save MSFS game window configuration
2023-07-12 22:41:31 +00:00
if (_appSettingData.ApplicationSetting.WindowedModeSetting.AutoResizeMsfsGameWindow)
_profileData.SaveMsfsGameWindowConfig();
2023-07-12 22:41:31 +00:00
panelConfig.IsSelectedPanelSource = false;
}
public void RemovePanelSource(PanelConfig panelConfig)
{
// Disable hooks if active
InputHookManager.EndMouseHook();
InputHookManager.EndKeyboardHook();
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
_profileData.ActiveProfile.CurrentMoveResizePanelId = Guid.Empty;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
OnOverlayRemoved?.Invoke(this, panelConfig);
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
_profileData.ActiveProfile.PanelConfigs.Remove(panelConfig);
2022-07-23 19:23:32 +00:00
}
}
}