SteamVR_ActionSet_Manager.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. using UnityEngine;
  3. using System.Collections;
  4. using System;
  5. using Valve.VR;
  6. using System.Runtime.InteropServices;
  7. using System.Collections.Generic;
  8. using System.Text;
  9. namespace Valve.VR
  10. {
  11. /// <summary>
  12. /// Action sets are logical groupings of actions. Multiple sets can be active at one time.
  13. /// </summary>
  14. public static class SteamVR_ActionSet_Manager
  15. {
  16. public static VRActiveActionSet_t[] rawActiveActionSetArray;
  17. [NonSerialized]
  18. private static uint activeActionSetSize;
  19. private static bool changed = false;
  20. public static void Initialize()
  21. {
  22. activeActionSetSize = (uint)(Marshal.SizeOf(typeof(VRActiveActionSet_t)));
  23. }
  24. /// <summary>
  25. /// Disable all known action sets.
  26. /// </summary>
  27. public static void DisableAllActionSets()
  28. {
  29. for (int actionSetIndex = 0; actionSetIndex < SteamVR_Input.actionSets.Length; actionSetIndex++)
  30. {
  31. SteamVR_Input.actionSets[actionSetIndex].Deactivate(SteamVR_Input_Sources.Any);
  32. SteamVR_Input.actionSets[actionSetIndex].Deactivate(SteamVR_Input_Sources.LeftHand);
  33. SteamVR_Input.actionSets[actionSetIndex].Deactivate(SteamVR_Input_Sources.RightHand);
  34. }
  35. }
  36. private static int lastFrameUpdated;
  37. public static void UpdateActionStates(bool force = false)
  38. {
  39. if (force || Time.frameCount != lastFrameUpdated)
  40. {
  41. lastFrameUpdated = Time.frameCount;
  42. if (changed)
  43. {
  44. UpdateActionSetsArray();
  45. }
  46. if (rawActiveActionSetArray != null && rawActiveActionSetArray.Length > 0)
  47. {
  48. EVRInputError err = OpenVR.Input.UpdateActionState(rawActiveActionSetArray, activeActionSetSize);
  49. if (err != EVRInputError.None)
  50. Debug.LogError("<b>[SteamVR]</b> UpdateActionState error: " + err.ToString());
  51. //else Debug.Log("Action sets activated: " + activeActionSets.Length);
  52. }
  53. else
  54. {
  55. //Debug.LogWarning("No sets active");
  56. }
  57. }
  58. }
  59. public static void SetChanged()
  60. {
  61. changed = true;
  62. }
  63. private static void UpdateActionSetsArray()
  64. {
  65. List<VRActiveActionSet_t> activeActionSetsList = new List<VRActiveActionSet_t>();
  66. SteamVR_Input_Sources[] sources = SteamVR_Input_Source.GetAllSources();
  67. for (int actionSetIndex = 0; actionSetIndex < SteamVR_Input.actionSets.Length; actionSetIndex++)
  68. {
  69. SteamVR_ActionSet set = SteamVR_Input.actionSets[actionSetIndex];
  70. for (int sourceIndex = 0; sourceIndex < sources.Length; sourceIndex++)
  71. {
  72. SteamVR_Input_Sources source = sources[sourceIndex];
  73. if (set.ReadRawSetActive(source))
  74. {
  75. VRActiveActionSet_t activeSet = new VRActiveActionSet_t();
  76. activeSet.ulActionSet = set.handle;
  77. activeSet.nPriority = set.ReadRawSetPriority(source);
  78. activeSet.ulRestrictedToDevice = SteamVR_Input_Source.GetHandle(source);
  79. int insertionIndex = 0;
  80. for (insertionIndex = 0; insertionIndex < activeActionSetsList.Count; insertionIndex++)
  81. {
  82. if (activeActionSetsList[insertionIndex].nPriority > activeSet.nPriority)
  83. break;
  84. }
  85. activeActionSetsList.Insert(insertionIndex, activeSet);
  86. }
  87. }
  88. }
  89. changed = false;
  90. rawActiveActionSetArray = activeActionSetsList.ToArray();
  91. if (Application.isEditor || updateDebugTextInBuilds)
  92. UpdateDebugText();
  93. }
  94. public static SteamVR_ActionSet GetSetFromHandle(ulong handle)
  95. {
  96. for (int actionSetIndex = 0; actionSetIndex < SteamVR_Input.actionSets.Length; actionSetIndex++)
  97. {
  98. SteamVR_ActionSet set = SteamVR_Input.actionSets[actionSetIndex];
  99. if (set.handle == handle)
  100. return set;
  101. }
  102. return null;
  103. }
  104. public static string debugActiveSetListText;
  105. public static bool updateDebugTextInBuilds = false;
  106. private static void UpdateDebugText()
  107. {
  108. StringBuilder stringBuilder = new StringBuilder();
  109. for (int activeIndex = 0; activeIndex < rawActiveActionSetArray.Length; activeIndex++)
  110. {
  111. VRActiveActionSet_t set = rawActiveActionSetArray[activeIndex];
  112. stringBuilder.Append(set.nPriority);
  113. stringBuilder.Append("\t");
  114. stringBuilder.Append(SteamVR_Input_Source.GetSource(set.ulRestrictedToDevice));
  115. stringBuilder.Append("\t");
  116. stringBuilder.Append(GetSetFromHandle(set.ulActionSet).GetShortName());
  117. stringBuilder.Append("\n");
  118. }
  119. debugActiveSetListText = stringBuilder.ToString();
  120. }
  121. }
  122. }