-
Notifications
You must be signed in to change notification settings - Fork 0
/
change_name_screen.go
86 lines (71 loc) · 2.08 KB
/
change_name_screen.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package client
import (
"encoding/json"
"github.com/Lama06/Oinky-Party/client/rescources"
"github.com/Lama06/Oinky-Party/client/ui"
"github.com/Lama06/Oinky-Party/protocol"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
type changeNameScreen struct {
client *client
newName string
newNameText *ui.Text
continueButton *ui.Button
}
var _ screen = (*changeNameScreen)(nil)
func newChangeNameScreen(client *client) *changeNameScreen {
screen := changeNameScreen{
client: client,
newName: client.name,
newNameText: ui.NewText(ui.TextConfig{
Pos: ui.DynamicPosition(func(width, height int) ui.Position {
return ui.CenteredPosition{X: width / 2, Y: height / 3}
}),
Text: client.name,
Colors: &ui.TitleColors,
Font: rescources.RobotoTitleFont,
}),
}
screen.continueButton = ui.NewButton(ui.ButtonConfig{
Pos: ui.DynamicPosition(func(width, height int) ui.Position {
return ui.CenteredPosition{X: width / 2, Y: (height / 3) * 2}
}),
Text: "Namen ändern",
Callback: func() {
changeName, err := json.Marshal(protocol.ChangeNamePacket{
PacketName: protocol.ChangeNamePacketName,
NewName: screen.newName,
})
if err != nil {
panic(err)
}
client.SendPacket(changeName)
client.name = screen.newName
client.currentScreen = newTitleScreen(client)
},
})
return &screen
}
func (c *changeNameScreen) components() []ui.Component {
return []ui.Component{c.newNameText, c.continueButton}
}
func (c *changeNameScreen) update() {
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
c.client.currentScreen = newTitleScreen(c.client)
}
c.newName = string(ebiten.AppendInputChars([]rune(c.newName)))
if len(c.newName) != 0 && inpututil.IsKeyJustPressed(ebiten.KeyBackspace) {
c.newName = c.newName[:len(c.newName)-1]
}
c.newNameText.Text = c.newName
for _, component := range c.components() {
component.Update()
}
}
func (c *changeNameScreen) draw(screen *ebiten.Image) {
screen.Fill(ui.BackgroundColor)
for _, component := range c.components() {
component.Draw(screen)
}
}