optimize system event type (#9130)

* support event callback type

update

* optimize eventKeyboard.keyCode type

* update

* update
This commit is contained in:
PP
2021-08-05 10:48:50 +08:00
committed by GitHub
parent 1d2f42e26b
commit b2bed360ca
2 changed files with 22 additions and 11 deletions
+5 -9
View File
@@ -35,6 +35,7 @@ import { Touch } from './touch';
import { Acceleration } from './acceleration';
import { legacyCC } from '../../global-exports';
import { SystemEventTypeUnion, SystemEventType } from './event-enum';
import { KeyCode } from './key-code';
const _vec2 = new Vec2();
@@ -597,15 +598,10 @@ export class EventAcceleration 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。
* @en The KeyCode enum value of current keyboard event.
* @zh 当前键盘事件的 KeyCode 枚举值
*/
public keyCode: number;
public keyCode: KeyCode;
/**
* @en Raw DOM KeyboardEvent.
@@ -637,7 +633,7 @@ export class EventKeyboard extends Event {
* @param eventType - The type of the event
* @param bubbles - Indicates whether the event bubbles up through the hierarchy or not.
*/
constructor (keyCode: number | KeyboardEvent, eventType: SystemEventTypeUnion, bubbles?: boolean);
constructor (keyCode: KeyCode | KeyboardEvent, eventType: SystemEventTypeUnion, bubbles?: boolean);
constructor (keyCode: any, eventType: SystemEventTypeUnion | boolean, bubbles?: boolean) {
if (typeof eventType === 'boolean') {
const isPressed = eventType;
@@ -53,6 +53,20 @@ export declare namespace SystemEvent {
export type EventType = EnumAlias<typeof SystemEventType>;
}
interface SystemEventMap {
[SystemEvent.EventType.MOUSE_DOWN]: (event: EventMouse) => void,
[SystemEvent.EventType.MOUSE_MOVE]: (event: EventMouse) => void,
[SystemEvent.EventType.MOUSE_UP]: (event: EventMouse) => void,
[SystemEvent.EventType.MOUSE_WHEEL]: (event: EventMouse) => void,
[SystemEvent.EventType.TOUCH_START]: (touch: Touch, event: EventTouch) => void,
[SystemEvent.EventType.TOUCH_MOVE]: (touch: Touch, event: EventTouch) => void,
[SystemEvent.EventType.TOUCH_END]: (touch: Touch, event: EventTouch) => void,
[SystemEvent.EventType.TOUCH_CANCEL]: (touch: Touch, event: EventTouch) => void,
[SystemEvent.EventType.KEY_DOWN]: (event: EventKeyboard) => void,
[SystemEvent.EventType.KEY_UP]: (event: EventKeyboard) => void,
[SystemEvent.EventType.DEVICEMOTION]: (event: EventAcceleration) => void,
}
/**
* @en
* The System event, it currently supports keyboard events and accelerometer events.<br/>
@@ -124,7 +138,8 @@ export class SystemEvent extends EventTarget {
* @param callback - The event listener's callback
* @param target - The event listener's target and callee
*/
public on<TFunction extends (...any) => void> (type: SystemEvent.EventType, callback: TFunction, target?, once?: boolean) {
// @ts-expect-error Property 'on' in type 'SystemEvent' is not assignable to the same property in base type
public on<K extends keyof SystemEventMap> (type: K, callback: SystemEventMap[K], target?: any, once?: boolean) {
if (EDITOR && !legacyCC.GAME_VIEW) {
return callback;
}
@@ -229,7 +244,7 @@ export class SystemEvent extends EventTarget {
* @param callback - The callback to remove.
* @param target - The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
*/
public off<TFunction extends (...any) => void> (type: SystemEvent.EventType, callback?: TFunction, target?) {
public off<K extends keyof SystemEventMap> (type: K, callback?: SystemEventMap[K], target?: any) {
if (EDITOR && !legacyCC.GAME_VIEW) {
return;
}