00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 using System.Runtime.Serialization;
00005 using System.IO;
00006 using Microsoft.DirectX;
00007 using Microsoft.DirectX.Direct3D;
00008 using Microsoft.Samples.DirectX.UtilityToolkit;
00009
00010 namespace DXGfxLib.Serialization
00011 {
00012 public class CursorSurrogate : ISerializationSurrogate
00013 {
00020 public void GetObjectData(Object obj, SerializationInfo info, StreamingContext context)
00021 {
00022 if (obj == null)
00023 {
00024 throw new NullReferenceException("No scene object to serialize, this is wrong!");
00025 }
00026
00027 SceneObject sobj = obj as SceneObject;
00028
00029 if (sobj == null)
00030 {
00031 throw new ArgumentException("The Object passed through obj argument is not a scene object, this is wrong!");
00032 }
00033
00034 info.AddValue("Name", sobj.name, typeof(string));
00035 info.AddValue("Type", sobj.GetType().ToString(), typeof(string));
00036 info.AddValue("Filename", sobj.fileName, typeof(string));
00037
00038 info.AddValue("X", sobj.X, typeof(float));
00039 info.AddValue("Y", sobj.Y, typeof(float));
00040 info.AddValue("Z", sobj.Z, typeof(float));
00041 info.AddValue("Yaw", sobj.Yaw, typeof(float));
00042 info.AddValue("Pitch", sobj.Pitch, typeof(float));
00043 info.AddValue("Roll", sobj.Roll, typeof(float));
00044
00045 if (sobj.OurScript != null)
00046 {
00047 info.AddValue("VarSrc", sobj.OurScript.VarSrc);
00048 info.AddValue("CodeSrc", sobj.OurScript.CodeSrc);
00049 info.AddValue("OnClickSrc", sobj.OurScript.ClickCodeSrc);
00050 info.AddValue("OnCollSrc", sobj.OurScript.CollisionCodeSrc);
00051 }
00052 else
00053 {
00054 info.AddValue("VarSrc", "");
00055 info.AddValue("CodeSrc", "");
00056 info.AddValue("OnClickSrc", "");
00057 info.AddValue("OnCollSrc", "");
00058 }
00059
00060
00061 }
00062
00071 public Object SetObjectData(Object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
00072 {
00073 if (obj == null)
00074 {
00075 throw new NullReferenceException("No scene object to serialize, this is wrong!");
00076 }
00077
00078 SceneObject sobj = obj as SceneObject;
00079
00080 if (sobj == null)
00081 {
00082 throw new ArgumentException("The Object passed through obj argument is not a scene object, this is wrong!");
00083 }
00084
00085 sobj.name = (string)info.GetValue("Name", typeof(string));
00086 sobj.Initialize();
00087 string type = (string)info.GetValue("Type", typeof(string));
00088 sobj.fileName = (string)info.GetValue("Filename", typeof(string));
00089
00090 float X = (float)info.GetValue("X", typeof(float));
00091 float Y = (float)info.GetValue("Y", typeof(float));
00092 float Z = (float)info.GetValue("Z", typeof(float));
00093 float Yaw = (float)info.GetValue("Yaw", typeof(float));
00094 float Pitch = (float)info.GetValue("Pitch", typeof(float));
00095 float Roll = (float)info.GetValue("Roll", typeof(float));
00096
00097 Matrix world = Matrix.Multiply(Matrix.RotationYawPitchRoll(Yaw, Pitch, Roll), Matrix.Translation(X, Y, Z));
00098
00099 sobj.worldMat = world;
00100
00101 Script theScript = new Script();
00102 theScript.UpdateVarSrc((string)info.GetValue("VarSrc", typeof(string)));
00103 theScript.UpdateCodeSrc((string)info.GetValue("CodeSrc", typeof(string)));
00104 theScript.UpdateClickCodeSrc((string)info.GetValue("OnClickSrc", typeof(string)));
00105 theScript.UpdateCollisionCodeSrc((string)info.GetValue("OnCollSrc", typeof(string)));
00106
00107 sobj.script = theScript;
00108
00109 return null;
00110 }
00111
00112 }
00113 }