@vnejs/plugins.controllers.debug.gamepad 0.0.0-stage → 0.2.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +7 -0
- package/dist/modules/controller.d.ts +12 -0
- package/dist/modules/controller.js +10 -0
- package/dist/modules/gamepad.debug.d.ts +16 -0
- package/dist/modules/gamepad.debug.js +41 -0
- package/dist/modules/view.d.ts +9 -0
- package/dist/modules/view.js +8 -0
- package/dist/types.d.ts +13 -0
- package/dist/types.js +1 -0
- package/dist/utils/gamepad-debug.d.ts +5 -0
- package/dist/utils/gamepad-debug.js +40 -0
- package/dist/view/index.d.ts +3 -0
- package/dist/view/index.js +8 -0
- package/package.json +50 -4
- package/src/index.ts +11 -0
- package/src/modules/controller.ts +21 -0
- package/src/modules/gamepad.debug.ts +58 -0
- package/src/modules/view.ts +21 -0
- package/src/tests/controller.test.ts +37 -0
- package/src/tests/gamepad-debug.test.ts +72 -0
- package/src/tests/setup.ts +35 -0
- package/src/types.ts +22 -0
- package/src/utils/gamepad-debug.ts +50 -0
- package/src/view/index.tsx +43 -0
- package/tsconfig.json +10 -0
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "@vnejs/contracts.controllers.debug.gamepad";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import "@vnejs/contracts.controllers.debug.gamepad";
|
|
2
|
+
import { regPlugin } from "@vnejs/registry";
|
|
3
|
+
import { PARAMS, PLUGIN_NAME, SETTINGS_KEYS, SUBSCRIBE_EVENTS } from "@vnejs/contracts.controllers.debug.gamepad";
|
|
4
|
+
import { GamepadDebugController } from "./modules/controller.js";
|
|
5
|
+
import { GamepadDebug } from "./modules/gamepad.debug.js";
|
|
6
|
+
import { GamepadDebugView } from "./modules/view.js";
|
|
7
|
+
regPlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, settings: SETTINGS_KEYS, params: PARAMS }, [GamepadDebugController, GamepadDebug, GamepadDebugView]);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ModuleController } from "@vnejs/module.components";
|
|
2
|
+
import type { GamepadDebugPluginConstants, GamepadDebugPluginEvents, GamepadDebugPluginParams, GamepadDebugPluginSettings } from "../types.js";
|
|
3
|
+
import type { GamepadDebugPluginState } from "../types.js";
|
|
4
|
+
export declare class GamepadDebugController extends ModuleController<GamepadDebugPluginEvents, GamepadDebugPluginConstants, GamepadDebugPluginSettings, GamepadDebugPluginParams, GamepadDebugPluginState> {
|
|
5
|
+
name: string;
|
|
6
|
+
EVENT_UPDATE_VIEW: "vne:gamepad.debug:update";
|
|
7
|
+
EVENT_UPDATE_STATE: "vne:gamepad.debug:update_state";
|
|
8
|
+
EVENT_UPDATE_FAST: "vne:gamepad.debug:update_fast";
|
|
9
|
+
EVENT_UPDATE_FORCE: "vne:gamepad.debug:update_force";
|
|
10
|
+
EVENT_SHOW: "vne:gamepad.debug:show";
|
|
11
|
+
EVENT_HIDE: "vne:gamepad.debug:hide";
|
|
12
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ModuleController } from "@vnejs/module.components";
|
|
2
|
+
export class GamepadDebugController extends ModuleController {
|
|
3
|
+
name = "gamepad.debug.controller";
|
|
4
|
+
EVENT_UPDATE_VIEW = this.EVENTS.GAMEPAD_DEBUG.UPDATE;
|
|
5
|
+
EVENT_UPDATE_STATE = this.EVENTS.GAMEPAD_DEBUG.UPDATE_STATE;
|
|
6
|
+
EVENT_UPDATE_FAST = this.EVENTS.GAMEPAD_DEBUG.UPDATE_FAST;
|
|
7
|
+
EVENT_UPDATE_FORCE = this.EVENTS.GAMEPAD_DEBUG.UPDATE_FORCE;
|
|
8
|
+
EVENT_SHOW = this.EVENTS.GAMEPAD_DEBUG.SHOW;
|
|
9
|
+
EVENT_HIDE = this.EVENTS.GAMEPAD_DEBUG.HIDE;
|
|
10
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
import type { SettingsChangedResult } from "@vnejs/module.core";
|
|
3
|
+
import type { GamepadDebugPluginConstants, GamepadDebugPluginEvents, GamepadDebugPluginParams, GamepadDebugPluginSettings } from "../types.js";
|
|
4
|
+
export declare class GamepadDebug extends ModuleCore<GamepadDebugPluginEvents, GamepadDebugPluginConstants, GamepadDebugPluginSettings, GamepadDebugPluginParams> {
|
|
5
|
+
name: string;
|
|
6
|
+
raf: number | null;
|
|
7
|
+
previousPressed: Record<number, boolean[]>;
|
|
8
|
+
lastByPad: Record<number, string>;
|
|
9
|
+
oldText: string;
|
|
10
|
+
subscribe: () => void;
|
|
11
|
+
setup: () => Promise<import("@vnejs/registry").EmitResults<"vne:settings:init">> | undefined;
|
|
12
|
+
start: () => Promise<import("@vnejs/registry").EmitResults<"vne:gamepad.debug:show">> | undefined;
|
|
13
|
+
onSettingChange: ({ name, value }: SettingsChangedResult) => false | Promise<import("@vnejs/registry").EmitResults<"vne:gamepad.debug:show">> | undefined;
|
|
14
|
+
setEnabled: (value: boolean) => Promise<import("@vnejs/registry").EmitResults<"vne:gamepad.debug:show">> | undefined;
|
|
15
|
+
poll: () => void;
|
|
16
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
import { diffPressedButtons, formatGamepadDebugText } from "../utils/gamepad-debug.js";
|
|
3
|
+
export class GamepadDebug extends ModuleCore {
|
|
4
|
+
name = "gamepad.debug";
|
|
5
|
+
raf = null;
|
|
6
|
+
previousPressed = {};
|
|
7
|
+
lastByPad = {};
|
|
8
|
+
oldText = "";
|
|
9
|
+
subscribe = () => {
|
|
10
|
+
this.on(this.EVENTS.SETTINGS.CHANGED, this.onSettingChange);
|
|
11
|
+
};
|
|
12
|
+
setup = () => this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.GAMEPAD_DEBUG.ENABLED, value: false });
|
|
13
|
+
start = () => this.setEnabled(Boolean(this.shared.settings[this.SETTINGS.GAMEPAD_DEBUG.ENABLED]));
|
|
14
|
+
onSettingChange = ({ name, value }) => name === this.SETTINGS.GAMEPAD_DEBUG.ENABLED && this.setEnabled(Boolean(value));
|
|
15
|
+
setEnabled = (value) => {
|
|
16
|
+
if (value) {
|
|
17
|
+
this.previousPressed = {};
|
|
18
|
+
this.lastByPad = {};
|
|
19
|
+
this.oldText = "";
|
|
20
|
+
this.raf = requestAnimationFrame(this.poll);
|
|
21
|
+
return this.emit(this.EVENTS.GAMEPAD_DEBUG.SHOW);
|
|
22
|
+
}
|
|
23
|
+
if (this.raf !== null)
|
|
24
|
+
cancelAnimationFrame(this.raf);
|
|
25
|
+
this.raf = null;
|
|
26
|
+
this.emit(this.EVENTS.GAMEPAD_DEBUG.HIDE);
|
|
27
|
+
};
|
|
28
|
+
poll = () => {
|
|
29
|
+
const gamepads = [...(globalThis.navigator?.getGamepads?.() ?? [])];
|
|
30
|
+
const { next, lastByPad } = diffPressedButtons(this.previousPressed, gamepads);
|
|
31
|
+
this.previousPressed = next;
|
|
32
|
+
this.lastByPad = { ...this.lastByPad, ...lastByPad };
|
|
33
|
+
const text = formatGamepadDebugText(gamepads, this.lastByPad);
|
|
34
|
+
if (text !== this.oldText) {
|
|
35
|
+
this.oldText = text;
|
|
36
|
+
this.emit(this.EVENTS.GAMEPAD_DEBUG.UPDATE_STATE, { text });
|
|
37
|
+
this.emit(this.EVENTS.GAMEPAD_DEBUG.UPDATE_FAST);
|
|
38
|
+
}
|
|
39
|
+
this.raf = requestAnimationFrame(this.poll);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ModuleView } from "@vnejs/module.components";
|
|
2
|
+
import type { GamepadDebugPluginConstants, GamepadDebugPluginEvents, GamepadDebugPluginParams, GamepadDebugPluginSettings } from "../types.js";
|
|
3
|
+
import type { GamepadDebugPluginState } from "../types.js";
|
|
4
|
+
export declare class GamepadDebugView extends ModuleView<GamepadDebugPluginEvents, GamepadDebugPluginConstants, GamepadDebugPluginSettings, GamepadDebugPluginParams, GamepadDebugPluginState> {
|
|
5
|
+
name: string;
|
|
6
|
+
EVENT_UPDATE_VIEW: "vne:gamepad.debug:update";
|
|
7
|
+
TRANSITION: number;
|
|
8
|
+
renderFunc: import("@vnejs/module.components").ViewRenderFunc<GamepadDebugPluginState>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ModuleView } from "@vnejs/module.components";
|
|
2
|
+
import { render } from "../view/index.js";
|
|
3
|
+
export class GamepadDebugView extends ModuleView {
|
|
4
|
+
name = "gamepad.debug.view";
|
|
5
|
+
EVENT_UPDATE_VIEW = this.EVENTS.GAMEPAD_DEBUG.UPDATE;
|
|
6
|
+
TRANSITION = this.PARAMS.GAMEPAD_DEBUG.TRANSITION;
|
|
7
|
+
renderFunc = render;
|
|
8
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ModuleComponentsConstants, ModuleComponentsEvents, ModuleComponentsParams, ModuleComponentsSettings } from "@vnejs/module.components";
|
|
2
|
+
import type { Params, PluginName, SettingsKeys, SubscribeEvents } from "@vnejs/contracts.controllers.debug.gamepad";
|
|
3
|
+
import type { PluginName as SettingsPluginName, SubscribeEvents as SettingsSubscribeEvents } from "@vnejs/contracts.core.settings";
|
|
4
|
+
import type { PluginName as SystemPluginName, SubscribeEvents as SystemSubscribeEvents } from "@vnejs/contracts.core.system";
|
|
5
|
+
export type GamepadDebugPluginEvents = ModuleComponentsEvents & Record<PluginName, SubscribeEvents> & Record<SettingsPluginName, SettingsSubscribeEvents> & Record<SystemPluginName, SystemSubscribeEvents>;
|
|
6
|
+
export type GamepadDebugPluginConstants = ModuleComponentsConstants;
|
|
7
|
+
export type GamepadDebugPluginSettings = ModuleComponentsSettings & Record<PluginName, SettingsKeys>;
|
|
8
|
+
export type GamepadDebugPluginParams = ModuleComponentsParams & Record<PluginName, Params>;
|
|
9
|
+
export type GamepadDebugPluginState = {
|
|
10
|
+
isShow: boolean;
|
|
11
|
+
isForce: boolean;
|
|
12
|
+
text?: string;
|
|
13
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const formatGamepadDebugText: (gamepads: Array<Gamepad | null>, lastByPad?: Record<number, string>) => string;
|
|
2
|
+
export declare const diffPressedButtons: (previous: Record<number, boolean[]>, gamepads: Array<Gamepad | null>) => {
|
|
3
|
+
next: Record<number, boolean[]>;
|
|
4
|
+
lastByPad: Record<number, string>;
|
|
5
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export const formatGamepadDebugText = (gamepads, lastByPad = {}) => {
|
|
2
|
+
const connected = gamepads.filter((gamepad) => Boolean(gamepad));
|
|
3
|
+
if (!connected.length)
|
|
4
|
+
return "pads: (none — press any button)";
|
|
5
|
+
return connected
|
|
6
|
+
.map((gamepad) => {
|
|
7
|
+
const pressed = gamepad.buttons
|
|
8
|
+
.map((button, index) => (button.pressed ? String(index) : null))
|
|
9
|
+
.filter((index) => index !== null)
|
|
10
|
+
.join(",");
|
|
11
|
+
const last = lastByPad[gamepad.index] ?? "-";
|
|
12
|
+
return [
|
|
13
|
+
`Gamepad index: ${gamepad.index}`,
|
|
14
|
+
`map: ${JSON.stringify(gamepad.mapping || "")}`,
|
|
15
|
+
`id: ${gamepad.id}`,
|
|
16
|
+
`pressed: ${pressed || "-"}`,
|
|
17
|
+
`last: ${last}`,
|
|
18
|
+
].join("\n");
|
|
19
|
+
})
|
|
20
|
+
.join("\n\n");
|
|
21
|
+
};
|
|
22
|
+
export const diffPressedButtons = (previous, gamepads) => {
|
|
23
|
+
const next = {};
|
|
24
|
+
const lastByPad = {};
|
|
25
|
+
for (const gamepad of gamepads) {
|
|
26
|
+
if (!gamepad)
|
|
27
|
+
continue;
|
|
28
|
+
const pressed = gamepad.buttons.map((button) => Boolean(button.pressed));
|
|
29
|
+
next[gamepad.index] = pressed;
|
|
30
|
+
const prev = previous[gamepad.index] ?? [];
|
|
31
|
+
const changed = [];
|
|
32
|
+
pressed.forEach((isPressed, index) => {
|
|
33
|
+
if (isPressed && !prev[index])
|
|
34
|
+
changed.push(`btn=${index}`);
|
|
35
|
+
});
|
|
36
|
+
if (changed.length)
|
|
37
|
+
lastByPad[gamepad.index] = changed.join(",");
|
|
38
|
+
}
|
|
39
|
+
return { next, lastByPad };
|
|
40
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createRenderFunc, Flex, Text, View, Wrap, useStoreState } from "@vnejs/uis.react";
|
|
3
|
+
const GamepadDebugViewComponent = ({ store, onMount, PARAMS }) => {
|
|
4
|
+
const { isShow = false, isForce = false, text = "" } = useStoreState(store, onMount);
|
|
5
|
+
const lines = String(text || "").split("\n");
|
|
6
|
+
return (_jsx(View, { ...PARAMS.GAMEPAD_DEBUG.VIEW_PROPS.view, isShow: isShow, isForce: isForce, children: _jsx(Wrap, { ...PARAMS.GAMEPAD_DEBUG.VIEW_PROPS.wrap, children: _jsx(Flex, { ...PARAMS.GAMEPAD_DEBUG.VIEW_PROPS.flex, children: lines.map((line, index) => (_jsx(Text, { ...PARAMS.GAMEPAD_DEBUG.VIEW_PROPS.text, text: line || " " }, `${index}:${line}`))) }) }) }));
|
|
7
|
+
};
|
|
8
|
+
export const render = createRenderFunc(GamepadDebugViewComponent);
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vnejs/plugins.controllers.debug.gamepad",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.15",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"tsconfig.json"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "npx @vnejs/monorepo test",
|
|
22
|
+
"build": "npx @vnejs/monorepo package",
|
|
23
|
+
"publish:major:plugin": "npm run publish:major",
|
|
24
|
+
"publish:minor:plugin": "npm run publish:minor",
|
|
25
|
+
"publish:patch:plugin": "npm run publish:patch",
|
|
26
|
+
"publish:major": "npx @vnejs/monorepo publish major --access public",
|
|
27
|
+
"publish:minor": "npx @vnejs/monorepo publish minor --access public",
|
|
28
|
+
"publish:patch": "npx @vnejs/monorepo publish patch --access public"
|
|
29
|
+
},
|
|
30
|
+
"author": "",
|
|
31
|
+
"license": "ISC",
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@vnejs/contracts.controllers.debug.gamepad": "~0.2.0"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@vnejs/module.core": "~0.2.0",
|
|
37
|
+
"@vnejs/module.components": "~0.2.0",
|
|
38
|
+
"@vnejs/registry": "~0.2.0",
|
|
39
|
+
"@vnejs/uis.react": "~0.2.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@vnejs/configs.ts-common": "~0.2.0",
|
|
43
|
+
"@vnejs/configs.vitest": "~0.2.0",
|
|
44
|
+
"@vnejs/test-utils": "~0.2.0",
|
|
45
|
+
"@vnejs/contracts.controllers.debug.gamepad": "~0.2.0",
|
|
46
|
+
"@vnejs/module.core": "~0.2.0",
|
|
47
|
+
"@vnejs/module.components": "~0.2.0",
|
|
48
|
+
"@vnejs/registry": "~0.2.0",
|
|
49
|
+
"@vnejs/sources-set": "~0.2.0",
|
|
50
|
+
"@vnejs/uis.react": "~0.2.0"
|
|
51
|
+
}
|
|
52
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import "@vnejs/contracts.controllers.debug.gamepad";
|
|
2
|
+
|
|
3
|
+
import { regPlugin } from "@vnejs/registry";
|
|
4
|
+
|
|
5
|
+
import { PARAMS, PLUGIN_NAME, SETTINGS_KEYS, SUBSCRIBE_EVENTS } from "@vnejs/contracts.controllers.debug.gamepad";
|
|
6
|
+
|
|
7
|
+
import { GamepadDebugController } from "./modules/controller.js";
|
|
8
|
+
import { GamepadDebug } from "./modules/gamepad.debug.js";
|
|
9
|
+
import { GamepadDebugView } from "./modules/view.js";
|
|
10
|
+
|
|
11
|
+
regPlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, settings: SETTINGS_KEYS, params: PARAMS }, [GamepadDebugController, GamepadDebug, GamepadDebugView]);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ModuleController } from "@vnejs/module.components";
|
|
2
|
+
|
|
3
|
+
import type { GamepadDebugPluginConstants, GamepadDebugPluginEvents, GamepadDebugPluginParams, GamepadDebugPluginSettings } from "../types.js";
|
|
4
|
+
import type { GamepadDebugPluginState } from "../types.js";
|
|
5
|
+
|
|
6
|
+
export class GamepadDebugController extends ModuleController<
|
|
7
|
+
GamepadDebugPluginEvents,
|
|
8
|
+
GamepadDebugPluginConstants,
|
|
9
|
+
GamepadDebugPluginSettings,
|
|
10
|
+
GamepadDebugPluginParams,
|
|
11
|
+
GamepadDebugPluginState
|
|
12
|
+
> {
|
|
13
|
+
name = "gamepad.debug.controller";
|
|
14
|
+
|
|
15
|
+
EVENT_UPDATE_VIEW = this.EVENTS.GAMEPAD_DEBUG.UPDATE;
|
|
16
|
+
EVENT_UPDATE_STATE = this.EVENTS.GAMEPAD_DEBUG.UPDATE_STATE;
|
|
17
|
+
EVENT_UPDATE_FAST = this.EVENTS.GAMEPAD_DEBUG.UPDATE_FAST;
|
|
18
|
+
EVENT_UPDATE_FORCE = this.EVENTS.GAMEPAD_DEBUG.UPDATE_FORCE;
|
|
19
|
+
EVENT_SHOW = this.EVENTS.GAMEPAD_DEBUG.SHOW;
|
|
20
|
+
EVENT_HIDE = this.EVENTS.GAMEPAD_DEBUG.HIDE;
|
|
21
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
|
|
3
|
+
import type { SettingsChangedResult } from "@vnejs/module.core";
|
|
4
|
+
|
|
5
|
+
import { diffPressedButtons, formatGamepadDebugText } from "../utils/gamepad-debug.js";
|
|
6
|
+
|
|
7
|
+
import type { GamepadDebugPluginConstants, GamepadDebugPluginEvents, GamepadDebugPluginParams, GamepadDebugPluginSettings } from "../types.js";
|
|
8
|
+
|
|
9
|
+
export class GamepadDebug extends ModuleCore<GamepadDebugPluginEvents, GamepadDebugPluginConstants, GamepadDebugPluginSettings, GamepadDebugPluginParams> {
|
|
10
|
+
name = "gamepad.debug";
|
|
11
|
+
|
|
12
|
+
raf: number | null = null;
|
|
13
|
+
previousPressed: Record<number, boolean[]> = {};
|
|
14
|
+
lastByPad: Record<number, string> = {};
|
|
15
|
+
oldText = "";
|
|
16
|
+
|
|
17
|
+
subscribe = () => {
|
|
18
|
+
this.on(this.EVENTS.SETTINGS.CHANGED, this.onSettingChange);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
setup = () => this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.GAMEPAD_DEBUG.ENABLED, value: false });
|
|
22
|
+
|
|
23
|
+
start = () => this.setEnabled(Boolean(this.shared.settings[this.SETTINGS.GAMEPAD_DEBUG.ENABLED]));
|
|
24
|
+
|
|
25
|
+
onSettingChange = ({ name, value }: SettingsChangedResult) => name === this.SETTINGS.GAMEPAD_DEBUG.ENABLED && this.setEnabled(Boolean(value));
|
|
26
|
+
|
|
27
|
+
setEnabled = (value: boolean) => {
|
|
28
|
+
if (value) {
|
|
29
|
+
this.previousPressed = {};
|
|
30
|
+
this.lastByPad = {};
|
|
31
|
+
this.oldText = "";
|
|
32
|
+
this.raf = requestAnimationFrame(this.poll);
|
|
33
|
+
|
|
34
|
+
return this.emit(this.EVENTS.GAMEPAD_DEBUG.SHOW);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (this.raf !== null) cancelAnimationFrame(this.raf);
|
|
38
|
+
this.raf = null;
|
|
39
|
+
this.emit(this.EVENTS.GAMEPAD_DEBUG.HIDE);
|
|
40
|
+
};
|
|
41
|
+
poll = () => {
|
|
42
|
+
const gamepads = [...(globalThis.navigator?.getGamepads?.() ?? [])];
|
|
43
|
+
const { next, lastByPad } = diffPressedButtons(this.previousPressed, gamepads);
|
|
44
|
+
|
|
45
|
+
this.previousPressed = next;
|
|
46
|
+
this.lastByPad = { ...this.lastByPad, ...lastByPad };
|
|
47
|
+
|
|
48
|
+
const text = formatGamepadDebugText(gamepads, this.lastByPad);
|
|
49
|
+
|
|
50
|
+
if (text !== this.oldText) {
|
|
51
|
+
this.oldText = text;
|
|
52
|
+
this.emit(this.EVENTS.GAMEPAD_DEBUG.UPDATE_STATE, { text });
|
|
53
|
+
this.emit(this.EVENTS.GAMEPAD_DEBUG.UPDATE_FAST);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
this.raf = requestAnimationFrame(this.poll);
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ModuleView } from "@vnejs/module.components";
|
|
2
|
+
|
|
3
|
+
import type { GamepadDebugPluginConstants, GamepadDebugPluginEvents, GamepadDebugPluginParams, GamepadDebugPluginSettings } from "../types.js";
|
|
4
|
+
import type { GamepadDebugPluginState } from "../types.js";
|
|
5
|
+
|
|
6
|
+
import { render } from "../view/index.js";
|
|
7
|
+
export class GamepadDebugView extends ModuleView<
|
|
8
|
+
GamepadDebugPluginEvents,
|
|
9
|
+
GamepadDebugPluginConstants,
|
|
10
|
+
GamepadDebugPluginSettings,
|
|
11
|
+
GamepadDebugPluginParams,
|
|
12
|
+
GamepadDebugPluginState
|
|
13
|
+
> {
|
|
14
|
+
name = "gamepad.debug.view";
|
|
15
|
+
|
|
16
|
+
EVENT_UPDATE_VIEW = this.EVENTS.GAMEPAD_DEBUG.UPDATE;
|
|
17
|
+
|
|
18
|
+
TRANSITION = this.PARAMS.GAMEPAD_DEBUG.TRANSITION;
|
|
19
|
+
|
|
20
|
+
renderFunc = render;
|
|
21
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { GamepadDebugController } from "../modules/controller.js";
|
|
4
|
+
|
|
5
|
+
import { createGamepadDebugTestModule, registerGamepadDebugPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
|
|
6
|
+
|
|
7
|
+
describe("GamepadDebugController", () => {
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
registerGamepadDebugPluginVne();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("UPDATE_STATE updates text in state", async () => {
|
|
13
|
+
const { module, observer } = createGamepadDebugTestModule(GamepadDebugController);
|
|
14
|
+
|
|
15
|
+
await observer.emit(SUBSCRIBE_EVENTS.UPDATE_STATE, { text: "pressed: 1" });
|
|
16
|
+
|
|
17
|
+
expect((module as GamepadDebugController).state.text).toBe("pressed: 1");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("SHOW sets isShow true", async () => {
|
|
21
|
+
const { module, observer } = createGamepadDebugTestModule(GamepadDebugController);
|
|
22
|
+
|
|
23
|
+
await observer.emit(SUBSCRIBE_EVENTS.SHOW);
|
|
24
|
+
|
|
25
|
+
expect((module as GamepadDebugController).state.isShow).toBe(true);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("HIDE sets isShow false", async () => {
|
|
29
|
+
const { module, observer } = createGamepadDebugTestModule(GamepadDebugController, {
|
|
30
|
+
state: { "gamepad.debug.controller": { isShow: true } },
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
await observer.emit(SUBSCRIBE_EVENTS.HIDE);
|
|
34
|
+
|
|
35
|
+
expect((module as GamepadDebugController).state.isShow).toBe(false);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { spyEvent } from "@vnejs/test-utils";
|
|
4
|
+
|
|
5
|
+
import { GamepadDebug } from "../modules/gamepad.debug.js";
|
|
6
|
+
|
|
7
|
+
import { createGamepadDebugTestModule, registerGamepadDebugPluginVne, SETTINGS_KEYS, SUBSCRIBE_EVENTS } from "./setup.js";
|
|
8
|
+
|
|
9
|
+
describe("GamepadDebug", () => {
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
registerGamepadDebugPluginVne();
|
|
12
|
+
vi.stubGlobal(
|
|
13
|
+
"requestAnimationFrame",
|
|
14
|
+
vi.fn(() => 1),
|
|
15
|
+
);
|
|
16
|
+
vi.stubGlobal("cancelAnimationFrame", vi.fn());
|
|
17
|
+
vi.stubGlobal("navigator", { getGamepads: vi.fn(() => []) });
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("setup registers enabled setting as false", async () => {
|
|
21
|
+
const { module } = createGamepadDebugTestModule(GamepadDebug, { subscribe: false });
|
|
22
|
+
const emit = vi.spyOn((module as GamepadDebug).observer!, "emit");
|
|
23
|
+
|
|
24
|
+
await (module as GamepadDebug).setup();
|
|
25
|
+
|
|
26
|
+
expect(emit).toHaveBeenCalledWith("vne:settings:init", { name: SETTINGS_KEYS.ENABLED, value: false }, { module: "gamepad.debug" });
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("setEnabled true emits SHOW and starts rAF", () => {
|
|
30
|
+
const { module, observer } = createGamepadDebugTestModule(GamepadDebug, { subscribe: false });
|
|
31
|
+
const show = spyEvent(observer, SUBSCRIBE_EVENTS.SHOW);
|
|
32
|
+
|
|
33
|
+
(module as GamepadDebug).setEnabled(true);
|
|
34
|
+
|
|
35
|
+
expect(show).toHaveBeenCalledOnce();
|
|
36
|
+
expect(requestAnimationFrame).toHaveBeenCalled();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("setEnabled false emits HIDE and cancels rAF", () => {
|
|
40
|
+
const { module, observer } = createGamepadDebugTestModule(GamepadDebug, { subscribe: false });
|
|
41
|
+
const hide = spyEvent(observer, SUBSCRIBE_EVENTS.HIDE);
|
|
42
|
+
|
|
43
|
+
(module as GamepadDebug).setEnabled(true);
|
|
44
|
+
(module as GamepadDebug).setEnabled(false);
|
|
45
|
+
|
|
46
|
+
expect(hide).toHaveBeenCalledOnce();
|
|
47
|
+
expect(cancelAnimationFrame).toHaveBeenCalled();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("poll emits UPDATE_STATE with pressed button indices", () => {
|
|
51
|
+
const { module, observer } = createGamepadDebugTestModule(GamepadDebug, { subscribe: false });
|
|
52
|
+
const updateState = spyEvent(observer, SUBSCRIBE_EVENTS.UPDATE_STATE);
|
|
53
|
+
|
|
54
|
+
vi.stubGlobal("navigator", {
|
|
55
|
+
getGamepads: vi.fn(() => [
|
|
56
|
+
{
|
|
57
|
+
index: 0,
|
|
58
|
+
id: "Valve Steam Deck",
|
|
59
|
+
mapping: "",
|
|
60
|
+
buttons: [{ pressed: false }, { pressed: true }, { pressed: false }],
|
|
61
|
+
},
|
|
62
|
+
]),
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
(module as GamepadDebug).setEnabled(true);
|
|
66
|
+
(module as GamepadDebug).poll();
|
|
67
|
+
|
|
68
|
+
expect(updateState).toHaveBeenCalledWith({
|
|
69
|
+
text: expect.stringContaining("pressed: 1"),
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { SourcesSet } from "@vnejs/sources-set";
|
|
2
|
+
import { createTestModule as baseCreateTestModule, registerCoreVne, registerVnePlugin, stubSilentEvent } from "@vnejs/test-utils";
|
|
3
|
+
|
|
4
|
+
import { PARAMS, PLUGIN_NAME, SETTINGS_KEYS, SUBSCRIBE_EVENTS } from "@vnejs/contracts.controllers.debug.gamepad";
|
|
5
|
+
|
|
6
|
+
export const registerGamepadDebugPluginVne = () => {
|
|
7
|
+
registerCoreVne();
|
|
8
|
+
registerVnePlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, params: PARAMS, settings: SETTINGS_KEYS });
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const createGamepadDebugTestModule = <T extends Parameters<typeof baseCreateTestModule>[0]>(
|
|
12
|
+
ModuleClass: T,
|
|
13
|
+
options: Parameters<typeof baseCreateTestModule>[1] = {},
|
|
14
|
+
) => {
|
|
15
|
+
const result = baseCreateTestModule(ModuleClass, {
|
|
16
|
+
state: {
|
|
17
|
+
"gamepad.debug.controller": { text: "", isShow: false },
|
|
18
|
+
...(options.state ?? {}),
|
|
19
|
+
},
|
|
20
|
+
shared: {
|
|
21
|
+
settings: { [SETTINGS_KEYS.ENABLED]: false, ...(options.shared?.settings ?? {}) },
|
|
22
|
+
viewForceAnimationSources: new SourcesSet(),
|
|
23
|
+
...(options.shared ?? {}),
|
|
24
|
+
},
|
|
25
|
+
...options,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.SHOW);
|
|
29
|
+
stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.HIDE);
|
|
30
|
+
stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.UPDATE);
|
|
31
|
+
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export { SUBSCRIBE_EVENTS, PARAMS, PLUGIN_NAME, SETTINGS_KEYS };
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ModuleComponentsConstants, ModuleComponentsEvents, ModuleComponentsParams, ModuleComponentsSettings } from "@vnejs/module.components";
|
|
2
|
+
|
|
3
|
+
import type { Params, PluginName, SettingsKeys, SubscribeEvents } from "@vnejs/contracts.controllers.debug.gamepad";
|
|
4
|
+
import type { PluginName as SettingsPluginName, SubscribeEvents as SettingsSubscribeEvents } from "@vnejs/contracts.core.settings";
|
|
5
|
+
import type { PluginName as SystemPluginName, SubscribeEvents as SystemSubscribeEvents } from "@vnejs/contracts.core.system";
|
|
6
|
+
|
|
7
|
+
export type GamepadDebugPluginEvents = ModuleComponentsEvents &
|
|
8
|
+
Record<PluginName, SubscribeEvents> &
|
|
9
|
+
Record<SettingsPluginName, SettingsSubscribeEvents> &
|
|
10
|
+
Record<SystemPluginName, SystemSubscribeEvents>;
|
|
11
|
+
|
|
12
|
+
export type GamepadDebugPluginConstants = ModuleComponentsConstants;
|
|
13
|
+
|
|
14
|
+
export type GamepadDebugPluginSettings = ModuleComponentsSettings & Record<PluginName, SettingsKeys>;
|
|
15
|
+
|
|
16
|
+
export type GamepadDebugPluginParams = ModuleComponentsParams & Record<PluginName, Params>;
|
|
17
|
+
|
|
18
|
+
export type GamepadDebugPluginState = {
|
|
19
|
+
isShow: boolean;
|
|
20
|
+
isForce: boolean;
|
|
21
|
+
text?: string;
|
|
22
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export const formatGamepadDebugText = (gamepads: Array<Gamepad | null>, lastByPad: Record<number, string> = {}): string => {
|
|
2
|
+
const connected = gamepads.filter((gamepad): gamepad is Gamepad => Boolean(gamepad));
|
|
3
|
+
|
|
4
|
+
if (!connected.length) return "pads: (none — press any button)";
|
|
5
|
+
|
|
6
|
+
return connected
|
|
7
|
+
.map((gamepad) => {
|
|
8
|
+
const pressed = gamepad.buttons
|
|
9
|
+
.map((button, index) => (button.pressed ? String(index) : null))
|
|
10
|
+
.filter((index): index is string => index !== null)
|
|
11
|
+
.join(",");
|
|
12
|
+
|
|
13
|
+
const last = lastByPad[gamepad.index] ?? "-";
|
|
14
|
+
|
|
15
|
+
return [
|
|
16
|
+
`Gamepad index: ${gamepad.index}`,
|
|
17
|
+
`map: ${JSON.stringify(gamepad.mapping || "")}`,
|
|
18
|
+
`id: ${gamepad.id}`,
|
|
19
|
+
`pressed: ${pressed || "-"}`,
|
|
20
|
+
`last: ${last}`,
|
|
21
|
+
].join("\n");
|
|
22
|
+
})
|
|
23
|
+
.join("\n\n");
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const diffPressedButtons = (
|
|
27
|
+
previous: Record<number, boolean[]>,
|
|
28
|
+
gamepads: Array<Gamepad | null>,
|
|
29
|
+
): { next: Record<number, boolean[]>; lastByPad: Record<number, string> } => {
|
|
30
|
+
const next: Record<number, boolean[]> = {};
|
|
31
|
+
const lastByPad: Record<number, string> = {};
|
|
32
|
+
|
|
33
|
+
for (const gamepad of gamepads) {
|
|
34
|
+
if (!gamepad) continue;
|
|
35
|
+
|
|
36
|
+
const pressed = gamepad.buttons.map((button) => Boolean(button.pressed));
|
|
37
|
+
next[gamepad.index] = pressed;
|
|
38
|
+
|
|
39
|
+
const prev = previous[gamepad.index] ?? [];
|
|
40
|
+
const changed: string[] = [];
|
|
41
|
+
|
|
42
|
+
pressed.forEach((isPressed, index) => {
|
|
43
|
+
if (isPressed && !prev[index]) changed.push(`btn=${index}`);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
if (changed.length) lastByPad[gamepad.index] = changed.join(",");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return { next, lastByPad };
|
|
50
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { ViewRenderFunc } from "@vnejs/module.components";
|
|
2
|
+
|
|
3
|
+
import { createRenderFunc, Flex, Text, View, Wrap, useStoreState } from "@vnejs/uis.react";
|
|
4
|
+
|
|
5
|
+
import type { ReactComponentProps } from "@vnejs/uis.react";
|
|
6
|
+
|
|
7
|
+
import type { GamepadDebugPluginConstants, GamepadDebugPluginEvents, GamepadDebugPluginParams, GamepadDebugPluginSettings } from "../types.js";
|
|
8
|
+
import type { GamepadDebugPluginState } from "../types.js";
|
|
9
|
+
type GamepadDebugComponentProps = ReactComponentProps<
|
|
10
|
+
GamepadDebugPluginEvents,
|
|
11
|
+
GamepadDebugPluginConstants,
|
|
12
|
+
GamepadDebugPluginSettings,
|
|
13
|
+
GamepadDebugPluginParams,
|
|
14
|
+
GamepadDebugPluginState
|
|
15
|
+
>;
|
|
16
|
+
|
|
17
|
+
const GamepadDebugViewComponent = ({ store, onMount, PARAMS }: GamepadDebugComponentProps) => {
|
|
18
|
+
const { isShow = false, isForce = false, text = "" } = useStoreState<GamepadDebugPluginState>(store, onMount);
|
|
19
|
+
|
|
20
|
+
const lines = String(text || "").split("\n");
|
|
21
|
+
|
|
22
|
+
return (
|
|
23
|
+
<View
|
|
24
|
+
{...PARAMS.GAMEPAD_DEBUG.VIEW_PROPS.view}
|
|
25
|
+
isShow={isShow}
|
|
26
|
+
isForce={isForce}
|
|
27
|
+
>
|
|
28
|
+
<Wrap {...PARAMS.GAMEPAD_DEBUG.VIEW_PROPS.wrap}>
|
|
29
|
+
<Flex {...PARAMS.GAMEPAD_DEBUG.VIEW_PROPS.flex}>
|
|
30
|
+
{lines.map((line, index) => (
|
|
31
|
+
<Text
|
|
32
|
+
key={`${index}:${line}`}
|
|
33
|
+
{...PARAMS.GAMEPAD_DEBUG.VIEW_PROPS.text}
|
|
34
|
+
text={line || " "}
|
|
35
|
+
/>
|
|
36
|
+
))}
|
|
37
|
+
</Flex>
|
|
38
|
+
</Wrap>
|
|
39
|
+
</View>
|
|
40
|
+
);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const render: ViewRenderFunc<GamepadDebugPluginState> = createRenderFunc(GamepadDebugViewComponent);
|
package/tsconfig.json
ADDED
package/README.md
DELETED