2019-04-11 19:23:51 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
|
|
|
|
"opslag.de/schobers/geom"
|
|
|
|
)
|
|
|
|
|
2020-05-15 07:20:44 +00:00
|
|
|
type AlphaPixelImageSource struct {
|
|
|
|
ImageAlphaPixelTestFn
|
|
|
|
|
|
|
|
Size geom.Point
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AlphaPixelImageSource) CreateImage() (image.Image, error) {
|
|
|
|
return DrawImageAlpha(s.Size, s.ImageAlphaPixelTestFn), nil
|
|
|
|
}
|
|
|
|
|
2019-07-08 17:12:10 +00:00
|
|
|
type ImageAlphaPixelTestFn func(geom.PointF32) uint8
|
2019-04-11 19:23:51 +00:00
|
|
|
|
2020-05-15 07:20:44 +00:00
|
|
|
func (f ImageAlphaPixelTestFn) CreateImageSource(size geom.Point) ImageSource {
|
|
|
|
return &AlphaPixelImageSource{f, size}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ImagePixelTestFn func(geom.PointF32) bool
|
|
|
|
|
|
|
|
func (f ImagePixelTestFn) CreateImageSource(size geom.Point) ImageSource {
|
|
|
|
return &PixelImageSource{f, size}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTexture(ctx Context, source ImageSource) Texture {
|
|
|
|
texture, err := ctx.Renderer().CreateTexture(source)
|
2019-04-11 19:23:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-12 21:03:43 +00:00
|
|
|
return texture
|
2019-04-11 19:23:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 21:03:43 +00:00
|
|
|
func CreateIcon(ctx Context, test ImagePixelTestFn) Texture {
|
2020-05-15 07:20:44 +00:00
|
|
|
return createTexture(ctx, test.CreateImageSource(IconSize()))
|
2019-07-08 17:12:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 21:03:43 +00:00
|
|
|
func CreateTexture(ctx Context, size geom.Point, test ImagePixelTestFn) Texture {
|
2020-05-15 07:20:44 +00:00
|
|
|
return createTexture(ctx, test.CreateImageSource(size))
|
2019-07-08 17:12:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 21:03:43 +00:00
|
|
|
func CreateTextureAlpha(ctx Context, size geom.Point, test ImageAlphaPixelTestFn) Texture {
|
2020-05-15 07:20:44 +00:00
|
|
|
return createTexture(ctx, test.CreateImageSource(size))
|
2019-07-08 17:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func DrawIcon(test ImagePixelTestFn) image.Image {
|
2019-04-11 19:23:51 +00:00
|
|
|
size := IconSize()
|
2019-07-08 17:12:10 +00:00
|
|
|
return DrawImage(size, test)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DrawImage(size geom.Point, test ImagePixelTestFn) image.Image {
|
2019-04-11 19:23:51 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-08 17:12:10 +00:00
|
|
|
func DrawImageAlpha(size geom.Point, test ImageAlphaPixelTestFn) image.Image {
|
|
|
|
r := image.Rect(0, 0, size.X, size.Y)
|
|
|
|
icon := image.NewAlpha(r)
|
|
|
|
for y := 0; y < size.Y; y++ {
|
|
|
|
for x := 0; x < size.X; x++ {
|
|
|
|
pt := geom.PtF32(float32(x), float32(y))
|
|
|
|
if a := test(pt); a > 0 {
|
|
|
|
icon.Set(x, y, color.Alpha{A: a})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return icon
|
|
|
|
}
|
|
|
|
|
2020-05-15 07:20:44 +00:00
|
|
|
func GetOrCreateIcon(ctx Context, name string, test ImagePixelTestFn) Texture {
|
2020-05-12 21:03:43 +00:00
|
|
|
texture := ctx.Textures().Texture(name)
|
|
|
|
if texture != nil {
|
|
|
|
return texture
|
2019-04-11 19:23:51 +00:00
|
|
|
}
|
2020-05-15 07:20:44 +00:00
|
|
|
texture, err := ctx.Textures().CreateTexture(name, test.CreateImageSource(IconSize()))
|
|
|
|
if err != nil {
|
2019-04-11 19:23:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-05-12 21:03:43 +00:00
|
|
|
return texture
|
2019-04-11 19:23:51 +00:00
|
|
|
}
|
2019-07-08 17:12:10 +00:00
|
|
|
|
|
|
|
func IconSize() geom.Point {
|
|
|
|
return geom.Pt(448, 512)
|
|
|
|
}
|
2020-05-15 07:20:44 +00:00
|
|
|
|
|
|
|
type PixelImageSource struct {
|
|
|
|
ImagePixelTestFn
|
|
|
|
|
|
|
|
Size geom.Point
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PixelImageSource) CreateImage() (image.Image, error) {
|
|
|
|
return DrawImage(s.Size, s.ImagePixelTestFn), nil
|
|
|
|
}
|