Пример #1
0
        internal virtual bool RemoveChild(Cv_SceneNode node)
        {
            Cv_SceneNode toErase = null;

            foreach (var c in Children)
            {
                if (c == node)
                {
                    toErase = c;
                    break;
                }
            }

            if (toErase != null)
            {
                Children.Remove(toErase);
                return(true);
            }

            var removed = false;

            foreach (var c in Children)
            {
                if (c.RemoveChild(node))
                {
                    removed = true;
                    break;
                }
            }

            return(removed);
        }
Пример #2
0
        internal virtual bool RemoveChild(Cv_EntityID entityId)
        {
            Cv_SceneNode toErase = null;

            foreach (var c in Children)
            {
                if (c.Properties.EntityID != Cv_EntityID.INVALID_ENTITY && c.Properties.EntityID == entityId)
                {
                    toErase = c;
                    break;
                }
            }

            if (toErase != null)
            {
                Children.Remove(toErase);
                return(true);
            }

            var removed = false;

            foreach (var c in Children)
            {
                if (c.RemoveChild(entityId))
                {
                    removed = true;
                    break;
                }
            }

            return(removed);
        }
Пример #3
0
        internal virtual bool AddChild(Cv_SceneNode child)
        {
            if (child != null)
            {
                Children.Add(child);
                child.Parent = this;

                return(true);
            }

            return(false);
        }
Пример #4
0
        private void SetNodeTransform(Cv_SceneNode node)
        {
            if (node.Properties.EntityID == Cv_EntityID.INVALID_ENTITY)
            {
                return;
            }

            var entity = Caravel.Logic.GetEntity(node.Properties.EntityID);

            var transformComponent = entity.GetComponent <Cv_TransformComponent>();

            if (transformComponent != null)
            {
                node.Position = transformComponent.Position;
                node.Scale    = transformComponent.Scale;
                node.Origin   = transformComponent.Origin;
                node.Rotation = transformComponent.Rotation;
            }
        }
Пример #5
0
        public Cv_SceneElement(CaravelApp app)
        {
            Caravel = app;
            EditorSelectedEntity = Cv_EntityID.INVALID_ENTITY;
            m_EntitiesMap        = new Dictionary <Cv_EntityID, List <Cv_SceneNode> >();
            m_HolderNodes        = new Dictionary <Cv_EntityID, Cv_HolderNode>();
            m_TransformStack     = new List <Cv_Transform>();
            m_Root = new Cv_HolderNode(Cv_EntityID.INVALID_ENTITY);

            m_Listeners += Cv_EventManager.Instance.AddListener <Cv_Event_NewRenderComponent>(OnNewRenderComponent);
            m_Listeners += Cv_EventManager.Instance.AddListener <Cv_Event_NewCameraComponent>(OnNewCameraComponent);
            m_Listeners += Cv_EventManager.Instance.AddListener <Cv_Event_NewClickableComponent>(OnNewClickableComponent);
            m_Listeners += Cv_EventManager.Instance.AddListener <Cv_Event_DestroyEntity>(OnDestroyEntity);
            m_Listeners += Cv_EventManager.Instance.AddListener <Cv_Event_DestroyCameraComponent>(OnDestroyCameraComponent);
            m_Listeners += Cv_EventManager.Instance.AddListener <Cv_Event_DestroyClickableComponent>(OnDestroyClickableComponent);
            m_Listeners += Cv_EventManager.Instance.AddListener <Cv_Event_DestroyRenderComponent>(OnDestroyRenderComponent);
            m_Listeners += Cv_EventManager.Instance.AddListener <Cv_Event_TransformEntity>(OnMoveEntity);
            m_Listeners += Cv_EventManager.Instance.AddListener <Cv_Event_ModifiedRenderComponent>(OnModifiedRenderComponent);
        }
Пример #6
0
        public bool AddNode(Cv_EntityID entityID, Cv_SceneNode node)
        {
            if (node == null)
            {
                Cv_Debug.Error("Trying to add nonexistant node to scene.");
                return(false);
            }

            if (entityID != Cv_EntityID.INVALID_ENTITY)
            {
                Cv_HolderNode holderNode = null;
                if (m_HolderNodes.TryGetValue(entityID, out holderNode))
                {
                    if (holderNode.Parent.Properties.EntityID != Cv_EntityID.INVALID_ENTITY)                     //holderNode is not direct child of root
                    {
                        Cv_Debug.Error("Cannot have two nodes belonging to the same entity with different parents.");
                        return(false);
                    }

                    m_EntitiesMap[entityID].Add(node);
                }

                if (holderNode == null)
                {
                    var nodes = new List <Cv_SceneNode>();
                    nodes.Add(node);
                    m_EntitiesMap.Add(entityID, nodes);

                    holderNode = new Cv_HolderNode(entityID);
                    m_Root.AddChild(holderNode);
                    m_HolderNodes.Add(entityID, holderNode);
                }

                SetNodeTransform(holderNode);

                return(holderNode.AddChild(node));
            }

            return(m_Root.AddChild(node));
        }
Пример #7
0
        private bool RemoveNode(Cv_SceneNode node)
        {
            if (m_Root.RemoveChild(node))
            {
                m_EntitiesMap[node.Properties.EntityID].Remove(node);
                if (m_EntitiesMap[node.Properties.EntityID].Count <= 0)
                {
                    m_EntitiesMap.Remove(node.Properties.EntityID);

                    if (m_HolderNodes.ContainsKey(node.Properties.EntityID))
                    {
                        var holderNode = m_HolderNodes[node.Properties.EntityID];
                        m_Root.RemoveChild(holderNode);
                        m_HolderNodes.Remove(node.Properties.EntityID);
                    }
                }

                return(true);
            }

            return(false);
        }
Пример #8
0
        public bool AddNodeAsChild(Cv_EntityID parentEntity, Cv_EntityID entityID, Cv_SceneNode node)
        {
            if (node == null)
            {
                Cv_Debug.Error("Trying to add nonexistant node to scene.");
                return(false);
            }

            if (entityID != Cv_EntityID.INVALID_ENTITY)
            {
                Cv_HolderNode ancestorNode = null;

                if (parentEntity == Cv_EntityID.INVALID_ENTITY)
                {
                    return(AddNode(entityID, node));
                }
                else if (!m_HolderNodes.ContainsKey(parentEntity))
                {
                    var currEntity  = Caravel.Logic.GetEntity(parentEntity);
                    var entityStack = new Stack <Cv_Entity>();

                    if (currEntity == null)
                    {
                        return(false);
                    }

                    //Rebuild path to root
                    entityStack.Push(currEntity);
                    while (currEntity.Parent != Cv_EntityID.INVALID_ENTITY)
                    {
                        currEntity = Caravel.Logic.GetEntity(currEntity.Parent);

                        if (m_HolderNodes.ContainsKey(currEntity.ID))
                        {
                            ancestorNode = m_HolderNodes[currEntity.ID];
                            break;
                        }

                        entityStack.Push(currEntity);
                    }

                    //Add all the nodes starting with the closest to the root
                    while (entityStack.Count > 0)
                    {
                        currEntity = entityStack.Pop();

                        var newNodes = new List <Cv_SceneNode>();
                        m_EntitiesMap.Add(currEntity.ID, newNodes);

                        var hNode = new Cv_HolderNode(currEntity.ID);
                        m_HolderNodes.Add(currEntity.ID, hNode);

                        if (ancestorNode != null)
                        {
                            ancestorNode.AddChild(hNode);
                        }
                        else
                        {
                            m_Root.AddChild(hNode);
                        }

                        ancestorNode = hNode;

                        SetNodeTransform(hNode);
                    }
                }
                else
                {
                    ancestorNode = m_HolderNodes[parentEntity];
                }

                if (ancestorNode == null)
                {
                    Cv_Debug.Error("Error while trying to find a parent for the new node.");
                    return(false);
                }

                Cv_HolderNode       holderNode = null;
                List <Cv_SceneNode> nodes      = null;
                if (m_HolderNodes.TryGetValue(entityID, out holderNode))
                {
                    if (holderNode.Parent.Properties.EntityID != parentEntity)
                    {
                        Cv_Debug.Error("Cannot have two nodes belonging to the same entity with different parents.");
                        return(false);
                    }

                    nodes = m_EntitiesMap[entityID];
                }
                else
                {
                    holderNode = new Cv_HolderNode(entityID);
                    ancestorNode.AddChild(holderNode);

                    SetNodeTransform(holderNode);

                    nodes = new List <Cv_SceneNode>();
                    m_EntitiesMap.Add(entityID, nodes);
                    m_HolderNodes.Add(entityID, holderNode);
                }

                nodes.Add(node);
                return(holderNode.AddChild(node));
            }
            else
            {
                if (parentEntity != Cv_EntityID.INVALID_ENTITY)
                {
                    Cv_Debug.Error("Trying to attach a node without entity to a node with an entity.");
                    return(false);
                }

                return(m_Root.AddChild(node));
            }
        }