1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-22 05:40:11 +00:00
msfs-popout-panel-manager/Provider/PanelSelectionManager.cs
2022-07-08 00:01:38 -04:00

100 lines
3.8 KiB
C#

using MSFSPopoutPanelManager.Model;
using MSFSPopoutPanelManager.Shared;
using System;
using System.Collections.Generic;
namespace MSFSPopoutPanelManager.Provider
{
public class PanelSelectionManager
{
private UserProfileManager _userProfileManager;
private int _panelIndex;
private List<PanelSourceCoordinate> _panelCoordinates;
public event EventHandler OnPanelSelectionCompleted;
public event EventHandler<EventArgs<PanelSourceCoordinate>> OnPanelLocationAdded;
public event EventHandler OnPanelLocationRemoved;
public event EventHandler OnAllPanelLocationsRemoved;
public UserProfile UserProfile { get; set; }
public AppSetting AppSetting { get; set; }
public PanelSelectionManager(UserProfileManager userProfileManager)
{
_userProfileManager = userProfileManager;
InputHookManager.OnPanelSelectionAdded += HandleOnPanelSelectionAdded;
InputHookManager.OnPanelSelectionRemoved += HandleOnPanelSelectionRemoved;
InputHookManager.OnPanelSelectionCompleted += HandleOnPanelSelectionCompleted;
}
public void Start()
{
_panelIndex = 1;
_panelCoordinates = new List<PanelSourceCoordinate>();
//ShowPanelLocationOverlay(_panelCoordinates, true);
InputHookManager.SubscribeToPanelSelectionEvent = true;
InputHookManager.StartHook();
}
public void ShowPanelLocationOverlay(List<PanelSourceCoordinate> panelCoordinates, bool show)
{
// close all overlays
OnAllPanelLocationsRemoved?.Invoke(this, null);
if (show && panelCoordinates.Count > 0)
{
foreach (var coor in panelCoordinates)
{
var panelSourceCoordinate = new PanelSourceCoordinate() { PanelIndex = coor.PanelIndex, X = coor.X, Y = coor.Y };
OnPanelLocationAdded?.Invoke(this, new EventArgs<PanelSourceCoordinate>(panelSourceCoordinate));
}
}
}
private void HandleOnPanelSelectionAdded(object sender, System.Drawing.Point e)
{
var newPanelCoordinates = new PanelSourceCoordinate() { PanelIndex = _panelIndex, X = e.X, Y = e.Y };
_panelCoordinates.Add(newPanelCoordinates);
_panelIndex++;
OnPanelLocationAdded?.Invoke(this, new EventArgs<PanelSourceCoordinate>(newPanelCoordinates));
}
private void HandleOnPanelSelectionRemoved(object sender, EventArgs e)
{
if (_panelCoordinates.Count > 0)
{
_panelCoordinates.RemoveAt(_panelCoordinates.Count - 1);
_panelIndex--;
OnPanelLocationRemoved?.Invoke(this, null);
}
}
private void HandleOnPanelSelectionCompleted(object sender, EventArgs e)
{
// If enable, save the current viewport into custom view by Ctrl-Alt-0
if (AppSetting.UseAutoPanning)
{
var simualatorProcess = DiagnosticManager.GetSimulatorProcess();
if (simualatorProcess != null)
{
InputEmulationManager.SaveCustomView(simualatorProcess.Handle, AppSetting.AutoPanningKeyBinding);
}
}
// Assign and save panel coordinates to active profile
UserProfile.PanelSourceCoordinates.Clear();
_panelCoordinates.ForEach(c => UserProfile.PanelSourceCoordinates.Add(c));
UserProfile.PanelConfigs.Clear();
UserProfile.IsLocked = false;
_userProfileManager.WriteUserProfiles();
InputHookManager.SubscribeToPanelSelectionEvent = false;
OnPanelSelectionCompleted?.Invoke(this, null);
}
}
}