- Main Page
- Packages
- Classes
- Files

- File List
- File Members

# D:/guillaume/guillara/3D/DXGfx/DXGfxLib/Serialization/SceneObjectSurrogate.cs

Go to the documentation of this file.

    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 SceneObjectSurrogate : 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             info.AddValue("m11", sobj.worldMat.M11, typeof(float));
    00046             info.AddValue("m12", sobj.worldMat.M12, typeof(float));
    00047             info.AddValue("m13", sobj.worldMat.M13, typeof(float));
    00048             info.AddValue("m14", sobj.worldMat.M14, typeof(float));
    00049
    00050             info.AddValue("m21", sobj.worldMat.M21, typeof(float));
    00051             info.AddValue("m22", sobj.worldMat.M22, typeof(float));
    00052             info.AddValue("m23", sobj.worldMat.M23, typeof(float));
    00053             info.AddValue("m24", sobj.worldMat.M24, typeof(float));
    00054
    00055             info.AddValue("m31", sobj.worldMat.M31, typeof(float));
    00056             info.AddValue("m32", sobj.worldMat.M32, typeof(float));
    00057             info.AddValue("m33", sobj.worldMat.M33, typeof(float));
    00058             info.AddValue("m34", sobj.worldMat.M34, typeof(float));
    00059
    00060             info.AddValue("m41", sobj.worldMat.M41, typeof(float));
    00061             info.AddValue("m42", sobj.worldMat.M42, typeof(float));
    00062             info.AddValue("m43", sobj.worldMat.M43, typeof(float));
    00063             info.AddValue("m44", sobj.worldMat.M44, typeof(float));
    00064
    00065
    00066             if (sobj.OurScript != null)
    00067             {
    00068                 info.AddValue("VarSrc", sobj.OurScript.VarSrc);
    00069                 info.AddValue("CodeSrc", sobj.OurScript.CodeSrc);
    00070                 info.AddValue("OnClickSrc", sobj.OurScript.ClickCodeSrc);
    00071                 info.AddValue("OnCollSrc", sobj.OurScript.CollisionCodeSrc);
    00072             }
    00073             else
    00074             {
    00075                 info.AddValue("VarSrc", "");
    00076                 info.AddValue("CodeSrc", "");
    00077                 info.AddValue("OnClickSrc", "");
    00078                 info.AddValue("OnCollSrc", "");
    00079             }
    00080
    00081
    00082         }
    00083
    00092         public Object SetObjectData(Object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
    00093         {
    00094             if (obj == null)
    00095             {
    00096                 throw new NullReferenceException("No scene object to serialize, this is wrong!");
    00097             }
    00098
    00099             SceneObject sobj = obj as SceneObject;
    00100
    00101             if (sobj == null)
    00102             {
    00103                 throw new ArgumentException("The Object passed through obj argument is not a scene object, this is wrong!");
    00104             }
    00105
    00106             sobj.name = (string)info.GetValue("Name",typeof(string));
    00107             sobj.Initialize();
    00108             string type = (string)info.GetValue("Type", typeof(string));
    00109             sobj.fileName = (string)info.GetValue("Filename", typeof(string));
    00110
    00111             if (!File.Exists(sobj.fileName))
    00112             {
    00113                 // Check if in Media subfolder.
    00114                 if (!File.Exists(DXGfxManager.GetGlobalInstance().mediaFolderPath + "\\" + sobj.fileName))
    00115                 {
    00116                     throw new Exception("File : " + sobj.fileName + " does not exist we are going to have a problem. Please check the Media folder associated with your scene is in the same folder as the scene xml file.");
    00117                 }
    00118
    00119             }
    00120
    00121             //float X = (float)info.GetValue("X", typeof(float));
    00122             //float Y = (float)info.GetValue("Y", typeof(float));
    00123             //float Z = (float)info.GetValue("Z", typeof(float));
    00124             //float Yaw = (float)info.GetValue("Yaw", typeof(float));
    00125             //float Pitch = (float)info.GetValue("Pitch", typeof(float));
    00126             //float Roll = (float)info.GetValue("Roll", typeof(float));
    00127
    00128             Matrix world;
    00129
    00130             world.M11 = (float)info.GetValue("m11", typeof(float));
    00131             world.M12 = (float)info.GetValue("m12", typeof(float));
    00132             world.M13 = (float)info.GetValue("m13", typeof(float));
    00133             world.M14 = (float)info.GetValue("m14", typeof(float));
    00134
    00135             world.M21 = (float)info.GetValue("m21", typeof(float));
    00136             world.M22 = (float)info.GetValue("m22", typeof(float));
    00137             world.M23 = (float)info.GetValue("m23", typeof(float));
    00138             world.M24 = (float)info.GetValue("m24", typeof(float));
    00139
    00140             world.M31 = (float)info.GetValue("m31", typeof(float));
    00141             world.M32 = (float)info.GetValue("m32", typeof(float));
    00142             world.M33 = (float)info.GetValue("m33", typeof(float));
    00143             world.M34 = (float)info.GetValue("m34", typeof(float));
    00144
    00145             world.M41 = (float)info.GetValue("m41", typeof(float));
    00146             world.M42 = (float)info.GetValue("m42", typeof(float));
    00147             world.M43 = (float)info.GetValue("m43", typeof(float));
    00148             world.M44 = (float)info.GetValue("m44", typeof(float));
    00149             //Matrix world = Matrix.Multiply(Matrix.RotationYawPitchRoll(Yaw, Pitch, Roll), Matrix.Translation(X, Y, Z));
    00150
    00151             sobj.worldMat = world;
    00152
    00153             Script theScript = new Script();
    00154             theScript.UpdateVarSrc((string)info.GetValue("VarSrc", typeof(string)));
    00155             theScript.UpdateCodeSrc((string)info.GetValue("CodeSrc", typeof(string)));
    00156             theScript.UpdateClickCodeSrc((string)info.GetValue("OnClickSrc", typeof(string)));
    00157             theScript.UpdateCollisionCodeSrc((string)info.GetValue("OnCollSrc", typeof(string)));
    00158
    00159             sobj.script = theScript;
    00160
    00161             return null;
    00162         }
    00163
    00164     }
    00165 }