op/clip: replace Rect and RoundRect with Rect type

Remembering the order of the corners in the RoundRect is difficult,
which suggest that RoundRect should be a struct with named fields.

Do that, and make Rect the special case where corner radii are all
zero.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-11-18 14:33:28 +01:00
parent 101b65f4e5
commit 7299d1c875
4 changed files with 46 additions and 30 deletions
+1 -1
View File
@@ -249,7 +249,7 @@ func (l *List) layout() Dimensions {
} }
var stack op.StackOp var stack op.StackOp
stack.Push(ops) stack.Push(ops)
clip.Rect(ops, toRectF(r)).Add(ops) clip.Rect{Rect: toRectF(r)}.Op(ops).Add(ops)
op.TransformOp{}.Offset(toPointF(axisPoint(l.Axis, pos, cross))).Add(ops) op.TransformOp{}.Offset(toPointF(axisPoint(l.Axis, pos, cross))).Add(ops)
child.macro.Add(ops) child.macro.Add(ops)
stack.Pop() stack.Pop()
+29 -16
View File
@@ -284,25 +284,38 @@ func (p *Path) End() Op {
} }
} }
// Rect returns the clip area of a rectangle. // Rect represents the clip area of a rectangle with rounded
func Rect(ops *op.Ops, r f32.Rectangle) Op { // corners.The origin is in the upper left
ri := image.Rectangle{
Min: image.Point{X: int(r.Min.X), Y: int(r.Min.Y)},
Max: image.Point{X: int(r.Max.X), Y: int(r.Max.Y)},
}
// Optimize pixel-aligned rectangles to just its bounds.
if r == toRectF(ri) {
return Op{bounds: r}
}
return RoundRect(ops, r, 0, 0, 0, 0)
}
// RoundRect returns the clip area of a rectangle with rounded
// corners defined by their radii. The origin is in the upper left
// corner. // corner.
// Specify a square with corner radii equal to half the square size to // Specify a square with corner radii equal to half the square size to
// construct a circular clip area. // construct a circular clip area.
func RoundRect(ops *op.Ops, r f32.Rectangle, se, sw, nw, ne float32) Op { type Rect struct {
Rect f32.Rectangle
// The corner radii.
SE, SW, NW, NE float32
}
// Op returns the Op for the rectangle.
func (rr Rect) Op(ops *op.Ops) Op {
r := rr.Rect
// Optimize for the common pixel aligned rectangle with no
// corner rounding.
if rr.SE == 0 && rr.SW == 0 && rr.NW == 0 && rr.NE == 0 {
ri := image.Rectangle{
Min: image.Point{X: int(r.Min.X), Y: int(r.Min.Y)},
Max: image.Point{X: int(r.Max.X), Y: int(r.Max.Y)},
}
// Optimize pixel-aligned rectangles to just its bounds.
if r == toRectF(ri) {
return Op{bounds: r}
}
}
return roundRect(ops, r, rr.SE, rr.SW, rr.NW, rr.NE)
}
// roundRect returns the clip area of a rectangle with rounded
// corners defined by their radii.
func roundRect(ops *op.Ops, r f32.Rectangle, se, sw, nw, ne float32) Op {
size := r.Size() size := r.Size()
// https://pomax.github.io/bezierinfo/#circles_cubic. // https://pomax.github.io/bezierinfo/#circles_cubic.
w, h := float32(size.X), float32(size.Y) w, h := float32(size.X), float32(size.Y)
+15 -12
View File
@@ -77,13 +77,13 @@ func (b Button) Layout(gtx *layout.Context, button *widget.Button) {
}) })
bg := st.Expand(gtx, func() { bg := st.Expand(gtx, func() {
rr := float32(gtx.Px(unit.Dp(4))) rr := float32(gtx.Px(unit.Dp(4)))
clip.RoundRect(gtx.Ops, clip.Rect{
f32.Rectangle{Max: f32.Point{ Rect: f32.Rectangle{Max: f32.Point{
X: float32(gtx.Constraints.Width.Min), X: float32(gtx.Constraints.Width.Min),
Y: float32(gtx.Constraints.Height.Min), Y: float32(gtx.Constraints.Height.Min),
}}, }},
rr, rr, rr, rr, NE: rr, NW: rr, SE: rr, SW: rr,
).Add(gtx.Ops) }.Op(gtx.Ops).Add(gtx.Ops)
fill(gtx, bgcol) fill(gtx, bgcol)
for _, c := range button.History() { for _, c := range button.History() {
drawInk(gtx, c) drawInk(gtx, c)
@@ -112,10 +112,10 @@ func (b IconButton) Layout(gtx *layout.Context, button *widget.Button) {
bg := st.Expand(gtx, func() { bg := st.Expand(gtx, func() {
size := float32(gtx.Constraints.Width.Min) size := float32(gtx.Constraints.Width.Min)
rr := float32(size) * .5 rr := float32(size) * .5
clip.RoundRect(gtx.Ops, clip.Rect{
f32.Rectangle{Max: f32.Point{X: size, Y: size}}, Rect: f32.Rectangle{Max: f32.Point{X: size, Y: size}},
rr, rr, rr, rr, NE: rr, NW: rr, SE: rr, SW: rr,
).Add(gtx.Ops) }.Op(gtx.Ops).Add(gtx.Ops)
fill(gtx, bgcol) fill(gtx, bgcol)
for _, c := range button.History() { for _, c := range button.History() {
drawInk(gtx, c) drawInk(gtx, c)
@@ -154,10 +154,13 @@ func drawInk(gtx *layout.Context, c widget.Click) {
X: -rr, X: -rr,
Y: -rr, Y: -rr,
}).Add(gtx.Ops) }).Add(gtx.Ops)
clip.RoundRect(gtx.Ops, f32.Rectangle{Max: f32.Point{ clip.Rect{
X: float32(size), Rect: f32.Rectangle{Max: f32.Point{
Y: float32(size), X: float32(size),
}}, rr, rr, rr, rr).Add(gtx.Ops) Y: float32(size),
}},
NE: rr, NW: rr, SE: rr, SW: rr,
}.Op(gtx.Ops).Add(gtx.Ops)
paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{X: float32(size), Y: float32(size)}}}.Add(gtx.Ops) paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{X: float32(size), Y: float32(size)}}}.Add(gtx.Ops)
stack.Pop() stack.Pop()
op.InvalidateOp{}.Add(gtx.Ops) op.InvalidateOp{}.Add(gtx.Ops)
+1 -1
View File
@@ -37,7 +37,7 @@ func (im Image) Layout(gtx *layout.Context) {
d := image.Point{X: cs.Width.Constrain(w), Y: cs.Height.Constrain(h)} d := image.Point{X: cs.Width.Constrain(w), Y: cs.Height.Constrain(h)}
var s op.StackOp var s op.StackOp
s.Push(gtx.Ops) s.Push(gtx.Ops)
clip.Rect(gtx.Ops, f32.Rectangle{Max: toPointF(d)}).Add(gtx.Ops) clip.Rect{Rect: f32.Rectangle{Max: toPointF(d)}}.Op(gtx.Ops).Add(gtx.Ops)
im.Src.Add(gtx.Ops) im.Src.Add(gtx.Ops)
paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{X: float32(w), Y: float32(h)}}}.Add(gtx.Ops) paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{X: float32(w), Y: float32(h)}}}.Add(gtx.Ops)
s.Pop() s.Pop()