-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scene.cs
214 lines (197 loc) · 7.38 KB
/
Scene.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
213
214
using Mogre;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace MOgreEditor
{
[Serializable]
public class SaveSceneNode
{
public string name;
public string meshName;
public Vector3 position;
public Quaternion orientation;
public SaveSceneNode() { }
}
[Serializable]
public class SaveScene
{
public string cameraName;
public Vector3 cameraPosition;
public Vector3 cameraLookAt;
public Quaternion cameraOrientation;
public List<SaveSceneNode> nodes = new List<SaveSceneNode>();
public SaveScene()
{
}
public static bool ReadScene(string name, EditorScene editorScene)
{
try
{
SaveScene scene;
XmlSerializer xmlSerializer = new XmlSerializer(typeof(SaveScene));
using (FileStream fs = new FileStream(name, FileMode.OpenOrCreate))
{
scene = (SaveScene)xmlSerializer.Deserialize(fs);
}
if (scene != null)
{
editorScene.children.Clear();
editorScene.sceneManager.ClearScene();
editorScene.camera = new Camera(scene.cameraName, editorScene.sceneManager);
editorScene.camera.Position = scene.cameraPosition;
editorScene.camera.Orientation = scene.cameraOrientation;
editorScene.camera.Direction = scene.cameraLookAt;
editorScene.AddEditorSceneNode(scene.cameraName, "Camera");
foreach (SaveSceneNode saveSceneNode in scene.nodes)
{
EditorSceneNode editorSceneNode = new EditorSceneNode();
SceneNode sceneNode = editorScene.AddEditorSceneNode(saveSceneNode.name, saveSceneNode.meshName);
sceneNode.Position = saveSceneNode.position;
sceneNode.Orientation = saveSceneNode.orientation;
}
}
return true;
}
catch (Exception e)
{
return false;
}
}
public static void WriteScene(string name, EditorScene scene)
{
SaveScene saveScene;
try
{
saveScene = new SaveScene();
saveScene.cameraName = scene.camera.Name;
saveScene.cameraPosition = scene.camera.Position;
saveScene.cameraOrientation = scene.camera.Orientation;
saveScene.cameraLookAt = scene.camera.Direction;
foreach (EditorSceneNode node in scene.children)
{
SaveSceneNode saveSceneNode = new SaveSceneNode();
saveSceneNode.name = node.name;
saveSceneNode.meshName = node.meshName;
saveSceneNode.orientation = node.sceneNode.Orientation;
saveSceneNode.position = node.sceneNode.Position;
saveScene.nodes.Add(saveSceneNode);
}
XmlSerializer xmlSerializer = new XmlSerializer(typeof(SaveScene));
// получаем поток, куда будем записывать сериализованный объект
using (FileStream fs = new FileStream(name, FileMode.OpenOrCreate))
{
xmlSerializer.Serialize(fs, saveScene);
}
}
catch { }
}
}
public class EditorSceneNode
{
public Entity entity;
public string name;
public string meshName;
public SceneNode sceneNode;
public TreeNode treeNode;
public LinkedList<EditorSceneNode> children;
public EditorSceneNode()
{
children = null;
}
}
public class EditorScene
{
public LinkedList<EditorSceneNode> children;
public TreeView treeView1;
public TreeNode rootNode;
public SceneNode rootSceneNode;
public SceneManager sceneManager;
public Camera camera;
public EditorScene(TreeView treeView, SceneManager sceneManager)
{
treeView1 = treeView;
children = new LinkedList<EditorSceneNode>();
rootNode = new TreeNode("Root");
// Добавляем rootNode вместе с дочерними узлами в TreeView
treeView1.Nodes.Add(rootNode);
this.rootSceneNode = sceneManager.RootSceneNode;
this.sceneManager = sceneManager;
}
public void Dispose()
{
foreach(EditorSceneNode node in children)
{
node.entity.Dispose();
node.entity = null;
node.sceneNode.Dispose();
node.sceneNode = null;
node.treeNode.Remove();
node.treeNode = null;
}
children.Clear();
camera.Dispose();
camera = null;
}
public SceneNode GetEditorSceneNodeByName(string nodeName)
{
try
{
foreach (EditorSceneNode node in children)
{
if (node.name == nodeName)
{
return node.sceneNode;
}
}
}
catch { }
return null;
}
public SceneNode AddEditorSceneNode(string nodeName, string v)
{
try
{
if (v != "Camera")
{
Entity entity = sceneManager.CreateEntity(v);
SceneNode sceneNode = rootSceneNode.CreateChildSceneNode(nodeName);
sceneNode.AttachObject(entity);
EditorSceneNode editorSceneNode = new EditorSceneNode();
editorSceneNode.name = nodeName;
editorSceneNode.meshName = v;
editorSceneNode.entity = entity;
editorSceneNode.sceneNode = sceneNode;
editorSceneNode.treeNode = new TreeNode(nodeName);
this.children.AddLast(editorSceneNode);
rootNode.Nodes.Add(editorSceneNode.treeNode);
return sceneNode;
}
else
{
EditorSceneNode editorSceneNode = new EditorSceneNode();
editorSceneNode.name = nodeName;
editorSceneNode.meshName = "Camera";
editorSceneNode.entity = null;
this.camera = sceneManager.GetCamera(nodeName);
editorSceneNode.sceneNode = null;
editorSceneNode.treeNode = new TreeNode(nodeName);
// this.children.AddLast(editorSceneNode);
rootNode.Nodes.Add(editorSceneNode.treeNode);
return null;
}
}
catch (Exception ex)
{
return null;
}
}
}
}