From 8802f30ff720fcb64d9207c4cb673dc6805390c2 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Fri, 3 Feb 2023 14:05:14 +0100 Subject: add text input and properly center text --- go/client/startMenu.go | 71 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 10 deletions(-) (limited to 'go/client') 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) } } -- cgit v1.2.3