Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions native/cocos/platform/SDLHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,20 +315,23 @@ void SDLHelper::dispatchSDLEvent(uint32_t windowId, const SDL_Event &sdlEvent) {
const SDL_TouchFingerEvent &event = sdlEvent.tfinger;
touch.type = TouchEvent::Type::ENDED;
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
touch.windowId = event.windowID;
events::Touch::broadcast(touch);
break;
}
case SDL_FINGERDOWN: {
const SDL_TouchFingerEvent &event = sdlEvent.tfinger;
touch.type = TouchEvent::Type::BEGAN;
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
touch.windowId = event.windowID;
events::Touch::broadcast(touch);
break;
}
case SDL_FINGERMOTION: {
const SDL_TouchFingerEvent &event = sdlEvent.tfinger;
touch.type = TouchEvent::Type::MOVED;
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
touch.windowId = event.windowID;
events::Touch::broadcast(touch);
break;
}
Expand Down
19 changes: 13 additions & 6 deletions native/cocos/platform/win32/modules/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,26 @@
namespace cc {

int Screen::getDPI() const {
// 参考:https://learn.microsoft.com/zh-cn/windows/win32/api/wingdi/nf-wingdi-getdevicecaps
static int dpi = -1;
if (dpi == -1) {

dpi = 96;
HDC hScreenDC = GetDC(nullptr);
int PixelsX = GetDeviceCaps(hScreenDC, HORZRES);
int MMX = GetDeviceCaps(hScreenDC, HORZSIZE);
ReleaseDC(nullptr, hScreenDC);
dpi = static_cast<int>(254.0f * PixelsX / MMX / 10);
if (hScreenDC) {
// LOGPIXELSX 对应水平方向每逻辑英寸的像素点数
dpi = GetDeviceCaps(hScreenDC, LOGPIXELSX);
ReleaseDC(nullptr, hScreenDC);
}
// win10 1607 以上
// HWND hDesktop = GetDesktopWindow();
// dpi = GetDpiForWindow(hDesktop);
Comment on lines +45 to +47

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Remove or document this commented alternative implementation. If GetDpiForWindow is preferred for Windows 10 1607+, consider implementing version detection and using the appropriate API.

Suggested change
// win10 1607 以上
// HWND hDesktop = GetDesktopWindow();
// dpi = GetDpiForWindow(hDesktop);

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: native/cocos/platform/win32/modules/Screen.cpp
Line: 45:47

Comment:
**style:** Remove or document this commented alternative implementation. If `GetDpiForWindow` is preferred for Windows 10 1607+, consider implementing version detection and using the appropriate API.

```suggestion
```

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +45 to +47

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code or convert to a version-detection strategy if GetDpiForWindow is needed for Windows 10 1607+.

Suggested change
// win10 1607 以上
// HWND hDesktop = GetDesktopWindow();
// dpi = GetDpiForWindow(hDesktop);
// Note: GetDpiForWindow is available on Windows 10 1607+ and provides per-window DPI
// but requires runtime OS version detection. Current implementation uses GetDeviceCaps
// for broader compatibility.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: native/cocos/platform/win32/modules/Screen.cpp
Line: 45:47

Comment:
Remove commented code or convert to a version-detection strategy if `GetDpiForWindow` is needed for Windows 10 1607+.

```suggestion
        // Note: GetDpiForWindow is available on Windows 10 1607+ and provides per-window DPI
        // but requires runtime OS version detection. Current implementation uses GetDeviceCaps
        // for broader compatibility.
```

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +45 to +47

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code - it creates clutter and is tracked in version control history if needed

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: native/cocos/platform/win32/modules/Screen.cpp
Line: 45:47

Comment:
Remove commented code - it creates clutter and is tracked in version control history if needed

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

}
return dpi;
}

float Screen::getDevicePixelRatio() const {
return 1;
return getDPI() / 96.0f;
}

void Screen::setKeepScreenOn(bool value) {
Expand Down Expand Up @@ -73,4 +80,4 @@ void Screen::setDisplayStats(bool isShow) {
se::ScriptEngine::getInstance()->evalString(commandBuf);
}

} // namespace cc
} // namespace cc
16 changes: 10 additions & 6 deletions native/cocos/platform/win32/modules/SystemWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "engine/EngineEvents.h"
#include "platform/SDLHelper.h"
#include "platform/win32/WindowsPlatform.h"
#include "platform/interfaces/modules/IScreen.h"

namespace cc {
SystemWindow::SystemWindow(uint32_t windowId, void *externalHandle)
Expand All @@ -45,27 +46,30 @@ SystemWindow::~SystemWindow() {

bool SystemWindow::createWindow(const char *title,
int w, int h, int flags) {
_window = SDLHelper::createWindow(title, w, h, flags);
float dpr = cc::BasePlatform::getPlatform()->getInterface<cc::IScreen>()->getDevicePixelRatio();
_width = w * dpr;
_height = h * dpr;
_window = SDLHelper::createWindow(title, _width, _height, flags);
if (!_window) {
return false;
}

_width = w;
_height = h;
_windowHandle = SDLHelper::getWindowHandle(_window);
return true;
}

bool SystemWindow::createWindow(const char *title,
int x, int y, int w,
int h, int flags) {
_window = SDLHelper::createWindow(title, x, y, w, h, flags);
float dpr = cc::BasePlatform::getPlatform()->getInterface<cc::IScreen>()->getDevicePixelRatio();
_width = w * dpr;
_height = h * dpr;

_window = SDLHelper::createWindow(title, x, y, _width, _height, flags);
if (!_window) {
return false;
}

_width = w;
_height = h;
_windowHandle = SDLHelper::getWindowHandle(_window);

return true;
Expand Down
4 changes: 4 additions & 0 deletions native/tools/simulator/frameworks/runtime-src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ else()
add_executable(${LIB_NAME} ${PROJ_SOURCES} ${PROJ_EXTRA_SOURCE})
endif()

if(WINDOWS AND MSVC)
set_property(TARGET ${LIB_NAME} PROPERTY VS_DPI_AWARE "PerMonitor")
endif()

target_link_libraries(${LIB_NAME} ${ENGINE_NAME} simulator)
target_include_directories(${LIB_NAME} PRIVATE
Classes
Expand Down
10 changes: 8 additions & 2 deletions pal/input/native/mouse-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ export class MouseInputSource {
private _getLocation (event: jsb.MouseEvent): Vec2 {
const window = this._windowManager.getWindow(event.windowId);
const windowSize = window.getViewSize();
const dpr = screenAdapter.devicePixelRatio;
let dpr = screenAdapter.devicePixelRatio;
if (systemInfo.os === OS.WINDOWS) { // 在windows下DPI变化时下发的是实际坐标
dpr = 1;
Comment on lines +120 to +121

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Touch input in touch-input.ts doesn't have the same Windows-specific DPR handling. Touch coordinates will be incorrectly scaled by DPR on Windows while mouse coordinates won't, causing inconsistent behavior.

Suggested change
if (systemInfo.os === OS.WINDOWS) { // 在windows下DPI变化时下发的是实际坐标
dpr = 1;
if (systemInfo.os === OS.WINDOWS) {
dpr = 1; // Native Windows coordinates are already in physical pixels after DPI awareness is enabled
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: pal/input/native/mouse-input.ts
Line: 120:121

Comment:
**logic:** Touch input in `touch-input.ts` doesn't have the same Windows-specific DPR handling. Touch coordinates will be incorrectly scaled by DPR on Windows while mouse coordinates won't, causing inconsistent behavior.

```suggestion
        if (systemInfo.os === OS.WINDOWS) {
            dpr = 1; // Native Windows coordinates are already in physical pixels after DPI awareness is enabled
        }
```

How can I resolve this? If you propose a fix, please make it concise.

}
const x = event.x * dpr;
const y = windowSize.height - event.y * dpr;
return new Vec2(x, y);
Expand Down Expand Up @@ -187,7 +190,10 @@ export class MouseInputSource {
const eventMouse = new EventMouse(eventType, false, this._preMousePos, mouseEvent.windowId);
eventMouse.setLocation(location.x, location.y);
eventMouse.setButton(button);
const dpr = screenAdapter.devicePixelRatio;
let dpr = screenAdapter.devicePixelRatio;
if (systemInfo.os === OS.WINDOWS) { // 在windows下DPI变化时下发的是实际坐标
dpr = 1;
}
Comment on lines +194 to +196

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Duplicated DPR adjustment logic - consider extracting to a helper method to avoid duplication and potential inconsistency

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: pal/input/native/mouse-input.ts
Line: 194:196

Comment:
**style:** Duplicated DPR adjustment logic - consider extracting to a helper method to avoid duplication and potential inconsistency

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

eventMouse.movementX = typeof mouseEvent.xDelta === 'undefined' ? 0 : mouseEvent.xDelta * dpr;
eventMouse.movementY = typeof mouseEvent.yDelta === 'undefined' ? 0 : mouseEvent.yDelta * dpr;
// update previous mouse position.
Expand Down
14 changes: 12 additions & 2 deletions pal/input/native/touch-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import { EventTouch, Touch as CCTouch } from '../../../cocos/input/types';
import { touchManager } from '../touch-manager';
import { InputEventType } from '../../../cocos/input/types/event-enum';
import { systemInfo } from 'pal/system-info';

Check failure on line 31 in pal/input/native/touch-input.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

`pal/system-info` import should occur before import of `../../../cocos/core/math`
import { OS } from '../../system-info/enum-type';

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

Expand Down Expand Up @@ -114,14 +116,19 @@
private _dispatchEvent (eventType: InputEventType, changedTouches: Touch[], windowId: number): void {
const handleTouches: CCTouch[] = [];
const length = changedTouches.length;
const windowSize = this._windowManager.getWindow(windowId).getViewSize() as Size;
const window = this._windowManager.getWindow(windowId);
if (window === null) {
return;
}
const windowSize = window.getViewSize();

Check failure on line 123 in pal/input/native/touch-input.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Trailing spaces not allowed
// const windowSize = this._windowManager.getWindow(windowId).getViewSize() as Size;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented-out code should be removed

Suggested change
// const windowSize = this._windowManager.getWindow(windowId).getViewSize() as Size;
const windowSize = window.getViewSize();

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: pal/input/native/touch-input.ts
Line: 124:124

Comment:
Commented-out code should be removed

```suggestion
        const windowSize = window.getViewSize();
```

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

for (let i = 0; i < length; ++i) {
const changedTouch = changedTouches[i];
const touchID = changedTouch.identifier;
if (touchID === null) {
continue;
}
const location = this._getLocation(changedTouch, windowSize);

Check failure on line 131 in pal/input/native/touch-input.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe argument of type `any` assigned to a parameter of type `Size`
const touch = touchManager.getOrCreateTouch(touchID, location.x, location.y);
if (!touch) {
continue;
Expand All @@ -144,7 +151,10 @@
}

private _getLocation (touch: globalThis.Touch, windowSize: Size): Vec2 {
const dpr = screenAdapter.devicePixelRatio;
let dpr = screenAdapter.devicePixelRatio;
if (systemInfo.os === OS.WINDOWS) { // 在windows下DPI变化时下发的是实际坐标
dpr = 1;
}
const x = touch.clientX * dpr;
const y = windowSize.height - touch.clientY * dpr;
return new Vec2(x, y);
Expand Down
2 changes: 1 addition & 1 deletion templates/compatibility-info.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"native": {
"default": ">=3.6.0"
"default": ">=3.8.0"
}
}
1 change: 1 addition & 0 deletions templates/windows/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ cc_windows_before_target(${EXECUTABLE_NAME})
add_executable(${EXECUTABLE_NAME}
${CC_ALL_SOURCES}
)
set_property(TARGET ${EXECUTABLE_NAME} PROPERTY VS_DPI_AWARE "PerMonitor")
cc_windows_after_target(${EXECUTABLE_NAME})
Loading