@dcl-regenesislabs/react-ecs 0.0.0-stage → 7.0.0-42707159203.commit-ff138cd
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/README.md +130 -2
- package/dist/components/Button/index.d.ts +15 -0
- package/dist/components/Button/index.js +62 -0
- package/dist/components/Button/types.d.ts +16 -0
- package/dist/components/Button/types.js +1 -0
- package/dist/components/Dropdown/index.d.ts +21 -0
- package/dist/components/Dropdown/index.js +48 -0
- package/dist/components/Dropdown/types.d.ts +13 -0
- package/dist/components/Dropdown/types.js +1 -0
- package/dist/components/Input/index.d.ts +25 -0
- package/dist/components/Input/index.js +49 -0
- package/dist/components/Input/types.d.ts +15 -0
- package/dist/components/Input/types.js +1 -0
- package/dist/components/InteractableArea/index.d.ts +23 -0
- package/dist/components/InteractableArea/index.js +34 -0
- package/dist/components/InteractableArea/types.d.ts +23 -0
- package/dist/components/InteractableArea/types.js +1 -0
- package/dist/components/Label/index.d.ts +17 -0
- package/dist/components/Label/index.js +37 -0
- package/dist/components/Label/types.d.ts +34 -0
- package/dist/components/Label/types.js +1 -0
- package/dist/components/Label/utils.d.ts +11 -0
- package/dist/components/Label/utils.js +69 -0
- package/dist/components/ScreenInsetArea/index.d.ts +22 -0
- package/dist/components/ScreenInsetArea/index.js +33 -0
- package/dist/components/ScreenInsetArea/types.d.ts +22 -0
- package/dist/components/ScreenInsetArea/types.js +1 -0
- package/dist/components/index.d.ts +26 -0
- package/dist/components/index.js +36 -0
- package/dist/components/listeners/types.d.ts +19 -0
- package/dist/components/listeners/types.js +11 -0
- package/dist/components/types.d.ts +45 -0
- package/dist/components/types.js +1 -0
- package/dist/components/uiBackground/index.d.ts +6 -0
- package/dist/components/uiBackground/index.js +16 -0
- package/dist/components/uiBackground/types.d.ts +61 -0
- package/dist/components/uiBackground/types.js +1 -0
- package/dist/components/uiBackground/utils.d.ts +1 -0
- package/dist/components/uiBackground/utils.js +51 -0
- package/dist/components/uiTransform/index.d.ts +6 -0
- package/dist/components/uiTransform/index.js +91 -0
- package/dist/components/uiTransform/types.d.ts +134 -0
- package/dist/components/uiTransform/types.js +1 -0
- package/dist/components/uiTransform/utils.d.ts +42 -0
- package/dist/components/uiTransform/utils.js +246 -0
- package/dist/components/utils.d.ts +1 -0
- package/dist/components/utils.js +195 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +15 -0
- package/dist/platform.d.ts +12 -0
- package/dist/platform.js +26 -0
- package/dist/react-ecs.d.ts +87 -0
- package/dist/react-ecs.js +10 -0
- package/dist/reconciler/index.d.ts +6 -0
- package/dist/reconciler/index.js +322 -0
- package/dist/reconciler/types.d.ts +31 -0
- package/dist/reconciler/types.js +1 -0
- package/dist/reconciler/utils.d.ts +40 -0
- package/dist/reconciler/utils.js +174 -0
- package/dist/system.d.ts +92 -0
- package/dist/system.js +264 -0
- package/package.json +41 -3
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { isListener } from '../components';
|
|
2
|
+
export function propsChanged(component, prevProps, nextProps) {
|
|
3
|
+
if (prevProps && !nextProps) {
|
|
4
|
+
return { type: 'delete', component };
|
|
5
|
+
}
|
|
6
|
+
if (!nextProps) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
if (!prevProps && nextProps) {
|
|
10
|
+
return { type: 'add', props: nextProps, component };
|
|
11
|
+
}
|
|
12
|
+
if (isListener(component)) {
|
|
13
|
+
if (!isEqual(prevProps, nextProps)) {
|
|
14
|
+
return { type: 'put', component, props: nextProps };
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const changes = {};
|
|
18
|
+
// Iterate prevProps (this also catches removed keys, where nextProps[k] is undefined)...
|
|
19
|
+
for (const k in prevProps) {
|
|
20
|
+
const propKey = k;
|
|
21
|
+
if (!isEqual(prevProps[propKey], nextProps[propKey])) {
|
|
22
|
+
changes[propKey] = nextProps[propKey];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
// ...then catch keys present only in nextProps (an optional prop set for the first time,
|
|
26
|
+
// e.g. an undefined value becoming defined). Iterating prevProps alone would drop these.
|
|
27
|
+
// Two allocation-free loops are used instead of a Set union to keep this hot path cheap.
|
|
28
|
+
// These keys aren't in prevProps, so emitting them whenever they're defined is both cheaper
|
|
29
|
+
// than (and avoids the falsy-value trap of) an isEqual(undefined, ...) comparison.
|
|
30
|
+
for (const k in nextProps) {
|
|
31
|
+
if (k in prevProps)
|
|
32
|
+
continue;
|
|
33
|
+
const propKey = k;
|
|
34
|
+
if (nextProps[propKey] !== undefined) {
|
|
35
|
+
changes[propKey] = nextProps[propKey];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (!Object.keys(changes).length) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
return { type: 'put', props: changes, component };
|
|
42
|
+
}
|
|
43
|
+
// as any HACK so every time we add a new component, we must add also the component here.
|
|
44
|
+
const entityComponent = {
|
|
45
|
+
uiText: undefined,
|
|
46
|
+
uiBackground: undefined,
|
|
47
|
+
uiTransform: undefined,
|
|
48
|
+
onMouseDown: undefined,
|
|
49
|
+
onMouseUp: undefined,
|
|
50
|
+
onMouseEnter: undefined,
|
|
51
|
+
onMouseLeave: undefined,
|
|
52
|
+
uiInput: undefined,
|
|
53
|
+
uiDropdown: undefined,
|
|
54
|
+
uiInputBinding: undefined
|
|
55
|
+
};
|
|
56
|
+
export const componentKeys = Object.keys(entityComponent);
|
|
57
|
+
export function isEqual(val1, val2) {
|
|
58
|
+
if (!val1 && !val2) {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
if (!val1 || !val2) {
|
|
62
|
+
return val1 === val2;
|
|
63
|
+
}
|
|
64
|
+
if (val1 === val2) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
if (typeof val1 !== typeof val2) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
if (typeof val1 !== 'object') {
|
|
71
|
+
return val1 === val2;
|
|
72
|
+
}
|
|
73
|
+
if (Array.isArray(val1) && Array.isArray(val2)) {
|
|
74
|
+
if (val1.length !== val2.length) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (Object.keys(val1).length !== Object.keys(val2).length) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
if (JSON.stringify(val1) === JSON.stringify(val2)) {
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
for (const key in val1) {
|
|
85
|
+
if (!isEqual(val1[key], val2[key])) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/* istanbul ignore next */
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
export const isNotUndefined = (val) => {
|
|
93
|
+
return !!val;
|
|
94
|
+
};
|
|
95
|
+
export const noopConfig = {
|
|
96
|
+
supportsMutation: true,
|
|
97
|
+
supportsPersistence: false,
|
|
98
|
+
noTimeout: -1,
|
|
99
|
+
isPrimaryRenderer: true,
|
|
100
|
+
supportsHydration: false,
|
|
101
|
+
/* istanbul ignore next */
|
|
102
|
+
insertInContainerBefore(_container, _child, _beforeChild) { },
|
|
103
|
+
detachDeletedInstance(_node) { },
|
|
104
|
+
/* istanbul ignore next */
|
|
105
|
+
hideInstance(_instance) { },
|
|
106
|
+
/* istanbul ignore next */
|
|
107
|
+
hideTextInstance(_textInstance) { },
|
|
108
|
+
/* istanbul ignore next */
|
|
109
|
+
unhideInstance(_instance, _props) { },
|
|
110
|
+
/* istanbul ignore next */
|
|
111
|
+
unhideTextInstance(_textInstance, _text) { },
|
|
112
|
+
/* istanbul ignore next */
|
|
113
|
+
clearContainer(_container) { },
|
|
114
|
+
/* istanbul ignore next */
|
|
115
|
+
getCurrentEventPriority() {
|
|
116
|
+
/* istanbul ignore next */
|
|
117
|
+
return 0;
|
|
118
|
+
},
|
|
119
|
+
/* istanbul ignore next */
|
|
120
|
+
getInstanceFromNode(_node) {
|
|
121
|
+
/* istanbul ignore next */
|
|
122
|
+
return null;
|
|
123
|
+
},
|
|
124
|
+
/* istanbul ignore next */
|
|
125
|
+
beforeActiveInstanceBlur() { },
|
|
126
|
+
/* istanbul ignore next */
|
|
127
|
+
afterActiveInstanceBlur() { },
|
|
128
|
+
/* istanbul ignore next */
|
|
129
|
+
prepareScopeUpdate() { },
|
|
130
|
+
/* istanbul ignore next */
|
|
131
|
+
getInstanceFromScope() {
|
|
132
|
+
/* istanbul ignore next */
|
|
133
|
+
return null;
|
|
134
|
+
},
|
|
135
|
+
/* istanbul ignore next */
|
|
136
|
+
commitMount(_instance, _type, _props, _internalInstanceHandle) { },
|
|
137
|
+
/* istanbul ignore next */
|
|
138
|
+
resetTextContent(_instance) { },
|
|
139
|
+
/* istanbul ignore next */
|
|
140
|
+
commitTextUpdate(_textInstance, _oldText, _newText) { },
|
|
141
|
+
prepareForCommit(_containerInfo) {
|
|
142
|
+
return null;
|
|
143
|
+
},
|
|
144
|
+
resetAfterCommit(_containerInfo) { },
|
|
145
|
+
/* istanbul ignore next */
|
|
146
|
+
preparePortalMount(_containerInfo) { },
|
|
147
|
+
/* istanbul ignore next */
|
|
148
|
+
createTextInstance(_text, _rootContainer, _hostContext, _internalHandle) {
|
|
149
|
+
/* istanbul ignore next */
|
|
150
|
+
return {};
|
|
151
|
+
},
|
|
152
|
+
/* istanbul ignore next */
|
|
153
|
+
scheduleTimeout(_fn, _delay) { },
|
|
154
|
+
/* istanbul ignore next */
|
|
155
|
+
cancelTimeout(_id) { },
|
|
156
|
+
shouldSetTextContent(_type, _props) {
|
|
157
|
+
return false;
|
|
158
|
+
},
|
|
159
|
+
getRootHostContext(_rootContainer) {
|
|
160
|
+
return null;
|
|
161
|
+
},
|
|
162
|
+
getChildHostContext(_parentHostContext, _type, _rootContainer) {
|
|
163
|
+
/* istanbul ignore next */
|
|
164
|
+
return null;
|
|
165
|
+
},
|
|
166
|
+
/* istanbul ignore next */
|
|
167
|
+
getPublicInstance(instance) {
|
|
168
|
+
/* istanbul ignore next */
|
|
169
|
+
return instance;
|
|
170
|
+
},
|
|
171
|
+
finalizeInitialChildren(_instance, _type, _props, _rootContainer, _hostContext) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
};
|
package/dist/system.d.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { type Entity, type IEngine, type PointerEventsSystem } from '@dcl/ecs';
|
|
2
|
+
import type { ReactEcs } from './react-ecs';
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export type UiComponent = () => ReactEcs.JSX.ReactNode;
|
|
7
|
+
/**
|
|
8
|
+
* Screen area used to position a renderer's UI entities:
|
|
9
|
+
* - `'device'` (default): the device safe area (excludes notch, status bar,
|
|
10
|
+
* rounded corners), reported in `UiCanvasInformation.screenInsetArea`.
|
|
11
|
+
* - `'interactable'`: the area free of the Explorer's native HUD (minimap, chat, ...),
|
|
12
|
+
* reported in `UiCanvasInformation.interactableArea`.
|
|
13
|
+
* - `'none'`: the whole screen, with 0,0 at its top-left corner.
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
export type UiScreenInset = 'device' | 'interactable' | 'none';
|
|
17
|
+
/**
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
export type UiRendererOptions = {
|
|
21
|
+
virtualWidth?: number;
|
|
22
|
+
virtualHeight?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Screen area the renderer's UI is positioned in. Defaults to `'device'`, so UI
|
|
25
|
+
* stays inside the device safe area unless the creator opts out with `'none'`.
|
|
26
|
+
* Each renderer honors its own value, so the main UI and additional renderers
|
|
27
|
+
* can use different insets simultaneously.
|
|
28
|
+
*/
|
|
29
|
+
screenInset?: UiScreenInset;
|
|
30
|
+
/**
|
|
31
|
+
* Stacking order of this renderer's UI relative to the other renderers (the main
|
|
32
|
+
* one and every `addUiRenderer`). Higher values render in front. When omitted,
|
|
33
|
+
* renderers stack in the order they first render, later ones on top; among those
|
|
34
|
+
* first rendered in the same tick the main UI goes at the back.
|
|
35
|
+
*
|
|
36
|
+
* It is applied to the renderer's root container entity, so it only orders whole
|
|
37
|
+
* renderers against each other; a renderer zIndex of 0 is interpreted as "unset"
|
|
38
|
+
* (positional order); elements inside a renderer keep their own `zIndex`.
|
|
39
|
+
*/
|
|
40
|
+
zIndex?: number;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export interface ReactBasedUiSystem {
|
|
46
|
+
/**
|
|
47
|
+
* Destroy all UI entities and unregister related systems.
|
|
48
|
+
*/
|
|
49
|
+
destroy(): void;
|
|
50
|
+
/**
|
|
51
|
+
* Set the main UI renderer. Optional virtual size defines the global UI scale factor.
|
|
52
|
+
*
|
|
53
|
+
* When no virtual size is provided, a platform default is used: 1600x720 on
|
|
54
|
+
* mobile, 1920x1080 otherwise. Providing an invalid size disables the virtual
|
|
55
|
+
* screen (no UI scaling): either a value \<= 0, or only one of the two
|
|
56
|
+
* dimensions, which additionally logs a warning. On mobile, a provided 16:9
|
|
57
|
+
* virtual size is overridden to 1600x720 to fit phone screens.
|
|
58
|
+
*
|
|
59
|
+
* The optional `screenInset` selects the screen area the UI is positioned in
|
|
60
|
+
* (see {@link UiScreenInset}); it defaults to `'device'`. Pass `'none'` to
|
|
61
|
+
* place the UI over the whole screen.
|
|
62
|
+
*/
|
|
63
|
+
setUiRenderer(ui: UiComponent, options?: UiRendererOptions): void;
|
|
64
|
+
/**
|
|
65
|
+
* Add a UI renderer associated with an entity. The UI will be automatically cleaned up
|
|
66
|
+
* when the entity is removed from the engine.
|
|
67
|
+
*
|
|
68
|
+
* If a renderer is already associated with the given entity, it will be replaced.
|
|
69
|
+
*
|
|
70
|
+
* This allows dynamically adding UI Renderers that are rendered alongside the main
|
|
71
|
+
* UI set via setUiRenderer().
|
|
72
|
+
*
|
|
73
|
+
* @param entity - The entity to associate with this UI renderer. When the entity is removed,
|
|
74
|
+
* the UI renderer is automatically cleaned up.
|
|
75
|
+
* @param ui - The UI component to render
|
|
76
|
+
* @param options - Optional virtual size used for UI scale factor when main UI has none.
|
|
77
|
+
* Defaults and the mobile 16:9 override behave as in {@link ReactBasedUiSystem.setUiRenderer}.
|
|
78
|
+
* `screenInset` is honored per renderer, independently of the main UI's value,
|
|
79
|
+
* and defaults to `'device'` here too.
|
|
80
|
+
*/
|
|
81
|
+
addUiRenderer(entity: Entity, ui: UiComponent, options?: UiRendererOptions): void;
|
|
82
|
+
/**
|
|
83
|
+
* Remove a previously added UI renderer by its associated entity.
|
|
84
|
+
* It does not affect the main UI renderer.
|
|
85
|
+
* @param entity - The entity whose UI renderer should be removed
|
|
86
|
+
*/
|
|
87
|
+
removeUiRenderer(entity: Entity): void;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* @public
|
|
91
|
+
*/
|
|
92
|
+
export declare function createReactBasedUiSystem(engine: IEngine, pointerSystem: PointerEventsSystem): ReactBasedUiSystem;
|
package/dist/system.js
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import { EntityState } from '@dcl/ecs';
|
|
2
|
+
import * as ecsComponents from '@dcl/ecs/dist/components';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { createReconciler } from './reconciler';
|
|
5
|
+
import { getUiScaleFactor, resetInteractableArea, resetScreenInsetArea, resetUiScaleFactor, setInteractableArea, setScreenInsetArea, setUiScaleFactor } from './components/utils';
|
|
6
|
+
import { InteractableArea, ScreenInsetArea, UiEntity } from './components';
|
|
7
|
+
import { isMobile } from './platform';
|
|
8
|
+
/**
|
|
9
|
+
* Default virtual screen size used on mobile platforms, and the size 16:9
|
|
10
|
+
* virtual screens are overridden to on mobile (phone screens are much wider
|
|
11
|
+
* than 16:9, so a 16:9 virtual canvas would letterbox the UI).
|
|
12
|
+
*/
|
|
13
|
+
const DEFAULT_MOBILE_VIRTUAL_SIZE = { virtualWidth: 1600, virtualHeight: 720 };
|
|
14
|
+
/**
|
|
15
|
+
* Default virtual screen size used on non-mobile platforms.
|
|
16
|
+
*/
|
|
17
|
+
const DEFAULT_VIRTUAL_SIZE = { virtualWidth: 1920, virtualHeight: 1080 };
|
|
18
|
+
/**
|
|
19
|
+
* Screen area a renderer uses when it doesn't pick one. UI defaults to the device
|
|
20
|
+
* safe area: drawing under a notch, a status bar or a rounded corner is something
|
|
21
|
+
* a creator should opt into, not the out-of-the-box behavior.
|
|
22
|
+
*/
|
|
23
|
+
const DEFAULT_SCREEN_INSET = 'device';
|
|
24
|
+
function hasVirtualSize(options) {
|
|
25
|
+
return !!options && (options.virtualWidth !== undefined || options.virtualHeight !== undefined);
|
|
26
|
+
}
|
|
27
|
+
function isValidVirtualSize(options) {
|
|
28
|
+
return !!options && (options.virtualWidth ?? 0) > 0 && (options.virtualHeight ?? 0) > 0;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Whether a provided size spells out one dimension but not the other. Both are
|
|
32
|
+
* optional in the type, so this is reachable. It is invalid either way — this only
|
|
33
|
+
* decides whether to warn, since a half-given size is a mistake while a value
|
|
34
|
+
* \<= 0 is the documented way to turn the virtual screen off.
|
|
35
|
+
*/
|
|
36
|
+
function isPartialVirtualSize(options) {
|
|
37
|
+
return (options.virtualWidth === undefined) !== (options.virtualHeight === undefined);
|
|
38
|
+
}
|
|
39
|
+
function is16by9(options) {
|
|
40
|
+
return options.virtualWidth * 9 === options.virtualHeight * 16;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export function createReactBasedUiSystem(engine, pointerSystem) {
|
|
46
|
+
const renderer = createReconciler(engine, pointerSystem);
|
|
47
|
+
let uiComponent = undefined;
|
|
48
|
+
let mainOptions = undefined;
|
|
49
|
+
const additionalRenderers = new Map();
|
|
50
|
+
// Renderers are laid out in the order they first render, later ones on top, with
|
|
51
|
+
// the main UI first among those first rendered in the same tick. That is the order
|
|
52
|
+
// their entities were created in before the stacking was explicit, so scenes that
|
|
53
|
+
// set no zIndex keep the stacking they had. A renderer gets its sequence number the
|
|
54
|
+
// first time it renders and keeps it when replaced; the Map iterates in insertion
|
|
55
|
+
// order, so it is already sorted by `order` and only the main UI has to be slotted in.
|
|
56
|
+
let nextOrder = 0;
|
|
57
|
+
let mainOrder = undefined;
|
|
58
|
+
const UiCanvasInformation = ecsComponents.UiCanvasInformation(engine);
|
|
59
|
+
// Unique owner to prevent other UI systems resetting this scale factor.
|
|
60
|
+
const uiScaleFactorOwner = Symbol('react-ecs-ui-scale');
|
|
61
|
+
// Unique owner for the screen inset module variable.
|
|
62
|
+
const screenInsetAreaOwner = Symbol('react-ecs-screen-inset-area');
|
|
63
|
+
// Unique owner for the interactable area module variable.
|
|
64
|
+
const interactableAreaOwner = Symbol('react-ecs-interactable-area');
|
|
65
|
+
// Last 16:9 size we already logged the mobile override for, so the log
|
|
66
|
+
// fires once per provided size instead of every tick. Tracked as raw numbers
|
|
67
|
+
// to avoid allocating a comparison string every tick.
|
|
68
|
+
let loggedMobileOverrideW = 0;
|
|
69
|
+
let loggedMobileOverrideH = 0;
|
|
70
|
+
// Same once-per-size guard for the incomplete-size warning. A partial size maps
|
|
71
|
+
// its missing dimension to 0, and the provided one can itself be 0, so -1 is the
|
|
72
|
+
// "nothing logged yet" sentinel — 0/0 is a reachable real value here.
|
|
73
|
+
let loggedPartialW = -1;
|
|
74
|
+
let loggedPartialH = -1;
|
|
75
|
+
function getActiveVirtualSize() {
|
|
76
|
+
// Main renderer options win; otherwise use the first additional renderer option.
|
|
77
|
+
// Options carrying no virtual dims (e.g. only a screen inset) are skipped so
|
|
78
|
+
// they don't count as a provided-but-invalid virtual size.
|
|
79
|
+
if (hasVirtualSize(mainOptions))
|
|
80
|
+
return mainOptions;
|
|
81
|
+
for (const entry of additionalRenderers.values()) {
|
|
82
|
+
if (hasVirtualSize(entry.options))
|
|
83
|
+
return entry.options;
|
|
84
|
+
}
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Resolves the virtual screen to scale the UI against, or `undefined` when
|
|
89
|
+
* the virtual screen is disabled.
|
|
90
|
+
*/
|
|
91
|
+
function resolveVirtualSize() {
|
|
92
|
+
const provided = getActiveVirtualSize();
|
|
93
|
+
const mobile = isMobile();
|
|
94
|
+
// No creator-provided size: fall back to the platform default.
|
|
95
|
+
if (!provided) {
|
|
96
|
+
return mobile ? DEFAULT_MOBILE_VIRTUAL_SIZE : DEFAULT_VIRTUAL_SIZE;
|
|
97
|
+
}
|
|
98
|
+
// An explicitly provided but invalid size disables the virtual screen — no UI
|
|
99
|
+
// scaling at all. That covers a value <= 0 (the deliberate opt-out) and a size
|
|
100
|
+
// that gives only one of its two dimensions.
|
|
101
|
+
if (!isValidVirtualSize(provided)) {
|
|
102
|
+
// A half-given size is a mistake rather than an opt-out, and disabling scaling
|
|
103
|
+
// is not what the creator was reaching for, so say so once per provided size.
|
|
104
|
+
// The <= 0 opt-out is documented and stays silent.
|
|
105
|
+
if (isPartialVirtualSize(provided)) {
|
|
106
|
+
const width = provided.virtualWidth ?? 0;
|
|
107
|
+
const height = provided.virtualHeight ?? 0;
|
|
108
|
+
if (loggedPartialW !== width || loggedPartialH !== height) {
|
|
109
|
+
loggedPartialW = width;
|
|
110
|
+
loggedPartialH = height;
|
|
111
|
+
console.log(`Incomplete virtual screen size (virtualWidth: ${provided.virtualWidth}, virtualHeight: ${provided.virtualHeight}): both dimensions are required, so the virtual screen is disabled and no UI scaling is applied.`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
// On mobile, 16:9 virtual screens don't fit phone aspect ratios — override them.
|
|
117
|
+
if (mobile && is16by9(provided)) {
|
|
118
|
+
if (loggedMobileOverrideW !== provided.virtualWidth || loggedMobileOverrideH !== provided.virtualHeight) {
|
|
119
|
+
loggedMobileOverrideW = provided.virtualWidth;
|
|
120
|
+
loggedMobileOverrideH = provided.virtualHeight;
|
|
121
|
+
console.log(`Mobile platform detected: overriding 16:9 virtual screen size ${provided.virtualWidth}x${provided.virtualHeight} with ${DEFAULT_MOBILE_VIRTUAL_SIZE.virtualWidth}x${DEFAULT_MOBILE_VIRTUAL_SIZE.virtualHeight}`);
|
|
122
|
+
}
|
|
123
|
+
return DEFAULT_MOBILE_VIRTUAL_SIZE;
|
|
124
|
+
}
|
|
125
|
+
return provided;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Wraps a renderer's component in a container positioned within the selected
|
|
129
|
+
* screen inset area. `'none'` adds no wrapper, leaving the UI on the whole
|
|
130
|
+
* screen, unless a `zIndex` needs a root entity to live on. Applied per
|
|
131
|
+
* renderer, so each renderer can use a different inset.
|
|
132
|
+
*/
|
|
133
|
+
function wrapWithScreenInset(ui, options, key) {
|
|
134
|
+
const zIndex = options?.zIndex;
|
|
135
|
+
// Only forward a provided zIndex: `{ zIndex: undefined }` would override the
|
|
136
|
+
// transform parser default and change what is written for every renderer.
|
|
137
|
+
const wrapperProps = zIndex === undefined ? { key } : { key, uiTransform: { zIndex } };
|
|
138
|
+
// An omitted inset is resolved to DEFAULT_SCREEN_INSET ('device') before the
|
|
139
|
+
// switch, so it lands on `case 'device'`. Only an explicit 'none' reaches the
|
|
140
|
+
// `default` clause below — the two are unrelated despite sharing a name.
|
|
141
|
+
switch (options?.screenInset ?? DEFAULT_SCREEN_INSET) {
|
|
142
|
+
case 'device':
|
|
143
|
+
return React.createElement(ScreenInsetArea, wrapperProps, React.createElement(ui));
|
|
144
|
+
case 'interactable':
|
|
145
|
+
return React.createElement(InteractableArea, wrapperProps, React.createElement(ui));
|
|
146
|
+
// 'none' — the whole screen, no wrapper entity
|
|
147
|
+
default:
|
|
148
|
+
if (zIndex === undefined)
|
|
149
|
+
return React.createElement(ui, { key });
|
|
150
|
+
// A whole-screen container: absolutely positioned on every edge so it
|
|
151
|
+
// fills the canvas, with the zIndex the renderer is stacked by.
|
|
152
|
+
return React.createElement(UiEntity, {
|
|
153
|
+
key,
|
|
154
|
+
uiTransform: { positionType: 'absolute', position: { top: 0, left: 0, right: 0, bottom: 0 }, zIndex }
|
|
155
|
+
}, React.createElement(ui));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function ReactBasedUiSystem() {
|
|
159
|
+
const components = [];
|
|
160
|
+
if (uiComponent !== undefined && mainOrder === undefined)
|
|
161
|
+
mainOrder = nextOrder++;
|
|
162
|
+
let mainPushed = uiComponent === undefined;
|
|
163
|
+
const entitiesToRemove = [];
|
|
164
|
+
for (const [entity, entry] of additionalRenderers) {
|
|
165
|
+
// Check for entity-based cleanup
|
|
166
|
+
if (engine.getEntityState(entity) === EntityState.Removed) {
|
|
167
|
+
entitiesToRemove.push(entity);
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (entry.order === undefined)
|
|
171
|
+
entry.order = nextOrder++;
|
|
172
|
+
// The main UI goes right before the first renderer that rendered after it.
|
|
173
|
+
if (!mainPushed && mainOrder < entry.order) {
|
|
174
|
+
components.push(wrapWithScreenInset(uiComponent, mainOptions, '__main__'));
|
|
175
|
+
mainPushed = true;
|
|
176
|
+
}
|
|
177
|
+
components.push(wrapWithScreenInset(entry.ui, entry.options, `__entity_${entity}__`));
|
|
178
|
+
}
|
|
179
|
+
// Main UI first rendered after every other renderer (or alone): it goes on top.
|
|
180
|
+
if (!mainPushed) {
|
|
181
|
+
components.push(wrapWithScreenInset(uiComponent, mainOptions, '__main__'));
|
|
182
|
+
}
|
|
183
|
+
// Entity-based cleanup
|
|
184
|
+
for (const entity of entitiesToRemove) {
|
|
185
|
+
additionalRenderers.delete(entity);
|
|
186
|
+
}
|
|
187
|
+
// Always update the renderer - pass null when empty to clear the UI
|
|
188
|
+
if (components.length > 0) {
|
|
189
|
+
renderer.update(React.createElement(React.Fragment, null, ...components));
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
renderer.update(null);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
function UiScaleSystem() {
|
|
196
|
+
const canvasInfo = UiCanvasInformation.getOrNull(engine.RootEntity);
|
|
197
|
+
// Update the screen inset module variable unconditionally — it is
|
|
198
|
+
// independent of the virtual size and useful even when the renderer has no
|
|
199
|
+
// virtual canvas.
|
|
200
|
+
if (canvasInfo?.screenInsetArea) {
|
|
201
|
+
setScreenInsetArea(canvasInfo.screenInsetArea, screenInsetAreaOwner);
|
|
202
|
+
}
|
|
203
|
+
// Update the interactable area module variable unconditionally.
|
|
204
|
+
if (canvasInfo?.interactableArea) {
|
|
205
|
+
setInteractableArea(canvasInfo.interactableArea, interactableAreaOwner);
|
|
206
|
+
}
|
|
207
|
+
// The virtual screen (provided or defaulted) only applies while some
|
|
208
|
+
// renderer is registered; with no UI at all the scale factor is released.
|
|
209
|
+
if (uiComponent === undefined && additionalRenderers.size === 0) {
|
|
210
|
+
// Reset only if this system owns the scale factor.
|
|
211
|
+
resetUiScaleFactor(uiScaleFactorOwner);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
const activeVirtualSize = resolveVirtualSize();
|
|
215
|
+
if (!activeVirtualSize) {
|
|
216
|
+
// Virtual screen explicitly disabled by an invalid provided size.
|
|
217
|
+
resetUiScaleFactor(uiScaleFactorOwner);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (!canvasInfo)
|
|
221
|
+
return;
|
|
222
|
+
const { width, height } = canvasInfo;
|
|
223
|
+
const { virtualWidth, virtualHeight } = activeVirtualSize;
|
|
224
|
+
// The scale factor is the contain-fit of the design resolution inside the canvas,
|
|
225
|
+
// and nothing else.
|
|
226
|
+
//
|
|
227
|
+
// devicePixelRatio is deliberately absent. It is a density hint — "how many physical
|
|
228
|
+
// pixels per canvas unit", for picking a 1x/2x/3x asset — not a layout unit, the same
|
|
229
|
+
// role it has in CSS and React Native, where it is exposed but never enters layout.
|
|
230
|
+
// Dividing by it made UI size inversely proportional to a quantity the scene author
|
|
231
|
+
// does not control and that measures something different on every renderer: panel
|
|
232
|
+
// density on mobile, OS display scaling on web, display/window on native desktop.
|
|
233
|
+
const nextScale = Math.min(width / virtualWidth, height / virtualHeight);
|
|
234
|
+
if (Number.isFinite(nextScale) && nextScale !== getUiScaleFactor()) {
|
|
235
|
+
// Track ownership when updating to avoid cross-system conflicts.
|
|
236
|
+
setUiScaleFactor(nextScale, uiScaleFactorOwner);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
engine.addSystem(UiScaleSystem, 100e3 + 1, '@dcl/react-ecs-ui-scale');
|
|
240
|
+
engine.addSystem(ReactBasedUiSystem, 100e3, '@dcl/react-ecs');
|
|
241
|
+
return {
|
|
242
|
+
destroy() {
|
|
243
|
+
engine.removeSystem(UiScaleSystem);
|
|
244
|
+
engine.removeSystem(ReactBasedUiSystem);
|
|
245
|
+
resetUiScaleFactor(uiScaleFactorOwner);
|
|
246
|
+
resetScreenInsetArea(screenInsetAreaOwner);
|
|
247
|
+
resetInteractableArea(interactableAreaOwner);
|
|
248
|
+
for (const entity of renderer.getEntities()) {
|
|
249
|
+
engine.removeEntity(entity);
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
setUiRenderer(ui, options) {
|
|
253
|
+
uiComponent = ui;
|
|
254
|
+
mainOptions = options;
|
|
255
|
+
},
|
|
256
|
+
addUiRenderer(entity, ui, options) {
|
|
257
|
+
// A replacement keeps its place in the stacking order.
|
|
258
|
+
additionalRenderers.set(entity, { ui, options, order: additionalRenderers.get(entity)?.order });
|
|
259
|
+
},
|
|
260
|
+
removeUiRenderer(entity) {
|
|
261
|
+
additionalRenderers.delete(entity);
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dcl-regenesislabs/react-ecs",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"description": "Decentraland ECS",
|
|
4
|
+
"version": "7.0.0-42707159203.commit-ff138cd",
|
|
5
|
+
"author": "DCL",
|
|
6
|
+
"bugs": "https://github.com/decentraland/dcl-one/issues",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"@dcl/ecs": "npm:@dcl-regenesislabs/ecs@7.0.0-42707159203.commit-ff138cd",
|
|
9
|
+
"react": "^18.2.0",
|
|
10
|
+
"react-reconciler": "^0.29.0"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@types/react": "^18.0.27",
|
|
14
|
+
"@types/react-reconciler": "^0.28.0"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"homepage": "https://github.com/decentraland/dcl-one/tree/main/library/js-sdk-toolchain#readme",
|
|
20
|
+
"keywords": [
|
|
21
|
+
"@dcl/react-ecs",
|
|
22
|
+
"dcl",
|
|
23
|
+
"ecs",
|
|
24
|
+
"react"
|
|
25
|
+
],
|
|
26
|
+
"license": "Apache-2.0",
|
|
27
|
+
"main": "./dist/index.js",
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/decentraland/dcl-one.git",
|
|
31
|
+
"directory": "library/js-sdk-toolchain/packages/@dcl/react-ecs"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsc -p tsconfig.json",
|
|
35
|
+
"start": "tsc -p tsconfig.json --watch"
|
|
36
|
+
},
|
|
37
|
+
"typedoc": {
|
|
38
|
+
"entryPoint": "./src/index.ts",
|
|
39
|
+
"readmeFile": "./README.md",
|
|
40
|
+
"displayName": "React ECS",
|
|
41
|
+
"tsconfig": "./tsconfig.json"
|
|
42
|
+
},
|
|
43
|
+
"types": "./dist/index.d.ts"
|
|
6
44
|
}
|