aboutsummaryrefslogtreecommitdiff
path: root/go/ui/textBox.go
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-02-03 14:05:14 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2025-08-20 15:46:38 +0200
commit8802f30ff720fcb64d9207c4cb673dc6805390c2 (patch)
tree9be529221105fc8681e3afb0e673e229ef9504d4 /go/ui/textBox.go
parente8f44219976edf8cf57cbd9e964f66fe9d145a9c (diff)
downloadmuhqs-game-8802f30ff720fcb64d9207c4cb673dc6805390c2.tar.gz
muhqs-game-8802f30ff720fcb64d9207c4cb673dc6805390c2.zip
add text input and properly center text
Diffstat (limited to 'go/ui/textBox.go')
-rw-r--r--go/ui/textBox.go67
1 files changed, 51 insertions, 16 deletions
diff --git a/go/ui/textBox.go b/go/ui/textBox.go
index b4a580bf..d0afbd9b 100644
--- a/go/ui/textBox.go
+++ b/go/ui/textBox.go
@@ -5,6 +5,7 @@ import (
"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"
@@ -19,7 +20,7 @@ var (
type TextBox struct {
WidgetBase
- Text string
+ text string
Fg color.Color
Bg color.Color
XMargin int
@@ -30,7 +31,7 @@ type TextBox struct {
func NewFixedTextBox(x, y int, width, height int, t string, center bool) *TextBox {
tb := &TextBox{
WidgetBase: NewWidgetBase(x, y, width, height),
- Text: t,
+ text: t,
Fg: TEXTBOX_DEFAULT_FOREGROUND,
Bg: TEXTBOX_DEFAULT_BACKGROUND,
XMargin: TEXTBOX_DEFAULT_MARGIN,
@@ -75,7 +76,7 @@ func NewPermInfo(x, y int, p game.Permanent) *TextBox {
func (tb *TextBox) render() *ebiten.Image {
var x, y int
- b := text.BoundString(font.Font24, tb.Text)
+ 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
@@ -85,21 +86,15 @@ func (tb *TextBox) render() *ebiten.Image {
if !tb.Center {
x = tb.XMargin + b.Min.X
} else {
- x = (tb.Width - b.Dx()) / 2
- if x < 0 {
- x = 0
- }
+ x = tb.Width/2 - centerTextXOffset(b)
}
- y = tb.Height - b.Dy()/2
- if y < 0 {
- y = 0
- }
+ 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)
+ text.Draw(img, tb.text, font.Font24, x, y, tb.Fg)
return img
}
@@ -114,7 +109,7 @@ func NewPocList(centerX, bottomY int, poc game.PileOfCards) *PocList {
w := &PocList{
TextBox: TextBox{
WidgetBase: NewWidgetBase(-1, -1, -1, -1),
- Text: "",
+ text: "",
Fg: TEXTBOX_DEFAULT_FOREGROUND,
Bg: TEXTBOX_DEFAULT_BACKGROUND,
XMargin: TEXTBOX_DEFAULT_MARGIN,
@@ -139,12 +134,12 @@ func (w *PocList) setText() {
}
if t != "" {
- w.Text = t[:len(t)-1]
+ w.text = t[:len(t)-1]
} else {
- w.Text = "No cards"
+ w.text = "No cards"
}
- b := text.BoundString(font.Font24, w.Text)
+ b := text.BoundString(font.Font24, w.text)
w.X = w.centerX - b.Dx()/2 - w.XMargin
w.Y = w.bottomY - b.Dy() - w.YMargin
}
@@ -159,3 +154,43 @@ func (w *PocList) FindObjectAt(x, y int) interface{} {
// i := b.Dy() / w.poc.Size()
return cards[0]
}
+
+type TextInput struct {
+ TextBox
+ input string
+ label string
+}
+
+func NewTextInput(x, y int, width, height int, label string) *TextInput {
+ w := &TextInput{*NewCenteringFixedTextBox(x, y, width, height, ""), "", 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) AddInput(input []rune) {
+ ti.input += string(input)
+ ti.ForceRedraw()
+}
+
+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
+}