InputKeys.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 = 2.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. /// For Long Press
  27. /// </summary>
  28. private Dictionary<InputKeyCode, float> inputKeyPressTimeDic = new Dictionary<InputKeyCode, float>();
  29. private Dictionary<InputKeyCode, bool> inputKeyIsPressDic = new Dictionary<InputKeyCode, bool>();
  30. /// <summary>
  31. /// 按键的简单处理,获取按下状态,存储于inputKeyPressDic列表
  32. /// </summary>
  33. ///
  34. Dictionary<InputKeyCode, InputKeyState> inputDataPreviousKeyDic = new Dictionary<InputKeyCode, InputKeyState>();
  35. InputKeyState state;
  36. #region Module Behavior
  37. public override void OnSCAwake() {
  38. base.OnSCAwake();
  39. inputKeyDic = new Dictionary<InputKeyCode, InputKeyState>();
  40. inputKeyPressDic = new Dictionary<InputKeyCode, InputKeyState>();
  41. inputKeyPressTimeDic = new Dictionary<InputKeyCode, float>();
  42. inputKeyIsPressDic = new Dictionary<InputKeyCode, bool>();
  43. inputDataPreviousKeyDic = new Dictionary<InputKeyCode, InputKeyState>();
  44. filterKeyList = new List<InputKeyCode>();
  45. }
  46. public override void OnSCLateUpdate() {
  47. base.OnSCLateUpdate();
  48. UpdateKeyEvent();
  49. foreach (var item in inputKeyIsPressDic) {
  50. if (!inputKeyPressTimeDic.ContainsKey(item.Key)) {
  51. inputKeyPressTimeDic.Add(item.Key, 0);
  52. }
  53. if (item.Value) {
  54. if (inputKeyPressTimeDic[item.Key] >= LongKeyDurationTime) {
  55. InputDataAddKey(item.Key, InputKeyState.LONG);
  56. }
  57. inputKeyPressTimeDic[item.Key] += Time.deltaTime;
  58. } else {
  59. inputKeyPressTimeDic[item.Key] = 0;
  60. }
  61. }
  62. }
  63. public override void OnSCDisable() {
  64. base.OnSCDisable();
  65. inputKeyDic.Clear();
  66. inputKeyPressDic.Clear();
  67. inputDataPreviousKeyDic.Clear();
  68. inputKeyIsPressDic.Clear();
  69. inputKeyPressTimeDic.Clear();
  70. filterKeyList.Clear();
  71. state = InputKeyState.Null;
  72. }
  73. public override void OnSCDestroy() {
  74. base.OnSCDestroy();
  75. inputKeyPressDic = null;
  76. inputKeyDic = null;
  77. inputDataPreviousKeyDic = null;
  78. inputDataBase = null;
  79. filterKeyList = null;
  80. }
  81. #endregion
  82. List<InputKeyCode> listKeyCode = new List<InputKeyCode>();
  83. public void VaildChanged() {
  84. listKeyCode.Clear();
  85. foreach(var item in inputKeyDic) {
  86. if(item.Value == InputKeyState.DOWN || item.Value == InputKeyState.LONG) {
  87. listKeyCode.Add(item.Key);
  88. }
  89. }
  90. for(int i = 0; i < listKeyCode.Count; i++) {
  91. InputDataAddKey(listKeyCode[i], InputKeyState.UP);
  92. }
  93. UpdateKeyEvent();
  94. }
  95. protected virtual void UpdateKeyEvent() {
  96. lock(inputKeyDic) {
  97. if (filterKeyList!= null && filterKeyList.Count > 0) {
  98. for (int i = 0; i < inputKeyDic.Count; i++) {
  99. foreach (var key in filterKeyList) {
  100. if (inputKeyDic.ContainsKey(key)) {
  101. inputKeyDic.Remove(key);
  102. DebugMy.Log("Filter Key:"+ key,this,true);
  103. }
  104. }
  105. }
  106. }
  107. //for lower GC
  108. inputKeyPressDic.Clear();
  109. foreach (var key in inputKeyDic) {
  110. inputKeyPressDic.Add(key.Key, key.Value);
  111. }
  112. //foreach(var item in inputKeyPressDic) {
  113. // if(item.Value != InputKeyState.Null) {
  114. // //DebugMy.Log("inputKeyDic: [" + item.Key + "]:" + "[" + item.Value + "]", this);
  115. // DebugMy.Log(" inputKeyDic: [" + item.Key + "]:" + "[" + item.Value + "]",this);
  116. // }
  117. //}
  118. foreach(var item in inputKeyDic) {
  119. inputDataPreviousKeyDic.TryGetValue(item.Key, out state);
  120. if(state == item.Value) {
  121. inputKeyPressDic[item.Key] = InputKeyState.Null;
  122. }
  123. }
  124. //for lower GC
  125. inputDataPreviousKeyDic.Clear();
  126. foreach (var key in inputKeyDic) {
  127. inputDataPreviousKeyDic.Add(key.Key, key.Value);
  128. }
  129. foreach (var item in inputKeyPressDic) {
  130. if (!inputKeyIsPressDic.ContainsKey(item.Key)) {
  131. inputKeyIsPressDic.Add(item.Key, false);
  132. }
  133. if (item.Value == InputKeyState.DOWN) {
  134. inputKeyIsPressDic[item.Key] = true;
  135. } else if (item.Value == InputKeyState.UP) {
  136. inputKeyIsPressDic[item.Key] = false;
  137. }
  138. }
  139. //foreach (var item in inputKeyPressDic) {
  140. // if (item.Value != InputKeyState.Null) {
  141. // //DebugMy.Log("inputKeyPressDic: [" + item.Key + "]:" + "[" + item.Value + "]", this);
  142. // DebugMy.Log(" inputKeyPressDic: [" + item.Key + "]:" + "[" + item.Value + "]", this, true);
  143. // }
  144. //}
  145. }
  146. }
  147. /// <summary>
  148. /// 向inputKeyDic列表增加按键信息
  149. /// </summary>
  150. /// <param name="inputKeyCode"> 按键KeyCode</param>
  151. /// <param name="inputKeyState"> 按键状态 </param>
  152. public void InputDataAddKey(InputKeyCode inputKeyCode, InputKeyState inputKeyState) {
  153. lock(inputKeyDic) {
  154. DebugMy.Log("InputDataAddKey: " + inputKeyCode + "=" + inputKeyState, this);
  155. if(!inputKeyDic.ContainsKey(inputKeyCode)) {
  156. inputKeyDic.Add(inputKeyCode, InputKeyState.Null);
  157. }
  158. inputKeyDic[inputKeyCode] = inputKeyState;
  159. //UpdateKeyEvent();
  160. }
  161. }
  162. /// <summary>
  163. /// 获取inputKeyPressDic列表中的某个按键状态
  164. /// </summary>
  165. /// <param name="inputKeyCode"> 按键KeyCode </param>
  166. /// <returns></returns>
  167. public bool GetKeyDown(InputKeyCode inputKeyCode) {
  168. InputKeyState inputKeyState;
  169. inputKeyPressDic.TryGetValue(inputKeyCode, out inputKeyState);
  170. if(inputKeyState == InputKeyState.DOWN) {
  171. return true;
  172. }
  173. return false;
  174. }
  175. /// <summary>
  176. /// 获取inputKeyPressDic列表中的某个按键状态
  177. /// </summary>
  178. /// <param name="inputKeyCode"> 按键KeyCode </param>
  179. /// <returns></returns>
  180. public bool GetKeyUp(InputKeyCode inputKeyCode) {
  181. InputKeyState inputKeyState;
  182. inputKeyPressDic.TryGetValue(inputKeyCode, out inputKeyState);
  183. if(inputKeyState == InputKeyState.UP) {
  184. return true;
  185. }
  186. return false;
  187. }
  188. /// <summary>
  189. /// 获取inputKeyPressDic列表中的某个按键状态
  190. /// </summary>
  191. /// <param name="inputKeyCode"> 按键KeyCode </param>
  192. /// <returns></returns>
  193. public bool GetKeyTouchEnter(InputKeyCode inputKeyCode) {
  194. InputKeyState inputKeyState;
  195. inputKeyPressDic.TryGetValue(inputKeyCode, out inputKeyState);
  196. if (inputKeyState == InputKeyState.TouchEnter) {
  197. return true;
  198. }
  199. return false;
  200. }
  201. /// <summary>
  202. /// 获取inputKeyPressDic列表中的某个按键状态
  203. /// </summary>
  204. /// <param name="inputKeyCode"> 按键KeyCode </param>
  205. /// <returns></returns>
  206. public bool GetKeyTouchExit(InputKeyCode inputKeyCode) {
  207. InputKeyState inputKeyState;
  208. inputKeyPressDic.TryGetValue(inputKeyCode, out inputKeyState);
  209. if (inputKeyState == InputKeyState.TouchExit) {
  210. return true;
  211. }
  212. return false;
  213. }
  214. /// <summary>
  215. /// 获取inputKeyPressDic列表中的某个按键状态
  216. /// </summary>
  217. /// <param name="inputKeyCode"> 按键KeyCode </param>
  218. /// <returns></returns>
  219. public InputKeyState GetKeyState(InputKeyCode inputKeyCode) {
  220. InputKeyState inputKeyState;
  221. inputKeyPressDic.TryGetValue(inputKeyCode, out inputKeyState);
  222. return inputKeyState;
  223. }
  224. /// <summary>
  225. /// 获取inputKeyDic列表中某个按键状态
  226. /// </summary>
  227. /// <param name="inputKeyCode"></param>
  228. /// <returns></returns>
  229. public InputKeyState GetKeyCurrentState(InputKeyCode inputKeyCode) {
  230. InputKeyState inputKeyState;
  231. inputKeyDic.TryGetValue(inputKeyCode, out inputKeyState);
  232. return inputKeyState;
  233. }
  234. static List<InputKeyCode> filterKeyList = new List<InputKeyCode>();
  235. public static void KeyFilterGlobal(KeyFilterEnum filterType, InputKeyCode inputKeyCode = InputKeyCode.NULL) {
  236. if (filterKeyList == null) {
  237. filterKeyList = new List<InputKeyCode>();
  238. }
  239. if (filterType == KeyFilterEnum.Add) {
  240. filterKeyList.Add(inputKeyCode);
  241. } else if (filterType == KeyFilterEnum.Remove) {
  242. filterKeyList.Remove(inputKeyCode);
  243. } else {
  244. filterKeyList.Clear();
  245. }
  246. }
  247. public void KeyFilterInstance(KeyFilterEnum filterType, InputKeyCode inputKeyCode = InputKeyCode.NULL) {
  248. if (filterKeyList == null) {
  249. filterKeyList = new List<InputKeyCode>();
  250. }
  251. if (filterType == KeyFilterEnum.Add) {
  252. filterKeyList.Add(inputKeyCode);
  253. } else if (filterType == KeyFilterEnum.Remove) {
  254. filterKeyList.Remove(inputKeyCode);
  255. } else {
  256. filterKeyList.Clear();
  257. }
  258. }
  259. public enum KeyFilterEnum {
  260. Add,
  261. Remove,
  262. RemoveAll
  263. };
  264. }
  265. }