package net.mostlyoriginal.api.event;

import com.artemis.EntitySystem;
import com.artemis.Manager;

import java.util.List;

/**
 * Listener registration and event dispatch from within artemis.
 *
 * Will scan all systems and managers for @Subscribe annotation
 * at initialization.
 *
 * @author Daan van Yperen
 */
public class EventManager extends Manager {

    private EventDispatchStrategy dispatcher;

    @Override
    protected void initialize() {
        dispatcher = newDispatcher();
        // register events for all systems and managers.
        registerAllSystemEvents();
        registerAllManagerEvents();
    }

    protected EventDispatchStrategy newDispatcher() {
        return new BasicEventDispatcher();
    }

    /** Resolve all listeners. */
    protected List<EventListener> resolveListeners( Object o )
    {
        return new SubscribeAnnotationFinder().resolve(o);
    }

    /** Register all @Subscribe listeners in passed object (typically system, manager). */
    public void registerEvents( Object o )
    {
        registerAll(resolveListeners(o));
    }

    /** Synchronously dispatch the event */
    public void dispatch( Event event )
    {
        dispatcher.dispatch(event);
    }


    /** Register all listeners with the handler. */
    private void registerAll ( List<EventListener> listeners )
    {
        for (EventListener listener : listeners) {
            dispatcher.register(listener);
        }
    }

    /** Register all systems in this world. */
    private void registerAllSystemEvents( )
    {
        for (EntitySystem entitySystem : world.getSystems()) {
            registerEvents(entitySystem);
        }
    }

    /** Register all managers in this world. */
    private void registerAllManagerEvents( )
    {
        for (Manager manager : world.getManagers()) {
            registerEvents(manager);
        }
    }

}

@@ -399,17 +399,727 @@
 spatcher
-;
+Strategy;%0A    private ListenerFinderStrategy listenerFinderStrategy;%0A%0A    /**%0A     * Init EventManager with default strategies.%0A     */%0A    public EventManager()%0A    %7B%0A        this(new BasicEventDispatcher(), new SubscribeAnnotationFinder());%0A    %7D%0A%0A    /**%0A     * Init EventManager with custom strategies.%0A     * @param dispatcherStrategy Strategy to use for dispatching events.%0A     * @param listenerFinderStrategy Strategy to use for finding listeners on objects.%0A     */%0A    public EventManager(EventDispatchStrategy dispatcherStrategy, ListenerFinderStrategy listenerFinderStrategy) %7B%0A        this.dispatcherStrategy = dispatcherStrategy;%0A        this.listenerFinderStrategy = listenerFinderStrategy;%0A    %7D
 %0A%0A    @O
@@ -1164,46 +1164,8 @@
 ) %7B%0A
-        dispatcher = newDispatcher();%0A
     
@@ -1299,112 +1299,8 @@
  %7D%0A%0A
-    protected EventDispatchStrategy newDispatcher() %7B%0A        return new BasicEventDispatcher();%0A    %7D%0A%0A
     
@@ -1410,14 +1410,32 @@
     
-return
+listenerFinderStrategy =
  new
@@ -1462,16 +1462,55 @@
 Finder()
+;%0A        return listenerFinderStrategy
 .resolve
@@ -1818,16 +1818,24 @@
 spatcher
+Strategy
 .dispatc
@@ -2046,16 +2046,24 @@
 spatcher
+Strategy
 .registe

Add option to customize strategies at EventManager initialization.