@lightningjs/renderer 3.0.0-beta10 → 3.0.0-beta11

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 (275) hide show
  1. package/LICENSE +202 -202
  2. package/NOTICE +3 -3
  3. package/README.md +133 -133
  4. package/dist/src/core/CoreTextNode.d.ts +3 -0
  5. package/dist/src/core/CoreTextNode.js +45 -10
  6. package/dist/src/core/CoreTextNode.js.map +1 -1
  7. package/dist/src/core/platform.d.ts +10 -0
  8. package/dist/src/core/platform.js +81 -0
  9. package/dist/src/core/platform.js.map +1 -0
  10. package/dist/src/core/renderers/CoreShader.d.ts +9 -0
  11. package/dist/src/core/renderers/CoreShader.js +28 -0
  12. package/dist/src/core/renderers/CoreShader.js.map +1 -0
  13. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.d.ts +33 -0
  14. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js +250 -0
  15. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js.map +1 -0
  16. package/dist/src/core/renderers/canvas/CanvasCoreTexture.d.ts +16 -0
  17. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js +124 -0
  18. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js.map +1 -0
  19. package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.d.ts +13 -0
  20. package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.js +113 -192
  21. package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.js.map +1 -1
  22. package/dist/src/core/renderers/canvas/internal/ColorUtils.d.ts +0 -2
  23. package/dist/src/core/renderers/canvas/internal/ColorUtils.js +0 -14
  24. package/dist/src/core/renderers/canvas/internal/ColorUtils.js.map +1 -1
  25. package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.d.ts +10 -0
  26. package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.js +43 -0
  27. package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.js.map +1 -0
  28. package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.d.ts +12 -0
  29. package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.js +58 -0
  30. package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.js.map +1 -0
  31. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.d.ts +9 -0
  32. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js +38 -0
  33. package/dist/src/core/renderers/webgl/WebGlCoreCtxSubTexture.js.map +1 -0
  34. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.d.ts +56 -0
  35. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +239 -0
  36. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -0
  37. package/dist/src/core/renderers/webgl/WebGlCoreRenderOp.d.ts +34 -0
  38. package/dist/src/core/renderers/webgl/WebGlCoreRenderOp.js +114 -0
  39. package/dist/src/core/renderers/webgl/WebGlCoreRenderOp.js.map +1 -0
  40. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.d.ts +133 -0
  41. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js +616 -0
  42. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js.map +1 -0
  43. package/dist/src/core/renderers/webgl/WebGlCoreShader.d.ts +83 -0
  44. package/dist/src/core/renderers/webgl/WebGlCoreShader.js +233 -0
  45. package/dist/src/core/renderers/webgl/WebGlCoreShader.js.map +1 -0
  46. package/dist/src/core/renderers/webgl/internal/ShaderUtils.js +35 -35
  47. package/dist/src/core/renderers/webgl/shaders/DefaultShader.d.ts +9 -0
  48. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +87 -0
  49. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js.map +1 -0
  50. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.d.ts +10 -0
  51. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +119 -0
  52. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js.map +1 -0
  53. package/dist/src/core/renderers/webgl/shaders/DynamicShader.d.ts +29 -0
  54. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +413 -0
  55. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js.map +1 -0
  56. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.d.ts +28 -0
  57. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +131 -0
  58. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js.map +1 -0
  59. package/dist/src/core/renderers/webgl/shaders/SdfShader.d.ts +47 -0
  60. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +160 -0
  61. package/dist/src/core/renderers/webgl/shaders/SdfShader.js.map +1 -0
  62. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.d.ts +31 -0
  63. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +71 -0
  64. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js.map +1 -0
  65. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.d.ts +30 -0
  66. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +58 -0
  67. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js.map +1 -0
  68. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.d.ts +31 -0
  69. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +71 -0
  70. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js.map +1 -0
  71. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.d.ts +31 -0
  72. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +71 -0
  73. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js.map +1 -0
  74. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.d.ts +31 -0
  75. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +71 -0
  76. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js.map +1 -0
  77. package/dist/src/core/renderers/webgl/shaders/effects/EffectUtils.d.ts +9 -0
  78. package/dist/src/core/renderers/webgl/shaders/effects/EffectUtils.js +136 -0
  79. package/dist/src/core/renderers/webgl/shaders/effects/EffectUtils.js.map +1 -0
  80. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.d.ts +36 -0
  81. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +85 -0
  82. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js.map +1 -0
  83. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.d.ts +45 -0
  84. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +104 -0
  85. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js.map +1 -0
  86. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.d.ts +22 -0
  87. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +45 -0
  88. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js.map +1 -0
  89. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.d.ts +58 -0
  90. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +80 -0
  91. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js.map +1 -0
  92. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.d.ts +35 -0
  93. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +129 -0
  94. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js.map +1 -0
  95. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.d.ts +39 -0
  96. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +116 -0
  97. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js.map +1 -0
  98. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.d.ts +61 -0
  99. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +127 -0
  100. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js.map +1 -0
  101. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.d.ts +40 -0
  102. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +71 -0
  103. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js.map +1 -0
  104. package/dist/src/core/renderers/webgl/shaders/effects/ShaderEffect.d.ts +115 -0
  105. package/dist/src/core/renderers/webgl/shaders/effects/ShaderEffect.js +61 -0
  106. package/dist/src/core/renderers/webgl/shaders/effects/ShaderEffect.js.map +1 -0
  107. package/dist/src/core/shaders/templates/shaderUtils.d.ts +5 -0
  108. package/dist/src/core/shaders/templates/shaderUtils.js +41 -0
  109. package/dist/src/core/shaders/templates/shaderUtils.js.map +1 -0
  110. package/dist/src/core/shaders/webgl/Border.js +82 -82
  111. package/dist/src/core/shaders/webgl/Default.js +47 -47
  112. package/dist/src/core/shaders/webgl/DefaultBatched.js +61 -61
  113. package/dist/src/core/shaders/webgl/HolePunch.js +32 -32
  114. package/dist/src/core/shaders/webgl/LinearGradient.js +36 -36
  115. package/dist/src/core/shaders/webgl/RadialGradient.js +33 -33
  116. package/dist/src/core/shaders/webgl/Rounded.js +71 -71
  117. package/dist/src/core/shaders/webgl/RoundedWithBorder.js +111 -111
  118. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js +130 -130
  119. package/dist/src/core/shaders/webgl/RoundedWithShadow.js +54 -54
  120. package/dist/src/core/shaders/webgl/SdfShader.js +62 -62
  121. package/dist/src/core/shaders/webgl/Shadow.js +83 -83
  122. package/dist/src/core/shaders/webgl/Spinner.d.ts +1 -0
  123. package/dist/src/core/shaders/webgl/Spinner.js +2 -0
  124. package/dist/src/core/shaders/webgl/Spinner.js.map +1 -0
  125. package/dist/src/core/text-rendering/CanvasFontHandler.d.ts +16 -0
  126. package/dist/src/core/text-rendering/CanvasFontHandler.js +29 -0
  127. package/dist/src/core/text-rendering/CanvasFontHandler.js.map +1 -1
  128. package/dist/src/core/text-rendering/SdfFontHandler.d.ts +15 -0
  129. package/dist/src/core/text-rendering/SdfFontHandler.js +34 -2
  130. package/dist/src/core/text-rendering/SdfFontHandler.js.map +1 -1
  131. package/dist/src/core/text-rendering/TextRenderer.d.ts +2 -0
  132. package/dist/src/core/text-rendering/TextTextureRendererUtils.js.map +1 -1
  133. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js +2 -2
  134. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js.map +1 -1
  135. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js +0 -5
  136. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js.map +1 -1
  137. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.d.ts +1 -7
  138. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js +2 -50
  139. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js.map +1 -1
  140. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.d.ts +3 -2
  141. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js +83 -42
  142. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js.map +1 -1
  143. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.d.ts +1 -1
  144. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js +8 -66
  145. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js.map +1 -1
  146. package/dist/src/core/text-rendering/renderers/TextRenderer.d.ts +4 -14
  147. package/dist/src/core/text-rendering/renderers/TextRenderer.js +0 -3
  148. package/dist/src/core/text-rendering/renderers/TextRenderer.js.map +1 -1
  149. package/dist/src/core/text-rendering/sdf/PeekableGenerator.d.ts +12 -0
  150. package/dist/src/core/text-rendering/sdf/PeekableGenerator.js +61 -0
  151. package/dist/src/core/text-rendering/sdf/PeekableGenerator.js.map +1 -0
  152. package/dist/src/core/text-rendering/sdf/SimpleFontShaper.d.ts +45 -0
  153. package/dist/src/core/text-rendering/sdf/SimpleFontShaper.js +69 -0
  154. package/dist/src/core/text-rendering/sdf/SimpleFontShaper.js.map +1 -0
  155. package/dist/src/main-api/DynamicShaderController.d.ts +29 -0
  156. package/dist/src/main-api/DynamicShaderController.js +58 -0
  157. package/dist/src/main-api/DynamicShaderController.js.map +1 -0
  158. package/dist/src/main-api/ShaderController.d.ts +31 -0
  159. package/dist/src/main-api/ShaderController.js +37 -0
  160. package/dist/src/main-api/ShaderController.js.map +1 -0
  161. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  162. package/exports/canvas-shaders.ts +28 -28
  163. package/exports/canvas.ts +45 -45
  164. package/exports/index.ts +82 -82
  165. package/exports/inspector.ts +24 -24
  166. package/exports/utils.ts +50 -50
  167. package/exports/webgl-shaders.ts +28 -28
  168. package/exports/webgl.ts +52 -52
  169. package/package.json +2 -1
  170. package/src/common/CommonTypes.ts +146 -146
  171. package/src/common/EventEmitter.ts +77 -77
  172. package/src/common/IAnimationController.ts +92 -92
  173. package/src/common/IEventEmitter.ts +28 -28
  174. package/src/core/CoreNode.test.ts +202 -202
  175. package/src/core/CoreNode.ts +2491 -2491
  176. package/src/core/CoreShaderManager.ts +188 -188
  177. package/src/core/CoreTextNode.ts +483 -443
  178. package/src/core/CoreTextureManager.ts +565 -565
  179. package/src/core/Stage.ts +906 -906
  180. package/src/core/TextureMemoryManager.ts +445 -445
  181. package/src/core/animations/AnimationManager.ts +38 -38
  182. package/src/core/animations/CoreAnimation.ts +291 -291
  183. package/src/core/animations/CoreAnimationController.ts +166 -166
  184. package/src/core/lib/ContextSpy.ts +41 -41
  185. package/src/core/lib/ImageWorker.ts +286 -286
  186. package/src/core/lib/Matrix3d.ts +244 -244
  187. package/src/core/lib/RenderCoords.ts +71 -71
  188. package/src/core/lib/WebGlContextWrapper.ts +1381 -1381
  189. package/src/core/lib/colorCache.ts +20 -20
  190. package/src/core/lib/colorParser.ts +85 -85
  191. package/src/core/lib/textureCompression.ts +152 -152
  192. package/src/core/lib/textureSvg.ts +78 -78
  193. package/src/core/lib/utils.ts +412 -412
  194. package/src/core/lib/validateImageBitmap.ts +87 -87
  195. package/src/core/platforms/Platform.ts +77 -77
  196. package/src/core/platforms/web/WebPlatform.ts +121 -121
  197. package/src/core/renderers/CoreContextTexture.ts +43 -43
  198. package/src/core/renderers/CoreRenderOp.ts +22 -22
  199. package/src/core/renderers/CoreRenderer.ts +110 -110
  200. package/src/core/renderers/CoreShaderNode.ts +175 -175
  201. package/src/core/renderers/CoreShaderProgram.ts +23 -23
  202. package/src/core/renderers/canvas/CanvasRenderer.ts +283 -283
  203. package/src/core/renderers/canvas/CanvasShaderNode.ts +96 -96
  204. package/src/core/renderers/canvas/CanvasTexture.ts +156 -156
  205. package/src/core/renderers/webgl/WebGlCtxRenderTexture.ts +91 -91
  206. package/src/core/renderers/webgl/WebGlCtxSubTexture.ts +50 -50
  207. package/src/core/renderers/webgl/WebGlCtxTexture.ts +310 -310
  208. package/src/core/renderers/webgl/WebGlRenderOp.ts +167 -167
  209. package/src/core/renderers/webgl/WebGlRenderer.ts +747 -747
  210. package/src/core/renderers/webgl/WebGlShaderNode.ts +435 -435
  211. package/src/core/renderers/webgl/WebGlShaderProgram.ts +341 -341
  212. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  213. package/src/core/renderers/webgl/internal/RendererUtils.ts +155 -155
  214. package/src/core/renderers/webgl/internal/ShaderUtils.ts +281 -281
  215. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  216. package/src/core/shaders/canvas/Border.ts +75 -75
  217. package/src/core/shaders/canvas/HolePunch.ts +62 -62
  218. package/src/core/shaders/canvas/LinearGradient.ts +71 -71
  219. package/src/core/shaders/canvas/RadialGradient.ts +99 -99
  220. package/src/core/shaders/canvas/Rounded.ts +55 -55
  221. package/src/core/shaders/canvas/RoundedWithBorder.ts +74 -74
  222. package/src/core/shaders/canvas/RoundedWithBorderAndShadow.ts +90 -90
  223. package/src/core/shaders/canvas/RoundedWithShadow.ts +70 -70
  224. package/src/core/shaders/canvas/Shadow.ts +52 -52
  225. package/src/core/shaders/canvas/utils/render.ts +151 -151
  226. package/src/core/shaders/templates/BorderTemplate.ts +115 -115
  227. package/src/core/shaders/templates/HolePunchTemplate.ts +82 -82
  228. package/src/core/shaders/templates/LinearGradientTemplate.ts +71 -71
  229. package/src/core/shaders/templates/RadialGradientTemplate.ts +81 -81
  230. package/src/core/shaders/templates/RoundedTemplate.ts +98 -98
  231. package/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.ts +38 -38
  232. package/src/core/shaders/templates/RoundedWithBorderTemplate.ts +35 -35
  233. package/src/core/shaders/templates/RoundedWithShadowTemplate.ts +35 -35
  234. package/src/core/shaders/templates/ShadowTemplate.ts +106 -106
  235. package/src/core/shaders/utils.ts +46 -46
  236. package/src/core/shaders/webgl/Border.ts +116 -116
  237. package/src/core/shaders/webgl/Default.ts +89 -89
  238. package/src/core/shaders/webgl/DefaultBatched.ts +129 -129
  239. package/src/core/shaders/webgl/HolePunch.ts +75 -75
  240. package/src/core/shaders/webgl/LinearGradient.ts +82 -82
  241. package/src/core/shaders/webgl/RadialGradient.ts +85 -85
  242. package/src/core/shaders/webgl/Rounded.ts +117 -117
  243. package/src/core/shaders/webgl/RoundedWithBorder.ts +155 -155
  244. package/src/core/shaders/webgl/RoundedWithBorderAndShadow.ts +175 -175
  245. package/src/core/shaders/webgl/RoundedWithShadow.ts +98 -98
  246. package/src/core/shaders/webgl/SdfShader.ts +134 -134
  247. package/src/core/shaders/webgl/Shadow.ts +115 -115
  248. package/src/core/text-rendering/CanvasFontHandler.ts +210 -176
  249. package/src/core/text-rendering/CanvasTextRenderer.ts +622 -622
  250. package/src/core/text-rendering/SdfFontHandler.ts +554 -517
  251. package/src/core/text-rendering/SdfTextRenderer.ts +466 -466
  252. package/src/core/text-rendering/TextRenderer.ts +406 -404
  253. package/src/core/text-rendering/Utils.ts +257 -257
  254. package/src/core/text-rendering/canvas/Settings.ts +99 -99
  255. package/src/core/text-rendering/canvas/Utils.test.ts +206 -206
  256. package/src/core/text-rendering/canvas/Utils.ts +178 -178
  257. package/src/core/text-rendering/canvas/calculateRenderInfo.ts +299 -299
  258. package/src/core/text-rendering/canvas/draw.ts +165 -165
  259. package/src/core/text-rendering/sdf/Utils.test.ts +402 -402
  260. package/src/core/text-rendering/sdf/Utils.ts +436 -436
  261. package/src/core/text-rendering/sdf/index.ts +20 -20
  262. package/src/core/textures/ColorTexture.ts +102 -102
  263. package/src/core/textures/ImageTexture.ts +418 -418
  264. package/src/core/textures/NoiseTexture.ts +104 -104
  265. package/src/core/textures/RenderTexture.ts +85 -85
  266. package/src/core/textures/SubTexture.ts +205 -205
  267. package/src/core/textures/Texture.ts +381 -381
  268. package/src/core/utils.ts +227 -227
  269. package/src/env.d.ts +7 -7
  270. package/src/main-api/INode.ts +100 -100
  271. package/src/main-api/Inspector.ts +567 -567
  272. package/src/main-api/Renderer.ts +873 -873
  273. package/src/main-api/utils.ts +45 -45
  274. package/src/utils.ts +267 -267
  275. package/COPYING +0 -1
@@ -1,2491 +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
- 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
- }
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
+ }