Skip to content

Commit

Permalink
Implement ShiftActions (Valkirie#370)
Browse files Browse the repository at this point in the history
* Implement ShiftActions

* add description to ShiftSlot

* Allow ShiftActions to be toggled

* Fix a few issues with options rendering
  • Loading branch information
Valkirie authored Nov 15, 2024
1 parent c4ef818 commit ab0d4f5
Show file tree
Hide file tree
Showing 9 changed files with 350 additions and 136 deletions.
4 changes: 2 additions & 2 deletions HandheldCompanion/Actions/ButtonActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public ButtonActions(ButtonFlags button) : this()
this.Button = button;
}

public override void Execute(ButtonFlags button, bool value)
public override void Execute(ButtonFlags button, bool value, ShiftSlot shiftSlot)
{
base.Execute(button, value);
base.Execute(button, value, shiftSlot);

switch (this.Value)
{
Expand Down
22 changes: 20 additions & 2 deletions HandheldCompanion/Actions/IActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public enum ActionType
Keyboard = 3,
Mouse = 4,
Trigger = 5,
Special = 6,
Shift = 6,
}

[Serializable]
Expand Down Expand Up @@ -96,6 +96,10 @@ public abstract class IActions : ICloneable

private int pressCount = 0; // used to store previous press value for double tap

public bool HasTurbo = true;
public bool HasToggle = true;
public bool HasInterruptable = true;

public bool Turbo;
public int TurboDelay = 30;
protected int TurboIdx;
Expand All @@ -105,6 +109,7 @@ public abstract class IActions : ICloneable
protected bool IsToggled;

public bool Interruptable = true;
public ShiftSlot ShiftSlot = 0;

public HapticMode HapticMode = HapticMode.Off;
public HapticStrength HapticStrength = HapticStrength.Low;
Expand All @@ -125,7 +130,7 @@ public virtual void SetHaptic(ButtonFlags button, bool up)
ControllerManager.GetTargetController()?.SetHaptic(this.HapticStrength, button);
}

public virtual void Execute(ButtonFlags button, bool value)
public virtual void Execute(ButtonFlags button, bool value, ShiftSlot shiftSlot = Actions.ShiftSlot.None)
{
if (actionState == ActionState.Suspended)
{
Expand All @@ -140,6 +145,19 @@ public virtual void Execute(ButtonFlags button, bool value)
value = true;
}

switch(ShiftSlot)
{
case ShiftSlot.None:
if (shiftSlot != ShiftSlot.None)
value = false;
break;

default:
if (!shiftSlot.HasFlag(ShiftSlot))
value = false;
break;
}

switch (pressType)
{
case PressType.Long:
Expand Down
4 changes: 2 additions & 2 deletions HandheldCompanion/Actions/KeyboardActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public KeyboardActions(VirtualKeyCode key) : this()
this.Key = key;
}

public override void Execute(ButtonFlags button, bool value)
public override void Execute(ButtonFlags button, bool value, ShiftSlot shiftSlot)
{
base.Execute(button, value);
base.Execute(button, value, shiftSlot);

switch (this.Value)
{
Expand Down
4 changes: 2 additions & 2 deletions HandheldCompanion/Actions/MouseActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public MouseActions(MouseActionsType type) : this()
this.MouseType = type;
}

public override void Execute(ButtonFlags button, bool value)
public override void Execute(ButtonFlags button, bool value, ShiftSlot shiftSlot)
{
base.Execute(button, value);
base.Execute(button, value, shiftSlot);

switch (this.Value)
{
Expand Down
47 changes: 47 additions & 0 deletions HandheldCompanion/Actions/ShiftActions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using HandheldCompanion.Inputs;
using HandheldCompanion.Simulators;
using System;
using System.ComponentModel;
using System.Numerics;
using WindowsInput.Events;

namespace HandheldCompanion.Actions
{
[Serializable]
public enum ShiftSlot
{
[Description("None")]
None,
[Description("Shift A")]
ShiftA,
[Description("Shift B")]
ShiftB,
[Description("Shift C")]
ShiftC,
[Description("Shift D")]
ShiftD,
}

[Serializable]
public class ShiftActions : ButtonActions
{
public ShiftSlot ShiftSlot;

public ShiftActions()
{
this.actionType = ActionType.Shift;

// disable few options
this.HasInterruptable = false;
this.HasTurbo = false;

this.Value = false;
this.prevValue = false;
}

public ShiftActions(ShiftSlot slot) : this()
{
this.ShiftSlot = slot;
}
}
}
96 changes: 68 additions & 28 deletions HandheldCompanion/Managers/LayoutManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
using System.Windows;
Expand Down Expand Up @@ -340,7 +341,36 @@ public static ControllerState MapController(ControllerState controllerState)
outputState.ButtonState.Clear();
outputState.AxisState.Clear();
outputState.GyroState = new(controllerState.GyroState.Accelerometer, controllerState.GyroState.Gyroscope);

// we need to check for shifter(s) first
ShiftSlot shiftSlot = ShiftSlot.None;
foreach (KeyValuePair<ButtonFlags, bool> buttonState in controllerState.ButtonState.State)
{
ButtonFlags button = buttonState.Key;
bool value = buttonState.Value;

// skip, if not mapped
if (!currentLayout.ButtonLayout.TryGetValue(button, out List<IActions> actions))
continue;

foreach (IActions action in actions)
{
switch (action.actionType)
{
// button to shift
case ActionType.Shift:
{
ShiftActions sAction = action as ShiftActions;
sAction.Execute(button, value, shiftSlot);
bool outVal = sAction.GetValue();

if (outVal) shiftSlot |= sAction.ShiftSlot;
}
break;
}
}
}

foreach (KeyValuePair<ButtonFlags, bool> buttonState in controllerState.ButtonState.State)
{
ButtonFlags button = buttonState.Key;
Expand All @@ -358,7 +388,7 @@ public static ControllerState MapController(ControllerState controllerState)
case ActionType.Button:
{
ButtonActions bAction = action as ButtonActions;
bAction.Execute(button, value);
bAction.Execute(button, value, shiftSlot);

bool outVal = bAction.GetValue() || outputState.ButtonState[bAction.Button];
outputState.ButtonState[bAction.Button] = outVal;
Expand All @@ -369,15 +399,15 @@ public static ControllerState MapController(ControllerState controllerState)
case ActionType.Keyboard:
{
KeyboardActions kAction = action as KeyboardActions;
kAction.Execute(button, value);
kAction.Execute(button, value, shiftSlot);
}
break;

// button to mouse click
case ActionType.Mouse:
{
MouseActions mAction = action as MouseActions;
mAction.Execute(button, value);
mAction.Execute(button, value, shiftSlot);
}
break;
}
Expand All @@ -386,46 +416,56 @@ public static ControllerState MapController(ControllerState controllerState)
{
case ActionState.Aborted:
case ActionState.Stopped:
foreach (IActions action2 in actions)
{
if (action2 == action)
continue;
foreach (IActions action2 in actions.Where(a => a.ShiftSlot == action.ShiftSlot))
{
if (action2 == action)
continue;

if (!action2.Interruptable)
continue;
if (!action2.Interruptable)
continue;

if (action2.actionState == ActionState.Succeed)
continue;
if (action2.actionState == ActionState.Succeed)
continue;

if (action2.actionState != ActionState.Stopped && action2.actionState != ActionState.Aborted)
action2.actionState = ActionState.Stopped;
}
if (action2.actionState != ActionState.Stopped && action2.actionState != ActionState.Aborted)
action2.actionState = ActionState.Stopped;
}

if (action.actionState == ActionState.Aborted)
{
int idx = actions.IndexOf(action);
if (idx < actions.Count - 1)
if (action.actionState == ActionState.Aborted)
{
IActions nAction = actions[idx + 1]; // next action
if (nAction.Interruptable)
nAction.actionState = ActionState.Forced;
int idx = actions.IndexOf(action);
if (idx >= 0 && idx < actions.Count - 1)
{
var currentShiftSlot = action.ShiftSlot;

// Find the next action with the same ShiftSlot after the current index
IActions nextAction = actions
.Skip(idx + 1)
.FirstOrDefault(a => a.ShiftSlot == currentShiftSlot && a.Interruptable);

if (nextAction != null)
nextAction.actionState = ActionState.Forced;
}
}
}
break;

case ActionState.Running:
foreach (IActions action2 in actions)
{
if (action2 == action)
continue;
foreach (IActions action2 in actions.Where(a => a.ShiftSlot == action.ShiftSlot))
{
if (action2 == action)
continue;

if (!action2.Interruptable)
continue;
if (!action2.Interruptable)
continue;

if (action2.actionState == ActionState.Succeed)
continue;
if (action2.actionState == ActionState.Succeed)
continue;

action2.actionState = ActionState.Suspended;
action2.actionState = ActionState.Suspended;
}
}
break;
}
Expand Down
Loading

0 comments on commit ab0d4f5

Please sign in to comment.