-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputMappingCursor.cs
314 lines (294 loc) · 10.6 KB
/
InputMappingCursor.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using UnityEngine;
enum MappingState
{
PLAYER,
BUTTON
}
/// <summary>
/// Invoked to control button mapping sequence in main menu lobby.
/// Runs either in Player or Button Mapping state, will be initalized
/// via the constructor and Check() will be called in Update().
/// Is turned off until Activate() is called.
///
/// Keeps track of cursor via either playerToMap and buttonToMap.
/// </summary>
public class InputMappingCursor
{
int playerToMap;
/// <summary> 0: accelerate, 1: decelerate, 2: left, 3: right</summary>
int buttonToMap;
bool buttonIsBeingHeld;
bool joyButton0InUse;
MappingState map;
InputManager inputManager;
MainMenu mainMenu;
readonly Vector2 inputMethodTextSize = new Vector2(52f, 6.2f);
readonly Vector2 actionTextSize = new Vector2(15f, 6.2f);
public SimpleQuad cursor;
public InputMappingCursor(GameObject go)
{
inputManager = InputManager.Instance;
mainMenu = go.GetComponent<MainMenu>();
cursor = go.AddComponent<SimpleQuad>();
cursor.enabled = false;
}
/// <summary>
/// Enable cursor and begin assigning procedure.
/// </summary>
public void Activate()
{
for (int i = 0; i < 4; i++)
inputManager.characterInputs[i].UnmapPlayer();
playerToMap = 0;
buttonToMap = -1;
buttonIsBeingHeld = true;
map = MappingState.PLAYER;
cursor.ShowAtWorld(
mainMenu.InputInfoDisplays[0].ShowNextKey(),
inputMethodTextSize.x,
inputMethodTextSize.y);
cursor.enabled = true;
}
/// <summary>
/// Hide cursor.
/// </summary>
public void Hide()
{
cursor.enabled = false;
}
/// <summary>
/// Check at every frame (called in MainMenu's update) which input
/// is being pressed and act accordingly.
/// </summary>
/// <returns>0: do nothing, -1: back to main menu, 1: start game</returns>
public int Check()
{
// Finished mapping, we can signal to start the game.
if (playerToMap == GameManager.Instance.NumPlayers)
{
if (!buttonIsBeingHeld && Input.GetAxisRaw("Cancel") == 1)
{
buttonIsBeingHeld = true;
playerToMap--;
buttonToMap = -1;
inputManager.characterInputs[playerToMap].UnmapPlayer();
cursor.enabled = true;
cursor.ShowAtWorld(
mainMenu.InputInfoDisplays[playerToMap].Reset(),
inputMethodTextSize.x,
inputMethodTextSize.y);
mainMenu.SetTitleToDefault();
return 0;
}
else
{
return 1;
}
}
// Check if any controller is holding A/button0
joyButton0InUse = false;
for (int i = 0; i < 8; i++)
if(Input.GetKeyDown(inputManager.joyButton0[i]))
joyButton0InUse = true;
// Check that undo/proceed buttons are not being held such that we only
// act on them once
if (Input.GetAxisRaw("Cancel") == 0 && Input.GetKey(KeyCode.Return) == false && !joyButton0InUse)
buttonIsBeingHeld = false;
if (map == MappingState.PLAYER)
{
if (UndoPlayer())
return -1;
if (buttonIsBeingHeld)
return 0;
if (PlayerCheckGamepad())
return 0;
if (PlayerCheckKeyboard())
return 0;
}
if (map == MappingState.BUTTON)
{
if (UndoButton())
return 0;
if (ButtonCheck())
return 0;
}
return 0;
}
/// <summary>
/// Checks for the undo button and jump back one action of go back to player mapping.
/// </summary>
/// <returns>true if the undo button was pressed; else false.</returns>
bool UndoButton()
{
if (Input.GetAxisRaw("Cancel") == 1)
{
if (!buttonIsBeingHeld)
{
buttonIsBeingHeld = true;
buttonToMap--;
if (buttonToMap == -1)
{
map = MappingState.PLAYER;
cursor.ShowAtWorld(
mainMenu.InputInfoDisplays[playerToMap].UndoKey(),
inputMethodTextSize.x,
inputMethodTextSize.y);
}
else
{
inputManager.characterInputs[playerToMap].UnmapButton(buttonToMap);
cursor.ShowAtWorld(
mainMenu.InputInfoDisplays[playerToMap].UndoKey() + Vector3.left*0.1f,
actionTextSize.x,
actionTextSize.y);
}
return true;
}
}
return false;
}
/// <summary>
/// Loops over all available buttons to see if any one should be assigned.
/// We assign the button to the current buttonToMap and increment.
/// </summary>
/// <returns>true if any button was assigned; else false.</returns>
bool ButtonCheck()
{
// Loop over all buttons
for (int i = 0; i < inputManager.allKeys.Length; i++)
{
// If one was pressed
if (Input.GetKeyDown(inputManager.allKeys[i]))
{
// And it hasn't been assigned yet
if (!inputManager.isButtonMapped(inputManager.allKeys[i]))
{
// We map it, increment buttonToMap and update cursor
inputManager.characterInputs[playerToMap].MapButton(
buttonToMap,
inputManager.allKeys[i]
);
buttonToMap++;
if (buttonToMap == 4)
{
mainMenu.InputInfoDisplays[playerToMap].ShowNextKey();
playerToMap++;
map = MappingState.PLAYER;
if (playerToMap != GameManager.Instance.NumPlayers)
{
cursor.ShowAtWorld(
mainMenu.InputInfoDisplays[playerToMap].ShowNextKey(),
inputMethodTextSize.x,
inputMethodTextSize.y);
}
else
{
cursor.enabled = false;
mainMenu.SetTitleToReadyPrompt(
InputManager.Instance.characterInputs[0].inputMode == InputMode.GAMEPAD);
}
}
else
{
cursor.ShowAtWorld(
mainMenu.InputInfoDisplays[playerToMap].ShowNextKey() + Vector3.left * 0.1f,
actionTextSize.x,
actionTextSize.y);
}
return true;
}
}
}
return false;
}
/// <summary>
/// Check if we pressed the undo key and jump back one character
/// or signal that we can go back to the main menu.
/// </summary>
/// <returns>
/// true if we press undo at playerToMap == 0;
/// false if we didn't press anything or went back one player;
/// </returns>
bool UndoPlayer()
{
if (Input.GetAxisRaw("Cancel") == 1)
{
if (!buttonIsBeingHeld)
{
buttonIsBeingHeld = true;
if (playerToMap == 0)
{
return true;
}
else
{
mainMenu.InputInfoDisplays[playerToMap].HideAllButName();
playerToMap--;
inputManager.characterInputs[playerToMap].UnmapPlayer();
cursor.ShowAtWorld(
mainMenu.InputInfoDisplays[playerToMap].Reset(),
inputMethodTextSize.x,
inputMethodTextSize.y);
}
}
}
return false;
}
/// <summary>
/// Check if we pressed any new button0 and map that player.
/// </summary>
/// <returns>
/// true if a new controller pressed button0 and we mapped it; false if no new button0 was pressed.
/// </returns>
bool PlayerCheckGamepad()
{
for (int i = 0; i < 8; i++)
{
if(Input.GetKeyDown(inputManager.joyButton0[i]))
{
if (!inputManager.isJoyMapped(i))
{
inputManager.characterInputs[playerToMap]
.MapPlayer(InputMode.GAMEPAD, i);
mainMenu.InputInfoDisplays[playerToMap].SetToGamepad();
playerToMap++;
buttonToMap = -1;
if (playerToMap < GameManager.Instance.NumPlayers)
{
cursor.ShowAtWorld(
mainMenu.InputInfoDisplays[playerToMap].ShowNextKey(),
inputMethodTextSize.x,
inputMethodTextSize.y);
}
else
{
cursor.enabled = false;
mainMenu.SetTitleToReadyPrompt(
InputManager.Instance.characterInputs[0].inputMode == InputMode.GAMEPAD);
}
return true;
}
}
}
return false;
}
/// <summary>
/// Checks if we pressed return and moves to buttonUI if that happened.
/// </summary>
/// <returns>true if we pressed return on the keyboard; false otherwise.</returns>
bool PlayerCheckKeyboard()
{
if (Input.GetKeyDown(KeyCode.Return))
{
inputManager.characterInputs[playerToMap].MapPlayer(InputMode.KEYBOARD, -1);
map = MappingState.BUTTON;
buttonToMap = 0;
cursor.ShowAtWorld(
mainMenu.InputInfoDisplays[playerToMap].ShowNextKey() + Vector3.left * 0.1f,
actionTextSize.x,
actionTextSize.y);
return true;
}
return false;
}
}