From 8604e675687340bdd520eb82d47bfbde085b5d34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Tue, 29 Nov 2022 22:50:07 +0100 Subject: [PATCH] Add `ContextState` to compare all active states in Bulk --- SteamController/Context.cs | 16 ++++++++-- SteamController/Controller.cs | 2 +- SteamController/Managers/ProcessManager.cs | 2 +- SteamController/Managers/ProfileSwitcher.cs | 30 +++---------------- SteamController/Managers/SteamManager.cs | 12 ++++---- SteamController/Profiles/SteamProfile.cs | 2 +- .../Profiles/SteamWithShorcutsProfile.cs | 2 +- SteamController/Profiles/X360Profile.cs | 2 +- 8 files changed, 28 insertions(+), 40 deletions(-) diff --git a/SteamController/Context.cs b/SteamController/Context.cs index 853b45b..cd4283d 100644 --- a/SteamController/Context.cs +++ b/SteamController/Context.cs @@ -19,10 +19,20 @@ namespace SteamController private int selectedProfile; + public struct ContextState + { + public bool GameProcessRunning { get; set; } + public bool SteamUsesX360Controller { get; set; } + public bool SteamUsesSteamInput { get; set; } + + public bool IsActive + { + get { return GameProcessRunning || SteamUsesSteamInput || SteamUsesSteamInput; } + } + } + public bool RequestEnable { get; set; } = true; - public bool GameProcessRunning { get; set; } = false; - public bool SteamUsesX360Controller { get; set; } = false; - public bool SteamUsesSteamInput { get; set; } = false; + public ContextState State; public event Action ProfileChanged; public Action? SelectDefault; diff --git a/SteamController/Controller.cs b/SteamController/Controller.cs index 800e7c0..386b4f7 100644 --- a/SteamController/Controller.cs +++ b/SteamController/Controller.cs @@ -196,7 +196,7 @@ namespace SteamController } else if (context.Enabled) { - if (context.SteamUsesSteamInput) + if (context.State.SteamUsesSteamInput) { notifyIcon.Icon = isDesktop ? Resources.monitor_off : Resources.microsoft_xbox_controller_off; notifyIcon.Text = TitleWithVersion + ". Steam uses Steam Input"; diff --git a/SteamController/Managers/ProcessManager.cs b/SteamController/Managers/ProcessManager.cs index b2cfc08..6bd6502 100644 --- a/SteamController/Managers/ProcessManager.cs +++ b/SteamController/Managers/ProcessManager.cs @@ -23,7 +23,7 @@ namespace SteamController.Managers public override void Tick(Context context) { - context.GameProcessRunning = FindActivationProcess() is not null; + context.State.GameProcessRunning = FindActivationProcess() is not null; } } } diff --git a/SteamController/Managers/ProfileSwitcher.cs b/SteamController/Managers/ProfileSwitcher.cs index f4dd488..26e6d3c 100644 --- a/SteamController/Managers/ProfileSwitcher.cs +++ b/SteamController/Managers/ProfileSwitcher.cs @@ -5,41 +5,19 @@ namespace SteamController.Managers { public sealed class ProfileSwitcher : Manager { - [Flags] - private enum ActiveMode - { - None, - SteamInput = 1, - SteamX360 = 2, - OtherGame = 4 - } - - private ActiveMode wasActive; + private Context.ContextState wasState; public override void Tick(Context context) { - ActiveMode active = GetActiveMode(context); - if (wasActive == active) + if (wasState.Equals(context.State)) return; - if (active != ActiveMode.None) + if (context.State.IsActive) context.SelectController(); else context.BackToDefault(); - wasActive = active; - } - - private ActiveMode GetActiveMode(Context context) - { - ActiveMode mode = ActiveMode.None; - if (context.SteamUsesSteamInput) - mode |= ActiveMode.SteamInput; - if (context.SteamUsesX360Controller) - mode |= ActiveMode.SteamX360; - if (context.GameProcessRunning) - mode |= ActiveMode.OtherGame; - return mode; + wasState = context.State; } } } diff --git a/SteamController/Managers/SteamManager.cs b/SteamController/Managers/SteamManager.cs index e4f649e..7464f2e 100644 --- a/SteamController/Managers/SteamManager.cs +++ b/SteamController/Managers/SteamManager.cs @@ -11,8 +11,8 @@ namespace SteamController.Managers { if (!Settings.Default.EnableSteamDetection) { - context.SteamUsesSteamInput = false; - context.SteamUsesX360Controller = false; + context.State.SteamUsesSteamInput = false; + context.State.SteamUsesX360Controller = false; lastState = false; return; } @@ -23,20 +23,20 @@ namespace SteamController.Managers if (usesController) { - context.SteamUsesSteamInput = Helpers.SteamConfiguration.IsControllerBlacklisted( + context.State.SteamUsesSteamInput = Helpers.SteamConfiguration.IsControllerBlacklisted( Devices.SteamController.VendorID, Devices.SteamController.ProductID ) != true; - context.SteamUsesX360Controller = Helpers.SteamConfiguration.IsControllerBlacklisted( + context.State.SteamUsesX360Controller = Helpers.SteamConfiguration.IsControllerBlacklisted( Devices.Xbox360Controller.VendorID, Devices.Xbox360Controller.ProductID ) != true; } else { - context.SteamUsesSteamInput = false; - context.SteamUsesX360Controller = false; + context.State.SteamUsesSteamInput = false; + context.State.SteamUsesX360Controller = false; } lastState = usesController; diff --git a/SteamController/Profiles/SteamProfile.cs b/SteamController/Profiles/SteamProfile.cs index 50c63b6..2f8b1be 100644 --- a/SteamController/Profiles/SteamProfile.cs +++ b/SteamController/Profiles/SteamProfile.cs @@ -10,7 +10,7 @@ namespace SteamController.Profiles public override bool Selected(Context context) { - return context.Enabled && context.SteamUsesSteamInput; + return context.Enabled && context.State.SteamUsesSteamInput; } public override Status Run(Context context) diff --git a/SteamController/Profiles/SteamWithShorcutsProfile.cs b/SteamController/Profiles/SteamWithShorcutsProfile.cs index 209c455..19bac3b 100644 --- a/SteamController/Profiles/SteamWithShorcutsProfile.cs +++ b/SteamController/Profiles/SteamWithShorcutsProfile.cs @@ -10,7 +10,7 @@ namespace SteamController.Profiles public override bool Selected(Context context) { - return context.Enabled && context.SteamUsesSteamInput; + return context.Enabled && context.State.SteamUsesSteamInput; } public override Status Run(Context context) diff --git a/SteamController/Profiles/X360Profile.cs b/SteamController/Profiles/X360Profile.cs index 0e43cb6..c60bdda 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.X360.Valid && context.Mouse.Valid && !context.SteamUsesSteamInput; + return context.Enabled && context.X360.Valid && context.Mouse.Valid && !context.State.SteamUsesSteamInput; } internal override ProfilesSettings.BackPanelSettings BackPanelSettings