adjust eventtouch (#5189)

This commit is contained in:
JoneLau
2019-08-21 14:09:02 +08:00
committed by pandamicro
parent cafb008388
commit 96fa98da8b
9 changed files with 111 additions and 49 deletions
@@ -31,8 +31,7 @@
import { EventHandler as ComponentEventHandler } from '../../../components';
import { ccclass, executionOrder, menu, property } from '../../../core/data/class-decorator';
import { Event } from '../../../core/event';
import { EventMouse, EventTouch, SystemEventType } from '../../../core/platform';
import Touch from '../../../core/platform/event-manager/CCTouch';
import { EventMouse, EventTouch, SystemEventType, Touch } from '../../../core/platform';
import { Size, Vec2, Vec3 } from '../../../core/math';
import { ccenum } from '../../../core/value-types/enum';
import { LayoutComponent } from './layout-component';
+1 -2
View File
@@ -30,8 +30,7 @@
import { Component, EventHandler as ComponentEventHandler } from '../../../components';
import { ccclass, executionOrder, menu, property } from '../../../core/data/class-decorator';
import { EventTouch, SystemEventType } from '../../../core/platform';
import Touch from '../../../core/platform/event-manager/CCTouch';
import { EventTouch, SystemEventType, Touch } from '../../../core/platform';
import { Vec3 } from '../../../core/math';
import { ccenum } from '../../../core/value-types/enum';
import { clamp01 } from '../../../core/math/utils';
+46 -7
View File
@@ -30,7 +30,7 @@
import Event from '../../event/event';
import { Vec2 } from '../../math';
import Touch from './CCTouch';
import { Touch } from './CCTouch';
/**
* @en The mouse event
@@ -162,7 +162,7 @@ export class EventMouse extends Event {
public movementY: number = 0;
private _eventType: number;
public _eventType: number;
private _button: number | null = 0;
@@ -520,11 +520,6 @@ export class EventTouch extends Event {
*/
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;
public simulate = false;
@@ -595,6 +590,17 @@ export class EventTouch extends Event {
return this.touch ? this.touch.getLocation(out) : new Vec2();
}
/**
* @en
* Returns touch location.
*
* @zh
* 获取触点位置。
*/
public getUILocation(out?: Vec2) {
return this.touch ? this.touch.getUILocation(out) : new Vec2();
}
/**
* @en
* Returns the current touch location in screen coordinates.
@@ -606,6 +612,17 @@ export class EventTouch extends Event {
return this.touch ? this.touch.getLocationInView(out) : new Vec2();
}
/**
* @en
* Returns the current touch location in screen coordinates.
*
* @zh
* 获取当前触点在游戏窗口中的位置。
*/
public getUILocationInView(out?: Vec2) {
return this.touch ? this.touch.getLocationInView(out) : new Vec2();
}
/**
* @en Returns the previous touch location.
* @zh 获取触点在上一次事件时的位置对象,对象包含 x 和 y 属性。
@@ -625,6 +642,17 @@ export class EventTouch extends Event {
return this.touch ? this.touch.getStartLocation(out) : new Vec2();
}
/**
* @en
* Returns the start touch location.
*
* @zh
* 获获取触点落下时的 UI 世界下位置对象,对象包含 x 和 y 属性。
*/
public getUIStartLocation(out?: Vec2) {
return this.touch ? this.touch.getUIStartLocation(out) : new Vec2();
}
/**
* @en
* Returns the id of cc.Touch.
@@ -647,6 +675,17 @@ export class EventTouch extends Event {
return this.touch ? this.touch.getDelta(out) : new Vec2();
}
/**
* @en
* Returns the delta distance from the previous location to current location.
*
* @zh
* 获取触点距离上一次事件 UI 世界下移动的距离对象,对象包含 x 和 y 属性。
*/
public getUIDelta(out?: Vec2) {
return this.touch ? this.touch.getUIDelta(out) : new Vec2();
}
/**
* @en
* Returns the X axis delta distance from the previous location to current location.
+11 -11
View File
@@ -40,7 +40,7 @@ const _vec2 = new Vec2();
* @param {Number} y
* @param {Number} id
*/
export default class Touch {
export class Touch {
public _point: Vec2 = new Vec2();
public _prevPoint: Vec2 = new Vec2();
public _lastModified = 0;
@@ -61,7 +61,7 @@ export default class Touch {
out = new Vec2();
}
Vec2.set(out, this._point.x, this._point.y);
out.set(this._point.x, this._point.y);
return out;
}
@@ -90,7 +90,7 @@ export default class Touch {
out = new Vec2();
}
Vec2.set(out, this._point.x, this._point.y);
out.set(this._point.x, this._point.y);
cc.view._convertPointWithScale(out);
return out;
}
@@ -122,7 +122,7 @@ export default class Touch {
out = new Vec2();
}
Vec2.set(out, this._prevPoint.x, this._prevPoint.y);
out.set(this._prevPoint.x, this._prevPoint.y);
return out;
}
@@ -135,7 +135,7 @@ export default class Touch {
out = new Vec2();
}
Vec2.set(out, this._prevPoint.x, this._prevPoint.y);
out.set(this._prevPoint.x, this._prevPoint.y);
cc.view._convertPointWithScale(out);
return out;
}
@@ -149,7 +149,7 @@ export default class Touch {
out = new Vec2();
}
Vec2.set(out, this._startPoint.x, this._startPoint.y);
out.set(this._startPoint.x, this._startPoint.y);
return out;
}
@@ -162,7 +162,7 @@ export default class Touch {
out = new Vec2();
}
Vec2.set(out, this._startPoint.x, this._startPoint.y);
out.set(this._startPoint.x, this._startPoint.y);
cc.view._convertPointWithScale(out);
return out;
}
@@ -192,7 +192,7 @@ export default class Touch {
_vec2.set(this._point);
_vec2.subtract(this._prevPoint);
Vec2.set(out, cc.view.getScaleX(), cc.view.getScaleY());
out.set(cc.view.getScaleX(), cc.view.getScaleY());
Vec2.divide(out, _vec2, out);
return out;
}
@@ -206,7 +206,7 @@ export default class Touch {
out = new Vec2();
}
Vec2.set(out, this._point.x, cc.view._designResolutionSize.height - this._point.y);
out.set(this._point.x, cc.view._designResolutionSize.height - this._point.y);
return out;
}
@@ -219,7 +219,7 @@ export default class Touch {
out = new Vec2();
}
Vec2.set(out, this._prevPoint.x, cc.view._designResolutionSize.height - this._prevPoint.y);
out.set(this._prevPoint.x, cc.view._designResolutionSize.height - this._prevPoint.y);
return out;
}
@@ -232,7 +232,7 @@ export default class Touch {
out = new Vec2();
}
Vec2.set(out, this._startPoint.x, cc.view._designResolutionSize.height - this._startPoint.y);
out.set(this._startPoint.x, cc.view._designResolutionSize.height - this._startPoint.y);
return out;
}
@@ -30,6 +30,7 @@
*/
import { INode } from '../../utils/interfaces';
import { EventKeyboard, EventAcceleration, EventMouse } from './CCEvent';
export interface IEventListenerCreateInfo {
event?: number;
@@ -371,7 +372,7 @@ export class Mouse extends EventListener {
this._onEvent = (event: any) => this._callback(event);
}
public _callback (event) {
public _callback (event: EventMouse) {
const eventType = cc.Event.EventMouse;
switch (event._eventType) {
case eventType.DOWN:
@@ -492,7 +493,7 @@ export class Acceleration extends EventListener {
this._onAccelerationEvent = callback;
}
public _callback (event) {
public _callback (event: EventAcceleration) {
if (this._onAccelerationEvent) {
this._onAccelerationEvent(event.acc, event);
}
@@ -518,7 +519,7 @@ export class Keyboard extends EventListener {
this._onEvent = (event: any) => this._callback(event);
}
public _callback (event) {
public _callback (event: EventKeyboard) {
if (event.isPressed) {
if (this.onKeyPressed) {
this.onKeyPressed(event.keyCode, event);
@@ -935,7 +935,7 @@ class EventManager {
}
const event = argsObj.event;
const selTouch = event.currentTouch;
const selTouch = event.touch;
event.currentTarget = listener._getSceneGraphPriority();
let isClaimed = false;
@@ -1010,7 +1010,7 @@ class EventManager {
if (oneByOneListeners) {
for (let i = 0; i < originalTouches.length; ++i) {
const originalTouch = originalTouches[i];
event.currentTouch = originalTouch;
event.touch = originalTouch;
event.propagationStopped = event.propagationImmediateStopped = false;
this._dispatchEventToListeners(oneByOneListeners, this._onTouchEventCallback, oneByOneArgsObj);
}
@@ -33,8 +33,8 @@ import { rect } from '../../math/rect';
import { macro } from '../CCMacro';
import sys from '../CCSys';
import { EventAcceleration, EventKeyboard, EventMouse, EventTouch } from './CCEvent';
import CCTouch from './CCTouch';
import eventManager from './event-manager';
import { Touch as CCTouch } from './CCTouch';
const TOUCH_TIMEOUT = macro.TOUCH_TIMEOUT;
@@ -34,6 +34,7 @@ import { SystemEventType } from './event-enum';
import { EventListener } from './event-listener';
import eventManager from './event-manager';
import inputManager from './input-manager';
import { Touch } from './CCTouch';
/**
* @en
@@ -98,13 +99,13 @@ export class SystemEvent extends EventTarget {
inputManager.setAccelerometerInterval(interval);
}
public on (type: SystemEventType.KEY_DOWN | SystemEventType.KEY_UP, callback: (event: EventKeyboard) => void, target?: Object);
public on (type: SystemEventType.KEY_DOWN | SystemEventType.KEY_UP, callback: (event?: EventKeyboard) => void, target?: Object);
public on (type: SystemEventType.MOUSE_DOWN | SystemEventType.MOUSE_ENTER | SystemEventType.MOUSE_LEAVE |
SystemEventType.MOUSE_MOVE | SystemEventType.MOUSE_UP | SystemEventType.MOUSE_WHEEL ,
callback: (event: EventMouse) => void, target?: Object);
callback: (event?: EventMouse) => void, target?: Object);
public on (type: SystemEventType.TOUCH_START | SystemEventType.TOUCH_MOVE | SystemEventType.TOUCH_END | SystemEventType.TOUCH_CANCEL,
callback: (event: EventTouch) => void, target?: Object);
public on (type: SystemEventType.DEVICEMOTION, callback: (event: EventAcceleration) => void, target?: Object);
callback: (touch?: Touch, event?: EventTouch) => void, target?: Object);
public on (type: SystemEventType.DEVICEMOTION, callback: (event?: EventAcceleration) => void, target?: Object);
/**
* @zh
* 系统事件注册。
@@ -124,17 +125,15 @@ export class SystemEvent extends EventTarget {
if (!keyboardListener) {
keyboardListener = EventListener.create({
event: EventListener.KEYBOARD,
onKeyPressed (keyCode, event) {
onKeyPressed (keyCode: number, event: EventKeyboard) {
event.type = SystemEventType.KEY_DOWN;
systemEvent.emit(event.type, event);
},
onKeyReleased (keyCode, event) {
onKeyReleased (keyCode: number, event: EventKeyboard) {
event.type = SystemEventType.KEY_UP;
systemEvent.emit(event.type, event);
},
});
}
if (!eventManager.hasEventListener(EventListener.ListenerID.KEYBOARD)) {
eventManager.addListener(keyboardListener, 256);
}
}
@@ -144,13 +143,11 @@ export class SystemEvent extends EventTarget {
if (!accelerationListener) {
accelerationListener = EventListener.create({
event: EventListener.ACCELERATION,
callback (acc, event) {
callback (acc: Object, event: EventAcceleration) {
event.type = SystemEventType.DEVICEMOTION;
cc.systemEvent.emit(event.type, event);
},
});
}
if (!eventManager.hasEventListener(EventListener.ListenerID.ACCELERATION)) {
eventManager.addListener(accelerationListener!, 256);
}
}
@@ -164,20 +161,20 @@ export class SystemEvent extends EventTarget {
if (!touchListener) {
touchListener = EventListener.create({
event: EventListener.TOUCH_ONE_BY_ONE,
onTouchBegan (touch, event) {
onTouchBegan (touch: Touch, event: EventTouch) {
event.type = SystemEventType.TOUCH_START;
cc.systemEvent.emit(event.type, touch, event);
return true;
},
onTouchMoved (touch, event) {
onTouchMoved (touch: Touch, event: EventTouch) {
event.type = SystemEventType.TOUCH_MOVE;
cc.systemEvent.emit(event.type, touch, event);
},
onTouchEnded (touch, event) {
onTouchEnded (touch: Touch, event: EventTouch) {
event.type = SystemEventType.TOUCH_END;
cc.systemEvent.emit(event.type, touch, event);
},
onTouchCancelled (touch, event) {
onTouchCancelled (touch: Touch, event: EventTouch) {
event.type = SystemEventType.TOUCH_CANCEL;
cc.systemEvent.emit(event.type, touch, event);
},
@@ -195,19 +192,19 @@ export class SystemEvent extends EventTarget {
if (!mouseListener) {
mouseListener = EventListener.create({
event: EventListener.MOUSE,
onMouseDown (event) {
onMouseDown (event: EventMouse) {
event.type = SystemEventType.MOUSE_DOWN;
cc.systemEvent.emit(event.type, event);
},
onMouseMove (event) {
onMouseMove (event:EventMouse) {
event.type = SystemEventType.MOUSE_MOVE;
cc.systemEvent.emit(event.type, event);
},
onMouseUp (event) {
onMouseUp (event: EventMouse) {
event.type = SystemEventType.MOUSE_UP;
cc.systemEvent.emit(event.type, event);
},
onMouseScroll (event) {
onMouseScroll (event: EventMouse) {
event.type = SystemEventType.MOUSE_WHEEL;
cc.systemEvent.emit(event.type, event);
},
@@ -239,12 +236,40 @@ export class SystemEvent extends EventTarget {
const hasKeyUpEventListener = this.hasEventListener(SystemEventType.KEY_UP);
if (!hasKeyDownEventListener && !hasKeyUpEventListener) {
eventManager.removeListener(keyboardListener);
keyboardListener = null;
}
}
// Acceleration
if (accelerationListener && type === SystemEventType.DEVICEMOTION) {
eventManager.removeListener(accelerationListener);
accelerationListener = null;
}
if (touchListener && (type === SystemEventType.TOUCH_START || type === SystemEventType.TOUCH_MOVE ||
type === SystemEventType.TOUCH_END || type === SystemEventType.TOUCH_CANCEL)
) {
const hasTouchStart = this.hasEventListener(SystemEventType.TOUCH_START);
const hasTouchMove = this.hasEventListener(SystemEventType.TOUCH_MOVE);
const hasTouchEnd = this.hasEventListener(SystemEventType.TOUCH_END);
const hasTouchCancel = this.hasEventListener(SystemEventType.TOUCH_CANCEL);
if(!hasTouchStart && !hasTouchMove && !hasTouchEnd && !hasTouchCancel){
eventManager.removeListener(touchListener);
touchListener = null;
}
}
if (mouseListener && (type === SystemEventType.MOUSE_DOWN || type === SystemEventType.MOUSE_MOVE ||
type === SystemEventType.MOUSE_UP || type === SystemEventType.MOUSE_WHEEL)
) {
const hasMouseDown = this.hasEventListener(SystemEventType.MOUSE_DOWN);
const hasMouseMove = this.hasEventListener(SystemEventType.MOUSE_MOVE);
const hasMouseUp = this.hasEventListener(SystemEventType.MOUSE_UP);
const hasMouseWheel = this.hasEventListener(SystemEventType.MOUSE_WHEEL);
if (!hasMouseDown && !hasMouseMove && !hasMouseUp && !hasMouseWheel) {
eventManager.removeListener(mouseListener);
mouseListener = null;
}
}
}
}
+1 -2
View File
@@ -29,12 +29,11 @@
import { INode } from '../core/utils/interfaces';
import { ITargetImpl, EventTarget } from '../core/event/event-target';
import { eventManager, EventMouse, EventTouch } from '../core/platform/event-manager';
import { eventManager, EventMouse, EventTouch, Touch } from '../core/platform/event-manager';
import { SystemEventType } from '../core/platform/event-manager/event-enum';
import { EventListener } from '../core/platform/event-manager/event-listener';
import { array } from '../core/utils/js';
import Event from '../core/event/event';
import Touch from '../core/platform/event-manager/CCTouch';
import { Vec2 } from '../core/math/vec2';
const fastRemove = array.fastRemove;