private void m_gameDocumentRegistry_DocumentAdded(object sender, ItemInsertedEventArgs <IGameDocument> e) { IGameDocument document = e.Item; IGame game = document.Cast <IGame>(); if (document == m_gameDocumentRegistry.MasterDocument) { NativeObjectAdapter gameLevel = document.Cast <NativeObjectAdapter>(); GameEngine.CreateObject(gameLevel); GameEngine.SetGameLevel(gameLevel); gameLevel.UpdateNativeOjbect(); //create vertex buffer for grid. IGrid grid = document.As <IGame>().Grid; GridRenderer gridRender = grid.Cast <GridRenderer>(); gridRender.CreateVertices(); m_designView.Context = document.Cast <IGameContext>(); } DomNode masterNode = m_gameDocumentRegistry.MasterDocument.As <DomNode>(); DomNode rooFolderNode = game.RootGameObjectFolder.Cast <DomNode>(); NativeGameWorldAdapter gworld = masterNode.Cast <NativeGameWorldAdapter>(); gworld.Insert(masterNode, rooFolderNode, masterNode.Type.GetChildInfo("gameObjectFolder"), -1); }
protected override void OnNodeSet() { base.OnNodeSet(); // we must register this document and get an id for it DomNode node = this.DomNode; var tag = node.Type.GetTag(NativeAnnotations.NativeDocumentType); var typeId = (tag != null) ? (uint)tag : 0; m_nativeDocId = GameEngine.CreateDocument(typeId); ManageNativeObjectLifeTime = true; foreach (var subnode in node.Subtree) { NativeObjectAdapter childObject = subnode.As <NativeObjectAdapter>(); if (childObject != null) { childObject.OnAddToDocument(this); NativeObjectAdapter parentObject = subnode.Parent.As <NativeObjectAdapter>(); if (parentObject != null) { childObject.OnSetParent(parentObject, -1); } } } node.ChildInserted += node_ChildInserted; node.ChildRemoved += node_ChildRemoved; }
private void node_ChildInserted_Internal(object child, object parent, int insertionPoint) { NativeObjectAdapter childObject = child.As <NativeObjectAdapter>(); if (childObject != null) { childObject.OnAddToDocument(this); NativeObjectAdapter parentObject = parent.As <NativeObjectAdapter>(); if (parentObject != null) { childObject.OnSetParent(parentObject, insertionPoint); } else { childObject.OnSetParent(null, -1); } } var children = child.As <DomNode>().Children; int index = 0; foreach (var c in children) { node_ChildInserted_Internal(c, child, index); ++index; } }
public void OnDocumentRemoved() { // we need to call OnRemoveFromDocument on all children // NativeObjectAdapter.OnRemoveFromDocument is hierarchical, // so we only need to call on the top level children DomNode node = this.DomNode; if (node != null) { foreach (var subnode in node.Children) { NativeObjectAdapter childObject = subnode.As <NativeObjectAdapter>(); if (childObject != null) { childObject.OnRemoveFromDocument(this); } } } // destroy the document on the native side, as well var tag = node.Type.GetTag(NativeAnnotations.NativeDocumentType); var typeId = (tag != null) ? (uint)tag : 0; GameEngine.DeleteDocument(m_nativeDocId, typeId); }
public static void ObjectRemoveChild(NativeObjectAdapter parent, NativeObjectAdapter child, uint listId) { uint typeId = parent != null ? parent.TypeId : 0; ulong parentId = parent != null ? parent.InstanceId : 0; ulong childId = child != null ? child.InstanceId : 0; ObjectRemoveChild(typeId, listId, parentId, childId); }
public static void DestroyObject(NativeObjectAdapter gob) { if (gob.InstanceId == 0) { return; } NativeDestroyObject(gob.TypeId, gob.InstanceId); ResetIds(gob); }
internal void UpdateLocalToWorld_Children() { foreach (DomNode child in DomNode.Subtree) { NativeObjectAdapter childObject = child.As <NativeObjectAdapter>(); if (childObject != null) { childObject.UpdateLocalToWorld(); } } }
public static ulong CreateObject(NativeObjectAdapter gob) { ulong instanceId = CreateObject(gob.TypeId, IntPtr.Zero, 0); if (instanceId != 0) { s_idToDomNode.Add(instanceId, gob); gob.SetNativeHandle(instanceId); } return(instanceId); }
private void GenThumbnail(Uri resourceUri, string thumbnailPath) { NativeObjectAdapter gameLevel = GameEngine.GetGameLevel(); try { IResource resource = m_resourceService.Load(resourceUri); IGameObject gob = m_resourceConverterService.Convert(resource); if (gob == null) { return; } m_game.RootGameObjectFolder.GameObjects.Add(gob); GameEngine.SetRenderState(m_renderState); GameEngine.SetGameLevel(m_game.Cast <NativeObjectAdapter>()); m_gameEngine.WaitForPendingResources(); FrameTime fr = new FrameTime(0, 0); m_gameEngine.Update(fr, UpdateType.Paused); IBoundable boundable = gob.Cast <IBoundable>(); Sphere3F sphere = boundable.BoundingBox.ToSphere(); if (Math.Abs(sphere.Radius) <= float.Epsilon) { sphere.Radius = 1.0f; } m_cam.SetPerspective( (float)Math.PI / 4, 1.0f, sphere.Radius * 0.01f, sphere.Radius * 4.0f); Vec3F camPos = sphere.Center + new Vec3F(sphere.Radius, sphere.Radius, sphere.Radius) * 1.5f; m_cam.Set(camPos, sphere.Center, new Vec3F(0, 1, 0)); GameEngine.Begin(m_renderSurface.InstanceId, m_cam.ViewMatrix, m_cam.ProjectionMatrix); GameEngine.RenderGame(); GameEngine.End(); GameEngine.SaveRenderSurfaceToFile(m_renderSurface.InstanceId, thumbnailPath); m_game.RootGameObjectFolder.GameObjects.Remove(gob); m_resourceService.Unload(resourceUri); } finally { GameEngine.SetGameLevel(gameLevel); } }
private static void ResetIds(NativeObjectAdapter gob) { s_idToDomNode.Remove(gob.InstanceId); gob.SetNativeHandle(0); foreach (DomNode child in gob.DomNode.Children) { NativeObjectAdapter childObject = child.As <NativeObjectAdapter>(); if (childObject != null) { ResetIds(childObject); } } }
public void OnSetParent(NativeObjectAdapter newParent, int insertionPosition) { if (newParent != null) { GameEngine.SetObjectParent( m_documentId, InstanceId, TypeId, newParent.InstanceId, newParent.TypeId, insertionPosition); } else { GameEngine.SetObjectParent( m_documentId, InstanceId, TypeId, 0, 0, insertionPosition); } }
private AABB GetAABB(NativeAttributeInfo attrInfo) { unsafe { NativeObjectAdapter nativeobj = this.As <NativeObjectAdapter>(); ulong instanceId = nativeobj != null ? nativeobj.InstanceId : 0; if (instanceId != 0) { int datasize = 0; IntPtr data; GameEngine.GetObjectProperty(attrInfo.TypeId, attrInfo.PropertyId, this.Cast <NativeObjectAdapter>().InstanceId , out data, out datasize); AABB bound = *(AABB *)data.ToPointer(); return(bound); } return(new AABB()); } }
public void Insert(DomNode parent, DomNode child, ChildInfo chInfo, int index) { NativeObjectAdapter childObject = child.As <NativeObjectAdapter>(); NativeObjectAdapter parentObject = parent.As <NativeObjectAdapter>(); object listIdObj = chInfo.GetTag(NativeAnnotations.NativeElement); if (childObject == null || parentObject == null || listIdObj == null) { return; } if (chInfo.IsList && index >= (parent.GetChildList(chInfo).Count - 1)) { index = -1; } if (ManageNativeObjectLifeTime) { GameEngine.CreateObject(childObject); childObject.UpdateNativeOjbect(); } System.Diagnostics.Debug.Assert(childObject.InstanceId != 0); uint listId = (uint)listIdObj; uint typeId = (uint)chInfo.DefiningType.GetTag(NativeAnnotations.NativeType); ulong parentId = parentObject.InstanceId; ulong childId = childObject.InstanceId; if (index >= 0) { GameEngine.ObjectInsertChild(typeId, listId, parentId, childId, index); } else { GameEngine.ObjectAddChild(typeId, listId, parentId, childId); } foreach (var node in child.Children) { Insert(child, node, node.ChildInfo, -1); // use -1 for index to indicate an append operation. } }
public void OnRemoveFromDocument(NativeDocumentAdapter doc) { OnRemoveFromDocument_NonHier(doc); // note -- use "Subtree" & not "Children" because this // will allow us to also touch NativeObjectAdapters // that are children of non-native objects // eg: // <<native object>> // <<non-native object> // <<native object>> foreach (DomNode child in DomNode.Subtree) { NativeObjectAdapter childObject = child.As <NativeObjectAdapter>(); if (childObject != null) { childObject.OnRemoveFromDocument_NonHier(doc); } } }
private AABB GetAABB(NativeAttributeInfo attrInfo) { unsafe { NativeObjectAdapter nativeobj = this.As <NativeObjectAdapter>(); ulong instanceId = nativeobj != null ? nativeobj.InstanceId : 0; if (instanceId != 0) { int datasize = 0; IntPtr data; var nao = this.Cast <NativeObjectAdapter>(); GameEngine.GetObjectProperty(attrInfo.TypeId, attrInfo.PropertyId, nao.DocumentId, nao.InstanceId, out data, out datasize); Vec3F *vecptr = (Vec3F *)data.ToPointer(); AABB bound = new AABB(vecptr[0], vecptr[1]); return(bound); } return(new AABB()); } }
public void Remove(DomNode parent, DomNode child, ChildInfo chInfo) { NativeObjectAdapter childObject = child.As <NativeObjectAdapter>(); NativeObjectAdapter parentObject = parent.As <NativeObjectAdapter>(); object listIdObj = chInfo.GetTag(NativeAnnotations.NativeElement); if (childObject == null || parentObject == null || listIdObj == null) { return; } uint listId = (uint)listIdObj; uint typeId = (uint)chInfo.DefiningType.GetTag(NativeAnnotations.NativeType); ulong parentId = parentObject.InstanceId; ulong childId = childObject.InstanceId; GameEngine.ObjectRemoveChild(typeId, listId, parentId, childId); if (ManageNativeObjectLifeTime) { GameEngine.DestroyObject(childObject); } }
private void Init() { if (m_game != null) { return; } NativeObjectAdapter curLevel = GameEngine.GetGameLevel(); try { // create new document by creating a Dom node of the root type defined by the schema DomNode rootNode = new DomNode(m_schemaLoader.GameType, m_schemaLoader.GameRootElement); INameable nameable = rootNode.As <INameable>(); nameable.Name = "Game"; NativeObjectAdapter gameLevel = rootNode.Cast <NativeObjectAdapter>(); GameEngine.CreateObject(gameLevel); GameEngine.SetGameLevel(gameLevel); gameLevel.UpdateNativeOjbect(); NativeGameWorldAdapter gworld = rootNode.Cast <NativeGameWorldAdapter>(); m_game = rootNode.Cast <IGame>(); IGameObjectFolder rootFolder = m_game.RootGameObjectFolder; m_renderSurface.Game = m_game; m_renderSurface.GameEngineProxy = m_gameEngine; } finally { GameEngine.SetGameLevel(curLevel); } m_mainWindow.Closed += delegate { GameEngine.DestroyObject(m_game.Cast <NativeObjectAdapter>()); m_renderSurface.Dispose(); }; }
private void Init() { NativeObjectAdapter curLevel = GameEngine.GetGameLevel(); try { // create new document by creating a Dom node of the root type defined by the schema DomNode rootNode = new DomNode(m_schemaLoader.GameType, m_schemaLoader.GameRootElement); INameable nameable = rootNode.Cast <INameable>(); nameable.Name = "ThumbnailGenerator"; NativeObjectAdapter gameLevel = rootNode.Cast <NativeObjectAdapter>(); GameEngine.CreateObject(gameLevel); GameEngine.SetGameLevel(gameLevel); gameLevel.UpdateNativeOjbect(); NativeGameWorldAdapter gworld = rootNode.Cast <NativeGameWorldAdapter>(); m_game = rootNode.Cast <IGame>(); IGameObjectFolder rootFolder = m_game.RootGameObjectFolder; m_renderSurface = new TextureRenderSurface(96, 96); m_renderState = new RenderState(); m_renderState.RenderFlag = GlobalRenderFlags.Solid | GlobalRenderFlags.Textured | GlobalRenderFlags.Lit | GlobalRenderFlags.Shadows; } finally { GameEngine.SetGameLevel(curLevel); } m_mainWindow.Closed += delegate { GameEngine.DestroyObject(m_game.Cast <NativeObjectAdapter>()); m_renderSurface.Dispose(); m_renderState.Dispose(); }; }
public static void DestroyObject(NativeObjectAdapter gob) { if (gob.InstanceId == 0) return; NativeDestroyObject(gob.TypeId, gob.InstanceId); ResetIds(gob); }
private static void ResetIds(NativeObjectAdapter gob) { s_idToDomNode.Remove(gob.InstanceId); gob.SetNativeHandle(0); foreach (DomNode child in gob.DomNode.Children) { NativeObjectAdapter childObject = child.As<NativeObjectAdapter>(); if (childObject != null) ResetIds(childObject); } }
public static void SetGameLevel(NativeObjectAdapter game) { ulong insId = game != null ? game.InstanceId : 0; NativeSetGameLevel(insId); }
public static ulong CreateObject(NativeObjectAdapter gob) { ulong instanceId = CreateObject(gob.TypeId, IntPtr.Zero, 0); if (instanceId != 0) { s_idToDomNode.Add(instanceId, gob); gob.SetNativeHandle(instanceId); } return instanceId; }
unsafe internal static void PushAttribute( uint propertyId, Type clrType, int arrayLength, object data, IList <PropertyInitializer> properties, System.IO.UnmanagedMemoryStream stream) { Type elmentType = clrType.GetElementType(); if (clrType.IsArray && elmentType.IsPrimitive) { if (elmentType == typeof(float)) { var count = Math.Min(arrayLength, ((float[])data).Length); properties.Add(GameEngine.CreateInitializer( propertyId, stream.PositionPointer, elmentType, (uint)count, false)); fixed(float *d = (float[])data) for (uint c = 0; c < count; ++c) { ((float *)stream.PositionPointer)[c] = d[c]; } stream.Position += sizeof(float) * count; } else if (elmentType == typeof(int)) { var count = Math.Min(arrayLength, ((int[])data).Length); properties.Add(GameEngine.CreateInitializer( propertyId, stream.PositionPointer, elmentType, (uint)count, false)); fixed(int *d = (int[])data) for (uint c = 0; c < count; ++c) { ((int *)stream.PositionPointer)[c] = d[c]; } stream.Position += sizeof(int) * count; } else if (elmentType == typeof(uint)) { var count = Math.Min(arrayLength, ((uint[])data).Length); properties.Add(GameEngine.CreateInitializer( propertyId, stream.PositionPointer, elmentType, (uint)count, false)); fixed(uint *d = (uint[])data) for (uint c = 0; c < count; ++c) { ((uint *)stream.PositionPointer)[c] = d[c]; } stream.Position += sizeof(int) * count; } else { System.Diagnostics.Debug.Assert(false); } } else { if (clrType == typeof(string)) { SetStringProperty(propertyId, (string)data, properties, stream); } else if (clrType == typeof(bool)) { uint d = Convert.ToUInt32((bool)data); SetBasicProperty(propertyId, d, properties, stream); *(uint *)stream.PositionPointer = d; stream.Position += sizeof(uint); } else if (clrType == typeof(byte)) { SetBasicProperty(propertyId, (byte)data, properties, stream); *(byte *)stream.PositionPointer = (byte)data; stream.Position += sizeof(byte); } else if (clrType == typeof(sbyte)) { SetBasicProperty(propertyId, (sbyte)data, properties, stream); *(sbyte *)stream.PositionPointer = (sbyte)data; stream.Position += sizeof(sbyte); } else if (clrType == typeof(short)) { SetBasicProperty(propertyId, (short)data, properties, stream); *(short *)stream.PositionPointer = (short)data; stream.Position += sizeof(short); } else if (clrType == typeof(ushort)) { SetBasicProperty(propertyId, (ushort)data, properties, stream); *(ushort *)stream.PositionPointer = (ushort)data; stream.Position += sizeof(ushort); } else if (clrType == typeof(int)) { SetBasicProperty(propertyId, (int)data, properties, stream); *(int *)stream.PositionPointer = (int)data; stream.Position += sizeof(int); } else if (clrType == typeof(uint)) { SetBasicProperty(propertyId, (uint)data, properties, stream); *(uint *)stream.PositionPointer = (uint)data; stream.Position += sizeof(uint); } else if (clrType == typeof(long)) { SetBasicProperty(propertyId, (long)data, properties, stream); *(long *)stream.PositionPointer = (long)data; stream.Position += sizeof(long); } else if (clrType == typeof(ulong)) { SetBasicProperty(propertyId, (ulong)data, properties, stream); *(ulong *)stream.PositionPointer = (ulong)data; stream.Position += sizeof(ulong); } else if (clrType == typeof(float)) { SetBasicProperty(propertyId, (float)data, properties, stream); *(float *)stream.PositionPointer = (float)data; stream.Position += sizeof(float); } else if (clrType == typeof(double)) { SetBasicProperty(propertyId, (double)data, properties, stream); *(double *)stream.PositionPointer = (double)data; stream.Position += sizeof(double); } else if (clrType == typeof(System.Uri)) { if (data != null) { string assetName = AsAssetName((Uri)data); if (!string.IsNullOrWhiteSpace(assetName)) { SetStringProperty(propertyId, assetName, properties, stream); } } } else if (clrType == typeof(DomNode)) { // this is a 'reference' to an object DomNode refNode = (DomNode)data; NativeObjectAdapter nativeGob = refNode.As <NativeObjectAdapter>(); if (nativeGob != null) { SetBasicProperty(propertyId, (ulong)nativeGob.InstanceId, properties, stream); *(ulong *)stream.PositionPointer = (ulong)nativeGob.InstanceId; stream.Position += sizeof(ulong); } } } }
protected override IList <object> Pick(MouseEventArgs e) { bool multiSelect = DragOverThreshold; List <object> paths = new List <object>(); HitRecord[] hits; if (multiSelect) {// frustum pick RectangleF rect = MakeRect(FirstMousePoint, CurrentMousePoint); hits = GameEngine.FrustumPick(SurfaceId, Camera.ViewMatrix, Camera.ProjectionMatrix, rect); } else {// ray pick Ray3F rayW = GetWorldRay(CurrentMousePoint); hits = GameEngine.RayPick(Camera.ViewMatrix, Camera.ProjectionMatrix, rayW, false); } // create unique list of hits HashSet <ulong> instanceSet = new HashSet <ulong>(); List <HitRecord> uniqueHits = new List <HitRecord>(); // build 'path' objects for each hit record. foreach (HitRecord hit in hits) { bool added = instanceSet.Add(hit.instanceId); if (added) { uniqueHits.Add(hit); } } HitRecord firstHit = new HitRecord(); // build 'path' objects for each hit record. foreach (HitRecord hit in uniqueHits) { NativeObjectAdapter nobj = GameEngine.GetAdapterFromId(hit.instanceId); DomNode dom = nobj.DomNode; object hitPath = Util.AdaptDomPath(dom); object obj = DesignView.PickFilter.Filter(hitPath, e); if (obj != null) { if (paths.Count == 0) { firstHit = hit; } var newPath = obj as AdaptablePath <object> ?? Util.AdaptDomPath((DomNode)obj); paths.Add(newPath); } } if (multiSelect == false && paths.Count > 0) { var path = paths[0]; ISelectionContext selection = DesignView.Context.As <ISelectionContext>(); ILinear linear = path.As <ILinear>(); if (linear != null && Control.ModifierKeys == System.Windows.Forms.Keys.Shift && selection.SelectionContains(path)) { ITransactionContext trans = DesignView.Context.As <ITransactionContext>(); trans.DoTransaction( delegate { linear.InsertPoint(firstHit.index, firstHit.hitPt.X, firstHit.hitPt.Y, firstHit.hitPt.Z); }, "insert control point".Localize() ); } } return(paths); }
unsafe private void UpdateNativeProperty(AttributeInfo attribInfo) { object idObj = attribInfo.GetTag(NativeAnnotations.NativeProperty); if (idObj == null) { return; } uint id = (uint)idObj; if (this.InstanceId == 0) { return; } AttributeInfo mappedAttribute = attribInfo.GetTag(NativeAnnotations.MappedAttribute) as AttributeInfo; DomNodeType definingType = (mappedAttribute != null) ? mappedAttribute.DefiningType : attribInfo.DefiningType; uint typeId = (uint)definingType.GetTag(NativeAnnotations.NativeType); Type clrType = attribInfo.Type.ClrType; Type elmentType = clrType.GetElementType(); object data = this.DomNode.GetAttribute(attribInfo); if (clrType.IsArray && elmentType.IsPrimitive) { GCHandle pinHandle = new GCHandle(); try { pinHandle = GCHandle.Alloc(data, GCHandleType.Pinned); IntPtr ptr = pinHandle.AddrOfPinnedObject(); int sz = Marshal.SizeOf(elmentType); GameEngine.SetObjectProperty(typeId, InstanceId, id, ptr, sz * attribInfo.Type.Length); } finally { if (pinHandle.IsAllocated) { pinHandle.Free(); } } } else { IntPtr ptr = IntPtr.Zero; int sz = 0; if (clrType == typeof(string)) { string str = (string)data; if (!string.IsNullOrEmpty(str)) { fixed(char *chptr = str) { ptr = new IntPtr((void *)chptr); sz = str.Length * 2; GameEngine.SetObjectProperty(typeId, InstanceId, id, ptr, sz); } return; } } else if (clrType == typeof(DateTime)) { DateTime dt = (DateTime)data; float seconds = (float)(dt.Hour * 60 * 60 + dt.Minute * 60 + dt.Second); float secondsInADay = 60.0f * 60.0f * 24.0f; // sec per minute * min per hour * hour per day float normalizedTime = seconds / secondsInADay; // normalize 0.0 to 1.0 ptr = new IntPtr(&normalizedTime); sz = sizeof(float); } else if (clrType == typeof(bool)) { bool val = (bool)data; ptr = new IntPtr(&val); sz = sizeof(bool); } else if (clrType == typeof(byte)) { byte val = (byte)data; ptr = new IntPtr(&val); sz = sizeof(byte); } else if (clrType == typeof(sbyte)) { sbyte val = (sbyte)data; ptr = new IntPtr(&val); sz = sizeof(sbyte); } else if (clrType == typeof(short)) { short val = (short)data; ptr = new IntPtr(&val); sz = sizeof(short); } else if (clrType == typeof(ushort)) { ushort val = (ushort)data; ptr = new IntPtr(&val); sz = sizeof(ushort); } else if (clrType == typeof(int)) { int val = (int)data; ptr = new IntPtr(&val); sz = sizeof(int); } else if (clrType == typeof(uint)) { uint val = (uint)data; ptr = new IntPtr(&val); sz = sizeof(uint); } else if (clrType == typeof(long)) { long val = (long)data; ptr = new IntPtr(&val); sz = sizeof(long); } else if (clrType == typeof(ulong)) { ulong val = (ulong)data; ptr = new IntPtr(&val); sz = sizeof(ulong); } else if (clrType == typeof(float)) { float val = (float)data; ptr = new IntPtr(&val); sz = sizeof(float); } else if (clrType == typeof(double)) { double val = (double)data; ptr = new IntPtr(&val); sz = sizeof(double); } else if (clrType == typeof(System.Uri)) { if (data != null && !string.IsNullOrWhiteSpace(data.ToString())) { Uri uri = (Uri)data; string str = uri.LocalPath; fixed(char *chptr = str) { ptr = new IntPtr((void *)chptr); sz = str.Length * 2; GameEngine.SetObjectProperty(typeId, InstanceId, id, ptr, sz); } return; } } else if (clrType == typeof(DomNode)) { // this is a 'reference' to an object DomNode node = (DomNode)data; NativeObjectAdapter nativeGob = node.As <NativeObjectAdapter>(); if (nativeGob != null) { ptr = new IntPtr((void *)nativeGob.InstanceId); sz = sizeof(ulong); } } GameEngine.SetObjectProperty(typeId, InstanceId, id, ptr, sz); } }
void IInitializable.Initialize() { m_controlInfo = new ControlInfo("DesignView", "DesignView", StandardControlGroup.CenterPermanent); m_controlHostService.RegisterControl(m_designView.HostControl, m_controlInfo, this); Application.ApplicationExit += delegate { Util3D.Shutdown(); GameEngine.Shutdown(); }; GameEngine.RefreshView += (sender, e) => m_designView.InvalidateViews(); m_gameDocumentRegistry.DocumentAdded += m_gameDocumentRegistry_DocumentAdded; m_gameDocumentRegistry.DocumentRemoved += m_gameDocumentRegistry_DocumentRemoved; string ns = m_schemaLoader.NameSpace; // register GridRenderer on grid child. DomNodeType gridType = m_schemaLoader.TypeCollection.GetNodeType(ns, "gridType"); gridType.Define(new ExtensionInfo <GridRenderer>()); // register NativeGameWorldAdapter on game type. m_schemaLoader.GameType.Define(new ExtensionInfo <NativeDocumentAdapter>()); // parse schema annotation. foreach (DomNodeType domType in m_schemaLoader.TypeCollection.GetNodeTypes()) { var topLevelAnnotations = domType.GetTagLocal <IEnumerable <XmlNode> >(); if (topLevelAnnotations == null) { continue; } // First, go through and interpret the annotations that are not inherited List <NativeAttributeInfo> nativeAttribs = new List <NativeAttributeInfo>(); foreach (XmlNode annot in topLevelAnnotations) { XmlElement elm = annot as XmlElement; if (elm.LocalName == NativeAnnotations.NativeType) { string typeName = elm.GetAttribute(NativeAnnotations.NativeName); domType.SetTag(NativeAnnotations.NativeType, GameEngine.GetObjectTypeId(typeName)); if (domType.IsAbstract == false) { domType.Define(new ExtensionInfo <NativeObjectAdapter>()); } } else if (elm.LocalName == NativeAnnotations.NativeDocumentType) { string typeName = elm.GetAttribute(NativeAnnotations.NativeName); domType.SetTag(NativeAnnotations.NativeDocumentType, GameEngine.GetDocumentTypeId(typeName)); if (domType.IsAbstract == false) { domType.Define(new ExtensionInfo <NativeDocumentAdapter>()); } } } if (domType.GetTag(NativeAnnotations.NativeType) == null) { continue; } uint typeId = (uint)domType.GetTag(NativeAnnotations.NativeType); bool isBoundableType = false; // Now, go through and interpret annotations that can be inheritted from base clases. // Sometimes a native property can be inheritted from a base class. In this model, we // will create a separate "property id" for each concrete class. When a property is // inheritted, the "property ids" for each type in the inheritance chain will be different // and unrelated. foreach (var inherittedType in domType.Lineage) { var annotations = inherittedType.GetTagLocal <IEnumerable <XmlNode> >(); if (annotations == null) { continue; } foreach (XmlNode annot in annotations) { XmlElement elm = annot as XmlElement; if (elm.LocalName == NativeAnnotations.NativeProperty) { // find a prop name and added to the attribute. string nativePropName = elm.GetAttribute(NativeAnnotations.NativeName); string attribName = elm.GetAttribute(NativeAnnotations.Name); uint propId = GameEngine.GetObjectPropertyId(typeId, nativePropName); if (!string.IsNullOrEmpty(attribName)) { AttributeInfo attribInfo = domType.GetAttributeInfo(elm.GetAttribute(NativeAnnotations.Name)); attribInfo.SetTag(NativeAnnotations.NativeProperty, propId); } else { NativeAttributeInfo attribInfo = new NativeAttributeInfo(domType, nativePropName, typeId, propId); nativeAttribs.Add(attribInfo); } if (nativePropName == "Bounds" || nativePropName == "LocalBounds") { isBoundableType = true; } } else if (elm.LocalName == NativeAnnotations.NativeElement) { ChildInfo info = domType.GetChildInfo(elm.GetAttribute(NativeAnnotations.Name)); string name = elm.GetAttribute(NativeAnnotations.NativeName); info.SetTag(NativeAnnotations.NativeElement, GameEngine.GetObjectChildListId(typeId, name)); } else if (elm.LocalName == NativeAnnotations.NativeVis) { using (var transfer = new NativeObjectAdapter.NativePropertyTransfer()) { using (var stream = transfer.CreateStream()) foreach (var a in elm.Attributes) { var attrib = a as XmlAttribute; if (attrib.Name == "geo") { NativeObjectAdapter.PushAttribute( 0, typeof(string), 1, attrib.Value, transfer.Properties, stream); } } GameEngine.SetTypeAnnotation(typeId, "vis", transfer.Properties); } } } } if (nativeAttribs.Count > 0) { domType.SetTag(nativeAttribs.ToArray()); } if (isBoundableType && domType.IsAbstract == false) { domType.Define(new ExtensionInfo <BoundableObject>()); } } // register BoundableObject m_schemaLoader.GameObjectFolderType.Define(new ExtensionInfo <BoundableObject>()); // doesn't have a bound native attributes -- is this really intended?s #region code to handle gameObjectFolder { // This code is fragile and need to be updated whenever // any relevant part of the schema changes. // purpose: // gameObjectFolderType does not exist in C++ // this code will map gameObjectFolderType to gameObjectGroupType. DomNodeType gobFolderType = m_schemaLoader.GameObjectFolderType; DomNodeType groupType = m_schemaLoader.GameObjectGroupType; // map native bound attrib from gameObject to GobFolder NativeAttributeInfo[] nativeAttribs = m_schemaLoader.GameObjectType.GetTag <NativeAttributeInfo[]>(); foreach (var attrib in nativeAttribs) { if (attrib.Name == "Bounds") { gobFolderType.SetTag(new NativeAttributeInfo[] { attrib }); break; } } // map type. // XLE --> Separate GameObjectFolder type from GameObjectGroup type // gobFolderType.Define(new ExtensionInfo<NativeObjectAdapter>()); // gobFolderType.SetTag(NativeAnnotations.NativeType, groupType.GetTag(NativeAnnotations.NativeType)); // map all native attributes of gameObjectGroup to gameFolder foreach (AttributeInfo srcAttrib in groupType.Attributes) { object nativeIdObject = srcAttrib.GetTag(NativeAnnotations.NativeProperty); if (nativeIdObject == null) { continue; } AttributeInfo destAttrib = gobFolderType.GetAttributeInfo(srcAttrib.Name); if (destAttrib == null) { continue; } destAttrib.SetTag(NativeAnnotations.NativeProperty, nativeIdObject); destAttrib.SetTag(NativeAnnotations.MappedAttribute, srcAttrib); } // map native element from gameObjectGroupType to gameObjectFolderType. object gobsId = groupType.GetChildInfo("gameObject").GetTag(NativeAnnotations.NativeElement); foreach (ChildInfo srcChildInfo in gobFolderType.Children) { if (srcChildInfo.IsList) { srcChildInfo.SetTag(NativeAnnotations.NativeElement, gobsId); } } m_schemaLoader.GameType.GetChildInfo("gameObjectFolder").SetTag(NativeAnnotations.NativeElement, gobsId); } #endregion // set up scripting bindings if (m_scriptingService != null) { m_scriptingService.SetVariable("cv", new GUILayer.TweakableBridge()); } }
public static void RegisterGob(ulong documentId, ulong instanceId, NativeObjectAdapter gob) { s_idToDomNode.Add(Tuple.Create(documentId, instanceId), gob); }
public static void DeregisterGob(ulong documentId, ulong instanceId, NativeObjectAdapter gob) { s_idToDomNode.Remove(Tuple.Create(documentId, instanceId)); }
// render the scene. public void Render() { if (GameEngine.IsInError || SurfaceId == 0 || Visible == false || Width == 0 || Height == 0 || Game == null) { return; } NativeObjectAdapter gameLevel = GameEngine.GetGameLevel(); try { NativeObjectAdapter game = Game.As <NativeObjectAdapter>(); GameEngine.SetGameLevel(game); GameEngine.SetRenderState(m_renderState); if (Game.RootGameObjectFolder.GameObjects.Count > 0) { GameEngine.Update(0, 0, true); } if (ResetCamera) { // save view type ViewTypes viewtype = this.ViewType; ViewType = ViewTypes.Perspective; Size sz = ClientSize; float aspect = (float)sz.Width / (float)sz.Height; IBoundable boundable = Game.RootGameObjectFolder.Cast <IBoundable>(); Sce.Atf.VectorMath.Sphere3F sphere = boundable.BoundingBox.ToSphere(); float nearZ = sphere.Radius * 0.01f; nearZ = Math.Min(0.1f, nearZ); Camera.SetPerspective( (float)Math.PI / 4, aspect, nearZ, sphere.Radius * 10.0f); Vec3F camPos = sphere.Center + new Vec3F(sphere.Radius, sphere.Radius, sphere.Radius) * 1.2f; Camera.Set(camPos, sphere.Center, new Vec3F(0, 1, 0)); ViewType = viewtype; ResetCamera = false; } GameEngine.Begin(SurfaceId, Camera.ViewMatrix, Camera.ProjectionMatrix); if (Game.RootGameObjectFolder.GameObjects.Count > 0) { GameEngine.RenderGame(); } string str = "View Type: " + ViewType.ToString(); GameEngine.DrawText2D(str, Util3D.CaptionFont, 1, 1, Color.White); GameEngine.End(); } finally { GameEngine.SetGameLevel(gameLevel); } }
/// <summary> /// Sets active game world.</summary> /// <param name="game">Game world to set</param> public void SetGameWorld(IGame game) { NativeObjectAdapter nobject = game.Cast <NativeObjectAdapter>(); NativeSetGameLevel(nobject.InstanceId); }