@vnejs/plugins.controllers.common.controls.hints 0.0.0-stage → 0.2.12
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 +2 -0
- package/dist/index.js +7 -0
- package/dist/modules/controller.d.ts +43 -0
- package/dist/modules/controller.js +102 -0
- package/dist/modules/view.d.ts +10 -0
- package/dist/modules/view.js +9 -0
- package/dist/types.d.ts +20 -0
- package/dist/types.js +1 -0
- package/dist/utils/controls.hints.d.ts +18 -0
- package/dist/utils/controls.hints.js +9 -0
- package/dist/view/index.d.ts +3 -0
- package/dist/view/index.js +33 -0
- package/package.json +56 -4
- package/src/index.ts +11 -0
- package/src/modules/controller.ts +148 -0
- package/src/modules/view.ts +23 -0
- package/src/tests/controller.test.ts +137 -0
- package/src/tests/setup.ts +42 -0
- package/src/types.ts +37 -0
- package/src/utils/controls.hints.ts +28 -0
- package/src/view/index.tsx +146 -0
- package/tsconfig.json +10 -0
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import "@vnejs/contracts.controls";
|
|
2
|
+
import "@vnejs/contracts.controllers.common.controls.hints";
|
|
3
|
+
import { regPlugin } from "@vnejs/registry";
|
|
4
|
+
import { PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.controllers.common.controls.hints";
|
|
5
|
+
import { ControlsHintsController } from "./modules/controller.js";
|
|
6
|
+
import { ControlsHintsView } from "./modules/view.js";
|
|
7
|
+
regPlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, params: PARAMS }, [ControlsHintsController, ControlsHintsView]);
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ModuleController, type ViewActionPayload } from "@vnejs/module.components";
|
|
2
|
+
import type { ControlsHintsGroup } from "@vnejs/contracts.controllers.common.controls.hints";
|
|
3
|
+
import type { ControlsDevice, ControlsDeviceChangedPayload } from "@vnejs/contracts.controls";
|
|
4
|
+
import { type ControlsHintsViewControl, type ControlsHintsViewGroup } from "../utils/controls.hints.js";
|
|
5
|
+
import type { ControlsHintsPluginConstants, ControlsHintsPluginEvents, ControlsHintsPluginParams, ControlsHintsPluginSettings, ControlsHintsPluginState } from "../types.js";
|
|
6
|
+
export declare class ControlsHintsController extends ModuleController<ControlsHintsPluginEvents, ControlsHintsPluginConstants, ControlsHintsPluginSettings, ControlsHintsPluginParams, ControlsHintsPluginState> {
|
|
7
|
+
name: string;
|
|
8
|
+
EVENT_UPDATE_VIEW: "vne:controls_hints:update";
|
|
9
|
+
EVENT_UPDATE_STATE: "vne:controls_hints:update_state";
|
|
10
|
+
EVENT_UPDATE_FAST: "vne:controls_hints:update_fast";
|
|
11
|
+
EVENT_UPDATE_FORCE: "vne:controls_hints:update_force";
|
|
12
|
+
EVENT_SHOW: "vne:controls_hints:show";
|
|
13
|
+
EVENT_HIDE: "vne:controls_hints:hide";
|
|
14
|
+
device: ControlsDevice;
|
|
15
|
+
subscribe: () => void;
|
|
16
|
+
start: () => Promise<import("@vnejs/registry").EmitResults<string> | undefined>;
|
|
17
|
+
onDeviceChanged: ({ device }?: ControlsDeviceChangedPayload) => Promise<import("@vnejs/registry").EmitResults<string>> | undefined;
|
|
18
|
+
onHideAllForce: () => Promise<import("@vnejs/registry").EmitResults<string>> | undefined;
|
|
19
|
+
onMemoryCleared: () => Promise<import("@vnejs/registry").EmitResults<string> | undefined>;
|
|
20
|
+
onGroupShow: (id: string, { isForce }?: ViewActionPayload) => Promise<import("@vnejs/registry").EmitResults<string> | undefined>;
|
|
21
|
+
onGroupHide: (id: string, { isForce }?: ViewActionPayload) => Promise<import("@vnejs/registry").EmitResults<string>> | undefined;
|
|
22
|
+
onExternalUpdate: () => Promise<import("@vnejs/registry").EmitResults<string>> | undefined;
|
|
23
|
+
updateGroupStateAndView: (state: Partial<ControlsHintsPluginState>, isForce: boolean) => Promise<import("@vnejs/registry").EmitResults<string>> | undefined;
|
|
24
|
+
getGroupConfig: (id: string) => {
|
|
25
|
+
id: string;
|
|
26
|
+
position: import("@vnejs/uis.react.view").ViewProps<unknown>;
|
|
27
|
+
direction: "row" | "column";
|
|
28
|
+
showEvents: string[];
|
|
29
|
+
hideEvents: string[];
|
|
30
|
+
controls: {
|
|
31
|
+
locKey: string;
|
|
32
|
+
keyboard: string[];
|
|
33
|
+
xbox: string[];
|
|
34
|
+
playstation: string[];
|
|
35
|
+
hideOnDevices?: ControlsDevice[] | undefined;
|
|
36
|
+
}[];
|
|
37
|
+
} | undefined;
|
|
38
|
+
preloadAllGroups: (preserveShow?: boolean) => Promise<import("@vnejs/registry").EmitResults<string> | undefined>;
|
|
39
|
+
buildEmptyGroups: () => ControlsHintsViewGroup[];
|
|
40
|
+
withLoadedGroup: (id: string, config: ControlsHintsGroup, isShow: boolean) => Promise<ControlsHintsViewGroup[]>;
|
|
41
|
+
loadControlMedia: (control: ControlsHintsGroup["controls"][number]) => Promise<ControlsHintsViewControl>;
|
|
42
|
+
loadHintIcon: (path: string) => Promise<string>;
|
|
43
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { ModuleController } from "@vnejs/module.components";
|
|
2
|
+
import { normalizeMediaPath, pickIconSrcs } from "../utils/controls.hints.js";
|
|
3
|
+
export class ControlsHintsController extends ModuleController {
|
|
4
|
+
name = "controls.hints.controller";
|
|
5
|
+
EVENT_UPDATE_VIEW = this.EVENTS.CONTROLS_HINTS.UPDATE;
|
|
6
|
+
EVENT_UPDATE_STATE = this.EVENTS.CONTROLS_HINTS.UPDATE_STATE;
|
|
7
|
+
EVENT_UPDATE_FAST = this.EVENTS.CONTROLS_HINTS.UPDATE_FAST;
|
|
8
|
+
EVENT_UPDATE_FORCE = this.EVENTS.CONTROLS_HINTS.UPDATE_FORCE;
|
|
9
|
+
EVENT_SHOW = this.EVENTS.CONTROLS_HINTS.SHOW;
|
|
10
|
+
EVENT_HIDE = this.EVENTS.CONTROLS_HINTS.HIDE;
|
|
11
|
+
device = this.CONST.CONTROLS.DEVICES.KEYBOARD;
|
|
12
|
+
subscribe = () => {
|
|
13
|
+
super.subscribe();
|
|
14
|
+
this.on(this.EVENTS.CONTROLS.DEVICE_CHANGED, this.onDeviceChanged);
|
|
15
|
+
this.on(this.EVENTS.STATE.CLEAR, this.onHideAllForce);
|
|
16
|
+
this.on(this.EVENTS.STATE.SET, this.onHideAllForce);
|
|
17
|
+
this.on(this.EVENTS.MEMORY.CLEARED, this.onMemoryCleared);
|
|
18
|
+
for (const group of this.PARAMS.CONTROLS_HINTS.GROUPS) {
|
|
19
|
+
for (const event of group.showEvents)
|
|
20
|
+
this.on(event, (payload) => this.onGroupShow(group.id, payload));
|
|
21
|
+
for (const event of group.hideEvents)
|
|
22
|
+
this.on(event, (payload) => this.onGroupHide(group.id, payload));
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
start = () => this.preloadAllGroups(false);
|
|
26
|
+
onDeviceChanged = ({ device } = {}) => {
|
|
27
|
+
if (!device)
|
|
28
|
+
return;
|
|
29
|
+
this.device = device;
|
|
30
|
+
const groups = (this.state.groups ?? []).map((group) => ({
|
|
31
|
+
...group,
|
|
32
|
+
controls: group.controls.map((control) => ({ ...control, iconSrcs: pickIconSrcs(control, device) })),
|
|
33
|
+
}));
|
|
34
|
+
return this.updateStateAndViewFast({ device, groups });
|
|
35
|
+
};
|
|
36
|
+
onHideAllForce = () => {
|
|
37
|
+
const groups = (this.state.groups ?? this.buildEmptyGroups()).map((group) => ({ ...group, isShow: false }));
|
|
38
|
+
return this.updateStateAndViewForce({ groups });
|
|
39
|
+
};
|
|
40
|
+
onMemoryCleared = () => this.preloadAllGroups(true);
|
|
41
|
+
onGroupShow = async (id, { isForce = false } = {}) => {
|
|
42
|
+
const current = this.state.groups ?? this.buildEmptyGroups();
|
|
43
|
+
const existing = current.find((group) => group.id === id);
|
|
44
|
+
const needsLoad = !existing || existing.controls.every((control) => !control.keyboardSrcs.length && !control.xboxSrcs.length && !control.playstationSrcs.length);
|
|
45
|
+
if (needsLoad) {
|
|
46
|
+
const config = this.getGroupConfig(id);
|
|
47
|
+
if (!config)
|
|
48
|
+
return;
|
|
49
|
+
const groups = await this.withLoadedGroup(id, config, true);
|
|
50
|
+
return this.updateGroupStateAndView({ groups, device: this.device }, isForce);
|
|
51
|
+
}
|
|
52
|
+
const groups = current.map((group) => (group.id === id ? { ...group, isShow: true } : group));
|
|
53
|
+
return this.updateGroupStateAndView({ groups, device: this.device }, isForce);
|
|
54
|
+
};
|
|
55
|
+
onGroupHide = (id, { isForce = false } = {}) => {
|
|
56
|
+
const groups = (this.state.groups ?? this.buildEmptyGroups()).map((group) => (group.id === id ? { ...group, isShow: false } : group));
|
|
57
|
+
return this.updateGroupStateAndView({ groups }, isForce);
|
|
58
|
+
};
|
|
59
|
+
onExternalUpdate = () => this.updateViewFast();
|
|
60
|
+
updateGroupStateAndView = (state, isForce) => isForce ? this.updateStateAndViewForce(state) : this.updateStateAndViewFast(state);
|
|
61
|
+
getGroupConfig = (id) => this.PARAMS.CONTROLS_HINTS.GROUPS.find((group) => group.id === id);
|
|
62
|
+
preloadAllGroups = async (preserveShow = false) => {
|
|
63
|
+
const showById = new Map((this.state.groups ?? []).map((group) => [group.id, group.isShow]));
|
|
64
|
+
const groups = await Promise.all(this.PARAMS.CONTROLS_HINTS.GROUPS.map(async (config) => {
|
|
65
|
+
const controls = await Promise.all(config.controls.map(this.loadControlMedia));
|
|
66
|
+
return {
|
|
67
|
+
id: config.id,
|
|
68
|
+
isShow: preserveShow ? Boolean(showById.get(config.id)) : false,
|
|
69
|
+
position: config.position,
|
|
70
|
+
direction: config.direction,
|
|
71
|
+
controls,
|
|
72
|
+
};
|
|
73
|
+
}));
|
|
74
|
+
return this.updateStateAndViewFast({ device: this.device, groups });
|
|
75
|
+
};
|
|
76
|
+
buildEmptyGroups = () => this.PARAMS.CONTROLS_HINTS.GROUPS.map((group) => ({
|
|
77
|
+
id: group.id,
|
|
78
|
+
isShow: false,
|
|
79
|
+
position: group.position,
|
|
80
|
+
direction: group.direction,
|
|
81
|
+
controls: group.controls.map((control) => ({ ...control, keyboardSrcs: [], xboxSrcs: [], playstationSrcs: [], iconSrcs: [] })),
|
|
82
|
+
}));
|
|
83
|
+
withLoadedGroup = async (id, config, isShow) => {
|
|
84
|
+
const controls = await Promise.all(config.controls.map(this.loadControlMedia));
|
|
85
|
+
const current = this.state.groups ?? this.buildEmptyGroups();
|
|
86
|
+
return current.map((group) => (group.id === id ? { id: config.id, isShow, position: config.position, direction: config.direction, controls } : group));
|
|
87
|
+
};
|
|
88
|
+
loadControlMedia = async (control) => {
|
|
89
|
+
const [keyboardSrcs, xboxSrcs, playstationSrcs] = await Promise.all([
|
|
90
|
+
Promise.all(control.keyboard.map(this.loadHintIcon)),
|
|
91
|
+
Promise.all(control.xbox.map(this.loadHintIcon)),
|
|
92
|
+
Promise.all(control.playstation.map(this.loadHintIcon)),
|
|
93
|
+
]);
|
|
94
|
+
const loaded = { ...control, keyboardSrcs, xboxSrcs, playstationSrcs, iconSrcs: [] };
|
|
95
|
+
return { ...loaded, iconSrcs: pickIconSrcs(loaded, this.device) };
|
|
96
|
+
};
|
|
97
|
+
loadHintIcon = async (path) => {
|
|
98
|
+
const name = normalizeMediaPath(path);
|
|
99
|
+
const image = await this.loadImageMedia(name, "view", this.shared.settings[this.SETTINGS.LAYER.QUALITY]);
|
|
100
|
+
return image.src;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ModuleView } from "@vnejs/module.components";
|
|
2
|
+
import type { ControlsHintsPluginConstants, ControlsHintsPluginEvents, ControlsHintsPluginParams, ControlsHintsPluginSettings } from "../types.js";
|
|
3
|
+
import type { ControlsHintsPluginState } from "../types.js";
|
|
4
|
+
export declare class ControlsHintsView extends ModuleView<ControlsHintsPluginEvents, ControlsHintsPluginConstants, ControlsHintsPluginSettings, ControlsHintsPluginParams, ControlsHintsPluginState> {
|
|
5
|
+
name: string;
|
|
6
|
+
EVENT_UPDATE_VIEW: "vne:controls_hints:update";
|
|
7
|
+
LOC_LABEL: string;
|
|
8
|
+
TRANSITION: number;
|
|
9
|
+
renderFunc: import("@vnejs/module.components").ViewRenderFunc<ControlsHintsPluginState>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ModuleView } from "@vnejs/module.components";
|
|
2
|
+
import { render } from "../view/index.js";
|
|
3
|
+
export class ControlsHintsView extends ModuleView {
|
|
4
|
+
name = "controls.hints.view";
|
|
5
|
+
EVENT_UPDATE_VIEW = this.EVENTS.CONTROLS_HINTS.UPDATE;
|
|
6
|
+
LOC_LABEL = this.PARAMS.CONTROLS_HINTS.LOC_LABEL;
|
|
7
|
+
TRANSITION = this.PARAMS.CONTROLS_HINTS.TRANSITION;
|
|
8
|
+
renderFunc = render;
|
|
9
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ModuleComponentsConstants, ModuleComponentsEvents, ModuleComponentsParams, ModuleComponentsSettings } from "@vnejs/module.components";
|
|
2
|
+
import type { SettingsKeys as LayerSettingsKeys, PluginName as LayerPluginName } from "@vnejs/contracts.canvas.layer";
|
|
3
|
+
import type { Params, PluginName, SubscribeEvents } from "@vnejs/contracts.controllers.common.controls.hints";
|
|
4
|
+
import type { ControlsDevice, Constants as ControlsConstants, PluginName as ControlsPluginName, SubscribeEvents as ControlsSubscribeEvents } from "@vnejs/contracts.controls";
|
|
5
|
+
import type { PluginName as MemoryPluginName, SubscribeEvents as MemorySubscribeEvents } from "@vnejs/contracts.core.memory";
|
|
6
|
+
import type { PluginName as StatePluginName, SubscribeEvents as StateSubscribeEvents } from "@vnejs/contracts.core.state";
|
|
7
|
+
import type { PluginName as SystemPluginName, SubscribeEvents as SystemSubscribeEvents } from "@vnejs/contracts.core.system";
|
|
8
|
+
import type { ControlsHintsViewGroup } from "./utils/controls.hints.js";
|
|
9
|
+
export type ControlsHintsPluginEvents = ModuleComponentsEvents & Record<PluginName, SubscribeEvents> & Record<ControlsPluginName, ControlsSubscribeEvents> & Record<StatePluginName, StateSubscribeEvents> & Record<SystemPluginName, SystemSubscribeEvents> & Record<MemoryPluginName, MemorySubscribeEvents>;
|
|
10
|
+
export type ControlsHintsPluginConstants = ModuleComponentsConstants & Record<ControlsPluginName, ControlsConstants>;
|
|
11
|
+
export type ControlsHintsPluginSettings = ModuleComponentsSettings & Record<LayerPluginName, LayerSettingsKeys>;
|
|
12
|
+
export type ControlsHintsPluginParams = ModuleComponentsParams & Record<PluginName, Params>;
|
|
13
|
+
export type ControlsHintsPluginState = {
|
|
14
|
+
isShow: boolean;
|
|
15
|
+
isForce: boolean;
|
|
16
|
+
isFast?: boolean;
|
|
17
|
+
device?: ControlsDevice;
|
|
18
|
+
groups?: ControlsHintsViewGroup[];
|
|
19
|
+
locs?: Record<string, string>;
|
|
20
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ControlsHintsControl, ControlsHintsGroup } from "@vnejs/contracts.controllers.common.controls.hints";
|
|
2
|
+
import type { ControlsDevice } from "@vnejs/contracts.controls";
|
|
3
|
+
export type ControlsHintsViewControl = ControlsHintsControl & {
|
|
4
|
+
keyboardSrcs: string[];
|
|
5
|
+
xboxSrcs: string[];
|
|
6
|
+
playstationSrcs: string[];
|
|
7
|
+
iconSrcs: string[];
|
|
8
|
+
};
|
|
9
|
+
export type ControlsHintsViewGroup = {
|
|
10
|
+
id: string;
|
|
11
|
+
isShow: boolean;
|
|
12
|
+
position: ControlsHintsGroup["position"];
|
|
13
|
+
direction: ControlsHintsGroup["direction"];
|
|
14
|
+
controls: ControlsHintsViewControl[];
|
|
15
|
+
};
|
|
16
|
+
export declare const normalizeMediaPath: (path: string) => string;
|
|
17
|
+
export declare const pickIconSrcs: (control: Pick<ControlsHintsViewControl, "keyboardSrcs" | "xboxSrcs" | "playstationSrcs">, device: ControlsDevice) => string[];
|
|
18
|
+
export declare const isControlVisibleOnDevice: (control: Pick<ControlsHintsViewControl, "hideOnDevices">, device: ControlsDevice) => boolean;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const normalizeMediaPath = (path) => path.replace(/^\//, "");
|
|
2
|
+
export const pickIconSrcs = (control, device) => {
|
|
3
|
+
if (device === "xbox")
|
|
4
|
+
return control.xboxSrcs;
|
|
5
|
+
if (device === "playstation")
|
|
6
|
+
return control.playstationSrcs;
|
|
7
|
+
return control.keyboardSrcs;
|
|
8
|
+
};
|
|
9
|
+
export const isControlVisibleOnDevice = (control, device) => !(control.hideOnDevices ?? []).includes(device);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createElement as _createElement } from "react";
|
|
3
|
+
import { Flex, Icon, Text, View, createRenderFunc, useEffect, useMemo, useState, useStoreState } from "@vnejs/uis.react";
|
|
4
|
+
import { isControlVisibleOnDevice } from "../utils/controls.hints.js";
|
|
5
|
+
const renderControl = (control, locs, propsIcon, propsText, propsControlFlex) => (_createElement(Flex, { ...propsControlFlex, key: control.locKey },
|
|
6
|
+
control.iconSrcs.map((src) => (_createElement(Icon, { ...propsIcon, key: src, src: src }))),
|
|
7
|
+
_jsx(Text, { ...propsText, text: locs[control.locKey] ?? "" })));
|
|
8
|
+
const ControlsHintsGroupView = ({ group, device, locs, transition, fallbackZIndex, isForce, propsIcon, propsText, propsControlFlex, propsGroupFlex, }) => {
|
|
9
|
+
const targetControls = useMemo(() => group.controls.filter((control) => isControlVisibleOnDevice(control, device)), [group.controls, device]);
|
|
10
|
+
const shouldShow = group.isShow && targetControls.length > 0;
|
|
11
|
+
const [displayControls, setDisplayControls] = useState(targetControls);
|
|
12
|
+
const [isViewShow, setIsViewShow] = useState(shouldShow);
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (shouldShow) {
|
|
15
|
+
setDisplayControls(targetControls);
|
|
16
|
+
setIsViewShow(true);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
setIsViewShow(false);
|
|
20
|
+
if (isForce) {
|
|
21
|
+
setDisplayControls(targetControls);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const timeout = setTimeout(() => setDisplayControls(targetControls), transition);
|
|
25
|
+
return () => clearTimeout(timeout);
|
|
26
|
+
}, [shouldShow, targetControls, transition, isForce]);
|
|
27
|
+
return (_jsx(View, { ...group.position, isShow: isViewShow, isForce: isForce, transition: transition, zIndex: group.position.zIndex ?? fallbackZIndex, children: _jsx(Flex, { ...propsGroupFlex, direction: group.direction, children: displayControls.map((control) => renderControl(control, locs, propsIcon, propsText, propsControlFlex)) }) }));
|
|
28
|
+
};
|
|
29
|
+
const ControlsHints = ({ store, onMount, PARAMS }) => {
|
|
30
|
+
const { groups = [], locs = {}, device = "keyboard", isForce = false } = useStoreState(store, onMount);
|
|
31
|
+
return (_jsx(_Fragment, { children: groups.map((group) => (_jsx(ControlsHintsGroupView, { group: group, device: device, locs: locs, transition: PARAMS.CONTROLS_HINTS.TRANSITION, fallbackZIndex: PARAMS.CONTROLS_HINTS.ZINDEX, isForce: isForce, propsIcon: PARAMS.CONTROLS_HINTS.VIEW_PROPS.icon, propsText: PARAMS.CONTROLS_HINTS.VIEW_PROPS.text, propsControlFlex: PARAMS.CONTROLS_HINTS.VIEW_PROPS.controlFlex, propsGroupFlex: PARAMS.CONTROLS_HINTS.VIEW_PROPS.groupFlex }, group.id))) }));
|
|
32
|
+
};
|
|
33
|
+
export const render = createRenderFunc(ControlsHints);
|
package/package.json
CHANGED
|
@@ -1,6 +1,58 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vnejs/plugins.controllers.common.controls.hints",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.12",
|
|
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.common.controls.hints": "~0.2.0"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@vnejs/module.core": "~0.2.0",
|
|
37
|
+
"@vnejs/module.components": "~0.2.0",
|
|
38
|
+
"@vnejs/contracts.canvas.layer": "~0.2.0",
|
|
39
|
+
"@vnejs/contracts.controls": "~0.2.0",
|
|
40
|
+
"@vnejs/contracts.core.memory": "~0.2.0",
|
|
41
|
+
"@vnejs/registry": "~0.2.0",
|
|
42
|
+
"@vnejs/uis.react": "~0.2.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@vnejs/configs.ts-common": "~0.2.0",
|
|
46
|
+
"@vnejs/configs.vitest": "~0.2.0",
|
|
47
|
+
"@vnejs/test-utils": "~0.2.0",
|
|
48
|
+
"@vnejs/contracts.canvas.layer": "~0.2.0",
|
|
49
|
+
"@vnejs/contracts.controls": "~0.2.0",
|
|
50
|
+
"@vnejs/contracts.core.memory": "~0.2.0",
|
|
51
|
+
"@vnejs/contracts.controllers.common.controls.hints": "~0.2.0",
|
|
52
|
+
"@vnejs/module.core": "~0.2.0",
|
|
53
|
+
"@vnejs/module.components": "~0.2.0",
|
|
54
|
+
"@vnejs/registry": "~0.2.0",
|
|
55
|
+
"@vnejs/sources-set": "~0.2.0",
|
|
56
|
+
"@vnejs/uis.react": "~0.2.0"
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import "@vnejs/contracts.controls";
|
|
2
|
+
import "@vnejs/contracts.controllers.common.controls.hints";
|
|
3
|
+
|
|
4
|
+
import { regPlugin } from "@vnejs/registry";
|
|
5
|
+
|
|
6
|
+
import { PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.controllers.common.controls.hints";
|
|
7
|
+
|
|
8
|
+
import { ControlsHintsController } from "./modules/controller.js";
|
|
9
|
+
import { ControlsHintsView } from "./modules/view.js";
|
|
10
|
+
|
|
11
|
+
regPlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, params: PARAMS }, [ControlsHintsController, ControlsHintsView]);
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { ModuleController, type ViewActionPayload } from "@vnejs/module.components";
|
|
2
|
+
|
|
3
|
+
import type { ControlsHintsGroup } from "@vnejs/contracts.controllers.common.controls.hints";
|
|
4
|
+
import type { ControlsDevice, ControlsDeviceChangedPayload } from "@vnejs/contracts.controls";
|
|
5
|
+
|
|
6
|
+
import { normalizeMediaPath, pickIconSrcs, type ControlsHintsViewControl, type ControlsHintsViewGroup } from "../utils/controls.hints.js";
|
|
7
|
+
|
|
8
|
+
import type {
|
|
9
|
+
ControlsHintsPluginConstants,
|
|
10
|
+
ControlsHintsPluginEvents,
|
|
11
|
+
ControlsHintsPluginParams,
|
|
12
|
+
ControlsHintsPluginSettings,
|
|
13
|
+
ControlsHintsPluginState,
|
|
14
|
+
} from "../types.js";
|
|
15
|
+
|
|
16
|
+
export class ControlsHintsController extends ModuleController<
|
|
17
|
+
ControlsHintsPluginEvents,
|
|
18
|
+
ControlsHintsPluginConstants,
|
|
19
|
+
ControlsHintsPluginSettings,
|
|
20
|
+
ControlsHintsPluginParams,
|
|
21
|
+
ControlsHintsPluginState
|
|
22
|
+
> {
|
|
23
|
+
name = "controls.hints.controller";
|
|
24
|
+
|
|
25
|
+
EVENT_UPDATE_VIEW = this.EVENTS.CONTROLS_HINTS.UPDATE;
|
|
26
|
+
EVENT_UPDATE_STATE = this.EVENTS.CONTROLS_HINTS.UPDATE_STATE;
|
|
27
|
+
EVENT_UPDATE_FAST = this.EVENTS.CONTROLS_HINTS.UPDATE_FAST;
|
|
28
|
+
EVENT_UPDATE_FORCE = this.EVENTS.CONTROLS_HINTS.UPDATE_FORCE;
|
|
29
|
+
EVENT_SHOW = this.EVENTS.CONTROLS_HINTS.SHOW;
|
|
30
|
+
EVENT_HIDE = this.EVENTS.CONTROLS_HINTS.HIDE;
|
|
31
|
+
|
|
32
|
+
device: ControlsDevice = this.CONST.CONTROLS.DEVICES.KEYBOARD;
|
|
33
|
+
|
|
34
|
+
subscribe = () => {
|
|
35
|
+
super.subscribe();
|
|
36
|
+
this.on(this.EVENTS.CONTROLS.DEVICE_CHANGED, this.onDeviceChanged);
|
|
37
|
+
this.on(this.EVENTS.STATE.CLEAR, this.onHideAllForce);
|
|
38
|
+
this.on(this.EVENTS.STATE.SET, this.onHideAllForce);
|
|
39
|
+
this.on(this.EVENTS.MEMORY.CLEARED, this.onMemoryCleared);
|
|
40
|
+
|
|
41
|
+
for (const group of this.PARAMS.CONTROLS_HINTS.GROUPS) {
|
|
42
|
+
for (const event of group.showEvents) this.on(event, (payload?: ViewActionPayload) => this.onGroupShow(group.id, payload));
|
|
43
|
+
for (const event of group.hideEvents) this.on(event, (payload?: ViewActionPayload) => this.onGroupHide(group.id, payload));
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
start = () => this.preloadAllGroups(false);
|
|
48
|
+
|
|
49
|
+
onDeviceChanged = ({ device }: ControlsDeviceChangedPayload = {}) => {
|
|
50
|
+
if (!device) return;
|
|
51
|
+
|
|
52
|
+
this.device = device;
|
|
53
|
+
const groups = (this.state.groups ?? []).map((group) => ({
|
|
54
|
+
...group,
|
|
55
|
+
controls: group.controls.map((control) => ({ ...control, iconSrcs: pickIconSrcs(control, device) })),
|
|
56
|
+
}));
|
|
57
|
+
|
|
58
|
+
return this.updateStateAndViewFast({ device, groups });
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
onHideAllForce = () => {
|
|
62
|
+
const groups = (this.state.groups ?? this.buildEmptyGroups()).map((group) => ({ ...group, isShow: false }));
|
|
63
|
+
|
|
64
|
+
return this.updateStateAndViewForce({ groups });
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
onMemoryCleared = () => this.preloadAllGroups(true);
|
|
68
|
+
|
|
69
|
+
onGroupShow = async (id: string, { isForce = false }: ViewActionPayload = {}) => {
|
|
70
|
+
const current = this.state.groups ?? this.buildEmptyGroups();
|
|
71
|
+
const existing = current.find((group) => group.id === id);
|
|
72
|
+
const needsLoad =
|
|
73
|
+
!existing || existing.controls.every((control) => !control.keyboardSrcs.length && !control.xboxSrcs.length && !control.playstationSrcs.length);
|
|
74
|
+
|
|
75
|
+
if (needsLoad) {
|
|
76
|
+
const config = this.getGroupConfig(id);
|
|
77
|
+
if (!config) return;
|
|
78
|
+
|
|
79
|
+
const groups = await this.withLoadedGroup(id, config, true);
|
|
80
|
+
|
|
81
|
+
return this.updateGroupStateAndView({ groups, device: this.device }, isForce);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const groups = current.map((group) => (group.id === id ? { ...group, isShow: true } : group));
|
|
85
|
+
|
|
86
|
+
return this.updateGroupStateAndView({ groups, device: this.device }, isForce);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
onGroupHide = (id: string, { isForce = false }: ViewActionPayload = {}) => {
|
|
90
|
+
const groups = (this.state.groups ?? this.buildEmptyGroups()).map((group) => (group.id === id ? { ...group, isShow: false } : group));
|
|
91
|
+
|
|
92
|
+
return this.updateGroupStateAndView({ groups }, isForce);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
onExternalUpdate = () => this.updateViewFast();
|
|
96
|
+
updateGroupStateAndView = (state: Partial<ControlsHintsPluginState>, isForce: boolean) =>
|
|
97
|
+
isForce ? this.updateStateAndViewForce(state) : this.updateStateAndViewFast(state);
|
|
98
|
+
getGroupConfig = (id: string) => this.PARAMS.CONTROLS_HINTS.GROUPS.find((group) => group.id === id);
|
|
99
|
+
preloadAllGroups = async (preserveShow = false) => {
|
|
100
|
+
const showById = new Map((this.state.groups ?? []).map((group) => [group.id, group.isShow]));
|
|
101
|
+
const groups = await Promise.all(
|
|
102
|
+
this.PARAMS.CONTROLS_HINTS.GROUPS.map(async (config) => {
|
|
103
|
+
const controls = await Promise.all(config.controls.map(this.loadControlMedia));
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
id: config.id,
|
|
107
|
+
isShow: preserveShow ? Boolean(showById.get(config.id)) : false,
|
|
108
|
+
position: config.position,
|
|
109
|
+
direction: config.direction,
|
|
110
|
+
controls,
|
|
111
|
+
} satisfies ControlsHintsViewGroup;
|
|
112
|
+
}),
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
return this.updateStateAndViewFast({ device: this.device, groups });
|
|
116
|
+
};
|
|
117
|
+
buildEmptyGroups = (): ControlsHintsViewGroup[] =>
|
|
118
|
+
this.PARAMS.CONTROLS_HINTS.GROUPS.map((group) => ({
|
|
119
|
+
id: group.id,
|
|
120
|
+
isShow: false,
|
|
121
|
+
position: group.position,
|
|
122
|
+
direction: group.direction,
|
|
123
|
+
controls: group.controls.map((control) => ({ ...control, keyboardSrcs: [], xboxSrcs: [], playstationSrcs: [], iconSrcs: [] })),
|
|
124
|
+
}));
|
|
125
|
+
withLoadedGroup = async (id: string, config: ControlsHintsGroup, isShow: boolean) => {
|
|
126
|
+
const controls = await Promise.all(config.controls.map(this.loadControlMedia));
|
|
127
|
+
const current = this.state.groups ?? this.buildEmptyGroups();
|
|
128
|
+
|
|
129
|
+
return current.map((group) => (group.id === id ? { id: config.id, isShow, position: config.position, direction: config.direction, controls } : group));
|
|
130
|
+
};
|
|
131
|
+
loadControlMedia = async (control: ControlsHintsGroup["controls"][number]): Promise<ControlsHintsViewControl> => {
|
|
132
|
+
const [keyboardSrcs, xboxSrcs, playstationSrcs] = await Promise.all([
|
|
133
|
+
Promise.all(control.keyboard.map(this.loadHintIcon)),
|
|
134
|
+
Promise.all(control.xbox.map(this.loadHintIcon)),
|
|
135
|
+
Promise.all(control.playstation.map(this.loadHintIcon)),
|
|
136
|
+
]);
|
|
137
|
+
|
|
138
|
+
const loaded = { ...control, keyboardSrcs, xboxSrcs, playstationSrcs, iconSrcs: [] as string[] };
|
|
139
|
+
|
|
140
|
+
return { ...loaded, iconSrcs: pickIconSrcs(loaded, this.device) };
|
|
141
|
+
};
|
|
142
|
+
loadHintIcon = async (path: string) => {
|
|
143
|
+
const name = normalizeMediaPath(path);
|
|
144
|
+
const image = await this.loadImageMedia(name, "view", this.shared.settings[this.SETTINGS.LAYER.QUALITY]);
|
|
145
|
+
|
|
146
|
+
return image.src;
|
|
147
|
+
};
|
|
148
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ModuleView } from "@vnejs/module.components";
|
|
2
|
+
|
|
3
|
+
import type { ControlsHintsPluginConstants, ControlsHintsPluginEvents, ControlsHintsPluginParams, ControlsHintsPluginSettings } from "../types.js";
|
|
4
|
+
import type { ControlsHintsPluginState } from "../types.js";
|
|
5
|
+
|
|
6
|
+
import { render } from "../view/index.js";
|
|
7
|
+
export class ControlsHintsView extends ModuleView<
|
|
8
|
+
ControlsHintsPluginEvents,
|
|
9
|
+
ControlsHintsPluginConstants,
|
|
10
|
+
ControlsHintsPluginSettings,
|
|
11
|
+
ControlsHintsPluginParams,
|
|
12
|
+
ControlsHintsPluginState
|
|
13
|
+
> {
|
|
14
|
+
name = "controls.hints.view";
|
|
15
|
+
|
|
16
|
+
EVENT_UPDATE_VIEW = this.EVENTS.CONTROLS_HINTS.UPDATE;
|
|
17
|
+
|
|
18
|
+
LOC_LABEL = this.PARAMS.CONTROLS_HINTS.LOC_LABEL;
|
|
19
|
+
|
|
20
|
+
TRANSITION = this.PARAMS.CONTROLS_HINTS.TRANSITION;
|
|
21
|
+
|
|
22
|
+
renderFunc = render;
|
|
23
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { ControlsHintsController } from "../modules/controller.js";
|
|
4
|
+
|
|
5
|
+
import { CONTROLS_CONSTANTS, CONTROLS_EVENTS, createControlsHintsTestModule, PARAMS, registerControlsHintsPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
|
|
6
|
+
|
|
7
|
+
describe("ControlsHintsController", () => {
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
registerControlsHintsPluginVne();
|
|
10
|
+
PARAMS.GROUPS.splice(0, PARAMS.GROUPS.length, {
|
|
11
|
+
id: "mainmenu",
|
|
12
|
+
position: { bottom: 120, right: 120 },
|
|
13
|
+
direction: "row",
|
|
14
|
+
showEvents: ["vne:mainmenu:show"],
|
|
15
|
+
hideEvents: ["vne:mainmenu:hide"],
|
|
16
|
+
controls: [
|
|
17
|
+
{
|
|
18
|
+
locKey: "select",
|
|
19
|
+
keyboard: ["/icons/buttons/keyboard/enter"],
|
|
20
|
+
xbox: ["/icons/buttons/xbox/a"],
|
|
21
|
+
playstation: [],
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("show event loads icon srcs and shows the group", async () => {
|
|
28
|
+
const { module, observer } = createControlsHintsTestModule(ControlsHintsController);
|
|
29
|
+
const controller = module as ControlsHintsController;
|
|
30
|
+
controller.loadImageMedia = vi.fn(async (name: string) => ({ src: `blob:${name}` }) as HTMLImageElement);
|
|
31
|
+
|
|
32
|
+
await observer.emit("vne:mainmenu:show");
|
|
33
|
+
|
|
34
|
+
const group = controller.state.groups?.[0];
|
|
35
|
+
expect(group?.isShow).toBe(true);
|
|
36
|
+
expect(group?.controls[0].keyboardSrcs).toEqual(["blob:icons/buttons/keyboard/enter"]);
|
|
37
|
+
expect(group?.controls[0].xboxSrcs).toEqual(["blob:icons/buttons/xbox/a"]);
|
|
38
|
+
expect(group?.controls[0].playstationSrcs).toEqual([]);
|
|
39
|
+
expect(group?.controls[0].iconSrcs).toEqual(["blob:icons/buttons/keyboard/enter"]);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("hide event hides the group", async () => {
|
|
43
|
+
const { module, observer } = createControlsHintsTestModule(ControlsHintsController);
|
|
44
|
+
const controller = module as ControlsHintsController;
|
|
45
|
+
controller.loadImageMedia = vi.fn(async (name: string) => ({ src: `blob:${name}` }) as HTMLImageElement);
|
|
46
|
+
|
|
47
|
+
await observer.emit("vne:mainmenu:show");
|
|
48
|
+
await observer.emit("vne:mainmenu:hide");
|
|
49
|
+
|
|
50
|
+
expect(controller.state.groups?.[0].isShow).toBe(false);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("DEVICE_CHANGED swaps active iconSrcs to xbox", async () => {
|
|
54
|
+
const { module, observer } = createControlsHintsTestModule(ControlsHintsController);
|
|
55
|
+
const controller = module as ControlsHintsController;
|
|
56
|
+
controller.loadImageMedia = vi.fn(async (name: string) => ({ src: `blob:${name}` }) as HTMLImageElement);
|
|
57
|
+
|
|
58
|
+
await observer.emit("vne:mainmenu:show");
|
|
59
|
+
await observer.emit(CONTROLS_EVENTS.DEVICE_CHANGED, {
|
|
60
|
+
device: CONTROLS_CONSTANTS.DEVICES.XBOX,
|
|
61
|
+
prevDevice: CONTROLS_CONSTANTS.DEVICES.KEYBOARD,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
expect(controller.device).toBe(CONTROLS_CONSTANTS.DEVICES.XBOX);
|
|
65
|
+
expect(controller.state.groups?.[0].controls[0].iconSrcs).toEqual(["blob:icons/buttons/xbox/a"]);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("start preloads icons for all groups", async () => {
|
|
69
|
+
const { module } = createControlsHintsTestModule(ControlsHintsController);
|
|
70
|
+
const controller = module as ControlsHintsController;
|
|
71
|
+
controller.loadImageMedia = vi.fn(async (name: string) => ({ src: `blob:${name}` }) as HTMLImageElement);
|
|
72
|
+
|
|
73
|
+
await controller.start();
|
|
74
|
+
|
|
75
|
+
expect(controller.loadImageMedia).toHaveBeenCalledWith("icons/buttons/xbox/a", "view", "original-4k");
|
|
76
|
+
expect(controller.state.groups?.[0].isShow).toBe(false);
|
|
77
|
+
expect(controller.state.groups?.[0].controls[0].xboxSrcs).toEqual(["blob:icons/buttons/xbox/a"]);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("show event without isForce updates the view as fast", async () => {
|
|
81
|
+
const { module, observer } = createControlsHintsTestModule(ControlsHintsController);
|
|
82
|
+
const controller = module as ControlsHintsController;
|
|
83
|
+
controller.loadImageMedia = vi.fn(async (name: string) => ({ src: `blob:${name}` }) as HTMLImageElement);
|
|
84
|
+
const updates = collectViewUpdates(observer);
|
|
85
|
+
|
|
86
|
+
await observer.emit("vne:mainmenu:show");
|
|
87
|
+
|
|
88
|
+
expect(updates.at(-1)).toMatchObject({ isFast: true, isForce: false });
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("show event with isForce updates the view as force", async () => {
|
|
92
|
+
const { module, observer } = createControlsHintsTestModule(ControlsHintsController);
|
|
93
|
+
const controller = module as ControlsHintsController;
|
|
94
|
+
controller.loadImageMedia = vi.fn(async (name: string) => ({ src: `blob:${name}` }) as HTMLImageElement);
|
|
95
|
+
const updates = collectViewUpdates(observer);
|
|
96
|
+
|
|
97
|
+
await observer.emit("vne:mainmenu:show", { isForce: true });
|
|
98
|
+
|
|
99
|
+
expect(updates.at(-1)).toMatchObject({ isForce: true });
|
|
100
|
+
expect(updates.at(-1)?.isFast).not.toBe(true);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("hide event without isForce updates the view as fast", async () => {
|
|
104
|
+
const { module, observer } = createControlsHintsTestModule(ControlsHintsController);
|
|
105
|
+
const controller = module as ControlsHintsController;
|
|
106
|
+
controller.loadImageMedia = vi.fn(async (name: string) => ({ src: `blob:${name}` }) as HTMLImageElement);
|
|
107
|
+
const updates = collectViewUpdates(observer);
|
|
108
|
+
|
|
109
|
+
await observer.emit("vne:mainmenu:show");
|
|
110
|
+
await observer.emit("vne:mainmenu:hide");
|
|
111
|
+
|
|
112
|
+
expect(updates.at(-1)).toMatchObject({ isFast: true, isForce: false });
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("hide event with isForce updates the view as force", async () => {
|
|
116
|
+
const { module, observer } = createControlsHintsTestModule(ControlsHintsController);
|
|
117
|
+
const controller = module as ControlsHintsController;
|
|
118
|
+
controller.loadImageMedia = vi.fn(async (name: string) => ({ src: `blob:${name}` }) as HTMLImageElement);
|
|
119
|
+
const updates = collectViewUpdates(observer);
|
|
120
|
+
|
|
121
|
+
await observer.emit("vne:mainmenu:show");
|
|
122
|
+
await observer.emit("vne:mainmenu:hide", { isForce: true });
|
|
123
|
+
|
|
124
|
+
expect(updates.at(-1)).toMatchObject({ isForce: true });
|
|
125
|
+
expect(updates.at(-1)?.isFast).not.toBe(true);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
const collectViewUpdates = (observer: { subscribe: (event: string, handler: (payload?: { isFast?: boolean; isForce?: boolean }) => void) => void }) => {
|
|
130
|
+
const updates: Array<{ isFast?: boolean; isForce?: boolean }> = [];
|
|
131
|
+
|
|
132
|
+
observer.subscribe(SUBSCRIBE_EVENTS.UPDATE, (payload) => {
|
|
133
|
+
if (payload) updates.push(payload);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
return updates;
|
|
137
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { SourcesSet } from "@vnejs/sources-set";
|
|
2
|
+
import { createTestModule as baseCreateTestModule, registerCoreVne, registerVnePlugin, stubSilentEvent } from "@vnejs/test-utils";
|
|
3
|
+
|
|
4
|
+
import { PLUGIN_NAME as LAYER, SETTINGS_KEYS as LAYER_SETTINGS } from "@vnejs/contracts.canvas.layer";
|
|
5
|
+
import { PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.controllers.common.controls.hints";
|
|
6
|
+
import { CONSTANTS as CONTROLS_CONSTANTS, PLUGIN_NAME as CONTROLS_PLUGIN_NAME, SUBSCRIBE_EVENTS as CONTROLS_EVENTS } from "@vnejs/contracts.controls";
|
|
7
|
+
|
|
8
|
+
export const registerControlsHintsPluginVne = () => {
|
|
9
|
+
registerCoreVne();
|
|
10
|
+
registerVnePlugin(LAYER, { settings: LAYER_SETTINGS });
|
|
11
|
+
registerVnePlugin(CONTROLS_PLUGIN_NAME, { constants: CONTROLS_CONSTANTS, events: CONTROLS_EVENTS });
|
|
12
|
+
registerVnePlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, params: PARAMS });
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const createControlsHintsTestModule = <T extends Parameters<typeof baseCreateTestModule>[0]>(
|
|
16
|
+
ModuleClass: T,
|
|
17
|
+
options: Parameters<typeof baseCreateTestModule>[1] = {},
|
|
18
|
+
) => {
|
|
19
|
+
const { state: optionsState, shared: optionsShared, ...restOptions } = options;
|
|
20
|
+
const result = baseCreateTestModule(ModuleClass, {
|
|
21
|
+
...restOptions,
|
|
22
|
+
state: {
|
|
23
|
+
"controls.hints.controller": {
|
|
24
|
+
groups: [],
|
|
25
|
+
device: CONTROLS_CONSTANTS.DEVICES.KEYBOARD,
|
|
26
|
+
...((optionsState?.["controls.hints.controller"] as object | undefined) ?? {}),
|
|
27
|
+
},
|
|
28
|
+
...optionsState,
|
|
29
|
+
},
|
|
30
|
+
shared: {
|
|
31
|
+
settings: { [LAYER_SETTINGS.QUALITY]: "original-4k", ...(optionsShared?.settings ?? {}) },
|
|
32
|
+
viewForceAnimationSources: new SourcesSet(),
|
|
33
|
+
...optionsShared,
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
stubSilentEvent(result.observer, SUBSCRIBE_EVENTS.UPDATE);
|
|
38
|
+
|
|
39
|
+
return result;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export { SUBSCRIBE_EVENTS, PARAMS, PLUGIN_NAME, CONTROLS_CONSTANTS, CONTROLS_EVENTS };
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { ModuleComponentsConstants, ModuleComponentsEvents, ModuleComponentsParams, ModuleComponentsSettings } from "@vnejs/module.components";
|
|
2
|
+
|
|
3
|
+
import type { SettingsKeys as LayerSettingsKeys, PluginName as LayerPluginName } from "@vnejs/contracts.canvas.layer";
|
|
4
|
+
import type { Params, PluginName, SubscribeEvents } from "@vnejs/contracts.controllers.common.controls.hints";
|
|
5
|
+
import type {
|
|
6
|
+
ControlsDevice,
|
|
7
|
+
Constants as ControlsConstants,
|
|
8
|
+
PluginName as ControlsPluginName,
|
|
9
|
+
SubscribeEvents as ControlsSubscribeEvents,
|
|
10
|
+
} from "@vnejs/contracts.controls";
|
|
11
|
+
import type { PluginName as MemoryPluginName, SubscribeEvents as MemorySubscribeEvents } from "@vnejs/contracts.core.memory";
|
|
12
|
+
import type { PluginName as StatePluginName, SubscribeEvents as StateSubscribeEvents } from "@vnejs/contracts.core.state";
|
|
13
|
+
import type { PluginName as SystemPluginName, SubscribeEvents as SystemSubscribeEvents } from "@vnejs/contracts.core.system";
|
|
14
|
+
|
|
15
|
+
import type { ControlsHintsViewGroup } from "./utils/controls.hints.js";
|
|
16
|
+
|
|
17
|
+
export type ControlsHintsPluginEvents = ModuleComponentsEvents &
|
|
18
|
+
Record<PluginName, SubscribeEvents> &
|
|
19
|
+
Record<ControlsPluginName, ControlsSubscribeEvents> &
|
|
20
|
+
Record<StatePluginName, StateSubscribeEvents> &
|
|
21
|
+
Record<SystemPluginName, SystemSubscribeEvents> &
|
|
22
|
+
Record<MemoryPluginName, MemorySubscribeEvents>;
|
|
23
|
+
|
|
24
|
+
export type ControlsHintsPluginConstants = ModuleComponentsConstants & Record<ControlsPluginName, ControlsConstants>;
|
|
25
|
+
|
|
26
|
+
export type ControlsHintsPluginSettings = ModuleComponentsSettings & Record<LayerPluginName, LayerSettingsKeys>;
|
|
27
|
+
|
|
28
|
+
export type ControlsHintsPluginParams = ModuleComponentsParams & Record<PluginName, Params>;
|
|
29
|
+
|
|
30
|
+
export type ControlsHintsPluginState = {
|
|
31
|
+
isShow: boolean;
|
|
32
|
+
isForce: boolean;
|
|
33
|
+
isFast?: boolean;
|
|
34
|
+
device?: ControlsDevice;
|
|
35
|
+
groups?: ControlsHintsViewGroup[];
|
|
36
|
+
locs?: Record<string, string>;
|
|
37
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ControlsHintsControl, ControlsHintsGroup } from "@vnejs/contracts.controllers.common.controls.hints";
|
|
2
|
+
import type { ControlsDevice } from "@vnejs/contracts.controls";
|
|
3
|
+
|
|
4
|
+
export type ControlsHintsViewControl = ControlsHintsControl & {
|
|
5
|
+
keyboardSrcs: string[];
|
|
6
|
+
xboxSrcs: string[];
|
|
7
|
+
playstationSrcs: string[];
|
|
8
|
+
iconSrcs: string[];
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type ControlsHintsViewGroup = {
|
|
12
|
+
id: string;
|
|
13
|
+
isShow: boolean;
|
|
14
|
+
position: ControlsHintsGroup["position"];
|
|
15
|
+
direction: ControlsHintsGroup["direction"];
|
|
16
|
+
controls: ControlsHintsViewControl[];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const normalizeMediaPath = (path: string) => path.replace(/^\//, "");
|
|
20
|
+
|
|
21
|
+
export const pickIconSrcs = (control: Pick<ControlsHintsViewControl, "keyboardSrcs" | "xboxSrcs" | "playstationSrcs">, device: ControlsDevice) => {
|
|
22
|
+
if (device === "xbox") return control.xboxSrcs;
|
|
23
|
+
if (device === "playstation") return control.playstationSrcs;
|
|
24
|
+
return control.keyboardSrcs;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const isControlVisibleOnDevice = (control: Pick<ControlsHintsViewControl, "hideOnDevices">, device: ControlsDevice) =>
|
|
28
|
+
!(control.hideOnDevices ?? []).includes(device);
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import type { ViewRenderFunc } from "@vnejs/module.components";
|
|
2
|
+
|
|
3
|
+
import { Flex, Icon, Text, View, createRenderFunc, useEffect, useMemo, useState, useStoreState } from "@vnejs/uis.react";
|
|
4
|
+
|
|
5
|
+
import type { ReactComponentProps } from "@vnejs/uis.react";
|
|
6
|
+
|
|
7
|
+
import type { ControlsDevice } from "@vnejs/contracts.controls";
|
|
8
|
+
|
|
9
|
+
import { isControlVisibleOnDevice, type ControlsHintsViewControl, type ControlsHintsViewGroup } from "../utils/controls.hints.js";
|
|
10
|
+
|
|
11
|
+
import type {
|
|
12
|
+
ControlsHintsPluginConstants,
|
|
13
|
+
ControlsHintsPluginEvents,
|
|
14
|
+
ControlsHintsPluginParams,
|
|
15
|
+
ControlsHintsPluginSettings,
|
|
16
|
+
ControlsHintsPluginState,
|
|
17
|
+
} from "../types.js";
|
|
18
|
+
|
|
19
|
+
type ControlsHintsComponentProps = ReactComponentProps<
|
|
20
|
+
ControlsHintsPluginEvents,
|
|
21
|
+
ControlsHintsPluginConstants,
|
|
22
|
+
ControlsHintsPluginSettings,
|
|
23
|
+
ControlsHintsPluginParams,
|
|
24
|
+
ControlsHintsPluginState
|
|
25
|
+
>;
|
|
26
|
+
|
|
27
|
+
type ControlsHintsGroupViewProps = {
|
|
28
|
+
group: ControlsHintsViewGroup;
|
|
29
|
+
device: ControlsDevice;
|
|
30
|
+
locs: Record<string, string>;
|
|
31
|
+
transition: number;
|
|
32
|
+
fallbackZIndex: number;
|
|
33
|
+
isForce: boolean;
|
|
34
|
+
propsIcon: Record<string, unknown>;
|
|
35
|
+
propsText: Record<string, unknown>;
|
|
36
|
+
propsControlFlex: Record<string, unknown>;
|
|
37
|
+
propsGroupFlex: Record<string, unknown>;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const renderControl = (
|
|
41
|
+
control: ControlsHintsViewControl,
|
|
42
|
+
locs: Record<string, string>,
|
|
43
|
+
propsIcon: Record<string, unknown>,
|
|
44
|
+
propsText: Record<string, unknown>,
|
|
45
|
+
propsControlFlex: Record<string, unknown>,
|
|
46
|
+
) => (
|
|
47
|
+
<Flex
|
|
48
|
+
{...propsControlFlex}
|
|
49
|
+
key={control.locKey}
|
|
50
|
+
>
|
|
51
|
+
{control.iconSrcs.map((src) => (
|
|
52
|
+
<Icon
|
|
53
|
+
{...propsIcon}
|
|
54
|
+
key={src}
|
|
55
|
+
src={src}
|
|
56
|
+
/>
|
|
57
|
+
))}
|
|
58
|
+
<Text
|
|
59
|
+
{...propsText}
|
|
60
|
+
text={locs[control.locKey] ?? ""}
|
|
61
|
+
/>
|
|
62
|
+
</Flex>
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const ControlsHintsGroupView = ({
|
|
66
|
+
group,
|
|
67
|
+
device,
|
|
68
|
+
locs,
|
|
69
|
+
transition,
|
|
70
|
+
fallbackZIndex,
|
|
71
|
+
isForce,
|
|
72
|
+
propsIcon,
|
|
73
|
+
propsText,
|
|
74
|
+
propsControlFlex,
|
|
75
|
+
propsGroupFlex,
|
|
76
|
+
}: ControlsHintsGroupViewProps) => {
|
|
77
|
+
const targetControls = useMemo(() => group.controls.filter((control) => isControlVisibleOnDevice(control, device)), [group.controls, device]);
|
|
78
|
+
const shouldShow = group.isShow && targetControls.length > 0;
|
|
79
|
+
|
|
80
|
+
const [displayControls, setDisplayControls] = useState(targetControls);
|
|
81
|
+
const [isViewShow, setIsViewShow] = useState(shouldShow);
|
|
82
|
+
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
if (shouldShow) {
|
|
85
|
+
setDisplayControls(targetControls);
|
|
86
|
+
setIsViewShow(true);
|
|
87
|
+
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
setIsViewShow(false);
|
|
92
|
+
|
|
93
|
+
if (isForce) {
|
|
94
|
+
setDisplayControls(targetControls);
|
|
95
|
+
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const timeout = setTimeout(() => setDisplayControls(targetControls), transition);
|
|
100
|
+
|
|
101
|
+
return () => clearTimeout(timeout);
|
|
102
|
+
}, [shouldShow, targetControls, transition, isForce]);
|
|
103
|
+
|
|
104
|
+
return (
|
|
105
|
+
<View
|
|
106
|
+
{...group.position}
|
|
107
|
+
isShow={isViewShow}
|
|
108
|
+
isForce={isForce}
|
|
109
|
+
transition={transition}
|
|
110
|
+
zIndex={group.position.zIndex ?? fallbackZIndex}
|
|
111
|
+
>
|
|
112
|
+
<Flex
|
|
113
|
+
{...propsGroupFlex}
|
|
114
|
+
direction={group.direction}
|
|
115
|
+
>
|
|
116
|
+
{displayControls.map((control) => renderControl(control, locs, propsIcon, propsText, propsControlFlex))}
|
|
117
|
+
</Flex>
|
|
118
|
+
</View>
|
|
119
|
+
);
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const ControlsHints = ({ store, onMount, PARAMS }: ControlsHintsComponentProps) => {
|
|
123
|
+
const { groups = [], locs = {}, device = "keyboard", isForce = false } = useStoreState<ControlsHintsPluginState>(store, onMount);
|
|
124
|
+
|
|
125
|
+
return (
|
|
126
|
+
<>
|
|
127
|
+
{groups.map((group) => (
|
|
128
|
+
<ControlsHintsGroupView
|
|
129
|
+
key={group.id}
|
|
130
|
+
group={group}
|
|
131
|
+
device={device}
|
|
132
|
+
locs={locs}
|
|
133
|
+
transition={PARAMS.CONTROLS_HINTS.TRANSITION}
|
|
134
|
+
fallbackZIndex={PARAMS.CONTROLS_HINTS.ZINDEX}
|
|
135
|
+
isForce={isForce}
|
|
136
|
+
propsIcon={PARAMS.CONTROLS_HINTS.VIEW_PROPS.icon}
|
|
137
|
+
propsText={PARAMS.CONTROLS_HINTS.VIEW_PROPS.text}
|
|
138
|
+
propsControlFlex={PARAMS.CONTROLS_HINTS.VIEW_PROPS.controlFlex}
|
|
139
|
+
propsGroupFlex={PARAMS.CONTROLS_HINTS.VIEW_PROPS.groupFlex}
|
|
140
|
+
/>
|
|
141
|
+
))}
|
|
142
|
+
</>
|
|
143
|
+
);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
export const render: ViewRenderFunc<ControlsHintsPluginState> = createRenderFunc(ControlsHints);
|
package/tsconfig.json
ADDED
package/README.md
DELETED