1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-29 09:10:09 +00:00
msfs-popout-panel-manager/Orchestration/PanelPopOutOrchestrator.cs

545 lines
24 KiB
C#
Raw Normal View History

2022-07-23 19:23:32 +00:00
using MSFSPopoutPanelManager.Shared;
using MSFSPopoutPanelManager.UserDataAgent;
using MSFSPopoutPanelManager.WindowsAgent;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MSFSPopoutPanelManager.Orchestration
{
public class PanelPopOutOrchestrator : ObservableObject
{
// This will be replaced by a signal from Ready to Fly Skipper into webserver in version 4.0
private const int READY_TO_FLY_BUTTON_APPEARANCE_DELAY = 2000;
private int _builtInPanelConfigDelay;
2022-07-23 19:23:32 +00:00
internal ProfileData ProfileData { get; set; }
internal AppSettingData AppSettingData { get; set; }
internal FlightSimData FlightSimData { get; set; }
internal FlightSimOrchestrator FlightSimOrchestrator { private get; set; }
internal PanelSourceOrchestrator PanelSourceOrchestrator { private get; set; }
internal TouchPanelOrchestrator TouchPanelOrchestrator { private get; set; }
private Profile ActiveProfile { get { return ProfileData == null ? null : ProfileData.ActiveProfile; } }
private AppSetting AppSetting { get { return AppSettingData == null ? null : AppSettingData.AppSetting; } }
public event EventHandler OnPopOutStarted;
2022-08-08 06:07:24 +00:00
public event EventHandler<bool> OnPopOutCompleted;
2022-07-23 19:23:32 +00:00
public event EventHandler<TouchPanelOpenEventArg> OnTouchPanelOpened;
public event EventHandler<PanelSourceCoordinate> OnPanelSourceOverlayFlashed;
2022-07-23 19:23:32 +00:00
public void ManualPopOut()
{
if (ActiveProfile == null)
return;
InputHookManager.EndHook();
if (ActiveProfile.PanelSourceCoordinates.Count > 0 || ActiveProfile.TouchPanelBindings.Count > 0 || ActiveProfile.IncludeInGamePanels)
2022-07-23 19:23:32 +00:00
{
StatusMessageWriter.WriteMessage($"Panels pop out in progress for profile:\n{ActiveProfile.ProfileName}", StatusMessageType.Info, true);
_builtInPanelConfigDelay = 0;
2022-07-23 19:23:32 +00:00
CorePopOutSteps();
}
}
public void AutoPopOut()
{
if (ActiveProfile == null)
return;
2022-08-13 06:14:49 +00:00
ProfileData.AutoSwitchProfile();
2022-07-23 19:23:32 +00:00
2022-08-01 23:21:42 +00:00
// find the profile with the matching binding aircraft
var profile = ProfileData.Profiles.FirstOrDefault(p => p.BindingAircrafts.Any(p => p == FlightSimData.CurrentMsfsAircraft));
2022-07-23 19:23:32 +00:00
2022-08-01 23:21:42 +00:00
// Do not do auto pop out if no profile matches the current aircraft
2022-07-23 19:23:32 +00:00
if (profile == null)
return;
// Match the delay for Ready to Fly button to disappear
Thread.Sleep(READY_TO_FLY_BUTTON_APPEARANCE_DELAY);
if (ActiveProfile.PanelSourceCoordinates.Count > 0 || ActiveProfile.TouchPanelBindings.Count > 0 || ActiveProfile.IncludeInGamePanels)
2022-07-23 19:23:32 +00:00
{
StatusMessageWriter.WriteMessage($"Automatic pop out is starting for profile:\n{profile.ProfileName}", StatusMessageType.Info, true);
2022-07-23 20:04:06 +00:00
// Extra wait for cockpit view to appear and align
Thread.Sleep(2000);
2022-07-23 19:23:32 +00:00
_builtInPanelConfigDelay = 4000;
2022-07-23 19:23:32 +00:00
CorePopOutSteps();
}
}
private void CorePopOutSteps()
{
// Set Windowed Display Mode window's configuration if needed
if (AppSettingData.AppSetting.AutoResizeMsfsGameWindow)
WindowActionManager.SetMsfsGameWindowLocation(ActiveProfile.MsfsGameWindowConfig);
2022-07-23 19:23:32 +00:00
// Has custom pop out panels
if (ActiveProfile.PanelSourceCoordinates.Count > 0)
{
// Turn off TrackIR if TrackIR is started
FlightSimOrchestrator.TurnOffTrackIR();
// Turn on power if required to pop out panels at least one (fix Cessna 208b grand caravan mod bug where battery is reported as on)
if (ActiveProfile.PowerOnRequiredForColdStart)
{
int count = 0;
do
{
FlightSimOrchestrator.TurnOnPower();
Thread.Sleep(500);
count++;
}
while (!FlightSimData.ElectricalMasterBatteryStatus && count < 10);
}
// Turn on avionics if required to pop out panels
if (ActiveProfile.PowerOnRequiredForColdStart)
FlightSimOrchestrator.TurnOnAvionics();
}
StartPopout();
// Has custom pop out panels
if (ActiveProfile.PanelSourceCoordinates.Count > 0)
{
// Turn off avionics if needed after pop out
FlightSimOrchestrator.TurnOffAvionics();
// Turn off power if needed after pop out
FlightSimOrchestrator.TurnOffPower();
// Return to custom camera view
ReturnToAfterPopOutCameraView();
// Turn TrackIR back on
FlightSimOrchestrator.TurnOnTrackIR();
}
}
private void StartPopout()
{
List<PanelConfig> panelConfigs = new List<PanelConfig>();
var simulatorProcess = WindowProcessManager.GetSimulatorProcess();
if (simulatorProcess == null || simulatorProcess.Handle == IntPtr.Zero)
{
StatusMessageWriter.WriteMessage("MSFS/SimConnect has not been started. Please try again at a later time.", StatusMessageType.Error, false);
return;
}
if (ActiveProfile == null)
{
StatusMessageWriter.WriteMessage("No profile has been selected. Please select a profile to continue.", StatusMessageType.Error, false);
return;
}
if (ActiveProfile.PanelSourceCoordinates.Count == 0 && ActiveProfile.TouchPanelBindings.Count == 0 && !ActiveProfile.IncludeInGamePanels)
2022-07-23 19:23:32 +00:00
{
StatusMessageWriter.WriteMessage("No panel has been selected for the profile. Please select at least one panel to continue.", StatusMessageType.Error, false);
return;
}
// Close all existing custom pop out panels
WindowActionManager.CloseAllPopOuts();
// Close all panel source overlays
PanelSourceOrchestrator.CloseAllPanelSource();
OnPopOutStarted?.Invoke(this, null);
// Must close out all existing custom pop out panels
if (WindowActionManager.GetWindowsCountByPanelType(new List<PanelType>() { PanelType.CustomPopout, PanelType.MSFSTouchPanel }) > 0)
{
StatusMessageWriter.WriteMessage("Please close all existing panel pop outs to continue.", StatusMessageType.Error, false);
return;
}
// Try to pop out and separate custom panels
if (ActiveProfile.PanelSourceCoordinates.Count > 0)
{
if (AppSetting.UseAutoPanning)
InputEmulationManager.LoadCustomView(AppSetting.AutoPanningKeyBinding);
var panelResults = ExecutePopoutAndSeparation();
if (panelResults == null)
return;
panelConfigs.AddRange(panelResults);
}
// Add the MSFS Touch Panel (My other github project) windows to the panel list
if (AppSetting.TouchPanelSettings.EnableTouchPanelIntegration)
{
var panelResults = AddMsfsTouchPanels(panelConfigs.Count + 100); // add a panelIndex gap
if (panelResults != null)
panelConfigs.AddRange(panelResults);
}
// Add the built-in panels from toolbar menu (ie. VFR Map, Check List, Weather, etc)
if (ActiveProfile.IncludeInGamePanels)
{
// Allow delay to wait for in game built-in pop outs to appear
Thread.Sleep(_builtInPanelConfigDelay);
2022-09-11 13:50:04 +00:00
var panelResults = AddBuiltInPanels();
2022-08-02 20:55:20 +00:00
if (panelResults != null)
panelConfigs.AddRange(panelResults);
2022-07-23 19:23:32 +00:00
}
if (panelConfigs.Count == 0)
{
2022-09-11 13:50:04 +00:00
StatusMessageWriter.WriteMessage("No panels have been found. Please select at least one in-game panel.", StatusMessageType.Error, true);
2022-07-23 19:23:32 +00:00
return;
}
// Line up all the panels
for (var i = panelConfigs.Count - 1; i >= 0; i--)
{
if (panelConfigs[i].PanelType == PanelType.CustomPopout)
{
2022-08-13 06:14:49 +00:00
WindowActionManager.MoveWindow(panelConfigs[i].PanelHandle, panelConfigs[i].Top, panelConfigs[i].Left, panelConfigs[i].Width, panelConfigs[i].Height);
2022-07-23 19:23:32 +00:00
PInvoke.SetForegroundWindow(panelConfigs[i].PanelHandle);
Thread.Sleep(200);
}
}
2022-08-08 06:07:24 +00:00
if (panelConfigs.Count > 0)
2022-07-23 19:23:32 +00:00
{
2022-08-08 06:07:24 +00:00
if (ActiveProfile.PanelConfigs.Count > 0)
{
LoadAndApplyPanelConfigs(panelConfigs);
StatusMessageWriter.WriteMessage("Panels have been popped out succesfully and saved panel settings have been applied.", StatusMessageType.Info, true);
OnPopOutCompleted?.Invoke(this, false);
}
else
{
2022-09-11 13:50:04 +00:00
LoadAndApplyPanelConfigs(panelConfigs);
2022-08-08 06:07:24 +00:00
StatusMessageWriter.WriteMessage("Panels have been popped out succesfully.", StatusMessageType.Info, true);
OnPopOutCompleted?.Invoke(this, true);
}
if (!ActiveProfile.IsLocked)
ProfileData.WriteProfiles();
// For migrating existing profile, if using windows mode, save MSFS game window configuration
if (AppSettingData.AppSetting.AutoResizeMsfsGameWindow && !ActiveProfile.MsfsGameWindowConfig.IsValid)
ProfileData.SaveMsfsGameWindowConfig();
2022-07-23 19:23:32 +00:00
}
}
private List<PanelConfig> ExecutePopoutAndSeparation()
{
List<PanelConfig> panels = new List<PanelConfig>();
// PanelIndex starts at 1
for (var i = 1; i <= ActiveProfile.PanelSourceCoordinates.Count; i++)
{
var x = ActiveProfile.PanelSourceCoordinates[i - 1].X;
var y = ActiveProfile.PanelSourceCoordinates[i - 1].Y;
// show the panel source overlay for split second
Task task = new Task(() => OnPanelSourceOverlayFlashed?.Invoke(this, ActiveProfile.PanelSourceCoordinates[i - 1]));
task.RunSynchronously();
2022-07-23 19:23:32 +00:00
InputEmulationManager.PopOutPanel(x, y, AppSetting.UseLeftRightControlToPopOut);
// Get an AceApp window with empty title
var handle = PInvoke.FindWindow("AceApp", String.Empty);
2022-07-23 19:23:32 +00:00
// Need to move the window to upper left corner first. There is a possible bug in the game that panel pop out to full screen that prevents further clicking.
if (handle != IntPtr.Zero)
2022-08-18 14:20:12 +00:00
WindowActionManager.MoveWindow(handle, 0, 0, 1000, 500);
2022-07-30 02:29:20 +00:00
// The joined panel is always the first panel that got popped out
2022-07-23 19:23:32 +00:00
if (i > 1)
SeparatePanel(panels[0].PanelHandle);
2022-07-23 19:23:32 +00:00
handle = PInvoke.FindWindow("AceApp", String.Empty);
2022-07-23 19:23:32 +00:00
if (handle == IntPtr.Zero && i == 1)
{
StatusMessageWriter.WriteMessage("Unable to pop out the first panel. Please check the first panel's number circle is positioned inside the panel, check for panel obstruction, and check if panel can be popped out. Pop out process stopped.", StatusMessageType.Error, true);
return null;
}
else if (handle == IntPtr.Zero)
{
StatusMessageWriter.WriteMessage($"Unable to pop out panel number {i}. Please check panel's number circle is positioned inside the panel, check for panel obstruction, and check if panel can be popped out. Pop out process stopped.", StatusMessageType.Error, true);
return null;
}
2022-08-02 15:51:28 +00:00
// Fix SU10+ bug where pop out window after separation is huge
2022-08-18 14:20:12 +00:00
if (i > 1)
WindowActionManager.MoveWindow(handle, -8, 0, 800, 600);
2022-08-02 15:51:28 +00:00
2022-07-23 19:23:32 +00:00
var panel = new PanelConfig();
panel.PanelHandle = handle;
panel.PanelType = PanelType.CustomPopout;
panel.PanelIndex = i;
panel.PanelName = $"Panel{i}";
panel.Top = (i - 1) * 30;
panel.Left = (i - 1) * 30;
panel.Width = 800;
panel.Height = 600;
panels.Add(panel);
PInvoke.SetWindowText(panel.PanelHandle, panel.PanelName + " (Custom)");
}
2022-08-18 14:20:12 +00:00
//Perform validation, make sure the number of pop out panels is equal to the number of selected panel
2022-07-23 19:23:32 +00:00
if (WindowActionManager.GetWindowsCountByPanelType(new List<PanelType>() { PanelType.CustomPopout }) != ActiveProfile.PanelSourceCoordinates.Count)
{
StatusMessageWriter.WriteMessage("Unable to pop out all panels. Please align all panel number circles with in-game panel locations.", StatusMessageType.Error, false);
return null;
}
return panels;
}
private void SeparatePanel(IntPtr hwnd)
{
// ToDo: Need to figure mouse click code to separate window
PInvoke.SetForegroundWindow(hwnd);
Thread.Sleep(500);
// Find the magnifying glass coordinate
var point = PanelAnalyzer.GetMagnifyingGlassIconCoordinate(hwnd);
InputEmulationManager.LeftClick(point.X, point.Y);
}
2022-09-11 13:50:04 +00:00
private List<PanelConfig> AddBuiltInPanels()
{
List<PanelConfig> builtinPanels = new List<PanelConfig>();
var panelHandles = WindowActionManager.GetWindowsByPanelType(new List<PanelType>() { PanelType.BuiltInPopout });
foreach (var panelHandle in panelHandles)
{
var rectangle = WindowActionManager.GetWindowRect(panelHandle);
var clientRectangle = WindowActionManager.GetClientRect(panelHandle);
builtinPanels.Add(new PanelConfig()
{
2022-09-11 13:50:04 +00:00
PanelIndex = -1,
PanelHandle = panelHandle,
PanelType = PanelType.BuiltInPopout,
PanelName = WindowActionManager.GetWindowCaption(panelHandle),
Top = rectangle.Top,
Left = rectangle.Left,
Width = clientRectangle.Width,
Height = clientRectangle.Height
});
}
return builtinPanels.Count == 0 ? null : builtinPanels;
}
2022-07-23 19:23:32 +00:00
private List<PanelConfig> AddMsfsTouchPanels(int panelIndex)
{
List<PanelConfig> touchPanels = new List<PanelConfig>();
if (AppSetting.TouchPanelSettings.EnableTouchPanelIntegration)
{
TouchPanelOrchestrator.LoadPlaneProfiles();
if (TouchPanelOrchestrator.PlaneProfiles == null)
return null;
// Find all selected panels
var panelConfigs = TouchPanelOrchestrator.PlaneProfiles.SelectMany(p => p.Panels.Where(c => c.IsSelected));
foreach (var panelConfig in panelConfigs)
{
var caption = $"{panelConfig.Name} (Touch Panel)";
// Change width and height to 1080p aspect ratio
double aspectRatio = 1;
if (panelConfig.Width > 1920)
{
aspectRatio = Convert.ToDouble(1920) / panelConfig.Width;
panelConfig.Width = 1920; // there are hidden padding to make it to 1920
panelConfig.Height = Convert.ToInt32(panelConfig.Height * aspectRatio);
}
OnTouchPanelOpened?.Invoke(this, new TouchPanelOpenEventArg() { PlaneId = panelConfig.PlaneId, PanelId = panelConfig.PanelId, Caption = caption, Width = panelConfig.Width, Height = panelConfig.Height });
// detect for a max of 5 seconds
int tryCount = 0;
while (tryCount < 10)
{
var touchPanelHandle = WindowActionManager.FindWindowByCaption(caption);
if (touchPanelHandle != IntPtr.Zero)
{
var dimension = WindowActionManager.GetWindowRect(touchPanelHandle);
var panelInfo = new PanelConfig
{
PanelIndex = panelIndex,
PanelHandle = touchPanelHandle,
PanelName = caption,
PanelType = PanelType.MSFSTouchPanel,
Top = dimension.Top,
Left = dimension.Left,
Width = panelConfig.Width,
Height = panelConfig.Height,
AlwaysOnTop = true // default to always on top
};
touchPanels.Add(panelInfo);
break;
}
Thread.Sleep(500);
tryCount++;
}
if (tryCount == 10)
return null;
panelIndex++;
}
}
return touchPanels.Count == 0 ? null : touchPanels;
2022-07-23 19:23:32 +00:00
}
private void LoadAndApplyPanelConfigs(List<PanelConfig> panelResults)
{
2022-09-11 13:50:04 +00:00
ActiveProfile.PanelConfigs.ToList().ForEach(p => p.PanelHandle = IntPtr.Zero);
2022-07-23 19:23:32 +00:00
Parallel.ForEach(panelResults, panel =>
{
// Something is wrong here where panel has no window handle
if (panel.PanelHandle == IntPtr.Zero)
return;
PanelConfig savedPanelConfig = null;
if (panel.PanelType == PanelType.CustomPopout || panel.PanelType == PanelType.MSFSTouchPanel)
savedPanelConfig = ActiveProfile.PanelConfigs.FirstOrDefault(s => s.PanelIndex == panel.PanelIndex);
else if (panel.PanelType == PanelType.BuiltInPopout)
savedPanelConfig = ActiveProfile.PanelConfigs.FirstOrDefault(s => s.PanelName == panel.PanelName);
2022-09-11 13:50:04 +00:00
if (savedPanelConfig == null) return;
2022-07-23 19:23:32 +00:00
2022-09-11 13:50:04 +00:00
// Assign window handle to panel config
savedPanelConfig.PanelHandle = panel.PanelHandle;
2022-07-23 19:23:32 +00:00
// Apply panel name
2022-09-11 13:50:04 +00:00
if (savedPanelConfig.PanelType == PanelType.CustomPopout)
2022-07-23 19:23:32 +00:00
{
2022-09-11 13:50:04 +00:00
var caption = savedPanelConfig.PanelName + " (Custom)";
PInvoke.SetWindowText(savedPanelConfig.PanelHandle, caption);
2022-07-23 19:23:32 +00:00
Thread.Sleep(500);
}
// Apply locations
2022-09-11 13:50:04 +00:00
if (savedPanelConfig.Width != 0 && savedPanelConfig.Height != 0)
2022-07-23 19:23:32 +00:00
{
2022-09-11 13:50:04 +00:00
PInvoke.ShowWindow(savedPanelConfig.PanelHandle, PInvokeConstant.SW_RESTORE);
2022-07-23 19:23:32 +00:00
Thread.Sleep(250);
2022-09-11 13:50:04 +00:00
WindowActionManager.MoveWindow(savedPanelConfig.PanelHandle, savedPanelConfig.Left, savedPanelConfig.Top, savedPanelConfig.Width, savedPanelConfig.Height);
2022-07-23 19:23:32 +00:00
Thread.Sleep(1000);
}
// Apply window size again to overcome a bug in MSFS that when moving panel between monitors, panel automatic resize for no reason
2022-09-11 13:50:04 +00:00
if (savedPanelConfig.PanelType == PanelType.BuiltInPopout)
{
Thread.Sleep(2000); // Overcome GTN750 bug
2022-09-11 13:50:04 +00:00
WindowActionManager.MoveWindow(savedPanelConfig.PanelHandle, savedPanelConfig.Left, savedPanelConfig.Top, savedPanelConfig.Width, savedPanelConfig.Height);
Thread.Sleep(1000);
}
2022-09-11 13:50:04 +00:00
if (!savedPanelConfig.FullScreen)
2022-07-23 19:23:32 +00:00
{
// Apply always on top
2022-09-11 13:50:04 +00:00
if (savedPanelConfig.AlwaysOnTop)
2022-07-23 19:23:32 +00:00
{
2022-09-11 13:50:04 +00:00
WindowActionManager.ApplyAlwaysOnTop(savedPanelConfig.PanelHandle, savedPanelConfig.PanelType, true, new Rectangle(savedPanelConfig.Left, savedPanelConfig.Top, savedPanelConfig.Width, savedPanelConfig.Height));
2022-07-23 19:23:32 +00:00
Thread.Sleep(1000);
}
// Apply hide title bar
2022-09-11 13:50:04 +00:00
if (savedPanelConfig.HideTitlebar)
WindowActionManager.ApplyHidePanelTitleBar(savedPanelConfig.PanelHandle, true);
2022-07-23 19:23:32 +00:00
}
2022-09-11 13:50:04 +00:00
PInvoke.ShowWindow(savedPanelConfig.PanelHandle, PInvokeConstant.SW_RESTORE);
2022-07-23 19:23:32 +00:00
});
2022-09-11 13:50:04 +00:00
// If profile is unlocked, add any new panel into profile
if (!ActiveProfile.IsLocked)
{
var isAdded = false;
panelResults.ForEach(panel =>
{
if (panel.PanelType == PanelType.BuiltInPopout && !ActiveProfile.PanelConfigs.Any(s => s.PanelName == panel.PanelName))
{
ActiveProfile.PanelConfigs.Add(panel);
isAdded = true;
}
else if (panel.PanelType == PanelType.CustomPopout && !ActiveProfile.PanelConfigs.Any(s => s.PanelIndex == panel.PanelIndex))
{
ActiveProfile.PanelConfigs.Add(panel);
isAdded = true;
}
});
if (isAdded)
ProfileData.WriteProfiles();
}
2022-07-23 19:23:32 +00:00
// Apply full screen (cannot combine with always on top or hide title bar)
// Cannot run in parallel process
2022-09-11 13:50:04 +00:00
ActiveProfile.PanelConfigs.ToList().ForEach(panel =>
2022-07-23 19:23:32 +00:00
{
if (panel.FullScreen && (!panel.AlwaysOnTop && !panel.HideTitlebar))
{
InputEmulationManager.ToggleFullScreenPanel(panel.PanelHandle);
Thread.Sleep(250);
2022-09-11 13:50:04 +00:00
// Set full screen mode panel coordinate
var windowRectangle = WindowActionManager.GetWindowRect(panel.PanelHandle);
var clientRectangle = WindowActionManager.GetClientRect(panel.PanelHandle);
panel.FullScreenLeft = windowRectangle.Left;
panel.FullScreenTop = windowRectangle.Top;
panel.FullScreenWidth = clientRectangle.Width;
panel.FullScreenHeight = clientRectangle.Height;
2022-07-23 19:23:32 +00:00
}
});
}
private void ReturnToAfterPopOutCameraView()
{
if (!AppSetting.AfterPopOutCameraView.EnableReturnToCameraView)
return;
switch (AppSetting.AfterPopOutCameraView.CameraView)
{
case AfterPopOutCameraViewType.CockpitCenterView:
InputEmulationManager.CenterView();
break;
case AfterPopOutCameraViewType.CustomCameraView:
InputEmulationManager.LoadCustomView(AppSetting.AfterPopOutCameraView.CustomCameraKeyBinding);
break;
}
}
}
}