2019-04-09 18:32:42 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import "opslag.de/schobers/geom"
|
|
|
|
|
2019-04-10 19:25:41 +00:00
|
|
|
type RenderBufferFn func(ctx Context, size geom.PointF32)
|
|
|
|
|
2019-04-09 18:32:42 +00:00
|
|
|
type Buffer struct {
|
2020-05-12 21:03:43 +00:00
|
|
|
texture Texture
|
|
|
|
size geom.PointF32
|
2019-04-09 18:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Buffer) Update(ctx Context, size geom.PointF32) error {
|
2020-05-12 21:03:43 +00:00
|
|
|
if b.texture != nil {
|
2019-04-10 19:25:41 +00:00
|
|
|
if size == b.size {
|
2019-04-09 18:32:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-05-12 21:03:43 +00:00
|
|
|
b.texture.Destroy()
|
|
|
|
b.texture = nil
|
2019-04-10 19:25:41 +00:00
|
|
|
b.size = geom.ZeroPtF32
|
2019-04-09 18:32:42 +00:00
|
|
|
}
|
2020-05-15 07:20:44 +00:00
|
|
|
texture, err := ctx.Renderer().CreateTextureTarget(size.X, size.Y)
|
2019-04-09 18:32:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-12 21:03:43 +00:00
|
|
|
b.texture = texture
|
2019-04-10 19:25:41 +00:00
|
|
|
b.size = size
|
2019-04-09 18:32:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-10 19:25:41 +00:00
|
|
|
func (b *Buffer) Render(ctx Context, pos geom.PointF32, fn RenderBufferFn) {
|
2020-05-12 21:03:43 +00:00
|
|
|
if b.texture == nil {
|
2019-04-10 19:25:41 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-09 18:32:42 +00:00
|
|
|
renderer := ctx.Renderer()
|
|
|
|
currTarget := renderer.Target()
|
2020-05-12 21:03:43 +00:00
|
|
|
renderer.RenderTo(b.texture)
|
2019-04-10 19:25:41 +00:00
|
|
|
fn(ctx, b.size)
|
2019-04-09 18:32:42 +00:00
|
|
|
renderer.RenderTo(currTarget)
|
2020-05-12 21:03:43 +00:00
|
|
|
renderer.DrawTexture(b.texture, pos)
|
2019-04-09 18:32:42 +00:00
|
|
|
}
|
2019-04-10 19:25:41 +00:00
|
|
|
|
|
|
|
type BufferControl struct {
|
|
|
|
ControlBase
|
|
|
|
|
|
|
|
buffer Buffer
|
|
|
|
}
|
|
|
|
|
2019-04-11 21:38:32 +00:00
|
|
|
func (c *BufferControl) Arrange(ctx Context, bounds geom.RectangleF32, offset geom.PointF32, parent Control) {
|
|
|
|
c.ControlBase.Arrange(ctx, bounds, offset, parent)
|
2019-04-10 19:25:41 +00:00
|
|
|
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) {}
|