@lightningjs/renderer 2.14.2 → 2.14.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.
Files changed (310) hide show
  1. package/LICENSE +202 -202
  2. package/NOTICE +3 -3
  3. package/README.md +147 -147
  4. package/dist/src/core/CoreTextNode.js +2 -2
  5. package/dist/src/core/CoreTextNode.js.map +1 -1
  6. package/dist/src/core/CoreTextureManager.d.ts +4 -3
  7. package/dist/src/core/CoreTextureManager.js +33 -23
  8. package/dist/src/core/CoreTextureManager.js.map +1 -1
  9. package/dist/src/core/Stage.js +7 -2
  10. package/dist/src/core/Stage.js.map +1 -1
  11. package/dist/src/core/TextureMemoryManager.d.ts +1 -1
  12. package/dist/src/core/TextureMemoryManager.js +25 -2
  13. package/dist/src/core/TextureMemoryManager.js.map +1 -1
  14. package/dist/src/core/renderers/CoreContextTexture.d.ts +1 -1
  15. package/dist/src/core/renderers/canvas/CanvasCoreTexture.d.ts +1 -1
  16. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js +7 -6
  17. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js.map +1 -1
  18. package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.js +3 -0
  19. package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.js.map +1 -1
  20. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.d.ts +5 -6
  21. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +30 -18
  22. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -1
  23. package/dist/src/core/renderers/webgl/WebGlCoreShader.destroy.js +2 -0
  24. package/dist/src/core/renderers/webgl/WebGlCoreShader.destroy.js.map +1 -0
  25. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +45 -45
  26. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  27. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  28. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  29. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +62 -62
  30. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  31. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +6 -6
  32. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  33. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  34. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  35. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  36. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  37. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  38. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +22 -22
  39. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +28 -28
  40. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +10 -10
  41. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  42. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  43. package/dist/src/core/textures/ImageTexture.d.ts +1 -1
  44. package/dist/src/core/textures/ImageTexture.js +1 -1
  45. package/dist/src/core/textures/ImageTexture.js.map +1 -1
  46. package/dist/src/core/textures/Texture.d.ts +4 -0
  47. package/dist/src/core/textures/Texture.js +9 -3
  48. package/dist/src/core/textures/Texture.js.map +1 -1
  49. package/dist/src/main-api/Renderer.js +1 -1
  50. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  51. package/exports/canvas.ts +39 -39
  52. package/exports/index.ts +89 -89
  53. package/exports/inspector.ts +24 -24
  54. package/exports/utils.ts +44 -44
  55. package/exports/webgl.ts +38 -38
  56. package/package.json +1 -1
  57. package/scripts/please-use-pnpm.js +13 -13
  58. package/src/common/CommonTypes.ts +146 -146
  59. package/src/common/EventEmitter.ts +77 -77
  60. package/src/common/IAnimationController.ts +92 -92
  61. package/src/common/IEventEmitter.ts +28 -28
  62. package/src/core/CoreNode.test.ts +202 -202
  63. package/src/core/CoreNode.ts +2468 -2468
  64. package/src/core/CoreShaderManager.ts +292 -292
  65. package/src/core/CoreTextNode.ts +455 -455
  66. package/src/core/CoreTextureManager.ts +561 -558
  67. package/src/core/Stage.ts +748 -743
  68. package/src/core/TextureMemoryManager.ts +431 -404
  69. package/src/core/animations/AnimationManager.ts +38 -38
  70. package/src/core/animations/CoreAnimation.ts +347 -347
  71. package/src/core/animations/CoreAnimationController.ts +166 -166
  72. package/src/core/lib/ContextSpy.ts +41 -41
  73. package/src/core/lib/ImageWorker.ts +280 -280
  74. package/src/core/lib/Matrix3d.ts +244 -244
  75. package/src/core/lib/RenderCoords.ts +86 -86
  76. package/src/core/lib/WebGlContextWrapper.ts +1332 -1332
  77. package/src/core/lib/textureCompression.ts +152 -152
  78. package/src/core/lib/textureSvg.ts +78 -78
  79. package/src/core/lib/utils.ts +384 -384
  80. package/src/core/lib/validateImageBitmap.ts +76 -76
  81. package/src/core/platform.ts +63 -63
  82. package/src/core/renderers/CoreContextTexture.ts +43 -43
  83. package/src/core/renderers/CoreRenderOp.ts +22 -22
  84. package/src/core/renderers/CoreRenderer.ts +115 -115
  85. package/src/core/renderers/CoreShader.ts +41 -41
  86. package/src/core/renderers/canvas/CanvasCoreRenderer.ts +375 -375
  87. package/src/core/renderers/canvas/CanvasCoreTexture.ts +153 -153
  88. package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +231 -231
  89. package/src/core/renderers/canvas/internal/ColorUtils.ts +69 -69
  90. package/src/core/renderers/canvas/shaders/UnsupportedShader.ts +48 -48
  91. package/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts +91 -86
  92. package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +50 -50
  93. package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +313 -301
  94. package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +125 -125
  95. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +821 -821
  96. package/src/core/renderers/webgl/WebGlCoreShader.ts +365 -365
  97. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  98. package/src/core/renderers/webgl/internal/RendererUtils.ts +155 -155
  99. package/src/core/renderers/webgl/internal/ShaderUtils.ts +143 -143
  100. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  101. package/src/core/renderers/webgl/shaders/DefaultShader.ts +93 -93
  102. package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
  103. package/src/core/renderers/webgl/shaders/DynamicShader.ts +580 -580
  104. package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +167 -167
  105. package/src/core/renderers/webgl/shaders/SdfShader.ts +204 -204
  106. package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
  107. package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +87 -87
  108. package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
  109. package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
  110. package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
  111. package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +159 -159
  112. package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +127 -127
  113. package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +148 -148
  114. package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
  115. package/src/core/renderers/webgl/shaders/effects/HolePunchEffect.ts +157 -157
  116. package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +171 -171
  117. package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +168 -168
  118. package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +187 -187
  119. package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +110 -110
  120. package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +196 -196
  121. package/src/core/text-rendering/TextRenderingUtils.ts +36 -36
  122. package/src/core/text-rendering/TextTextureRendererUtils.ts +263 -263
  123. package/src/core/text-rendering/TrFontManager.ts +183 -183
  124. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +176 -176
  125. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  126. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -173
  127. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +171 -171
  128. package/src/core/text-rendering/font-face-types/TrFontFace.ts +187 -187
  129. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +94 -94
  130. package/src/core/text-rendering/font-face-types/utils.ts +39 -39
  131. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +509 -509
  132. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +808 -808
  133. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +853 -853
  134. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  135. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  136. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  137. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  138. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +117 -117
  139. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  140. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  141. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +408 -408
  142. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -49
  143. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +52 -52
  144. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  145. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  146. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  147. package/src/core/text-rendering/renderers/TextRenderer.ts +557 -557
  148. package/src/core/textures/ColorTexture.ts +102 -102
  149. package/src/core/textures/ImageTexture.ts +400 -399
  150. package/src/core/textures/NoiseTexture.ts +104 -104
  151. package/src/core/textures/RenderTexture.ts +85 -85
  152. package/src/core/textures/SubTexture.ts +205 -205
  153. package/src/core/textures/Texture.ts +376 -367
  154. package/src/core/utils.ts +227 -227
  155. package/src/env.d.ts +7 -7
  156. package/src/main-api/DynamicShaderController.ts +104 -104
  157. package/src/main-api/INode.ts +101 -101
  158. package/src/main-api/Inspector.ts +522 -522
  159. package/src/main-api/Renderer.ts +751 -751
  160. package/src/main-api/ShaderController.ts +80 -80
  161. package/src/main-api/utils.ts +45 -45
  162. package/src/utils.ts +248 -248
  163. package/COPYING +0 -1
  164. package/dist/exports/canvas-shaders.d.ts +0 -10
  165. package/dist/exports/canvas-shaders.js +0 -27
  166. package/dist/exports/canvas-shaders.js.map +0 -1
  167. package/dist/exports/webgl-shaders.d.ts +0 -11
  168. package/dist/exports/webgl-shaders.js +0 -28
  169. package/dist/exports/webgl-shaders.js.map +0 -1
  170. package/dist/src/core/platforms/Platform.d.ts +0 -37
  171. package/dist/src/core/platforms/Platform.js +0 -22
  172. package/dist/src/core/platforms/Platform.js.map +0 -1
  173. package/dist/src/core/platforms/web/WebPlatform.d.ts +0 -9
  174. package/dist/src/core/platforms/web/WebPlatform.js +0 -58
  175. package/dist/src/core/platforms/web/WebPlatform.js.map +0 -1
  176. package/dist/src/core/renderers/CoreShaderNode.d.ts +0 -58
  177. package/dist/src/core/renderers/CoreShaderNode.js +0 -104
  178. package/dist/src/core/renderers/CoreShaderNode.js.map +0 -1
  179. package/dist/src/core/renderers/CoreShaderProgram.d.ts +0 -4
  180. package/dist/src/core/renderers/CoreShaderProgram.js +0 -20
  181. package/dist/src/core/renderers/CoreShaderProgram.js.map +0 -1
  182. package/dist/src/core/renderers/canvas/CanvasRenderer.d.ts +0 -36
  183. package/dist/src/core/renderers/canvas/CanvasRenderer.js +0 -212
  184. package/dist/src/core/renderers/canvas/CanvasRenderer.js.map +0 -1
  185. package/dist/src/core/renderers/canvas/CanvasShaderNode.d.ts +0 -21
  186. package/dist/src/core/renderers/canvas/CanvasShaderNode.js +0 -60
  187. package/dist/src/core/renderers/canvas/CanvasShaderNode.js.map +0 -1
  188. package/dist/src/core/renderers/canvas/CanvasTexture.d.ts +0 -16
  189. package/dist/src/core/renderers/canvas/CanvasTexture.js +0 -124
  190. package/dist/src/core/renderers/canvas/CanvasTexture.js.map +0 -1
  191. package/dist/src/core/renderers/webgl/WebGlCtxRenderTexture.d.ts +0 -12
  192. package/dist/src/core/renderers/webgl/WebGlCtxRenderTexture.js +0 -55
  193. package/dist/src/core/renderers/webgl/WebGlCtxRenderTexture.js.map +0 -1
  194. package/dist/src/core/renderers/webgl/WebGlCtxSubTexture.d.ts +0 -9
  195. package/dist/src/core/renderers/webgl/WebGlCtxSubTexture.js +0 -38
  196. package/dist/src/core/renderers/webgl/WebGlCtxSubTexture.js.map +0 -1
  197. package/dist/src/core/renderers/webgl/WebGlCtxTexture.d.ts +0 -57
  198. package/dist/src/core/renderers/webgl/WebGlCtxTexture.js +0 -227
  199. package/dist/src/core/renderers/webgl/WebGlCtxTexture.js.map +0 -1
  200. package/dist/src/core/renderers/webgl/WebGlRenderOp.d.ts +0 -44
  201. package/dist/src/core/renderers/webgl/WebGlRenderOp.js +0 -118
  202. package/dist/src/core/renderers/webgl/WebGlRenderOp.js.map +0 -1
  203. package/dist/src/core/renderers/webgl/WebGlRenderer.d.ts +0 -135
  204. package/dist/src/core/renderers/webgl/WebGlRenderer.js +0 -571
  205. package/dist/src/core/renderers/webgl/WebGlRenderer.js.map +0 -1
  206. package/dist/src/core/renderers/webgl/WebGlShaderNode.d.ts +0 -222
  207. package/dist/src/core/renderers/webgl/WebGlShaderNode.js +0 -334
  208. package/dist/src/core/renderers/webgl/WebGlShaderNode.js.map +0 -1
  209. package/dist/src/core/renderers/webgl/WebGlShaderProgram.d.ts +0 -35
  210. package/dist/src/core/renderers/webgl/WebGlShaderProgram.js +0 -201
  211. package/dist/src/core/renderers/webgl/WebGlShaderProgram.js.map +0 -1
  212. package/dist/src/core/shaders/canvas/Border.d.ts +0 -9
  213. package/dist/src/core/shaders/canvas/Border.js +0 -57
  214. package/dist/src/core/shaders/canvas/Border.js.map +0 -1
  215. package/dist/src/core/shaders/canvas/HolePunch.d.ts +0 -7
  216. package/dist/src/core/shaders/canvas/HolePunch.js +0 -38
  217. package/dist/src/core/shaders/canvas/HolePunch.js.map +0 -1
  218. package/dist/src/core/shaders/canvas/LinearGradient.d.ts +0 -10
  219. package/dist/src/core/shaders/canvas/LinearGradient.js +0 -46
  220. package/dist/src/core/shaders/canvas/LinearGradient.js.map +0 -1
  221. package/dist/src/core/shaders/canvas/RadialGradient.d.ts +0 -11
  222. package/dist/src/core/shaders/canvas/RadialGradient.js +0 -68
  223. package/dist/src/core/shaders/canvas/RadialGradient.js.map +0 -1
  224. package/dist/src/core/shaders/canvas/Rounded.d.ts +0 -7
  225. package/dist/src/core/shaders/canvas/Rounded.js +0 -33
  226. package/dist/src/core/shaders/canvas/Rounded.js.map +0 -1
  227. package/dist/src/core/shaders/canvas/RoundedWithBorder.d.ts +0 -7
  228. package/dist/src/core/shaders/canvas/RoundedWithBorder.js +0 -41
  229. package/dist/src/core/shaders/canvas/RoundedWithBorder.js.map +0 -1
  230. package/dist/src/core/shaders/canvas/RoundedWithBorderAndShadow.d.ts +0 -8
  231. package/dist/src/core/shaders/canvas/RoundedWithBorderAndShadow.js +0 -39
  232. package/dist/src/core/shaders/canvas/RoundedWithBorderAndShadow.js.map +0 -1
  233. package/dist/src/core/shaders/canvas/RoundedWithShadow.d.ts +0 -7
  234. package/dist/src/core/shaders/canvas/RoundedWithShadow.js +0 -38
  235. package/dist/src/core/shaders/canvas/RoundedWithShadow.js.map +0 -1
  236. package/dist/src/core/shaders/canvas/Shadow.d.ts +0 -8
  237. package/dist/src/core/shaders/canvas/Shadow.js +0 -31
  238. package/dist/src/core/shaders/canvas/Shadow.js.map +0 -1
  239. package/dist/src/core/shaders/canvas/utils/render.d.ts +0 -5
  240. package/dist/src/core/shaders/canvas/utils/render.js +0 -84
  241. package/dist/src/core/shaders/canvas/utils/render.js.map +0 -1
  242. package/dist/src/core/shaders/templates/BorderTemplate.d.ts +0 -37
  243. package/dist/src/core/shaders/templates/BorderTemplate.js +0 -73
  244. package/dist/src/core/shaders/templates/BorderTemplate.js.map +0 -1
  245. package/dist/src/core/shaders/templates/HolePunchTemplate.d.ts +0 -46
  246. package/dist/src/core/shaders/templates/HolePunchTemplate.js +0 -35
  247. package/dist/src/core/shaders/templates/HolePunchTemplate.js.map +0 -1
  248. package/dist/src/core/shaders/templates/LinearGradientTemplate.d.ts +0 -23
  249. package/dist/src/core/shaders/templates/LinearGradientTemplate.js +0 -47
  250. package/dist/src/core/shaders/templates/LinearGradientTemplate.js.map +0 -1
  251. package/dist/src/core/shaders/templates/RadialGradientTemplate.d.ts +0 -31
  252. package/dist/src/core/shaders/templates/RadialGradientTemplate.js +0 -49
  253. package/dist/src/core/shaders/templates/RadialGradientTemplate.js.map +0 -1
  254. package/dist/src/core/shaders/templates/RoundedTemplate.d.ts +0 -29
  255. package/dist/src/core/shaders/templates/RoundedTemplate.js +0 -67
  256. package/dist/src/core/shaders/templates/RoundedTemplate.js.map +0 -1
  257. package/dist/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.d.ts +0 -7
  258. package/dist/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.js +0 -24
  259. package/dist/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.js.map +0 -1
  260. package/dist/src/core/shaders/templates/RoundedWithBorderTemplate.d.ts +0 -6
  261. package/dist/src/core/shaders/templates/RoundedWithBorderTemplate.js +0 -23
  262. package/dist/src/core/shaders/templates/RoundedWithBorderTemplate.js.map +0 -1
  263. package/dist/src/core/shaders/templates/RoundedWithShadowTemplate.d.ts +0 -6
  264. package/dist/src/core/shaders/templates/RoundedWithShadowTemplate.js +0 -23
  265. package/dist/src/core/shaders/templates/RoundedWithShadowTemplate.js.map +0 -1
  266. package/dist/src/core/shaders/templates/ShadowTemplate.d.ts +0 -34
  267. package/dist/src/core/shaders/templates/ShadowTemplate.js +0 -66
  268. package/dist/src/core/shaders/templates/ShadowTemplate.js.map +0 -1
  269. package/dist/src/core/shaders/templates/shaderUtils.d.ts +0 -5
  270. package/dist/src/core/shaders/templates/shaderUtils.js +0 -41
  271. package/dist/src/core/shaders/templates/shaderUtils.js.map +0 -1
  272. package/dist/src/core/shaders/webgl/Border.d.ts +0 -3
  273. package/dist/src/core/shaders/webgl/Border.js +0 -110
  274. package/dist/src/core/shaders/webgl/Border.js.map +0 -1
  275. package/dist/src/core/shaders/webgl/Default.d.ts +0 -2
  276. package/dist/src/core/shaders/webgl/Default.js +0 -86
  277. package/dist/src/core/shaders/webgl/Default.js.map +0 -1
  278. package/dist/src/core/shaders/webgl/DefaultBatched.d.ts +0 -2
  279. package/dist/src/core/shaders/webgl/DefaultBatched.js +0 -104
  280. package/dist/src/core/shaders/webgl/DefaultBatched.js.map +0 -1
  281. package/dist/src/core/shaders/webgl/HolePunch.d.ts +0 -3
  282. package/dist/src/core/shaders/webgl/HolePunch.js +0 -64
  283. package/dist/src/core/shaders/webgl/HolePunch.js.map +0 -1
  284. package/dist/src/core/shaders/webgl/LinearGradient.d.ts +0 -3
  285. package/dist/src/core/shaders/webgl/LinearGradient.js +0 -75
  286. package/dist/src/core/shaders/webgl/LinearGradient.js.map +0 -1
  287. package/dist/src/core/shaders/webgl/RadialGradient.d.ts +0 -3
  288. package/dist/src/core/shaders/webgl/RadialGradient.js +0 -73
  289. package/dist/src/core/shaders/webgl/RadialGradient.js.map +0 -1
  290. package/dist/src/core/shaders/webgl/Rounded.d.ts +0 -7
  291. package/dist/src/core/shaders/webgl/Rounded.js +0 -86
  292. package/dist/src/core/shaders/webgl/Rounded.js.map +0 -1
  293. package/dist/src/core/shaders/webgl/RoundedWithBorder.d.ts +0 -3
  294. package/dist/src/core/shaders/webgl/RoundedWithBorder.js +0 -129
  295. package/dist/src/core/shaders/webgl/RoundedWithBorder.js.map +0 -1
  296. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.d.ts +0 -3
  297. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js +0 -149
  298. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js.map +0 -1
  299. package/dist/src/core/shaders/webgl/RoundedWithShadow.d.ts +0 -3
  300. package/dist/src/core/shaders/webgl/RoundedWithShadow.js +0 -84
  301. package/dist/src/core/shaders/webgl/RoundedWithShadow.js.map +0 -1
  302. package/dist/src/core/shaders/webgl/SdfShader.d.ts +0 -32
  303. package/dist/src/core/shaders/webgl/SdfShader.js +0 -116
  304. package/dist/src/core/shaders/webgl/SdfShader.js.map +0 -1
  305. package/dist/src/core/shaders/webgl/Shadow.d.ts +0 -3
  306. package/dist/src/core/shaders/webgl/Shadow.js +0 -110
  307. package/dist/src/core/shaders/webgl/Shadow.js.map +0 -1
  308. package/dist/src/core/temp.js +0 -77
  309. package/dist/src/core/temp.js.map +0 -1
  310. /package/dist/src/core/{temp.d.ts → renderers/webgl/WebGlCoreShader.destroy.d.ts} +0 -0
@@ -1,2468 +1,2468 @@
1
- /*
2
- * If not stated otherwise in this file or this component's LICENSE file the
3
- * following copyright and licenses apply:
4
- *
5
- * Copyright 2023 Comcast Cable Communications Management, LLC.
6
- *
7
- * Licensed under the Apache License, Version 2.0 (the License);
8
- * you may not use this file except in compliance with the License.
9
- * You may obtain a copy of the License at
10
- *
11
- * http://www.apache.org/licenses/LICENSE-2.0
12
- *
13
- * Unless required by applicable law or agreed to in writing, software
14
- * distributed under the License is distributed on an "AS IS" BASIS,
15
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- * See the License for the specific language governing permissions and
17
- * limitations under the License.
18
- */
19
-
20
- import {
21
- assertTruthy,
22
- getNewId,
23
- isProductionEnvironment,
24
- mergeColorAlphaPremultiplied,
25
- } from '../utils.js';
26
- import type { TextureOptions } from './CoreTextureManager.js';
27
- import type { CoreRenderer } from './renderers/CoreRenderer.js';
28
- import type { Stage } from './Stage.js';
29
- import {
30
- type Texture,
31
- type TextureFailedEventHandler,
32
- type TextureFreedEventHandler,
33
- type TextureLoadedEventHandler,
34
- } from './textures/Texture.js';
35
- import type {
36
- Dimensions,
37
- NodeTextureFailedPayload,
38
- NodeTextureFreedPayload,
39
- NodeTextureLoadedPayload,
40
- } from '../common/CommonTypes.js';
41
- import { EventEmitter } from '../common/EventEmitter.js';
42
- import {
43
- copyRect,
44
- intersectRect,
45
- type Bound,
46
- type RectWithValid,
47
- createBound,
48
- boundInsideBound,
49
- boundLargeThanBound,
50
- createPreloadBounds,
51
- } from './lib/utils.js';
52
- import { Matrix3d } from './lib/Matrix3d.js';
53
- import { RenderCoords } from './lib/RenderCoords.js';
54
- import type { AnimationSettings } from './animations/CoreAnimation.js';
55
- import type { IAnimationController } from '../common/IAnimationController.js';
56
- import { CoreAnimation } from './animations/CoreAnimation.js';
57
- import { CoreAnimationController } from './animations/CoreAnimationController.js';
58
- import type { BaseShaderController } from '../main-api/ShaderController.js';
59
-
60
- export enum CoreNodeRenderState {
61
- Init = 0,
62
- OutOfBounds = 2,
63
- InBounds = 4,
64
- InViewport = 8,
65
- }
66
-
67
- const CoreNodeRenderStateMap: Map<CoreNodeRenderState, string> = new Map();
68
- CoreNodeRenderStateMap.set(CoreNodeRenderState.Init, 'init');
69
- CoreNodeRenderStateMap.set(CoreNodeRenderState.OutOfBounds, 'outOfBounds');
70
- CoreNodeRenderStateMap.set(CoreNodeRenderState.InBounds, 'inBounds');
71
- CoreNodeRenderStateMap.set(CoreNodeRenderState.InViewport, 'inViewport');
72
-
73
- export enum UpdateType {
74
- /**
75
- * Child updates
76
- */
77
- Children = 1,
78
-
79
- /**
80
- * Scale/Rotate transform update
81
- *
82
- * @remarks
83
- * CoreNode Properties Updated:
84
- * - `scaleRotateTransform`
85
- */
86
- ScaleRotate = 2,
87
-
88
- /**
89
- * Translate transform update (x/y/width/height/pivot/mount)
90
- *
91
- * @remarks
92
- * CoreNode Properties Updated:
93
- * - `localTransform`
94
- */
95
- Local = 4,
96
-
97
- /**
98
- * Global Transform update
99
- *
100
- * @remarks
101
- * CoreNode Properties Updated:
102
- * - `globalTransform`
103
- * - `renderCoords`
104
- * - `renderBound`
105
- */
106
- Global = 8,
107
-
108
- /**
109
- * Clipping rect update
110
- *
111
- * @remarks
112
- * CoreNode Properties Updated:
113
- * - `clippingRect`
114
- */
115
- Clipping = 16,
116
-
117
- /**
118
- * Calculated ZIndex update
119
- *
120
- * @remarks
121
- * CoreNode Properties Updated:
122
- * - `calcZIndex`
123
- */
124
- CalculatedZIndex = 32,
125
-
126
- /**
127
- * Z-Index Sorted Children update
128
- *
129
- * @remarks
130
- * CoreNode Properties Updated:
131
- * - `children` (sorts children by their `calcZIndex`)
132
- */
133
- ZIndexSortedChildren = 64,
134
-
135
- /**
136
- * Premultiplied Colors update
137
- *
138
- * @remarks
139
- * CoreNode Properties Updated:
140
- * - `premultipliedColorTl`
141
- * - `premultipliedColorTr`
142
- * - `premultipliedColorBl`
143
- * - `premultipliedColorBr`
144
- */
145
- PremultipliedColors = 128,
146
-
147
- /**
148
- * World Alpha update
149
- *
150
- * @remarks
151
- * CoreNode Properties Updated:
152
- * - `worldAlpha` = `parent.worldAlpha` * `alpha`
153
- */
154
- WorldAlpha = 256,
155
-
156
- /**
157
- * Render State update
158
- *
159
- * @remarks
160
- * CoreNode Properties Updated:
161
- * - `renderState`
162
- */
163
- RenderState = 512,
164
-
165
- /**
166
- * Is Renderable update
167
- *
168
- * @remarks
169
- * CoreNode Properties Updated:
170
- * - `isRenderable`
171
- */
172
- IsRenderable = 1024,
173
-
174
- /**
175
- * Render Texture update
176
- */
177
- RenderTexture = 2048,
178
-
179
- /**
180
- * Track if parent has render texture
181
- */
182
- ParentRenderTexture = 4096,
183
-
184
- /**
185
- * Render Bounds update
186
- */
187
- RenderBounds = 8192,
188
-
189
- /**
190
- * None
191
- */
192
- None = 0,
193
-
194
- /**
195
- * All
196
- */
197
- All = 14335,
198
- }
199
-
200
- /**
201
- * A custom data map which can be stored on an CoreNode
202
- *
203
- * @remarks
204
- * This is a map of key-value pairs that can be stored on an INode. It is used
205
- * to store custom data that can be used by the application.
206
- * The data stored can only be of type string, number or boolean.
207
- */
208
- export type CustomDataMap = {
209
- [key: string]: string | number | boolean | undefined;
210
- };
211
-
212
- /**
213
- * Writable properties of a Node.
214
- */
215
- export interface CoreNodeProps {
216
- /**
217
- * The x coordinate of the Node's Mount Point.
218
- *
219
- * @remarks
220
- * See {@link mountX} and {@link mountY} for more information about setting
221
- * the Mount Point.
222
- *
223
- * @default `0`
224
- */
225
- x: number;
226
- /**
227
- * The y coordinate of the Node's Mount Point.
228
- *
229
- * @remarks
230
- * See {@link mountX} and {@link mountY} for more information about setting
231
- * the Mount Point.
232
- *
233
- * @default `0`
234
- */
235
- y: number;
236
- /**
237
- * The width of the Node.
238
- *
239
- * @default `0`
240
- */
241
- width: number;
242
- /**
243
- * The height of the Node.
244
- *
245
- * @default `0`
246
- */
247
- height: number;
248
- /**
249
- * The alpha opacity of the Node.
250
- *
251
- * @remarks
252
- * The alpha value is a number between 0 and 1, where 0 is fully transparent
253
- * and 1 is fully opaque.
254
- *
255
- * @default `1`
256
- */
257
- alpha: number;
258
- /**
259
- * Autosize mode
260
- *
261
- * @remarks
262
- * When enabled, when a texture is loaded into the Node, the Node will
263
- * automatically resize to the dimensions of the texture.
264
- *
265
- * Text Nodes are always autosized based on their text content regardless
266
- * of this mode setting.
267
- *
268
- * @default `false`
269
- */
270
- autosize: boolean;
271
- /**
272
- * Margin around the Node's bounds for preloading
273
- *
274
- * @default `null`
275
- */
276
- boundsMargin: number | [number, number, number, number] | null;
277
- /**
278
- * Clipping Mode
279
- *
280
- * @remarks
281
- * Enable Clipping Mode when you want to prevent the drawing of a Node and
282
- * its descendants from overflowing outside of the Node's x/y/width/height
283
- * bounds.
284
- *
285
- * For WebGL, clipping is implemented using the high-performance WebGL
286
- * operation scissor. As a consequence, clipping does not work for
287
- * non-rectangular areas. So, if the element is rotated
288
- * (by itself or by any of its ancestors), clipping will not work as intended.
289
- *
290
- * TODO: Add support for non-rectangular clipping either automatically or
291
- * via Render-To-Texture.
292
- *
293
- * @default `false`
294
- */
295
- clipping: boolean;
296
- /**
297
- * The color of the Node.
298
- *
299
- * @remarks
300
- * The color value is a number in the format 0xRRGGBBAA, where RR is the red
301
- * component, GG is the green component, BB is the blue component, and AA is
302
- * the alpha component.
303
- *
304
- * Gradient colors may be set by setting the different color sub-properties:
305
- * {@link colorTop}, {@link colorBottom}, {@link colorLeft}, {@link colorRight},
306
- * {@link colorTl}, {@link colorTr}, {@link colorBr}, {@link colorBl} accordingly.
307
- *
308
- * @default `0xffffffff` (opaque white)
309
- */
310
- color: number;
311
- /**
312
- * The color of the top edge of the Node for gradient rendering.
313
- *
314
- * @remarks
315
- * See {@link color} for more information about color values and gradient
316
- * rendering.
317
- */
318
- colorTop: number;
319
- /**
320
- * The color of the bottom edge of the Node for gradient rendering.
321
- *
322
- * @remarks
323
- * See {@link color} for more information about color values and gradient
324
- * rendering.
325
- */
326
- colorBottom: number;
327
- /**
328
- * The color of the left edge of the Node for gradient rendering.
329
- *
330
- * @remarks
331
- * See {@link color} for more information about color values and gradient
332
- * rendering.
333
- */
334
- colorLeft: number;
335
- /**
336
- * The color of the right edge of the Node for gradient rendering.
337
- *
338
- * @remarks
339
- * See {@link color} for more information about color values and gradient
340
- * rendering.
341
- */
342
- colorRight: number;
343
- /**
344
- * The color of the top-left corner of the Node for gradient rendering.
345
- *
346
- * @remarks
347
- * See {@link color} for more information about color values and gradient
348
- * rendering.
349
- */
350
- colorTl: number;
351
- /**
352
- * The color of the top-right corner of the Node for gradient rendering.
353
- *
354
- * @remarks
355
- * See {@link color} for more information about color values and gradient
356
- * rendering.
357
- */
358
- colorTr: number;
359
- /**
360
- * The color of the bottom-right corner of the Node for gradient rendering.
361
- *
362
- * @remarks
363
- * See {@link color} for more information about color values and gradient
364
- * rendering.
365
- */
366
- colorBr: number;
367
- /**
368
- * The color of the bottom-left corner of the Node for gradient rendering.
369
- *
370
- * @remarks
371
- * See {@link color} for more information about color values and gradient
372
- * rendering.
373
- */
374
- colorBl: number;
375
- /**
376
- * The Node's parent Node.
377
- *
378
- * @remarks
379
- * The value `null` indicates that the Node has no parent. This may either be
380
- * because the Node is the root Node of the scene graph, or because the Node
381
- * has been removed from the scene graph.
382
- *
383
- * In order to make sure that a Node can be rendered on the screen, it must
384
- * be added to the scene graph by setting it's parent property to a Node that
385
- * is already in the scene graph such as the root Node.
386
- *
387
- * @default `null`
388
- */
389
- parent: CoreNode | null;
390
- /**
391
- * The Node's z-index.
392
- *
393
- * @remarks
394
- * TBD
395
- */
396
- zIndex: number;
397
- /**
398
- * The Node's Texture.
399
- *
400
- * @remarks
401
- * The `texture` defines a rasterized image that is contained within the
402
- * {@link width} and {@link height} dimensions of the Node. If null, the
403
- * Node will use an opaque white {@link ColorTexture} when being drawn, which
404
- * essentially enables colors (including gradients) to be drawn.
405
- *
406
- * If set, by default, the texture will be drawn, as is, stretched to the
407
- * dimensions of the Node. This behavior can be modified by setting the TBD
408
- * and TBD properties.
409
- *
410
- * To create a Texture in order to set it on this property, call
411
- * {@link RendererMain.createTexture}.
412
- *
413
- * If the {@link src} is set on a Node, the Node will use the
414
- * {@link ImageTexture} by default and the Node will simply load the image at
415
- * the specified URL.
416
- *
417
- * Note: If this is a Text Node, the Texture will be managed by the Node's
418
- * {@link TextRenderer} and should not be set explicitly.
419
- */
420
- texture: Texture | null;
421
-
422
- /**
423
- * [Deprecated]: Prevents the texture from being cleaned up when the Node is removed
424
- *
425
- * @remarks
426
- * Please use the `preventCleanup` property on {@link TextureOptions} instead.
427
- *
428
- * @default false
429
- */
430
- preventCleanup: boolean;
431
- /**
432
- * Options to associate with the Node's Texture
433
- */
434
- textureOptions: TextureOptions;
435
-
436
- /**
437
- * The Node's shader
438
- *
439
- * @remarks
440
- * The `shader` defines a {@link Shader} used to draw the Node. By default,
441
- * the Default Shader is used which simply draws the defined {@link texture}
442
- * or {@link color}(s) within the Node without any special effects.
443
- *
444
- * To create a Shader in order to set it on this property, call
445
- * {@link RendererMain.createShader}.
446
- *
447
- * Note: If this is a Text Node, the Shader will be managed by the Node's
448
- * {@link TextRenderer} and should not be set explicitly.
449
- */
450
- shader: BaseShaderController;
451
- /**
452
- * Image URL
453
- *
454
- * @remarks
455
- * When set, the Node's {@link texture} is automatically set to an
456
- * {@link ImageTexture} using the source image URL provided (with all other
457
- * settings being defaults)
458
- */
459
- src: string | null;
460
- zIndexLocked: number;
461
- /**
462
- * Scale to render the Node at
463
- *
464
- * @remarks
465
- * The scale value multiplies the provided {@link width} and {@link height}
466
- * of the Node around the Node's Pivot Point (defined by the {@link pivot}
467
- * props).
468
- *
469
- * Behind the scenes, setting this property sets both the {@link scaleX} and
470
- * {@link scaleY} props to the same value.
471
- *
472
- * NOTE: When the scaleX and scaleY props are explicitly set to different values,
473
- * this property returns `null`. Setting `null` on this property will have no
474
- * effect.
475
- *
476
- * @default 1.0
477
- */
478
- scale: number | null;
479
- /**
480
- * Scale to render the Node at (X-Axis)
481
- *
482
- * @remarks
483
- * The scaleX value multiplies the provided {@link width} of the Node around
484
- * the Node's Pivot Point (defined by the {@link pivot} props).
485
- *
486
- * @default 1.0
487
- */
488
- scaleX: number;
489
- /**
490
- * Scale to render the Node at (Y-Axis)
491
- *
492
- * @remarks
493
- * The scaleY value multiplies the provided {@link height} of the Node around
494
- * the Node's Pivot Point (defined by the {@link pivot} props).
495
- *
496
- * @default 1.0
497
- */
498
- scaleY: number;
499
- /**
500
- * Combined position of the Node's Mount Point
501
- *
502
- * @remarks
503
- * The value can be any number between `0.0` and `1.0`:
504
- * - `0.0` defines the Mount Point at the top-left corner of the Node.
505
- * - `0.5` defines it at the center of the Node.
506
- * - `1.0` defines it at the bottom-right corner of the node.
507
- *
508
- * Use the {@link mountX} and {@link mountY} props seperately for more control
509
- * of the Mount Point.
510
- *
511
- * When assigned, the same value is also passed to both the {@link mountX} and
512
- * {@link mountY} props.
513
- *
514
- * @default 0 (top-left)
515
- */
516
- mount: number;
517
- /**
518
- * X position of the Node's Mount Point
519
- *
520
- * @remarks
521
- * The value can be any number between `0.0` and `1.0`:
522
- * - `0.0` defines the Mount Point's X position as the left-most edge of the
523
- * Node
524
- * - `0.5` defines it as the horizontal center of the Node
525
- * - `1.0` defines it as the right-most edge of the Node.
526
- *
527
- * The combination of {@link mountX} and {@link mountY} define the Mount Point
528
- *
529
- * @default 0 (left-most edge)
530
- */
531
- mountX: number;
532
- /**
533
- * Y position of the Node's Mount Point
534
- *
535
- * @remarks
536
- * The value can be any number between `0.0` and `1.0`:
537
- * - `0.0` defines the Mount Point's Y position as the top-most edge of the
538
- * Node
539
- * - `0.5` defines it as the vertical center of the Node
540
- * - `1.0` defines it as the bottom-most edge of the Node.
541
- *
542
- * The combination of {@link mountX} and {@link mountY} define the Mount Point
543
- *
544
- * @default 0 (top-most edge)
545
- */
546
- mountY: number;
547
- /**
548
- * Combined position of the Node's Pivot Point
549
- *
550
- * @remarks
551
- * The value can be any number between `0.0` and `1.0`:
552
- * - `0.0` defines the Pivot Point at the top-left corner of the Node.
553
- * - `0.5` defines it at the center of the Node.
554
- * - `1.0` defines it at the bottom-right corner of the node.
555
- *
556
- * Use the {@link pivotX} and {@link pivotY} props seperately for more control
557
- * of the Pivot Point.
558
- *
559
- * When assigned, the same value is also passed to both the {@link pivotX} and
560
- * {@link pivotY} props.
561
- *
562
- * @default 0.5 (center)
563
- */
564
- pivot: number;
565
- /**
566
- * X position of the Node's Pivot Point
567
- *
568
- * @remarks
569
- * The value can be any number between `0.0` and `1.0`:
570
- * - `0.0` defines the Pivot Point's X position as the left-most edge of the
571
- * Node
572
- * - `0.5` defines it as the horizontal center of the Node
573
- * - `1.0` defines it as the right-most edge of the Node.
574
- *
575
- * The combination of {@link pivotX} and {@link pivotY} define the Pivot Point
576
- *
577
- * @default 0.5 (centered on x-axis)
578
- */
579
- pivotX: number;
580
- /**
581
- * Y position of the Node's Pivot Point
582
- *
583
- * @remarks
584
- * The value can be any number between `0.0` and `1.0`:
585
- * - `0.0` defines the Pivot Point's Y position as the top-most edge of the
586
- * Node
587
- * - `0.5` defines it as the vertical center of the Node
588
- * - `1.0` defines it as the bottom-most edge of the Node.
589
- *
590
- * The combination of {@link pivotX} and {@link pivotY} define the Pivot Point
591
- *
592
- * @default 0.5 (centered on y-axis)
593
- */
594
- pivotY: number;
595
- /**
596
- * Rotation of the Node (in Radians)
597
- *
598
- * @remarks
599
- * Sets the amount to rotate the Node by around it's Pivot Point (defined by
600
- * the {@link pivot} props). Positive values rotate the Node clockwise, while
601
- * negative values rotate it counter-clockwise.
602
- *
603
- * Example values:
604
- * - `-Math.PI / 2`: 90 degree rotation counter-clockwise
605
- * - `0`: No rotation
606
- * - `Math.PI / 2`: 90 degree rotation clockwise
607
- * - `Math.PI`: 180 degree rotation clockwise
608
- * - `3 * Math.PI / 2`: 270 degree rotation clockwise
609
- * - `2 * Math.PI`: 360 rotation clockwise
610
- */
611
- rotation: number;
612
-
613
- /**
614
- * Whether the Node is rendered to a texture
615
- *
616
- * @remarks
617
- * TBD
618
- *
619
- * @default false
620
- */
621
- rtt: boolean;
622
-
623
- /**
624
- * Node data element for custom data storage (optional)
625
- *
626
- * @remarks
627
- * This property is used to store custom data on the Node as a key/value data store.
628
- * Data values are limited to string, numbers, booleans. Strings will be truncated
629
- * to a 2048 character limit for performance reasons.
630
- *
631
- * This is not a data storage mechanism for large amounts of data please use a
632
- * dedicated data storage mechanism for that.
633
- *
634
- * The custom data will be reflected in the inspector as part of `data-*` attributes
635
- *
636
- * @default `undefined`
637
- */
638
- data?: CustomDataMap;
639
-
640
- /**
641
- * Image Type to explicitly set the image type that is being loaded
642
- *
643
- * @remarks
644
- * This property must be used with a `src` that points at an image. In some cases
645
- * the extension doesn't provide a reliable representation of the image type. In such
646
- * cases set the ImageType explicitly.
647
- *
648
- * `regular` is used for normal images such as png, jpg, etc
649
- * `compressed` is used for ETC1/ETC2 compressed images with a PVR or KTX container
650
- * `svg` is used for scalable vector graphics
651
- *
652
- * @default `undefined`
653
- */
654
- imageType?: 'regular' | 'compressed' | 'svg' | null;
655
-
656
- /**
657
- * She width of the rectangle from which the Image Texture will be extracted.
658
- * This value can be negative. If not provided, the image's source natural
659
- * width will be used.
660
- */
661
- srcWidth?: number;
662
- /**
663
- * The height of the rectangle from which the Image Texture will be extracted.
664
- * This value can be negative. If not provided, the image's source natural
665
- * height will be used.
666
- */
667
- srcHeight?: number;
668
- /**
669
- * The x coordinate of the reference point of the rectangle from which the Texture
670
- * will be extracted. `width` and `height` are provided. And only works when
671
- * createImageBitmap is available. Only works when createImageBitmap is supported on the browser.
672
- */
673
- srcX?: number;
674
- /**
675
- * The y coordinate of the reference point of the rectangle from which the Texture
676
- * will be extracted. Only used when source `srcWidth` width and `srcHeight` height
677
- * are provided. Only works when createImageBitmap is supported on the browser.
678
- */
679
- srcY?: number;
680
- /**
681
- * By enabling Strict bounds the renderer will not process & render child nodes of a node that is out of the visible area
682
- *
683
- * @remarks
684
- * When enabled out of bound nodes, i.e. nodes that are out of the visible area, will
685
- * **NOT** have their children processed and renderer anymore. This means the children of a out of bound
686
- * node will not receive update processing such as positioning updates and will not be drawn on screen.
687
- * As such the rest of the branch of the update tree that sits below this node will not be processed anymore
688
- *
689
- * This is a big performance gain but may be disabled in cases where the width of the parent node is
690
- * unknown and the render must process the child nodes regardless of the viewport status of the parent node
691
- *
692
- * @default false
693
- */
694
- strictBounds: boolean;
695
- }
696
-
697
- /**
698
- * Grab all the number properties of type T
699
- */
700
- type NumberProps<T> = {
701
- [Key in keyof T as NonNullable<T[Key]> extends number ? Key : never]: number;
702
- };
703
-
704
- /**
705
- * Properties of a Node used by the animate() function
706
- */
707
- export interface CoreNodeAnimateProps extends NumberProps<CoreNodeProps> {
708
- /**
709
- * Shader properties to animate
710
- */
711
- shaderProps: Record<string, number>;
712
- // TODO: textureProps: Record<string, number>;
713
- }
714
-
715
- /**
716
- * A visual Node in the Renderer scene graph.
717
- *
718
- * @remarks
719
- * CoreNode is an internally used class that represents a Renderer Node in the
720
- * scene graph. See INode.ts for the public APIs exposed to Renderer users
721
- * that include generic types for Shaders.
722
- */
723
- export class CoreNode extends EventEmitter {
724
- readonly children: CoreNode[] = [];
725
- protected _id: number = getNewId();
726
- readonly props: CoreNodeProps;
727
-
728
- public updateType = UpdateType.All;
729
- public childUpdateType = UpdateType.None;
730
-
731
- public globalTransform?: Matrix3d;
732
- public scaleRotateTransform?: Matrix3d;
733
- public localTransform?: Matrix3d;
734
- public sceneGlobalTransform?: Matrix3d;
735
- public renderCoords?: RenderCoords;
736
- public sceneRenderCoords?: RenderCoords;
737
- public renderBound?: Bound;
738
- public strictBound?: Bound;
739
- public preloadBound?: Bound;
740
- public clippingRect: RectWithValid = {
741
- x: 0,
742
- y: 0,
743
- width: 0,
744
- height: 0,
745
- valid: false,
746
- };
747
- public isRenderable = false;
748
- public renderState: CoreNodeRenderState = CoreNodeRenderState.Init;
749
-
750
- public worldAlpha = 1;
751
- public premultipliedColorTl = 0;
752
- public premultipliedColorTr = 0;
753
- public premultipliedColorBl = 0;
754
- public premultipliedColorBr = 0;
755
- public calcZIndex = 0;
756
- public hasRTTupdates = false;
757
- public parentHasRenderTexture = false;
758
- public rttParent: CoreNode | null = null;
759
-
760
- public destroyed = false;
761
-
762
- constructor(readonly stage: Stage, props: CoreNodeProps) {
763
- super();
764
-
765
- this.props = {
766
- ...props,
767
- parent: null,
768
- texture: null,
769
- src: null,
770
- rtt: false,
771
- };
772
-
773
- // Assign props to instance
774
- this.parent = props.parent;
775
- this.texture = props.texture;
776
- this.src = props.src;
777
- this.rtt = props.rtt;
778
-
779
- if (props.boundsMargin) {
780
- this.boundsMargin = Array.isArray(props.boundsMargin)
781
- ? props.boundsMargin
782
- : [
783
- props.boundsMargin,
784
- props.boundsMargin,
785
- props.boundsMargin,
786
- props.boundsMargin,
787
- ];
788
- }
789
-
790
- this.setUpdateType(
791
- UpdateType.ScaleRotate |
792
- UpdateType.Local |
793
- UpdateType.RenderBounds |
794
- UpdateType.RenderState,
795
- );
796
-
797
- if (isProductionEnvironment() === false && props.preventCleanup === true) {
798
- console.warn(
799
- 'CoreNode.preventCleanup: Is deprecated and will be removed in upcoming release, please use textureOptions.preventCleanup instead',
800
- );
801
- }
802
-
803
- // if the default texture isn't loaded yet, wait for it to load
804
- // this only happens when the node is created before the stage is ready
805
- if (
806
- this.stage.defaultTexture &&
807
- this.stage.defaultTexture.state !== 'loaded'
808
- ) {
809
- this.stage.defaultTexture.once('loaded', () => {
810
- this.setUpdateType(UpdateType.IsRenderable);
811
- });
812
- }
813
- }
814
-
815
- //#region Textures
816
- loadTexture(): void {
817
- const { texture } = this.props;
818
- assertTruthy(texture);
819
-
820
- // If texture is already loaded / failed, trigger loaded event manually
821
- // so that users get a consistent event experience.
822
- // We do this in a microtask to allow listeners to be attached in the same
823
- // synchronous task after calling loadTexture()
824
- queueMicrotask(() => {
825
- if (this.textureOptions.preload === true) {
826
- this.stage.txManager.loadTexture(texture);
827
- }
828
-
829
- texture.preventCleanup =
830
- this.props.textureOptions?.preventCleanup ?? false;
831
- texture.on('loaded', this.onTextureLoaded);
832
- texture.on('failed', this.onTextureFailed);
833
- texture.on('freed', this.onTextureFreed);
834
-
835
- // If the parent is a render texture, the initial texture status
836
- // will be set to freed until the texture is processed by the
837
- // Render RTT nodes. So we only need to listen fo changes and
838
- // no need to check the texture.state until we restructure how
839
- // textures are being processed.
840
- if (this.parentHasRenderTexture) {
841
- this.notifyParentRTTOfUpdate();
842
- return;
843
- }
844
-
845
- if (texture.state === 'loaded') {
846
- assertTruthy(texture.dimensions);
847
- this.onTextureLoaded(texture, texture.dimensions);
848
- } else if (texture.state === 'failed') {
849
- assertTruthy(texture.error);
850
- this.onTextureFailed(texture, texture.error);
851
- } else if (texture.state === 'freed') {
852
- this.onTextureFreed(texture);
853
- }
854
- });
855
- }
856
-
857
- unloadTexture(): void {
858
- if (this.texture !== null) {
859
- this.texture.off('loaded', this.onTextureLoaded);
860
- this.texture.off('failed', this.onTextureFailed);
861
- this.texture.off('freed', this.onTextureFreed);
862
- this.texture.setRenderableOwner(this, false);
863
- }
864
- }
865
-
866
- autosizeNode(dimensions: Dimensions) {
867
- if (this.autosize) {
868
- this.width = dimensions.width;
869
- this.height = dimensions.height;
870
- }
871
- }
872
-
873
- private onTextureLoaded: TextureLoadedEventHandler = (_, dimensions) => {
874
- this.autosizeNode(dimensions);
875
- this.setUpdateType(UpdateType.IsRenderable);
876
-
877
- // Texture was loaded. In case the RAF loop has already stopped, we request
878
- // a render to ensure the texture is rendered.
879
- this.stage.requestRender();
880
-
881
- // If parent has a render texture, flag that we need to update
882
- if (this.parentHasRenderTexture) {
883
- this.notifyParentRTTOfUpdate();
884
- }
885
-
886
- // ignore 1x1 pixel textures
887
- if (dimensions.width > 1 && dimensions.height > 1) {
888
- this.emit('loaded', {
889
- type: 'texture',
890
- dimensions,
891
- } satisfies NodeTextureLoadedPayload);
892
- }
893
-
894
- // Trigger a local update if the texture is loaded and the resizeMode is 'contain'
895
- if (this.props.textureOptions?.resizeMode?.type === 'contain') {
896
- this.setUpdateType(UpdateType.Local);
897
- }
898
- };
899
-
900
- private onTextureFailed: TextureFailedEventHandler = (_, error) => {
901
- // immediately set isRenderable to false, so that we handle the error
902
- // without waiting for the next frame loop
903
- this.isRenderable = false;
904
- this.setUpdateType(UpdateType.IsRenderable);
905
-
906
- // If parent has a render texture, flag that we need to update
907
- if (this.parentHasRenderTexture) {
908
- this.notifyParentRTTOfUpdate();
909
- }
910
-
911
- this.emit('failed', {
912
- type: 'texture',
913
- error,
914
- } satisfies NodeTextureFailedPayload);
915
- };
916
-
917
- private onTextureFreed: TextureFreedEventHandler = () => {
918
- // immediately set isRenderable to false, so that we handle the error
919
- // without waiting for the next frame loop
920
- this.isRenderable = false;
921
- this.setUpdateType(UpdateType.IsRenderable);
922
-
923
- // If parent has a render texture, flag that we need to update
924
- if (this.parentHasRenderTexture) {
925
- this.notifyParentRTTOfUpdate();
926
- }
927
-
928
- this.emit('freed', {
929
- type: 'texture',
930
- } satisfies NodeTextureFreedPayload);
931
- };
932
- //#endregion Textures
933
-
934
- /**
935
- * Change types types is used to determine the scope of the changes being applied
936
- *
937
- * @remarks
938
- * See {@link UpdateType} for more information on each type
939
- *
940
- * @param type
941
- */
942
- setUpdateType(type: UpdateType): void {
943
- this.updateType |= type;
944
-
945
- const parent = this.props.parent;
946
- if (!parent) return;
947
-
948
- if ((parent.updateType & UpdateType.Children) === 0) {
949
- // Inform the parent if it doesn’t already have a child update
950
- parent.setUpdateType(UpdateType.Children);
951
- }
952
- }
953
-
954
- sortChildren() {
955
- this.children.sort((a, b) => a.calcZIndex - b.calcZIndex);
956
- }
957
-
958
- updateScaleRotateTransform() {
959
- const { rotation, scaleX, scaleY } = this.props;
960
-
961
- // optimize simple translation cases
962
- if (rotation === 0 && scaleX === 1 && scaleY === 1) {
963
- this.scaleRotateTransform = undefined;
964
- return;
965
- }
966
-
967
- this.scaleRotateTransform = Matrix3d.rotate(
968
- rotation,
969
- this.scaleRotateTransform,
970
- ).scale(scaleX, scaleY);
971
- }
972
-
973
- updateLocalTransform() {
974
- const { x, y, width, height } = this.props;
975
- const mountTranslateX = this.props.mountX * width;
976
- const mountTranslateY = this.props.mountY * height;
977
-
978
- if (this.scaleRotateTransform) {
979
- const pivotTranslateX = this.props.pivotX * width;
980
- const pivotTranslateY = this.props.pivotY * height;
981
-
982
- this.localTransform = Matrix3d.translate(
983
- x - mountTranslateX + pivotTranslateX,
984
- y - mountTranslateY + pivotTranslateY,
985
- this.localTransform,
986
- )
987
- .multiply(this.scaleRotateTransform)
988
- .translate(-pivotTranslateX, -pivotTranslateY);
989
- } else {
990
- this.localTransform = Matrix3d.translate(
991
- x - mountTranslateX,
992
- y - mountTranslateY,
993
- this.localTransform,
994
- );
995
- }
996
-
997
- // Handle 'contain' resize mode
998
- const texture = this.props.texture;
999
- if (
1000
- texture &&
1001
- texture.dimensions &&
1002
- this.props.textureOptions?.resizeMode?.type === 'contain'
1003
- ) {
1004
- let resizeModeScaleX = 1;
1005
- let resizeModeScaleY = 1;
1006
- let extraX = 0;
1007
- let extraY = 0;
1008
- const { width: tw, height: th } = texture.dimensions;
1009
- const txAspectRatio = tw / th;
1010
- const nodeAspectRatio = width / height;
1011
- if (txAspectRatio > nodeAspectRatio) {
1012
- // Texture is wider than node
1013
- // Center the node vertically (shift down by extraY)
1014
- // Scale the node vertically to maintain original aspect ratio
1015
- const scaleX = width / tw;
1016
- const scaledTxHeight = th * scaleX;
1017
- extraY = (height - scaledTxHeight) / 2;
1018
- resizeModeScaleY = scaledTxHeight / height;
1019
- } else {
1020
- // Texture is taller than node (or equal)
1021
- // Center the node horizontally (shift right by extraX)
1022
- // Scale the node horizontally to maintain original aspect ratio
1023
- const scaleY = height / th;
1024
- const scaledTxWidth = tw * scaleY;
1025
- extraX = (width - scaledTxWidth) / 2;
1026
- resizeModeScaleX = scaledTxWidth / width;
1027
- }
1028
-
1029
- // Apply the extra translation and scale to the local transform
1030
- this.localTransform
1031
- .translate(extraX, extraY)
1032
- .scale(resizeModeScaleX, resizeModeScaleY);
1033
- }
1034
-
1035
- this.setUpdateType(UpdateType.Global);
1036
- }
1037
-
1038
- /**
1039
- * @todo: test for correct calculation flag
1040
- * @param delta
1041
- */
1042
- update(delta: number, parentClippingRect: RectWithValid): void {
1043
- if (this.updateType & UpdateType.ScaleRotate) {
1044
- this.updateScaleRotateTransform();
1045
- this.setUpdateType(UpdateType.Local);
1046
- }
1047
-
1048
- if (this.updateType & UpdateType.Local) {
1049
- this.updateLocalTransform();
1050
- this.setUpdateType(UpdateType.Global);
1051
- }
1052
-
1053
- const parent = this.props.parent;
1054
- let renderState = null;
1055
-
1056
- // Handle specific RTT updates at this node level
1057
- if (this.updateType & UpdateType.RenderTexture && this.rtt) {
1058
- this.hasRTTupdates = true;
1059
- }
1060
-
1061
- if (this.updateType & UpdateType.Global) {
1062
- assertTruthy(this.localTransform);
1063
-
1064
- if (this.parentHasRenderTexture === true && parent?.rtt === true) {
1065
- // we are at the start of the RTT chain, so we need to reset the globalTransform
1066
- // for correct RTT rendering
1067
- this.globalTransform = Matrix3d.identity();
1068
-
1069
- // Maintain a full scene global transform for bounds detection
1070
- this.sceneGlobalTransform = Matrix3d.copy(
1071
- parent?.globalTransform || Matrix3d.identity(),
1072
- ).multiply(this.localTransform);
1073
- } else if (
1074
- this.parentHasRenderTexture === true &&
1075
- parent?.rtt === false
1076
- ) {
1077
- // we're part of an RTT chain but our parent is not the main RTT node
1078
- // so we need to propogate the sceneGlobalTransform of the parent
1079
- // to maintain a full scene global transform for bounds detection
1080
- this.sceneGlobalTransform = Matrix3d.copy(
1081
- parent?.sceneGlobalTransform || this.localTransform,
1082
- ).multiply(this.localTransform);
1083
-
1084
- this.globalTransform = Matrix3d.copy(
1085
- parent?.globalTransform || this.localTransform,
1086
- this.globalTransform,
1087
- );
1088
- } else {
1089
- this.globalTransform = Matrix3d.copy(
1090
- parent?.globalTransform || this.localTransform,
1091
- this.globalTransform,
1092
- );
1093
- }
1094
-
1095
- if (parent !== null) {
1096
- this.globalTransform.multiply(this.localTransform);
1097
- }
1098
- this.calculateRenderCoords();
1099
- this.updateBoundingRect();
1100
-
1101
- this.setUpdateType(UpdateType.RenderState | UpdateType.Children);
1102
- this.childUpdateType |= UpdateType.Global;
1103
-
1104
- if (this.clipping === true) {
1105
- this.setUpdateType(UpdateType.Clipping | UpdateType.RenderBounds);
1106
- this.childUpdateType |= UpdateType.RenderBounds;
1107
- }
1108
- }
1109
-
1110
- if (this.updateType & UpdateType.RenderBounds) {
1111
- this.createRenderBounds();
1112
- this.setUpdateType(UpdateType.RenderState);
1113
- this.setUpdateType(UpdateType.Children);
1114
-
1115
- this.childUpdateType |= UpdateType.RenderBounds;
1116
- }
1117
-
1118
- if (this.updateType & UpdateType.RenderState) {
1119
- renderState = this.checkRenderBounds();
1120
- this.setUpdateType(UpdateType.IsRenderable);
1121
-
1122
- // if we're not going out of bounds, update the render state
1123
- // this is done so the update loop can finish before we mark a node
1124
- // as out of bounds
1125
- if (renderState !== CoreNodeRenderState.OutOfBounds) {
1126
- this.updateRenderState(renderState);
1127
- }
1128
- }
1129
-
1130
- if (this.updateType & UpdateType.WorldAlpha) {
1131
- if (parent) {
1132
- this.worldAlpha = parent.worldAlpha * this.props.alpha;
1133
- } else {
1134
- this.worldAlpha = this.props.alpha;
1135
- }
1136
- this.setUpdateType(
1137
- UpdateType.Children |
1138
- UpdateType.PremultipliedColors |
1139
- UpdateType.IsRenderable,
1140
- );
1141
- this.childUpdateType |= UpdateType.WorldAlpha;
1142
- }
1143
-
1144
- if (this.updateType & UpdateType.IsRenderable) {
1145
- this.updateIsRenderable();
1146
- }
1147
-
1148
- if (this.updateType & UpdateType.Clipping) {
1149
- this.calculateClippingRect(parentClippingRect);
1150
- this.setUpdateType(UpdateType.Children);
1151
-
1152
- this.childUpdateType |= UpdateType.Clipping;
1153
- this.childUpdateType |= UpdateType.RenderBounds;
1154
- }
1155
-
1156
- if (this.updateType & UpdateType.PremultipliedColors) {
1157
- this.premultipliedColorTl = mergeColorAlphaPremultiplied(
1158
- this.props.colorTl,
1159
- this.worldAlpha,
1160
- true,
1161
- );
1162
-
1163
- // If all the colors are the same just sent them all to the same value
1164
- if (
1165
- this.props.colorTl === this.props.colorTr &&
1166
- this.props.colorBl === this.props.colorBr &&
1167
- this.props.colorTl === this.props.colorBl
1168
- ) {
1169
- this.premultipliedColorTr =
1170
- this.premultipliedColorBl =
1171
- this.premultipliedColorBr =
1172
- this.premultipliedColorTl;
1173
- } else {
1174
- this.premultipliedColorTr = mergeColorAlphaPremultiplied(
1175
- this.props.colorTr,
1176
- this.worldAlpha,
1177
- true,
1178
- );
1179
- this.premultipliedColorBl = mergeColorAlphaPremultiplied(
1180
- this.props.colorBl,
1181
- this.worldAlpha,
1182
- true,
1183
- );
1184
- this.premultipliedColorBr = mergeColorAlphaPremultiplied(
1185
- this.props.colorBr,
1186
- this.worldAlpha,
1187
- true,
1188
- );
1189
- }
1190
- }
1191
-
1192
- // No need to update zIndex if there is no parent
1193
- if (parent !== null && this.updateType & UpdateType.CalculatedZIndex) {
1194
- this.calculateZIndex();
1195
- // Tell parent to re-sort children
1196
- parent.setUpdateType(UpdateType.ZIndexSortedChildren);
1197
- }
1198
-
1199
- if (
1200
- this.props.strictBounds === true &&
1201
- this.renderState === CoreNodeRenderState.OutOfBounds
1202
- ) {
1203
- this.updateType &= ~UpdateType.RenderBounds; // remove render bounds update
1204
- return;
1205
- }
1206
-
1207
- if (this.updateType & UpdateType.Children && this.children.length > 0) {
1208
- for (let i = 0, length = this.children.length; i < length; i++) {
1209
- const child = this.children[i] as CoreNode;
1210
-
1211
- child.setUpdateType(this.childUpdateType);
1212
-
1213
- if (child.updateType === 0) {
1214
- continue;
1215
- }
1216
-
1217
- let childClippingRect = this.clippingRect;
1218
- if (this.rtt === true) {
1219
- childClippingRect = {
1220
- x: 0,
1221
- y: 0,
1222
- width: 0,
1223
- height: 0,
1224
- valid: false,
1225
- };
1226
- }
1227
-
1228
- child.update(delta, childClippingRect);
1229
- }
1230
- }
1231
-
1232
- // If the node has an RTT parent and requires a texture re-render, inform the RTT parent
1233
- // if (this.parentHasRenderTexture && this.updateType & UpdateType.RenderTexture) {
1234
- // @TODO have a more scoped down updateType for RTT updates
1235
- if (this.parentHasRenderTexture && this.updateType > 0) {
1236
- this.notifyParentRTTOfUpdate();
1237
- }
1238
-
1239
- // Sorting children MUST happen after children have been updated so
1240
- // that they have the oppotunity to update their calculated zIndex.
1241
- if (this.updateType & UpdateType.ZIndexSortedChildren) {
1242
- // reorder z-index
1243
- this.sortChildren();
1244
- }
1245
-
1246
- // If we're out of bounds, apply the render state now
1247
- // this is done so nodes can finish their entire update loop before
1248
- // being marked as out of bounds
1249
- if (renderState === CoreNodeRenderState.OutOfBounds) {
1250
- this.updateRenderState(renderState);
1251
- this.updateIsRenderable();
1252
-
1253
- if (
1254
- this.rtt === true &&
1255
- renderState === CoreNodeRenderState.OutOfBounds
1256
- ) {
1257
- // notify children that we are going out of bounds
1258
- // we have to do this now before we stop processing the render tree
1259
- this.notifyChildrenRTTOfUpdate(renderState);
1260
- // this.childUpdateType |= UpdateType.RenderState;
1261
- }
1262
- }
1263
-
1264
- // reset update type
1265
- this.updateType = 0;
1266
- this.childUpdateType = 0;
1267
- }
1268
-
1269
- private findParentRTTNode(): CoreNode | null {
1270
- let rttNode: CoreNode | null = this.parent;
1271
- while (rttNode && !rttNode.rtt) {
1272
- rttNode = rttNode.parent;
1273
- }
1274
- return rttNode;
1275
- }
1276
-
1277
- private getRTTParentRenderState(): CoreNodeRenderState | null {
1278
- const rttNode = this.rttParent || this.findParentRTTNode();
1279
- if (!rttNode) {
1280
- return null;
1281
- }
1282
-
1283
- return rttNode.renderState;
1284
- }
1285
-
1286
- private notifyChildrenRTTOfUpdate(renderState: CoreNodeRenderState) {
1287
- for (const child of this.children) {
1288
- // force child to update render state
1289
- child.updateRenderState(renderState);
1290
- child.updateIsRenderable();
1291
- child.notifyChildrenRTTOfUpdate(renderState);
1292
- }
1293
- }
1294
-
1295
- private notifyParentRTTOfUpdate() {
1296
- if (this.parent === null) {
1297
- return;
1298
- }
1299
-
1300
- const rttNode = this.rttParent || this.findParentRTTNode();
1301
- if (!rttNode) {
1302
- return;
1303
- }
1304
-
1305
- // If an RTT node is found, mark it for re-rendering
1306
- rttNode.hasRTTupdates = true;
1307
- rttNode.setUpdateType(UpdateType.RenderTexture);
1308
-
1309
- // if rttNode is nested, also make it update its RTT parent
1310
- if (rttNode.parentHasRenderTexture === true) {
1311
- rttNode.notifyParentRTTOfUpdate();
1312
- }
1313
- }
1314
-
1315
- checkRenderBounds(): CoreNodeRenderState {
1316
- assertTruthy(this.renderBound);
1317
- assertTruthy(this.strictBound);
1318
- assertTruthy(this.preloadBound);
1319
-
1320
- if (boundInsideBound(this.renderBound, this.strictBound)) {
1321
- return CoreNodeRenderState.InViewport;
1322
- }
1323
-
1324
- if (boundInsideBound(this.renderBound, this.preloadBound)) {
1325
- return CoreNodeRenderState.InBounds;
1326
- }
1327
-
1328
- // check if we're larger then our parent, we're definitely in the viewport
1329
- if (boundLargeThanBound(this.renderBound, this.strictBound)) {
1330
- return CoreNodeRenderState.InViewport;
1331
- }
1332
-
1333
- // check if we dont have dimensions, take our parent's render state
1334
- if (
1335
- this.parent !== null &&
1336
- (this.props.width === 0 || this.props.height === 0)
1337
- ) {
1338
- return this.parent.renderState;
1339
- }
1340
-
1341
- return CoreNodeRenderState.OutOfBounds;
1342
- }
1343
-
1344
- updateBoundingRect() {
1345
- const transform = this.sceneGlobalTransform || this.globalTransform;
1346
- const renderCoords = this.sceneRenderCoords || this.renderCoords;
1347
-
1348
- assertTruthy(transform);
1349
- assertTruthy(renderCoords);
1350
-
1351
- const { tb, tc } = transform;
1352
- const { x1, y1, x3, y3 } = renderCoords;
1353
- if (tb === 0 || tc === 0) {
1354
- this.renderBound = createBound(x1, y1, x3, y3, this.renderBound);
1355
- } else {
1356
- const { x2, x4, y2, y4 } = renderCoords;
1357
- this.renderBound = createBound(
1358
- Math.min(x1, x2, x3, x4),
1359
- Math.min(y1, y2, y3, y4),
1360
- Math.max(x1, x2, x3, x4),
1361
- Math.max(y1, y2, y3, y4),
1362
- this.renderBound,
1363
- );
1364
- }
1365
- }
1366
-
1367
- createRenderBounds(): void {
1368
- assertTruthy(this.stage);
1369
-
1370
- if (this.parent !== null && this.parent.strictBound !== undefined) {
1371
- // we have a parent with a valid bound, copy it
1372
- const parentBound = this.parent.strictBound;
1373
- this.strictBound = createBound(
1374
- parentBound.x1,
1375
- parentBound.y1,
1376
- parentBound.x2,
1377
- parentBound.y2,
1378
- );
1379
-
1380
- this.preloadBound = createPreloadBounds(
1381
- this.strictBound,
1382
- this.boundsMargin as [number, number, number, number],
1383
- );
1384
- } else {
1385
- // no parent or parent does not have a bound, take the stage boundaries
1386
- this.strictBound = this.stage.strictBound;
1387
- this.preloadBound = this.stage.preloadBound;
1388
- }
1389
-
1390
- // if clipping is disabled, we're done
1391
- if (this.props.clipping === false) {
1392
- return;
1393
- }
1394
-
1395
- // only create local clipping bounds if node itself is in bounds
1396
- // this can only be done if we have a render bound already
1397
- if (this.renderBound === undefined) {
1398
- return;
1399
- }
1400
-
1401
- // if we're out of bounds, we're done
1402
- if (boundInsideBound(this.renderBound, this.strictBound) === false) {
1403
- return;
1404
- }
1405
-
1406
- // clipping is enabled and we are in bounds create our own bounds
1407
- const { x, y, width, height } = this.props;
1408
-
1409
- // Pick the global transform if available, otherwise use the local transform
1410
- // global transform is only available if the node in an RTT chain
1411
- const { tx, ty } = this.sceneGlobalTransform || this.globalTransform || {};
1412
- const _x = tx ?? x;
1413
- const _y = ty ?? y;
1414
- this.strictBound = createBound(
1415
- _x,
1416
- _y,
1417
- _x + width,
1418
- _y + height,
1419
- this.strictBound,
1420
- );
1421
-
1422
- this.preloadBound = createPreloadBounds(
1423
- this.strictBound,
1424
- this.boundsMargin as [number, number, number, number],
1425
- );
1426
- }
1427
-
1428
- updateRenderState(renderState: CoreNodeRenderState) {
1429
- if (renderState === this.renderState) {
1430
- return;
1431
- }
1432
-
1433
- const previous = this.renderState;
1434
- this.renderState = renderState;
1435
- const event = CoreNodeRenderStateMap.get(renderState);
1436
- assertTruthy(event);
1437
- this.emit(event, {
1438
- previous,
1439
- current: renderState,
1440
- });
1441
- }
1442
-
1443
- /**
1444
- * Updates the `isRenderable` property based on various conditions.
1445
- */
1446
- updateIsRenderable() {
1447
- let newIsRenderable = false;
1448
- let needsTextureOwnership = false;
1449
-
1450
- // If the node is out of bounds or has an alpha of 0, it is not renderable
1451
- if (this.checkBasicRenderability() === false) {
1452
- this.updateTextureOwnership(false);
1453
- this.setRenderable(false);
1454
- return;
1455
- }
1456
-
1457
- if (this.texture !== null) {
1458
- needsTextureOwnership = true;
1459
-
1460
- // we're only renderable if the texture state is loaded
1461
- newIsRenderable = this.texture.state === 'loaded';
1462
- } else if (
1463
- (this.hasShader() || this.hasColorProperties() === true) &&
1464
- this.hasDimensions() === true
1465
- ) {
1466
- // This mean we have dimensions and a color set, so we can render a ColorTexture
1467
- if (
1468
- this.stage.defaultTexture &&
1469
- this.stage.defaultTexture.state === 'loaded'
1470
- ) {
1471
- newIsRenderable = true;
1472
- }
1473
- }
1474
-
1475
- this.updateTextureOwnership(needsTextureOwnership);
1476
- this.setRenderable(newIsRenderable);
1477
- }
1478
-
1479
- /**
1480
- * Checks if the node is renderable based on world alpha, dimensions and out of bounds status.
1481
- */
1482
- checkBasicRenderability(): boolean {
1483
- if (this.worldAlpha === 0 || this.isOutOfBounds() === true) {
1484
- return false;
1485
- } else {
1486
- return true;
1487
- }
1488
- }
1489
-
1490
- /**
1491
- * Sets the renderable state and triggers changes if necessary.
1492
- * @param isRenderable - The new renderable state
1493
- */
1494
- setRenderable(isRenderable: boolean) {
1495
- this.isRenderable = isRenderable;
1496
- }
1497
-
1498
- /**
1499
- * Changes the renderable state of the node.
1500
- */
1501
- updateTextureOwnership(isRenderable: boolean) {
1502
- this.texture?.setRenderableOwner(this, isRenderable);
1503
- }
1504
-
1505
- /**
1506
- * Checks if the node is out of the viewport bounds.
1507
- */
1508
- isOutOfBounds(): boolean {
1509
- return this.renderState <= CoreNodeRenderState.OutOfBounds;
1510
- }
1511
-
1512
- /**
1513
- * Checks if the node has dimensions (width/height)
1514
- */
1515
- hasDimensions(): boolean {
1516
- return this.props.width !== 0 && this.props.height !== 0;
1517
- }
1518
-
1519
- /**
1520
- * Checks if the node has any color properties set.
1521
- */
1522
- hasColorProperties(): boolean {
1523
- return (
1524
- this.props.color !== 0 ||
1525
- this.props.colorTop !== 0 ||
1526
- this.props.colorBottom !== 0 ||
1527
- this.props.colorLeft !== 0 ||
1528
- this.props.colorRight !== 0 ||
1529
- this.props.colorTl !== 0 ||
1530
- this.props.colorTr !== 0 ||
1531
- this.props.colorBl !== 0 ||
1532
- this.props.colorBr !== 0
1533
- );
1534
- }
1535
-
1536
- hasShader(): boolean {
1537
- return this.props.shader !== null;
1538
- }
1539
-
1540
- calculateRenderCoords() {
1541
- const { width, height } = this;
1542
- const { tx, ty, ta, tb, tc, td } = this.globalTransform!;
1543
- if (tb === 0 && tc === 0) {
1544
- const minX = tx;
1545
- const maxX = tx + width * ta;
1546
- const minY = ty;
1547
- const maxY = ty + height * td;
1548
- this.renderCoords = RenderCoords.translate(
1549
- //top-left
1550
- minX,
1551
- minY,
1552
- //top-right
1553
- maxX,
1554
- minY,
1555
- //bottom-right
1556
- maxX,
1557
- maxY,
1558
- //bottom-left
1559
- minX,
1560
- maxY,
1561
- this.renderCoords,
1562
- );
1563
- } else {
1564
- this.renderCoords = RenderCoords.translate(
1565
- //top-left
1566
- tx,
1567
- ty,
1568
- //top-right
1569
- tx + width * ta,
1570
- ty + width * tc,
1571
- //bottom-right
1572
- tx + width * ta + height * tb,
1573
- ty + width * tc + height * td,
1574
- //bottom-left
1575
- tx + height * tb,
1576
- ty + height * td,
1577
- this.renderCoords,
1578
- );
1579
- }
1580
- if (this.sceneGlobalTransform === undefined) {
1581
- return;
1582
- }
1583
-
1584
- const {
1585
- tx: stx,
1586
- ty: sty,
1587
- ta: sta,
1588
- tb: stb,
1589
- tc: stc,
1590
- td: std,
1591
- } = this.sceneGlobalTransform;
1592
- if (stb === 0 && stc === 0) {
1593
- const minX = stx;
1594
- const maxX = stx + width * sta;
1595
- const minY = sty;
1596
- const maxY = sty + height * std;
1597
- this.sceneRenderCoords = RenderCoords.translate(
1598
- //top-left
1599
- minX,
1600
- minY,
1601
- //top-right
1602
- maxX,
1603
- minY,
1604
- //bottom-right
1605
- maxX,
1606
- maxY,
1607
- //bottom-left
1608
- minX,
1609
- maxY,
1610
- this.sceneRenderCoords,
1611
- );
1612
- } else {
1613
- this.sceneRenderCoords = RenderCoords.translate(
1614
- //top-left
1615
- stx,
1616
- sty,
1617
- //top-right
1618
- stx + width * sta,
1619
- sty + width * stc,
1620
- //bottom-right
1621
- stx + width * sta + height * stb,
1622
- sty + width * stc + height * std,
1623
- //bottom-left
1624
- stx + height * stb,
1625
- sty + height * std,
1626
- this.sceneRenderCoords,
1627
- );
1628
- }
1629
- }
1630
-
1631
- /**
1632
- * This function calculates the clipping rectangle for a node.
1633
- *
1634
- * The function then checks if the node is rotated. If the node requires clipping and is not rotated, a new clipping rectangle is created based on the node's global transform and dimensions.
1635
- * If a parent clipping rectangle exists, it is intersected with the node's clipping rectangle (if it exists), or replaces the node's clipping rectangle.
1636
- *
1637
- * Finally, the node's parentClippingRect and clippingRect properties are updated.
1638
- */
1639
- calculateClippingRect(parentClippingRect: RectWithValid) {
1640
- assertTruthy(this.globalTransform);
1641
- const { clippingRect, props, globalTransform: gt } = this;
1642
- const { clipping } = props;
1643
- const isRotated = gt.tb !== 0 || gt.tc !== 0;
1644
-
1645
- if (clipping === true && isRotated === false) {
1646
- clippingRect.x = gt.tx;
1647
- clippingRect.y = gt.ty;
1648
- clippingRect.width = this.width * gt.ta;
1649
- clippingRect.height = this.height * gt.td;
1650
- clippingRect.valid = true;
1651
- } else {
1652
- clippingRect.valid = false;
1653
- }
1654
-
1655
- if (parentClippingRect.valid === true && clippingRect.valid === true) {
1656
- // Intersect parent clipping rect with node clipping rect
1657
- intersectRect(parentClippingRect, clippingRect, clippingRect);
1658
- } else if (parentClippingRect.valid === true) {
1659
- // Copy parent clipping rect
1660
- copyRect(parentClippingRect, clippingRect);
1661
- clippingRect.valid = true;
1662
- }
1663
- }
1664
-
1665
- calculateZIndex(): void {
1666
- const props = this.props;
1667
- const z = props.zIndex || 0;
1668
- const p = props.parent?.zIndex || 0;
1669
-
1670
- let zIndex = z;
1671
- if (props.parent?.zIndexLocked) {
1672
- zIndex = z < p ? z : p;
1673
- }
1674
- this.calcZIndex = zIndex;
1675
- }
1676
-
1677
- /**
1678
- * Destroy the node and cleanup all resources
1679
- */
1680
- destroy(): void {
1681
- if (this.destroyed === true) {
1682
- return;
1683
- }
1684
-
1685
- this.destroyed = true;
1686
- this.unloadTexture();
1687
-
1688
- this.clippingRect.valid = false;
1689
- this.isRenderable = false;
1690
-
1691
- this.renderCoords = undefined;
1692
- this.renderBound = undefined;
1693
- this.strictBound = undefined;
1694
- this.preloadBound = undefined;
1695
- this.globalTransform = undefined;
1696
- this.scaleRotateTransform = undefined;
1697
- this.localTransform = undefined;
1698
-
1699
- this.props.texture = null;
1700
- this.props.shader = this.stage.defShaderCtr;
1701
-
1702
- while (this.children.length > 0) {
1703
- this.children[0]?.destroy();
1704
- }
1705
-
1706
- // This very action will also remove the node from the parent's children array
1707
- this.parent = null;
1708
-
1709
- if (this.rtt) {
1710
- this.stage.renderer.removeRTTNode(this);
1711
- }
1712
-
1713
- this.removeAllListeners();
1714
- }
1715
-
1716
- renderQuads(renderer: CoreRenderer): void {
1717
- // Prevent quad rendering if parent has a render texture
1718
- // and renderer is not currently rendering to a texture
1719
- if (this.parentHasRenderTexture) {
1720
- if (!renderer.renderToTextureActive) {
1721
- return;
1722
- }
1723
- // Prevent quad rendering if parent render texture is not the active render texture
1724
- if (this.parentRenderTexture !== renderer.activeRttNode) {
1725
- return;
1726
- }
1727
- }
1728
-
1729
- assertTruthy(this.globalTransform);
1730
- assertTruthy(this.renderCoords);
1731
-
1732
- // add to list of renderables to be sorted before rendering
1733
- renderer.addQuad({
1734
- width: this.props.width,
1735
- height: this.props.height,
1736
- colorTl: this.premultipliedColorTl,
1737
- colorTr: this.premultipliedColorTr,
1738
- colorBl: this.premultipliedColorBl,
1739
- colorBr: this.premultipliedColorBr,
1740
- // if we do not have a texture, use the default texture
1741
- // this assumes any renderable node is either a distinct texture or a ColorTexture
1742
- texture: this.texture || this.stage.defaultTexture,
1743
- textureOptions: this.textureOptions,
1744
- zIndex: this.zIndex,
1745
- shader: this.shader.shader,
1746
- shaderProps: this.shader.getResolvedProps(),
1747
- alpha: this.worldAlpha,
1748
- clippingRect: this.clippingRect,
1749
- tx: this.globalTransform.tx,
1750
- ty: this.globalTransform.ty,
1751
- ta: this.globalTransform.ta,
1752
- tb: this.globalTransform.tb,
1753
- tc: this.globalTransform.tc,
1754
- td: this.globalTransform.td,
1755
- renderCoords: this.renderCoords,
1756
- rtt: this.rtt,
1757
- parentHasRenderTexture: this.parentHasRenderTexture,
1758
- framebufferDimensions: this.framebufferDimensions,
1759
- });
1760
- }
1761
-
1762
- //#region Properties
1763
- get id(): number {
1764
- return this._id;
1765
- }
1766
-
1767
- get data(): CustomDataMap | undefined {
1768
- return this.props.data;
1769
- }
1770
-
1771
- set data(d: CustomDataMap | undefined) {
1772
- this.props.data = d;
1773
- }
1774
-
1775
- get x(): number {
1776
- return this.props.x;
1777
- }
1778
-
1779
- set x(value: number) {
1780
- if (this.props.x !== value) {
1781
- this.props.x = value;
1782
- this.setUpdateType(UpdateType.Local);
1783
- }
1784
- }
1785
-
1786
- get absX(): number {
1787
- return (
1788
- this.props.x +
1789
- -this.props.width * this.props.mountX +
1790
- (this.props.parent?.absX || this.props.parent?.globalTransform?.tx || 0)
1791
- );
1792
- }
1793
-
1794
- get absY(): number {
1795
- return (
1796
- this.props.y +
1797
- -this.props.height * this.props.mountY +
1798
- (this.props.parent?.absY ?? 0)
1799
- );
1800
- }
1801
-
1802
- get y(): number {
1803
- return this.props.y;
1804
- }
1805
-
1806
- set y(value: number) {
1807
- if (this.props.y !== value) {
1808
- this.props.y = value;
1809
- this.setUpdateType(UpdateType.Local);
1810
- }
1811
- }
1812
-
1813
- get width(): number {
1814
- return this.props.width;
1815
- }
1816
-
1817
- set width(value: number) {
1818
- if (this.props.width !== value) {
1819
- this.props.width = value;
1820
- this.setUpdateType(UpdateType.Local);
1821
-
1822
- if (this.props.rtt) {
1823
- this.texture = this.stage.txManager.createTexture('RenderTexture', {
1824
- width: this.width,
1825
- height: this.height,
1826
- });
1827
-
1828
- this.setUpdateType(UpdateType.RenderTexture);
1829
- }
1830
- }
1831
- }
1832
-
1833
- get height(): number {
1834
- return this.props.height;
1835
- }
1836
-
1837
- set height(value: number) {
1838
- if (this.props.height !== value) {
1839
- this.props.height = value;
1840
- this.setUpdateType(UpdateType.Local);
1841
-
1842
- if (this.props.rtt) {
1843
- this.texture = this.stage.txManager.createTexture('RenderTexture', {
1844
- width: this.width,
1845
- height: this.height,
1846
- });
1847
-
1848
- this.setUpdateType(UpdateType.RenderTexture);
1849
- }
1850
- }
1851
- }
1852
-
1853
- get scale(): number {
1854
- // The CoreNode `scale` property is only used by Animations.
1855
- // Unlike INode, `null` should never be possibility for Animations.
1856
- return this.scaleX;
1857
- }
1858
-
1859
- set scale(value: number) {
1860
- // The CoreNode `scale` property is only used by Animations.
1861
- // Unlike INode, `null` should never be possibility for Animations.
1862
- this.scaleX = value;
1863
- this.scaleY = value;
1864
- }
1865
-
1866
- get scaleX(): number {
1867
- return this.props.scaleX;
1868
- }
1869
-
1870
- set scaleX(value: number) {
1871
- if (this.props.scaleX !== value) {
1872
- this.props.scaleX = value;
1873
- this.setUpdateType(UpdateType.ScaleRotate);
1874
- }
1875
- }
1876
-
1877
- get scaleY(): number {
1878
- return this.props.scaleY;
1879
- }
1880
-
1881
- set scaleY(value: number) {
1882
- if (this.props.scaleY !== value) {
1883
- this.props.scaleY = value;
1884
- this.setUpdateType(UpdateType.ScaleRotate);
1885
- }
1886
- }
1887
-
1888
- get mount(): number {
1889
- return this.props.mount;
1890
- }
1891
-
1892
- set mount(value: number) {
1893
- if (this.props.mountX !== value || this.props.mountY !== value) {
1894
- this.props.mountX = value;
1895
- this.props.mountY = value;
1896
- this.props.mount = value;
1897
- this.setUpdateType(UpdateType.Local);
1898
- }
1899
- }
1900
-
1901
- get mountX(): number {
1902
- return this.props.mountX;
1903
- }
1904
-
1905
- set mountX(value: number) {
1906
- if (this.props.mountX !== value) {
1907
- this.props.mountX = value;
1908
- this.setUpdateType(UpdateType.Local);
1909
- }
1910
- }
1911
-
1912
- get mountY(): number {
1913
- return this.props.mountY;
1914
- }
1915
-
1916
- set mountY(value: number) {
1917
- if (this.props.mountY !== value) {
1918
- this.props.mountY = value;
1919
- this.setUpdateType(UpdateType.Local);
1920
- }
1921
- }
1922
-
1923
- get pivot(): number {
1924
- return this.props.pivot;
1925
- }
1926
-
1927
- set pivot(value: number) {
1928
- if (this.props.pivotX !== value || this.props.pivotY !== value) {
1929
- this.props.pivotX = value;
1930
- this.props.pivotY = value;
1931
- this.props.pivot = value;
1932
- this.setUpdateType(UpdateType.Local);
1933
- }
1934
- }
1935
-
1936
- get pivotX(): number {
1937
- return this.props.pivotX;
1938
- }
1939
-
1940
- set pivotX(value: number) {
1941
- if (this.props.pivotX !== value) {
1942
- this.props.pivotX = value;
1943
- this.setUpdateType(UpdateType.Local);
1944
- }
1945
- }
1946
-
1947
- get pivotY(): number {
1948
- return this.props.pivotY;
1949
- }
1950
-
1951
- set pivotY(value: number) {
1952
- if (this.props.pivotY !== value) {
1953
- this.props.pivotY = value;
1954
- this.setUpdateType(UpdateType.Local);
1955
- }
1956
- }
1957
-
1958
- get rotation(): number {
1959
- return this.props.rotation;
1960
- }
1961
-
1962
- set rotation(value: number) {
1963
- if (this.props.rotation !== value) {
1964
- this.props.rotation = value;
1965
- this.setUpdateType(UpdateType.ScaleRotate);
1966
- }
1967
- }
1968
-
1969
- get alpha(): number {
1970
- return this.props.alpha;
1971
- }
1972
-
1973
- set alpha(value: number) {
1974
- this.props.alpha = value;
1975
- this.setUpdateType(
1976
- UpdateType.PremultipliedColors |
1977
- UpdateType.WorldAlpha |
1978
- UpdateType.Children |
1979
- UpdateType.IsRenderable,
1980
- );
1981
- this.childUpdateType |= UpdateType.WorldAlpha;
1982
- }
1983
-
1984
- get autosize(): boolean {
1985
- return this.props.autosize;
1986
- }
1987
-
1988
- set autosize(value: boolean) {
1989
- this.props.autosize = value;
1990
- }
1991
-
1992
- get boundsMargin(): number | [number, number, number, number] | null {
1993
- return (
1994
- this.props.boundsMargin ??
1995
- this.parent?.boundsMargin ??
1996
- this.stage.boundsMargin
1997
- );
1998
- }
1999
-
2000
- set boundsMargin(value: number | [number, number, number, number] | null) {
2001
- if (value === this.props.boundsMargin) {
2002
- return;
2003
- }
2004
-
2005
- if (value === null) {
2006
- this.props.boundsMargin = value;
2007
- } else {
2008
- const bm: [number, number, number, number] = Array.isArray(value)
2009
- ? value
2010
- : [value, value, value, value];
2011
-
2012
- this.props.boundsMargin = bm;
2013
- }
2014
- this.setUpdateType(UpdateType.RenderBounds);
2015
- }
2016
-
2017
- get clipping(): boolean {
2018
- return this.props.clipping;
2019
- }
2020
-
2021
- set clipping(value: boolean) {
2022
- this.props.clipping = value;
2023
- this.setUpdateType(
2024
- UpdateType.Clipping | UpdateType.RenderBounds | UpdateType.Children,
2025
- );
2026
- this.childUpdateType |= UpdateType.Global | UpdateType.Clipping;
2027
- }
2028
-
2029
- get color(): number {
2030
- return this.props.color;
2031
- }
2032
-
2033
- set color(value: number) {
2034
- this.colorTop = value;
2035
- this.colorBottom = value;
2036
- this.colorLeft = value;
2037
- this.colorRight = value;
2038
- this.props.color = value;
2039
-
2040
- this.setUpdateType(UpdateType.PremultipliedColors);
2041
- }
2042
-
2043
- get colorTop(): number {
2044
- return this.props.colorTop;
2045
- }
2046
-
2047
- set colorTop(value: number) {
2048
- if (this.props.colorTl !== value || this.props.colorTr !== value) {
2049
- this.colorTl = value;
2050
- this.colorTr = value;
2051
- }
2052
- this.props.colorTop = value;
2053
- this.setUpdateType(UpdateType.PremultipliedColors);
2054
- }
2055
-
2056
- get colorBottom(): number {
2057
- return this.props.colorBottom;
2058
- }
2059
-
2060
- set colorBottom(value: number) {
2061
- if (this.props.colorBl !== value || this.props.colorBr !== value) {
2062
- this.colorBl = value;
2063
- this.colorBr = value;
2064
- }
2065
- this.props.colorBottom = value;
2066
- this.setUpdateType(UpdateType.PremultipliedColors);
2067
- }
2068
-
2069
- get colorLeft(): number {
2070
- return this.props.colorLeft;
2071
- }
2072
-
2073
- set colorLeft(value: number) {
2074
- if (this.props.colorTl !== value || this.props.colorBl !== value) {
2075
- this.colorTl = value;
2076
- this.colorBl = value;
2077
- }
2078
- this.props.colorLeft = value;
2079
- this.setUpdateType(UpdateType.PremultipliedColors);
2080
- }
2081
-
2082
- get colorRight(): number {
2083
- return this.props.colorRight;
2084
- }
2085
-
2086
- set colorRight(value: number) {
2087
- if (this.props.colorTr !== value || this.props.colorBr !== value) {
2088
- this.colorTr = value;
2089
- this.colorBr = value;
2090
- }
2091
- this.props.colorRight = value;
2092
- this.setUpdateType(UpdateType.PremultipliedColors);
2093
- }
2094
-
2095
- get colorTl(): number {
2096
- return this.props.colorTl;
2097
- }
2098
-
2099
- set colorTl(value: number) {
2100
- this.props.colorTl = value;
2101
- this.setUpdateType(UpdateType.PremultipliedColors);
2102
- }
2103
-
2104
- get colorTr(): number {
2105
- return this.props.colorTr;
2106
- }
2107
-
2108
- set colorTr(value: number) {
2109
- this.props.colorTr = value;
2110
- this.setUpdateType(UpdateType.PremultipliedColors);
2111
- }
2112
-
2113
- get colorBl(): number {
2114
- return this.props.colorBl;
2115
- }
2116
-
2117
- set colorBl(value: number) {
2118
- this.props.colorBl = value;
2119
- this.setUpdateType(UpdateType.PremultipliedColors);
2120
- }
2121
-
2122
- get colorBr(): number {
2123
- return this.props.colorBr;
2124
- }
2125
-
2126
- set colorBr(value: number) {
2127
- this.props.colorBr = value;
2128
- this.setUpdateType(UpdateType.PremultipliedColors);
2129
- }
2130
-
2131
- // we're only interested in parent zIndex to test
2132
- // if we should use node zIndex is higher then parent zIndex
2133
- get zIndexLocked(): number {
2134
- return this.props.zIndexLocked || 0;
2135
- }
2136
-
2137
- set zIndexLocked(value: number) {
2138
- this.props.zIndexLocked = value;
2139
- this.setUpdateType(UpdateType.CalculatedZIndex | UpdateType.Children);
2140
- for (let i = 0, length = this.children.length; i < length; i++) {
2141
- this.children[i]!.setUpdateType(UpdateType.CalculatedZIndex);
2142
- }
2143
- }
2144
-
2145
- get zIndex(): number {
2146
- return this.props.zIndex;
2147
- }
2148
-
2149
- set zIndex(value: number) {
2150
- this.props.zIndex = value;
2151
- this.setUpdateType(UpdateType.CalculatedZIndex | UpdateType.Children);
2152
- for (let i = 0, length = this.children.length; i < length; i++) {
2153
- this.children[i]!.setUpdateType(UpdateType.CalculatedZIndex);
2154
- }
2155
- }
2156
-
2157
- get parent(): CoreNode | null {
2158
- return this.props.parent;
2159
- }
2160
-
2161
- set parent(newParent: CoreNode | null) {
2162
- const oldParent = this.props.parent;
2163
- if (oldParent === newParent) {
2164
- return;
2165
- }
2166
- this.props.parent = newParent;
2167
- if (oldParent) {
2168
- const index = oldParent.children.indexOf(this);
2169
- assertTruthy(
2170
- index !== -1,
2171
- "CoreNode.parent: Node not found in old parent's children!",
2172
- );
2173
- oldParent.children.splice(index, 1);
2174
- oldParent.setUpdateType(
2175
- UpdateType.Children | UpdateType.ZIndexSortedChildren,
2176
- );
2177
- }
2178
- if (newParent) {
2179
- newParent.children.push(this);
2180
- // Since this node has a new parent, to be safe, have it do a full update.
2181
- this.setUpdateType(UpdateType.All);
2182
- // Tell parent that it's children need to be updated and sorted.
2183
- newParent.setUpdateType(
2184
- UpdateType.Children | UpdateType.ZIndexSortedChildren,
2185
- );
2186
-
2187
- // If the new parent has an RTT enabled, apply RTT inheritance
2188
- if (newParent.rtt || newParent.parentHasRenderTexture) {
2189
- this.applyRTTInheritance(newParent);
2190
- }
2191
- }
2192
- this.updateScaleRotateTransform();
2193
-
2194
- // fetch render bounds from parent
2195
- this.setUpdateType(UpdateType.RenderBounds | UpdateType.Children);
2196
- }
2197
-
2198
- get preventCleanup(): boolean {
2199
- return this.props.textureOptions.preventCleanup || false;
2200
- }
2201
-
2202
- set preventCleanup(value: boolean) {
2203
- if (isProductionEnvironment() === false) {
2204
- console.warn(
2205
- 'CoreNode.preventCleanup: Is deprecated and will be removed in upcoming release, please use textureOptions.preventCleanup instead',
2206
- );
2207
- }
2208
-
2209
- this.props.textureOptions.preventCleanup = value;
2210
- }
2211
-
2212
- get rtt(): boolean {
2213
- return this.props.rtt;
2214
- }
2215
-
2216
- set rtt(value: boolean) {
2217
- if (this.props.rtt === value) {
2218
- return;
2219
- }
2220
- this.props.rtt = value;
2221
-
2222
- if (value === true) {
2223
- this.initRenderTexture();
2224
- this.markChildrenWithRTT();
2225
- } else {
2226
- this.cleanupRenderTexture();
2227
- }
2228
-
2229
- this.setUpdateType(UpdateType.RenderTexture);
2230
-
2231
- if (this.parentHasRenderTexture === true) {
2232
- this.notifyParentRTTOfUpdate();
2233
- }
2234
- }
2235
- private initRenderTexture() {
2236
- this.texture = this.stage.txManager.createTexture('RenderTexture', {
2237
- width: this.width,
2238
- height: this.height,
2239
- });
2240
-
2241
- this.stage.renderer.renderToTexture(this);
2242
- }
2243
-
2244
- private cleanupRenderTexture() {
2245
- this.unloadTexture();
2246
- this.clearRTTInheritance();
2247
-
2248
- this.hasRTTupdates = false;
2249
- this.texture = null;
2250
- }
2251
-
2252
- private markChildrenWithRTT(node: CoreNode | null = null) {
2253
- const parent = node || this;
2254
-
2255
- for (const child of parent.children) {
2256
- child.setUpdateType(UpdateType.All);
2257
- child.parentHasRenderTexture = true;
2258
- child.markChildrenWithRTT();
2259
- }
2260
- }
2261
-
2262
- // Apply RTT inheritance when a node has an RTT-enabled parent
2263
- private applyRTTInheritance(parent: CoreNode) {
2264
- if (parent.rtt) {
2265
- // Only the RTT node should be added to `renderToTexture`
2266
- parent.setUpdateType(UpdateType.RenderTexture);
2267
- }
2268
-
2269
- // Propagate `parentHasRenderTexture` downwards
2270
- this.markChildrenWithRTT(parent);
2271
- }
2272
-
2273
- // Clear RTT inheritance when detaching from an RTT chain
2274
- private clearRTTInheritance() {
2275
- // if this node is RTT itself stop the propagation important for nested RTT nodes
2276
- // for the initial RTT node this is already handled in `set rtt`
2277
- if (this.rtt) {
2278
- return;
2279
- }
2280
-
2281
- for (const child of this.children) {
2282
- // force child to update everything as the RTT inheritance has changed
2283
- child.parentHasRenderTexture = false;
2284
- child.rttParent = null;
2285
- child.setUpdateType(UpdateType.All);
2286
- child.clearRTTInheritance();
2287
- }
2288
- }
2289
-
2290
- get shader(): BaseShaderController {
2291
- return this.props.shader;
2292
- }
2293
-
2294
- set shader(value: BaseShaderController) {
2295
- if (this.props.shader === value) {
2296
- return;
2297
- }
2298
-
2299
- this.props.shader = value;
2300
-
2301
- this.setUpdateType(UpdateType.IsRenderable);
2302
- }
2303
-
2304
- get src(): string | null {
2305
- return this.props.src;
2306
- }
2307
-
2308
- set src(imageUrl: string | null) {
2309
- if (this.props.src === imageUrl) {
2310
- return;
2311
- }
2312
-
2313
- this.props.src = imageUrl;
2314
-
2315
- if (!imageUrl) {
2316
- this.texture = null;
2317
- return;
2318
- }
2319
-
2320
- this.texture = this.stage.txManager.createTexture('ImageTexture', {
2321
- src: imageUrl,
2322
- width: this.props.width,
2323
- height: this.props.height,
2324
- type: this.props.imageType,
2325
- sx: this.props.srcX,
2326
- sy: this.props.srcY,
2327
- sw: this.props.srcWidth,
2328
- sh: this.props.srcHeight,
2329
- });
2330
- }
2331
-
2332
- set imageType(type: 'regular' | 'compressed' | 'svg' | null) {
2333
- if (this.props.imageType === type) {
2334
- return;
2335
- }
2336
-
2337
- this.props.imageType = type;
2338
- }
2339
-
2340
- get imageType() {
2341
- return this.props.imageType || null;
2342
- }
2343
-
2344
- get srcHeight(): number | undefined {
2345
- return this.props.srcHeight;
2346
- }
2347
-
2348
- set srcHeight(value: number) {
2349
- this.props.srcHeight = value;
2350
- }
2351
-
2352
- get srcWidth(): number | undefined {
2353
- return this.props.srcWidth;
2354
- }
2355
-
2356
- set srcWidth(value: number) {
2357
- this.props.srcWidth = value;
2358
- }
2359
-
2360
- get srcX(): number | undefined {
2361
- return this.props.srcX;
2362
- }
2363
-
2364
- set srcX(value: number) {
2365
- this.props.srcX = value;
2366
- }
2367
-
2368
- get srcY(): number | undefined {
2369
- return this.props.srcY;
2370
- }
2371
-
2372
- set srcY(value: number) {
2373
- this.props.srcY = value;
2374
- }
2375
-
2376
- /**
2377
- * Returns the framebuffer dimensions of the node.
2378
- * If the node has a render texture, the dimensions are the same as the node's dimensions.
2379
- * If the node does not have a render texture, the dimensions are inherited from the parent.
2380
- * If the node parent has a render texture and the node is a render texture, the nodes dimensions are used.
2381
- */
2382
- get framebufferDimensions(): Dimensions {
2383
- if (this.parentHasRenderTexture && !this.rtt && this.parent) {
2384
- return this.parent.framebufferDimensions;
2385
- }
2386
- return { width: this.width, height: this.height };
2387
- }
2388
-
2389
- /**
2390
- * Returns the parent render texture node if it exists.
2391
- */
2392
- get parentRenderTexture(): CoreNode | null {
2393
- let parent = this.parent;
2394
- while (parent) {
2395
- if (parent.rtt) {
2396
- return parent;
2397
- }
2398
- parent = parent.parent;
2399
- }
2400
- return null;
2401
- }
2402
-
2403
- get texture(): Texture | null {
2404
- return this.props.texture;
2405
- }
2406
-
2407
- set texture(value: Texture | null) {
2408
- if (this.props.texture === value) {
2409
- return;
2410
- }
2411
-
2412
- const oldTexture = this.props.texture;
2413
- if (oldTexture) {
2414
- oldTexture.setRenderableOwner(this, false);
2415
- this.unloadTexture();
2416
- }
2417
-
2418
- this.props.texture = value;
2419
- if (value !== null) {
2420
- value.setRenderableOwner(this, this.isRenderable);
2421
- this.loadTexture();
2422
- }
2423
-
2424
- this.setUpdateType(UpdateType.IsRenderable);
2425
- }
2426
-
2427
- set textureOptions(value: TextureOptions) {
2428
- this.props.textureOptions = value;
2429
- }
2430
-
2431
- get textureOptions(): TextureOptions {
2432
- return this.props.textureOptions;
2433
- }
2434
-
2435
- get strictBounds(): boolean {
2436
- return this.props.strictBounds;
2437
- }
2438
-
2439
- set strictBounds(v) {
2440
- if (v === this.props.strictBounds) {
2441
- return;
2442
- }
2443
-
2444
- this.props.strictBounds = v;
2445
- this.setUpdateType(UpdateType.RenderBounds | UpdateType.Children);
2446
- this.childUpdateType |= UpdateType.RenderBounds | UpdateType.Children;
2447
- }
2448
-
2449
- animate(
2450
- props: Partial<CoreNodeAnimateProps>,
2451
- settings: Partial<AnimationSettings>,
2452
- ): IAnimationController {
2453
- const animation = new CoreAnimation(this, props, settings);
2454
-
2455
- const controller = new CoreAnimationController(
2456
- this.stage.animationManager,
2457
- animation,
2458
- );
2459
-
2460
- return controller;
2461
- }
2462
-
2463
- flush() {
2464
- // no-op
2465
- }
2466
-
2467
- //#endregion Properties
2468
- }
1
+ /*
2
+ * If not stated otherwise in this file or this component's LICENSE file the
3
+ * following copyright and licenses apply:
4
+ *
5
+ * Copyright 2023 Comcast Cable Communications Management, LLC.
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the License);
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ import {
21
+ assertTruthy,
22
+ getNewId,
23
+ isProductionEnvironment,
24
+ mergeColorAlphaPremultiplied,
25
+ } from '../utils.js';
26
+ import type { TextureOptions } from './CoreTextureManager.js';
27
+ import type { CoreRenderer } from './renderers/CoreRenderer.js';
28
+ import type { Stage } from './Stage.js';
29
+ import {
30
+ type Texture,
31
+ type TextureFailedEventHandler,
32
+ type TextureFreedEventHandler,
33
+ type TextureLoadedEventHandler,
34
+ } from './textures/Texture.js';
35
+ import type {
36
+ Dimensions,
37
+ NodeTextureFailedPayload,
38
+ NodeTextureFreedPayload,
39
+ NodeTextureLoadedPayload,
40
+ } from '../common/CommonTypes.js';
41
+ import { EventEmitter } from '../common/EventEmitter.js';
42
+ import {
43
+ copyRect,
44
+ intersectRect,
45
+ type Bound,
46
+ type RectWithValid,
47
+ createBound,
48
+ boundInsideBound,
49
+ boundLargeThanBound,
50
+ createPreloadBounds,
51
+ } from './lib/utils.js';
52
+ import { Matrix3d } from './lib/Matrix3d.js';
53
+ import { RenderCoords } from './lib/RenderCoords.js';
54
+ import type { AnimationSettings } from './animations/CoreAnimation.js';
55
+ import type { IAnimationController } from '../common/IAnimationController.js';
56
+ import { CoreAnimation } from './animations/CoreAnimation.js';
57
+ import { CoreAnimationController } from './animations/CoreAnimationController.js';
58
+ import type { BaseShaderController } from '../main-api/ShaderController.js';
59
+
60
+ export enum CoreNodeRenderState {
61
+ Init = 0,
62
+ OutOfBounds = 2,
63
+ InBounds = 4,
64
+ InViewport = 8,
65
+ }
66
+
67
+ const CoreNodeRenderStateMap: Map<CoreNodeRenderState, string> = new Map();
68
+ CoreNodeRenderStateMap.set(CoreNodeRenderState.Init, 'init');
69
+ CoreNodeRenderStateMap.set(CoreNodeRenderState.OutOfBounds, 'outOfBounds');
70
+ CoreNodeRenderStateMap.set(CoreNodeRenderState.InBounds, 'inBounds');
71
+ CoreNodeRenderStateMap.set(CoreNodeRenderState.InViewport, 'inViewport');
72
+
73
+ export enum UpdateType {
74
+ /**
75
+ * Child updates
76
+ */
77
+ Children = 1,
78
+
79
+ /**
80
+ * Scale/Rotate transform update
81
+ *
82
+ * @remarks
83
+ * CoreNode Properties Updated:
84
+ * - `scaleRotateTransform`
85
+ */
86
+ ScaleRotate = 2,
87
+
88
+ /**
89
+ * Translate transform update (x/y/width/height/pivot/mount)
90
+ *
91
+ * @remarks
92
+ * CoreNode Properties Updated:
93
+ * - `localTransform`
94
+ */
95
+ Local = 4,
96
+
97
+ /**
98
+ * Global Transform update
99
+ *
100
+ * @remarks
101
+ * CoreNode Properties Updated:
102
+ * - `globalTransform`
103
+ * - `renderCoords`
104
+ * - `renderBound`
105
+ */
106
+ Global = 8,
107
+
108
+ /**
109
+ * Clipping rect update
110
+ *
111
+ * @remarks
112
+ * CoreNode Properties Updated:
113
+ * - `clippingRect`
114
+ */
115
+ Clipping = 16,
116
+
117
+ /**
118
+ * Calculated ZIndex update
119
+ *
120
+ * @remarks
121
+ * CoreNode Properties Updated:
122
+ * - `calcZIndex`
123
+ */
124
+ CalculatedZIndex = 32,
125
+
126
+ /**
127
+ * Z-Index Sorted Children update
128
+ *
129
+ * @remarks
130
+ * CoreNode Properties Updated:
131
+ * - `children` (sorts children by their `calcZIndex`)
132
+ */
133
+ ZIndexSortedChildren = 64,
134
+
135
+ /**
136
+ * Premultiplied Colors update
137
+ *
138
+ * @remarks
139
+ * CoreNode Properties Updated:
140
+ * - `premultipliedColorTl`
141
+ * - `premultipliedColorTr`
142
+ * - `premultipliedColorBl`
143
+ * - `premultipliedColorBr`
144
+ */
145
+ PremultipliedColors = 128,
146
+
147
+ /**
148
+ * World Alpha update
149
+ *
150
+ * @remarks
151
+ * CoreNode Properties Updated:
152
+ * - `worldAlpha` = `parent.worldAlpha` * `alpha`
153
+ */
154
+ WorldAlpha = 256,
155
+
156
+ /**
157
+ * Render State update
158
+ *
159
+ * @remarks
160
+ * CoreNode Properties Updated:
161
+ * - `renderState`
162
+ */
163
+ RenderState = 512,
164
+
165
+ /**
166
+ * Is Renderable update
167
+ *
168
+ * @remarks
169
+ * CoreNode Properties Updated:
170
+ * - `isRenderable`
171
+ */
172
+ IsRenderable = 1024,
173
+
174
+ /**
175
+ * Render Texture update
176
+ */
177
+ RenderTexture = 2048,
178
+
179
+ /**
180
+ * Track if parent has render texture
181
+ */
182
+ ParentRenderTexture = 4096,
183
+
184
+ /**
185
+ * Render Bounds update
186
+ */
187
+ RenderBounds = 8192,
188
+
189
+ /**
190
+ * None
191
+ */
192
+ None = 0,
193
+
194
+ /**
195
+ * All
196
+ */
197
+ All = 14335,
198
+ }
199
+
200
+ /**
201
+ * A custom data map which can be stored on an CoreNode
202
+ *
203
+ * @remarks
204
+ * This is a map of key-value pairs that can be stored on an INode. It is used
205
+ * to store custom data that can be used by the application.
206
+ * The data stored can only be of type string, number or boolean.
207
+ */
208
+ export type CustomDataMap = {
209
+ [key: string]: string | number | boolean | undefined;
210
+ };
211
+
212
+ /**
213
+ * Writable properties of a Node.
214
+ */
215
+ export interface CoreNodeProps {
216
+ /**
217
+ * The x coordinate of the Node's Mount Point.
218
+ *
219
+ * @remarks
220
+ * See {@link mountX} and {@link mountY} for more information about setting
221
+ * the Mount Point.
222
+ *
223
+ * @default `0`
224
+ */
225
+ x: number;
226
+ /**
227
+ * The y coordinate of the Node's Mount Point.
228
+ *
229
+ * @remarks
230
+ * See {@link mountX} and {@link mountY} for more information about setting
231
+ * the Mount Point.
232
+ *
233
+ * @default `0`
234
+ */
235
+ y: number;
236
+ /**
237
+ * The width of the Node.
238
+ *
239
+ * @default `0`
240
+ */
241
+ width: number;
242
+ /**
243
+ * The height of the Node.
244
+ *
245
+ * @default `0`
246
+ */
247
+ height: number;
248
+ /**
249
+ * The alpha opacity of the Node.
250
+ *
251
+ * @remarks
252
+ * The alpha value is a number between 0 and 1, where 0 is fully transparent
253
+ * and 1 is fully opaque.
254
+ *
255
+ * @default `1`
256
+ */
257
+ alpha: number;
258
+ /**
259
+ * Autosize mode
260
+ *
261
+ * @remarks
262
+ * When enabled, when a texture is loaded into the Node, the Node will
263
+ * automatically resize to the dimensions of the texture.
264
+ *
265
+ * Text Nodes are always autosized based on their text content regardless
266
+ * of this mode setting.
267
+ *
268
+ * @default `false`
269
+ */
270
+ autosize: boolean;
271
+ /**
272
+ * Margin around the Node's bounds for preloading
273
+ *
274
+ * @default `null`
275
+ */
276
+ boundsMargin: number | [number, number, number, number] | null;
277
+ /**
278
+ * Clipping Mode
279
+ *
280
+ * @remarks
281
+ * Enable Clipping Mode when you want to prevent the drawing of a Node and
282
+ * its descendants from overflowing outside of the Node's x/y/width/height
283
+ * bounds.
284
+ *
285
+ * For WebGL, clipping is implemented using the high-performance WebGL
286
+ * operation scissor. As a consequence, clipping does not work for
287
+ * non-rectangular areas. So, if the element is rotated
288
+ * (by itself or by any of its ancestors), clipping will not work as intended.
289
+ *
290
+ * TODO: Add support for non-rectangular clipping either automatically or
291
+ * via Render-To-Texture.
292
+ *
293
+ * @default `false`
294
+ */
295
+ clipping: boolean;
296
+ /**
297
+ * The color of the Node.
298
+ *
299
+ * @remarks
300
+ * The color value is a number in the format 0xRRGGBBAA, where RR is the red
301
+ * component, GG is the green component, BB is the blue component, and AA is
302
+ * the alpha component.
303
+ *
304
+ * Gradient colors may be set by setting the different color sub-properties:
305
+ * {@link colorTop}, {@link colorBottom}, {@link colorLeft}, {@link colorRight},
306
+ * {@link colorTl}, {@link colorTr}, {@link colorBr}, {@link colorBl} accordingly.
307
+ *
308
+ * @default `0xffffffff` (opaque white)
309
+ */
310
+ color: number;
311
+ /**
312
+ * The color of the top edge of the Node for gradient rendering.
313
+ *
314
+ * @remarks
315
+ * See {@link color} for more information about color values and gradient
316
+ * rendering.
317
+ */
318
+ colorTop: number;
319
+ /**
320
+ * The color of the bottom edge of the Node for gradient rendering.
321
+ *
322
+ * @remarks
323
+ * See {@link color} for more information about color values and gradient
324
+ * rendering.
325
+ */
326
+ colorBottom: number;
327
+ /**
328
+ * The color of the left edge of the Node for gradient rendering.
329
+ *
330
+ * @remarks
331
+ * See {@link color} for more information about color values and gradient
332
+ * rendering.
333
+ */
334
+ colorLeft: number;
335
+ /**
336
+ * The color of the right edge of the Node for gradient rendering.
337
+ *
338
+ * @remarks
339
+ * See {@link color} for more information about color values and gradient
340
+ * rendering.
341
+ */
342
+ colorRight: number;
343
+ /**
344
+ * The color of the top-left corner of the Node for gradient rendering.
345
+ *
346
+ * @remarks
347
+ * See {@link color} for more information about color values and gradient
348
+ * rendering.
349
+ */
350
+ colorTl: number;
351
+ /**
352
+ * The color of the top-right corner of the Node for gradient rendering.
353
+ *
354
+ * @remarks
355
+ * See {@link color} for more information about color values and gradient
356
+ * rendering.
357
+ */
358
+ colorTr: number;
359
+ /**
360
+ * The color of the bottom-right corner of the Node for gradient rendering.
361
+ *
362
+ * @remarks
363
+ * See {@link color} for more information about color values and gradient
364
+ * rendering.
365
+ */
366
+ colorBr: number;
367
+ /**
368
+ * The color of the bottom-left corner of the Node for gradient rendering.
369
+ *
370
+ * @remarks
371
+ * See {@link color} for more information about color values and gradient
372
+ * rendering.
373
+ */
374
+ colorBl: number;
375
+ /**
376
+ * The Node's parent Node.
377
+ *
378
+ * @remarks
379
+ * The value `null` indicates that the Node has no parent. This may either be
380
+ * because the Node is the root Node of the scene graph, or because the Node
381
+ * has been removed from the scene graph.
382
+ *
383
+ * In order to make sure that a Node can be rendered on the screen, it must
384
+ * be added to the scene graph by setting it's parent property to a Node that
385
+ * is already in the scene graph such as the root Node.
386
+ *
387
+ * @default `null`
388
+ */
389
+ parent: CoreNode | null;
390
+ /**
391
+ * The Node's z-index.
392
+ *
393
+ * @remarks
394
+ * TBD
395
+ */
396
+ zIndex: number;
397
+ /**
398
+ * The Node's Texture.
399
+ *
400
+ * @remarks
401
+ * The `texture` defines a rasterized image that is contained within the
402
+ * {@link width} and {@link height} dimensions of the Node. If null, the
403
+ * Node will use an opaque white {@link ColorTexture} when being drawn, which
404
+ * essentially enables colors (including gradients) to be drawn.
405
+ *
406
+ * If set, by default, the texture will be drawn, as is, stretched to the
407
+ * dimensions of the Node. This behavior can be modified by setting the TBD
408
+ * and TBD properties.
409
+ *
410
+ * To create a Texture in order to set it on this property, call
411
+ * {@link RendererMain.createTexture}.
412
+ *
413
+ * If the {@link src} is set on a Node, the Node will use the
414
+ * {@link ImageTexture} by default and the Node will simply load the image at
415
+ * the specified URL.
416
+ *
417
+ * Note: If this is a Text Node, the Texture will be managed by the Node's
418
+ * {@link TextRenderer} and should not be set explicitly.
419
+ */
420
+ texture: Texture | null;
421
+
422
+ /**
423
+ * [Deprecated]: Prevents the texture from being cleaned up when the Node is removed
424
+ *
425
+ * @remarks
426
+ * Please use the `preventCleanup` property on {@link TextureOptions} instead.
427
+ *
428
+ * @default false
429
+ */
430
+ preventCleanup: boolean;
431
+ /**
432
+ * Options to associate with the Node's Texture
433
+ */
434
+ textureOptions: TextureOptions;
435
+
436
+ /**
437
+ * The Node's shader
438
+ *
439
+ * @remarks
440
+ * The `shader` defines a {@link Shader} used to draw the Node. By default,
441
+ * the Default Shader is used which simply draws the defined {@link texture}
442
+ * or {@link color}(s) within the Node without any special effects.
443
+ *
444
+ * To create a Shader in order to set it on this property, call
445
+ * {@link RendererMain.createShader}.
446
+ *
447
+ * Note: If this is a Text Node, the Shader will be managed by the Node's
448
+ * {@link TextRenderer} and should not be set explicitly.
449
+ */
450
+ shader: BaseShaderController;
451
+ /**
452
+ * Image URL
453
+ *
454
+ * @remarks
455
+ * When set, the Node's {@link texture} is automatically set to an
456
+ * {@link ImageTexture} using the source image URL provided (with all other
457
+ * settings being defaults)
458
+ */
459
+ src: string | null;
460
+ zIndexLocked: number;
461
+ /**
462
+ * Scale to render the Node at
463
+ *
464
+ * @remarks
465
+ * The scale value multiplies the provided {@link width} and {@link height}
466
+ * of the Node around the Node's Pivot Point (defined by the {@link pivot}
467
+ * props).
468
+ *
469
+ * Behind the scenes, setting this property sets both the {@link scaleX} and
470
+ * {@link scaleY} props to the same value.
471
+ *
472
+ * NOTE: When the scaleX and scaleY props are explicitly set to different values,
473
+ * this property returns `null`. Setting `null` on this property will have no
474
+ * effect.
475
+ *
476
+ * @default 1.0
477
+ */
478
+ scale: number | null;
479
+ /**
480
+ * Scale to render the Node at (X-Axis)
481
+ *
482
+ * @remarks
483
+ * The scaleX value multiplies the provided {@link width} of the Node around
484
+ * the Node's Pivot Point (defined by the {@link pivot} props).
485
+ *
486
+ * @default 1.0
487
+ */
488
+ scaleX: number;
489
+ /**
490
+ * Scale to render the Node at (Y-Axis)
491
+ *
492
+ * @remarks
493
+ * The scaleY value multiplies the provided {@link height} of the Node around
494
+ * the Node's Pivot Point (defined by the {@link pivot} props).
495
+ *
496
+ * @default 1.0
497
+ */
498
+ scaleY: number;
499
+ /**
500
+ * Combined position of the Node's Mount Point
501
+ *
502
+ * @remarks
503
+ * The value can be any number between `0.0` and `1.0`:
504
+ * - `0.0` defines the Mount Point at the top-left corner of the Node.
505
+ * - `0.5` defines it at the center of the Node.
506
+ * - `1.0` defines it at the bottom-right corner of the node.
507
+ *
508
+ * Use the {@link mountX} and {@link mountY} props seperately for more control
509
+ * of the Mount Point.
510
+ *
511
+ * When assigned, the same value is also passed to both the {@link mountX} and
512
+ * {@link mountY} props.
513
+ *
514
+ * @default 0 (top-left)
515
+ */
516
+ mount: number;
517
+ /**
518
+ * X position of the Node's Mount Point
519
+ *
520
+ * @remarks
521
+ * The value can be any number between `0.0` and `1.0`:
522
+ * - `0.0` defines the Mount Point's X position as the left-most edge of the
523
+ * Node
524
+ * - `0.5` defines it as the horizontal center of the Node
525
+ * - `1.0` defines it as the right-most edge of the Node.
526
+ *
527
+ * The combination of {@link mountX} and {@link mountY} define the Mount Point
528
+ *
529
+ * @default 0 (left-most edge)
530
+ */
531
+ mountX: number;
532
+ /**
533
+ * Y position of the Node's Mount Point
534
+ *
535
+ * @remarks
536
+ * The value can be any number between `0.0` and `1.0`:
537
+ * - `0.0` defines the Mount Point's Y position as the top-most edge of the
538
+ * Node
539
+ * - `0.5` defines it as the vertical center of the Node
540
+ * - `1.0` defines it as the bottom-most edge of the Node.
541
+ *
542
+ * The combination of {@link mountX} and {@link mountY} define the Mount Point
543
+ *
544
+ * @default 0 (top-most edge)
545
+ */
546
+ mountY: number;
547
+ /**
548
+ * Combined position of the Node's Pivot Point
549
+ *
550
+ * @remarks
551
+ * The value can be any number between `0.0` and `1.0`:
552
+ * - `0.0` defines the Pivot Point at the top-left corner of the Node.
553
+ * - `0.5` defines it at the center of the Node.
554
+ * - `1.0` defines it at the bottom-right corner of the node.
555
+ *
556
+ * Use the {@link pivotX} and {@link pivotY} props seperately for more control
557
+ * of the Pivot Point.
558
+ *
559
+ * When assigned, the same value is also passed to both the {@link pivotX} and
560
+ * {@link pivotY} props.
561
+ *
562
+ * @default 0.5 (center)
563
+ */
564
+ pivot: number;
565
+ /**
566
+ * X position of the Node's Pivot Point
567
+ *
568
+ * @remarks
569
+ * The value can be any number between `0.0` and `1.0`:
570
+ * - `0.0` defines the Pivot Point's X position as the left-most edge of the
571
+ * Node
572
+ * - `0.5` defines it as the horizontal center of the Node
573
+ * - `1.0` defines it as the right-most edge of the Node.
574
+ *
575
+ * The combination of {@link pivotX} and {@link pivotY} define the Pivot Point
576
+ *
577
+ * @default 0.5 (centered on x-axis)
578
+ */
579
+ pivotX: number;
580
+ /**
581
+ * Y position of the Node's Pivot Point
582
+ *
583
+ * @remarks
584
+ * The value can be any number between `0.0` and `1.0`:
585
+ * - `0.0` defines the Pivot Point's Y position as the top-most edge of the
586
+ * Node
587
+ * - `0.5` defines it as the vertical center of the Node
588
+ * - `1.0` defines it as the bottom-most edge of the Node.
589
+ *
590
+ * The combination of {@link pivotX} and {@link pivotY} define the Pivot Point
591
+ *
592
+ * @default 0.5 (centered on y-axis)
593
+ */
594
+ pivotY: number;
595
+ /**
596
+ * Rotation of the Node (in Radians)
597
+ *
598
+ * @remarks
599
+ * Sets the amount to rotate the Node by around it's Pivot Point (defined by
600
+ * the {@link pivot} props). Positive values rotate the Node clockwise, while
601
+ * negative values rotate it counter-clockwise.
602
+ *
603
+ * Example values:
604
+ * - `-Math.PI / 2`: 90 degree rotation counter-clockwise
605
+ * - `0`: No rotation
606
+ * - `Math.PI / 2`: 90 degree rotation clockwise
607
+ * - `Math.PI`: 180 degree rotation clockwise
608
+ * - `3 * Math.PI / 2`: 270 degree rotation clockwise
609
+ * - `2 * Math.PI`: 360 rotation clockwise
610
+ */
611
+ rotation: number;
612
+
613
+ /**
614
+ * Whether the Node is rendered to a texture
615
+ *
616
+ * @remarks
617
+ * TBD
618
+ *
619
+ * @default false
620
+ */
621
+ rtt: boolean;
622
+
623
+ /**
624
+ * Node data element for custom data storage (optional)
625
+ *
626
+ * @remarks
627
+ * This property is used to store custom data on the Node as a key/value data store.
628
+ * Data values are limited to string, numbers, booleans. Strings will be truncated
629
+ * to a 2048 character limit for performance reasons.
630
+ *
631
+ * This is not a data storage mechanism for large amounts of data please use a
632
+ * dedicated data storage mechanism for that.
633
+ *
634
+ * The custom data will be reflected in the inspector as part of `data-*` attributes
635
+ *
636
+ * @default `undefined`
637
+ */
638
+ data?: CustomDataMap;
639
+
640
+ /**
641
+ * Image Type to explicitly set the image type that is being loaded
642
+ *
643
+ * @remarks
644
+ * This property must be used with a `src` that points at an image. In some cases
645
+ * the extension doesn't provide a reliable representation of the image type. In such
646
+ * cases set the ImageType explicitly.
647
+ *
648
+ * `regular` is used for normal images such as png, jpg, etc
649
+ * `compressed` is used for ETC1/ETC2 compressed images with a PVR or KTX container
650
+ * `svg` is used for scalable vector graphics
651
+ *
652
+ * @default `undefined`
653
+ */
654
+ imageType?: 'regular' | 'compressed' | 'svg' | null;
655
+
656
+ /**
657
+ * She width of the rectangle from which the Image Texture will be extracted.
658
+ * This value can be negative. If not provided, the image's source natural
659
+ * width will be used.
660
+ */
661
+ srcWidth?: number;
662
+ /**
663
+ * The height of the rectangle from which the Image Texture will be extracted.
664
+ * This value can be negative. If not provided, the image's source natural
665
+ * height will be used.
666
+ */
667
+ srcHeight?: number;
668
+ /**
669
+ * The x coordinate of the reference point of the rectangle from which the Texture
670
+ * will be extracted. `width` and `height` are provided. And only works when
671
+ * createImageBitmap is available. Only works when createImageBitmap is supported on the browser.
672
+ */
673
+ srcX?: number;
674
+ /**
675
+ * The y coordinate of the reference point of the rectangle from which the Texture
676
+ * will be extracted. Only used when source `srcWidth` width and `srcHeight` height
677
+ * are provided. Only works when createImageBitmap is supported on the browser.
678
+ */
679
+ srcY?: number;
680
+ /**
681
+ * By enabling Strict bounds the renderer will not process & render child nodes of a node that is out of the visible area
682
+ *
683
+ * @remarks
684
+ * When enabled out of bound nodes, i.e. nodes that are out of the visible area, will
685
+ * **NOT** have their children processed and renderer anymore. This means the children of a out of bound
686
+ * node will not receive update processing such as positioning updates and will not be drawn on screen.
687
+ * As such the rest of the branch of the update tree that sits below this node will not be processed anymore
688
+ *
689
+ * This is a big performance gain but may be disabled in cases where the width of the parent node is
690
+ * unknown and the render must process the child nodes regardless of the viewport status of the parent node
691
+ *
692
+ * @default false
693
+ */
694
+ strictBounds: boolean;
695
+ }
696
+
697
+ /**
698
+ * Grab all the number properties of type T
699
+ */
700
+ type NumberProps<T> = {
701
+ [Key in keyof T as NonNullable<T[Key]> extends number ? Key : never]: number;
702
+ };
703
+
704
+ /**
705
+ * Properties of a Node used by the animate() function
706
+ */
707
+ export interface CoreNodeAnimateProps extends NumberProps<CoreNodeProps> {
708
+ /**
709
+ * Shader properties to animate
710
+ */
711
+ shaderProps: Record<string, number>;
712
+ // TODO: textureProps: Record<string, number>;
713
+ }
714
+
715
+ /**
716
+ * A visual Node in the Renderer scene graph.
717
+ *
718
+ * @remarks
719
+ * CoreNode is an internally used class that represents a Renderer Node in the
720
+ * scene graph. See INode.ts for the public APIs exposed to Renderer users
721
+ * that include generic types for Shaders.
722
+ */
723
+ export class CoreNode extends EventEmitter {
724
+ readonly children: CoreNode[] = [];
725
+ protected _id: number = getNewId();
726
+ readonly props: CoreNodeProps;
727
+
728
+ public updateType = UpdateType.All;
729
+ public childUpdateType = UpdateType.None;
730
+
731
+ public globalTransform?: Matrix3d;
732
+ public scaleRotateTransform?: Matrix3d;
733
+ public localTransform?: Matrix3d;
734
+ public sceneGlobalTransform?: Matrix3d;
735
+ public renderCoords?: RenderCoords;
736
+ public sceneRenderCoords?: RenderCoords;
737
+ public renderBound?: Bound;
738
+ public strictBound?: Bound;
739
+ public preloadBound?: Bound;
740
+ public clippingRect: RectWithValid = {
741
+ x: 0,
742
+ y: 0,
743
+ width: 0,
744
+ height: 0,
745
+ valid: false,
746
+ };
747
+ public isRenderable = false;
748
+ public renderState: CoreNodeRenderState = CoreNodeRenderState.Init;
749
+
750
+ public worldAlpha = 1;
751
+ public premultipliedColorTl = 0;
752
+ public premultipliedColorTr = 0;
753
+ public premultipliedColorBl = 0;
754
+ public premultipliedColorBr = 0;
755
+ public calcZIndex = 0;
756
+ public hasRTTupdates = false;
757
+ public parentHasRenderTexture = false;
758
+ public rttParent: CoreNode | null = null;
759
+
760
+ public destroyed = false;
761
+
762
+ constructor(readonly stage: Stage, props: CoreNodeProps) {
763
+ super();
764
+
765
+ this.props = {
766
+ ...props,
767
+ parent: null,
768
+ texture: null,
769
+ src: null,
770
+ rtt: false,
771
+ };
772
+
773
+ // Assign props to instance
774
+ this.parent = props.parent;
775
+ this.texture = props.texture;
776
+ this.src = props.src;
777
+ this.rtt = props.rtt;
778
+
779
+ if (props.boundsMargin) {
780
+ this.boundsMargin = Array.isArray(props.boundsMargin)
781
+ ? props.boundsMargin
782
+ : [
783
+ props.boundsMargin,
784
+ props.boundsMargin,
785
+ props.boundsMargin,
786
+ props.boundsMargin,
787
+ ];
788
+ }
789
+
790
+ this.setUpdateType(
791
+ UpdateType.ScaleRotate |
792
+ UpdateType.Local |
793
+ UpdateType.RenderBounds |
794
+ UpdateType.RenderState,
795
+ );
796
+
797
+ if (isProductionEnvironment() === false && props.preventCleanup === true) {
798
+ console.warn(
799
+ 'CoreNode.preventCleanup: Is deprecated and will be removed in upcoming release, please use textureOptions.preventCleanup instead',
800
+ );
801
+ }
802
+
803
+ // if the default texture isn't loaded yet, wait for it to load
804
+ // this only happens when the node is created before the stage is ready
805
+ if (
806
+ this.stage.defaultTexture &&
807
+ this.stage.defaultTexture.state !== 'loaded'
808
+ ) {
809
+ this.stage.defaultTexture.once('loaded', () => {
810
+ this.setUpdateType(UpdateType.IsRenderable);
811
+ });
812
+ }
813
+ }
814
+
815
+ //#region Textures
816
+ loadTexture(): void {
817
+ const { texture } = this.props;
818
+ assertTruthy(texture);
819
+
820
+ // If texture is already loaded / failed, trigger loaded event manually
821
+ // so that users get a consistent event experience.
822
+ // We do this in a microtask to allow listeners to be attached in the same
823
+ // synchronous task after calling loadTexture()
824
+ queueMicrotask(() => {
825
+ if (this.textureOptions.preload === true) {
826
+ this.stage.txManager.loadTexture(texture);
827
+ }
828
+
829
+ texture.preventCleanup =
830
+ this.props.textureOptions?.preventCleanup ?? false;
831
+ texture.on('loaded', this.onTextureLoaded);
832
+ texture.on('failed', this.onTextureFailed);
833
+ texture.on('freed', this.onTextureFreed);
834
+
835
+ // If the parent is a render texture, the initial texture status
836
+ // will be set to freed until the texture is processed by the
837
+ // Render RTT nodes. So we only need to listen fo changes and
838
+ // no need to check the texture.state until we restructure how
839
+ // textures are being processed.
840
+ if (this.parentHasRenderTexture) {
841
+ this.notifyParentRTTOfUpdate();
842
+ return;
843
+ }
844
+
845
+ if (texture.state === 'loaded') {
846
+ assertTruthy(texture.dimensions);
847
+ this.onTextureLoaded(texture, texture.dimensions);
848
+ } else if (texture.state === 'failed') {
849
+ assertTruthy(texture.error);
850
+ this.onTextureFailed(texture, texture.error);
851
+ } else if (texture.state === 'freed') {
852
+ this.onTextureFreed(texture);
853
+ }
854
+ });
855
+ }
856
+
857
+ unloadTexture(): void {
858
+ if (this.texture !== null) {
859
+ this.texture.off('loaded', this.onTextureLoaded);
860
+ this.texture.off('failed', this.onTextureFailed);
861
+ this.texture.off('freed', this.onTextureFreed);
862
+ this.texture.setRenderableOwner(this, false);
863
+ }
864
+ }
865
+
866
+ autosizeNode(dimensions: Dimensions) {
867
+ if (this.autosize) {
868
+ this.width = dimensions.width;
869
+ this.height = dimensions.height;
870
+ }
871
+ }
872
+
873
+ private onTextureLoaded: TextureLoadedEventHandler = (_, dimensions) => {
874
+ this.autosizeNode(dimensions);
875
+ this.setUpdateType(UpdateType.IsRenderable);
876
+
877
+ // Texture was loaded. In case the RAF loop has already stopped, we request
878
+ // a render to ensure the texture is rendered.
879
+ this.stage.requestRender();
880
+
881
+ // If parent has a render texture, flag that we need to update
882
+ if (this.parentHasRenderTexture) {
883
+ this.notifyParentRTTOfUpdate();
884
+ }
885
+
886
+ // ignore 1x1 pixel textures
887
+ if (dimensions.width > 1 && dimensions.height > 1) {
888
+ this.emit('loaded', {
889
+ type: 'texture',
890
+ dimensions,
891
+ } satisfies NodeTextureLoadedPayload);
892
+ }
893
+
894
+ // Trigger a local update if the texture is loaded and the resizeMode is 'contain'
895
+ if (this.props.textureOptions?.resizeMode?.type === 'contain') {
896
+ this.setUpdateType(UpdateType.Local);
897
+ }
898
+ };
899
+
900
+ private onTextureFailed: TextureFailedEventHandler = (_, error) => {
901
+ // immediately set isRenderable to false, so that we handle the error
902
+ // without waiting for the next frame loop
903
+ this.isRenderable = false;
904
+ this.setUpdateType(UpdateType.IsRenderable);
905
+
906
+ // If parent has a render texture, flag that we need to update
907
+ if (this.parentHasRenderTexture) {
908
+ this.notifyParentRTTOfUpdate();
909
+ }
910
+
911
+ this.emit('failed', {
912
+ type: 'texture',
913
+ error,
914
+ } satisfies NodeTextureFailedPayload);
915
+ };
916
+
917
+ private onTextureFreed: TextureFreedEventHandler = () => {
918
+ // immediately set isRenderable to false, so that we handle the error
919
+ // without waiting for the next frame loop
920
+ this.isRenderable = false;
921
+ this.setUpdateType(UpdateType.IsRenderable);
922
+
923
+ // If parent has a render texture, flag that we need to update
924
+ if (this.parentHasRenderTexture) {
925
+ this.notifyParentRTTOfUpdate();
926
+ }
927
+
928
+ this.emit('freed', {
929
+ type: 'texture',
930
+ } satisfies NodeTextureFreedPayload);
931
+ };
932
+ //#endregion Textures
933
+
934
+ /**
935
+ * Change types types is used to determine the scope of the changes being applied
936
+ *
937
+ * @remarks
938
+ * See {@link UpdateType} for more information on each type
939
+ *
940
+ * @param type
941
+ */
942
+ setUpdateType(type: UpdateType): void {
943
+ this.updateType |= type;
944
+
945
+ const parent = this.props.parent;
946
+ if (!parent) return;
947
+
948
+ if ((parent.updateType & UpdateType.Children) === 0) {
949
+ // Inform the parent if it doesn’t already have a child update
950
+ parent.setUpdateType(UpdateType.Children);
951
+ }
952
+ }
953
+
954
+ sortChildren() {
955
+ this.children.sort((a, b) => a.calcZIndex - b.calcZIndex);
956
+ }
957
+
958
+ updateScaleRotateTransform() {
959
+ const { rotation, scaleX, scaleY } = this.props;
960
+
961
+ // optimize simple translation cases
962
+ if (rotation === 0 && scaleX === 1 && scaleY === 1) {
963
+ this.scaleRotateTransform = undefined;
964
+ return;
965
+ }
966
+
967
+ this.scaleRotateTransform = Matrix3d.rotate(
968
+ rotation,
969
+ this.scaleRotateTransform,
970
+ ).scale(scaleX, scaleY);
971
+ }
972
+
973
+ updateLocalTransform() {
974
+ const { x, y, width, height } = this.props;
975
+ const mountTranslateX = this.props.mountX * width;
976
+ const mountTranslateY = this.props.mountY * height;
977
+
978
+ if (this.scaleRotateTransform) {
979
+ const pivotTranslateX = this.props.pivotX * width;
980
+ const pivotTranslateY = this.props.pivotY * height;
981
+
982
+ this.localTransform = Matrix3d.translate(
983
+ x - mountTranslateX + pivotTranslateX,
984
+ y - mountTranslateY + pivotTranslateY,
985
+ this.localTransform,
986
+ )
987
+ .multiply(this.scaleRotateTransform)
988
+ .translate(-pivotTranslateX, -pivotTranslateY);
989
+ } else {
990
+ this.localTransform = Matrix3d.translate(
991
+ x - mountTranslateX,
992
+ y - mountTranslateY,
993
+ this.localTransform,
994
+ );
995
+ }
996
+
997
+ // Handle 'contain' resize mode
998
+ const texture = this.props.texture;
999
+ if (
1000
+ texture &&
1001
+ texture.dimensions &&
1002
+ this.props.textureOptions?.resizeMode?.type === 'contain'
1003
+ ) {
1004
+ let resizeModeScaleX = 1;
1005
+ let resizeModeScaleY = 1;
1006
+ let extraX = 0;
1007
+ let extraY = 0;
1008
+ const { width: tw, height: th } = texture.dimensions;
1009
+ const txAspectRatio = tw / th;
1010
+ const nodeAspectRatio = width / height;
1011
+ if (txAspectRatio > nodeAspectRatio) {
1012
+ // Texture is wider than node
1013
+ // Center the node vertically (shift down by extraY)
1014
+ // Scale the node vertically to maintain original aspect ratio
1015
+ const scaleX = width / tw;
1016
+ const scaledTxHeight = th * scaleX;
1017
+ extraY = (height - scaledTxHeight) / 2;
1018
+ resizeModeScaleY = scaledTxHeight / height;
1019
+ } else {
1020
+ // Texture is taller than node (or equal)
1021
+ // Center the node horizontally (shift right by extraX)
1022
+ // Scale the node horizontally to maintain original aspect ratio
1023
+ const scaleY = height / th;
1024
+ const scaledTxWidth = tw * scaleY;
1025
+ extraX = (width - scaledTxWidth) / 2;
1026
+ resizeModeScaleX = scaledTxWidth / width;
1027
+ }
1028
+
1029
+ // Apply the extra translation and scale to the local transform
1030
+ this.localTransform
1031
+ .translate(extraX, extraY)
1032
+ .scale(resizeModeScaleX, resizeModeScaleY);
1033
+ }
1034
+
1035
+ this.setUpdateType(UpdateType.Global);
1036
+ }
1037
+
1038
+ /**
1039
+ * @todo: test for correct calculation flag
1040
+ * @param delta
1041
+ */
1042
+ update(delta: number, parentClippingRect: RectWithValid): void {
1043
+ if (this.updateType & UpdateType.ScaleRotate) {
1044
+ this.updateScaleRotateTransform();
1045
+ this.setUpdateType(UpdateType.Local);
1046
+ }
1047
+
1048
+ if (this.updateType & UpdateType.Local) {
1049
+ this.updateLocalTransform();
1050
+ this.setUpdateType(UpdateType.Global);
1051
+ }
1052
+
1053
+ const parent = this.props.parent;
1054
+ let renderState = null;
1055
+
1056
+ // Handle specific RTT updates at this node level
1057
+ if (this.updateType & UpdateType.RenderTexture && this.rtt) {
1058
+ this.hasRTTupdates = true;
1059
+ }
1060
+
1061
+ if (this.updateType & UpdateType.Global) {
1062
+ assertTruthy(this.localTransform);
1063
+
1064
+ if (this.parentHasRenderTexture === true && parent?.rtt === true) {
1065
+ // we are at the start of the RTT chain, so we need to reset the globalTransform
1066
+ // for correct RTT rendering
1067
+ this.globalTransform = Matrix3d.identity();
1068
+
1069
+ // Maintain a full scene global transform for bounds detection
1070
+ this.sceneGlobalTransform = Matrix3d.copy(
1071
+ parent?.globalTransform || Matrix3d.identity(),
1072
+ ).multiply(this.localTransform);
1073
+ } else if (
1074
+ this.parentHasRenderTexture === true &&
1075
+ parent?.rtt === false
1076
+ ) {
1077
+ // we're part of an RTT chain but our parent is not the main RTT node
1078
+ // so we need to propogate the sceneGlobalTransform of the parent
1079
+ // to maintain a full scene global transform for bounds detection
1080
+ this.sceneGlobalTransform = Matrix3d.copy(
1081
+ parent?.sceneGlobalTransform || this.localTransform,
1082
+ ).multiply(this.localTransform);
1083
+
1084
+ this.globalTransform = Matrix3d.copy(
1085
+ parent?.globalTransform || this.localTransform,
1086
+ this.globalTransform,
1087
+ );
1088
+ } else {
1089
+ this.globalTransform = Matrix3d.copy(
1090
+ parent?.globalTransform || this.localTransform,
1091
+ this.globalTransform,
1092
+ );
1093
+ }
1094
+
1095
+ if (parent !== null) {
1096
+ this.globalTransform.multiply(this.localTransform);
1097
+ }
1098
+ this.calculateRenderCoords();
1099
+ this.updateBoundingRect();
1100
+
1101
+ this.setUpdateType(UpdateType.RenderState | UpdateType.Children);
1102
+ this.childUpdateType |= UpdateType.Global;
1103
+
1104
+ if (this.clipping === true) {
1105
+ this.setUpdateType(UpdateType.Clipping | UpdateType.RenderBounds);
1106
+ this.childUpdateType |= UpdateType.RenderBounds;
1107
+ }
1108
+ }
1109
+
1110
+ if (this.updateType & UpdateType.RenderBounds) {
1111
+ this.createRenderBounds();
1112
+ this.setUpdateType(UpdateType.RenderState);
1113
+ this.setUpdateType(UpdateType.Children);
1114
+
1115
+ this.childUpdateType |= UpdateType.RenderBounds;
1116
+ }
1117
+
1118
+ if (this.updateType & UpdateType.RenderState) {
1119
+ renderState = this.checkRenderBounds();
1120
+ this.setUpdateType(UpdateType.IsRenderable);
1121
+
1122
+ // if we're not going out of bounds, update the render state
1123
+ // this is done so the update loop can finish before we mark a node
1124
+ // as out of bounds
1125
+ if (renderState !== CoreNodeRenderState.OutOfBounds) {
1126
+ this.updateRenderState(renderState);
1127
+ }
1128
+ }
1129
+
1130
+ if (this.updateType & UpdateType.WorldAlpha) {
1131
+ if (parent) {
1132
+ this.worldAlpha = parent.worldAlpha * this.props.alpha;
1133
+ } else {
1134
+ this.worldAlpha = this.props.alpha;
1135
+ }
1136
+ this.setUpdateType(
1137
+ UpdateType.Children |
1138
+ UpdateType.PremultipliedColors |
1139
+ UpdateType.IsRenderable,
1140
+ );
1141
+ this.childUpdateType |= UpdateType.WorldAlpha;
1142
+ }
1143
+
1144
+ if (this.updateType & UpdateType.IsRenderable) {
1145
+ this.updateIsRenderable();
1146
+ }
1147
+
1148
+ if (this.updateType & UpdateType.Clipping) {
1149
+ this.calculateClippingRect(parentClippingRect);
1150
+ this.setUpdateType(UpdateType.Children);
1151
+
1152
+ this.childUpdateType |= UpdateType.Clipping;
1153
+ this.childUpdateType |= UpdateType.RenderBounds;
1154
+ }
1155
+
1156
+ if (this.updateType & UpdateType.PremultipliedColors) {
1157
+ this.premultipliedColorTl = mergeColorAlphaPremultiplied(
1158
+ this.props.colorTl,
1159
+ this.worldAlpha,
1160
+ true,
1161
+ );
1162
+
1163
+ // If all the colors are the same just sent them all to the same value
1164
+ if (
1165
+ this.props.colorTl === this.props.colorTr &&
1166
+ this.props.colorBl === this.props.colorBr &&
1167
+ this.props.colorTl === this.props.colorBl
1168
+ ) {
1169
+ this.premultipliedColorTr =
1170
+ this.premultipliedColorBl =
1171
+ this.premultipliedColorBr =
1172
+ this.premultipliedColorTl;
1173
+ } else {
1174
+ this.premultipliedColorTr = mergeColorAlphaPremultiplied(
1175
+ this.props.colorTr,
1176
+ this.worldAlpha,
1177
+ true,
1178
+ );
1179
+ this.premultipliedColorBl = mergeColorAlphaPremultiplied(
1180
+ this.props.colorBl,
1181
+ this.worldAlpha,
1182
+ true,
1183
+ );
1184
+ this.premultipliedColorBr = mergeColorAlphaPremultiplied(
1185
+ this.props.colorBr,
1186
+ this.worldAlpha,
1187
+ true,
1188
+ );
1189
+ }
1190
+ }
1191
+
1192
+ // No need to update zIndex if there is no parent
1193
+ if (parent !== null && this.updateType & UpdateType.CalculatedZIndex) {
1194
+ this.calculateZIndex();
1195
+ // Tell parent to re-sort children
1196
+ parent.setUpdateType(UpdateType.ZIndexSortedChildren);
1197
+ }
1198
+
1199
+ if (
1200
+ this.props.strictBounds === true &&
1201
+ this.renderState === CoreNodeRenderState.OutOfBounds
1202
+ ) {
1203
+ this.updateType &= ~UpdateType.RenderBounds; // remove render bounds update
1204
+ return;
1205
+ }
1206
+
1207
+ if (this.updateType & UpdateType.Children && this.children.length > 0) {
1208
+ for (let i = 0, length = this.children.length; i < length; i++) {
1209
+ const child = this.children[i] as CoreNode;
1210
+
1211
+ child.setUpdateType(this.childUpdateType);
1212
+
1213
+ if (child.updateType === 0) {
1214
+ continue;
1215
+ }
1216
+
1217
+ let childClippingRect = this.clippingRect;
1218
+ if (this.rtt === true) {
1219
+ childClippingRect = {
1220
+ x: 0,
1221
+ y: 0,
1222
+ width: 0,
1223
+ height: 0,
1224
+ valid: false,
1225
+ };
1226
+ }
1227
+
1228
+ child.update(delta, childClippingRect);
1229
+ }
1230
+ }
1231
+
1232
+ // If the node has an RTT parent and requires a texture re-render, inform the RTT parent
1233
+ // if (this.parentHasRenderTexture && this.updateType & UpdateType.RenderTexture) {
1234
+ // @TODO have a more scoped down updateType for RTT updates
1235
+ if (this.parentHasRenderTexture && this.updateType > 0) {
1236
+ this.notifyParentRTTOfUpdate();
1237
+ }
1238
+
1239
+ // Sorting children MUST happen after children have been updated so
1240
+ // that they have the oppotunity to update their calculated zIndex.
1241
+ if (this.updateType & UpdateType.ZIndexSortedChildren) {
1242
+ // reorder z-index
1243
+ this.sortChildren();
1244
+ }
1245
+
1246
+ // If we're out of bounds, apply the render state now
1247
+ // this is done so nodes can finish their entire update loop before
1248
+ // being marked as out of bounds
1249
+ if (renderState === CoreNodeRenderState.OutOfBounds) {
1250
+ this.updateRenderState(renderState);
1251
+ this.updateIsRenderable();
1252
+
1253
+ if (
1254
+ this.rtt === true &&
1255
+ renderState === CoreNodeRenderState.OutOfBounds
1256
+ ) {
1257
+ // notify children that we are going out of bounds
1258
+ // we have to do this now before we stop processing the render tree
1259
+ this.notifyChildrenRTTOfUpdate(renderState);
1260
+ // this.childUpdateType |= UpdateType.RenderState;
1261
+ }
1262
+ }
1263
+
1264
+ // reset update type
1265
+ this.updateType = 0;
1266
+ this.childUpdateType = 0;
1267
+ }
1268
+
1269
+ private findParentRTTNode(): CoreNode | null {
1270
+ let rttNode: CoreNode | null = this.parent;
1271
+ while (rttNode && !rttNode.rtt) {
1272
+ rttNode = rttNode.parent;
1273
+ }
1274
+ return rttNode;
1275
+ }
1276
+
1277
+ private getRTTParentRenderState(): CoreNodeRenderState | null {
1278
+ const rttNode = this.rttParent || this.findParentRTTNode();
1279
+ if (!rttNode) {
1280
+ return null;
1281
+ }
1282
+
1283
+ return rttNode.renderState;
1284
+ }
1285
+
1286
+ private notifyChildrenRTTOfUpdate(renderState: CoreNodeRenderState) {
1287
+ for (const child of this.children) {
1288
+ // force child to update render state
1289
+ child.updateRenderState(renderState);
1290
+ child.updateIsRenderable();
1291
+ child.notifyChildrenRTTOfUpdate(renderState);
1292
+ }
1293
+ }
1294
+
1295
+ private notifyParentRTTOfUpdate() {
1296
+ if (this.parent === null) {
1297
+ return;
1298
+ }
1299
+
1300
+ const rttNode = this.rttParent || this.findParentRTTNode();
1301
+ if (!rttNode) {
1302
+ return;
1303
+ }
1304
+
1305
+ // If an RTT node is found, mark it for re-rendering
1306
+ rttNode.hasRTTupdates = true;
1307
+ rttNode.setUpdateType(UpdateType.RenderTexture);
1308
+
1309
+ // if rttNode is nested, also make it update its RTT parent
1310
+ if (rttNode.parentHasRenderTexture === true) {
1311
+ rttNode.notifyParentRTTOfUpdate();
1312
+ }
1313
+ }
1314
+
1315
+ checkRenderBounds(): CoreNodeRenderState {
1316
+ assertTruthy(this.renderBound);
1317
+ assertTruthy(this.strictBound);
1318
+ assertTruthy(this.preloadBound);
1319
+
1320
+ if (boundInsideBound(this.renderBound, this.strictBound)) {
1321
+ return CoreNodeRenderState.InViewport;
1322
+ }
1323
+
1324
+ if (boundInsideBound(this.renderBound, this.preloadBound)) {
1325
+ return CoreNodeRenderState.InBounds;
1326
+ }
1327
+
1328
+ // check if we're larger then our parent, we're definitely in the viewport
1329
+ if (boundLargeThanBound(this.renderBound, this.strictBound)) {
1330
+ return CoreNodeRenderState.InViewport;
1331
+ }
1332
+
1333
+ // check if we dont have dimensions, take our parent's render state
1334
+ if (
1335
+ this.parent !== null &&
1336
+ (this.props.width === 0 || this.props.height === 0)
1337
+ ) {
1338
+ return this.parent.renderState;
1339
+ }
1340
+
1341
+ return CoreNodeRenderState.OutOfBounds;
1342
+ }
1343
+
1344
+ updateBoundingRect() {
1345
+ const transform = this.sceneGlobalTransform || this.globalTransform;
1346
+ const renderCoords = this.sceneRenderCoords || this.renderCoords;
1347
+
1348
+ assertTruthy(transform);
1349
+ assertTruthy(renderCoords);
1350
+
1351
+ const { tb, tc } = transform;
1352
+ const { x1, y1, x3, y3 } = renderCoords;
1353
+ if (tb === 0 || tc === 0) {
1354
+ this.renderBound = createBound(x1, y1, x3, y3, this.renderBound);
1355
+ } else {
1356
+ const { x2, x4, y2, y4 } = renderCoords;
1357
+ this.renderBound = createBound(
1358
+ Math.min(x1, x2, x3, x4),
1359
+ Math.min(y1, y2, y3, y4),
1360
+ Math.max(x1, x2, x3, x4),
1361
+ Math.max(y1, y2, y3, y4),
1362
+ this.renderBound,
1363
+ );
1364
+ }
1365
+ }
1366
+
1367
+ createRenderBounds(): void {
1368
+ assertTruthy(this.stage);
1369
+
1370
+ if (this.parent !== null && this.parent.strictBound !== undefined) {
1371
+ // we have a parent with a valid bound, copy it
1372
+ const parentBound = this.parent.strictBound;
1373
+ this.strictBound = createBound(
1374
+ parentBound.x1,
1375
+ parentBound.y1,
1376
+ parentBound.x2,
1377
+ parentBound.y2,
1378
+ );
1379
+
1380
+ this.preloadBound = createPreloadBounds(
1381
+ this.strictBound,
1382
+ this.boundsMargin as [number, number, number, number],
1383
+ );
1384
+ } else {
1385
+ // no parent or parent does not have a bound, take the stage boundaries
1386
+ this.strictBound = this.stage.strictBound;
1387
+ this.preloadBound = this.stage.preloadBound;
1388
+ }
1389
+
1390
+ // if clipping is disabled, we're done
1391
+ if (this.props.clipping === false) {
1392
+ return;
1393
+ }
1394
+
1395
+ // only create local clipping bounds if node itself is in bounds
1396
+ // this can only be done if we have a render bound already
1397
+ if (this.renderBound === undefined) {
1398
+ return;
1399
+ }
1400
+
1401
+ // if we're out of bounds, we're done
1402
+ if (boundInsideBound(this.renderBound, this.strictBound) === false) {
1403
+ return;
1404
+ }
1405
+
1406
+ // clipping is enabled and we are in bounds create our own bounds
1407
+ const { x, y, width, height } = this.props;
1408
+
1409
+ // Pick the global transform if available, otherwise use the local transform
1410
+ // global transform is only available if the node in an RTT chain
1411
+ const { tx, ty } = this.sceneGlobalTransform || this.globalTransform || {};
1412
+ const _x = tx ?? x;
1413
+ const _y = ty ?? y;
1414
+ this.strictBound = createBound(
1415
+ _x,
1416
+ _y,
1417
+ _x + width,
1418
+ _y + height,
1419
+ this.strictBound,
1420
+ );
1421
+
1422
+ this.preloadBound = createPreloadBounds(
1423
+ this.strictBound,
1424
+ this.boundsMargin as [number, number, number, number],
1425
+ );
1426
+ }
1427
+
1428
+ updateRenderState(renderState: CoreNodeRenderState) {
1429
+ if (renderState === this.renderState) {
1430
+ return;
1431
+ }
1432
+
1433
+ const previous = this.renderState;
1434
+ this.renderState = renderState;
1435
+ const event = CoreNodeRenderStateMap.get(renderState);
1436
+ assertTruthy(event);
1437
+ this.emit(event, {
1438
+ previous,
1439
+ current: renderState,
1440
+ });
1441
+ }
1442
+
1443
+ /**
1444
+ * Updates the `isRenderable` property based on various conditions.
1445
+ */
1446
+ updateIsRenderable() {
1447
+ let newIsRenderable = false;
1448
+ let needsTextureOwnership = false;
1449
+
1450
+ // If the node is out of bounds or has an alpha of 0, it is not renderable
1451
+ if (this.checkBasicRenderability() === false) {
1452
+ this.updateTextureOwnership(false);
1453
+ this.setRenderable(false);
1454
+ return;
1455
+ }
1456
+
1457
+ if (this.texture !== null) {
1458
+ needsTextureOwnership = true;
1459
+
1460
+ // we're only renderable if the texture state is loaded
1461
+ newIsRenderable = this.texture.state === 'loaded';
1462
+ } else if (
1463
+ (this.hasShader() || this.hasColorProperties() === true) &&
1464
+ this.hasDimensions() === true
1465
+ ) {
1466
+ // This mean we have dimensions and a color set, so we can render a ColorTexture
1467
+ if (
1468
+ this.stage.defaultTexture &&
1469
+ this.stage.defaultTexture.state === 'loaded'
1470
+ ) {
1471
+ newIsRenderable = true;
1472
+ }
1473
+ }
1474
+
1475
+ this.updateTextureOwnership(needsTextureOwnership);
1476
+ this.setRenderable(newIsRenderable);
1477
+ }
1478
+
1479
+ /**
1480
+ * Checks if the node is renderable based on world alpha, dimensions and out of bounds status.
1481
+ */
1482
+ checkBasicRenderability(): boolean {
1483
+ if (this.worldAlpha === 0 || this.isOutOfBounds() === true) {
1484
+ return false;
1485
+ } else {
1486
+ return true;
1487
+ }
1488
+ }
1489
+
1490
+ /**
1491
+ * Sets the renderable state and triggers changes if necessary.
1492
+ * @param isRenderable - The new renderable state
1493
+ */
1494
+ setRenderable(isRenderable: boolean) {
1495
+ this.isRenderable = isRenderable;
1496
+ }
1497
+
1498
+ /**
1499
+ * Changes the renderable state of the node.
1500
+ */
1501
+ updateTextureOwnership(isRenderable: boolean) {
1502
+ this.texture?.setRenderableOwner(this, isRenderable);
1503
+ }
1504
+
1505
+ /**
1506
+ * Checks if the node is out of the viewport bounds.
1507
+ */
1508
+ isOutOfBounds(): boolean {
1509
+ return this.renderState <= CoreNodeRenderState.OutOfBounds;
1510
+ }
1511
+
1512
+ /**
1513
+ * Checks if the node has dimensions (width/height)
1514
+ */
1515
+ hasDimensions(): boolean {
1516
+ return this.props.width !== 0 && this.props.height !== 0;
1517
+ }
1518
+
1519
+ /**
1520
+ * Checks if the node has any color properties set.
1521
+ */
1522
+ hasColorProperties(): boolean {
1523
+ return (
1524
+ this.props.color !== 0 ||
1525
+ this.props.colorTop !== 0 ||
1526
+ this.props.colorBottom !== 0 ||
1527
+ this.props.colorLeft !== 0 ||
1528
+ this.props.colorRight !== 0 ||
1529
+ this.props.colorTl !== 0 ||
1530
+ this.props.colorTr !== 0 ||
1531
+ this.props.colorBl !== 0 ||
1532
+ this.props.colorBr !== 0
1533
+ );
1534
+ }
1535
+
1536
+ hasShader(): boolean {
1537
+ return this.props.shader !== null;
1538
+ }
1539
+
1540
+ calculateRenderCoords() {
1541
+ const { width, height } = this;
1542
+ const { tx, ty, ta, tb, tc, td } = this.globalTransform!;
1543
+ if (tb === 0 && tc === 0) {
1544
+ const minX = tx;
1545
+ const maxX = tx + width * ta;
1546
+ const minY = ty;
1547
+ const maxY = ty + height * td;
1548
+ this.renderCoords = RenderCoords.translate(
1549
+ //top-left
1550
+ minX,
1551
+ minY,
1552
+ //top-right
1553
+ maxX,
1554
+ minY,
1555
+ //bottom-right
1556
+ maxX,
1557
+ maxY,
1558
+ //bottom-left
1559
+ minX,
1560
+ maxY,
1561
+ this.renderCoords,
1562
+ );
1563
+ } else {
1564
+ this.renderCoords = RenderCoords.translate(
1565
+ //top-left
1566
+ tx,
1567
+ ty,
1568
+ //top-right
1569
+ tx + width * ta,
1570
+ ty + width * tc,
1571
+ //bottom-right
1572
+ tx + width * ta + height * tb,
1573
+ ty + width * tc + height * td,
1574
+ //bottom-left
1575
+ tx + height * tb,
1576
+ ty + height * td,
1577
+ this.renderCoords,
1578
+ );
1579
+ }
1580
+ if (this.sceneGlobalTransform === undefined) {
1581
+ return;
1582
+ }
1583
+
1584
+ const {
1585
+ tx: stx,
1586
+ ty: sty,
1587
+ ta: sta,
1588
+ tb: stb,
1589
+ tc: stc,
1590
+ td: std,
1591
+ } = this.sceneGlobalTransform;
1592
+ if (stb === 0 && stc === 0) {
1593
+ const minX = stx;
1594
+ const maxX = stx + width * sta;
1595
+ const minY = sty;
1596
+ const maxY = sty + height * std;
1597
+ this.sceneRenderCoords = RenderCoords.translate(
1598
+ //top-left
1599
+ minX,
1600
+ minY,
1601
+ //top-right
1602
+ maxX,
1603
+ minY,
1604
+ //bottom-right
1605
+ maxX,
1606
+ maxY,
1607
+ //bottom-left
1608
+ minX,
1609
+ maxY,
1610
+ this.sceneRenderCoords,
1611
+ );
1612
+ } else {
1613
+ this.sceneRenderCoords = RenderCoords.translate(
1614
+ //top-left
1615
+ stx,
1616
+ sty,
1617
+ //top-right
1618
+ stx + width * sta,
1619
+ sty + width * stc,
1620
+ //bottom-right
1621
+ stx + width * sta + height * stb,
1622
+ sty + width * stc + height * std,
1623
+ //bottom-left
1624
+ stx + height * stb,
1625
+ sty + height * std,
1626
+ this.sceneRenderCoords,
1627
+ );
1628
+ }
1629
+ }
1630
+
1631
+ /**
1632
+ * This function calculates the clipping rectangle for a node.
1633
+ *
1634
+ * The function then checks if the node is rotated. If the node requires clipping and is not rotated, a new clipping rectangle is created based on the node's global transform and dimensions.
1635
+ * If a parent clipping rectangle exists, it is intersected with the node's clipping rectangle (if it exists), or replaces the node's clipping rectangle.
1636
+ *
1637
+ * Finally, the node's parentClippingRect and clippingRect properties are updated.
1638
+ */
1639
+ calculateClippingRect(parentClippingRect: RectWithValid) {
1640
+ assertTruthy(this.globalTransform);
1641
+ const { clippingRect, props, globalTransform: gt } = this;
1642
+ const { clipping } = props;
1643
+ const isRotated = gt.tb !== 0 || gt.tc !== 0;
1644
+
1645
+ if (clipping === true && isRotated === false) {
1646
+ clippingRect.x = gt.tx;
1647
+ clippingRect.y = gt.ty;
1648
+ clippingRect.width = this.width * gt.ta;
1649
+ clippingRect.height = this.height * gt.td;
1650
+ clippingRect.valid = true;
1651
+ } else {
1652
+ clippingRect.valid = false;
1653
+ }
1654
+
1655
+ if (parentClippingRect.valid === true && clippingRect.valid === true) {
1656
+ // Intersect parent clipping rect with node clipping rect
1657
+ intersectRect(parentClippingRect, clippingRect, clippingRect);
1658
+ } else if (parentClippingRect.valid === true) {
1659
+ // Copy parent clipping rect
1660
+ copyRect(parentClippingRect, clippingRect);
1661
+ clippingRect.valid = true;
1662
+ }
1663
+ }
1664
+
1665
+ calculateZIndex(): void {
1666
+ const props = this.props;
1667
+ const z = props.zIndex || 0;
1668
+ const p = props.parent?.zIndex || 0;
1669
+
1670
+ let zIndex = z;
1671
+ if (props.parent?.zIndexLocked) {
1672
+ zIndex = z < p ? z : p;
1673
+ }
1674
+ this.calcZIndex = zIndex;
1675
+ }
1676
+
1677
+ /**
1678
+ * Destroy the node and cleanup all resources
1679
+ */
1680
+ destroy(): void {
1681
+ if (this.destroyed === true) {
1682
+ return;
1683
+ }
1684
+
1685
+ this.destroyed = true;
1686
+ this.unloadTexture();
1687
+
1688
+ this.clippingRect.valid = false;
1689
+ this.isRenderable = false;
1690
+
1691
+ this.renderCoords = undefined;
1692
+ this.renderBound = undefined;
1693
+ this.strictBound = undefined;
1694
+ this.preloadBound = undefined;
1695
+ this.globalTransform = undefined;
1696
+ this.scaleRotateTransform = undefined;
1697
+ this.localTransform = undefined;
1698
+
1699
+ this.props.texture = null;
1700
+ this.props.shader = this.stage.defShaderCtr;
1701
+
1702
+ while (this.children.length > 0) {
1703
+ this.children[0]?.destroy();
1704
+ }
1705
+
1706
+ // This very action will also remove the node from the parent's children array
1707
+ this.parent = null;
1708
+
1709
+ if (this.rtt) {
1710
+ this.stage.renderer.removeRTTNode(this);
1711
+ }
1712
+
1713
+ this.removeAllListeners();
1714
+ }
1715
+
1716
+ renderQuads(renderer: CoreRenderer): void {
1717
+ // Prevent quad rendering if parent has a render texture
1718
+ // and renderer is not currently rendering to a texture
1719
+ if (this.parentHasRenderTexture) {
1720
+ if (!renderer.renderToTextureActive) {
1721
+ return;
1722
+ }
1723
+ // Prevent quad rendering if parent render texture is not the active render texture
1724
+ if (this.parentRenderTexture !== renderer.activeRttNode) {
1725
+ return;
1726
+ }
1727
+ }
1728
+
1729
+ assertTruthy(this.globalTransform);
1730
+ assertTruthy(this.renderCoords);
1731
+
1732
+ // add to list of renderables to be sorted before rendering
1733
+ renderer.addQuad({
1734
+ width: this.props.width,
1735
+ height: this.props.height,
1736
+ colorTl: this.premultipliedColorTl,
1737
+ colorTr: this.premultipliedColorTr,
1738
+ colorBl: this.premultipliedColorBl,
1739
+ colorBr: this.premultipliedColorBr,
1740
+ // if we do not have a texture, use the default texture
1741
+ // this assumes any renderable node is either a distinct texture or a ColorTexture
1742
+ texture: this.texture || this.stage.defaultTexture,
1743
+ textureOptions: this.textureOptions,
1744
+ zIndex: this.zIndex,
1745
+ shader: this.shader.shader,
1746
+ shaderProps: this.shader.getResolvedProps(),
1747
+ alpha: this.worldAlpha,
1748
+ clippingRect: this.clippingRect,
1749
+ tx: this.globalTransform.tx,
1750
+ ty: this.globalTransform.ty,
1751
+ ta: this.globalTransform.ta,
1752
+ tb: this.globalTransform.tb,
1753
+ tc: this.globalTransform.tc,
1754
+ td: this.globalTransform.td,
1755
+ renderCoords: this.renderCoords,
1756
+ rtt: this.rtt,
1757
+ parentHasRenderTexture: this.parentHasRenderTexture,
1758
+ framebufferDimensions: this.framebufferDimensions,
1759
+ });
1760
+ }
1761
+
1762
+ //#region Properties
1763
+ get id(): number {
1764
+ return this._id;
1765
+ }
1766
+
1767
+ get data(): CustomDataMap | undefined {
1768
+ return this.props.data;
1769
+ }
1770
+
1771
+ set data(d: CustomDataMap | undefined) {
1772
+ this.props.data = d;
1773
+ }
1774
+
1775
+ get x(): number {
1776
+ return this.props.x;
1777
+ }
1778
+
1779
+ set x(value: number) {
1780
+ if (this.props.x !== value) {
1781
+ this.props.x = value;
1782
+ this.setUpdateType(UpdateType.Local);
1783
+ }
1784
+ }
1785
+
1786
+ get absX(): number {
1787
+ return (
1788
+ this.props.x +
1789
+ -this.props.width * this.props.mountX +
1790
+ (this.props.parent?.absX || this.props.parent?.globalTransform?.tx || 0)
1791
+ );
1792
+ }
1793
+
1794
+ get absY(): number {
1795
+ return (
1796
+ this.props.y +
1797
+ -this.props.height * this.props.mountY +
1798
+ (this.props.parent?.absY ?? 0)
1799
+ );
1800
+ }
1801
+
1802
+ get y(): number {
1803
+ return this.props.y;
1804
+ }
1805
+
1806
+ set y(value: number) {
1807
+ if (this.props.y !== value) {
1808
+ this.props.y = value;
1809
+ this.setUpdateType(UpdateType.Local);
1810
+ }
1811
+ }
1812
+
1813
+ get width(): number {
1814
+ return this.props.width;
1815
+ }
1816
+
1817
+ set width(value: number) {
1818
+ if (this.props.width !== value) {
1819
+ this.props.width = value;
1820
+ this.setUpdateType(UpdateType.Local);
1821
+
1822
+ if (this.props.rtt) {
1823
+ this.texture = this.stage.txManager.createTexture('RenderTexture', {
1824
+ width: this.width,
1825
+ height: this.height,
1826
+ });
1827
+
1828
+ this.setUpdateType(UpdateType.RenderTexture);
1829
+ }
1830
+ }
1831
+ }
1832
+
1833
+ get height(): number {
1834
+ return this.props.height;
1835
+ }
1836
+
1837
+ set height(value: number) {
1838
+ if (this.props.height !== value) {
1839
+ this.props.height = value;
1840
+ this.setUpdateType(UpdateType.Local);
1841
+
1842
+ if (this.props.rtt) {
1843
+ this.texture = this.stage.txManager.createTexture('RenderTexture', {
1844
+ width: this.width,
1845
+ height: this.height,
1846
+ });
1847
+
1848
+ this.setUpdateType(UpdateType.RenderTexture);
1849
+ }
1850
+ }
1851
+ }
1852
+
1853
+ get scale(): number {
1854
+ // The CoreNode `scale` property is only used by Animations.
1855
+ // Unlike INode, `null` should never be possibility for Animations.
1856
+ return this.scaleX;
1857
+ }
1858
+
1859
+ set scale(value: number) {
1860
+ // The CoreNode `scale` property is only used by Animations.
1861
+ // Unlike INode, `null` should never be possibility for Animations.
1862
+ this.scaleX = value;
1863
+ this.scaleY = value;
1864
+ }
1865
+
1866
+ get scaleX(): number {
1867
+ return this.props.scaleX;
1868
+ }
1869
+
1870
+ set scaleX(value: number) {
1871
+ if (this.props.scaleX !== value) {
1872
+ this.props.scaleX = value;
1873
+ this.setUpdateType(UpdateType.ScaleRotate);
1874
+ }
1875
+ }
1876
+
1877
+ get scaleY(): number {
1878
+ return this.props.scaleY;
1879
+ }
1880
+
1881
+ set scaleY(value: number) {
1882
+ if (this.props.scaleY !== value) {
1883
+ this.props.scaleY = value;
1884
+ this.setUpdateType(UpdateType.ScaleRotate);
1885
+ }
1886
+ }
1887
+
1888
+ get mount(): number {
1889
+ return this.props.mount;
1890
+ }
1891
+
1892
+ set mount(value: number) {
1893
+ if (this.props.mountX !== value || this.props.mountY !== value) {
1894
+ this.props.mountX = value;
1895
+ this.props.mountY = value;
1896
+ this.props.mount = value;
1897
+ this.setUpdateType(UpdateType.Local);
1898
+ }
1899
+ }
1900
+
1901
+ get mountX(): number {
1902
+ return this.props.mountX;
1903
+ }
1904
+
1905
+ set mountX(value: number) {
1906
+ if (this.props.mountX !== value) {
1907
+ this.props.mountX = value;
1908
+ this.setUpdateType(UpdateType.Local);
1909
+ }
1910
+ }
1911
+
1912
+ get mountY(): number {
1913
+ return this.props.mountY;
1914
+ }
1915
+
1916
+ set mountY(value: number) {
1917
+ if (this.props.mountY !== value) {
1918
+ this.props.mountY = value;
1919
+ this.setUpdateType(UpdateType.Local);
1920
+ }
1921
+ }
1922
+
1923
+ get pivot(): number {
1924
+ return this.props.pivot;
1925
+ }
1926
+
1927
+ set pivot(value: number) {
1928
+ if (this.props.pivotX !== value || this.props.pivotY !== value) {
1929
+ this.props.pivotX = value;
1930
+ this.props.pivotY = value;
1931
+ this.props.pivot = value;
1932
+ this.setUpdateType(UpdateType.Local);
1933
+ }
1934
+ }
1935
+
1936
+ get pivotX(): number {
1937
+ return this.props.pivotX;
1938
+ }
1939
+
1940
+ set pivotX(value: number) {
1941
+ if (this.props.pivotX !== value) {
1942
+ this.props.pivotX = value;
1943
+ this.setUpdateType(UpdateType.Local);
1944
+ }
1945
+ }
1946
+
1947
+ get pivotY(): number {
1948
+ return this.props.pivotY;
1949
+ }
1950
+
1951
+ set pivotY(value: number) {
1952
+ if (this.props.pivotY !== value) {
1953
+ this.props.pivotY = value;
1954
+ this.setUpdateType(UpdateType.Local);
1955
+ }
1956
+ }
1957
+
1958
+ get rotation(): number {
1959
+ return this.props.rotation;
1960
+ }
1961
+
1962
+ set rotation(value: number) {
1963
+ if (this.props.rotation !== value) {
1964
+ this.props.rotation = value;
1965
+ this.setUpdateType(UpdateType.ScaleRotate);
1966
+ }
1967
+ }
1968
+
1969
+ get alpha(): number {
1970
+ return this.props.alpha;
1971
+ }
1972
+
1973
+ set alpha(value: number) {
1974
+ this.props.alpha = value;
1975
+ this.setUpdateType(
1976
+ UpdateType.PremultipliedColors |
1977
+ UpdateType.WorldAlpha |
1978
+ UpdateType.Children |
1979
+ UpdateType.IsRenderable,
1980
+ );
1981
+ this.childUpdateType |= UpdateType.WorldAlpha;
1982
+ }
1983
+
1984
+ get autosize(): boolean {
1985
+ return this.props.autosize;
1986
+ }
1987
+
1988
+ set autosize(value: boolean) {
1989
+ this.props.autosize = value;
1990
+ }
1991
+
1992
+ get boundsMargin(): number | [number, number, number, number] | null {
1993
+ return (
1994
+ this.props.boundsMargin ??
1995
+ this.parent?.boundsMargin ??
1996
+ this.stage.boundsMargin
1997
+ );
1998
+ }
1999
+
2000
+ set boundsMargin(value: number | [number, number, number, number] | null) {
2001
+ if (value === this.props.boundsMargin) {
2002
+ return;
2003
+ }
2004
+
2005
+ if (value === null) {
2006
+ this.props.boundsMargin = value;
2007
+ } else {
2008
+ const bm: [number, number, number, number] = Array.isArray(value)
2009
+ ? value
2010
+ : [value, value, value, value];
2011
+
2012
+ this.props.boundsMargin = bm;
2013
+ }
2014
+ this.setUpdateType(UpdateType.RenderBounds);
2015
+ }
2016
+
2017
+ get clipping(): boolean {
2018
+ return this.props.clipping;
2019
+ }
2020
+
2021
+ set clipping(value: boolean) {
2022
+ this.props.clipping = value;
2023
+ this.setUpdateType(
2024
+ UpdateType.Clipping | UpdateType.RenderBounds | UpdateType.Children,
2025
+ );
2026
+ this.childUpdateType |= UpdateType.Global | UpdateType.Clipping;
2027
+ }
2028
+
2029
+ get color(): number {
2030
+ return this.props.color;
2031
+ }
2032
+
2033
+ set color(value: number) {
2034
+ this.colorTop = value;
2035
+ this.colorBottom = value;
2036
+ this.colorLeft = value;
2037
+ this.colorRight = value;
2038
+ this.props.color = value;
2039
+
2040
+ this.setUpdateType(UpdateType.PremultipliedColors);
2041
+ }
2042
+
2043
+ get colorTop(): number {
2044
+ return this.props.colorTop;
2045
+ }
2046
+
2047
+ set colorTop(value: number) {
2048
+ if (this.props.colorTl !== value || this.props.colorTr !== value) {
2049
+ this.colorTl = value;
2050
+ this.colorTr = value;
2051
+ }
2052
+ this.props.colorTop = value;
2053
+ this.setUpdateType(UpdateType.PremultipliedColors);
2054
+ }
2055
+
2056
+ get colorBottom(): number {
2057
+ return this.props.colorBottom;
2058
+ }
2059
+
2060
+ set colorBottom(value: number) {
2061
+ if (this.props.colorBl !== value || this.props.colorBr !== value) {
2062
+ this.colorBl = value;
2063
+ this.colorBr = value;
2064
+ }
2065
+ this.props.colorBottom = value;
2066
+ this.setUpdateType(UpdateType.PremultipliedColors);
2067
+ }
2068
+
2069
+ get colorLeft(): number {
2070
+ return this.props.colorLeft;
2071
+ }
2072
+
2073
+ set colorLeft(value: number) {
2074
+ if (this.props.colorTl !== value || this.props.colorBl !== value) {
2075
+ this.colorTl = value;
2076
+ this.colorBl = value;
2077
+ }
2078
+ this.props.colorLeft = value;
2079
+ this.setUpdateType(UpdateType.PremultipliedColors);
2080
+ }
2081
+
2082
+ get colorRight(): number {
2083
+ return this.props.colorRight;
2084
+ }
2085
+
2086
+ set colorRight(value: number) {
2087
+ if (this.props.colorTr !== value || this.props.colorBr !== value) {
2088
+ this.colorTr = value;
2089
+ this.colorBr = value;
2090
+ }
2091
+ this.props.colorRight = value;
2092
+ this.setUpdateType(UpdateType.PremultipliedColors);
2093
+ }
2094
+
2095
+ get colorTl(): number {
2096
+ return this.props.colorTl;
2097
+ }
2098
+
2099
+ set colorTl(value: number) {
2100
+ this.props.colorTl = value;
2101
+ this.setUpdateType(UpdateType.PremultipliedColors);
2102
+ }
2103
+
2104
+ get colorTr(): number {
2105
+ return this.props.colorTr;
2106
+ }
2107
+
2108
+ set colorTr(value: number) {
2109
+ this.props.colorTr = value;
2110
+ this.setUpdateType(UpdateType.PremultipliedColors);
2111
+ }
2112
+
2113
+ get colorBl(): number {
2114
+ return this.props.colorBl;
2115
+ }
2116
+
2117
+ set colorBl(value: number) {
2118
+ this.props.colorBl = value;
2119
+ this.setUpdateType(UpdateType.PremultipliedColors);
2120
+ }
2121
+
2122
+ get colorBr(): number {
2123
+ return this.props.colorBr;
2124
+ }
2125
+
2126
+ set colorBr(value: number) {
2127
+ this.props.colorBr = value;
2128
+ this.setUpdateType(UpdateType.PremultipliedColors);
2129
+ }
2130
+
2131
+ // we're only interested in parent zIndex to test
2132
+ // if we should use node zIndex is higher then parent zIndex
2133
+ get zIndexLocked(): number {
2134
+ return this.props.zIndexLocked || 0;
2135
+ }
2136
+
2137
+ set zIndexLocked(value: number) {
2138
+ this.props.zIndexLocked = value;
2139
+ this.setUpdateType(UpdateType.CalculatedZIndex | UpdateType.Children);
2140
+ for (let i = 0, length = this.children.length; i < length; i++) {
2141
+ this.children[i]!.setUpdateType(UpdateType.CalculatedZIndex);
2142
+ }
2143
+ }
2144
+
2145
+ get zIndex(): number {
2146
+ return this.props.zIndex;
2147
+ }
2148
+
2149
+ set zIndex(value: number) {
2150
+ this.props.zIndex = value;
2151
+ this.setUpdateType(UpdateType.CalculatedZIndex | UpdateType.Children);
2152
+ for (let i = 0, length = this.children.length; i < length; i++) {
2153
+ this.children[i]!.setUpdateType(UpdateType.CalculatedZIndex);
2154
+ }
2155
+ }
2156
+
2157
+ get parent(): CoreNode | null {
2158
+ return this.props.parent;
2159
+ }
2160
+
2161
+ set parent(newParent: CoreNode | null) {
2162
+ const oldParent = this.props.parent;
2163
+ if (oldParent === newParent) {
2164
+ return;
2165
+ }
2166
+ this.props.parent = newParent;
2167
+ if (oldParent) {
2168
+ const index = oldParent.children.indexOf(this);
2169
+ assertTruthy(
2170
+ index !== -1,
2171
+ "CoreNode.parent: Node not found in old parent's children!",
2172
+ );
2173
+ oldParent.children.splice(index, 1);
2174
+ oldParent.setUpdateType(
2175
+ UpdateType.Children | UpdateType.ZIndexSortedChildren,
2176
+ );
2177
+ }
2178
+ if (newParent) {
2179
+ newParent.children.push(this);
2180
+ // Since this node has a new parent, to be safe, have it do a full update.
2181
+ this.setUpdateType(UpdateType.All);
2182
+ // Tell parent that it's children need to be updated and sorted.
2183
+ newParent.setUpdateType(
2184
+ UpdateType.Children | UpdateType.ZIndexSortedChildren,
2185
+ );
2186
+
2187
+ // If the new parent has an RTT enabled, apply RTT inheritance
2188
+ if (newParent.rtt || newParent.parentHasRenderTexture) {
2189
+ this.applyRTTInheritance(newParent);
2190
+ }
2191
+ }
2192
+ this.updateScaleRotateTransform();
2193
+
2194
+ // fetch render bounds from parent
2195
+ this.setUpdateType(UpdateType.RenderBounds | UpdateType.Children);
2196
+ }
2197
+
2198
+ get preventCleanup(): boolean {
2199
+ return this.props.textureOptions.preventCleanup || false;
2200
+ }
2201
+
2202
+ set preventCleanup(value: boolean) {
2203
+ if (isProductionEnvironment() === false) {
2204
+ console.warn(
2205
+ 'CoreNode.preventCleanup: Is deprecated and will be removed in upcoming release, please use textureOptions.preventCleanup instead',
2206
+ );
2207
+ }
2208
+
2209
+ this.props.textureOptions.preventCleanup = value;
2210
+ }
2211
+
2212
+ get rtt(): boolean {
2213
+ return this.props.rtt;
2214
+ }
2215
+
2216
+ set rtt(value: boolean) {
2217
+ if (this.props.rtt === value) {
2218
+ return;
2219
+ }
2220
+ this.props.rtt = value;
2221
+
2222
+ if (value === true) {
2223
+ this.initRenderTexture();
2224
+ this.markChildrenWithRTT();
2225
+ } else {
2226
+ this.cleanupRenderTexture();
2227
+ }
2228
+
2229
+ this.setUpdateType(UpdateType.RenderTexture);
2230
+
2231
+ if (this.parentHasRenderTexture === true) {
2232
+ this.notifyParentRTTOfUpdate();
2233
+ }
2234
+ }
2235
+ private initRenderTexture() {
2236
+ this.texture = this.stage.txManager.createTexture('RenderTexture', {
2237
+ width: this.width,
2238
+ height: this.height,
2239
+ });
2240
+
2241
+ this.stage.renderer.renderToTexture(this);
2242
+ }
2243
+
2244
+ private cleanupRenderTexture() {
2245
+ this.unloadTexture();
2246
+ this.clearRTTInheritance();
2247
+
2248
+ this.hasRTTupdates = false;
2249
+ this.texture = null;
2250
+ }
2251
+
2252
+ private markChildrenWithRTT(node: CoreNode | null = null) {
2253
+ const parent = node || this;
2254
+
2255
+ for (const child of parent.children) {
2256
+ child.setUpdateType(UpdateType.All);
2257
+ child.parentHasRenderTexture = true;
2258
+ child.markChildrenWithRTT();
2259
+ }
2260
+ }
2261
+
2262
+ // Apply RTT inheritance when a node has an RTT-enabled parent
2263
+ private applyRTTInheritance(parent: CoreNode) {
2264
+ if (parent.rtt) {
2265
+ // Only the RTT node should be added to `renderToTexture`
2266
+ parent.setUpdateType(UpdateType.RenderTexture);
2267
+ }
2268
+
2269
+ // Propagate `parentHasRenderTexture` downwards
2270
+ this.markChildrenWithRTT(parent);
2271
+ }
2272
+
2273
+ // Clear RTT inheritance when detaching from an RTT chain
2274
+ private clearRTTInheritance() {
2275
+ // if this node is RTT itself stop the propagation important for nested RTT nodes
2276
+ // for the initial RTT node this is already handled in `set rtt`
2277
+ if (this.rtt) {
2278
+ return;
2279
+ }
2280
+
2281
+ for (const child of this.children) {
2282
+ // force child to update everything as the RTT inheritance has changed
2283
+ child.parentHasRenderTexture = false;
2284
+ child.rttParent = null;
2285
+ child.setUpdateType(UpdateType.All);
2286
+ child.clearRTTInheritance();
2287
+ }
2288
+ }
2289
+
2290
+ get shader(): BaseShaderController {
2291
+ return this.props.shader;
2292
+ }
2293
+
2294
+ set shader(value: BaseShaderController) {
2295
+ if (this.props.shader === value) {
2296
+ return;
2297
+ }
2298
+
2299
+ this.props.shader = value;
2300
+
2301
+ this.setUpdateType(UpdateType.IsRenderable);
2302
+ }
2303
+
2304
+ get src(): string | null {
2305
+ return this.props.src;
2306
+ }
2307
+
2308
+ set src(imageUrl: string | null) {
2309
+ if (this.props.src === imageUrl) {
2310
+ return;
2311
+ }
2312
+
2313
+ this.props.src = imageUrl;
2314
+
2315
+ if (!imageUrl) {
2316
+ this.texture = null;
2317
+ return;
2318
+ }
2319
+
2320
+ this.texture = this.stage.txManager.createTexture('ImageTexture', {
2321
+ src: imageUrl,
2322
+ width: this.props.width,
2323
+ height: this.props.height,
2324
+ type: this.props.imageType,
2325
+ sx: this.props.srcX,
2326
+ sy: this.props.srcY,
2327
+ sw: this.props.srcWidth,
2328
+ sh: this.props.srcHeight,
2329
+ });
2330
+ }
2331
+
2332
+ set imageType(type: 'regular' | 'compressed' | 'svg' | null) {
2333
+ if (this.props.imageType === type) {
2334
+ return;
2335
+ }
2336
+
2337
+ this.props.imageType = type;
2338
+ }
2339
+
2340
+ get imageType() {
2341
+ return this.props.imageType || null;
2342
+ }
2343
+
2344
+ get srcHeight(): number | undefined {
2345
+ return this.props.srcHeight;
2346
+ }
2347
+
2348
+ set srcHeight(value: number) {
2349
+ this.props.srcHeight = value;
2350
+ }
2351
+
2352
+ get srcWidth(): number | undefined {
2353
+ return this.props.srcWidth;
2354
+ }
2355
+
2356
+ set srcWidth(value: number) {
2357
+ this.props.srcWidth = value;
2358
+ }
2359
+
2360
+ get srcX(): number | undefined {
2361
+ return this.props.srcX;
2362
+ }
2363
+
2364
+ set srcX(value: number) {
2365
+ this.props.srcX = value;
2366
+ }
2367
+
2368
+ get srcY(): number | undefined {
2369
+ return this.props.srcY;
2370
+ }
2371
+
2372
+ set srcY(value: number) {
2373
+ this.props.srcY = value;
2374
+ }
2375
+
2376
+ /**
2377
+ * Returns the framebuffer dimensions of the node.
2378
+ * If the node has a render texture, the dimensions are the same as the node's dimensions.
2379
+ * If the node does not have a render texture, the dimensions are inherited from the parent.
2380
+ * If the node parent has a render texture and the node is a render texture, the nodes dimensions are used.
2381
+ */
2382
+ get framebufferDimensions(): Dimensions {
2383
+ if (this.parentHasRenderTexture && !this.rtt && this.parent) {
2384
+ return this.parent.framebufferDimensions;
2385
+ }
2386
+ return { width: this.width, height: this.height };
2387
+ }
2388
+
2389
+ /**
2390
+ * Returns the parent render texture node if it exists.
2391
+ */
2392
+ get parentRenderTexture(): CoreNode | null {
2393
+ let parent = this.parent;
2394
+ while (parent) {
2395
+ if (parent.rtt) {
2396
+ return parent;
2397
+ }
2398
+ parent = parent.parent;
2399
+ }
2400
+ return null;
2401
+ }
2402
+
2403
+ get texture(): Texture | null {
2404
+ return this.props.texture;
2405
+ }
2406
+
2407
+ set texture(value: Texture | null) {
2408
+ if (this.props.texture === value) {
2409
+ return;
2410
+ }
2411
+
2412
+ const oldTexture = this.props.texture;
2413
+ if (oldTexture) {
2414
+ oldTexture.setRenderableOwner(this, false);
2415
+ this.unloadTexture();
2416
+ }
2417
+
2418
+ this.props.texture = value;
2419
+ if (value !== null) {
2420
+ value.setRenderableOwner(this, this.isRenderable);
2421
+ this.loadTexture();
2422
+ }
2423
+
2424
+ this.setUpdateType(UpdateType.IsRenderable);
2425
+ }
2426
+
2427
+ set textureOptions(value: TextureOptions) {
2428
+ this.props.textureOptions = value;
2429
+ }
2430
+
2431
+ get textureOptions(): TextureOptions {
2432
+ return this.props.textureOptions;
2433
+ }
2434
+
2435
+ get strictBounds(): boolean {
2436
+ return this.props.strictBounds;
2437
+ }
2438
+
2439
+ set strictBounds(v) {
2440
+ if (v === this.props.strictBounds) {
2441
+ return;
2442
+ }
2443
+
2444
+ this.props.strictBounds = v;
2445
+ this.setUpdateType(UpdateType.RenderBounds | UpdateType.Children);
2446
+ this.childUpdateType |= UpdateType.RenderBounds | UpdateType.Children;
2447
+ }
2448
+
2449
+ animate(
2450
+ props: Partial<CoreNodeAnimateProps>,
2451
+ settings: Partial<AnimationSettings>,
2452
+ ): IAnimationController {
2453
+ const animation = new CoreAnimation(this, props, settings);
2454
+
2455
+ const controller = new CoreAnimationController(
2456
+ this.stage.animationManager,
2457
+ animation,
2458
+ );
2459
+
2460
+ return controller;
2461
+ }
2462
+
2463
+ flush() {
2464
+ // no-op
2465
+ }
2466
+
2467
+ //#endregion Properties
2468
+ }