Class EventDispatcher

java.lang.Object
com.prineside.tdi2.events.EventDispatcher
All Implemented Interfaces:
KryoSerializable

public class EventDispatcher extends Object implements KryoSerializable
Creates and manages instances of EventListeners on demand. Allows to access listener groups by event type, queue events and trigger them. If you want to subscribe to an event or to trigger an event, use this.
  • Field Details

  • Constructor Details

    • EventDispatcher

      public EventDispatcher()
  • Method Details

    • write

      public void write(Kryo kryo, Output output)
      Specified by:
      write in interface KryoSerializable
    • read

      public void read(Kryo kryo, Input input)
      Specified by:
      read in interface KryoSerializable
    • getListeners

      public <T extends Event> EventListeners<T> getListeners(Class<T> eventClass)
      Get listeners of some specific event type. New event listener group will be registered if it does not exist yet.
      Parameters:
      eventClass - type of an event
      Returns:
      listener group for the specified event type
    • queue

      public <T extends Event> void queue(T event)
      Use Pools.obtain(EventType.class) to create an event object. It will be freed back to the pool after trigger. Not used by the game currently.
    • hasQueuedEvents

      public boolean hasQueuedEvents()
      Not used by the game currently.
    • trigger

      public <T extends Event> T trigger(T event)
      Notify listeners about the event.
      Type Parameters:
      T - type of event
      Parameters:
      event - event object to pass to the listeners
      Returns:
      event passed to this method (for chaining purposes)
    • getClassToId

      public ObjectIntMap<Class<? extends Event>> getClassToId()
      Returns:
      registry of event type classes where value is an id / index in an array of listenerGroups
    • getListenerGroups

      public Array<EventListeners<?>> getListenerGroups()
      Returns:
      all of the registered EventListeners groups where index is a value from the classToId map
    • getQueuedEvents

      public Array<Event> getQueuedEvents()
      Returns:
      an array of queued events scheduled by queue() and are awaiting to be triggered by triggerQueuedEvents()
    • reset

      public void reset(boolean full)
      Reset the dispatcher and clear it from listeners
      Parameters:
      full - type of reset: - false (partial) - will keep the class registry and all of the FLAG_PERSISTENT listeners - true (full) - no listeners nor registered classes will remain
    • describe

      public StringBuilder describe()
      Describes the dispatcher and all existing listener groups as a string, for debug purposes
      Returns:
      description of this dispatcher as a string