mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 02:15:34 +00:00
ui/pointer: introduce OpArea for pointer hit testing
Split out OpArea from OpHandler to allow stacked areas in a followup. Replace hit closures with static shapes (rectangles and ellipses) to avoid allocations. If needed, generic hit functions can be introduced again later. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+4
-36
@@ -3,7 +3,6 @@
|
|||||||
package gesture
|
package gesture
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"image"
|
|
||||||
"math"
|
"math"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
@@ -38,14 +37,6 @@ type Scroll struct {
|
|||||||
scroll float32
|
scroll float32
|
||||||
}
|
}
|
||||||
|
|
||||||
type Rect struct {
|
|
||||||
Size image.Point
|
|
||||||
}
|
|
||||||
|
|
||||||
type Ellipse struct {
|
|
||||||
Size image.Point
|
|
||||||
}
|
|
||||||
|
|
||||||
type flinger struct {
|
type flinger struct {
|
||||||
// Current offset in pixels.
|
// Current offset in pixels.
|
||||||
x float32
|
x float32
|
||||||
@@ -84,8 +75,8 @@ const (
|
|||||||
thresholdVelocity = 1
|
thresholdVelocity = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Click) Op(ops *ui.Ops, a pointer.Area) {
|
func (c *Click) Add(ops *ui.Ops) {
|
||||||
op := pointer.OpHandler{Area: a, Key: c}
|
op := pointer.OpHandler{Key: c}
|
||||||
op.Add(ops)
|
op.Add(ops)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,8 +108,8 @@ func (c *Click) Update(q pointer.Events) []ClickEvent {
|
|||||||
return events
|
return events
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scroll) Op(ops *ui.Ops, a pointer.Area) {
|
func (s *Scroll) Add(ops *ui.Ops) {
|
||||||
oph := pointer.OpHandler{Area: a, Key: s, Grab: s.grab}
|
oph := pointer.OpHandler{Key: s, Grab: s.grab}
|
||||||
oph.Add(ops)
|
oph.Add(ops)
|
||||||
if s.flinger.Active() {
|
if s.flinger.Active() {
|
||||||
ui.OpRedraw{}.Add(ops)
|
ui.OpRedraw{}.Add(ops)
|
||||||
@@ -264,29 +255,6 @@ func (f *flinger) Tick(now time.Time) int {
|
|||||||
return idist
|
return idist
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Rect) Hit(pos f32.Point) pointer.HitResult {
|
|
||||||
if 0 <= pos.X && pos.X < float32(r.Size.X) &&
|
|
||||||
0 <= pos.Y && pos.Y < float32(r.Size.Y) {
|
|
||||||
return pointer.HitOpaque
|
|
||||||
} else {
|
|
||||||
return pointer.HitNone
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Ellipse) Hit(pos f32.Point) pointer.HitResult {
|
|
||||||
rx := float32(e.Size.X) / 2
|
|
||||||
ry := float32(e.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 pointer.HitOpaque
|
|
||||||
} else {
|
|
||||||
return pointer.HitNone
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a Axis) String() string {
|
func (a Axis) String() string {
|
||||||
switch a {
|
switch a {
|
||||||
case Horizontal:
|
case Horizontal:
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ const (
|
|||||||
TypeImage
|
TypeImage
|
||||||
TypeDraw
|
TypeDraw
|
||||||
TypeColor
|
TypeColor
|
||||||
|
TypeArea
|
||||||
TypePointerHandler
|
TypePointerHandler
|
||||||
TypeKeyHandler
|
TypeKeyHandler
|
||||||
TypeHideInput
|
TypeHideInput
|
||||||
@@ -32,6 +33,7 @@ const (
|
|||||||
TypeImageLen = 1 + 4*4
|
TypeImageLen = 1 + 4*4
|
||||||
TypeDrawLen = 1 + 4*4
|
TypeDrawLen = 1 + 4*4
|
||||||
TypeColorLen = 1 + 4
|
TypeColorLen = 1 + 4
|
||||||
|
TypeAreaLen = 1 + 1 + 2*4
|
||||||
TypePointerHandlerLen = 1 + 1
|
TypePointerHandlerLen = 1 + 1
|
||||||
TypeKeyHandlerLen = 1 + 1
|
TypeKeyHandlerLen = 1 + 1
|
||||||
TypeHideInputLen = 1
|
TypeHideInputLen = 1
|
||||||
@@ -51,6 +53,7 @@ func (t OpType) Size() int {
|
|||||||
TypeImageLen,
|
TypeImageLen,
|
||||||
TypeDrawLen,
|
TypeDrawLen,
|
||||||
TypeColorLen,
|
TypeColorLen,
|
||||||
|
TypeAreaLen,
|
||||||
TypePointerHandlerLen,
|
TypePointerHandlerLen,
|
||||||
TypeKeyHandlerLen,
|
TypeKeyHandlerLen,
|
||||||
TypeHideInputLen,
|
TypeHideInputLen,
|
||||||
@@ -63,10 +66,8 @@ func (t OpType) Size() int {
|
|||||||
|
|
||||||
func (t OpType) NumRefs() int {
|
func (t OpType) NumRefs() int {
|
||||||
switch t {
|
switch t {
|
||||||
case TypeBlock, TypeImage, TypeKeyHandler:
|
case TypeBlock, TypeImage, TypeKeyHandler, TypePointerHandler:
|
||||||
return 1
|
return 1
|
||||||
case TypePointerHandler:
|
|
||||||
return 2
|
|
||||||
default:
|
default:
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-3
@@ -24,7 +24,6 @@ type List struct {
|
|||||||
// The distance scrolled since last call to Init.
|
// The distance scrolled since last call to Init.
|
||||||
Distance int
|
Distance int
|
||||||
|
|
||||||
area gesture.Rect
|
|
||||||
scroll gesture.Scroll
|
scroll gesture.Scroll
|
||||||
scrollDir int
|
scrollDir int
|
||||||
|
|
||||||
@@ -78,7 +77,7 @@ func (l *List) Next(ops *ui.Ops) (int, Constraints, bool) {
|
|||||||
var cs Constraints
|
var cs Constraints
|
||||||
if ok {
|
if ok {
|
||||||
if len(l.children) == 0 {
|
if len(l.children) == 0 {
|
||||||
l.scroll.Op(ops, &l.area)
|
ops.Begin()
|
||||||
}
|
}
|
||||||
cs = axisConstraints(l.Axis, Constraint{Max: ui.Inf}, l.crossConstraintChild(l.cs))
|
cs = axisConstraints(l.Axis, Constraint{Max: ui.Inf}, l.crossConstraintChild(l.cs))
|
||||||
ops.Begin()
|
ops.Begin()
|
||||||
@@ -193,7 +192,12 @@ func (l *List) Layout(ops *ui.Ops) Dimens {
|
|||||||
l.scroll.Stop()
|
l.scroll.Stop()
|
||||||
}
|
}
|
||||||
dims := axisPoint(l.Axis, mainc.Constrain(pos), maxCross)
|
dims := axisPoint(l.Axis, mainc.Constrain(pos), maxCross)
|
||||||
l.area.Size = dims
|
if len(l.children) > 0 {
|
||||||
|
block := ops.End()
|
||||||
|
pointer.AreaRect(dims).Add(ops)
|
||||||
|
l.scroll.Add(ops)
|
||||||
|
block.Add(ops)
|
||||||
|
}
|
||||||
return Dimens{Size: dims}
|
return Dimens{Size: dims}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+81
-8
@@ -3,6 +3,8 @@
|
|||||||
package pointer
|
package pointer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"image"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gioui.org/ui"
|
"gioui.org/ui"
|
||||||
@@ -21,14 +23,14 @@ type Event struct {
|
|||||||
Scroll f32.Point
|
Scroll f32.Point
|
||||||
}
|
}
|
||||||
|
|
||||||
type OpHandler struct {
|
type OpArea struct {
|
||||||
Key Key
|
kind areaKind
|
||||||
Area Area
|
size image.Point
|
||||||
Grab bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Area interface {
|
type OpHandler struct {
|
||||||
Hit(pos f32.Point) HitResult
|
Key Key
|
||||||
|
Grab bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Key interface{}
|
type Key interface{}
|
||||||
@@ -50,6 +52,8 @@ type Type uint8
|
|||||||
type Priority uint8
|
type Priority uint8
|
||||||
type Source uint8
|
type Source uint8
|
||||||
|
|
||||||
|
type areaKind uint8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Cancel Type = iota
|
Cancel Type = iota
|
||||||
Press
|
Press
|
||||||
@@ -68,13 +72,83 @@ const (
|
|||||||
Grabbed
|
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, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
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 HitOpaque
|
||||||
|
} 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 HitOpaque
|
||||||
|
} else {
|
||||||
|
return HitNone
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
panic("invalid area kind")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (h OpHandler) Add(o *ui.Ops) {
|
func (h OpHandler) Add(o *ui.Ops) {
|
||||||
data := make([]byte, ops.TypePointerHandlerLen)
|
data := make([]byte, ops.TypePointerHandlerLen)
|
||||||
data[0] = byte(ops.TypePointerHandler)
|
data[0] = byte(ops.TypePointerHandler)
|
||||||
if h.Grab {
|
if h.Grab {
|
||||||
data[1] = 1
|
data[1] = 1
|
||||||
}
|
}
|
||||||
o.Write(data, []interface{}{h.Key, h.Area})
|
o.Write(data, []interface{}{h.Key})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *OpHandler) Decode(d []byte, refs []interface{}) {
|
func (h *OpHandler) Decode(d []byte, refs []interface{}) {
|
||||||
@@ -84,7 +158,6 @@ func (h *OpHandler) Decode(d []byte, refs []interface{}) {
|
|||||||
*h = OpHandler{
|
*h = OpHandler{
|
||||||
Grab: d[1] != 0,
|
Grab: d[1] != 0,
|
||||||
Key: refs[0].(Key),
|
Key: refs[0].(Key),
|
||||||
Area: refs[1].(Area),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+9
-5
@@ -30,14 +30,14 @@ type pointerInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type handler struct {
|
type handler struct {
|
||||||
area Area
|
area OpArea
|
||||||
active bool
|
active bool
|
||||||
transform ui.Transform
|
transform ui.Transform
|
||||||
events []Event
|
events []Event
|
||||||
wantsGrab bool
|
wantsGrab bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queue) collectHandlers(r *ui.OpsReader, t ui.Transform, layer int) {
|
func (q *Queue) collectHandlers(r *ui.OpsReader, t ui.Transform, layer int, area OpArea) {
|
||||||
for {
|
for {
|
||||||
encOp, ok := r.Decode()
|
encOp, ok := r.Decode()
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -45,12 +45,16 @@ func (q *Queue) collectHandlers(r *ui.OpsReader, t ui.Transform, layer int) {
|
|||||||
}
|
}
|
||||||
switch ops.OpType(encOp.Data[0]) {
|
switch ops.OpType(encOp.Data[0]) {
|
||||||
case ops.TypePush:
|
case ops.TypePush:
|
||||||
q.collectHandlers(r, t, layer)
|
q.collectHandlers(r, t, layer, area)
|
||||||
case ops.TypePop:
|
case ops.TypePop:
|
||||||
return
|
return
|
||||||
case ops.TypeLayer:
|
case ops.TypeLayer:
|
||||||
layer++
|
layer++
|
||||||
q.hitTree = append(q.hitTree, hitNode{level: layer})
|
q.hitTree = append(q.hitTree, hitNode{level: layer})
|
||||||
|
case ops.TypeArea:
|
||||||
|
var op OpArea
|
||||||
|
op.decode(encOp.Data)
|
||||||
|
area = op
|
||||||
case ops.TypeTransform:
|
case ops.TypeTransform:
|
||||||
var op ui.OpTransform
|
var op ui.OpTransform
|
||||||
op.Decode(encOp.Data)
|
op.Decode(encOp.Data)
|
||||||
@@ -64,7 +68,7 @@ func (q *Queue) collectHandlers(r *ui.OpsReader, t ui.Transform, layer int) {
|
|||||||
h = new(handler)
|
h = new(handler)
|
||||||
q.handlers[op.Key] = h
|
q.handlers[op.Key] = h
|
||||||
}
|
}
|
||||||
h.area = op.Area
|
h.area = area
|
||||||
h.transform = t
|
h.transform = t
|
||||||
h.wantsGrab = h.wantsGrab || op.Grab
|
h.wantsGrab = h.wantsGrab || op.Grab
|
||||||
}
|
}
|
||||||
@@ -117,7 +121,7 @@ func (q *Queue) Frame(root *ui.Ops) {
|
|||||||
}
|
}
|
||||||
q.hitTree = q.hitTree[:0]
|
q.hitTree = q.hitTree[:0]
|
||||||
q.reader.Reset(root)
|
q.reader.Reset(root)
|
||||||
q.collectHandlers(&q.reader, ui.Transform{}, 0)
|
q.collectHandlers(&q.reader, ui.Transform{}, 0, OpArea{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queue) For(k Key) []Event {
|
func (q *Queue) For(k Key) []Event {
|
||||||
|
|||||||
+3
-3
@@ -201,9 +201,9 @@ func (e *Editor) Layout(ops *ui.Ops, cs layout.Constraints) layout.Dimens {
|
|||||||
}
|
}
|
||||||
|
|
||||||
baseline := e.padTop + e.dims.Baseline
|
baseline := e.padTop + e.dims.Baseline
|
||||||
area := &gesture.Rect{e.viewSize}
|
pointer.AreaRect(e.viewSize).Add(ops)
|
||||||
e.scroller.Op(ops, area)
|
e.scroller.Add(ops)
|
||||||
e.clicker.Op(ops, area)
|
e.clicker.Add(ops)
|
||||||
return layout.Dimens{Size: e.viewSize, Baseline: baseline}
|
return layout.Dimens{Size: e.viewSize, Baseline: baseline}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user