@vibe-engine/web 0.0.0-stage → 0.1.1
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/LICENSE-APACHE +202 -0
- package/LICENSE-MIT +22 -0
- package/README.md +10 -2
- package/dist/audio.d.ts +27 -0
- package/dist/audio.js +190 -0
- package/dist/boot.d.ts +44 -0
- package/dist/boot.js +50 -0
- package/dist/debug.d.ts +8 -0
- package/dist/debug.js +26 -0
- package/dist/fit.d.ts +9 -0
- package/dist/fit.js +21 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +148 -0
- package/dist/keyboard.d.ts +20 -0
- package/dist/keyboard.js +54 -0
- package/dist/save-store.d.ts +6 -0
- package/dist/save-store.js +28 -0
- package/dist/spatial.d.ts +49 -0
- package/dist/spatial.js +21 -0
- package/dist/ui-theme.d.ts +22 -0
- package/dist/ui-theme.js +133 -0
- package/dist/ui.d.ts +23 -0
- package/dist/ui.js +221 -0
- package/package.json +39 -3
package/dist/ui.js
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Overlay DOM da UI do jogo: desenha as entidades `Ui*` por cima do canvas.
|
|
3
|
+
* Só lê o estado; clique e teclado viram `world.ui.click/navigate/confirm`
|
|
4
|
+
* (o mesmo caminho de testes e agentes). Botões são <button> de verdade; campos
|
|
5
|
+
* (`UiInput`) são <input>, e o texto digitado vai para o estado por `world.ui.type`.
|
|
6
|
+
*/
|
|
7
|
+
import { UiAnchor, UiButton, UiFocus, UiFocused, UiImage, UiInput, UiStack, UiStyle, UiText, sheetFrameRect, uiScope, uiVisible } from '@vibe-engine/core';
|
|
8
|
+
import { fontFamily, mergeTheme, themeCss } from './ui-theme.js';
|
|
9
|
+
import { isTyping } from './keyboard.js';
|
|
10
|
+
const CORNERS = {
|
|
11
|
+
// [left|right|center, top|bottom|middle, transform]
|
|
12
|
+
'top-left': ['left', 'top', ''], top: ['center', 'top', 'translateX(-50%)'], 'top-right': ['right', 'top', ''],
|
|
13
|
+
left: ['left', 'middle', 'translateY(-50%)'], center: ['center', 'middle', 'translate(-50%, -50%)'], right: ['right', 'middle', 'translateY(-50%)'],
|
|
14
|
+
'bottom-left': ['left', 'bottom', ''], bottom: ['center', 'bottom', 'translateX(-50%)'], 'bottom-right': ['right', 'bottom', ''],
|
|
15
|
+
};
|
|
16
|
+
export function createUiOverlay(opts) {
|
|
17
|
+
const root = document.createElement('div');
|
|
18
|
+
root.className = 'vibe-ui';
|
|
19
|
+
root.style.cssText = 'position:absolute;inset:0;pointer-events:none';
|
|
20
|
+
const sheet = document.createElement('style');
|
|
21
|
+
sheet.textContent = themeCss(opts.theme ?? {}, { images: opts.assets ?? {}, meta: opts.meta ?? {}, fonts: opts.fonts ?? {} });
|
|
22
|
+
root.appendChild(sheet);
|
|
23
|
+
opts.parent.style.position ||= 'relative';
|
|
24
|
+
opts.parent.appendChild(root);
|
|
25
|
+
for (const [id, url] of Object.entries(opts.fonts ?? {})) {
|
|
26
|
+
const face = new FontFace(fontFamily(id), `url(${url})`);
|
|
27
|
+
document.fonts.add(face);
|
|
28
|
+
face.load().catch((err) => console.warn(`[vibe] font '${id}' failed to load: ${err.message}`));
|
|
29
|
+
}
|
|
30
|
+
const styles = new Set(Object.keys(mergeTheme(opts.theme).styles));
|
|
31
|
+
const warned = new Set();
|
|
32
|
+
const nodes = new Map();
|
|
33
|
+
const sizes = new Map();
|
|
34
|
+
for (const [id, url] of Object.entries(opts.assets ?? {})) {
|
|
35
|
+
const img = new Image();
|
|
36
|
+
img.onload = () => sizes.set(id, { w: img.naturalWidth, h: img.naturalHeight });
|
|
37
|
+
img.src = url;
|
|
38
|
+
}
|
|
39
|
+
// Teclado: com um painel de foco aberto, setas/WASD navegam e Enter/Espaço confirmam (o jogo não recebe essas teclas).
|
|
40
|
+
const keys = (e) => {
|
|
41
|
+
if (isTyping(e.target) || !uiScope(opts.world).trap)
|
|
42
|
+
return; // num campo, as teclas são do campo (ver `field`)
|
|
43
|
+
const dir = { ArrowUp: 'prev', KeyW: 'prev', ArrowLeft: 'prev', KeyA: 'prev', ArrowDown: 'next', KeyS: 'next', ArrowRight: 'next', KeyD: 'next' }[e.code];
|
|
44
|
+
if (dir)
|
|
45
|
+
opts.world.ui.navigate(dir);
|
|
46
|
+
else if (e.code === 'Enter' || e.code === 'Space') {
|
|
47
|
+
if (!e.repeat)
|
|
48
|
+
opts.world.ui.confirm();
|
|
49
|
+
}
|
|
50
|
+
else
|
|
51
|
+
return;
|
|
52
|
+
e.preventDefault();
|
|
53
|
+
e.stopPropagation(); // o jogo (listeners na fase de bolha) não recebe; quem escuta na captura (desbloqueio de áudio) sim
|
|
54
|
+
};
|
|
55
|
+
addEventListener('keydown', keys, { capture: true });
|
|
56
|
+
// Campo de texto: digitar vai para o estado; Enter envia; setas/Tab navegam; Esc solta o foco. Nenhuma tecla chega ao jogo.
|
|
57
|
+
const field = (el, id) => {
|
|
58
|
+
el.type = 'text';
|
|
59
|
+
el.autocomplete = 'off';
|
|
60
|
+
el.spellcheck = false;
|
|
61
|
+
el.addEventListener('input', () => { if (opts.world.exists(id))
|
|
62
|
+
opts.world.ui.type(id, el.value); });
|
|
63
|
+
el.addEventListener('focus', () => { if (opts.world.exists(id) && opts.world.ui.focused() !== id)
|
|
64
|
+
opts.world.ui.focus(id); });
|
|
65
|
+
el.addEventListener('mousedown', (ev) => ev.stopPropagation()); // clique no campo não vira tiro
|
|
66
|
+
el.addEventListener('keydown', (ev) => {
|
|
67
|
+
ev.stopPropagation();
|
|
68
|
+
const dir = { ArrowUp: 'prev', ArrowDown: 'next', Tab: ev.shiftKey ? 'prev' : 'next' }[ev.code];
|
|
69
|
+
if (ev.code === 'Enter') {
|
|
70
|
+
if (!ev.repeat && opts.world.exists(id))
|
|
71
|
+
opts.world.ui.submit(id);
|
|
72
|
+
}
|
|
73
|
+
else if (ev.code === 'Escape')
|
|
74
|
+
el.blur();
|
|
75
|
+
else if (dir)
|
|
76
|
+
opts.world.ui.navigate(dir);
|
|
77
|
+
else
|
|
78
|
+
return;
|
|
79
|
+
ev.preventDefault();
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
let lastFocused = null;
|
|
83
|
+
const element = (world, id) => {
|
|
84
|
+
const e = world.entity(id);
|
|
85
|
+
const key = e.has(UiButton) ? 'button' : e.has(UiInput) ? 'input' : e.has(UiImage) ? 'image' : 'div';
|
|
86
|
+
let n = nodes.get(id);
|
|
87
|
+
if (!n || n.key !== key) {
|
|
88
|
+
n?.el.remove();
|
|
89
|
+
const el = document.createElement(key === 'button' ? 'button' : key === 'input' ? 'input' : 'div');
|
|
90
|
+
if (key === 'input')
|
|
91
|
+
field(el, id);
|
|
92
|
+
el.dataset.ui = id;
|
|
93
|
+
if (key === 'image')
|
|
94
|
+
el.dataset.image = '';
|
|
95
|
+
if (key === 'button') {
|
|
96
|
+
el.addEventListener('click', (ev) => { ev.preventDefault(); if (world.exists(id))
|
|
97
|
+
opts.world.ui.click(id); });
|
|
98
|
+
el.addEventListener('mousedown', (ev) => ev.stopPropagation()); // clique no menu não vira tiro
|
|
99
|
+
}
|
|
100
|
+
n = { el, key };
|
|
101
|
+
nodes.set(id, n);
|
|
102
|
+
}
|
|
103
|
+
return n;
|
|
104
|
+
};
|
|
105
|
+
return {
|
|
106
|
+
render(world) {
|
|
107
|
+
const ui = world.query().filter((e) => e.has(UiAnchor) || e.has(UiStack) || e.has(UiText) || e.has(UiButton) || e.has(UiInput) || e.has(UiImage) || e.has(UiFocus));
|
|
108
|
+
const ids = new Set(ui.map((e) => e.id));
|
|
109
|
+
for (const [id, n] of nodes)
|
|
110
|
+
if (!ids.has(id)) {
|
|
111
|
+
n.el.remove();
|
|
112
|
+
nodes.delete(id);
|
|
113
|
+
}
|
|
114
|
+
for (const e of ui) {
|
|
115
|
+
const { el } = element(world, e.id);
|
|
116
|
+
const parentId = e.id.includes('/') ? e.id.slice(0, e.id.lastIndexOf('/')) : null;
|
|
117
|
+
const parent = parentId && ids.has(parentId) ? element(world, parentId).el : root;
|
|
118
|
+
if (el.parentElement !== parent)
|
|
119
|
+
parent.appendChild(el);
|
|
120
|
+
el.style.display = uiVisible(world, e.id) ? '' : 'none';
|
|
121
|
+
const use = e.has(UiStyle) ? e.read(UiStyle).use.split(/\s+/).filter(Boolean) : [];
|
|
122
|
+
for (const name of use)
|
|
123
|
+
if (!styles.has(name) && !warned.has(name)) {
|
|
124
|
+
warned.add(name);
|
|
125
|
+
console.warn(`[vibe] ${e.id}: style '${name}' is not in ui.styles of vibe.config.ts`);
|
|
126
|
+
}
|
|
127
|
+
const className = [e.has(UiButton) ? 'button' : e.has(UiInput) ? 'input' : e.has(UiText) ? 'text' : '', ...use].filter(Boolean).map((n) => `vibe-s-${n}`).join(' ');
|
|
128
|
+
if (el.className !== className)
|
|
129
|
+
el.className = className;
|
|
130
|
+
if (e.has(UiAnchor) && parent === root) {
|
|
131
|
+
const a = e.read(UiAnchor);
|
|
132
|
+
const [h, v, transform] = CORNERS[a.corner];
|
|
133
|
+
Object.assign(el.style, { position: 'absolute', left: '', right: '', top: '', bottom: '', transform });
|
|
134
|
+
if (h === 'center')
|
|
135
|
+
el.style.left = `calc(50% + ${a.offset.x}px)`;
|
|
136
|
+
else
|
|
137
|
+
el.style[h] = `${a.margin + (h === 'left' ? a.offset.x : -a.offset.x)}px`;
|
|
138
|
+
if (v === 'middle')
|
|
139
|
+
el.style.top = `calc(50% + ${a.offset.y}px)`;
|
|
140
|
+
else
|
|
141
|
+
el.style[v] = `${a.margin + (v === 'top' ? a.offset.y : -a.offset.y)}px`;
|
|
142
|
+
}
|
|
143
|
+
if (e.has(UiStack)) {
|
|
144
|
+
const s = e.read(UiStack);
|
|
145
|
+
Object.assign(el.style, { display: el.style.display === 'none' ? 'none' : 'flex', flexDirection: s.direction, gap: `${s.gap}px`, alignItems: { center: 'center', start: 'flex-start', end: 'flex-end', stretch: 'stretch' }[s.align], background: s.background, padding: s.padding ? `${s.padding}px` : '' });
|
|
146
|
+
}
|
|
147
|
+
if (e.has(UiText) && !e.has(UiButton)) {
|
|
148
|
+
const t = e.read(UiText);
|
|
149
|
+
if (el.textContent !== t.text)
|
|
150
|
+
el.textContent = t.text;
|
|
151
|
+
Object.assign(el.style, { fontSize: t.size ? `${t.size}px` : '', color: t.color ?? '', whiteSpace: 'pre' });
|
|
152
|
+
}
|
|
153
|
+
if (e.has(UiButton)) {
|
|
154
|
+
const b = e.read(UiButton);
|
|
155
|
+
if (el.textContent !== b.label)
|
|
156
|
+
el.textContent = b.label;
|
|
157
|
+
el.disabled = b.disabled;
|
|
158
|
+
el.toggleAttribute('data-focused', e.has(UiFocused));
|
|
159
|
+
}
|
|
160
|
+
if (e.has(UiInput)) {
|
|
161
|
+
const f = e.read(UiInput);
|
|
162
|
+
const input = el;
|
|
163
|
+
// Com o campo em uso, quem manda no texto é quem digita (o estado chega um tick depois).
|
|
164
|
+
if (document.activeElement !== input && input.value !== f.value)
|
|
165
|
+
input.value = f.value;
|
|
166
|
+
if (input.placeholder !== f.placeholder)
|
|
167
|
+
input.placeholder = f.placeholder;
|
|
168
|
+
if (input.maxLength !== f.maxLength)
|
|
169
|
+
input.maxLength = f.maxLength;
|
|
170
|
+
input.disabled = f.disabled;
|
|
171
|
+
input.toggleAttribute('data-focused', e.has(UiFocused));
|
|
172
|
+
}
|
|
173
|
+
if (e.has(UiImage)) {
|
|
174
|
+
const img = e.read(UiImage);
|
|
175
|
+
const url = opts.assets?.[img.asset];
|
|
176
|
+
const size = sizes.get(img.asset);
|
|
177
|
+
const meta = opts.meta?.[img.asset];
|
|
178
|
+
const frame = meta && size ? sheetFrameRect(meta, size.w, img.frame) : null;
|
|
179
|
+
const w = img.width || frame?.width || size?.w || 0;
|
|
180
|
+
const h = img.height || frame?.height || size?.h || 0;
|
|
181
|
+
const scale = frame ? w / frame.width : 1;
|
|
182
|
+
Object.assign(el.style, {
|
|
183
|
+
width: `${w}px`, height: `${h}px`, imageRendering: 'pixelated', backgroundRepeat: 'no-repeat',
|
|
184
|
+
backgroundImage: url ? `url(${url})` : '',
|
|
185
|
+
backgroundSize: frame && size ? `${size.w * scale}px ${size.h * scale}px` : '100% 100%',
|
|
186
|
+
backgroundPosition: frame ? `-${frame.x * scale}px -${frame.y * scale}px` : '0 0',
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
// O foco do DOM segue o do estado só quando ele muda (navegação): clicar num campo pede o foco pelo caminho inverso.
|
|
191
|
+
const focused = world.query(UiFocused)[0]?.id ?? null;
|
|
192
|
+
if (focused !== lastFocused) {
|
|
193
|
+
const now = focused ? nodes.get(focused) : undefined;
|
|
194
|
+
const before = lastFocused ? nodes.get(lastFocused) : undefined;
|
|
195
|
+
if (now?.key === 'input')
|
|
196
|
+
now.el.focus();
|
|
197
|
+
else if (before?.key === 'input' && document.activeElement === before.el)
|
|
198
|
+
before.el.blur();
|
|
199
|
+
lastFocused = focused;
|
|
200
|
+
}
|
|
201
|
+
// Ordem de spawn dentro de cada pai. Só mexe no DOM se a ordem estiver errada:
|
|
202
|
+
// reinserir um botão a cada frame cancela o clique (mousedown e mouseup em nós "diferentes").
|
|
203
|
+
const byParent = new Map();
|
|
204
|
+
for (const e of ui) {
|
|
205
|
+
const el = nodes.get(e.id).el;
|
|
206
|
+
if (el.parentElement)
|
|
207
|
+
byParent.set(el.parentElement, [...(byParent.get(el.parentElement) ?? []), el]);
|
|
208
|
+
}
|
|
209
|
+
for (const [parent, wanted] of byParent) {
|
|
210
|
+
const actual = [...parent.children].filter((c) => wanted.includes(c));
|
|
211
|
+
if (actual.some((c, i) => c !== wanted[i]))
|
|
212
|
+
for (const el of wanted)
|
|
213
|
+
parent.appendChild(el);
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
destroy() {
|
|
217
|
+
removeEventListener('keydown', keys, { capture: true });
|
|
218
|
+
root.remove();
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vibe-engine/web",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "vibe-engine browser runtime: game loop, input, UI overlay, audio",
|
|
5
|
+
"license": "MIT OR Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/sergiobanhos/vibe-engine.git",
|
|
9
|
+
"directory": "packages/web"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/sergiobanhos/vibe-engine#readme",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"game-engine",
|
|
14
|
+
"ecs",
|
|
15
|
+
"typescript",
|
|
16
|
+
"headless",
|
|
17
|
+
"ai-native",
|
|
18
|
+
"vibe-engine"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=24"
|
|
22
|
+
},
|
|
23
|
+
"type": "module",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@vibe-engine/core": "0.1.1"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist",
|
|
35
|
+
"LICENSE-MIT",
|
|
36
|
+
"LICENSE-APACHE",
|
|
37
|
+
"README.md"
|
|
38
|
+
],
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
}
|
|
6
42
|
}
|