Exemple #1
0
    private static void RegisterInstanceListener(int priority, EventComponent target, EventCallback callback)
    {
        SortedList <int, List <EventCallback> > instance;

        // See if we we have any events registered to this target already
        if (InstanceListeners.TryGetValue(target, out instance))
        {
            List <EventCallback> listeners;

            // Check if there are any events registered to this priority already.
            if (instance.TryGetValue(priority, out listeners))
            {
                listeners.Add(callback);
            }
            else
            {
                instance.Add(priority, new List <EventCallback> {
                    callback
                });
            }
        }
        else
        {
            // If there isn't. We need to create all the collections and add this to the instance listeners.
            instance = new SortedList <int, List <EventCallback> > {
                { priority, new List <EventCallback> {
                      callback
                  } }
            };

            InstanceListeners.Add(target, instance);
        }
    }
Exemple #2
0
    private static void RemoveInstanceListener(int priority, EventComponent target, EventCallback callback)
    {
        SortedList <int, List <EventCallback> > instance;

        // See if we we have any events registered to this target already
        if (InstanceListeners.TryGetValue(target, out instance))
        {
            List <EventCallback> listeners;
            if (instance.TryGetValue(priority, out listeners))
            {
                listeners.Remove(callback);

                // Remove this priority if there are no more listeners
                if (listeners == null || listeners.Count == 0)
                {
                    GlobalListeners.RemoveAt(priority);
                }
            }
            else
            {
                // Only log as a warning as this isn't technically an error, just something to keep an eye on as it shouldn't happen.
                Debug.LogWarning($"Trying to remove a callback from priority {priority}, which does not exist for {GenericTypeName}.");
            }
        }
        else
        {
            // Only log as a warning as this isn't technically an error, just something to keep an eye on as it shouldn't happen.
            Debug.LogWarning($"Trying to remove a callback from the instance {target.name} but there are no listeners for it");
        }
    }
Exemple #3
0
    private static void CreateInstanceEvent(object sender, EventComponent target, T eventArgs)
    {
        SortedList <int, List <EventCallback> > instance;

        // Check there are actually listeners registered for this target.
        // It's okay if there are none. We just don't do anything
        if (InstanceListeners.TryGetValue(target, out instance))
        {
            foreach (var listenerGroup in instance)
            {
                // Cache values so they don't create constant lookups
                List <EventCallback> callbacks = listenerGroup.Value;
                int callbackCount = callbacks.Count;

                // For loop is slightly faster than foreach
                for (int i = 0; i < callbackCount; i++)
                {
                    callbacks[i]?.Invoke(eventArgs);
                }
            }
        }
    }