-
Notifications
You must be signed in to change notification settings - Fork 0
/
DelayedToggle.cs
64 lines (53 loc) · 1.36 KB
/
DelayedToggle.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
using System.Diagnostics;
namespace Ostsoft.Games.SuperKinectroid
{
public class DelayedToggle
{
private Stopwatch _timer = new Stopwatch();
private Stopwatch _bounceTimer = new Stopwatch();
private long delayOn;
private long bounceDelay;
public DelayedToggle(long delayOn = 2000, long bounceDelay = 1000)
{
this.delayOn = delayOn;
this.bounceDelay = bounceDelay;
}
public void setActive(bool state)
{
if (_bounceTimer.IsRunning)
{
if (state)
{
_bounceTimer.Restart();
}
if (_bounceTimer.ElapsedMilliseconds < bounceDelay)
{
return;
}
_bounceTimer.Reset();
}
if (state)
{
_timer.Start();
}
else
{
_timer.Reset();
}
}
public bool isDelayOnActive()
{
return _timer.IsRunning;
}
public bool isTrigged()
{
if (_timer.ElapsedMilliseconds > delayOn)
{
_timer.Reset();
_bounceTimer.Start();
return true;
}
return false;
}
}
}