zntg/ui/buffer.go

62 lines
1.3 KiB
Go
Raw Permalink Normal View History

2019-04-09 18:32:42 +00:00
package ui
import "opslag.de/schobers/geom"
type RenderBufferFn func(ctx Context, size geom.PointF32)
2019-04-09 18:32:42 +00:00
type Buffer struct {
im Image
size geom.PointF32
2019-04-09 18:32:42 +00:00
}
func (b *Buffer) Update(ctx Context, size geom.PointF32) error {
if b.im != nil {
if size == b.size {
2019-04-09 18:32:42 +00:00
return nil
}
b.im.Destroy()
b.im = nil
b.size = geom.ZeroPtF32
2019-04-09 18:32:42 +00:00
}
im, err := ctx.Renderer().CreateImageSize(size.X, size.Y)
if err != nil {
return err
}
b.im = im
b.size = size
2019-04-09 18:32:42 +00:00
return nil
}
func (b *Buffer) Render(ctx Context, pos geom.PointF32, fn RenderBufferFn) {
if b.im == nil {
return
}
2019-04-09 18:32:42 +00:00
renderer := ctx.Renderer()
currTarget := renderer.Target()
renderer.RenderTo(b.im)
fn(ctx, b.size)
2019-04-09 18:32:42 +00:00
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, parent Control) {
c.ControlBase.Arrange(ctx, bounds, offset, parent)
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) {}