2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2013 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2013-08-28 06:02:55 +00:00
|
|
|
/**
|
|
|
|
* Phaser.SignalBinding
|
|
|
|
*
|
|
|
|
* Object that represents a binding between a Signal and a listener function.
|
2013-11-28 15:57:09 +00:00
|
|
|
* This is an internal constructor and shouldn't be called by regular users.
|
|
|
|
* Inspired by Joa Ebert AS3 SignalBinding and Robert Penner's Slot classes.
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
2013-10-01 12:54:29 +00:00
|
|
|
* @class Phaser.SignalBinding
|
|
|
|
* @name SignalBinding
|
2013-08-28 06:02:55 +00:00
|
|
|
* @author Miller Medeiros http://millermedeiros.github.com/js-signals/
|
|
|
|
* @constructor
|
2013-10-01 12:54:29 +00:00
|
|
|
* @inner
|
2013-11-28 15:57:09 +00:00
|
|
|
* @param {Phaser.Signal} signal - Reference to Signal object that listener is currently bound to.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {function} listener - Handler function bound to the signal.
|
2013-10-01 15:39:39 +00:00
|
|
|
* @param {boolean} isOnce - If binding should be executed just once.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {object} [listenerContext] - Context on which listener will be executed (object that should represent the `this` variable inside listener function).
|
|
|
|
* @param {number} [priority] - The priority level of the event listener. (default = 0).
|
2013-08-28 06:02:55 +00:00
|
|
|
*/
|
|
|
|
Phaser.SignalBinding = function (signal, listener, isOnce, listenerContext, priority) {
|
|
|
|
|
|
|
|
/**
|
2013-11-25 03:13:04 +00:00
|
|
|
* @property {Phaser.Game} _listener - Handler function bound to the signal.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
this._listener = listener;
|
|
|
|
|
|
|
|
/**
|
2013-11-25 03:13:04 +00:00
|
|
|
* @property {boolean} _isOnce - If binding should be executed just once.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
this._isOnce = isOnce;
|
|
|
|
|
|
|
|
/**
|
2013-11-25 03:13:04 +00:00
|
|
|
* @property {object|undefined|null} context - Context on which listener will be executed (object that should represent the `this` variable inside listener function).
|
|
|
|
* @memberof SignalBinding.prototype
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
this.context = listenerContext;
|
|
|
|
|
|
|
|
/**
|
2013-11-28 15:57:09 +00:00
|
|
|
* @property {Phaser.Signal} _signal - Reference to Signal object that listener is currently bound to.
|
2013-11-25 03:13:04 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
this._signal = signal;
|
|
|
|
|
|
|
|
/**
|
2013-11-25 03:13:04 +00:00
|
|
|
* @property {number} _priority - Listener priority.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
this._priority = priority || 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.SignalBinding.prototype = {
|
|
|
|
|
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* If binding is active and should be executed.
|
2013-10-01 15:39:39 +00:00
|
|
|
* @property {boolean} active
|
2013-10-01 12:54:29 +00:00
|
|
|
* @default
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
active: true,
|
|
|
|
|
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* Default parameters passed to listener during `Signal.dispatch` and `SignalBinding.execute` (curried parameters).
|
|
|
|
* @property {array|null} params
|
|
|
|
* @default
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
params: null,
|
|
|
|
|
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* Call listener passing arbitrary parameters.
|
2013-11-28 15:57:09 +00:00
|
|
|
* If binding was added using `Signal.addOnce()` it will be automatically removed from signal dispatch queue, this method is used internally for the signal dispatch.
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.SignalBinding#execute
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {array} [paramsArr] - Array of parameters that should be passed to the listener.
|
2013-11-28 15:57:09 +00:00
|
|
|
* @return {any} Value returned by the listener.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
execute: function (paramsArr) {
|
|
|
|
|
|
|
|
var handlerReturn, params;
|
|
|
|
|
|
|
|
if (this.active && !!this._listener)
|
|
|
|
{
|
|
|
|
params = this.params? this.params.concat(paramsArr) : paramsArr;
|
|
|
|
handlerReturn = this._listener.apply(this.context, params);
|
|
|
|
|
|
|
|
if (this._isOnce)
|
|
|
|
{
|
|
|
|
this.detach();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return handlerReturn;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* Detach binding from signal.
|
2013-11-28 15:57:09 +00:00
|
|
|
* alias to: @see mySignal.remove(myBinding.getListener());
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.SignalBinding#detach
|
2013-10-01 12:54:29 +00:00
|
|
|
* @return {function|null} Handler function bound to the signal or `null` if binding was previously detached.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
detach: function () {
|
|
|
|
return this.isBound() ? this._signal.remove(this._listener, this.context) : null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.SignalBinding#isBound
|
2013-10-01 15:39:39 +00:00
|
|
|
* @return {boolean} True if binding is still bound to the signal and has a listener.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
isBound: function () {
|
|
|
|
return (!!this._signal && !!this._listener);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.SignalBinding#isOnce
|
2013-10-01 15:39:39 +00:00
|
|
|
* @return {boolean} If SignalBinding will only be executed once.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
isOnce: function () {
|
|
|
|
return this._isOnce;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.SignalBinding#getListener
|
2013-10-01 12:54:29 +00:00
|
|
|
* @return {Function} Handler function bound to the signal.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
getListener: function () {
|
|
|
|
return this._listener;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.SignalBinding#getSignal
|
2013-10-01 12:54:29 +00:00
|
|
|
* @return {Signal} Signal that listener is currently bound to.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
getSignal: function () {
|
|
|
|
return this._signal;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.SignalBinding#_destroy
|
2013-10-01 12:54:29 +00:00
|
|
|
* Delete instance properties
|
|
|
|
* @private
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
_destroy: function () {
|
|
|
|
delete this._signal;
|
|
|
|
delete this._listener;
|
|
|
|
delete this.context;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.SignalBinding#toString
|
2013-10-01 12:54:29 +00:00
|
|
|
* @return {string} String representation of the object.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
toString: function () {
|
|
|
|
return '[Phaser.SignalBinding isOnce:' + this._isOnce +', isBound:'+ this.isBound() +', active:' + this.active + ']';
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2013-12-30 16:54:00 +00:00
|
|
|
|
|
|
|
Phaser.SignalBinding.prototype.constructor = Phaser.SignalBinding;
|