aboutsummaryrefslogtreecommitdiff
path: root/go/ui/textBox.go
blob: e07440b516f0a0b8ee6b9d4ffc2837bbd249e372 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
}