Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 增加触控转鼠标示例代码 #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions QuickStart/Web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
16. 虚拟键鼠
17. 本地输入法显示中文合成过程
18. 自动回收时长
19. 触控转鼠标

方便用户快速接入云游戏客户端 Web SDK。

Expand Down Expand Up @@ -57,6 +58,7 @@
│   │   ├── sync-local-keyboard-close-status.js # 同步本地键盘状态到云端
│   │   ├── update-video-scale.js # 更新画面放缩比
│   │   ├── virtual-input-suite.js # 虚拟键鼠
│   │   ├── touch-to-mouse-event.js # 触控转鼠标
│   └── style.css # 样式文件
└── vite.config.js # vite 配置文件
└── .env # Web SDK 启动需要的配置
Expand Down
21 changes: 21 additions & 0 deletions QuickStart/Web/src/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,24 @@ export const veGamePadList = [
value: veGamePad.DisableVibrate,
},
];

// 触控类型
export const ACTION = {
DOWN: 0,
UP: 1,
MOVE: 2,
TOUCH_START: 0,
TOUCH_END: 1,
TOUCH_MOVE: 2,
WHEEL: 8,
PC_TOUCH_UP: 0,
PC_TOUCH_DOWN: 1,
PC_TOUCH_MOVE: 2,
};

// 鼠标按键
export const MOUSE_BUTTON = {
LEFT: 0,
CENTER: 1,
RIGHT: 2,
};
1 change: 1 addition & 0 deletions QuickStart/Web/src/features/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export { default as getUserProfilePath } from "./get-user-profile-path.js";
export { default as setTouchToMouse } from "./set-touch-to-mouse.js";
export { default as virtualInputSuite } from "./virtual-input-suite.js";
export { default as gamePad } from "./game-pad.js";
export { default as touchToMouseEvent } from "./touch-to-mouse-event.js";
76 changes: 76 additions & 0 deletions QuickStart/Web/src/features/touch-to-mouse-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { ACTION, MOUSE_BUTTON } from "../constant";

export const mouseLeftClick = async (x, y, veGameSdkInstance) => {
await veGameSdkInstance.sendMouseMessage({
action: ACTION.MOVE,
x,
y,
});
await veGameSdkInstance.sendMouseMessage({
button: MOUSE_BUTTON.LEFT,
action: ACTION.DOWN,
});
await veGameSdkInstance.sendMouseMessage({
button: MOUSE_BUTTON.LEFT,
action: ACTION.UP,
});
};

const touchToMouseMoveEvent = (veGameSdkInstance) => {
let touchstartTs = 0; // 记录touchstart的时间戳
const clickInterval = 1000; // 如果间隔大于1s,则认为是鼠标滑动,否则认为是点击
// 在触摸移动事件中,检查从触摸开始到当前的时间间隔, 如果时间间隔大于 dragInterval,则将状态设置为拖拽,并发送鼠标按下的消息。
// 如果时间间隔小于 dragInterval,则继续处理为滑动。
const dragInterval = 200;
let inTouchMove = false;
let inDrag = false;

veGameSdkInstance.on("on-touch-event", async (touchList) => {
if (touchList.length !== 1) return;

const { action, x, y, movementX, movementY } = touchList.pop();
if (action === ACTION.TOUCH_START) {
touchstartTs = Date.now(); // 记录手指触屏的时间
}
if (action === ACTION.TOUCH_END) {
inTouchMove = false;
// 拖拽过程中,手指抬起离屏
if (inDrag) {
console.log("drag end");
inDrag = false;
veGameSdkInstance.sendMouseMessage({
button: MOUSE_BUTTON.LEFT,
action: ACTION.UP,
});
return;
}
if (Date.now() - touchstartTs < clickInterval) {
mouseLeftClick(x, y, veGameSdkInstance); // 如果触屏和离屏的时间小于某个阈值,则视为一次点击
return;
}
// 其他情况,滑动结束
}
if (action === ACTION.TOUCH_MOVE) {
if (!inTouchMove && Date.now() - touchstartTs > dragInterval) {
inDrag = true;
// 如果是拖拽效果,鼠标左键先按下
console.log("drag start");
veGameSdkInstance.sendMouseMessage({
button: MOUSE_BUTTON.LEFT,
action: ACTION.DOWN,
});
}
// 鼠标滑动
veGameSdkInstance.sendMouseMessage({
action: ACTION.MOVE,
x,
y,
movementX,
movementY,
});
inTouchMove = true;
}
});
};

export default touchToMouseMoveEvent;
2 changes: 2 additions & 0 deletions QuickStart/Web/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ function bindEventListener(veGameSdkInstance, callback) {
"com.pkg3",
]),
},
// 关闭默认Touch事件
disableDefaultTouchEvent: true,
});
console.log("start success", startRes);

Expand Down