package ui import ( "fmt" "image/color" "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/text" "muhq.space/muhqs-game/go/game" ) var ( TEXTBOX_BACKGROUND = gray TEXTBOX_FOREGROUND = color.White ) type TextBox struct { X, Y int Width, Height int img *ebiten.Image text string } func NewFixedTextBox(x, y int, width, height int, t string) *TextBox { tb := &TextBox{x, y, width, height, nil, t} tb.render() return tb } func NewAutoTextBox(x, y int, t string) *TextBox { tb := &TextBox{x, y, -1, -1, nil, t} tb.render() return tb } func NewUnitInfo(x, y int, u *game.Unit) *TextBox { info := fmt.Sprintf("%s\nDamage: %d\nHealth: %d\nMovement: %d\nAttack %s\nUpkeep: %d", u.String(), u.GetDamage(), u.Health, u.Movement, u.Attack.String(), u.Upkeep) return NewAutoTextBox(x, y, info) } func NewPermInfo(x, y int, p game.Permanent) *TextBox { info := fmt.Sprintf("%s\nDamage: %d", p.String(), p.GetDamage()) return NewAutoTextBox(x, y, info) } func (tb *TextBox) render() { b := text.BoundString(Font, tb.text) if tb.Width == -1 || tb.Height == -1 { tb.Width = b.Dx() tb.Height = b.Dy() } tb.img = ebiten.NewImage(tb.Width, tb.Height) tb.img.Fill(TEXTBOX_BACKGROUND) text.Draw(tb.img, tb.text, Font, b.Min.X, -b.Min.Y, TEXTBOX_FOREGROUND) } func (tb *TextBox) Draw(screen *ebiten.Image) { op := &ebiten.DrawImageOptions{} op.GeoM.Translate(float64(tb.X), float64(tb.Y)) screen.DrawImage(tb.img, op) } func (tb TextBox) FindObjectAt(x, y int) interface{} { return nil }