Repository navigation
Expand file tree
/
Copy pathui.js
More file actions
666 lines (591 loc) · 22.3 KB
/
Copy pathui.js
File metadata and controls
666 lines (591 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
let flock;
//let fontFamily = "Asap";
let fontFamily = "Atkinson Hyperlegible Next";
export function setFlockReference(ref) {
flock = ref;
}
export const flockUI = {
UIText({ text, x, y, fontSize, color, duration, id = null } = {}) {
if (!flock.scene || !flock.GUI) {
return;
}
flock.scene.UITexture ??=
flock.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
const textBlockId =
id || `textBlock_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
const maxWidth = flock.scene.getEngine().getRenderWidth();
const maxHeight = flock.scene.getEngine().getRenderHeight();
const adjustedX = x < 0 ? maxWidth + x : x;
const adjustedY = y < 0 ? maxHeight + y : y;
let textBlock = flock.scene.UITexture.getControlByName(textBlockId);
if (!textBlock) {
textBlock = new flock.GUI.TextBlock(textBlockId);
textBlock.name = textBlockId;
flock.scene.UITexture.addControl(textBlock);
// Anchor control at screen top-left; don't block input.
textBlock.horizontalAlignment = flock.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
textBlock.verticalAlignment = flock.GUI.Control.VERTICAL_ALIGNMENT_TOP;
textBlock.textWrapping = false;
textBlock.isPointerBlocker = false;
// Keep text centered inside its line box.
textBlock.textHorizontalAlignment = flock.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
textBlock.textVerticalAlignment = flock.GUI.Control.VERTICAL_ALIGNMENT_CENTER;
}
// Update text + style
textBlock.text = text;
textBlock.color = color || "white";
// Font & line box
const px = Number(fontSize || 24);
textBlock.fontSize = px;
const linePx = Math.max(1, Math.round(px * 1.2)); // 1.2× prevents ascender clipping
textBlock.lineHeight = `${linePx}px`;
textBlock.height = `${Math.max(linePx, px + 4)}px`;
// Position
textBlock.left = adjustedX;
textBlock.top = adjustedY;
// WEB FONT STABILIZER: avoid the “first update jump”
if (document.fonts && document.fonts.status !== "loaded") {
// Hide until fonts are ready, then force a clean measure.
const prevAlpha = textBlock.alpha;
textBlock.alpha = 0;
document.fonts.ready.then(() => {
textBlock._markAsDirty(); // re-measure with real font
textBlock.alpha = prevAlpha ?? 1;
});
} else {
// If fonts are already ready, still nudge a re-measure on update.
// This helps when the first call happened before the font loaded.
setTimeout(() => textBlock._markAsDirty(), 0);
}
// Auto-hide after duration
if (duration > 0) {
setTimeout(() => {
const ctl = flock.scene.UITexture.getControlByName(textBlockId);
if (ctl) ctl.isVisible = false;
}, duration * 1000);
}
return textBlockId;
},
UIButton({
text,
x,
y,
width,
textSize,
textColor,
backgroundColor,
buttonId,
} = {}) {
// Ensure flock.scene and flock.GUI are initialized
if (!flock.scene || !flock.GUI) {
throw new Error("flock.scene or flock.GUI is not initialized.");
}
// Ensure UITexture exists
flock.scene.UITexture ??=
flock.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
// Validate buttonId
if (!buttonId || typeof buttonId !== "string") {
throw new Error("buttonId must be a valid non-empty string.");
}
// Create a Babylon.js GUI button
const button = flock.GUI.Button.CreateSimpleButton(buttonId, text);
// Preset button sizes for consistency
const buttonSizes = {
SMALL: { width: "100px", height: "40px" },
MEDIUM: { width: "150px", height: "50px" },
LARGE: { width: "200px", height: "60px" },
};
// Validate and apply the selected size
if (typeof width !== "string") {
throw new Error(
"Invalid button size. Please provide a valid size: 'SMALL', 'MEDIUM', or 'LARGE'.",
);
}
const size = buttonSizes[width.toUpperCase()] || buttonSizes["SMALL"];
button.width = size.width;
button.height = size.height;
// Configure text block settings
if (button.textBlock) {
button.textBlock.textWrapping = true;
button.textBlock.resizeToFit = true;
button.textBlock.fontSize = textSize ? `${textSize}px` : "16px"; // Default font size
} else {
console.warn(
"No textBlock found for the button. Text-related settings will not be applied.",
);
}
// Set button text color and background color
button.color = textColor || "white";
button.background = backgroundColor || "blue";
// Validate x and y positions
if (typeof x !== "number" || typeof y !== "number") {
throw new Error("x and y must be numbers.");
}
// Set button alignment
button.left = `${x}px`;
button.top = `${y}px`;
button.horizontalAlignment =
x < 0
? flock.GUI.Control.HORIZONTAL_ALIGNMENT_RIGHT
: flock.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
button.verticalAlignment =
y < 0
? flock.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM
: flock.GUI.Control.VERTICAL_ALIGNMENT_TOP;
// Add the button to the UI
flock.scene.UITexture.addControl(button);
// Return the buttonId for future reference
return buttonId;
},
async UIInput({
text,
x,
y,
size,
fontSize,
textColor,
backgroundColor,
id = null,
mode = "AWAIT", // "START" or "AWAIT"
} = {}) {
if (!flock.scene || !flock.GUI) {
throw new Error("flock.scene or flock.GUI is not initialized.");
}
flock.scene.UITexture ??=
flock.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
flock.scene.UITexture.controls ??= [];
const inputId =
id || `input_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
const sizeMap = {
SMALL: { width: "200px", height: "40px" },
MEDIUM: { width: "300px", height: "50px" },
LARGE: { width: "400px", height: "60px" },
};
const resolvedSize =
sizeMap[(size || "").toUpperCase()] || sizeMap["MEDIUM"];
const inputWidth = resolvedSize.width;
const inputHeight = resolvedSize.height;
const buttonWidth = "50px";
const buttonHeight = resolvedSize.height;
const spacing = 10;
// Create input box
const input = new flock.GUI.InputText(inputId);
input.placeholderText = text;
input.width = inputWidth;
input.height = inputHeight;
input.color = textColor || "black";
input.background = backgroundColor || "white";
input.focusedBackground = backgroundColor || "white";
input.fontSize = fontSize || 24;
input.text = "";
input.left = `${x}px`;
input.top = `${y}px`;
input.horizontalAlignment =
x < 0
? flock.GUI.Control.HORIZONTAL_ALIGNMENT_RIGHT
: flock.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
input.verticalAlignment =
y < 0
? flock.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM
: flock.GUI.Control.VERTICAL_ALIGNMENT_TOP;
// Create submit button
const button = flock.GUI.Button.CreateSimpleButton(
`submit_${inputId}`,
"✓",
);
button.width = buttonWidth;
button.height = buttonHeight;
button.color = backgroundColor || "gray";
button.background = textColor || "white";
button.fontSize = fontSize || 24;
button.left = `${x + parseInt(inputWidth) + spacing}px`;
button.top = `${y}px`;
button.horizontalAlignment = input.horizontalAlignment;
button.verticalAlignment = input.verticalAlignment;
flock.scene.UITexture.addControl(input);
flock.scene.UITexture.addControl(button);
flock.scene.UITexture.controls.push(input, button);
if (mode === "START") {
// Start mode: return the ID immediately (no internal listener)
return inputId;
}
// Await mode: return a Promise that resolves once
return new Promise((resolve) => {
const submit = () => {
const value = input.text;
input.isVisible = false;
button.isVisible = false;
resolve(value);
};
button.onPointerUpObservable.add(submit);
input.onKeyboardEventProcessedObservable.add((event) => {
if (event.type === "keydown" && event.key === "Enter") {
submit();
}
});
});
},
UISlider({
id,
min,
max,
value,
x,
y,
size,
textColor,
backgroundColor,
} = {}) {
if (!flock.scene || !flock.GUI) {
throw new Error("flock.scene or flock.GUI is not initialized.");
}
flock.scene.UITexture ??=
flock.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
const sliderSizes = {
SMALL: { width: "100px", height: "20px" },
MEDIUM: { width: "200px", height: "30px" },
LARGE: { width: "300px", height: "40px" },
};
const resolvedSize =
sliderSizes[(size || "MEDIUM").toUpperCase()] || sliderSizes.MEDIUM;
const slider = new flock.GUI.Slider();
slider.name = id;
slider.minimum = min;
slider.maximum = max;
slider.value = value;
slider.color = textColor || "#000000";
slider.background = backgroundColor || "#ffffff";
slider.height = resolvedSize.height;
slider.width = resolvedSize.width;
slider.left = `${x}px`;
slider.top = `${y}px`;
slider.horizontalAlignment =
x < 0
? flock.GUI.Control.HORIZONTAL_ALIGNMENT_RIGHT
: flock.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
slider.verticalAlignment =
y < 0
? flock.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM
: flock.GUI.Control.VERTICAL_ALIGNMENT_TOP;
flock.scene.UITexture.addControl(slider);
return slider;
},
createSmallButton(text, key, color) {
if (!flock.controlsTexture) return;
const button = flock.GUI.Button.CreateSimpleButton("but", text);
button.width = `${70 * flock.displayScale}px`; // Scale size
button.height = `${70 * flock.displayScale}px`;
button.color = color;
button.background = "transparent";
button.fontSize = `${40 * flock.displayScale}px`; // Scale font size
button.fontFamily = fontFamily;
button.onPointerDownObservable.add(() => {
flock.canvas.pressedButtons.add(key);
flock.gridKeyPressObservable.notifyObservers(key);
});
button.onPointerUpObservable.add(() => {
flock.canvas.pressedButtons.delete(key);
flock.gridKeyReleaseObservable.notifyObservers(key);
});
return button;
},
createArrowControls(color) {
// Add a safety check at the beginning of the function
if (!flock.controlsTexture) return;
// Create a grid
const grid = new flock.GUI.Grid();
grid.width = `${240 * flock.displayScale}px`;
grid.height = `${160 * flock.displayScale}px`;
grid.horizontalAlignment = flock.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
grid.verticalAlignment = flock.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM;
grid.addRowDefinition(1);
grid.addRowDefinition(1);
grid.addColumnDefinition(1);
grid.addColumnDefinition(1);
grid.addColumnDefinition(1);
flock.controlsTexture.addControl(grid);
const upButton = flock.createSmallButton("△", "w", color);
const downButton = flock.createSmallButton("▽", "s", color);
const leftButton = flock.createSmallButton("◁", "a", color);
const rightButton = flock.createSmallButton("▷", "d", color);
// Add buttons to the grid
grid.addControl(upButton, 0, 1); // Add to row 0, column 1
grid.addControl(leftButton, 1, 0); // Add to row 1, column 0
grid.addControl(downButton, 1, 1); // Add to row 1, column 1
grid.addControl(rightButton, 1, 2); // Add to row 1, column 2
},
createButtonControls(color) {
if (!flock.controlsTexture) return;
// Create another grid for the buttons on the right
const rightGrid = new flock.GUI.Grid();
rightGrid.width = `${160 * flock.displayScale}px`; // Scale width
rightGrid.height = `${160 * flock.displayScale}px`; // Scale height
rightGrid.horizontalAlignment =
flock.GUI.Control.HORIZONTAL_ALIGNMENT_RIGHT;
rightGrid.verticalAlignment = flock.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM;
rightGrid.addRowDefinition(1);
rightGrid.addRowDefinition(1);
rightGrid.addColumnDefinition(1);
rightGrid.addColumnDefinition(1);
flock.controlsTexture.addControl(rightGrid);
// Create buttons for the right grid
const button1 = flock.createSmallButton("■", "q", color);
const button2 = flock.createSmallButton("✿", "e", color);
const button3 = flock.createSmallButton("✱", "f", color);
const button4 = flock.createSmallButton("∞", " ", color);
// Add buttons to the right grid in a 2x2 layout
rightGrid.addControl(button1, 0, 0); // Row 0, Column 0
rightGrid.addControl(button2, 0, 1); // Row 0, Column 1
rightGrid.addControl(button3, 1, 0); // Row 1, Column 0
rightGrid.addControl(button4, 1, 1); // Row 1, Column 1
},
buttonControls(control, enabled, color) {
if (flock.controlsTexture) {
flock.controlsTexture.dispose();
}
flock.controlsTexture =
flock.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
// Set all controls to be non-interactive if disabled
flock.controlsTexture.rootContainer.isEnabled = enabled;
// Only create/update controls if they don't exist yet
if (enabled) {
if (control == "ARROWS" || control == "BOTH")
flock.createArrowControls(color);
if (control == "ACTIONS" || control == "BOTH")
flock.createButtonControls(color);
}
},
canvasControls(setting) {
if (setting) {
flock.scene.activeCamera.attachControl(flock.canvas, false);
} else {
flock.scene.activeCamera.detachControl();
}
},
say(meshName, options = {}) {
const {
text,
duration,
textColor = "white",
backgroundColor = "#000000",
alpha = 0.7,
size = 16,
mode = "APPEND",
} = options;
if (!flock.scene) {
console.error("Scene is not available.");
return Promise.reject("Scene is not available.");
}
const mesh = flock.scene.getMeshByName(meshName);
if (mesh) {
// Mesh is available immediately — always return the Promise!
return handleMesh(mesh);
} else {
// Mesh not ready: wait for it, and only resolve after text has been removed
return new Promise((resolve, reject) => {
flock.whenModelReady(meshName, function (mesh) {
if (mesh) {
handleMesh(mesh).then(resolve).catch(reject);
} else {
console.error("Mesh is not defined.");
reject("Mesh is not defined.");
}
});
});
}
function handleMesh(mesh) {
return new Promise((resolve) => {
const targetMesh = mesh;
let plane;
let background = "transparent";
if (targetMesh.metadata && targetMesh.metadata.shape == "plane") {
plane = targetMesh;
background = plane.material.diffuseColor.toHexString();
plane.material.needDepthPrePass = true;
} else {
plane = mesh
.getDescendants()
.find((child) => child.name === "textPlane");
}
let advancedTexture;
if (!plane) {
plane = flock.BABYLON.MeshBuilder.CreatePlane(
"textPlane",
{ width: 4, height: 4 },
flock.scene,
);
plane.name = "textPlane";
plane.parent = targetMesh;
plane.alpha = 1;
plane.checkCollisions = false;
plane.isPickable = false;
// Get initial bounding info.
let boundingInfo = targetMesh.getBoundingInfo();
plane.position.y =
boundingInfo.boundingBox.maximum.y + 2.5 / targetMesh.scaling.y;
plane.billboardMode = flock.BABYLON.Mesh.BILLBOARDMODE_ALL;
flock.scene.onBeforeRenderObservable.add(() => {
boundingInfo = targetMesh.getBoundingInfo();
const parentScale = targetMesh.scaling;
plane.scaling.x = 1 / parentScale.x;
plane.scaling.y = 1 / parentScale.y;
plane.scaling.z = 1 / parentScale.z;
plane.position.y =
boundingInfo.boundingBox.maximum.y + 2.1 / parentScale.y;
});
}
if (!plane.advancedTexture) {
const planeBoundingInfo = plane.getBoundingInfo();
const planeWidth = planeBoundingInfo.boundingBox.extendSize.x * 2;
const planeHeight = planeBoundingInfo.boundingBox.extendSize.y * 2;
const aspectRatio = planeWidth / planeHeight;
const baseResolution = 1024;
const textureWidth =
baseResolution * (aspectRatio > 1 ? 1 : aspectRatio);
const textureHeight =
baseResolution * (aspectRatio > 1 ? 1 / aspectRatio : 1);
advancedTexture = flock.GUI.AdvancedDynamicTexture.CreateForMesh(
plane,
textureWidth,
textureHeight,
);
advancedTexture.isTransparent = true;
plane.advancedTexture = advancedTexture;
if (targetMesh.metadata && targetMesh.metadata.shape == "plane") {
let fullScreenRect = new flock.GUI.Rectangle();
fullScreenRect.width = "100%";
fullScreenRect.height = "100%";
fullScreenRect.background = background;
fullScreenRect.color = "transparent";
advancedTexture.addControl(fullScreenRect);
}
const stackPanel = new flock.GUI.StackPanel();
stackPanel.name = "stackPanel";
stackPanel.horizontalAlignment =
flock.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
stackPanel.verticalAlignment =
flock.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM;
stackPanel.isVertical = true;
stackPanel.width = "100%";
stackPanel.adaptHeightToChildren = true;
stackPanel.resizeToFit = true;
stackPanel.forceResizeWidth = true;
stackPanel.forceResizeHeight = true;
stackPanel.spacing = 4;
advancedTexture.addControl(stackPanel);
} else {
advancedTexture = plane.advancedTexture;
}
const stackPanel = advancedTexture.getControlByName("stackPanel");
if (mode === "REPLACE") {
stackPanel.clearControls();
}
if (text) {
const bg = new flock.GUI.Rectangle("textBackground");
bg.background = flock.hexToRgba(backgroundColor, alpha);
bg.adaptWidthToChildren = true;
bg.adaptHeightToChildren = true;
bg.cornerRadius = 30;
bg.thickness = 0;
bg.resizeToFit = true;
bg.forceResizeWidth = true;
bg.checkCollisions = false;
bg.isPickable = false;
stackPanel.addControl(bg);
const scale = 8;
const textBlock = new flock.GUI.TextBlock();
textBlock.text = String(text);
textBlock.color = textColor;
textBlock.fontSize = size * scale;
textBlock.fontFamily = fontFamily;
textBlock.alpha = 1;
textBlock.textWrapping = flock.GUI.TextWrapping.WordWrap;
textBlock.resizeToFit = true;
textBlock.forceResizeWidth = true;
textBlock.paddingLeft = 50;
textBlock.paddingRight = 50;
textBlock.paddingTop = 20;
textBlock.paddingBottom = 20;
textBlock.textVerticalAlignment =
flock.GUI.Control.VERTICAL_ALIGNMENT_TOP;
textBlock.textHorizontalAlignment =
flock.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
bg.addControl(textBlock);
if (duration > 0) {
const timeoutId = setTimeout(function () {
stackPanel.removeControl(bg);
bg.dispose();
textBlock.dispose();
resolve();
}, duration * 1000);
flock.abortController.signal.addEventListener("abort", () => {
clearTimeout(timeoutId);
bg.dispose();
textBlock.dispose();
resolve(new Error("Action aborted"));
});
} else {
resolve();
}
} else {
resolve();
}
});
}
},
printText({
text,
duration = 30,
color = "white"
} = {}) {
if (!flock.scene || !flock.stackPanel) return;
console.log(text);
try {
// Create a rectangle container for the text
const bg = new flock.GUI.Rectangle("textBackground");
bg.background = "rgba(255, 255, 255, 0.5)";
bg.adaptWidthToChildren = true; // Adjust width to fit the text
bg.adaptHeightToChildren = true; // Adjust height to fit the text
bg.cornerRadius = 2; // Match the original corner rounding
bg.thickness = 0; // No border
bg.horizontalAlignment = flock.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT; // Align the container to the left
bg.verticalAlignment = flock.GUI.Control.VERTICAL_ALIGNMENT_TOP; // Align to the top
bg.left = "5px"; // Preserve original spacing
bg.top = "5px";
// Create the text block inside the rectangle
const textBlock = new flock.GUI.TextBlock("textBlock", text);
textBlock.color = color;
textBlock.fontSize = "20"; // Match the original font size
textBlock.fontFamily = fontFamily; // Retain original font
textBlock.height = "25px"; // Match the original height
textBlock.paddingLeft = "10px"; // Padding for left alignment
textBlock.paddingRight = "10px";
textBlock.paddingTop = "2px";
textBlock.paddingBottom = "2px";
textBlock.textHorizontalAlignment =
flock.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT; // Left align the text
textBlock.textVerticalAlignment =
flock.GUI.Control.VERTICAL_ALIGNMENT_CENTER; // Center vertically within the rectangle
textBlock.textWrapping = flock.GUI.TextWrapping.WordWrap; // Enable word wrap
textBlock.resizeToFit = true; // Allow resizing
textBlock.forceResizeWidth = true;
// Add the text block to the rectangle
bg.addControl(textBlock);
// Add the rectangle to the stack panel
flock.stackPanel.addControl(bg);
// Remove the text after the specified duration
const timeoutId = setTimeout(() => {
if (flock.scene) {
// Ensure the scene is still valid
flock.stackPanel.removeControl(bg);
}
}, duration * 1000);
// Handle cleanup in case of scene disposal
flock.abortController.signal.addEventListener("abort", () => {
clearTimeout(timeoutId);
});
} catch (error) {
console.warn("Unable to print text:", error);
}
},
};