From 04779d6588f99f755e1254393480b521f4574f89 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Wed, 10 Apr 2019 21:25:41 +0200 Subject: [PATCH] Changed Buffer.Render signature. Added BufferControl on top of Buffer. --- ui/buffer.go | 37 +++++++++++++++++++++++++++++++++---- ui/overflow.go | 2 +- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/ui/buffer.go b/ui/buffer.go index 634217a..73b7c1e 100644 --- a/ui/buffer.go +++ b/ui/buffer.go @@ -2,31 +2,60 @@ package ui import "opslag.de/schobers/geom" +type RenderBufferFn func(ctx Context, size geom.PointF32) + type Buffer struct { - im Image + im Image + size geom.PointF32 } func (b *Buffer) Update(ctx Context, size geom.PointF32) error { if b.im != nil { - if b.im.Width() == size.X && b.im.Height() == size.Y { + if size == b.size { return nil } b.im.Destroy() b.im = nil + b.size = geom.ZeroPtF32 } im, err := ctx.Renderer().CreateImageSize(size.X, size.Y) if err != nil { return err } b.im = im + b.size = size return nil } -func (b *Buffer) Render(ctx Context, pos geom.PointF32, render func()) { +func (b *Buffer) Render(ctx Context, pos geom.PointF32, fn RenderBufferFn) { + if b.im == nil { + return + } renderer := ctx.Renderer() currTarget := renderer.Target() renderer.RenderTo(b.im) - render() + fn(ctx, b.size) renderer.RenderTo(currTarget) renderer.DrawImage(b.im, pos) } + +type BufferControl struct { + ControlBase + + buffer Buffer +} + +func (c *BufferControl) Arrange(ctx Context, bounds geom.RectangleF32, offset geom.PointF32) { + c.ControlBase.Arrange(ctx, bounds, offset) + c.buffer.Update(ctx, bounds.Size()) +} + +func (c *BufferControl) Render(ctx Context) { + c.RenderFn(ctx, c.RenderBuffer) +} + +func (c *BufferControl) RenderFn(ctx Context, render RenderBufferFn) { + c.buffer.Render(ctx, c.bounds.Min, render) +} + +func (c *BufferControl) RenderBuffer(ctx Context, size geom.PointF32) {} diff --git a/ui/overflow.go b/ui/overflow.go index 046ad62..16a1e36 100644 --- a/ui/overflow.go +++ b/ui/overflow.go @@ -128,7 +128,7 @@ func (o *overflow) Render(ctx Context) { if err != nil { panic(err) } - o.content.Render(ctx, o.bounds.Min, func() { + o.content.Render(ctx, o.bounds.Min, func(Context, geom.PointF32) { renderer.Clear(color.Transparent) o.Content.Render(ctx) })