-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinput.go
86 lines (72 loc) · 1.79 KB
/
input.go
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2025 The Ebitengine Authors
package debugui
import (
"image"
"slices"
"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
duration int
}
func (p *pointing) update() {
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]
}
if p.pressed() {
p.duration++
} else {
p.duration = 0
}
}
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 repeated(p.duration)
}
func keyRepeated(key ebiten.Key) bool {
return repeated(inpututil.KeyPressDuration(key))
}
func repeated(duration int) bool {
if duration == 1 {
return true
}
delay := ebiten.TPS() * 24 / 60
if duration < delay {
return false
}
return (duration-delay)%4 == 0
}