Improve profile selection to remove special handling of IsDesktop

This commit is contained in:
Kamil Trzciński 2022-11-28 23:43:46 +01:00
parent 44e5751d75
commit 6ea0329cbb
8 changed files with 84 additions and 89 deletions

View file

@ -17,35 +17,32 @@ namespace SteamController
public List<Profiles.Profile> Profiles { get; } = new List<Profiles.Profile>();
public List<Managers.Manager> Managers { get; } = new List<Managers.Manager>();
private List<Profiles.Profile>? 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<Profiles.Profile> 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<Profiles.Profile> 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();
}
}
}

View file

@ -10,7 +10,8 @@ namespace SteamController
{
var items = new List<string>();
if (DesktopMode)
var profile = CurrentProfile;
if (profile?.IsDesktop ?? false)
items.Add("[DESKTOP]");
else
items.Add("[CONTROLLER]");

View file

@ -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";
}

View file

@ -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();
}
}
}

View file

@ -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;

View file

@ -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;
}

View file

@ -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)

View file

@ -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