gui/image.go
2023-01-22 12:38:03 +01:00

82 lines
1.7 KiB
Go

package gui
import (
"image"
"image/color"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/text"
"golang.org/x/image/font"
)
type Image struct {
img *ebiten.Image
bounds image.Rectangle
}
func newImage(img *ebiten.Image) *Image {
return &Image{
img: img,
bounds: img.Bounds(),
}
}
func NewImage(width, height int) *Image {
return &Image{
img: ebiten.NewImage(width, height),
bounds: image.Rect(0, 0, width, height),
}
}
func (i *Image) SubImage(rect image.Rectangle) image.Image {
img := i.img.SubImage(rect).(*ebiten.Image)
return &Image{
img: img,
bounds: img.Bounds().Intersect(i.bounds),
}
}
func (i *Image) At(x, y int) color.Color {
return i.image().At(x, y)
}
func (i *Image) Constrain(rect image.Rectangle) *Image {
return &Image{
img: i.img,
bounds: i.bounds.Intersect(rect),
}
}
func (i *Image) ColorModel() color.Model {
return i.image().ColorModel()
}
func (i *Image) Const() image.Rectangle {
return i.bounds
}
func (i *Image) Bounds() image.Rectangle {
return i.img.Bounds()
}
func (i *Image) image() *ebiten.Image {
return i.img.SubImage(i.bounds).(*ebiten.Image)
}
func (i *Image) Fill(c color.Color) {
i.image().Fill(c)
}
func (i *Image) DrawString(s string, f font.Face, x, y int, c color.Color) {
text.Draw(i.image(), s, f, i.img.Bounds().Min.X+x, i.img.Bounds().Min.Y+y, c)
}
func (i *Image) DrawImage(img *Image, op *ebiten.DrawImageOptions) {
i.image().DrawImage(img.img, op)
}
func (i *Image) DrawRect(rect image.Rectangle, colr color.Color) {
ebitenutil.DrawRect(i.image(), float64(rect.Min.X), float64(rect.Min.Y), float64(rect.Dx()), float64(rect.Dy()), colr)
}