zntg/ui/icon.go

53 lines
958 B
Go
Raw Permalink Normal View History

package ui
import (
"image"
"image/color"
"opslag.de/schobers/geom"
)
type IconPixelTestFn func(geom.PointF32) bool
func IconSize() geom.Point {
return geom.Pt(448, 512)
}
func CreateIcon(ctx Context, test IconPixelTestFn) Image {
icon := DrawIcon(test)
im, err := ctx.Renderer().CreateImage(icon)
if err != nil {
return nil
}
return im
}
func DrawIcon(test IconPixelTestFn) image.Image {
size := IconSize()
r := image.Rect(0, 0, size.X, size.Y)
icon := image.NewRGBA(r)
for y := 0; y < size.Y; y++ {
for x := 0; x < size.X; x++ {
pt := geom.PtF32(float32(x), float32(y))
if test(pt) {
icon.Set(x, y, color.White)
}
}
}
return icon
}
func GetOrCreateIcon(ctx Context, name string, testFactory func() IconPixelTestFn) Image {
im := ctx.Images().Image(name)
if im != nil {
return im
}
test := testFactory()
im = CreateIcon(ctx, test)
if im == nil {
return nil
}
ctx.Images().AddImage(name, im)
return im
}