zntg/ui/buffer.go

62 lines
1.3 KiB
Go

package ui
import "opslag.de/schobers/geom"
type RenderBufferFn func(ctx Context, size geom.PointF32)
type Buffer struct {
texture Texture
size geom.PointF32
}
func (b *Buffer) Update(ctx Context, size geom.PointF32) error {
if b.texture != nil {
if size == b.size {
return nil
}
b.texture.Destroy()
b.texture = nil
b.size = geom.ZeroPtF32
}
texture, err := ctx.Renderer().CreateTextureTarget(size.X, size.Y)
if err != nil {
return err
}
b.texture = texture
b.size = size
return nil
}
func (b *Buffer) Render(ctx Context, pos geom.PointF32, fn RenderBufferFn) {
if b.texture == nil {
return
}
renderer := ctx.Renderer()
currTarget := renderer.Target()
renderer.RenderTo(b.texture)
fn(ctx, b.size)
renderer.RenderTo(currTarget)
renderer.DrawTexturePoint(b.texture, 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) {}