Skip to content

Commit 59ef482

Browse files
committed
Fixes Touch coordinates on the native Windows platform
1 parent 31a930a commit 59ef482

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

native/cocos/platform/SDLHelper.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,20 +315,23 @@ void SDLHelper::dispatchSDLEvent(uint32_t windowId, const SDL_Event &sdlEvent) {
315315
const SDL_TouchFingerEvent &event = sdlEvent.tfinger;
316316
touch.type = TouchEvent::Type::ENDED;
317317
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
318+
touch.windowId = event.windowID;
318319
events::Touch::broadcast(touch);
319320
break;
320321
}
321322
case SDL_FINGERDOWN: {
322323
const SDL_TouchFingerEvent &event = sdlEvent.tfinger;
323324
touch.type = TouchEvent::Type::BEGAN;
324325
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
326+
touch.windowId = event.windowID;
325327
events::Touch::broadcast(touch);
326328
break;
327329
}
328330
case SDL_FINGERMOTION: {
329331
const SDL_TouchFingerEvent &event = sdlEvent.tfinger;
330332
touch.type = TouchEvent::Type::MOVED;
331333
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
334+
touch.windowId = event.windowID;
332335
events::Touch::broadcast(touch);
333336
break;
334337
}

pal/input/native/touch-input.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import { EventTarget } from '../../../cocos/core/event';
2828
import { EventTouch, Touch as CCTouch } from '../../../cocos/input/types';
2929
import { touchManager } from '../touch-manager';
3030
import { InputEventType } from '../../../cocos/input/types/event-enum';
31+
import { systemInfo } from 'pal/system-info';
32+
import { OS } from '../../system-info/enum-type';
3133

3234
export type TouchCallback = (res: EventTouch) => void;
3335

@@ -114,7 +116,12 @@ export class TouchInputSource {
114116
private _dispatchEvent (eventType: InputEventType, changedTouches: Touch[], windowId: number): void {
115117
const handleTouches: CCTouch[] = [];
116118
const length = changedTouches.length;
117-
const windowSize = this._windowManager.getWindow(windowId).getViewSize() as Size;
119+
const window = this._windowManager.getWindow(windowId);
120+
if (window === null) {
121+
return;
122+
}
123+
const windowSize = window.getViewSize();
124+
// const windowSize = this._windowManager.getWindow(windowId).getViewSize() as Size;
118125
for (let i = 0; i < length; ++i) {
119126
const changedTouch = changedTouches[i];
120127
const touchID = changedTouch.identifier;
@@ -144,7 +151,10 @@ export class TouchInputSource {
144151
}
145152

146153
private _getLocation (touch: globalThis.Touch, windowSize: Size): Vec2 {
147-
const dpr = screenAdapter.devicePixelRatio;
154+
let dpr = screenAdapter.devicePixelRatio;
155+
if (systemInfo.os === OS.WINDOWS) { // 在windows下DPI变化时下发的是实际坐标
156+
dpr = 1;
157+
}
148158
const x = touch.clientX * dpr;
149159
const y = windowSize.height - touch.clientY * dpr;
150160
return new Vec2(x, y);

0 commit comments

Comments
 (0)