feat: initial game screen and input window defs

This commit is contained in:
Zhongheng Liu 2024-10-23 15:27:14 +03:00
commit 024eb0c2bb
Signed by: steven
GPG key ID: DC8F48E7B4C40905
6 changed files with 156 additions and 74 deletions

View file

@ -0,0 +1,19 @@
package helper
import (
"time"
. "github.com/gbin/goncurses"
)
func IncrementalPrint(scr *Window, text string, from_y int, from_x int, interval_millis int) {
for i:=0; i < len(text); i++ {
ch := string([]rune(text)[i])
_, mx := scr.MaxYX()
cy := i / mx + 2 + from_y
cx := i % mx + 2
scr.MovePrint(cy, cx, ch)
time.Sleep( time.Duration(1000 / len(text)) * time.Millisecond)
scr.Refresh()
}
}

View file

@ -1,13 +0,0 @@
package window_helper
import (
. "github.com/gbin/goncurses"
)
func CreateMenu(win *Window, menu_string []string) {
x, y := 2, 2
win.Clear()
win.Box(0, 0)
for i, str := range menu_string {
win.MovePrint(y+i, x, str)
}
}

49
utils/windows/windows.go Normal file
View file

@ -0,0 +1,49 @@
package windows
import (
. "github.com/gbin/goncurses"
)
const (
LEFT_PAD = 2
RIGHT_PAD = 2
)
func InputPrompt(
scr *Window,
title string,
prompt string,
input_length int,
) string {
height, length := 8, len(prompt)+input_length+LEFT_PAD+RIGHT_PAD
my, mx := scr.MaxYX()
w, err := NewWindow(
height,
length,
(my/2)-(height/2),
(mx/2)-(length/2),
)
if err != nil {
panic("Oh no sth went wrong in input!!")
}
w.Box(0, 0)
Echo(true)
w.MovePrint(0, 1, title)
w.MovePrint(2, 2, prompt)
w.Move(2, 2+len(prompt))
input, err := w.GetString(input_length) // character input box
if err != nil {
panic("Oh no sth went wrong in input 2!!")
}
w.MovePrintf(height-2, 2, "Press q to continue...")
w.Refresh()
Echo(false)
for {
ch := w.GetChar()
switch Key(ch) {
case 'q':
w.Erase()
w.Refresh()
w.Delete()
return input
}
}
}