@lightningjs/renderer 2.16.0 → 3.0.0-beta10

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