SteamVR_ActionSet.cs 23 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 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
//======= 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;
using System.Text;

namespace Valve.VR
{
    /// <summary>
    /// Action sets are logical groupings of actions. Multiple sets can be active at one time.
    /// </summary>
    [Serializable]
    public class SteamVR_ActionSet : IEquatable<SteamVR_ActionSet>, ISteamVR_ActionSet, ISerializationCallbackReceiver
    {
        public SteamVR_ActionSet() { }

        [SerializeField]
        private string actionSetPath;

        [NonSerialized]
        protected SteamVR_ActionSet_Data setData;


        /// <summary>All actions within this set (including out actions)</summary>
        public SteamVR_Action[] allActions
        {
            get
            {
                if (initialized == false)
                    Initialize();

                return setData.allActions;
            }
        }

        /// <summary>All IN actions within this set that are NOT pose or skeleton actions</summary>
        public ISteamVR_Action_In[] nonVisualInActions
        {
            get
            {
                if (initialized == false)
                    Initialize();

                return setData.nonVisualInActions;
            }
        }

        /// <summary>All pose and skeleton actions within this set</summary>
        public ISteamVR_Action_In[] visualActions
        {
            get
            {
                if (initialized == false)
                    Initialize();

                return setData.visualActions;
            }
        }

        /// <summary>All pose actions within this set</summary>
        public SteamVR_Action_Pose[] poseActions
        {
            get
            {
                if (initialized == false)
                    Initialize();

                return setData.poseActions;
            }
        }

        /// <summary>All skeleton actions within this set</summary>
        public SteamVR_Action_Skeleton[] skeletonActions
        {
            get
            {
                if (initialized == false)
                    Initialize();

                return setData.skeletonActions;
            }
        }

        /// <summary>All out actions within this set</summary>
        public ISteamVR_Action_Out[] outActionArray
        {
            get
            {
                if (initialized == false)
                    Initialize();

                return setData.outActionArray;
            }
        }


        /// <summary>The full path to this action set (ex: /actions/in/default)</summary>
        public string fullPath
        {
            get
            {
                if (initialized == false)
                    Initialize();

                return setData.fullPath;
            }
        }
        public string usage
        {
            get
            {
                if (initialized == false)
                    Initialize();

                return setData.usage;
            }
        }

        public ulong handle
        {
            get
            {
                if (initialized == false)
                    Initialize();

                return setData.handle;
            }
        }

        [NonSerialized]
        protected bool initialized = false;


        public static CreateType Create<CreateType>(string newSetPath) where CreateType : SteamVR_ActionSet, new()
        {
            CreateType actionSet = new CreateType();
            actionSet.PreInitialize(newSetPath);
            return actionSet;
        }
        public static CreateType CreateFromName<CreateType>(string newSetName) where CreateType : SteamVR_ActionSet, new()
        {
            CreateType actionSet = new CreateType();
            actionSet.PreInitialize(SteamVR_Input_ActionFile_ActionSet.GetPathFromName(newSetName));
            return actionSet;
        }

        public void PreInitialize(string newActionPath)
        {
            actionSetPath = newActionPath;

            setData = new SteamVR_ActionSet_Data();
            setData.fullPath = actionSetPath;
            setData.PreInitialize();

            initialized = true;
        }

        public virtual void FinishPreInitialize()
        {
            setData.FinishPreInitialize();
        }

        /// <summary>
        /// Initializes the handle for the action
        /// </summary>
        public virtual void Initialize(bool createNew = false, bool throwErrors = true)
        {
            if (createNew)
            {
                setData.Initialize();
            }
            else
            {
                setData = SteamVR_Input.GetActionSetDataFromPath(actionSetPath);

                if (setData == null)
                {
#if UNITY_EDITOR
                    if (throwErrors)
                    {
                        if (string.IsNullOrEmpty(actionSetPath))
                        {
                            Debug.LogError("<b>[SteamVR]</b> Action has not been assigned.");
                        }
                        else
                        {
                            Debug.LogError("<b>[SteamVR]</b> Could not find action with path: " + actionSetPath);
                        }
                    }
#endif
                }
            }

            initialized = true;
        }

        public string GetPath()
        {
            return actionSetPath;
        }

        /// <summary>
        /// Returns whether the set is currently active or not.
        /// </summary>
        /// <param name="source">The device to check. Any means all devices here (not left or right, but all)</param>
        public bool IsActive(SteamVR_Input_Sources source = SteamVR_Input_Sources.Any)
        {
            return setData.IsActive(source);
        }

        /// <summary>
        /// Returns the last time this action set was changed (set to active or inactive)
        /// </summary>
        /// <param name="source">The device to check. Any means all devices here (not left or right, but all)</param>
        public float GetTimeLastChanged(SteamVR_Input_Sources source = SteamVR_Input_Sources.Any)
        {
            return setData.GetTimeLastChanged(source);
        }

        /// <summary>
        /// Activate this set so its actions can be called
        /// </summary>
        /// <param name="disableAllOtherActionSets">Disable all other action sets at the same time</param>
        /// <param name="priority">The priority of this action set. If you have two actions bound to the same input (button) the higher priority set will override the lower priority. If they are the same priority both will execute.</param>
        /// <param name="activateForSource">Will activate this action set only for the specified source. Any if you want to activate for everything</param>
        public void Activate(SteamVR_Input_Sources activateForSource = SteamVR_Input_Sources.Any, int priority = 0, bool disableAllOtherActionSets = false)
        {
            setData.Activate(activateForSource, priority, disableAllOtherActionSets);
        }

        /// <summary>
        /// Deactivate the action set so its actions can no longer be called
        /// </summary>
        public void Deactivate(SteamVR_Input_Sources forSource = SteamVR_Input_Sources.Any)
        {
            setData.Deactivate(forSource);
        }

        /// <summary>Gets the last part of the path for this action. Removes "actions" and direction.</summary>
        public string GetShortName()
        {
            return setData.GetShortName();
        }

        /// <summary>
        /// Shows all the bindings for the actions in this set.
        /// </summary>
        /// <param name="originToHighlight">Highlights the binding of the passed in action (must be in an active set)</param>
        /// <returns></returns>
        public bool ShowBindingHints(ISteamVR_Action_In originToHighlight = null)
        {
            if (originToHighlight == null)
                return SteamVR_Input.ShowBindingHints(this);
            else
                return SteamVR_Input.ShowBindingHints(originToHighlight);
        }


        public bool ReadRawSetActive(SteamVR_Input_Sources inputSource)
        {
            return setData.ReadRawSetActive(inputSource);
        }

        public float ReadRawSetLastChanged(SteamVR_Input_Sources inputSource)
        {
            return setData.ReadRawSetLastChanged(inputSource);
        }

        public int ReadRawSetPriority(SteamVR_Input_Sources inputSource)
        {
            return setData.ReadRawSetPriority(inputSource);
        }

        public SteamVR_ActionSet_Data GetActionSetData()
        {
            return setData;
        }

        public CreateType GetCopy<CreateType>() where CreateType : SteamVR_ActionSet, new()
        {
            if (SteamVR_Input.ShouldMakeCopy()) //no need to make copies at runtime
            {
                CreateType actionSet = new CreateType();
                actionSet.actionSetPath = this.actionSetPath;
                actionSet.setData = this.setData;
                actionSet.initialized = true;
                return actionSet;
            }
            else
            {
                return (CreateType)this;
            }
        }

        public bool Equals(SteamVR_ActionSet other)
        {
            if (ReferenceEquals(null, other))
                return false;

            return this.actionSetPath == other.actionSetPath;
        }

        public override bool Equals(object other)
        {
            if (ReferenceEquals(null, other))
            {
                if (string.IsNullOrEmpty(this.actionSetPath)) //if we haven't set a path, say this action set is equal to null
                    return true;
                return false;
            }

            if (ReferenceEquals(this, other))
                return true;

            if (other is SteamVR_ActionSet)
                return this.Equals((SteamVR_ActionSet)other);

            return false;
        }

        public override int GetHashCode()
        {
            if (actionSetPath == null)
                return 0;
            else
                return actionSetPath.GetHashCode();
        }

        public static bool operator !=(SteamVR_ActionSet set1, SteamVR_ActionSet set2)
        {
            return !(set1 == set2);
        }

        public static bool operator ==(SteamVR_ActionSet set1, SteamVR_ActionSet set2)
        {
            bool set1null = (ReferenceEquals(null, set1) || string.IsNullOrEmpty(set1.actionSetPath) || set1.GetActionSetData() == null);
            bool set2null = (ReferenceEquals(null, set2) || string.IsNullOrEmpty(set2.actionSetPath) || set2.GetActionSetData() == null);

            if (set1null && set2null)
                return true;
            else if (set1null != set2null)
                return false;

            return set1.Equals(set2);
        }

        void ISerializationCallbackReceiver.OnBeforeSerialize()
        {
        }

        void ISerializationCallbackReceiver.OnAfterDeserialize()
        {
            if (setData != null)
            {
                if (setData.fullPath != actionSetPath)
                {
                    setData = SteamVR_Input.GetActionSetDataFromPath(actionSetPath);
                }
            }

            if (initialized == false)
                Initialize(false, false);
        }
    }
    /// <summary>
    /// Action sets are logical groupings of actions. Multiple sets can be active at one time.
    /// </summary>
    public class SteamVR_ActionSet_Data : ISteamVR_ActionSet
    {
        public SteamVR_ActionSet_Data() { }

        /// <summary>All actions within this set (including out actions)</summary>
        public SteamVR_Action[] allActions { get; set; }

        /// <summary>All IN actions within this set that are NOT pose or skeleton actions</summary>
        public ISteamVR_Action_In[] nonVisualInActions { get; set; }

        /// <summary>All pose and skeleton actions within this set</summary>
        public ISteamVR_Action_In[] visualActions { get; set; }

        /// <summary>All pose actions within this set</summary>
        public SteamVR_Action_Pose[] poseActions { get; set; }

        /// <summary>All skeleton actions within this set</summary>
        public SteamVR_Action_Skeleton[] skeletonActions { get; set; }

        /// <summary>All out actions within this set</summary>
        public ISteamVR_Action_Out[] outActionArray { get; set; }


        /// <summary>The full path to this action set (ex: /actions/in/default)</summary>
        public string fullPath { get; set; }
        public string usage { get; set; }


        public ulong handle { get; set; }

        protected bool[] rawSetActive = new bool[SteamVR_Input_Source.numSources];

        protected float[] rawSetLastChanged = new float[SteamVR_Input_Source.numSources];

        protected int[] rawSetPriority = new int[SteamVR_Input_Source.numSources];

        protected bool initialized = false;

        public void PreInitialize()
        {
        }

        public void FinishPreInitialize()
        {
            List<SteamVR_Action> allActionsList = new List<SteamVR_Action>();
            List<ISteamVR_Action_In> nonVisualInActionsList = new List<ISteamVR_Action_In>();
            List<ISteamVR_Action_In> visualActionsList = new List<ISteamVR_Action_In>();
            List<SteamVR_Action_Pose> poseActionsList = new List<SteamVR_Action_Pose>();
            List<SteamVR_Action_Skeleton> skeletonActionsList = new List<SteamVR_Action_Skeleton>();
            List<ISteamVR_Action_Out> outActionList = new List<ISteamVR_Action_Out>();

            if (SteamVR_Input.actions == null)
            {
                Debug.LogError("<b>[SteamVR Input]</b> Actions not initialized!");
                return;
            }

            for (int actionIndex = 0; actionIndex < SteamVR_Input.actions.Length; actionIndex++)
            {
                SteamVR_Action action = SteamVR_Input.actions[actionIndex];

                if (action.actionSet.GetActionSetData() == this)
                {
                    allActionsList.Add(action);

                    if (action is ISteamVR_Action_Boolean || action is ISteamVR_Action_Single || action is ISteamVR_Action_Vector2 || action is ISteamVR_Action_Vector3)
                    {
                        nonVisualInActionsList.Add((ISteamVR_Action_In)action);
                    }
                    else if (action is SteamVR_Action_Pose)
                    {
                        visualActionsList.Add((ISteamVR_Action_In)action);
                        poseActionsList.Add((SteamVR_Action_Pose)action);
                    }
                    else if (action is SteamVR_Action_Skeleton)
                    {
                        visualActionsList.Add((ISteamVR_Action_In)action);
                        skeletonActionsList.Add((SteamVR_Action_Skeleton)action);
                    }
                    else if (action is ISteamVR_Action_Out)
                    {
                        outActionList.Add((ISteamVR_Action_Out)action);
                    }
                    else
                    {
                        Debug.LogError("<b>[SteamVR Input]</b> Action doesn't implement known interface: " + action.fullPath);
                    }
                }
            }

            allActions = allActionsList.ToArray();
            nonVisualInActions = nonVisualInActionsList.ToArray();
            visualActions = visualActionsList.ToArray();
            poseActions = poseActionsList.ToArray();
            skeletonActions = skeletonActionsList.ToArray();
            outActionArray = outActionList.ToArray();
        }

        public void Initialize()
        {
            ulong newHandle = 0;
            EVRInputError err = OpenVR.Input.GetActionSetHandle(fullPath.ToLower(), ref newHandle);
            handle = newHandle;

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

            initialized = true;
        }

        /// <summary>
        /// Returns whether the set is currently active or not.
        /// </summary>
        /// <param name="source">The device to check. Any means all devices here (not left or right, but all)</param>
        public bool IsActive(SteamVR_Input_Sources source = SteamVR_Input_Sources.Any)
        {
            int sourceIndex = (int)source;

            if (initialized)
                return rawSetActive[sourceIndex] || rawSetActive[0];

            return false;
        }

        /// <summary>
        /// Returns the last time this action set was changed (set to active or inactive)
        /// </summary>
        /// <param name="source">The device to check. Any means all devices here (not left or right, but all)</param>
        public float GetTimeLastChanged(SteamVR_Input_Sources source = SteamVR_Input_Sources.Any)
        {
            int sourceIndex = (int)source;

            if (initialized)
                return rawSetLastChanged[sourceIndex];
            return 0;
        }

        /// <summary>
        /// Activate this set so its actions can be called
        /// </summary>
        /// <param name="disableAllOtherActionSets">Disable all other action sets at the same time</param>
        /// <param name="priority">The priority of this action set. If you have two actions bound to the same input (button) the higher priority set will override the lower priority. If they are the same priority both will execute.</param>
        /// <param name="activateForSource">Will activate this action set only for the specified source. Any if you want to activate for everything</param>
        public void Activate(SteamVR_Input_Sources activateForSource = SteamVR_Input_Sources.Any, int priority = 0, bool disableAllOtherActionSets = false)
        {
            int sourceIndex = (int)activateForSource;

            if (disableAllOtherActionSets)
                SteamVR_ActionSet_Manager.DisableAllActionSets();

            if (rawSetActive[sourceIndex] == false)
            {
                rawSetActive[sourceIndex] = true;
                SteamVR_ActionSet_Manager.SetChanged();

                rawSetLastChanged[sourceIndex] = Time.realtimeSinceStartup;
            }

            if (rawSetPriority[sourceIndex] != priority)
            {
                rawSetPriority[sourceIndex] = priority;
                SteamVR_ActionSet_Manager.SetChanged();

                rawSetLastChanged[sourceIndex] = Time.realtimeSinceStartup;
            }
        }

        /// <summary>
        /// Deactivate the action set so its actions can no longer be called
        /// </summary>
        public void Deactivate(SteamVR_Input_Sources forSource = SteamVR_Input_Sources.Any)
        {
            int sourceIndex = (int)forSource;

            if (rawSetActive[sourceIndex] != false)
            {
                rawSetLastChanged[sourceIndex] = Time.realtimeSinceStartup;
                SteamVR_ActionSet_Manager.SetChanged();
            }

            rawSetActive[sourceIndex] = false;
            rawSetPriority[sourceIndex] = 0;
        }

        private string cachedShortName;

        /// <summary>Gets the last part of the path for this action. Removes "actions" and direction.</summary>
        public string GetShortName()
        {
            if (cachedShortName == null)
            {
                cachedShortName = SteamVR_Input_ActionFile.GetShortName(fullPath);
            }

            return cachedShortName;
        }

        public bool ReadRawSetActive(SteamVR_Input_Sources inputSource)
        {
            int sourceIndex = (int)inputSource;
            return rawSetActive[sourceIndex];
        }

        public float ReadRawSetLastChanged(SteamVR_Input_Sources inputSource)
        {
            int sourceIndex = (int)inputSource;
            return rawSetLastChanged[sourceIndex];
        }

        public int ReadRawSetPriority(SteamVR_Input_Sources inputSource)
        {
            int sourceIndex = (int)inputSource;
            return rawSetPriority[sourceIndex];
        }
    }
    /// <summary>
    /// Action sets are logical groupings of actions. Multiple sets can be active at one time.
    /// </summary>
    public interface ISteamVR_ActionSet
    {
        /// <summary>All actions within this set (including out actions)</summary>
        SteamVR_Action[] allActions { get; }

        /// <summary>All IN actions within this set that are NOT pose or skeleton actions</summary>
        ISteamVR_Action_In[] nonVisualInActions { get; }

        /// <summary>All pose and skeleton actions within this set</summary>
        ISteamVR_Action_In[] visualActions { get; }

        /// <summary>All pose actions within this set</summary>
        SteamVR_Action_Pose[] poseActions { get; }

        /// <summary>All skeleton actions within this set</summary>
        SteamVR_Action_Skeleton[] skeletonActions { get; }

        /// <summary>All out actions within this set</summary>
        ISteamVR_Action_Out[] outActionArray { get; }


        /// <summary>The full path to this action set (ex: /actions/in/default)</summary>
        string fullPath { get; }

        /// <summary>How the binding UI should display this set</summary>
        string usage { get; }

        ulong handle { get; }

        bool ReadRawSetActive(SteamVR_Input_Sources inputSource);
        float ReadRawSetLastChanged(SteamVR_Input_Sources inputSource);
        int ReadRawSetPriority(SteamVR_Input_Sources inputSource);


        /// <summary>
        /// Returns whether the set is currently active or not.
        /// </summary>
        /// <param name="source">The device to check. Any means all devices here (not left or right, but all)</param>
        bool IsActive(SteamVR_Input_Sources source = SteamVR_Input_Sources.Any);

        /// <summary>
        /// Returns the last time this action set was changed (set to active or inactive)
        /// </summary>
        /// <param name="source">The device to check. Any means all devices here (not left or right, but all)</param>
        float GetTimeLastChanged(SteamVR_Input_Sources source = SteamVR_Input_Sources.Any);

        /// <summary>
        /// Activate this set so its actions can be called
        /// </summary>
        /// <param name="disableAllOtherActionSets">Disable all other action sets at the same time</param>
        /// <param name="priority">The priority of this action set. If you have two actions bound to the same input (button) the higher priority set will override the lower priority. If they are the same priority both will execute.</param>
        /// <param name="activateForSource">Will activate this action set only for the specified source. Any if you want to activate for everything</param>
        void Activate(SteamVR_Input_Sources activateForSource = SteamVR_Input_Sources.Any, int priority = 0, bool disableAllOtherActionSets = false);

        /// <summary>Deactivate the action set so its actions can no longer be called</summary>
        void Deactivate(SteamVR_Input_Sources forSource = SteamVR_Input_Sources.Any);

        /// <summary>Gets the last part of the path for this action. Removes "actions" and direction.</summary>
        string GetShortName();
    }
}