| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package gesture |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"time" |
| 12 |
|
| 13 |
"golang.org/x/exp/shiny/screen" |
| 14 |
"golang.org/x/mobile/event/mouse" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
const ( |
| 22 |
|
| 23 |
dragThreshold = 10 |
| 24 |
|
| 25 |
doublePressThreshold = 300 * time.Millisecond |
| 26 |
longPressThreshold = 500 * time.Millisecond |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
type Type uint8 |
| 31 |
|
| 32 |
const ( |
| 33 |
|
| 34 |
|
| 35 |
TypeStart Type = 0 |
| 36 |
TypeEnd Type = 1 |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
TypeIsLongPress Type = 10 |
| 57 |
TypeIsDoublePress Type = 11 |
| 58 |
TypeIsDrag Type = 12 |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
TypeTap Type = 20 |
| 67 |
TypeDrag Type = 21 |
| 68 |
|
| 69 |
|
| 70 |
typeInternal Type = 100 |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
typeDoublePressSchedule Type = 100 |
| 79 |
typeDoublePressResolve Type = 101 |
| 80 |
|
| 81 |
typeLongPressSchedule Type = 110 |
| 82 |
typeLongPressResolve Type = 111 |
| 83 |
) |
| 84 |
|
| 85 |
func (t Type) String() string { |
| 86 |
switch t { |
| 87 |
case TypeStart: |
| 88 |
return "Start" |
| 89 |
case TypeEnd: |
| 90 |
return "End" |
| 91 |
case TypeIsLongPress: |
| 92 |
return "IsLongPress" |
| 93 |
case TypeIsDoublePress: |
| 94 |
return "IsDoublePress" |
| 95 |
case TypeIsDrag: |
| 96 |
return "IsDrag" |
| 97 |
case TypeTap: |
| 98 |
return "Tap" |
| 99 |
case TypeDrag: |
| 100 |
return "Drag" |
| 101 |
default: |
| 102 |
return fmt.Sprintf("gesture.Type(%d)", t) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
type Point struct { |
| 108 |
X, Y float32 |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
type Event struct { |
| 113 |
|
| 114 |
Type Type |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
Drag bool |
| 123 |
LongPress bool |
| 124 |
DoublePress bool |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
InitialPos Point |
| 129 |
|
| 130 |
|
| 131 |
CurrentPos Point |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
Time time.Time |
| 140 |
|
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
type internalEvent struct { |
| 145 |
eventFilter *EventFilter |
| 146 |
|
| 147 |
typ Type |
| 148 |
x, y float32 |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
pressCounter uint32 |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
type EventFilter struct { |
| 160 |
EventDeque screen.EventDeque |
| 161 |
|
| 162 |
inProgress bool |
| 163 |
drag bool |
| 164 |
longPress bool |
| 165 |
doublePress bool |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
initialPos Point |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
pressButton mouse.Button |
| 174 |
|
| 175 |
|
| 176 |
pressCounter uint32 |
| 177 |
} |
| 178 |
|
| 179 |
func (f *EventFilter) sendFirst(t Type, x, y float32, now time.Time) { |
| 180 |
if t >= typeInternal { |
| 181 |
f.EventDeque.SendFirst(internalEvent{ |
| 182 |
eventFilter: f, |
| 183 |
typ: t, |
| 184 |
x: x, |
| 185 |
y: y, |
| 186 |
pressCounter: f.pressCounter, |
| 187 |
}) |
| 188 |
return |
| 189 |
} |
| 190 |
f.EventDeque.SendFirst(Event{ |
| 191 |
Type: t, |
| 192 |
Drag: f.drag, |
| 193 |
LongPress: f.longPress, |
| 194 |
DoublePress: f.doublePress, |
| 195 |
InitialPos: f.initialPos, |
| 196 |
CurrentPos: Point{ |
| 197 |
X: x, |
| 198 |
Y: y, |
| 199 |
}, |
| 200 |
|
| 201 |
Time: now, |
| 202 |
}) |
| 203 |
} |
| 204 |
|
| 205 |
func (f *EventFilter) sendAfter(e internalEvent, sleep time.Duration) { |
| 206 |
time.Sleep(sleep) |
| 207 |
f.EventDeque.SendFirst(e) |
| 208 |
} |
| 209 |
|
| 210 |
func (f *EventFilter) end(x, y float32, now time.Time) { |
| 211 |
f.sendFirst(TypeEnd, x, y, now) |
| 212 |
f.inProgress = false |
| 213 |
f.drag = false |
| 214 |
f.longPress = false |
| 215 |
f.doublePress = false |
| 216 |
f.initialPos = Point{} |
| 217 |
f.pressButton = mouse.ButtonNone |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
func (f *EventFilter) Filter(e interface{}) interface{} { |
| 224 |
switch e := e.(type) { |
| 225 |
case internalEvent: |
| 226 |
if e.eventFilter != f { |
| 227 |
break |
| 228 |
} |
| 229 |
|
| 230 |
now := time.Now() |
| 231 |
|
| 232 |
switch e.typ { |
| 233 |
case typeDoublePressSchedule: |
| 234 |
e.typ = typeDoublePressResolve |
| 235 |
go f.sendAfter(e, doublePressThreshold) |
| 236 |
|
| 237 |
case typeDoublePressResolve: |
| 238 |
if e.pressCounter == f.pressCounter { |
| 239 |
|
| 240 |
f.end(e.x, e.y, now) |
| 241 |
} |
| 242 |
|
| 243 |
case typeLongPressSchedule: |
| 244 |
e.typ = typeLongPressResolve |
| 245 |
go f.sendAfter(e, longPressThreshold) |
| 246 |
|
| 247 |
case typeLongPressResolve: |
| 248 |
if e.pressCounter == f.pressCounter && !f.drag { |
| 249 |
f.longPress = true |
| 250 |
f.sendFirst(TypeIsLongPress, e.x, e.y, now) |
| 251 |
} |
| 252 |
} |
| 253 |
return nil |
| 254 |
|
| 255 |
case mouse.Event: |
| 256 |
now := time.Now() |
| 257 |
|
| 258 |
switch e.Direction { |
| 259 |
case mouse.DirNone: |
| 260 |
if f.pressButton == mouse.ButtonNone { |
| 261 |
break |
| 262 |
} |
| 263 |
startDrag := false |
| 264 |
if !f.drag && |
| 265 |
(abs(e.X-f.initialPos.X) > dragThreshold || abs(e.Y-f.initialPos.Y) > dragThreshold) { |
| 266 |
f.drag = true |
| 267 |
startDrag = true |
| 268 |
} |
| 269 |
if f.drag { |
| 270 |
f.sendFirst(TypeDrag, e.X, e.Y, now) |
| 271 |
} |
| 272 |
if startDrag { |
| 273 |
f.sendFirst(TypeIsDrag, e.X, e.Y, now) |
| 274 |
} |
| 275 |
|
| 276 |
case mouse.DirPress: |
| 277 |
if f.pressButton != mouse.ButtonNone { |
| 278 |
break |
| 279 |
} |
| 280 |
|
| 281 |
oldInProgress := f.inProgress |
| 282 |
oldDoublePress := f.doublePress |
| 283 |
|
| 284 |
f.drag = false |
| 285 |
f.longPress = false |
| 286 |
f.doublePress = f.inProgress |
| 287 |
f.initialPos = Point{e.X, e.Y} |
| 288 |
f.pressButton = e.Button |
| 289 |
f.pressCounter++ |
| 290 |
|
| 291 |
f.inProgress = true |
| 292 |
|
| 293 |
f.sendFirst(typeLongPressSchedule, e.X, e.Y, now) |
| 294 |
if !oldDoublePress && f.doublePress { |
| 295 |
f.sendFirst(TypeIsDoublePress, e.X, e.Y, now) |
| 296 |
} |
| 297 |
if !oldInProgress { |
| 298 |
f.sendFirst(TypeStart, e.X, e.Y, now) |
| 299 |
} |
| 300 |
|
| 301 |
case mouse.DirRelease: |
| 302 |
if f.pressButton != e.Button { |
| 303 |
break |
| 304 |
} |
| 305 |
f.pressButton = mouse.ButtonNone |
| 306 |
f.pressCounter++ |
| 307 |
|
| 308 |
if f.drag { |
| 309 |
f.end(e.X, e.Y, now) |
| 310 |
break |
| 311 |
} |
| 312 |
f.sendFirst(typeDoublePressSchedule, e.X, e.Y, now) |
| 313 |
f.sendFirst(TypeTap, e.X, e.Y, now) |
| 314 |
} |
| 315 |
} |
| 316 |
return e |
| 317 |
} |
| 318 |
|
| 319 |
func abs(x float32) float32 { |
| 320 |
if x < 0 { |
| 321 |
return -x |
| 322 |
} else if x == 0 { |
| 323 |
return 0 |
| 324 |
} |
| 325 |
return x |
| 326 |
} |