diff --git a/cocos/core/event/callbacks-invoker.js b/cocos/core/event/callbacks-invoker.js index 72d38d73b3..cd36ff9b74 100644 --- a/cocos/core/event/callbacks-invoker.js +++ b/cocos/core/event/callbacks-invoker.js @@ -24,52 +24,52 @@ THE SOFTWARE. ****************************************************************************/ -const js = require('./js'); +import * as js from '../utils/js'; const fastRemoveAt = js.array.fastRemoveAt; -function CallbackList () { - this.callbacks = []; - this.targets = []; // same length with callbacks, nullable - this.isInvoking = false; - this.containCanceled = false; -} -var proto = CallbackList.prototype; +class CallbackList { + constructor () { + this.callbacks = []; + this.targets = []; // same length with callbacks, nullable + this.isInvoking = false; + this.containCanceled = false; + } -proto.removeBy = function (array, value) { - var callbacks = this.callbacks; - var targets = this.targets; - for (var i = 0; i < array.length; ++i) { - if (array[i] === value) { - fastRemoveAt(callbacks, i); - fastRemoveAt(targets, i); - --i; + removeBy (array, value) { + var callbacks = this.callbacks; + var targets = this.targets; + for (var i = 0; i < array.length; ++i) { + if (array[i] === value) { + fastRemoveAt(callbacks, i); + fastRemoveAt(targets, i); + --i; + } } } -}; -proto.cancel = function (index) { - this.callbacks[index] = this.targets[index] = null; - this.containCanceled = true; -}; - -proto.cancelAll = function () { - let callbacks = this.callbacks; - let targets = this.targets; - for (let i = 0; i < callbacks.length; i++) { - callbacks[i] = targets[i] = null; + cancel (index) { + this.callbacks[index] = this.targets[index] = null; + this.containCanceled = true; } - this.containCanceled = true; -}; -// filter all removed callbacks and compact array -proto.purgeCanceled = function () { - this.removeBy(this.callbacks, null); - this.containCanceled = false; -}; + cancelAll () { + let callbacks = this.callbacks; + let targets = this.targets; + for (let i = 0; i < callbacks.length; i++) { + callbacks[i] = targets[i] = null; + } + this.containCanceled = true; + } + // filter all removed callbacks and compact array + purgeCanceled () { + this.removeBy(this.callbacks, null); + this.containCanceled = false; + } +} const MAX_SIZE = 16; -const callbackListPool = new js.Pool(function (list) { +let callbackListPool = new js.Pool(function (list) { list.callbacks.length = 0; list.targets.length = 0; list.isInvoking = false; @@ -86,133 +86,133 @@ callbackListPool.get = function () { * * @private */ -function CallbacksHandler () { - this._callbackTable = js.createMap(true); -} -proto = CallbacksHandler.prototype; - -/** - * @method add - * @param {String} key - * @param {Function} callback - * @param {Object} [target] - can be null - */ -proto.add = function (key, callback, target) { - var list = this._callbackTable[key]; - if (!list) { - list = this._callbackTable[key] = callbackListPool.get(); +export class CallbacksHandler { + constructor () { + this._callbackTable = js.createMap(true); } - list.callbacks.push(callback); - list.targets.push(target || null); -}; -/** - * Check if the specified key has any registered callback. If a callback is also specified, - * it will only return true if the callback is registered. - * @method hasEventListener - * @param {String} key - * @param {Function} [callback] - * @param {Object} [target] - * @return {Boolean} - */ -proto.hasEventListener = function (key, callback, target) { - var list = this._callbackTable[key]; - if (!list) { + /** + * @method add + * @param {String} key + * @param {Function} callback + * @param {Object} [target] - can be null + */ + add (key, callback, target) { + var list = this._callbackTable[key]; + if (!list) { + list = this._callbackTable[key] = callbackListPool.get(); + } + list.callbacks.push(callback); + list.targets.push(target || null); + } + + /** + * Check if the specified key has any registered callback. If a callback is also specified, + * it will only return true if the callback is registered. + * @method hasEventListener + * @param {String} key + * @param {Function} [callback] + * @param {Object} [target] + * @return {Boolean} + */ + hasEventListener (key, callback, target) { + var list = this._callbackTable[key]; + if (!list) { + return false; + } + + // check any valid callback + var callbacks = list.callbacks; + if (!callback) { + // Make sure no cancelled callbacks + if (list.isInvoking) { + for (let i = 0; i < callbacks.length; i++) { + if (callbacks[i]) { + return true; + } + } + return false; + } + else { + return callbacks.length > 0; + } + } + + target = target || null; + var targets = list.targets; + for (let i = 0; i < callbacks.length; ++i) { + if (callbacks[i] === callback && targets[i] === target) { + return true; + } + } return false; } - // check any valid callback - var callbacks = list.callbacks; - if (!callback) { - // Make sure no cancelled callbacks - if (list.isInvoking) { - for (let i = 0; i < callbacks.length; i++) { - if (callbacks[i]) { - return true; - } - } - return false; - } - else { - return callbacks.length > 0; - } - } - - target = target || null; - var targets = list.targets; - for (let i = 0; i < callbacks.length; ++i) { - if (callbacks[i] === callback && targets[i] === target) { - return true; - } - } - return false; -}; - -/** - * Removes all callbacks registered in a certain event type or all callbacks registered with a certain target - * @method removeAll - * @param {String|Object} keyOrTarget - The event key to be removed or the target to be removed - */ -proto.removeAll = function (keyOrTarget) { - if (typeof keyOrTarget === 'string') { - // remove by key - let list = this._callbackTable[keyOrTarget]; - if (list) { - if (list.isInvoking) { - list.cancelAll(); - } - else { - callbackListPool.put(list); - delete this._callbackTable[keyOrTarget]; - } - } - } - else if (keyOrTarget) { - // remove by target - for (let key in this._callbackTable) { - let list = this._callbackTable[key]; - if (list.isInvoking) { - let targets = list.targets; - for (let i = 0; i < targets.length; ++i) { - if (targets[i] === keyOrTarget) { - list.cancel(i); - } - } - } - else { - list.removeBy(list.targets, keyOrTarget); - } - } - } -}; - -/** - * @method remove - * @param {String} key - * @param {Function} callback - * @param {Object} [target] - */ -proto.remove = function (key, callback, target) { - var list = this._callbackTable[key]; - if (list) { - target = target || null; - var callbacks = list.callbacks; - var targets = list.targets; - for (var i = 0; i < callbacks.length; ++i) { - if (callbacks[i] === callback && targets[i] === target) { + /** + * Removes all callbacks registered in a certain event type or all callbacks registered with a certain target + * @method removeAll + * @param {String|Object} keyOrTarget - The event key to be removed or the target to be removed + */ + removeAll (keyOrTarget) { + if (typeof keyOrTarget === 'string') { + // remove by key + let list = this._callbackTable[keyOrTarget]; + if (list) { if (list.isInvoking) { - list.cancel(i); + list.cancelAll(); } else { - fastRemoveAt(callbacks, i); - fastRemoveAt(targets, i); + callbackListPool.put(list); + delete this._callbackTable[keyOrTarget]; + } + } + } + else if (keyOrTarget) { + // remove by target + for (let key in this._callbackTable) { + let list = this._callbackTable[key]; + if (list.isInvoking) { + let targets = list.targets; + for (let i = 0; i < targets.length; ++i) { + if (targets[i] === keyOrTarget) { + list.cancel(i); + } + } + } + else { + list.removeBy(list.targets, keyOrTarget); } - break; } } } -}; + /** + * @method remove + * @param {String} key + * @param {Function} callback + * @param {Object} [target] + */ + remove (key, callback, target) { + var list = this._callbackTable[key]; + if (list) { + target = target || null; + var callbacks = list.callbacks; + var targets = list.targets; + for (var i = 0; i < callbacks.length; ++i) { + if (callbacks[i] === callback && targets[i] === target) { + if (list.isInvoking) { + list.cancel(i); + } + else { + fastRemoveAt(callbacks, i); + fastRemoveAt(targets, i); + } + break; + } + } + } + } +} /** * !#en The callbacks invoker to handle and invoke callbacks by key. @@ -221,53 +221,51 @@ proto.remove = function (key, callback, target) { * * @extends _CallbacksHandler */ -var CallbacksInvoker = function () { - CallbacksHandler.call(this); -}; -js.extend(CallbacksInvoker, CallbacksHandler); +export class CallbacksInvoker extends CallbacksHandler{ + constructor () { + CallbacksHandler.call(this); + } -if (CC_TEST) { - cc._Test.CallbacksInvoker = CallbacksInvoker; -} + /** + * @method emit + * @param {String} key + * @param {any} [p1] + * @param {any} [p2] + * @param {any} [p3] + * @param {any} [p4] + * @param {any} [p5] + */ + emit (key, p1, p2, p3, p4, p5) { + var list = this._callbackTable[key]; + if (list) { + var rootInvoker = !list.isInvoking; + list.isInvoking = true; -/** - * @method invoke - * @param {String} key - * @param {any} [p1] - * @param {any} [p2] - * @param {any} [p3] - * @param {any} [p4] - * @param {any} [p5] - */ -CallbacksInvoker.prototype.invoke = function (key, p1, p2, p3, p4, p5) { - var list = this._callbackTable[key]; - if (list) { - var rootInvoker = !list.isInvoking; - list.isInvoking = true; - - var callbacks = list.callbacks; - var targets = list.targets; - for (var i = 0, len = callbacks.length; i < len; ++i) { - var callback = callbacks[i]; - if (callback) { - var target = targets[i]; - if (target) { - callback.call(target, p1, p2, p3, p4, p5); - } - else { - callback(p1, p2, p3, p4, p5); + var callbacks = list.callbacks; + var targets = list.targets; + for (var i = 0, len = callbacks.length; i < len; ++i) { + var callback = callbacks[i]; + if (callback) { + var target = targets[i]; + if (target) { + callback.call(target, p1, p2, p3, p4, p5); + } + else { + callback(p1, p2, p3, p4, p5); + } } } - } - if (rootInvoker) { - list.isInvoking = false; - if (list.containCanceled) { - list.purgeCanceled(); + if (rootInvoker) { + list.isInvoking = false; + if (list.containCanceled) { + list.purgeCanceled(); + } } } } -}; - -CallbacksInvoker.CallbacksHandler = CallbacksHandler; -module.exports = CallbacksInvoker; +} + +if (CC_TEST) { + cc._Test.CallbacksInvoker = CallbacksInvoker; +} \ No newline at end of file diff --git a/cocos/core/event/event-listeners.js b/cocos/core/event/event-listeners.js deleted file mode 100644 index d238cf06dc..0000000000 --- a/cocos/core/event/event-listeners.js +++ /dev/null @@ -1,68 +0,0 @@ -/**************************************************************************** - Copyright (c) 2013-2016 Chukong Technologies Inc. - Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. - - http://www.cocos.com - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated engine source code (the "Software"), a limited, - worldwide, royalty-free, non-assignable, revocable and non-exclusive license - to use Cocos Creator solely to develop games on your target platforms. You shall - not use Cocos Creator software for developing other software or tools that's - used for developing games. You are not granted to publish, distribute, - sublicense, and/or sell copies of Cocos Creator. - - The software or tools in this License Agreement are licensed, not sold. - Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - ****************************************************************************/ - -var js = cc.js; -var CallbacksHandler = require('../platform/callbacks-invoker').CallbacksHandler; - -// Extends CallbacksHandler to handle and invoke event callbacks. -function EventListeners () { - CallbacksHandler.call(this); -} -js.extend(EventListeners, CallbacksHandler); - -EventListeners.prototype.invoke = function (event, captureListeners) { - var key = event.type; - var list = this._callbackTable[key]; - if (list) { - var rootInvoker = !list.isInvoking; - list.isInvoking = true; - - var callbacks = list.callbacks; - var targets = list.targets; - for (var i = 0, len = callbacks.length; i < len; ++i) { - var callback = callbacks[i]; - if (callback) { - var target = targets[i] || event.currentTarget; - callback.call(target, event, captureListeners); - if (event._propagationImmediateStopped) { - break; - } - } - } - - if (rootInvoker) { - list.isInvoking = false; - if (list.containCanceled) { - list.purgeCanceled(); - } - } - } -}; - -module.exports = EventListeners; -if (CC_TEST) { - cc._Test.EventListeners = EventListeners; -} diff --git a/cocos/core/event/event-target.js b/cocos/core/event/event-target.js index bdf2c40e72..1c4d48a48f 100644 --- a/cocos/core/event/event-target.js +++ b/cocos/core/event/event-target.js @@ -24,8 +24,8 @@ THE SOFTWARE. ****************************************************************************/ -const js = require('../platform/js'); -const CallbacksInvoker = require('../platform/callbacks-invoker'); +import * as js from '../utils/js'; +import {CallbacksInvoker} from './callbacks-invoker'; var fastRemove = js.array.fastRemove; @@ -54,177 +54,178 @@ var fastRemove = js.array.fastRemove; * 但是其他对象也可以是事件目标。
* * @class EventTarget + * @extends CallbacksInvoker */ -function EventTarget () { - CallbacksInvoker.call(this); -} -js.extend(EventTarget, CallbacksInvoker); - -var proto = EventTarget.prototype; - -/** - * !#en Checks whether the EventTarget object has any callback registered for a specific type of event. - * !#zh 检查事件目标对象是否有为特定类型的事件注册的回调。 - * @method hasEventListener - * @param {String} type - The type of event. - * @return {Boolean} True if a callback of the specified type is registered; false otherwise. - */ - -/** - * !#en - * Register an callback of a specific event type on the EventTarget. - * This type of event should be triggered via `emit`. - * !#zh - * 注册事件目标的特定事件类型回调。这种类型的事件应该被 `emit` 触发。 - * - * @method on - * @param {String} type - A string representing the event type to listen for. - * @param {Function} callback - The callback that will be invoked when the event is dispatched. - * The callback is ignored if it is a duplicate (the callbacks are unique). - * @param {any} [callback.arg1] arg1 - * @param {any} [callback.arg2] arg2 - * @param {any} [callback.arg3] arg3 - * @param {any} [callback.arg4] arg4 - * @param {any} [callback.arg5] arg5 - * @param {Object} [target] - The target (this object) to invoke the callback, can be null - * @return {Function} - Just returns the incoming callback so you can save the anonymous function easier. - * @typescript - * on(type: string, callback: T, target?: any, useCapture?: boolean): T - * @example - * eventTarget.on('fire', function () { - * cc.log("fire in the hole"); - * }, node); - */ -proto.on = function (type, callback, target) { - if (!callback) { - cc.errorID(6800); - return; +export default class EventTarget extends CallbacksInvoker { + constructor () { + super(); } - if ( !this.hasEventListener(type, callback, target) ) { - this.add(type, callback, target); + /** + * !#en Checks whether the EventTarget object has any callback registered for a specific type of event. + * !#zh 检查事件目标对象是否有为特定类型的事件注册的回调。 + * @method hasEventListener + * @param {String} type - The type of event. + * @return {Boolean} True if a callback of the specified type is registered; false otherwise. + */ - if (target && target.__eventTargets) - target.__eventTargets.push(this); - } - return callback; -}; - -/** - * !#en - * Removes the listeners previously registered with the same type, callback, target and or useCapture, - * if only type is passed as parameter, all listeners registered with that type will be removed. - * !#zh - * 删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。 - * - * @method off - * @param {String} type - A string representing the event type being removed. - * @param {Function} [callback] - The callback to remove. - * @param {Object} [target] - The target (this object) to invoke the callback, if it's not given, only callback without target will be removed - * @example - * // register fire eventListener - * var callback = eventTarget.on('fire', function () { - * cc.log("fire in the hole"); - * }, target); - * // remove fire event listener - * eventTarget.off('fire', callback, target); - * // remove all fire event listeners - * eventTarget.off('fire'); - */ -proto.off = function (type, callback, target) { - if (!callback) { - this.removeAll(type); - } - else { - this.remove(type, callback, target); - - if (target && target.__eventTargets) { - fastRemove(target.__eventTargets, this); + /** + * !#en + * Register an callback of a specific event type on the EventTarget. + * This type of event should be triggered via `emit`. + * !#zh + * 注册事件目标的特定事件类型回调。这种类型的事件应该被 `emit` 触发。 + * + * @method on + * @param {String} type - A string representing the event type to listen for. + * @param {Function} callback - The callback that will be invoked when the event is dispatched. + * The callback is ignored if it is a duplicate (the callbacks are unique). + * @param {any} [callback.arg1] arg1 + * @param {any} [callback.arg2] arg2 + * @param {any} [callback.arg3] arg3 + * @param {any} [callback.arg4] arg4 + * @param {any} [callback.arg5] arg5 + * @param {Object} [target] - The target (this object) to invoke the callback, can be null + * @return {Function} - Just returns the incoming callback so you can save the anonymous function easier. + * @typescript + * on(type: string, callback: T, target?: any, useCapture?: boolean): T + * @example + * eventTarget.on('fire', function () { + * cc.log("fire in the hole"); + * }, node); + */ + on (type, callback, target) { + if (!callback) { + cc.errorID(6800); + return; } + + if ( !this.hasEventListener(type, callback, target) ) { + this.add(type, callback, target); + + if (target && target.__eventTargets) + target.__eventTargets.push(this); + } + return callback; + }; + + /** + * !#en + * Removes the listeners previously registered with the same type, callback, target and or useCapture, + * if only type is passed as parameter, all listeners registered with that type will be removed. + * !#zh + * 删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。 + * + * @method off + * @param {String} type - A string representing the event type being removed. + * @param {Function} [callback] - The callback to remove. + * @param {Object} [target] - The target (this object) to invoke the callback, if it's not given, only callback without target will be removed + * @example + * // register fire eventListener + * var callback = eventTarget.on('fire', function () { + * cc.log("fire in the hole"); + * }, target); + * // remove fire event listener + * eventTarget.off('fire', callback, target); + * // remove all fire event listeners + * eventTarget.off('fire'); + */ + off (type, callback, target) { + if (!callback) { + this.removeAll(type); + } + else { + this.remove(type, callback, target); + + if (target && target.__eventTargets) { + fastRemove(target.__eventTargets, this); + } + } + }; + + /** + * !#en Removes all callbacks previously registered with the same target (passed as parameter). + * This is not for removing all listeners in the current event target, + * and this is not for removing all listeners the target parameter have registered. + * It's only for removing all listeners (callback and target couple) registered on the current event target by the target parameter. + * !#zh 在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。 + * 这个函数无法删除当前 EventTarget 的所有事件监听器,也无法删除 target 参数所注册的所有事件监听器。 + * 这个函数只能删除 target 参数在当前 EventTarget 上注册的所有事件监听器。 + * @method targetOff + * @param {Object} target - The target to be searched for all related listeners + */ + targetOff () { + this.removeAll(); } -}; -/** - * !#en Removes all callbacks previously registered with the same target (passed as parameter). - * This is not for removing all listeners in the current event target, - * and this is not for removing all listeners the target parameter have registered. - * It's only for removing all listeners (callback and target couple) registered on the current event target by the target parameter. - * !#zh 在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。 - * 这个函数无法删除当前 EventTarget 的所有事件监听器,也无法删除 target 参数所注册的所有事件监听器。 - * 这个函数只能删除 target 参数在当前 EventTarget 上注册的所有事件监听器。 - * @method targetOff - * @param {Object} target - The target to be searched for all related listeners - */ -proto.targetOff = proto.removeAll; + /** + * !#en + * Register an callback of a specific event type on the EventTarget, + * the callback will remove itself after the first time it is triggered. + * !#zh + * 注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。 + * + * @method once + * @param {String} type - A string representing the event type to listen for. + * @param {Function} callback - The callback that will be invoked when the event is dispatched. + * The callback is ignored if it is a duplicate (the callbacks are unique). + * @param {any} [callback.arg1] arg1 + * @param {any} [callback.arg2] arg2 + * @param {any} [callback.arg3] arg3 + * @param {any} [callback.arg4] arg4 + * @param {any} [callback.arg5] arg5 + * @param {Object} [target] - The target (this object) to invoke the callback, can be null + * @example + * eventTarget.once('fire', function () { + * cc.log("this is the callback and will be invoked only once"); + * }, node); + */ + once (type, callback, target) { + var eventType_hasOnceListener = '__ONCE_FLAG:' + type; + var hasOnceListener = this.hasEventListener(eventType_hasOnceListener, callback, target); + if (!hasOnceListener) { + var self = this; + var onceWrapper = function (arg1, arg2, arg3, arg4, arg5) { + self.off(type, onceWrapper, target); + self.remove(eventType_hasOnceListener, callback, target); + callback.call(this, arg1, arg2, arg3, arg4, arg5); + }; + this.on(type, onceWrapper, target); + this.add(eventType_hasOnceListener, callback, target); + } + }; -/** - * !#en - * Register an callback of a specific event type on the EventTarget, - * the callback will remove itself after the first time it is triggered. - * !#zh - * 注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。 - * - * @method once - * @param {String} type - A string representing the event type to listen for. - * @param {Function} callback - The callback that will be invoked when the event is dispatched. - * The callback is ignored if it is a duplicate (the callbacks are unique). - * @param {any} [callback.arg1] arg1 - * @param {any} [callback.arg2] arg2 - * @param {any} [callback.arg3] arg3 - * @param {any} [callback.arg4] arg4 - * @param {any} [callback.arg5] arg5 - * @param {Object} [target] - The target (this object) to invoke the callback, can be null - * @example - * eventTarget.once('fire', function () { - * cc.log("this is the callback and will be invoked only once"); - * }, node); - */ -proto.once = function (type, callback, target) { - var eventType_hasOnceListener = '__ONCE_FLAG:' + type; - var hasOnceListener = this.hasEventListener(eventType_hasOnceListener, callback, target); - if (!hasOnceListener) { - var self = this; - var onceWrapper = function (arg1, arg2, arg3, arg4, arg5) { - self.off(type, onceWrapper, target); - self.remove(eventType_hasOnceListener, callback, target); - callback.call(this, arg1, arg2, arg3, arg4, arg5); - }; - this.on(type, onceWrapper, target); - this.add(eventType_hasOnceListener, callback, target); - } -}; + /** + * !#en + * Trigger an event directly with the event name and necessary arguments. + * !#zh + * 通过事件名发送自定义事件 + * + * @method emit + * @param {String} type - event type + * @param {*} [arg1] - First argument + * @param {*} [arg2] - Second argument + * @param {*} [arg3] - Third argument + * @param {*} [arg4] - Fourth argument + * @param {*} [arg5] - Fifth argument + * @example + * + * eventTarget.emit('fire', event); + * eventTarget.emit('fire', message, emitter); + */ -/** - * !#en - * Trigger an event directly with the event name and necessary arguments. - * !#zh - * 通过事件名发送自定义事件 - * - * @method emit - * @param {String} type - event type - * @param {*} [arg1] - First argument - * @param {*} [arg2] - Second argument - * @param {*} [arg3] - Third argument - * @param {*} [arg4] - Fourth argument - * @param {*} [arg5] - Fifth argument - * @example - * - * eventTarget.emit('fire', event); - * eventTarget.emit('fire', message, emitter); - */ -proto.emit = CallbacksInvoker.prototype.invoke; + /** + * !#en + * Send an event with the event object. + * !#zh + * 通过事件对象派发事件 + * + * @method dispatchEvent + * @param {Event} event + */ + dispatchEvent (event) { + this.invoke(event.type, event); + }; +} -/** - * !#en - * Send an event with the event object. - * !#zh - * 通过事件对象派发事件 - * - * @method dispatchEvent - * @param {Event} event - */ -proto.dispatchEvent = function (event) { - this.invoke(event.type, event); -}; - -cc.EventTarget = module.exports = EventTarget; +cc.EventTarget = EventTarget; diff --git a/cocos/core/event/event.js b/cocos/core/event/event.js index 21fe7e4980..280a9209b8 100644 --- a/cocos/core/event/event.js +++ b/cocos/core/event/event.js @@ -24,86 +24,82 @@ THE SOFTWARE. ****************************************************************************/ -var js = require("../platform/js"); - /** * !#en Base class of all kinds of events. * !#zh 包含事件相关信息的对象。 * @class Event */ - -/** - * @method constructor - * @param {String} type - The name of the event (case-sensitive), e.g. "click", "fire", or "submit" - * @param {Boolean} bubbles - A boolean indicating whether the event bubbles up through the tree or not - */ -cc.Event = function(type, bubbles) { +export default class Event { /** - * !#en The name of the event (case-sensitive), e.g. "click", "fire", or "submit". - * !#zh 事件类型。 - * @property type - * @type {String} + * @method constructor + * @param {String} type - The name of the event (case-sensitive), e.g. "click", "fire", or "submit" + * @param {Boolean} bubbles - A boolean indicating whether the event bubbles up through the tree or not */ - this.type = type; + constructor (type, bubbles) { + /** + * !#en The name of the event (case-sensitive), e.g. "click", "fire", or "submit". + * !#zh 事件类型。 + * @property type + * @type {String} + */ + this.type = type; - /** - * !#en Indicate whether the event bubbles up through the tree or not. - * !#zh 表示该事件是否进行冒泡。 - * @property bubbles - * @type {Boolean} - */ - this.bubbles = !!bubbles; + /** + * !#en Indicate whether the event bubbles up through the tree or not. + * !#zh 表示该事件是否进行冒泡。 + * @property bubbles + * @type {Boolean} + */ + this.bubbles = !!bubbles; - /** - * !#en A reference to the target to which the event was originally dispatched. - * !#zh 最初事件触发的目标 - * @property target - * @type {Object} - */ - this.target = null; + /** + * !#en A reference to the target to which the event was originally dispatched. + * !#zh 最初事件触发的目标 + * @property target + * @type {Object} + */ + this.target = null; - /** - * !#en A reference to the currently registered target for the event. - * !#zh 当前目标 - * @property currentTarget - * @type {Object} - */ - this.currentTarget = null; + /** + * !#en A reference to the currently registered target for the event. + * !#zh 当前目标 + * @property currentTarget + * @type {Object} + */ + this.currentTarget = null; - /** - * !#en - * Indicates which phase of the event flow is currently being evaluated. - * Returns an integer value represented by 4 constants: - * - Event.NONE = 0 - * - Event.CAPTURING_PHASE = 1 - * - Event.AT_TARGET = 2 - * - Event.BUBBLING_PHASE = 3 - * The phases are explained in the [section 3.1, Event dispatch and DOM event flow] - * (http://www.w3.org/TR/DOM-Level-3-Events/#event-flow), of the DOM Level 3 Events specification. - * !#zh 事件阶段 - * @property eventPhase - * @type {Number} - */ - this.eventPhase = 0; + /** + * !#en + * Indicates which phase of the event flow is currently being evaluated. + * Returns an integer value represented by 4 constants: + * - Event.NONE = 0 + * - Event.CAPTURING_PHASE = 1 + * - Event.AT_TARGET = 2 + * - Event.BUBBLING_PHASE = 3 + * The phases are explained in the [section 3.1, Event dispatch and DOM event flow] + * (http://www.w3.org/TR/DOM-Level-3-Events/#event-flow), of the DOM Level 3 Events specification. + * !#zh 事件阶段 + * @property eventPhase + * @type {Number} + */ + this.eventPhase = 0; - /* - * Indicates whether or not event.stopPropagation() has been called on the event. - * @property _propagationStopped - * @type {Boolean} - * @private - */ - this._propagationStopped = false; + /* + * Indicates whether or not event.stopPropagation() has been called on the event. + * @property _propagationStopped + * @type {Boolean} + * @private + */ + this._propagationStopped = false; - /* - * Indicates whether or not event.stopPropagationImmediate() has been called on the event. - * @property _propagationImmediateStopped - * @type {Boolean} - * @private - */ - this._propagationImmediateStopped = false; -}; -cc.Event.prototype = { - constructor: cc.Event, + /* + * Indicates whether or not event.stopPropagationImmediate() has been called on the event. + * @property _propagationImmediateStopped + * @type {Boolean} + * @private + */ + this._propagationImmediateStopped = false; + } /** * !#en Reset the event for being stored in the object pool. @@ -111,14 +107,14 @@ cc.Event.prototype = { * @method unuse * @returns {String} */ - unuse: function () { + unuse () { this.type = cc.Event.NO_TYPE; this.target = null; this.currentTarget = null; this.eventPhase = cc.Event.NONE; this._propagationStopped = false; this._propagationImmediateStopped = false; - }, + } /** * !#en Reuse the event for being used again by the object pool. @@ -126,19 +122,19 @@ cc.Event.prototype = { * @method reuse * @returns {String} */ - reuse: function (type, bubbles) { + reuse (type, bubbles) { this.type = type; this.bubbles = bubbles || false; - }, + } /** * !#en Stops propagation for current event. * !#zh 停止传递当前事件。 * @method stopPropagation */ - stopPropagation: function () { + stopPropagation () { this._propagationStopped = true; - }, + } /** * !#en Stops propagation for current event immediately, @@ -146,9 +142,9 @@ cc.Event.prototype = { * !#zh 立即停止当前事件的传递,事件甚至不会被分派到所连接的当前目标。 * @method stopPropagationImmediate */ - stopPropagationImmediate: function () { + stopPropagationImmediate () { this._propagationImmediateStopped = true; - }, + } /** * !#en Checks whether the event has been stopped. @@ -156,9 +152,9 @@ cc.Event.prototype = { * @method isStopped * @returns {Boolean} */ - isStopped: function () { + isStopped () { return this._propagationStopped || this._propagationImmediateStopped; - }, + } /** * !#en @@ -171,9 +167,9 @@ cc.Event.prototype = { * @method getCurrentTarget * @returns {Node} The target with which the event associates. */ - getCurrentTarget: function () { + getCurrentTarget () { return this.currentTarget; - }, + } /** * !#en Gets the event type. @@ -181,7 +177,7 @@ cc.Event.prototype = { * @method getType * @returns {String} */ - getType: function () { + getType () { return this.type; } }; @@ -194,7 +190,7 @@ cc.Event.prototype = { * @static * @type {string} */ -cc.Event.NO_TYPE = 'no_type'; +Event.NO_TYPE = 'no_type'; /** * !#en The type code of Touch event. @@ -203,7 +199,7 @@ cc.Event.NO_TYPE = 'no_type'; * @static * @type {String} */ -cc.Event.TOUCH = 'touch'; +Event.TOUCH = 'touch'; /** * !#en The type code of Mouse event. * !#zh 鼠标事件类型 @@ -211,7 +207,7 @@ cc.Event.TOUCH = 'touch'; * @static * @type {String} */ -cc.Event.MOUSE = 'mouse'; +Event.MOUSE = 'mouse'; /** * !#en The type code of Keyboard event. * !#zh 键盘事件类型 @@ -219,7 +215,7 @@ cc.Event.MOUSE = 'mouse'; * @static * @type {String} */ -cc.Event.KEYBOARD = 'keyboard'; +Event.KEYBOARD = 'keyboard'; /** * !#en The type code of Acceleration event. * !#zh 加速器事件类型 @@ -227,7 +223,7 @@ cc.Event.KEYBOARD = 'keyboard'; * @static * @type {String} */ -cc.Event.ACCELERATION = 'acceleration'; +Event.ACCELERATION = 'acceleration'; //event phase /** @@ -237,7 +233,7 @@ cc.Event.ACCELERATION = 'acceleration'; * @type {Number} * @static */ -cc.Event.NONE = 0; +Event.NONE = 0; /** * !#en * The capturing phase comprises the journey from the root to the last node before the event target's node @@ -247,7 +243,7 @@ cc.Event.NONE = 0; * @type {Number} * @static */ -cc.Event.CAPTURING_PHASE = 1; +Event.CAPTURING_PHASE = 1; /** * !#en * The target phase comprises only the event target node @@ -257,7 +253,7 @@ cc.Event.CAPTURING_PHASE = 1; * @type {Number} * @static */ -cc.Event.AT_TARGET = 2; +Event.AT_TARGET = 2; /** * !#en * The bubbling phase comprises any subsequent nodes encountered on the return trip to the root of the hierarchy @@ -267,81 +263,6 @@ cc.Event.AT_TARGET = 2; * @type {Number} * @static */ -cc.Event.BUBBLING_PHASE = 3; +Event.BUBBLING_PHASE = 3; -/** - * !#en The Custom event - * !#zh 自定义事件 - * @class Event.EventCustom - * - * @extends Event - */ - -/** - * @method constructor - * @param {String} type - The name of the event (case-sensitive), e.g. "click", "fire", or "submit" - * @param {Boolean} bubbles - A boolean indicating whether the event bubbles up through the tree or not - */ -var EventCustom = function (type, bubbles) { - cc.Event.call(this, type, bubbles); - - /** - * !#en A reference to the detailed data of the event - * !#zh 事件的详细数据 - * @property detail - * @type {Object} - */ - this.detail = null; -}; - -js.extend(EventCustom, cc.Event); - -EventCustom.prototype.reset = EventCustom; - -/** - * !#en Sets user data - * !#zh 设置用户数据 - * @method setUserData - * @param {*} data - */ -EventCustom.prototype.setUserData = function (data) { - this.detail = data; -}; - -/** - * !#en Gets user data - * !#zh 获取用户数据 - * @method getUserData - * @returns {*} - */ -EventCustom.prototype.getUserData = function () { - return this.detail; -}; - -/** - * !#en Gets event name - * !#zh 获取事件名称 - * @method getEventName - * @returns {String} - */ -EventCustom.prototype.getEventName = cc.Event.prototype.getType; - -var MAX_POOL_SIZE = 10; -var _eventPool = new js.Pool(MAX_POOL_SIZE); -EventCustom.put = function (event) { - _eventPool.put(event); -}; -EventCustom.get = function (type, bubbles) { - var event = _eventPool._get(); - if (event) { - event.reset(type, bubbles); - } - else { - event = new EventCustom(type, bubbles); - } - return event; -}; - -cc.Event.EventCustom = EventCustom; - -module.exports = cc.Event; +cc.Event = Event; \ No newline at end of file diff --git a/cocos/core/event/index.js b/cocos/core/event/index.js index 25158f1a57..298fb5ea72 100644 --- a/cocos/core/event/index.js +++ b/cocos/core/event/index.js @@ -23,8 +23,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ - -require('./event'); -require('./event-listeners'); -require('./event-target'); -require('./system-event'); + +export { default as Event } from './event'; +export { default as EventTarget } from './event-target'; \ No newline at end of file diff --git a/cocos/core/event/system-event.js b/cocos/core/platform/event-manager/system-event.js similarity index 100% rename from cocos/core/event/system-event.js rename to cocos/core/platform/event-manager/system-event.js diff --git a/cocos/vmath/bits.js b/cocos/core/vmath/bits.js similarity index 100% rename from cocos/vmath/bits.js rename to cocos/core/vmath/bits.js diff --git a/cocos/vmath/color3.js b/cocos/core/vmath/color3.js similarity index 100% rename from cocos/vmath/color3.js rename to cocos/core/vmath/color3.js diff --git a/cocos/vmath/color4.js b/cocos/core/vmath/color4.js similarity index 100% rename from cocos/vmath/color4.js rename to cocos/core/vmath/color4.js diff --git a/cocos/vmath/index.js b/cocos/core/vmath/index.js similarity index 100% rename from cocos/vmath/index.js rename to cocos/core/vmath/index.js diff --git a/cocos/vmath/mat2.js b/cocos/core/vmath/mat2.js similarity index 100% rename from cocos/vmath/mat2.js rename to cocos/core/vmath/mat2.js diff --git a/cocos/vmath/mat23.js b/cocos/core/vmath/mat23.js similarity index 100% rename from cocos/vmath/mat23.js rename to cocos/core/vmath/mat23.js diff --git a/cocos/vmath/mat3.js b/cocos/core/vmath/mat3.js similarity index 100% rename from cocos/vmath/mat3.js rename to cocos/core/vmath/mat3.js diff --git a/cocos/vmath/mat4.js b/cocos/core/vmath/mat4.js similarity index 100% rename from cocos/vmath/mat4.js rename to cocos/core/vmath/mat4.js diff --git a/cocos/vmath/quat.js b/cocos/core/vmath/quat.js similarity index 100% rename from cocos/vmath/quat.js rename to cocos/core/vmath/quat.js diff --git a/cocos/vmath/utils.js b/cocos/core/vmath/utils.js similarity index 100% rename from cocos/vmath/utils.js rename to cocos/core/vmath/utils.js diff --git a/cocos/vmath/vec2.js b/cocos/core/vmath/vec2.js similarity index 100% rename from cocos/vmath/vec2.js rename to cocos/core/vmath/vec2.js diff --git a/cocos/vmath/vec3.js b/cocos/core/vmath/vec3.js similarity index 100% rename from cocos/vmath/vec3.js rename to cocos/core/vmath/vec3.js diff --git a/cocos/vmath/vec4.js b/cocos/core/vmath/vec4.js similarity index 100% rename from cocos/vmath/vec4.js rename to cocos/core/vmath/vec4.js