Btw did I mention it also supports event bubbling?
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Bjorn
Schultheiss
Sent: Thursday, 13 April 2006 11:39 PM
To: 'Flashcoders mailing list'
Subject: RE: [Flashcoders] Need help understanding EventDispatcher
class com.qdc.events.QDispatcher
{
static var $instance:QDispatcher = undefined;
private var qDispatcher_listeners:Object;
private var qDispatcher_queues:Object;
public static var CLASS_NAME : String = "QDispatcher";
static function initialize(p_obj:Object):Void {
if ($instance == undefined) { $instance = new QDispatcher; }
p_obj.dispatchEvent = $instance.dispatchEvent;
p_obj.queueEvent = $instance.queueEvent;
p_obj.dispatchQueue = $instance.dispatchQueue;
p_obj.eventListenerExists = $instance.eventListenerExists;
p_obj.addEventListener = $instance.addEventListener;
p_obj.listAllListeners = $instance.listAllListeners;
p_obj.removeEventListener = $instance.removeEventListener;
p_obj.removeAllEventListeners =
$instance.removeAllEventListeners;
}
// internal function to locate listeners:
static function
$indexOfListener(p_listeners:Array,p_obj:Object,p_function:String):Number {
var l:Number = p_listeners.length;
var i:Number = -1;
while (++i < l) {
var obj:Object = p_listeners[i];
if (obj.o == p_obj && obj.f == p_function) { return
i; }
}
return -1;
}
static function
$dispatchEvent(p_dispatchObj:Object,p_listeners:Array,p_eventObj:Object) {
//_global.log.debug(
CLASS_NAME+".plisteners.length="+p_listeners.length );
//trace( CLASS_NAME+".plisteners.length="+p_listeners.length
);
// Note : Previous gSkinner code copitualted on me( noticed
when trying to destroy
// DetailsUI it only looped through 3 of 8 elements.
//var i:String;
// trick from MM: fixes problem with users removing items
from listeners while it executes.
//for (i in p_listeners) {
var len : Number = (p_listeners.length-1);
for ( var i : Number = len; i >= 0; i-- )
{
var o:Object = p_listeners[i].o;
var oType:String = typeof(o);
var f:String = p_listeners[i].f;
//_global.log.debug( CLASS_NAME+" i="+i+"
oType="+oType+" o.handleEvent="+o.handleEvent+" f="+f );
if (oType == "object" || oType == "movieclip") {
if (o.handleEvent != undefined && f ==
undefined) {
o.handleEvent(p_eventObj);
} else {
if (f == undefined) { f =
p_eventObj.type; }
o[f](p_eventObj);
}
} else { // function
o.apply(p_dispatchObj,[p_eventObj]);
}
}
}
/**
* Make dispatcher a bubbling dispatcher
* Given that dispatcher has a method dispatchEvent(evt), this
* method is replaced by a method which additionally dispatches the
event
* to the parent of dispatcher, unless evt.bubbles === false
* @param dispatcher - a MovieClip with a EventDispatcher compatible
* dispatchEvent method
* @return the changed dispatcher
*/
public static function initializeBubbling( dispatcher:Object ) :
Void
{
var parentDispatcher = findParentDispatcher( dispatcher);
var oldDispatchEvent = dispatcher.dispatchEvent;
dispatcher.dispatchEvent = function( evt){
evt.currentTarget = dispatcher;
if( evt.eventPhase == undefined){
evt.eventPhase = 2; //at target
} else {
evt.eventPhase = 3; //bubbling
}
oldDispatchEvent.call( dispatcher, evt);
if( evt.bubbles !== false){
parentDispatcher.dispatchEvent( evt);
}
}
}
/**
* Return the next parent with a dispatchEvent method or null, if
not found
* @param dispatcher - a MovieClip with a EventDispatcher compatible
* dispatchEvent method
* @return - a parent of dispatcher with a EventDispatcher
compatible
* dispatchEvent method or null, if none could be found
*/
private static function findParentDispatcher( dispatcher:Object ) :
Object
{
var o = dispatcher._parent;
while( o != null){
if( o.dispatchEvent != null) return o;
o = o._parent;
}
return null;
}
// functions used by dispatchers:
private function dispatchEvent(p_eventObj:Object):Void {
if (p_eventObj.type == "ALL") { return; } // disallow events
of type "ALL"
if (p_eventObj.target == undefined) { p_eventObj.target =
this; }
this[p_eventObj.type + "Handler"](p_eventObj);
var listeners:Array =
qDispatcher_listeners[p_eventObj.type];
//_global.log.debug( CLASS_NAME+"dispatchEvent
listener1length="+listeners6.length );
if (listeners != undefined) {
QDispatcher.$dispatchEvent(this,listeners,p_eventObj); }
listeners = qDispatcher_listeners["ALL"];
//_global.log.debug( CLASS_NAME+"dispatchEvent
listener2length="+listeners.length );
if (listeners != undefined) {
QDispatcher.$dispatchEvent(this,listeners,p_eventObj); }
}
private function queueEvent( p_eventObj : Object ) : Void
{
//_global.log.debug( CLASS_NAME+".queueEvent
"+p_eventObj.key+" "+p_eventObj.type)
var qKey : String = String( p_eventObj.key );
if ( qKey == undefined )
{
trace( "**ERROR** Queue Event requires a key to be
defined")
return;
}
if (qDispatcher_queues == undefined) { qDispatcher_queues =
{}; }
if ( qDispatcher_queues[ qKey ] == undefined ) {
qDispatcher_queues[ qKey ] = {}; }
var loaded : Boolean = qDispatcher_queues[ qKey ].loaded;
if ( qDispatcher_queues[ qKey ].loaded == undefined ) {
qDispatcher_queues[ qKey ].loaded = loaded = false; }
if ( qDispatcher_queues[ qKey ].loaded )
{
this.dispatchEvent( p_eventObj );
}
else
{
var queue : Array = qDispatcher_queues[ qKey
].queue;
if ( queue == undefined ) { qDispatcher_queues[ qKey
].queue = queue = []; }
qDispatcher_queues[ qKey ].queue.push( p_eventObj );
}
}
private function dispatchQueue( key : Object ) : Void
{
////_global.log.debug( CLASS_NAME+".dispatchQueue "+key );
var qKey : String = String( key );
if ( qKey == undefined )
{
trace( "**ERROR** dispatchQueue requires a key to be
defined")
return;
}
if (qDispatcher_queues == undefined) { qDispatcher_queues =
{}; }
if ( qDispatcher_queues[ qKey ] == undefined ) {
qDispatcher_queues[ qKey ] = {}; }
var loaded : Boolean = qDispatcher_queues[ qKey ].loaded;
if ( qDispatcher_queues[ qKey ].loaded == undefined ||
qDispatcher_queues[ qKey ].loaded == false ) { qDispatcher_queues[ qKey
].loaded = loaded = true; }
for ( var i : Number = 0; i < qDispatcher_queues[ qKey
].queue.length; i++ )
{
this.dispatchEvent( qDispatcher_queues[ qKey
].queue[ i ] );
}
}
private function
eventListenerExists(p_event:String,p_obj:Object,p_function:String):Boolean {
return
(QDispatcher.$indexOfListener(qDispatcher_listeners[p_event],p_obj,p_functio
n) != -1);
}
private function
addEventListener(p_event:String,p_obj:Object,p_function:String):Void {
if (qDispatcher_listeners == undefined) {
qDispatcher_listeners = {};
_global.ASSetPropFlags(this,qDispatcher_listeners,1); }
var listeners:Array = qDispatcher_listeners[p_event];
if (listeners == undefined) { qDispatcher_listeners[p_event]
= listeners = []; }
if (QDispatcher.$indexOfListener(listeners,p_obj,p_function)
== -1) { listeners.push({o:p_obj,f:p_function}); }
}
private function
removeEventListener(p_event:String,p_obj:Object,p_function:String):Void {
var listeners = qDispatcher_listeners[p_event];
if (listeners == undefined) { return; }
var index:Number =
QDispatcher.$indexOfListener(listeners,p_obj,p_function);
if (index != -1) { listeners.splice(index,1); }
}
private function removeAllEventListeners(p_event:String):Void {
if (p_event == undefined) { delete(qDispatcher_listeners); }
else { delete(qDispatcher_listeners[p_event]); }
}
private function listAllListeners() : Void
{
var listeners = qDispatcher_listeners["ALL"];
//_global.log.debug(
CLASS_NAME+".listeners.length="+listeners.length );
for ( var i = 0; i<listeners.length; i++ )
{
//_global.log.debug(
CLASS_NAME+".listener="+listeners[i].o.CLASS_NAME );
}
}
}
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of jim
Sent: Thursday, 13 April 2006 11:32 PM
To: 'Flashcoders mailing list'
Subject: RE: [Flashcoders] Need help understanding EventDispatcher
Ok, that's very nice but if you are going to give an example using it you
might want to give out the class as well otherwise the example isn't much
help.
Jim
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Bjorn
Schultheiss
Sent: 13 April 2006 14:01
To: 'Flashcoders mailing list'
Subject: RE: [Flashcoders] Need help understanding EventDispatcher
It's an eventDispatcher class based off Grant Skinner's GDispatcher '
http://gskinner.com/blog/'.
My class handles queues and dispatching queues nicer :)
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of jim
Sent: Thursday, 13 April 2006 9:09 PM
To: 'Flashcoders mailing list'
Subject: RE: [Flashcoders] Need help understanding EventDispatcher
But what is QDispatcher? Do you have a link to it?
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Bjorn
Schultheiss
Sent: 13 April 2006 05:19
To: 'Flashcoders mailing list'
Subject: RE: [Flashcoders] Need help understanding EventDispatcher
This is better, more extensible
// on timeline
var myBall:Ball2 = Ball2.createInstance( this, 2 ).init();
myBall.addEventListener( 'ALL', this );
function onBallPress():Void {
trace("onBallPress");
}
// Ball Class
import com.qdc.events.QDispatcher;
import com.dynamicflash.utils.Delegate;
class Ball2 extends MovieClip
{
// constants
public static var CLASS_NAME : String = "Ball2";
public static var SYMBOL_NAME : String = "testBall";
// methods
public function dispatchEvent() {};
public function addEventListener() {};
public static function createInstance( parent:MovieClip,
depth:Number, name:String ) : Ball2
{
if( depth == undefined) depth =
parent.getNextHighestDepth();
if( name == undefined) name = SYMBOL_NAME + depth;
return Ball2( parent.attachMovie( SYMBOL_NAME, name,
depth));
}
public function init() : Ball2
{
QDispatcher.initialize( this );
this._x = 100;
this._y = 100;
this.lineStyle(20, 0xff0000, 100);
this.lineTo(0,.2);
return this;
}
public function onLoad() : Void
{
aBall.onPress = dispatchEvent( { type : 'onBallPress' } );
}
}
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Patrick
Matte
Sent: Thursday, 13 April 2006 1:12 PM
To: Flashcoders mailing list
Subject: Re: [Flashcoders] Need help understanding EventDispatcher
I dont see exactly why use for the event dispatcher.
Wouldn't this be good as well ?
// on timeline
import Ball2.as;
import mx.utils.Delegate;
var myBall:Ball2 = new Ball2();
myBall.onBallPress = Delegate.create(this,onBallPress);
myBall.addEvents();
function onBallPress():Void {
trace("onBallPress");
}
// ball class
import mx.utils.Delegate;
class Ball2 {
public var onBallPress:Function;
private var aBall:MovieClip;
public function Ball2 () {
// create a ball MC
aBall =
_level0.createEmptyMovieClip('testBall',_level0.getNextHighestDepth() );
// draw ball
aBall._x = 100;
aBall._y = 100;
aBall.lineStyle(20, 0xff0000, 100);
aBall.lineTo(0,.2);
}
public function addEvents():Void{
aBall.onPress = Delegate.create(this,onBallPress);
}
}
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com