Esempio n. 1
0
        public static void Trigger <TArgs>(EventHook hook, TArgs args)
        {
            HashSet <Action <TArgs> > handlers = null;

            if (events.TryGetValue(hook, out var potentialHandlers))
            {
                foreach (var potentialHandler in potentialHandlers)
                {
                    if (potentialHandler is Action <TArgs> handler)
                    {
                        if (handlers == null)
                        {
                            handlers = HashSetPool <Action <TArgs> > .New();
                        }

                        handlers.Add(handler);
                    }
                }
            }

            if (handlers != null)
            {
                foreach (var handler in handlers)
                {
                    if (!potentialHandlers.Contains(handler))
                    {
                        continue;
                    }

                    handler.Invoke(args);
                }

                handlers.Free();
            }
        }
Esempio n. 2
0
        public static HashSet<T> ToHashSetPooled<T>(this IEnumerable<T> source)
        {
            var hashSet = HashSetPool<T>.New();

            foreach (var item in source)
            {
                hashSet.Add(item);
            }

            return hashSet;
        }
Esempio n. 3
0
        public static IEnumerable <T> OrderByDependencies <T>(this IEnumerable <T> source, Func <T, IEnumerable <T> > getDependencies, bool throwOnCycle = true)
        {
            var sorted  = new List <T>();
            var visited = HashSetPool <T> .New();

            foreach (var item in source)
            {
                OrderByDependenciesVisit(item, visited, sorted, getDependencies, throwOnCycle);
            }

            HashSetPool <T> .Free(visited);

            return(sorted);
        }
Esempio n. 4
0
        // Active state detection happens twice:
        //
        // 1. Before the enumeration, because any state
        //    that becomes active during an update shouldn't
        //    be updated until the next update
        //
        // 2. Inside the update method, because a state
        //    that was active during enumeration and no longer
        //    is shouldn't be updated.

        private HashSet <IState> GetActiveStatesNoAlloc(GraphPointer pointer)
        {
            var activeStates = HashSetPool <IState> .New();

            foreach (var state in states)
            {
                var stateData = pointer.GetElementData <State.Data>(state);

                if (stateData.isActive)
                {
                    activeStates.Add(state);
                }
            }

            return(activeStates);
        }
        public static HashSet <GraphReference> ChildrenOfPooled(IGraphParent parent)
        {
            Ensure.That(nameof(parent)).IsNotNull(parent);

            lock (@lock)
            {
                if (byParent.TryGetValue(parent, out var instances))
                {
                    // Debug.Log($"Found {instances.Count} instances of {parent.ToSafeString()}\n{instances.ToLineSeparatedString()}");

                    return(instances.ToHashSetPooled());
                }
                else
                {
                    // Debug.Log($"Found no instances of {parent.ToSafeString()}.\n");

                    return(HashSetPool <GraphReference> .New());
                }
            }
        }
        public static HashSet <GraphReference> OfPooled(IGraph graph)
        {
            Ensure.That(nameof(graph)).IsNotNull(graph);

            lock (@lock)
            {
                if (byGraph.TryGetValue(graph, out var instances))
                {
                    // Debug.Log($"Found {instances.Count} instances of {graph}\n{instances.ToLineSeparatedString()}");

                    return(instances.ToHashSetPooled());
                }
                else
                {
                    // Debug.Log($"Found no instances of {graph}.\n");

                    return(HashSetPool <GraphReference> .New());
                }
            }
        }
        private void DrawHandle()
        {
            // Trying to be very speed / memory efficient in this method

            if (!e.IsRepaint)
            {
                return;
            }

            var color = Color.white;

            var highlight = false;

            var invalid = false;

            var willDisconnect = false;

            var connections = HashSetPool <IUnitConnection> .New();

            GetConnectionsNoAlloc(connections);

            var isConnected = connections.Count > 0;

            if (isConnected)
            {
                foreach (var connection in connections)
                {
                    if (connection is InvalidConnection)
                    {
                        invalid = true;
                    }

                    var sourceWidget      = canvas.Widget <IUnitPortWidget>(connection.source);
                    var destinationWidget = canvas.Widget <IUnitPortWidget>(connection.destination);

                    if (sourceWidget.isMouseOver || destinationWidget.isMouseOver)
                    {
                        highlight = true;
                    }

                    if (sourceWidget.willDisconnect || destinationWidget.willDisconnect)
                    {
                        willDisconnect = true;
                    }
                }
            }

            if (isMouseOver)
            {
                highlight = true;
            }

            if (willDisconnect)
            {
                color = UnitConnectionStyles.disconnectColor;
            }
            else if (highlight)
            {
                color = UnitConnectionStyles.highlightColor;
            }
            else if (invalid)
            {
                color = UnitConnectionStyles.invalidColor;
            }
            else if (canvas.isCreatingConnection && (canvas.connectionSource == port || canvas.connectionSource.CanValidlyConnectTo(port)))
            {
                color = this.color;
            }
            else if (isConnected)
            {
                Color?resolvedColor = null;

                foreach (var connection in connections)
                {
                    var connectionColor = canvas.Widget <IUnitConnectionWidget>(connection).color;

                    if (resolvedColor == null)
                    {
                        resolvedColor = connectionColor;
                    }
                    else if (resolvedColor != connectionColor)
                    {
                        resolvedColor = this.color;

                        break;
                    }
                }

                color = resolvedColor.Value;
            }

            if (colorIfActive)
            {
                foreach (var connection in connections)
                {
                    var connectionEditorData = reference.GetElementDebugData <IUnitConnectionDebugData>(connection);

                    if (EditorApplication.isPaused)
                    {
                        if (EditorTimeBinding.frame == connectionEditorData.lastInvokeFrame)
                        {
                            color = UnitConnectionStyles.activeColor;

                            break;
                        }
                    }
                    else
                    {
                        color = Color.Lerp(UnitConnectionStyles.activeColor, color, (EditorTimeBinding.time - connectionEditorData.lastInvokeTime) / UnitWidget <IUnit> .Styles.invokeFadeDuration);
                    }
                }
            }

            var handlePosition = this.handlePosition;

            if (highlight)
            {
                var widthExpansion  = handlePosition.width * (Styles.highlightScaling - 1);
                var heightExpansion = handlePosition.height * (Styles.highlightScaling - 1);
                handlePosition.width  += widthExpansion;
                handlePosition.height += heightExpansion;
                handlePosition.x      -= widthExpansion / 2;
                handlePosition.y      -= heightExpansion / 2;
            }

            if (highlight ||
                isConnected ||
                canvas.connectionSource == port ||
                canvas.isCreatingConnection && canvas.connectionSource.CanValidlyConnectTo(port))
            {
                using (LudiqGUI.color.Override(color.WithAlphaMultiplied(LudiqGUI.color.value.a * 0.85f))) // Full color is a bit hard on the eyes
                {
                    if (handleTextureConnected != null)
                    {
                        GUI.DrawTexture(handlePosition, handleTextureConnected);
                    }
                }
            }
            else
            {
                if (handleTextureUnconnected != null)
                {
                    GUI.DrawTexture(handlePosition, handleTextureUnconnected);
                }
            }

            HashSetPool <IUnitConnection> .Free(connections);
        }