zntg/ui/renderer.go
Sander Schobers 0f03760e66 Added Resize & SetIcon to Renderer.
Refactored Size (on Renderer) to return geom.Point instead of geom.PointF32.
Refactored Width and Height (on Texture) to return int instead of float32.

Refactored texture dimensions to be represented by ints instead of float32s.
2020-12-13 07:40:19 +01:00

63 lines
1.8 KiB
Go

package ui
import (
"image"
"image/color"
"opslag.de/schobers/geom"
"opslag.de/schobers/zntg"
)
type Renderer interface {
// Events
PushEvents(t EventTarget, wait bool) bool
Refresh()
// Lifetime
Destroy() error
// Drawing
Clear(c color.Color)
CreateFontPath(path string, size int) (Font, error)
CreateTexture(m ImageSource) (Texture, error)
CreateTextureGo(m image.Image, source bool) (Texture, error)
CreateTexturePath(path string, source bool) (Texture, error)
CreateTextureTarget(w, h float32) (Texture, error)
DefaultTarget() Texture
DrawTexture(t Texture, p geom.RectangleF32)
DrawTextureOptions(t Texture, p geom.RectangleF32, opts DrawOptions)
DrawTexturePoint(t Texture, p geom.PointF32)
DrawTexturePointOptions(t Texture, p geom.PointF32, opts DrawOptions)
FillRectangle(r geom.RectangleF32, c color.Color)
Rectangle(r geom.RectangleF32, c color.Color, thickness float32)
RenderTo(Texture)
RenderToDisplay()
Resize(width, height int)
SetIcon(source ImageSource)
SetMouseCursor(c MouseCursor)
Size() geom.Point
Target() Texture
Text(font Font, p geom.PointF32, color color.Color, text string)
TextAlign(font Font, p geom.PointF32, color color.Color, text string, align HorizontalAlignment)
TextTexture(font Font, color color.Color, text string) (Texture, error)
// Resources
Resources() Resources
SetResourceProvider(resources Resources)
}
// TextTexture renders specified text to a new texture.
func TextTexture(render Renderer, font Font, color color.Color, text string) (Texture, error) {
target := render.Target()
defer render.RenderTo(target)
bounds := font.Measure(text)
texture, err := render.CreateTextureTarget(bounds.Dx(), bounds.Dy())
if err != nil {
return nil, err
}
render.RenderTo(texture)
render.Clear(zntg.MustHexColor(`#00000000`))
render.Text(font, bounds.Min.Invert(), color, text)
return texture, nil
}