diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2023-01-20 03:18:06 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-01-27 16:43:44 +0100 |
| commit | c4cb9637f5c85d03aa162968f0dfec3c193fc9dc (patch) | |
| tree | 3ad32e5dfd7c4ae72f66b86e77edef7b700a8b7a /go/ui/textBox.go | |
| parent | 9df5cc7b55d8ac9ecd775a14a14f80a6c36c4d74 (diff) | |
| download | muhqs-game-c4cb9637f5c85d03aa162968f0dfec3c193fc9dc.tar.gz muhqs-game-c4cb9637f5c85d03aa162968f0dfec3c193fc9dc.zip | |
intermediate commit
Implement actions and multiple ui widgets
Diffstat (limited to 'go/ui/textBox.go')
| -rw-r--r-- | go/ui/textBox.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/go/ui/textBox.go b/go/ui/textBox.go new file mode 100644 index 00000000..e07440b5 --- /dev/null +++ b/go/ui/textBox.go @@ -0,0 +1,67 @@ +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 +} |
