-
Notifications
You must be signed in to change notification settings - Fork 1
/
loginmenu.go
154 lines (140 loc) · 4.7 KB
/
loginmenu.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* loginmenu.go
*
* Copyright 2020-2024 Dariusz Sikora <ds@isangeles.dev>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
package mainmenu
import (
"github.com/gopxl/pixel"
"github.com/isangeles/flame/data/res/lang"
"github.com/isangeles/fire/request"
"github.com/isangeles/mtk"
)
// Struct for login menu.
type LoginMenu struct {
mainmenu *MainMenu
title *mtk.Text
loginLabel *mtk.Text
passLabel *mtk.Text
loginEdit *mtk.Textedit
passEdit *mtk.Textedit
loginButton *mtk.Button
backButton *mtk.Button
opened bool
}
// newLoginMenu creates new login menu.
func newLoginMenu(mainmenu *MainMenu) *LoginMenu {
lm := new(LoginMenu)
lm.mainmenu = mainmenu
// Labels.
labelParams := mtk.Params{
FontSize: mtk.SizeMedium,
}
lm.title = mtk.NewText(labelParams)
lm.title.SetText(lang.Text("login_menu_title"))
lm.loginLabel = mtk.NewText(labelParams)
lm.loginLabel.SetText(lang.Text("login_menu_login_label"))
lm.passLabel = mtk.NewText(labelParams)
lm.passLabel.SetText(lang.Text("login_menu_pass_label"))
// Text edit fields.
texteditParams := mtk.Params{
FontSize: mtk.SizeMedium,
MainColor: mainColor,
}
lm.loginEdit = mtk.NewTextedit(texteditParams)
lm.passEdit = mtk.NewTextedit(texteditParams)
// Buttons.
buttonParams := mtk.Params{
Size: mtk.SizeMedium,
FontSize: mtk.SizeMedium,
Shape: mtk.ShapeRectangle,
MainColor: accentColor,
}
lm.loginButton = mtk.NewButton(buttonParams)
lm.loginButton.SetLabel(lang.Text("login_menu_login_button_label"))
lm.loginButton.SetOnClickFunc(lm.onLoginButtonClicked)
lm.backButton = mtk.NewButton(buttonParams)
lm.backButton.SetLabel(lang.Text("back_button_label"))
lm.backButton.SetOnClickFunc(lm.onBackButtonClicked)
return lm
}
// Draw draws login menu in specified window.
func (lm *LoginMenu) Draw(win *mtk.Window) {
// Title.
titlePos := pixel.V(win.Bounds().Center().X,
win.Bounds().H()-lm.title.Size().Y)
lm.title.Draw(win, mtk.Matrix().Moved(titlePos))
// Login field.
loginLabelPos := mtk.BottomOf(lm.title.DrawArea(), lm.loginEdit.Size(), 100)
lm.loginLabel.Draw(win, mtk.Matrix().Moved(loginLabelPos))
loginEditPos := mtk.BottomOf(lm.loginLabel.DrawArea(), lm.loginEdit.Size(), 10)
loginEditSize := lm.title.DrawArea().Size()
lm.loginEdit.SetSize(loginEditSize)
lm.loginEdit.Draw(win.Window, mtk.Matrix().Moved(loginEditPos))
// Password field.
passLabelPos := mtk.BottomOf(lm.loginEdit.DrawArea(), lm.passEdit.Size(), 10)
lm.passLabel.Draw(win, mtk.Matrix().Moved(passLabelPos))
passEditPos := mtk.BottomOf(lm.passLabel.DrawArea(), lm.passEdit.Size(), 10)
passEditSize := lm.title.DrawArea().Size()
lm.passEdit.SetSize(passEditSize)
lm.passEdit.Draw(win.Window, mtk.Matrix().Moved(passEditPos))
// Buttons.
loginButtonPos := mtk.BottomOf(lm.passEdit.DrawArea(), lm.loginButton.Size(), 10)
lm.loginButton.Draw(win.Window, mtk.Matrix().Moved(loginButtonPos))
backButtonPos := mtk.DrawPosBL(win.Bounds(), lm.backButton.Size())
lm.backButton.Draw(win.Window, mtk.Matrix().Moved(backButtonPos))
}
// Update updates all menu elements.
func (lm *LoginMenu) Update(win *mtk.Window) {
if lm.mainmenu.server != nil && lm.mainmenu.server.Authorized() {
lm.mainmenu.ShowMessage(lang.Text("login_logged_in_msg"))
lm.mainmenu.OpenMenu()
return
}
lm.loginEdit.Update(win)
lm.passEdit.Update(win)
lm.loginButton.Update(win)
lm.backButton.Update(win)
}
// Show shows menu.
func (lm *LoginMenu) Show() {
lm.opened = true
}
// Hide hides menu.
func (lm *LoginMenu) Hide() {
lm.opened = false
}
// Opened checks if menu is open.
func (lm *LoginMenu) Opened() bool {
return lm.opened
}
// Triggered on login button click.
func (lm *LoginMenu) onLoginButtonClicked(b *mtk.Button) {
loginReq := request.Login{lm.loginEdit.Text(), lm.passEdit.Text()}
req := request.Request{Login: []request.Login{loginReq}}
err := lm.mainmenu.server.Send(req)
if err != nil {
lm.mainmenu.ShowMessage(lang.Text("login_menu_create_login_req_err"))
}
}
// Triggered on back button click.
func (lm *LoginMenu) onBackButtonClicked(b *mtk.Button) {
lm.mainmenu.OpenMenu()
}