-
Notifications
You must be signed in to change notification settings - Fork 83
/
bindingSet.go
39 lines (32 loc) · 985 Bytes
/
bindingSet.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
package event
// Mapping stores a slice of event names and bindings
type Mapping struct {
eventNames []string
binds []Bindable
}
// BindingSet maps sets of bindings so that entitys can switch between sets of predefined EventMappings
type BindingSet map[string]Mapping
// Set makes a new EventMapping for BindingSet
func (b BindingSet) Set(setName string, mappingSets ...map[string]Bindable) BindingSet {
numMappings := 0
for _, m := range mappingSets {
numMappings += len(m)
}
bindings := make([]Bindable, numMappings)
events := make([]string, numMappings)
i := 0
for _, m := range mappingSets {
for k, v := range m {
bindings[i] = v
events[i] = k
i++
}
}
b[setName] = Mapping{eventNames: events, binds: bindings}
return b
}
// RebindMapping resets the entity controlling this cid to only have the bindings
// in the passed in event mapping
func (c CID) RebindMapping(mapping Mapping) {
c.UnbindAllAndRebind(mapping.binds, mapping.eventNames)
}