-
Notifications
You must be signed in to change notification settings - Fork 18
/
properties.go
174 lines (149 loc) · 4.8 KB
/
properties.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
package tetra3d
// Properties is an unordered map of property names to values, representing a means of identifying Nodes or carrying data on Nodes.
type Properties map[string]*Property
// NewProperties returns a new Properties object.
func NewProperties() Properties {
return Properties{}
}
// Clone clones the Properties object.
func (props Properties) Clone() Properties {
newTags := NewProperties()
for k, v := range props {
newTags.Add(k).Set(v.Value)
}
return newTags
}
// Clear clears the Properties object of all game properties.
func (props Properties) Clear() {
for k := range props {
delete(props, k)
}
}
// CopyFrom copies the properties from another Properties object.
func (props Properties) CopyFrom(other Properties) {
props.Clear()
for key := range other {
props.Add(key).Set(other[key].Value)
}
}
// Remove removes the tag specified from the Properties object.
func (props Properties) Remove(tag string) {
delete(props, tag)
}
// Has returns true if the Properties object has properties by all of the names specified, and false otherwise.
func (props Properties) Has(propNames ...string) bool {
for _, propName := range propNames {
if props.Get(propName) == nil {
return false
}
}
return true
}
// Add adds a property to the Properties map using the given name.
// If a property of the specified name already exists, it will return that property instead.
func (props Properties) Add(propName string) *Property {
if _, ok := props[propName]; !ok {
props[propName] = &Property{}
}
return props[propName]
}
// Get returns the value associated with the specified property name. If a property with the
// passed name (propName) doesn't exist, Get will return nil.
func (props Properties) Get(propName string) *Property {
if _, ok := props[propName]; ok {
return props[propName]
}
return nil
}
// Set sets the given value to the property name, creating it if it doesn't exist.
func (props Properties) Set(propName string, value any) *Property {
prop := props.Add(propName)
prop.Set(value)
return prop
}
// Count returns the number of properties in the Properties set.
func (props Properties) Count() int {
return len(props)
}
// Property represents a game property on a Node or other resource.
type Property struct {
Value any
}
// Set sets the property's value to the given value.
func (prop *Property) Set(value any) {
prop.Value = value
}
// IsBool returns true if the Property is a boolean value.
func (prop *Property) IsBool() bool {
if _, ok := prop.Value.(bool); ok {
return true
}
return false
}
// AsBool returns the value associated with the Property as a bool.
// Note that this does not sanity check to ensure the Property is a bool first.
func (prop *Property) AsBool() bool {
return prop.Value.(bool)
}
// IsString returns true if the Property is a string.
func (prop *Property) IsString() bool {
if _, ok := prop.Value.(string); ok {
return true
}
return false
}
// AsString returns the value associated with the Property as a string.
// Note that this does not sanity check to ensure the Property is a string first.
func (prop *Property) AsString() string {
return prop.Value.(string)
}
// IsFloat returns true if the Property is a float64.
func (prop *Property) IsFloat64() bool {
if _, ok := prop.Value.(float64); ok {
return true
}
return false
}
// AsFloat returns the value associated with the Property as a float64.
// Note that this does not sanity check to ensure the Property is a float64 first.
func (prop *Property) AsFloat64() float64 {
return prop.Value.(float64)
}
// IsInt returns true if the Property is an int.
func (prop *Property) IsInt() bool {
if _, ok := prop.Value.(int); ok {
return true
}
return false
}
// AsInt returns the value associated with the Property as a float.
// Note that this does not sanity check to ensure the Property is an int first.
func (prop *Property) AsInt() int {
return prop.Value.(int)
}
// IsColor returns true if the Property is a color.
func (prop *Property) IsColor() bool {
if _, ok := prop.Value.(Color); ok {
return true
}
return false
}
// AsColor returns the value associated with the Property as a Color clone.
// Note that this does not sanity check to ensure the Property is a Color first.
func (prop *Property) AsColor() Color {
return prop.Value.(Color)
}
// IsVector returns true if the Property is a vector.
func (prop *Property) IsVector() bool {
if _, ok := prop.Value.(Vector); ok {
return true
}
return false
}
// AsVector returns the value associated with the Property as a 3D position Vector clone.
// The axes are corrected to account for the difference between Blender's axis order and Tetra3D's (i.e.
// Blender's +X, +Y, +Z becomes Tetra3D's +X, +Z, +Y).
// Note that this does not sanity check to ensure the Property is a vector first.
func (prop *Property) AsVector() Vector {
return prop.Value.(Vector)
}