123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2017 Qualcomm Technologies, Inc.
- // All Rights Reserved. Qualcomm Technologies Proprietary and Confidential.
- //-----------------------------------------------------------------------------
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- /// <summary>
- /// GSXRController
- /// </summary>
- public class GSXRController : MonoBehaviour, GSXRManager.SlamEventListener {
- string controllerParams = "";
- //! \brief Events to use in slamControllerSendEvent
- public enum slamControllerMessageType
- {
- kControllerMessageRecenter = 0,
- kControllerMessageVibration = 1
- };
- //! \brief Query Values
- public enum slamControllerQueryType
- {
- kControllerBatteryRemaining,
- kControllerControllerCaps
- };
- //! Controller Connection state
- public enum slamControllerConnectionState {
- kNotInitialized = 0,
- kDisconnected = 1,
- kConnected = 2,
- kConnecting = 3,
- kError = 4
- };
- /// <summary>
- /// Start this instance.
- /// </summary>
- //---------------------------------------------------------------------------------------------
- void Start ()
- {
- //Register for SlamEvents
- GSXRManager.Instance.AddEventListener (this);
- }
- /// <summary>
- /// Raises the slam event event.
- /// </summary>
- /// <param name="ev">Ev.</param>
- //---------------------------------------------------------------------------------------------
- public void OnSlamEvent(GSXRManager.SlamEvent ev)
- {
- switch (ev.eventType) {
- case GSXRManager.slamEventType.kEventVrModeStarted:
- handle = GSXRManager.Instance.ControllerStartTracking (controllerParams);
- space = GetCapability.caps != 0 ? 1 : 0; // Has Position so needs to be transformed from HMD to World space
- break;
- }
- }
- /// <summary>
- /// Raises the application pause event.
- /// </summary>
- /// <param name="isPaused">If set to <c>true</c> is paused.</param>
- //---------------------------------------------------------------------------------------------
- void OnApplicationPause(bool isPaused)
- {
- return;
- if (isPaused) {
- GSXRManager.Instance.ControllerStopTracking (handle);
- }
- }
- /// <summary>
- /// Get the current controller state
- /// </summary>
- /// <returns>The state.</returns>
- //---------------------------------------------------------------------------------------------
- public GSXRControllerState State
- {
- get {
- return currentState;
- }
- }
- /// <summary>
- /// Gets the state of the connection.
- /// </summary>
- /// <value>The state of the connection.</value>
- //---------------------------------------------------------------------------------------------
- public slamControllerConnectionState ConnectionState {
- get {
- return (slamControllerConnectionState)currentState.connectionState;
- }
- }
- /// <summary>
- /// Sends the message.
- /// </summary>
- /// <param name="what">What.</param>
- /// <param name="arg1">Arg1.</param>
- /// <param name="arg2">Arg2.</param>
- //---------------------------------------------------------------------------------------------
- public void SendMessage(slamControllerMessageType what, int arg1, int arg2)
- {
- GSXRManager.Instance.ControllerSendMessage(handle, what, arg1, arg2);
- }
- /// <summary>
- /// Recenter this instance.
- /// </summary>
- //---------------------------------------------------------------------------------------------
- public void Recenter( )
- {
- GSXRManager.Instance.ControllerSendMessage(handle,
- slamControllerMessageType.kControllerMessageRecenter,
- 0,
- 0);
- }
- /// <summary>
- /// Send message to vibrate
- /// </summary>
- //---------------------------------------------------------------------------------------------
- public void Vibrate(int arg1, int arg2)
- {
- GSXRManager.Instance.ControllerSendMessage (handle,
- slamControllerMessageType.kControllerMessageVibration,
- arg1,
- arg2);
- }
- /// <summary>
- /// Gets the current state of the button.
- /// </summary>
- /// <returns><c>true</c>, if button is down, <c>false</c> otherwise.</returns>
- /// <param name="buttonId">Button identifier.</param>
- //---------------------------------------------------------------------------------------------
- public bool GetButton(slamControllerButton buttonId)
- {
- int mask = (int)buttonId;
- return ((currentState.buttonState & mask) != 0);
- }
- /// <summary>
- /// Gets the button up.
- /// </summary>
- /// <returns><c>true</c>, if button is up this frame, <c>false</c> otherwise.</returns>
- /// <param name="buttonId">Button identifier.</param>
- //---------------------------------------------------------------------------------------------
- public bool GetButtonUp(slamControllerButton buttonId)
- {
- int mask = (int)(buttonId);
- return ((previousButtonState & mask) != 0) && ((currentState.buttonState & mask) == 0);
- }
- /// <summary>
- /// Gets the button down.
- /// </summary>
- /// <returns><c>true</c>, if button is down this frame, <c>false</c> otherwise.</returns>
- /// <param name="buttonId">Button identifier.</param>
- //---------------------------------------------------------------------------------------------
- public bool GetButtonDown(slamControllerButton buttonId)
- {
- int mask = (int)buttonId;
- return ((previousButtonState & mask) == 0) && ((currentState.buttonState & mask) != 0);
- }
- /// <summary>
- /// Get the current orientation.
- /// </summary>
- /// <value>The orientation.</value>
- //---------------------------------------------------------------------------------------------
- public Quaternion Orientation
- {
- get {
- return currentState.rotation;
- }
- }
- /// <summary>
- /// Get the current position.
- /// </summary>
- /// <value>The position.</value>
- //---------------------------------------------------------------------------------------------
- public Vector3 Position {
- get {
- return currentState.position;
- }
- }
- /// <summary>
- /// the timestamp.
- /// </summary>
- /// <value>The timestamp.</value>
- //---------------------------------------------------------------------------------------------
- public long Timestamp {
- get {
- return currentState.timestamp;
- }
- }
- /// <summary>
- /// Gets the analog.
- /// </summary>
- /// <returns>The analog.</returns>
- /// <param name="id">Identifier.</param>
- //---------------------------------------------------------------------------------------------
- public Vector2 GetAxis2D(slamControllerAxis2D axi2d)
- {
- return currentState.analog2D != null ? currentState.analog2D [(int)axi2d] : Vector2.zero;
- }
- /// <summary>
- /// Gets the analog.
- /// </summary>
- /// <returns>The analog.</returns>
- /// <param name="id">Identifier.</param>
- //---------------------------------------------------------------------------------------------
- public float GetAxis1D(slamControllerAxis1D axis1d)
- {
- return currentState.analog1D != null ? currentState.analog1D [(int)axis1d] : 0f;
- }
- /// <summary>
- /// Determines whether this instance is touching the specified id.
- /// </summary>
- /// <returns><c>true</c> if this instance is touching the specified id; otherwise, <c>false</c>.</returns>
- /// <param name="id">Identifier.</param>
- //---------------------------------------------------------------------------------------------
- public bool GetTouch(slamControllerTouch touch)
- {
- int mask = (int)touch;
- return ((currentState.isTouching & mask) != 0);
- }
- /// <summary>
- /// Gets the touch down.
- /// </summary>
- /// <returns><c>true</c>, if touch down was gotten, <c>false</c> otherwise.</returns>
- /// <param name="id">Identifier.</param>
- //---------------------------------------------------------------------------------------------
- public bool GetTouchDown(slamControllerTouch touch)
- {
- int mask = (int)touch;
- return ((previousTouchState & mask) == 0) && ((currentState.isTouching & mask) != 0);
- }
- /// <summary>
- /// Gets the touch up.
- /// </summary>
- /// <returns><c>true</c>, if touch up was gotten, <c>false</c> otherwise.</returns>
- /// <param name="id">Identifier.</param>
- //---------------------------------------------------------------------------------------------
- public bool GetTouchUp(slamControllerTouch touch)
- {
- int mask = (int)touch;
- return ((previousTouchState & mask) != 0) && ((currentState.isTouching & mask) == 0);
- }
- /// <summary>
- /// Gets the battery.
- /// </summary>
- /// <value>The battery.</value>
- //---------------------------------------------------------------------------------------------
- public int BatteryLevel {
- get {
- int batteryLevel = -1;
- object obj = (GSXRManager.Instance.ControllerQuery(handle, slamControllerQueryType.kControllerBatteryRemaining));
- if (obj != null) {
- batteryLevel = (int)(obj);
- }
- return batteryLevel;
- }
- }
- /// <summary>
- public SlamControllerCaps GetCapability
- {
- get
- {
- SlamControllerCaps Cap = new SlamControllerCaps();
- object obj = (GSXRManager.Instance.ControllerQuery(handle, slamControllerQueryType.kControllerControllerCaps));
- if (obj != null)
- {
- Cap = (SlamControllerCaps)(obj);
- }
- return Cap;
- }
- }
- /// Raises the enable event.
- /// </summary>
- //---------------------------------------------------------------------------------------------
- public void OnEnable()
- {
- frameDelimiter = StartCoroutine (OnFrameEnd());
- }
- /// <summary>
- /// Raises the disable event.
- /// </summary>
- //---------------------------------------------------------------------------------------------
- public void OnDisable()
- {
- StopCoroutine (frameDelimiter);
- }
- /// <summary>
- /// Raises the per frame event.
- /// </summary>
- //---------------------------------------------------------------------------------------------
- IEnumerator OnFrameEnd()
- {
- while (true) {
- yield return waitForEndOfFrame;
- previousButtonState = currentState.buttonState;
- previousTouchState = currentState.isTouching;
- currentState = GSXRManager.Instance.ControllerGetState (handle, space);
- }
- }
- /**
- *
- */
- public object Query(slamControllerQueryType what)
- {
- return GSXRManager.Instance.ControllerQuery (handle, what);
- }
- /**
- * Handle for the Controller.
- */
- private int handle = -1;
- /**
- * Handle for the Controller.
- */
- private int space = 0;
- /**
- * The Current State. Updated each frame.
- */
- private GSXRControllerState currentState;
- /**
- * Previous Button State.
- */
- private int previousButtonState = 0;
- /**
- * Previous Touch State.
- */
- private int previousTouchState = 0;
- /**
- * Coroutine for WaitForEndOfFrame
- */
- private Coroutine frameDelimiter = null;
- /**
- *
- */
- private WaitForEndOfFrame waitForEndOfFrame = new WaitForEndOfFrame();
- //! Controller Touch button enumerations
- public enum slamControllerTouch {
- None = 0x00000000,
- One = 0x00000001,
- Two = 0x00000002,
- Three = 0x00000004,
- Four = 0x00000008,
- PrimaryThumbstick = 0x00000010,
- SecondaryThumstick = 0x00000020,
- Any = ~None
- };
- //! Controller Trigger enumerations
- public enum slamControllerAxis1D {
- PrimaryIndexTrigger = 0x00000000,
- SecondaryIndexTrigger = 0x00000001,
- PrimaryHandTrigger = 0x00000002,
- SecondaryHandTrigger = 0x00000003
- };
- //! Controller Joystick enumerations
- public enum slamControllerAxis2D {
- PrimaryThumbstick = 0x00000000,
- SecondaryThumbstick = 0x00000001
- };
- //! Controller Button enumerations
- public enum slamControllerButton {
- 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
- };
- }
|