@gd-kirie/platform
@gd-kirie/platform 曝光浏览器侧式桌面能力
古多特东道. 从已有的创建客户端 @gd-kirie/ipc-eventa 上下文.
import { createContext } from "@gd-kirie/ipc-eventa";
import { createPlatformClient, hostWindowPointerPositionChanged } from "@gd-kirie/platform";
const eventa = createContext();
const platform = createPlatformClient(eventa.context);
await platform.hostWindow.setAlwaysOnTop(true);
const windowBounds = await platform.hostWindow.getBounds();
const displayBounds = await platform.hostWindow.getCurrentDisplayBounds();
const stopPointer = eventa.context.on(hostWindowPointerPositionChanged, ({ body }) => {
console.log(body);
});
const pointer = await platform.hostWindow.getPointerPosition();
const windowState = await platform.hostWindow.getState();
console.log(windowBounds, displayBounds, pointer, windowState);
stopPointer();
eventa.dispose();
请求/响应能力是返回客户端的方法. 主机发起
导出事件 @moeru/eventa 合同; 以相同方式签署合同
上下文和返回的函数取消订阅.
外部網址
使用 openExternalUrl() 用系统打开一个绝对的 HTTP 或 HTTPS URL
浏览器 :
await platform.openExternalUrl("https://example.com");
Godot主机拒绝相对的URL和其他URI方案. 用这个
OS.shell_open()
来选择系统浏览器.
使用 openApplicationDataDirectory() 打开当前 Godot 应用程序
数据目录并接收其绝对路径 :
const path = await platform.openApplicationDataDirectory();
这种能力不接受任意路径.
主机窗口
- 窗口位置和屏幕坐标大小
- 当前显示位置和屏幕坐标大小
- 相对于主机窗口的指针位置, 包括此时 点击通过
- 指针通过
- 本地移动和调整手势大小
- 总是在顶上
- 以当前显示为中心
- 可见度、重点和最小化状态快照和变化事件
指针坐标使用主机-窗口像素,不与正态化
浏览器取景口. 订阅 hostWindowPointerPositionChanged 先说 礛
调用 getPointerPosition() 获取快照并开始本地观察.
主机在窗口不突出重点或点击通过时发出后期更改.
getState() 返回一个生命周期快照并开始原生状态观察.
设置时叫它一次 后来的更改以 hostWindowStateChanged
事件; 订阅 context.on(hostWindowStateChanged, ({ body }) => ...)
并调用返回的函数以取消订阅.
全球快捷键
MacOS 和 Windows 主机可以注册一个仍然活动的快捷键 godot 窗口被隐藏或未聚焦 :
const shortcut = {
keycode: 0x4b, // Godot Key.K
shiftPressed: false,
altPressed: false,
ctrlPressed: false,
metaPressed: false,
commandOrControlAutoremap: true,
};
await platform.globalShortcuts.register(shortcut, ({ state }) => {
console.log(state); // "pressed" or "released"
});
await platform.globalShortcuts.unregister(shortcut);
keycode 使用 Godot 逻辑 Key 数值。 需要每个修改字段;
commandOrControlAutoremap 在 MacOS 和 Windows 控制上选择命令.
一个 onKeyEvent 处理器接收两个状态; 键盘自动重排不
产生额外的呼叫.
系统返回
Android将系统后按键作为平台事件交付. 申请 决定Back的含义:
import { backRequested } from "@gd-kirie/platform";
const stop = context.on(backRequested, () => {
console.log("The system Back button was pressed.");
});
stop();
要处理返回而不是让Android关闭应用程序, 设置
SceneTree.quit_on_go_back
改为 false。没有系统后键,该事件不会在平台上发布.