123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- //======= Copyright (c) Valve Corporation, All rights reserved. ===============
- using UnityEngine;
- using Valve.VR;
- public struct ClickedEventArgs
- {
- public uint controllerIndex;
- public uint flags;
- public float padX, padY;
- }
- public delegate void ClickedEventHandler(object sender, ClickedEventArgs e);
- public class SteamVR_TrackedController : MonoBehaviour
- {
- public uint controllerIndex;
- public VRControllerState_t controllerState;
- public bool triggerPressed = false;
- public bool steamPressed = false;
- public bool menuPressed = false;
- public bool padPressed = false;
- public bool padTouched = false;
- public bool gripped = false;
- public event ClickedEventHandler MenuButtonClicked;
- public event ClickedEventHandler MenuButtonUnclicked;
- public event ClickedEventHandler TriggerClicked;
- public event ClickedEventHandler TriggerUnclicked;
- public event ClickedEventHandler SteamClicked;
- public event ClickedEventHandler PadClicked;
- public event ClickedEventHandler PadUnclicked;
- public event ClickedEventHandler PadTouched;
- public event ClickedEventHandler PadUntouched;
- public event ClickedEventHandler Gripped;
- public event ClickedEventHandler Ungripped;
- // Use this for initialization
- protected virtual void Start()
- {
- if (this.GetComponent<SteamVR_TrackedObject>() == null)
- {
- gameObject.AddComponent<SteamVR_TrackedObject>();
- }
- if (controllerIndex != 0)
- {
- this.GetComponent<SteamVR_TrackedObject>().index = (SteamVR_TrackedObject.EIndex)controllerIndex;
- if (this.GetComponent<SteamVR_RenderModel>() != null)
- {
- this.GetComponent<SteamVR_RenderModel>().index = (SteamVR_TrackedObject.EIndex)controllerIndex;
- }
- }
- else
- {
- controllerIndex = (uint)this.GetComponent<SteamVR_TrackedObject>().index;
- }
- }
- public void SetDeviceIndex(int index)
- {
- this.controllerIndex = (uint)index;
- }
- public virtual void OnTriggerClicked(ClickedEventArgs e)
- {
- if (TriggerClicked != null)
- TriggerClicked(this, e);
- }
- public virtual void OnTriggerUnclicked(ClickedEventArgs e)
- {
- if (TriggerUnclicked != null)
- TriggerUnclicked(this, e);
- }
- public virtual void OnMenuClicked(ClickedEventArgs e)
- {
- if (MenuButtonClicked != null)
- MenuButtonClicked(this, e);
- }
- public virtual void OnMenuUnclicked(ClickedEventArgs e)
- {
- if (MenuButtonUnclicked != null)
- MenuButtonUnclicked(this, e);
- }
- public virtual void OnSteamClicked(ClickedEventArgs e)
- {
- if (SteamClicked != null)
- SteamClicked(this, e);
- }
- public virtual void OnPadClicked(ClickedEventArgs e)
- {
- if (PadClicked != null)
- PadClicked(this, e);
- }
- public virtual void OnPadUnclicked(ClickedEventArgs e)
- {
- if (PadUnclicked != null)
- PadUnclicked(this, e);
- }
- public virtual void OnPadTouched(ClickedEventArgs e)
- {
- if (PadTouched != null)
- PadTouched(this, e);
- }
- public virtual void OnPadUntouched(ClickedEventArgs e)
- {
- if (PadUntouched != null)
- PadUntouched(this, e);
- }
- public virtual void OnGripped(ClickedEventArgs e)
- {
- if (Gripped != null)
- Gripped(this, e);
- }
- public virtual void OnUngripped(ClickedEventArgs e)
- {
- if (Ungripped != null)
- Ungripped(this, e);
- }
- // Update is called once per frame
- protected virtual void Update()
- {
- var system = OpenVR.System;
- if (system != null && system.GetControllerState(controllerIndex, ref controllerState, (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VRControllerState_t))))
- {
- ulong trigger = controllerState.ulButtonPressed & (1UL << ((int)EVRButtonId.k_EButton_SteamVR_Trigger));
- if (trigger > 0L && !triggerPressed)
- {
- triggerPressed = true;
- ClickedEventArgs e;
- e.controllerIndex = controllerIndex;
- e.flags = (uint)controllerState.ulButtonPressed;
- e.padX = controllerState.rAxis0.x;
- e.padY = controllerState.rAxis0.y;
- OnTriggerClicked(e);
- }
- else if (trigger == 0L && triggerPressed)
- {
- triggerPressed = false;
- ClickedEventArgs e;
- e.controllerIndex = controllerIndex;
- e.flags = (uint)controllerState.ulButtonPressed;
- e.padX = controllerState.rAxis0.x;
- e.padY = controllerState.rAxis0.y;
- OnTriggerUnclicked(e);
- }
- ulong grip = controllerState.ulButtonPressed & (1UL << ((int)EVRButtonId.k_EButton_Grip));
- if (grip > 0L && !gripped)
- {
- gripped = true;
- ClickedEventArgs e;
- e.controllerIndex = controllerIndex;
- e.flags = (uint)controllerState.ulButtonPressed;
- e.padX = controllerState.rAxis0.x;
- e.padY = controllerState.rAxis0.y;
- OnGripped(e);
- }
- else if (grip == 0L && gripped)
- {
- gripped = false;
- ClickedEventArgs e;
- e.controllerIndex = controllerIndex;
- e.flags = (uint)controllerState.ulButtonPressed;
- e.padX = controllerState.rAxis0.x;
- e.padY = controllerState.rAxis0.y;
- OnUngripped(e);
- }
- ulong pad = controllerState.ulButtonPressed & (1UL << ((int)EVRButtonId.k_EButton_SteamVR_Touchpad));
- if (pad > 0L && !padPressed)
- {
- padPressed = true;
- ClickedEventArgs e;
- e.controllerIndex = controllerIndex;
- e.flags = (uint)controllerState.ulButtonPressed;
- e.padX = controllerState.rAxis0.x;
- e.padY = controllerState.rAxis0.y;
- OnPadClicked(e);
- }
- else if (pad == 0L && padPressed)
- {
- padPressed = false;
- ClickedEventArgs e;
- e.controllerIndex = controllerIndex;
- e.flags = (uint)controllerState.ulButtonPressed;
- e.padX = controllerState.rAxis0.x;
- e.padY = controllerState.rAxis0.y;
- OnPadUnclicked(e);
- }
- ulong menu = controllerState.ulButtonPressed & (1UL << ((int)EVRButtonId.k_EButton_ApplicationMenu));
- if (menu > 0L && !menuPressed)
- {
- menuPressed = true;
- ClickedEventArgs e;
- e.controllerIndex = controllerIndex;
- e.flags = (uint)controllerState.ulButtonPressed;
- e.padX = controllerState.rAxis0.x;
- e.padY = controllerState.rAxis0.y;
- OnMenuClicked(e);
- }
- else if (menu == 0L && menuPressed)
- {
- menuPressed = false;
- ClickedEventArgs e;
- e.controllerIndex = controllerIndex;
- e.flags = (uint)controllerState.ulButtonPressed;
- e.padX = controllerState.rAxis0.x;
- e.padY = controllerState.rAxis0.y;
- OnMenuUnclicked(e);
- }
- pad = controllerState.ulButtonTouched & (1UL << ((int)EVRButtonId.k_EButton_SteamVR_Touchpad));
- if (pad > 0L && !padTouched)
- {
- padTouched = true;
- ClickedEventArgs e;
- e.controllerIndex = controllerIndex;
- e.flags = (uint)controllerState.ulButtonPressed;
- e.padX = controllerState.rAxis0.x;
- e.padY = controllerState.rAxis0.y;
- OnPadTouched(e);
- }
- else if (pad == 0L && padTouched)
- {
- padTouched = false;
- ClickedEventArgs e;
- e.controllerIndex = controllerIndex;
- e.flags = (uint)controllerState.ulButtonPressed;
- e.padX = controllerState.rAxis0.x;
- e.padY = controllerState.rAxis0.y;
- OnPadUntouched(e);
- }
- }
- }
- }
|