DebugManager.Actions.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #if ENABLE_INPUT_SYSTEM && ENABLE_INPUT_SYSTEM_PACKAGE
  2. #define USE_INPUT_SYSTEM
  3. using UnityEngine.InputSystem;
  4. using UnityEngine.InputSystem.Controls;
  5. #endif
  6. using System.Collections.Generic;
  7. namespace UnityEngine.Rendering
  8. {
  9. internal enum DebugAction
  10. {
  11. EnableDebugMenu,
  12. PreviousDebugPanel,
  13. NextDebugPanel,
  14. Action,
  15. MakePersistent,
  16. MoveVertical,
  17. MoveHorizontal,
  18. Multiplier,
  19. ResetAll,
  20. DebugActionCount
  21. }
  22. enum DebugActionRepeatMode
  23. {
  24. Never,
  25. Delay
  26. }
  27. public sealed partial class DebugManager
  28. {
  29. const string kEnableDebugBtn1 = "Enable Debug Button 1";
  30. const string kEnableDebugBtn2 = "Enable Debug Button 2";
  31. const string kDebugPreviousBtn = "Debug Previous";
  32. const string kDebugNextBtn = "Debug Next";
  33. const string kValidateBtn = "Debug Validate";
  34. const string kPersistentBtn = "Debug Persistent";
  35. const string kDPadVertical = "Debug Vertical";
  36. const string kDPadHorizontal = "Debug Horizontal";
  37. const string kMultiplierBtn = "Debug Multiplier";
  38. const string kResetBtn = "Debug Reset";
  39. DebugActionDesc[] m_DebugActions;
  40. DebugActionState[] m_DebugActionStates;
  41. void RegisterActions()
  42. {
  43. m_DebugActions = new DebugActionDesc[(int)DebugAction.DebugActionCount];
  44. m_DebugActionStates = new DebugActionState[(int)DebugAction.DebugActionCount];
  45. var enableDebugMenu = new DebugActionDesc();
  46. enableDebugMenu.buttonTriggerList.Add(new[] { kEnableDebugBtn1, kEnableDebugBtn2 });
  47. enableDebugMenu.keyTriggerList.Add(new[] { KeyCode.LeftControl, KeyCode.Backspace });
  48. enableDebugMenu.repeatMode = DebugActionRepeatMode.Never;
  49. AddAction(DebugAction.EnableDebugMenu, enableDebugMenu);
  50. var resetDebugMenu = new DebugActionDesc();
  51. resetDebugMenu.buttonTriggerList.Add(new[] { kResetBtn, kEnableDebugBtn2 });
  52. resetDebugMenu.keyTriggerList.Add(new[] { KeyCode.LeftAlt, KeyCode.Backspace });
  53. resetDebugMenu.repeatMode = DebugActionRepeatMode.Never;
  54. AddAction(DebugAction.ResetAll, resetDebugMenu);
  55. var nextDebugPanel = new DebugActionDesc();
  56. nextDebugPanel.buttonTriggerList.Add(new[] { kDebugNextBtn });
  57. nextDebugPanel.repeatMode = DebugActionRepeatMode.Never;
  58. AddAction(DebugAction.NextDebugPanel, nextDebugPanel);
  59. var previousDebugPanel = new DebugActionDesc();
  60. previousDebugPanel.buttonTriggerList.Add(new[] { kDebugPreviousBtn });
  61. previousDebugPanel.repeatMode = DebugActionRepeatMode.Never;
  62. AddAction(DebugAction.PreviousDebugPanel, previousDebugPanel);
  63. var validate = new DebugActionDesc();
  64. validate.buttonTriggerList.Add(new[] { kValidateBtn });
  65. validate.repeatMode = DebugActionRepeatMode.Never;
  66. AddAction(DebugAction.Action, validate);
  67. var persistent = new DebugActionDesc();
  68. persistent.buttonTriggerList.Add(new[] { kPersistentBtn });
  69. persistent.repeatMode = DebugActionRepeatMode.Never;
  70. AddAction(DebugAction.MakePersistent, persistent);
  71. var multiplier = new DebugActionDesc();
  72. multiplier.buttonTriggerList.Add(new[] { kMultiplierBtn });
  73. multiplier.repeatMode = DebugActionRepeatMode.Delay;
  74. validate.repeatDelay = 0f;
  75. AddAction(DebugAction.Multiplier, multiplier);
  76. AddAction(DebugAction.MoveVertical, new DebugActionDesc { axisTrigger = kDPadVertical, repeatMode = DebugActionRepeatMode.Delay, repeatDelay = 0.16f });
  77. AddAction(DebugAction.MoveHorizontal, new DebugActionDesc { axisTrigger = kDPadHorizontal, repeatMode = DebugActionRepeatMode.Delay, repeatDelay = 0.16f });
  78. }
  79. void AddAction(DebugAction action, DebugActionDesc desc)
  80. {
  81. int index = (int)action;
  82. m_DebugActions[index] = desc;
  83. m_DebugActionStates[index] = new DebugActionState();
  84. }
  85. void SampleAction(int actionIndex)
  86. {
  87. var desc = m_DebugActions[actionIndex];
  88. var state = m_DebugActionStates[actionIndex];
  89. // Disable all input events if we're using the new input system
  90. #if ENABLE_LEGACY_INPUT_MANAGER
  91. //bool canSampleAction = (state.actionTriggered == false) || (desc.repeatMode == DebugActionRepeatMode.Delay && state.timer > desc.repeatDelay);
  92. if (state.runningAction == false)
  93. {
  94. // Check button triggers
  95. for (int buttonListIndex = 0; buttonListIndex < desc.buttonTriggerList.Count; ++buttonListIndex)
  96. {
  97. var buttons = desc.buttonTriggerList[buttonListIndex];
  98. bool allButtonPressed = true;
  99. foreach (var button in buttons)
  100. {
  101. allButtonPressed = Input.GetButton(button);
  102. if (!allButtonPressed)
  103. break;
  104. }
  105. if (allButtonPressed)
  106. {
  107. state.TriggerWithButton(buttons, 1f);
  108. break;
  109. }
  110. }
  111. // Check axis triggers
  112. if (desc.axisTrigger != "")
  113. {
  114. float axisValue = Input.GetAxis(desc.axisTrigger);
  115. if (axisValue != 0f)
  116. state.TriggerWithAxis(desc.axisTrigger, axisValue);
  117. }
  118. // Check key triggers
  119. for (int keyListIndex = 0; keyListIndex < desc.keyTriggerList.Count; ++keyListIndex)
  120. {
  121. bool allKeyPressed = true;
  122. var keys = desc.keyTriggerList[keyListIndex];
  123. foreach (var key in keys)
  124. {
  125. allKeyPressed = Input.GetKey(key);
  126. if (!allKeyPressed)
  127. break;
  128. }
  129. if (allKeyPressed)
  130. {
  131. state.TriggerWithKey(keys, 1f);
  132. break;
  133. }
  134. }
  135. }
  136. #elif USE_INPUT_SYSTEM
  137. // TODO: make the new input system work
  138. #endif
  139. }
  140. void UpdateAction(int actionIndex)
  141. {
  142. var desc = m_DebugActions[actionIndex];
  143. var state = m_DebugActionStates[actionIndex];
  144. if (state.runningAction)
  145. state.Update(desc);
  146. }
  147. internal void UpdateActions()
  148. {
  149. for (int actionIndex = 0; actionIndex < m_DebugActions.Length; ++actionIndex)
  150. {
  151. UpdateAction(actionIndex);
  152. SampleAction(actionIndex);
  153. }
  154. }
  155. internal float GetAction(DebugAction action)
  156. {
  157. return m_DebugActionStates[(int)action].actionState;
  158. }
  159. void RegisterInputs()
  160. {
  161. #if UNITY_EDITOR
  162. var inputEntries = new List<InputManagerEntry>
  163. {
  164. new InputManagerEntry { name = kEnableDebugBtn1, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "left ctrl", altBtnPositive = "joystick button 8" },
  165. new InputManagerEntry { name = kEnableDebugBtn2, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "backspace", altBtnPositive = "joystick button 9" },
  166. new InputManagerEntry { name = kResetBtn, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "left alt", altBtnPositive = "joystick button 1" },
  167. new InputManagerEntry { name = kDebugNextBtn, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "page down", altBtnPositive = "joystick button 5" },
  168. new InputManagerEntry { name = kDebugPreviousBtn, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "page up", altBtnPositive = "joystick button 4" },
  169. new InputManagerEntry { name = kValidateBtn, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "return", altBtnPositive = "joystick button 0" },
  170. new InputManagerEntry { name = kPersistentBtn, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "right shift", altBtnPositive = "joystick button 2" },
  171. new InputManagerEntry { name = kMultiplierBtn, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "left shift", altBtnPositive = "joystick button 3" },
  172. new InputManagerEntry { name = kDPadHorizontal, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "right", btnNegative = "left", gravity = 1000f, deadZone = 0.001f, sensitivity = 1000f },
  173. new InputManagerEntry { name = kDPadVertical, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "up", btnNegative = "down", gravity = 1000f, deadZone = 0.001f, sensitivity = 1000f },
  174. new InputManagerEntry { name = kDPadVertical, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Seventh, btnPositive = "up", btnNegative = "down", gravity = 1000f, deadZone = 0.001f, sensitivity = 1000f },
  175. new InputManagerEntry { name = kDPadHorizontal, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Sixth, btnPositive = "right", btnNegative = "left", gravity = 1000f, deadZone = 0.001f, sensitivity = 1000f },
  176. };
  177. InputRegistering.RegisterInputs(inputEntries);
  178. #endif
  179. }
  180. }
  181. class DebugActionDesc
  182. {
  183. public List<string[]> buttonTriggerList = new List<string[]>();
  184. public string axisTrigger = "";
  185. public List<KeyCode[]> keyTriggerList = new List<KeyCode[]>();
  186. public DebugActionRepeatMode repeatMode = DebugActionRepeatMode.Never;
  187. public float repeatDelay;
  188. }
  189. class DebugActionState
  190. {
  191. enum DebugActionKeyType
  192. {
  193. Button,
  194. Axis,
  195. Key
  196. }
  197. DebugActionKeyType m_Type;
  198. string[] m_PressedButtons;
  199. string m_PressedAxis = "";
  200. KeyCode[] m_PressedKeys;
  201. bool[] m_TriggerPressedUp;
  202. float m_Timer;
  203. internal bool runningAction { get; private set; }
  204. internal float actionState { get; private set; }
  205. void Trigger(int triggerCount, float state)
  206. {
  207. actionState = state;
  208. runningAction = true;
  209. m_Timer = 0f;
  210. m_TriggerPressedUp = new bool[triggerCount];
  211. for (int i = 0; i < m_TriggerPressedUp.Length; ++i)
  212. m_TriggerPressedUp[i] = false;
  213. }
  214. public void TriggerWithButton(string[] buttons, float state)
  215. {
  216. m_Type = DebugActionKeyType.Button;
  217. m_PressedButtons = buttons;
  218. m_PressedAxis = "";
  219. Trigger(buttons.Length, state);
  220. }
  221. public void TriggerWithAxis(string axis, float state)
  222. {
  223. m_Type = DebugActionKeyType.Axis;
  224. m_PressedAxis = axis;
  225. Trigger(1, state);
  226. }
  227. public void TriggerWithKey(KeyCode[] keys, float state)
  228. {
  229. m_Type = DebugActionKeyType.Key;
  230. m_PressedKeys = keys;
  231. m_PressedAxis = "";
  232. Trigger(keys.Length, state);
  233. }
  234. void Reset()
  235. {
  236. runningAction = false;
  237. m_Timer = 0f;
  238. m_TriggerPressedUp = null;
  239. }
  240. public void Update(DebugActionDesc desc)
  241. {
  242. // Always reset this so that the action can only be caught once until repeat/reset
  243. actionState = 0f;
  244. if (m_TriggerPressedUp != null)
  245. {
  246. m_Timer += Time.deltaTime;
  247. for (int i = 0; i < m_TriggerPressedUp.Length; ++i)
  248. {
  249. if (m_Type == DebugActionKeyType.Button)
  250. m_TriggerPressedUp[i] |= Input.GetButtonUp(m_PressedButtons[i]);
  251. else if (m_Type == DebugActionKeyType.Axis)
  252. m_TriggerPressedUp[i] |= Mathf.Approximately(Input.GetAxis(m_PressedAxis), 0f);
  253. else
  254. m_TriggerPressedUp[i] |= Input.GetKeyUp(m_PressedKeys[i]);
  255. }
  256. bool allTriggerUp = true;
  257. foreach (bool value in m_TriggerPressedUp)
  258. allTriggerUp &= value;
  259. if (allTriggerUp || (m_Timer > desc.repeatDelay && desc.repeatMode == DebugActionRepeatMode.Delay))
  260. Reset();
  261. }
  262. }
  263. }
  264. }