SteamVR_Action_In.cs 22.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
//======= Copyright (c) Valve Corporation, All rights reserved. ===============

using UnityEngine;
using System.Collections;
using System;
using Valve.VR;
using System.Runtime.InteropServices;
using System.Collections.Generic;

namespace Valve.VR
{
    [Serializable]
    /// <summary>
    /// In actions are all input type actions. Boolean, Single, Vector2, Vector3, Skeleton, and Pose.
    /// </summary>
    public abstract class SteamVR_Action_In<SourceMap, SourceElement> : SteamVR_Action<SourceMap, SourceElement>, ISteamVR_Action_In
        where SourceMap : SteamVR_Action_In_Source_Map<SourceElement>, new()
        where SourceElement : SteamVR_Action_In_Source, new()
    {
        /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Returns true if the action has been changed since the previous update</summary>
        public bool changed { get { return sourceMap[SteamVR_Input_Sources.Any].changed; } }

        /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> Returns true if the action was changed for the previous update cycle</summary>
        public bool lastChanged { get { return sourceMap[SteamVR_Input_Sources.Any].changed; } }

        /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The time the action was changed (Time.realtimeSinceStartup)</summary>
        public float changedTime { get { return sourceMap[SteamVR_Input_Sources.Any].changedTime; } }

        /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The time the action was updated (Time.realtimeSinceStartup)</summary>
        public float updateTime { get { return sourceMap[SteamVR_Input_Sources.Any].updateTime; } }

        /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The handle to the component that triggered the action to be changed</summary>
        public ulong activeOrigin { get { return sourceMap[SteamVR_Input_Sources.Any].activeOrigin; } }

        /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The handle to the component that triggered the action to be changed in the previous update</summary>
        public ulong lastActiveOrigin { get { return sourceMap[SteamVR_Input_Sources.Any].lastActiveOrigin; } }

        /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The input source that triggered the action to be changed</summary>
        public SteamVR_Input_Sources activeDevice { get { return sourceMap[SteamVR_Input_Sources.Any].activeDevice; } }

        /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The device index (used by Render Models) used by the device that triggered the action to be changed</summary>
        public uint trackedDeviceIndex { get { return sourceMap[SteamVR_Input_Sources.Any].trackedDeviceIndex; } }

        /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The name of the component on the render model that caused the action to be changed (not localized)</summary>
        public string renderModelComponentName { get { return sourceMap[SteamVR_Input_Sources.Any].renderModelComponentName; } }

        /// <summary><strong>[Shortcut to: SteamVR_Input_Sources.Any]</strong> The full localized name for the component, controller, and hand that caused the action to be changed</summary>
        public string localizedOriginName { get { return sourceMap[SteamVR_Input_Sources.Any].localizedOriginName; } }

        /// <summary>
        /// <strong>[Should not be called by user code]</strong>
        /// Updates the data for all the input sources the system has detected need to be updated.
        /// </summary>
        public virtual void UpdateValues()
        {
            sourceMap.UpdateValues();
        }

        /// <summary>
        /// The name of the component on the render model that caused the action to be updated (not localized)
        /// </summary>
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        public virtual string GetRenderModelComponentName(SteamVR_Input_Sources inputSource)
        {
            return sourceMap[inputSource].renderModelComponentName;
        }

        /// <summary>
        /// The input source that triggered the action to be updated last
        /// </summary>
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        public virtual SteamVR_Input_Sources GetActiveDevice(SteamVR_Input_Sources inputSource)
        {
            return sourceMap[inputSource].activeDevice;
        }

        /// <summary>
        /// Gets the device index for the controller this action is bound to. This can be used for render models or the pose tracking system.
        /// </summary>
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        public virtual uint GetDeviceIndex(SteamVR_Input_Sources inputSource)
        {
            return sourceMap[inputSource].trackedDeviceIndex;
        }

        /// <summary>
        /// Indicates whether or not the data for this action and specified input source has changed since the last update. Determined by SteamVR or 'changeTolerance'.
        /// </summary>
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        public virtual bool GetChanged(SteamVR_Input_Sources inputSource)
        {
            return sourceMap[inputSource].changed;
        }

        /// <summary>
        /// The time the action was changed (Time.realtimeSinceStartup)
        /// </summary>
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        public override float GetTimeLastChanged(SteamVR_Input_Sources inputSource)
        {
            return sourceMap[inputSource].changedTime;
        }


        /// <summary>
        /// Gets the localized name of the device that the action corresponds to. Include as many EVRInputStringBits as you want to add to the localized string
        /// </summary>
        /// <param name="inputSource"></param>
        /// <param name="localizedParts">
        /// <list type="bullet">
        /// <item><description>VRInputString_Hand - Which hand the origin is in. ex: "Left Hand". </description></item>
        /// <item><description>VRInputString_ControllerType - What kind of controller the user has in that hand. ex: "Vive Controller". </description></item>
        /// <item><description>VRInputString_InputSource - What part of that controller is the origin. ex: "Trackpad". </description></item>
        /// <item><description>VRInputString_All - All of the above. ex: "Left Hand Vive Controller Trackpad". </description></item>
        /// </list>
        /// </param>
        public string GetLocalizedOriginPart(SteamVR_Input_Sources inputSource, params EVRInputStringBits[] localizedParts)
        {
            return sourceMap[inputSource].GetLocalizedOriginPart(localizedParts);
        }

        /// <summary>
        /// Gets the localized full name of the device that the action was updated by. ex: "Left Hand Vive Controller Trackpad"
        /// </summary>
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        public string GetLocalizedOrigin(SteamVR_Input_Sources inputSource)
        {
            return sourceMap[inputSource].GetLocalizedOrigin();
        }

        /// <summary>
        /// <strong>[Should not be called by user code]</strong>
        /// Returns whether the system has determined this source should be updated (based on code calls)
        /// Should only be used if you've set SteamVR_Action.startUpdatingSourceOnAccess to false.
        /// </summary>
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        public override bool IsUpdating(SteamVR_Input_Sources inputSource)
        {
            return sourceMap.IsUpdating(inputSource);
        }

        /// <summary>
        /// <strong>[Should not be called by user code]</strong>
        /// Forces the system to start updating the data for this action and the specified input source.
        /// Should only be used if you've set SteamVR_Action.startUpdatingSourceOnAccess to false.
        /// </summary>
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        public void ForceAddSourceToUpdateList(SteamVR_Input_Sources inputSource)
        {
            sourceMap.ForceAddSourceToUpdateList(inputSource);
        }

        /// <summary>
        /// Returns a string for the type of controller that was being used the last time the action was triggered. Common types: 
        /// vive_controller, oculus_touch, knuckles, vive_cosmos_controller, logitech_stylus
        /// </summary>
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        public string GetControllerType(SteamVR_Input_Sources inputSource)
        {
            return SteamVR.instance.GetStringProperty(ETrackedDeviceProperty.Prop_ControllerType_String, GetDeviceIndex(inputSource));
        }
    }

    public class SteamVR_Action_In_Source_Map<SourceElement> : SteamVR_Action_Source_Map<SourceElement>
        where SourceElement : SteamVR_Action_In_Source, new()
    {
        protected List<int> updatingSources = new List<int>();

        /// <summary>
        /// <strong>[Should not be called by user code]</strong>
        /// Returns whether the system has determined this source should be updated (based on code calls)
        /// Should only be used if you've set SteamVR_Action.startUpdatingSourceOnAccess to false.
        /// </summary>
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        public bool IsUpdating(SteamVR_Input_Sources inputSource)
        {
            int isUpdatingSourceIndex = (int)inputSource;

            for (int sourceIndex = 0; sourceIndex < updatingSources.Count; sourceIndex++)
            {
                if (isUpdatingSourceIndex == updatingSources[sourceIndex])
                    return true;
            }

            return false;
        }

        protected override void OnAccessSource(SteamVR_Input_Sources inputSource)
        {
            if (SteamVR_Action.startUpdatingSourceOnAccess)
            {
                ForceAddSourceToUpdateList(inputSource);
            }
        }

        /// <summary>
        /// <strong>[Should not be called by user code]</strong>
        /// Forces the system to start updating the data for this action and the specified input source.
        /// Should only be used if you've set SteamVR_Action.startUpdatingSourceOnAccess to false.
        /// </summary>
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        public void ForceAddSourceToUpdateList(SteamVR_Input_Sources inputSource)
        {
            int sourceIndex = (int)inputSource;

            if (sources[sourceIndex] == null)
            {
                sources[sourceIndex] = new SourceElement();
            }

            if (sources[sourceIndex].isUpdating == false)
            {
                updatingSources.Add(sourceIndex);
                sources[sourceIndex].isUpdating = true;

                if (SteamVR_Input.isStartupFrame == false)
                    sources[sourceIndex].UpdateValue();
            }
        }

        /// <summary>
        /// <strong>[Should not be called by user code]</strong>
        /// Updates the data for all the input sources the system has detected need to be updated.
        /// </summary>
        public void UpdateValues()
        {
            for (int sourceIndex = 0; sourceIndex < updatingSources.Count; sourceIndex++)
            {
                sources[updatingSources[sourceIndex]].UpdateValue();
            }
        }
    }

    /// <summary>
    /// In actions are all input type actions. Boolean, Single, Vector2, Vector3, Skeleton, and Pose.
    /// This class fires onChange and onUpdate events.
    /// </summary>
    public abstract class SteamVR_Action_In_Source : SteamVR_Action_Source, ISteamVR_Action_In_Source
    {
        protected static uint inputOriginInfo_size = 0;

        /// <summary>
        /// <strong>[Should not be called by user code]</strong>
        /// Forces the system to start updating the data for this action and the specified input source.
        /// Should only be used if you've set SteamVR_Action.startUpdatingSourceOnAccess to false.
        /// </summary>
        public bool isUpdating { get; set; }

        /// <summary>The time the action was updated (Time.realtimeSinceStartup)</summary>
        public float updateTime { get; protected set; }

        /// <summary>The handle to the component that triggered the action to be changed</summary>
        public abstract ulong activeOrigin { get; }

        /// <summary>The handle to the component that triggered the action to be changed in the previous update</summary>
        public abstract ulong lastActiveOrigin { get; }

        /// <summary>Returns true if the action has been changed since the previous update</summary>
        public abstract bool changed { get; protected set; }

        /// <summary>Returns true if the action was changed for the previous update cycle</summary>
        public abstract bool lastChanged { get; protected set; }

        /// <summary>The input source that triggered the action to be updated</summary>
        public SteamVR_Input_Sources activeDevice { get { UpdateOriginTrackedDeviceInfo();  return SteamVR_Input_Source.GetSource(inputOriginInfo.devicePath); } }

        /// <summary>The device index (used by Render Models) used by the device that triggered the action to be updated</summary>
        public uint trackedDeviceIndex { get { UpdateOriginTrackedDeviceInfo(); return inputOriginInfo.trackedDeviceIndex; } }

        /// <summary>The name of the component on the render model that caused the action to be updated (not localized)</summary>
        public string renderModelComponentName { get { UpdateOriginTrackedDeviceInfo(); return inputOriginInfo.rchRenderModelComponentName; } }

        /// <summary>
        /// Gets the localized full name of the device that the action was updated by. ex: "Left Hand Vive Controller Trackpad"
        /// </summary>
        public string localizedOriginName { get { UpdateOriginTrackedDeviceInfo(); return GetLocalizedOrigin(); } }


        /// <summary>The Time.realtimeSinceStartup that this action was last changed.</summary>
        public float changedTime { get; protected set; }

        protected int lastOriginGetFrame { get; set; }

        protected InputOriginInfo_t inputOriginInfo = new InputOriginInfo_t();
        protected InputOriginInfo_t lastInputOriginInfo = new InputOriginInfo_t();

        /// <summary><strong>[Should not be called by user code]</strong> Updates the data for this action and this input source</summary>
        public abstract void UpdateValue();

        /// <summary>
        /// <strong>[Should not be called by user code]</strong> Initializes the handle for the action, the size of the InputOriginInfo struct, and any other related SteamVR data.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();

            if (inputOriginInfo_size == 0)
                inputOriginInfo_size = (uint)Marshal.SizeOf(typeof(InputOriginInfo_t));
        }

        protected void UpdateOriginTrackedDeviceInfo()
        {
            if (lastOriginGetFrame != Time.frameCount) //only get once per frame
            {
                EVRInputError err = OpenVR.Input.GetOriginTrackedDeviceInfo(activeOrigin, ref inputOriginInfo, inputOriginInfo_size);

                if (err != EVRInputError.None)
                    Debug.LogError("<b>[SteamVR]</b> GetOriginTrackedDeviceInfo error (" + fullPath + "): " + err.ToString() + " handle: " + handle.ToString() + " activeOrigin: " + activeOrigin.ToString() + " active: " + active);

                lastInputOriginInfo = inputOriginInfo;
                lastOriginGetFrame = Time.frameCount;
            }
        }

        /// <summary>
        /// Gets the localized name of the device that the action corresponds to. Include as many EVRInputStringBits as you want to add to the localized string
        /// </summary>
        /// <param name="inputSource"></param>
        /// <param name="localizedParts">
        /// <list type="bullet">
        /// <item><description>VRInputString_Hand - Which hand the origin is in. ex: "Left Hand". </description></item>
        /// <item><description>VRInputString_ControllerType - What kind of controller the user has in that hand. ex: "Vive Controller". </description></item>
        /// <item><description>VRInputString_InputSource - What part of that controller is the origin. ex: "Trackpad". </description></item>
        /// <item><description>VRInputString_All - All of the above. ex: "Left Hand Vive Controller Trackpad". </description></item>
        /// </list>
        /// </param>
        public string GetLocalizedOriginPart(params EVRInputStringBits[] localizedParts)
        {
            UpdateOriginTrackedDeviceInfo();

            if (active)
            {
                return SteamVR_Input.GetLocalizedName(activeOrigin, localizedParts);
            }

            return null;
        }

        /// <summary>
        /// Gets the localized full name of the device that the action was updated by. ex: "Left Hand Vive Controller Trackpad"
        /// </summary>
        public string GetLocalizedOrigin()
        {
            UpdateOriginTrackedDeviceInfo();

            if (active)
            {
                return SteamVR_Input.GetLocalizedName(activeOrigin, EVRInputStringBits.VRInputString_All);
            }

            return null;
        }
    }

    public interface ISteamVR_Action_In : ISteamVR_Action, ISteamVR_Action_In_Source
    {
        /// <summary>
        /// <strong>[Should not be called by user code]</strong>
        /// Updates the data for all the input sources the system has detected need to be updated.
        /// </summary>
        void UpdateValues();

        /// <summary>
        /// The name of the component on the render model that caused the action to be updated (not localized)
        /// </summary>
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        string GetRenderModelComponentName(SteamVR_Input_Sources inputSource);

        /// <summary>
        /// The input source that triggered the action to be updated last
        /// </summary>
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        SteamVR_Input_Sources GetActiveDevice(SteamVR_Input_Sources inputSource);

        /// <summary>
        /// Gets the device index for the controller this action is bound to. This can be used for render models or the pose tracking system.
        /// </summary>
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        uint GetDeviceIndex(SteamVR_Input_Sources inputSource);

        /// <summary>
        /// Indicates whether or not the data for this action and specified input source has changed since the last update. Determined by SteamVR or 'changeTolerance'.
        /// </summary>
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        bool GetChanged(SteamVR_Input_Sources inputSource);


        /// <summary>
        /// Gets the localized name of the device that the action corresponds to. Include as many EVRInputStringBits as you want to add to the localized string
        /// </summary>
        /// <param name="inputSource"></param>
        /// <param name="localizedParts">
        /// <list type="bullet">
        /// <item><description>VRInputString_Hand - Which hand the origin is in. ex: "Left Hand". </description></item>
        /// <item><description>VRInputString_ControllerType - What kind of controller the user has in that hand. ex: "Vive Controller". </description></item>
        /// <item><description>VRInputString_InputSource - What part of that controller is the origin. ex: "Trackpad". </description></item>
        /// <item><description>VRInputString_All - All of the above. ex: "Left Hand Vive Controller Trackpad". </description></item>
        /// </list>
        /// </param>
        string GetLocalizedOriginPart(SteamVR_Input_Sources inputSource, params EVRInputStringBits[] localizedParts);

        /// <summary>
        /// Gets the localized full name of the device that the action was updated by. ex: "Left Hand Vive Controller Trackpad"
        /// </summary>
        /// <param name="inputSource">The device you would like to get data from. Any if the action is not device specific.</param>
        string GetLocalizedOrigin(SteamVR_Input_Sources inputSource);
    }

    public interface ISteamVR_Action_In_Source : ISteamVR_Action_Source
    {

        /// <summary>Returns true if the action has been changed in the most recent update</summary>
        bool changed { get; }

        /// <summary>Returns true if the action was changed for the previous update cycle</summary>
        bool lastChanged { get; }

        /// <summary>The Time.realtimeSinceStartup that this action was last changed.</summary>
        float changedTime { get; }

        /// <summary>The time the action was updated (Time.realtimeSinceStartup)</summary>
        float updateTime { get; }

        /// <summary>The handle to the component that triggered the action to be changed</summary>
        ulong activeOrigin { get; }

        /// <summary>The handle to the component that triggered the action to be changed in the previous update</summary>
        ulong lastActiveOrigin { get; }

        /// <summary>The input source that triggered the action to be updated</summary>
        SteamVR_Input_Sources activeDevice { get; }

        /// <summary>The device index (used by Render Models) used by the device that triggered the action to be updated</summary>
        uint trackedDeviceIndex { get; }

        /// <summary>The name of the component on the render model that caused the action to be updated (not localized)</summary>
        string renderModelComponentName { get; }

        /// <summary>Gets the localized full name of the device that the action was updated by. ex: "Left Hand Vive Controller Trackpad"</summary>
        string localizedOriginName { get; }
    }
}