Created
January 6, 2019 14:08
-
-
Save andywatts/b0e93886574e0fb123b7627d49cb1d19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class PlayerInput : Singleton<PlayerInput> { | |
public GameObject leftAnchor; | |
public GameObject rightAnchor; | |
public GameObject headAnchor; | |
private Dictionary<OVRInput.Controller, GameObject> controllerAnchors = null; | |
private OVRInput.Controller primaryController; | |
private OVRInput.Controller secondaryController; | |
private bool hmdMounted = true; | |
private float primaryButtonOneTimer; | |
public static Action HMDMounted = null; | |
public static Action HMDUnmounted = null; | |
public static Action onPrimaryIndexTrigger = null; | |
public static Action onPrimaryHandTrigger = null; | |
public static Action onPrimaryButtonOne = null; | |
public static Action onLongPrimaryButtonOne = null; | |
public static Action onPrimaryButtonTwo = null; | |
public static Action onPrimaryThumbstick = null; | |
public static Action onSecondaryIndexTrigger = null; | |
public static Action onSecondaryHandTrigger = null; | |
public static Action onSecondaryButtonOne = null; | |
public static Action onSecondaryButtonTwo = null; | |
public static Action onSecondaryThumbstick = null; | |
public static bool rightHanded = true; | |
private void Awake(){ | |
CreateControllerAnchors(); | |
OVRManager.HMDMounted += HMDMount; | |
OVRManager.HMDUnmounted += HMDUnmount; | |
} | |
void Update(){ | |
if (!hmdMounted) { return; } | |
// Debug.Log($"PlayerInput#Update Connected controllers {OVRInput.GetConnectedControllers()}"); | |
// Touch; TODO Override OVRControllerTouch#ConfigureButtonMap for left|right handedness | |
if (OVRInput.GetActiveController() == OVRInput.Controller.Touch){ | |
primaryController = rightHanded ? OVRInput.Controller.RTouch : OVRInput.Controller.LTouch; | |
secondaryController = rightHanded ? OVRInput.Controller.LTouch : OVRInput.Controller.RTouch; | |
// Primary | |
if (OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger, primaryController) && onPrimaryIndexTrigger != null) { onPrimaryIndexTrigger(); } | |
if (OVRInput.GetDown(OVRInput.Button.PrimaryHandTrigger, primaryController) && onPrimaryHandTrigger != null) { onPrimaryHandTrigger(); } | |
if (OVRInput.GetDown(OVRInput.Button.One, primaryController) && onPrimaryButtonOne != null){ onPrimaryButtonOne(); } | |
if (OVRInput.GetDown(OVRInput.Button.Two, primaryController) && onPrimaryButtonTwo != null){ onPrimaryButtonTwo(); } | |
if (OVRInput.GetDown(OVRInput.Button.PrimaryThumbstick, primaryController) && onPrimaryThumbstick != null){ onPrimaryThumbstick(); } | |
// Secondary | |
if (OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger, secondaryController) && onSecondaryIndexTrigger != null) { onSecondaryIndexTrigger(); } | |
if (OVRInput.GetDown(OVRInput.Button.PrimaryHandTrigger, secondaryController) && onSecondaryHandTrigger != null) { onSecondaryHandTrigger(); } | |
if (OVRInput.GetDown(OVRInput.Button.One, secondaryController) && onSecondaryButtonOne != null){ onSecondaryButtonOne(); } | |
if (OVRInput.GetDown(OVRInput.Button.Two, secondaryController) && onSecondaryButtonTwo != null){ onSecondaryButtonTwo(); } | |
if (OVRInput.GetDown(OVRInput.Button.SecondaryThumbstick, secondaryController) && onSecondaryThumbstick != null){ onSecondaryThumbstick(); } | |
} | |
// Go | |
if ( OVRInput.GetActiveController() == OVRInput.Controller.LTrackedRemote || OVRInput.GetActiveController() == OVRInput.Controller.RTrackedRemote ){ | |
if (OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger) && onPrimaryIndexTrigger != null) { onPrimaryIndexTrigger(); } | |
if (OVRInput.GetDown(OVRInput.Button.Two) && onPrimaryButtonTwo != null) { onPrimaryButtonTwo(); } | |
// Time press | |
if (OVRInput.Get(OVRInput.Button.One)) { primaryButtonOneTimer += Time.deltaTime; } | |
// long press | |
if (OVRInput.GetUp(OVRInput.Button.One) && primaryButtonOneTimer > 2.0f && onLongPrimaryButtonOne != null) { | |
onLongPrimaryButtonOne(); | |
primaryButtonOneTimer = 0.0f; // zero timer | |
// Short press | |
} else if (OVRInput.GetUp(OVRInput.Button.One) && onPrimaryButtonOne != null) { | |
onPrimaryButtonOne(); | |
primaryButtonOneTimer = 0.0f; // zero timer | |
} | |
} | |
} | |
void HMDMount(){ | |
hmdMounted = true; | |
} | |
void HMDUnmount(){ | |
hmdMounted = false; | |
} | |
void OnDestroy(){ | |
OVRManager.HMDMounted -= HMDMount; | |
OVRManager.HMDUnmounted -= HMDUnmount; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment