-
Notifications
You must be signed in to change notification settings - Fork 7
/
child.go
176 lines (159 loc) · 3.95 KB
/
child.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package furex
import (
"image"
"math"
"time"
"github.com/hajimehoshi/ebiten/v2"
)
type child struct {
absolute bool
item *View
bounds image.Rectangle
isButtonPressed bool
isMouseLeftButtonHandler bool
isMouseEntered bool
handledTouchID ebiten.TouchID
swipe
}
type swipe struct {
downX, downY int
upX, upY int
downTime time.Time
upTime time.Time
swipeDir SwipeDirection
swipeTouchID ebiten.TouchID
}
func (c *child) HandleJustPressedTouchID(
frame *image.Rectangle, touchID ebiten.TouchID, x, y int) bool {
var result = false
if c.checkButtonHandlerStart(frame, touchID, x, y) {
result = true
}
if !result && c.checkTouchHandlerStart(frame, touchID, x, y) {
result = true
}
c.checkSwipeHandlerStart(frame, touchID, x, y)
return result
}
func (c *child) HandleJustReleasedTouchID(
frame *image.Rectangle, touchID ebiten.TouchID, x, y int) {
c.checkTouchHandlerEnd(frame, touchID, x, y)
c.checkButtonHandlerEnd(frame, touchID, x, y)
c.checkSwipeHandlerEnd(frame, touchID, x, y)
}
func (c *child) checkTouchHandlerStart(frame *image.Rectangle, touchID ebiten.TouchID, x, y int) bool {
touchHandler, ok := c.item.Handler.(TouchHandler)
if ok {
if isInside(frame, x, y) {
if touchHandler.HandleJustPressedTouchID(touchID, x, y) {
c.handledTouchID = touchID
return true
}
}
}
return false
}
func (c *child) checkTouchHandlerEnd(frame *image.Rectangle, touchID ebiten.TouchID, x, y int) {
touchHandler, ok := c.item.Handler.(TouchHandler)
if ok {
if c.handledTouchID == touchID {
touchHandler.HandleJustReleasedTouchID(touchID, x, y)
c.handledTouchID = -1
}
}
}
func (c *child) checkSwipeHandlerStart(frame *image.Rectangle, touchID ebiten.TouchID, x, y int) bool {
_, ok := c.item.Handler.(SwipeHandler)
if ok {
if isInside(frame, x, y) {
c.swipeTouchID = touchID
c.swipe.downTime = time.Now()
c.swipe.downX, c.swipe.downY = x, y
return true
}
}
return false
}
func (c *child) checkSwipeHandlerEnd(frame *image.Rectangle, touchID ebiten.TouchID, x, y int) bool {
swipeHandler, ok := c.item.Handler.(SwipeHandler)
if ok {
if c.swipeTouchID != touchID {
return false
}
c.swipeTouchID = -1
c.upTime = time.Now()
c.upX, c.upY = x, y
if c.checkSwipe() {
swipeHandler.HandleSwipe(c.swipeDir)
return true
}
}
return false
}
const swipeThresholdDist = 50.
const swipeThresholdTime = time.Millisecond * 300
func (c *child) checkSwipe() bool {
dur := c.upTime.Sub(c.downTime)
if dur > swipeThresholdTime {
return false
}
deltaX := float64(c.downX - c.upX)
if math.Abs(deltaX) >= swipeThresholdDist {
if deltaX > 0 {
c.swipeDir = SwipeDirectionLeft
} else {
c.swipeDir = SwipeDirectionRight
}
return true
}
deltaY := float64(c.downY - c.upY)
if math.Abs(deltaY) >= swipeThresholdDist {
if deltaY > 0 {
c.swipeDir = SwipeDirectionUp
} else {
c.swipeDir = SwipeDirectionDown
}
return true
}
return false
}
func (c *child) checkButtonHandlerStart(frame *image.Rectangle, touchID ebiten.TouchID, x, y int) bool {
button, ok := c.item.Handler.(ButtonHandler)
if ok {
for {
if button, ok := c.item.Handler.(NotButton); ok {
if !button.IsButton() {
break
}
}
if isInside(frame, x, y) {
if !c.isButtonPressed {
c.isButtonPressed = true
c.handledTouchID = touchID
button.HandlePress(x, y, touchID)
}
return true
} else if c.handledTouchID == touchID {
c.handledTouchID = -1
}
break
}
}
return false
}
func (c *child) checkButtonHandlerEnd(frame *image.Rectangle, touchID ebiten.TouchID, x, y int) {
button, ok := c.item.Handler.(ButtonHandler)
if ok {
if c.handledTouchID == touchID {
if c.isButtonPressed {
c.isButtonPressed = false
c.handledTouchID = -1
if x == 0 && y == 0 {
button.HandleRelease(x, y, false)
} else {
button.HandleRelease(x, y, !isInside(frame, x, y))
}
}
}
}
}