aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhongheng Liu <z.liu@outlook.com.gr>2024-10-21 09:07:20 +0300
committerZhongheng Liu <z.liu@outlook.com.gr>2024-10-21 09:07:20 +0300
commitc71be875a584e97e3b79c0d00a162d0fd2224e2c (patch)
treec78425f98c71b5c066b5b5e3f3ae6feac7af8b1a
parenta83b6a66ae62c9100ec57957e7e1505d6f4480dc (diff)
downloadrulmarc-c71be875a584e97e3b79c0d00a162d0fd2224e2c.tar.gz
rulmarc-c71be875a584e97e3b79c0d00a162d0fd2224e2c.tar.bz2
rulmarc-c71be875a584e97e3b79c0d00a162d0fd2224e2c.zip
feat: make menu creation into module
-rw-r--r--main.go83
-rw-r--r--menu/menu.go85
2 files changed, 88 insertions, 80 deletions
diff --git a/main.go b/main.go
index 84a20ed..4629213 100644
--- a/main.go
+++ b/main.go
@@ -1,87 +1,10 @@
package main
import (
- . "github.com/gbin/goncurses"
- "log"
-)
-
-const (
- HEIGHT = 10
- WIDTH = 30
+ "gitlab.com/stvnliu/ai_game/menu"
)
func main() {
- var active int
- menu := []string{"Choice 1", "Choice 2", "Choice 3", "Choice 4", "Exit"}
-
- stdscr, err := Init()
- if err != nil {
- log.Fatal(err)
- }
- defer End()
-
- Raw(true)
- Echo(false)
- Cursor(0)
- stdscr.Clear()
- stdscr.Keypad(true)
-
- my, mx := stdscr.MaxYX()
- y, x := 2, (mx/2)-(WIDTH/2)
-
- win, _ := NewWindow(HEIGHT, WIDTH, y, x)
- win.Keypad(true)
-
- stdscr.Print("Use arrow keys to go up and down, Press enter to select")
- stdscr.Refresh()
-
- printmenu(win, menu, active)
-
- for {
- ch := stdscr.GetChar()
- switch Key(ch) {
- case 'q':
- return
- case KEY_UP:
- if active == 0 {
- active = len(menu) - 1
- } else {
- active -= 1
- }
- case KEY_DOWN:
- if active == len(menu)-1 {
- active = 0
- } else {
- active += 1
- }
- case KEY_RETURN, KEY_ENTER, Key('\r'):
- stdscr.MovePrintf(my-2, 0, "Choice #%d: %s selected",
- active,
- menu[active])
- stdscr.ClearToEOL()
- stdscr.Refresh()
- default:
- stdscr.MovePrintf(my-2, 0, "Character pressed = %3d/%c",
- ch, ch)
- stdscr.ClearToEOL()
- stdscr.Refresh()
- }
-
- printmenu(win, menu, active)
- }
-}
-
-func printmenu(w *Window, menu []string, active int) {
- y, x := 2, 2
- w.Box(0, 0)
- for i, s := range menu {
- if i == active {
- w.AttrOn(A_REVERSE)
- w.MovePrint(y+i, x, s)
- w.AttrOff(A_REVERSE)
- } else {
- w.MovePrint(y+i, x, s)
- }
- }
- w.Refresh()
+ menu_items := []string{"New game", "Continue", "Exit"}
+ menu.CreateMenu(menu_items)
}
diff --git a/menu/menu.go b/menu/menu.go
new file mode 100644
index 0000000..51bb89d
--- /dev/null
+++ b/menu/menu.go
@@ -0,0 +1,85 @@
+package menu
+
+import (
+ . "github.com/gbin/goncurses"
+ "log"
+)
+
+const (
+ HEIGHT = 10
+ WIDTH = 30
+)
+
+func CreateMenu(menu []string) {
+ var active int
+ stdscr, err := Init()
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer End()
+
+ Raw(true)
+ Echo(false)
+ Cursor(0)
+ stdscr.Clear()
+ stdscr.Keypad(true)
+
+ my, mx := stdscr.MaxYX()
+ y, x := 2, (mx/2)-(WIDTH/2)
+
+ win, _ := NewWindow(HEIGHT, WIDTH, y, x)
+ win.Keypad(true)
+
+ stdscr.Print("Use arrow keys to go up and down, Press enter to select")
+ stdscr.Refresh()
+
+ printmenu(win, menu, active)
+
+ for {
+ ch := stdscr.GetChar()
+ switch Key(ch) {
+ case 'q':
+ return
+ case KEY_UP:
+ if active == 0 {
+ active = len(menu) - 1
+ } else {
+ active -= 1
+ }
+ case KEY_DOWN:
+ if active == len(menu)-1 {
+ active = 0
+ } else {
+ active += 1
+ }
+ case KEY_RETURN, KEY_ENTER, Key('\r'):
+ stdscr.MovePrintf(my-2, 0, "Choice #%d: %s selected",
+ active,
+ menu[active])
+ stdscr.ClearToEOL()
+ stdscr.Refresh()
+ default:
+ stdscr.MovePrintf(my-2, 0, "Character pressed = %3d/%c",
+ ch, ch)
+ stdscr.ClearToEOL()
+ stdscr.Refresh()
+ }
+
+ printmenu(win, menu, active)
+ }
+}
+
+func printmenu(w *Window, menu []string, active int) {
+ y, x := 2, 2
+ w.Box(0, 0)
+ for i, s := range menu {
+ if i == active {
+ w.AttrOn(A_REVERSE)
+ w.MovePrint(y+i, x, s)
+ w.AttrOff(A_REVERSE)
+ } else {
+ w.MovePrint(y+i, x, s)
+ }
+ }
+ w.Refresh()
+}