Skip to content

Commit

Permalink
Implement ControllerManager GetTargetOrDefault()
Browse files Browse the repository at this point in the history
  • Loading branch information
Valkirie committed Jan 4, 2025
1 parent 2c3ad8c commit 7ec0b94
Show file tree
Hide file tree
Showing 20 changed files with 50 additions and 44 deletions.
2 changes: 1 addition & 1 deletion HandheldCompanion/Actions/IActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public virtual void SetHaptic(ButtonFlags button, bool up)
if (this.HapticMode == HapticMode.Down && up) return;
if (this.HapticMode == HapticMode.Up && !up) return;

ControllerManager.GetTargetController()?.SetHaptic(this.HapticStrength, button);
ControllerManager.GetTarget()?.SetHaptic(this.HapticStrength, button);
}

public virtual void Execute(ButtonFlags button, bool value, ShiftSlot shiftSlot = Actions.ShiftSlot.None)
Expand Down
9 changes: 7 additions & 2 deletions HandheldCompanion/Managers/ControllerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ private static async void XUsbDeviceRemoved(PnPDetails details, Guid IntefaceGui
}
}

public static bool HasTargetController => GetTargetController() != null;
public static bool HasTargetController => GetTarget() != null;

private static void ClearTargetController()
{
Expand Down Expand Up @@ -1187,11 +1187,16 @@ public static bool ResumeControllers()
return false;
}

public static IController GetTargetController()
public static IController GetTarget()
{
return targetController;
}

public static IController GetTargetOrDefault()
{
return targetController is not null ? targetController : GetDefault();
}

public static bool IsTargetController(string InstanceId)
{
return targetController?.GetInstanceId() == InstanceId;
Expand Down
6 changes: 3 additions & 3 deletions HandheldCompanion/Managers/InputsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private static void M_GlobalHook_KeyEvent(object? sender, KeyEventArgs e)
BufferKeys[args.IsKeyDown].Add(new KeyEventArgsExt((Keys)keyCode, args.ScanCode, args.Timestamp, args.IsKeyDown, args.IsKeyUp, false, args.Flags));

// calls current controller (if connected)
IController controller = ControllerManager.GetTargetController();
IController controller = ControllerManager.GetTarget();
controller?.InjectState(chord.state, args.IsKeyDown, args.IsKeyUp);

// remove chord
Expand Down Expand Up @@ -394,7 +394,7 @@ private static void M_GlobalHook_KeyEvent(object? sender, KeyEventArgs e)
BufferKeys[args.IsKeyDown].Clear();

// calls current controller (if connected)
IController controller = ControllerManager.GetTargetController();
IController controller = ControllerManager.GetTarget();
controller?.InjectState(chord.state, args.IsKeyDown, args.IsKeyUp);

return;
Expand Down Expand Up @@ -425,7 +425,7 @@ private static void M_GlobalHook_KeyEvent(object? sender, KeyEventArgs e)
// store successful hotkey
successkeyChords.Add(chord);

IController controller = ControllerManager.GetTargetController();
IController controller = ControllerManager.GetTarget();
controller?.InjectState(chord.state, args.IsKeyDown, args.IsKeyUp);

return;
Expand Down
31 changes: 17 additions & 14 deletions HandheldCompanion/Managers/LayoutManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,25 +350,28 @@ private void UpdateInherit()
lock (updateLock)
{
// Check for inherit(s) and replace actions with default layout actions where necessary
IController controller = ControllerManager.GetTargetController();
foreach (ButtonFlags buttonFlags in controller.GetTargetButtons())
IController controller = ControllerManager.GetTargetOrDefault();
if (controller is not null)
{
if (currentLayout.ButtonLayout.TryGetValue(buttonFlags, out var actions) && actions.Any(action => action is InheritActions))
foreach (ButtonFlags buttonFlags in controller.GetTargetButtons())
{
// Replace with default layout actions
if (defaultLayout.ButtonLayout.TryGetValue(buttonFlags, out var defaultActions))
currentLayout.ButtonLayout[buttonFlags].AddRange(defaultActions);
if (currentLayout.ButtonLayout.TryGetValue(buttonFlags, out var actions) && actions.Any(action => action is InheritActions))
{
// Replace with default layout actions
if (defaultLayout.ButtonLayout.TryGetValue(buttonFlags, out var defaultActions))
currentLayout.ButtonLayout[buttonFlags].AddRange(defaultActions);
}
}
}

// Check for inherit(s) and replace actions with default layout actions where necessary
foreach (AxisLayoutFlags axisLayout in controller.GetTargetAxis().Union(controller.GetTargetTriggers()))
{
if (currentLayout.AxisLayout.TryGetValue(axisLayout, out var actions) && actions is InheritActions)
// Check for inherit(s) and replace actions with default layout actions where necessary
foreach (AxisLayoutFlags axisLayout in controller.GetTargetAxis().Union(controller.GetTargetTriggers()))
{
// Replace with default layout actions
if (defaultLayout.AxisLayout.TryGetValue(axisLayout, out var defaultActions))
currentLayout.AxisLayout[axisLayout] = defaultActions;
if (currentLayout.AxisLayout.TryGetValue(axisLayout, out var actions) && actions is InheritActions)
{
// Replace with default layout actions
if (defaultLayout.AxisLayout.TryGetValue(axisLayout, out var defaultActions))
currentLayout.AxisLayout[axisLayout] = defaultActions;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion HandheldCompanion/Managers/ProfileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static void Start()

if (ControllerManager.IsInitialized)
{
ControllerManager_ControllerPlugged(ControllerManager.GetTargetController(), false);
ControllerManager_ControllerPlugged(ControllerManager.GetTarget(), false);
}

IsInitialized = true;
Expand Down
6 changes: 3 additions & 3 deletions HandheldCompanion/Managers/SensorsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static void Start()

if (ControllerManager.HasTargetController)
{
ControllerManager_ControllerSelected(ControllerManager.GetTargetController());
ControllerManager_ControllerSelected(ControllerManager.GetTarget());
}

IsInitialized = true;
Expand Down Expand Up @@ -143,7 +143,7 @@ private static void DeviceManager_UsbDeviceRemoved(PnPDevice device, Guid Intefa
private static void PickNextSensor()
{
// get current controller
IController controller = ControllerManager.GetTargetController();
IController controller = ControllerManager.GetTarget();

if (controller is not null && controller.HasMotionSensor())
ManagerFactory.settingsManager.SetProperty("SensorSelection", (int)SensorFamily.Controller);
Expand Down Expand Up @@ -227,7 +227,7 @@ private static void SettingsManager_SettingValueChanged(string name, object valu
case SensorFamily.Controller:
{
// get current controller
IController controller = ControllerManager.GetTargetController();
IController controller = ControllerManager.GetTarget();
if (controller is null || !controller.Capabilities.HasFlag(ControllerCapabilities.MotionSensor))
{
PickNextSensor();
Expand Down
2 changes: 1 addition & 1 deletion HandheldCompanion/Misc/Hotkey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void Execute(bool onKeyDown, bool onKeyUp, bool IsBackground)
{
bool Rumble = ManagerFactory.settingsManager.GetBoolean("HotkeyRumbleOnExecution");
if (Rumble && !IsBackground && !IsInternal)
ControllerManager.GetTargetController()?.Rumble();
ControllerManager.GetTarget()?.Rumble();

command?.Execute(command.OnKeyDown, command.OnKeyUp, IsBackground);
}
Expand Down
4 changes: 1 addition & 3 deletions HandheldCompanion/ViewModels/HotkeyViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,7 @@ public void DrawChords()
foreach (FontIconViewModel FontIconViewModel in ButtonGlyphs.ToList())
ButtonGlyphs.SafeRemove(FontIconViewModel);

IController? controller = ControllerManager.GetTargetController();
if (controller is null)
controller = ControllerManager.GetDefault();
IController controller = ControllerManager.GetTargetOrDefault();

// UI thread
UIHelper.TryInvoke(() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public ButtonMappingViewModel(ButtonStackViewModel parentStack, ButtonFlags butt

if (isInitialMapping)
{
var controller = ControllerManager.GetTargetController();
var controller = ControllerManager.GetTarget();
if (controller is not null) UpdateController(controller);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ButtonStackViewModel(ButtonFlags flag)
// send events
if (ControllerManager.HasTargetController)
{
UpdateController(ControllerManager.GetTargetController());
UpdateController(ControllerManager.GetTarget());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public MappingViewModel(object value)
// send events
if (ControllerManager.HasTargetController)
{
UpdateController(ControllerManager.GetTargetController());
UpdateController(ControllerManager.GetTarget());
}

// Send update event to Model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public ILayoutPageViewModel()
// send events
if (ControllerManager.HasTargetController)
{
UpdateController(ControllerManager.GetTargetController());
UpdateController(ControllerManager.GetTarget());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ControllerPageViewModel(ControllerPage controllerPage)
// send events
if (ControllerManager.HasTargetController)
{
ControllerManager_ControllerSelected(ControllerManager.GetTargetController());
ControllerManager_ControllerSelected(ControllerManager.GetTarget());
}
}

Expand Down
2 changes: 1 addition & 1 deletion HandheldCompanion/ViewModels/Pages/HotkeyPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public HotkeyPageViewModel()
// raise event
if (ControllerManager.HasTargetController)
{
ControllerManager_ControllerSelected(ControllerManager.GetTargetController());
ControllerManager_ControllerSelected(ControllerManager.GetTarget());
}

if (HotkeysManager.IsInitialized)
Expand Down
4 changes: 2 additions & 2 deletions HandheldCompanion/ViewModels/Pages/LayoutPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public LayoutPageViewModel(LayoutPage layoutPage)

if (ControllerManager.HasTargetController)
{
ControllerManager_ControllerSelected(ControllerManager.GetTargetController());
ControllerManager_ControllerSelected(ControllerManager.GetTarget());
}
}

Expand Down Expand Up @@ -106,7 +106,7 @@ private void RefreshLayoutList()
bool FilterOnDevice = ManagerFactory.settingsManager.GetBoolean("LayoutFilterOnDevice");

// Get current controller
IController? controller = ControllerManager.GetTargetController();
IController? controller = ControllerManager.GetTarget();

foreach (LayoutTemplateViewModel layoutTemplate in LayoutList)
{
Expand Down
2 changes: 1 addition & 1 deletion HandheldCompanion/Views/Pages/ControllerPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private TextBlock CreateFormattedContent(string title, string description)

public void ControllerRefresh()
{
IController targetController = ControllerManager.GetTargetController();
IController targetController = ControllerManager.GetTarget();
bool hasPhysical = ControllerManager.HasPhysicalController();
bool hasVirtual = ControllerManager.HasVirtualController();
bool hasTarget = targetController != null;
Expand Down
4 changes: 2 additions & 2 deletions HandheldCompanion/Views/Pages/DevicePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public DevicePage(string? Tag) : this()
// raise events
if (ControllerManager.HasTargetController)
{
ControllerManager_ControllerSelected(ControllerManager.GetTargetController());
ControllerManager_ControllerSelected(ControllerManager.GetTarget());
}
}

Expand Down Expand Up @@ -507,7 +507,7 @@ private void ui_button_calibrate_Click(object sender, RoutedEventArgs e)
break;

case SensorFamily.Controller:
IController controller = ControllerManager.GetTargetController();
IController controller = ControllerManager.GetTarget();
controller?.Calibrate();
break;
}
Expand Down
4 changes: 2 additions & 2 deletions HandheldCompanion/Views/Pages/LayoutPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void Initialize()
// raise events
if (ControllerManager.HasTargetController)
{
ControllerManager_ControllerSelected(ControllerManager.GetTargetController());
ControllerManager_ControllerSelected(ControllerManager.GetTarget());
}
}

Expand Down Expand Up @@ -277,7 +277,7 @@ private void LayoutExportButton_Click(object sender, RoutedEventArgs e)
}

if (ExportForCurrent.IsChecked == true)
newLayout.ControllerType = ControllerManager.GetTargetController()?.GetType();
newLayout.ControllerType = ControllerManager.GetTarget()?.GetType();

ManagerFactory.layoutManager.SerializeLayoutTemplate(newLayout);

Expand Down
2 changes: 1 addition & 1 deletion HandheldCompanion/Views/Windows/OverlayQuickTools.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public OverlayQuickTools()
// raise events
if (ControllerManager.HasTargetController)
{
ControllerManager_ControllerSelected(ControllerManager.GetTargetController());
ControllerManager_ControllerSelected(ControllerManager.GetTarget());
}

// load gamepad navigation manager
Expand Down
4 changes: 2 additions & 2 deletions HandheldCompanion/Views/Windows/OverlayTrackpad.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private void Trackpad_PreviewTouchDown(object sender, TouchEventArgs e)
LeftTrackpad.Opacity = TrackpadOpacity + TrackpadOpacityTouched;

// send vibration (todo: make it a setting)
ControllerManager.GetTargetController()?.Rumble(); // (1, 25, 0, 60);
ControllerManager.GetTarget()?.Rumble(); // (1, 25, 0, 60);
}
break;
case "RightTrackpad":
Expand All @@ -188,7 +188,7 @@ private void Trackpad_PreviewTouchDown(object sender, TouchEventArgs e)
RightTrackpad.Opacity = TrackpadOpacity + TrackpadOpacityTouched;

// send vibration (todo: make it a setting)
ControllerManager.GetTargetController()?.Rumble(); // (1, 25, 0, 60);
ControllerManager.GetTarget()?.Rumble(); // (1, 25, 0, 60);
}
break;
}
Expand Down

0 comments on commit 7ec0b94

Please sign in to comment.