-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.go
61 lines (57 loc) · 1.61 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
package tinygl
type Event uint8
const (
NoEvent Event = iota
TouchStart
TouchMove
TouchEnd
TouchTap
)
// Set the position of the current touch point, or (-1, -1) if nothing currently
// touching the screen.
//
// TODO: handle multitouch. This will require an API change. In other words,
// this API is going to change at some point in the future.
func (s *Screen[T]) SetTouchState(x, y int16) {
if x >= 0 || y >= 0 {
// Currently touching.
if s.touchEvent == NoEvent {
// This is the start of a touch event.
s.touchEvent = TouchTap
s.touchX = x
s.touchY = y
s.child.HandleEvent(TouchStart, int(x), int(y))
} else if s.touchEvent == TouchTap {
// Continuing touch event.
if int(s.touchX) != int(x) || int(s.touchY) != int(y) {
// The touch point moved. Treat it as a move event.
// The caller is responsible for implementing some form of
// hysteresis to detect actual movement instead of just ADC
// noise.
s.touchEvent = TouchMove
s.child.HandleEvent(TouchMove, int(x), int(y))
}
// TODO: long press, double tap, etc.
} else if s.touchEvent == TouchMove {
s.child.HandleEvent(TouchMove, int(x), int(y))
}
} else if s.touchEvent != NoEvent {
// Not touching anymore: the touch event ended.
s.child.HandleEvent(TouchEnd, -1, -1)
if s.touchEvent == TouchTap {
// Notify the tap event.
s.child.HandleEvent(TouchTap, int(s.touchX), int(s.touchY))
}
// ...and reset the touch state to the initial state.
s.touchEvent = NoEvent
s.touchX = x
s.touchY = y
}
}
// Abs returns the absolute value of x.
func abs(x int) int {
if x < 0 {
return -x
}
return x
}