/
bindings.go
98 lines (86 loc) · 2.09 KB
/
bindings.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
package main
import (
"github.com/runningwild/glop/gin"
"strings"
"fmt"
"os"
"encoding/json"
"io/ioutil"
)
// Opens the file named by path, reads it all, decodes it as json into target,
// then closes the file. Returns the first error found while doing this or nil.
func LoadJson(path string, target interface{}) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return err
}
err = json.Unmarshal(data, target)
return err
}
func SaveJson(path string, source interface{}) error {
data, err := json.Marshal(source)
if err != nil {
return err
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
_,err = f.Write(data)
return err
}
type KeyBinds map[string]string
type KeyMap map[string]gin.Key
func getKeysFromString(str string) []gin.KeyId {
parts := strings.Split(str, "+")
var kids []gin.KeyId
for _,part := range parts {
part = osSpecifyKey(part)
var kid gin.KeyId
switch {
case len(part) == 1: // Single character - should be ascii
kid = gin.KeyId(part[0])
case part == "ctrl":
kid = gin.EitherControl
case part == "shift":
kid = gin.EitherShift
case part == "alt":
kid = gin.EitherAlt
case part == "gui":
kid = gin.EitherGui
default:
key := gin.In().GetKeyByName(part)
if key == nil {
panic(fmt.Sprintf("Unknown key '%s'", part))
}
kid = key.Id()
}
kids = append(kids, kid)
}
return kids
}
func (kb KeyBinds) MakeKeyMap() KeyMap {
key_map := make(KeyMap)
for key,val := range kb {
kids := getKeysFromString(val)
if len(kids) == 1 {
key_map[key] = gin.In().GetKey(kids[0])
} else {
// The last kid is the main kid and the rest are modifiers
main := kids[len(kids) - 1]
kids = kids[0 : len(kids) - 1]
var down []bool
for _ = range kids {
down = append(down, true)
}
key_map[key] = gin.In().BindDerivedKey(key, gin.In().MakeBinding(main, kids, down))
}
}
return key_map
}