-
Notifications
You must be signed in to change notification settings - Fork 0
/
nameform.go
72 lines (58 loc) · 1.54 KB
/
nameform.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
package main
import (
"log"
gc "github.com/rthornton128/goncurses"
)
const defaultPlayerName = "Anon"
const playerNameWindowTitle = "Player name"
const playerNameWindowHeight = 10
const playerNameWindowWidth = 50
// GetPlayerName create and show the window with player name input form
func GetPlayerName(s *gc.Window) string {
lines, cols := s.MaxYX()
height, width := playerNameWindowHeight, playerNameWindowWidth
wnd, windowCreateError := createWindow(
height,
width,
(lines/2)-height/2,
(cols/2)-width/2)
// we need to enable echo and cursor to be able to input something in the terminal
gc.Echo(true)
gc.Cursor(1)
defer gc.Echo(false)
defer gc.Cursor(0)
//
if windowCreateError != nil {
log.Println("Error creating player name input form window: ", windowCreateError)
return defaultPlayerName
}
wnd.Box(0, 0)
wnd.ColorOn(1)
wnd.MovePrint(
1,
(width/2)-(len(playerNameWindowTitle)/2),
playerNameWindowTitle)
wnd.ColorOff(1)
wnd.MoveAddChar(2, 0, gc.ACS_LTEE)
wnd.HLine(2, 1, gc.ACS_HLINE, width-2)
wnd.MoveAddChar(2, width-1, gc.ACS_RTEE)
wnd.Refresh()
log.Println("High score window created")
playerName := promptPlayerName(wnd)
log.Println("player name is: ", playerName)
removeWindow(wnd)
return playerName
}
func promptPlayerName(w *gc.Window) string {
msg := "Enter your name: "
row, col := w.MaxYX()
row, col = (row/2)-1, 4
w.MovePrint(row, col, msg)
str, err := w.GetString(12)
if err != nil {
log.Panic("Error getting player name string: ", err)
return defaultPlayerName
}
w.Refresh()
return str
}