InputKeys.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. namespace SC.XR.Unity.Module_InputSystem {
  8. public class InputKeys : SCModule {
  9. public InputDataBase inputDataBase;
  10. public InputKeys(InputDataBase inputDataBase) {
  11. this.inputDataBase = inputDataBase;
  12. }
  13. /// <summary>
  14. /// Long按键时长
  15. /// </summary>
  16. public float LongKeyDurationTime = 3.0f;
  17. /// <summary>
  18. /// 按键的实时信息
  19. /// </summary>
  20. public Dictionary<InputKeyCode, InputKeyState> inputKeyDic;
  21. /// <summary>
  22. /// 按键的按下状态
  23. /// </summary>
  24. public Dictionary<InputKeyCode, InputKeyState> inputKeyPressDic = new Dictionary<InputKeyCode, InputKeyState>();
  25. /// <summary>
  26. /// 按键的简单处理,获取按下状态,存储于inputKeyPressDic列表
  27. /// </summary>
  28. ///
  29. Dictionary<InputKeyCode, InputKeyState> inputDataPreviousKeyDic = new Dictionary<InputKeyCode, InputKeyState>();
  30. InputKeyState state;
  31. #region Module Behavior
  32. public override void OnSCAwake() {
  33. base.OnSCAwake();
  34. inputKeyDic = new Dictionary<InputKeyCode, InputKeyState>();
  35. inputKeyPressDic = new Dictionary<InputKeyCode, InputKeyState>();
  36. }
  37. public override void OnSCLateUpdate() {
  38. base.OnSCLateUpdate();
  39. UpdateKeyEvent();
  40. }
  41. public override void OnSCDisable() {
  42. base.OnSCDisable();
  43. inputKeyDic.Clear();
  44. inputKeyPressDic.Clear();
  45. inputDataPreviousKeyDic.Clear();
  46. state = InputKeyState.Null;
  47. }
  48. public override void OnSCDestroy() {
  49. base.OnSCDestroy();
  50. inputKeyPressDic = null;
  51. inputKeyDic = null;
  52. inputDataPreviousKeyDic = null;
  53. inputDataBase = null;
  54. }
  55. #endregion
  56. List<InputKeyCode> listKeyCode = new List<InputKeyCode>();
  57. public void VaildChanged() {
  58. listKeyCode.Clear();
  59. foreach(var item in inputKeyDic) {
  60. if(item.Value == InputKeyState.DOWN || item.Value == InputKeyState.LONG) {
  61. listKeyCode.Add(item.Key);
  62. }
  63. }
  64. for(int i = 0; i < listKeyCode.Count; i++) {
  65. InputDataAddKey(listKeyCode[i], InputKeyState.UP);
  66. }
  67. UpdateKeyEvent();
  68. }
  69. protected virtual void UpdateKeyEvent() {
  70. lock(inputKeyDic) {
  71. //for lower GC
  72. inputKeyPressDic.Clear();
  73. foreach (var key in inputKeyDic) {
  74. inputKeyPressDic.Add(key.Key, key.Value);
  75. }
  76. //foreach(var item in inputKeyPressDic) {
  77. // if(item.Value != InputKeyState.Null) {
  78. // //DebugMy.Log("inputKeyDic: [" + item.Key + "]:" + "[" + item.Value + "]", this);
  79. // DebugMy.Log(" inputKeyDic: [" + item.Key + "]:" + "[" + item.Value + "]",this);
  80. // }
  81. //}
  82. foreach(var item in inputKeyDic) {
  83. inputDataPreviousKeyDic.TryGetValue(item.Key, out state);
  84. if(state == item.Value) {
  85. inputKeyPressDic[item.Key] = InputKeyState.Null;
  86. }
  87. }
  88. //for lower GC
  89. inputDataPreviousKeyDic.Clear();
  90. foreach (var key in inputKeyDic) {
  91. inputDataPreviousKeyDic.Add(key.Key, key.Value);
  92. }
  93. //foreach(var item in inputKeyPressDic) {
  94. // if(item.Value != InputKeyState.Null) {
  95. // //DebugMy.Log("inputKeyPressDic: [" + item.Key + "]:" + "[" + item.Value + "]", this);
  96. // DebugMy.Log(" inputKeyPressDic: [" + item.Key + "]:" + "[" + item.Value + "]", this);
  97. // }
  98. //}
  99. }
  100. }
  101. /// <summary>
  102. /// 向inputKeyDic列表增加按键信息
  103. /// </summary>
  104. /// <param name="inputKeyCode"> 按键KeyCode</param>
  105. /// <param name="inputKeyState"> 按键状态 </param>
  106. public void InputDataAddKey(InputKeyCode inputKeyCode, InputKeyState inputKeyState) {
  107. lock(inputKeyDic) {
  108. DebugMy.Log("InputDataAddKey: " + inputKeyCode + "=" + inputKeyState, this);
  109. if(!inputKeyDic.ContainsKey(inputKeyCode)) {
  110. inputKeyDic.Add(inputKeyCode, InputKeyState.Null);
  111. }
  112. inputKeyDic[inputKeyCode] = inputKeyState;
  113. //UpdateKeyEvent();
  114. }
  115. }
  116. /// <summary>
  117. /// 获取inputKeyPressDic列表中的某个按键状态
  118. /// </summary>
  119. /// <param name="inputKeyCode"> 按键KeyCode </param>
  120. /// <returns></returns>
  121. public bool GetKeyDown(InputKeyCode inputKeyCode) {
  122. InputKeyState inputKeyState;
  123. inputKeyPressDic.TryGetValue(inputKeyCode, out inputKeyState);
  124. if(inputKeyState == InputKeyState.DOWN) {
  125. return true;
  126. }
  127. return false;
  128. }
  129. /// <summary>
  130. /// 获取inputKeyPressDic列表中的某个按键状态
  131. /// </summary>
  132. /// <param name="inputKeyCode"> 按键KeyCode </param>
  133. /// <returns></returns>
  134. public bool GetKeyUp(InputKeyCode inputKeyCode) {
  135. InputKeyState inputKeyState;
  136. inputKeyPressDic.TryGetValue(inputKeyCode, out inputKeyState);
  137. if(inputKeyState == InputKeyState.UP) {
  138. return true;
  139. }
  140. return false;
  141. }
  142. /// <summary>
  143. /// 获取inputKeyPressDic列表中的某个按键状态
  144. /// </summary>
  145. /// <param name="inputKeyCode"> 按键KeyCode </param>
  146. /// <returns></returns>
  147. public InputKeyState GetKeyState(InputKeyCode inputKeyCode) {
  148. InputKeyState inputKeyState;
  149. inputKeyPressDic.TryGetValue(inputKeyCode, out inputKeyState);
  150. return inputKeyState;
  151. }
  152. /// <summary>
  153. /// 获取inputKeyDic列表中某个按键状态
  154. /// </summary>
  155. /// <param name="inputKeyCode"></param>
  156. /// <returns></returns>
  157. public InputKeyState GetKeyCurrentState(InputKeyCode inputKeyCode) {
  158. InputKeyState inputKeyState;
  159. inputKeyDic.TryGetValue(inputKeyCode, out inputKeyState);
  160. return inputKeyState;
  161. }
  162. }
  163. }