aboutsummaryrefslogtreecommitdiff
path: root/go/ui/textBox.go
blob: 66b90698f25166788b5b537c6d82a2596c62664f (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package ui

import (
	"fmt"
	"image/color"

	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/inpututil"
	"github.com/hajimehoshi/ebiten/v2/text"
	"muhq.space/muhqs-game/go/font"
	"muhq.space/muhqs-game/go/game"
)

var (
	TEXTBOX_DEFAULT_BACKGROUND = color.Black
	TEXTBOX_DEFAULT_FOREGROUND = color.White

	TEXTBOX_DEFAULT_MARGIN = 3

	TEXTBOX_AUTO_DIMENSION = -1
)

type TextBox struct {
	WidgetBase
	text    string
	fg      color.Color
	bg      color.Color
	XMargin int
	YMargin int
	center  bool
}

func NewFixedTextBox(x, y int, width, height int, t string) *TextBox {
	tb := &TextBox{
		WidgetBase: NewWidgetBase(x, y, width, height),
		text:       t,
		fg:         TEXTBOX_DEFAULT_FOREGROUND,
		bg:         TEXTBOX_DEFAULT_BACKGROUND,
		XMargin:    TEXTBOX_DEFAULT_MARGIN,
		YMargin:    TEXTBOX_DEFAULT_MARGIN,
	}
	tb.renderImpl = func() *ebiten.Image { return tb.render() }
	return tb
}

func NewAutoTextBox(x, y int, text string) *TextBox {
	return NewFixedTextBox(x, y, TEXTBOX_AUTO_DIMENSION, TEXTBOX_AUTO_DIMENSION, text)
}

func (tb *TextBox) Bg(bg color.Color) *TextBox {
	tb.bg = bg
	return tb
}

func (tb *TextBox) Fg(fg color.Color) *TextBox {
	tb.fg = fg
	return tb
}

func (tb *TextBox) Centering(centering bool) *TextBox {
	tb.center = centering
	return tb
}

func NewUnitInfo(x, y int, u *game.Unit) *TextBox {
	info := fmt.Sprintf("%s\nDamage: %d\nHealth: %d\nUpkeep: %d",
		u.String(), u.Damage(), u.Health, u.UpkeepCost())

	if u.Movement != game.INVALID_MOVEMENT() {
		info = fmt.Sprintf("%s\nMovement: %s", info, u.Movement.String())
	}

	if !game.INVALID_ATTACK(u.Attack) {
		info = fmt.Sprintf("%s\nAttack: %s", info, u.Attack.String())
	}

	if len(u.Pile()) > 0 {
		info = fmt.Sprintf("%s\nPile:", info)
		for _, p := range u.Pile() {
			info = fmt.Sprintf("%s\n  %s", info, p.Card().Name)
		}
	}

	return NewAutoTextBox(x, y, info)
}

func NewGenericPermInfo(x, y int, p game.Permanent) *TextBox {
	info := fmt.Sprintf("%s\nDamage: %d", p.String(), p.Damage())
	return NewAutoTextBox(x, y, info)
}

func NewPermInfo(x, y int, p game.Permanent) *TextBox {
	switch p := p.(type) {
	case *game.Unit:
		return NewUnitInfo(x, y, p)
	default:
		return NewGenericPermInfo(x, y, p)
	}
}

func (tb *TextBox) render() *ebiten.Image {
	var x, y int
	b := text.BoundString(font.Font24, tb.text)
	if tb.Width == -1 || tb.Height == -1 {
		tb.Width = b.Dx() + 2*tb.XMargin
		tb.Height = b.Dy() + 2*tb.YMargin
		x = tb.XMargin + b.Min.X
		y = tb.YMargin - b.Min.Y
	} else {
		if !tb.center {
			x = tb.XMargin + b.Min.X
		} else {
			x = tb.Width/2 - centerTextXOffset(b)
		}

		y = tb.Height/2 - centerTextYOffset(b)
	}

	img := ebiten.NewImage(tb.Width, tb.Height)
	img.Fill(tb.bg)
	text.Draw(img, tb.text, font.Font24, x, y, tb.fg)
	return img
}

type PocList struct {
	TextBox
	hoverCardView
	centerX int
	bottomY int
	poc     game.PileOfCards
}

func NewPocList(centerX, bottomY int, poc game.PileOfCards) *PocList {
	w := &PocList{
		TextBox: *(NewAutoTextBox(-1, -1, "").Centering(true)),
		centerX: centerX,
		bottomY: bottomY,
		poc:     poc,
	}

	w.hoverCardView.init(w)

	w.renderImpl = func() *ebiten.Image {
		w.setText()
		return w.render()
	}
	return w
}

func (w *PocList) setText() {
	w.Width, w.Height = -1, -1
	t := ""
	for _, c := range w.poc.Cards() {
		t = fmt.Sprintf("%s%s\n", t, c.Name)
	}

	if t != "" {
		w.text = t[:len(t)-1]
	} else {
		w.text = "No cards"
	}

	b := text.BoundString(font.Font24, w.text)
	w.X = w.centerX - b.Dx()/2 - w.XMargin
	w.Y = w.bottomY - b.Dy() - w.YMargin
}

func (w *PocList) FindObjectAt(_x, _y int) interface{} {
	if !w.Contains(_x, _y) {
		return nil
	}

	cards := w.poc.Cards()
	if len(cards) == 0 {
		return nil
	}

	y := _y - w.Y

	b := text.BoundString(font.Font24, w.text)
	i := y / (b.Dy() / w.poc.Size())

	if i >= len(cards) {
		return nil
	}

	return cards[i]
}

type TextInput struct {
	TextBox
	input string
	label string
}

func NewTextInput(x, y int, width, height int, label string) *TextInput {
	w := &TextInput{
		TextBox: *(NewFixedTextBox(x, y, width, height, "").Centering(true)),
		input:   "",
		label:   label,
	}

	w.renderImpl = func() *ebiten.Image {
		if w.input == "" {
			w.text = w.label
		} else {
			w.text = w.input
		}

		return w.render()
	}

	return w
}

func (ti *TextInput) SetInput(input string) {
	ti.input = input
	ti.ForceRedraw()
}

func (ti *TextInput) AddInput(input []rune) {
	ti.SetInput(ti.input + string(input))
}

func (ti *TextInput) HandleKey() {
	if inpututil.IsKeyJustPressed(ebiten.KeyBackspace) {
		if len(ti.input) > 0 {
			ti.input = ti.input[:len(ti.input)-1]
			ti.ForceRedraw()
		}
	}
}

func (ti *TextInput) Text() string {
	return ti.input
}