Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
MultiKeyboard.cs
浏览该文件的文档.
1using Engine;
2using Engine.Input;
3
4public static class MultiKeyboard {
6 // ReSharper disable MemberHidesStaticFromOuterClass
7 public bool IsConnected;
8
9 public Key? LastKey;
10
11 public char? LastChar;
12 // ReSharper restore MemberHidesStaticFromOuterClass
13
14 public bool[] KeysDownArray = new bool[Enum.GetValues<Key>().Length];
15
16 public bool[] KeysDownOnceArray = new bool[Enum.GetValues<Key>().Length];
17
18 public double[] KeysDownRepeatArray = new double[Enum.GetValues<Key>().Length];
19 }
20
21 static double KeyFirstRepeatTime = 0.3;
22
23 static double KeyNextRepeatTime = 0.04;
24
25 static KeyboardData[] _KeyboardData = [new(), new(), new(), new()];
26
27 // ReSharper disable UnusedAutoPropertyAccessor.Global
28 public static bool BackButtonQuitsApp { get; set; }
29 // ReSharper restore UnusedAutoPropertyAccessor.Global
30
31 public static event Action<int, Key> KeyDown;
32
33 public static event Action<int, Key> KeyUp;
34
35 public static event Action<int, char> CharacterEntered;
36
37 public static bool IsConnected(int keyboardIndex) => _KeyboardData[keyboardIndex].IsConnected;
38
39 public static bool IsKeyDown(int keyboardIndex, Key key) => _KeyboardData[keyboardIndex].KeysDownArray[(int)key];
40
41 public static bool IsKeyDownOnce(int keyboardIndex, Key key) => _KeyboardData[keyboardIndex].KeysDownOnceArray[(int)key];
42
43 public static bool IsKeyDownRepeat(int keyboardIndex, Key key) {
44 double num = _KeyboardData[keyboardIndex].KeysDownRepeatArray[(int)key];
45 return num < 0.0 || (num != 0.0 && Time.FrameStartTime >= num);
46 }
47
48 public static Key? LastKey(int keyboardIndex) => _KeyboardData[keyboardIndex].LastKey;
49
50 public static char? LastChar(int keyboardIndex) => _KeyboardData[keyboardIndex].LastChar;
51
52 public static void Clear() {
53 for (int i = 0; i < 4; i++) {
54 _KeyboardData[i].LastKey = null;
55 _KeyboardData[i].LastChar = null;
56 for (int j = 0; j < _KeyboardData[i].KeysDownArray.Length; j++) {
57 _KeyboardData[i].KeysDownArray[j] = false;
58 _KeyboardData[i].KeysDownOnceArray[j] = false;
59 _KeyboardData[i].KeysDownRepeatArray[j] = 0.0;
60 }
61 }
62 }
63
64 internal static void BeforeFrame() { }
65
66 internal static void AfterFrame() {
67 for (int i = 0; i < 4; i++) {
68 if (BackButtonQuitsApp && IsKeyDownOnce(i, Key.Back)) {
69 Window.Close();
70 }
71 _KeyboardData[i].LastKey = null;
72 _KeyboardData[i].LastChar = null;
73 for (int j = 0; j < _KeyboardData[i].KeysDownOnceArray.Length; j++) {
74 _KeyboardData[i].KeysDownOnceArray[j] = false;
75 }
76 for (int k = 0; k < _KeyboardData[i].KeysDownRepeatArray.Length; k++) {
77 if (_KeyboardData[i].KeysDownArray[k]) {
78 if (_KeyboardData[i].KeysDownRepeatArray[k] < 0.0) {
79 _KeyboardData[i].KeysDownRepeatArray[k] = Time.FrameStartTime + KeyFirstRepeatTime;
80 }
81 else if (Time.FrameStartTime >= _KeyboardData[i].KeysDownRepeatArray[k]) {
82 _KeyboardData[i].KeysDownRepeatArray[k] = Math.Max(
84 _KeyboardData[i].KeysDownRepeatArray[k] + KeyNextRepeatTime
85 );
86 }
87 }
88 else {
89 _KeyboardData[i].KeysDownRepeatArray[k] = 0.0;
90 }
91 }
92 }
93 }
94
95 // ReSharper disable UnusedMember.Local
96 static void SetIsConnected(int keyboardIndex, bool value) {
97 _KeyboardData[keyboardIndex].IsConnected = value;
98 }
99
100 static bool ProcessKeyDown(int keyboardIndex, Key key) {
101 if (!Window.IsActive) {
102 return false;
103 }
104 _KeyboardData[keyboardIndex].LastKey = key;
105 if (!_KeyboardData[keyboardIndex].KeysDownArray[(int)key]) {
106 _KeyboardData[keyboardIndex].KeysDownArray[(int)key] = true;
107 _KeyboardData[keyboardIndex].KeysDownOnceArray[(int)key] = true;
108 _KeyboardData[keyboardIndex].KeysDownRepeatArray[(int)key] = -1.0;
109 }
110 KeyDown?.Invoke(keyboardIndex, key);
111 return true;
112 }
113
114 static bool ProcessKeyUp(int keyboardIndex, Key key) {
115 if (!Window.IsActive) {
116 return false;
117 }
118 if (_KeyboardData[keyboardIndex].KeysDownArray[(int)key]) {
119 _KeyboardData[keyboardIndex].KeysDownArray[(int)key] = false;
120 }
121 KeyUp?.Invoke(keyboardIndex, key);
122 return true;
123 }
124
125 static bool ProcessCharacterEntered(int keyboardIndex, char ch) {
126 if (!Window.IsActive) {
127 return false;
128 }
129 _KeyboardData[keyboardIndex].LastChar = ch;
130 CharacterEntered?.Invoke(keyboardIndex, ch);
131 return true;
132 }
133 // ReSharper restore UnusedMember.Local
134
135 internal static void Initialize() { }
136
137 internal static void Dispose() { }
138}
static double FrameStartTime
定义 Time.cs:42
static bool IsActive
static void Close()
static bool IsKeyDownRepeat(int keyboardIndex, Key key)
static void Dispose()
static bool IsKeyDownOnce(int keyboardIndex, Key key)
static bool IsConnected(int keyboardIndex)
static Action< int, char > CharacterEntered
static void AfterFrame()
static void SetIsConnected(int keyboardIndex, bool value)
static bool BackButtonQuitsApp
static bool IsKeyDown(int keyboardIndex, Key key)
static double KeyFirstRepeatTime
static Action< int, Key > KeyUp
static KeyboardData[] _KeyboardData
static void Initialize()
static ? char LastChar(int keyboardIndex)
static double KeyNextRepeatTime
static bool ProcessKeyDown(int keyboardIndex, Key key)
static bool ProcessCharacterEntered(int keyboardIndex, char ch)
static void Clear()
static Action< int, Key > KeyDown
static bool ProcessKeyUp(int keyboardIndex, Key key)
static ? Key LastKey(int keyboardIndex)
static void BeforeFrame()