@vnejs/plugins.views.screens.mainmenu 0.1.6
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/const/events.js +9 -0
- package/const/params.js +18 -0
- package/index.js +10 -0
- package/modules/controller.js +85 -0
- package/modules/mainmenu.js +44 -0
- package/modules/view.js +14 -0
- package/package.json +17 -0
- package/view/index.jsx +42 -0
package/const/events.js
ADDED
package/const/params.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export const PRELOAD_LABELS = [];
|
|
2
|
+
export const PRELOAD_EVENTS = [];
|
|
3
|
+
|
|
4
|
+
export const ITEMS = [];
|
|
5
|
+
|
|
6
|
+
export const BACKGROUND = "loading";
|
|
7
|
+
|
|
8
|
+
export const BACKGROUNDS = [];
|
|
9
|
+
export const BACKGROUNDS_INTERVAL = 0;
|
|
10
|
+
|
|
11
|
+
export const TRANSITION = 500;
|
|
12
|
+
export const ZINDEX = 3000;
|
|
13
|
+
export const LOC_LABEL = "mainMenu";
|
|
14
|
+
|
|
15
|
+
export const VIEW_PROPS = {
|
|
16
|
+
screen: { withBackgroundDark: false, withBackgroundBlur: false, zIndex: ZINDEX, transition: TRANSITION },
|
|
17
|
+
controls: { position: { bottom: 240, left: 240 }, props: { textSize: 96, flexGap: 60, flexDirection: "column" } },
|
|
18
|
+
};
|
package/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { regPlugin } from "@vnejs/shared";
|
|
2
|
+
|
|
3
|
+
import { SUBSCRIBE_EVENTS } from "./const/events";
|
|
4
|
+
import * as params from "./const/params";
|
|
5
|
+
|
|
6
|
+
import { MainMenuController } from "./modules/controller";
|
|
7
|
+
import { MainMenu } from "./modules/mainmenu";
|
|
8
|
+
import { MainMenuView } from "./modules/view";
|
|
9
|
+
|
|
10
|
+
regPlugin("MAINMENU", { events: SUBSCRIBE_EVENTS, params }, [MainMenuController, MainMenu, MainMenuView]);
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { ModuleController } from "@vnejs/module.components";
|
|
2
|
+
import { isNotDisabledNotHided, omitOnClick } from "@vnejs/helpers";
|
|
3
|
+
|
|
4
|
+
const getRandomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
|
|
5
|
+
|
|
6
|
+
export class MainMenuController extends ModuleController {
|
|
7
|
+
name = "mainmenu.controller";
|
|
8
|
+
|
|
9
|
+
emitAccept = () => this.emit(this.EVENTS.MAINMENU.ACCEPT);
|
|
10
|
+
|
|
11
|
+
bgName = this.PARAMS.MAINMENU.BACKGROUND;
|
|
12
|
+
|
|
13
|
+
bgTimeout = null;
|
|
14
|
+
bgIndex = -1;
|
|
15
|
+
|
|
16
|
+
updateEvent = this.EVENTS.MAINMENU.UPDATE;
|
|
17
|
+
controls = {
|
|
18
|
+
[this.CONST.CONTROLS.BUTTONS.ARROW_UP]: this.decCurrentItem,
|
|
19
|
+
[this.CONST.CONTROLS.BUTTONS.ARROW_BOTTOM]: this.incCurrentItem,
|
|
20
|
+
[this.CONST.CONTROLS.BUTTONS.ACCEPT]: this.emitAccept,
|
|
21
|
+
[this.CONST.CONTROLS.BUTTONS.INTERACT]: this.emitAccept,
|
|
22
|
+
};
|
|
23
|
+
controlsIndex = this.PARAMS.MAINMENU.ZINDEX;
|
|
24
|
+
|
|
25
|
+
subscribe = () => {
|
|
26
|
+
this.on(this.EVENTS.MAINMENU.SHOW, this.onShow);
|
|
27
|
+
this.on(this.EVENTS.MAINMENU.HIDE, this.onHide);
|
|
28
|
+
|
|
29
|
+
this.on(this.EVENTS.MAINMENU.CLICK, this.onClick);
|
|
30
|
+
this.on(this.EVENTS.MAINMENU.ACCEPT, this.onAccept);
|
|
31
|
+
|
|
32
|
+
this.on(this.EVENTS.LOCS.CHANGED, this.updateView);
|
|
33
|
+
|
|
34
|
+
this.on(this.EVENTS.STATE.SET, this.onHideForce);
|
|
35
|
+
this.on(this.EVENTS.STATE.CLEAR, this.onHideForce);
|
|
36
|
+
this.on(this.EVENTS.STATE.CLEAR, this.loadBgMedia);
|
|
37
|
+
|
|
38
|
+
this.on(this.EVENTS.SYSTEM.STARTED, this.loadBgMedia);
|
|
39
|
+
|
|
40
|
+
this.on(this.EVENTS.MEMORY.CLEARED, this.updateBg);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
beforeShow = async () => {
|
|
44
|
+
const items = (await this.emitOne(this.EVENTS.MAINMENU.ITEMS)).map(omitOnClick);
|
|
45
|
+
|
|
46
|
+
this.updateState({ items, realItems: items.filter(isNotDisabledNotHided), bgSrc: (await this.loadBgMedia()).src });
|
|
47
|
+
|
|
48
|
+
this.maxCurrentItem = this.state.realItems.length - 1;
|
|
49
|
+
};
|
|
50
|
+
afterShow = () => {
|
|
51
|
+
if (this.PARAMS.MAINMENU.BACKGROUNDS.length && this.PARAMS.MAINMENU.BACKGROUNDS_INTERVAL) {
|
|
52
|
+
this.recalcNewBgIndex();
|
|
53
|
+
this.bgTimeout = setTimeout(this.intervalTickFunc, this.PARAMS.MAINMENU.BACKGROUNDS_INTERVAL);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
afterHide = () => {
|
|
57
|
+
this.setDefaultState();
|
|
58
|
+
|
|
59
|
+
clearTimeout(this.bgTimeout);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
onAccept = async () => this.click((await this.emitOne(this.EVENTS.MAINMENU.ITEMS)).filter(isNotDisabledNotHided), this.currentItem);
|
|
63
|
+
onClick = async ({ index } = {}) => this.click(await this.emitOne(this.EVENTS.MAINMENU.ITEMS), index);
|
|
64
|
+
|
|
65
|
+
updateBg = async () => this.updateStateAndViewFast({ bgSrc: (await this.loadBgMedia()).src });
|
|
66
|
+
|
|
67
|
+
intervalTickFunc = async () => {
|
|
68
|
+
if (!this.state.isShow) return;
|
|
69
|
+
|
|
70
|
+
this.recalcNewBgIndex(this.bgIndex);
|
|
71
|
+
this.bgName = this.PARAMS.MAINMENU.BACKGROUNDS[this.bgIndex];
|
|
72
|
+
|
|
73
|
+
await this.waitRerender();
|
|
74
|
+
await this.updateBg();
|
|
75
|
+
|
|
76
|
+
this.bgTimeout = setTimeout(this.intervalTickFunc, this.PARAMS.MAINMENU.BACKGROUNDS_INTERVAL);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
recalcNewBgIndex = (oldIndex) => {
|
|
80
|
+
do this.bgIndex = getRandomInt(0, this.PARAMS.MAINMENU.BACKGROUNDS.length - 1);
|
|
81
|
+
while (this.bgIndex === oldIndex);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
click = (items, index) => index !== null && items[index]?.onClick?.();
|
|
85
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
|
|
3
|
+
export class MainMenu extends Module {
|
|
4
|
+
name = "mainmenu";
|
|
5
|
+
|
|
6
|
+
subscribe = () => {
|
|
7
|
+
this.on(this.EVENTS.MAINMENU.SHOW, this.onShow);
|
|
8
|
+
this.on(this.EVENTS.MAINMENU.HIDE, this.onHide);
|
|
9
|
+
this.on(this.EVENTS.MAINMENU.ITEMS, this.onItems);
|
|
10
|
+
|
|
11
|
+
this.on(this.EVENTS.MEMORY.CLEARED, this.onMemoryCleared);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
init = () =>
|
|
15
|
+
Promise.all([
|
|
16
|
+
this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { module: this.name, handler: this.onLineExec }),
|
|
17
|
+
this.emit(this.EVENTS.SCENARIO.LINE_LOAD_REG, { module: this.name, handler: this.onLineLoad }),
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
onLineExec = () => this.emit(this.EVENTS.MAINMENU.SHOW);
|
|
21
|
+
onLineLoad = () => this.preload();
|
|
22
|
+
|
|
23
|
+
onShow = () => {
|
|
24
|
+
this.isShow = true;
|
|
25
|
+
this.preload();
|
|
26
|
+
};
|
|
27
|
+
onHide = () => {
|
|
28
|
+
this.isShow = false;
|
|
29
|
+
};
|
|
30
|
+
onItems = async () => {
|
|
31
|
+
await this.waitIsReady();
|
|
32
|
+
|
|
33
|
+
return Promise.all(this.PARAMS.MAINMENU.ITEMS.map(this.getItemByFunc));
|
|
34
|
+
};
|
|
35
|
+
onMemoryCleared = () => this.isShow && this.preload();
|
|
36
|
+
|
|
37
|
+
preload = () => Promise.all([...this.PARAMS.MAINMENU.PRELOAD_LABELS.map(this.emitLabelGet), ...this.PARAMS.MAINMENU.PRELOAD_EVENTS.map(this.emitEvent)]);
|
|
38
|
+
|
|
39
|
+
getItemByFunc = (func) => func(this);
|
|
40
|
+
|
|
41
|
+
emitNext = () => void this.emit(this.EVENTS.SCENARIO.NEXT, { module: this.name });
|
|
42
|
+
emitLabelGet = (label) => this.emit(this.EVENTS.SCENARIO.LABEL_GET, { label });
|
|
43
|
+
emitEvent = (event) => this.emit(event);
|
|
44
|
+
}
|
package/modules/view.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ModuleView } from "@vnejs/module.components";
|
|
2
|
+
|
|
3
|
+
import { render } from "../view";
|
|
4
|
+
|
|
5
|
+
export class MainMenuView extends ModuleView {
|
|
6
|
+
name = "mainmenu.view";
|
|
7
|
+
|
|
8
|
+
locLabel = this.PARAMS.MAINMENU.LOC_LABEL;
|
|
9
|
+
animationTime = this.PARAMS.MAINMENU.TRANSITION;
|
|
10
|
+
updateEvent = this.EVENTS.MAINMENU.UPDATE;
|
|
11
|
+
|
|
12
|
+
renderFunc = render;
|
|
13
|
+
updateHandler = this.onUpdateStoreComponent;
|
|
14
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vnejs/plugins.views.screens.mainmenu",
|
|
3
|
+
"version": "0.1.6",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
7
|
+
"publish:major:plugin": "npm run publish:major",
|
|
8
|
+
"publish:minor:plugin": "npm run publish:minor",
|
|
9
|
+
"publish:patch:plugin": "npm run publish:patch",
|
|
10
|
+
"publish:major": "npm version major && npm publish --access public",
|
|
11
|
+
"publish:minor": "npm version minor && npm publish --access public",
|
|
12
|
+
"publish:patch": "npm version patch && npm publish --access public"
|
|
13
|
+
},
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"description": ""
|
|
17
|
+
}
|
package/view/index.jsx
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
2
|
+
import { createRoot } from "react-dom/client";
|
|
3
|
+
|
|
4
|
+
import { PositionBox, Screen, TextControls } from "@vnejs/uis.react";
|
|
5
|
+
|
|
6
|
+
const EMPTY_ARRAY = [];
|
|
7
|
+
const EMPTY_OBJ = {};
|
|
8
|
+
|
|
9
|
+
const MainMenuControls = ({ locs = EMPTY_OBJ, currentItem = null, realItems = EMPTY_ARRAY, items = EMPTY_ARRAY, ...props } = EMPTY_OBJ) => {
|
|
10
|
+
const mapItems = useCallback(({ locKey, isDisabled, isHided }, i) => ({ text: locs[locKey], isDisabled, isNotRender: isHided, value: i }), [locs]);
|
|
11
|
+
const findSelectedIndex = useCallback((value) => realItems[currentItem]?.locKey === value?.locKey, [realItems, currentItem]);
|
|
12
|
+
const onClick = useCallback((index) => props.emit(props.EVENTS.MAINMENU.CLICK, { index }), EMPTY_ARRAY);
|
|
13
|
+
|
|
14
|
+
const texts = useMemo(() => items.map(mapItems), [items, mapItems]);
|
|
15
|
+
const selectedIndex = useMemo(() => items.findIndex(findSelectedIndex), [items, findSelectedIndex]);
|
|
16
|
+
|
|
17
|
+
const propsControls = useMemo(() => ({ ...props.PARAMS.MAINMENU.VIEW_PROPS.controls.props, texts, onClick, selectedIndex }), [texts, selectedIndex]);
|
|
18
|
+
|
|
19
|
+
return <TextControls {...propsControls} />;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const MainMenu = ({ store, onMount, ...props } = EMPTY_OBJ) => {
|
|
23
|
+
const [state, setState] = useState(EMPTY_OBJ);
|
|
24
|
+
|
|
25
|
+
const { isShow = false, isForce = false, items = EMPTY_ARRAY, bgSrc = "", currentItem = null, realItems = EMPTY_ARRAY, locs = EMPTY_OBJ } = state;
|
|
26
|
+
|
|
27
|
+
useEffect(() => store.subscribe(setState), EMPTY_ARRAY);
|
|
28
|
+
useEffect(() => void onMount(), EMPTY_ARRAY);
|
|
29
|
+
|
|
30
|
+
const propsScreen = useMemo(() => ({ ...props.PARAMS.MAINMENU.VIEW_PROPS.screen, bgSrc, isShow, isForce }), [bgSrc, isShow, isForce]);
|
|
31
|
+
const propsControls = useMemo(() => ({ ...props, items, realItems, currentItem, locs }), [items, realItems, currentItem, locs]);
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<Screen {...propsScreen}>
|
|
35
|
+
<PositionBox {...props.PARAMS.MAINMENU.VIEW_PROPS.controls.position}>
|
|
36
|
+
<MainMenuControls {...propsControls} />
|
|
37
|
+
</PositionBox>
|
|
38
|
+
</Screen>
|
|
39
|
+
);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const render = (props, root) => createRoot(root).render(<MainMenu {...props} />);
|