InputKeys.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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;
  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. inputKeyPressDic = new Dictionary<InputKeyCode, InputKeyState>(inputKeyDic);
  72. //foreach(var item in inputKeyPressDic) {
  73. // if(item.Value != InputKeyState.Null) {
  74. // //DebugMy.Log("inputKeyDic: [" + item.Key + "]:" + "[" + item.Value + "]", this);
  75. // DebugMy.Log(" inputKeyDic: [" + item.Key + "]:" + "[" + item.Value + "]",this);
  76. // }
  77. //}
  78. foreach(var item in inputKeyDic) {
  79. inputDataPreviousKeyDic.TryGetValue(item.Key, out state);
  80. if(state == item.Value) {
  81. inputKeyPressDic[item.Key] = InputKeyState.Null;
  82. }
  83. }
  84. inputDataPreviousKeyDic = new Dictionary<InputKeyCode, InputKeyState>(inputKeyDic);
  85. //foreach(var item in inputKeyPressDic) {
  86. // if(item.Value != InputKeyState.Null) {
  87. // //DebugMy.Log("inputKeyPressDic: [" + item.Key + "]:" + "[" + item.Value + "]", this);
  88. // DebugMy.Log(" inputKeyPressDic: [" + item.Key + "]:" + "[" + item.Value + "]", this);
  89. // }
  90. //}
  91. }
  92. }
  93. /// <summary>
  94. /// 向inputKeyDic列表增加按键信息
  95. /// </summary>
  96. /// <param name="inputKeyCode"> 按键KeyCode</param>
  97. /// <param name="inputKeyState"> 按键状态 </param>
  98. public void InputDataAddKey(InputKeyCode inputKeyCode, InputKeyState inputKeyState) {
  99. lock(inputKeyDic) {
  100. DebugMy.Log("InputDataAddKey: " + inputKeyCode + "=" + inputKeyState, this);
  101. if(!inputKeyDic.ContainsKey(inputKeyCode)) {
  102. inputKeyDic.Add(inputKeyCode, InputKeyState.Null);
  103. }
  104. inputKeyDic[inputKeyCode] = inputKeyState;
  105. //UpdateKeyEvent();
  106. }
  107. }
  108. /// <summary>
  109. /// 获取inputKeyPressDic列表中的某个按键状态
  110. /// </summary>
  111. /// <param name="inputKeyCode"> 按键KeyCode </param>
  112. /// <returns></returns>
  113. public bool GetKeyDown(InputKeyCode inputKeyCode) {
  114. InputKeyState inputKeyState;
  115. inputKeyPressDic.TryGetValue(inputKeyCode, out inputKeyState);
  116. if(inputKeyState == InputKeyState.DOWN) {
  117. return true;
  118. }
  119. return false;
  120. }
  121. /// <summary>
  122. /// 获取inputKeyPressDic列表中的某个按键状态
  123. /// </summary>
  124. /// <param name="inputKeyCode"> 按键KeyCode </param>
  125. /// <returns></returns>
  126. public bool GetKeyUp(InputKeyCode inputKeyCode) {
  127. InputKeyState inputKeyState;
  128. inputKeyPressDic.TryGetValue(inputKeyCode, out inputKeyState);
  129. if(inputKeyState == InputKeyState.UP) {
  130. return true;
  131. }
  132. return false;
  133. }
  134. /// <summary>
  135. /// 获取inputKeyPressDic列表中的某个按键状态
  136. /// </summary>
  137. /// <param name="inputKeyCode"> 按键KeyCode </param>
  138. /// <returns></returns>
  139. public InputKeyState GetKeyState(InputKeyCode inputKeyCode) {
  140. InputKeyState inputKeyState;
  141. inputKeyPressDic.TryGetValue(inputKeyCode, out inputKeyState);
  142. return inputKeyState;
  143. }
  144. /// <summary>
  145. /// 获取inputKeyDic列表中某个按键状态
  146. /// </summary>
  147. /// <param name="inputKeyCode"></param>
  148. /// <returns></returns>
  149. public InputKeyState GetKeyCurrentState(InputKeyCode inputKeyCode) {
  150. InputKeyState inputKeyState;
  151. inputKeyDic.TryGetValue(inputKeyCode, out inputKeyState);
  152. return inputKeyState;
  153. }
  154. }
  155. }