-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathelement.go
98 lines (88 loc) · 2.08 KB
/
element.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 ofbx
import "strconv"
// Element stores a set of properties and a set of children
type Element struct {
ID *DataView
Children []*Element
Properties []*Property
}
func (e *Element) getProperty(idx int) *Property {
if len(e.Properties) <= idx {
return nil
}
return e.Properties[idx]
}
func (e *Element) String() string {
return e.stringPrefix("")
}
func (e *Element) stringPrefix(prefix string) string {
s := prefix + "Element: "
if e.ID != nil {
s += e.ID.String()
}
if len(e.Properties) > 1 {
if fmter, ok := propFormats[e.Properties[1].String()]; ok {
s += " " + fmter(e.Properties)
} else {
for idx, p := range e.Properties {
v := p.stringPrefix("\t" + prefix + "prop" + strconv.Itoa(idx) + "=")
if v == "" {
continue
}
s += "\n" + v
}
}
} else if len(e.Properties) == 1 {
s += e.Properties[0].stringPrefix(", prop=")
}
if len(e.Children) != 0 {
s += "\n"
//s += "\n" + prefix + "children: " + "\n"
for _, c := range e.Children {
s += c.stringPrefix(prefix + "\t")
}
return s
}
return s + "\n"
}
type propertyFormat func([]*Property) string
func numberPropFormat(props []*Property) string {
if len(props) != 5 {
return "bad number"
}
return props[0].String() + "=" + props[4].String()
}
func colorPropFormat(props []*Property) string {
if len(props) != 7 {
return "bad color"
}
s := props[0].String()
s += ":"
s += " R=" + props[4].String()
s += " G=" + props[5].String()
s += " B=" + props[6].String()
return s
}
func vectorPropFormat(props []*Property) string {
if len(props) != 7 {
return "bad vector"
}
s := props[0].String()
s += ":"
s += " X=" + props[4].String()
s += " Y=" + props[5].String()
s += " Z=" + props[6].String()
return s
}
var (
propFormats = map[string]propertyFormat{
"Number": numberPropFormat,
"int": numberPropFormat,
"enum": numberPropFormat,
"KTime": numberPropFormat,
"Color": colorPropFormat,
"Lcl Scaling": vectorPropFormat,
"Lcl Translation": vectorPropFormat,
"Lcl Rotation": vectorPropFormat,
}
)