~eliasnaur/gio

ref: b981ccf9eda43816ae9d809466fe96c8e0c2475b gio/ui/pointer/pointer.go -rw-r--r-- 3.1 KiB
b981ccf9Elias Naur ui/input: support single stepping through events 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
// SPDX-License-Identifier: Unlicense OR MIT

package pointer

import (
	"encoding/binary"
	"image"
	"time"

	"gioui.org/ui"
	"gioui.org/ui/f32"
	"gioui.org/ui/internal/ops"
)

type Event struct {
	Type      Type
	Source    Source
	PointerID ID
	Priority  Priority
	Time      time.Duration
	Hit       bool
	Position  f32.Point
	Scroll    f32.Point
}

type OpArea struct {
	Transparent bool

	kind areaKind
	size image.Point
}

type OpHandler struct {
	Key  Key
	Grab bool
}

type Key interface{}

type HitResult uint8

const (
	HitNone HitResult = iota
	HitTransparent
	HitOpaque
)

type ID uint16
type Type uint8
type Priority uint8
type Source uint8

type areaKind uint8

const (
	Cancel Type = iota
	Press
	Release
	Move
)

const (
	Mouse Source = iota
	Touch
)

const (
	Shared Priority = iota
	Foremost
	Grabbed
)

const (
	areaRect areaKind = iota
	areaEllipse
)

func AreaRect(size image.Point) OpArea {
	return OpArea{
		kind: areaRect,
		size: size,
	}
}

func AreaEllipse(size image.Point) OpArea {
	return OpArea{
		kind: areaEllipse,
		size: size,
	}
}

func (op OpArea) Add(o *ui.Ops) {
	data := make([]byte, ops.TypeAreaLen)
	data[0] = byte(ops.TypeArea)
	data[1] = byte(op.kind)
	bo := binary.LittleEndian
	bo.PutUint32(data[2:], uint32(op.size.X))
	bo.PutUint32(data[6:], uint32(op.size.Y))
	o.Write(data)
}

func (op *OpArea) Decode(d []byte) {
	if ops.OpType(d[0]) != ops.TypeArea {
		panic("invalid op")
	}
	bo := binary.LittleEndian
	size := image.Point{
		X: int(bo.Uint32(d[2:])),
		Y: int(bo.Uint32(d[6:])),
	}
	*op = OpArea{
		kind: areaKind(d[1]),
		size: size,
	}
}

func (op *OpArea) Hit(pos f32.Point) HitResult {
	res := HitOpaque
	if op.Transparent {
		res = HitTransparent
	}
	switch op.kind {
	case areaRect:
		if 0 <= pos.X && pos.X < float32(op.size.X) &&
			0 <= pos.Y && pos.Y < float32(op.size.Y) {
			return res
		} else {
			return HitNone
		}
	case areaEllipse:
		rx := float32(op.size.X) / 2
		ry := float32(op.size.Y) / 2
		rx2 := rx * rx
		ry2 := ry * ry
		xh := pos.X - rx
		yk := pos.Y - ry
		if xh*xh*ry2+yk*yk*rx2 <= rx2*ry2 {
			return res
		} else {
			return HitNone
		}
	default:
		panic("invalid area kind")
	}
}

func (h OpHandler) Add(o *ui.Ops) {
	data := make([]byte, ops.TypePointerHandlerLen)
	data[0] = byte(ops.TypePointerHandler)
	if h.Grab {
		data[1] = 1
	}
	o.Write(data, h.Key)
}

func (h *OpHandler) Decode(d []byte, refs []interface{}) {
	if ops.OpType(d[0]) != ops.TypePointerHandler {
		panic("invalid op")
	}
	*h = OpHandler{
		Grab: d[1] != 0,
		Key:  refs[0].(Key),
	}
}

func (t Type) String() string {
	switch t {
	case Press:
		return "Press"
	case Release:
		return "Release"
	case Cancel:
		return "Cancel"
	case Move:
		return "Move"
	default:
		panic("unknown Type")
	}
}

func (p Priority) String() string {
	switch p {
	case Shared:
		return "Shared"
	case Foremost:
		return "Foremost"
	case Grabbed:
		return "Grabbed"
	default:
		panic("unknown priority")
	}
}

func (s Source) String() string {
	switch s {
	case Mouse:
		return "Mouse"
	case Touch:
		return "Touch"
	default:
		panic("unknown source")
	}
}

func (Event) ImplementsEvent()      {}
func (Event) ImplementsInputEvent() {}