mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
Merge branch '3d' of https://github.com/jerrywwl/engine into 3d
This commit is contained in:
@@ -28,18 +28,17 @@ import * as js from '../utils/js';
|
||||
const fastRemoveAt = js.array.fastRemoveAt;
|
||||
|
||||
class CallbackList {
|
||||
public callbacks: Array<Function | null> = [];
|
||||
constructor () {
|
||||
this.callbacks = [];
|
||||
this.targets = []; // same length with callbacks, nullable
|
||||
this.isInvoking = false;
|
||||
this.containCanceled = false;
|
||||
}
|
||||
|
||||
public targets: Array<Object | null> = []; // same length with callbacks, nullable
|
||||
|
||||
public isInvoking = false;
|
||||
|
||||
public containCanceled = false;
|
||||
|
||||
public removeBy (array, value) {
|
||||
const callbacks = this.callbacks;
|
||||
const targets = this.targets;
|
||||
for (let i = 0; i < array.length; ++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);
|
||||
@@ -48,14 +47,14 @@ class CallbackList {
|
||||
}
|
||||
}
|
||||
|
||||
public cancel (index: number) {
|
||||
cancel (index) {
|
||||
this.callbacks[index] = this.targets[index] = null;
|
||||
this.containCanceled = true;
|
||||
}
|
||||
|
||||
public cancelAll () {
|
||||
const callbacks = this.callbacks;
|
||||
const targets = this.targets;
|
||||
cancelAll () {
|
||||
let callbacks = this.callbacks;
|
||||
let targets = this.targets;
|
||||
for (let i = 0; i < callbacks.length; i++) {
|
||||
callbacks[i] = targets[i] = null;
|
||||
}
|
||||
@@ -63,14 +62,14 @@ class CallbackList {
|
||||
}
|
||||
|
||||
// filter all removed callbacks and compact array
|
||||
public purgeCanceled () {
|
||||
purgeCanceled () {
|
||||
this.removeBy(this.callbacks, null);
|
||||
this.containCanceled = false;
|
||||
}
|
||||
}
|
||||
|
||||
const MAX_SIZE = 16;
|
||||
const callbackListPool = new js.Pool((list: CallbackList) => {
|
||||
let callbackListPool = new js.Pool(function (list) {
|
||||
list.callbacks.length = 0;
|
||||
list.targets.length = 0;
|
||||
list.isInvoking = false;
|
||||
@@ -80,10 +79,6 @@ callbackListPool.get = function () {
|
||||
return this._get() || new CallbackList();
|
||||
};
|
||||
|
||||
interface ICallbackTable {
|
||||
[x: string]: CallbackList | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* The CallbacksHandler is an abstract class that can register and unregister callbacks by key.
|
||||
* Subclasses should implement their own methods about how to invoke the callbacks.
|
||||
@@ -91,97 +86,105 @@ interface ICallbackTable {
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
export class CallbacksHandler {
|
||||
protected _callbackTable: ICallbackTable = js.createMap(true);
|
||||
|
||||
function CallbacksHandler () {
|
||||
this._callbackTable = js.createMap(true);
|
||||
}
|
||||
CallbacksHandler.prototype = {
|
||||
constructor: CallbacksHandler,
|
||||
/**
|
||||
* @param key
|
||||
* @param callback
|
||||
* @param [target] - can be null
|
||||
* @method add
|
||||
* @param {String} key
|
||||
* @param {Function} callback
|
||||
* @param {Object} [target] - can be null
|
||||
*/
|
||||
public add (key: string, callback: Function, target: object | null = null) {
|
||||
let list = this._callbackTable[key];
|
||||
add (key, callback, target) {
|
||||
var list = this._callbackTable[key];
|
||||
if (!list) {
|
||||
list = this._callbackTable[key] = callbackListPool.get() as CallbackList;
|
||||
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.
|
||||
* @param key
|
||||
* @param [callback]
|
||||
* @param [target]
|
||||
* @method hasEventListener
|
||||
* @param {String} key
|
||||
* @param {Function} [callback]
|
||||
* @param {Object} [target]
|
||||
* @return {Boolean}
|
||||
*/
|
||||
public hasEventListener (key: string, callback?: Function, target: Object | null = null) {
|
||||
const list = this._callbackTable[key];
|
||||
hasEventListener (key, callback, target) {
|
||||
var list = this._callbackTable[key];
|
||||
if (!list) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// check any valid callback
|
||||
const callbacks = list.callbacks;
|
||||
var callbacks = list.callbacks;
|
||||
if (!callback) {
|
||||
// Make sure no cancelled callbacks
|
||||
if (list.isInvoking) {
|
||||
for (const cb of callbacks) {
|
||||
if (cb) {
|
||||
for (let i = 0; i < callbacks.length; i++) {
|
||||
if (callbacks[i]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return callbacks.length > 0;
|
||||
}
|
||||
}
|
||||
|
||||
target = target || null;
|
||||
const targets = list.targets;
|
||||
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
|
||||
* @param keyOrTarget - The event key to be removed or the target to be removed
|
||||
* @method removeAll
|
||||
* @param {String|Object} keyOrTarget - The event key to be removed or the target to be removed
|
||||
*/
|
||||
public removeAll (keyOrTarget?: string | object) {
|
||||
removeAll (keyOrTarget) {
|
||||
if (typeof keyOrTarget === 'string') {
|
||||
// remove by key
|
||||
const list = this._callbackTable[keyOrTarget];
|
||||
let list = this._callbackTable[keyOrTarget];
|
||||
if (list) {
|
||||
if (list.isInvoking) {
|
||||
list.cancelAll();
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
callbackListPool.put(list);
|
||||
delete this._callbackTable[keyOrTarget];
|
||||
}
|
||||
}
|
||||
} else if (keyOrTarget) {
|
||||
}
|
||||
else if (keyOrTarget) {
|
||||
// remove by target
|
||||
for (const key in this._callbackTable) {
|
||||
if (this._callbackTable.hasOwnProperty(key)) {
|
||||
const list = this._callbackTable[key]!;
|
||||
if (list.isInvoking) {
|
||||
const targets = list.targets;
|
||||
for (let i = 0; i < targets.length; ++i) {
|
||||
if (targets[i] === keyOrTarget) {
|
||||
list.cancel(i);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
else {
|
||||
list.removeBy(list.targets, keyOrTarget);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @method remove
|
||||
@@ -189,17 +192,18 @@ export class CallbacksHandler {
|
||||
* @param {Function} callback
|
||||
* @param {Object} [target]
|
||||
*/
|
||||
public remove (key, callback, target) {
|
||||
const list = this._callbackTable[key];
|
||||
remove (key, callback, target) {
|
||||
var list = this._callbackTable[key];
|
||||
if (list) {
|
||||
target = target || null;
|
||||
const callbacks = list.callbacks;
|
||||
const targets = list.targets;
|
||||
for (let i = 0; i < callbacks.length; ++i) {
|
||||
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 {
|
||||
}
|
||||
else {
|
||||
fastRemoveAt(callbacks, i);
|
||||
fastRemoveAt(targets, i);
|
||||
}
|
||||
@@ -208,28 +212,45 @@ export class CallbacksHandler {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* !#en The callbacks invoker to handle and invoke callbacks by key.
|
||||
* !#zh CallbacksInvoker 用来根据 Key 管理并调用回调方法。
|
||||
* @class CallbacksInvoker
|
||||
*
|
||||
* @extends _CallbacksHandler
|
||||
*/
|
||||
export class CallbacksInvoker extends CallbacksHandler {
|
||||
public emit (key: string, p1?: any, p2?: any, p3?: any, p4?: any, p5?: any) {
|
||||
const list = this._callbackTable[key];
|
||||
function CallbacksInvoker () {
|
||||
CallbacksHandler.call(this);
|
||||
}
|
||||
js.extend(CallbacksInvoker, CallbacksHandler);
|
||||
js.mixin(CallbacksInvoker.prototype, {
|
||||
/**
|
||||
* @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) {
|
||||
const rootInvoker = !list.isInvoking;
|
||||
var rootInvoker = !list.isInvoking;
|
||||
list.isInvoking = true;
|
||||
|
||||
const callbacks = list.callbacks;
|
||||
const targets = list.targets;
|
||||
for (let i = 0, len = callbacks.length; i < len; ++i) {
|
||||
const callback = callbacks[i];
|
||||
var callbacks = list.callbacks;
|
||||
var targets = list.targets;
|
||||
for (var i = 0, len = callbacks.length; i < len; ++i) {
|
||||
var callback = callbacks[i];
|
||||
if (callback) {
|
||||
const target = targets[i];
|
||||
var target = targets[i];
|
||||
if (target) {
|
||||
callback.call(target, p1, p2, p3, p4, p5);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
callback(p1, p2, p3, p4, p5);
|
||||
}
|
||||
}
|
||||
@@ -243,8 +264,13 @@ export class CallbacksInvoker extends CallbacksHandler {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (CC_TEST) {
|
||||
cc._Test.CallbacksInvoker = CallbacksInvoker;
|
||||
}
|
||||
|
||||
export {
|
||||
CallbacksHandler,
|
||||
CallbacksInvoker
|
||||
};
|
||||
@@ -25,8 +25,7 @@
|
||||
****************************************************************************/
|
||||
|
||||
import * as js from '../utils/js';
|
||||
import { CallbacksInvoker } from './callbacks-invoker';
|
||||
import Event from './event';
|
||||
import {CallbacksInvoker} from './callbacks-invoker';
|
||||
|
||||
const fastRemove = js.array.fastRemove;
|
||||
|
||||
@@ -54,17 +53,21 @@ const fastRemove = js.array.fastRemove;
|
||||
* 事件目标是事件触发时,分派的事件对象,Node 是最常见的事件目标,
|
||||
* 但是其他对象也可以是事件目标。<br/>
|
||||
*
|
||||
* @class EventTarget
|
||||
* @extends CallbacksInvoker
|
||||
*/
|
||||
export default class EventTarget extends CallbacksInvoker {
|
||||
function EventTarget () {
|
||||
CallbacksInvoker.call(this);
|
||||
}
|
||||
js.extend(EventTarget, CallbacksInvoker);
|
||||
js.mixin(EventTarget.prototype, {
|
||||
/**
|
||||
* !#en Checks whether the EventTarget object has any callback registered for a specific type of event.
|
||||
* !#zh 检查事件目标对象是否有为特定类型的事件注册的回调。
|
||||
* @param type - The type of event.
|
||||
* @return True if a callback of the specified type is registered; false otherwise.
|
||||
* @method hasEventListener
|
||||
* @param {String} type - The type of event.
|
||||
* @return {Boolean} True if a callback of the specified type is registered; false otherwise.
|
||||
*/
|
||||
public hasEventListener (key: string) {
|
||||
return super.hasEventListener(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
@@ -73,28 +76,38 @@ export default class EventTarget extends CallbacksInvoker {
|
||||
* !#zh
|
||||
* 注册事件目标的特定事件类型回调。这种类型的事件应该被 `emit` 触发。
|
||||
*
|
||||
* @param type - A string representing the event type to listen for.
|
||||
* @param 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 target - The target (this object) to invoke the callback, can be null
|
||||
* @return Just returns the incoming callback so you can save the anonymous function easier.
|
||||
* @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<T extends Function>(type: string, callback: T, target?: any, useCapture?: boolean): T
|
||||
* @example
|
||||
* eventTarget.on('fire', function () {
|
||||
* cc.log("fire in the hole");
|
||||
* }, node);
|
||||
*/
|
||||
public on<T extends Function> (type: string, callback: T, target: Object | null = null) {
|
||||
if (!super.hasEventListener(type, callback, target)) {
|
||||
on (type, callback, target) {
|
||||
if (!callback) {
|
||||
cc.errorID(6800);
|
||||
return;
|
||||
}
|
||||
|
||||
if ( !this.hasEventListener(type, callback, target) ) {
|
||||
this.add(type, callback, target);
|
||||
|
||||
// @ts-ignore
|
||||
if (target && target.__eventTargets) {
|
||||
// @ts-ignore
|
||||
if (target && target.__eventTargets)
|
||||
target.__eventTargets.push(this);
|
||||
}
|
||||
}
|
||||
return callback;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en
|
||||
@@ -104,10 +117,9 @@ export default class EventTarget extends CallbacksInvoker {
|
||||
* 删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。
|
||||
*
|
||||
* @method off
|
||||
* @param type - A string representing the event type being removed.
|
||||
* @param [callback] - The callback to remove.
|
||||
* @param [target] - The target (this object) to invoke the callback,
|
||||
* if it's not given, only callback without target will be removed
|
||||
* @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 () {
|
||||
@@ -118,35 +130,33 @@ export default class EventTarget extends CallbacksInvoker {
|
||||
* // remove all fire event listeners
|
||||
* eventTarget.off('fire');
|
||||
*/
|
||||
public off (type: string, callback?: Function, target: Object | null = null) {
|
||||
off (type, callback, target) {
|
||||
if (!callback) {
|
||||
this.removeAll(type);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
this.remove(type, callback, target);
|
||||
|
||||
// @ts-ignore
|
||||
if (target && target.__eventTargets) {
|
||||
// @ts-ignore
|
||||
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.
|
||||
* 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
|
||||
*/
|
||||
public targetOff () {
|
||||
targetOff () {
|
||||
this.removeAll();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en
|
||||
@@ -170,21 +180,20 @@ export default class EventTarget extends CallbacksInvoker {
|
||||
* cc.log("this is the callback and will be invoked only once");
|
||||
* }, node);
|
||||
*/
|
||||
public once (type, callback, target) {
|
||||
const eventType_hasOnceListener = '__ONCE_FLAG:' + type;
|
||||
const hasOnceListener = super.hasEventListener(eventType_hasOnceListener, callback, target);
|
||||
once (type, callback, target) {
|
||||
var eventType_hasOnceListener = '__ONCE_FLAG:' + type;
|
||||
var hasOnceListener = this.hasEventListener(eventType_hasOnceListener, callback, target);
|
||||
if (!hasOnceListener) {
|
||||
const self = this;
|
||||
const onceWrapper = function (arg1, arg2, arg3, arg4, arg5) {
|
||||
var self = this;
|
||||
var onceWrapper = function (arg1, arg2, arg3, arg4, arg5) {
|
||||
self.off(type, onceWrapper, target);
|
||||
self.remove(eventType_hasOnceListener, callback, target);
|
||||
// @ts-ignore
|
||||
callback.call(this, arg1, arg2, arg3, arg4, arg5);
|
||||
};
|
||||
this.on(type, onceWrapper, target);
|
||||
this.add(eventType_hasOnceListener, callback, target);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en
|
||||
@@ -192,20 +201,18 @@ export default class EventTarget extends CallbacksInvoker {
|
||||
* !#zh
|
||||
* 通过事件名发送自定义事件
|
||||
*
|
||||
* @param type - event type
|
||||
* @param arg1 - First argument
|
||||
* @param arg2 - Second argument
|
||||
* @param arg3 - Third argument
|
||||
* @param arg4 - Fourth argument
|
||||
* @param arg5 - Fifth argument
|
||||
* @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);
|
||||
*/
|
||||
public emit (key: string, p1?: any, p2?: any, p3?: any, p4?: any, p5?: any) {
|
||||
return super.emit(key, p1, p2, p3, p4, p5);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
@@ -213,13 +220,13 @@ export default class EventTarget extends CallbacksInvoker {
|
||||
* !#zh
|
||||
* 通过事件对象派发事件
|
||||
*
|
||||
* @param event
|
||||
* @method dispatchEvent
|
||||
* @param {Event} event
|
||||
*/
|
||||
public dispatchEvent (event: Event) {
|
||||
// @ts-ignore
|
||||
dispatchEvent (event) {
|
||||
this.invoke(event.type, event);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/* tslint:disable:no-string-literal */
|
||||
cc['EventTarget'] = EventTarget;
|
||||
cc.EventTarget = EventTarget;
|
||||
export default EventTarget;
|
||||
@@ -0,0 +1,269 @@
|
||||
/****************************************************************************
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* !#en Base class of all kinds of events.
|
||||
* !#zh 包含事件相关信息的对象。
|
||||
* @class Event
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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 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
|
||||
* 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.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.
|
||||
* !#zh 重置对象池中存储的事件。
|
||||
* @method unuse
|
||||
* @returns {String}
|
||||
*/
|
||||
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.
|
||||
* !#zh 用于对象池再次使用的事件。
|
||||
* @method reuse
|
||||
* @returns {String}
|
||||
*/
|
||||
reuse (type, bubbles) {
|
||||
this.type = type;
|
||||
this.bubbles = bubbles || false;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Stops propagation for current event.
|
||||
* !#zh 停止传递当前事件。
|
||||
* @method stopPropagation
|
||||
*/
|
||||
stopPropagation () {
|
||||
this._propagationStopped = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Stops propagation for current event immediately,
|
||||
* the event won't even be dispatched to the listeners attached in the current target.
|
||||
* !#zh 立即停止当前事件的传递,事件甚至不会被分派到所连接的当前目标。
|
||||
* @method stopPropagationImmediate
|
||||
*/
|
||||
stopPropagationImmediate () {
|
||||
this._propagationImmediateStopped = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Checks whether the event has been stopped.
|
||||
* !#zh 检查该事件是否已经停止传递.
|
||||
* @method isStopped
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
isStopped () {
|
||||
return this._propagationStopped || this._propagationImmediateStopped;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* <p>
|
||||
* Gets current target of the event <br/>
|
||||
* note: It only be available when the event listener is associated with node. <br/>
|
||||
* It returns 0 when the listener is associated with fixed priority.
|
||||
* </p>
|
||||
* !#zh 获取当前目标节点
|
||||
* @method getCurrentTarget
|
||||
* @returns {Node} The target with which the event associates.
|
||||
*/
|
||||
getCurrentTarget () {
|
||||
return this.currentTarget;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Gets the event type.
|
||||
* !#zh 获取事件类型
|
||||
* @method getType
|
||||
* @returns {String}
|
||||
*/
|
||||
getType () {
|
||||
return this.type;
|
||||
}
|
||||
}
|
||||
|
||||
//event type
|
||||
/**
|
||||
* !#en Code for event without type.
|
||||
* !#zh 没有类型的事件
|
||||
* @property NO_TYPE
|
||||
* @static
|
||||
* @type {string}
|
||||
*/
|
||||
Event.NO_TYPE = 'no_type';
|
||||
|
||||
/**
|
||||
* !#en The type code of Touch event.
|
||||
* !#zh 触摸事件类型
|
||||
* @property TOUCH
|
||||
* @static
|
||||
* @type {String}
|
||||
*/
|
||||
Event.TOUCH = 'touch';
|
||||
/**
|
||||
* !#en The type code of Mouse event.
|
||||
* !#zh 鼠标事件类型
|
||||
* @property MOUSE
|
||||
* @static
|
||||
* @type {String}
|
||||
*/
|
||||
Event.MOUSE = 'mouse';
|
||||
/**
|
||||
* !#en The type code of Keyboard event.
|
||||
* !#zh 键盘事件类型
|
||||
* @property KEYBOARD
|
||||
* @static
|
||||
* @type {String}
|
||||
*/
|
||||
Event.KEYBOARD = 'keyboard';
|
||||
/**
|
||||
* !#en The type code of Acceleration event.
|
||||
* !#zh 加速器事件类型
|
||||
* @property ACCELERATION
|
||||
* @static
|
||||
* @type {String}
|
||||
*/
|
||||
Event.ACCELERATION = 'acceleration';
|
||||
|
||||
//event phase
|
||||
/**
|
||||
* !#en Events not currently dispatched are in this phase
|
||||
* !#zh 尚未派发事件阶段
|
||||
* @property NONE
|
||||
* @type {Number}
|
||||
* @static
|
||||
*/
|
||||
Event.NONE = 0;
|
||||
/**
|
||||
* !#en
|
||||
* The capturing phase comprises the journey from the root to the last node before the event target's node
|
||||
* see http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
|
||||
* !#zh 捕获阶段,包括事件目标节点之前从根节点到最后一个节点的过程。
|
||||
* @property CAPTURING_PHASE
|
||||
* @type {Number}
|
||||
* @static
|
||||
*/
|
||||
Event.CAPTURING_PHASE = 1;
|
||||
/**
|
||||
* !#en
|
||||
* The target phase comprises only the event target node
|
||||
* see http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
|
||||
* !#zh 目标阶段仅包括事件目标节点。
|
||||
* @property AT_TARGET
|
||||
* @type {Number}
|
||||
* @static
|
||||
*/
|
||||
Event.AT_TARGET = 2;
|
||||
/**
|
||||
* !#en
|
||||
* The bubbling phase comprises any subsequent nodes encountered on the return trip to the root of the hierarchy
|
||||
* see http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
|
||||
* !#zh 冒泡阶段, 包括回程遇到到层次根节点的任何后续节点。
|
||||
* @property BUBBLING_PHASE
|
||||
* @type {Number}
|
||||
* @static
|
||||
*/
|
||||
Event.BUBBLING_PHASE = 3;
|
||||
|
||||
cc.Event = Event;
|
||||
export default Event;
|
||||
@@ -1,222 +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.
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* !#en Base class of all kinds of events.
|
||||
* !#zh 包含事件相关信息的对象。
|
||||
* @class Event
|
||||
*/
|
||||
export default class Event {
|
||||
// Event types
|
||||
|
||||
/**
|
||||
* !#en Code for event without type.
|
||||
* !#zh 没有类型的事件
|
||||
*/
|
||||
public static NO_TYPE = 'no_type';
|
||||
|
||||
/**
|
||||
* !#en The type code of Touch event.
|
||||
* !#zh 触摸事件类型
|
||||
*/
|
||||
public static TOUCH = 'touch';
|
||||
/**
|
||||
* !#en The type code of Mouse event.
|
||||
* !#zh 鼠标事件类型
|
||||
*/
|
||||
public static MOUSE = 'mouse';
|
||||
/**
|
||||
* !#en The type code of Keyboard event.
|
||||
* !#zh 键盘事件类型
|
||||
*/
|
||||
public static KEYBOARD = 'keyboard';
|
||||
/**
|
||||
* !#en The type code of Acceleration event.
|
||||
* !#zh 加速器事件类型
|
||||
*/
|
||||
public static ACCELERATION = 'acceleration';
|
||||
|
||||
// Event phases
|
||||
|
||||
/**
|
||||
* !#en Events not currently dispatched are in this phase
|
||||
* !#zh 尚未派发事件阶段
|
||||
*/
|
||||
public static NONE = 0;
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* The capturing phase comprises the journey from the root to the last node before the event target's node
|
||||
* see http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
|
||||
* !#zh 捕获阶段,包括事件目标节点之前从根节点到最后一个节点的过程。
|
||||
*/
|
||||
public static CAPTURING_PHASE = 1;
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* The target phase comprises only the event target node
|
||||
* see http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
|
||||
* !#zh 目标阶段仅包括事件目标节点。
|
||||
*/
|
||||
public static AT_TARGET = 2;
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* The bubbling phase comprises any subsequent nodes encountered on the return trip to the root of the hierarchy
|
||||
* see http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
|
||||
* !#zh 冒泡阶段, 包括回程遇到到层次根节点的任何后续节点。
|
||||
*/
|
||||
public static BUBBLING_PHASE = 3;
|
||||
|
||||
/**
|
||||
* !#en The name of the event (case-sensitive), e.g. "click", "fire", or "submit".
|
||||
* !#zh 事件类型。
|
||||
*/
|
||||
public type: string;
|
||||
|
||||
/**
|
||||
* !#en Indicate whether the event bubbles up through the tree or not.
|
||||
* !#zh 表示该事件是否进行冒泡。
|
||||
*/
|
||||
public bubbles: boolean;
|
||||
|
||||
/**
|
||||
* !#en A reference to the target to which the event was originally dispatched.
|
||||
* !#zh 最初事件触发的目标
|
||||
*/
|
||||
public target: Object | null = null;
|
||||
|
||||
/**
|
||||
* !#en A reference to the currently registered target for the event.
|
||||
* !#zh 当前目标
|
||||
*/
|
||||
public currentTarget: Object | null = 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 事件阶段
|
||||
*/
|
||||
public eventPhase = 0;
|
||||
|
||||
/*
|
||||
* Indicates whether or not event.stopPropagation() has been called on the event.
|
||||
*/
|
||||
private _propagationStopped = false;
|
||||
|
||||
/*
|
||||
* Indicates whether or not event.stopPropagationImmediate() has been called on the event.
|
||||
*/
|
||||
private _propagationImmediateStopped = false;
|
||||
|
||||
/**
|
||||
* @param type - The name of the event (case-sensitive), e.g. "click", "fire", or "submit"
|
||||
* @param bubbles - A boolean indicating whether the event bubbles up through the tree or not
|
||||
*/
|
||||
constructor (type: string, bubbles?: boolean) {
|
||||
this.type = type;
|
||||
this.bubbles = !!bubbles;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Reset the event for being stored in the object pool.
|
||||
* !#zh 重置对象池中存储的事件。
|
||||
*/
|
||||
public unuse () {
|
||||
this.type = Event.NO_TYPE;
|
||||
this.target = null;
|
||||
this.currentTarget = null;
|
||||
this.eventPhase = Event.NONE;
|
||||
this._propagationStopped = false;
|
||||
this._propagationImmediateStopped = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Reuse the event for being used again by the object pool.
|
||||
* !#zh 用于对象池再次使用的事件。
|
||||
*/
|
||||
public reuse (type: string, bubbles?: boolean) {
|
||||
this.type = type;
|
||||
this.bubbles = bubbles || false;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Stops propagation for current event.
|
||||
* !#zh 停止传递当前事件。
|
||||
*/
|
||||
public stopPropagation () {
|
||||
this._propagationStopped = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Stops propagation for current event immediately,
|
||||
* the event won't even be dispatched to the listeners attached in the current target.
|
||||
* !#zh 立即停止当前事件的传递,事件甚至不会被分派到所连接的当前目标。
|
||||
*/
|
||||
public stopPropagationImmediate () {
|
||||
this._propagationImmediateStopped = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Checks whether the event has been stopped.
|
||||
* !#zh 检查该事件是否已经停止传递.
|
||||
*/
|
||||
public isStopped () {
|
||||
return this._propagationStopped || this._propagationImmediateStopped;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* <p>
|
||||
* Gets current target of the event <br/>
|
||||
* note: It only be available when the event listener is associated with node. <br/>
|
||||
* It returns 0 when the listener is associated with fixed priority.
|
||||
* </p>
|
||||
* !#zh 获取当前目标节点
|
||||
* @returns The target with which the event associates.
|
||||
*/
|
||||
public getCurrentTarget () {
|
||||
return this.currentTarget;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Gets the event type.
|
||||
* !#zh 获取事件类型
|
||||
*/
|
||||
public getType () {
|
||||
return this.type;
|
||||
}
|
||||
}
|
||||
|
||||
/* tslint:disable:no-string-literal */
|
||||
cc['Event'] = Event;
|
||||
@@ -25,4 +25,4 @@
|
||||
****************************************************************************/
|
||||
|
||||
export { default as Event } from './event';
|
||||
export { default as EventTarget } from './event-target';
|
||||
export { default as EventTarget } from './event-target';
|
||||
Reference in New Issue
Block a user