Skip to content

Commit

Permalink
New way of doing vr input bindings, also adds stow button (#17)
Browse files Browse the repository at this point in the history
* Add inputs debug

* Trying new bindings way

* Fix look

* Better way, but also worse

* I think maybe

* Didn't help

* Fix a bunch of bindings

* Add stow binding
  • Loading branch information
Raicuparta authored Nov 14, 2021
1 parent 696718d commit ae86b7b
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 59 deletions.
25 changes: 25 additions & 0 deletions TwoForksVR/Debug/GeneralDebugger.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Linq;
using TwoForksVR.Helpers;
using UnityEngine;
using UnityEngine.EventSystems;

namespace TwoForksVR.Debug
{
Expand All @@ -12,6 +13,30 @@ private void Update()
{
UpdateTimeScale();
UpdateAnimator();
UpdateInputsDebug();
UpdateSaveActions();
}

private static void UpdateInputsDebug()
{
if (!UnityEngine.Input.GetKeyDown(KeyCode.Minus)) return;
Logs.LogInfo("## Starting key bind logs##");
var inputManager = FindObjectOfType<vgInputManager>();
foreach (var bind in inputManager.virtualKeyKeyBindMap.Values.Where(bind => bind.commands.Count != 0))
{
Logs.LogInfo($"{bind.commands[0].command}");
for (var i = 0; i < bind.keyData.names.Count; i++)
{
bind.keyData.names[i] = bind.commands[0].command;
}
}
Logs.LogInfo("## Ended key bind logs ##");
}

private static void UpdateSaveActions()
{
if (!UnityEngine.Input.GetKeyDown(KeyCode.Alpha0)) return;
vgSaveManager.Instance.LoadMostRecent();
}

private static void UpdateTimeScale()
Expand Down
131 changes: 112 additions & 19 deletions TwoForksVR/Input/InputName.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,119 @@
namespace TwoForksVR.Input
using System.Collections.Generic;

namespace TwoForksVR.Input
{
public struct InputName
{
public const string Notes = "tab";
public const string Pause = "escape";
public const string Climb = "space";
public const string Jog = "r";
public const string Read = "q";
public const string Keep = "e";
public const string Map = "DPadVertical";
public const string Camera = "DPadHorizontal";
public const string Flashlight = "f";
public const string AltUse = "AltUse";
public const string BackwardKeyDown = "BackwardKeyDown";
public const string BackwardKeyUp = "BackwardKeyUp";
public const string Camera = "Camera";
public const string CompassZoomIn = "CompassZoomIn";
public const string CompassZoomOut = "CompassZoomOut";
public const string DemoTeleport = "DemoTeleport";
public const string DialogSelectionDown = "DialogSelectionDown";
public const string DialogSelectionDownOrUse = "DialogSelectionDownOrUse";
public const string DialogSelectionScroll = "DialogSelectionScroll";
public const string DialogSelectionUp = "DialogSelectionUp";
public const string DisplayInventory = "DisplayInventory";
public const string EquipCompass = "EquipCompass";
public const string EquipMap = "EquipMap";
public const string Flashlight = "Flashlight";
public const string ForwardKeyDown = "ForwardKeyDown";
public const string ForwardKeyUp = "ForwardKeyUp";
public const string HideInventory = "HideInventory";
public const string HorizontalMouse = "Horizontal_Mouse";
public const string HorizontalStick = "Horizontal_Stick";
public const string InventoryRotateHorizontal = "InventoryRotateHorizontal";
public const string InventoryRotateHorizontalStick = "InventoryRotateHorizontal_Stick";
public const string InventoryRotateVertical = "InventoryRotateVertical";
public const string InventoryRotateVerticalStick = "InventoryRotateVertical_Stick";
public const string InventoryZoomIn = "InventoryZoomIn";
public const string InventoryZoomInTrigger = "InventoryZoomInTrigger";
public const string Key0 = "Key0";
public const string Key1 = "Key1";
public const string Key2 = "Key2";
public const string Key3 = "Key3";
public const string Key4 = "Key4";
public const string Key5 = "Key5";
public const string Key6 = "Key6";
public const string Key7 = "Key7";
public const string Key8 = "Key8";
public const string Key9 = "Key9";
public const string Lean = "Lean";
public const string LeanDown = "LeanDown";
public const string LeanHorizontal = "LeanHorizontal";
public const string LeanLeft = "LeanLeft";
public const string LeanRight = "LeanRight";
public const string LeanUp = "LeanUp";
public const string LeanVertical = "LeanVertical";
public const string LockCancel = "LockCancel";
public const string LockNumberDown = "LockNumberDown";
public const string LockNumberUp = "LockNumberUp";
public const string LockTry = "LockTry";
public const string LockTumblerLeft = "LockTumblerLeft";
public const string LockTumblerRight = "LockTumblerRight";
public const string LocomotionAction = "LocomotionAction";
public const string LocomotionActionOrToggleJog = "LocomotionActionOrToggleJog";
public const string LocomotionActionRelease = "LocomotionActionRelease";
public const string LookHorizontalMouse = "LookHorizontal_Mouse";
public const string LookHorizontalStick = "LookHorizontal_Stick";
public const string LookVerticalMouse = "LookVertical_Mouse";
public const string LookVerticalStick = "LookVertical_Stick";
public const string MapZoomIn = "MapZoomIn";
public const string MapZoomOut = "MapZoomOut";
public const string MoveForward = "MoveForward";
public const string MoveStrafe = "MoveStrafe";
public const string LookVertical = "LookVertical_Stick";
public const string LookHorizontal = "LookHorizontal_Stick";
public const string ChooseDown = "down";
public const string Radio = "LeftTrigger";
public const string ChooseUp = "up";
public const string Zoom = "q";
public const string Interact = "mouse 0";
public const string NextPage = "e";
public const string PreviousPage = "q";
public const string NextMenu = "NextMenu";
public const string Pause = "Pause";
public const string PressDemoKey1 = "PressDemoKey1";
public const string PressDemoKey2 = "PressDemoKey2";
public const string PressDemoKey3 = "PressDemoKey3";
public const string PressDemoKey4 = "PressDemoKey4";
public const string PreviousMenu = "PreviousMenu";
public const string PutDownItem = "PutDownItem";
public const string QuickLoad = "QuickLoad";
public const string QuickSave = "QuickSave";
public const string RadioDown = "RadioDown";
public const string RadioUp = "RadioUp";
public const string ReadMode = "ReadMode";
public const string Scroll = "Scroll";
public const string StartInventoryRotation = "StartInventoryRotation";
public const string StopInventoryRotation = "StopInventoryRotation";
public const string StopLean = "StopLean";
public const string StopStowHeldObject = "StopStowHeldObject";
public const string StowHeldObject = "StowHeldObject";
public const string StrafeLeftKeyDown = "StrafeLeftKeyDown";
public const string StrafeLeftKeyUp = "StrafeLeftKeyUp";
public const string StrafeRightKeyDown = "StrafeRightKeyDown";
public const string StrafeRightKeyUp = "StrafeRightKeyUp";
public const string Titlecard = "Titlecard";
public const string ToggleCompass = "ToggleCompass";
public const string ToggleJog = "ToggleJog";
public const string ToggleMap = "ToggleMap";
public const string ToggleReading = "ToggleReading";
public const string TossItem = "TossItem";
public const string UICancel = "UICancel";
public const string UISubmit = "UISubmit";
public const string UIClick = "UIClick";
public const string UILeft = "UILeft";
public const string UIRight = "UIRight";
public const string UIUp = "UIUp";
public const string UIDown = "UIDown";
public const string UIVertical = "UIVertical";
public const string UIHorizontal = "UIHorizontal";
public const string UnCrouch = "UnCrouch";
public const string UnequipCompass = "UnequipCompass";
public const string UnequipMap = "UnequipMap";
public const string UnPause = "UnPause";
public const string Use = "Use";
public const string UseCamera = "UseCamera";
public const string VerticalMouse = "Vertical_Mouse";
public const string VerticalStick = "Vertical_Stick";
public const string WindUpToss = "WindUpToss";
public const string ZoomIn = "ZoomIn";
public const string ZoomInOnHeldObject = "ZoomInOnHeldObject";
public const string ZoomOut = "ZoomOut";
public const string ZoomOutOnHeldObject = "ZoomOutOnHeldObject";
}
}
91 changes: 51 additions & 40 deletions TwoForksVR/Input/Patches/BindingsPatches.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using HarmonyLib;
using Valve.VR;

namespace TwoForksVR.Input.Patches
{
[HarmonyPatch]
public static class InputPatches
public static class BindingsPatches
{
private static SteamVR_Input_ActionSet_default actionSet;
private static Dictionary<string, SteamVR_Action_Boolean> booleanActionMap;
Expand All @@ -19,24 +18,37 @@ private static void Initialize()
actionSet = SteamVR_Actions._default;
booleanActionMap = new Dictionary<string, SteamVR_Action_Boolean>
{
{InputName.Climb, actionSet.Interact},
{InputName.ChooseUp, actionSet.UIUp},
{InputName.ChooseDown, actionSet.UIDown},
{InputName.Jog, actionSet.Jog},
{InputName.LocomotionAction, actionSet.Interact},
{InputName.Use, actionSet.Interact},
{InputName.UISubmit, actionSet.Interact},
{InputName.StowHeldObject, actionSet.Jog},
{InputName.UICancel, actionSet.Cancel},
{InputName.DialogSelectionUp, actionSet.UIUp},
{InputName.DialogSelectionDown, actionSet.UIDown},
{InputName.UIUp, actionSet.UIUp},
{InputName.UIDown, actionSet.UIDown},
{InputName.LockNumberUp, actionSet.UIUp},
{InputName.LockNumberDown, actionSet.UIDown},
{InputName.LockTumblerRight, actionSet.NextPage},
{InputName.LockTumblerLeft, actionSet.PreviousPage},
{InputName.LockCancel, actionSet.Cancel},
{InputName.ToggleJog, actionSet.Jog},
{InputName.Pause, actionSet.Cancel},
{InputName.Interact, actionSet.Interact},
{InputName.NextPage, actionSet.NextPage},
{InputName.PreviousPage, actionSet.PreviousPage}
{InputName.NextMenu, actionSet.NextPage},
{InputName.PreviousMenu, actionSet.PreviousPage}
};
vector2XActionMap = new Dictionary<string, SteamVR_Action_Vector2>
{
{InputName.MoveStrafe, actionSet.Move},
{InputName.LookHorizontal, actionSet.Rotate}
{InputName.LookHorizontalStick, actionSet.Rotate},
{InputName.UIHorizontal, actionSet.Move},
};
vector2YActionMap = new Dictionary<string, SteamVR_Action_Vector2>
{
{InputName.MoveForward, actionSet.Move},
{InputName.LookVertical, actionSet.Rotate}
{InputName.LookVerticalStick, actionSet.Rotate},
{InputName.Scroll, actionSet.Move},
{InputName.UIVertical, actionSet.Move},
};

// Pick dialog option with interact button.
Expand All @@ -47,44 +59,43 @@ private static void Initialize()
vgDialogTreeManager.Instance.OnConfirmDialogChoice();
vgDialogTreeManager.Instance.ClearNonRadioDialogChoices();
};
}

[HarmonyPostfix]
[HarmonyPatch(typeof(vgAxisData), nameof(vgAxisData.Update))]
private static void ReadAxisValuesFromSteamVR(vgAxisData __instance)
{
if (!SteamVR_Input.initialized) return;

if (actionSet == null) Initialize();

foreach (var name in __instance.names)
foreach (var entry in vector2XActionMap)
{
if (vector2XActionMap.ContainsKey(name))
{
__instance.axisValue = vector2XActionMap[name].axis.x;
__instance.axisValueLastFrame = vector2XActionMap[name].lastAxis.x;
}
else if (vector2YActionMap.ContainsKey(name))
entry.Value.onChange += (action, source, axis, delta) => TriggerCommand(entry.Key, axis.x);
}
foreach (var entry in vector2YActionMap)
{
entry.Value.onChange += (action, source, axis, delta) => TriggerCommand(entry.Key, axis.y);
}
foreach (var entry in booleanActionMap)
{
entry.Value.onChange += (action, source, state) =>
{
__instance.axisValue = vector2YActionMap[name].axis.y;
__instance.axisValueLastFrame = vector2YActionMap[name].lastAxis.y;
}
if (!state) return;
TriggerCommand(entry.Key, 1);
};
}
}

public static void TriggerCommand(string command, float axisValue)
{
if (!vgInputManager.Instance || vgInputManager.Instance.commandCallbackMap == null) return;
var commandCallbackMap = vgInputManager.Instance.commandCallbackMap;
if (!vgInputManager.Instance.flushCommands && commandCallbackMap.TryGetValue(command, out var inputDelegate))
{
inputDelegate?.Invoke(axisValue);
}
}

[HarmonyPostfix]
[HarmonyPatch(typeof(vgButtonData), nameof(vgButtonData.Update))]
private static void ReadButtonValuesFromSteamVR(vgButtonData __instance)
[HarmonyPrefix]
[HarmonyPatch(typeof(vgAxisData), nameof(vgAxisData.Update))]
private static void ReadAxisValuesFromSteamVR(vgAxisData __instance)
{
if (!SteamVR_Input.initialized) return;


// TODO move this elsewhere.
if (actionSet == null) Initialize();

foreach (var name in __instance.names.Where(name => booleanActionMap.ContainsKey(name)))
{
__instance.keyUp = booleanActionMap[name].stateUp;
__instance.keyDown = booleanActionMap[name].stateDown;
}
}

[HarmonyPrefix]
Expand Down
62 changes: 62 additions & 0 deletions TwoForksVR/Input/VrAxisData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Collections.Generic;

namespace TwoForksVR.Input
{
public class VrAxisData : vgKeyData
{
public VrAxisData(string name, float deadzone = 0.5f)
{
names = new List<string>(1);
names.Add(name);
buttonDeadzone = deadzone;
}

public override void Update()
{
axisValueLastFrame = axisValue;
string axisName = names[0];
axisValue = UnityEngine.Input.GetAxisRaw(axisName);
}

public override bool Released()
{
return CheckThreshold(buttonDeadzone) == ThresholdResult.CrossedBelow;
}

public override bool Pressed()
{
return CheckThreshold(buttonDeadzone) == ThresholdResult.CrossedAbove;
}

public override bool IsHeld()
{
return GetAxisValue() >= buttonDeadzone;
}

public override bool ShouldHold(float duration)
{
return false;
}

public override float GetAxisValue()
{
return axisValue;
}

public override float GetPreviousAxisValue()
{
return axisValueLastFrame;
}

public override vgKeyCodeToVirtualKey.KeyType GetKeyType()
{
return vgKeyCodeToVirtualKey.KeyType.Axis;
}

private float axisValue;

private float axisValueLastFrame;

private float buttonDeadzone = 0.5f;
}
}
1 change: 1 addition & 0 deletions TwoForksVR/TwoForksVR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<Compile Include="Helpers\LayerFromName.cs" />
<Compile Include="Helpers\Logs.cs" />
<Compile Include="Helpers\ShaderProperty.cs" />
<Compile Include="Input\VrAxisData.cs" />
<Compile Include="PlayerBody\Patches\BodyPatches.cs" />
<Compile Include="PlayerBody\VRBodyManager.cs" />
<Compile Include="Debug\DebugAxes.cs" />
Expand Down

0 comments on commit ae86b7b

Please sign in to comment.