-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputRepeatDelay.cs
More file actions
66 lines (54 loc) · 2.32 KB
/
InputRepeatDelay.cs
File metadata and controls
66 lines (54 loc) · 2.32 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
namespace Celeste.Mod.CollabLobbyUI {
public class InputRepeatDelay {
/* A class to implement a button input delay that when held
* - allows the action to perform right away
* - then has an inital delay before triggering again
* - after that uses a faster moving delay to trigger
* e.g. like when holding down Left/Right Arrow Key in a text editor.
*/
public ButtonBinding InputA { get; private set; }
public ButtonBinding InputB { get; private set; }
public float InitialDelay { get; private set; }
public float MoveDelay { get; private set; }
protected bool IsDownA => InputA.Check;
protected bool IsDownB => InputB.Check;
public bool CanMove => timeSinceMoved > (moveFast ? MoveDelay : InitialDelay) || !useInitialDelay;
private bool useInitialDelay = false, moveFast = false;
private float timeSinceMoved = 0;
public InputRepeatDelay(ButtonBinding A, ButtonBinding B = null, float initialDelay = 0.3f, float moveDelay = 0.05f) {
SetBinds(A, B);
SetDelays(initialDelay, moveDelay);
}
public void SetBinds(ButtonBinding A, ButtonBinding B = null) {
// can be used with a single button, where InputB gets set to A also
InputA = A;
InputB = (B != null) ? B : A;
}
public void SetDelays(float initialDelay, float moveDelay) {
InitialDelay = initialDelay;
MoveDelay = moveDelay;
}
public void Update(float time) {
timeSinceMoved += time;
if (!IsDownA && !IsDownB) {
// always reset so that first press has no delays
timeSinceMoved = 0;
useInitialDelay = false;
moveFast = false;
}
}
public bool Check(ButtonBinding bb) {
if (bb != InputA && bb != InputB)
return false;
return bb.Check && CanMove;
}
public void Triggered() {
timeSinceMoved = 0;
// this is where moveFast gets set, so that MoveDelay only gets used after first trigger had InitialDelay
if (!useInitialDelay)
useInitialDelay = true;
else
moveFast = true;
}
}
}