-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenus.go
executable file
·125 lines (113 loc) · 2.04 KB
/
menus.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
package main
import (
"fmt"
"sync"
)
type menu struct {
initfunc func()
handler func(uint32) bool
}
var menus = map[uint32]menu{}
var menusmutex sync.Mutex
// key: the key to open the menu
// handler: the function executed on input.
//
// return "true" if the menu is still active/open
// return "false" if the menu has been closed
//
// initfunc: called every time the mneu is opened
//
// used to set initial state
func NewMenu(key uint32, handler func(uint32) bool, initfunc func()) {
menusmutex.Lock()
defer menusmutex.Unlock()
_, exists := menus[key]
if exists {
panic(fmt.Sprintf("Key already in use: %d", key))
}
if handler == nil {
panic(fmt.Sprintf("menu with key %d has no handler declared", key))
}
menus[key] = menu{initfunc: initfunc, handler: handler}
}
var activemenu uint32
// returns wether we handled it (true), or if it should be passed on to the game (false)
// todo:
//
// ptr to special keys pressed?
// we would need that for key combos like shift+f7
func Menuhandler(c uint32, pinputting *bool) bool {
//log.Println("rune:", c)
// we are not in our menu but we maybe want to be
if !*pinputting {
menu, exists := menus[c]
if !exists {
// we are not in any menu and we didnt activate any ether
return false //returning false is only ever relevant here
}
// the menu exists and we just opened it
activemenu = c
// and if there is some initial state to init, do it now
if menu.initfunc != nil {
menu.initfunc()
}
// we are now inputting
*pinputting = true
}
menu, exists := menus[activemenu]
if !exists {
panic("should not happen")
}
*pinputting = menu.handler(c)
return true //we have handled the input
}
// Same as enum in C, keep in sync!
const (
Backspace uint32 = iota + 1000
Tab
Enter
Shift
ShiftL
ShiftR
Ctrl
CtrlL
CtrlR
Alt
AltL
AltR
Pause
Capslock
Esc
PageUp
PageDown
End
Home
ArrLeft
ArrUp
ArrRight
ArrDown
Insert
Delete
Numpad0
Numpad1
Numpad2
Numpad3
Numpad4
Numpad5
Numpad6
Numpad7
Numpad8
Numpad9
F1
F2
F3
F4
F5
F6
F7
F8
F9
F10
F11
F12
)