forked from Deadcows/MyBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyInput.cs
217 lines (190 loc) · 6.5 KB
/
MyInput.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System.Text.RegularExpressions;
using UnityEngine;
namespace MyBox
{
public static class MyInput
{
#region Get Number Pressed
/// <summary>
/// Is number key pressed (Numpad included)
/// </summary>
public static bool GetNumberDown(int num)
{
Debug.Assert(num <= 9 && num >= 0);
switch (num)
{
case 0:
if (Input.GetKeyDown(KeyCode.Alpha0) || Input.GetKeyDown(KeyCode.Keypad0)) return true;
break;
case 1:
if (Input.GetKeyDown(KeyCode.Alpha1) || Input.GetKeyDown(KeyCode.Keypad1)) return true;
break;
case 2:
if (Input.GetKeyDown(KeyCode.Alpha2) || Input.GetKeyDown(KeyCode.Keypad2)) return true;
break;
case 3:
if (Input.GetKeyDown(KeyCode.Alpha3) || Input.GetKeyDown(KeyCode.Keypad3)) return true;
break;
case 4:
if (Input.GetKeyDown(KeyCode.Alpha4) || Input.GetKeyDown(KeyCode.Keypad4)) return true;
break;
case 5:
if (Input.GetKeyDown(KeyCode.Alpha5) || Input.GetKeyDown(KeyCode.Keypad5)) return true;
break;
case 6:
if (Input.GetKeyDown(KeyCode.Alpha6) || Input.GetKeyDown(KeyCode.Keypad6)) return true;
break;
case 7:
if (Input.GetKeyDown(KeyCode.Alpha7) || Input.GetKeyDown(KeyCode.Keypad7)) return true;
break;
case 8:
if (Input.GetKeyDown(KeyCode.Alpha8) || Input.GetKeyDown(KeyCode.Keypad8)) return true;
break;
case 9:
if (Input.GetKeyDown(KeyCode.Alpha9) || Input.GetKeyDown(KeyCode.Keypad9)) return true;
break;
}
return false;
}
/// <summary>
/// Is KeyCode is number (Numpad included)
/// </summary>
/// <returns>If KeyCode is not a number returns -1</returns>
public static int GetNumberDown(KeyCode key)
{
if (key == KeyCode.Alpha0 || key == KeyCode.Keypad0) return 0;
if (key == KeyCode.Alpha1 || key == KeyCode.Keypad1) return 1;
if (key == KeyCode.Alpha2 || key == KeyCode.Keypad2) return 2;
if (key == KeyCode.Alpha3 || key == KeyCode.Keypad3) return 3;
if (key == KeyCode.Alpha4 || key == KeyCode.Keypad4) return 4;
if (key == KeyCode.Alpha5 || key == KeyCode.Keypad5) return 5;
if (key == KeyCode.Alpha6 || key == KeyCode.Keypad6) return 6;
if (key == KeyCode.Alpha7 || key == KeyCode.Keypad7) return 7;
if (key == KeyCode.Alpha8 || key == KeyCode.Keypad8) return 8;
if (key == KeyCode.Alpha9 || key == KeyCode.Keypad9) return 9;
return -1;
}
/// <summary>
/// Is Input.GetKeyDown is number (Numpad included)
/// </summary>
/// <returns>If none pressed returns -1</returns>
public static int GetNumberDown()
{
if (Input.GetKeyDown(KeyCode.Alpha0) || Input.GetKeyDown(KeyCode.Keypad0)) return 0;
if (Input.GetKeyDown(KeyCode.Alpha1) || Input.GetKeyDown(KeyCode.Keypad1)) return 1;
if (Input.GetKeyDown(KeyCode.Alpha2) || Input.GetKeyDown(KeyCode.Keypad2)) return 2;
if (Input.GetKeyDown(KeyCode.Alpha3) || Input.GetKeyDown(KeyCode.Keypad3)) return 3;
if (Input.GetKeyDown(KeyCode.Alpha4) || Input.GetKeyDown(KeyCode.Keypad4)) return 4;
if (Input.GetKeyDown(KeyCode.Alpha5) || Input.GetKeyDown(KeyCode.Keypad5)) return 5;
if (Input.GetKeyDown(KeyCode.Alpha6) || Input.GetKeyDown(KeyCode.Keypad6)) return 6;
if (Input.GetKeyDown(KeyCode.Alpha7) || Input.GetKeyDown(KeyCode.Keypad7)) return 7;
if (Input.GetKeyDown(KeyCode.Alpha8) || Input.GetKeyDown(KeyCode.Keypad8)) return 8;
if (Input.GetKeyDown(KeyCode.Alpha9) || Input.GetKeyDown(KeyCode.Keypad9)) return 9;
return -1;
}
#endregion
/// <summary>
/// Convert KeyCode to read-friendly format
/// Full = "Left Mouse Button", otherwise "LMB"
/// </summary>
public static string ToReadableString(this KeyCode key, bool full = false)
{
switch (key)
{
case KeyCode.Mouse0:
return full ? "Left Mouse Button" : "LMB";
case KeyCode.Mouse1:
return full ? "Right Mouse Button" : "RMB";
case KeyCode.Mouse2:
return full ? "Middle Mouse Button" : "MMB";
default:
return Regex.Replace(key.ToString(), "(\\B[A-Z])", " $1");
}
}
/// <summary>
/// key1 or key2 is pressed
/// </summary>
public static bool AnyKeyDown(KeyCode key1, KeyCode key2)
{
return Input.GetKeyDown(key1) || Input.GetKeyDown(key2);
}
/// <summary>
/// key1, key2 or key3 is pressed
/// </summary>
public static bool AnyKeyDown(KeyCode key1, KeyCode key2, KeyCode key3)
{
return AnyKeyDown(key1, key2) || Input.GetKeyDown(key3);
}
/// <summary>
/// "A", "Left Arrow" and "Numpad 4"
/// </summary>
public static bool IsLeft()
{
return AnyKeyDown(KeyCode.A, KeyCode.LeftArrow, KeyCode.Keypad4);
}
/// <summary>
/// "D", "Right Arrow" and "Numpad 6"
/// </summary>
public static bool IsRight()
{
return AnyKeyDown(KeyCode.D, KeyCode.RightArrow, KeyCode.Keypad6);
}
/// <summary>
/// "W", "Up Arrow" and "Numpad 8"
/// </summary>
public static bool IsUp()
{
return AnyKeyDown(KeyCode.W, KeyCode.UpArrow, KeyCode.Keypad8);
}
/// <summary>
/// "S", "Down Arrow" and "Numpad 2"
/// </summary>
public static bool IsDown()
{
return AnyKeyDown(KeyCode.S, KeyCode.DownArrow, KeyCode.Keypad2);
}
/// <summary>
/// Roguelike movement input, where top-left is 7 and bottom-right is 3
/// </summary>
public static int KeypadDirection()
{
if (IsLeft()) return 4;
if (IsRight()) return 6;
if (IsUp()) return 8;
if (IsDown()) return 2;
if (Input.GetKeyDown(KeyCode.Keypad1)) return 1;
if (Input.GetKeyDown(KeyCode.Keypad3)) return 3;
if (Input.GetKeyDown(KeyCode.Keypad7)) return 7;
if (Input.GetKeyDown(KeyCode.Keypad9)) return 9;
return 0;
}
/// <summary>
/// Roguelike movement input on X axis
/// </summary>
/// <returns>1 if moved to right/bottom-right/top-right, -1 if moved to left/bottom-left/top-left, </returns>
public static int KeypadX()
{
if (IsLeft()) return -1;
if (IsRight()) return 1;
if (Input.GetKeyDown(KeyCode.Keypad1)) return -1;
if (Input.GetKeyDown(KeyCode.Keypad7)) return -1;
if (Input.GetKeyDown(KeyCode.Keypad3)) return 1;
if (Input.GetKeyDown(KeyCode.Keypad9)) return 1;
return 0;
}
/// <summary>
/// Roguelike movement input on Y axis
/// </summary>
/// <returns>1 if moved to top/top-left/top-right, -1 if moved to bottom/bottom-left/bottom-right</returns>
public static int KeypadY()
{
if (IsUp()) return 1;
if (IsDown()) return -1;
if (Input.GetKeyDown(KeyCode.Keypad1)) return -1;
if (Input.GetKeyDown(KeyCode.Keypad3)) return -1;
if (Input.GetKeyDown(KeyCode.Keypad7)) return 1;
if (Input.GetKeyDown(KeyCode.Keypad9)) return 1;
return 0;
}
}
}