package ui import ( "errors" "opslag.de/schobers/geom" ) type RenderBufferFn func(ctx Context, size geom.PointF32) type Buffer struct { texture Texture size geom.PointF32 } var ErrNewBufferSize = errors.New("buffer has been resized") 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 ErrNewBufferSize } func (b *Buffer) Render(ctx Context, pos geom.PointF32, fn RenderBufferFn) { if b.texture == nil { return } b.RenderContent(ctx, fn) b.RenderToDisplay(ctx, pos) } func (b *Buffer) RenderContent(ctx Context, fn RenderBufferFn) { renderer := ctx.Renderer() currTarget := renderer.Target() renderer.RenderTo(b.texture) fn(ctx, b.size) renderer.RenderTo(currTarget) } func (b *Buffer) RenderToDisplay(ctx Context, pos geom.PointF32) { if b.texture == nil { return } ctx.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) {}