123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using UnityEngine;
- namespace SC.XR.Unity.Module_InputSystem {
- public class InputKeys : SCModule {
- public InputDataBase inputDataBase;
- public InputKeys(InputDataBase inputDataBase) {
- this.inputDataBase = inputDataBase;
- }
- /// <summary>
- /// Long按键时长
- /// </summary>
- public float LongKeyDurationTime = 3.0f;
- /// <summary>
- /// 按键的实时信息
- /// </summary>
- public Dictionary<InputKeyCode, InputKeyState> inputKeyDic;
- /// <summary>
- /// 按键的按下状态
- /// </summary>
- public Dictionary<InputKeyCode, InputKeyState> inputKeyPressDic;
- /// <summary>
- /// 按键的简单处理,获取按下状态,存储于inputKeyPressDic列表
- /// </summary>
- ///
- Dictionary<InputKeyCode, InputKeyState> inputDataPreviousKeyDic = new Dictionary<InputKeyCode, InputKeyState>();
- InputKeyState state;
- #region Module Behavior
- public override void OnSCAwake() {
- base.OnSCAwake();
- inputKeyDic = new Dictionary<InputKeyCode, InputKeyState>();
- inputKeyPressDic = new Dictionary<InputKeyCode, InputKeyState>();
- }
- public override void OnSCLateUpdate() {
- base.OnSCLateUpdate();
- UpdateKeyEvent();
- }
- public override void OnSCDisable() {
- base.OnSCDisable();
- inputKeyDic.Clear();
- inputKeyPressDic.Clear();
- inputDataPreviousKeyDic.Clear();
- state = InputKeyState.Null;
- }
- public override void OnSCDestroy() {
- base.OnSCDestroy();
- inputKeyPressDic = null;
- inputKeyDic = null;
- inputDataPreviousKeyDic = null;
- inputDataBase = null;
- }
- #endregion
- List<InputKeyCode> listKeyCode = new List<InputKeyCode>();
- public void VaildChanged() {
- listKeyCode.Clear();
- foreach(var item in inputKeyDic) {
- if(item.Value == InputKeyState.DOWN || item.Value == InputKeyState.LONG) {
- listKeyCode.Add(item.Key);
- }
- }
- for(int i = 0; i < listKeyCode.Count; i++) {
- InputDataAddKey(listKeyCode[i], InputKeyState.UP);
- }
- UpdateKeyEvent();
- }
- protected virtual void UpdateKeyEvent() {
- lock(inputKeyDic) {
- inputKeyPressDic = new Dictionary<InputKeyCode, InputKeyState>(inputKeyDic);
- //foreach(var item in inputKeyPressDic) {
- // if(item.Value != InputKeyState.Null) {
- // //DebugMy.Log("inputKeyDic: [" + item.Key + "]:" + "[" + item.Value + "]", this);
- // DebugMy.Log(" inputKeyDic: [" + item.Key + "]:" + "[" + item.Value + "]",this);
- // }
- //}
- foreach(var item in inputKeyDic) {
- inputDataPreviousKeyDic.TryGetValue(item.Key, out state);
- if(state == item.Value) {
- inputKeyPressDic[item.Key] = InputKeyState.Null;
- }
- }
- inputDataPreviousKeyDic = new Dictionary<InputKeyCode, InputKeyState>(inputKeyDic);
- //foreach(var item in inputKeyPressDic) {
- // if(item.Value != InputKeyState.Null) {
- // //DebugMy.Log("inputKeyPressDic: [" + item.Key + "]:" + "[" + item.Value + "]", this);
- // DebugMy.Log(" inputKeyPressDic: [" + item.Key + "]:" + "[" + item.Value + "]", this);
- // }
- //}
- }
- }
- /// <summary>
- /// 向inputKeyDic列表增加按键信息
- /// </summary>
- /// <param name="inputKeyCode"> 按键KeyCode</param>
- /// <param name="inputKeyState"> 按键状态 </param>
- public void InputDataAddKey(InputKeyCode inputKeyCode, InputKeyState inputKeyState) {
- lock(inputKeyDic) {
- DebugMy.Log("InputDataAddKey: " + inputKeyCode + "=" + inputKeyState, this);
- if(!inputKeyDic.ContainsKey(inputKeyCode)) {
- inputKeyDic.Add(inputKeyCode, InputKeyState.Null);
- }
- inputKeyDic[inputKeyCode] = inputKeyState;
- //UpdateKeyEvent();
- }
- }
- /// <summary>
- /// 获取inputKeyPressDic列表中的某个按键状态
- /// </summary>
- /// <param name="inputKeyCode"> 按键KeyCode </param>
- /// <returns></returns>
- public bool GetKeyDown(InputKeyCode inputKeyCode) {
- InputKeyState inputKeyState;
- inputKeyPressDic.TryGetValue(inputKeyCode, out inputKeyState);
- if(inputKeyState == InputKeyState.DOWN) {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 获取inputKeyPressDic列表中的某个按键状态
- /// </summary>
- /// <param name="inputKeyCode"> 按键KeyCode </param>
- /// <returns></returns>
- public bool GetKeyUp(InputKeyCode inputKeyCode) {
- InputKeyState inputKeyState;
- inputKeyPressDic.TryGetValue(inputKeyCode, out inputKeyState);
- if(inputKeyState == InputKeyState.UP) {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 获取inputKeyPressDic列表中的某个按键状态
- /// </summary>
- /// <param name="inputKeyCode"> 按键KeyCode </param>
- /// <returns></returns>
- public InputKeyState GetKeyState(InputKeyCode inputKeyCode) {
- InputKeyState inputKeyState;
- inputKeyPressDic.TryGetValue(inputKeyCode, out inputKeyState);
- return inputKeyState;
- }
- /// <summary>
- /// 获取inputKeyDic列表中某个按键状态
- /// </summary>
- /// <param name="inputKeyCode"></param>
- /// <returns></returns>
- public InputKeyState GetKeyCurrentState(InputKeyCode inputKeyCode) {
- InputKeyState inputKeyState;
- inputKeyDic.TryGetValue(inputKeyCode, out inputKeyState);
- return inputKeyState;
- }
- }
- }
|