2022-11-24 22:37:24 +01:00
|
|
|
using static CommonHelpers.Log;
|
|
|
|
|
|
2022-11-25 10:56:17 +01:00
|
|
|
namespace SteamController
|
2022-11-24 22:37:24 +01:00
|
|
|
{
|
2022-11-25 10:56:17 +01:00
|
|
|
public partial class Context : IDisposable
|
2022-11-24 22:37:24 +01:00
|
|
|
{
|
|
|
|
|
public const double JoystickToMouseSensitivity = 1200;
|
2022-11-25 10:56:17 +01:00
|
|
|
public const double PadToMouseSensitivity = 150;
|
2022-11-25 07:48:01 +01:00
|
|
|
public const double PadToWhellSensitivity = 4;
|
2022-11-27 15:32:23 +01:00
|
|
|
public const double ThumbToWhellSensitivity = 20;
|
2022-11-24 22:37:24 +01:00
|
|
|
|
|
|
|
|
public Devices.SteamController Steam { get; private set; }
|
|
|
|
|
public Devices.Xbox360Controller X360 { get; private set; }
|
|
|
|
|
public Devices.KeyboardController Keyboard { get; private set; }
|
|
|
|
|
public Devices.MouseController Mouse { get; private set; }
|
|
|
|
|
|
2022-11-25 10:56:17 +01:00
|
|
|
public List<Profiles.Profile> Profiles { get; } = new List<Profiles.Profile>();
|
|
|
|
|
public List<Managers.Manager> Managers { get; } = new List<Managers.Manager>();
|
|
|
|
|
|
2022-11-28 23:43:46 +01:00
|
|
|
private int selectedProfile;
|
2022-11-24 22:37:24 +01:00
|
|
|
|
2022-11-25 07:48:01 +01:00
|
|
|
public bool RequestEnable { get; set; } = true;
|
2022-11-29 21:39:54 +01:00
|
|
|
public bool GameProcessRunning { get; set; } = false;
|
2022-11-27 13:41:28 +01:00
|
|
|
public bool SteamUsesX360Controller { get; set; } = false;
|
|
|
|
|
public bool SteamUsesSteamInput { get; set; } = false;
|
2022-11-25 07:48:01 +01:00
|
|
|
|
2022-11-26 14:27:21 +01:00
|
|
|
public event Action<Profiles.Profile> ProfileChanged;
|
2022-11-28 23:43:46 +01:00
|
|
|
public Action? SelectDefault;
|
2022-11-26 14:27:21 +01:00
|
|
|
|
2022-11-25 07:48:01 +01:00
|
|
|
public bool Enabled
|
|
|
|
|
{
|
2022-11-25 10:56:17 +01:00
|
|
|
get { return RequestEnable; }
|
2022-11-25 07:48:01 +01:00
|
|
|
}
|
2022-11-24 22:37:24 +01:00
|
|
|
|
2022-11-28 23:43:46 +01:00
|
|
|
public Profiles.Profile? CurrentProfile
|
2022-11-24 22:37:24 +01:00
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2022-11-28 23:43:46 +01:00
|
|
|
for (int i = 0; i < Profiles.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var profile = Profiles[(selectedProfile + i) % Profiles.Count];
|
|
|
|
|
if (profile.Selected(this))
|
|
|
|
|
return profile;
|
|
|
|
|
}
|
2022-11-24 22:37:24 +01:00
|
|
|
|
2022-11-28 23:43:46 +01:00
|
|
|
return null;
|
2022-11-25 10:56:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 22:37:24 +01:00
|
|
|
public Context()
|
|
|
|
|
{
|
|
|
|
|
Steam = new Devices.SteamController();
|
|
|
|
|
X360 = new Devices.Xbox360Controller();
|
|
|
|
|
Keyboard = new Devices.KeyboardController();
|
|
|
|
|
Mouse = new Devices.MouseController();
|
2022-11-26 14:27:21 +01:00
|
|
|
|
|
|
|
|
ProfileChanged += (_) => X360.Beep();
|
2022-11-29 21:39:54 +01:00
|
|
|
ProfileChanged += (profile) => TraceLine("Context: Selected Profile: {0}", profile.Name);
|
2022-11-24 22:37:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
using (Steam) { }
|
|
|
|
|
using (X360) { }
|
|
|
|
|
using (Keyboard) { }
|
|
|
|
|
using (Mouse) { }
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-25 07:48:01 +01:00
|
|
|
public void Tick()
|
|
|
|
|
{
|
|
|
|
|
X360.Tick();
|
|
|
|
|
|
2022-11-25 10:56:17 +01:00
|
|
|
foreach (var manager in Managers)
|
2022-11-25 07:48:01 +01:00
|
|
|
{
|
2022-11-25 10:56:17 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
manager.Tick(this);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2022-11-27 15:32:23 +01:00
|
|
|
TraceLine("Manager: {0}. Exception: {1}", manager, e);
|
2022-11-25 10:56:17 +01:00
|
|
|
}
|
2022-11-25 07:48:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 22:37:24 +01:00
|
|
|
public bool Update()
|
|
|
|
|
{
|
|
|
|
|
Steam.BeforeUpdate();
|
|
|
|
|
X360.BeforeUpdate();
|
|
|
|
|
Keyboard.BeforeUpdate();
|
|
|
|
|
Mouse.BeforeUpdate();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-11-28 23:43:46 +01:00
|
|
|
var profile = CurrentProfile;
|
2022-11-25 21:28:43 +01:00
|
|
|
if (profile is not null)
|
|
|
|
|
profile.Run(this);
|
2022-11-24 22:37:24 +01:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
TraceLine("Controller: Exception: {0}", e);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Steam.Update();
|
|
|
|
|
X360.Update();
|
|
|
|
|
Keyboard.Update();
|
|
|
|
|
Mouse.Update();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-25 10:56:17 +01:00
|
|
|
|
2022-11-28 23:43:46 +01:00
|
|
|
public bool SelectProfile(String name)
|
2022-11-25 10:56:17 +01:00
|
|
|
{
|
2022-11-28 20:29:32 +01:00
|
|
|
lock (this)
|
|
|
|
|
{
|
2022-11-28 23:43:46 +01:00
|
|
|
for (int i = 0; i < Profiles.Count; i++)
|
2022-11-28 20:29:32 +01:00
|
|
|
{
|
2022-11-28 23:43:46 +01:00
|
|
|
var profile = Profiles[i];
|
|
|
|
|
if (profile.Name != name)
|
|
|
|
|
continue;
|
|
|
|
|
if (!profile.Selected(this))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (i != selectedProfile)
|
|
|
|
|
{
|
|
|
|
|
selectedProfile = i;
|
|
|
|
|
ProfileChanged(profile);
|
|
|
|
|
}
|
2022-11-28 20:29:32 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-28 23:43:46 +01:00
|
|
|
|
|
|
|
|
return false;
|
2022-11-25 10:56:17 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-28 23:43:46 +01:00
|
|
|
public void SelectController()
|
2022-11-25 10:56:17 +01:00
|
|
|
{
|
2022-11-28 23:43:46 +01:00
|
|
|
var current = CurrentProfile;
|
|
|
|
|
if (current is null)
|
|
|
|
|
return;
|
2022-11-29 21:39:54 +01:00
|
|
|
|
2022-11-28 23:43:46 +01:00
|
|
|
if (current.IsDesktop)
|
2022-11-29 21:39:54 +01:00
|
|
|
{
|
|
|
|
|
TraceLine("Context: SelectController");
|
2022-11-28 23:43:46 +01:00
|
|
|
SelectNext();
|
2022-11-29 21:39:54 +01:00
|
|
|
}
|
2022-11-25 10:56:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SelectNext()
|
|
|
|
|
{
|
2022-11-28 23:43:46 +01:00
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
// Update selectedProfile index
|
|
|
|
|
var current = CurrentProfile;
|
|
|
|
|
if (current is null)
|
|
|
|
|
return false;
|
|
|
|
|
selectedProfile = Profiles.IndexOf(current);
|
2022-11-25 10:56:17 +01:00
|
|
|
|
2022-11-28 23:43:46 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-25 10:56:17 +01:00
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-11-25 21:28:43 +01:00
|
|
|
|
2022-11-28 23:43:46 +01:00
|
|
|
public void BackToDefault()
|
2022-11-25 21:28:43 +01:00
|
|
|
{
|
2022-11-29 21:39:54 +01:00
|
|
|
TraceLine("Context: Back To Default.");
|
2022-11-28 23:43:46 +01:00
|
|
|
if (SelectDefault is not null)
|
|
|
|
|
SelectDefault();
|
2022-11-25 21:28:43 +01:00
|
|
|
}
|
2022-11-24 22:37:24 +01:00
|
|
|
}
|
|
|
|
|
}
|