-
Notifications
You must be signed in to change notification settings - Fork 1
/
InputRebind.cs
140 lines (106 loc) · 5.25 KB
/
InputRebind.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
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
namespace Settings {
enum ControlScheme {
KEYBOARD = 0,
GAMEPAD = 1
}
// this code don't use the PlayerInput but set the actions on a class with a code like this :
// public InputActionReference jumpActionReference;
//
// private void OnEnable() {
// jumpActionReference.action.Enable();
// jumpActionReference.action.performed += Jump;
// }
// private void OnDisable() {
// jumpActionReference.action.Disable();
// jumpActionReference.action.performed -= Jump;
// }
public class InputRebind : MonoBehaviour {
[SerializeField] private InputActionReference inputActionReference;
public Transform rebindHelper;
[SerializeField] private TextMeshProUGUI keyText;
private readonly string[] ControlsExcluding = { "Mouse", "<Keyboard>/enter", "<Keyboard>/escape" };
private readonly string[] CancelingThrough = { "Mouse", "<Keyboard>/enter", "<Keyboard>/escape" };
[SerializeField] private ControlScheme controlScheme;
public bool isComposite;
// the composite part of the binding (consult the inputActionAsset for getting the correct indexs)
[SerializeField] private int compositeIndex;
private Button rebindButton;
public InputActionRebindingExtensions.RebindingOperation current_rebind;
// ui presentation :
// a list for keyboards action to rebind
// a list for gamepads action to rebind
// each item in the list contain an instance of this class
private void Start() {
rebindButton = GetComponent<Button>();
}
public void Init() {
keyText.text = GetLocalizedDisplayName();
}
public void Rebind() {
ObjectsReference.Instance.uiSettings.CancelAllRebinds();
rebindButton.enabled = false;
rebindHelper.gameObject.SetActive(true);
keyText.enabled = false;
inputActionReference.action.Disable();
// typically for this code to work, you want to set the
// inputActionAsset with one entry for keyboard
// and one for gamepad and ALWAYS follow the same order
// same if you want to work with a composite and a non composite
// for example : movement is a composite for keybord (WASD) and
// a non composite for gamepad (leftstick)
if (isComposite) {
current_rebind = inputActionReference.action.PerformInteractiveRebinding();
var binding = inputActionReference.action.bindings[compositeIndex];
if(binding.isPartOfComposite) current_rebind.WithTargetBinding(compositeIndex);
}
else {
current_rebind = inputActionReference.action.PerformInteractiveRebinding((int)controlScheme);
}
if (ControlsExcluding != null) {
foreach (var rebind in ControlsExcluding)
current_rebind.WithControlsExcluding(rebind);
}
// if you setup for gamepad, you don't want to let players use gamepad input
// in the keyboard panel
current_rebind.WithControlsExcluding(controlScheme == ControlScheme.GAMEPAD ? "<Keyboard>" : "<Gamepad>");
if (CancelingThrough != null) {
foreach (var rebind in CancelingThrough)
current_rebind.WithCancelingThrough(rebind);
}
current_rebind.OnCancel(_ => {
inputActionReference.action.Enable();
current_rebind?.Cancel();
rebindHelper.gameObject.SetActive(false);
rebindButton.enabled = true;
keyText.enabled = true;
});
current_rebind.OnComplete(_ => {
inputActionReference.action.Enable();
rebindHelper.gameObject.SetActive(false);
rebindButton.enabled = true;
keyText.enabled = true;
keyText.text = GetLocalizedDisplayName();
ObjectsReference.Instance.gameSettings.SetKeysBinding();
// equals to :
// prefs.SetString("inputBindings", inputActionAsset.SaveBindingOverridesAsJson());
// prefs.Save();
current_rebind?.Dispose();
current_rebind = null;
});
current_rebind.Start();
}
private string GetLocalizedDisplayName() {
if (isComposite) {
var path = inputActionReference.ToInputAction().GetBindingDisplayString(compositeIndex);
return path;
}
if (controlScheme == ControlScheme.GAMEPAD)
return inputActionReference.ToInputAction().GetBindingDisplayString(group:"Gamepad");
return inputActionReference.ToInputAction().GetBindingDisplayString(group:"Keyboard");
}
}
}