-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputManager.cs
More file actions
172 lines (122 loc) · 5.21 KB
/
Copy pathInputManager.cs
File metadata and controls
172 lines (122 loc) · 5.21 KB
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
using UnityEngine;
using UnityEngine.InputSystem;
using System;
using TMPro;
public static class InputManager{
private static PlayerInputActions inputActions;
public static PlayerInputActions InputActions => inputActions ??= new();
private static InputActionRebindingExtensions.RebindingOperation operation;
public static event Action<InputAction, int> onRebindStarted;
public static event Action onRebindCompleted;
public static event Action onRebindCanceled;
static InputManager(){
LoadBindingsOverrides();
}
public static void StartInteractiveRebind(string actionName, int bindingIndex, TMP_Text statusText, TMP_Text bindingText, GameObject rebindOverlay){
InputAction action = inputActions.asset.FindAction(actionName);
if(action is null){
Debug.LogWarning("Input action is null");
return;
}
if(action.bindings.Count <= bindingIndex){
Debug.LogWarning($"Invalid binding index for {action.name}");
return;
}
if(action.bindings[bindingIndex].isComposite){
int firstIndex = bindingIndex + 1;
if(firstIndex < action.bindings.Count && action.bindings[firstIndex].isPartOfComposite){
PerformInteractiveRebind(action, firstIndex, statusText, bindingText, rebindOverlay, true);
}
}
else{
PerformInteractiveRebind(action, bindingIndex, statusText, bindingText, rebindOverlay, false);
}
}
private static void PerformInteractiveRebind(InputAction action, int bindingIndex, TMP_Text statusText, TMP_Text bindingText, GameObject rebindOverlay, bool compositeBinding){
void CleanUp(){
operation?.Dispose();
operation = null;
action.Enable();
}
if(action is null){
Debug.LogWarning("Input action is null");
return;
}
if(bindingIndex < 0){
Debug.LogWarning($"Invalid binding index for {action.name}");
return;
}
operation?.Cancel();
action.Disable();
operation = action.PerformInteractiveRebinding(bindingIndex)
.OnComplete(operation =>{
CleanUp();
if(rebindOverlay != null){
rebindOverlay.SetActive(false);
}
if(compositeBinding){
int nextBindingIndex = bindingIndex + 1;
if(nextBindingIndex < action.bindings.Count && action.bindings[nextBindingIndex].isPartOfComposite){
PerformInteractiveRebind(action, nextBindingIndex, statusText, bindingText, rebindOverlay, compositeBinding);
}
}
SaveBindingsOverrides();
onRebindCompleted?.Invoke();
})
.OnCancel(operation =>{
CleanUp();
if(rebindOverlay != null){
rebindOverlay.SetActive(false);
}
onRebindCanceled?.Invoke();
})
.WithCancelingThrough("<Keyboard>/escape");
string partName = default;
if(action.bindings[bindingIndex].isPartOfComposite){
partName = $"Binding '{action.bindings[bindingIndex].name}'. ";
}
if(rebindOverlay != null){
rebindOverlay.SetActive(true);
}
if(statusText != null){
string text = !string.IsNullOrEmpty(operation.expectedControlType)
? $"{partName}Waiting for {operation.expectedControlType} input..."
: $"{partName}Waiting for input...";
statusText.text = text;
}
if((rebindOverlay == null || statusText == null) && bindingText != null){
bindingText.text = "Waiting...";
}
onRebindStarted?.Invoke(action, bindingIndex);
operation.Start();
}
public static string GetBindingName(string actionName, int bindingIndex, InputBinding.DisplayStringOptions displayStringOptions = default){
InputAction action = InputActions.asset.FindAction(actionName);
return action.GetBindingDisplayString(bindingIndex, displayStringOptions);
}
private static void SaveBindingsOverrides(){
PlayerPrefs.SetString("rebinds", InputActions.SaveBindingOverridesAsJson());
}
private static void LoadBindingsOverrides(){
string rebinds = PlayerPrefs.GetString("rebinds");
if(!string.IsNullOrEmpty(rebinds)){
InputActions.LoadBindingOverridesFromJson(rebinds);
}
}
public static void ResetToDefault(string actionName, int bindingIndex){
InputAction action = InputActions.asset.FindAction(actionName);
if(action is null || action.bindings.Count <= bindingIndex){
Debug.Log("Could not find action or binding");
return;
}
if(action.bindings[bindingIndex].isComposite){
for(int i = bindingIndex; i < action.bindings.Count && action.bindings[i].isComposite; i++){
action.RemoveBindingOverride(i);
}
}
else{
action.RemoveBindingOverride(bindingIndex);
}
SaveBindingsOverrides();
}
}