Skip to content

Commit

Permalink
fixed #17866: [bug] EventTouch.getUIDelta return wrong delta values o…
Browse files Browse the repository at this point in the history
…n native platforms
  • Loading branch information
dumganhar committed Nov 20, 2024
1 parent 158e15e commit 32f4d98
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 124 deletions.
3 changes: 3 additions & 0 deletions @types/pal/input.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare module 'pal/input' {
* Register the touch event callback.
*/
public on (eventType: import('cocos/input/types/event-enum').InputEventType, callback: TouchCallback, target?: any);
public dispatchEventsInCache (): void;
}

type MouseCallback = (res: import('cocos/input/types').EventMouse) => void;
Expand All @@ -30,6 +31,8 @@ declare module 'pal/input' {
public dispatchMouseMoveEvent? (nativeMouseEvent: any);
public dispatchMouseUpEvent? (nativeMouseEvent: any);
public dispatchScrollEvent? (nativeMouseEvent: any);

public dispatchEventsInCache (): void
}

type KeyboardCallback = (res: import('cocos/input/types').EventKeyboard) => void;
Expand Down
71 changes: 24 additions & 47 deletions cocos/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ export class Input {
private _hmdInput$ = new HMDInputDevice();
private _handheldInput$ = new HandheldInputDevice();

private _eventTouchList$: EventTouch[] = [];
private _eventMouseList$: EventMouse[] = [];
private _eventKeyboardList$: EventKeyboard[] = [];
private _eventAccelerationList$: EventAcceleration[] = [];
private _eventGamepadList$: EventGamepad[] = [];
Expand Down Expand Up @@ -324,7 +322,7 @@ export class Input {
if (eventType === InputEventType.TOUCH_END) {
touchManager.releaseTouch(touchID);
}
this._dispatchOrPushEventTouch$(eventTouch, this._eventTouchList$);
this._dispatchEventTouch$(eventTouch);
}

/**
Expand Down Expand Up @@ -353,47 +351,45 @@ export class Input {

private _registerEvent$ (): void {
if (sys.hasFeature(sys.Feature.INPUT_TOUCH)) {
const eventTouchList = this._eventTouchList$;
this._touchInput$.on(InputEventType.TOUCH_START, (event): void => {
this._dispatchOrPushEventTouch$(event, eventTouchList);
this._dispatchEventTouch$(event);
});
this._touchInput$.on(InputEventType.TOUCH_MOVE, (event): void => {
this._dispatchOrPushEventTouch$(event, eventTouchList);
this._dispatchEventTouch$(event);
});
this._touchInput$.on(InputEventType.TOUCH_END, (event): void => {
this._dispatchOrPushEventTouch$(event, eventTouchList);
this._dispatchEventTouch$(event);
});
this._touchInput$.on(InputEventType.TOUCH_CANCEL, (event): void => {
this._dispatchOrPushEventTouch$(event, eventTouchList);
this._dispatchEventTouch$(event);
});
}

if (sys.hasFeature(sys.Feature.EVENT_MOUSE)) {
const eventMouseList = this._eventMouseList$;
this._mouseInput$.on(InputEventType.MOUSE_DOWN, (event): void => {
this._needSimulateTouchMoveEvent$ = true;
this._simulateEventTouch$(event);
this._dispatchOrPushEvent$(event, eventMouseList);
this._dispatchMouseEvent$(event);
});
this._mouseInput$.on(InputEventType.MOUSE_MOVE, (event): void => {
if (this._needSimulateTouchMoveEvent$) {
this._simulateEventTouch$(event);
}
this._dispatchOrPushEvent$(event, eventMouseList);
this._dispatchMouseEvent$(event);
});
this._mouseInput$.on(InputEventType.MOUSE_UP, (event): void => {
this._needSimulateTouchMoveEvent$ = false;
this._simulateEventTouch$(event);
this._dispatchOrPushEvent$(event, eventMouseList);
this._dispatchMouseEvent$(event);
});
this._mouseInput$.on(InputEventType.MOUSE_WHEEL, (event): void => {
this._dispatchOrPushEvent$(event, eventMouseList);
this._dispatchMouseEvent$(event);
});
this._mouseInput$.on(InputEventType.MOUSE_LEAVE, (event): void => {
this._dispatchOrPushEvent$(event, eventMouseList);
this._dispatchMouseEvent$(event);
});
this._mouseInput$.on(InputEventType.MOUSE_ENTER, (event): void => {
this._dispatchOrPushEvent$(event, eventMouseList);
this._dispatchMouseEvent$(event);
});
}

Expand Down Expand Up @@ -459,8 +455,6 @@ export class Input {
* @engineInternal
*/
public _clearEvents (): void {
this._eventMouseList$.length = 0;
this._eventTouchList$.length = 0;
this._eventKeyboardList$.length = 0;
this._eventAccelerationList$.length = 0;
this._eventGamepadList$.length = 0;
Expand All @@ -476,17 +470,17 @@ export class Input {
}
}

private _dispatchOrPushEventTouch$ (eventTouch: EventTouch, touchEventList: EventTouch[]): void {
if (dispatchImmediately) {
const touches = eventTouch.getTouches();
const touchesLength = touches.length;
for (let i = 0; i < touchesLength; ++i) {
eventTouch.touch = touches[i];
eventTouch.propagationStopped = eventTouch.propagationImmediateStopped = false;
this._emitEvent$(eventTouch);
}
} else {
touchEventList.push(eventTouch);
private _dispatchMouseEvent$ (event: Event): void {
this._emitEvent$(event);
}

private _dispatchEventTouch$ (eventTouch: EventTouch): void {
const touches = eventTouch.getTouches();
const touchesLength = touches.length;
for (let i = 0; i < touchesLength; ++i) {
eventTouch.touch = touches[i];
eventTouch.propagationStopped = eventTouch.propagationImmediateStopped = false;
this._emitEvent$(eventTouch);
}
}

Expand All @@ -509,25 +503,8 @@ export class Input {
this._emitEvent$(eventHandheld);
}

const eventMouseList = this._eventMouseList$;
// TODO: culling event queue
for (let i = 0, length = eventMouseList.length; i < length; ++i) {
const eventMouse = eventMouseList[i];
this._emitEvent$(eventMouse);
}

const eventTouchList = this._eventTouchList$;
// TODO: culling event queue
for (let i = 0, length = eventTouchList.length; i < length; ++i) {
const eventTouch = eventTouchList[i];
const touches = eventTouch.getTouches();
const touchesLength = touches.length;
for (let j = 0; j < touchesLength; ++j) {
eventTouch.touch = touches[j];
eventTouch.propagationStopped = eventTouch.propagationImmediateStopped = false;
this._emitEvent$(eventTouch);
}
}
this._mouseInput$.dispatchEventsInCache();
this._touchInput$.dispatchEventsInCache();

const eventKeyboardList = this._eventKeyboardList$;
// TODO: culling event queue
Expand Down
4 changes: 4 additions & 0 deletions pal/input/minigame/mouse-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,8 @@ export class MouseInputSource {
public on (eventType: InputEventType, callback: MouseCallback, target?: any): void {
this._eventTarget$.on(eventType, callback, target);
}

public dispatchEventsInCache (): void {
// Do nothing
}
}
4 changes: 4 additions & 0 deletions pal/input/minigame/touch-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@ export class TouchInputSource {
public on (eventType: InputEventType, callback: TouchCallback, target?: any): void {
this._eventTarget$.on(eventType, callback, target);
}

public dispatchEventsInCache (): void {
// Do nothing
}
}
Loading

0 comments on commit 32f4d98

Please sign in to comment.