diff --git a/SteamController/Context.cs b/SteamController/Context.cs index ebad5f1..1723422 100644 --- a/SteamController/Context.cs +++ b/SteamController/Context.cs @@ -17,35 +17,32 @@ namespace SteamController public List Profiles { get; } = new List(); public List Managers { get; } = new List(); - private List? orderedProfiles; + private int selectedProfile; public bool RequestEnable { get; set; } = true; - public bool RequestDesktopMode { get; set; } = true; public bool SteamUsesX360Controller { get; set; } = false; public bool SteamUsesSteamInput { get; set; } = false; public event Action ProfileChanged; + public Action? SelectDefault; public bool Enabled { get { return RequestEnable; } } - public bool DesktopMode + public Profiles.Profile? CurrentProfile { get { - return RequestDesktopMode || !X360.Valid || !Mouse.Valid; - } - } + for (int i = 0; i < Profiles.Count; i++) + { + var profile = Profiles[(selectedProfile + i) % Profiles.Count]; + if (profile.Selected(this)) + return profile; + } - public List OrderedProfiles - { - get - { - if (orderedProfiles == null) - orderedProfiles = Profiles.ToList(); - return orderedProfiles; + return null; } } @@ -84,19 +81,6 @@ namespace SteamController } } - public Profiles.Profile? GetCurrentProfile() - { - foreach (var profile in OrderedProfiles) - { - if (profile.Selected(this)) - { - return profile; - } - } - - return null; - } - public bool Update() { Steam.BeforeUpdate(); @@ -106,11 +90,9 @@ namespace SteamController try { - var profile = GetCurrentProfile(); + var profile = CurrentProfile; if (profile is not null) - { profile.Run(this); - } return true; } @@ -128,62 +110,71 @@ namespace SteamController } } - private bool SelectProfile(Profiles.Profile profile) + public bool SelectProfile(String name) { lock (this) { - var list = OrderedProfiles.ToList(); - if (!list.Remove(profile)) - return false; - list.Insert(0, profile); - orderedProfiles = list; - RequestDesktopMode = profile.IsDesktop; - - if (profile.Selected(this)) + for (int i = 0; i < Profiles.Count; i++) { - ProfileChanged(profile); + var profile = Profiles[i]; + if (profile.Name != name) + continue; + if (!profile.Selected(this)) + continue; + + if (i != selectedProfile) + { + selectedProfile = i; + ProfileChanged(profile); + } return true; } - return false; } - } - - public bool SelectProfile(String name) - { - var profile = Profiles.Find((profile) => profile.Name == name); - if (profile is null) - return false; - - return SelectProfile(profile); - } - - public bool SelectNext() - { - var profile = OrderedProfiles. - Where((profile) => profile.Selected(this)). - Skip(1). - FirstOrDefault(); - - if (profile is not null) - return SelectProfile(profile); return false; } - public void ToggleDesktopMode(bool? forceState = null) + public void SelectController() + { + var current = CurrentProfile; + if (current is null) + return; + if (current.IsDesktop) + SelectNext(); + } + + public bool SelectNext() { lock (this) { - var oldProfile = GetCurrentProfile(); - if (forceState is null) - RequestDesktopMode = !RequestDesktopMode; - else - RequestDesktopMode = forceState.Value; + // Update selectedProfile index + var current = CurrentProfile; + if (current is null) + return false; + selectedProfile = Profiles.IndexOf(current); - var newProfile = GetCurrentProfile(); - if (oldProfile != newProfile && newProfile is not null) - ProfileChanged(newProfile); + for (int i = 1; i < Profiles.Count; i++) + { + var idx = (selectedProfile + i) % Profiles.Count; + var profile = Profiles[idx]; + if (profile.IsDesktop) + continue; + if (!profile.Selected(this)) + continue; + + selectedProfile = idx; + ProfileChanged(profile); + return true; + } } + + return false; + } + + public void BackToDefault() + { + if (SelectDefault is not null) + SelectDefault(); } } } diff --git a/SteamController/ContextDebug.cs b/SteamController/ContextDebug.cs index 16dbe67..134f842 100644 --- a/SteamController/ContextDebug.cs +++ b/SteamController/ContextDebug.cs @@ -10,7 +10,8 @@ namespace SteamController { var items = new List(); - if (DesktopMode) + var profile = CurrentProfile; + if (profile?.IsDesktop ?? false) items.Add("[DESKTOP]"); else items.Add("[CONTROLLER]"); diff --git a/SteamController/Controller.cs b/SteamController/Controller.cs index bc91100..726bb37 100644 --- a/SteamController/Controller.cs +++ b/SteamController/Controller.cs @@ -47,7 +47,6 @@ namespace SteamController var contextMenu = new ContextMenuStrip(components); var enabledItem = new ToolStripMenuItem("&Enabled"); - enabledItem.Checked = context.RequestEnable; enabledItem.Click += delegate { context.RequestEnable = !context.RequestEnable; }; contextMenu.Opening += delegate { enabledItem.Checked = context.RequestEnable; }; contextMenu.Items.Add(enabledItem); @@ -60,7 +59,7 @@ namespace SteamController var profileItem = new ToolStripMenuItem(profile.Name); profileItem.Click += delegate { context.SelectProfile(profile.Name); }; - contextMenu.Opening += delegate { profileItem.Checked = context.GetCurrentProfile() == profile; }; + contextMenu.Opening += delegate { profileItem.Checked = context.CurrentProfile == profile; }; contextMenu.Items.Add(profileItem); } @@ -117,6 +116,12 @@ namespace SteamController contextStateUpdate.Enabled = true; contextStateUpdate.Tick += ContextStateUpdate_Tick; + context.SelectDefault = () => + { + context.SelectProfile(Settings.Default.StartupProfile); + }; + context.BackToDefault(); + context.ProfileChanged += (profile) => { #if false @@ -131,8 +136,6 @@ namespace SteamController stopwatch.Start(); - context.SelectProfile(Settings.Default.StartupProfile); - contextThread = new Thread(ContextState_Update); contextThread.Start(); } @@ -168,7 +171,7 @@ namespace SteamController sharedData.SetValue(new SteamControllerSetting() { - CurrentProfile = context.OrderedProfiles.FirstOrDefault((profile) => profile.Selected(context))?.Name ?? "", + CurrentProfile = context.CurrentProfile?.Name ?? "", SelectableProfiles = context.Profiles.Where((profile) => profile.Selected(context) || profile.Visible).JoinWithN((profile) => profile.Name), }); } @@ -178,6 +181,8 @@ namespace SteamController context.Tick(); SharedData_Update(); + var isDesktop = context.CurrentProfile?.IsDesktop ?? false; + if (!context.Mouse.Valid) { notifyIcon.Text = TitleWithVersion + ". Cannot send input."; @@ -192,22 +197,22 @@ namespace SteamController { if (context.SteamUsesSteamInput) { - notifyIcon.Icon = context.DesktopMode ? Resources.monitor_off : Resources.microsoft_xbox_controller_off; + notifyIcon.Icon = isDesktop ? Resources.monitor_off : Resources.microsoft_xbox_controller_off; notifyIcon.Text = TitleWithVersion + ". Steam uses Steam Input"; } else { - notifyIcon.Icon = context.DesktopMode ? Resources.monitor : Resources.microsoft_xbox_controller; + notifyIcon.Icon = isDesktop ? Resources.monitor : Resources.microsoft_xbox_controller; notifyIcon.Text = TitleWithVersion; } - var profile = context.GetCurrentProfile(); + var profile = context.CurrentProfile; if (profile is not null) notifyIcon.Text = TitleWithVersion + ". Profile: " + profile.Name; } else { - notifyIcon.Icon = context.DesktopMode ? Resources.monitor_off : Resources.microsoft_xbox_controller_off; + notifyIcon.Icon = isDesktop ? Resources.monitor_off : Resources.microsoft_xbox_controller_off; notifyIcon.Text = TitleWithVersion + ". Disabled"; } diff --git a/SteamController/Managers/ProcessManager.cs b/SteamController/Managers/ProcessManager.cs index 3cb91fe..42899d8 100644 --- a/SteamController/Managers/ProcessManager.cs +++ b/SteamController/Managers/ProcessManager.cs @@ -31,7 +31,7 @@ namespace SteamController.Managers if (!activated) { activated = true; - context.ToggleDesktopMode(false); + context.SelectController(); } } else @@ -39,7 +39,7 @@ namespace SteamController.Managers if (activated) { activated = false; - context.ToggleDesktopMode(true); + context.BackToDefault(); } } } diff --git a/SteamController/Managers/SteamManager.cs b/SteamController/Managers/SteamManager.cs index 930b650..55049a7 100644 --- a/SteamController/Managers/SteamManager.cs +++ b/SteamController/Managers/SteamManager.cs @@ -33,13 +33,13 @@ namespace SteamController.Managers Devices.Xbox360Controller.ProductID ) != true; - context.ToggleDesktopMode(false); + context.SelectController(); } else { context.SteamUsesSteamInput = false; context.SteamUsesX360Controller = false; - context.ToggleDesktopMode(true); + context.BackToDefault(); } lastState = usesController; diff --git a/SteamController/Profiles/DefaultShortcutsProfile.cs b/SteamController/Profiles/DefaultShortcutsProfile.cs index 98e6fc9..f8eb0ea 100644 --- a/SteamController/Profiles/DefaultShortcutsProfile.cs +++ b/SteamController/Profiles/DefaultShortcutsProfile.cs @@ -28,14 +28,12 @@ namespace SteamController.Profiles if (c.Steam.BtnOptions.HoldOnce(HoldToSwitchProfile, ShortcutConsumed)) { if (!c.SelectNext()) - { - c.ToggleDesktopMode(); - } + c.BackToDefault(); return Status.Done; } else if (c.Steam.BtnOptions.HoldChain(HoldToSwitchDesktop, ShortcutConsumed, "SwitchToDesktop")) { - c.ToggleDesktopMode(); + c.BackToDefault(); return Status.Done; } diff --git a/SteamController/Profiles/DesktopProfile.cs b/SteamController/Profiles/DesktopProfile.cs index f43337b..8ebc8ea 100644 --- a/SteamController/Profiles/DesktopProfile.cs +++ b/SteamController/Profiles/DesktopProfile.cs @@ -18,7 +18,7 @@ namespace SteamController.Profiles public override bool Selected(Context context) { - return context.Enabled && context.DesktopMode; + return context.Enabled; } public override Status Run(Context c) diff --git a/SteamController/Profiles/X360Profile.cs b/SteamController/Profiles/X360Profile.cs index 707ac0a..0e43cb6 100644 --- a/SteamController/Profiles/X360Profile.cs +++ b/SteamController/Profiles/X360Profile.cs @@ -6,7 +6,7 @@ namespace SteamController.Profiles { public override bool Selected(Context context) { - return context.Enabled && !context.DesktopMode && !context.SteamUsesSteamInput; + return context.Enabled && context.X360.Valid && context.Mouse.Valid && !context.SteamUsesSteamInput; } internal override ProfilesSettings.BackPanelSettings BackPanelSettings