forked from gomatcha/matcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointer.go
274 lines (237 loc) · 5.96 KB
/
pointer.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
Package pointer implements gesture recognizers.
Create the pointer recognizer in the Build function.
func (v *MyView) Build(ctx view.Context) view.Model {
tap := &pointer.TapGesture{
Count: 1,
OnEvent: func(e *pointer.TapEvent) {
if e.Kind == EventKindRecognized {
// Respond to pointer events. This callback occurs on main thread.
fmt.Println("view tapped")
}
},
}
...
Attach the recognizer to the view.
...
return view.Model{
Options: []view.Option{
pointer.GestureList{tap},
},
}
}
*/
package pointer
import (
"fmt"
"sync/atomic"
"time"
"github.com/gogo/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"gomatcha.io/matcha/layout"
pbtouch "gomatcha.io/matcha/proto/pointer"
)
var maxFuncId int64 = 0
// NewFuncId generates a new func identifier for serialization.
func newFuncId() int64 {
return atomic.AddInt64(&maxFuncId, 1)
}
type GestureList []Gesture
func (r GestureList) OptionKey() string {
return "gomatcha.io/matcha/touch"
}
type Gesture interface {
Build() Model
TouchKey() int64
}
type Model struct {
NativeViewName string
NativeViewState proto.Message
NativeFuncs map[string]interface{}
}
// EventKind are the possible recognizer states
//
// Discrete gestures:
// EventKindPossible -> EventKindFailed
// EventKindPossible -> EventKindRecognized
// Continuous gestures:
// EventKindPossible -> EventKindFailed
// EventKindPossible -> EventKindRecognized
// EventKindPossible -> EventKindChanged(optionally) -> EventKindFailed
// EventKindPossible -> EventKindChanged(optionally) -> EventKindRecognized
type EventKind int
const (
// Finger is down, but before gesture has been recognized.
EventKindPossible EventKind = iota
// After the continuous gesture has been recognized, while the finger is still down. Only for continuous recognizers.
EventKindChanged
// Gesture recognition failed or cancelled.
EventKindFailed
// Gesture recognition succeded.
EventKindRecognized
)
// TapEvent is emitted by TapGesture, representing its current state.
type TapEvent struct {
Kind EventKind
Timestamp time.Time
Position layout.Point
}
func (e *TapEvent) unmarshalProtobuf(ev *pbtouch.TapEvent) error {
t, _ := ptypes.Timestamp(ev.Timestamp)
e.Timestamp = t
e.Kind = EventKind(ev.Kind)
e.Position.UnmarshalProtobuf(ev.Position)
return nil
}
// PressRecognizer is a discrete recognizer that detects a number of taps.
type TapGesture struct {
Key int64
Count int
OnEvent func(*TapEvent)
}
func (r *TapGesture) TouchKey() int64 {
return r.Key
}
func (r *TapGesture) Build() Model {
funcId := newFuncId()
f := func(data []byte) {
pbevent := &pbtouch.TapEvent{}
err := proto.Unmarshal(data, pbevent)
if err != nil {
fmt.Println("error", err)
return
}
event := &TapEvent{}
if err := event.unmarshalProtobuf(pbevent); err != nil {
fmt.Println("error", err)
return
}
if r.OnEvent != nil {
r.OnEvent(event)
}
}
return Model{
NativeViewName: "",
NativeViewState: &pbtouch.TapRecognizer{
Count: int64(r.Count),
OnEvent: funcId,
},
NativeFuncs: map[string]interface{}{
fmt.Sprintf("gomatcha.io/matcha/touch %v", funcId): f,
},
}
}
// PressEvent is emitted by PressRecognizer, representing its current state.
type PressEvent struct {
Kind EventKind // TODO(KD): Does this work?
Timestamp time.Time
Position layout.Point
Duration time.Duration
}
func (e *PressEvent) unmarshalProtobuf(ev *pbtouch.PressEvent) error {
d, err := ptypes.Duration(ev.Duration)
if err != nil {
return err
}
t, err := ptypes.Timestamp(ev.Timestamp)
if err != nil {
return err
}
e.Kind = EventKind(ev.Kind)
e.Timestamp = t
e.Position.UnmarshalProtobuf(ev.Position)
e.Duration = d
return nil
}
// PressGesture is a continuous recognizer that detects single presses with a given duration.
type PressGesture struct {
Key int64
MinDuration time.Duration
OnEvent func(e *PressEvent)
}
func (r *PressGesture) TouchKey() int64 {
return r.Key
}
func (r *PressGesture) Build() Model {
funcId := newFuncId()
f := func(data []byte) {
event := &PressEvent{}
pbevent := &pbtouch.PressEvent{}
err := proto.Unmarshal(data, pbevent)
if err != nil {
fmt.Println("error", err)
return
}
if err := event.unmarshalProtobuf(pbevent); err != nil {
fmt.Println("error", err)
return
}
if r.OnEvent != nil {
r.OnEvent(event)
}
}
return Model{
NativeViewName: "",
NativeViewState: &pbtouch.PressRecognizer{
MinDuration: ptypes.DurationProto(r.MinDuration),
OnEvent: funcId,
},
NativeFuncs: map[string]interface{}{
fmt.Sprintf("gomatcha.io/matcha/touch %v", funcId): f,
},
}
}
// ButtonEvent is emitted by ButtonRecognizer, representing its current state.
type ButtonEvent struct {
Timestamp time.Time
Inside bool
Kind EventKind
}
func (e *ButtonEvent) unmarshalProtobuf(ev *pbtouch.ButtonEvent) error {
t, err := ptypes.Timestamp(ev.Timestamp)
if err != nil {
return err
}
e.Timestamp = t
e.Inside = ev.Inside
e.Kind = EventKind(ev.Kind)
return nil
}
// ButtonGesture is a discrete recognizer that mimics the behavior of a button. The recognizer will fail if the touch ends outside of the view's bounds.
type ButtonGesture struct {
Key int64
OnEvent func(e *ButtonEvent)
IgnoresScroll bool
}
func (r *ButtonGesture) TouchKey() int64 {
return r.Key
}
func (r *ButtonGesture) Build() Model {
funcId := newFuncId()
f := func(data []byte) {
event := &ButtonEvent{}
pbevent := &pbtouch.ButtonEvent{}
err := proto.Unmarshal(data, pbevent)
if err != nil {
fmt.Println("error", err)
return
}
if err := event.unmarshalProtobuf(pbevent); err != nil {
fmt.Println("error", err)
return
}
if r.OnEvent != nil {
r.OnEvent(event)
}
}
return Model{
NativeViewName: "",
NativeViewState: &pbtouch.ButtonRecognizer{
OnEvent: funcId,
IgnoresScroll: r.IgnoresScroll,
},
NativeFuncs: map[string]interface{}{
fmt.Sprintf("gomatcha.io/matcha/touch %v", funcId): f,
},
}
}