Example #1
0
        public EntityNode CreateFromExisting(Transform t, Transform parent)
        {
            var entity = Registry.CreateEntity(
                new UTinyId(System.Guid.NewGuid()),
                t.name);

            var entityRef      = (UTinyEntity.Reference)entity;
            var entityGroupRef = EntityGroupRef.Dereference(Registry);

            Assert.IsNotNull(entityGroupRef);
            entity.EntityGroup = entityGroupRef;
            entityGroupRef.AddEntityReference(entityRef);

            entity.AddComponent(Registry.GetTransformType());
            entity.View = t.GetComponent <UTinyEntityView>();

            CreateLink(entity);
            var node = new EntityNode {
                Entity = entityRef, Graph = this
            };

            node.Graph.Add(node);

            if (parent)
            {
                var parentview = parent.GetComponent <UTinyEntityView>();
                Assert.IsNotNull(parentview);
                var parentNode = FindNode((UTinyEntity.Reference)parentview.EntityRef.Dereference(parentview.Registry));
                node.SetParent(parentNode as EntityNode);
            }

            return(node);
        }
Example #2
0
        public EntityNode Create(EntityNode parent = null)
        {
            Assert.IsTrue(null == parent || this == parent.Graph);

            var entity = Registry.CreateEntity(
                new UTinyId(System.Guid.NewGuid()),
                GetUniqueName((parent?.Children.Select(c => c.Entity) ?? Roots.Select(r => r.Entity)), "Entity"));

            var entityRef = (UTinyEntity.Reference)entity;

            var entityGroup = EntityGroupRef.Dereference(Registry);

            Assert.IsNotNull(entityGroup);
            entity.EntityGroup = entityGroup;
            entityGroup.AddEntityReference(entityRef);

            entity.AddComponent(Registry.GetTransformType());
            var view = CreateLink(entity);

            if (null != parent)
            {
                view.transform.SetParent(parent.Entity.Dereference(Registry).View.transform, false);
            }

            var node = new EntityNode {
                Entity = entityRef, Graph = this
            };

            node.Graph.Add(node, parent);
            return(node);
        }
        private void BuildFromNode(EntityNode node, HierarchyTreeItemBase parentItem)
        {
            var entity = node.Entity.Dereference(m_Context.Registry);
            var item   = new HierarchyEntity
            {
                Value       = node,
                id          = GetInstanceId(node),
                depth       = parentItem.depth + 1,
                displayName = entity.Name
            };

            if (entity.Name.IndexOf(FilterString, StringComparison.OrdinalIgnoreCase) >= 0)
            {
                parentItem.AddChild(item);
            }

            foreach (var child in node.Children)
            {
                if (string.IsNullOrEmpty(m_FilterString))
                {
                    BuildFromNode(child, item);
                }
                else
                {
                    BuildFromNode(child, parentItem);
                }
            }
        }
Example #4
0
 private void SetAsCurrent(EntityNode node)
 {
     node.Graph = this;
     foreach (var child in node.Children)
     {
         SetAsCurrent(child);
     }
 }
Example #5
0
        private EntityNode FindRecursive(EntityNode node, UTinyEntity.Reference entity)
        {
            if (node.Entity.Equals(entity))
            {
                return(node);
            }

            return(node.Children.Select(r => FindRecursive(r, entity)).NotNull().FirstOrDefault());
        }
Example #6
0
 private static void InsertInList(int index, List <EntityNode> list, EntityNode node)
 {
     list.Remove(node);
     if (index < 0 || index >= list.Count)
     {
         list.Add(node);
     }
     else
     {
         list.Insert(index, node);
     }
 }
Example #7
0
 public void Insert(int siblingIndex, List <EntityNode> nodes, EntityNode parent = null)
 {
     foreach (var node in nodes)
     {
         if (node.IsAncestorOrParentOf(parent))
         {
             continue;
         }
         Insert(siblingIndex, node, parent);
         siblingIndex += siblingIndex < 0 ? 0 : 1;
     }
 }
Example #8
0
        private static bool IsChildOfAny(EntityNode child, List <IEntityNode> candidates)
        {
            var parents = new List <EntityNode>();
            var parent  = child.Parent;

            while (null != parent)
            {
                parents.Add(parent);
                parent = parent.Parent;
            }

            return(parents.Intersect(candidates.Where(c => c is EntityNode).Cast <EntityNode>()).Any());
        }
Example #9
0
        public void SyncFromUTiny()
        {
            Roots.Clear();
            StaticEntities.Clear();

            var cache = new Dictionary <UTinyEntity, EntityNode>();

            var entityGroup = EntityGroupRef.Dereference(Registry);

            Assert.IsNotNull(entityGroup);

            foreach (var entityRef in entityGroup.Entities)
            {
                var entity = entityRef.Dereference(Registry);

                if (null == entity)
                {
                    Debug.LogWarning($"SceneGraph failed to load entity Name=[{entityRef.Name}] Id=[{entityRef.Id}]");
                    continue;
                }

                if (entity.HasTransform())
                {
                    var node = new EntityNode()
                    {
                        Entity = entityRef, Graph = this
                    };
                    cache[entity] = node;

                    EntityNode parentNode = null;
                    var        parent     = entity.Parent();
                    if (UTinyEntity.Reference.None.Id == parent.Id)
                    {
                        Roots.Add(node);
                        continue;
                    }

                    if (cache.TryGetValue(parent.Dereference(Registry), out parentNode))
                    {
                        node.Parent = parentNode;
                        parentNode.Children.Add(node);
                    }
                }
                else
                {
                    StaticEntities.Add(new StaticEntityNode {
                        Entity = entityRef, Graph = this
                    });
                }
            }
        }
Example #10
0
        private void UpdateTransform(EntityNode node, EntityNode parent)
        {
            var entity        = node.Entity.Dereference(Registry);
            var transform     = entity.View.transform;
            var tinyTransform = entity.GetComponent(Registry.GetTransformType());

            if (null == parent)
            {
                transform.SetParent(null, true);
            }
            else
            {
                transform.SetParent(parent.Entity.Dereference(Registry).View.transform, true);
            }
            TransformInversedBindings.SyncTransform(transform, tinyTransform);
        }
Example #11
0
        private void RemoveFromGraph(List <EntityNode> inspect, EntityNode toRemove, bool unlink = false)
        {
            if (inspect.Remove(toRemove))
            {
                if (unlink)
                {
                    DeleteLink(toRemove.Entity.Dereference(Registry));
                }
                return;
            }

            foreach (var node in inspect)
            {
                RemoveFromGraph(node.Children, toRemove, unlink);
            }
        }
        public bool IsAncestorOrParentOf(EntityNode node)
        {
            if (this == node)
            {
                return(true);
            }

            foreach (var child in Children)
            {
                if (child == node || child.IsAncestorOrParentOf(node))
                {
                    return(true);
                }
            }
            return(false);
        }
Example #13
0
        private void PopulateChildren(EntityNode parent, HashSet <UTinyEntity.Reference> cache)
        {
            for (int i = 0; i < parent.Children.Count; ++i)
            {
                var child  = parent.Children[i];
                var entity = child.Entity.Dereference(Registry);
                CreateLink(entity);
                var view = entity.View;
                view.gameObject.name = child.Entity.Name;
                cache.Add(child.Entity);

                view.transform.SetParent(parent.Entity.Dereference(Registry).View.transform, true);
                view.transform.SetSiblingIndex(i);
                PopulateChildren(child, cache);
            }
        }
Example #14
0
        private EntityNode CreateNodes(EntityNode source, EntityNode parent)
        {
            var node         = Create(parent);
            var entity       = node.Entity.Dereference(Registry);
            var sourceEntity = source.Entity.Dereference(Registry);

            entity.Name    = sourceEntity.Name;
            entity.Layer   = sourceEntity.Layer;
            entity.Enabled = sourceEntity.Enabled;

            foreach (var child in source.Children)
            {
                CreateNodes(child, node);
            }

            return(node);
        }
Example #15
0
        private void PopulateFromNode(EntityNode node, List <UTinyEntity.Reference> list)
        {
            var entity = node.Entity.Dereference(Registry);

            list.Add(node.Entity);
            if (null == node.Parent)
            {
                entity.SetParent(UTinyEntity.Reference.None);
            }
            else
            {
                entity.SetParent(node.Parent.Entity);
            }

            foreach (var child in node.Children)
            {
                PopulateFromNode(child, list);
            }
        }
        public void SetParent(int siblingIndex, EntityNode parent)
        {
            // Cannot SetParent on a node that is part of the children.
            if (IsAncestorOrParentOf(parent))
            {
                return;
            }

            // We defer the operations to the owning graph, since the current node and the parent node may not be in the
            // same graph.
            if (null == parent)
            {
                Graph.Insert(siblingIndex, this);
            }
            else
            {
                parent.Graph.Insert(siblingIndex, this, parent);
            }
        }
Example #17
0
        public void Insert(int siblingIndex, EntityNode node, EntityNode parent = null)
        {
            if (node.IsAncestorOrParentOf(parent))
            {
                return;
            }

            if (this != node.Graph)
            {
                // Remove from old Graph
                node.Graph.RemoveFromGraph(node.Graph.Roots, node);
                SetAsCurrent(node);
            }

            // Remove from previous parent
            if (null != node.Parent)
            {
                node.Parent.Children.Remove(node);
            }
            else
            {
                Roots.Remove(node);
            }

            node.Parent = parent;
            UpdateTransform(node, parent);
            if (null == node.Parent)
            {
                // Add as root
                InsertInList(siblingIndex, Roots, node);
            }
            else
            {
                InsertInList(siblingIndex, node.Parent.Children, node);
            }
        }
 public bool IsChildrenOf(EntityNode parent)
 {
     return(null != parent && parent.Children.Contains(this));
 }
Example #19
0
 public void Delete(EntityNode node)
 {
     Assert.IsTrue(this == node.Graph);
     RemoveFromGraph(Roots, node, true);
     Unregister(node);
 }
Example #20
0
 public void Add(EntityNode node)
 {
     Insert(-1, node, null);
 }
 public void Insert(int siblingIndex, EntityNode child)
 {
     child.SetParent(siblingIndex, this);
 }
Example #22
0
 public void Add(EntityNode node, EntityNode parent)
 {
     Insert(-1, node, parent);
 }
 public void Add(EntityNode child)
 {
     child.SetParent(this);
 }
        private DragAndDropVisualMode HandleResourceDropped(Object obj, DragAndDropArgs args)
        {
            var        parent     = args.parentItem as HierarchyTreeItemBase;
            EntityNode entityNode = null;

            switch (args.dragAndDropPosition)
            {
            case DragAndDropPosition.UponItem:
            {
                if (parent is HierarchyEntityGroupGraph)
                {
                    var graph = (parent as HierarchyEntityGroupGraph).Value;
                    entityNode = graph.Create();
                }

                if (parent is HierarchyEntity)
                {
                    var node = (parent as HierarchyEntity).Value;
                    entityNode = node.Graph.Create(node);
                }
                // Set as last non-static sibling.
                else if (parent is HierarchyStaticEntity)
                {
                    var node = (parent as HierarchyStaticEntity).Value;
                    entityNode = node.Graph.Create();
                }
            }
            break;

            case DragAndDropPosition.BetweenItems:
            {
                if (rootItem == parent)
                {
                    if (args.insertAtIndex <= 0)
                    {
                        return(DragAndDropVisualMode.Rejected);
                    }

                    var graph = (parent.children[args.insertAtIndex - 1] as HierarchyEntityGroupGraph).Value;
                    entityNode = graph.Create();
                }
                else if (parent is HierarchyEntityGroupGraph)
                {
                    var groupItem = parent as HierarchyEntityGroupGraph;
                    var graph     = groupItem.Value;

                    var index = (args.insertAtIndex >= parent.children.Count || args.insertAtIndex >= graph.Roots.Count) ? -1 : args.insertAtIndex;

                    entityNode = graph.Create();
                    graph.Insert(index, entityNode);
                }
                else if (parent is HierarchyEntity)
                {
                    var parentNode = (parent as HierarchyEntity).Value;
                    var firstIndex = args.insertAtIndex;
                    entityNode = parentNode.Graph.Create();
                    entityNode.SetParent(firstIndex, parentNode);
                }
                // Between static entities, set as last sibling of non-static entities
                else if (parent is HierarchyStaticEntity)
                {
                    var graph = (parent as HierarchyStaticEntity).Value.Graph;
                    entityNode = graph.Create();
                }
            }
            break;

            case DragAndDropPosition.OutsideItems:
            {
                var graph = UTinyHierarchyWindow.GetSceneGraph(m_EntityGroups.Last());
                entityNode = graph.Create();
            }
            break;

            default:
            {
                return(DragAndDropVisualMode.Rejected);
            }
            }


            if (!UTinyEntity.Reference.None.Equals(entityNode.Entity))
            {
                AddToEntity(entityNode.Entity, obj);
                var ids = AsInstanceIds(entityNode);
                Selection.instanceIDs = ids;
                IdsToExpand           = ids;
                return(DragAndDropVisualMode.Link);
            }
            return(DragAndDropVisualMode.Rejected);
        }
 public void SetParent(EntityNode parent)
 {
     SetParent(-1, parent);
 }