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/Orchestration/ProfileData.cs

289 lines
10 KiB
C#
Raw Permalink Normal View History

2023-07-12 22:41:31 +00:00
using MSFSPopoutPanelManager.DomainModel.Profile;
using MSFSPopoutPanelManager.Shared;
using MSFSPopoutPanelManager.WindowsAgent;
using System;
2022-07-23 19:23:32 +00:00
using System.ComponentModel;
2023-07-12 22:41:31 +00:00
using System.Diagnostics;
2022-07-23 19:23:32 +00:00
using System.Linq;
namespace MSFSPopoutPanelManager.Orchestration
{
public class ProfileData : ObservableObject
{
2024-02-28 02:44:21 +00:00
public event PropertyChangedEventHandler OnActiveProfileChanged;
2024-03-01 11:28:09 +00:00
public event EventHandler<bool> OnUseFloatingPanelChanged;
2022-07-23 19:23:32 +00:00
2024-02-28 02:44:21 +00:00
public SortedObservableCollection<UserProfile> Profiles { get; private set; } = new();
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
[IgnorePropertyChanged]
internal FlightSimData FlightSimDataRef { private get; set; }
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
[IgnorePropertyChanged]
2024-07-28 00:12:07 +00:00
public AppSettingData AppSettingDataRef { private get; set; }
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
public void AddProfile(string profileName)
2022-07-23 19:23:32 +00:00
{
2024-02-28 02:44:21 +00:00
var newProfile = new UserProfile { Name = profileName, IsUsedLegacyCameraSystem = false };
newProfile.OnProfileChanged += (_, _) => WriteProfiles();
2023-07-12 22:41:31 +00:00
Profiles.Add(newProfile);
SetActiveProfile(newProfile.Id);
2024-07-28 00:12:07 +00:00
ProfileDataManager.WriteProfiles(Profiles, AppSettingDataRef.ApplicationSetting.GeneralSetting.UseApplicationDataPath);
2023-07-12 22:41:31 +00:00
AppSettingDataRef.ApplicationSetting.SystemSetting.LastUsedProfileId = newProfile.Id;
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
public void AddProfile(string profileName, UserProfile copiedProfile)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
if (copiedProfile == null)
return;
2024-02-28 02:44:21 +00:00
var newProfile = new UserProfile { Name = profileName};
2023-07-12 22:41:31 +00:00
foreach (var copiedPanelConfig in copiedProfile.PanelConfigs)
{
var copied = copiedPanelConfig.Copy();
copied.Id = Guid.NewGuid();
copied.PanelHandle = IntPtr.MaxValue;
copied.IsEditingPanel = false;
2024-02-28 02:44:21 +00:00
copied.IsSelectedPanelSource = false;
copied.IsShownPanelSource = false;
2023-07-12 22:41:31 +00:00
newProfile.PanelConfigs.Add(copied);
}
newProfile.ProfileSetting = copiedProfile.ProfileSetting.Copy();
newProfile.MsfsGameWindowConfig = copiedProfile.MsfsGameWindowConfig.Copy();
2024-02-28 02:44:21 +00:00
newProfile.PanelSourceCockpitZoomFactor = copiedProfile.PanelSourceCockpitZoomFactor;
newProfile.IsUsedLegacyCameraSystem = copiedProfile.IsUsedLegacyCameraSystem;
newProfile.OnProfileChanged += (_, _) => WriteProfiles();
2023-07-12 22:41:31 +00:00
Profiles.Add(newProfile);
SetActiveProfile(newProfile.Id);
2024-07-28 00:12:07 +00:00
ProfileDataManager.WriteProfiles(Profiles, AppSettingDataRef.ApplicationSetting.GeneralSetting.UseApplicationDataPath);
2023-07-12 22:41:31 +00:00
AppSettingDataRef.ApplicationSetting.SystemSetting.LastUsedProfileId = newProfile.Id;
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
public bool DeleteActiveProfile()
2022-07-23 19:23:32 +00:00
{
if (ActiveProfile == null)
return false;
2023-07-12 22:41:31 +00:00
var activeProfileIndex = Profiles.IndexOf(ActiveProfile);
Profiles.Remove(ActiveProfile);
if (activeProfileIndex == 0 && Profiles.Count == 0)
SetActiveProfile(-1);
else if (activeProfileIndex == Profiles.Count)
SetActiveProfile(0);
else
SetActiveProfile(activeProfileIndex);
2022-07-23 20:04:06 +00:00
return true;
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
public void AddProfileBinding(string aircraft)
2022-07-23 19:23:32 +00:00
{
if (ActiveProfile == null)
return;
2024-02-28 02:44:21 +00:00
var boundProfile = Profiles.FirstOrDefault(p => p.AircraftBindings.Any(a => a == aircraft));
2023-07-12 22:41:31 +00:00
if (boundProfile != null)
return;
ActiveProfile.AircraftBindings.Add(aircraft);
2024-07-28 00:12:07 +00:00
ProfileDataManager.WriteProfiles(Profiles, AppSettingDataRef.ApplicationSetting.GeneralSetting.UseApplicationDataPath);
2022-07-23 19:23:32 +00:00
RefreshProfile();
}
2023-07-12 22:41:31 +00:00
public void DeleteProfileBinding(string aircraft)
2022-07-23 19:23:32 +00:00
{
if (ActiveProfile == null)
return;
2023-07-12 22:41:31 +00:00
ActiveProfile.AircraftBindings.Remove(aircraft);
2024-07-28 00:12:07 +00:00
ProfileDataManager.WriteProfiles(Profiles, AppSettingDataRef.ApplicationSetting.GeneralSetting.UseApplicationDataPath);
2022-07-23 19:23:32 +00:00
RefreshProfile();
}
public void ReadProfiles()
{
2024-07-28 00:12:07 +00:00
Profiles = new SortedObservableCollection<UserProfile>(ProfileDataManager.ReadProfiles(AppSettingDataRef.ApplicationSetting.GeneralSetting.UseApplicationDataPath));
2024-02-28 02:44:21 +00:00
Profiles.ToList().ForEach(p => p.OnProfileChanged += (_, _) => WriteProfiles());
2023-07-12 22:41:31 +00:00
// Detect profiles collection changes
2024-02-28 02:44:21 +00:00
Profiles.CollectionChanged += (_, e) =>
2023-07-12 22:41:31 +00:00
{
switch (e.Action)
{
case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
WriteProfiles();
break;
}
};
2022-07-23 19:23:32 +00:00
}
public void WriteProfiles()
{
2023-07-12 22:41:31 +00:00
Debug.WriteLine("Saving Data ... ");
2024-07-28 00:12:07 +00:00
ProfileDataManager.WriteProfiles(Profiles, AppSettingDataRef.ApplicationSetting.GeneralSetting.UseApplicationDataPath);
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
public void SetActiveProfile(Guid id)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
StatusMessageWriter.ClearMessage();
if (id == Guid.Empty && Profiles.Count == 0)
{
2022-07-23 19:23:32 +00:00
ActiveProfile = null;
2023-07-12 22:41:31 +00:00
AppSettingDataRef.ApplicationSetting.SystemSetting.LastUsedProfileId = Guid.Empty;
}
else if (id == Guid.Empty && Profiles.Count > 0)
{
ActiveProfile = Profiles.First();
AppSettingDataRef.ApplicationSetting.SystemSetting.LastUsedProfileId = ActiveProfile.Id;
}
2022-07-23 19:23:32 +00:00
else
2023-07-12 22:41:31 +00:00
{
ActiveProfile = Profiles.FirstOrDefault(p => p.Id == id);
2022-07-23 19:23:32 +00:00
2024-02-28 02:44:21 +00:00
switch (ActiveProfile)
2023-07-12 22:41:31 +00:00
{
2024-02-28 02:44:21 +00:00
case null when Profiles.Count > 0:
ActiveProfile = Profiles.First();
AppSettingDataRef.ApplicationSetting.SystemSetting.LastUsedProfileId = ActiveProfile.Id;
break;
case null when Profiles.Count == 0:
AppSettingDataRef.ApplicationSetting.SystemSetting.LastUsedProfileId = Guid.Empty;
return;
default:
{
if(ActiveProfile != null)
AppSettingDataRef.ApplicationSetting.SystemSetting.LastUsedProfileId = ActiveProfile.Id;
break;
}
2023-07-12 22:41:31 +00:00
}
}
ResetActiveProfile();
2022-07-23 19:23:32 +00:00
2024-02-28 02:44:21 +00:00
// Set active profile flag, this is used only for UI binding
if (Profiles.Count <= 0)
return;
Profiles.ToList().ForEach(p => p.IsActive = false);
2024-03-01 11:28:09 +00:00
if (ActiveProfile != null)
{
2022-07-23 19:23:32 +00:00
ActiveProfile.IsActive = true;
2024-03-01 11:28:09 +00:00
ActiveProfile.OnUseFloatingPanelChanged += (sender, e) => OnUseFloatingPanelChanged?.Invoke(sender, e);
}
OnActiveProfileChanged?.Invoke(this, new PropertyChangedEventArgs(string.Empty));
2023-07-12 22:41:31 +00:00
}
public void SetActiveProfile(int profileIndex)
{
2024-02-28 02:44:21 +00:00
SetActiveProfile(profileIndex == -1 ? Guid.Empty : Profiles[profileIndex].Id);
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
public UserProfile ActiveProfile { get; private set; }
2022-07-23 19:23:32 +00:00
public bool IsAircraftBoundToProfile
{
get
{
if (ActiveProfile == null)
return false;
2023-07-12 22:41:31 +00:00
return ActiveProfile.AircraftBindings.Any(p => p == FlightSimDataRef.AircraftName);
2022-07-23 19:23:32 +00:00
}
}
public bool IsAllowedAddAircraftBinding
{
get
{
2023-07-12 22:41:31 +00:00
if (ActiveProfile == null || !FlightSimDataRef.HasAircraftName)
return true;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
var uProfile = Profiles.FirstOrDefault(u => u.AircraftBindings.Any(p => p == FlightSimDataRef.AircraftName));
if (uProfile != null && uProfile != ActiveProfile)
2022-07-25 12:40:21 +00:00
return false;
2023-07-12 22:41:31 +00:00
return true;
2022-07-23 19:23:32 +00:00
}
}
public void RefreshProfile()
{
2023-07-12 22:41:31 +00:00
int profileIndex;
2022-07-23 19:23:32 +00:00
if (ActiveProfile == null)
2023-07-12 22:41:31 +00:00
profileIndex = -1;
2022-07-23 19:23:32 +00:00
else
2023-07-12 22:41:31 +00:00
profileIndex = Profiles.IndexOf(ActiveProfile);
2022-07-23 19:23:32 +00:00
ActiveProfile = null;
2023-07-12 22:41:31 +00:00
2024-02-28 02:44:21 +00:00
SetActiveProfile(profileIndex == -1 ? Guid.Empty : Profiles[profileIndex].Id);
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
public void ResetActiveProfile()
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
InputHookManager.EndMouseHook();
//InputHookManager.EndKeyboardHook();
2023-07-12 22:41:31 +00:00
if (ActiveProfile == null)
return;
ActiveProfile.CurrentMoveResizePanelId = Guid.Empty;
ActiveProfile.IsEditingPanelSource = false;
2024-02-28 02:44:21 +00:00
ActiveProfile.IsSelectingPanelSource = false;
2023-07-12 22:41:31 +00:00
ActiveProfile.IsPoppedOut = false;
2024-02-28 02:44:21 +00:00
2023-07-12 22:41:31 +00:00
foreach (var panelConfig in ActiveProfile.PanelConfigs)
2022-07-23 19:23:32 +00:00
{
2024-02-28 02:44:21 +00:00
panelConfig.IsSelectedPanelSource = false;
2023-07-12 22:41:31 +00:00
panelConfig.IsEditingPanel = false;
panelConfig.PanelHandle = IntPtr.MaxValue;
2022-07-23 19:23:32 +00:00
}
}
2022-08-05 03:43:51 +00:00
2023-07-12 22:41:31 +00:00
public void AutoSwitchProfile()
2022-08-05 03:43:51 +00:00
{
2023-07-12 22:41:31 +00:00
// Automatic switching of active profile when SimConnect active aircraft changes
if (Profiles != null && !string.IsNullOrEmpty(FlightSimDataRef.AircraftName))
2022-08-05 03:43:51 +00:00
{
2023-07-12 22:41:31 +00:00
var matchedProfile = Profiles.FirstOrDefault(p => p.AircraftBindings.Any(t => t == FlightSimDataRef.AircraftName));
2022-08-19 16:03:12 +00:00
if (matchedProfile != null)
{
2023-07-12 22:41:31 +00:00
SetActiveProfile(matchedProfile.Id);
2022-08-13 06:14:49 +00:00
RefreshProfile();
2022-08-05 03:43:51 +00:00
}
}
}
2022-08-13 06:14:49 +00:00
public void SaveMsfsGameWindowConfig()
{
if (ActiveProfile == null)
return;
2024-02-28 02:44:21 +00:00
var msfsGameWindowConfig = WindowActionManager.GetMsfsGameWindowLocation();
if (msfsGameWindowConfig.IsValid)
{
ActiveProfile.MsfsGameWindowConfig = msfsGameWindowConfig;
WriteProfiles();
}
}
2022-07-23 19:23:32 +00:00
}
}