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

289 lines
9.2 KiB
C#
Raw 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
{
public event PropertyChangedEventHandler ActiveProfileChanged;
public ProfileData()
{
2023-07-12 22:41:31 +00:00
Profiles = new SortedObservableCollection<UserProfile>();
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
public SortedObservableCollection<UserProfile> Profiles { get; private set; }
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]
internal 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
{
2023-07-12 22:41:31 +00:00
var newProfile = new UserProfile();
newProfile.Name = profileName;
newProfile.ProfileChanged += (sender, e) => WriteProfiles();
Profiles.Add(newProfile);
SetActiveProfile(newProfile.Id);
ProfileDataManager.WriteProfiles(Profiles);
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;
var newProfile = new UserProfile();
newProfile.Name = profileName;
foreach (var copiedPanelConfig in copiedProfile.PanelConfigs)
{
var copied = copiedPanelConfig.Copy();
copied.Id = Guid.NewGuid();
copied.PanelHandle = IntPtr.MaxValue;
copied.IsEditingPanel = false;
newProfile.PanelConfigs.Add(copied);
}
newProfile.ProfileSetting = copiedProfile.ProfileSetting.Copy();
newProfile.MsfsGameWindowConfig = copiedProfile.MsfsGameWindowConfig.Copy();
newProfile.ProfileChanged += (sender, e) => WriteProfiles();
Profiles.Add(newProfile);
SetActiveProfile(newProfile.Id);
ProfileDataManager.WriteProfiles(Profiles);
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;
2023-07-12 22:41:31 +00:00
var boundProfile = Profiles.FirstOrDefault(p => p.AircraftBindings.Any(p => p == aircraft));
if (boundProfile != null)
return;
ActiveProfile.AircraftBindings.Add(aircraft);
ProfileDataManager.WriteProfiles(Profiles);
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);
ProfileDataManager.WriteProfiles(Profiles);
2022-07-23 19:23:32 +00:00
RefreshProfile();
}
public void ReadProfiles()
{
2023-07-12 22:41:31 +00:00
Profiles = new SortedObservableCollection<UserProfile>(ProfileDataManager.ReadProfiles());
Profiles.ToList().ForEach(p => p.ProfileChanged += (sender, e) => WriteProfiles());
// Detect profiles collection changes
Profiles.CollectionChanged += (sender, e) =>
{
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 ... ");
ProfileDataManager.WriteProfiles(Profiles);
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
2023-07-12 22:41:31 +00:00
if (ActiveProfile == null && Profiles.Count > 0)
{
ActiveProfile = Profiles.First();
AppSettingDataRef.ApplicationSetting.SystemSetting.LastUsedProfileId = ActiveProfile.Id;
}
else if (ActiveProfile == null && Profiles.Count == 0)
{
AppSettingDataRef.ApplicationSetting.SystemSetting.LastUsedProfileId = Guid.Empty;
return;
}
else
{
AppSettingDataRef.ApplicationSetting.SystemSetting.LastUsedProfileId = ActiveProfile.Id;
}
}
ResetActiveProfile();
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
// Set active profile flag, this is used only for MVVM binding
if (Profiles.Count > 0)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
Profiles.ToList().ForEach(p => p.IsActive = false);
2022-07-23 19:23:32 +00:00
ActiveProfile.IsActive = true;
}
2023-07-12 22:41:31 +00:00
}
public void SetActiveProfile(int profileIndex)
{
if (profileIndex == -1)
SetActiveProfile(Guid.Empty);
2022-07-23 19:23:32 +00:00
else
2023-07-12 22:41:31 +00:00
SetActiveProfile(Profiles[profileIndex].Id);
2022-07-23 19:23:32 +00:00
ActiveProfileChanged?.Invoke(this, null);
}
2023-07-12 22:41:31 +00:00
public UserProfile ActiveProfile { get; private set; }
2022-07-23 19:23:32 +00:00
public bool HasActiveProfile { get { return ActiveProfile != null; } }
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
if (profileIndex == -1)
SetActiveProfile(Guid.Empty);
else
SetActiveProfile(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();
if (ActiveProfile == null)
return;
ActiveProfile.CurrentMoveResizePanelId = Guid.Empty;
ActiveProfile.IsEditingPanelSource = false;
ActiveProfile.IsPoppedOut = false;
foreach (var panelConfig in ActiveProfile.PanelConfigs)
2022-07-23 19:23:32 +00:00
{
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;
var msfsGameWindowConfig = WindowsAgent.WindowActionManager.GetMsfsGameWindowLocation();
if (msfsGameWindowConfig.IsValid)
{
ActiveProfile.MsfsGameWindowConfig = msfsGameWindowConfig;
WriteProfiles();
}
}
2022-07-23 19:23:32 +00:00
}
}