aboutsummaryrefslogtreecommitdiff
path: root/go/client/startMenu.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/client/startMenu.go
parente8f44219976edf8cf57cbd9e964f66fe9d145a9c (diff)
downloadmuhqs-game-8802f30ff720fcb64d9207c4cb673dc6805390c2.tar.gz
muhqs-game-8802f30ff720fcb64d9207c4cb673dc6805390c2.zip
add text input and properly center text
Diffstat (limited to 'go/client/startMenu.go')
-rw-r--r--go/client/startMenu.go71
1 files changed, 61 insertions, 10 deletions
diff --git a/go/client/startMenu.go b/go/client/startMenu.go
index 735753b0..9d5d6129 100644
--- a/go/client/startMenu.go
+++ b/go/client/startMenu.go
@@ -1,6 +1,7 @@
package main
import (
+ "image/color"
"log"
"github.com/hajimehoshi/ebiten/v2"
@@ -32,13 +33,38 @@ func NewStartMenu(app *app) *StartMenu {
}
func (m *StartMenu) build() {
+ playerLabel := ui.NewCenteringFixedTextBox(
+ (m.width-START_BUTTON_WIDTH)/2-START_BUTTON_WIDTH,
+ (m.height-START_BUTTON_HEIGHT)/2-START_BUTTON_HEIGHT,
+ START_BUTTON_WIDTH,
+ START_BUTTON_HEIGHT,
+ "name:",
+ )
+ playerLabel.Bg = color.Black
+ m.widgets = append(m.widgets, playerLabel)
+
+ playerInputLabel := "player name"
+ if m.playerName != "" {
+ playerInputLabel = m.playerName
+ }
+
+ playerInput := ui.NewTextInput(
+ (m.width-START_BUTTON_WIDTH)/2,
+ (m.height-START_BUTTON_HEIGHT)/2-START_BUTTON_HEIGHT,
+ START_BUTTON_WIDTH,
+ START_BUTTON_HEIGHT,
+ playerInputLabel,
+ )
+ m.widgets = append(m.widgets, playerInput)
+
m.widgets = append(m.widgets, ui.NewSimpleButton(
(m.width-START_BUTTON_WIDTH)/2,
(m.height-START_BUTTON_HEIGHT)/2,
- 150,
- 50,
+ START_BUTTON_WIDTH,
+ START_BUTTON_HEIGHT,
"Start",
func(*ui.SimpleButton) {
+ m.playerName = playerInput.Text()
m.app.active = m.next()
}))
}
@@ -49,15 +75,40 @@ func (m *StartMenu) Update() error {
}
x, y := ebiten.CursorPosition()
+
+ var activeWidget ui.Widget
+ for _, w := range m.widgets {
+ if !w.Contains(x, y) {
+ continue
+ }
+ activeWidget = w
+ break
+ }
+
+ if activeWidget == nil {
+ return nil
+ }
+
+ scrollX, scrollY := ebiten.Wheel()
+ if scrollX != 0 || scrollY != 0 {
+ if scrollable, ok := activeWidget.(ui.Scrollable); ok {
+ scrollable.Scroll(int(scrollX), int(scrollY))
+ }
+ }
+
+ if textInput, ok := activeWidget.(*ui.TextInput); ok {
+ // var input []rune
+ // ebiten.AppendInputChars(input)
+ input := ebiten.InputChars()
+ if len(input) > 0 {
+ textInput.AddInput(input)
+ }
+ textInput.HandleKey()
+ }
+
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
- for _, w := range m.widgets {
- if !w.Contains(x, y) {
- continue
- }
-
- if btn, ok := w.(ui.Button); ok {
- btn.Click(x, y)
- }
+ if btn, ok := activeWidget.(ui.Button); ok {
+ btn.Click(x, y)
}
}