-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameComponent.cs
212 lines (178 loc) · 7.04 KB
/
GameComponent.cs
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using Caravel.Core.Entity;
using System;
using System.Collections.Generic;
using System.Xml;
namespace CaravelEditor
{
class GameComponent : Cv_EntityComponent
{
public override Cv_ComponentID ID
{
get
{
if (m_ID == Cv_ComponentID.INVALID_COMPONENT)
{
m_ID = GetID(ComponentName);
}
return m_ID;
}
}
public string ComponentName;
private Dictionary<string, Tuple<string, List<Tuple<string, string>>>> m_FieldValues;
private Cv_ComponentID m_ID = Cv_ComponentID.INVALID_COMPONENT;
public void Init(string compName)
{
ComponentName = compName;
m_FieldValues = new Dictionary<string, Tuple<string, List<Tuple<string, string>>>>();
XmlNode compInfo;
if (!EntityComponentEditor.ComponentsByName.TryGetValue(compName, out compInfo))
{
return;
}
var componentElements = compInfo.ChildNodes;
foreach (XmlElement element in componentElements)
{
var name = element.Attributes["name"].Value;
var type = element.Attributes["type"].Value;
var fieldNames = new string[] { };
if (element.Attributes["fieldNames"] != null)
{
fieldNames = element.Attributes["fieldNames"].Value.Split(',');
}
var comboBoxOptions = new string[] { };
if (element.Attributes["options"] != null)
{
fieldNames = element.Attributes["options"].Value.Split(',');
}
var fieldList = new List<Tuple<string, string>>();
if (type == "rgba")
{
var r = new Tuple<string, string>("r", "255");
fieldList.Add(r);
var g = new Tuple<string, string>("g", "255");
fieldList.Add(g);
var b = new Tuple<string, string>("b", "255");
fieldList.Add(b);
var a = new Tuple<string, string>("a", "255");
fieldList.Add(a);
}
else
{
foreach (var field in fieldNames)
{
Tuple<string, string> fieldItem = null;
if (type == "float")
{
fieldItem = new Tuple<string, string>(field, "0.0");
}
else if (type == "int")
{
fieldItem = new Tuple<string, string>(field, "0");
}
else if (type == "boolean")
{
fieldItem = new Tuple<string, string>(field, "False");
}
else if (type == "file" || type == "string")
{
fieldItem = new Tuple<string, string>(field, "");
}
else if (type == "comboBox")
{
fieldItem = new Tuple<string, string>(field, comboBoxOptions[0]);
}
else
{
fieldItem = new Tuple<string, string>(field, "");
}
fieldList.Add(fieldItem);
}
}
var compElementInfo = new Tuple<string, List<Tuple<string, string>>>(type, fieldList);
m_FieldValues[name] = compElementInfo;
}
}
public override bool VInitialize(XmlElement componentData)
{
XmlNode compInfo;
if (!EntityComponentEditor.ComponentsByName.TryGetValue(componentData.Name, out compInfo))
{
return false;
}
var componentElements = compInfo.ChildNodes;
foreach (XmlElement element in componentElements)
{
var name = element.Attributes["name"].Value;
var type = element.Attributes["type"].Value;
var fieldNames = new string[] { };
var initializationNode = componentData.SelectSingleNode(name);
if (initializationNode == null)
{
continue;
}
if (element.Attributes["fieldNames"] != null)
{
fieldNames = element.Attributes["fieldNames"].Value.Split(',');
}
var fieldList = new List<Tuple<string, string>>();
if (type == "rgba")
{
var r = new Tuple<string, string>("r", initializationNode.Attributes["r"].Value);
fieldList.Add(r);
var g = new Tuple<string, string>("g", initializationNode.Attributes["g"].Value);
fieldList.Add(g);
var b = new Tuple<string, string>("b", initializationNode.Attributes["b"].Value);
fieldList.Add(b);
var a = new Tuple<string, string>("a", "255");
if (initializationNode.Attributes["a"] != null)
{
a = new Tuple<string, string>("a", initializationNode.Attributes["a"].Value);
}
fieldList.Add(a);
}
else
{
foreach (var field in fieldNames)
{
var fieldItem = new Tuple<string, string>(field, initializationNode.Attributes[field].Value);
fieldList.Add(fieldItem);
}
}
var compElementInfo = new Tuple<string, List<Tuple<string, string>>>(type, fieldList);
m_FieldValues[name] = compElementInfo;
}
return true;
}
public override void VOnChanged()
{
}
public override void VOnDestroy()
{
}
public override bool VPostInitialize()
{
return true;
}
public override void VPostLoad()
{
}
public override XmlElement VToXML()
{
XmlDocument doc = new XmlDocument();
XmlElement baseElement = doc.CreateElement(ComponentName);
foreach (var element in m_FieldValues)
{
XmlElement elementNode = doc.CreateElement(element.Key);
foreach (var field in element.Value.Item2)
{
elementNode.SetAttribute(field.Item1, field.Item2);
}
baseElement.AppendChild(elementNode);
}
return baseElement;
}
protected override void VOnUpdate(float elapsedTime)
{
}
}
}