@lightningjs/renderer 2.9.0 → 2.9.1

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