~eliasnaur/gio

ref: 7aa7bb3be40858efe2a98f866fcc7db2026b02fd gio/ui/input/pointer.go -rw-r--r-- 5.8 KiB
7aa7bb3bElias Naur ui: rename ops to have Op suffixed, not prefixed 2 years ago
                                                                                
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
275
276
277
278
// SPDX-License-Identifier: Unlicense OR MIT

package input

import (
	"gioui.org/ui"
	"gioui.org/ui/f32"
	"gioui.org/ui/internal/ops"
	"gioui.org/ui/pointer"
)

type pointerQueue struct {
	hitTree  []hitNode
	handlers map[Key]*pointerHandler
	pointers []pointerInfo
	reader   ui.OpsReader
	scratch  []Key
	areas    areaStack
}

type hitNode struct {
	// The layer depth.
	level int
	// The handler, or nil for a layer.
	key Key
}

type pointerInfo struct {
	id       pointer.ID
	pressed  bool
	handlers []Key
}

type pointerHandler struct {
	area      areaIntersection
	active    bool
	transform ui.Transform
	wantsGrab bool
}

type area struct {
	trans ui.Transform
	area  pointer.AreaOp
}

type areaIntersection []area

type areaStack struct {
	stack   []int
	areas   []area
	backing []area
}

func (q *pointerQueue) collectHandlers(r *ui.OpsReader, t ui.Transform, layer int, events handlerEvents) {
	for {
		encOp, ok := r.Decode()
		if !ok {
			return
		}
		switch ops.OpType(encOp.Data[0]) {
		case ops.TypePush:
			q.areas.push()
			q.collectHandlers(r, t, layer, events)
		case ops.TypePop:
			q.areas.pop()
			return
		case ops.TypeLayer:
			layer++
			q.hitTree = append(q.hitTree, hitNode{level: layer})
		case ops.TypeArea:
			var op pointer.AreaOp
			op.Decode(encOp.Data)
			q.areas.add(t, op)
		case ops.TypeTransform:
			var op ui.TransformOp
			op.Decode(encOp.Data)
			t = t.Mul(op.Transform)
		case ops.TypePointerHandler:
			var op pointer.HandlerOp
			op.Decode(encOp.Data, encOp.Refs)
			q.hitTree = append(q.hitTree, hitNode{level: layer, key: op.Key})
			h, ok := q.handlers[op.Key]
			if !ok {
				h = new(pointerHandler)
				q.handlers[op.Key] = h
				events[op.Key] = []Event{pointer.Event{Type: pointer.Cancel}}
			}
			h.active = true
			h.area = q.areas.intersection()
			h.transform = t
			h.wantsGrab = h.wantsGrab || op.Grab
		}
	}
}

func (q *pointerQueue) opHit(handlers *[]Key, pos f32.Point) {
	level := 1 << 30
	opaque := false
	for i := len(q.hitTree) - 1; i >= 0; i-- {
		n := q.hitTree[i]
		if n.key == nil {
			// Layer
			if opaque {
				opaque = false
				// Skip sibling handlers.
				level = n.level - 1
			}
		} else if n.level <= level {
			// Handler
			h, ok := q.handlers[n.key]
			if !ok {
				continue
			}
			res := h.area.hit(pos)
			opaque = opaque || res == pointer.HitOpaque
			if res != pointer.HitNone {
				*handlers = append(*handlers, n.key)
			}
		}
	}
}

func (q *pointerQueue) init() {
	if q.handlers == nil {
		q.handlers = make(map[Key]*pointerHandler)
	}
}

func (q *pointerQueue) Frame(root *ui.Ops, events handlerEvents) {
	q.init()
	for _, h := range q.handlers {
		// Reset handler.
		h.active = false
	}
	q.hitTree = q.hitTree[:0]
	q.areas.reset()
	q.reader.Reset(root)
	q.collectHandlers(&q.reader, ui.Transform{}, 0, events)
	for k, h := range q.handlers {
		if !h.active {
			q.dropHandler(k)
		}
	}
}

func (q *pointerQueue) dropHandler(k Key) {
	delete(q.handlers, k)
	for i := range q.pointers {
		p := &q.pointers[i]
		for i := len(p.handlers) - 1; i >= 0; i-- {
			if p.handlers[i] == k {
				p.handlers = append(p.handlers[:i], p.handlers[i+1:]...)
			}
		}
	}
}

func (q *pointerQueue) Push(e pointer.Event, events handlerEvents) {
	q.init()
	if e.Type == pointer.Cancel {
		q.pointers = q.pointers[:0]
		for k := range q.handlers {
			q.dropHandler(k)
		}
		return
	}
	pidx := -1
	for i, p := range q.pointers {
		if p.id == e.PointerID {
			pidx = i
			break
		}
	}
	if pidx == -1 {
		q.pointers = append(q.pointers, pointerInfo{id: e.PointerID})
		pidx = len(q.pointers) - 1
	}
	p := &q.pointers[pidx]
	if !p.pressed && (e.Type == pointer.Move || e.Type == pointer.Press) {
		p.handlers, q.scratch = q.scratch[:0], p.handlers
		q.opHit(&p.handlers, e.Position)
		// Drop handlers no longer hit.
	loop:
		for _, h := range q.scratch {
			for _, h2 := range p.handlers {
				if h == h2 {
					continue loop
				}
			}
			q.dropHandler(h)
		}
		if e.Type == pointer.Press {
			p.pressed = true
		}
	}
	if p.pressed {
		// Resolve grabs.
		q.scratch = q.scratch[:0]
		for i, k := range p.handlers {
			h := q.handlers[k]
			if h.wantsGrab {
				q.scratch = append(q.scratch, p.handlers[:i]...)
				q.scratch = append(q.scratch, p.handlers[i+1:]...)
				break
			}
		}
		// Drop handlers that lost their grab.
		for _, k := range q.scratch {
			q.dropHandler(k)
		}
	}
	if e.Type == pointer.Release {
		q.pointers = append(q.pointers[:pidx], q.pointers[pidx+1:]...)
	}
	for i, k := range p.handlers {
		h := q.handlers[k]
		e := e
		switch {
		case p.pressed && len(p.handlers) == 1:
			e.Priority = pointer.Grabbed
		case i == 0:
			e.Priority = pointer.Foremost
		}
		e.Hit = h.area.hit(e.Position) != pointer.HitNone
		e.Position = h.transform.InvTransform(e.Position)
		events[k] = append(events[k], e)
		if e.Type == pointer.Release {
			// Release grab when the number of grabs reaches zero.
			grabs := 0
			for _, p := range q.pointers {
				if p.pressed && len(p.handlers) == 1 && p.handlers[0] == k {
					grabs++
				}
			}
			if grabs == 0 {
				h.wantsGrab = false
			}
		}
	}
}

func (a areaIntersection) hit(p f32.Point) pointer.HitResult {
	res := pointer.HitNone
	for _, area := range a {
		tp := area.trans.InvTransform(p)
		res = area.area.Hit(tp)
		if res == pointer.HitNone {
			break
		}
	}
	return res
}

func (s *areaStack) add(t ui.Transform, a pointer.AreaOp) {
	s.areas = append(s.areas, area{t, a})
}

func (s *areaStack) push() {
	s.stack = append(s.stack, len(s.areas))
}

func (s *areaStack) pop() {
	off := s.stack[len(s.stack)-1]
	s.stack = s.stack[:len(s.stack)-1]
	s.areas = s.areas[:off]
}

func (s *areaStack) intersection() areaIntersection {
	off := len(s.backing)
	s.backing = append(s.backing, s.areas...)
	return areaIntersection(s.backing[off:len(s.backing):len(s.backing)])
}

func (a *areaStack) reset() {
	a.areas = a.areas[:0]
	a.stack = a.stack[:0]
	a.backing = a.backing[:0]
}