1// tAttribute.cpp 
2// 
3// All scene objects may have attributes. Attributes are extra data (that has a type) that the content author 
4// attributed to the scene object. 
5// 
6// Copyright (c) 2006, 2017 Tristan Grimmer. 
7// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby 
8// granted, provided that the above copyright notice and this permission notice appear in all copies. 
9// 
10// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL 
11// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 
12// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 
13// AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 
14// PERFORMANCE OF THIS SOFTWARE. 
15 
16#include "Scene/tAttribute.h" 
17namespace tScene 
18
19 
20 
21void tAttribute::Save(tChunkWriter& chunk) const 
22
23 if (!IsValid()) 
24 return
25 
26 chunk.Begin(tChunkID::Scene_Attribute); 
27
28 chunk.Begin(tChunkID::Scene_AttributeName); 
29
30 chunk.Write(Name); 
31
32 chunk.End(); 
33 
34 chunk.Begin(tChunkID::Scene_AttributeType); 
35
36 chunk.Write(Type); 
37
38 chunk.End(); 
39 
40 // Values must be switched on type so that endianness corrections are made. 
41 chunk.Begin(tChunkID::Scene_AttributeValue); 
42
43 switch (Type
44
45 case tType::Bool
46 chunk.Write(GetAsBool()); 
47 break
48 
49 case tType::Int
50 chunk.Write(GetAsInt()); 
51 break
52 
53 case tType::Colour
54 chunk.Write(GetAsColour()); 
55 break
56 
57 case tType::Float
58 chunk.Write(GetAsFloat()); 
59 break
60 }; 
61
62 chunk.End(); 
63
64 chunk.End(); 
65
66 
67 
68void tAttribute::Load(const tChunk& attribChunk
69
70 tAssert(attribChunk.GetID() == tChunkID::Scene_Attribute); 
71 Clear(); 
72 
73 for (tChunk chunk = attribChunk.First(); chunk.IsValid(); chunk = chunk.Next()) 
74
75 switch (chunk.GetID()) 
76
77 case tChunkID::Scene_AttributeName
78 chunk.GetItem(Name); 
79 break
80 
81 case tChunkID::Scene_AttributeType
82 chunk.GetItem(Type); 
83 break
84 
85 case tChunkID::Scene_AttributeValue
86 tAssert(Type != tType::Invalid); 
87 
88 // We switch on the type here so that the tChunk system can handle any endian issues. 
89 switch (Type
90
91 case tType::Bool
92
93 bool val
94 chunk.GetItem(val); 
95 Value = val ? 1 : 0
96 break
97
98 
99 case tType::Int
100
101 int val
102 chunk.GetItem(val); 
103 Value = int(val); 
104 break
105
106 
107 case tType::Colour
108
109 tColouri val
110 chunk.GetItem(val); 
111 Value = *((uint32*)(&val)); 
112 break
113
114 
115 case tType::Float
116
117 float val
118 chunk.GetItem(val); 
119 Value = *((uint32*)(&val)); 
120 break
121
122
123 break
124
125
126
127 
128 
129bool tAttributes::Contains(const tAttribute& attrib) const 
130
131 for (tItList<tAttribute>::Iter attr = Attributes.First(); attr; ++attr
132
133 if (*attr == attrib
134 return true
135
136 
137 return false
138
139 
140 
141void tAttributes::Save(tChunkWriter& chunk) const 
142
143 chunk.Begin(tChunkID::Scene_AttributeList); 
144 for (tItList<tAttribute>::Iter attr = Attributes.First(); attr; ++attr
145 attr->Save(chunk); 
146 chunk.End(); 
147
148 
149 
150void tAttributes::Load(const tChunk& chunk
151
152 Clear(); 
153 tAssert(chunk.GetID() == tChunkID::Scene_AttributeList); 
154 for (tChunk attr = chunk.First(); attr.Valid(); attr = attr.Next()) 
155 Attributes.Append(new tAttribute(attr)); 
156
157 
158 
159bool tAttributes::operator==(const tAttributes& attribs) const 
160
161 if (Count() != attribs.Count()) 
162 return false
163 
164 for (tItList<tAttribute>::Iter attr = Attributes.First(); attr; ++attr
165
166 if (!attribs.Contains(*attr)) 
167 return false
168
169 
170 return true
171
172 
173 
174tAttributes& tAttributes::operator=(const tAttributes& attribs
175
176 if (this == &attribs
177 return *this
178 
179 Clear(); 
180 for (tItList<tAttribute>::Iter attr = attribs.Attributes.First(); attr; ++attr
181 Attributes.Append(new tAttribute(*attr)); 
182 
183 return *this
184
185 
186 
187
188