Пример #1
0
        /// <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;
            }
        }
Пример #2
0
        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);
        }
Пример #3
0
        /// <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);
        }
Пример #4
0
        /// <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);
        }
Пример #5
0
        public void Resolve(UniqueNamer namer)
        {
            try
            {
                m_updating = true;
                Uri resUri = GetAttribute <Uri>(Schema.prefabInstanceType.prefabRefAttribute);
                if (resUri != null)
                {
                    m_prefab = Globals.ResourceService.Load(resUri) as IPrefab;
                }
                if (m_prefab == null)
                {
                    return;
                }

                // update name and uri
                if (resUri == null)
                {
                    SetAttribute(Schema.prefabInstanceType.prefabRefAttribute, m_prefab.Uri);
                }

                IGameObjectGroup gobgroup = DomNode.As <IGameObjectGroup>();
                if (string.IsNullOrWhiteSpace(gobgroup.Name))
                {
                    gobgroup.Name = "PrefabInst_" + m_prefab.Name;
                }

                DomNode[]        gobs   = DomNode.Copy(m_prefab.GameObjects.AsIEnumerable <DomNode>());
                HashSet <string> gobIds = new HashSet <string>();

                gobgroup.GameObjects.Clear();
                foreach (var gobNode in gobs)
                {
                    gobNode.InitializeExtensions();
                    IGameObject gob = gobNode.As <IGameObject>();
                    m_intsToOriginal.Add(gobNode, gob.Name);
                    gobIds.Add(gob.Name);
                    ObjectOverride objectOverride;
                    m_overridesMap.TryGetValue(gob.Name, out objectOverride);
                    updateNode(gobNode, objectOverride);
                    string name = gob.Name;
                    if (namer != null)
                    {
                        gob.Name = namer.Name(gob.Name);
                    }

                    gobgroup.GameObjects.Add(gob);
                }

                // cleanup m_overridesmap
                List <string> overrideIds = new List <string>(m_overridesMap.Keys);
                foreach (string id in overrideIds)
                {
                    if (!gobIds.Contains(id))
                    {
                        ObjectOverride objectOverride = m_overridesMap[id];
                        m_overridesMap.Remove(id);
                        m_overrideList.Remove(objectOverride);
                    }
                }
            }
            finally
            {
                m_updating = false;
            }
        }