@umituz/web-virtual 2.1.0 → 2.2.0
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 +53 -0
- package/package.json +1 -1
- package/src/components/WorldPage.tsx +283 -0
- package/src/components/index.ts +1 -0
- package/src/core/index.ts +1 -0
- package/src/core/time.test.ts +22 -0
- package/src/core/time.ts +15 -0
- package/src/engine/index.ts +1 -0
- package/src/engine/tickBinder.test.ts +56 -0
- package/src/engine/tickBinder.ts +50 -0
- package/src/entities/architecture/FarmArchitectureBuilder.ts +3 -1
- package/src/entities/nature/CropFieldBuilder.ts +0 -1
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useStableSnapshot.ts +17 -0
- package/src/index.ts +1 -0
- package/src/styles/virtual.css +315 -0
package/README.md
CHANGED
|
@@ -25,6 +25,7 @@ Entities without an explicit `position` are auto-placed onto preset slots — by
|
|
|
25
25
|
|
|
26
26
|
- **Universal Engine**: Agnostic to domain (Corporate Offices, Agriculture, Automation Factories, Campus, Healthcare).
|
|
27
27
|
- **Engine Tick Animations (2.1.0)**: Register per-frame scene motion (`registerTick`) on the engine's own render loop — wind sway, water ripples, spinning wheels, grazing herds. No separate `requestAnimationFrame` needed; pauses with the tab, cleared on dispose.
|
|
28
|
+
- **Standalone World Page Shell (2.2.0)**: `VirtualWorldPage` — the full-screen chrome every data-driven world needs (header with back/title/meta + label, night, top-view & reset actions; selected-entity card; hint pill; sectioned sidebar rows that focus entities; optional footer). All strings arrive via the `labels` prop (i18n stays yours), all theming via `--wv-page-*` CSS custom properties. Ships with `createTickBinder()` (bind/queue bridge for the engine tick), `useStableSnapshot()` (freeze the first world build per visit so refetches never steal the camera) and `isNightHour()` (local-hour night default).
|
|
28
29
|
- **Nature / Farm / Animal Builders (2.1.0)**: Procedural, config-driven `createCropPlot` (growth 0–1), `createTree`/`createTreeRow` (+`applyWindSway`), `createFence`, `createPond`, `createAnimal`/`createHerd` (cow/sheep/goat/chicken), `createTractor`, plus farm architecture (barn, silo, greenhouse, machine shed, coop).
|
|
29
30
|
- **Hot-Synced Entities (1.2.0)**: When the `world` prop changes (e.g. a status poll), entities are diffed and updated **in place** — the scene, camera position, lights and cached GLB models are never rebuilt. No flicker, no reloads, no lost camera angle.
|
|
30
31
|
- **WebGL Resilience (1.2.0)**: Graceful fallback UI when WebGL is unavailable, and a one-click restore overlay if the GPU context is lost mid-session.
|
|
@@ -116,6 +117,58 @@ const onReady = (ref: VirtualWorldRef) =>
|
|
|
116
117
|
})
|
|
117
118
|
```
|
|
118
119
|
|
|
120
|
+
### Standalone world page (2.2.0)
|
|
121
|
+
|
|
122
|
+
`VirtualWorldPage` wraps a `VirtualWorld` with the whole page — hand it data,
|
|
123
|
+
labels and two render slots; it owns pinMode/night/selection state, the focus
|
|
124
|
+
wiring and the responsive layout:
|
|
125
|
+
|
|
126
|
+
```tsx
|
|
127
|
+
import { VirtualWorldPage, createEnvironment } from '@umituz/web-virtual'
|
|
128
|
+
import '@umituz/web-virtual/styles.css'
|
|
129
|
+
|
|
130
|
+
<VirtualWorldPage
|
|
131
|
+
world={world}
|
|
132
|
+
preset={myPreset}
|
|
133
|
+
title="Club Aria"
|
|
134
|
+
meta={`${liveCount} live · ${personaCount} co-hosts`}
|
|
135
|
+
back={<Link to="/discover">Discover</Link>}
|
|
136
|
+
initialNight="auto" // local-hour day/night; 'nightToggle' adds the button
|
|
137
|
+
selectedAlign="start" // or 'center'
|
|
138
|
+
hint="Click a stage to watch · drag to orbit · scroll to zoom"
|
|
139
|
+
sections={[
|
|
140
|
+
{ key: 'live', heading: 'Now live', rows: liveRows },
|
|
141
|
+
{ key: 'vips', heading: 'VIP lofts', rows: vipRows }, // rows: {id, label, meta?, dotColor?, dashed?}
|
|
142
|
+
]}
|
|
143
|
+
labels={{ topView: 'Üst görünüm', resetView: 'Sıfırla' }} // your i18n
|
|
144
|
+
renderOpenLink={(entity) => <Link to={`/x/${entity.id}`}>Open</Link>}
|
|
145
|
+
footer="Click a stage to watch its stream"
|
|
146
|
+
/>
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Theme it from consumer CSS by overriding the custom properties (dark clubs,
|
|
150
|
+
paper-white farms — same shell):
|
|
151
|
+
|
|
152
|
+
```css
|
|
153
|
+
.club {
|
|
154
|
+
--wv-page-bg: #0b0b0f;
|
|
155
|
+
--wv-page-surface: #111827;
|
|
156
|
+
--wv-page-fg: #e8edf8;
|
|
157
|
+
--wv-page-muted: #8b93a7;
|
|
158
|
+
--wv-page-line: #27334a;
|
|
159
|
+
--wv-page-accent: #7c3aed;
|
|
160
|
+
--wv-page-accent-soft: rgba(124, 58, 237, 0.14);
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Companion utilities:
|
|
165
|
+
|
|
166
|
+
| Export | What it does |
|
|
167
|
+
| --- | --- |
|
|
168
|
+
| `createTickBinder()` | Module-scoped bind/queue bridge for `registerTick` — animations registered before the engine mounts flush on the next bind |
|
|
169
|
+
| `useStableSnapshot(fresh)` | Freezes the first non-null value per visit (world builds) so background refetches never rebuild the scene |
|
|
170
|
+
| `isNightHour(from?, to?)` | Local-hour night check (wraps midnight) for `initialNight="auto"` |
|
|
171
|
+
|
|
119
172
|
### Procedural nature / farm builders (2.1.0)
|
|
120
173
|
|
|
121
174
|
All builders are pure config — no domain data, no fetching, nothing
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-virtual",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "Universal 3D Virtual Environment Engine for Web & React — Offices, Farms, Factories, Schools, Hospitals with 60fps performance, raycasting and interactive entities.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* VirtualWorldPage — the generic standalone world page: full-screen chrome
|
|
3
|
+
* around a VirtualWorld so data-driven worlds (a club, a farm, any venue)
|
|
4
|
+
* need zero local scaffolding. Owns the shared UI contract — header with
|
|
5
|
+
* back/title/meta + label, night, top-view and reset actions; canvas with
|
|
6
|
+
* selected-entity card and hint pill; sectioned sidebar rows that focus
|
|
7
|
+
* entities; optional footer bar.
|
|
8
|
+
*
|
|
9
|
+
* Everything visual is config: strings arrive via `labels`, slots via
|
|
10
|
+
* ReactNode props, theming via `--wv-page-*` CSS custom properties.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
type ReactNode,
|
|
15
|
+
useCallback,
|
|
16
|
+
useRef,
|
|
17
|
+
useState,
|
|
18
|
+
} from 'react'
|
|
19
|
+
import { LayoutGrid, Moon, RotateCcw, Sun, Tags } from 'lucide-react'
|
|
20
|
+
import type { PinDisplayMode, WorldDefinition, WorldEntity, WorldPreset } from '../core/types'
|
|
21
|
+
import { isNightHour } from '../core/time'
|
|
22
|
+
import { VirtualWorld, type VirtualWorldProps, type VirtualWorldRef } from './VirtualWorld'
|
|
23
|
+
|
|
24
|
+
export interface WorldPageRow {
|
|
25
|
+
/** Entity id focused when the row is clicked. */
|
|
26
|
+
id: string
|
|
27
|
+
label: ReactNode
|
|
28
|
+
meta?: ReactNode
|
|
29
|
+
/** Row dot color; omit for a dotless row. */
|
|
30
|
+
dotColor?: string
|
|
31
|
+
/** Dashed border variant (e.g. VIP rows). */
|
|
32
|
+
dashed?: boolean
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface WorldPageSection {
|
|
36
|
+
key: string
|
|
37
|
+
heading: ReactNode
|
|
38
|
+
rows: WorldPageRow[]
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface WorldPageLabels {
|
|
42
|
+
/** Title for the pin-labels toggle button. */
|
|
43
|
+
toggleLabels: string
|
|
44
|
+
/** Title for the day/night toggle button. */
|
|
45
|
+
toggleNight: string
|
|
46
|
+
/** Aria-label for the top-view camera button. */
|
|
47
|
+
topView: string
|
|
48
|
+
/** Aria-label for the reset-camera button. */
|
|
49
|
+
resetView: string
|
|
50
|
+
/** Selected-card fallback when the entity opens nowhere. */
|
|
51
|
+
focusOnly?: string
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type VirtualWorldPageProps = {
|
|
55
|
+
world: WorldDefinition
|
|
56
|
+
preset?: WorldPreset
|
|
57
|
+
title: ReactNode
|
|
58
|
+
/** Header meta line (counts, live status, …). */
|
|
59
|
+
meta?: ReactNode
|
|
60
|
+
/** Back navigation — a router link from the consumer (rendered in a pill). */
|
|
61
|
+
back?: ReactNode
|
|
62
|
+
sections?: WorldPageSection[]
|
|
63
|
+
/** Extra content at the bottom of the sidebar (overflow notes, legends). */
|
|
64
|
+
sidebarFooter?: ReactNode
|
|
65
|
+
/** Full-width bar under the world canvas. */
|
|
66
|
+
footer?: ReactNode
|
|
67
|
+
/** Pill at the top of the canvas (controls hint). */
|
|
68
|
+
hint?: ReactNode
|
|
69
|
+
/** Selected-card alignment: centered (default) or anchored to the start. */
|
|
70
|
+
selectedAlign?: 'center' | 'start'
|
|
71
|
+
/** Initial pin display mode. */
|
|
72
|
+
initialPinMode?: PinDisplayMode
|
|
73
|
+
/** Initial night state; `'auto'` follows the viewer's local hour (19–07). */
|
|
74
|
+
initialNight?: boolean | 'auto'
|
|
75
|
+
/** Show the day/night toggle button. */
|
|
76
|
+
nightToggle?: boolean
|
|
77
|
+
labels?: Partial<WorldPageLabels>
|
|
78
|
+
/** Selected-card subtitle; defaults to `entity.metadata.subtitle` when a string. */
|
|
79
|
+
renderSubtitle?: (entity: WorldEntity) => ReactNode
|
|
80
|
+
/** Selected-card action (a router link/button); absent → focusOnly label. */
|
|
81
|
+
renderOpenLink?: (entity: WorldEntity) => ReactNode
|
|
82
|
+
onSelectEntity?: (entity: WorldEntity | null) => void
|
|
83
|
+
/** Escape hatch for extra VirtualWorld props (showInspector, onHoverEntity, …). */
|
|
84
|
+
worldProps?: Omit<
|
|
85
|
+
VirtualWorldProps,
|
|
86
|
+
'world' | 'preset' | 'pinMode' | 'nightMode' | 'selectedEntityId' | 'onSelectEntity' | 'className'
|
|
87
|
+
>
|
|
88
|
+
className?: string
|
|
89
|
+
canvasClassName?: string
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const DEFAULT_LABELS: WorldPageLabels = {
|
|
93
|
+
toggleLabels: 'Toggle labels',
|
|
94
|
+
toggleNight: 'Toggle day/night',
|
|
95
|
+
topView: 'Top view',
|
|
96
|
+
resetView: 'Reset view',
|
|
97
|
+
focusOnly: 'focus only',
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function metadataSubtitle(entity: WorldEntity): ReactNode {
|
|
101
|
+
const value = entity.metadata?.subtitle
|
|
102
|
+
return typeof value === 'string' ? value : null
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function VirtualWorldPage({
|
|
106
|
+
world,
|
|
107
|
+
preset,
|
|
108
|
+
title,
|
|
109
|
+
meta,
|
|
110
|
+
back,
|
|
111
|
+
sections = [],
|
|
112
|
+
sidebarFooter,
|
|
113
|
+
footer,
|
|
114
|
+
hint,
|
|
115
|
+
selectedAlign = 'center',
|
|
116
|
+
initialPinMode = 'labels',
|
|
117
|
+
initialNight = false,
|
|
118
|
+
nightToggle = false,
|
|
119
|
+
labels,
|
|
120
|
+
renderSubtitle,
|
|
121
|
+
renderOpenLink,
|
|
122
|
+
onSelectEntity,
|
|
123
|
+
worldProps,
|
|
124
|
+
className = '',
|
|
125
|
+
canvasClassName = '',
|
|
126
|
+
}: VirtualWorldPageProps): ReactNode {
|
|
127
|
+
const worldRef = useRef<VirtualWorldRef>(null)
|
|
128
|
+
const [pinMode, setPinMode] = useState<PinDisplayMode>(initialPinMode)
|
|
129
|
+
const [nightMode, setNightMode] = useState<boolean>(
|
|
130
|
+
initialNight === 'auto' ? isNightHour() : initialNight,
|
|
131
|
+
)
|
|
132
|
+
const [selectedId, setSelectedId] = useState<string | null>(null)
|
|
133
|
+
|
|
134
|
+
const selected: WorldEntity | null = world.entities.find((e) => e.id === selectedId) ?? null
|
|
135
|
+
|
|
136
|
+
const handleCanvasSelect = useCallback(
|
|
137
|
+
(entity: WorldEntity | null): void => {
|
|
138
|
+
setSelectedId(entity?.id ?? null)
|
|
139
|
+
onSelectEntity?.(entity)
|
|
140
|
+
},
|
|
141
|
+
[onSelectEntity],
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
const focusRow = useCallback(
|
|
145
|
+
(id: string): void => {
|
|
146
|
+
setSelectedId(id)
|
|
147
|
+
worldRef.current?.focusOnEntity(id)
|
|
148
|
+
},
|
|
149
|
+
[],
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
const resolved: WorldPageLabels = { ...DEFAULT_LABELS, ...labels }
|
|
153
|
+
const openLink = selected ? renderOpenLink?.(selected) : null
|
|
154
|
+
const subtitle = selected
|
|
155
|
+
? (renderSubtitle?.(selected) ?? metadataSubtitle(selected))
|
|
156
|
+
: null
|
|
157
|
+
|
|
158
|
+
return (
|
|
159
|
+
<div className={`wv-page${className ? ` ${className}` : ''}`} data-wv-page>
|
|
160
|
+
<header className="wv-page__header">
|
|
161
|
+
{back && <div className="wv-page__back">{back}</div>}
|
|
162
|
+
<div className="wv-page__heading">
|
|
163
|
+
<span className="wv-page__title">{title}</span>
|
|
164
|
+
{meta && <span className="wv-page__meta">{meta}</span>}
|
|
165
|
+
</div>
|
|
166
|
+
<div className="wv-page__actions">
|
|
167
|
+
<button
|
|
168
|
+
type="button"
|
|
169
|
+
className="wv-page__btn"
|
|
170
|
+
data-wv-labels
|
|
171
|
+
title={resolved.toggleLabels}
|
|
172
|
+
aria-label={resolved.toggleLabels}
|
|
173
|
+
onClick={() => setPinMode(pinMode === 'labels' ? 'off' : 'labels')}
|
|
174
|
+
>
|
|
175
|
+
<Tags size={15} />
|
|
176
|
+
</button>
|
|
177
|
+
{nightToggle && (
|
|
178
|
+
<button
|
|
179
|
+
type="button"
|
|
180
|
+
className="wv-page__btn"
|
|
181
|
+
data-wv-night
|
|
182
|
+
title={resolved.toggleNight}
|
|
183
|
+
aria-label={resolved.toggleNight}
|
|
184
|
+
onClick={() => setNightMode(!nightMode)}
|
|
185
|
+
>
|
|
186
|
+
{nightMode ? <Sun size={15} /> : <Moon size={15} />}
|
|
187
|
+
</button>
|
|
188
|
+
)}
|
|
189
|
+
<button
|
|
190
|
+
type="button"
|
|
191
|
+
className="wv-page__btn"
|
|
192
|
+
data-wv-view-top
|
|
193
|
+
title={resolved.topView}
|
|
194
|
+
aria-label={resolved.topView}
|
|
195
|
+
onClick={() => worldRef.current?.setCameraPreset('top')}
|
|
196
|
+
>
|
|
197
|
+
<LayoutGrid size={15} />
|
|
198
|
+
</button>
|
|
199
|
+
<button
|
|
200
|
+
type="button"
|
|
201
|
+
className="wv-page__btn"
|
|
202
|
+
data-wv-reset
|
|
203
|
+
title={resolved.resetView}
|
|
204
|
+
aria-label={resolved.resetView}
|
|
205
|
+
onClick={() => worldRef.current?.resetCamera()}
|
|
206
|
+
>
|
|
207
|
+
<RotateCcw size={15} />
|
|
208
|
+
</button>
|
|
209
|
+
</div>
|
|
210
|
+
</header>
|
|
211
|
+
|
|
212
|
+
<div className="wv-page__main">
|
|
213
|
+
<div className="wv-page__world" data-wv-canvas>
|
|
214
|
+
<VirtualWorld
|
|
215
|
+
ref={worldRef}
|
|
216
|
+
world={world}
|
|
217
|
+
preset={preset}
|
|
218
|
+
pinMode={pinMode}
|
|
219
|
+
nightMode={nightMode}
|
|
220
|
+
selectedEntityId={selectedId}
|
|
221
|
+
onSelectEntity={handleCanvasSelect}
|
|
222
|
+
showControls={false}
|
|
223
|
+
showInspector={false}
|
|
224
|
+
{...worldProps}
|
|
225
|
+
className={`wv-page__canvas${canvasClassName ? ` ${canvasClassName}` : ''}`}
|
|
226
|
+
/>
|
|
227
|
+
|
|
228
|
+
{selected && (
|
|
229
|
+
<div
|
|
230
|
+
className={`wv-page__selected${selectedAlign === 'start' ? ' wv-page__selected--start' : ''}`}
|
|
231
|
+
data-wv-selected
|
|
232
|
+
>
|
|
233
|
+
<span className="wv-page__selected-dot" style={{ backgroundColor: selected.avatarColor }} />
|
|
234
|
+
<span className="wv-page__selected-body">
|
|
235
|
+
<span className="wv-page__selected-name">{selected.name}</span>
|
|
236
|
+
{subtitle && <span className="wv-page__selected-sub">{subtitle}</span>}
|
|
237
|
+
</span>
|
|
238
|
+
{openLink ? (
|
|
239
|
+
<span className="wv-page__selected-action">{openLink}</span>
|
|
240
|
+
) : resolved.focusOnly ? (
|
|
241
|
+
<span className="wv-page__selected-hint">{resolved.focusOnly}</span>
|
|
242
|
+
) : null}
|
|
243
|
+
</div>
|
|
244
|
+
)}
|
|
245
|
+
|
|
246
|
+
{hint && <div className="wv-page__hint">{hint}</div>}
|
|
247
|
+
</div>
|
|
248
|
+
|
|
249
|
+
{(sections.length > 0 || sidebarFooter) && (
|
|
250
|
+
<aside className="wv-page__sidebar" data-wv-sidebar>
|
|
251
|
+
{sections.map((section) => (
|
|
252
|
+
<section className="wv-page__section" key={section.key}>
|
|
253
|
+
<h2 className="wv-page__section-heading">{section.heading}</h2>
|
|
254
|
+
<ul className="wv-page__list">
|
|
255
|
+
{section.rows.map((row) => (
|
|
256
|
+
<li key={row.id}>
|
|
257
|
+
<button
|
|
258
|
+
type="button"
|
|
259
|
+
className={`wv-page-row${selectedId === row.id ? ' wv-page-row--active' : ''}${
|
|
260
|
+
row.dashed ? ' wv-page-row--dashed' : ''
|
|
261
|
+
}`}
|
|
262
|
+
onClick={() => focusRow(row.id)}
|
|
263
|
+
>
|
|
264
|
+
{row.dotColor && (
|
|
265
|
+
<span className="wv-page-row__dot" style={{ backgroundColor: row.dotColor }} />
|
|
266
|
+
)}
|
|
267
|
+
<span className="wv-page-row__label">{row.label}</span>
|
|
268
|
+
{row.meta && <span className="wv-page-row__meta">{row.meta}</span>}
|
|
269
|
+
</button>
|
|
270
|
+
</li>
|
|
271
|
+
))}
|
|
272
|
+
</ul>
|
|
273
|
+
</section>
|
|
274
|
+
))}
|
|
275
|
+
{sidebarFooter}
|
|
276
|
+
</aside>
|
|
277
|
+
)}
|
|
278
|
+
</div>
|
|
279
|
+
|
|
280
|
+
{footer && <footer className="wv-page__footer">{footer}</footer>}
|
|
281
|
+
</div>
|
|
282
|
+
)
|
|
283
|
+
}
|
package/src/components/index.ts
CHANGED
package/src/core/index.ts
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { isNightHour } from './time'
|
|
3
|
+
|
|
4
|
+
const at = (hour: number): Date => new Date(2026, 10, 16, hour, 30)
|
|
5
|
+
|
|
6
|
+
describe('isNightHour', () => {
|
|
7
|
+
it('covers the default 19→07 window across midnight', () => {
|
|
8
|
+
expect(isNightHour(19, 7, at(19))).toBe(true)
|
|
9
|
+
expect(isNightHour(19, 7, at(23))).toBe(true)
|
|
10
|
+
expect(isNightHour(19, 7, at(3))).toBe(true)
|
|
11
|
+
expect(isNightHour(19, 7, at(6))).toBe(true)
|
|
12
|
+
expect(isNightHour(19, 7, at(7))).toBe(false)
|
|
13
|
+
expect(isNightHour(19, 7, at(12))).toBe(false)
|
|
14
|
+
expect(isNightHour(19, 7, at(18))).toBe(false)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('handles same-day windows without wrapping', () => {
|
|
18
|
+
expect(isNightHour(9, 17, at(12))).toBe(true)
|
|
19
|
+
expect(isNightHour(9, 17, at(8))).toBe(false)
|
|
20
|
+
expect(isNightHour(9, 17, at(17))).toBe(false)
|
|
21
|
+
})
|
|
22
|
+
})
|
package/src/core/time.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Clock helpers for day/night theming. Pure, timezone-local by design: the
|
|
3
|
+
* viewer's own hour decides, so a world page defaults to night without any
|
|
4
|
+
* consumer-side plumbing.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** True when the given clock falls inside the night window `[from, to)`
|
|
8
|
+
* (wraps past midnight, e.g. 19 → 7). */
|
|
9
|
+
export function isNightHour(fromHour = 19, toHour = 7, at: Date = new Date()): boolean {
|
|
10
|
+
const h = at.getHours()
|
|
11
|
+
if (fromHour <= toHour) {
|
|
12
|
+
return h >= fromHour && h < toHour
|
|
13
|
+
}
|
|
14
|
+
return h >= fromHour || h < toHour
|
|
15
|
+
}
|
package/src/engine/index.ts
CHANGED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import { createTickBinder } from './tickBinder'
|
|
3
|
+
|
|
4
|
+
function fakeEngine() {
|
|
5
|
+
const registered = new Set<(delta: number, elapsed: number) => void>()
|
|
6
|
+
const step = (delta: number, elapsed: number): void => {
|
|
7
|
+
for (const fn of registered) fn(delta, elapsed)
|
|
8
|
+
}
|
|
9
|
+
return {
|
|
10
|
+
register: (fn: (delta: number, elapsed: number) => void) => {
|
|
11
|
+
registered.add(fn)
|
|
12
|
+
return () => registered.delete(fn)
|
|
13
|
+
},
|
|
14
|
+
step,
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe('createTickBinder', () => {
|
|
19
|
+
it('queues registrations made before bind and flushes them on bind', () => {
|
|
20
|
+
const binder = createTickBinder()
|
|
21
|
+
const early = vi.fn()
|
|
22
|
+
binder.register(early)
|
|
23
|
+
|
|
24
|
+
const engine = fakeEngine()
|
|
25
|
+
binder.bind(engine.register)
|
|
26
|
+
engine.step(0.1, 1)
|
|
27
|
+
expect(early).toHaveBeenCalledWith(0.1, 1)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('registers straight through after bind and stops on unregister', () => {
|
|
31
|
+
const binder = createTickBinder()
|
|
32
|
+
const engine = fakeEngine()
|
|
33
|
+
binder.bind(engine.register)
|
|
34
|
+
|
|
35
|
+
const late = vi.fn()
|
|
36
|
+
const stop = binder.register(late)
|
|
37
|
+
engine.step(0.2, 2)
|
|
38
|
+
expect(late).toHaveBeenCalledTimes(1)
|
|
39
|
+
|
|
40
|
+
stop()
|
|
41
|
+
engine.step(0.3, 3)
|
|
42
|
+
expect(late).toHaveBeenCalledTimes(1)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('clear drops the binding; later registrations queue again', () => {
|
|
46
|
+
const binder = createTickBinder()
|
|
47
|
+
const engine = fakeEngine()
|
|
48
|
+
binder.bind(engine.register)
|
|
49
|
+
binder.clear()
|
|
50
|
+
|
|
51
|
+
const fn = vi.fn()
|
|
52
|
+
binder.register(fn)
|
|
53
|
+
engine.step(0.4, 4)
|
|
54
|
+
expect(fn).not.toHaveBeenCalled()
|
|
55
|
+
})
|
|
56
|
+
})
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tick binder — a module-scoped bridge between consumer animation code and
|
|
3
|
+
* the engine's tick loop. Registrations made before the engine binds (or
|
|
4
|
+
* between epoch rebuilds) queue up and flush on the next bind, so callers
|
|
5
|
+
* never need to know whether the world is mounted yet.
|
|
6
|
+
*
|
|
7
|
+
* Consumer pattern:
|
|
8
|
+
* const ticks = createTickBinder()
|
|
9
|
+
* // in the preset's buildScene:
|
|
10
|
+
* buildScene: (scene, options) => { if (options.registerTick) ticks.bind(options.registerTick) }
|
|
11
|
+
* // anywhere in feature code:
|
|
12
|
+
* const stop = ticks.register((delta, elapsed) => { ... })
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { WorldTickFn, UnregisterTick } from '../core/types'
|
|
16
|
+
|
|
17
|
+
export interface TickBinder {
|
|
18
|
+
/** Hand the engine's registerTick hook to the binder (from buildScene options). */
|
|
19
|
+
bind(register: (fn: WorldTickFn) => UnregisterTick): void
|
|
20
|
+
/** Register a per-frame step; runs immediately when bound, queues otherwise. */
|
|
21
|
+
register(fn: WorldTickFn): () => void
|
|
22
|
+
/** Drop the binding and any queued steps (the engine clears its own ticks on dispose). */
|
|
23
|
+
clear(): void
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function createTickBinder(): TickBinder {
|
|
27
|
+
let engineRegister: ((fn: WorldTickFn) => UnregisterTick) | null = null
|
|
28
|
+
const pending = new Set<WorldTickFn>()
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
bind(register) {
|
|
32
|
+
engineRegister = register
|
|
33
|
+
for (const fn of pending) register(fn)
|
|
34
|
+
pending.clear()
|
|
35
|
+
},
|
|
36
|
+
register(fn) {
|
|
37
|
+
if (engineRegister) {
|
|
38
|
+
return engineRegister(fn)
|
|
39
|
+
}
|
|
40
|
+
pending.add(fn)
|
|
41
|
+
return () => {
|
|
42
|
+
pending.delete(fn)
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
clear() {
|
|
46
|
+
engineRegister = null
|
|
47
|
+
pending.clear()
|
|
48
|
+
},
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -147,7 +147,8 @@ export function createGreenhouse(options: GreenhouseOptions = {}): THREE.Group {
|
|
|
147
147
|
const {
|
|
148
148
|
width = 9,
|
|
149
149
|
depth = 6,
|
|
150
|
-
height
|
|
150
|
+
// `height` is accepted for API symmetry — the vault derives from depth.
|
|
151
|
+
height: _height = 2.8,
|
|
151
152
|
frameColor = '#e8eaed',
|
|
152
153
|
glassColor = '#cdebdd',
|
|
153
154
|
glassOpacity = 0.3,
|
|
@@ -156,6 +157,7 @@ export function createGreenhouse(options: GreenhouseOptions = {}): THREE.Group {
|
|
|
156
157
|
benches,
|
|
157
158
|
entityId,
|
|
158
159
|
} = options
|
|
160
|
+
void _height
|
|
159
161
|
|
|
160
162
|
const group = new THREE.Group()
|
|
161
163
|
group.name = 'greenhouse'
|
|
@@ -67,7 +67,6 @@ export function createCropPlot(options: CropPlotOptions = {}): THREE.Group {
|
|
|
67
67
|
const rowCount = Math.max(1, Math.round(rows * (0.35 + 0.65 * g)))
|
|
68
68
|
|
|
69
69
|
const rowAxis = rowDirection === 'x' ? 'x' : 'z'
|
|
70
|
-
const crossAxis = rowAxis === 'x' ? 'z' : 'x'
|
|
71
70
|
const rowLength = rowAxis === 'x' ? width : depth
|
|
72
71
|
const crossLength = rowAxis === 'x' ? depth : width
|
|
73
72
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useStableSnapshot'
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Freeze the first non-null value for the lifetime of the visit. Later
|
|
3
|
+
* refetches/recomputations never replace the snapshot — essential for world
|
|
4
|
+
* builds, where a rebuilt scene would steal the user's camera angle.
|
|
5
|
+
*
|
|
6
|
+
* const build = useStableSnapshot(useMemo(() => (loading ? null : buildWorld(data)), [loading, data]))
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { useState } from 'react'
|
|
10
|
+
|
|
11
|
+
export function useStableSnapshot<T>(fresh: T | null | undefined): T | null {
|
|
12
|
+
const [snapshot, setSnapshot] = useState<T | null>(fresh ?? null)
|
|
13
|
+
if (fresh && snapshot === null && fresh !== snapshot) {
|
|
14
|
+
setSnapshot(fresh)
|
|
15
|
+
}
|
|
16
|
+
return snapshot
|
|
17
|
+
}
|
package/src/index.ts
CHANGED
package/src/styles/virtual.css
CHANGED
|
@@ -2072,3 +2072,318 @@
|
|
|
2072
2072
|
.is-night .wv-hq-empty-floor-card p {
|
|
2073
2073
|
color: #94a3b8;
|
|
2074
2074
|
}
|
|
2075
|
+
|
|
2076
|
+
/* ==========================================================================
|
|
2077
|
+
World page shell — standalone full-screen chrome for data-driven worlds
|
|
2078
|
+
(VirtualWorldPage). Theme via the --wv-page-* custom properties, either
|
|
2079
|
+
on .wv-page itself or on a consumer root class passed via `className`.
|
|
2080
|
+
========================================================================== */
|
|
2081
|
+
|
|
2082
|
+
.wv-page {
|
|
2083
|
+
--wv-page-bg: #f6f8f4;
|
|
2084
|
+
--wv-page-surface: #fbfcfa;
|
|
2085
|
+
--wv-page-card: #ffffff;
|
|
2086
|
+
--wv-page-fg: #1c2b20;
|
|
2087
|
+
--wv-page-muted: #6b7a6e;
|
|
2088
|
+
--wv-page-line: #e2e8e0;
|
|
2089
|
+
--wv-page-accent: #2f6b3b;
|
|
2090
|
+
--wv-page-accent-soft: rgba(47, 107, 59, 0.1);
|
|
2091
|
+
--wv-page-shadow: 0 10px 30px rgba(15, 32, 20, 0.14);
|
|
2092
|
+
|
|
2093
|
+
position: fixed;
|
|
2094
|
+
inset: 0;
|
|
2095
|
+
z-index: 40;
|
|
2096
|
+
display: flex;
|
|
2097
|
+
flex-direction: column;
|
|
2098
|
+
overflow: hidden;
|
|
2099
|
+
background: var(--wv-page-bg);
|
|
2100
|
+
color: var(--wv-page-fg);
|
|
2101
|
+
}
|
|
2102
|
+
|
|
2103
|
+
.wv-page__header {
|
|
2104
|
+
display: flex;
|
|
2105
|
+
align-items: center;
|
|
2106
|
+
gap: 12px;
|
|
2107
|
+
padding: 10px 16px;
|
|
2108
|
+
flex-shrink: 0;
|
|
2109
|
+
flex-wrap: wrap;
|
|
2110
|
+
border-bottom: 1px solid var(--wv-page-line);
|
|
2111
|
+
background: var(--wv-page-surface);
|
|
2112
|
+
}
|
|
2113
|
+
|
|
2114
|
+
.wv-page__back {
|
|
2115
|
+
display: inline-flex;
|
|
2116
|
+
align-items: center;
|
|
2117
|
+
padding: 6px 12px;
|
|
2118
|
+
border: 1px solid var(--wv-page-line);
|
|
2119
|
+
border-radius: 999px;
|
|
2120
|
+
font-size: 13px;
|
|
2121
|
+
font-weight: 550;
|
|
2122
|
+
}
|
|
2123
|
+
.wv-page__back a {
|
|
2124
|
+
display: inline-flex;
|
|
2125
|
+
align-items: center;
|
|
2126
|
+
gap: 6px;
|
|
2127
|
+
color: inherit;
|
|
2128
|
+
text-decoration: none;
|
|
2129
|
+
white-space: nowrap;
|
|
2130
|
+
}
|
|
2131
|
+
.wv-page__back a:hover {
|
|
2132
|
+
color: var(--wv-page-accent);
|
|
2133
|
+
}
|
|
2134
|
+
|
|
2135
|
+
.wv-page__heading {
|
|
2136
|
+
display: flex;
|
|
2137
|
+
flex-direction: column;
|
|
2138
|
+
gap: 1px;
|
|
2139
|
+
min-width: 0;
|
|
2140
|
+
line-height: 1.2;
|
|
2141
|
+
}
|
|
2142
|
+
.wv-page__title {
|
|
2143
|
+
font-weight: 700;
|
|
2144
|
+
letter-spacing: 0.01em;
|
|
2145
|
+
}
|
|
2146
|
+
.wv-page__meta {
|
|
2147
|
+
font-size: 11.5px;
|
|
2148
|
+
color: var(--wv-page-muted);
|
|
2149
|
+
white-space: nowrap;
|
|
2150
|
+
overflow: hidden;
|
|
2151
|
+
text-overflow: ellipsis;
|
|
2152
|
+
}
|
|
2153
|
+
|
|
2154
|
+
.wv-page__actions {
|
|
2155
|
+
display: flex;
|
|
2156
|
+
align-items: center;
|
|
2157
|
+
gap: 6px;
|
|
2158
|
+
margin-left: auto;
|
|
2159
|
+
}
|
|
2160
|
+
.wv-page__btn {
|
|
2161
|
+
display: inline-flex;
|
|
2162
|
+
align-items: center;
|
|
2163
|
+
justify-content: center;
|
|
2164
|
+
width: 32px;
|
|
2165
|
+
height: 32px;
|
|
2166
|
+
border: 1px solid var(--wv-page-line);
|
|
2167
|
+
border-radius: 8px;
|
|
2168
|
+
background: var(--wv-page-card);
|
|
2169
|
+
color: var(--wv-page-muted);
|
|
2170
|
+
cursor: pointer;
|
|
2171
|
+
transition: background 120ms ease, color 120ms ease;
|
|
2172
|
+
}
|
|
2173
|
+
.wv-page__btn:hover {
|
|
2174
|
+
background: var(--wv-page-accent-soft);
|
|
2175
|
+
color: var(--wv-page-accent);
|
|
2176
|
+
}
|
|
2177
|
+
|
|
2178
|
+
.wv-page__main {
|
|
2179
|
+
flex: 1;
|
|
2180
|
+
display: flex;
|
|
2181
|
+
min-height: 0;
|
|
2182
|
+
}
|
|
2183
|
+
|
|
2184
|
+
.wv-page__world {
|
|
2185
|
+
position: relative;
|
|
2186
|
+
flex: 1;
|
|
2187
|
+
min-width: 0;
|
|
2188
|
+
}
|
|
2189
|
+
.wv-page__canvas {
|
|
2190
|
+
position: absolute;
|
|
2191
|
+
inset: 0;
|
|
2192
|
+
}
|
|
2193
|
+
|
|
2194
|
+
.wv-page__selected {
|
|
2195
|
+
position: absolute;
|
|
2196
|
+
left: 50%;
|
|
2197
|
+
bottom: 18px;
|
|
2198
|
+
transform: translateX(-50%);
|
|
2199
|
+
z-index: 5;
|
|
2200
|
+
display: flex;
|
|
2201
|
+
align-items: center;
|
|
2202
|
+
gap: 10px;
|
|
2203
|
+
max-width: min(520px, calc(100% - 32px));
|
|
2204
|
+
padding: 10px 14px;
|
|
2205
|
+
border: 1px solid var(--wv-page-line);
|
|
2206
|
+
border-radius: 12px;
|
|
2207
|
+
background: color-mix(in srgb, var(--wv-page-card) 92%, transparent);
|
|
2208
|
+
box-shadow: var(--wv-page-shadow);
|
|
2209
|
+
backdrop-filter: blur(6px);
|
|
2210
|
+
}
|
|
2211
|
+
.wv-page__selected--start {
|
|
2212
|
+
left: 1rem;
|
|
2213
|
+
bottom: 1rem;
|
|
2214
|
+
transform: none;
|
|
2215
|
+
}
|
|
2216
|
+
.wv-page__selected-dot {
|
|
2217
|
+
flex: none;
|
|
2218
|
+
width: 10px;
|
|
2219
|
+
height: 10px;
|
|
2220
|
+
border-radius: 999px;
|
|
2221
|
+
}
|
|
2222
|
+
.wv-page__selected-body {
|
|
2223
|
+
display: flex;
|
|
2224
|
+
flex-direction: column;
|
|
2225
|
+
gap: 1px;
|
|
2226
|
+
min-width: 0;
|
|
2227
|
+
}
|
|
2228
|
+
.wv-page__selected-name {
|
|
2229
|
+
font-size: 13.5px;
|
|
2230
|
+
font-weight: 650;
|
|
2231
|
+
overflow: hidden;
|
|
2232
|
+
text-overflow: ellipsis;
|
|
2233
|
+
white-space: nowrap;
|
|
2234
|
+
}
|
|
2235
|
+
.wv-page__selected-sub {
|
|
2236
|
+
font-size: 12px;
|
|
2237
|
+
color: var(--wv-page-muted);
|
|
2238
|
+
overflow: hidden;
|
|
2239
|
+
text-overflow: ellipsis;
|
|
2240
|
+
white-space: nowrap;
|
|
2241
|
+
}
|
|
2242
|
+
.wv-page__selected-hint {
|
|
2243
|
+
flex: none;
|
|
2244
|
+
font-size: 12px;
|
|
2245
|
+
font-style: italic;
|
|
2246
|
+
color: var(--wv-page-muted);
|
|
2247
|
+
}
|
|
2248
|
+
.wv-page__selected-action {
|
|
2249
|
+
flex: none;
|
|
2250
|
+
}
|
|
2251
|
+
.wv-page__selected-action a,
|
|
2252
|
+
.wv-page__selected-action button {
|
|
2253
|
+
display: inline-flex;
|
|
2254
|
+
align-items: center;
|
|
2255
|
+
padding: 6px 12px;
|
|
2256
|
+
border: none;
|
|
2257
|
+
border-radius: 8px;
|
|
2258
|
+
background: var(--wv-page-accent);
|
|
2259
|
+
color: #fff;
|
|
2260
|
+
font-size: 12.5px;
|
|
2261
|
+
font-weight: 600;
|
|
2262
|
+
text-decoration: none;
|
|
2263
|
+
white-space: nowrap;
|
|
2264
|
+
cursor: pointer;
|
|
2265
|
+
}
|
|
2266
|
+
.wv-page__selected-action a:hover,
|
|
2267
|
+
.wv-page__selected-action button:hover {
|
|
2268
|
+
filter: brightness(0.94);
|
|
2269
|
+
}
|
|
2270
|
+
|
|
2271
|
+
.wv-page__hint {
|
|
2272
|
+
position: absolute;
|
|
2273
|
+
top: 12px;
|
|
2274
|
+
left: 50%;
|
|
2275
|
+
transform: translateX(-50%);
|
|
2276
|
+
z-index: 4;
|
|
2277
|
+
padding: 5px 12px;
|
|
2278
|
+
border-radius: 999px;
|
|
2279
|
+
border: 1px solid var(--wv-page-line);
|
|
2280
|
+
background: color-mix(in srgb, var(--wv-page-card) 82%, transparent);
|
|
2281
|
+
font-size: 11.5px;
|
|
2282
|
+
color: var(--wv-page-muted);
|
|
2283
|
+
pointer-events: none;
|
|
2284
|
+
white-space: nowrap;
|
|
2285
|
+
}
|
|
2286
|
+
|
|
2287
|
+
.wv-page__sidebar {
|
|
2288
|
+
width: 288px;
|
|
2289
|
+
flex: none;
|
|
2290
|
+
overflow-y: auto;
|
|
2291
|
+
padding: 14px;
|
|
2292
|
+
border-left: 1px solid var(--wv-page-line);
|
|
2293
|
+
background: var(--wv-page-surface);
|
|
2294
|
+
display: flex;
|
|
2295
|
+
flex-direction: column;
|
|
2296
|
+
gap: 16px;
|
|
2297
|
+
}
|
|
2298
|
+
.wv-page__section {
|
|
2299
|
+
display: flex;
|
|
2300
|
+
flex-direction: column;
|
|
2301
|
+
gap: 6px;
|
|
2302
|
+
}
|
|
2303
|
+
.wv-page__section-heading {
|
|
2304
|
+
display: flex;
|
|
2305
|
+
align-items: center;
|
|
2306
|
+
gap: 6px;
|
|
2307
|
+
margin: 0;
|
|
2308
|
+
font-size: 11px;
|
|
2309
|
+
font-weight: 700;
|
|
2310
|
+
letter-spacing: 0.08em;
|
|
2311
|
+
text-transform: uppercase;
|
|
2312
|
+
color: var(--wv-page-muted);
|
|
2313
|
+
}
|
|
2314
|
+
.wv-page__list {
|
|
2315
|
+
list-style: none;
|
|
2316
|
+
margin: 0;
|
|
2317
|
+
padding: 0;
|
|
2318
|
+
display: flex;
|
|
2319
|
+
flex-direction: column;
|
|
2320
|
+
gap: 3px;
|
|
2321
|
+
}
|
|
2322
|
+
.wv-page-row {
|
|
2323
|
+
display: grid;
|
|
2324
|
+
grid-template-columns: auto minmax(0, 1fr) auto;
|
|
2325
|
+
align-items: center;
|
|
2326
|
+
column-gap: 8px;
|
|
2327
|
+
width: 100%;
|
|
2328
|
+
padding: 7px 9px;
|
|
2329
|
+
border: 1px solid transparent;
|
|
2330
|
+
border-radius: 9px;
|
|
2331
|
+
background: transparent;
|
|
2332
|
+
color: inherit;
|
|
2333
|
+
font-size: 13px;
|
|
2334
|
+
text-align: left;
|
|
2335
|
+
cursor: pointer;
|
|
2336
|
+
}
|
|
2337
|
+
.wv-page-row:hover {
|
|
2338
|
+
background: var(--wv-page-accent-soft);
|
|
2339
|
+
}
|
|
2340
|
+
.wv-page-row--active {
|
|
2341
|
+
border-color: var(--wv-page-accent);
|
|
2342
|
+
background: var(--wv-page-accent-soft);
|
|
2343
|
+
}
|
|
2344
|
+
.wv-page-row--dashed {
|
|
2345
|
+
border-color: var(--wv-page-line);
|
|
2346
|
+
border-style: dashed;
|
|
2347
|
+
}
|
|
2348
|
+
.wv-page-row__dot {
|
|
2349
|
+
width: 10px;
|
|
2350
|
+
height: 10px;
|
|
2351
|
+
border-radius: 999px;
|
|
2352
|
+
flex: none;
|
|
2353
|
+
}
|
|
2354
|
+
.wv-page-row__label {
|
|
2355
|
+
min-width: 0;
|
|
2356
|
+
overflow: hidden;
|
|
2357
|
+
text-overflow: ellipsis;
|
|
2358
|
+
white-space: nowrap;
|
|
2359
|
+
font-weight: 500;
|
|
2360
|
+
}
|
|
2361
|
+
.wv-page-row__meta {
|
|
2362
|
+
flex-shrink: 0;
|
|
2363
|
+
font-size: 11.5px;
|
|
2364
|
+
color: var(--wv-page-muted);
|
|
2365
|
+
}
|
|
2366
|
+
|
|
2367
|
+
.wv-page__footer {
|
|
2368
|
+
display: flex;
|
|
2369
|
+
align-items: center;
|
|
2370
|
+
justify-content: center;
|
|
2371
|
+
height: 2.25rem;
|
|
2372
|
+
flex-shrink: 0;
|
|
2373
|
+
border-top: 1px solid var(--wv-page-line);
|
|
2374
|
+
background: var(--wv-page-surface);
|
|
2375
|
+
font-size: 11.5px;
|
|
2376
|
+
color: var(--wv-page-muted);
|
|
2377
|
+
}
|
|
2378
|
+
|
|
2379
|
+
@media (max-width: 859.98px) {
|
|
2380
|
+
.wv-page__main {
|
|
2381
|
+
flex-direction: column;
|
|
2382
|
+
}
|
|
2383
|
+
.wv-page__sidebar {
|
|
2384
|
+
width: 100%;
|
|
2385
|
+
max-height: 42vh;
|
|
2386
|
+
border-left: none;
|
|
2387
|
+
border-top: 1px solid var(--wv-page-line);
|
|
2388
|
+
}
|
|
2389
|
+
}
|