Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package max4as.event
- {
- import max4as.event.Event;
- import max4as.event.enum.EventType;
- /**
- * @author Stéphane Robert Richard - kabnot@gmail.com
- */
- public interface IEventDispatcher {
- function addEventListener (listenerReference : ListenerReference) : Boolean ;
- function removeEventListener (listenerReference : ListenerReference) : Boolean ;
- function dispatchEvent (event : Event) : void ;
- function hasEventListener (listener : Object, type : EventType) : Boolean ;
- }
- }
- package max4as.event
- {
- import max4as.error.NullPointerError;
- import max4as.event.IEventDispatcher;
- import max4as.event.ListenerReference;
- import max4as.event.enum.EventType;
- /**
- * @author Stéphane Robert Richard - kabnot@gmail.com
- */
- public class EventDispatcher implements IEventDispatcher {
- private var _listeners : Array = [];
- public function addEventListener (listenerReference : ListenerReference) : Boolean {
- if (listenerReference == null) throw new NullPointerError( );
- for each (var i : ListenerReference in _listeners) {
- if (i == listenerReference || i.equals( listenerReference )) return false ;
- }
- _listeners.push( listenerReference );
- return true;
- }
- public function removeEventListener (listenerReference : ListenerReference) : Boolean {
- if (listenerReference == null) throw new NullPointerError( );
- for (var i : Number = 0; i < _listeners.length ; i++) {
- var l : ListenerReference = _listeners[i];
- if (l == listenerReference) {
- _listeners.splice( i, 1 );
- return true;
- }
- }
- return false;
- }
- public function dispatchEvent (event : Event) : void {
- for each (var i : ListenerReference in _listeners) {
- //Hack for compatibility with the parser, but we should be able to write : i.handlingFunction() directly.
- var f : Function = i.handlingFunction;
- f( );
- }
- }
- public function hasEventListener (listener : Object, type : EventType) : Boolean {
- for (var i : Number = 0; i < _listeners.length ; i++) {
- var l : ListenerReference = _listeners[i] as ListenerReference;
- if (l.listener == listener && l.type == type) return true;
- }
- return false;
- }
- }
- }
- package max4as.event
- {
- import max4as.event.enum.EventType;
- /**
- * @author Stéphane Robert Richard - kabnot@gmail.com
- */
- public class Event {
- private var _type : EventType;
- public function Event (type : EventType) {
- _type = type;
- }
- public function get type () : EventType {
- return _type;
- }
- public function set type (type : EventType) : void {
- _type = type;
- }
- }
- }
- package max4as.event
- {
- import max4as.error.NullPointerError;
- import max4as.event.enum.EventType;
- import max4as.util.IComparable;
- /**
- * @author Stéphane Robert Richard - kabnot@gmail.com
- */
- public class ListenerReference implements IComparable {
- private var _listener : Object;
- private var _handlingFunction : Function;
- private var _type : EventType;
- public function ListenerReference (listener : Object, type : EventType, handlingFunction : Function) {
- if(listener == null || type == null || handlingFunction == null ) throw new NullPointerError( );
- _listener = listener;
- _type = type;
- _handlingFunction = handlingFunction;
- }
- public function get listener () : Object {
- return _listener;
- }
- public function get type () : EventType {
- return _type;
- }
- public function get handlingFunction () : Function {
- return _handlingFunction;
- }
- public function equals (o : Object) : Boolean {
- if ((o == this)) return true ;
- var l:ListenerReference ;
- if (!(o is ListenerReference)) {
- return false ;
- }else{
- l = o as ListenerReference ;
- }
- if (l.listener == _listener && l.type == type && l.handlingFunction == _handlingFunction)
- return true ;
- return false ;
- }
- }
- }
- package max4as.event.enum
- {
- /**
- * @author Stéphane Robert Richard - kabnot@gmail.com
- */
- public class EventType {
- }
- }
- /**
- * Implémentation d'une enum
- */
- package max4as.test.mock.object
- {
- import max4as.event.enum.EventType;
- /**
- * @author Stéphane Robert Richard - kabnot@gmail.com
- */
- public final class MockEventTypeEnum extends EventType {
- public static const MOCK_EVENT : MockEventTypeEnum = new MockEventTypeEnum( );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement