123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
-
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class SvrController : MonoBehaviour, SvrManager.SvrEventListener {
- string controllerParams = "";
-
- public enum svrControllerMessageType
- {
- kControllerMessageRecenter = 0,
- kControllerMessageVibration = 1
- };
-
- public enum svrControllerQueryType
- {
- kControllerBatteryRemaining,
- kControllerControllerCaps
- };
-
- public enum svrControllerConnectionState {
- kNotInitialized = 0,
- kDisconnected = 1,
- kConnected = 2,
- kConnecting = 3,
- kError = 4
- };
-
-
-
-
- void Start ()
- {
-
- SvrManager.Instance.AddEventListener (this);
- }
-
-
-
-
-
- public void OnSvrEvent(SvrManager.SvrEvent ev)
- {
- switch (ev.eventType) {
- case SvrManager.svrEventType.kEventVrModeStarted:
- handle = SvrManager.Instance.ControllerStartTracking (controllerParams);
- space = GetCapability.caps != 0 ? 1 : 0;
- break;
- }
- }
-
-
-
-
-
- void OnApplicationPause(bool isPaused)
- {
- if (isPaused) {
- SvrManager.Instance.ControllerStopTracking (handle);
- }
- }
-
-
-
-
-
- public SvrControllerState State
- {
- get {
- return currentState;
- }
- }
-
-
-
-
-
- public svrControllerConnectionState ConnectionState {
- get {
- return (svrControllerConnectionState)currentState.connectionState;
- }
- }
-
-
-
-
-
-
-
- public void SendMessage(svrControllerMessageType what, int arg1, int arg2)
- {
- SvrManager.Instance.ControllerSendMessage(handle, what, arg1, arg2);
- }
-
-
-
-
- public void Recenter( )
- {
- SvrManager.Instance.ControllerSendMessage(handle,
- svrControllerMessageType.kControllerMessageRecenter,
- 0,
- 0);
- }
-
-
-
-
- public void Vibrate(int arg1, int arg2)
- {
- SvrManager.Instance.ControllerSendMessage (handle,
- svrControllerMessageType.kControllerMessageVibration,
- arg1,
- arg2);
- }
-
-
-
-
-
-
- public bool GetButton(svrControllerButton buttonId)
- {
- int mask = (int)buttonId;
- return ((currentState.buttonState & mask) != 0);
- }
-
-
-
-
-
-
- public bool GetButtonUp(svrControllerButton buttonId)
- {
- int mask = (int)(buttonId);
- return ((previousButtonState & mask) != 0) && ((currentState.buttonState & mask) == 0);
- }
-
-
-
-
-
-
- public bool GetButtonDown(svrControllerButton buttonId)
- {
- int mask = (int)buttonId;
- return ((previousButtonState & mask) == 0) && ((currentState.buttonState & mask) != 0);
- }
-
-
-
-
-
- public Quaternion Orientation
- {
- get {
- return currentState.rotation;
- }
- }
-
-
-
-
-
- public Vector3 Position {
- get {
- return currentState.position;
- }
- }
-
-
-
-
-
- public long Timestamp {
- get {
- return currentState.timestamp;
- }
- }
-
-
-
-
-
-
- public Vector2 GetAxis2D(svrControllerAxis2D axi2d)
- {
- return currentState.analog2D != null ? currentState.analog2D [(int)axi2d] : Vector2.zero;
- }
-
-
-
-
-
-
- public float GetAxis1D(svrControllerAxis1D axis1d)
- {
- return currentState.analog1D != null ? currentState.analog1D [(int)axis1d] : 0f;
- }
-
-
-
-
-
-
- public bool GetTouch(svrControllerTouch touch)
- {
- int mask = (int)touch;
- return ((currentState.isTouching & mask) != 0);
- }
-
-
-
-
-
-
- public bool GetTouchDown(svrControllerTouch touch)
- {
- int mask = (int)touch;
- return ((previousTouchState & mask) == 0) && ((currentState.isTouching & mask) != 0);
- }
-
-
-
-
-
-
- public bool GetTouchUp(svrControllerTouch touch)
- {
- int mask = (int)touch;
- return ((previousTouchState & mask) != 0) && ((currentState.isTouching & mask) == 0);
- }
-
-
-
-
-
- public int BatteryLevel {
- get {
- int batteryLevel = -1;
- object obj = (SvrManager.Instance.ControllerQuery(handle, svrControllerQueryType.kControllerBatteryRemaining));
- if (obj != null) {
- batteryLevel = (int)(obj);
- }
- return batteryLevel;
- }
- }
-
- public SvrControllerCaps GetCapability
- {
- get
- {
- SvrControllerCaps Cap = new SvrControllerCaps();
- object obj = (SvrManager.Instance.ControllerQuery(handle, svrControllerQueryType.kControllerControllerCaps));
- if (obj != null)
- {
- Cap = (SvrControllerCaps)(obj);
- }
- return Cap;
- }
- }
-
-
-
- public void OnEnable()
- {
- frameDelimiter = StartCoroutine (OnFrameEnd());
- }
-
-
-
-
- public void OnDisable()
- {
- StopCoroutine (frameDelimiter);
- }
-
-
-
-
- IEnumerator OnFrameEnd()
- {
- while (true) {
- yield return waitForEndOfFrame;
- previousButtonState = currentState.buttonState;
- previousTouchState = currentState.isTouching;
- currentState = SvrManager.Instance.ControllerGetState (handle, space);
- }
- }
-
- public object Query(svrControllerQueryType what)
- {
- return SvrManager.Instance.ControllerQuery (handle, what);
- }
-
- private int handle = -1;
-
- private int space = 0;
-
- private SvrControllerState currentState;
-
- private int previousButtonState = 0;
-
- private int previousTouchState = 0;
-
- private Coroutine frameDelimiter = null;
-
- private WaitForEndOfFrame waitForEndOfFrame = new WaitForEndOfFrame();
-
- public enum svrControllerTouch {
- None = 0x00000000,
- One = 0x00000001,
- Two = 0x00000002,
- Three = 0x00000004,
- Four = 0x00000008,
- PrimaryThumbstick = 0x00000010,
- SecondaryThumstick = 0x00000020,
- Any = ~None
- };
-
- public enum svrControllerAxis1D {
- PrimaryIndexTrigger = 0x00000000,
- SecondaryIndexTrigger = 0x00000001,
- PrimaryHandTrigger = 0x00000002,
- SecondaryHandTrigger = 0x00000003
- };
-
- public enum svrControllerAxis2D {
- PrimaryThumbstick = 0x00000000,
- SecondaryThumbstick = 0x00000001
- };
-
- public enum svrControllerButton {
- None = 0x00000000,
- One = 0x00000001,
- Two = 0x00000002,
- Three = 0x00000004,
- Four = 0x00000008,
- DpadUp = 0x00000010,
- DpadDown = 0x00000020,
- DpadLeft = 0x00000040,
- DpadRight = 0x00000080,
- Start = 0x00000100,
- Back = 0x00000200,
- PrimaryShoulder = 0x00001000,
- PrimaryIndexTrigger = 0x00002000,
- PrimaryHandTrigger = 0x00004000,
- PrimaryThumbstick = 0x00008000,
- PrimaryThumbstickUp = 0x00010000,
- PrimaryThumbstickDown = 0x00020000,
- PrimaryThumbstickLeft = 0x00040000,
- PrimaryThumbstickRight = 0x00080000,
- SecondaryShoulder = 0x00100000,
- SecondaryIndexTrigger = 0x00200000,
- SecondaryHandTrigger = 0x00400000,
- SecondaryThumbstick = 0x00800000,
- SecondaryThumbstickUp = 0x01000000,
- SecondaryThumbstickDown = 0x02000000,
- SecondaryThumbstickLeft = 0x04000000,
- SecondaryThumbstickRight = 0x08000000,
- Up = 0x10000000,
- Down = 0x20000000,
- Left = 0x40000000,
- Right = unchecked((int)0x80000000),
- Any = ~None
- };
- }
|