fix changing Canvas Bounds

This commit is contained in:
faiface 2017-03-08 19:19:20 +01:00
parent 5a9c43bc6c
commit 46f21a3096
1 changed files with 23 additions and 21 deletions

View File

@ -16,20 +16,20 @@ import (
// //
// It supports TrianglesPosition, TrianglesColor, TrianglesPicture and PictureColor. // It supports TrianglesPosition, TrianglesColor, TrianglesPicture and PictureColor.
type Canvas struct { type Canvas struct {
f *glhf.Frame // these should **only** be accessed through orig
s *glhf.Shader f *glhf.Frame
borders pixel.Rect
pixels []uint8
dirty bool
// these should **never** be accessed through orig
s *glhf.Shader
bounds pixel.Rect
mat mgl32.Mat3
col mgl32.Vec4
smooth bool smooth bool
mat mgl32.Mat3 orig *Canvas
col mgl32.Vec4
pixels []uint8
dirty bool
borders pixel.Rect
bounds pixel.Rect
orig *Canvas
} }
// NewCanvas creates a new empty, fully transparent Canvas with given bounds. If the smooth flag // NewCanvas creates a new empty, fully transparent Canvas with given bounds. If the smooth flag
@ -169,15 +169,15 @@ func (c *Canvas) SetBounds(bounds pixel.Rect) {
} }
mainthread.Call(func() { mainthread.Call(func() {
oldF := c.f oldF := c.orig.f
_, _, w, h := intBounds(bounds) _, _, w, h := intBounds(bounds)
c.f = glhf.NewFrame(w, h, c.smooth) c.f = glhf.NewFrame(w, h, c.smooth)
// preserve old content // preserve old content
if oldF != nil { if oldF != nil {
relBounds := c.bounds relBounds := bounds
relBounds.Pos -= bounds.Pos relBounds.Pos -= c.orig.borders.Pos
ox, oy, ow, oh := intBounds(relBounds) ox, oy, ow, oh := intBounds(relBounds)
oldF.Blit( oldF.Blit(
c.f, c.f,
@ -187,9 +187,11 @@ func (c *Canvas) SetBounds(bounds pixel.Rect) {
} }
}) })
c.orig = c // detach from the Original // detach from orig
c.borders = bounds c.borders = bounds
c.pixels = nil
c.dirty = true c.dirty = true
c.orig = c
} }
// Bounds returns the rectangular bounds of the Canvas. // Bounds returns the rectangular bounds of the Canvas.
@ -233,14 +235,14 @@ func (c *Canvas) Clear(color color.Color) {
mainthread.CallNonBlock(func() { mainthread.CallNonBlock(func() {
c.setGlhfBounds() c.setGlhfBounds()
c.f.Begin() c.orig.f.Begin()
glhf.Clear( glhf.Clear(
float32(nrgba.R), float32(nrgba.R),
float32(nrgba.G), float32(nrgba.G),
float32(nrgba.B), float32(nrgba.B),
float32(nrgba.A), float32(nrgba.A),
) )
c.f.End() c.orig.f.End()
}) })
} }
@ -267,7 +269,7 @@ func (c *Canvas) Original() pixel.Picture {
func (c *Canvas) Color(at pixel.Vec) pixel.NRGBA { func (c *Canvas) Color(at pixel.Vec) pixel.NRGBA {
if c.orig.dirty { if c.orig.dirty {
mainthread.Call(func() { mainthread.Call(func() {
tex := c.f.Texture() tex := c.orig.f.Texture()
tex.Begin() tex.Begin()
c.orig.pixels = tex.Pixels(0, 0, tex.Width(), tex.Height()) c.orig.pixels = tex.Pixels(0, 0, tex.Width(), tex.Height())
tex.End() tex.End()
@ -303,7 +305,7 @@ func (ct *canvasTriangles) draw(tex *glhf.Texture, borders, bounds pixel.Rect) {
mainthread.CallNonBlock(func() { mainthread.CallNonBlock(func() {
ct.dst.setGlhfBounds() ct.dst.setGlhfBounds()
ct.dst.f.Begin() ct.dst.orig.f.Begin()
ct.dst.s.Begin() ct.dst.s.Begin()
ct.dst.s.SetUniformAttr(canvasBounds, mgl32.Vec4{ ct.dst.s.SetUniformAttr(canvasBounds, mgl32.Vec4{
@ -347,7 +349,7 @@ func (ct *canvasTriangles) draw(tex *glhf.Texture, borders, bounds pixel.Rect) {
} }
ct.dst.s.End() ct.dst.s.End()
ct.dst.f.End() ct.dst.orig.f.End()
}) })
} }
@ -436,7 +438,7 @@ func (ccp *canvasCanvasPicture) Draw(t pixel.TargetTriangles) {
if ccp.dst != ct.dst { if ccp.dst != ct.dst {
panic(fmt.Errorf("%T.Draw: TargetTriangles generated by different Canvas", ccp)) panic(fmt.Errorf("%T.Draw: TargetTriangles generated by different Canvas", ccp))
} }
ct.draw(ccp.src.f.Texture(), ccp.src.orig.borders, ccp.bounds) ct.draw(ccp.src.orig.f.Texture(), ccp.src.orig.borders, ccp.bounds)
} }
const ( const (