zntg/ui/buffer.go
Sander Schobers cdfb863ab0 Added SDL backend.
Added Action{,s}. List of actions that can be used to defer cleanup code (see NewRenderer implementations).
Added TextInputEvent (replaces the old KeyPressEvent) and added to new events KeyDown & KeyUp.
Added VSync to NewRendererOptions.
Removed IconScale from button.
Added ImageSource interface that replaces the Image/Texture method on the Texture interface. This makes converting back a texture to an image optional (since this is atypical for a hardware texture for instance).
Added new KeyModifier: OSCommand (Windows/Command key).
Added KeyState that can keep the state of keys (pressed or not).
Added KeyEnter, representing the Enter key.
Changed signatures of CreateTexture methods in Renderer.
Changed signatures of icon related method (removed factories).
Basic example now depends on sdlgui.
2020-05-15 09:20:44 +02:00

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.DrawTexture(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) {}