Files
cocos4/cocos/core/platform/event-manager/CCTouch.ts
T
JoneLau 6c52969826 fix global event and other bug (#4032)
* components such as event-target to ts

* divide CCEvent and CCTouch into game coordinates and ui coordinate mod

* repair editor environment where mask does not take effect bug

* fix render component blendState modification does not take effect bug

* fix bug

* fix ts tip
2019-03-14 11:36:43 +08:00

223 lines
7.3 KiB
TypeScript

import { Vec2 } from '../../value-types';
import { v2 } from '../../value-types/vec2';
import { vec2 } from '../../vmath';
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
/**
* !#en The touch event class
* !#zh 封装了触摸相关的信息。
* @class Touch
*
* @param {Number} x
* @param {Number} y
* @param {Number} id
*/
export default class Touch {
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 获取当前触点位置。
*/
public getLocation () {
return v2(this._point.x, this._point.y);
}
/**
* !#en Returns X axis location value.
* !#zh 获取当前触点 X 轴位置。
*/
public getLocationX () {
return this._point.x;
}
/**
* !#en Returns Y axis location value.
* !#zh 获取当前触点 Y 轴位置。
*/
public getLocationY () {
return this._point.y;
}
/**
* !#en Returns the current touch location in OpenGL coordinates.、
* !#zh 获取当前触点位置。
*/
public getUILocation () {
const pos = v2(this._point.x, this._point.y);
cc.view._convertPointWithScale(pos);
return pos;
}
/**
* !#en Returns X axis location value.
* !#zh 获取当前触点 X 轴位置。
*/
public getUILocationX () {
const viewport = cc.view.getViewportRect();
return (this._point.x - viewport.x) / cc.view.getScaleX();
}
/**
* !#en Returns Y axis location value.
* !#zh 获取当前触点 Y 轴位置。
*/
public getUILocationY () {
const viewport = cc.view.getViewportRect();
return (this._point.y - viewport.y) / cc.view.getScaleY();
}
/**
* !#en Returns the previous touch location in OpenGL coordinates.
* !#zh 获取触点在上一次事件时的位置对象,对象包含 x 和 y 属性。
*/
public getPreviousLocation () {
return v2(this._prevPoint.x, this._prevPoint.y);
}
/**
* !#en Returns the previous touch location in OpenGL coordinates.
* !#zh 获取触点在上一次事件时的位置对象,对象包含 x 和 y 属性。
*/
public getUIPreviousLocation () {
const pos = v2(this._prevPoint.x, this._prevPoint.y);
cc.view._convertPointWithScale(pos);
return pos;
}
/**
* !#en Returns the start touch location in OpenGL coordinates.
* !#zh 获获取触点落下时的位置对象,对象包含 x 和 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 属性。
*/
public getDelta () {
return this._point.sub(this._prevPoint);
}
/**
* !#en Returns the delta distance from the previous touche to the current one in screen coordinates.
* !#zh 获取触点距离上一次事件移动的距离对象,对象包含 x 和 y 属性。
*/
public getUIDelta () {
const scale = v2(cc.view.getScaleX(), cc.view.getScaleY());
vec2.divide(scale, this._point.sub(this._prevPoint), scale);
return scale;
}
/**
* !#en Returns the current touch location in screen coordinates.
* !#zh 获取当前事件在游戏窗口内的坐标位置对象,对象包含 x 和 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 属性。
*/
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 属性。
*/
public getStartLocationInView () {
return v2(this._startPoint.x, cc.view._designResolutionSize.height - this._startPoint.y);
}
/**
* !#en Returns the id of cc.Touch.
* !#zh 触点的标识 ID,可以用来在多点触摸中跟踪触点。
*/
public getID () {
return this._id;
}
/**
* !#en Sets information to touch.
* !#zh 设置触摸相关的信息。用于监控触摸事件。
*/
public setTouchInfo (id: number | null = null, x?: number, y?: number) {
this._prevPoint = this._point;
this._point = v2(x || 0, y || 0);
this._id = id;
if (!this._startPointCaptured) {
this._startPoint = v2(this._point);
cc.view._convertPointWithScale(this._startPoint);
this._startPointCaptured = true;
}
}
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 || 0;
this._point.y = 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);
}
}
}
cc.Touch = Touch;