-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
232 lines (201 loc) · 6.17 KB
/
config.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package render
import (
"os"
"encoding/json"
// "fmt"
glm "github.com/Jragonmiris/mathgl"
glfw "github.com/go-gl/glfw3"
"reflect"
"regexp"
"strconv"
)
type keyEvent struct {
Key glfw.Key
Action glfw.Action
}
type mouseButtonEvent struct {
Button glfw.MouseButton
Action glfw.Action
}
type Action func()
type MouseMoveAction func(position, delta glm.Vec2d)
type ScrollAction func(xoff, yoff float64)
type ControlBindings struct {
keyBindings map[keyEvent]Action
mouseButtonBindings map[mouseButtonEvent]Action
mouseMovementBinding MouseMoveAction
scrollBinding ScrollAction
lastMousePosition glm.Vec2d
hasLastMousePosition bool
}
func (c *ControlBindings) ResetBindings() {
c.keyBindings = map[keyEvent]Action{}
c.mouseButtonBindings = map[mouseButtonEvent]Action{}
c.mouseMovementBinding = nil
c.scrollBinding = nil
}
func (c *ControlBindings) BindKeyPress(key glfw.Key, press Action, release Action) {
if press != nil {
c.keyBindings[keyEvent{key, glfw.Press}] = press
}
if release != nil {
c.keyBindings[keyEvent{key, glfw.Release}] = release
}
}
func (c *ControlBindings) BindMouseClick(button glfw.MouseButton, press Action, release Action) {
if press != nil {
c.mouseButtonBindings[mouseButtonEvent{button, glfw.Press}] = press
}
if release != nil {
c.mouseButtonBindings[mouseButtonEvent{button, glfw.Release}] = release
}
}
func (c *ControlBindings) BindScroll(action ScrollAction) {
c.scrollBinding = action
}
func (c *ControlBindings) UnbindScroll() {
c.scrollBinding = nil
}
func (c *ControlBindings) UnbindKeyPress(key glfw.Key) {
delete(c.keyBindings, keyEvent{key, glfw.Press})
delete(c.keyBindings, keyEvent{key, glfw.Release})
}
func (c *ControlBindings) UnbindMouseClick(button glfw.MouseButton) {
delete(c.mouseButtonBindings, mouseButtonEvent{button, glfw.Press})
delete(c.mouseButtonBindings, mouseButtonEvent{button, glfw.Release})
}
func (c *ControlBindings) BindMouseMovement(action MouseMoveAction) {
c.mouseMovementBinding = action
}
func (c *ControlBindings) UnbindMouseMovement() {
c.mouseMovementBinding = nil
}
func (c *ControlBindings) DoKeyAction(key glfw.Key, keyAction glfw.Action) {
boundAction, ok := c.FindKeyAction(key, keyAction)
if ok {
boundAction()
}
}
func (c *ControlBindings) DoMouseButtonAction(button glfw.MouseButton, mouseAction glfw.Action) {
boundAction, ok := c.FindClickAction(button, mouseAction)
if ok {
boundAction()
}
}
func MouseCoord(window *glfw.Window, xpos, ypos float64) glm.Vec2d {
width, height := window.GetSize()
return glm.Vec2d{xpos / float64(width), 1 - ypos / float64(height)}
}
func (c *ControlBindings) DoMouseMoveAction(window *glfw.Window, xpos, ypos float64) {
pos := MouseCoord(window, xpos, ypos)
boundAction, ok := c.FindMouseMovementAction()
if ok {
delta := pos.Sub(c.lastMousePosition)
if !c.hasLastMousePosition {
c.hasLastMousePosition = true
delta = glm.Vec2d{}
}
boundAction(pos, delta)
}
c.lastMousePosition = pos
}
func (c *ControlBindings) DoScrollAction(xoff, yoff float64) {
boundAction, ok := c.FindScrollAction()
if ok {
boundAction(xoff, yoff)
}
}
func (c *ControlBindings) FindKeyAction(key glfw.Key, keyAction glfw.Action) (Action, bool) {
action, ok := c.keyBindings[keyEvent{key, keyAction}]
return action, ok
}
func (c *ControlBindings) FindClickAction(button glfw.MouseButton, buttonAction glfw.Action) (Action, bool) {
action, ok := c.mouseButtonBindings[mouseButtonEvent{button, buttonAction}]
return action, ok
}
func (c *ControlBindings) FindMouseMovementAction() (MouseMoveAction, bool) {
return c.mouseMovementBinding, c.mouseMovementBinding != nil
}
func (c *ControlBindings) FindScrollAction() (ScrollAction, bool) {
return c.scrollBinding, c.scrollBinding != nil
}
func FindActionMethod(v reflect.Value, name string) Action {
m := v.MethodByName(name)
if m.IsValid() {
return Action(m.Interface().(func()))
} else {
return nil
}
}
func FindMouseMoveActionMethod(v reflect.Value, name string) MouseMoveAction {
m := v.MethodByName(name)
if m.IsValid() {
return MouseMoveAction(m.Interface().(func(glm.Vec2d, glm.Vec2d)))
} else {
return nil
}
}
func (c *ControlBindings) Apply(receiver interface{}, bindings map[string]string) {
mb, err := regexp.Compile("^mouse([1-9])$")
if err != nil {
panic(err)
}
receiverValue := reflect.ValueOf(receiver)
for k, name := range bindings {
stopName := "Stop" + name
if len(k) == 1 {
key := glfw.Key(k[0])
if name == "" {
c.UnbindKeyPress(key)
} else {
startAction := FindActionMethod(receiverValue, name)
stopAction := FindActionMethod(receiverValue, stopName)
c.BindKeyPress(key, startAction, stopAction)
}
} else if mb.MatchString(k) {
i, err := strconv.Atoi(mb.FindStringSubmatch(k)[1])
if err == nil {
button := glfw.MouseButton1 + glfw.MouseButton(i-1)
if name == "" {
c.UnbindMouseClick(button)
} else {
// fmt.Println("bind", i, button, glfw.MouseButton1)
startAction := FindActionMethod(receiverValue, name)
stopAction := FindActionMethod(receiverValue, stopName)
c.BindMouseClick(button, startAction, stopAction)
}
}
} else if k == "mousemove" {
if name == "" {
c.UnbindMouseMovement()
} else {
moveAction := FindMouseMoveActionMethod(receiverValue, name)
c.BindMouseMovement(moveAction)
}
}
}
}
func LoadConfiguration(confFile string, constants interface{}, bindings *ControlBindings, receiver interface{}) error {
file, err := os.Open(confFile)
if err == nil {
defer file.Close()
decoder := json.NewDecoder(file)
root := map[string]interface{}{}
err = decoder.Decode(&root)
if err != nil { return err }
if constants, ok := root["constants"]; ok {
bytes, err := json.Marshal(constants)
if err != nil { return err }
err = json.Unmarshal(bytes, &constants)
if err != nil { return err }
}
if controls, ok := root["controls"]; ok {
sc := make(map[string]string)
for k, v := range controls.(map[string]interface{}) {
sc[k] = v.(string)
}
bindings.Apply(receiver, sc)
}
}
return err
}