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/PanelSourceOrchestrator.cs

224 lines
8 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
{
private const int CAMERA_VIEW_HOME_COCKPIT_MODE = 8;
private const int CAMERA_VIEW_CUSTOM_CAMERA = 7;
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;
2023-08-18 05:03:56 +00:00
private bool _isEditingPanelSourceLock = false;
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;
2023-08-17 06:03:17 +00:00
public event EventHandler<PanelConfig> OnNonEditOverlayShowed;
2023-07-12 22:41:31 +00:00
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)
{
2023-08-18 05:03:56 +00:00
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()
{
2023-08-18 05:03:56 +00:00
if (_isEditingPanelSourceLock)
return;
_isEditingPanelSourceLock = true;
2023-07-12 22:41:31 +00:00
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)
{
if(_flightSimData.CameraViewTypeAndIndex1 == CAMERA_VIEW_HOME_COCKPIT_MODE)
{
2023-08-17 06:03:17 +00:00
FlightSimOrchestrator.SetCockpitCameraZoomLevel(_profileData.ActiveProfile.PanelSourceCockpitZoomFactor);
}
else
{
LoadCustomView(AppSetting.PopOutSetting.AutoPanning.KeyBinding);
FlightSimOrchestrator.SetCockpitCameraZoomLevel(50);
}
2023-07-12 22:41:31 +00:00
WindowActionManager.BringWindowToForeground(ApplicationHandle);
}
2023-08-18 05:03:56 +00:00
foreach (var panel in _profileData.ActiveProfile.PanelConfigs)
{
if (panel.HasPanelSource)
OnOverlayShowed?.Invoke(this, panel);
}
2022-07-23 19:23:32 +00:00
2023-08-18 05:03:56 +00:00
// Turn off TrackIR if TrackIR is started
FlightSimOrchestrator.TurnOffTrackIR();
});
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-08-18 05:03:56 +00:00
if (!_isEditingPanelSourceLock)
return;
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)
{
// If using windows mode, save MSFS game window configuration
if (_appSettingData.ApplicationSetting.WindowedModeSetting.AutoResizeMsfsGameWindow)
_profileData.SaveMsfsGameWindowConfig();
2023-08-16 03:41:14 +00:00
2023-08-17 06:03:17 +00:00
if(_flightSimData.CameraViewTypeAndIndex1 == CAMERA_VIEW_HOME_COCKPIT_MODE)
{
2023-08-17 06:03:17 +00:00
_profileData.ActiveProfile.PanelSourceCockpitZoomFactor = _flightSimData.CockpitCameraZoom;
}
else
{
2023-08-17 06:03:17 +00:00
// !!! Fix MSFS bug that without setting zoom, everything will be off by few pixels at a time
SetCockpitZoomLevel(_flightSimData.CockpitCameraZoom);
InputEmulationManager.SaveCustomView(AppSetting.PopOutSetting.AutoPanning.KeyBinding);
}
2023-07-12 22:41:31 +00:00
}
2022-07-23 19:23:32 +00:00
2023-08-18 05:03:56 +00:00
_isEditingPanelSourceLock = false;
foreach (var panel in _profileData.ActiveProfile.PanelConfigs)
{
OnOverlayRemoved?.Invoke(this, panel);
}
2023-07-12 22:41:31 +00:00
await Task.Run(() =>
{
2023-08-16 03:41:14 +00:00
Thread.Sleep(500); // wait for custom view save to be completed
2023-07-12 22:41:31 +00:00
WindowActionManager.BringWindowToForeground(ApplicationHandle);
// Turn TrackIR back on
2023-08-16 03:41:14 +00:00
FlightSimOrchestrator.TurnOnTrackIR();
2023-07-12 22:41:31 +00:00
});
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)
2023-08-17 06:03:17 +00:00
OnNonEditOverlayShowed?.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;
2023-08-18 05:03:56 +00:00
_isEditingPanelSourceLock = 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();
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
}
2023-08-14 04:35:14 +00:00
private void LoadCustomView(string keybinding)
{
int retry = 3;
2023-08-14 04:35:14 +00:00
for (var i = 0; i < retry; i++)
{
InputEmulationManager.LoadCustomView(keybinding);
Thread.Sleep(750); // wait for flightsimdata to be updated
if (_flightSimData.CameraViewTypeAndIndex1 == CAMERA_VIEW_CUSTOM_CAMERA) // custom camera view enum
2023-08-14 04:35:14 +00:00
break;
}
}
private void SetCockpitZoomLevel(int zoom)
{
int retry = 3;
for (var i = 0; i < retry; i++)
{
FlightSimOrchestrator.SetCockpitCameraZoomLevel(zoom);
2023-08-16 06:42:18 +00:00
Thread.Sleep(750); // wait for flightsimdata to be updated
if (_flightSimData.CockpitCameraZoom == zoom)
break;
}
}
2022-07-23 19:23:32 +00:00
}
}