Skip to content

Commit

Permalink
Bug fix: only render one frame at a time in Sequence (#824)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbroussard authored Jul 12, 2023
1 parent ad63bc4 commit 6792829
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
1 change: 1 addition & 0 deletions render/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (s Sequence) Paint(dc *gg.Context, bounds image.Rectangle, frameIdx int) {
dc.Push()
c.Paint(dc, bounds, frameIdx-fc)
dc.Pop()
break
}

fc += c.FrameCount()
Expand Down
166 changes: 166 additions & 0 deletions render/sequence_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package render

import (
"image"
"image/color"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSequenceOnlyOneFrameAtATime(t *testing.T) {
seq := Sequence{
Children: []Widget{
Box{Width: 3, Height: 3, Color: color.RGBA{0xff, 0, 0, 0xff}},
Box{Width: 6, Height: 3, Color: color.RGBA{0, 0xff, 0, 0xff}},
Box{Width: 9, Height: 3, Color: color.RGBA{0, 0, 0xff, 0xff}},
},
}

// Frame 0
im := PaintWidget(seq, image.Rect(0, 0, 10, 3), 0)
assert.Equal(t, nil, checkImage([]string{
"rrr",
"rrr",
"rrr",
}, im))

// Frame 1
im = PaintWidget(seq, image.Rect(0, 0, 10, 3), 1)
assert.Equal(t, nil, checkImage([]string{
"gggggg",
"gggggg",
"gggggg",
}, im))

// Frame 2
im = PaintWidget(seq, image.Rect(0, 0, 10, 3), 2)
assert.Equal(t, nil, checkImage([]string{
"bbbbbbbbb",
"bbbbbbbbb",
"bbbbbbbbb",
}, im))
}

func TestSequenceWithAnimatedChildren(t *testing.T) {
// Returns a 2x2 grid with background color, and a single
// pixel of foreground color at x,y.
frame := func(x, y int, fg color.Color, bg color.Color) Widget {
row0 := Row{
Children: []Widget{
Box{Width: 1, Height: 1, Color: bg},
Box{Width: 1, Height: 1, Color: bg},
},
}
row1 := Row{
Children: []Widget{
Box{Width: 1, Height: 1, Color: bg},
Box{Width: 1, Height: 1, Color: bg},
},
}
if y == 0 {
row0.Children[x] = Box{Width: 1, Height: 1, Color: fg}
} else {
row1.Children[x] = Box{Width: 1, Height: 1, Color: fg}
}
return Column{
Children: []Widget{row0, row1},
}
}

black := color.RGBA{0, 0, 0, 0}
red := color.RGBA{0xff, 0, 0, 0xff}
green := color.RGBA{0, 0xff, 0, 0xff}
blue := color.RGBA{0, 0, 0xff, 0xff}

anim0 := Animation{
Children: []Widget{
frame(0, 0, red, black),
frame(1, 0, red, black),
frame(1, 1, red, black),
frame(0, 1, red, black),
},
}
anim1 := Animation{
Children: []Widget{
frame(0, 0, green, black),
frame(1, 0, green, black),
frame(1, 1, green, black),
frame(0, 1, green, black),
},
}
anim2 := Animation{
Children: []Widget{
frame(0, 0, blue, black),
frame(1, 0, blue, black),
frame(1, 1, blue, black),
frame(0, 1, blue, black),
},
}

seq := Sequence{
Children: []Widget{
anim0,
anim1,
anim2,
},
}

assert.Equal(t, 12, seq.FrameCount())

expected := [][]string{
{
"r.",
"..",
},
{
".r",
"..",
},
{
"..",
".r",
},
{
"..",
"r.",
},
{
"g.",
"..",
},
{
".g",
"..",
},
{
"..",
".g",
},
{
"..",
"g.",
},
{
"b.",
"..",
},
{
".b",
"..",
},
{
"..",
".b",
},
{
"..",
"b.",
},
}

for i := 0; i < seq.FrameCount(); i++ {
im := PaintWidget(seq, image.Rect(0, 0, 2, 2), i)
assert.Equal(t, nil, checkImage(expected[i], im))
}
}

0 comments on commit 6792829

Please sign in to comment.