mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
Merge pull request #73 from shrinktofit/node-events
udpate events related file
This commit is contained in:
@@ -1,578 +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.
|
||||
****************************************************************************/
|
||||
|
||||
import Event from '../../event/event';
|
||||
|
||||
/**
|
||||
* !#en The mouse event
|
||||
* !#zh 鼠标事件类型
|
||||
* @class Event.EventMouse
|
||||
* @extends Event
|
||||
*/
|
||||
export class EventMouse extends Event {
|
||||
/**
|
||||
* @method constructor
|
||||
* @param {Number} eventType - The mouse event type, UP, DOWN, MOVE, CANCELED
|
||||
* @param {Boolean} [bubbles=false] - A boolean indicating whether the event bubbles up through the tree or not
|
||||
*/
|
||||
constructor (eventType, bubbles) {
|
||||
super(Event.MOUSE, bubbles);
|
||||
this._eventType = eventType;
|
||||
this._button = 0;
|
||||
this._x = 0;
|
||||
this._y = 0;
|
||||
this._prevX = 0;
|
||||
this._prevY = 0;
|
||||
this._scrollX = 0;
|
||||
this._scrollY = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Sets scroll data.
|
||||
* !#zh 设置鼠标的滚动数据。
|
||||
* @method setScrollData
|
||||
* @param {Number} scrollX
|
||||
* @param {Number} scrollY
|
||||
*/
|
||||
setScrollData (scrollX, scrollY) {
|
||||
this._scrollX = scrollX;
|
||||
this._scrollY = scrollY;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the x axis scroll value.
|
||||
* !#zh 获取鼠标滚动的X轴距离,只有滚动时才有效。
|
||||
* @method getScrollX
|
||||
* @returns {Number}
|
||||
*/
|
||||
getScrollX () {
|
||||
return this._scrollX;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the y axis scroll value.
|
||||
* !#zh 获取滚轮滚动的 Y 轴距离,只有滚动时才有效。
|
||||
* @method getScrollY
|
||||
* @returns {Number}
|
||||
*/
|
||||
getScrollY () {
|
||||
return this._scrollY;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Sets cursor location.
|
||||
* !#zh 设置当前鼠标位置。
|
||||
* @method setLocation
|
||||
* @param {Number} x
|
||||
* @param {Number} y
|
||||
*/
|
||||
setLocation (x, y) {
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns cursor location.
|
||||
* !#zh 获取鼠标位置对象,对象包含 x 和 y 属性。
|
||||
* @method getLocation
|
||||
* @return {Vec2} location
|
||||
*/
|
||||
getLocation () {
|
||||
return cc.v2(this._x, this._y);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the current cursor location in screen coordinates.
|
||||
* !#zh 获取当前事件在游戏窗口内的坐标位置对象,对象包含 x 和 y 属性。
|
||||
* @method getLocationInView
|
||||
* @return {Vec2}
|
||||
*/
|
||||
getLocationInView() {
|
||||
return cc.v2(this._x, cc.view._designResolutionSize.height - this._y);
|
||||
}
|
||||
|
||||
_setPrevCursor (x, y) {
|
||||
this._prevX = x;
|
||||
this._prevY = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the previous touch location.
|
||||
* !#zh 获取鼠标点击在上一次事件时的位置对象,对象包含 x 和 y 属性。
|
||||
* @method getPreviousLocation
|
||||
* @return {Vec2}
|
||||
*/
|
||||
getPreviousLocation () {
|
||||
return cc.v2(this._prevX, this._prevY);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the delta distance from the previous location to current location.
|
||||
* !#zh 获取鼠标距离上一次事件移动的距离对象,对象包含 x 和 y 属性。
|
||||
* @method getDelta
|
||||
* @return {Vec2}
|
||||
*/
|
||||
getDelta () {
|
||||
return cc.v2(this._x - this._prevX, this._y - this._prevY);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the X axis delta distance from the previous location to current location.
|
||||
* !#zh 获取鼠标距离上一次事件移动的 X 轴距离。
|
||||
* @method getDeltaX
|
||||
* @return {Number}
|
||||
*/
|
||||
getDeltaX () {
|
||||
return this._x - this._prevX;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the Y axis delta distance from the previous location to current location.
|
||||
* !#zh 获取鼠标距离上一次事件移动的 Y 轴距离。
|
||||
* @method getDeltaY
|
||||
* @return {Number}
|
||||
*/
|
||||
getDeltaY () {
|
||||
return this._y - this._prevY;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Sets mouse button.
|
||||
* !#zh 设置鼠标按键。
|
||||
* @method setButton
|
||||
* @param {Number} button
|
||||
*/
|
||||
setButton (button) {
|
||||
this._button = button;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns mouse button.
|
||||
* !#zh 获取鼠标按键。
|
||||
* @method getButton
|
||||
* @returns {Number}
|
||||
*/
|
||||
getButton () {
|
||||
return this._button;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns location X axis data.
|
||||
* !#zh 获取鼠标当前位置 X 轴。
|
||||
* @method getLocationX
|
||||
* @returns {Number}
|
||||
*/
|
||||
getLocationX () {
|
||||
return this._x;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns location Y axis data.
|
||||
* !#zh 获取鼠标当前位置 Y 轴。
|
||||
* @method getLocationY
|
||||
* @returns {Number}
|
||||
*/
|
||||
getLocationY () {
|
||||
return this._y;
|
||||
}
|
||||
}
|
||||
|
||||
//Inner event types of MouseEvent
|
||||
/**
|
||||
* !#en The none event code of mouse event.
|
||||
* !#zh 无。
|
||||
* @property NONE
|
||||
* @static
|
||||
* @type {Number}
|
||||
*/
|
||||
EventMouse.NONE = 0;
|
||||
/**
|
||||
* !#en The event type code of mouse down event.
|
||||
* !#zh 鼠标按下事件。
|
||||
* @property DOWN
|
||||
* @static
|
||||
* @type {Number}
|
||||
*/
|
||||
EventMouse.DOWN = 1;
|
||||
/**
|
||||
* !#en The event type code of mouse up event.
|
||||
* !#zh 鼠标按下后释放事件。
|
||||
* @property UP
|
||||
* @static
|
||||
* @type {Number}
|
||||
*/
|
||||
EventMouse.UP = 2;
|
||||
/**
|
||||
* !#en The event type code of mouse move event.
|
||||
* !#zh 鼠标移动事件。
|
||||
* @property MOVE
|
||||
* @static
|
||||
* @type {Number}
|
||||
*/
|
||||
EventMouse.MOVE = 3;
|
||||
/**
|
||||
* !#en The event type code of mouse scroll event.
|
||||
* !#zh 鼠标滚轮事件。
|
||||
* @property SCROLL
|
||||
* @static
|
||||
* @type {Number}
|
||||
*/
|
||||
EventMouse.SCROLL = 4;
|
||||
|
||||
/**
|
||||
* !#en The tag of Mouse left button.
|
||||
* !#zh 鼠标左键的标签。
|
||||
* @property BUTTON_LEFT
|
||||
* @static
|
||||
* @type {Number}
|
||||
*/
|
||||
EventMouse.BUTTON_LEFT = 0;
|
||||
|
||||
/**
|
||||
* !#en The tag of Mouse right button (The right button number is 2 on browser).
|
||||
* !#zh 鼠标右键的标签。
|
||||
* @property BUTTON_RIGHT
|
||||
* @static
|
||||
* @type {Number}
|
||||
*/
|
||||
EventMouse.BUTTON_RIGHT = 2;
|
||||
|
||||
/**
|
||||
* !#en The tag of Mouse middle button (The right button number is 1 on browser).
|
||||
* !#zh 鼠标中键的标签。
|
||||
* @property BUTTON_MIDDLE
|
||||
* @static
|
||||
* @type {Number}
|
||||
*/
|
||||
EventMouse.BUTTON_MIDDLE = 1;
|
||||
|
||||
/**
|
||||
* !#en The tag of Mouse button 4.
|
||||
* !#zh 鼠标按键 4 的标签。
|
||||
* @property BUTTON_4
|
||||
* @static
|
||||
* @type {Number}
|
||||
*/
|
||||
EventMouse.BUTTON_4 = 3;
|
||||
|
||||
/**
|
||||
* !#en The tag of Mouse button 5.
|
||||
* !#zh 鼠标按键 5 的标签。
|
||||
* @property BUTTON_5
|
||||
* @static
|
||||
* @type {Number}
|
||||
*/
|
||||
EventMouse.BUTTON_5 = 4;
|
||||
|
||||
/**
|
||||
* !#en The tag of Mouse button 6.
|
||||
* !#zh 鼠标按键 6 的标签。
|
||||
* @property BUTTON_6
|
||||
* @static
|
||||
* @type {Number}
|
||||
*/
|
||||
EventMouse.BUTTON_6 = 5;
|
||||
|
||||
/**
|
||||
* !#en The tag of Mouse button 7.
|
||||
* !#zh 鼠标按键 7 的标签。
|
||||
* @property BUTTON_7
|
||||
* @static
|
||||
* @type {Number}
|
||||
*/
|
||||
EventMouse.BUTTON_7 = 6;
|
||||
|
||||
/**
|
||||
* !#en The tag of Mouse button 8.
|
||||
* !#zh 鼠标按键 8 的标签。
|
||||
* @property BUTTON_8
|
||||
* @static
|
||||
* @type {Number}
|
||||
*/
|
||||
EventMouse.BUTTON_8 = 7;
|
||||
|
||||
/**
|
||||
* !#en The touch event
|
||||
* !#zh 触摸事件
|
||||
* @class Event.EventTouch
|
||||
* @constructor
|
||||
* @extends Event
|
||||
*/
|
||||
export class EventTouch extends Event {
|
||||
/**
|
||||
* @method constructor
|
||||
* @param {Array} touchArr - The array of the touches
|
||||
* @param {Boolean} bubbles - A boolean indicating whether the event bubbles up through the tree or not
|
||||
*/
|
||||
constructor (touchArr, bubbles) {
|
||||
super(Event.TOUCH, bubbles);
|
||||
this._eventCode = 0;
|
||||
this._touches = touchArr || [];
|
||||
/**
|
||||
* !#en The current touch object
|
||||
* !#zh 当前触点对象
|
||||
* @property touch
|
||||
* @type {Touch}
|
||||
*/
|
||||
this.touch = null;
|
||||
// Actually duplicated, because of history issue, currentTouch was in the original design, touch was added in creator engine
|
||||
// They should point to the same object
|
||||
this.currentTouch = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns event code.
|
||||
* !#zh 获取事件类型。
|
||||
* @method getEventCode
|
||||
* @returns {Number}
|
||||
*/
|
||||
getEventCode () {
|
||||
return this._eventCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns touches of event.
|
||||
* !#zh 获取触摸点的列表。
|
||||
* @method getTouches
|
||||
* @returns {Array}
|
||||
*/
|
||||
getTouches () {
|
||||
return this._touches;
|
||||
}
|
||||
|
||||
_setEventCode (eventCode) {
|
||||
this._eventCode = eventCode;
|
||||
}
|
||||
|
||||
_setTouches (touches) {
|
||||
this._touches = touches;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Sets touch location.
|
||||
* !#zh 设置当前触点位置
|
||||
* @method setLocation
|
||||
* @param {Number} x
|
||||
* @param {Number} y
|
||||
*/
|
||||
setLocation (x, y) {
|
||||
this.touch && this.touch.setTouchInfo(this.touch.getID(), x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns touch location.
|
||||
* !#zh 获取触点位置。
|
||||
* @method getLocation
|
||||
* @return {Vec2} location
|
||||
*/
|
||||
getLocation () {
|
||||
return this.touch ? this.touch.getLocation() : cc.v2();
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the current touch location in screen coordinates.
|
||||
* !#zh 获取当前触点在游戏窗口中的位置。
|
||||
* @method getLocationInView
|
||||
* @return {Vec2}
|
||||
*/
|
||||
getLocationInView() {
|
||||
return this.touch ? this.touch.getLocationInView() : cc.v2();
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the previous touch location.
|
||||
* !#zh 获取触点在上一次事件时的位置对象,对象包含 x 和 y 属性。
|
||||
* @method getPreviousLocation
|
||||
* @return {Vec2}
|
||||
*/
|
||||
getPreviousLocation () {
|
||||
return this.touch ? this.touch.getPreviousLocation() : cc.v2();
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the start touch location.
|
||||
* !#zh 获获取触点落下时的位置对象,对象包含 x 和 y 属性。
|
||||
* @method getStartLocation
|
||||
* @returns {Vec2}
|
||||
*/
|
||||
getStartLocation() {
|
||||
return this.touch ? this.touch.getStartLocation() : cc.v2();
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the id of cc.Touch.
|
||||
* !#zh 触点的标识 ID,可以用来在多点触摸中跟踪触点。
|
||||
* @method getID
|
||||
* @return {Number}
|
||||
*/
|
||||
getID () {
|
||||
return this.touch ? this.touch.getID() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the delta distance from the previous location to current location.
|
||||
* !#zh 获取触点距离上一次事件移动的距离对象,对象包含 x 和 y 属性。
|
||||
* @method getDelta
|
||||
* @return {Vec2}
|
||||
*/
|
||||
getDelta () {
|
||||
return this.touch ? this.touch.getDelta() : cc.v2();
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the X axis delta distance from the previous location to current location.
|
||||
* !#zh 获取触点距离上一次事件移动的 x 轴距离。
|
||||
* @method getDeltaX
|
||||
* @return {Number}
|
||||
*/
|
||||
getDeltaX () {
|
||||
return this.touch ? this.touch.getDelta().x : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the Y axis delta distance from the previous location to current location.
|
||||
* !#zh 获取触点距离上一次事件移动的 y 轴距离。
|
||||
* @method getDeltaY
|
||||
* @return {Number}
|
||||
*/
|
||||
getDeltaY () {
|
||||
return this.touch ? this.touch.getDelta().y : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns location X axis data.
|
||||
* !#zh 获取当前触点 X 轴位置。
|
||||
* @method getLocationX
|
||||
* @returns {Number}
|
||||
*/
|
||||
getLocationX () {
|
||||
return this.touch ? this.touch.getLocationX() : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns location Y axis data.
|
||||
* !#zh 获取当前触点 Y 轴位置。
|
||||
* @method getLocationY
|
||||
* @returns {Number}
|
||||
*/
|
||||
getLocationY () {
|
||||
return this.touch ? this.touch.getLocationY() : 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The maximum touch numbers
|
||||
* !#zh 最大触摸数量。
|
||||
* @constant
|
||||
* @type {Number}
|
||||
*/
|
||||
EventTouch.MAX_TOUCHES = 5;
|
||||
|
||||
/**
|
||||
* !#en The event type code of touch began event.
|
||||
* !#zh 开始触摸事件
|
||||
* @constant
|
||||
* @type {Number}
|
||||
*/
|
||||
EventTouch.BEGAN = 0;
|
||||
/**
|
||||
* !#en The event type code of touch moved event.
|
||||
* !#zh 触摸后移动事件
|
||||
* @constant
|
||||
* @type {Number}
|
||||
*/
|
||||
EventTouch.MOVED = 1;
|
||||
/**
|
||||
* !#en The event type code of touch ended event.
|
||||
* !#zh 结束触摸事件
|
||||
* @constant
|
||||
* @type {Number}
|
||||
*/
|
||||
EventTouch.ENDED = 2;
|
||||
/**
|
||||
* !#en The event type code of touch cancelled event.
|
||||
* !#zh 取消触摸事件
|
||||
* @constant
|
||||
* @type {Number}
|
||||
*/
|
||||
EventTouch.CANCELED = 3;
|
||||
|
||||
/**
|
||||
* !#en The acceleration event
|
||||
* !#zh 加速度事件
|
||||
* @class Event.EventAcceleration
|
||||
* @extends Event
|
||||
*/
|
||||
export class EventAcceleration extends Event {
|
||||
/**
|
||||
* @method constructor
|
||||
* @param {Object} acc - The acceleration
|
||||
* @param {Boolean} bubbles - A boolean indicating whether the event bubbles up through the tree or not
|
||||
*/
|
||||
constructor (acc, bubbles) {
|
||||
super(Event.ACCELERATION, bubbles);
|
||||
this.acc = acc;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The keyboard event
|
||||
* !#zh 键盘事件
|
||||
* @class Event.EventKeyboard
|
||||
* @extends Event
|
||||
*/
|
||||
export class EventKeyboard extends Event {
|
||||
/**
|
||||
* @method constructor
|
||||
* @param {Number} keyCode - The key code of which triggered this event
|
||||
* @param {Boolean} isPressed - A boolean indicating whether the key have been pressed
|
||||
* @param {Boolean} bubbles - A boolean indicating whether the event bubbles up through the tree or not
|
||||
*/
|
||||
constructor (keyCode, isPressed, bubbles) {
|
||||
super(Event.KEYBOARD, bubbles);
|
||||
/**
|
||||
* !#en
|
||||
* The keyCode read-only property represents a system and implementation dependent numerical code identifying the unmodified value of the pressed key.
|
||||
* This is usually the decimal ASCII (RFC 20) or Windows 1252 code corresponding to the key.
|
||||
* If the key can't be identified, this value is 0.
|
||||
*
|
||||
* !#zh
|
||||
* keyCode 是只读属性它表示一个系统和依赖于实现的数字代码,可以识别按键的未修改值。
|
||||
* 这通常是十进制 ASCII (RFC20) 或者 Windows 1252 代码,所对应的密钥。
|
||||
* 如果无法识别该键,则该值为 0。
|
||||
*
|
||||
* @property keyCode
|
||||
* @type {Number}
|
||||
*/
|
||||
this.keyCode = keyCode;
|
||||
this.isPressed = isPressed;
|
||||
}
|
||||
}
|
||||
|
||||
Event.EventMouse = EventMouse;
|
||||
Event.EventTouch = EventTouch;
|
||||
Event.EventAcceleration = EventAcceleration;
|
||||
Event.EventKeyboard = EventKeyboard;
|
||||
@@ -0,0 +1,510 @@
|
||||
/****************************************************************************
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
import Event from '../../event/event';
|
||||
import { v2 } from '../../value-types/vec2';
|
||||
import Touch from './CCTouch';
|
||||
|
||||
/**
|
||||
* !#en The mouse event
|
||||
* !#zh 鼠标事件类型
|
||||
* @class Event.EventMouse
|
||||
* @extends Event
|
||||
*/
|
||||
export class EventMouse extends Event {
|
||||
// Inner event types of MouseEvent
|
||||
|
||||
/**
|
||||
* !#en The none event code of mouse event.
|
||||
* !#zh 无。
|
||||
*/
|
||||
public static NONE = 0;
|
||||
|
||||
/**
|
||||
* !#en The event type code of mouse down event.
|
||||
* !#zh 鼠标按下事件。
|
||||
*/
|
||||
public static DOWN = 1;
|
||||
|
||||
/**
|
||||
* !#en The event type code of mouse up event.
|
||||
* !#zh 鼠标按下后释放事件。
|
||||
*/
|
||||
public static UP = 2;
|
||||
|
||||
/**
|
||||
* !#en The event type code of mouse move event.
|
||||
* !#zh 鼠标移动事件。
|
||||
*/
|
||||
public static MOVE = 3;
|
||||
|
||||
/**
|
||||
* !#en The event type code of mouse scroll event.
|
||||
* !#zh 鼠标滚轮事件。
|
||||
*/
|
||||
public static SCROLL = 4;
|
||||
|
||||
/**
|
||||
* !#en The tag of Mouse left button.
|
||||
* !#zh 鼠标左键的标签。
|
||||
*/
|
||||
public static BUTTON_LEFT = 0;
|
||||
|
||||
/**
|
||||
* !#en The tag of Mouse right button (The right button number is 2 on browser).
|
||||
* !#zh 鼠标右键的标签。
|
||||
*/
|
||||
public static BUTTON_RIGHT = 2;
|
||||
|
||||
/**
|
||||
* !#en The tag of Mouse middle button (The right button number is 1 on browser).
|
||||
* !#zh 鼠标中键的标签。
|
||||
*/
|
||||
public static BUTTON_MIDDLE = 1;
|
||||
|
||||
/**
|
||||
* !#en The tag of Mouse button 4.
|
||||
* !#zh 鼠标按键 4 的标签。
|
||||
*/
|
||||
public static BUTTON_4 = 3;
|
||||
|
||||
/**
|
||||
* !#en The tag of Mouse button 5.
|
||||
* !#zh 鼠标按键 5 的标签。
|
||||
*/
|
||||
public static BUTTON_5 = 4;
|
||||
|
||||
/**
|
||||
* !#en The tag of Mouse button 6.
|
||||
* !#zh 鼠标按键 6 的标签。
|
||||
*/
|
||||
public static BUTTON_6 = 5;
|
||||
|
||||
/**
|
||||
* !#en The tag of Mouse button 7.
|
||||
* !#zh 鼠标按键 7 的标签。
|
||||
*/
|
||||
public static BUTTON_7 = 6;
|
||||
|
||||
/**
|
||||
* !#en The tag of Mouse button 8.
|
||||
* !#zh 鼠标按键 8 的标签。
|
||||
*/
|
||||
public static BUTTON_8 = 7;
|
||||
|
||||
public movementX: number = 0;
|
||||
|
||||
public movementY: number = 0;
|
||||
|
||||
private _eventType: number;
|
||||
|
||||
private _button: number | null = 0;
|
||||
|
||||
private _x: number = 0;
|
||||
|
||||
private _y: number = 0;
|
||||
|
||||
private _prevX: number = 0;
|
||||
|
||||
private _prevY: number = 0;
|
||||
|
||||
private _scrollX: number = 0;
|
||||
|
||||
private _scrollY: number = 0;
|
||||
|
||||
/**
|
||||
* @param eventType - The mouse event type, UP, DOWN, MOVE, CANCELED
|
||||
* @param [bubbles=false] - A boolean indicating whether the event bubbles up through the tree or not
|
||||
*/
|
||||
constructor (eventType: number, bubbles?: boolean) {
|
||||
super(Event.MOUSE, bubbles);
|
||||
this._eventType = eventType;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Sets scroll data.
|
||||
* !#zh 设置鼠标的滚动数据。
|
||||
*/
|
||||
public setScrollData (scrollX: number, scrollY: number) {
|
||||
this._scrollX = scrollX;
|
||||
this._scrollY = scrollY;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the x axis scroll value.
|
||||
* !#zh 获取鼠标滚动的X轴距离,只有滚动时才有效。
|
||||
*/
|
||||
public getScrollX () {
|
||||
return this._scrollX;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the y axis scroll value.
|
||||
* !#zh 获取滚轮滚动的 Y 轴距离,只有滚动时才有效。
|
||||
*/
|
||||
public getScrollY () {
|
||||
return this._scrollY;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Sets cursor location.
|
||||
* !#zh 设置当前鼠标位置。
|
||||
*/
|
||||
public setLocation (x: number, y: number) {
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns cursor location.
|
||||
* !#zh 获取鼠标位置对象,对象包含 x 和 y 属性。
|
||||
*/
|
||||
public getLocation () {
|
||||
return v2(this._x, this._y);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the current cursor location in screen coordinates.
|
||||
* !#zh 获取当前事件在游戏窗口内的坐标位置对象,对象包含 x 和 y 属性。
|
||||
*/
|
||||
public getLocationInView () {
|
||||
return v2(this._x, cc.view._designResolutionSize.height - this._y);
|
||||
}
|
||||
|
||||
public _setPrevCursor (x: number, y: number) {
|
||||
this._prevX = x;
|
||||
this._prevY = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the previous touch location.
|
||||
* !#zh 获取鼠标点击在上一次事件时的位置对象,对象包含 x 和 y 属性。
|
||||
*/
|
||||
public getPreviousLocation () {
|
||||
return v2(this._prevX, this._prevY);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the delta distance from the previous location to current location.
|
||||
* !#zh 获取鼠标距离上一次事件移动的距离对象,对象包含 x 和 y 属性。
|
||||
*/
|
||||
public getDelta () {
|
||||
return v2(this._x - this._prevX, this._y - this._prevY);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the X axis delta distance from the previous location to current location.
|
||||
* !#zh 获取鼠标距离上一次事件移动的 X 轴距离。
|
||||
*/
|
||||
public getDeltaX () {
|
||||
return this._x - this._prevX;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the Y axis delta distance from the previous location to current location.
|
||||
* !#zh 获取鼠标距离上一次事件移动的 Y 轴距离。
|
||||
*/
|
||||
public getDeltaY () {
|
||||
return this._y - this._prevY;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Sets mouse button.
|
||||
* !#zh 设置鼠标按键。
|
||||
*/
|
||||
public setButton (button: number | null) {
|
||||
this._button = button;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns mouse button.
|
||||
* !#zh 获取鼠标按键。
|
||||
*/
|
||||
public getButton () {
|
||||
return this._button;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns location X axis data.
|
||||
* !#zh 获取鼠标当前位置 X 轴。
|
||||
*/
|
||||
public getLocationX () {
|
||||
return this._x;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns location Y axis data.
|
||||
* !#zh 获取鼠标当前位置 Y 轴。
|
||||
*/
|
||||
public getLocationY () {
|
||||
return this._y;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The touch event
|
||||
* !#zh 触摸事件
|
||||
* @class Event.EventTouch
|
||||
* @constructor
|
||||
* @extends Event
|
||||
*/
|
||||
export class EventTouch extends Event {
|
||||
/**
|
||||
* !#en The maximum touch numbers
|
||||
* !#zh 最大触摸数量。
|
||||
*/
|
||||
public static MAX_TOUCHES = 5;
|
||||
|
||||
/**
|
||||
* !#en The event type code of touch began event.
|
||||
* !#zh 开始触摸事件
|
||||
*/
|
||||
public static BEGAN = 0;
|
||||
/**
|
||||
* !#en The event type code of touch moved event.
|
||||
* !#zh 触摸后移动事件
|
||||
*/
|
||||
public static MOVED = 1;
|
||||
/**
|
||||
* !#en The event type code of touch ended event.
|
||||
* !#zh 结束触摸事件
|
||||
*/
|
||||
public static ENDED = 2;
|
||||
/**
|
||||
* !#en The event type code of touch cancelled event.
|
||||
* !#zh 取消触摸事件
|
||||
*/
|
||||
public static CANCELLED = 3;
|
||||
|
||||
/**
|
||||
* !#en The current touch object
|
||||
* !#zh 当前触点对象
|
||||
*/
|
||||
public touch: Touch | null = null;
|
||||
|
||||
// Actually duplicated, because of history issue,
|
||||
// currentTouch was in the original design, touch was added in creator engine
|
||||
// They should point to the same object
|
||||
public currentTouch: Touch | null = null;
|
||||
|
||||
public _eventCode: number = 0;
|
||||
|
||||
private _touches: Touch[];
|
||||
|
||||
/**
|
||||
* @param touches - The array of the touches
|
||||
* @param bubbles - A boolean indicating whether the event bubbles up through the tree or not
|
||||
*/
|
||||
constructor (touches?: Touch[], bubbles?: boolean) {
|
||||
super(Event.TOUCH, bubbles);
|
||||
this._eventCode = 0;
|
||||
this._touches = touches || [];
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns event code.
|
||||
* !#zh 获取事件类型。
|
||||
*/
|
||||
public getEventCode () {
|
||||
return this._eventCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns touches of event.
|
||||
* !#zh 获取触摸点的列表。
|
||||
*/
|
||||
public getTouches () {
|
||||
return this._touches;
|
||||
}
|
||||
|
||||
public _setEventCode (eventCode: number) {
|
||||
this._eventCode = eventCode;
|
||||
}
|
||||
|
||||
public _setTouches (touches: Touch[]) {
|
||||
this._touches = touches;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Sets touch location.
|
||||
* !#zh 设置当前触点位置
|
||||
*/
|
||||
public setLocation (x: number, y: number) {
|
||||
if (this.touch) {
|
||||
this.touch.setTouchInfo(this.touch.getID(), x, y);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns touch location.
|
||||
* !#zh 获取触点位置。
|
||||
*/
|
||||
public getLocation () {
|
||||
return this.touch ? this.touch.getLocation() : v2();
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the current touch location in screen coordinates.
|
||||
* !#zh 获取当前触点在游戏窗口中的位置。
|
||||
*/
|
||||
public getLocationInView () {
|
||||
return this.touch ? this.touch.getLocationInView() : v2();
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the previous touch location.
|
||||
* !#zh 获取触点在上一次事件时的位置对象,对象包含 x 和 y 属性。
|
||||
*/
|
||||
public getPreviousLocation () {
|
||||
return this.touch ? this.touch.getPreviousLocation() : v2();
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the start touch location.
|
||||
* !#zh 获获取触点落下时的位置对象,对象包含 x 和 y 属性。
|
||||
*/
|
||||
public getStartLocation () {
|
||||
return this.touch ? this.touch.getStartLocation() : v2();
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the id of cc.Touch.
|
||||
* !#zh 触点的标识 ID,可以用来在多点触摸中跟踪触点。
|
||||
*/
|
||||
public getID () {
|
||||
return this.touch ? this.touch.getID() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the delta distance from the previous location to current location.
|
||||
* !#zh 获取触点距离上一次事件移动的距离对象,对象包含 x 和 y 属性。
|
||||
*/
|
||||
public getDelta () {
|
||||
return this.touch ? this.touch.getDelta() : v2();
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the X axis delta distance from the previous location to current location.
|
||||
* !#zh 获取触点距离上一次事件移动的 x 轴距离。
|
||||
*/
|
||||
public getDeltaX () {
|
||||
return this.touch ? this.touch.getDelta().x : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the Y axis delta distance from the previous location to current location.
|
||||
* !#zh 获取触点距离上一次事件移动的 y 轴距离。
|
||||
*/
|
||||
public getDeltaY () {
|
||||
return this.touch ? this.touch.getDelta().y : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns location X axis data.
|
||||
* !#zh 获取当前触点 X 轴位置。
|
||||
*/
|
||||
public getLocationX () {
|
||||
return this.touch ? this.touch.getLocationX() : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns location Y axis data.
|
||||
* !#zh 获取当前触点 Y 轴位置。
|
||||
*/
|
||||
public getLocationY () {
|
||||
return this.touch ? this.touch.getLocationY() : 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The acceleration event
|
||||
* !#zh 加速度事件
|
||||
* @class Event.EventAcceleration
|
||||
* @extends Event
|
||||
*/
|
||||
export class EventAcceleration extends Event {
|
||||
public acc: Object;
|
||||
|
||||
/**
|
||||
* @param acc - The acceleration
|
||||
* @param bubbles - A boolean indicating whether the event bubbles up through the tree or not
|
||||
*/
|
||||
constructor (acc: Object, bubbles?: boolean) {
|
||||
super(Event.ACCELERATION, bubbles);
|
||||
this.acc = acc;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en The keyboard event
|
||||
* !#zh 键盘事件
|
||||
* @class Event.EventKeyboard
|
||||
* @extends Event
|
||||
*/
|
||||
export class EventKeyboard extends Event {
|
||||
/**
|
||||
* !#en
|
||||
* The keyCode read-only property represents a system and implementation dependent numerical code
|
||||
* identifying the unmodified value of the pressed key.
|
||||
* This is usually the decimal ASCII (RFC 20) or Windows 1252 code corresponding to the key.
|
||||
* If the key can't be identified, this value is 0.
|
||||
*
|
||||
* !#zh
|
||||
* keyCode 是只读属性它表示一个系统和依赖于实现的数字代码,可以识别按键的未修改值。
|
||||
* 这通常是十进制 ASCII (RFC20) 或者 Windows 1252 代码,所对应的密钥。
|
||||
* 如果无法识别该键,则该值为 0。
|
||||
*/
|
||||
public keyCode: number;
|
||||
|
||||
public isPressed: boolean;
|
||||
|
||||
/**
|
||||
* @param keyCode - The key code of which triggered this event
|
||||
* @param isPressed - A boolean indicating whether the key have been pressed
|
||||
* @param bubbles - A boolean indicating whether the event bubbles up through the tree or not
|
||||
*/
|
||||
constructor (keyCode: number, isPressed: boolean, bubbles?: boolean) {
|
||||
super(Event.KEYBOARD, bubbles);
|
||||
this.keyCode = keyCode;
|
||||
this.isPressed = isPressed;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
// @ts-ignore
|
||||
Event.EventMouse = EventMouse;
|
||||
|
||||
// TODO
|
||||
// @ts-ignore
|
||||
Event.EventTouch = EventTouch;
|
||||
|
||||
// TODO
|
||||
// @ts-ignore
|
||||
Event.EventAcceleration = EventAcceleration;
|
||||
|
||||
// TODO
|
||||
// @ts-ignore
|
||||
Event.EventKeyboard = EventKeyboard;
|
||||
+232
-228
@@ -24,6 +24,12 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
export interface IEventListenerCreateInfo {
|
||||
event?: number;
|
||||
|
||||
[x: string]: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* <p>
|
||||
@@ -36,32 +42,135 @@
|
||||
* !#zh
|
||||
* 封装用户的事件处理逻辑。
|
||||
* 注意:这是一个抽象类,开发者不应该直接实例化这个类,请参考 {{#crossLink "EventListener/create:method"}}cc.EventListener.create{{/crossLink}}。
|
||||
*
|
||||
* @class EventListener
|
||||
*/
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @method constructor
|
||||
* @param {Number} type
|
||||
* @param {Number} listenerID
|
||||
* @param {Number} callback
|
||||
*/
|
||||
export class EventListener {
|
||||
constructor(type, listenerID, callback) {
|
||||
this._onEvent = callback; // Event callback function
|
||||
this._type = type || 0; // Event listener type
|
||||
this._listenerID = listenerID || ""; // Event listener ID
|
||||
this._registered = false; // Whether the listener has been added to dispatcher.
|
||||
|
||||
this._fixedPriority = 0; // The higher the number, the higher the priority, 0 is for scene graph base priority.
|
||||
this._node = null; // scene graph based priority
|
||||
this._target = null;
|
||||
this._paused = true; // Whether the listener is paused
|
||||
this._isEnabled = true; // Whether the listener is enabled
|
||||
public get onEvent () {
|
||||
return this._onEvent;
|
||||
}
|
||||
/**
|
||||
* !#en The type code of unknown event listener.
|
||||
* !#zh 未知的事件监听器类型
|
||||
*/
|
||||
public static UNKNOWN = 0;
|
||||
|
||||
/*
|
||||
* !#en The type code of one by one touch event listener.
|
||||
* !#zh 触摸事件监听器类型,触点会一个一个得分开被派发
|
||||
*/
|
||||
public static TOUCH_ONE_BY_ONE = 1;
|
||||
|
||||
/*
|
||||
* !#en The type code of all at once touch event listener.
|
||||
* !#zh 触摸事件监听器类型,触点会被一次性全部派发
|
||||
*/
|
||||
public static TOUCH_ALL_AT_ONCE = 2;
|
||||
|
||||
/**
|
||||
* !#en The type code of keyboard event listener.
|
||||
* !#zh 键盘事件监听器类型
|
||||
*/
|
||||
public static KEYBOARD = 3;
|
||||
|
||||
/*
|
||||
* !#en The type code of mouse event listener.
|
||||
* !#zh 鼠标事件监听器类型
|
||||
*/
|
||||
public static MOUSE = 4;
|
||||
|
||||
/**
|
||||
* !#en The type code of acceleration event listener.
|
||||
* !#zh 加速器事件监听器类型
|
||||
*/
|
||||
public static ACCELERATION = 6;
|
||||
|
||||
/*
|
||||
* !#en The type code of custom event listener.
|
||||
* !#zh 自定义事件监听器类型
|
||||
*/
|
||||
public static CUSTOM = 8;
|
||||
|
||||
public static ListenerID = {
|
||||
MOUSE: '__cc_mouse',
|
||||
TOUCH_ONE_BY_ONE: '__cc_touch_one_by_one',
|
||||
TOUCH_ALL_AT_ONCE: '__cc_touch_all_at_once',
|
||||
KEYBOARD: '__cc_keyboard',
|
||||
ACCELERATION: '__cc_acceleration',
|
||||
};
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Create a EventListener object with configuration including the event type, handlers and other parameters.
|
||||
* In handlers, this refer to the event listener object itself.
|
||||
* You can also pass custom parameters in the configuration object,
|
||||
* all custom parameters will be polyfilled into the event listener object and can be accessed in handlers.
|
||||
* !#zh 通过指定不同的 Event 对象来设置想要创建的事件监听器。
|
||||
* @param {Object} argObj a json object
|
||||
* @example {@link cocos2d/core/event-manager/CCEventListener/create.js}
|
||||
*/
|
||||
public static create (argObj: IEventListenerCreateInfo): EventListener {
|
||||
cc.assertID(argObj && argObj.event, 1900);
|
||||
|
||||
const listenerType = argObj.event;
|
||||
delete argObj.event;
|
||||
|
||||
let listener: EventListener | null = null;
|
||||
if (listenerType === cc.EventListener.TOUCH_ONE_BY_ONE) {
|
||||
listener = new TouchOneByOne();
|
||||
} else if (listenerType === cc.EventListener.TOUCH_ALL_AT_ONCE) {
|
||||
listener = new TouchAllAtOnce();
|
||||
} else if (listenerType === cc.EventListener.MOUSE) {
|
||||
listener = new Mouse();
|
||||
} else if (listenerType === cc.EventListener.KEYBOARD) {
|
||||
listener = new Keyboard();
|
||||
} else if (listenerType === cc.EventListener.ACCELERATION) {
|
||||
listener = new Acceleration(argObj.callback);
|
||||
delete argObj.callback;
|
||||
}
|
||||
|
||||
if (listener) {
|
||||
for (const key of Object.keys(argObj)) {
|
||||
listener[key] = argObj[key];
|
||||
}
|
||||
}
|
||||
|
||||
return listener!;
|
||||
}
|
||||
|
||||
public _target: any = null;
|
||||
|
||||
// Event callback function
|
||||
protected _onEvent: ((...args: any[]) => any) | null;
|
||||
|
||||
// Event listener type
|
||||
private _type: number;
|
||||
|
||||
// Event listener ID
|
||||
private _listenerID: string;
|
||||
|
||||
// Whether the listener has been added to dispatcher.
|
||||
private _registered = false;
|
||||
|
||||
// The higher the number, the higher the priority, 0 is for scene graph base priority.
|
||||
private _fixedPriority = 0;
|
||||
|
||||
// scene graph based priority
|
||||
// @type {Node}
|
||||
private _node: any = null; // scene graph based priority
|
||||
|
||||
// Whether the listener is paused
|
||||
private _paused = true; // Whether the listener is paused
|
||||
|
||||
// Whether the listener is enabled
|
||||
private _isEnabled = true; // Whether the listener is enabled
|
||||
|
||||
constructor (type: number, listenerID: string, callback: ((...args: any[]) => any) | null) {
|
||||
this._onEvent = callback;
|
||||
this._type = type || 0;
|
||||
this._listenerID = listenerID || '';
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Sets paused state for the listener
|
||||
* The paused state is only used for scene graph priority listeners.
|
||||
@@ -69,247 +178,176 @@ export class EventListener {
|
||||
* while `EventDispatcher::pauseAllEventListenersForTarget(node)` will set it to `false`.
|
||||
* @note 1) Fixed priority listeners will never get paused. If a fixed priority doesn't want to receive events,
|
||||
* call `setEnabled(false)` instead.
|
||||
* 2) In `Node`'s onEnter and onExit, the `paused state` of the listeners which associated with that node will be automatically updated.
|
||||
* 2) In `Node`'s onEnter and onExit, the `paused state` of the listeners
|
||||
* which associated with that node will be automatically updated.
|
||||
* </p>
|
||||
* @param {Boolean} paused
|
||||
* @private
|
||||
*/
|
||||
_setPaused(paused) {
|
||||
public _setPaused (paused: boolean) {
|
||||
this._paused = paused;
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* Checks whether the listener is paused.
|
||||
* @returns {Boolean}
|
||||
* @private
|
||||
*/
|
||||
_isPaused() {
|
||||
public _isPaused () {
|
||||
return this._paused;
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* Marks the listener was registered by EventDispatcher.
|
||||
* @param {Boolean} registered
|
||||
* @private
|
||||
*/
|
||||
_setRegistered(registered) {
|
||||
public _setRegistered (registered: boolean) {
|
||||
this._registered = registered;
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* Checks whether the listener was registered by EventDispatcher
|
||||
* @returns {Boolean}
|
||||
* @private
|
||||
*/
|
||||
_isRegistered() {
|
||||
public _isRegistered () {
|
||||
return this._registered;
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* Gets the type of this listener
|
||||
* @note It's different from `EventType`, e.g. TouchEvent has two kinds of event listeners - EventListenerOneByOne, EventListenerAllAtOnce
|
||||
* @returns {Number}
|
||||
* @note It's different from `EventType`, e.g.
|
||||
* TouchEvent has two kinds of event listeners - EventListenerOneByOne, EventListenerAllAtOnce
|
||||
* @private
|
||||
*/
|
||||
_getType() {
|
||||
public _getType () {
|
||||
return this._type;
|
||||
}
|
||||
|
||||
/*
|
||||
* Gets the listener ID of this listener
|
||||
* When event is being dispatched, listener ID is used as key for searching listeners according to event type.
|
||||
* @returns {String}
|
||||
/**
|
||||
* Gets the listener ID of this listener
|
||||
* When event is being dispatched, listener ID is used as key for searching listeners according to event type.
|
||||
* @private
|
||||
*/
|
||||
_getListenerID() {
|
||||
public _getListenerID () {
|
||||
return this._listenerID;
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* Sets the fixed priority for this listener
|
||||
* @note This method is only used for `fixed priority listeners`, it needs to access a non-zero value. 0 is reserved for scene graph priority listeners
|
||||
* @param {Number} fixedPriority
|
||||
* @note This method is only used for `fixed priority listeners`,
|
||||
* it needs to access a non-zero value. 0 is reserved for scene graph priority listeners
|
||||
* @private
|
||||
*/
|
||||
_setFixedPriority(fixedPriority) {
|
||||
public _setFixedPriority (fixedPriority: number) {
|
||||
this._fixedPriority = fixedPriority;
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* Gets the fixed priority of this listener
|
||||
* @returns {Number} 0 if it's a scene graph priority listener, non-zero for fixed priority listener
|
||||
* @return 0 if it's a scene graph priority listener, non-zero for fixed priority listener
|
||||
* @private
|
||||
*/
|
||||
_getFixedPriority() {
|
||||
public _getFixedPriority () {
|
||||
return this._fixedPriority;
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* Sets scene graph priority for this listener
|
||||
* @param {cc.Node} node
|
||||
* @private
|
||||
* @param {Node} node
|
||||
*/
|
||||
_setSceneGraphPriority(node) {
|
||||
public _setSceneGraphPriority (node: any) {
|
||||
this._target = node;
|
||||
this._node = node;
|
||||
}
|
||||
|
||||
/*
|
||||
/**
|
||||
* Gets scene graph priority of this listener
|
||||
* @returns {cc.Node} if it's a fixed priority listener, non-null for scene graph priority listener
|
||||
* @return if it's a fixed priority listener, non-null for scene graph priority listener
|
||||
* @private
|
||||
*/
|
||||
_getSceneGraphPriority() {
|
||||
public _getSceneGraphPriority () {
|
||||
return this._node;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Checks whether the listener is available.
|
||||
* !#zh 检测监听器是否有效
|
||||
* @method checkAvailable
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
checkAvailable() {
|
||||
public checkAvailable () {
|
||||
return this._onEvent !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Clones the listener, its subclasses have to override this method.
|
||||
* !#zh 克隆监听器,它的子类必须重写此方法。
|
||||
* @method clone
|
||||
* @returns {EventListener}
|
||||
*/
|
||||
clone() {
|
||||
public clone (): EventListener | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Enables or disables the listener
|
||||
* !#zh 启用或禁用监听器。
|
||||
* @method setEnabled
|
||||
* @param {Boolean} enabled
|
||||
* @note Only listeners with `enabled` state will be able to receive events.
|
||||
* When an listener was initialized, it's enabled by default.
|
||||
* An event listener can receive events when it is enabled and is not paused.
|
||||
* paused state is always false when it is a fixed priority listener.
|
||||
*/
|
||||
setEnabled(enabled) {
|
||||
public setEnabled (enabled: boolean) {
|
||||
this._isEnabled = enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Checks whether the listener is enabled
|
||||
* !#zh 检查监听器是否可用。
|
||||
* @method isEnabled
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
isEnabled() {
|
||||
public isEnabled () {
|
||||
return this._isEnabled;
|
||||
}
|
||||
}
|
||||
|
||||
// event listener type
|
||||
/**
|
||||
* !#en The type code of unknown event listener.
|
||||
* !#zh 未知的事件监听器类型
|
||||
* @property UNKNOWN
|
||||
* @type {Number}
|
||||
* @static
|
||||
*/
|
||||
EventListener.UNKNOWN = 0;
|
||||
/*
|
||||
* !#en The type code of one by one touch event listener.
|
||||
* !#zh 触摸事件监听器类型,触点会一个一个得分开被派发
|
||||
* @property TOUCH_ONE_BY_ONE
|
||||
* @type {Number}
|
||||
* @static
|
||||
*/
|
||||
EventListener.TOUCH_ONE_BY_ONE = 1;
|
||||
/*
|
||||
* !#en The type code of all at once touch event listener.
|
||||
* !#zh 触摸事件监听器类型,触点会被一次性全部派发
|
||||
* @property TOUCH_ALL_AT_ONCE
|
||||
* @type {Number}
|
||||
* @static
|
||||
*/
|
||||
EventListener.TOUCH_ALL_AT_ONCE = 2;
|
||||
/**
|
||||
* !#en The type code of keyboard event listener.
|
||||
* !#zh 键盘事件监听器类型
|
||||
* @property KEYBOARD
|
||||
* @type {Number}
|
||||
* @static
|
||||
*/
|
||||
EventListener.KEYBOARD = 3;
|
||||
/*
|
||||
* !#en The type code of mouse event listener.
|
||||
* !#zh 鼠标事件监听器类型
|
||||
* @property MOUSE
|
||||
* @type {Number}
|
||||
* @static
|
||||
*/
|
||||
EventListener.MOUSE = 4;
|
||||
/**
|
||||
* !#en The type code of acceleration event listener.
|
||||
* !#zh 加速器事件监听器类型
|
||||
* @property ACCELERATION
|
||||
* @type {Number}
|
||||
* @static
|
||||
*/
|
||||
EventListener.ACCELERATION = 6;
|
||||
/*
|
||||
* !#en The type code of custom event listener.
|
||||
* !#zh 自定义事件监听器类型
|
||||
* @property CUSTOM
|
||||
* @type {Number}
|
||||
* @static
|
||||
*/
|
||||
EventListener.CUSTOM = 8;
|
||||
|
||||
var ListenerID = EventListener.ListenerID = {
|
||||
MOUSE: '__cc_mouse',
|
||||
TOUCH_ONE_BY_ONE: '__cc_touch_one_by_one',
|
||||
TOUCH_ALL_AT_ONCE: '__cc_touch_all_at_once',
|
||||
KEYBOARD: '__cc_keyboard',
|
||||
ACCELERATION: '__cc_acceleration',
|
||||
};
|
||||
const ListenerID = EventListener.ListenerID;
|
||||
|
||||
export class Mouse extends EventListener {
|
||||
constructor() {
|
||||
super(EventListener.MOUSE, ListenerID.MOUSE);
|
||||
public onMouseDown: Function | null = null;
|
||||
public onMouseUp: Function | null = null;
|
||||
public onMouseMove: Function | null = null;
|
||||
public onMouseScroll: Function | null = null;
|
||||
|
||||
constructor () {
|
||||
super(EventListener.MOUSE, ListenerID.MOUSE, null);
|
||||
this._onEvent = this._callback;
|
||||
this.onMouseDown = null;
|
||||
this.onMouseUp = null;
|
||||
this.onMouseMove = null;
|
||||
this.onMouseScroll = null;
|
||||
}
|
||||
|
||||
_callback(event) {
|
||||
var eventType = cc.Event.EventMouse;
|
||||
public _callback (event) {
|
||||
const eventType = cc.Event.EventMouse;
|
||||
switch (event._eventType) {
|
||||
case eventType.DOWN:
|
||||
if (this.onMouseDown)
|
||||
if (this.onMouseDown) {
|
||||
this.onMouseDown(event);
|
||||
}
|
||||
break;
|
||||
case eventType.UP:
|
||||
if (this.onMouseUp)
|
||||
if (this.onMouseUp) {
|
||||
this.onMouseUp(event);
|
||||
}
|
||||
break;
|
||||
case eventType.MOVE:
|
||||
if (this.onMouseMove)
|
||||
if (this.onMouseMove) {
|
||||
this.onMouseMove(event);
|
||||
}
|
||||
break;
|
||||
case eventType.SCROLL:
|
||||
if (this.onMouseScroll)
|
||||
if (this.onMouseScroll) {
|
||||
this.onMouseScroll(event);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
clone() {
|
||||
var eventListener = new Mouse();
|
||||
public clone () {
|
||||
const eventListener = new Mouse();
|
||||
eventListener.onMouseDown = this.onMouseDown;
|
||||
eventListener.onMouseUp = this.onMouseUp;
|
||||
eventListener.onMouseMove = this.onMouseMove;
|
||||
@@ -317,34 +355,34 @@ export class Mouse extends EventListener {
|
||||
return eventListener;
|
||||
}
|
||||
|
||||
checkAvailable() {
|
||||
public checkAvailable () {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
export class TouchOneByOne extends EventListener {
|
||||
constructor() {
|
||||
super(EventListener.TOUCH_ONE_BY_ONE, ListenerID.TOUCH_ONE_BY_ONE);
|
||||
this._claimedTouches = [];
|
||||
public swallowTouches = false;
|
||||
public onTouchBegan: Function | null = null;
|
||||
public onTouchMoved: Function | null = null;
|
||||
public onTouchEnded: Function | null = null;
|
||||
public onTouchCancelled: Function | null = null;
|
||||
|
||||
// this._claimedTouches = null;
|
||||
this.swallowTouches = false;
|
||||
this.onTouchBegan = null;
|
||||
this.onTouchMoved = null;
|
||||
this.onTouchEnded = null;
|
||||
this.onTouchCancelled = null;
|
||||
private _claimedTouches: any[] = [];
|
||||
|
||||
constructor () {
|
||||
super(EventListener.TOUCH_ONE_BY_ONE, ListenerID.TOUCH_ONE_BY_ONE, null);
|
||||
}
|
||||
|
||||
setSwallowTouches(needSwallow) {
|
||||
public setSwallowTouches (needSwallow) {
|
||||
this.swallowTouches = needSwallow;
|
||||
}
|
||||
|
||||
isSwallowTouches() {
|
||||
public isSwallowTouches () {
|
||||
return this.swallowTouches;
|
||||
}
|
||||
|
||||
clone() {
|
||||
var eventListener = new TouchOneByOne();
|
||||
public clone () {
|
||||
const eventListener = new TouchOneByOne();
|
||||
eventListener.onTouchBegan = this.onTouchBegan;
|
||||
eventListener.onTouchMoved = this.onTouchMoved;
|
||||
eventListener.onTouchEnded = this.onTouchEnded;
|
||||
@@ -353,7 +391,7 @@ export class TouchOneByOne extends EventListener {
|
||||
return eventListener;
|
||||
}
|
||||
|
||||
checkAvailable() {
|
||||
public checkAvailable () {
|
||||
if (!this.onTouchBegan) {
|
||||
cc.logID(1801);
|
||||
return false;
|
||||
@@ -363,17 +401,17 @@ export class TouchOneByOne extends EventListener {
|
||||
}
|
||||
|
||||
export class TouchAllAtOnce extends EventListener {
|
||||
constructor() {
|
||||
super(EventListener.TOUCH_ALL_AT_ONCE, ListenerID.TOUCH_ALL_AT_ONCE);
|
||||
public onTouchesBegan: Function | null = null;
|
||||
public onTouchesMoved: Function | null = null;
|
||||
public onTouchesEnded: Function | null = null;
|
||||
public onTouchesCancelled: Function | null = null;
|
||||
|
||||
this.onTouchesBegan = null;
|
||||
this.onTouchesMoved = null;
|
||||
this.onTouchesEnded = null;
|
||||
this.onTouchesCancelled = null;
|
||||
constructor () {
|
||||
super(EventListener.TOUCH_ALL_AT_ONCE, ListenerID.TOUCH_ALL_AT_ONCE, null);
|
||||
}
|
||||
|
||||
clone() {
|
||||
var eventListener = new TouchAllAtOnce();
|
||||
public clone () {
|
||||
const eventListener = new TouchAllAtOnce();
|
||||
eventListener.onTouchesBegan = this.onTouchesBegan;
|
||||
eventListener.onTouchesMoved = this.onTouchesMoved;
|
||||
eventListener.onTouchesEnded = this.onTouchesEnded;
|
||||
@@ -381,7 +419,7 @@ export class TouchAllAtOnce extends EventListener {
|
||||
return eventListener;
|
||||
}
|
||||
|
||||
checkAvailable() {
|
||||
public checkAvailable () {
|
||||
if (this.onTouchesBegan === null && this.onTouchesMoved === null
|
||||
&& this.onTouchesEnded === null && this.onTouchesCancelled === null) {
|
||||
cc.logID(1802);
|
||||
@@ -391,57 +429,62 @@ export class TouchAllAtOnce extends EventListener {
|
||||
}
|
||||
}
|
||||
|
||||
//Acceleration
|
||||
// Acceleration
|
||||
export class Acceleration extends EventListener {
|
||||
constructor(callback) {
|
||||
super(EventListener.ACCELERATION, ListenerID.ACCELERATION);
|
||||
public _onAccelerationEvent: Function | null = null;
|
||||
|
||||
constructor (callback: Function | null) {
|
||||
super(EventListener.ACCELERATION, ListenerID.ACCELERATION, null);
|
||||
this._onEvent = this._callback;
|
||||
this._onAccelerationEvent = callback;
|
||||
this_onAccelerationEvent = null;
|
||||
}
|
||||
|
||||
_callback(event) {
|
||||
this._onAccelerationEvent(event.acc, event);
|
||||
public _callback (event) {
|
||||
if (this._onAccelerationEvent) {
|
||||
this._onAccelerationEvent(event.acc, event);
|
||||
}
|
||||
}
|
||||
|
||||
checkAvailable() {
|
||||
public checkAvailable () {
|
||||
cc.assertID(this._onAccelerationEvent, 1803);
|
||||
return true;
|
||||
}
|
||||
|
||||
clone() {
|
||||
public clone () {
|
||||
return new Acceleration(this._onAccelerationEvent);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Keyboard
|
||||
// Keyboard
|
||||
export class Keyboard extends EventListener {
|
||||
constructor() {
|
||||
super(EventListener.KEYBOARD, ListenerID.KEYBOARD);
|
||||
public onKeyPressed: Function | null = null;
|
||||
public onKeyReleased: Function | null = null;
|
||||
|
||||
constructor () {
|
||||
super(EventListener.KEYBOARD, ListenerID.KEYBOARD, null);
|
||||
this._onEvent = this._callback;
|
||||
this.onKeyPressed = null;
|
||||
this.onKeyReleased = null;
|
||||
}
|
||||
|
||||
_callback(event) {
|
||||
public _callback (event) {
|
||||
if (event.isPressed) {
|
||||
if (this.onKeyPressed)
|
||||
if (this.onKeyPressed) {
|
||||
this.onKeyPressed(event.keyCode, event);
|
||||
}
|
||||
} else {
|
||||
if (this.onKeyReleased)
|
||||
if (this.onKeyReleased) {
|
||||
this.onKeyReleased(event.keyCode, event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
clone() {
|
||||
var eventListener = new Keyboard();
|
||||
public clone () {
|
||||
const eventListener = new Keyboard();
|
||||
eventListener.onKeyPressed = this.onKeyPressed;
|
||||
eventListener.onKeyReleased = this.onKeyReleased;
|
||||
return eventListener;
|
||||
}
|
||||
|
||||
checkAvailable() {
|
||||
public checkAvailable () {
|
||||
if (this.onKeyPressed === null && this.onKeyReleased === null) {
|
||||
cc.logID(1800);
|
||||
return false;
|
||||
@@ -450,43 +493,4 @@ export class Keyboard extends EventListener {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Create a EventListener object with configuration including the event type, handlers and other parameters.
|
||||
* In handlers, this refer to the event listener object itself.
|
||||
* You can also pass custom parameters in the configuration object,
|
||||
* all custom parameters will be polyfilled into the event listener object and can be accessed in handlers.
|
||||
* !#zh 通过指定不同的 Event 对象来设置想要创建的事件监听器。
|
||||
* @method create
|
||||
* @param {Object} argObj a json object
|
||||
* @returns {EventListener}
|
||||
* @static
|
||||
* @example {@link cocos2d/core/event-manager/CCEventListener/create.js}
|
||||
*/
|
||||
EventListener.create = function (argObj) {
|
||||
cc.assertID(argObj && argObj.event, 1900);
|
||||
|
||||
var listenerType = argObj.event;
|
||||
delete argObj.event;
|
||||
|
||||
var listener = null;
|
||||
if (listenerType === cc.EventListener.TOUCH_ONE_BY_ONE)
|
||||
listener = new TouchOneByOne();
|
||||
else if (listenerType === cc.EventListener.TOUCH_ALL_AT_ONCE)
|
||||
listener = new TouchAllAtOnce();
|
||||
else if (listenerType === cc.EventListener.MOUSE)
|
||||
listener = new Mouse();
|
||||
else if (listenerType === cc.EventListener.KEYBOARD)
|
||||
listener = new Keyboard();
|
||||
else if (listenerType === cc.EventListener.ACCELERATION) {
|
||||
listener = new Acceleration(argObj.callback);
|
||||
delete argObj.callback;
|
||||
}
|
||||
|
||||
for (var key in argObj) {
|
||||
listener[key] = argObj[key];
|
||||
}
|
||||
return listener;
|
||||
}
|
||||
|
||||
cc.EventListener = EventListener;
|
||||
+816
-722
File diff suppressed because it is too large
Load Diff
@@ -1,759 +0,0 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2011-2012 cocos2d-x.org
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
import macro from '../CCMacro';
|
||||
import sys from '../CCSys';
|
||||
import eventManager from './CCEventManager';
|
||||
|
||||
const TOUCH_TIMEOUT = macro.TOUCH_TIMEOUT;
|
||||
|
||||
const PORTRAIT = 0;
|
||||
const LANDSCAPE_LEFT = -90;
|
||||
const PORTRAIT_UPSIDE_DOWN = 180;
|
||||
const LANDSCAPE_RIGHT = 90;
|
||||
|
||||
let _didAccelerateFun;
|
||||
let _vec2 = cc.v2();
|
||||
|
||||
/**
|
||||
* !#en the device accelerometer reports values for each axis in units of g-force.
|
||||
* !#zh 设备重力传感器传递的各个轴的数据。
|
||||
* @class Acceleration
|
||||
* @method constructor
|
||||
* @param {Number} x
|
||||
* @param {Number} y
|
||||
* @param {Number} z
|
||||
* @param {Number} timestamp
|
||||
*/
|
||||
let Acceleration = function (x, y, z, timestamp) {
|
||||
this.x = x || 0;
|
||||
this.y = y || 0;
|
||||
this.z = z || 0;
|
||||
this.timestamp = timestamp || 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* This class manages all events of input. include: touch, mouse, accelerometer, keyboard
|
||||
*/
|
||||
let inputManager = {
|
||||
_mousePressed: false,
|
||||
|
||||
_isRegisterEvent: false,
|
||||
|
||||
_preTouchPoint: cc.v2(0,0),
|
||||
_prevMousePoint: cc.v2(0,0),
|
||||
|
||||
_preTouchPool: [],
|
||||
_preTouchPoolPointer: 0,
|
||||
|
||||
_touches: [],
|
||||
_touchesIntegerDict:{},
|
||||
|
||||
_indexBitsUsed: 0,
|
||||
_maxTouches: 8,
|
||||
|
||||
_accelEnabled: false,
|
||||
_accelInterval: 1/5,
|
||||
_accelMinus: 1,
|
||||
_accelCurTime: 0,
|
||||
_acceleration: null,
|
||||
_accelDeviceEvent: null,
|
||||
|
||||
_getUnUsedIndex () {
|
||||
let temp = this._indexBitsUsed;
|
||||
let now = cc.sys.now();
|
||||
|
||||
for (let i = 0; i < this._maxTouches; i++) {
|
||||
if (!(temp & 0x00000001)) {
|
||||
this._indexBitsUsed |= (1 << i);
|
||||
return i;
|
||||
}
|
||||
else {
|
||||
let touch = this._touches[i];
|
||||
if (now - touch._lastModified > TOUCH_TIMEOUT) {
|
||||
this._removeUsedIndexBit(i);
|
||||
delete this._touchesIntegerDict[touch.getID()];
|
||||
return i;
|
||||
}
|
||||
}
|
||||
temp >>= 1;
|
||||
}
|
||||
|
||||
// all bits are used
|
||||
return -1;
|
||||
},
|
||||
|
||||
_removeUsedIndexBit (index) {
|
||||
if (index < 0 || index >= this._maxTouches)
|
||||
return;
|
||||
|
||||
let temp = 1 << index;
|
||||
temp = ~temp;
|
||||
this._indexBitsUsed &= temp;
|
||||
},
|
||||
|
||||
_glView: null,
|
||||
|
||||
/**
|
||||
* @method handleTouchesBegin
|
||||
* @param {Array} touches
|
||||
*/
|
||||
handleTouchesBegin (touches) {
|
||||
let selTouch, index, curTouch, touchID,
|
||||
handleTouches = [], locTouchIntDict = this._touchesIntegerDict,
|
||||
now = sys.now();
|
||||
for (let i = 0, len = touches.length; i < len; i ++) {
|
||||
selTouch = touches[i];
|
||||
touchID = selTouch.getID();
|
||||
index = locTouchIntDict[touchID];
|
||||
|
||||
if (index == null) {
|
||||
let unusedIndex = this._getUnUsedIndex();
|
||||
if (unusedIndex === -1) {
|
||||
cc.logID(2300, unusedIndex);
|
||||
continue;
|
||||
}
|
||||
//curTouch = this._touches[unusedIndex] = selTouch;
|
||||
curTouch = this._touches[unusedIndex] = new cc.Touch(selTouch._point.x, selTouch._point.y, selTouch.getID());
|
||||
curTouch._lastModified = now;
|
||||
curTouch._setPrevPoint(selTouch._prevPoint);
|
||||
locTouchIntDict[touchID] = unusedIndex;
|
||||
handleTouches.push(curTouch);
|
||||
}
|
||||
}
|
||||
if (handleTouches.length > 0) {
|
||||
this._glView._convertTouchesWithScale(handleTouches);
|
||||
let touchEvent = new cc.Event.EventTouch(handleTouches);
|
||||
touchEvent._eventCode = cc.Event.EventTouch.BEGAN;
|
||||
eventManager.dispatchEvent(touchEvent);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @method handleTouchesMove
|
||||
* @param {Array} touches
|
||||
*/
|
||||
handleTouchesMove (touches) {
|
||||
let selTouch, index, touchID,
|
||||
handleTouches = [], locTouches = this._touches,
|
||||
now = sys.now();
|
||||
for (let i = 0, len = touches.length; i < len; i++) {
|
||||
selTouch = touches[i];
|
||||
touchID = selTouch.getID();
|
||||
index = this._touchesIntegerDict[touchID];
|
||||
|
||||
if (index == null) {
|
||||
//cc.log("if the index doesn't exist, it is an error");
|
||||
continue;
|
||||
}
|
||||
if (locTouches[index]) {
|
||||
locTouches[index]._setPoint(selTouch._point);
|
||||
locTouches[index]._setPrevPoint(selTouch._prevPoint);
|
||||
locTouches[index]._lastModified = now;
|
||||
handleTouches.push(locTouches[index]);
|
||||
}
|
||||
}
|
||||
if (handleTouches.length > 0) {
|
||||
this._glView._convertTouchesWithScale(handleTouches);
|
||||
let touchEvent = new cc.Event.EventTouch(handleTouches);
|
||||
touchEvent._eventCode = cc.Event.EventTouch.MOVED;
|
||||
eventManager.dispatchEvent(touchEvent);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @method handleTouchesEnd
|
||||
* @param {Array} touches
|
||||
*/
|
||||
handleTouchesEnd (touches) {
|
||||
let handleTouches = this.getSetOfTouchesEndOrCancel(touches);
|
||||
if (handleTouches.length > 0) {
|
||||
this._glView._convertTouchesWithScale(handleTouches);
|
||||
let touchEvent = new cc.Event.EventTouch(handleTouches);
|
||||
touchEvent._eventCode = cc.Event.EventTouch.ENDED;
|
||||
eventManager.dispatchEvent(touchEvent);
|
||||
}
|
||||
this._preTouchPool.length = 0;
|
||||
},
|
||||
|
||||
/**
|
||||
* @method handleTouchesCancel
|
||||
* @param {Array} touches
|
||||
*/
|
||||
handleTouchesCancel (touches) {
|
||||
let handleTouches = this.getSetOfTouchesEndOrCancel(touches);
|
||||
if (handleTouches.length > 0) {
|
||||
this._glView._convertTouchesWithScale(handleTouches);
|
||||
let touchEvent = new cc.Event.EventTouch(handleTouches);
|
||||
touchEvent._eventCode = cc.Event.EventTouch.CANCELLED;
|
||||
eventManager.dispatchEvent(touchEvent);
|
||||
}
|
||||
this._preTouchPool.length = 0;
|
||||
},
|
||||
|
||||
/**
|
||||
* @method getSetOfTouchesEndOrCancel
|
||||
* @param {Array} touches
|
||||
* @returns {Array}
|
||||
*/
|
||||
getSetOfTouchesEndOrCancel (touches) {
|
||||
let selTouch, index, touchID, handleTouches = [], locTouches = this._touches, locTouchesIntDict = this._touchesIntegerDict;
|
||||
for (let i = 0, len = touches.length; i< len; i ++) {
|
||||
selTouch = touches[i];
|
||||
touchID = selTouch.getID();
|
||||
index = locTouchesIntDict[touchID];
|
||||
|
||||
if (index == null) {
|
||||
continue; //cc.log("if the index doesn't exist, it is an error");
|
||||
}
|
||||
if (locTouches[index]) {
|
||||
locTouches[index]._setPoint(selTouch._point);
|
||||
locTouches[index]._setPrevPoint(selTouch._prevPoint);
|
||||
handleTouches.push(locTouches[index]);
|
||||
this._removeUsedIndexBit(index);
|
||||
delete locTouchesIntDict[touchID];
|
||||
}
|
||||
}
|
||||
return handleTouches;
|
||||
},
|
||||
|
||||
/**
|
||||
* @method getHTMLElementPosition
|
||||
* @param {HTMLElement} element
|
||||
* @return {Object}
|
||||
*/
|
||||
getHTMLElementPosition (element) {
|
||||
if (sys.platform === sys.WECHAT_GAME) {
|
||||
return {
|
||||
left: 0,
|
||||
top: 0,
|
||||
width: window.innerWidth,
|
||||
height: window.innerHeight
|
||||
};
|
||||
}
|
||||
|
||||
let docElem = document.documentElement;
|
||||
let leftOffset = window.pageXOffset - docElem.clientLeft;
|
||||
let topOffset = window.pageYOffset - docElem.clientTop;
|
||||
if (element.getBoundingClientRect) {
|
||||
let box = element.getBoundingClientRect();
|
||||
return {
|
||||
left: box.left + leftOffset,
|
||||
top: box.top + topOffset,
|
||||
width: box.width,
|
||||
height: box.height
|
||||
};
|
||||
}
|
||||
else {
|
||||
if (element instanceof HTMLCanvasElement) {
|
||||
return {
|
||||
left: leftOffset,
|
||||
top: topOffset,
|
||||
width: element.width,
|
||||
height: element.height
|
||||
};
|
||||
}
|
||||
else {
|
||||
return {
|
||||
left: leftOffset,
|
||||
top: topOffset,
|
||||
width: parseInt(element.style.width),
|
||||
height: parseInt(element.style.height)
|
||||
};
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @method getPreTouch
|
||||
* @param {Touch} touch
|
||||
* @return {Touch}
|
||||
*/
|
||||
getPreTouch (touch) {
|
||||
let preTouch = null;
|
||||
let locPreTouchPool = this._preTouchPool;
|
||||
let id = touch.getID();
|
||||
for (let i = locPreTouchPool.length - 1; i >= 0; i--) {
|
||||
if (locPreTouchPool[i].getID() === id) {
|
||||
preTouch = locPreTouchPool[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!preTouch)
|
||||
preTouch = touch;
|
||||
return preTouch;
|
||||
},
|
||||
|
||||
/**
|
||||
* @method setPreTouch
|
||||
* @param {Touch} touch
|
||||
*/
|
||||
setPreTouch (touch) {
|
||||
let find = false;
|
||||
let locPreTouchPool = this._preTouchPool;
|
||||
let id = touch.getID();
|
||||
for (let i = locPreTouchPool.length - 1; i >= 0; i--) {
|
||||
if (locPreTouchPool[i].getID() === id) {
|
||||
locPreTouchPool[i] = touch;
|
||||
find = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!find) {
|
||||
if (locPreTouchPool.length <= 50) {
|
||||
locPreTouchPool.push(touch);
|
||||
} else {
|
||||
locPreTouchPool[this._preTouchPoolPointer] = touch;
|
||||
this._preTouchPoolPointer = (this._preTouchPoolPointer + 1) % 50;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @method getTouchByXY
|
||||
* @param {Number} tx
|
||||
* @param {Number} ty
|
||||
* @param {Vec2} pos
|
||||
* @return {Touch}
|
||||
*/
|
||||
getTouchByXY (tx, ty, pos) {
|
||||
let locPreTouch = this._preTouchPoint;
|
||||
let location = this._glView.convertToLocationInView(tx, ty, pos);
|
||||
let touch = new cc.Touch(location.x, location.y, 0);
|
||||
touch._setPrevPoint(locPreTouch.x, locPreTouch.y);
|
||||
locPreTouch.x = location.x;
|
||||
locPreTouch.y = location.y;
|
||||
return touch;
|
||||
},
|
||||
|
||||
/**
|
||||
* @method getMouseEvent
|
||||
* @param {Vec2} location
|
||||
* @param {Vec2} pos
|
||||
* @param {Number} eventType
|
||||
* @returns {Event.EventMouse}
|
||||
*/
|
||||
getMouseEvent (location, pos, eventType) {
|
||||
let locPreMouse = this._prevMousePoint;
|
||||
let mouseEvent = new cc.Event.EventMouse(eventType);
|
||||
mouseEvent._setPrevCursor(locPreMouse.x, locPreMouse.y);
|
||||
locPreMouse.x = location.x;
|
||||
locPreMouse.y = location.y;
|
||||
this._glView._convertMouseToLocationInView(locPreMouse, pos);
|
||||
mouseEvent.setLocation(locPreMouse.x, locPreMouse.y);
|
||||
return mouseEvent;
|
||||
},
|
||||
|
||||
/**
|
||||
* @method getPointByEvent
|
||||
* @param {Touch} event
|
||||
* @param {Vec2} pos
|
||||
* @return {Vec2}
|
||||
*/
|
||||
getPointByEvent (event, pos) {
|
||||
if (event.pageX != null) //not avalable in <= IE8
|
||||
return {x: event.pageX, y: event.pageY};
|
||||
|
||||
if (sys.platform === sys.WECHAT_GAME) {
|
||||
pos.left = 0;
|
||||
pos.top = 0;
|
||||
}
|
||||
else {
|
||||
pos.left -= document.body.scrollLeft;
|
||||
pos.top -= document.body.scrollTop;
|
||||
}
|
||||
return {x: event.clientX, y: event.clientY};
|
||||
},
|
||||
|
||||
/**
|
||||
* @method getTouchesByEvent
|
||||
* @param {Touch} event
|
||||
* @param {Vec2} pos
|
||||
* @returns {Array}
|
||||
*/
|
||||
getTouchesByEvent (event, pos) {
|
||||
let touchArr = [], locView = this._glView;
|
||||
let touch_event, touch, preLocation;
|
||||
let locPreTouch = this._preTouchPoint;
|
||||
|
||||
let length = event.changedTouches.length;
|
||||
for (let i = 0; i < length; i++) {
|
||||
touch_event = event.changedTouches[i];
|
||||
if (touch_event) {
|
||||
let location;
|
||||
if (sys.BROWSER_TYPE_FIREFOX === sys.browserType)
|
||||
location = locView.convertToLocationInView(touch_event.pageX, touch_event.pageY, pos, _vec2);
|
||||
else
|
||||
location = locView.convertToLocationInView(touch_event.clientX, touch_event.clientY, pos, _vec2);
|
||||
if (touch_event.identifier != null) {
|
||||
touch = new cc.Touch(location.x, location.y, touch_event.identifier);
|
||||
//use Touch Pool
|
||||
preLocation = this.getPreTouch(touch).getLocation();
|
||||
touch._setPrevPoint(preLocation.x, preLocation.y);
|
||||
this.setPreTouch(touch);
|
||||
} else {
|
||||
touch = new cc.Touch(location.x, location.y);
|
||||
touch._setPrevPoint(locPreTouch.x, locPreTouch.y);
|
||||
}
|
||||
locPreTouch.x = location.x;
|
||||
locPreTouch.y = location.y;
|
||||
touchArr.push(touch);
|
||||
}
|
||||
}
|
||||
return touchArr;
|
||||
},
|
||||
|
||||
/**
|
||||
* @method registerSystemEvent
|
||||
* @param {HTMLElement} element
|
||||
*/
|
||||
registerSystemEvent (element) {
|
||||
if(this._isRegisterEvent) return;
|
||||
|
||||
this._glView = cc.view;
|
||||
let selfPointer = this;
|
||||
|
||||
let prohibition = sys.isMobile;
|
||||
let supportMouse = ('mouse' in sys.capabilities);
|
||||
let supportTouches = ('touches' in sys.capabilities);
|
||||
|
||||
if (sys.platform === sys.WECHAT_GAME) {
|
||||
prohibition = false;
|
||||
supportTouches = true;
|
||||
supportMouse = false;
|
||||
}
|
||||
|
||||
if (supportMouse) {
|
||||
//HACK
|
||||
// - At the same time to trigger the ontouch event and onmouse event
|
||||
// - The function will execute 2 times
|
||||
//The known browser:
|
||||
// liebiao
|
||||
// miui
|
||||
// WECHAT
|
||||
if (!prohibition) {
|
||||
window.addEventListener('mousedown', function () {
|
||||
selfPointer._mousePressed = true;
|
||||
}, false);
|
||||
|
||||
window.addEventListener('mouseup', function (event) {
|
||||
if (!selfPointer._mousePressed)
|
||||
return;
|
||||
|
||||
selfPointer._mousePressed = false;
|
||||
|
||||
let pos = selfPointer.getHTMLElementPosition(element);
|
||||
let location = selfPointer.getPointByEvent(event, pos);
|
||||
if (!cc.rect(pos.left, pos.top, pos.width, pos.height).contains(location)){
|
||||
selfPointer.handleTouchesEnd([selfPointer.getTouchByXY(location.x, location.y, pos)]);
|
||||
|
||||
let mouseEvent = selfPointer.getMouseEvent(location,pos,cc.Event.EventMouse.UP);
|
||||
mouseEvent.setButton(event.button);
|
||||
eventManager.dispatchEvent(mouseEvent);
|
||||
}
|
||||
}, false);
|
||||
}
|
||||
|
||||
// register canvas mouse event
|
||||
let EventMouse = cc.Event.EventMouse;
|
||||
let _mouseEventsOnElement = [
|
||||
!prohibition && ["mousedown", EventMouse.DOWN, function (event, mouseEvent, location, pos) {
|
||||
selfPointer._mousePressed = true;
|
||||
selfPointer.handleTouchesBegin([selfPointer.getTouchByXY(location.x, location.y, pos)]);
|
||||
element.focus();
|
||||
}],
|
||||
!prohibition && ["mouseup", EventMouse.UP, function (event, mouseEvent, location, pos) {
|
||||
selfPointer._mousePressed = false;
|
||||
selfPointer.handleTouchesEnd([selfPointer.getTouchByXY(location.x, location.y, pos)]);
|
||||
}],
|
||||
!prohibition && ["mousemove", EventMouse.MOVE, function (event, mouseEvent, location, pos) {
|
||||
selfPointer.handleTouchesMove([selfPointer.getTouchByXY(location.x, location.y, pos)]);
|
||||
if (!selfPointer._mousePressed) {
|
||||
mouseEvent.setButton(null);
|
||||
}
|
||||
if (event.movementX !== undefined && event.movementY !== undefined) {
|
||||
mouseEvent.movementX = event.movementX;
|
||||
mouseEvent.movementY = event.movementY;
|
||||
}
|
||||
}],
|
||||
["mousewheel", EventMouse.SCROLL, function (event, mouseEvent) {
|
||||
mouseEvent.setScrollData(0, event.wheelDelta);
|
||||
}],
|
||||
/* firefox fix */
|
||||
["DOMMouseScroll", EventMouse.SCROLL, function (event, mouseEvent) {
|
||||
mouseEvent.setScrollData(0, event.detail * -120);
|
||||
}]
|
||||
];
|
||||
for (let i = 0; i < _mouseEventsOnElement.length; ++i) {
|
||||
let entry = _mouseEventsOnElement[i];
|
||||
if (entry) {
|
||||
let name = entry[0];
|
||||
let type = entry[1];
|
||||
let handler = entry[2];
|
||||
element.addEventListener(name, function (event) {
|
||||
let pos = selfPointer.getHTMLElementPosition(element);
|
||||
let location = selfPointer.getPointByEvent(event, pos);
|
||||
let mouseEvent = selfPointer.getMouseEvent(location, pos, type);
|
||||
mouseEvent.setButton(event.button);
|
||||
|
||||
handler(event, mouseEvent, location, pos);
|
||||
|
||||
eventManager.dispatchEvent(mouseEvent);
|
||||
event.stopPropagation();
|
||||
event.preventDefault();
|
||||
}, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (window.navigator.msPointerEnabled) {
|
||||
let _pointerEventsMap = {
|
||||
"MSPointerDown" : selfPointer.handleTouchesBegin,
|
||||
"MSPointerMove" : selfPointer.handleTouchesMove,
|
||||
"MSPointerUp" : selfPointer.handleTouchesEnd,
|
||||
"MSPointerCancel" : selfPointer.handleTouchesCancel
|
||||
};
|
||||
for (let eventName in _pointerEventsMap) {
|
||||
let touchEvent = _pointerEventsMap[eventName];
|
||||
element.addEventListener(eventName, function (event){
|
||||
let pos = selfPointer.getHTMLElementPosition(element);
|
||||
pos.left -= document.documentElement.scrollLeft;
|
||||
pos.top -= document.documentElement.scrollTop;
|
||||
|
||||
touchEvent.call(selfPointer, [selfPointer.getTouchByXY(event.clientX, event.clientY, pos)]);
|
||||
event.stopPropagation();
|
||||
}, false);
|
||||
}
|
||||
}
|
||||
|
||||
//register touch event
|
||||
if (supportTouches) {
|
||||
let _touchEventsMap = {
|
||||
"touchstart": function (touchesToHandle) {
|
||||
selfPointer.handleTouchesBegin(touchesToHandle);
|
||||
if (sys.platform !== sys.WECHAT_GAME) {
|
||||
element.focus();
|
||||
}
|
||||
},
|
||||
"touchmove": function (touchesToHandle) {
|
||||
selfPointer.handleTouchesMove(touchesToHandle);
|
||||
},
|
||||
"touchend": function (touchesToHandle) {
|
||||
selfPointer.handleTouchesEnd(touchesToHandle);
|
||||
},
|
||||
"touchcancel": function (touchesToHandle) {
|
||||
selfPointer.handleTouchesCancel(touchesToHandle);
|
||||
}
|
||||
};
|
||||
|
||||
let registerTouchEvent;
|
||||
if (cc.sys.browserType === cc.sys.BROWSER_TYPE_WECHAT_GAME_SUB) {
|
||||
_touchEventsMap = {
|
||||
onTouchStart: _touchEventsMap.touchstart,
|
||||
onTouchMove: _touchEventsMap.touchmove,
|
||||
onTouchEnd: _touchEventsMap.touchend,
|
||||
onTouchCancel: _touchEventsMap.touchcancel,
|
||||
};
|
||||
registerTouchEvent = function(eventName) {
|
||||
let handler = _touchEventsMap[eventName];
|
||||
wx[eventName](function(event) {
|
||||
if (!event.changedTouches) return;
|
||||
let pos = selfPointer.getHTMLElementPosition(element);
|
||||
let body = document.body;
|
||||
pos.left -= body.scrollLeft || 0;
|
||||
pos.top -= body.scrollTop || 0;
|
||||
handler(selfPointer.getTouchesByEvent(event, pos));
|
||||
});
|
||||
};
|
||||
}
|
||||
else {
|
||||
registerTouchEvent = function(eventName) {
|
||||
let handler = _touchEventsMap[eventName];
|
||||
element.addEventListener(eventName, (function(event) {
|
||||
if (!event.changedTouches) return;
|
||||
let pos = selfPointer.getHTMLElementPosition(element);
|
||||
let body = document.body;
|
||||
pos.left -= body.scrollLeft || 0;
|
||||
pos.top -= body.scrollTop || 0;
|
||||
handler(selfPointer.getTouchesByEvent(event, pos));
|
||||
event.stopPropagation();
|
||||
event.preventDefault();
|
||||
}), false);
|
||||
};
|
||||
}
|
||||
for (let eventName in _touchEventsMap) {
|
||||
registerTouchEvent(eventName);
|
||||
}
|
||||
}
|
||||
|
||||
if (cc.sys.browserType !== cc.sys.BROWSER_TYPE_WECHAT_GAME_SUB) {
|
||||
//register keyboard event
|
||||
this._registerKeyboardEvent();
|
||||
}
|
||||
|
||||
this._isRegisterEvent = true;
|
||||
},
|
||||
|
||||
/**
|
||||
* whether enable accelerometer event
|
||||
* @method setAccelerometerEnabled
|
||||
* @param {Boolean} isEnable
|
||||
*/
|
||||
setAccelerometerEnabled (isEnable) {
|
||||
let _t = this;
|
||||
if(_t._accelEnabled === isEnable)
|
||||
return;
|
||||
|
||||
_t._accelEnabled = isEnable;
|
||||
let scheduler = cc.director.getScheduler();
|
||||
scheduler.enableForTarget(_t);
|
||||
if (_t._accelEnabled) {
|
||||
_t._registerAccelerometerEvent();
|
||||
_t._accelCurTime = 0;
|
||||
scheduler.scheduleUpdate(_t);
|
||||
} else {
|
||||
_t._unregisterAccelerometerEvent();
|
||||
_t._accelCurTime = 0;
|
||||
scheduler.unscheduleUpdate(_t);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* set accelerometer interval value
|
||||
* @method setAccelerometerInterval
|
||||
* @param {Number} interval
|
||||
*/
|
||||
setAccelerometerInterval (interval) {
|
||||
if (this._accelInterval !== interval) {
|
||||
this._accelInterval = interval;
|
||||
}
|
||||
},
|
||||
|
||||
_registerKeyboardEvent () {
|
||||
cc.game.canvas.addEventListener("keydown", function (e) {
|
||||
eventManager.dispatchEvent(new cc.Event.EventKeyboard(e.keyCode, true));
|
||||
e.stopPropagation();
|
||||
e.preventDefault();
|
||||
}, false);
|
||||
cc.game.canvas.addEventListener("keyup", function (e) {
|
||||
eventManager.dispatchEvent(new cc.Event.EventKeyboard(e.keyCode, false));
|
||||
e.stopPropagation();
|
||||
e.preventDefault();
|
||||
}, false);
|
||||
},
|
||||
|
||||
_registerAccelerometerEvent () {
|
||||
let w = window, _t = this;
|
||||
_t._acceleration = new Acceleration();
|
||||
_t._accelDeviceEvent = w.DeviceMotionEvent || w.DeviceOrientationEvent;
|
||||
|
||||
//TODO fix DeviceMotionEvent bug on QQ Browser version 4.1 and below.
|
||||
if (cc.sys.browserType === cc.sys.BROWSER_TYPE_MOBILE_QQ)
|
||||
_t._accelDeviceEvent = window.DeviceOrientationEvent;
|
||||
|
||||
let _deviceEventType = (_t._accelDeviceEvent === w.DeviceMotionEvent) ? "devicemotion" : "deviceorientation";
|
||||
let ua = navigator.userAgent;
|
||||
if (/Android/.test(ua) || (/Adr/.test(ua) && cc.sys.browserType === cc.BROWSER_TYPE_UC)) {
|
||||
_t._minus = -1;
|
||||
}
|
||||
|
||||
_didAccelerateFun = _t.didAccelerate.bind(_t);
|
||||
w.addEventListener(_deviceEventType, _didAccelerateFun, false);
|
||||
},
|
||||
|
||||
_unregisterAccelerometerEvent () {
|
||||
let w = window, _t = this;
|
||||
let _deviceEventType = (_t._accelDeviceEvent === w.DeviceMotionEvent) ? "devicemotion" : "deviceorientation";
|
||||
if (_didAccelerateFun) {
|
||||
w.removeEventListener(_deviceEventType, _didAccelerateFun, false);
|
||||
}
|
||||
},
|
||||
|
||||
didAccelerate (eventData) {
|
||||
let _t = this, w = window;
|
||||
if (!_t._accelEnabled)
|
||||
return;
|
||||
|
||||
let mAcceleration = _t._acceleration;
|
||||
|
||||
let x, y, z;
|
||||
|
||||
if (_t._accelDeviceEvent === window.DeviceMotionEvent) {
|
||||
let eventAcceleration = eventData["accelerationIncludingGravity"];
|
||||
x = _t._accelMinus * eventAcceleration.x * 0.1;
|
||||
y = _t._accelMinus * eventAcceleration.y * 0.1;
|
||||
z = eventAcceleration.z * 0.1;
|
||||
} else {
|
||||
x = (eventData["gamma"] / 90) * 0.981;
|
||||
y = -(eventData["beta"] / 90) * 0.981;
|
||||
z = (eventData["alpha"] / 90) * 0.981;
|
||||
}
|
||||
|
||||
if (cc.view._isRotated) {
|
||||
let tmp = x;
|
||||
x = -y;
|
||||
y = tmp;
|
||||
}
|
||||
mAcceleration.x = x;
|
||||
mAcceleration.y = y;
|
||||
mAcceleration.z = z;
|
||||
|
||||
mAcceleration.timestamp = eventData.timeStamp || Date.now();
|
||||
|
||||
let tmpX = mAcceleration.x;
|
||||
if (w.orientation === LANDSCAPE_RIGHT) {
|
||||
mAcceleration.x = -mAcceleration.y;
|
||||
mAcceleration.y = tmpX;
|
||||
} else if (w.orientation === LANDSCAPE_LEFT) {
|
||||
mAcceleration.x = mAcceleration.y;
|
||||
mAcceleration.y = -tmpX;
|
||||
} else if (w.orientation === PORTRAIT_UPSIDE_DOWN) {
|
||||
mAcceleration.x = -mAcceleration.x;
|
||||
mAcceleration.y = -mAcceleration.y;
|
||||
}
|
||||
// fix android acc values are opposite
|
||||
if (cc.sys.os === cc.sys.OS_ANDROID &&
|
||||
cc.sys.browserType !== cc.sys.BROWSER_TYPE_MOBILE_QQ) {
|
||||
mAcceleration.x = -mAcceleration.x;
|
||||
mAcceleration.y = -mAcceleration.y;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @method update
|
||||
* @param {Number} dt
|
||||
*/
|
||||
update (dt) {
|
||||
if (this._accelCurTime > this._accelInterval) {
|
||||
this._accelCurTime -= this._accelInterval;
|
||||
eventManager.dispatchEvent(new cc.Event.EventAcceleration(this._acceleration));
|
||||
}
|
||||
this._accelCurTime += dt;
|
||||
}
|
||||
};
|
||||
|
||||
_cc.inputManager = inputManager;
|
||||
export default inputManager;
|
||||
@@ -0,0 +1,786 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2011-2012 cocos2d-x.org
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
import { rect } from '../../value-types/rect';
|
||||
import Vec2, { v2 } from '../../value-types/vec2';
|
||||
import macro from '../CCMacro';
|
||||
import sys from '../CCSys';
|
||||
import { EventAcceleration, EventKeyboard, EventMouse, EventTouch } from './CCEvent';
|
||||
import eventManager from './CCEventManager';
|
||||
import CCTouch from './CCTouch';
|
||||
|
||||
const TOUCH_TIMEOUT = macro.TOUCH_TIMEOUT;
|
||||
|
||||
const PORTRAIT = 0;
|
||||
const LANDSCAPE_LEFT = -90;
|
||||
const PORTRAIT_UPSIDE_DOWN = 180;
|
||||
const LANDSCAPE_RIGHT = 90;
|
||||
|
||||
let _didAccelerateFun;
|
||||
|
||||
const _vec2 = v2();
|
||||
|
||||
interface IHTMLElementPosition {
|
||||
left: number;
|
||||
top: number;
|
||||
width: number;
|
||||
height: number;
|
||||
}
|
||||
|
||||
interface IView {
|
||||
convertToLocationInView (tx: number, ty: number, elementPosition: IHTMLElementPosition, out?: Vec2): Vec2;
|
||||
|
||||
_convertMouseToLocationInView (point: Vec2, elementPosition: IHTMLElementPosition): void;
|
||||
|
||||
_convertTouchesWithScale (touches: CCTouch[]): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en the device accelerometer reports values for each axis in units of g-force.
|
||||
* !#zh 设备重力传感器传递的各个轴的数据。
|
||||
*/
|
||||
class Acceleration {
|
||||
public x: number;
|
||||
public y: number;
|
||||
public z: number;
|
||||
public timestamp: number;
|
||||
constructor (x = 0, y = 0, z = 0, timestamp = 0) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This class manages all events of input. include: touch, mouse, accelerometer, keyboard
|
||||
*/
|
||||
class InputManager {
|
||||
private _mousePressed = false;
|
||||
|
||||
private _isRegisterEvent = false;
|
||||
|
||||
private _preTouchPoint = v2(0, 0);
|
||||
private _prevMousePoint = v2(0, 0);
|
||||
|
||||
private _preTouchPool: CCTouch[] = [];
|
||||
private _preTouchPoolPointer = 0;
|
||||
|
||||
private _touches: CCTouch[] = [];
|
||||
private _touchesIntegerDict: { [index: number]: number | undefined; } = { };
|
||||
|
||||
private _indexBitsUsed = 0;
|
||||
private _maxTouches = 8;
|
||||
|
||||
private _accelEnabled = false;
|
||||
private _accelInterval = 1 / 5;
|
||||
private _accelMinus = 1;
|
||||
private _accelCurTime = 0;
|
||||
private _acceleration: Acceleration | null = null;
|
||||
private _accelDeviceEvent = null;
|
||||
|
||||
private _glView: IView | null = null;
|
||||
|
||||
private _minus = 0;
|
||||
|
||||
public handleTouchesBegin (touches: CCTouch[]) {
|
||||
const handleTouches: CCTouch[] = [];
|
||||
const locTouchIntDict = this._touchesIntegerDict;
|
||||
const now = sys.now();
|
||||
for (const touch of touches) {
|
||||
const touchID = touch.getID();
|
||||
if (touchID === null) {
|
||||
continue;
|
||||
}
|
||||
const index = locTouchIntDict[touchID];
|
||||
if (index === undefined) {
|
||||
continue;
|
||||
}
|
||||
const unusedIndex = this._getUnUsedIndex();
|
||||
if (unusedIndex === -1) {
|
||||
cc.logID(2300, unusedIndex);
|
||||
continue;
|
||||
}
|
||||
// curTouch = this._touches[unusedIndex] = touch;
|
||||
const curTouch = new CCTouch(touch._point.x, touch._point.y, touch.getID());
|
||||
this._touches[unusedIndex] = curTouch;
|
||||
curTouch._lastModified = now;
|
||||
curTouch._setPrevPoint(touch._prevPoint);
|
||||
locTouchIntDict[touchID] = unusedIndex;
|
||||
handleTouches.push(curTouch);
|
||||
}
|
||||
if (handleTouches.length > 0) {
|
||||
this._glView!._convertTouchesWithScale(handleTouches);
|
||||
const touchEvent = new EventTouch(handleTouches);
|
||||
touchEvent._eventCode = EventTouch.BEGAN;
|
||||
eventManager.dispatchEvent(touchEvent);
|
||||
}
|
||||
}
|
||||
|
||||
public handleTouchesMove (touches: CCTouch[]) {
|
||||
const handleTouches: CCTouch[] = [];
|
||||
const locTouches = this._touches;
|
||||
const now = sys.now();
|
||||
for (const touch of touches) {
|
||||
const touchID = touch.getID();
|
||||
if (touchID === null) {
|
||||
continue;
|
||||
}
|
||||
const index = this._touchesIntegerDict[touchID];
|
||||
if (index === undefined) {
|
||||
// cc.log("if the index doesn't exist, it is an error");
|
||||
continue;
|
||||
}
|
||||
if (locTouches[index]) {
|
||||
locTouches[index]._setPoint(touch._point);
|
||||
locTouches[index]._setPrevPoint(touch._prevPoint);
|
||||
locTouches[index]._lastModified = now;
|
||||
handleTouches.push(locTouches[index]);
|
||||
}
|
||||
}
|
||||
if (handleTouches.length > 0) {
|
||||
this._glView!._convertTouchesWithScale(handleTouches);
|
||||
const touchEvent = new EventTouch(handleTouches);
|
||||
touchEvent._eventCode = EventTouch.MOVED;
|
||||
eventManager.dispatchEvent(touchEvent);
|
||||
}
|
||||
}
|
||||
|
||||
public handleTouchesEnd (touches: CCTouch[]) {
|
||||
const handleTouches = this.getSetOfTouchesEndOrCancel(touches);
|
||||
if (handleTouches.length > 0) {
|
||||
this._glView!._convertTouchesWithScale(handleTouches);
|
||||
const touchEvent = new EventTouch(handleTouches);
|
||||
touchEvent._eventCode = EventTouch.ENDED;
|
||||
eventManager.dispatchEvent(touchEvent);
|
||||
}
|
||||
this._preTouchPool.length = 0;
|
||||
}
|
||||
|
||||
public handleTouchesCancel (touches: CCTouch[]) {
|
||||
const handleTouches = this.getSetOfTouchesEndOrCancel(touches);
|
||||
if (handleTouches.length > 0) {
|
||||
this._glView!._convertTouchesWithScale(handleTouches);
|
||||
const touchEvent = new EventTouch(handleTouches);
|
||||
touchEvent._eventCode = EventTouch.CANCELLED;
|
||||
eventManager.dispatchEvent(touchEvent);
|
||||
}
|
||||
this._preTouchPool.length = 0;
|
||||
}
|
||||
|
||||
public getSetOfTouchesEndOrCancel (touches: CCTouch[]) {
|
||||
const handleTouches: CCTouch[] = [];
|
||||
const locTouches = this._touches;
|
||||
const locTouchesIntDict = this._touchesIntegerDict;
|
||||
for (const touch of touches) {
|
||||
const touchID = touch.getID();
|
||||
if (touchID === null) {
|
||||
continue;
|
||||
}
|
||||
const index = locTouchesIntDict[touchID];
|
||||
if (index === undefined) {
|
||||
// cc.log("if the index doesn't exist, it is an error");
|
||||
continue;
|
||||
}
|
||||
if (locTouches[index]) {
|
||||
locTouches[index]._setPoint(touch._point);
|
||||
locTouches[index]._setPrevPoint(touch._prevPoint);
|
||||
handleTouches.push(locTouches[index]);
|
||||
this._removeUsedIndexBit(index);
|
||||
delete locTouchesIntDict[touchID];
|
||||
}
|
||||
}
|
||||
return handleTouches;
|
||||
}
|
||||
|
||||
public getHTMLElementPosition (element: HTMLElement): IHTMLElementPosition {
|
||||
if (sys.platform === sys.WECHAT_GAME) {
|
||||
return {
|
||||
left: 0,
|
||||
top: 0,
|
||||
width: window.innerWidth,
|
||||
height: window.innerHeight,
|
||||
};
|
||||
}
|
||||
|
||||
const docElem = document.documentElement;
|
||||
const leftOffset = window.pageXOffset - docElem.clientLeft;
|
||||
const topOffset = window.pageYOffset - docElem.clientTop;
|
||||
if (element.getBoundingClientRect) {
|
||||
const box = element.getBoundingClientRect();
|
||||
return {
|
||||
left: box.left + leftOffset,
|
||||
top: box.top + topOffset,
|
||||
width: box.width,
|
||||
height: box.height,
|
||||
};
|
||||
} else {
|
||||
if (element instanceof HTMLCanvasElement) {
|
||||
return {
|
||||
left: leftOffset,
|
||||
top: topOffset,
|
||||
width: element.width,
|
||||
height: element.height,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
left: leftOffset,
|
||||
top: topOffset,
|
||||
width: parseInt(element.style.width || '0', undefined),
|
||||
height: parseInt(element.style.height || '0', undefined),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public getPreTouch (touch: CCTouch) {
|
||||
let preTouch: CCTouch | null = null;
|
||||
const locPreTouchPool = this._preTouchPool;
|
||||
const id = touch.getID();
|
||||
for (let i = locPreTouchPool.length - 1; i >= 0; i--) {
|
||||
if (locPreTouchPool[i].getID() === id) {
|
||||
preTouch = locPreTouchPool[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!preTouch) {
|
||||
preTouch = touch;
|
||||
}
|
||||
return preTouch;
|
||||
}
|
||||
|
||||
public setPreTouch (touch: CCTouch) {
|
||||
let find = false;
|
||||
const locPreTouchPool = this._preTouchPool;
|
||||
const id = touch.getID();
|
||||
for (let i = locPreTouchPool.length - 1; i >= 0; i--) {
|
||||
if (locPreTouchPool[i].getID() === id) {
|
||||
locPreTouchPool[i] = touch;
|
||||
find = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!find) {
|
||||
if (locPreTouchPool.length <= 50) {
|
||||
locPreTouchPool.push(touch);
|
||||
} else {
|
||||
locPreTouchPool[this._preTouchPoolPointer] = touch;
|
||||
this._preTouchPoolPointer = (this._preTouchPoolPointer + 1) % 50;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public getTouchByXY (tx: number, ty: number, pos: IHTMLElementPosition) {
|
||||
const locPreTouch = this._preTouchPoint;
|
||||
const location = this._glView!.convertToLocationInView(tx, ty, pos);
|
||||
const touch = new CCTouch(location.x, location.y, 0);
|
||||
touch._setPrevPoint(locPreTouch.x, locPreTouch.y);
|
||||
locPreTouch.x = location.x;
|
||||
locPreTouch.y = location.y;
|
||||
return touch;
|
||||
}
|
||||
|
||||
public getMouseEvent (
|
||||
location: { x: number; y: number; }, pos: IHTMLElementPosition, eventType: number): EventMouse {
|
||||
const locPreMouse = this._prevMousePoint;
|
||||
const mouseEvent = new EventMouse(eventType);
|
||||
mouseEvent._setPrevCursor(locPreMouse.x, locPreMouse.y);
|
||||
locPreMouse.x = location.x;
|
||||
locPreMouse.y = location.y;
|
||||
this._glView!._convertMouseToLocationInView(locPreMouse, pos);
|
||||
mouseEvent.setLocation(locPreMouse.x, locPreMouse.y);
|
||||
return mouseEvent;
|
||||
}
|
||||
|
||||
public getPointByEvent (event: MouseEvent, pos: IHTMLElementPosition) {
|
||||
if (event.pageX != null) { // not avalable in <= IE8
|
||||
return {x: event.pageX, y: event.pageY};
|
||||
}
|
||||
|
||||
if (sys.platform === sys.WECHAT_GAME) {
|
||||
pos.left = 0;
|
||||
pos.top = 0;
|
||||
} else {
|
||||
pos.left -= document.body.scrollLeft;
|
||||
pos.top -= document.body.scrollTop;
|
||||
}
|
||||
return {x: event.clientX, y: event.clientY};
|
||||
}
|
||||
|
||||
public getTouchesByEvent (event: TouchEvent, position: IHTMLElementPosition) {
|
||||
const touches: CCTouch[] = [];
|
||||
const locView = this._glView;
|
||||
const locPreTouch = this._preTouchPoint;
|
||||
|
||||
const length = event.changedTouches.length;
|
||||
for (let i = 0; i < length; i++) {
|
||||
const changedTouch = event.changedTouches.item(i);
|
||||
if (!changedTouch) {
|
||||
continue;
|
||||
}
|
||||
let location;
|
||||
if (sys.BROWSER_TYPE_FIREFOX === sys.browserType) {
|
||||
location = locView!.convertToLocationInView(
|
||||
changedTouch.pageX, changedTouch.pageY, position, _vec2);
|
||||
} else {
|
||||
location = locView!.convertToLocationInView(
|
||||
changedTouch.clientX, changedTouch.clientY, position, _vec2);
|
||||
}
|
||||
let touch: CCTouch;
|
||||
if (changedTouch.identifier != null) {
|
||||
touch = new CCTouch(location.x, location.y, changedTouch.identifier);
|
||||
// use Touch Pool
|
||||
const preLocation = this.getPreTouch(touch).getLocation();
|
||||
touch._setPrevPoint(preLocation.x, preLocation.y);
|
||||
this.setPreTouch(touch);
|
||||
} else {
|
||||
touch = new CCTouch(location.x, location.y);
|
||||
touch._setPrevPoint(locPreTouch.x, locPreTouch.y);
|
||||
}
|
||||
locPreTouch.x = location.x;
|
||||
locPreTouch.y = location.y;
|
||||
touches.push(touch);
|
||||
}
|
||||
return touches;
|
||||
}
|
||||
|
||||
public registerSystemEvent (element: HTMLElement) {
|
||||
if (this._isRegisterEvent) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._glView = cc.view;
|
||||
|
||||
let prohibition = sys.isMobile;
|
||||
let supportMouse = ('mouse' in sys.capabilities);
|
||||
let supportTouches = ('touches' in sys.capabilities);
|
||||
if (sys.platform === sys.WECHAT_GAME) {
|
||||
prohibition = false;
|
||||
supportTouches = true;
|
||||
supportMouse = false;
|
||||
}
|
||||
|
||||
// Register mouse events.
|
||||
if (supportMouse) {
|
||||
this._registerMouseEvents(element, prohibition);
|
||||
}
|
||||
|
||||
// Register mouse pointer events.
|
||||
if (window.navigator.msPointerEnabled) {
|
||||
this._registerMousePointerEvents(element);
|
||||
}
|
||||
|
||||
// Register touch events.
|
||||
if (supportTouches) {
|
||||
this._registerTouchEvents(element);
|
||||
}
|
||||
|
||||
// Register keyboard events.
|
||||
if (cc.sys.browserType !== cc.sys.BROWSER_TYPE_WECHAT_GAME_SUB) {
|
||||
this._registerKeyboardEvent();
|
||||
}
|
||||
|
||||
this._isRegisterEvent = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether enable accelerometer event.
|
||||
*/
|
||||
public setAccelerometerEnabled (isEnable: boolean) {
|
||||
if (this._accelEnabled === isEnable) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._accelEnabled = isEnable;
|
||||
const scheduler = cc.director.getScheduler();
|
||||
scheduler.enableForTarget(this);
|
||||
if (this._accelEnabled) {
|
||||
this._registerAccelerometerEvent();
|
||||
this._accelCurTime = 0;
|
||||
scheduler.scheduleUpdate(this);
|
||||
} else {
|
||||
this._unregisterAccelerometerEvent();
|
||||
this._accelCurTime = 0;
|
||||
scheduler.unscheduleUpdate(this);
|
||||
}
|
||||
}
|
||||
|
||||
public didAccelerate (eventData: DeviceMotionEvent | DeviceOrientationEvent) {
|
||||
if (!this._accelEnabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
const mAcceleration = this._acceleration!;
|
||||
|
||||
let x = 0;
|
||||
let y = 0;
|
||||
let z = 0;
|
||||
|
||||
// TODO
|
||||
// @ts-ignore
|
||||
if (this._accelDeviceEvent === window.DeviceMotionEvent) {
|
||||
const deviceMotionEvent = eventData as DeviceMotionEvent;
|
||||
const eventAcceleration = deviceMotionEvent.accelerationIncludingGravity;
|
||||
if (eventAcceleration) {
|
||||
x = this._accelMinus * (eventAcceleration.x || 0) * 0.1;
|
||||
y = this._accelMinus * (eventAcceleration.y || 0) * 0.1;
|
||||
z = (eventAcceleration.z || 0) * 0.1;
|
||||
}
|
||||
} else {
|
||||
const deviceOrientationEvent = eventData as DeviceOrientationEvent;
|
||||
x = ((deviceOrientationEvent.gamma || 0) / 90) * 0.981;
|
||||
y = -((deviceOrientationEvent.beta || 0) / 90) * 0.981;
|
||||
z = ((deviceOrientationEvent.alpha || 0) / 90) * 0.981;
|
||||
}
|
||||
|
||||
if (cc.view._isRotated) {
|
||||
const tmp = x;
|
||||
x = -y;
|
||||
y = tmp;
|
||||
}
|
||||
mAcceleration.x = x;
|
||||
mAcceleration.y = y;
|
||||
mAcceleration.z = z;
|
||||
|
||||
mAcceleration.timestamp = eventData.timeStamp || Date.now();
|
||||
|
||||
const tmpX = mAcceleration.x;
|
||||
if (window.orientation === LANDSCAPE_RIGHT) {
|
||||
mAcceleration.x = -mAcceleration.y;
|
||||
mAcceleration.y = tmpX;
|
||||
} else if (window.orientation === LANDSCAPE_LEFT) {
|
||||
mAcceleration.x = mAcceleration.y;
|
||||
mAcceleration.y = -tmpX;
|
||||
} else if (window.orientation === PORTRAIT_UPSIDE_DOWN) {
|
||||
mAcceleration.x = -mAcceleration.x;
|
||||
mAcceleration.y = -mAcceleration.y;
|
||||
}
|
||||
// fix android acc values are opposite
|
||||
if (cc.sys.os === cc.sys.OS_ANDROID &&
|
||||
cc.sys.browserType !== cc.sys.BROWSER_TYPE_MOBILE_QQ) {
|
||||
mAcceleration.x = -mAcceleration.x;
|
||||
mAcceleration.y = -mAcceleration.y;
|
||||
}
|
||||
}
|
||||
|
||||
public update (dt: number) {
|
||||
if (this._accelCurTime > this._accelInterval) {
|
||||
this._accelCurTime -= this._accelInterval;
|
||||
eventManager.dispatchEvent(new EventAcceleration(this._acceleration!));
|
||||
}
|
||||
this._accelCurTime += dt;
|
||||
}
|
||||
|
||||
private _getUnUsedIndex () {
|
||||
let temp = this._indexBitsUsed;
|
||||
const now = cc.sys.now();
|
||||
|
||||
for (let i = 0; i < this._maxTouches; i++) {
|
||||
if (!(temp & 0x00000001)) {
|
||||
this._indexBitsUsed |= (1 << i);
|
||||
return i;
|
||||
} else {
|
||||
const touch = this._touches[i];
|
||||
if (now - touch._lastModified > TOUCH_TIMEOUT) {
|
||||
this._removeUsedIndexBit(i);
|
||||
const touchID = touch.getID();
|
||||
if (touchID !== null) {
|
||||
delete this._touchesIntegerDict[touchID];
|
||||
}
|
||||
return i;
|
||||
}
|
||||
}
|
||||
temp >>= 1;
|
||||
}
|
||||
|
||||
// all bits are used
|
||||
return -1;
|
||||
}
|
||||
|
||||
private _removeUsedIndexBit (index) {
|
||||
if (index < 0 || index >= this._maxTouches) {
|
||||
return;
|
||||
}
|
||||
|
||||
let temp = 1 << index;
|
||||
temp = ~temp;
|
||||
this._indexBitsUsed &= temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* set accelerometer interval value
|
||||
* @method setAccelerometerInterval
|
||||
* @param {Number} interval
|
||||
*/
|
||||
private setAccelerometerInterval (interval) {
|
||||
if (this._accelInterval !== interval) {
|
||||
this._accelInterval = interval;
|
||||
}
|
||||
}
|
||||
|
||||
private _registerMouseEvents (element: HTMLElement, prohibition: boolean) {
|
||||
// HACK
|
||||
// - At the same time to trigger the ontouch event and onmouse event
|
||||
// - The function will execute 2 times
|
||||
// The known browser:
|
||||
// liebiao
|
||||
// miui
|
||||
// WECHAT
|
||||
if (!prohibition) {
|
||||
this._registerWindowMouseEvents(element);
|
||||
}
|
||||
this._registerElementMouseEvents(element, prohibition);
|
||||
}
|
||||
|
||||
private _registerWindowMouseEvents (element: HTMLElement) {
|
||||
window.addEventListener('mousedown', () => {
|
||||
this._mousePressed = true;
|
||||
}, false);
|
||||
window.addEventListener('mouseup', (event: MouseEvent) => {
|
||||
if (!this._mousePressed) {
|
||||
return;
|
||||
}
|
||||
this._mousePressed = false;
|
||||
const position = this.getHTMLElementPosition(element);
|
||||
const location = this.getPointByEvent(event, position);
|
||||
const positionRect = rect(position.left, position.top, position.width, position.height);
|
||||
if (!positionRect.contains(v2(location.x, location.y))) {
|
||||
this.handleTouchesEnd([this.getTouchByXY(location.x, location.y, position)]);
|
||||
const mouseEvent = this.getMouseEvent(location, position, EventMouse.UP);
|
||||
mouseEvent.setButton(event.button);
|
||||
eventManager.dispatchEvent(mouseEvent);
|
||||
}
|
||||
}, false);
|
||||
}
|
||||
|
||||
private _registerElementMouseEvents (element: HTMLElement, prohibition: boolean) {
|
||||
// Register canvas mouse events.
|
||||
type Handler = (
|
||||
event: MouseEvent,
|
||||
mouseEvent: EventMouse,
|
||||
location: { x: number; y: number; },
|
||||
elementPosition: IHTMLElementPosition,
|
||||
) => void;
|
||||
|
||||
type MouseEventNames = 'mousedown' | 'mouseup' | 'mousemove';
|
||||
|
||||
const listenDOMMouseEvent = (eventName: MouseEventNames, type: number, handler: Handler) => {
|
||||
element.addEventListener(eventName, (event) => {
|
||||
const pos = this.getHTMLElementPosition(element);
|
||||
const location = this.getPointByEvent(event, pos);
|
||||
const mouseEvent = this.getMouseEvent(location, pos, type);
|
||||
mouseEvent.setButton(event.button);
|
||||
|
||||
handler(event, mouseEvent, location, pos);
|
||||
|
||||
eventManager.dispatchEvent(mouseEvent);
|
||||
event.stopPropagation();
|
||||
event.preventDefault();
|
||||
});
|
||||
};
|
||||
|
||||
if (!prohibition) {
|
||||
listenDOMMouseEvent('mousedown', EventMouse.DOWN, (event, mouseEvent, location, pos) => {
|
||||
this._mousePressed = true;
|
||||
this.handleTouchesBegin([this.getTouchByXY(location.x, location.y, pos)]);
|
||||
element.focus();
|
||||
});
|
||||
|
||||
listenDOMMouseEvent('mouseup', EventMouse.UP, (event, mouseEvent, location, pos) => {
|
||||
this._mousePressed = false;
|
||||
this.handleTouchesEnd([this.getTouchByXY(location.x, location.y, pos)]);
|
||||
});
|
||||
|
||||
listenDOMMouseEvent('mousemove', EventMouse.MOVE, (event, mouseEvent, location, pos) => {
|
||||
this.handleTouchesMove([this.getTouchByXY(location.x, location.y, pos)]);
|
||||
if (!this._mousePressed) {
|
||||
mouseEvent.setButton(null);
|
||||
}
|
||||
if (event.movementX !== undefined && event.movementY !== undefined) {
|
||||
mouseEvent.movementX = event.movementX;
|
||||
mouseEvent.movementY = event.movementY;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
listenDOMMouseEvent('mousewheel', EventMouse.SCROLL, (event, mouseEvent, location, pos) => {
|
||||
mouseEvent.setScrollData(0, event.wheelDelta);
|
||||
});
|
||||
|
||||
/* firefox fix */
|
||||
// @ts-ignore
|
||||
listenDOMMouseEvent('DOMMouseScroll', EventMouse.SCROLL, (event, mouseEvent, location, pos) => {
|
||||
mouseEvent.setScrollData(0, event.detail * -120);
|
||||
});
|
||||
}
|
||||
|
||||
private _registerMousePointerEvents (element: HTMLElement) {
|
||||
const _pointerEventsMap = {
|
||||
MSPointerDown : this.handleTouchesBegin,
|
||||
MSPointerMove : this.handleTouchesMove,
|
||||
MSPointerUp : this.handleTouchesEnd,
|
||||
MSPointerCancel : this.handleTouchesCancel,
|
||||
};
|
||||
for (const eventName of Object.keys(_pointerEventsMap)) {
|
||||
const touchEvent = _pointerEventsMap[eventName];
|
||||
// @ts-ignore
|
||||
element.addEventListener(eventName as MSPointerEventNames, (event: MSPointerEvent) => {
|
||||
const pos = this.getHTMLElementPosition(element);
|
||||
pos.left -= document.documentElement.scrollLeft;
|
||||
pos.top -= document.documentElement.scrollTop;
|
||||
touchEvent.call(this, [this.getTouchByXY(event.clientX, event.clientY, pos)]);
|
||||
event.stopPropagation();
|
||||
}, false);
|
||||
}
|
||||
}
|
||||
|
||||
private _registerTouchEvents (element: HTMLElement) {
|
||||
if (cc.sys.browserType === cc.sys.BROWSER_TYPE_WECHAT_GAME_SUB) {
|
||||
this._registerWXGameTouchEvents(element);
|
||||
} else {
|
||||
this._registerHTMLTouchEvents(element);
|
||||
}
|
||||
}
|
||||
|
||||
private _registerWXGameTouchEvents (element: HTMLElement) {
|
||||
const makeTouchListener = (touchesHandler: (touchesToHandle: any) => void) => {
|
||||
return (event: TouchEvent) => {
|
||||
const pos = this.getHTMLElementPosition(element);
|
||||
const body = document.body;
|
||||
pos.left -= body.scrollLeft || 0;
|
||||
pos.top -= body.scrollTop || 0;
|
||||
touchesHandler(this.getTouchesByEvent(event, pos));
|
||||
};
|
||||
};
|
||||
|
||||
wx.onTouchStart(makeTouchListener((touchesToHandle) => {
|
||||
this.handleTouchesBegin(touchesToHandle);
|
||||
}));
|
||||
wx.onTouchEnd(makeTouchListener((touchesToHandle) => {
|
||||
this.handleTouchesEnd(touchesToHandle);
|
||||
}));
|
||||
wx.onTouchMove(makeTouchListener((touchesToHandle) => {
|
||||
this.handleTouchesMove(touchesToHandle);
|
||||
}));
|
||||
wx.onTouchCancel(makeTouchListener((touchesToHandle) => {
|
||||
this.handleTouchesCancel(touchesToHandle);
|
||||
}));
|
||||
}
|
||||
|
||||
private _registerHTMLTouchEvents (element: HTMLElement) {
|
||||
const makeTouchListener = (touchesHandler: (touchesToHandle: any) => void) => {
|
||||
return (event: TouchEvent) => {
|
||||
if (!event.changedTouches) {
|
||||
return;
|
||||
}
|
||||
const pos = this.getHTMLElementPosition(element);
|
||||
const body = document.body;
|
||||
pos.left -= body.scrollLeft || 0;
|
||||
pos.top -= body.scrollTop || 0;
|
||||
touchesHandler(this.getTouchesByEvent(event, pos));
|
||||
event.stopPropagation();
|
||||
event.preventDefault();
|
||||
};
|
||||
};
|
||||
|
||||
element.addEventListener('touchstart', makeTouchListener((touchesToHandle) => {
|
||||
this.handleTouchesBegin(touchesToHandle);
|
||||
if (sys.platform !== sys.WECHAT_GAME) {
|
||||
element.focus();
|
||||
}
|
||||
}), false);
|
||||
|
||||
element.addEventListener('touchmove', makeTouchListener((touchesToHandle) => {
|
||||
this.handleTouchesMove(touchesToHandle);
|
||||
}), false);
|
||||
|
||||
element.addEventListener('touchend', makeTouchListener((touchesToHandle) => {
|
||||
this.handleTouchesEnd(touchesToHandle);
|
||||
}), false);
|
||||
|
||||
element.addEventListener('touchcancel', makeTouchListener((touchesToHandle) => {
|
||||
this.handleTouchesCancel(touchesToHandle);
|
||||
}), false);
|
||||
}
|
||||
|
||||
private _registerKeyboardEvent () {
|
||||
const canvas = cc.game.canvas as HTMLCanvasElement;
|
||||
canvas.addEventListener('keydown', (event: KeyboardEvent) => {
|
||||
eventManager.dispatchEvent(new EventKeyboard(event.keyCode, true));
|
||||
event.stopPropagation();
|
||||
event.preventDefault();
|
||||
}, false);
|
||||
canvas.addEventListener('keyup', (event: KeyboardEvent) => {
|
||||
eventManager.dispatchEvent(new EventKeyboard(event.keyCode, false));
|
||||
event.stopPropagation();
|
||||
event.preventDefault();
|
||||
}, false);
|
||||
}
|
||||
|
||||
private _registerAccelerometerEvent () {
|
||||
this._acceleration = new Acceleration();
|
||||
// TODO
|
||||
// @ts-ignore
|
||||
this._accelDeviceEvent = window.DeviceMotionEvent || window.DeviceOrientationEvent;
|
||||
|
||||
// TODO fix DeviceMotionEvent bug on QQ Browser version 4.1 and below.
|
||||
if (cc.sys.browserType === cc.sys.BROWSER_TYPE_MOBILE_QQ) {
|
||||
// TODO
|
||||
// @ts-ignore
|
||||
this._accelDeviceEvent = window.DeviceOrientationEvent;
|
||||
}
|
||||
|
||||
const _deviceEventType =
|
||||
// TODO
|
||||
// @ts-ignore
|
||||
this._accelDeviceEvent === window.DeviceMotionEvent ? 'devicemotion' : 'deviceorientation';
|
||||
const ua = navigator.userAgent;
|
||||
if (/Android/.test(ua) || (/Adr/.test(ua) && cc.sys.browserType === cc.BROWSER_TYPE_UC)) {
|
||||
this._minus = -1;
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
_didAccelerateFun = (...args: any[]) => this.didAccelerate(...args);
|
||||
window.addEventListener(_deviceEventType, _didAccelerateFun, false);
|
||||
}
|
||||
|
||||
private _unregisterAccelerometerEvent () {
|
||||
const _deviceEventType =
|
||||
// TODO
|
||||
// @ts-ignore
|
||||
this._accelDeviceEvent === window.DeviceMotionEvent ? 'devicemotion' : 'deviceorientation';
|
||||
if (_didAccelerateFun) {
|
||||
window.removeEventListener(_deviceEventType, _didAccelerateFun, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const inputManager = new InputManager();
|
||||
|
||||
export default inputManager;
|
||||
|
||||
_cc.inputManager = inputManager;
|
||||
+59
-65
@@ -1,3 +1,6 @@
|
||||
import { Vec2 } from '../../value-types';
|
||||
import { v2 } from '../../value-types/vec2';
|
||||
|
||||
/****************************************************************************
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
@@ -34,145 +37,136 @@
|
||||
* @param {Number} id
|
||||
*/
|
||||
export default class Touch {
|
||||
constructor (x, y, id) {
|
||||
this._lastModified = 0;
|
||||
public _point: Vec2 = v2();
|
||||
public _prevPoint: Vec2 = v2();
|
||||
public _lastModified = 0;
|
||||
private _id: number | null = null;
|
||||
private _startPoint: Vec2 = v2();
|
||||
private _startPointCaptured: boolean = false;
|
||||
|
||||
constructor (x: number, y: number, id: number | null = null) {
|
||||
this.setTouchInfo(id, x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the current touch location in OpenGL coordinates.、
|
||||
* !#zh 获取当前触点位置。
|
||||
* @method getLocation
|
||||
* @return {Vec2}
|
||||
*/
|
||||
getLocation () {
|
||||
return cc.v2(this._point.x, this._point.y);
|
||||
public getLocation () {
|
||||
return v2(this._point.x, this._point.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns X axis location value.
|
||||
/**
|
||||
* !#en Returns X axis location value.
|
||||
* !#zh 获取当前触点 X 轴位置。
|
||||
* @method getLocationX
|
||||
* @returns {Number}
|
||||
*/
|
||||
getLocationX () {
|
||||
return this._point.x;
|
||||
}
|
||||
*/
|
||||
public getLocationX () {
|
||||
return this._point.x;
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* !#en Returns Y axis location value.
|
||||
* !#zh 获取当前触点 Y 轴位置。
|
||||
* @method getLocationY
|
||||
* @returns {Number}
|
||||
*/
|
||||
getLocationY () {
|
||||
return this._point.y;
|
||||
}
|
||||
*/
|
||||
public getLocationY () {
|
||||
return this._point.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the previous touch location in OpenGL coordinates.
|
||||
* !#zh 获取触点在上一次事件时的位置对象,对象包含 x 和 y 属性。
|
||||
* @method getPreviousLocation
|
||||
* @return {Vec2}
|
||||
*/
|
||||
getPreviousLocation () {
|
||||
return cc.v2(this._prevPoint.x, this._prevPoint.y);
|
||||
public getPreviousLocation () {
|
||||
return v2(this._prevPoint.x, this._prevPoint.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the start touch location in OpenGL coordinates.
|
||||
* !#zh 获获取触点落下时的位置对象,对象包含 x 和 y 属性。
|
||||
* @method getStartLocation
|
||||
* @returns {Vec2}
|
||||
*/
|
||||
getStartLocation () {
|
||||
return cc.v2(this._startPoint.x, this._startPoint.y);
|
||||
public getStartLocation () {
|
||||
return v2(this._startPoint.x, this._startPoint.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the delta distance from the previous touche to the current one in screen coordinates.
|
||||
* !#zh 获取触点距离上一次事件移动的距离对象,对象包含 x 和 y 属性。
|
||||
* @method getDelta
|
||||
* @return {Vec2}
|
||||
*/
|
||||
getDelta () {
|
||||
public getDelta () {
|
||||
return this._point.sub(this._prevPoint);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the current touch location in screen coordinates.
|
||||
* !#zh 获取当前事件在游戏窗口内的坐标位置对象,对象包含 x 和 y 属性。
|
||||
* @method getLocationInView
|
||||
* @return {Vec2}
|
||||
*/
|
||||
getLocationInView () {
|
||||
return cc.v2(this._point.x, cc.view._designResolutionSize.height - this._point.y);
|
||||
public getLocationInView () {
|
||||
return v2(this._point.x, cc.view._designResolutionSize.height - this._point.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the previous touch location in screen coordinates.
|
||||
* !#zh 获取触点在上一次事件时在游戏窗口中的位置对象,对象包含 x 和 y 属性。
|
||||
* @method getPreviousLocationInView
|
||||
* @return {Vec2}
|
||||
*/
|
||||
getPreviousLocationInView () {
|
||||
return cc.v2(this._prevPoint.x, cc.view._designResolutionSize.height - this._prevPoint.y);
|
||||
public getPreviousLocationInView () {
|
||||
return v2(this._prevPoint.x, cc.view._designResolutionSize.height - this._prevPoint.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the start touch location in screen coordinates.
|
||||
* !#zh 获取触点落下时在游戏窗口中的位置对象,对象包含 x 和 y 属性。
|
||||
* @method getStartLocationInView
|
||||
* @return {Vec2}
|
||||
*/
|
||||
getStartLocationInView () {
|
||||
return cc.v2(this._startPoint.x, cc.view._designResolutionSize.height - this._startPoint.y);
|
||||
public getStartLocationInView () {
|
||||
return v2(this._startPoint.x, cc.view._designResolutionSize.height - this._startPoint.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Returns the id of cc.Touch.
|
||||
* !#zh 触点的标识 ID,可以用来在多点触摸中跟踪触点。
|
||||
* @method getID
|
||||
* @return {Number}
|
||||
*/
|
||||
getID () {
|
||||
public getID () {
|
||||
return this._id;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Sets information to touch.
|
||||
* !#zh 设置触摸相关的信息。用于监控触摸事件。
|
||||
* @method setTouchInfo
|
||||
* @param {Number} id
|
||||
* @param {Number} x
|
||||
* @param {Number} y
|
||||
*/
|
||||
setTouchInfo (id, x, y) {
|
||||
public setTouchInfo (id: number | null = null, x?: number, y?: number) {
|
||||
this._prevPoint = this._point;
|
||||
this._point = cc.v2(x || 0, y || 0);
|
||||
this._point = v2(x || 0, y || 0);
|
||||
this._id = id;
|
||||
if(!this._startPointCaptured){
|
||||
this._startPoint = cc.v2(this._point);
|
||||
if (!this._startPointCaptured) {
|
||||
this._startPoint = v2(this._point);
|
||||
cc.view._convertPointWithScale(this._startPoint);
|
||||
this._startPointCaptured = true;
|
||||
}
|
||||
}
|
||||
|
||||
_setPoint (x, y) {
|
||||
if(y === undefined){
|
||||
public _setPoint (point: Vec2): void;
|
||||
|
||||
public _setPoint (x: number, y: number): void;
|
||||
|
||||
public _setPoint (x: number | Vec2, y?: number) {
|
||||
if (typeof x === 'object') {
|
||||
this._point.x = x.x;
|
||||
this._point.y = x.y;
|
||||
}else{
|
||||
this._point.x = x;
|
||||
this._point.y = y;
|
||||
} else {
|
||||
this._point.x = x || 0;
|
||||
this._point.y = y || 0;
|
||||
}
|
||||
}
|
||||
|
||||
_setPrevPoint (x, y) {
|
||||
if(y === undefined)
|
||||
this._prevPoint = cc.v2(x.x, x.y);
|
||||
else
|
||||
this._prevPoint = cc.v2(x || 0, y || 0);
|
||||
public _setPrevPoint (point: Vec2): void;
|
||||
|
||||
public _setPrevPoint (x: number, y: number): void;
|
||||
|
||||
public _setPrevPoint (x: number | Vec2, y?: number) {
|
||||
if (typeof x === 'object') {
|
||||
this._prevPoint = v2(x.x, x.y);
|
||||
} else {
|
||||
this._prevPoint = v2(x || 0, y || 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-2
@@ -31,6 +31,5 @@ export * from './CCEvent';
|
||||
export * from './CCTouch';
|
||||
|
||||
if (CC_TEST) {
|
||||
cc._Test.eventManager = eventManager;
|
||||
// cc._Test.eventManager = eventManager;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
declare interface IWXEventManager<Event> {
|
||||
(listener: (event: Event) => void): void;
|
||||
}
|
||||
|
||||
declare namespace wx {
|
||||
const onTouchStart: IWXEventManager<TouchEvent>;
|
||||
const onTouchMove: IWXEventManager<TouchEvent>;
|
||||
const onTouchEnd: IWXEventManager<TouchEvent>;
|
||||
const onTouchCancel: IWXEventManager<TouchEvent>;
|
||||
}
|
||||
Reference in New Issue
Block a user