/// <summary> /// Does the specified command</summary> /// <param name="commandTag">Command</param> void ICommandClient.DoCommand(object commandTag) { ITransactionContext transactionContext = m_selectionContext.As <ITransactionContext>(); if (transactionContext == null) { return; } switch ((StandardCommand)commandTag) { case StandardCommand.EditGroup: transactionContext.DoTransaction(delegate { IGameObjectGroup group = Group(SelectedGobs); m_selectionContext.Set(Util.AdaptDomPath(group.As <DomNode>())); }, "Group".Localize()); break; case StandardCommand.EditUngroup: transactionContext.DoTransaction(delegate { IEnumerable <IGameObject> gobs = Ungroup(SelectedGobs); List <object> newselection = new List <object>(); foreach (var gob in gobs) { newselection.Add(Util.AdaptDomPath(gob.As <DomNode>())); } m_selectionContext.SetRange(newselection); }, "Ungroup".Localize()); break; } }
private DomNode CreatePrototype(IEnumerable <IGameObject> gobs) { DomNode[] originals = new DomNode[1]; List <IGameObject> copyList = new List <IGameObject>(); AABB bound = new AABB(); foreach (IGameObject gameObject in SelectedGobs) { IBoundable boundable = gameObject.As <IBoundable>(); bound.Extend(boundable.BoundingBox); Matrix4F world = TransformUtils.ComputeWorldTransform(gameObject); originals[0] = gameObject.As <DomNode>(); DomNode[] copies = DomNode.Copy(originals); IGameObject copy = copies[0].As <IGameObject>(); TransformUtils.SetTransform(copy, world); copyList.Add(copy); } DomNode gobchild = null; if (copyList.Count > 1) {// create group IGame game = m_contextRegistry.GetActiveContext <IGame>(); IGameObjectGroup gobgroup = game.CreateGameObjectGroup(); gobgroup.Translation = bound.Center; gobgroup.UpdateTransform(); Matrix4F worldInv = new Matrix4F(); worldInv.Invert(gobgroup.Transform); foreach (IGameObject gob in copyList) { Vec3F translate = gob.Translation; worldInv.Transform(ref translate); gob.Translation = translate; gob.UpdateTransform(); gobgroup.GameObjects.Add(gob); } gobchild = gobgroup.As <DomNode>(); } else { gobchild = copyList[0].As <DomNode>(); } gobchild.InitializeExtensions(); gobchild.As <IGameObject>().Translation = new Vec3F(0, 0, 0); DomNode prototype = null; if (gobchild != null) { prototype = new DomNode(Schema.prototypeType.Type, Schema.prototypeRootElement); prototype.SetChild(Schema.prototypeType.gameObjectChild, gobchild); } return(prototype); }
/// <summary> /// Groups the specified GameObjects</summary> /// <param name="gobs">GameObjects to be grouped</param> /// <remarks>Creates a new GameObjectGroup and moves all /// the GameObjects into it.</remarks> public IGameObjectGroup Group(IEnumerable <IGameObject> gobs) { // extra check. if (!CanGroup(gobs)) { return(null); } IGame game = null; AABB groupBox = new AABB(); List <IGameObject> gameObjects = new List <IGameObject>(); foreach (IGameObject gameObject in gobs) { if (game == null) { game = gameObject.As <DomNode>().GetRoot().As <IGame>(); } gameObjects.Add(gameObject); IBoundable boundable = gameObject.As <IBoundable>(); groupBox.Extend(boundable.BoundingBox); } IGameObjectGroup group = game.CreateGameObjectGroup(); DomNode node = group.As <DomNode>(); node.InitializeExtensions(); ITransformable transformable = node.As <ITransformable>(); transformable.Translation = groupBox.Center; Matrix4F invWorld = new Matrix4F(); invWorld.Invert(transformable.Transform); game.RootGameObjectFolder.GameObjects.Add(group); foreach (IGameObject gameObject in gameObjects) { ITransformable xformable = gameObject.As <ITransformable>(); Matrix4F world = ComputeWorldTransform(xformable); SetTransform(xformable, world); group.GameObjects.Add(gameObject); Vec3F trans = world.Translation; invWorld.Transform(ref trans); xformable.Translation = trans; } return(group); }
/// <summary> /// Ungroups the specified gameobjects.</summary> public IEnumerable <IGameObject> Ungroup(IEnumerable <IGameObject> gobs) { if (!CanUngroup(gobs)) { return(EmptyArray <IGameObject> .Instance); } List <IGameObject> ungrouplist = new List <IGameObject>(); List <IGameObject> goblist = new List <IGameObject>(gobs); foreach (IGameObject gameObject in goblist) { // Ungroup selected groups IGameObjectGroup group = gameObject.As <IGameObjectGroup>(); if (group == null) { continue; } List <IGameObject> childList = new List <IGameObject>(group.GameObjects); IGame game = group.As <DomNode>().GetRoot().As <IGame>(); // Move children to the root game object folder foreach (IGameObject child in childList) { ITransformable xformChild = child.As <ITransformable>(); Matrix4F world = ComputeWorldTransform(xformChild); game.RootGameObjectFolder.GameObjects.Add(child); SetTransform(xformChild, world); ungrouplist.Add(child); } // Remove group group.Cast <DomNode>().RemoveFromParent(); } return(ungrouplist); }