-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinput.go
More file actions
119 lines (99 loc) · 2.7 KB
/
Copy pathinput.go
File metadata and controls
119 lines (99 loc) · 2.7 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2025 The Ebitengine Authors
package debugui
import (
"image"
"slices"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
type pointing struct {
justPressedTouchIDs []ebiten.TouchID
touchIDs []ebiten.TouchID
hasPrimaryTouchID bool
primaryTouchID ebiten.TouchID
repeat inputRepeat
}
func (p *pointing) update(now time.Duration) {
p.justPressedTouchIDs = inpututil.AppendJustPressedTouchIDs(p.justPressedTouchIDs[:0])
p.touchIDs = ebiten.AppendTouchIDs(p.touchIDs[:0])
if len(p.touchIDs) == 0 {
p.hasPrimaryTouchID = false
p.primaryTouchID = 0
} else if !p.hasPrimaryTouchID {
p.hasPrimaryTouchID = true
p.primaryTouchID = p.touchIDs[0]
}
p.repeat.update(p.pressed(), now)
}
func (p *pointing) isTouchActive() bool {
if !p.hasPrimaryTouchID {
return false
}
return slices.Contains(p.touchIDs, p.primaryTouchID)
}
func (p *pointing) position() image.Point {
if p.isTouchActive() {
return image.Pt(ebiten.TouchPosition(p.primaryTouchID))
}
return image.Pt(ebiten.CursorPosition())
}
func (p *pointing) pressed() bool {
if p.isTouchActive() {
return true
}
return ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft)
}
func (p *pointing) justPressed() bool {
if p.isTouchActive() {
return slices.Contains(p.justPressedTouchIDs, p.primaryTouchID)
}
return inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft)
}
func (p *pointing) repeated() bool {
return p.repeat.repeated
}
func (c *Context) isInputIgnored() bool {
return ebiten.Tick() < c.ignoreInputUntilTick
}
func (c *Context) pointingPressed() bool {
return !c.isInputIgnored() && c.pointing.pressed()
}
func (c *Context) pointingJustPressed() bool {
return !c.isInputIgnored() && c.pointing.justPressed()
}
func (c *Context) pointingRepeated() bool {
return !c.isInputIgnored() && c.pointing.repeated()
}
func (c *Context) keyRepeated(key ebiten.Key) bool {
return !c.isInputIgnored() && c.keyRepeats[key].repeated
}
func (c *Context) updateInput() {
c.now = currentTimerTime()
c.pointing.update(c.now)
for _, key := range [...]ebiten.Key{
ebiten.KeyLeft, ebiten.KeyRight, ebiten.KeyUp, ebiten.KeyDown,
ebiten.KeyHome, ebiten.KeyEnd, ebiten.KeyBackspace, ebiten.KeyDelete,
} {
c.keyRepeats[key].update(ebiten.IsKeyPressed(key), c.now)
}
}
type inputRepeat struct {
timer timer
repeated bool
}
func (r *inputRepeat) update(pressed bool, now time.Duration) {
r.timer.update(now)
r.repeated = false
if !pressed {
r.timer.stop()
return
}
if !r.timer.active() {
r.repeated = true
r.timer.start(now, 400*time.Millisecond, time.Second/15)
return
}
r.repeated = r.timer.fired()
}