@pretty-chitty/core 2.0.0 → 2.1.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.
Files changed (318) hide show
  1. package/README.md +8 -0
  2. package/dist/components/ActionLogDisplay.d.ts.map +1 -1
  3. package/dist/components/ActionLogDisplay.js +1 -1
  4. package/dist/components/ActionLogDisplay.js.map +1 -1
  5. package/dist/components/BottomBarButton.d.ts +3 -1
  6. package/dist/components/BottomBarButton.d.ts.map +1 -1
  7. package/dist/components/BottomBarButton.js +3 -2
  8. package/dist/components/BottomBarButton.js.map +1 -1
  9. package/dist/components/ContextGalleryDisplay.d.ts.map +1 -1
  10. package/dist/components/ContextGalleryDisplay.js +1 -1
  11. package/dist/components/ContextGalleryDisplay.js.map +1 -1
  12. package/dist/components/DemoWrapper.d.ts +30 -2
  13. package/dist/components/DemoWrapper.d.ts.map +1 -1
  14. package/dist/components/DemoWrapper.js +79 -36
  15. package/dist/components/DemoWrapper.js.map +1 -1
  16. package/dist/components/FullScreenGalleryDisplay.d.ts.map +1 -1
  17. package/dist/components/FullScreenGalleryDisplay.js +2 -2
  18. package/dist/components/FullScreenGalleryDisplay.js.map +1 -1
  19. package/dist/components/Gallery/BuiltItem.d.ts +6 -2
  20. package/dist/components/Gallery/BuiltItem.d.ts.map +1 -1
  21. package/dist/components/Gallery/BuiltItem.js +28 -9
  22. package/dist/components/Gallery/BuiltItem.js.map +1 -1
  23. package/dist/components/Gallery/GalleryController.d.ts +18 -0
  24. package/dist/components/Gallery/GalleryController.d.ts.map +1 -1
  25. package/dist/components/Gallery/GalleryController.js +67 -0
  26. package/dist/components/Gallery/GalleryController.js.map +1 -1
  27. package/dist/components/Gallery/GalleryViewer.d.ts +6 -1
  28. package/dist/components/Gallery/GalleryViewer.d.ts.map +1 -1
  29. package/dist/components/Gallery/GalleryViewer.js +48 -10
  30. package/dist/components/Gallery/GalleryViewer.js.map +1 -1
  31. package/dist/components/Gallery/LayoutManager.d.ts +6 -0
  32. package/dist/components/Gallery/LayoutManager.d.ts.map +1 -1
  33. package/dist/components/Gallery/LayoutManager.js +23 -3
  34. package/dist/components/Gallery/LayoutManager.js.map +1 -1
  35. package/dist/components/Gallery/constants.d.ts +2 -0
  36. package/dist/components/Gallery/constants.d.ts.map +1 -1
  37. package/dist/components/Gallery/constants.js +2 -0
  38. package/dist/components/Gallery/constants.js.map +1 -1
  39. package/dist/components/Gallery/registry.d.ts +34 -0
  40. package/dist/components/Gallery/registry.d.ts.map +1 -0
  41. package/dist/components/Gallery/registry.js +17 -0
  42. package/dist/components/Gallery/registry.js.map +1 -0
  43. package/dist/components/GameDesigner.d.ts.map +1 -1
  44. package/dist/components/GameDesigner.js +42 -10
  45. package/dist/components/GameDesigner.js.map +1 -1
  46. package/dist/components/InlineGalleryDisplay.d.ts.map +1 -1
  47. package/dist/components/InlineGalleryDisplay.js +2 -2
  48. package/dist/components/InlineGalleryDisplay.js.map +1 -1
  49. package/dist/components/LiveButton.d.ts.map +1 -1
  50. package/dist/components/LiveButton.js +5 -2
  51. package/dist/components/LiveButton.js.map +1 -1
  52. package/dist/components/MatchViewer.d.ts +6 -1
  53. package/dist/components/MatchViewer.d.ts.map +1 -1
  54. package/dist/components/MatchViewer.js +9 -3
  55. package/dist/components/MatchViewer.js.map +1 -1
  56. package/dist/components/Panel/MultiPanel.d.ts.map +1 -1
  57. package/dist/components/Panel/MultiPanel.js +18 -0
  58. package/dist/components/Panel/MultiPanel.js.map +1 -1
  59. package/dist/components/Panel/PanelContents.d.ts.map +1 -1
  60. package/dist/components/Panel/PanelContents.js +92 -23
  61. package/dist/components/Panel/PanelContents.js.map +1 -1
  62. package/dist/components/Panel/PanelTabStack.d.ts.map +1 -1
  63. package/dist/components/Panel/PanelTabStack.js +1 -1
  64. package/dist/components/Panel/PanelTabStack.js.map +1 -1
  65. package/dist/components/PanelSpark.d.ts.map +1 -1
  66. package/dist/components/PanelSpark.js +3 -2
  67. package/dist/components/PanelSpark.js.map +1 -1
  68. package/dist/components/Playground.d.ts.map +1 -1
  69. package/dist/components/Playground.js +10 -4
  70. package/dist/components/Playground.js.map +1 -1
  71. package/dist/components/PromptControls.d.ts.map +1 -1
  72. package/dist/components/PromptControls.js +4 -2
  73. package/dist/components/PromptControls.js.map +1 -1
  74. package/dist/components/SparkLineChart.d.ts.map +1 -1
  75. package/dist/components/SparkLineChart.js +22 -26
  76. package/dist/components/SparkLineChart.js.map +1 -1
  77. package/dist/components/TimeControlBar.d.ts.map +1 -1
  78. package/dist/components/TimeControlBar.js +5 -3
  79. package/dist/components/TimeControlBar.js.map +1 -1
  80. package/dist/components/TokenizedMarkdown.d.ts +23 -0
  81. package/dist/components/TokenizedMarkdown.d.ts.map +1 -0
  82. package/dist/components/TokenizedMarkdown.js +144 -0
  83. package/dist/components/TokenizedMarkdown.js.map +1 -0
  84. package/dist/components/TokenizedMessage.d.ts +6 -0
  85. package/dist/components/TokenizedMessage.d.ts.map +1 -1
  86. package/dist/components/TokenizedMessage.js +20 -19
  87. package/dist/components/TokenizedMessage.js.map +1 -1
  88. package/dist/components/TopBarPlayers.d.ts.map +1 -1
  89. package/dist/components/TopBarPlayers.js +1 -1
  90. package/dist/components/TopBarPlayers.js.map +1 -1
  91. package/dist/components/Tutorial/BrowserNarration.d.ts +55 -0
  92. package/dist/components/Tutorial/BrowserNarration.d.ts.map +1 -0
  93. package/dist/components/Tutorial/BrowserNarration.js +240 -0
  94. package/dist/components/Tutorial/BrowserNarration.js.map +1 -0
  95. package/dist/components/Tutorial/TutorialBox.d.ts +26 -0
  96. package/dist/components/Tutorial/TutorialBox.d.ts.map +1 -0
  97. package/dist/components/Tutorial/TutorialBox.js +239 -0
  98. package/dist/components/Tutorial/TutorialBox.js.map +1 -0
  99. package/dist/components/Tutorial/TutorialDevPanel.d.ts +9 -0
  100. package/dist/components/Tutorial/TutorialDevPanel.d.ts.map +1 -0
  101. package/dist/components/Tutorial/TutorialDevPanel.js +92 -0
  102. package/dist/components/Tutorial/TutorialDevPanel.js.map +1 -0
  103. package/dist/components/Tutorial/TutorialOverlay.d.ts +7 -0
  104. package/dist/components/Tutorial/TutorialOverlay.d.ts.map +1 -0
  105. package/dist/components/Tutorial/TutorialOverlay.js +1132 -0
  106. package/dist/components/Tutorial/TutorialOverlay.js.map +1 -0
  107. package/dist/components/Tutorial/targetRects.d.ts +67 -0
  108. package/dist/components/Tutorial/targetRects.d.ts.map +1 -0
  109. package/dist/components/Tutorial/targetRects.js +241 -0
  110. package/dist/components/Tutorial/targetRects.js.map +1 -0
  111. package/dist/game/Chit.d.ts +7 -0
  112. package/dist/game/Chit.d.ts.map +1 -1
  113. package/dist/game/Chit.js +20 -5
  114. package/dist/game/Chit.js.map +1 -1
  115. package/dist/game/ClientTimeState.d.ts +14 -0
  116. package/dist/game/ClientTimeState.d.ts.map +1 -1
  117. package/dist/game/ClientTimeState.js +17 -0
  118. package/dist/game/ClientTimeState.js.map +1 -1
  119. package/dist/game/Game.d.ts +6 -0
  120. package/dist/game/Game.d.ts.map +1 -1
  121. package/dist/game/GameTheme.d.ts +3 -0
  122. package/dist/game/GameTheme.d.ts.map +1 -1
  123. package/dist/game/GameTheme.js +2 -0
  124. package/dist/game/GameTheme.js.map +1 -1
  125. package/dist/game/Match.d.ts +8 -2
  126. package/dist/game/Match.d.ts.map +1 -1
  127. package/dist/game/Match.js +4 -1
  128. package/dist/game/Match.js.map +1 -1
  129. package/dist/game/Pick.d.ts +2 -0
  130. package/dist/game/Pick.d.ts.map +1 -1
  131. package/dist/game/Pick.js +17 -1
  132. package/dist/game/Pick.js.map +1 -1
  133. package/dist/game/PlayerPromptStatusChit.d.ts +8 -0
  134. package/dist/game/PlayerPromptStatusChit.d.ts.map +1 -1
  135. package/dist/game/PlayerPromptStatusChit.js +5 -1
  136. package/dist/game/PlayerPromptStatusChit.js.map +1 -1
  137. package/dist/game/Prompt.d.ts +15 -0
  138. package/dist/game/Prompt.d.ts.map +1 -1
  139. package/dist/game/Prompt.js +16 -0
  140. package/dist/game/Prompt.js.map +1 -1
  141. package/dist/game/Turn.d.ts +15 -4
  142. package/dist/game/Turn.d.ts.map +1 -1
  143. package/dist/game/Turn.js +173 -60
  144. package/dist/game/Turn.js.map +1 -1
  145. package/dist/game/TurnState.d.ts +12 -1
  146. package/dist/game/TurnState.d.ts.map +1 -1
  147. package/dist/game/TurnState.js +15 -3
  148. package/dist/game/TurnState.js.map +1 -1
  149. package/dist/game/ai/AiClientPrompts.d.ts +20 -3
  150. package/dist/game/ai/AiClientPrompts.d.ts.map +1 -1
  151. package/dist/game/ai/AiClientPrompts.js +42 -6
  152. package/dist/game/ai/AiClientPrompts.js.map +1 -1
  153. package/dist/game/ai/AiPlayerView.js +2 -0
  154. package/dist/game/ai/AiPlayerView.js.map +1 -1
  155. package/dist/game/clientTransport/ClientPrompts.d.ts +20 -1
  156. package/dist/game/clientTransport/ClientPrompts.d.ts.map +1 -1
  157. package/dist/game/clientTransport/ClientPrompts.js +30 -1
  158. package/dist/game/clientTransport/ClientPrompts.js.map +1 -1
  159. package/dist/game/clientTransport/ClientTime.d.ts +13 -0
  160. package/dist/game/clientTransport/ClientTime.d.ts.map +1 -1
  161. package/dist/game/clientTransport/ClientTime.js +51 -11
  162. package/dist/game/clientTransport/ClientTime.js.map +1 -1
  163. package/dist/game/tutorial/TutorialController.d.ts +300 -0
  164. package/dist/game/tutorial/TutorialController.d.ts.map +1 -0
  165. package/dist/game/tutorial/TutorialController.js +1182 -0
  166. package/dist/game/tutorial/TutorialController.js.map +1 -0
  167. package/dist/game/tutorial/TutorialRuntime.d.ts +89 -0
  168. package/dist/game/tutorial/TutorialRuntime.d.ts.map +1 -0
  169. package/dist/game/tutorial/TutorialRuntime.js +261 -0
  170. package/dist/game/tutorial/TutorialRuntime.js.map +1 -0
  171. package/dist/game/tutorial/TutorialTheme.d.ts +28 -0
  172. package/dist/game/tutorial/TutorialTheme.d.ts.map +1 -0
  173. package/dist/game/tutorial/TutorialTheme.js +39 -0
  174. package/dist/game/tutorial/TutorialTheme.js.map +1 -0
  175. package/dist/game/tutorial/allowed.d.ts +2 -0
  176. package/dist/game/tutorial/allowed.d.ts.map +1 -0
  177. package/dist/game/tutorial/allowed.js +119 -0
  178. package/dist/game/tutorial/allowed.js.map +1 -0
  179. package/dist/game/tutorial/checks.d.ts +58 -0
  180. package/dist/game/tutorial/checks.d.ts.map +1 -0
  181. package/dist/game/tutorial/checks.js +144 -0
  182. package/dist/game/tutorial/checks.js.map +1 -0
  183. package/dist/game/tutorial/createDemoMatch.d.ts +47 -0
  184. package/dist/game/tutorial/createDemoMatch.d.ts.map +1 -0
  185. package/dist/game/tutorial/createDemoMatch.js +67 -0
  186. package/dist/game/tutorial/createDemoMatch.js.map +1 -0
  187. package/dist/game/tutorial/defineTutorial.d.ts +353 -0
  188. package/dist/game/tutorial/defineTutorial.d.ts.map +1 -0
  189. package/dist/game/tutorial/defineTutorial.js +86 -0
  190. package/dist/game/tutorial/defineTutorial.js.map +1 -0
  191. package/dist/game/tutorial/runTutorial.d.ts +63 -0
  192. package/dist/game/tutorial/runTutorial.d.ts.map +1 -0
  193. package/dist/game/tutorial/runTutorial.js +273 -0
  194. package/dist/game/tutorial/runTutorial.js.map +1 -0
  195. package/dist/hooks/usePanelNavigation.d.ts +28 -0
  196. package/dist/hooks/usePanelNavigation.d.ts.map +1 -0
  197. package/dist/hooks/usePanelNavigation.js +36 -0
  198. package/dist/hooks/usePanelNavigation.js.map +1 -0
  199. package/dist/hooks/useTimeController.d.ts +2 -1
  200. package/dist/hooks/useTimeController.d.ts.map +1 -1
  201. package/dist/hooks/useTimeController.js +6 -2
  202. package/dist/hooks/useTimeController.js.map +1 -1
  203. package/dist/hooks/useTutorial.d.ts +48 -0
  204. package/dist/hooks/useTutorial.d.ts.map +1 -0
  205. package/dist/hooks/useTutorial.js +16 -0
  206. package/dist/hooks/useTutorial.js.map +1 -0
  207. package/dist/index.d.ts +36 -0
  208. package/dist/index.d.ts.map +1 -1
  209. package/dist/index.html +12 -0
  210. package/dist/index.js +19 -2
  211. package/dist/index.js.map +1 -1
  212. package/dist/main.0792d734cde2c688d5f7.hot-update.js +16991 -0
  213. package/dist/main.0792d734cde2c688d5f7.hot-update.js.map +1 -0
  214. package/dist/main.0792d734cde2c688d5f7.hot-update.json +1 -0
  215. package/dist/pretty-chitty.js +204513 -0
  216. package/dist/pretty-chitty.js.map +1 -0
  217. package/dist/rendering/CameraWrapperPerspective.d.ts +5 -0
  218. package/dist/rendering/CameraWrapperPerspective.d.ts.map +1 -1
  219. package/dist/rendering/CameraWrapperPerspective.js +16 -0
  220. package/dist/rendering/CameraWrapperPerspective.js.map +1 -1
  221. package/dist/rendering/ChitGalleryItemInstance.d.ts +1 -0
  222. package/dist/rendering/ChitGalleryItemInstance.d.ts.map +1 -1
  223. package/dist/rendering/ChitGalleryItemInstance.js +12 -0
  224. package/dist/rendering/ChitGalleryItemInstance.js.map +1 -1
  225. package/dist/rendering/ChitRenderInstance.d.ts +9 -1
  226. package/dist/rendering/ChitRenderInstance.d.ts.map +1 -1
  227. package/dist/rendering/ChitRenderInstance.js +28 -0
  228. package/dist/rendering/ChitRenderInstance.js.map +1 -1
  229. package/dist/rendering/RootChitRenderInstance.d.ts +2 -0
  230. package/dist/rendering/RootChitRenderInstance.d.ts.map +1 -1
  231. package/dist/rendering/RootChitRenderInstance.js +29 -0
  232. package/dist/rendering/RootChitRenderInstance.js.map +1 -1
  233. package/dist/utilities/Annotations.d.ts +15 -0
  234. package/dist/utilities/Annotations.d.ts.map +1 -1
  235. package/dist/utilities/Annotations.js +31 -7
  236. package/dist/utilities/Annotations.js.map +1 -1
  237. package/dist/utilities/CanvasStack/CanvasStack.d.ts +2 -0
  238. package/dist/utilities/CanvasStack/CanvasStack.d.ts.map +1 -1
  239. package/dist/utilities/CanvasStack/CanvasStack.js +4 -0
  240. package/dist/utilities/CanvasStack/CanvasStack.js.map +1 -1
  241. package/dist/utilities/TokenizedText.d.ts +6 -0
  242. package/dist/utilities/TokenizedText.d.ts.map +1 -1
  243. package/dist/utilities/TokenizedText.js.map +1 -1
  244. package/dist/utilities/zIndex.d.ts +1 -0
  245. package/dist/utilities/zIndex.d.ts.map +1 -1
  246. package/dist/utilities/zIndex.js +1 -0
  247. package/dist/utilities/zIndex.js.map +1 -1
  248. package/package.json +3 -2
  249. package/src/library/components/ActionLogDisplay.tsx +1 -0
  250. package/src/library/components/BottomBarButton.tsx +5 -0
  251. package/src/library/components/ContextGalleryDisplay.tsx +2 -0
  252. package/src/library/components/DemoWrapper.tsx +147 -44
  253. package/src/library/components/FullScreenGalleryDisplay.tsx +3 -0
  254. package/src/library/components/Gallery/BuiltItem.ts +30 -9
  255. package/src/library/components/Gallery/GalleryController.ts +72 -1
  256. package/src/library/components/Gallery/GalleryViewer.tsx +54 -10
  257. package/src/library/components/Gallery/LayoutManager.ts +30 -6
  258. package/src/library/components/Gallery/constants.ts +2 -0
  259. package/src/library/components/Gallery/registry.ts +44 -0
  260. package/src/library/components/GameDesigner.tsx +121 -12
  261. package/src/library/components/InlineGalleryDisplay.tsx +5 -0
  262. package/src/library/components/LiveButton.tsx +8 -5
  263. package/src/library/components/MatchViewer.tsx +14 -3
  264. package/src/library/components/Panel/MultiPanel.tsx +21 -0
  265. package/src/library/components/Panel/PanelContents.tsx +70 -1
  266. package/src/library/components/Panel/PanelTabStack.tsx +1 -0
  267. package/src/library/components/PanelSpark.tsx +9 -4
  268. package/src/library/components/Playground.tsx +48 -21
  269. package/src/library/components/PromptControls.tsx +7 -3
  270. package/src/library/components/SparkLineChart.tsx +23 -27
  271. package/src/library/components/TimeControlBar.tsx +5 -3
  272. package/src/library/components/TokenizedMarkdown.tsx +224 -0
  273. package/src/library/components/TokenizedMessage.tsx +28 -32
  274. package/src/library/components/TopBarPlayers.tsx +12 -9
  275. package/src/library/components/Tutorial/BrowserNarration.ts +233 -0
  276. package/src/library/components/Tutorial/TutorialBox.tsx +506 -0
  277. package/src/library/components/Tutorial/TutorialDevPanel.tsx +297 -0
  278. package/src/library/components/Tutorial/TutorialOverlay.tsx +1301 -0
  279. package/src/library/components/Tutorial/targetRects.ts +273 -0
  280. package/src/library/game/Chit.ts +21 -5
  281. package/src/library/game/ClientTimeState.ts +18 -0
  282. package/src/library/game/Game.ts +7 -0
  283. package/src/library/game/GameTheme.ts +4 -0
  284. package/src/library/game/Match.ts +12 -2
  285. package/src/library/game/Pick.ts +16 -1
  286. package/src/library/game/PlayerPromptStatusChit.ts +5 -1
  287. package/src/library/game/Prompt.ts +20 -0
  288. package/src/library/game/Turn.ts +179 -61
  289. package/src/library/game/TurnState.ts +26 -3
  290. package/src/library/game/ai/AI_GUIDE.md +10 -0
  291. package/src/library/game/ai/AiClientPrompts.ts +60 -11
  292. package/src/library/game/ai/AiPlayerView.ts +2 -0
  293. package/src/library/game/clientTransport/ClientPrompts.ts +31 -1
  294. package/src/library/game/clientTransport/ClientTime.ts +54 -13
  295. package/src/library/game/serialize.test.ts +275 -0
  296. package/src/library/game/tutorial/TUTORIAL_GUIDE.md +411 -0
  297. package/src/library/game/tutorial/TutorialController.test.ts +731 -0
  298. package/src/library/game/tutorial/TutorialController.ts +1286 -0
  299. package/src/library/game/tutorial/TutorialRuntime.ts +311 -0
  300. package/src/library/game/tutorial/TutorialTheme.ts +67 -0
  301. package/src/library/game/tutorial/allowed.ts +145 -0
  302. package/src/library/game/tutorial/checks.ts +186 -0
  303. package/src/library/game/tutorial/createDemoMatch.ts +115 -0
  304. package/src/library/game/tutorial/defineTutorial.ts +414 -0
  305. package/src/library/game/tutorial/runTutorial.ts +331 -0
  306. package/src/library/game/undo.test.ts +282 -0
  307. package/src/library/hooks/usePanelNavigation.tsx +65 -0
  308. package/src/library/hooks/useTimeController.tsx +6 -2
  309. package/src/library/hooks/useTutorial.tsx +76 -0
  310. package/src/library/index.ts +57 -2
  311. package/src/library/rendering/CameraWrapperPerspective.ts +17 -0
  312. package/src/library/rendering/ChitGalleryItemInstance.ts +12 -0
  313. package/src/library/rendering/ChitRenderInstance.ts +34 -0
  314. package/src/library/rendering/RootChitRenderInstance.ts +30 -1
  315. package/src/library/utilities/Annotations.ts +35 -7
  316. package/src/library/utilities/CanvasStack/CanvasStack.ts +6 -1
  317. package/src/library/utilities/TokenizedText.ts +6 -0
  318. package/src/library/utilities/zIndex.ts +1 -0
@@ -0,0 +1,1132 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
3
+ import { Box } from "@mui/material";
4
+ import useSize from "@react-hook/size";
5
+ import { useTutorial } from "../../hooks/useTutorial";
6
+ import { useClientPrompts, useTimeController, useTimeState } from "../../hooks/useTimeController";
7
+ import { usePlayerId } from "../../hooks/usePlayer";
8
+ import { useGameTheme } from "../../hooks/useGameTheme";
9
+ import { useTokenMap } from "../../hooks/useTokenMap";
10
+ import { usePanelNavigation } from "../../hooks/usePanelNavigation";
11
+ import { useEventChannelState } from "../../hooks/useEventChannelState";
12
+ import { TutorialController } from "../../game/tutorial/TutorialController";
13
+ import { EventChannel } from "../../utilities/EventChannel";
14
+ import { samePasses } from "../../game/ClockDetails";
15
+ import { resolveTutorialTheme } from "../../game/tutorial/TutorialTheme";
16
+ import { PickPrompt } from "../../game/Prompt";
17
+ import { Chit } from "../../game/Chit";
18
+ import { ZINDEX_TUTORIAL } from "../../utilities/zIndex";
19
+ import { requestSharedAnimationFrame } from "../../utilities/RequestSharedAnimationFrame";
20
+ import { BrowserNarration } from "./BrowserNarration";
21
+ import { rectCenter, resolveTarget, tableRects, toLocal, unionRects } from "./targetRects";
22
+ import { UI_ALIASES } from "../../game/tutorial/allowed";
23
+ import { TutorialBox } from "./TutorialBox";
24
+ import { useModalState } from "../../hooks/useModalState";
25
+ import { selectChits } from "../../game/tutorial/defineTutorial";
26
+ import { GalleryItemChitChildrenSource } from "../../game/GalleryItemChitChildrenSource";
27
+ import { GalleryItemRawSource } from "../../game/GalleryItemRawSource";
28
+ import { chitsToGalleryItems } from "../../utilities/GalleryItemConversion";
29
+ const RING_PADDING = 8;
30
+ /** The box keeps this far from the screen's sides */
31
+ const BOX_MARGIN = 16;
32
+ /** A ringed thing smaller than this (its longer side, in pixels, with the whole table shown) gets zoomed in on */
33
+ const ZOOM_BELOW_PX = 28;
34
+ /** ...until its longer side is this share of its panel's shorter side, or this many pixels: enough to read it,
35
+ * with its surroundings still in view (a zoom that fills the panel loses where the thing is) */
36
+ const ZOOM_TO_SHARE = 0.3;
37
+ const ZOOM_TO_MAX_PX = 140;
38
+ /** Full screen hides every other panel, so it is used only when it buys something the camera cannot: a ringed thing
39
+ * smaller than FOCUS_TARGET_PX that zooming in its own panel cannot bring to READABLE_PX (the panel is too small, or the
40
+ * camera's max zoom too low), and whose panel full screen would grow at least FOCUS_GAIN times. A board overview, a
41
+ * badge, anything readable in place stays in the normal layout, beside the rest of the table (the developer: "it should
42
+ * only go full screen when it'd gain something meaningful. otherwise you're losing context") */
43
+ const FOCUS_TARGET_PX = 24;
44
+ const READABLE_PX = 40;
45
+ const FOCUS_GAIN = 1.5;
46
+ /** A zoom the step asks for outright (`zoom: 1.6`) applies on a screen smaller than this (its shorter side: a phone) */
47
+ const SMALL_SCREEN_PX = 500;
48
+ /** The dim stays this long after nothing asks for it (a ring hidden by a tab slide, a sentence with no ring), so the
49
+ * gaps between rings do not blink it off and on */
50
+ const DIM_HOLD_MS = 400;
51
+ /** How long a ring that leapt in one frame stays where it was (see the ring tick) */
52
+ const JUMP_HOLD_MS = 120;
53
+ function sameFrame(a, b) {
54
+ return JSON.stringify(a) === JSON.stringify(b);
55
+ }
56
+ function overlapArea(a, b) {
57
+ const w = Math.min(a.right, b.right) - Math.max(a.left, b.left);
58
+ const h = Math.min(a.bottom, b.bottom) - Math.max(a.top, b.top);
59
+ return w > 0 && h > 0 ? w * h : 0;
60
+ }
61
+ const RING_BREATHING_ROOM = 20;
62
+ /** Arrows need room to be seen, so their targets push the box further away */
63
+ const ARROW_BREATHING_ROOM = 44;
64
+ /**
65
+ * How badly `box` hides the targets: the sum over targets of the fraction of each target (plus its
66
+ * breathing room) that the box covers. A small counter fully under the box counts as 1, the same as
67
+ * a whole card; the box's own size does not matter.
68
+ */
69
+ function coverage(box, targets) {
70
+ return targets.reduce((acc, { rect: t, room, weight }) => {
71
+ const padded = { left: t.left - room, top: t.top - room, right: t.right + room, bottom: t.bottom + room };
72
+ const area = Math.max(1, (padded.right - padded.left) * (padded.bottom - padded.top));
73
+ return acc + (overlapArea(box, padded) / area) * weight;
74
+ }, 0);
75
+ }
76
+ /** Every rect the overlay is drawing right now: the box must not sit on any of them */
77
+ function targetRects(frame) {
78
+ return frame.focus.flatMap((f) => [
79
+ ...f.rings.map((rect) => ({ rect, room: RING_BREATHING_ROOM, weight: 1 })),
80
+ ...f.arrows.map((rect) => ({ rect, room: ARROW_BREATHING_ROOM, weight: 3 })),
81
+ ]);
82
+ }
83
+ function sameAvoid(a, b) {
84
+ return (a.room === b.room &&
85
+ Math.abs(a.rect.left - b.rect.left) < 2 &&
86
+ Math.abs(a.rect.top - b.rect.top) < 2 &&
87
+ Math.abs(a.rect.right - b.rect.right) < 2 &&
88
+ Math.abs(a.rect.bottom - b.rect.bottom) < 2);
89
+ }
90
+ /**
91
+ * The 2D tutorial layer over the whole player: rings and arrows (SVG) that follow chits and UI, the
92
+ * draggable text box, and the controller that drives them. Renders nothing unless the match was
93
+ * started with a tutorial (see `TutorialProvider`).
94
+ */
95
+ export function TutorialOverlay() {
96
+ const tutorial = useTutorial();
97
+ if (!tutorial)
98
+ return null;
99
+ return _jsx(InnerOverlay, {});
100
+ }
101
+ function InnerOverlay() {
102
+ const tutorial = useTutorial();
103
+ const clientTime = useTimeController();
104
+ const clientPrompts = useClientPrompts();
105
+ const timeState = useTimeState();
106
+ const playerId = usePlayerId();
107
+ const gameTheme = useGameTheme();
108
+ const tokenMap = useTokenMap();
109
+ const navigation = usePanelNavigation();
110
+ const modalState = useModalState();
111
+ const theme = useMemo(() => resolveTutorialTheme(gameTheme, tutorial.spec.theme, tutorial.themeOverrides), [gameTheme, tutorial.spec.theme, tutorial.themeOverrides]);
112
+ const ref = useRef(null);
113
+ const [width, height] = useSize(ref);
114
+ const [rootChit] = useEventChannelState(clientTime.rootChit);
115
+ const [isLoading] = useEventChannelState(timeState.isLoading);
116
+ const [live] = useEventChannelState(timeState.live);
117
+ const [controller, setController] = useState();
118
+ const [started, setStarted] = useState(tutorial.autoStart);
119
+ const [dev] = useEventChannelState(tutorial.dev);
120
+ const [muted] = useEventChannelState(tutorial.muted);
121
+ // ---- the controller, built once the board exists -------------------------------------------
122
+ useEffect(() => {
123
+ if (!rootChit || controller)
124
+ return;
125
+ const view = {
126
+ root: () => clientTime.rootChit.value,
127
+ me: () => clientTime.rootChit.value?.players.find((p) => p.id === playerId),
128
+ prompt: () => {
129
+ const p = clientPrompts.currentPrompt.value;
130
+ return p instanceof PickPrompt ? p : undefined;
131
+ },
132
+ clock: () => clientTime.currentClock.value.clock,
133
+ find: (id) => clientTime.findChitUnsafe(id),
134
+ latestLog: () => clientTime.activeLog.value,
135
+ live: () => timeState.live.value,
136
+ onChange: (cb) => {
137
+ const unsubs = [
138
+ clientTime.currentClock.on(cb, false),
139
+ clientTime.rootChit.on(cb, false),
140
+ clientTime.activeLog.on(cb, false),
141
+ clientPrompts.currentPrompt.on(cb, false),
142
+ timeState.live.on(cb, false),
143
+ modalState.gallerySource.on(cb, false),
144
+ modalState.inlineGallerySource.on(cb, false),
145
+ ];
146
+ return () => unsubs.forEach((u) => u());
147
+ },
148
+ setGuard: (guard) => {
149
+ clientPrompts.guard = guard ? (_prompt, action) => guard(action) : undefined;
150
+ },
151
+ setUndo: (undo) => {
152
+ clientPrompts.undoOverride.value = undo;
153
+ },
154
+ galleries: () => describeGalleries(modalState),
155
+ setGallery: (command) => applyGalleryCommand(modalState, command, clientTime.rootChit.value, clientTime.rootChit.value?.players.find((p) => p.id === playerId)),
156
+ onBlocked: (cb) => clientPrompts.blocked.on((b) => b && cb(b.action), false),
157
+ // Poses change this client's copy of a chit only; the match never hears of them
158
+ pose: (chit, change) => clientTime.poseChit(chit, change),
159
+ unpose: (chit) => clientTime.restoreChit(chit),
160
+ // Holds only how far this board plays; the match runs on behind it
161
+ timeline: {
162
+ at: () => Math.max(clientTime.currentClock.value.clock, timeState.targetClock.value),
163
+ hold: (clock) => {
164
+ timeState.timelineHold.value = clock;
165
+ },
166
+ },
167
+ pressUi: (name) => {
168
+ const el = (UI_ALIASES[name] ?? [name])
169
+ .map((n) => document.querySelector(`[data-tutorial-target="${CSS.escape(n)}"]`))
170
+ .find((e) => e && e.getBoundingClientRect().width > 0);
171
+ if (!el)
172
+ return false;
173
+ // a target is often a wrapper around the actual control
174
+ const control = (el.matches("button, [role=button]") ? el : el.querySelector("button, [role=button]")) ?? el;
175
+ control.click();
176
+ return true;
177
+ },
178
+ settled: () => !timeState.isWaitingOnAnimations.value &&
179
+ clientTime.currentClock.value.clock >= clientTime.maxClock.value.clock,
180
+ setFastForward: (on) => {
181
+ timeState.animationSpeedOverrideMultiplier.value = on ? 0.05 : undefined;
182
+ },
183
+ };
184
+ const { snapshot, rewind, runtime } = tutorial;
185
+ const c = new TutorialController({
186
+ spec: tutorial.spec,
187
+ view,
188
+ runtime,
189
+ narration: new BrowserNarration(),
190
+ ai: tutorial.ai,
191
+ onEvent: tutorial.onEvent,
192
+ muted: tutorial.muted.value,
193
+ rate: readRate(),
194
+ history: snapshot && rewind
195
+ ? {
196
+ save: (stepId) => runtime.saveSnapshot(stepId, snapshot()),
197
+ restore: async (stepId, index) => {
198
+ // Watch the history live, then wait for the rerun (a new clock pass) to reach this view
199
+ if (!timeState.live.value)
200
+ timeState.live.value = true;
201
+ const before = clientTime.currentClock.value.pass;
202
+ // Skipping ahead shows the board as it is at that step, not each move on the way there:
203
+ // the view goes straight to the rerun's latest clock, with no time to animate
204
+ const ahead = index > (tutorial.controller.value?.frontier.value ?? -1);
205
+ const started = Date.now();
206
+ const unsubJump = ahead
207
+ ? clientTime.maxClock.on((max) => {
208
+ if (max.pass !== before)
209
+ timeState.targetClock.value = max.clock;
210
+ }, false)
211
+ : undefined;
212
+ if (ahead)
213
+ timeState.animationSpeedOverrideMultiplier.value = 0.001;
214
+ const done = (ok) => {
215
+ unsubJump?.();
216
+ if (ahead) {
217
+ timeState.animationSpeedOverrideMultiplier.value = undefined;
218
+ console.info(`[tutorial] skipped to step ${index + 1} in ${Date.now() - started} ms`);
219
+ }
220
+ return ok;
221
+ };
222
+ if (!(await rewind(stepId)))
223
+ return done(false);
224
+ const deadline = Date.now() + 8000;
225
+ while (Date.now() < deadline) {
226
+ const current = clientTime.currentClock.value;
227
+ const max = clientTime.maxClock.value;
228
+ if (ahead)
229
+ console.info("ZZTIME wait", Date.now() - started, current.clock, max.clock, current.pass === max.pass, [...(timeState.currentlyAnimating ?? [])].map((k) => k.replace(/\d{6,}/, "")).join(","));
230
+ if (current.pass !== before &&
231
+ current.clock === max.clock &&
232
+ samePasses(current, max) &&
233
+ !timeState.isWaitingOnAnimations.value) {
234
+ break;
235
+ }
236
+ await new Promise((resolve) => setTimeout(resolve, ahead ? 5 : 50));
237
+ }
238
+ return done(true);
239
+ },
240
+ }
241
+ : undefined,
242
+ });
243
+ setController(c);
244
+ tutorial.controller.value = c;
245
+ c.rate.on((rate) => writeRate(rate), false);
246
+ if (tutorial.autoStart) {
247
+ // After a rewind or a dev restart: the board is already there and the player already tapped once
248
+ setStarted(true);
249
+ c.start(tutorial.startAt, { paused: tutorial.startPaused });
250
+ }
251
+ }, [rootChit, controller, clientTime, clientPrompts, timeState, playerId, tutorial, modalState]);
252
+ useEffect(() => {
253
+ return () => {
254
+ controller?.dispose();
255
+ if (tutorial.controller.value === controller)
256
+ tutorial.controller.value = undefined;
257
+ };
258
+ }, [controller, tutorial]);
259
+ useEffect(() => {
260
+ controller?.setMuted(muted);
261
+ }, [controller, muted]);
262
+ // Tapping a control the next "do it" step asks for counts as moving on to that step. Listen in the
263
+ // capture phase so the step is entered (and its check baselined) before the control acts.
264
+ useEffect(() => {
265
+ if (!controller)
266
+ return;
267
+ const onClick = (e) => {
268
+ const el = e.target?.closest?.("[data-tutorial-target]");
269
+ const name = el?.getAttribute("data-tutorial-target");
270
+ if (name)
271
+ controller.usedUi(name);
272
+ };
273
+ document.addEventListener("click", onClick, true);
274
+ return () => document.removeEventListener("click", onClick, true);
275
+ }, [controller]);
276
+ // Keyboard: → next step (or, while a step waits for the player, makes its move for them), ← previous
277
+ // step, Space play/pause. Otherwise the same as the box's buttons. Before the start, → or Enter starts.
278
+ const startRef = useRef(() => undefined);
279
+ useEffect(() => {
280
+ if (!controller)
281
+ return;
282
+ const onKey = (e) => {
283
+ if (e.defaultPrevented || e.altKey || e.ctrlKey || e.metaKey || e.shiftKey)
284
+ return;
285
+ const el = e.target;
286
+ if (el && (el.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(el.tagName)))
287
+ return;
288
+ const status = controller.status.value;
289
+ if (status === "idle") {
290
+ if (e.key !== "ArrowRight" && e.key !== "Enter")
291
+ return;
292
+ startRef.current();
293
+ e.preventDefault();
294
+ return;
295
+ }
296
+ if (status === "done")
297
+ return;
298
+ if (e.key === "ArrowRight")
299
+ controller.forward();
300
+ else if (e.key === "ArrowLeft")
301
+ controller.back();
302
+ else if (e.key === " ") {
303
+ // a focused button would also take the space as a click
304
+ if (el?.tagName === "BUTTON")
305
+ el.blur();
306
+ controller.toggleNarration();
307
+ }
308
+ else
309
+ return;
310
+ e.preventDefault();
311
+ };
312
+ window.addEventListener("keydown", onKey);
313
+ return () => window.removeEventListener("keydown", onKey);
314
+ }, [controller]);
315
+ const onStart = useCallback(() => {
316
+ if (!controller || controller.status.value !== "idle")
317
+ return;
318
+ setStarted(true);
319
+ controller.start(tutorial.startAt);
320
+ }, [controller, tutorial.startAt]);
321
+ startRef.current = onStart;
322
+ // Skipping to a step (the dev panel) starts the tutorial too
323
+ useEffect(() => {
324
+ if (!controller)
325
+ return;
326
+ const update = () => {
327
+ if (controller.status.value !== "idle" || controller.rewinding.value)
328
+ setStarted(true);
329
+ };
330
+ const unsubs = [controller.status.on(update, false), controller.rewinding.on(update, false)];
331
+ return () => unsubs.forEach((u) => u());
332
+ }, [controller]);
333
+ // ---- per-frame target resolution ------------------------------------------------------------
334
+ const [focus] = useEventChannelState(controller?.focus ?? EMPTY_FOCUS);
335
+ const [status] = useEventChannelState(controller?.status ?? IDLE);
336
+ // Everything this step has pointed at so far: an earlier sentence's target stays relevant while
337
+ // the step is on screen, so the box keeps clear of it too. Keyed by the step the frame was drawn for.
338
+ const stepTargetsRef = useRef({ step: -1, targets: [] });
339
+ const [placement] = useEventChannelState(controller?.box ?? NO_BOX);
340
+ const [frame, setFrame] = useState({ focus: [], debug: [], step: -1 });
341
+ const frameRef = useRef(frame);
342
+ /** Rings that leapt in one frame, per focus entry: since when (see the ring tick) */
343
+ const jumpRef = useRef({ focus: undefined, since: [] });
344
+ const [boxSize, setBoxSize] = useState({ width: theme.boxWidth, height: 160 });
345
+ // Dragging a narrower box is still allowed; the drag is clamped to the viewer
346
+ const [boxPos, setBoxPos] = useState({ x: 16, y: 16 });
347
+ // The latest box rect, for the framing (an effect that outlives renders)
348
+ const boxPosRef = useRef(boxPos);
349
+ boxPosRef.current = boxPos;
350
+ const boxSizeRef = useRef(boxSize);
351
+ boxSizeRef.current = boxSize;
352
+ const userDraggedRef = useRef(false);
353
+ const targetRectRef = useRef(undefined);
354
+ const active = started && status !== "done" && live;
355
+ // Keep the box off the top and bottom bars: the player needs the prompt buttons the script points at
356
+ const safeTop = gameTheme.topBarHeight + 12;
357
+ const safeBottom = gameTheme.bottomBarHeight + 12;
358
+ /** Where the box may sit: inside the screen, clear of the top and bottom bars */
359
+ const clampBox = useCallback((p) => ({
360
+ x: Math.max(BOX_MARGIN, Math.min(width - boxSize.width - BOX_MARGIN, p.x)),
361
+ y: Math.max(safeTop, Math.min(height - boxSize.height - safeBottom, p.y)),
362
+ }), [width, height, boxSize.width, boxSize.height, safeTop, safeBottom]);
363
+ // A chit is on screen only if its panel is in front of its tab group: one behind still paints during
364
+ // a tab slide, from where it would be, and a ring drawn there points at the wrong panel
365
+ const shown = useCallback((chit) => {
366
+ if (!navigation)
367
+ return true;
368
+ const roots = new Set(navigation.panelRoots().map((c) => c.id));
369
+ let panel = chit;
370
+ while (panel && !roots.has(panel.id))
371
+ panel = panel.parent;
372
+ return !panel?.id || navigation.isFront(panel.id);
373
+ }, [navigation]);
374
+ // The box floats on every screen size; on a narrow one it just gets narrower
375
+ const boxWidth = Math.max(220, Math.min(theme.boxWidth, width - 32));
376
+ // Bring hidden targets on screen, and keep doing so for as long as the step has one: the panel
377
+ // stack flips to wherever the bots act, and a target that is off screen explains nothing
378
+ useEffect(() => {
379
+ if (!active || !navigation || !controller)
380
+ return;
381
+ let cancelled = false;
382
+ let revealing = false;
383
+ let revealedOnce = false;
384
+ const attempt = () => {
385
+ if (cancelled || revealing)
386
+ return;
387
+ const container = ref.current;
388
+ if (!container)
389
+ return;
390
+ const ctx = controller.context();
391
+ const root = ctx?.root;
392
+ const me = ctx?.me;
393
+ const hidden = new Set();
394
+ const visible = new Set();
395
+ const scroll = [];
396
+ focus.forEach((f) => {
397
+ if (f.reveal === false)
398
+ return;
399
+ const targets = [];
400
+ if (f.ring)
401
+ targets.push(f.ring);
402
+ if (f.arrow && f.arrow !== true)
403
+ targets.push(f.arrow);
404
+ targets.forEach((t) => {
405
+ const resolved = resolveTarget(t, root, me, container, shown, ctx?.find);
406
+ resolved.hidden.forEach((c) => hidden.add(c));
407
+ resolved.chits.forEach((c) => !resolved.hidden.includes(c) && visible.add(c));
408
+ scroll.push(...resolved.offscreenInGallery);
409
+ });
410
+ });
411
+ // Items scrolled out of an open gallery: bring the first one into view
412
+ if (scroll.length > 0)
413
+ scroll[0].gallery.scrollTo(scroll[0].chit);
414
+ // What shows stays in front: its tab group would otherwise slide off to wherever a bot just moved
415
+ visible.forEach((c) => navigation.keepFront(c));
416
+ if (hidden.size === 0)
417
+ return;
418
+ // Targets in two panels of one tab group cannot both show: switch once, not back and forth
419
+ if (visible.size > 0 && revealedOnce)
420
+ return;
421
+ revealedOnce = true;
422
+ revealing = true;
423
+ void navigation.revealChit([...hidden][0]).finally(() => {
424
+ revealing = false;
425
+ });
426
+ };
427
+ const first = setTimeout(attempt, 50);
428
+ const interval = setInterval(attempt, 400);
429
+ return () => {
430
+ cancelled = true;
431
+ clearTimeout(first);
432
+ clearInterval(interval);
433
+ };
434
+ }, [focus, active, navigation, controller, shown]);
435
+ // Frame what the step is about. When everything it rings on the table sits in one panel and is too small
436
+ // to read even with that panel's camera zoomed (see FOCUS_TARGET_PX), show that panel full screen (as
437
+ // zooming in does, with every panel as a tab); anything readable in place stays beside the rest of the
438
+ // table. Rings in another panel go back
439
+ // to the normal layout first, and full screen comes back only if that one needs it too: switching
440
+ // tabs inside full screen, sentence after sentence, flips panels with no context. Then, if the thing
441
+ // is still tiny, zoom the camera in on it, and keep that zoom while the step's later rings still show
442
+ // in it. Both are undone when no longer needed (not if the player zoomed or focused meanwhile: that is
443
+ // theirs). Only what is ringed counts, and only on the table (a gallery shows things at a readable
444
+ // size already).
445
+ const zoomRef = useRef(undefined);
446
+ const focusedByUsRef = useRef(undefined);
447
+ /** The step whose rings set the current framing */
448
+ const framedStepRef = useRef(-1);
449
+ // The framing moved the targets: the box's memory of where they were this step is out of date
450
+ const forgetTargets = () => {
451
+ stepTargetsRef.current.targets = [];
452
+ };
453
+ const unzoom = useCallback(() => {
454
+ const z = zoomRef.current;
455
+ zoomRef.current = undefined;
456
+ if (z && Math.abs(z.panel.cameraZoom - z.zoom) < 0.01) {
457
+ z.panel.focusCamera(z.panel.width / 2, z.panel.height / 2, 1);
458
+ forgetTargets();
459
+ }
460
+ // eslint-disable-next-line react-hooks/exhaustive-deps
461
+ }, []);
462
+ const unfocus = useCallback(() => {
463
+ const ours = focusedByUsRef.current;
464
+ focusedByUsRef.current = undefined;
465
+ if (ours && navigation?.focusedPanel())
466
+ void navigation.focusPanelOf(undefined);
467
+ }, [navigation]);
468
+ useEffect(() => {
469
+ if (!active || !controller) {
470
+ unzoom();
471
+ unfocus();
472
+ return;
473
+ }
474
+ let cancelled = false;
475
+ const ringed = () => {
476
+ const container = ref.current;
477
+ const ctx = controller.context();
478
+ const found = [];
479
+ const panels = new Map(); // panel root id → a ringed chit in it
480
+ let inGallery = false;
481
+ let asked; // a zoom the step asks for outright
482
+ if (!container || !navigation)
483
+ return { found, panels, inGallery, asked };
484
+ const roots = new Set(navigation.panelRoots().map((c) => c.id));
485
+ focus.forEach((f) => {
486
+ if (f.reveal === false || f.zoom === false || !f.ring)
487
+ return;
488
+ const t = f.ring;
489
+ if (typeof t !== "function" && !("chit" in t))
490
+ return; // a UI or screen target
491
+ if (typeof f.zoom === "number")
492
+ asked = Math.max(asked ?? 1, f.zoom);
493
+ const table = tableRects(t, ctx?.root, ctx?.me, container, shown);
494
+ inGallery || (inGallery = table.inGallery);
495
+ found.push(...table.rects);
496
+ for (const c of selectChits(typeof t === "function" ? t : t.chit, ctx?.root, ctx?.me)) {
497
+ let p = c;
498
+ while (p && !roots.has(p.id))
499
+ p = p.parent;
500
+ if (p?.id)
501
+ panels.set(p.id, c);
502
+ }
503
+ });
504
+ return { found, panels, inGallery, asked };
505
+ };
506
+ const zoomIn = () => {
507
+ const { found, inGallery, asked, panels } = ringed();
508
+ // Nothing to frame in this cue (only rings that flash by, or UI): keep the step's framing; a new
509
+ // step starts zoomed out
510
+ if (panels.size === 0 && !inGallery) {
511
+ if (controller.index.value !== framedStepRef.current)
512
+ unzoom();
513
+ return;
514
+ }
515
+ const sameStep = framedStepRef.current === controller.index.value;
516
+ framedStepRef.current = controller.index.value;
517
+ const panel = found[0]?.panel;
518
+ if (!panel || inGallery || found.some((f) => f.panel !== panel))
519
+ return unzoom();
520
+ // Only one panel is remembered as zoomed: moving the framing to another panel zooms the first back out, or
521
+ // it would stay zoomed in for good and its later rings land off-screen (Lisboa's Court card → Influence disc
522
+ // → Manuel's office)
523
+ if (zoomRef.current && zoomRef.current.panel !== panel)
524
+ unzoom();
525
+ const union = unionRects(found.map((f) => f.rect));
526
+ const topLeft = panel.convertCameraSpaceToScreenSpace(-1, 1);
527
+ const bottomRight = panel.convertCameraSpaceToScreenSpace(1, -1);
528
+ if (!topLeft || !bottomRight)
529
+ return;
530
+ // Later in the same step, a zoom whose view still shows every ring stays: re-framing each sentence is a
531
+ // camera that never stops moving
532
+ const inset = 8;
533
+ if (sameStep &&
534
+ zoomRef.current?.panel === panel &&
535
+ union.left >= topLeft.x + inset &&
536
+ union.right <= bottomRight.x - inset &&
537
+ union.top >= topLeft.y + inset &&
538
+ union.bottom <= bottomRight.y - inset)
539
+ return;
540
+ // its longer side with the whole table shown
541
+ const current = panel.cameraZoom;
542
+ const size = Math.max(union.right - union.left, union.bottom - union.top) / current;
543
+ // Where the thing should end up: the part of the panel the box leaves free (above or below it)
544
+ const bounds = ref.current.getBoundingClientRect();
545
+ const box = {
546
+ left: bounds.left + boxPosRef.current.x,
547
+ top: bounds.top + boxPosRef.current.y,
548
+ right: bounds.left + boxPosRef.current.x + boxSizeRef.current.width,
549
+ bottom: bounds.top + boxPosRef.current.y + boxSizeRef.current.height,
550
+ };
551
+ let bandTop = topLeft.y;
552
+ let bandBottom = bottomRight.y;
553
+ if (box.right > topLeft.x && box.left < bottomRight.x && box.bottom > topLeft.y && box.top < bottomRight.y) {
554
+ if (box.top - topLeft.y >= bottomRight.y - box.bottom)
555
+ bandBottom = Math.max(topLeft.y + 1, box.top);
556
+ else
557
+ bandTop = Math.min(bottomRight.y - 1, box.bottom);
558
+ }
559
+ const panelShort = Math.min(bottomRight.x - topLeft.x, bottomRight.y - topLeft.y);
560
+ // a zoom the step asks for is for small screens, like the full-screen panel
561
+ const smallScreen = Math.min(width, height) < SMALL_SCREEN_PX;
562
+ const fits = Math.min(0.85 * (bandBottom - bandTop), 0.85 * (bottomRight.x - topLeft.x)) / size;
563
+ const wanted = asked && smallScreen ? asked : size < ZOOM_BELOW_PX ? Math.min(Math.min(panelShort * ZOOM_TO_SHARE, ZOOM_TO_MAX_PX) / size, fits) : 1;
564
+ const zoom = Math.min(wanted, panel.cameraWrapper.cameraSpec.maxZoom);
565
+ if (zoom < 1.25)
566
+ return unzoom();
567
+ // focusCamera brings a point to the middle: pick the point that puts the thing in the free band
568
+ const c = rectCenter(union);
569
+ const shift = ((bandTop + bandBottom) / 2 - (topLeft.y + bottomRight.y) / 2) * (current / zoom);
570
+ panel.focusCamera(c.x - topLeft.x, c.y - shift - topLeft.y, zoom);
571
+ zoomRef.current = { panel, zoom: panel.cameraZoom };
572
+ forgetTargets();
573
+ };
574
+ const frame = async () => {
575
+ if (cancelled || !navigation)
576
+ return;
577
+ const { panels, inGallery } = ringed();
578
+ // A new step with nothing on the table (a button, the box alone): full screen we chose goes
579
+ if (panels.size === 0 &&
580
+ !inGallery &&
581
+ focusedByUsRef.current &&
582
+ navigation.focusedPanel() &&
583
+ controller.index.value !== framedStepRef.current) {
584
+ unzoom();
585
+ focusedByUsRef.current = undefined;
586
+ await navigation.focusPanelOf(undefined);
587
+ forgetTargets();
588
+ }
589
+ if (!inGallery && panels.size > 1 && navigation.focusedPanel()) {
590
+ // the step needs more than one panel: the normal layout shows them side by side
591
+ unzoom();
592
+ focusedByUsRef.current = undefined;
593
+ await navigation.focusPanelOf(undefined);
594
+ forgetTargets();
595
+ }
596
+ else if (!inGallery && panels.size === 1) {
597
+ const [[, chit]] = [...panels];
598
+ // Full screen we chose for another panel: back to the normal layout, then decide afresh from there
599
+ const ours = focusedByUsRef.current;
600
+ if (ours && navigation.focusedPanel() && ours.renderInstance?.rootRenderInstance !== chit.renderInstance?.rootRenderInstance) {
601
+ unzoom();
602
+ focusedByUsRef.current = undefined;
603
+ await navigation.focusPanelOf(undefined);
604
+ forgetTargets();
605
+ if (cancelled)
606
+ return;
607
+ }
608
+ if (navigation.focusedPanel()) {
609
+ // full screen already (the player's, or ours for this panel): switch to its tab
610
+ await navigation.focusPanelOf(chit);
611
+ forgetTargets();
612
+ }
613
+ else {
614
+ const panel = chit.renderInstance?.rootRenderInstance;
615
+ const union = unionRects(ringed().found.map((f) => f.rect));
616
+ if (panel && union && width && height) {
617
+ const size = Math.max(union.right - union.left, union.bottom - union.top) / panel.cameraZoom;
618
+ const zoomable = Math.min(Math.min(panel.width, panel.height) * ZOOM_TO_SHARE, size * panel.cameraWrapper.cameraSpec.maxZoom);
619
+ const gain = Math.min(width / panel.width, height / panel.height);
620
+ if (size < FOCUS_TARGET_PX && zoomable < READABLE_PX && gain >= FOCUS_GAIN) {
621
+ await navigation.focusPanelOf(chit);
622
+ focusedByUsRef.current = chit;
623
+ forgetTargets();
624
+ }
625
+ }
626
+ }
627
+ }
628
+ if (!cancelled)
629
+ zoomIn();
630
+ };
631
+ // After a tab switch has slid in (see the reveal above), and the zoom once more in case it was slow
632
+ const first = setTimeout(() => void frame(), 450);
633
+ const again = setTimeout(() => !cancelled && zoomIn(), 1500);
634
+ return () => {
635
+ cancelled = true;
636
+ clearTimeout(first);
637
+ clearTimeout(again);
638
+ };
639
+ }, [focus, active, controller, shown, unzoom, unfocus, navigation, width, height]);
640
+ // While the player only watches, the prompt's own strip (the gallery its toggle button opens by
641
+ // itself) is put away, so the board gets the room; it comes back when it is the player's move or
642
+ // the step points into it. A strip the player opens themselves stays open.
643
+ const putAwayRef = useRef(undefined);
644
+ useEffect(() => {
645
+ if (!controller || !active)
646
+ return;
647
+ const promptStrips = () => {
648
+ const p = clientPrompts.currentPrompt.value;
649
+ if (!(p instanceof PickPrompt))
650
+ return [];
651
+ return p.picks.flatMap((k) => {
652
+ // a chit pick's toggle button (Hand, Actions...), when it opens its gallery by itself
653
+ const button = k.button;
654
+ return button?.autoShow && button.galleryItemSource ? [button.galleryItemSource] : [];
655
+ });
656
+ };
657
+ const playersMove = () => {
658
+ const allow = controller.step?.allow;
659
+ const list = allow === undefined ? [] : Array.isArray(allow) ? allow : [allow];
660
+ return controller.status.value === "waiting" && !controller.reviewing.value && list.length > 0;
661
+ };
662
+ const pointsInto = (source) => {
663
+ const ids = new Set(describeSource(source, "inline").chitIds);
664
+ const ctx = controller.context();
665
+ return controller.focus.value.some((f) => [f.ring, f.arrow].some((t) => {
666
+ if (!t || t === true)
667
+ return false;
668
+ if (typeof t !== "function" && "ui" in t) {
669
+ const name = typeof t.ui === "function" ? (ctx ? t.ui(ctx.root, ctx.me) : "") : t.ui;
670
+ return typeof name === "string" && (name.startsWith("gallery:") || name.startsWith("button:"));
671
+ }
672
+ if (typeof t !== "function" && !("chit" in t))
673
+ return false;
674
+ const selector = typeof t === "function" ? t : t.chit;
675
+ return selectChits(selector, ctx?.root, ctx?.me).some((c) => ids.has(c.id ?? ""));
676
+ }));
677
+ };
678
+ // Inline or full screen, as the player prefers
679
+ const channels = [modalState.inlineGallerySource, modalState.gallerySource];
680
+ const update = () => {
681
+ const strips = promptStrips();
682
+ const away = putAwayRef.current;
683
+ if (away && !strips.includes(away.source))
684
+ putAwayRef.current = undefined; // its prompt is gone
685
+ for (const channel of channels) {
686
+ const open = channel.value;
687
+ if (open && strips.includes(open) && !playersMove() && !pointsInto(open)) {
688
+ putAwayRef.current = { source: open, channel };
689
+ channel.value = undefined;
690
+ }
691
+ }
692
+ const back = putAwayRef.current;
693
+ if (back && !channels.some((c) => c.value) && (playersMove() || pointsInto(back.source))) {
694
+ back.channel.value = back.source;
695
+ putAwayRef.current = undefined;
696
+ }
697
+ };
698
+ update();
699
+ const unsubs = [
700
+ controller.status.on(update, false),
701
+ controller.focus.on(update, false),
702
+ controller.index.on(update, false),
703
+ clientPrompts.currentPrompt.on(update, false),
704
+ ];
705
+ return () => unsubs.forEach((u) => u());
706
+ }, [controller, active, clientPrompts, modalState]);
707
+ // Project every target each frame while something is drawn; publish a new frame only when it changed
708
+ useEffect(() => {
709
+ if (!controller || (!active && !dev.showTargetRects)) {
710
+ if (frameRef.current.focus.length || frameRef.current.debug.length) {
711
+ frameRef.current = { focus: [], debug: [], step: -1 };
712
+ setFrame(frameRef.current);
713
+ }
714
+ return;
715
+ }
716
+ let cancelled = false;
717
+ const tick = () => {
718
+ if (cancelled)
719
+ return;
720
+ requestSharedAnimationFrame(tick);
721
+ const container = ref.current;
722
+ if (!container)
723
+ return;
724
+ const bounds = container.getBoundingClientRect();
725
+ const ctx = controller.context();
726
+ const root = ctx?.root;
727
+ const me = ctx?.me;
728
+ const drawn = [];
729
+ const debug = [];
730
+ let firstRing;
731
+ focus.forEach((f) => {
732
+ let rings = [];
733
+ if (f.ring) {
734
+ const resolved = resolveTarget(f.ring, root, me, container, shown, ctx?.find);
735
+ const local = resolved.rects.map((r) => toLocal(r, bounds));
736
+ if (f.separate) {
737
+ rings = local;
738
+ }
739
+ else {
740
+ const union = unionRects(local);
741
+ rings = union ? [union] : [];
742
+ }
743
+ debug.push(...local);
744
+ }
745
+ const ring = rings[0];
746
+ let arrows = [];
747
+ if (f.arrow === true) {
748
+ arrows = rings;
749
+ }
750
+ else if (f.arrow) {
751
+ const resolved = resolveTarget(f.arrow, root, me, container, shown, ctx?.find);
752
+ arrows = resolved.rects.map((r) => toLocal(r, bounds));
753
+ debug.push(...arrows);
754
+ }
755
+ if (ring && !firstRing)
756
+ firstRing = ring;
757
+ if (!firstRing && arrows[0])
758
+ firstRing = unionRects(arrows);
759
+ drawn.push({ rings, shape: f.shape ?? "rect", arrows, dim: !!f.dim });
760
+ });
761
+ // A ring that leaps across the screen in one frame, for the same focus, is the board mid-switch (a
762
+ // panel reporting where it is going a frame before it gets there): keep it where it was until
763
+ // the new place has lasted a moment. Zooms and slides move smoothly and are never held back.
764
+ const jumps = jumpRef.current;
765
+ if (jumps.focus !== focus)
766
+ jumpRef.current = { focus, since: [] };
767
+ else {
768
+ const now = performance.now();
769
+ const far = 0.2 * Math.min(bounds.width, bounds.height);
770
+ drawn.forEach((d, i) => {
771
+ var _a;
772
+ const before = frameRef.current.focus[i];
773
+ const moved = before && before.rings.length === d.rings.length && d.rings.length > 0
774
+ ? Math.max(...d.rings.map((r, k) => {
775
+ const a = rectCenter(r), b = rectCenter(before.rings[k]);
776
+ return Math.hypot(a.x - b.x, a.y - b.y);
777
+ }))
778
+ : 0;
779
+ if (moved <= far) {
780
+ jumps.since[i] = undefined;
781
+ return;
782
+ }
783
+ (_a = jumps.since)[i] ?? (_a[i] = now);
784
+ if (now - jumps.since[i] < JUMP_HOLD_MS)
785
+ drawn[i] = before;
786
+ else
787
+ jumps.since[i] = undefined;
788
+ });
789
+ }
790
+ targetRectRef.current = firstRing;
791
+ const next = { focus: drawn, debug: dev.showTargetRects ? debug : [], step: controller.index.value };
792
+ if (!sameFrame(next, frameRef.current)) {
793
+ frameRef.current = next;
794
+ setFrame(next);
795
+ }
796
+ };
797
+ tick();
798
+ return () => {
799
+ cancelled = true;
800
+ };
801
+ }, [controller, focus, active, dev.showTargetRects, shown]);
802
+ // ---- box placement --------------------------------------------------------------------------
803
+ useEffect(() => {
804
+ userDraggedRef.current = false;
805
+ }, [placement]);
806
+ useEffect(() => {
807
+ const container = ref.current;
808
+ if (!container || width === 0)
809
+ return;
810
+ if (stepTargetsRef.current.step !== frame.step) {
811
+ stepTargetsRef.current = { step: frame.step, targets: [] };
812
+ }
813
+ const seen = stepTargetsRef.current.targets;
814
+ targetRects(frame).forEach((t) => {
815
+ if (!seen.some((s) => sameAvoid(s, t)))
816
+ seen.push(t);
817
+ });
818
+ const targetsNow = seen;
819
+ const rectNow = {
820
+ left: boxPos.x,
821
+ top: boxPos.y,
822
+ right: boxPos.x + boxSize.width,
823
+ bottom: boxPos.y + boxSize.height,
824
+ };
825
+ const margin = BOX_MARGIN;
826
+ const bw = boxSize.width;
827
+ const bh = boxSize.height;
828
+ const clamp = clampBox;
829
+ // A dragged box stays put unless a target shows up underneath it (but stays on screen when it
830
+ // grows or the screen turns)
831
+ if (userDraggedRef.current && coverage(rectNow, targetsNow) <= 0.1) {
832
+ const kept = clamp(boxPos);
833
+ if (Math.abs(kept.x - boxPos.x) > 1 || Math.abs(kept.y - boxPos.y) > 1)
834
+ setBoxPos(kept);
835
+ return;
836
+ }
837
+ const place = (p) => {
838
+ const anchor = p?.anchor ?? "top-right";
839
+ switch (anchor) {
840
+ case "top-left":
841
+ return { x: margin, y: safeTop };
842
+ case "top":
843
+ return { x: (width - bw) / 2, y: safeTop };
844
+ case "top-right":
845
+ return { x: width - bw - margin, y: safeTop };
846
+ case "bottom-left":
847
+ return { x: margin, y: height - bh - safeBottom };
848
+ case "bottom":
849
+ return { x: (width - bw) / 2, y: height - bh - safeBottom };
850
+ case "bottom-right":
851
+ return { x: width - bw - margin, y: height - bh - safeBottom };
852
+ case "center":
853
+ return { x: (width - bw) / 2, y: (height - bh) / 2 };
854
+ case "keep":
855
+ return boxPos;
856
+ case "near": {
857
+ const bounds = container.getBoundingClientRect();
858
+ const ctx = controller?.context();
859
+ const resolved = resolveTarget(p.target, ctx?.root, ctx?.me, container, shown);
860
+ const target = unionRects(resolved.rects.map((r) => toLocal(r, bounds)));
861
+ if (!target)
862
+ return boxPos;
863
+ const gap = 24;
864
+ const side = p.side ??
865
+ (target.bottom + gap + bh < height ? "below" : target.top - gap - bh > 0 ? "above" : "right");
866
+ const c = rectCenter(target);
867
+ switch (side) {
868
+ case "below":
869
+ return { x: c.x - bw / 2, y: target.bottom + gap };
870
+ case "above":
871
+ return { x: c.x - bw / 2, y: target.top - gap - bh };
872
+ case "left":
873
+ return { x: target.left - gap - bw, y: c.y - bh / 2 };
874
+ default:
875
+ return { x: target.right + gap, y: c.y - bh / 2 };
876
+ }
877
+ }
878
+ }
879
+ };
880
+ const targets = targetsNow;
881
+ const rectAt = (p) => ({ left: p.x, top: p.y, right: p.x + bw, bottom: p.y + bh });
882
+ // The start card sits in the middle of the darkened table
883
+ let next = clamp(place(started ? placement : { anchor: "center" }));
884
+ // Never sit on what the step is pointing at: if the requested spot covers a target, take the
885
+ // corner that covers the least (the current spot wins ties so the box does not wander)
886
+ if (coverage(rectAt(next), targets) > 0.1) {
887
+ const candidates = ["top-left", "top-right", "bottom-left", "bottom-right", "top", "bottom"];
888
+ let best = { pos: boxPos, cover: coverage(rectAt(boxPos), targets) };
889
+ for (const anchor of candidates) {
890
+ const pos = clamp(place({ anchor }));
891
+ const cover = coverage(rectAt(pos), targets);
892
+ if (cover < best.cover - 0.02)
893
+ best = { pos, cover };
894
+ }
895
+ next = best.pos;
896
+ }
897
+ if (Math.abs(next.x - boxPos.x) > 1 || Math.abs(next.y - boxPos.y) > 1) {
898
+ setBoxPos(next);
899
+ }
900
+ // eslint-disable-next-line react-hooks/exhaustive-deps
901
+ }, [placement, width, height, boxSize.width, boxSize.height, frame, controller, safeTop, safeBottom, clampBox, shown, started]);
902
+ // Dragging keeps the box on screen too: a box dragged off it could not be dragged back
903
+ const clampRef = useRef(clampBox);
904
+ clampRef.current = clampBox;
905
+ const onDrag = useCallback((pos) => {
906
+ userDraggedRef.current = true;
907
+ setBoxPos(clampRef.current(pos));
908
+ }, []);
909
+ const onMeasure = useCallback((size) => {
910
+ setBoxSize((prev) => (prev.width === size.width && prev.height === size.height ? prev : size));
911
+ }, []);
912
+ const wantDim = frame.focus.some((f) => f.dim) && frame.focus.some((f) => f.rings.length > 0 || f.arrows.length > 0);
913
+ const litNow = frame.focus.flatMap((f) => [
914
+ ...f.rings.map((rect) => ({ rect, shape: f.shape })),
915
+ ...f.arrows.map((rect) => ({ rect, shape: "rect" })),
916
+ ]);
917
+ const litRef = useRef(litNow);
918
+ if (wantDim)
919
+ litRef.current = litNow;
920
+ const [anyDim, setAnyDim] = useState(false);
921
+ useEffect(() => {
922
+ if (wantDim) {
923
+ setAnyDim(true);
924
+ return;
925
+ }
926
+ const t = setTimeout(() => setAnyDim(false), DIM_HOLD_MS);
927
+ return () => clearTimeout(t);
928
+ }, [wantDim]);
929
+ if (!controller) {
930
+ return _jsx(Box, { ref: ref, sx: { position: "absolute", inset: 0, pointerEvents: "none", zIndex: ZINDEX_TUTORIAL } });
931
+ }
932
+ const boxRect = {
933
+ left: boxPos.x,
934
+ top: boxPos.y,
935
+ right: boxPos.x + boxSize.width,
936
+ bottom: boxPos.y + boxSize.height,
937
+ };
938
+ const hasNarration = !!controller.step?.audio || controller.spec.steps.some((s) => !!s.audio);
939
+ // Once the tutorial is finished or skipped the box goes away and the match is just a game
940
+ const showBox = !isLoading && (live || !started) && status !== "done";
941
+ // A spotlight keeps everything the step highlights lit: every ring and every arrow target. Through a short
942
+ // gap (see DIM_HOLD_MS) the dim stays, with the holes it last had
943
+ const lit = anyDim && !wantDim ? litRef.current : litNow;
944
+ return (_jsxs(Box, { ref: ref, sx: {
945
+ position: "absolute",
946
+ inset: 0,
947
+ pointerEvents: "none",
948
+ zIndex: ZINDEX_TUTORIAL,
949
+ overflow: "hidden",
950
+ fontFamily: theme.fontFamily,
951
+ }, children: [active && (_jsxs("svg", { width: width, height: height, style: { position: "absolute", inset: 0, pointerEvents: "none" }, children: [_jsxs("defs", { children: [_jsx("marker", { id: "tutorial-arrowhead", viewBox: "0 0 10 10", refX: "8", refY: "5", markerWidth: "7", markerHeight: "7", orient: "auto-start-reverse", children: _jsx("path", { d: "M 0 0 L 10 5 L 0 10 z", fill: theme.arrowColor }) }), _jsxs("filter", { id: "tutorial-glow", filterUnits: "userSpaceOnUse", x: 0, y: 0, width: width, height: height, children: [_jsx("feGaussianBlur", { stdDeviation: "4", result: "blur" }), _jsxs("feMerge", { children: [_jsx("feMergeNode", { in: "blur" }), _jsx("feMergeNode", { in: "SourceGraphic" })] })] }), _jsxs("mask", { id: "tutorial-dim-mask", children: [_jsx("rect", { x: 0, y: 0, width: width, height: height, fill: "white" }), lit.map((l, i) => (_jsx(RingShape, { rect: l.rect, shape: l.shape, pad: RING_PADDING + 4, fill: "black" }, i))), _jsx("rect", { x: boxRect.left - 4, y: boxRect.top - 4, width: boxRect.right - boxRect.left + 8, height: boxRect.bottom - boxRect.top + 8, rx: theme.borderRadius, fill: "black" })] })] }), _jsx("rect", { x: 0, y: 0, width: width, height: height, fill: theme.dimColor, opacity: anyDim ? theme.dimOpacity : 0, mask: "url(#tutorial-dim-mask)", style: { transition: "opacity 0.3s" } }), _jsx("style", { children: `
952
+ @keyframes tutorialRingPulse { 0% { stroke-opacity: 0.55; } 50% { stroke-opacity: 1; } 100% { stroke-opacity: 0.55; } }
953
+ @keyframes tutorialDash { to { stroke-dashoffset: -24; } }
954
+ @keyframes tutorialArrowNudge { 0% { transform: translate(0,0); } 50% { transform: translate(var(--nx), var(--ny)); } 100% { transform: translate(0,0); } }
955
+ ` }), frame.debug.map((r, i) => (_jsx("rect", { x: r.left, y: r.top, width: r.right - r.left, height: r.bottom - r.top, fill: "none", stroke: "#ff00ff", strokeWidth: 1, strokeDasharray: "3 3" }, `d${i}`))), frame.focus.map((f, i) => (_jsxs("g", { children: [f.rings.map((ring, j) => (_jsx(RingShape, { rect: ring, shape: f.shape, pad: ringPadding(ring, f.rings), fill: "none", stroke: theme.highlightColor, strokeWidth: 3, filter: "url(#tutorial-glow)", style: { animation: "tutorialRingPulse 1.6s ease-in-out infinite" } }, j))), f.arrows.map((target, j) => (_jsx(Arrow, { from: boxRect, to: target, color: theme.arrowColor }, j)))] }, i)))] })), showBox && !started && (_jsx(Box, { sx: { position: "absolute", inset: 0, pointerEvents: "auto", background: theme.dimColor, opacity: theme.dimOpacity } })), showBox && (_jsx(TutorialBox, { controller: controller, theme: theme, tokenMap: tokenMap, position: boxPos, width: boxWidth, narrow: width < 600, onDrag: onDrag, onMeasure: onMeasure, started: started, onStart: onStart, hasNarration: hasNarration }))] }));
956
+ }
957
+ // ---- galleries ---------------------------------------------------------------------------------
958
+ const DEFAULT_INLINE_GALLERY_SIZE = 125;
959
+ function describeSource(source, mode) {
960
+ const backing = source.backingObject;
961
+ return {
962
+ mode,
963
+ of: backing instanceof Chit ? backing.id : undefined,
964
+ chitIds: source.items.map((item) => (item.chit instanceof Chit ? item.chit.id : item.id)),
965
+ };
966
+ }
967
+ function describeGalleries(modal) {
968
+ const result = [];
969
+ if (modal.gallerySource.value)
970
+ result.push(describeSource(modal.gallerySource.value, "fullscreen"));
971
+ if (modal.inlineGallerySource.value)
972
+ result.push(describeSource(modal.inlineGallerySource.value, "inline"));
973
+ return result;
974
+ }
975
+ /** The inline/full-screen choice the gallery buttons remember */
976
+ function preferredGalleryMode() {
977
+ try {
978
+ return window.localStorage.getItem("galleryFullScreen") === '"modal"' ? "fullscreen" : "inline";
979
+ }
980
+ catch {
981
+ return "inline";
982
+ }
983
+ }
984
+ /**
985
+ * Opens, switches or closes a gallery through the same modal state the gallery buttons use, so the
986
+ * player can keep using those buttons. Returns true when it opened a new gallery.
987
+ */
988
+ function applyGalleryCommand(modal, command, root, me) {
989
+ if (command === "close") {
990
+ modal.gallerySource.value = undefined;
991
+ modal.inlineGallerySource.value = undefined;
992
+ return false;
993
+ }
994
+ const open = modal.gallerySource.value ?? modal.inlineGallerySource.value;
995
+ let source;
996
+ let opened = false;
997
+ if (command.of) {
998
+ const container = selectChits(command.of, root, me)[0];
999
+ if (container) {
1000
+ source = new GalleryItemChitChildrenSource(container);
1001
+ opened = true;
1002
+ }
1003
+ }
1004
+ else if (command.items) {
1005
+ const chits = selectChits(command.items, root, me);
1006
+ if (chits.length > 0) {
1007
+ source = new GalleryItemRawSource(chitsToGalleryItems(chits));
1008
+ opened = true;
1009
+ }
1010
+ }
1011
+ else if (open) {
1012
+ // Switching modes: a display closes the source it leaves, so hand the other one a fresh copy
1013
+ source = open.backingObject instanceof Chit ? new GalleryItemChitChildrenSource(open.backingObject) : open;
1014
+ source.inlineGallerySize = open.inlineGallerySize;
1015
+ }
1016
+ if (!source)
1017
+ return false;
1018
+ source.inlineGallerySize ?? (source.inlineGallerySize = DEFAULT_INLINE_GALLERY_SIZE);
1019
+ const mode = command.mode && command.mode !== "preferred"
1020
+ ? command.mode
1021
+ : opened
1022
+ ? preferredGalleryMode()
1023
+ : modal.gallerySource.value
1024
+ ? "fullscreen"
1025
+ : "inline";
1026
+ if (mode === "fullscreen") {
1027
+ modal.inlineGallerySource.value = undefined;
1028
+ modal.gallerySource.value = source;
1029
+ }
1030
+ else {
1031
+ modal.gallerySource.value = undefined;
1032
+ modal.inlineGallerySource.value = source;
1033
+ }
1034
+ return opened;
1035
+ }
1036
+ const RATE_KEY = "tutorialPlaybackRate";
1037
+ /** The narration speed this viewer chose last time (a per-device convenience; 1 when unavailable) */
1038
+ function readRate() {
1039
+ try {
1040
+ const value = parseFloat(window.localStorage.getItem(RATE_KEY) ?? "1");
1041
+ return value > 0 ? value : 1;
1042
+ }
1043
+ catch {
1044
+ return 1;
1045
+ }
1046
+ }
1047
+ function writeRate(rate) {
1048
+ try {
1049
+ window.localStorage.setItem(RATE_KEY, String(rate));
1050
+ }
1051
+ catch {
1052
+ // private window or blocked storage: the speed just is not remembered
1053
+ }
1054
+ }
1055
+ const EMPTY_FOCUS = new EventChannel([]);
1056
+ const IDLE = new EventChannel("idle");
1057
+ const NO_BOX = new EventChannel(undefined);
1058
+ /** A ring's padding: the usual breathing room, shrunk so neighbouring rings (with `separate`) never touch */
1059
+ function ringPadding(ring, all) {
1060
+ let pad = RING_PADDING;
1061
+ for (const other of all) {
1062
+ if (other === ring)
1063
+ continue;
1064
+ const gapX = Math.max(other.left - ring.right, ring.left - other.right);
1065
+ const gapY = Math.max(other.top - ring.bottom, ring.top - other.bottom);
1066
+ const gap = Math.max(gapX, gapY);
1067
+ pad = Math.min(pad, gap / 2 - 2);
1068
+ }
1069
+ return Math.max(1, pad);
1070
+ }
1071
+ function RingShape({ rect, shape, pad, ...rest }) {
1072
+ const w = rect.right - rect.left + pad * 2;
1073
+ const h = rect.bottom - rect.top + pad * 2;
1074
+ if (shape === "ellipse") {
1075
+ return (_jsx("ellipse", { cx: rect.left - pad + w / 2, cy: rect.top - pad + h / 2, rx: w / 2, ry: h / 2, ...rest }));
1076
+ }
1077
+ return (_jsx("rect", { x: rect.left - pad, y: rect.top - pad, width: w, height: h, rx: Math.min(14, w / 4, h / 4), ...rest }));
1078
+ }
1079
+ /** A curved arrow from the nearest edge of the box to the nearest edge of the target. */
1080
+ function Arrow({ from, to, color }) {
1081
+ const tc = rectCenter(to);
1082
+ const fc = rectCenter(from);
1083
+ // Start on the box edge facing the target
1084
+ const dx = tc.x - fc.x;
1085
+ const dy = tc.y - fc.y;
1086
+ let start;
1087
+ // ...as close to straight across from the target as the edge allows, so several arrows fan out
1088
+ // from their own points instead of one shared one
1089
+ const edgeMargin = 18;
1090
+ if (Math.abs(dx) * (from.bottom - from.top) > Math.abs(dy) * (from.right - from.left)) {
1091
+ start = {
1092
+ x: dx > 0 ? from.right : from.left,
1093
+ y: Math.max(from.top + edgeMargin, Math.min(from.bottom - edgeMargin, tc.y)),
1094
+ };
1095
+ }
1096
+ else {
1097
+ start = {
1098
+ x: Math.max(from.left + edgeMargin, Math.min(from.right - edgeMargin, tc.x)),
1099
+ y: dy > 0 ? from.bottom : from.top,
1100
+ };
1101
+ }
1102
+ // End on the target edge facing the box, a little outside it
1103
+ const pad = 10;
1104
+ const ex = tc.x - start.x;
1105
+ const ey = tc.y - start.y;
1106
+ let end;
1107
+ if (Math.abs(ex) * (to.bottom - to.top) > Math.abs(ey) * (to.right - to.left)) {
1108
+ end = { x: ex > 0 ? to.left - pad : to.right + pad, y: tc.y };
1109
+ }
1110
+ else {
1111
+ end = { x: tc.x, y: ey > 0 ? to.top - pad : to.bottom + pad };
1112
+ }
1113
+ const len = Math.hypot(end.x - start.x, end.y - start.y);
1114
+ if (len < 24)
1115
+ return null;
1116
+ // Control point: perpendicular offset for a gentle curve
1117
+ const mx = (start.x + end.x) / 2;
1118
+ const my = (start.y + end.y) / 2;
1119
+ const nx = -(end.y - start.y) / len;
1120
+ const ny = (end.x - start.x) / len;
1121
+ const bend = Math.min(60, len * 0.2);
1122
+ const cx = mx + nx * bend;
1123
+ const cy = my + ny * bend;
1124
+ const ux = (end.x - start.x) / len;
1125
+ const uy = (end.y - start.y) / len;
1126
+ return (_jsxs("g", { style: {
1127
+ "--nx": `${ux * 6}px`,
1128
+ "--ny": `${uy * 6}px`,
1129
+ animation: "tutorialArrowNudge 1.1s ease-in-out infinite",
1130
+ }, children: [_jsx("path", { d: `M ${start.x} ${start.y} Q ${cx} ${cy} ${end.x} ${end.y}`, fill: "none", stroke: "rgba(0,0,0,0.35)", strokeWidth: 6, strokeLinecap: "round" }), _jsx("path", { d: `M ${start.x} ${start.y} Q ${cx} ${cy} ${end.x} ${end.y}`, fill: "none", stroke: color, strokeWidth: 3, strokeLinecap: "round", markerEnd: "url(#tutorial-arrowhead)", filter: "url(#tutorial-glow)" }), _jsx("circle", { cx: start.x, cy: start.y, r: 4, fill: color })] }));
1131
+ }
1132
+ //# sourceMappingURL=TutorialOverlay.js.map