@almadar/ui 5.76.2 → 5.76.4

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.
@@ -12447,14 +12447,318 @@ var init_useCanvasEffects = __esm({
12447
12447
  "use client";
12448
12448
  }
12449
12449
  });
12450
+ function pickPath(entry) {
12451
+ if (Array.isArray(entry.path)) {
12452
+ return entry.path[Math.floor(Math.random() * entry.path.length)];
12453
+ }
12454
+ return entry.path;
12455
+ }
12456
+ function useGameAudio({
12457
+ manifest,
12458
+ baseUrl = "",
12459
+ initialMuted = false,
12460
+ initialVolume = 1
12461
+ }) {
12462
+ const [muted, setMutedState] = useState(initialMuted);
12463
+ const [masterVolume, setMasterVolumeState] = useState(initialVolume);
12464
+ const mutedRef = useRef(muted);
12465
+ const volumeRef = useRef(masterVolume);
12466
+ const manifestRef = useRef(manifest);
12467
+ mutedRef.current = muted;
12468
+ volumeRef.current = masterVolume;
12469
+ manifestRef.current = manifest;
12470
+ const poolsRef = useRef(/* @__PURE__ */ new Map());
12471
+ const getOrCreateElement = useCallback((key) => {
12472
+ const entry = manifestRef.current[key];
12473
+ if (!entry) return null;
12474
+ let pool = poolsRef.current.get(key);
12475
+ if (!pool) {
12476
+ pool = [];
12477
+ poolsRef.current.set(key, pool);
12478
+ }
12479
+ const maxSize = entry.poolSize ?? 1;
12480
+ for (const audio of pool) {
12481
+ if (audio.paused && (audio.ended || audio.currentTime === 0)) {
12482
+ return audio;
12483
+ }
12484
+ }
12485
+ if (pool.length < maxSize) {
12486
+ const src = baseUrl + pickPath(entry);
12487
+ const audio = new Audio(src);
12488
+ audio.loop = entry.loop ?? false;
12489
+ pool.push(audio);
12490
+ return audio;
12491
+ }
12492
+ if (!entry.loop) {
12493
+ let oldest = pool[0];
12494
+ for (const audio of pool) {
12495
+ if (audio.currentTime > oldest.currentTime) {
12496
+ oldest = audio;
12497
+ }
12498
+ }
12499
+ oldest.pause();
12500
+ oldest.currentTime = 0;
12501
+ return oldest;
12502
+ }
12503
+ return null;
12504
+ }, [baseUrl]);
12505
+ const play = useCallback((key) => {
12506
+ if (mutedRef.current) return;
12507
+ const entry = manifestRef.current[key];
12508
+ if (!entry) return;
12509
+ const audio = getOrCreateElement(key);
12510
+ if (!audio) return;
12511
+ audio.volume = Math.min(1, (entry.volume ?? 1) * volumeRef.current);
12512
+ if (!entry.loop) {
12513
+ audio.currentTime = 0;
12514
+ }
12515
+ const promise = audio.play();
12516
+ if (promise) {
12517
+ promise.catch(() => {
12518
+ });
12519
+ }
12520
+ }, [getOrCreateElement]);
12521
+ const stop = useCallback((key) => {
12522
+ const pool = poolsRef.current.get(key);
12523
+ if (!pool) return;
12524
+ for (const audio of pool) {
12525
+ audio.pause();
12526
+ audio.currentTime = 0;
12527
+ }
12528
+ }, []);
12529
+ const currentMusicKeyRef = useRef(null);
12530
+ const currentMusicElRef = useRef(null);
12531
+ const musicFadeRef = useRef(null);
12532
+ const pendingMusicKeyRef = useRef(null);
12533
+ const clearMusicFade = useCallback(() => {
12534
+ if (musicFadeRef.current) {
12535
+ clearInterval(musicFadeRef.current);
12536
+ musicFadeRef.current = null;
12537
+ }
12538
+ }, []);
12539
+ const playMusic = useCallback((key) => {
12540
+ if (key === currentMusicKeyRef.current) return;
12541
+ pendingMusicKeyRef.current = key;
12542
+ const entry = manifestRef.current[key];
12543
+ if (!entry) return;
12544
+ const fadeDurationMs = entry.crossfadeDurationMs ?? 1500;
12545
+ const stepMs = 50;
12546
+ const totalSteps = Math.max(1, fadeDurationMs / stepMs);
12547
+ const targetVolume = Math.min(1, (entry.volume ?? 1) * volumeRef.current);
12548
+ clearMusicFade();
12549
+ const src = baseUrl + (Array.isArray(entry.path) ? entry.path[0] : entry.path);
12550
+ const incoming = new Audio(src);
12551
+ incoming.loop = true;
12552
+ incoming.volume = 0;
12553
+ const outgoing = currentMusicElRef.current;
12554
+ const outgoingStartVol = outgoing?.volume ?? 0;
12555
+ currentMusicKeyRef.current = key;
12556
+ currentMusicElRef.current = incoming;
12557
+ if (!mutedRef.current) {
12558
+ incoming.play().catch(() => {
12559
+ currentMusicKeyRef.current = null;
12560
+ currentMusicElRef.current = outgoing;
12561
+ });
12562
+ }
12563
+ let step = 0;
12564
+ musicFadeRef.current = setInterval(() => {
12565
+ step++;
12566
+ const progress = Math.min(step / totalSteps, 1);
12567
+ incoming.volume = Math.min(1, targetVolume * progress);
12568
+ if (outgoing) {
12569
+ outgoing.volume = Math.max(0, outgoingStartVol * (1 - progress));
12570
+ }
12571
+ if (progress >= 1) {
12572
+ clearMusicFade();
12573
+ if (outgoing) {
12574
+ outgoing.pause();
12575
+ outgoing.src = "";
12576
+ }
12577
+ }
12578
+ }, stepMs);
12579
+ }, [baseUrl, clearMusicFade]);
12580
+ const stopMusic = useCallback((fadeDurationMs = 1e3) => {
12581
+ const outgoing = currentMusicElRef.current;
12582
+ if (!outgoing) return;
12583
+ currentMusicKeyRef.current = null;
12584
+ currentMusicElRef.current = null;
12585
+ pendingMusicKeyRef.current = null;
12586
+ clearMusicFade();
12587
+ const startVolume = outgoing.volume;
12588
+ const stepMs = 50;
12589
+ const totalSteps = Math.max(1, fadeDurationMs / stepMs);
12590
+ let step = 0;
12591
+ musicFadeRef.current = setInterval(() => {
12592
+ step++;
12593
+ const progress = step / totalSteps;
12594
+ outgoing.volume = Math.max(0, startVolume * (1 - progress));
12595
+ if (progress >= 1) {
12596
+ clearMusicFade();
12597
+ outgoing.pause();
12598
+ outgoing.src = "";
12599
+ }
12600
+ }, stepMs);
12601
+ }, [clearMusicFade]);
12602
+ const stopAll = useCallback(() => {
12603
+ for (const pool of poolsRef.current.values()) {
12604
+ for (const audio of pool) {
12605
+ audio.pause();
12606
+ audio.currentTime = 0;
12607
+ }
12608
+ }
12609
+ stopMusic(0);
12610
+ }, [stopMusic]);
12611
+ const setMuted = useCallback((value) => {
12612
+ setMutedState(value);
12613
+ if (value) {
12614
+ for (const [key, pool] of poolsRef.current.entries()) {
12615
+ if (manifestRef.current[key]?.loop) {
12616
+ for (const audio of pool) {
12617
+ if (!audio.paused) audio.pause();
12618
+ }
12619
+ }
12620
+ }
12621
+ currentMusicElRef.current?.pause();
12622
+ } else {
12623
+ for (const [key, pool] of poolsRef.current.entries()) {
12624
+ const entry = manifestRef.current[key];
12625
+ if (entry?.loop && entry?.autostart) {
12626
+ for (const audio of pool) {
12627
+ if (audio.paused) audio.play().catch(() => {
12628
+ });
12629
+ }
12630
+ }
12631
+ }
12632
+ const musicEl = currentMusicElRef.current;
12633
+ if (musicEl) {
12634
+ musicEl.play().catch(() => {
12635
+ });
12636
+ }
12637
+ }
12638
+ }, []);
12639
+ const setMasterVolume = useCallback((volume) => {
12640
+ const clamped = Math.max(0, Math.min(1, volume));
12641
+ setMasterVolumeState(clamped);
12642
+ for (const [key, pool] of poolsRef.current.entries()) {
12643
+ const entryVol = manifestRef.current[key]?.volume ?? 1;
12644
+ for (const audio of pool) {
12645
+ audio.volume = Math.min(1, entryVol * clamped);
12646
+ }
12647
+ }
12648
+ if (!musicFadeRef.current && currentMusicElRef.current) {
12649
+ const key = currentMusicKeyRef.current;
12650
+ const entryVol = key ? manifestRef.current[key]?.volume ?? 1 : 1;
12651
+ currentMusicElRef.current.volume = Math.min(1, entryVol * clamped);
12652
+ }
12653
+ }, []);
12654
+ const unlockedRef = useRef(false);
12655
+ useEffect(() => {
12656
+ const autoKeys = Object.keys(manifest).filter((k) => manifest[k].autostart);
12657
+ const hasPendingMusic = () => pendingMusicKeyRef.current !== null;
12658
+ const hasAutoStart = autoKeys.length > 0;
12659
+ if (!hasAutoStart && !hasPendingMusic()) return;
12660
+ const unlock = () => {
12661
+ if (unlockedRef.current) return;
12662
+ unlockedRef.current = true;
12663
+ if (!mutedRef.current) {
12664
+ for (const key of autoKeys) {
12665
+ play(key);
12666
+ }
12667
+ const pending = pendingMusicKeyRef.current;
12668
+ if (pending && pending !== currentMusicKeyRef.current) {
12669
+ playMusic(pending);
12670
+ }
12671
+ }
12672
+ };
12673
+ document.addEventListener("click", unlock, { once: true });
12674
+ document.addEventListener("keydown", unlock, { once: true });
12675
+ document.addEventListener("touchstart", unlock, { once: true });
12676
+ return () => {
12677
+ document.removeEventListener("click", unlock);
12678
+ document.removeEventListener("keydown", unlock);
12679
+ document.removeEventListener("touchstart", unlock);
12680
+ };
12681
+ }, [manifest, play, playMusic]);
12682
+ useEffect(() => {
12683
+ return () => {
12684
+ clearMusicFade();
12685
+ for (const pool of poolsRef.current.values()) {
12686
+ for (const audio of pool) {
12687
+ audio.pause();
12688
+ audio.src = "";
12689
+ }
12690
+ }
12691
+ poolsRef.current.clear();
12692
+ if (currentMusicElRef.current) {
12693
+ currentMusicElRef.current.pause();
12694
+ currentMusicElRef.current.src = "";
12695
+ currentMusicElRef.current = null;
12696
+ }
12697
+ };
12698
+ }, [clearMusicFade]);
12699
+ return {
12700
+ play,
12701
+ stop,
12702
+ stopAll,
12703
+ playMusic,
12704
+ stopMusic,
12705
+ muted,
12706
+ setMuted,
12707
+ masterVolume,
12708
+ setMasterVolume
12709
+ };
12710
+ }
12450
12711
  var init_useGameAudio = __esm({
12451
12712
  "components/game/shared/hooks/useGameAudio.ts"() {
12452
12713
  "use client";
12714
+ useGameAudio.displayName = "useGameAudio";
12453
12715
  }
12454
12716
  });
12455
12717
  function useGameAudioContextOptional() {
12456
12718
  return useContext(GameAudioContext);
12457
12719
  }
12720
+ function GameAudioProvider({
12721
+ manifest,
12722
+ baseUrl = "",
12723
+ children,
12724
+ initialMuted = false
12725
+ }) {
12726
+ const eventBus = useEventBus();
12727
+ const { play, stop, stopAll, playMusic, stopMusic, muted, setMuted, masterVolume, setMasterVolume } = useGameAudio({ manifest, baseUrl, initialMuted });
12728
+ useEffect(() => {
12729
+ const unsubPlay = eventBus.on("UI:PLAY_SOUND", (event) => {
12730
+ const key = event.payload?.key;
12731
+ if (key) play(key);
12732
+ });
12733
+ const unsubStop = eventBus.on("UI:STOP_SOUND", (event) => {
12734
+ const key = event.payload?.key;
12735
+ if (key) {
12736
+ stop(key);
12737
+ } else {
12738
+ stopAll();
12739
+ }
12740
+ });
12741
+ const unsubChangeMusic = eventBus.on("UI:CHANGE_MUSIC", (event) => {
12742
+ const key = event.payload?.key;
12743
+ if (key) {
12744
+ playMusic(key);
12745
+ } else {
12746
+ stopMusic();
12747
+ }
12748
+ });
12749
+ const unsubStopMusic = eventBus.on("UI:STOP_MUSIC", () => {
12750
+ stopMusic();
12751
+ });
12752
+ return () => {
12753
+ unsubPlay();
12754
+ unsubStop();
12755
+ unsubChangeMusic();
12756
+ unsubStopMusic();
12757
+ };
12758
+ }, [eventBus, play, stop, stopAll, playMusic, stopMusic]);
12759
+ const value = { muted, setMuted, masterVolume, setMasterVolume, play, playMusic, stopMusic };
12760
+ return /* @__PURE__ */ jsx(GameAudioContext.Provider, { value, children });
12761
+ }
12458
12762
  var GameAudioContext;
12459
12763
  var init_GameAudioProvider = __esm({
12460
12764
  "components/game/shared/providers/GameAudioProvider.tsx"() {
@@ -12463,6 +12767,7 @@ var init_GameAudioProvider = __esm({
12463
12767
  init_useGameAudio();
12464
12768
  GameAudioContext = createContext(null);
12465
12769
  GameAudioContext.displayName = "GameAudioContext";
12770
+ GameAudioProvider.displayName = "GameAudioProvider";
12466
12771
  }
12467
12772
  });
12468
12773
  function GameAudioToggle({
@@ -50406,9 +50711,33 @@ var init_ToastSlot = __esm({
50406
50711
  ToastSlot.displayName = "ToastSlot";
50407
50712
  }
50408
50713
  });
50409
-
50410
- // components/core/organisms/component-registry.generated.ts
50411
- var COMPONENT_REGISTRY;
50714
+ function lazyThree(name, loader) {
50715
+ const Lazy = React105__default.lazy(
50716
+ () => loader().then((m) => {
50717
+ const Resolved = m[name];
50718
+ if (!Resolved) {
50719
+ throw new Error(
50720
+ `[@almadar/ui] 3D component "${name}" was not found in the three subpath bundle.`
50721
+ );
50722
+ }
50723
+ return { default: Resolved };
50724
+ })
50725
+ );
50726
+ function ThreeWrapper(props) {
50727
+ return React105__default.createElement(
50728
+ ThreeBoundary,
50729
+ { name },
50730
+ React105__default.createElement(
50731
+ React105__default.Suspense,
50732
+ { fallback: null },
50733
+ React105__default.createElement(Lazy, props)
50734
+ )
50735
+ );
50736
+ }
50737
+ ThreeWrapper.displayName = `Lazy(${name})`;
50738
+ return ThreeWrapper;
50739
+ }
50740
+ var ThreeBoundary, GameBoard3D, GameCanvas3D, GameCanvas3DBattleTemplate, GameCanvas3DCastleTemplate, GameCanvas3DWorldMapTemplate, COMPONENT_REGISTRY;
50412
50741
  var init_component_registry_generated = __esm({
50413
50742
  "components/core/organisms/component-registry.generated.ts"() {
50414
50743
  init_AboutPageTemplate();
@@ -50519,9 +50848,11 @@ var init_component_registry_generated = __esm({
50519
50848
  init_Form();
50520
50849
  init_FormField();
50521
50850
  init_FormSectionHeader();
50851
+ init_GameAudioProvider();
50522
50852
  init_GameAudioToggle();
50523
50853
  init_GameCard();
50524
50854
  init_GameHud();
50855
+ init_GameIcon();
50525
50856
  init_GameMenu();
50526
50857
  init_GameShell();
50527
50858
  init_GameTemplate();
@@ -50706,6 +51037,33 @@ var init_component_registry_generated = __esm({
50706
51037
  init_WizardProgress();
50707
51038
  init_WorldMapBoard();
50708
51039
  init_WorldMapTemplate();
51040
+ ThreeBoundary = class extends React105__default.Component {
51041
+ constructor() {
51042
+ super(...arguments);
51043
+ __publicField(this, "state", { failed: false });
51044
+ }
51045
+ static getDerivedStateFromError() {
51046
+ return { failed: true };
51047
+ }
51048
+ render() {
51049
+ if (this.state.failed) {
51050
+ return React105__default.createElement(
51051
+ "div",
51052
+ {
51053
+ "data-testid": "three-unavailable",
51054
+ style: { padding: 16, fontSize: 13, lineHeight: 1.5, opacity: 0.7 }
51055
+ },
51056
+ `3D pattern "${this.props.name}" requires three.js. Install the optional peers three + @react-three/fiber + @react-three/drei (matching the host React major) to render it.`
51057
+ );
51058
+ }
51059
+ return this.props.children;
51060
+ }
51061
+ };
51062
+ GameBoard3D = lazyThree("GameBoard3D", () => import('@almadar/ui/components/molecules/game/three'));
51063
+ GameCanvas3D = lazyThree("GameCanvas3D", () => import('@almadar/ui/components/molecules/game/three'));
51064
+ GameCanvas3DBattleTemplate = lazyThree("GameCanvas3DBattleTemplate", () => import('@almadar/ui/components/molecules/game/three'));
51065
+ GameCanvas3DCastleTemplate = lazyThree("GameCanvas3DCastleTemplate", () => import('@almadar/ui/components/molecules/game/three'));
51066
+ GameCanvas3DWorldMapTemplate = lazyThree("GameCanvas3DWorldMapTemplate", () => import('@almadar/ui/components/molecules/game/three'));
50709
51067
  COMPONENT_REGISTRY = {
50710
51068
  "AboutPageTemplate": AboutPageTemplate,
50711
51069
  "Accordion": Accordion,
@@ -50821,9 +51179,16 @@ var init_component_registry_generated = __esm({
50821
51179
  "Form": Form,
50822
51180
  "FormField": FormField,
50823
51181
  "FormSectionHeader": FormSectionHeader,
51182
+ "GameAudioProvider": GameAudioProvider,
50824
51183
  "GameAudioToggle": GameAudioToggle,
51184
+ "GameBoard3D": GameBoard3D,
51185
+ "GameCanvas3D": GameCanvas3D,
51186
+ "GameCanvas3DBattleTemplate": GameCanvas3DBattleTemplate,
51187
+ "GameCanvas3DCastleTemplate": GameCanvas3DCastleTemplate,
51188
+ "GameCanvas3DWorldMapTemplate": GameCanvas3DWorldMapTemplate,
50825
51189
  "GameCard": GameCard,
50826
51190
  "GameHud": GameHud,
51191
+ "GameIcon": GameIcon,
50827
51192
  "GameMenu": GameMenu,
50828
51193
  "GameShell": GameShell,
50829
51194
  "GameTemplate": GameTemplate,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@almadar/ui",
3
- "version": "5.76.2",
3
+ "version": "5.76.4",
4
4
  "description": "React UI components, hooks, and providers for Almadar",
5
5
  "type": "module",
6
6
  "sideEffects": [