Esempio n. 1
0
        public void DuplicateSelection()
        {
            var selection = new List <int>();
            var expanded  = new List <int>();

            foreach (var group in GetEntitySelection().GroupBy(n => n.Graph))
            {
                var nodes = group.Key.Duplicate(group.ToList());
                foreach (var node in nodes)
                {
                    selection.Add(GetInstanceId(node));
                    if (node is EntityNode)
                    {
                        var parentNode = (node as EntityNode).Parent;
                        if (null != parentNode)
                        {
                            expanded.Add(GetInstanceId(parentNode));
                        }
                    }
                }
            }
            Selection.instanceIDs = selection.ToArray();
            IdsToExpand           = selection;
            UTinyEventDispatcher <ChangeSource> .Dispatch(ChangeSource.SceneGraph);
        }
        public void Unregister(IRegistryObject t)
        {
            if (t == null)
            {
                return;
            }

            if (!m_Objects.Remove(t.Id))
            {
                return;
            }

            m_UnregisteredObjects.Add(t);

            var type = t.GetType();
            HashSet <UTinyId> typeIds;

            if (m_IdsByType.TryGetValue(type, out typeIds))
            {
                if (typeIds.Remove(t.Id))
                {
                    UTinyEventDispatcher.Dispatch(UTinyRegistryEventType.Unregistered, t);
                }
                if (typeIds.Count == 0)
                {
                    m_IdsByType.Remove(type);
                }
            }
        }
        public void Register(IRegistryObject t)
        {
            Assert.IsNotNull(t);

            IRegistryObject oldObject;

            if (m_Objects.TryGetValue(t.Id, out oldObject))
            {
                if (oldObject == t)
                {
                    return;
                }
                Unregister(oldObject);
            }

            Assert.AreNotEqual(UTinyId.Empty, t.Id);

            m_Objects[t.Id] = t;
            var type = t.GetType();
            HashSet <UTinyId> typeIds;

            if (!m_IdsByType.TryGetValue(type, out typeIds))
            {
                m_IdsByType[type] = typeIds = new HashSet <UTinyId>();
            }

            SetSourceIdentifier(t);

            if (typeIds.Add(t.Id))
            {
                UTinyEventDispatcher.Dispatch(UTinyRegistryEventType.Registered, t);
            }
        }
        private static void HandleCoreTypeRegistered(UTinyRegistryEventType @event, IRegistryObject obj)
        {
            if (!(obj is UTinyType) || null == obj?.Registry)
            {
                return;
            }
            var type = (UTinyType)obj;

            UTinyEventDispatcher.AddListener <UTinyType.Reference, IEnumerable <UTinyEntity> >((UTinyType.Reference)type, ProcessDependency);
        }
Esempio n. 5
0
        public void DeleteSelection()
        {
            var nodes = GetEntitySelection();

            foreach (var node in nodes)
            {
                node.Graph.Delete(node);
            }

            UTinyEventDispatcher <ChangeSource> .Dispatch(ChangeSource.SceneGraph);
        }
        private static void HierarchyChanged()
        {
            var changed = false;

            for (var i = 0; i < SceneManager.sceneCount; ++i)
            {
                var scene = SceneManager.GetSceneAt(i);
                if (!scene.isLoaded || !scene.IsValid())
                {
                    continue;
                }

                var graph = EntityGroupManager.GetSceneGraph(EntityGroupManager?.ActiveEntityGroup ?? UTinyEntityGroup.Reference.None);
                if (null == graph)
                {
                    continue;
                }

                var transforms = scene.GetRootGameObjects()
                                 .SelectMany(root => root.GetComponentsInChildren <Transform>(true))
                                 .NotNull()
                                 // Dealing with prefabs
                                 .Where(t => (t.root.gameObject.hideFlags & HideFlags.HideInHierarchy) != HideFlags.HideInHierarchy)
                                 .GroupBy(t => null == t.GetComponent <UTinyEntityView>());

                foreach (var group in transforms)
                {
                    // Without an entity view
                    // We will try to create an entity from the components of the object.
                    // If we couldn't (or if it is a prefab), we just display a dialog box and delete the GameObjects
                    if (group.Key)
                    {
                        changed = ProcessNewGameObjects(group);
                    }
                    // With an entity view
                    else
                    {
                        foreach (var t in group)
                        {
                            var view = t.GetComponent <UTinyEntityView>();
                            view.DestroyIfUnlinked();
                        }
                    }
                }
            }

            if (changed)
            {
                UTinyEventDispatcher <ChangeSource> .Dispatch(ChangeSource.SceneGraph);
            }
        }
Esempio n. 7
0
        private DragAndDropVisualMode DropOutsideOfItems(IEnumerable <IEntityNode> entities, DragAndDropArgs args)
        {
            var graph = EntityGroupManager.GetSceneGraph(m_EntityGroups.Last());

            graph.Add(entities.ToList());

            var ids = AsInstanceIds(entities);

            Selection.instanceIDs = ids;
            IdsToExpand           = ids;
            UTinyEventDispatcher <ChangeSource> .Dispatch(ChangeSource.SceneGraph);

            return(DragAndDropVisualMode.Link);
        }
Esempio n. 8
0
        protected override void OnItemClicked(UTinyType type)
        {
            var module = ValueToModules[type];

            foreach (var entity in Entities)
            {
                entity.GetOrAddComponent((UTinyType.Reference)type);
            }

            if (!IsIncluded(module))
            {
                Debug.Log($"{UTinyConstants.ApplicationName}: The '{module.Name}' module was included to the project because the '{type.Name}' component was added to an entity.");
            }
            MainModule.AddExplicitModuleDependency((UTinyModule.Reference)module);
            // This is called manually because we want the scene graphs to be recreated.
            UTinyEventDispatcher <ChangeSource> .Dispatch(ChangeSource.DataModel);
        }
Esempio n. 9
0
        private DragAndDropVisualMode HandleDropBetweenItems(IEnumerable <IEntityNode> entities, DragAndDropArgs args)
        {
            var parentItem = args.parentItem as HierarchyTreeItemBase;
            DropBetweenAction method;

            if (DroppedBetweenMethod.TryGetValue(parentItem.GetType(), out method))
            {
                method(parentItem, entities, args.insertAtIndex);
                var ids = AsInstanceIds(entities);
                Selection.instanceIDs = ids;
                IdsToExpand           = ids;
                UTinyEventDispatcher <ChangeSource> .Dispatch(ChangeSource.SceneGraph);

                return(DragAndDropVisualMode.Link);
            }
            return(DragAndDropVisualMode.Rejected);
        }
Esempio n. 10
0
        private bool AddToEntity(UTinyEntity.Reference entityRef, Sprite sprite, bool runBindings)
        {
            var entity = entityRef.Dereference(m_Context.Registry);

            entity.Name = sprite?.name ?? "NullSprite";

            var renderer = entity.GetOrAddComponent(Registry.GetSprite2DRendererType());

            renderer["sprite"] = sprite;

            if (runBindings)
            {
                BindingsHelper.RunAllBindings(entity);
            }

            UTinyEventDispatcher <ChangeSource> .Dispatch(ChangeSource.SceneGraph);

            return(true);
        }
Esempio n. 11
0
        public UTinyEntity.Reference CreateEntity(UTinyEntityGroup.Reference entityGroupRef, bool addTransform)
        {
            var graph = EntityGroupManager.GetSceneGraph(entityGroupRef);

            if (null == graph)
            {
                return(UTinyEntity.Reference.None);
            }
            var node = addTransform ? graph.Create() : graph.CreateStatic();

            var ids = AsInstanceIds(node);

            Selection.instanceIDs = ids;
            IdsToExpand           = ids;

            UTinyEventDispatcher <ChangeSource> .Dispatch(ChangeSource.SceneGraph);

            return(node.Entity);
        }
Esempio n. 12
0
        private bool AddToEntity(UTinyEntity.Reference entityRef, Font font, bool runBindings)
        {
            var entity = entityRef.Dereference(m_Context.Registry);

            entity.Name = font.name;

            var renderer = entity.GetOrAddComponent(Registry.GetTextRendererType());

            renderer["text"] = "Sample Text";
            renderer["font"] = font;

            if (runBindings)
            {
                BindingsHelper.RunAllBindings(entity);
            }

            UTinyEventDispatcher <ChangeSource> .Dispatch(ChangeSource.SceneGraph);

            return(true);
        }
Esempio n. 13
0
        private bool AddToEntity(UTinyEntity.Reference entityRef, AudioClip audioClip, bool runBindings)
        {
            var entity = entityRef.Dereference(m_Context.Registry);

            entity.Name = audioClip.name;

            var audioSource = entity.GetOrAddComponent(Registry.GetAudioSourceType());

            audioSource.Refresh();
            audioSource["clip"]   = audioClip;
            audioSource["volume"] = 1.0f;

            if (runBindings)
            {
                BindingsHelper.RunAllBindings(entity);
            }
            UTinyEventDispatcher <ChangeSource> .Dispatch(ChangeSource.SceneGraph);

            return(true);
        }
        private static void Unregister(UTinyEntityView view)
        {
            if (!ActiveViews.Remove(view))
            {
                return;
            }

            if (ActiveViews.Count == 0)
            {
                Undo.postprocessModifications -= HandlePostProcessModification;
            }

            // From this point, we know that the entity view is being destroyed. What we do not know is if the view is
            // being destroyed because we are unloading the scene or if the user deleted the entity through the hierarchy
            // or the scene view.
            var entity = view.EntityRef.Dereference(Registry);

            if (entity?.EntityGroup == null)
            {
                return;
            }

            var entityGroupRef = (UTinyEntityGroup.Reference)entity.EntityGroup;

            if (UnloadingEntityGroups.Contains(entityGroupRef))
            {
                return;
            }

            entity.View = null;

            var graph = EntityGroupManager.GetSceneGraph(entityGroupRef);

            if (null == graph)
            {
                return;
            }

            graph.Delete(graph.FindNode(view.EntityRef));
            UTinyEventDispatcher <ChangeSource> .Dispatch(ChangeSource.SceneGraph);
        }
Esempio n. 15
0
        private bool AddToEntity(UTinyEntity.Reference entityRef, Texture2D texture, bool runBindings)
        {
            var entity = entityRef.Dereference(m_Context.Registry);

            entity.Name = texture.name;

            var image2d = entity.GetOrAddComponent(Registry.GetImage2DType());

            image2d.Refresh();
            image2d["imageFile"] = texture;

            // @TODO Pull from texture importer
            image2d["pixelsToWorldUnits"] = 1.0f;

            if (runBindings)
            {
                BindingsHelper.RunAllBindings(entity);
            }
            UTinyEventDispatcher <ChangeSource> .Dispatch(ChangeSource.SceneGraph);

            return(true);
        }
Esempio n. 16
0
        protected override void OnComplete()
        {
            foreach (var type in s_Bindings)
            {
                var typeRef = (UTinyType.Reference)type;

                if (type.HasAttribute <BindingsAttribute>())
                {
                    foreach (var entity in Targets.OfType <UTinyEntity>())
                    {
                        var component = entity.GetComponent(typeRef);
                        if (null == component)
                        {
                            continue;
                        }
                        type.GetAttribute <BindingsAttribute>().Binding.Run(BindingTiming.OnUpdateBindings, entity, component);
                    }
                }

                UTinyEventDispatcher.Dispatch(typeRef, Targets.OfType <UTinyEntity>());
            }
        }
        private void ShowRemoveComponent(UTinyType.Reference typeRef)
        {
            var type = typeRef.Dereference(Registry);

            if ((null == type || type.TypeCode == UTinyTypeCode.Component) && TargetType == typeof(UTinyEntity))
            {
                var rect = EditorGUILayout.GetControlRect(false, GUILayout.Width(16.0f));
                if (GUI.Button(rect, UTinyIcons.X_Icon_8, UTinyStyles.MiddleCenteredLabel))
                {
                    var targets = Targets.Cast <UTinyEntity>().ToList();
                    EditorApplication.delayCall += () =>
                    {
                        foreach (var entity in targets.Cast <UTinyEntity>())
                        {
                            entity.RemoveComponent(typeRef);
                        }
                        // This is called manually because we want the scene graphs to be recreated.
                        UTinyEventDispatcher <ChangeSource> .Dispatch(ChangeSource.DataModel);
                    };
                }
                GUILayout.Space(5.0f);
            }
        }
Esempio n. 18
0
 private static void Register()
 {
     UTinyEventDispatcher.AddListener <UTinyRegistryEventType, IRegistryObject>(UTinyRegistryEventType.Registered, HandleCoreTypeRegistered);
 }
 public static void Dispatch<TEvent, TValue>(TEvent type, TValue @event)
     where TEvent : struct
     where TValue : class
 {
     UTinyEventDispatcher<TEvent>.Dispatch(type, @event);
 }
 public static void RemoveListener<TEvent, TValue>(TEvent type, UTinyEventHandler<TEvent, TValue> @event)
     where TEvent : struct
     where TValue : class
 {
     UTinyEventDispatcher<TEvent>.RemoveListener(type, @event);
 }
Esempio n. 21
0
        private static void RegisterChangeHandlers()
        {
            UTinyEventDispatcher <ChangeSource> .AddListener <object>(ChangeSource.DataModel, HandleDataModelChange);

            UTinyEventDispatcher <ChangeSource> .AddListener <object>(ChangeSource.SceneGraph, HandleSceneGraphChange);
        }