From be2aad1142bde494229d9d27d4123923aa578c1a Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Tue, 29 Apr 2025 14:44:48 -0500 Subject: overhaul ui * implement events instead of interfaces Interfaces and the wrappers head the problem that they could not support multiple events at once or only a single one without loosing the others. * add text input support for WASM on mobile A hidden html input is required to open the keyboard and receive the input. * improve the number choice * fix using the default number * add submit button only if a onclick method was provided --- go/ui/textInput.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 go/ui/textInput.go (limited to 'go/ui/textInput.go') diff --git a/go/ui/textInput.go b/go/ui/textInput.go new file mode 100644 index 00000000..3a7939c8 --- /dev/null +++ b/go/ui/textInput.go @@ -0,0 +1,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 } -- cgit v1.2.3