mirror of
https://github.com/smallmain/cocos-enhance-kit.git
synced 2026-01-11 17:26:52 +00:00
初始化
This commit is contained in:
66
engine/cocos2d/core/event/event-listeners.js
Normal file
66
engine/cocos2d/core/event/event-listeners.js
Normal file
@@ -0,0 +1,66 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://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.
|
||||
****************************************************************************/
|
||||
|
||||
const js = cc.js;
|
||||
const CallbacksInvoker = require('../platform/callbacks-invoker');
|
||||
|
||||
// Extends CallbacksInvoker to handle and invoke event callbacks.
|
||||
function EventListeners () {
|
||||
CallbacksInvoker.call(this);
|
||||
}
|
||||
js.extend(EventListeners, CallbacksInvoker);
|
||||
|
||||
EventListeners.prototype.emit = function (event, captureListeners) {
|
||||
let key = event.type;
|
||||
const list = this._callbackTable[key];
|
||||
if (list) {
|
||||
let rootInvoker = !list.isInvoking;
|
||||
list.isInvoking = true;
|
||||
|
||||
const infos = list.callbackInfos;
|
||||
for (let i = 0, len = infos.length; i < len; ++i) {
|
||||
const info = infos[i];
|
||||
if (info && info.callback) {
|
||||
info.callback.call(info.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;
|
||||
}
|
||||
240
engine/cocos2d/core/event/event-target.js
Normal file
240
engine/cocos2d/core/event/event-target.js
Normal file
@@ -0,0 +1,240 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://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.
|
||||
****************************************************************************/
|
||||
|
||||
const js = require('../platform/js');
|
||||
const CallbacksInvoker = require('../platform/callbacks-invoker');
|
||||
|
||||
var fastRemove = js.array.fastRemove;
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* EventTarget is an object to which an event is dispatched when something has occurred.
|
||||
* Entity are the most common event targets, but other objects can be event targets too.
|
||||
*
|
||||
* Event targets are an important part of the Fireball event model.
|
||||
* The event target serves as the focal point for how events flow through the scene graph.
|
||||
* When an event such as a mouse click or a keypress occurs, Fireball dispatches an event object
|
||||
* into the event flow from the root of the hierarchy. The event object then makes its way through
|
||||
* the scene graph until it reaches the event target, at which point it begins its return trip through
|
||||
* the scene graph. This round-trip journey to the event target is conceptually divided into three phases:
|
||||
* - The capture phase comprises the journey from the root to the last node before the event target's node
|
||||
* - The target phase comprises only the event target node
|
||||
* - The bubbling phase comprises any subsequent nodes encountered on the return trip to the root of the tree
|
||||
* See also: http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
|
||||
*
|
||||
* Event targets can implement the following methods:
|
||||
* - _getCapturingTargets
|
||||
* - _getBubblingTargets
|
||||
*
|
||||
* !#zh
|
||||
* 事件目标是事件触发时,分派的事件对象,Node 是最常见的事件目标,
|
||||
* 但是其他对象也可以是事件目标。<br/>
|
||||
*
|
||||
* @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<T extends Function>(type: string, callback: T, target?: any, useCapture?: boolean): T
|
||||
* @example
|
||||
* eventTarget.on('fire', function () {
|
||||
* cc.log("fire in the hole");
|
||||
* }, node);
|
||||
*/
|
||||
proto.__on = proto.on;
|
||||
proto.on = function (type, callback, target, once) {
|
||||
if (!callback) {
|
||||
cc.errorID(6800);
|
||||
return;
|
||||
}
|
||||
|
||||
if ( !this.hasEventListener(type, callback, target) ) {
|
||||
this.__on(type, callback, target, once);
|
||||
|
||||
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 = proto.off;
|
||||
proto.off = function (type, callback, target) {
|
||||
if (!callback) {
|
||||
let list = this._callbackTable[type];
|
||||
if (!list) return;
|
||||
let infos = list.callbackInfos;
|
||||
for (let i = 0; i < infos.length; ++i) {
|
||||
let target = infos[i] && infos[i].target;
|
||||
if (target && target.__eventTargets) {
|
||||
fastRemove(target.__eventTargets, this);
|
||||
}
|
||||
}
|
||||
this.removeAll(type);
|
||||
}
|
||||
else {
|
||||
this.__off(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
|
||||
*/
|
||||
proto.targetOff = function (target) {
|
||||
this.removeAll(target);
|
||||
|
||||
if (target && target.__eventTargets) {
|
||||
fastRemove(target.__eventTargets, this);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* !#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) {
|
||||
this.on(type, callback, target, true);
|
||||
};
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Send an event with the event object.
|
||||
* !#zh
|
||||
* 通过事件对象派发事件
|
||||
*
|
||||
* @method dispatchEvent
|
||||
* @param {Event} event
|
||||
*/
|
||||
proto.dispatchEvent = function (event) {
|
||||
this.emit(event.type, event);
|
||||
};
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Destroy all callbackInfos.
|
||||
* !#zh
|
||||
* 销毁记录的事件
|
||||
*
|
||||
* @method clear
|
||||
*/
|
||||
proto.clear = function () {
|
||||
// remove all callback
|
||||
for (const key in this._callbackTable) {
|
||||
const list = this._callbackTable[key];
|
||||
const infos = list.callbackInfos;
|
||||
for (let i = infos.length - 1; i >= 0; i--) {
|
||||
const info = infos[i];
|
||||
if (info) {
|
||||
this.off(key, info.callback, info.target);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
cc.EventTarget = module.exports = EventTarget;
|
||||
347
engine/cocos2d/core/event/event.js
Normal file
347
engine/cocos2d/core/event/event.js
Normal file
@@ -0,0 +1,347 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://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 = 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) {
|
||||
/**
|
||||
* !#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;
|
||||
};
|
||||
cc.Event.prototype = {
|
||||
constructor: cc.Event,
|
||||
|
||||
/**
|
||||
* !#en Reset the event for being stored in the object pool.
|
||||
* !#zh 重置对象池中存储的事件。
|
||||
* @method unuse
|
||||
* @returns {String}
|
||||
*/
|
||||
unuse: function () {
|
||||
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: function (type, bubbles) {
|
||||
this.type = type;
|
||||
this.bubbles = bubbles || false;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Stops propagation for current event.
|
||||
* !#zh 停止传递当前事件。
|
||||
* @method stopPropagation
|
||||
*/
|
||||
stopPropagation: function () {
|
||||
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: function () {
|
||||
this._propagationImmediateStopped = true;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Checks whether the event has been stopped.
|
||||
* !#zh 检查该事件是否已经停止传递.
|
||||
* @method isStopped
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
isStopped: function () {
|
||||
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: function () {
|
||||
return this.currentTarget;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Gets the event type.
|
||||
* !#zh 获取事件类型
|
||||
* @method getType
|
||||
* @returns {String}
|
||||
*/
|
||||
getType: function () {
|
||||
return this.type;
|
||||
}
|
||||
};
|
||||
|
||||
//event type
|
||||
/**
|
||||
* !#en Code for event without type.
|
||||
* !#zh 没有类型的事件
|
||||
* @property NO_TYPE
|
||||
* @static
|
||||
* @type {string}
|
||||
*/
|
||||
cc.Event.NO_TYPE = 'no_type';
|
||||
|
||||
/**
|
||||
* !#en The type code of Touch event.
|
||||
* !#zh 触摸事件类型
|
||||
* @property TOUCH
|
||||
* @static
|
||||
* @type {String}
|
||||
*/
|
||||
cc.Event.TOUCH = 'touch';
|
||||
/**
|
||||
* !#en The type code of Mouse event.
|
||||
* !#zh 鼠标事件类型
|
||||
* @property MOUSE
|
||||
* @static
|
||||
* @type {String}
|
||||
*/
|
||||
cc.Event.MOUSE = 'mouse';
|
||||
/**
|
||||
* !#en The type code of Keyboard event.
|
||||
* !#zh 键盘事件类型
|
||||
* @property KEYBOARD
|
||||
* @static
|
||||
* @type {String}
|
||||
*/
|
||||
cc.Event.KEYBOARD = 'keyboard';
|
||||
/**
|
||||
* !#en The type code of Acceleration event.
|
||||
* !#zh 加速器事件类型
|
||||
* @property ACCELERATION
|
||||
* @static
|
||||
* @type {String}
|
||||
*/
|
||||
cc.Event.ACCELERATION = 'acceleration';
|
||||
|
||||
//event phase
|
||||
/**
|
||||
* !#en Events not currently dispatched are in this phase
|
||||
* !#zh 尚未派发事件阶段
|
||||
* @property NONE
|
||||
* @type {Number}
|
||||
* @static
|
||||
*/
|
||||
cc.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
|
||||
*/
|
||||
cc.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
|
||||
*/
|
||||
cc.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
|
||||
*/
|
||||
cc.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;
|
||||
30
engine/cocos2d/core/event/index.js
Normal file
30
engine/cocos2d/core/event/index.js
Normal file
@@ -0,0 +1,30 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://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.
|
||||
****************************************************************************/
|
||||
|
||||
require('./event');
|
||||
require('./event-listeners');
|
||||
require('./event-target');
|
||||
require('./system-event');
|
||||
203
engine/cocos2d/core/event/system-event.js
Normal file
203
engine/cocos2d/core/event/system-event.js
Normal file
@@ -0,0 +1,203 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://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 EventTarget = require('../event/event-target');
|
||||
var eventManager = require('../event-manager');
|
||||
var inputManger = require('../platform/CCInputManager');;
|
||||
|
||||
/**
|
||||
* !#en The event type supported by SystemEvent
|
||||
* !#zh SystemEvent 支持的事件类型
|
||||
* @class SystemEvent.EventType
|
||||
* @static
|
||||
* @namespace SystemEvent
|
||||
*/
|
||||
var EventType = cc.Enum({
|
||||
/**
|
||||
* !#en The event type for press the key down event, you can use its value directly: 'keydown'
|
||||
* !#zh 当按下按键时触发的事件
|
||||
* @property KEY_DOWN
|
||||
* @type {String}
|
||||
* @static
|
||||
*/
|
||||
KEY_DOWN: 'keydown',
|
||||
/**
|
||||
* !#en The event type for press the key up event, you can use its value directly: 'keyup'
|
||||
* !#zh 当松开按键时触发的事件
|
||||
* @property KEY_UP
|
||||
* @type {String}
|
||||
* @static
|
||||
*/
|
||||
KEY_UP: 'keyup',
|
||||
/**
|
||||
* !#en The event type for press the devicemotion event, you can use its value directly: 'devicemotion'
|
||||
* !#zh 重力感应
|
||||
* @property DEVICEMOTION
|
||||
* @type {String}
|
||||
* @static
|
||||
*/
|
||||
DEVICEMOTION: 'devicemotion'
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* The System event, it currently supports keyboard events and accelerometer events.<br>
|
||||
* You can get the SystemEvent instance with cc.systemEvent.<br>
|
||||
* !#zh
|
||||
* 系统事件,它目前支持按键事件和重力感应事件。<br>
|
||||
* 你可以通过 cc.systemEvent 获取到 SystemEvent 的实例。<br>
|
||||
* @class SystemEvent
|
||||
* @extends EventTarget
|
||||
* @example
|
||||
* cc.systemEvent.on(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
|
||||
* cc.systemEvent.off(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);
|
||||
*/
|
||||
|
||||
var keyboardListener = null;
|
||||
var accelerationListener = null;
|
||||
var SystemEvent = cc.Class({
|
||||
name: 'SystemEvent',
|
||||
extends: EventTarget,
|
||||
|
||||
statics: {
|
||||
EventType: EventType
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en whether enable accelerometer event
|
||||
* !#zh 是否启用加速度计事件
|
||||
* @method setAccelerometerEnabled
|
||||
* @param {Boolean} isEnable
|
||||
*/
|
||||
setAccelerometerEnabled: function (isEnable) {
|
||||
if (CC_EDITOR) {
|
||||
return;
|
||||
}
|
||||
|
||||
// for iOS 13+
|
||||
if (isEnable && window.DeviceMotionEvent && typeof DeviceMotionEvent.requestPermission === 'function') {
|
||||
DeviceMotionEvent.requestPermission().then(response => {
|
||||
console.log(`Device Motion Event request permission: ${response}`);
|
||||
inputManger.setAccelerometerEnabled(response === 'granted');
|
||||
});
|
||||
} else {
|
||||
inputManger.setAccelerometerEnabled(isEnable);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en set accelerometer interval value
|
||||
* !#zh 设置加速度计间隔值
|
||||
* @method setAccelerometerInterval
|
||||
* @param {Number} interval
|
||||
*/
|
||||
setAccelerometerInterval: function(interval) {
|
||||
if (CC_EDITOR) {
|
||||
return;
|
||||
}
|
||||
inputManger.setAccelerometerInterval(interval);
|
||||
},
|
||||
|
||||
on: function (type, callback, target, once) {
|
||||
if (CC_EDITOR) {
|
||||
return;
|
||||
}
|
||||
this._super(type, callback, target, once);
|
||||
|
||||
// Keyboard
|
||||
if (type === EventType.KEY_DOWN || type === EventType.KEY_UP) {
|
||||
if (!keyboardListener) {
|
||||
keyboardListener = cc.EventListener.create({
|
||||
event: cc.EventListener.KEYBOARD,
|
||||
onKeyPressed: function (keyCode, event) {
|
||||
event.type = EventType.KEY_DOWN;
|
||||
cc.systemEvent.dispatchEvent(event);
|
||||
},
|
||||
onKeyReleased: function (keyCode, event) {
|
||||
event.type = EventType.KEY_UP;
|
||||
cc.systemEvent.dispatchEvent(event);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (!eventManager.hasEventListener(cc.EventListener.ListenerID.KEYBOARD)) {
|
||||
eventManager.addListener(keyboardListener, 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Acceleration
|
||||
if (type === EventType.DEVICEMOTION) {
|
||||
if (!accelerationListener) {
|
||||
accelerationListener = cc.EventListener.create({
|
||||
event: cc.EventListener.ACCELERATION,
|
||||
callback: function (acc, event) {
|
||||
event.type = EventType.DEVICEMOTION;
|
||||
cc.systemEvent.dispatchEvent(event);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (!eventManager.hasEventListener(cc.EventListener.ListenerID.ACCELERATION)) {
|
||||
eventManager.addListener(accelerationListener, 1);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
off: function (type, callback, target) {
|
||||
if (CC_EDITOR) {
|
||||
return;
|
||||
}
|
||||
this._super(type, callback, target);
|
||||
|
||||
// Keyboard
|
||||
if (keyboardListener && (type === EventType.KEY_DOWN || type === EventType.KEY_UP)) {
|
||||
var hasKeyDownEventListener = this.hasEventListener(EventType.KEY_DOWN);
|
||||
var hasKeyUpEventListener = this.hasEventListener(EventType.KEY_UP);
|
||||
if (!hasKeyDownEventListener && !hasKeyUpEventListener) {
|
||||
eventManager.removeListener(keyboardListener);
|
||||
}
|
||||
}
|
||||
|
||||
// Acceleration
|
||||
if (accelerationListener && type === EventType.DEVICEMOTION) {
|
||||
eventManager.removeListener(accelerationListener);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
cc.SystemEvent = module.exports = SystemEvent;
|
||||
/**
|
||||
* @module cc
|
||||
*/
|
||||
|
||||
/**
|
||||
* !#en The System event singleton for global usage
|
||||
* !#zh 系统事件单例,方便全局使用
|
||||
* @property systemEvent
|
||||
* @type {SystemEvent}
|
||||
*/
|
||||
cc.systemEvent = new cc.SystemEvent();
|
||||
Reference in New Issue
Block a user