2
0

SteamVR_ActionSet_Manager.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. if (OpenVR.Input != null)
  49. {
  50. EVRInputError err = OpenVR.Input.UpdateActionState(rawActiveActionSetArray, activeActionSetSize);
  51. if (err != EVRInputError.None)
  52. Debug.LogError("<b>[SteamVR]</b> UpdateActionState error: " + err.ToString());
  53. //else Debug.Log("Action sets activated: " + activeActionSets.Length);
  54. }
  55. }
  56. else
  57. {
  58. //Debug.LogWarning("No sets active");
  59. }
  60. }
  61. }
  62. public static void SetChanged()
  63. {
  64. changed = true;
  65. }
  66. private static void UpdateActionSetsArray()
  67. {
  68. List<VRActiveActionSet_t> activeActionSetsList = new List<VRActiveActionSet_t>();
  69. SteamVR_Input_Sources[] sources = SteamVR_Input_Source.GetAllSources();
  70. for (int actionSetIndex = 0; actionSetIndex < SteamVR_Input.actionSets.Length; actionSetIndex++)
  71. {
  72. SteamVR_ActionSet set = SteamVR_Input.actionSets[actionSetIndex];
  73. for (int sourceIndex = 0; sourceIndex < sources.Length; sourceIndex++)
  74. {
  75. SteamVR_Input_Sources source = sources[sourceIndex];
  76. if (set.ReadRawSetActive(source))
  77. {
  78. VRActiveActionSet_t activeSet = new VRActiveActionSet_t();
  79. activeSet.ulActionSet = set.handle;
  80. activeSet.nPriority = set.ReadRawSetPriority(source);
  81. activeSet.ulRestrictedToDevice = SteamVR_Input_Source.GetHandle(source);
  82. int insertionIndex = 0;
  83. for (insertionIndex = 0; insertionIndex < activeActionSetsList.Count; insertionIndex++)
  84. {
  85. if (activeActionSetsList[insertionIndex].nPriority > activeSet.nPriority)
  86. break;
  87. }
  88. activeActionSetsList.Insert(insertionIndex, activeSet);
  89. }
  90. }
  91. }
  92. changed = false;
  93. rawActiveActionSetArray = activeActionSetsList.ToArray();
  94. if (Application.isEditor || updateDebugTextInBuilds)
  95. UpdateDebugText();
  96. }
  97. public static SteamVR_ActionSet GetSetFromHandle(ulong handle)
  98. {
  99. for (int actionSetIndex = 0; actionSetIndex < SteamVR_Input.actionSets.Length; actionSetIndex++)
  100. {
  101. SteamVR_ActionSet set = SteamVR_Input.actionSets[actionSetIndex];
  102. if (set.handle == handle)
  103. return set;
  104. }
  105. return null;
  106. }
  107. public static string debugActiveSetListText;
  108. public static bool updateDebugTextInBuilds = false;
  109. private static void UpdateDebugText()
  110. {
  111. StringBuilder stringBuilder = new StringBuilder();
  112. for (int activeIndex = 0; activeIndex < rawActiveActionSetArray.Length; activeIndex++)
  113. {
  114. VRActiveActionSet_t set = rawActiveActionSetArray[activeIndex];
  115. stringBuilder.Append(set.nPriority);
  116. stringBuilder.Append("\t");
  117. stringBuilder.Append(SteamVR_Input_Source.GetSource(set.ulRestrictedToDevice));
  118. stringBuilder.Append("\t");
  119. stringBuilder.Append(GetSetFromHandle(set.ulActionSet).GetShortName());
  120. stringBuilder.Append("\n");
  121. }
  122. debugActiveSetListText = stringBuilder.ToString();
  123. }
  124. }
  125. }