all: [API] change op.Offset to take integer coordinates

op.Offset is a convenience function most often used by layouts. Layouts
usually operate in integer coordinates, and the float32 version of op.Offset
needlessly force conversions from int to float32. This change makes op.Offset
take integer coordinates, to better match its intended use.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2022-04-26 09:22:10 +02:00
parent 14805af367
commit a63e0cb44a
23 changed files with 70 additions and 72 deletions
+9 -9
View File
@@ -86,7 +86,7 @@ func BenchmarkDrawUI(b *testing.B) {
resetOps(gtx)
off := float32(math.Mod(float64(i)/10, 10))
t := op.Offset(f32.Pt(off, off)).Push(gtx.Ops)
t := op.Affine(f32.Affine2D{}.Offset(f32.Pt(off, off))).Push(gtx.Ops)
drawCore(gtx, th)
@@ -157,13 +157,13 @@ func Benchmark1000CirclesInstanced(b *testing.B) {
func draw1000Circles(gtx layout.Context) {
ops := gtx.Ops
for x := 0; x < 100; x++ {
op.Offset(f32.Pt(float32(x*10), 0)).Add(ops)
op.Offset(image.Pt(x*10, 0)).Add(ops)
for y := 0; y < 10; y++ {
paint.FillShape(ops,
color.NRGBA{R: 100 + uint8(x), G: 100 + uint8(y), B: 100, A: 120},
clip.RRect{Rect: f32.Rect(0, 0, 10, 10), NE: 5, SE: 5, SW: 5, NW: 5}.Op(ops),
)
op.Offset(f32.Pt(0, float32(100))).Add(ops)
op.Offset(image.Pt(0, 100)).Add(ops)
}
}
}
@@ -178,11 +178,11 @@ func draw1000CirclesInstanced(gtx layout.Context) {
c := r.Stop()
for x := 0; x < 100; x++ {
op.Offset(f32.Pt(float32(x*10), 0)).Add(ops)
op.Offset(image.Pt(x*10, 0)).Add(ops)
for y := 0; y < 10; y++ {
paint.ColorOp{Color: color.NRGBA{R: 100 + uint8(x), G: 100 + uint8(y), B: 100, A: 120}}.Add(ops)
c.Add(ops)
op.Offset(f32.Pt(0, float32(100))).Add(ops)
op.Offset(image.Pt(0, 100)).Add(ops)
}
}
}
@@ -203,13 +203,13 @@ func drawIndividualShapes(gtx layout.Context, th *material.Theme) chan op.CallOp
ops := &op1
c := op.Record(ops)
for x := 0; x < 9; x++ {
op.Offset(f32.Pt(float32(x*50), 0)).Add(ops)
op.Offset(image.Pt(x*50, 0)).Add(ops)
for y := 0; y < 9; y++ {
paint.FillShape(ops,
color.NRGBA{R: 100 + uint8(x), G: 100 + uint8(y), B: 100, A: 120},
clip.RRect{Rect: f32.Rect(0, 0, 25, 25), NE: 10, SE: 10, SW: 10, NW: 10}.Op(ops),
)
op.Offset(f32.Pt(0, float32(50))).Add(ops)
op.Offset(image.Pt(0, 50)).Add(ops)
}
}
c1 <- c.Stop()
@@ -233,7 +233,7 @@ func drawShapeInstances(gtx layout.Context, th *material.Theme) chan op.CallOp {
rad := float32(0)
for x := 0; x < 20; x++ {
for y := 0; y < 20; y++ {
t := op.Offset(f32.Pt(float32(x*50+25), float32(y*50+25))).Push(ops)
t := op.Offset(image.Pt(x*50+25, y*50+25)).Push(ops)
c.Add(ops)
t.Pop()
rad += math.Pi * 2 / 400
@@ -253,7 +253,7 @@ func drawText(gtx layout.Context, th *material.Theme) chan op.CallOp {
txt := material.H6(th, "")
for x := 0; x < 40; x++ {
txt.Text = textRows[x]
t := op.Offset(f32.Pt(float32(0), float32(24*x))).Push(ops)
t := op.Offset(image.Pt(0, 24*x)).Push(ops)
gtx.Ops = ops
txt.Layout(gtx)
t.Pop()
+4 -4
View File
@@ -124,13 +124,13 @@ func TestPaintTexture(t *testing.T) {
func TestTexturedStrokeClipped(t *testing.T) {
run(t, func(o *op.Ops) {
smallSquares.Add(o)
defer op.Offset(f32.Pt(50, 50)).Push(o).Pop()
defer op.Offset(image.Pt(50, 50)).Push(o).Pop()
defer clip.Stroke{
Path: clip.RRect{Rect: f32.Rect(0, 0, 30, 30)}.Path(o),
Width: 10,
}.Op().Push(o).Pop()
defer clip.RRect{Rect: f32.Rect(-30, -30, 60, 60)}.Push(o).Pop()
defer op.Offset(f32.Pt(-10, -10)).Push(o).Pop()
defer op.Offset(image.Pt(-10, -10)).Push(o).Pop()
paint.PaintOp{}.Add(o)
}, func(r result) {
})
@@ -139,12 +139,12 @@ func TestTexturedStrokeClipped(t *testing.T) {
func TestTexturedStroke(t *testing.T) {
run(t, func(o *op.Ops) {
smallSquares.Add(o)
defer op.Offset(f32.Pt(50, 50)).Push(o).Pop()
defer op.Offset(image.Pt(50, 50)).Push(o).Pop()
defer clip.Stroke{
Path: clip.RRect{Rect: f32.Rect(0, 0, 30, 30)}.Path(o),
Width: 10,
}.Op().Push(o).Pop()
defer op.Offset(f32.Pt(-10, -10)).Push(o).Pop()
defer op.Offset(image.Pt(-10, -10)).Push(o).Pop()
paint.PaintOp{}.Add(o)
}, func(r result) {
})
+6 -6
View File
@@ -35,7 +35,7 @@ func TestTransformMacro(t *testing.T) {
m2 := op.Record(o)
paint.ColorOp{Color: red}.Add(o)
// Simulate a draw text call
t := op.Offset(f32.Pt(0, 10)).Push(o)
t := op.Offset(image.Pt(0, 10)).Push(o)
// Apply the clip-path.
cl := c.Push(o)
@@ -47,10 +47,10 @@ func TestTransformMacro(t *testing.T) {
c2 := m2.Stop()
// Call each of them in a transform
t = op.Offset(f32.Pt(0, 0)).Push(o)
t = op.Offset(image.Pt(0, 0)).Push(o)
c1.Add(o)
t.Pop()
t = op.Offset(f32.Pt(0, 0)).Push(o)
t = op.Offset(image.Pt(0, 0)).Push(o)
c2.Add(o)
t.Pop()
}, func(r result) {
@@ -164,7 +164,7 @@ func TestReuseStencil(t *testing.T) {
// lay out the children
c1.Add(ops)
defer op.Offset(f32.Pt(0, 50)).Push(ops).Pop()
defer op.Offset(image.Pt(0, 50)).Push(ops).Pop()
c2.Add(ops)
}, func(r result) {
r.expect(5, 5, colornames.Black)
@@ -178,8 +178,8 @@ func TestBuildOffscreen(t *testing.T) {
// frame.
txt := constSqCirc()
draw := func(off float32, o *op.Ops) {
defer op.Offset(f32.Pt(0, off)).Push(o).Pop()
draw := func(off int, o *op.Ops) {
defer op.Offset(image.Pt(0, off)).Push(o).Pop()
defer txt.Push(o).Pop()
paint.PaintOp{}.Add(o)
}
+5 -5
View File
@@ -17,7 +17,7 @@ import (
func TestPaintOffset(t *testing.T) {
run(t, func(o *op.Ops) {
defer op.Offset(f32.Pt(10, 20)).Push(o).Pop()
defer op.Offset(image.Pt(10, 20)).Push(o).Pop()
paint.FillShape(o, red, clip.Rect(image.Rect(0, 0, 50, 50)).Op())
}, func(r result) {
r.expect(0, 0, transparent)
@@ -53,7 +53,7 @@ func TestPaintShear(t *testing.T) {
func TestClipPaintOffset(t *testing.T) {
run(t, func(o *op.Ops) {
defer clip.RRect{Rect: f32.Rect(10, 10, 30, 30)}.Push(o).Pop()
defer op.Offset(f32.Pt(20, 20)).Push(o).Pop()
defer op.Offset(image.Pt(20, 20)).Push(o).Pop()
paint.FillShape(o, red, clip.Rect(image.Rect(0, 0, 100, 100)).Op())
}, func(r result) {
r.expect(0, 0, transparent)
@@ -65,7 +65,7 @@ func TestClipPaintOffset(t *testing.T) {
func TestClipOffset(t *testing.T) {
run(t, func(o *op.Ops) {
defer op.Offset(f32.Pt(20, 20)).Push(o).Pop()
defer op.Offset(image.Pt(20, 20)).Push(o).Pop()
defer clip.RRect{Rect: f32.Rect(10, 10, 30, 30)}.Push(o).Pop()
paint.FillShape(o, red, clip.Rect(image.Rect(0, 0, 100, 100)).Op())
}, func(r result) {
@@ -105,7 +105,7 @@ func TestClipRotate(t *testing.T) {
func TestOffsetTexture(t *testing.T) {
run(t, func(o *op.Ops) {
defer op.Offset(f32.Pt(15, 15)).Push(o).Pop()
defer op.Offset(image.Pt(15, 15)).Push(o).Pop()
squares.Add(o)
defer scale(50.0/512, 50.0/512).Push(o).Pop()
paint.PaintOp{}.Add(o)
@@ -119,7 +119,7 @@ func TestOffsetTexture(t *testing.T) {
func TestOffsetScaleTexture(t *testing.T) {
run(t, func(o *op.Ops) {
defer op.Offset(f32.Pt(15, 15)).Push(o).Pop()
defer op.Offset(image.Pt(15, 15)).Push(o).Pop()
squares.Add(o)
defer op.Affine(f32.Affine2D{}.Scale(f32.Point{}, f32.Pt(2, 1))).Push(o).Pop()
defer scale(50.0/512, 50.0/512).Push(o).Pop()