support inputManager.frameDispatchEvents() in main loop (#8954)

* support pollEvent() API on pal/input

update

update pal

* support inputManager.frameDispatchEvents() in main loop

* remove event-manager module

* fix CI

* fix _glView

* fix CI

* fix director pause

* update for
This commit is contained in:
PP
2021-07-16 13:45:01 +08:00
committed by GitHub
parent a70f818ffa
commit 5a4bed2379
12 changed files with 364 additions and 276 deletions
+59 -36
View File
@@ -1,5 +1,5 @@
import { EDITOR } from 'internal:constants';
import { BaseInputEvent } from 'pal/input';
import { TouchInputEvent, MouseInputEvent, KeyboardInputEvent, AccelerometerInputEvent } from 'pal/input';
import { js } from '../../../cocos/core/utils/js';
import { AccelerometerInputSource } from './accelerometer';
import { InputBox } from './input-box';
import { KeyboardInputSource } from './keyboard';
@@ -12,50 +12,56 @@ export class Input {
public _keyboard = new KeyboardInputSource();
public _accelerometer = new AccelerometerInputSource();
public _inputBox = new InputBox();
private _inputEventList: BaseInputEvent[] = [];
private _touchEvents: TouchInputEvent[] = [];
private _mouseEvents: MouseInputEvent[] = [];
private _keyboardEvents: KeyboardInputEvent[] = [];
private _accelerometerEvents: AccelerometerInputEvent[] = [];
constructor () {
this._registerEvent();
}
private _registerEvent () {
// if (EDITOR) {
// return;
// }
// TODO: implement event main loop
// if (this._touch.support) {
// this._touch.onStart(this._pushEvent);
// this._touch.onMove(this._pushEvent);
// this._touch.onEnd(this._pushEvent);
// this._touch.onCancel(this._pushEvent);
// }
if (this._touch.support) {
const touchEvents = this._touchEvents;
this._touch.onStart((event) => { touchEvents.push(event); });
this._touch.onMove((event) => { touchEvents.push(event); });
this._touch.onEnd((event) => { touchEvents.push(event); });
this._touch.onCancel((event) => { touchEvents.push(event); });
}
// if (this._mouse.support) {
// this._mouse.onDown(this._pushEvent);
// this._mouse.onMove(this._pushEvent);
// this._mouse.onUp(this._pushEvent);
// this._mouse.onCancel(this._pushEvent);
// this._mouse.onWheel(this._pushEvent);
// }
if (this._mouse.support) {
const mouseEvents = this._mouseEvents;
this._mouse.onDown((event) => { mouseEvents.push(event); });
this._mouse.onMove((event) => { mouseEvents.push(event); });
this._mouse.onUp((event) => { mouseEvents.push(event); });
this._mouse.onWheel((event) => { mouseEvents.push(event); });
}
// if (this._keyboard.support) {
// this._keyboard.onDown(this._pushEvent);
// this._keyboard.onUp(this._pushEvent);
// }
if (this._keyboard.support) {
const keyboardEvents = this._keyboardEvents;
// this._keyboard.onDown((event) => { keyboardEvents.push(event); });
this._keyboard.onPressing((event) => { keyboardEvents.push(event); });
this._keyboard.onUp((event) => { keyboardEvents.push(event); });
}
// if (this._accelerometer.support) {
// this._accelerometer.onChange(this._pushEvent);
// }
if (this._accelerometer.support) {
const accelerometerEvents = this._accelerometerEvents;
this._accelerometer.onChange((event) => { accelerometerEvents.push(event); });
}
}
private _pushEvent (inputEvent: BaseInputEvent) {
this._inputEventList.push(inputEvent);
// accelerometer
public startAccelerometer (): void {
this._accelerometer.start();
}
public stopAccelerometer (): void {
this._accelerometer.stop();
}
public setAccelerometerInterval (interval: number): void {
this._accelerometer.setInterval(interval);
}
// // accelerometer
// public startAccelerometer (): Promise<void>;
// public stopAccelerometer (): Promise<void>;
// public setAccelerometerInterval (intercal: number): void;
// // input box
// public showInputBox (): Promise<void>;
@@ -63,8 +69,25 @@ export class Input {
// public onInputBoxChange (cb: Function);
// public onInputBoxComplete (cb: Function);
public pollEvent (): BaseInputEvent | undefined {
return this._inputEventList.shift();
public pollTouchEvents (): TouchInputEvent[] {
const events = js.array.copy(this._touchEvents);
this._touchEvents.length = 0;
return events;
}
public pollMouseEvents (): MouseInputEvent[] {
const events = js.array.copy(this._mouseEvents);
this._mouseEvents.length = 0;
return events;
}
public pollKeyboardEvents (): KeyboardInputEvent[] {
const events = js.array.copy(this._keyboardEvents);
this._keyboardEvents.length = 0;
return events;
}
public pollAccelerometerEvents (): AccelerometerInputEvent[] {
const events = js.array.copy(this._accelerometerEvents);
this._accelerometerEvents.length = 0;
return events;
}
}