io/pointer,io/router: replace AreaOp with clip.Op

Pointer hit areas and paint clip areas are separate concepts, but
similar enough to warrant merging. This change replaces pointer hit
areas with clip areas, so Gio is left with just one area concept (in
package op/clip).

The reason for separating the concepts in the original Gio release was
because of my being unsure general path/stroke hit areas would ever be
implemented, let alone efficient.

This change represents a change of mind, in the sense that it's better
to have an incomplete API than two separate area concepts.

Leave the deprecated pointer.Rect, pointer.Ellipse for temporary
backwards compatibility.

This is an API change. Most existing programs should continue to build
with this change, but may have to adjust to having all clip.Ops participate
in InputOp hit areas.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-10-25 11:31:04 +02:00
parent 28fc08a508
commit 29cea1db49
12 changed files with 127 additions and 140 deletions
+3 -2
View File
@@ -47,7 +47,6 @@ func (p Op) Push(o *op.Ops) Stack {
func (p Op) add(o *op.Ops) {
path := p.path
outline := p.outline
bo := binary.LittleEndian
if path.hasSegments {
@@ -77,9 +76,10 @@ func (p Op) add(o *op.Ops) {
bo.PutUint32(data[5:], uint32(bounds.Min.Y))
bo.PutUint32(data[9:], uint32(bounds.Max.X))
bo.PutUint32(data[13:], uint32(bounds.Max.Y))
if outline {
if p.outline {
data[17] = byte(1)
}
data[18] = byte(path.shape)
}
func (s Stack) Pop() {
@@ -96,6 +96,7 @@ type PathSpec struct {
// hasSegments tracks whether there are any segments in the path.
hasSegments bool
bounds image.Rectangle
shape ops.Shape
hash uint64
}
+3 -3
View File
@@ -1,10 +1,10 @@
// SPDX-License-Identifier: Unlicense OR MIT
/*
Package clip provides operations for clipping paint operations.
Drawing outside the current clip area is ignored.
Package clip provides operations for defining areas that applies to operations
such as paints and pointer handlers.
The current clip is initially the infinite set. Pushing and Op sets the clip
The current clip is initially the infinite set. Pushing an Op sets the clip
to the intersection of the current clip and pushed clip area. Popping the
area restores the clip to its state before pushing.
+15 -12
View File
@@ -7,6 +7,7 @@ import (
"math"
"gioui.org/f32"
"gioui.org/internal/ops"
"gioui.org/op"
)
@@ -18,6 +19,7 @@ func (r Rect) Op() Op {
return Op{
outline: true,
path: PathSpec{
shape: ops.Rect,
bounds: image.Rectangle(r),
},
}
@@ -132,18 +134,16 @@ func (c Circle) Path(ops *op.Ops) PathSpec {
Min: f32.Pt(c.Center.X-c.Radius, c.Center.Y-c.Radius),
Max: f32.Pt(c.Center.X+c.Radius, c.Center.Y+c.Radius),
}
return Ellipse{b}.Path(ops)
return Ellipse(b).path(ops)
}
// Ellipse represents the largest axis-aligned ellipse that
// is contained in its bounds.
type Ellipse struct {
Bounds f32.Rectangle
}
type Ellipse f32.Rectangle
// Op returns the op for the filled ellipse.
func (e Ellipse) Op(ops *op.Ops) Op {
return Outline{Path: e.Path(ops)}.Op()
return Outline{Path: e.path(ops)}.Op()
}
// Push the filled ellipse clip op on the clip stack.
@@ -151,17 +151,18 @@ func (e Ellipse) Push(ops *op.Ops) Stack {
return e.Op(ops).Push(ops)
}
// Path constructs a path for the ellipse.
func (e Ellipse) Path(ops *op.Ops) PathSpec {
// path constructs a path for the ellipse.
func (e Ellipse) path(o *op.Ops) PathSpec {
var p Path
p.Begin(ops)
p.Begin(o)
center := e.Bounds.Max.Add(e.Bounds.Min).Mul(.5)
diam := e.Bounds.Dx()
bounds := f32.Rectangle(e)
center := bounds.Max.Add(bounds.Min).Mul(.5)
diam := bounds.Dx()
r := diam * .5
// We'll model the ellipse as a circle scaled in the Y
// direction.
scale := e.Bounds.Dy() / diam
scale := bounds.Dy() / diam
// https://pomax.github.io/bezierinfo/#circles_cubic.
const q = 4 * (math.Sqrt2 - 1) / 3
@@ -190,7 +191,9 @@ func (e Ellipse) Path(ops *op.Ops) PathSpec {
f32.Point{X: center.X - curve, Y: center.Y - r*scale},
top,
)
return p.End()
ellipse := p.End()
ellipse.shape = ops.Ellipse
return ellipse
}
func fPt(p image.Point) f32.Point {
+3 -2
View File
@@ -3,8 +3,9 @@
/*
Package paint provides drawing operations for 2D graphics.
The PaintOp operation fills the current clip with the current brush,
taking the current transformation into account.
The PaintOp operation fills the current clip with the current brush, taking the
current transformation into account. Drawing outside the current clip area is
ignored.
The current brush is set by either a ColorOp for a constant color, or
ImageOp for an image, or LinearGradientOp for gradients.