File: libs/EventDispatcher.js
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
/**
* @module annie
*/
var annie;
(function (annie) {
/**
* annie引擎类的基类
* @class annie.AObject
* @since 1.0.0
*/
var AObject = (function () {
function AObject() {
this._instanceId = 0;
this._instanceType = "AObject";
this._instanceId = AObject._object_id++;
}
Object.defineProperty(AObject.prototype, "instanceId", {
/**
* 每一个annie引擎对象都会有一个唯一的id码。
* @property instanceId
* @public
* @since 1.0.0
* @returns {number}
* @readonly
* @example
* //获取 annie引擎类对象唯一码
* trace(this.instanceId);
*/
get: function () {
return this._instanceId;
},
enumerable: true,
configurable: true
});
Object.defineProperty(AObject.prototype, "instanceType", {
/**
* 每一个annie类都有一个实例类型字符串,通过这个字符串,你能知道这个实例是从哪个类实例而来
* @property instanceType
* @since 1.0.3
* @public
* @return {string}
* @readonly
*/
get: function () {
return this._instanceType;
},
enumerable: true,
configurable: true
});
AObject._object_id = 0;
return AObject;
}());
annie.AObject = AObject;
/**
* 事件触发基类
* @class annie.EventDispatcher
* @extends annie.AObject
* @public
* @since 1.0.0
*/
var EventDispatcher = (function (_super) {
__extends(EventDispatcher, _super);
function EventDispatcher() {
var _this = _super.call(this) || this;
_this.eventTypes = null;
_this._instanceType = "annie.EventDispatcher";
_this.eventTypes = {};
return _this;
}
/**
* 看看有多少mouse或者touch侦听数
* @method getMouseEventCount
* @returns {number}
* @static
* @private
* @since 1.0.0
* @param {string} type 获取事件类型,默认是所有
*/
EventDispatcher.getMouseEventCount = function (type) {
if (type === void 0) { type = ""; }
var count = 0;
if (type == "") {
//返回所有鼠标事件数
for (var item in EventDispatcher._MECO) {
if (item.indexOf("onMouse") == 0) {
count += EventDispatcher._MECO[item];
}
}
}
else {
if (EventDispatcher._MECO[type]) {
count = EventDispatcher._MECO[type];
}
}
return count;
};
/**
* 给对象添加一个侦听
* @method addEventListener
* @public
* @since 1.0.0
* @param {string} type 侦听类形
* @param {Function}listener 侦听后的回调方法,如果这个方法是类实例的方法,为了this引用的正确性,请在方法参数后加上.bind(this);
* @example
* this.addEventListener(annie.Event.ADD_TO_STAGE,function(e){trace(this);}.bind(this));
*/
EventDispatcher.prototype.addEventListener = function (type, listener) {
if (!type) {
throw new Error("添加侦听的type值为undefined");
}
if (!listener) {
throw new Error("侦听回调函数不能为null");
}
var s = this;
if (!s.eventTypes[type]) {
s.eventTypes[type] = [];
}
if (s.eventTypes[type].indexOf(listener) < 0) {
s.eventTypes[type].unshift(listener);
if (type.indexOf("onMouse") == 0) {
s._changeMouseCount(type, true);
}
}
};
/**
* 增加或删除相应mouse或touch侦听记数
* @method _changeMouseCount
* @private
* @since 1.0.0
* @param {string} type
* @param {boolean} isAdd
*/
EventDispatcher.prototype._changeMouseCount = function (type, isAdd) {
var count = isAdd ? 1 : -1;
if (!EventDispatcher._MECO[type]) {
EventDispatcher._MECO[type] = 0;
}
EventDispatcher._MECO[type] += count;
if (EventDispatcher._MECO[type] < 0) {
EventDispatcher._MECO[type] = 0;
}
EventDispatcher._totalMEC += count;
};
/**
* 广播侦听
* @method dispatchEvent
* @public
* @since 1.0.0
* @param {annie.Event|string} event 广播所带的事件对象,如果传的是字符串则直接自动生成一个的事件对象,事件类型就是你传入进来的字符串的值
* @param {Object} data 广播后跟着事件一起传过去的其他任信息,默认值为null
* @returns {boolean} 如果有收听者则返回true
* @example
* var mySprite=new annie.Sprite(),
* yourEvent=new annie.Event("yourCustomerEvent");
* yourEvent.data='Flash2x';
* mySprite.addEventListener("yourCustomerEvent",function(e){
* trace(e.data);
* })
* mySprite.dispatchEvent(yourEvent);
*/
EventDispatcher.prototype.dispatchEvent = function (event, data) {
if (data === void 0) { data = null; }
var s = this;
if (typeof (event) == "string") {
event = new annie.Event(event);
}
var listeners = s.eventTypes[event.type];
if (listeners) {
var len = listeners.length;
if (event.target == null) {
event.target = s;
}
if (data != null) {
event.data = data;
}
for (var i = len - 1; i >= 0; i--) {
if (listeners[i]) {
listeners[i](event);
}
else {
listeners.splice(i, 1);
}
}
return true;
}
else {
return false;
}
};
/**
* 是否有添加过此类形的侦听
* @method hasEventListener
* @public
* @since 1.0.0
* @param {string} type 侦听类形
* @returns {boolean} 如果有则返回true
*/
EventDispatcher.prototype.hasEventListener = function (type) {
if (this.eventTypes[type] && this.eventTypes[type].length > 0) {
return true;
}
return false;
};
/**
* 移除对应类型的侦听
* @method removeEventListener
* @public
* @since 1.0.0
* @param {string} type 要移除的侦听类型
* @param {Function} listener 及侦听时绑定的回调方法
*/
EventDispatcher.prototype.removeEventListener = function (type, listener) {
var s = this;
var listeners = s.eventTypes[type];
if (listeners) {
var len = listeners.length;
for (var i = len - 1; i >= 0; i--) {
if (listeners[i] === listener) {
listeners.splice(i, 1);
if (type.indexOf("onMouse") == 0) {
s._changeMouseCount(type, false);
}
}
}
}
};
/**
* 移除对象中所有的侦听
* @method removeAllEventListener
* @public
* @since 1.0.0
*/
EventDispatcher.prototype.removeAllEventListener = function () {
var s = this;
for (var type in s.eventTypes) {
if (type.indexOf("onMouse") == 0) {
for (var j = 0; j < s.eventTypes[type].length; j++) {
s._changeMouseCount(type, false);
}
}
}
s.eventTypes = {};
};
/**
* 全局的鼠标事件的监听数对象表
* @property _MECO
* @private
* @since 1.0.0
*/
EventDispatcher._MECO = {};
EventDispatcher._totalMEC = 0;
return EventDispatcher;
}(AObject));
annie.EventDispatcher = EventDispatcher;
})(annie || (annie = {}));