SteamVR_Input_ActionSetPropertyEditor.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.CodeDom;
  4. using Microsoft.CSharp;
  5. using System.IO;
  6. using System.CodeDom.Compiler;
  7. using System.Linq;
  8. using System.Collections.Generic;
  9. using System.Reflection;
  10. using System.Linq.Expressions;
  11. using System;
  12. namespace Valve.VR
  13. {
  14. [CustomPropertyDrawer(typeof(SteamVR_ActionSet))]
  15. public class SteamVR_Input_ActionSetPropertyEditor : PropertyDrawer
  16. {
  17. protected SteamVR_ActionSet[] actionSets;
  18. protected string[] enumItems;
  19. public int selectedIndex = notInitializedIndex;
  20. protected const int notInitializedIndex = -1;
  21. protected const int noneIndex = 0;
  22. protected int addIndex = 1;
  23. protected const string defaultPathTemplate = " \u26A0 Missing action set: {0}";
  24. protected string defaultPathLabel = null;
  25. protected void Awake()
  26. {
  27. actionSets = SteamVR_Input.GetActionSets();
  28. if (actionSets != null && actionSets.Length > 0)
  29. {
  30. List<string> enumList = actionSets.Select(actionSet => actionSet.fullPath).ToList();
  31. enumList.Insert(noneIndex, "None");
  32. //replace forward slashes with backslack instead
  33. for (int index = 0; index < enumList.Count; index++)
  34. enumList[index] = enumList[index].Replace('/', '\\');
  35. enumList.Add("Add...");
  36. enumItems = enumList.ToArray();
  37. }
  38. else
  39. {
  40. enumItems = new string[] { "None", "Add..." };
  41. }
  42. addIndex = enumItems.Length - 1;
  43. /*
  44. //keep sub menus:
  45. for (int index = 0; index < enumItems.Length; index++)
  46. if (enumItems[index][0] == '/')
  47. enumItems[index] = enumItems[index].Substring(1);
  48. */
  49. }
  50. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  51. {
  52. float height = base.GetPropertyHeight(property, label);
  53. SerializedProperty actionPathProperty = property.FindPropertyRelative("actionSetPath");
  54. if (string.IsNullOrEmpty(actionPathProperty.stringValue) == false)
  55. {
  56. if (selectedIndex == 0)
  57. return height * 2;
  58. }
  59. return height;
  60. }
  61. // Draw the property inside the given rect
  62. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  63. {
  64. if (SteamVR_Input.actions == null || SteamVR_Input.actions.Length == 0)
  65. {
  66. EditorGUI.BeginProperty(position, label, property);
  67. EditorGUI.LabelField(position, "Please generate SteamVR Input actions");
  68. EditorGUI.EndProperty();
  69. return;
  70. }
  71. if (enumItems == null || enumItems.Length == 0)
  72. {
  73. Awake();
  74. }
  75. // Using BeginProperty / EndProperty on the parent property means that
  76. // prefab override logic works on the entire property.
  77. EditorGUI.BeginProperty(position, label, property);
  78. SerializedProperty actionPathProperty = property.FindPropertyRelative("actionSetPath");
  79. string currentPath = null;
  80. if (actionPathProperty != null)
  81. {
  82. currentPath = actionPathProperty.stringValue;
  83. if (string.IsNullOrEmpty(currentPath) == false)
  84. {
  85. for (int actionSetIndex = 0; actionSetIndex < actionSets.Length; actionSetIndex++)
  86. {
  87. if (actionSets[actionSetIndex].fullPath == currentPath)
  88. {
  89. selectedIndex = actionSetIndex + 1; // account for none option
  90. break;
  91. }
  92. }
  93. }
  94. }
  95. if (selectedIndex == notInitializedIndex)
  96. selectedIndex = 0;
  97. Rect labelPosition = position;
  98. labelPosition.width = EditorGUIUtility.labelWidth;
  99. EditorGUI.LabelField(labelPosition, label);
  100. Rect fieldPosition = position;
  101. fieldPosition.x = (labelPosition.x + labelPosition.width);
  102. fieldPosition.width = EditorGUIUtility.currentViewWidth - (labelPosition.x + labelPosition.width) - 5 - 16;
  103. if (selectedIndex == 0 && string.IsNullOrEmpty(currentPath) == false)
  104. {
  105. if (defaultPathLabel == null)
  106. defaultPathLabel = string.Format(defaultPathTemplate, currentPath);
  107. Rect defaultLabelPosition = position;
  108. defaultLabelPosition.y = position.y + fieldPosition.height / 2f;
  109. EditorGUI.LabelField(defaultLabelPosition, defaultPathLabel);
  110. }
  111. Rect objectRect = position;
  112. objectRect.x = fieldPosition.x + fieldPosition.width + 15;
  113. objectRect.width = 10;
  114. bool showInputWindow = false;
  115. int wasSelected = selectedIndex;
  116. selectedIndex = EditorGUI.Popup(fieldPosition, selectedIndex, enumItems);
  117. if (selectedIndex != wasSelected)
  118. {
  119. if (selectedIndex == noneIndex || selectedIndex == notInitializedIndex)
  120. {
  121. selectedIndex = noneIndex;
  122. actionPathProperty.stringValue = null;
  123. }
  124. else if (selectedIndex == addIndex)
  125. {
  126. selectedIndex = wasSelected; // don't change the index
  127. showInputWindow = true;
  128. }
  129. else
  130. {
  131. int actionIndex = selectedIndex - 1; // account for none option
  132. actionPathProperty.stringValue = actionSets[actionIndex].GetPath();
  133. //property.objectReferenceValue = actions[actionIndex];
  134. }
  135. property.serializedObject.ApplyModifiedProperties();
  136. }
  137. EditorGUI.EndProperty();
  138. if (showInputWindow)
  139. SteamVR_Input_EditorWindow.ShowWindow(); //show the input window so they can add a new actionset
  140. }
  141. }
  142. }