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 (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
type TextInput struct {
TextBox
input string
label string
focused bool
}
func (ti *TextInput) InitTextInput(x, y int, width, height int, label string) {
ti.TextBox = *(NewFixedTextBox(x, y, width, height, "").Centering(true))
ti.input = ""
ti.label = label
ti.renderImpl = func() *ebiten.Image {
if ti.input == "" {
ti.text = ti.label
} else {
ti.text = ti.input
}
return ti.render()
}
}
func NewTextInput(x, y int, width, height int, label string) *TextInput {
w := &TextInput{}
w.InitTextInput(x, y, width, height, label)
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
}
// return the text or use label as fallback
func (ti *TextInput) TextOrLabel() string {
if ti.input != "" {
return ti.input
}
return ti.label
}
func (*TextInput) IsFocusable() bool { return true }
|