-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtimer.go
More file actions
56 lines (47 loc) · 1015 Bytes
/
Copy pathtimer.go
File metadata and controls
56 lines (47 loc) · 1015 Bytes
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2026 The Ebitengine Authors
package debugui
import "time"
type timer struct {
running bool
last time.Duration
remaining time.Duration
interval time.Duration
didFire bool
}
func (t *timer) update(now time.Duration) {
t.didFire = false
if !t.running {
return
}
wasExpired := t.expired()
t.remaining -= now - t.last
t.last = now
if wasExpired || !t.expired() {
return
}
t.didFire = true
if t.interval > 0 {
// Skip missed repeats while preserving the repeat cadence.
t.remaining += (-t.remaining/t.interval + 1) * t.interval
}
}
func (t *timer) start(now, duration, interval time.Duration) {
t.running = true
t.last = now
t.remaining = duration
t.interval = interval
t.didFire = false
}
func (t *timer) stop() {
*t = timer{}
}
func (t *timer) active() bool {
return t.running
}
func (t *timer) expired() bool {
return t.running && t.remaining <= 0
}
func (t *timer) fired() bool {
return t.didFire
}