source: OniSplit/Objects/ObjectBase.cs@ 1147

Last change on this file since 1147 was 1114, checked in by iritscen, 5 years ago

Adding OniSplit source code (v0.9.99.0). Many thanks to Neo for all his work over the years.

File size: 3.3 KB
RevLine 
[1114]1using System;
2using System.Xml;
3using Oni.Xml;
4
5namespace Oni.Objects
6{
7 internal abstract class ObjectBase
8 {
9 private ObjectType typeId;
10 private int objectId;
11 private ObjectFlags objectFlags;
12 private Vector3 position;
13 private Vector3 rotation;
14 private Matrix? transform;
15
16 public ObjectType TypeId
17 {
18 get { return typeId; }
19 protected set { typeId = value; }
20 }
21
22 public int ObjectId
23 {
24 get { return objectId; }
25 set { objectId = value; }
26 }
27
28 public Matrix Transform
29 {
30 get
31 {
32 if (transform == null)
33 {
34 transform = Matrix.CreateRotationX(MathHelper.ToRadians(rotation.X))
35 * Matrix.CreateRotationY(MathHelper.ToRadians(rotation.Y))
36 * Matrix.CreateRotationZ(MathHelper.ToRadians(rotation.Z))
37 * Matrix.CreateTranslation(position);
38 }
39
40 return transform.Value;
41 }
42 }
43
44 public void Write(BinaryWriter writer)
45 {
46 writer.Write(objectId);
47 writer.Write((int)objectFlags);
48 writer.Write(position);
49 writer.Write(rotation);
50 WriteOsd(writer);
51 }
52
53 protected abstract void WriteOsd(BinaryWriter writer);
54
55 public void Read(BinaryReader reader)
56 {
57 objectId = reader.ReadInt32();
58 objectFlags = (ObjectFlags)reader.ReadInt32();
59 position = reader.ReadVector3();
60 rotation = reader.ReadVector3();
61 ReadOsd(reader);
62 }
63
64 protected abstract void ReadOsd(BinaryReader reader);
65
66 public void Write(XmlWriter xml)
67 {
68 xml.WriteStartElement("Header");
69 xml.WriteEndElement();
70 xml.WriteStartElement("OSD");
71 xml.WriteEndElement();
72 xml.WriteEndElement();
73 }
74
75 protected abstract void WriteOsd(XmlWriter xml);
76
77 public void Read(XmlReader xml, ObjectLoadContext context)
78 {
79 xml.ReadStartElement();
80 xml.ReadStartElement("Header");
81
82 while (xml.IsStartElement())
83 {
84 switch (xml.LocalName)
85 {
86 case "Flags":
87 objectFlags = Oni.Metadata.MetaEnum.Parse<ObjectFlags>(xml.ReadElementContentAsString("Flags", ""));
88 break;
89 case "Position":
90 position = xml.ReadElementContentAsVector3("Position");
91 break;
92 case "Rotation":
93 rotation = xml.ReadElementContentAsVector3("Rotation");
94 break;
95 default:
96 xml.Skip();
97 break;
98 }
99 }
100
101 xml.ReadEndElement();
102
103 xml.ReadStartElement("OSD");
104 ReadOsd(xml, context);
105 xml.ReadEndElement();
106
107 xml.ReadEndElement();
108 }
109
110 protected abstract void ReadOsd(XmlReader xml, ObjectLoadContext context);
111 }
112}
Note: See TracBrowser for help on using the repository browser.