@lightningjs/renderer 0.7.1 → 0.7.3

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 (215) hide show
  1. package/COPYING +1 -0
  2. package/LICENSE +202 -202
  3. package/NOTICE +3 -3
  4. package/README.md +221 -221
  5. package/dist/exports/main-api.d.ts +1 -0
  6. package/dist/src/common/CommonTypes.d.ts +7 -0
  7. package/dist/src/core/CoreShaderManager.d.ts +13 -6
  8. package/dist/src/core/CoreShaderManager.js +6 -6
  9. package/dist/src/core/CoreShaderManager.js.map +1 -1
  10. package/dist/src/core/Stage.d.ts +2 -1
  11. package/dist/src/core/Stage.js +4 -0
  12. package/dist/src/core/Stage.js.map +1 -1
  13. package/dist/src/core/animations/CoreAnimation.js +3 -0
  14. package/dist/src/core/animations/CoreAnimation.js.map +1 -1
  15. package/dist/src/core/animations/CoreAnimationController.d.ts +8 -0
  16. package/dist/src/core/animations/CoreAnimationController.js +26 -0
  17. package/dist/src/core/animations/CoreAnimationController.js.map +1 -1
  18. package/dist/src/core/lib/ImageWorker.js +21 -21
  19. package/dist/src/core/lib/WebGlContextWrapper.d.ts +9 -0
  20. package/dist/src/core/lib/WebGlContextWrapper.js +12 -0
  21. package/dist/src/core/lib/WebGlContextWrapper.js.map +1 -1
  22. package/dist/src/core/lib/textureCompression.d.ts +16 -0
  23. package/dist/src/core/lib/textureCompression.js +129 -0
  24. package/dist/src/core/lib/textureCompression.js.map +1 -0
  25. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +12 -0
  26. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -1
  27. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +47 -47
  28. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  29. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  30. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  31. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +49 -49
  32. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  33. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +5 -5
  34. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  35. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  36. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  37. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  38. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  39. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  40. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +31 -31
  41. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +13 -13
  42. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  43. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  44. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.d.ts +4 -0
  45. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js +11 -0
  46. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.js.map +1 -1
  47. package/dist/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.js.map +1 -1
  48. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js +10 -1
  49. package/dist/src/core/text-rendering/renderers/CanvasTextRenderer.js.map +1 -1
  50. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js +21 -7
  51. package/dist/src/core/text-rendering/renderers/LightningTextTextureRenderer.js.map +1 -1
  52. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js.map +1 -1
  53. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js +2 -1
  54. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.js.map +1 -1
  55. package/dist/src/core/textures/ImageTexture.js +5 -0
  56. package/dist/src/core/textures/ImageTexture.js.map +1 -1
  57. package/dist/src/core/textures/Texture.d.ts +28 -1
  58. package/dist/src/core/textures/Texture.js.map +1 -1
  59. package/dist/src/main-api/ICoreDriver.d.ts +2 -1
  60. package/dist/src/main-api/Inspector.d.ts +21 -0
  61. package/dist/src/main-api/Inspector.js +302 -0
  62. package/dist/src/main-api/Inspector.js.map +1 -0
  63. package/dist/src/main-api/RendererMain.d.ts +12 -0
  64. package/dist/src/main-api/RendererMain.js +19 -1
  65. package/dist/src/main-api/RendererMain.js.map +1 -1
  66. package/dist/src/render-drivers/main/MainCoreDriver.d.ts +2 -1
  67. package/dist/src/render-drivers/main/MainCoreDriver.js +7 -1
  68. package/dist/src/render-drivers/main/MainCoreDriver.js.map +1 -1
  69. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.d.ts +2 -1
  70. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js +7 -0
  71. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js.map +1 -1
  72. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.d.ts +9 -1
  73. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js.map +1 -1
  74. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  75. package/exports/core-api.ts +102 -102
  76. package/exports/main-api.ts +60 -51
  77. package/exports/utils.ts +41 -41
  78. package/package.json +5 -3
  79. package/scripts/please-use-pnpm.js +13 -0
  80. package/src/common/CommonTypes.ts +113 -105
  81. package/src/common/EventEmitter.ts +77 -77
  82. package/src/common/IAnimationController.ts +29 -29
  83. package/src/core/CoreExtension.ts +32 -32
  84. package/src/core/CoreNode.ts +926 -926
  85. package/src/core/CoreShaderManager.ts +243 -210
  86. package/src/core/CoreTextNode.ts +391 -391
  87. package/src/core/CoreTextureManager.ts +326 -326
  88. package/src/core/Stage.ts +354 -342
  89. package/src/core/animations/AnimationManager.ts +38 -38
  90. package/src/core/animations/CoreAnimation.ts +181 -177
  91. package/src/core/animations/CoreAnimationController.ts +148 -117
  92. package/src/core/lib/ContextSpy.ts +41 -41
  93. package/src/core/lib/ImageWorker.ts +124 -124
  94. package/src/core/lib/Matrix3d.ts +290 -290
  95. package/src/core/lib/WebGlContextWrapper.ts +992 -965
  96. package/src/core/lib/textureCompression.ts +152 -0
  97. package/src/core/lib/utils.ts +241 -241
  98. package/src/core/platform.ts +46 -46
  99. package/src/core/renderers/CoreContextTexture.ts +30 -30
  100. package/src/core/renderers/CoreRenderOp.ts +22 -22
  101. package/src/core/renderers/CoreRenderer.ts +63 -63
  102. package/src/core/renderers/CoreShader.ts +41 -41
  103. package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +37 -37
  104. package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +231 -211
  105. package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +107 -107
  106. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +520 -520
  107. package/src/core/renderers/webgl/WebGlCoreShader.ts +337 -337
  108. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  109. package/src/core/renderers/webgl/internal/RendererUtils.ts +131 -131
  110. package/src/core/renderers/webgl/internal/ShaderUtils.ts +136 -136
  111. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  112. package/src/core/renderers/webgl/shaders/DefaultShader.ts +95 -95
  113. package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
  114. package/src/core/renderers/webgl/shaders/DynamicShader.ts +474 -474
  115. package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +161 -161
  116. package/src/core/renderers/webgl/shaders/SdfShader.ts +174 -174
  117. package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
  118. package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +86 -86
  119. package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
  120. package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
  121. package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
  122. package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +33 -33
  123. package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +135 -135
  124. package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +145 -145
  125. package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
  126. package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +176 -176
  127. package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +159 -159
  128. package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +186 -186
  129. package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +121 -121
  130. package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +114 -114
  131. package/src/core/text-rendering/TextTextureRendererUtils.ts +189 -189
  132. package/src/core/text-rendering/TrFontManager.ts +96 -96
  133. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +141 -127
  134. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  135. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -165
  136. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +169 -166
  137. package/src/core/text-rendering/font-face-types/TrFontFace.ts +105 -105
  138. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +77 -77
  139. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +751 -738
  140. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +741 -707
  141. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +775 -777
  142. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  143. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  144. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  145. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  146. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +84 -84
  147. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  148. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  149. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +393 -392
  150. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -46
  151. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +51 -51
  152. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  153. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  154. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  155. package/src/core/text-rendering/renderers/TextRenderer.ts +504 -504
  156. package/src/core/textures/ColorTexture.ts +86 -86
  157. package/src/core/textures/ImageTexture.ts +133 -124
  158. package/src/core/textures/NoiseTexture.ts +96 -96
  159. package/src/core/textures/SubTexture.ts +143 -143
  160. package/src/core/textures/Texture.ts +218 -186
  161. package/src/core/utils.ts +224 -224
  162. package/src/env.d.ts +7 -7
  163. package/src/main-api/ICoreDriver.ts +66 -61
  164. package/src/main-api/INode.ts +470 -470
  165. package/src/main-api/Inspector.ts +432 -0
  166. package/src/main-api/RendererMain.ts +649 -610
  167. package/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.ts +45 -45
  168. package/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.ts +154 -154
  169. package/src/main-api/texture-usage-trackers/TextureUsageTracker.ts +54 -54
  170. package/src/render-drivers/main/MainCoreDriver.ts +148 -133
  171. package/src/render-drivers/main/MainOnlyNode.ts +453 -453
  172. package/src/render-drivers/main/MainOnlyTextNode.ts +261 -261
  173. package/src/render-drivers/threadx/NodeStruct.ts +300 -300
  174. package/src/render-drivers/threadx/SharedNode.ts +97 -97
  175. package/src/render-drivers/threadx/TextNodeStruct.ts +211 -211
  176. package/src/render-drivers/threadx/ThreadXCoreDriver.ts +285 -273
  177. package/src/render-drivers/threadx/ThreadXMainAnimationController.ts +99 -99
  178. package/src/render-drivers/threadx/ThreadXMainNode.ts +178 -178
  179. package/src/render-drivers/threadx/ThreadXMainTextNode.ts +85 -85
  180. package/src/render-drivers/threadx/ThreadXRendererMessage.ts +110 -97
  181. package/src/render-drivers/threadx/worker/ThreadXRendererNode.ts +238 -238
  182. package/src/render-drivers/threadx/worker/ThreadXRendererTextNode.ts +149 -149
  183. package/src/render-drivers/threadx/worker/renderer.ts +151 -151
  184. package/src/render-drivers/utils.ts +57 -57
  185. package/src/utils.ts +207 -207
  186. package/dist/src/core/lib/WebGlContext.d.ts +0 -414
  187. package/dist/src/core/lib/WebGlContext.js +0 -640
  188. package/dist/src/core/lib/WebGlContext.js.map +0 -1
  189. package/dist/src/core/scene/Scene.d.ts +0 -59
  190. package/dist/src/core/scene/Scene.js +0 -106
  191. package/dist/src/core/scene/Scene.js.map +0 -1
  192. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/findNearestMultiple.d.ts +0 -8
  193. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/findNearestMultiple.js +0 -29
  194. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/findNearestMultiple.js.map +0 -1
  195. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/SdfBufferHelper.d.ts +0 -19
  196. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/SdfBufferHelper.js +0 -84
  197. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/SdfBufferHelper.js.map +0 -1
  198. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutLine.d.ts +0 -8
  199. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutLine.js +0 -40
  200. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutLine.js.map +0 -1
  201. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutText2.d.ts +0 -2
  202. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutText2.js +0 -41
  203. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutText2.js.map +0 -1
  204. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/utils.d.ts +0 -1
  205. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/utils.js +0 -4
  206. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/utils.js.map +0 -1
  207. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2.d.ts +0 -1
  208. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2.js +0 -2
  209. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2.js.map +0 -1
  210. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.d.ts +0 -20
  211. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.js +0 -55
  212. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.js.map +0 -1
  213. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/roundUpToMultiple.d.ts +0 -9
  214. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/roundUpToMultiple.js +0 -32
  215. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/roundUpToMultiple.js.map +0 -1
@@ -1,926 +1,926 @@
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 { assertTruthy, mergeColorAlphaPremultiplied } from '../utils.js';
21
- import type { ShaderMap } from './CoreShaderManager.js';
22
- import type {
23
- ExtractProps,
24
- TextureMap,
25
- TextureOptions,
26
- } from './CoreTextureManager.js';
27
- import type { CoreRenderer } from './renderers/CoreRenderer.js';
28
- import type { CoreShader } from './renderers/CoreShader.js';
29
- import type { Stage } from './Stage.js';
30
- import type {
31
- Texture,
32
- TextureFailedEventHandler,
33
- TextureLoadedEventHandler,
34
- } from './textures/Texture.js';
35
- import type {
36
- NodeTextureFailedPayload,
37
- NodeTextureLoadedPayload,
38
- } from '../common/CommonTypes.js';
39
- import { EventEmitter } from '../common/EventEmitter.js';
40
- import { copyRect, intersectRect, type RectWithValid } from './lib/utils.js';
41
- import { Matrix3d } from './lib/Matrix3d.js';
42
-
43
- export interface CoreNodeProps {
44
- id: number;
45
- // External facing properties whose defaults are determined by
46
- // RendererMain's resolveNodeDefaults() method
47
- x: number;
48
- y: number;
49
- width: number;
50
- height: number;
51
- alpha: number;
52
- clipping: boolean;
53
- color: number;
54
- colorTop: number;
55
- colorBottom: number;
56
- colorLeft: number;
57
- colorRight: number;
58
- colorTl: number;
59
- colorTr: number;
60
- colorBl: number;
61
- colorBr: number;
62
- parent: CoreNode | null;
63
- zIndex: number;
64
- texture: Texture | null;
65
- textureOptions: TextureOptions | null;
66
- shader: CoreShader | null;
67
- shaderProps: Record<string, unknown> | null;
68
- zIndexLocked: number;
69
- scaleX: number;
70
- scaleY: number;
71
- mount: number;
72
- mountX: number;
73
- mountY: number;
74
- pivot: number;
75
- pivotX: number;
76
- pivotY: number;
77
- rotation: number;
78
- }
79
-
80
- type ICoreNode = Omit<
81
- CoreNodeProps,
82
- 'texture' | 'textureOptions' | 'shader' | 'shaderProps'
83
- >;
84
-
85
- enum UpdateType {
86
- /**
87
- * Child updates
88
- */
89
- Children = 1,
90
-
91
- /**
92
- * Scale/Rotate transform update
93
- */
94
- ScaleRotate = 2,
95
-
96
- /**
97
- * Translate transform update (x/y/width/height/pivot/mount)
98
- */
99
- Local = 4,
100
-
101
- /**
102
- * Global transform update
103
- */
104
- Global = 8,
105
-
106
- /**
107
- * Clipping rect update
108
- */
109
- Clipping = 16,
110
-
111
- /**
112
- * Calculated ZIndex update
113
- */
114
- CalculatedZIndex = 32,
115
-
116
- /**
117
- * Z-Index Sorted Children update
118
- */
119
- ZIndexSortedChildren = 64,
120
-
121
- /**
122
- * Premultiplied Colors
123
- */
124
- PremultipliedColors = 128,
125
-
126
- /**
127
- * World Alpha
128
- *
129
- * @remarks
130
- * World Alpha = Parent World Alpha * Alpha
131
- */
132
- WorldAlpha = 256,
133
-
134
- /**
135
- * None
136
- */
137
- None = 0,
138
-
139
- /**
140
- * All
141
- */
142
- All = 511,
143
- }
144
-
145
- export class CoreNode extends EventEmitter implements ICoreNode {
146
- readonly children: CoreNode[] = [];
147
- protected props: Required<CoreNodeProps>;
148
-
149
- public updateType = UpdateType.All;
150
- public globalTransform?: Matrix3d;
151
- public scaleRotateTransform?: Matrix3d;
152
- public localTransform?: Matrix3d;
153
- public clippingRect: RectWithValid = {
154
- x: 0,
155
- y: 0,
156
- width: 0,
157
- height: 0,
158
- valid: false,
159
- };
160
- public isRenderable = false;
161
- public worldAlpha = 1;
162
- public premultipliedColorTl = 0;
163
- public premultipliedColorTr = 0;
164
- public premultipliedColorBl = 0;
165
- public premultipliedColorBr = 0;
166
- public calcZIndex = 0;
167
-
168
- private isComplex = false;
169
-
170
- constructor(protected stage: Stage, props: CoreNodeProps) {
171
- super();
172
- this.props = {
173
- ...props,
174
- parent: null,
175
- };
176
- // Allow for parent to be processed appropriately
177
- this.parent = props.parent;
178
- this.updateScaleRotateTransform();
179
- }
180
-
181
- //#region Textures
182
- loadTexture<Type extends keyof TextureMap>(
183
- textureType: Type,
184
- props: ExtractProps<TextureMap[Type]>,
185
- options: TextureOptions | null = null,
186
- ): void {
187
- // First unload any existing texture
188
- if (this.props.texture) {
189
- this.unloadTexture();
190
- }
191
- const { txManager } = this.stage;
192
- const texture = txManager.loadTexture(textureType, props, options);
193
-
194
- this.props.texture = texture;
195
- this.props.textureOptions = options;
196
- this.checkIsRenderable();
197
-
198
- // If texture is already loaded / failed, trigger loaded event manually
199
- // so that users get a consistent event experience.
200
- // We do this in a microtask to allow listeners to be attached in the same
201
- // synchronous task after calling loadTexture()
202
- queueMicrotask(() => {
203
- if (texture.state === 'loaded') {
204
- this.onTextureLoaded(texture, texture.dimensions!);
205
- } else if (texture.state === 'failed') {
206
- this.onTextureFailed(texture, texture.error!);
207
- }
208
- texture.on('loaded', this.onTextureLoaded);
209
- texture.on('failed', this.onTextureFailed);
210
- });
211
- }
212
-
213
- unloadTexture(): void {
214
- if (this.props.texture) {
215
- this.props.texture.off('loaded', this.onTextureLoaded);
216
- this.props.texture.off('failed', this.onTextureFailed);
217
- }
218
- this.props.texture = null;
219
- this.props.textureOptions = null;
220
- this.checkIsRenderable();
221
- }
222
-
223
- private onTextureLoaded: TextureLoadedEventHandler = (target, dimensions) => {
224
- // Texture was loaded. In case the RAF loop has already stopped, we request
225
- // a render to ensure the texture is rendered.
226
- this.stage.requestRender();
227
- this.emit('loaded', {
228
- type: 'texture',
229
- dimensions,
230
- } satisfies NodeTextureLoadedPayload);
231
- };
232
-
233
- private onTextureFailed: TextureFailedEventHandler = (target, error) => {
234
- this.emit('failed', {
235
- type: 'texture',
236
- error,
237
- } satisfies NodeTextureFailedPayload);
238
- };
239
- //#endregion Textures
240
-
241
- loadShader<Type extends keyof ShaderMap>(
242
- shaderType: Type,
243
- props: ExtractProps<ShaderMap[Type]>,
244
- ): void {
245
- const shManager = this.stage.renderer.getShaderManager();
246
- assertTruthy(shManager);
247
- const { shader, props: p } = shManager.loadShader(shaderType, props);
248
- this.props.shader = shader;
249
- this.props.shaderProps = p;
250
- this.checkIsRenderable();
251
- }
252
-
253
- /**
254
- * Change types types is used to determine the scope of the changes being applied
255
- *
256
- * @remarks
257
- * See {@link UpdateType} for more information on each type
258
- *
259
- * @param type
260
- */
261
- setUpdateType(type: UpdateType): void {
262
- this.updateType |= type;
263
-
264
- // If we're updating this node at all, we need to inform the parent
265
- // (and all ancestors) that their children need updating as well
266
- const parent = this.props.parent;
267
- if (parent && !(parent.updateType & UpdateType.Children)) {
268
- parent.setUpdateType(UpdateType.Children);
269
- }
270
- }
271
-
272
- sortChildren() {
273
- this.children.sort((a, b) => a.calcZIndex - b.calcZIndex);
274
- }
275
-
276
- updateScaleRotateTransform() {
277
- this.scaleRotateTransform = Matrix3d.rotate(
278
- this.props.rotation,
279
- this.scaleRotateTransform,
280
- ).scale(this.props.scaleX, this.props.scaleY);
281
- }
282
-
283
- updateLocalTransform() {
284
- assertTruthy(this.scaleRotateTransform);
285
- const pivotTranslateX = this.props.pivotX * this.props.width;
286
- const pivotTranslateY = this.props.pivotY * this.props.height;
287
- const mountTranslateX = this.props.mountX * this.props.width;
288
- const mountTranslateY = this.props.mountY * this.props.height;
289
-
290
- this.localTransform = Matrix3d.translate(
291
- pivotTranslateX - mountTranslateX + this.props.x,
292
- pivotTranslateY - mountTranslateY + this.props.y,
293
- this.localTransform,
294
- )
295
- .multiply(this.scaleRotateTransform)
296
- .translate(-pivotTranslateX, -pivotTranslateY);
297
- this.setUpdateType(UpdateType.Global);
298
- }
299
-
300
- /**
301
- * @todo: test for correct calculation flag
302
- * @param delta
303
- */
304
- update(delta: number, parentClippingRect: RectWithValid): void {
305
- if (this.updateType & UpdateType.ScaleRotate) {
306
- this.updateScaleRotateTransform();
307
- this.setUpdateType(UpdateType.Local);
308
- }
309
-
310
- if (this.updateType & UpdateType.Local) {
311
- this.updateLocalTransform();
312
- this.setUpdateType(UpdateType.Global);
313
- }
314
-
315
- const parent = this.props.parent;
316
- let childUpdateType = UpdateType.None;
317
- if (this.updateType & UpdateType.Global) {
318
- assertTruthy(this.localTransform);
319
- this.globalTransform = Matrix3d.copy(
320
- parent?.globalTransform || this.localTransform,
321
- this.globalTransform,
322
- );
323
-
324
- if (parent) {
325
- this.globalTransform.multiply(this.localTransform);
326
- }
327
-
328
- this.setUpdateType(UpdateType.Clipping | UpdateType.Children);
329
- childUpdateType |= UpdateType.Global;
330
- }
331
-
332
- if (this.updateType & UpdateType.Clipping) {
333
- this.calculateClippingRect(parentClippingRect);
334
- this.checkIsRenderable();
335
- this.setUpdateType(UpdateType.Children);
336
- childUpdateType |= UpdateType.Clipping;
337
- }
338
-
339
- if (this.updateType & UpdateType.WorldAlpha) {
340
- if (parent) {
341
- this.worldAlpha = parent.worldAlpha * this.props.alpha;
342
- } else {
343
- this.worldAlpha = this.props.alpha;
344
- }
345
- this.setUpdateType(UpdateType.Children | UpdateType.PremultipliedColors);
346
- childUpdateType |= UpdateType.WorldAlpha;
347
- }
348
-
349
- if (this.updateType & UpdateType.PremultipliedColors) {
350
- this.premultipliedColorTl = mergeColorAlphaPremultiplied(
351
- this.props.colorTl,
352
- this.worldAlpha,
353
- true,
354
- );
355
-
356
- // If all the colors are the same just sent them all to the same value
357
- if (
358
- this.props.colorTl === this.props.colorTr &&
359
- this.props.colorBl === this.props.colorBr &&
360
- this.props.colorTl === this.props.colorBl
361
- ) {
362
- this.premultipliedColorTr =
363
- this.premultipliedColorBl =
364
- this.premultipliedColorBr =
365
- this.premultipliedColorTl;
366
- } else {
367
- this.premultipliedColorTr = mergeColorAlphaPremultiplied(
368
- this.props.colorTr,
369
- this.worldAlpha,
370
- true,
371
- );
372
- this.premultipliedColorBl = mergeColorAlphaPremultiplied(
373
- this.props.colorBl,
374
- this.worldAlpha,
375
- true,
376
- );
377
- this.premultipliedColorBr = mergeColorAlphaPremultiplied(
378
- this.props.colorBr,
379
- this.worldAlpha,
380
- true,
381
- );
382
- }
383
- this.checkIsRenderable();
384
- this.setUpdateType(UpdateType.Children);
385
- childUpdateType |= UpdateType.PremultipliedColors;
386
- }
387
-
388
- // No need to update zIndex if there is no parent
389
- if (parent && this.updateType & UpdateType.CalculatedZIndex) {
390
- this.calculateZIndex();
391
- // Tell parent to re-sort children
392
- parent.setUpdateType(UpdateType.ZIndexSortedChildren);
393
- }
394
-
395
- if (this.updateType & UpdateType.Children && this.children.length) {
396
- this.children.forEach((child) => {
397
- // Trigger the depenedent update types on the child
398
- child.setUpdateType(childUpdateType);
399
- // If child has no updates, skip
400
- if (child.updateType === 0) {
401
- return;
402
- }
403
- child.update(delta, this.clippingRect);
404
- });
405
- }
406
-
407
- // Sorting children MUST happen after children have been updated so
408
- // that they have the oppotunity to update their calculated zIndex.
409
- if (this.updateType & UpdateType.ZIndexSortedChildren) {
410
- // reorder z-index
411
- this.sortChildren();
412
- }
413
-
414
- // reset update type
415
- this.updateType = 0;
416
- }
417
-
418
- // This function checks if the current node is renderable based on certain properties.
419
- // It returns true if any of the specified properties are truthy or if any color property is not 0, otherwise it returns false.
420
- checkIsRenderable(): boolean {
421
- if (this.props.texture) {
422
- return (this.isRenderable = true);
423
- }
424
-
425
- if (!this.props.width || !this.props.height) {
426
- return (this.isRenderable = false);
427
- }
428
-
429
- if (this.props.shader) {
430
- return (this.isRenderable = true);
431
- }
432
-
433
- if (this.props.clipping) {
434
- return (this.isRenderable = true);
435
- }
436
-
437
- if (this.props.color !== 0) {
438
- return (this.isRenderable = true);
439
- }
440
-
441
- // Consider removing these checks and just using the color property check above.
442
- // Maybe add a forceRender prop for nodes that should always render.
443
- if (this.props.colorTop !== 0) {
444
- return (this.isRenderable = true);
445
- }
446
-
447
- if (this.props.colorBottom !== 0) {
448
- return (this.isRenderable = true);
449
- }
450
-
451
- if (this.props.colorLeft !== 0) {
452
- return (this.isRenderable = true);
453
- }
454
-
455
- if (this.props.colorRight !== 0) {
456
- return (this.isRenderable = true);
457
- }
458
-
459
- if (this.props.colorTl !== 0) {
460
- return (this.isRenderable = true);
461
- }
462
-
463
- if (this.props.colorTr !== 0) {
464
- return (this.isRenderable = true);
465
- }
466
-
467
- if (this.props.colorBl !== 0) {
468
- return (this.isRenderable = true);
469
- }
470
-
471
- if (this.props.colorBr !== 0) {
472
- return (this.isRenderable = true);
473
- }
474
-
475
- return (this.isRenderable = false);
476
- }
477
-
478
- /**
479
- * This function calculates the clipping rectangle for a node.
480
- *
481
- * 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.
482
- * 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.
483
- *
484
- * Finally, the node's parentClippingRect and clippingRect properties are updated.
485
- */
486
- calculateClippingRect(parentClippingRect: RectWithValid) {
487
- assertTruthy(this.globalTransform);
488
- const { clippingRect, props, globalTransform: gt } = this;
489
- const { clipping } = props;
490
-
491
- const isRotated = gt.tb !== 0 || gt.tc !== 0;
492
-
493
- if (clipping && !isRotated) {
494
- clippingRect.x = gt.tx;
495
- clippingRect.y = gt.ty;
496
- clippingRect.width = this.width * gt.ta;
497
- clippingRect.height = this.height * gt.td;
498
- clippingRect.valid = true;
499
- } else {
500
- clippingRect.valid = false;
501
- }
502
-
503
- if (parentClippingRect.valid && clippingRect.valid) {
504
- // Intersect parent clipping rect with node clipping rect
505
- intersectRect(parentClippingRect, clippingRect, clippingRect);
506
- } else if (parentClippingRect.valid) {
507
- // Copy parent clipping rect
508
- copyRect(parentClippingRect, clippingRect);
509
- clippingRect.valid = true;
510
- }
511
- }
512
-
513
- calculateZIndex(): void {
514
- const props = this.props;
515
- const z = props.zIndex || 0;
516
- const p = props.parent?.zIndex || 0;
517
-
518
- let zIndex = z;
519
- if (props.parent?.zIndexLocked) {
520
- zIndex = z < p ? z : p;
521
- }
522
- this.calcZIndex = zIndex;
523
- }
524
-
525
- renderQuads(renderer: CoreRenderer): void {
526
- const { width, height, texture, textureOptions, shader, shaderProps } =
527
- this.props;
528
- const {
529
- premultipliedColorTl,
530
- premultipliedColorTr,
531
- premultipliedColorBl,
532
- premultipliedColorBr,
533
- } = this;
534
-
535
- const { zIndex, worldAlpha, globalTransform: gt, clippingRect } = this;
536
-
537
- assertTruthy(gt);
538
-
539
- // add to list of renderables to be sorted before rendering
540
- renderer.addQuad({
541
- width,
542
- height,
543
- colorTl: premultipliedColorTl,
544
- colorTr: premultipliedColorTr,
545
- colorBl: premultipliedColorBl,
546
- colorBr: premultipliedColorBr,
547
- texture,
548
- textureOptions,
549
- zIndex,
550
- shader,
551
- shaderProps,
552
- alpha: worldAlpha,
553
- clippingRect,
554
- tx: gt.tx,
555
- ty: gt.ty,
556
- ta: gt.ta,
557
- tb: gt.tb,
558
- tc: gt.tc,
559
- td: gt.td,
560
- });
561
-
562
- // Calculate absolute X and Y based on all ancestors
563
- // renderer.addQuad(absX, absY, w, h, color, texture, textureOptions, zIndex);
564
- }
565
-
566
- //#region Properties
567
- get id(): number {
568
- return this.props.id;
569
- }
570
-
571
- get x(): number {
572
- return this.props.x;
573
- }
574
-
575
- set x(value: number) {
576
- if (this.props.x !== value) {
577
- this.props.x = value;
578
- this.setUpdateType(UpdateType.Local);
579
- }
580
- }
581
-
582
- get absX(): number {
583
- return (
584
- this.props.x +
585
- (this.props.parent?.absX || this.props.parent?.globalTransform?.tx || 0)
586
- );
587
- }
588
-
589
- get absY(): number {
590
- return this.props.y + (this.props.parent?.absY ?? 0);
591
- }
592
-
593
- get y(): number {
594
- return this.props.y;
595
- }
596
-
597
- set y(value: number) {
598
- if (this.props.y !== value) {
599
- this.props.y = value;
600
- this.setUpdateType(UpdateType.Local);
601
- }
602
- }
603
-
604
- get width(): number {
605
- return this.props.width;
606
- }
607
-
608
- set width(value: number) {
609
- if (this.props.width !== value) {
610
- this.props.width = value;
611
- this.setUpdateType(UpdateType.Local);
612
- }
613
- }
614
-
615
- get height(): number {
616
- return this.props.height;
617
- }
618
-
619
- set height(value: number) {
620
- if (this.props.height !== value) {
621
- this.props.height = value;
622
- this.setUpdateType(UpdateType.Local);
623
- }
624
- }
625
-
626
- get scale(): number {
627
- // The CoreNode `scale` property is only used by Animations.
628
- // Unlike INode, `null` should never be possibility for Animations.
629
- return this.scaleX;
630
- }
631
-
632
- set scale(value: number) {
633
- // The CoreNode `scale` property is only used by Animations.
634
- // Unlike INode, `null` should never be possibility for Animations.
635
- this.scaleX = value;
636
- this.scaleY = value;
637
- }
638
-
639
- get scaleX(): number {
640
- return this.props.scaleX;
641
- }
642
-
643
- set scaleX(value: number) {
644
- if (this.props.scaleX !== value) {
645
- this.props.scaleX = value;
646
- this.setUpdateType(UpdateType.ScaleRotate);
647
- }
648
- }
649
-
650
- get scaleY(): number {
651
- return this.props.scaleY;
652
- }
653
-
654
- set scaleY(value: number) {
655
- if (this.props.scaleY !== value) {
656
- this.props.scaleY = value;
657
- this.setUpdateType(UpdateType.ScaleRotate);
658
- }
659
- }
660
-
661
- get mount(): number {
662
- return this.props.mount;
663
- }
664
-
665
- set mount(value: number) {
666
- if (this.props.mountX !== value || this.props.mountY !== value) {
667
- this.props.mountX = value;
668
- this.props.mountY = value;
669
- this.props.mount = value;
670
- this.setUpdateType(UpdateType.Local);
671
- }
672
- }
673
-
674
- get mountX(): number {
675
- return this.props.mountX;
676
- }
677
-
678
- set mountX(value: number) {
679
- if (this.props.mountX !== value) {
680
- this.props.mountX = value;
681
- this.setUpdateType(UpdateType.Local);
682
- }
683
- }
684
-
685
- get mountY(): number {
686
- return this.props.mountY;
687
- }
688
-
689
- set mountY(value: number) {
690
- if (this.props.mountY !== value) {
691
- this.props.mountY = value;
692
- this.setUpdateType(UpdateType.Local);
693
- }
694
- }
695
-
696
- get pivot(): number {
697
- return this.props.pivot;
698
- }
699
-
700
- set pivot(value: number) {
701
- if (this.props.pivotX !== value || this.props.pivotY !== value) {
702
- this.props.pivotX = value;
703
- this.props.pivotY = value;
704
- this.props.pivot = value;
705
- this.setUpdateType(UpdateType.Local);
706
- }
707
- }
708
-
709
- get pivotX(): number {
710
- return this.props.pivotX;
711
- }
712
-
713
- set pivotX(value: number) {
714
- if (this.props.pivotX !== value) {
715
- this.props.pivotX = value;
716
- this.setUpdateType(UpdateType.Local);
717
- }
718
- }
719
-
720
- get pivotY(): number {
721
- return this.props.pivotY;
722
- }
723
-
724
- set pivotY(value: number) {
725
- if (this.props.pivotY !== value) {
726
- this.props.pivotY = value;
727
- this.setUpdateType(UpdateType.Local);
728
- }
729
- }
730
-
731
- get rotation(): number {
732
- return this.props.rotation;
733
- }
734
-
735
- set rotation(value: number) {
736
- if (this.props.rotation !== value) {
737
- this.props.rotation = value;
738
- this.setUpdateType(UpdateType.ScaleRotate);
739
- }
740
- }
741
-
742
- get alpha(): number {
743
- return this.props.alpha;
744
- }
745
-
746
- set alpha(value: number) {
747
- this.props.alpha = value;
748
- this.setUpdateType(UpdateType.PremultipliedColors | UpdateType.WorldAlpha);
749
- }
750
-
751
- get clipping(): boolean {
752
- return this.props.clipping;
753
- }
754
-
755
- set clipping(value: boolean) {
756
- this.props.clipping = value;
757
- this.setUpdateType(UpdateType.Clipping);
758
- }
759
-
760
- get color(): number {
761
- return this.props.color;
762
- }
763
-
764
- set color(value: number) {
765
- if (
766
- this.props.colorTl !== value ||
767
- this.props.colorTr !== value ||
768
- this.props.colorBl !== value ||
769
- this.props.colorBr !== value
770
- ) {
771
- this.colorTl = value;
772
- this.colorTr = value;
773
- this.colorBl = value;
774
- this.colorBr = value;
775
- }
776
- this.props.color = value;
777
-
778
- this.setUpdateType(UpdateType.PremultipliedColors);
779
- }
780
-
781
- get colorTop(): number {
782
- return this.props.colorTop;
783
- }
784
-
785
- set colorTop(value: number) {
786
- if (this.props.colorTl !== value || this.props.colorTr !== value) {
787
- this.colorTl = value;
788
- this.colorTr = value;
789
- }
790
- this.props.colorTop = value;
791
- this.setUpdateType(UpdateType.PremultipliedColors);
792
- }
793
-
794
- get colorBottom(): number {
795
- return this.props.colorBottom;
796
- }
797
-
798
- set colorBottom(value: number) {
799
- if (this.props.colorBl !== value || this.props.colorBr !== value) {
800
- this.colorBl = value;
801
- this.colorBr = value;
802
- }
803
- this.props.colorBottom = value;
804
- this.setUpdateType(UpdateType.PremultipliedColors);
805
- }
806
-
807
- get colorLeft(): number {
808
- return this.props.colorLeft;
809
- }
810
-
811
- set colorLeft(value: number) {
812
- if (this.props.colorTl !== value || this.props.colorBl !== value) {
813
- this.colorTl = value;
814
- this.colorBl = value;
815
- }
816
- this.props.colorLeft = value;
817
- this.setUpdateType(UpdateType.PremultipliedColors);
818
- }
819
-
820
- get colorRight(): number {
821
- return this.props.colorRight;
822
- }
823
-
824
- set colorRight(value: number) {
825
- if (this.props.colorTr !== value || this.props.colorBr !== value) {
826
- this.colorTr = value;
827
- this.colorBr = value;
828
- }
829
- this.props.colorRight = value;
830
- this.setUpdateType(UpdateType.PremultipliedColors);
831
- }
832
-
833
- get colorTl(): number {
834
- return this.props.colorTl;
835
- }
836
-
837
- set colorTl(value: number) {
838
- this.props.colorTl = value;
839
- this.setUpdateType(UpdateType.PremultipliedColors);
840
- }
841
-
842
- get colorTr(): number {
843
- return this.props.colorTr;
844
- }
845
-
846
- set colorTr(value: number) {
847
- this.props.colorTr = value;
848
- this.setUpdateType(UpdateType.PremultipliedColors);
849
- }
850
-
851
- get colorBl(): number {
852
- return this.props.colorBl;
853
- }
854
-
855
- set colorBl(value: number) {
856
- this.props.colorBl = value;
857
- this.setUpdateType(UpdateType.PremultipliedColors);
858
- }
859
-
860
- get colorBr(): number {
861
- return this.props.colorBr;
862
- }
863
-
864
- set colorBr(value: number) {
865
- this.props.colorBr = value;
866
- this.setUpdateType(UpdateType.PremultipliedColors);
867
- }
868
-
869
- // we're only interested in parent zIndex to test
870
- // if we should use node zIndex is higher then parent zIndex
871
- get zIndexLocked(): number {
872
- return this.props.zIndexLocked || 0;
873
- }
874
-
875
- set zIndexLocked(value: number) {
876
- this.props.zIndexLocked = value;
877
- this.setUpdateType(UpdateType.CalculatedZIndex | UpdateType.Children);
878
- this.children.forEach((child) => {
879
- child.setUpdateType(UpdateType.CalculatedZIndex);
880
- });
881
- }
882
-
883
- get zIndex(): number {
884
- return this.props.zIndex;
885
- }
886
-
887
- set zIndex(value: number) {
888
- this.props.zIndex = value;
889
- this.setUpdateType(UpdateType.CalculatedZIndex | UpdateType.Children);
890
- this.children.forEach((child) => {
891
- child.setUpdateType(UpdateType.CalculatedZIndex);
892
- });
893
- }
894
-
895
- get parent(): CoreNode | null {
896
- return this.props.parent;
897
- }
898
-
899
- set parent(newParent: CoreNode | null) {
900
- const oldParent = this.props.parent;
901
- if (oldParent === newParent) {
902
- return;
903
- }
904
- this.props.parent = newParent;
905
- if (oldParent) {
906
- const index = oldParent.children.indexOf(this);
907
- assertTruthy(
908
- index !== -1,
909
- "CoreNode.parent: Node not found in old parent's children!",
910
- );
911
- oldParent.children.splice(index, 1);
912
- }
913
- if (newParent) {
914
- newParent.children.push(this);
915
- // Since this node has a new parent, to be safe, have it do a full update.
916
- this.setUpdateType(UpdateType.All);
917
- // Tell parent that it's children need to be updated and sorted.
918
- newParent.setUpdateType(
919
- UpdateType.Children | UpdateType.ZIndexSortedChildren,
920
- );
921
- }
922
-
923
- this.updateScaleRotateTransform();
924
- }
925
- //#endregion Properties
926
- }
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 { assertTruthy, mergeColorAlphaPremultiplied } from '../utils.js';
21
+ import type { ShaderMap } from './CoreShaderManager.js';
22
+ import type {
23
+ ExtractProps,
24
+ TextureMap,
25
+ TextureOptions,
26
+ } from './CoreTextureManager.js';
27
+ import type { CoreRenderer } from './renderers/CoreRenderer.js';
28
+ import type { CoreShader } from './renderers/CoreShader.js';
29
+ import type { Stage } from './Stage.js';
30
+ import type {
31
+ Texture,
32
+ TextureFailedEventHandler,
33
+ TextureLoadedEventHandler,
34
+ } from './textures/Texture.js';
35
+ import type {
36
+ NodeTextureFailedPayload,
37
+ NodeTextureLoadedPayload,
38
+ } from '../common/CommonTypes.js';
39
+ import { EventEmitter } from '../common/EventEmitter.js';
40
+ import { copyRect, intersectRect, type RectWithValid } from './lib/utils.js';
41
+ import { Matrix3d } from './lib/Matrix3d.js';
42
+
43
+ export interface CoreNodeProps {
44
+ id: number;
45
+ // External facing properties whose defaults are determined by
46
+ // RendererMain's resolveNodeDefaults() method
47
+ x: number;
48
+ y: number;
49
+ width: number;
50
+ height: number;
51
+ alpha: number;
52
+ clipping: boolean;
53
+ color: number;
54
+ colorTop: number;
55
+ colorBottom: number;
56
+ colorLeft: number;
57
+ colorRight: number;
58
+ colorTl: number;
59
+ colorTr: number;
60
+ colorBl: number;
61
+ colorBr: number;
62
+ parent: CoreNode | null;
63
+ zIndex: number;
64
+ texture: Texture | null;
65
+ textureOptions: TextureOptions | null;
66
+ shader: CoreShader | null;
67
+ shaderProps: Record<string, unknown> | null;
68
+ zIndexLocked: number;
69
+ scaleX: number;
70
+ scaleY: number;
71
+ mount: number;
72
+ mountX: number;
73
+ mountY: number;
74
+ pivot: number;
75
+ pivotX: number;
76
+ pivotY: number;
77
+ rotation: number;
78
+ }
79
+
80
+ type ICoreNode = Omit<
81
+ CoreNodeProps,
82
+ 'texture' | 'textureOptions' | 'shader' | 'shaderProps'
83
+ >;
84
+
85
+ enum UpdateType {
86
+ /**
87
+ * Child updates
88
+ */
89
+ Children = 1,
90
+
91
+ /**
92
+ * Scale/Rotate transform update
93
+ */
94
+ ScaleRotate = 2,
95
+
96
+ /**
97
+ * Translate transform update (x/y/width/height/pivot/mount)
98
+ */
99
+ Local = 4,
100
+
101
+ /**
102
+ * Global transform update
103
+ */
104
+ Global = 8,
105
+
106
+ /**
107
+ * Clipping rect update
108
+ */
109
+ Clipping = 16,
110
+
111
+ /**
112
+ * Calculated ZIndex update
113
+ */
114
+ CalculatedZIndex = 32,
115
+
116
+ /**
117
+ * Z-Index Sorted Children update
118
+ */
119
+ ZIndexSortedChildren = 64,
120
+
121
+ /**
122
+ * Premultiplied Colors
123
+ */
124
+ PremultipliedColors = 128,
125
+
126
+ /**
127
+ * World Alpha
128
+ *
129
+ * @remarks
130
+ * World Alpha = Parent World Alpha * Alpha
131
+ */
132
+ WorldAlpha = 256,
133
+
134
+ /**
135
+ * None
136
+ */
137
+ None = 0,
138
+
139
+ /**
140
+ * All
141
+ */
142
+ All = 511,
143
+ }
144
+
145
+ export class CoreNode extends EventEmitter implements ICoreNode {
146
+ readonly children: CoreNode[] = [];
147
+ protected props: Required<CoreNodeProps>;
148
+
149
+ public updateType = UpdateType.All;
150
+ public globalTransform?: Matrix3d;
151
+ public scaleRotateTransform?: Matrix3d;
152
+ public localTransform?: Matrix3d;
153
+ public clippingRect: RectWithValid = {
154
+ x: 0,
155
+ y: 0,
156
+ width: 0,
157
+ height: 0,
158
+ valid: false,
159
+ };
160
+ public isRenderable = false;
161
+ public worldAlpha = 1;
162
+ public premultipliedColorTl = 0;
163
+ public premultipliedColorTr = 0;
164
+ public premultipliedColorBl = 0;
165
+ public premultipliedColorBr = 0;
166
+ public calcZIndex = 0;
167
+
168
+ private isComplex = false;
169
+
170
+ constructor(protected stage: Stage, props: CoreNodeProps) {
171
+ super();
172
+ this.props = {
173
+ ...props,
174
+ parent: null,
175
+ };
176
+ // Allow for parent to be processed appropriately
177
+ this.parent = props.parent;
178
+ this.updateScaleRotateTransform();
179
+ }
180
+
181
+ //#region Textures
182
+ loadTexture<Type extends keyof TextureMap>(
183
+ textureType: Type,
184
+ props: ExtractProps<TextureMap[Type]>,
185
+ options: TextureOptions | null = null,
186
+ ): void {
187
+ // First unload any existing texture
188
+ if (this.props.texture) {
189
+ this.unloadTexture();
190
+ }
191
+ const { txManager } = this.stage;
192
+ const texture = txManager.loadTexture(textureType, props, options);
193
+
194
+ this.props.texture = texture;
195
+ this.props.textureOptions = options;
196
+ this.checkIsRenderable();
197
+
198
+ // If texture is already loaded / failed, trigger loaded event manually
199
+ // so that users get a consistent event experience.
200
+ // We do this in a microtask to allow listeners to be attached in the same
201
+ // synchronous task after calling loadTexture()
202
+ queueMicrotask(() => {
203
+ if (texture.state === 'loaded') {
204
+ this.onTextureLoaded(texture, texture.dimensions!);
205
+ } else if (texture.state === 'failed') {
206
+ this.onTextureFailed(texture, texture.error!);
207
+ }
208
+ texture.on('loaded', this.onTextureLoaded);
209
+ texture.on('failed', this.onTextureFailed);
210
+ });
211
+ }
212
+
213
+ unloadTexture(): void {
214
+ if (this.props.texture) {
215
+ this.props.texture.off('loaded', this.onTextureLoaded);
216
+ this.props.texture.off('failed', this.onTextureFailed);
217
+ }
218
+ this.props.texture = null;
219
+ this.props.textureOptions = null;
220
+ this.checkIsRenderable();
221
+ }
222
+
223
+ private onTextureLoaded: TextureLoadedEventHandler = (target, dimensions) => {
224
+ // Texture was loaded. In case the RAF loop has already stopped, we request
225
+ // a render to ensure the texture is rendered.
226
+ this.stage.requestRender();
227
+ this.emit('loaded', {
228
+ type: 'texture',
229
+ dimensions,
230
+ } satisfies NodeTextureLoadedPayload);
231
+ };
232
+
233
+ private onTextureFailed: TextureFailedEventHandler = (target, error) => {
234
+ this.emit('failed', {
235
+ type: 'texture',
236
+ error,
237
+ } satisfies NodeTextureFailedPayload);
238
+ };
239
+ //#endregion Textures
240
+
241
+ loadShader<Type extends keyof ShaderMap>(
242
+ shaderType: Type,
243
+ props: ExtractProps<ShaderMap[Type]>,
244
+ ): void {
245
+ const shManager = this.stage.renderer.getShaderManager();
246
+ assertTruthy(shManager);
247
+ const { shader, props: p } = shManager.loadShader(shaderType, props);
248
+ this.props.shader = shader;
249
+ this.props.shaderProps = p;
250
+ this.checkIsRenderable();
251
+ }
252
+
253
+ /**
254
+ * Change types types is used to determine the scope of the changes being applied
255
+ *
256
+ * @remarks
257
+ * See {@link UpdateType} for more information on each type
258
+ *
259
+ * @param type
260
+ */
261
+ setUpdateType(type: UpdateType): void {
262
+ this.updateType |= type;
263
+
264
+ // If we're updating this node at all, we need to inform the parent
265
+ // (and all ancestors) that their children need updating as well
266
+ const parent = this.props.parent;
267
+ if (parent && !(parent.updateType & UpdateType.Children)) {
268
+ parent.setUpdateType(UpdateType.Children);
269
+ }
270
+ }
271
+
272
+ sortChildren() {
273
+ this.children.sort((a, b) => a.calcZIndex - b.calcZIndex);
274
+ }
275
+
276
+ updateScaleRotateTransform() {
277
+ this.scaleRotateTransform = Matrix3d.rotate(
278
+ this.props.rotation,
279
+ this.scaleRotateTransform,
280
+ ).scale(this.props.scaleX, this.props.scaleY);
281
+ }
282
+
283
+ updateLocalTransform() {
284
+ assertTruthy(this.scaleRotateTransform);
285
+ const pivotTranslateX = this.props.pivotX * this.props.width;
286
+ const pivotTranslateY = this.props.pivotY * this.props.height;
287
+ const mountTranslateX = this.props.mountX * this.props.width;
288
+ const mountTranslateY = this.props.mountY * this.props.height;
289
+
290
+ this.localTransform = Matrix3d.translate(
291
+ pivotTranslateX - mountTranslateX + this.props.x,
292
+ pivotTranslateY - mountTranslateY + this.props.y,
293
+ this.localTransform,
294
+ )
295
+ .multiply(this.scaleRotateTransform)
296
+ .translate(-pivotTranslateX, -pivotTranslateY);
297
+ this.setUpdateType(UpdateType.Global);
298
+ }
299
+
300
+ /**
301
+ * @todo: test for correct calculation flag
302
+ * @param delta
303
+ */
304
+ update(delta: number, parentClippingRect: RectWithValid): void {
305
+ if (this.updateType & UpdateType.ScaleRotate) {
306
+ this.updateScaleRotateTransform();
307
+ this.setUpdateType(UpdateType.Local);
308
+ }
309
+
310
+ if (this.updateType & UpdateType.Local) {
311
+ this.updateLocalTransform();
312
+ this.setUpdateType(UpdateType.Global);
313
+ }
314
+
315
+ const parent = this.props.parent;
316
+ let childUpdateType = UpdateType.None;
317
+ if (this.updateType & UpdateType.Global) {
318
+ assertTruthy(this.localTransform);
319
+ this.globalTransform = Matrix3d.copy(
320
+ parent?.globalTransform || this.localTransform,
321
+ this.globalTransform,
322
+ );
323
+
324
+ if (parent) {
325
+ this.globalTransform.multiply(this.localTransform);
326
+ }
327
+
328
+ this.setUpdateType(UpdateType.Clipping | UpdateType.Children);
329
+ childUpdateType |= UpdateType.Global;
330
+ }
331
+
332
+ if (this.updateType & UpdateType.Clipping) {
333
+ this.calculateClippingRect(parentClippingRect);
334
+ this.checkIsRenderable();
335
+ this.setUpdateType(UpdateType.Children);
336
+ childUpdateType |= UpdateType.Clipping;
337
+ }
338
+
339
+ if (this.updateType & UpdateType.WorldAlpha) {
340
+ if (parent) {
341
+ this.worldAlpha = parent.worldAlpha * this.props.alpha;
342
+ } else {
343
+ this.worldAlpha = this.props.alpha;
344
+ }
345
+ this.setUpdateType(UpdateType.Children | UpdateType.PremultipliedColors);
346
+ childUpdateType |= UpdateType.WorldAlpha;
347
+ }
348
+
349
+ if (this.updateType & UpdateType.PremultipliedColors) {
350
+ this.premultipliedColorTl = mergeColorAlphaPremultiplied(
351
+ this.props.colorTl,
352
+ this.worldAlpha,
353
+ true,
354
+ );
355
+
356
+ // If all the colors are the same just sent them all to the same value
357
+ if (
358
+ this.props.colorTl === this.props.colorTr &&
359
+ this.props.colorBl === this.props.colorBr &&
360
+ this.props.colorTl === this.props.colorBl
361
+ ) {
362
+ this.premultipliedColorTr =
363
+ this.premultipliedColorBl =
364
+ this.premultipliedColorBr =
365
+ this.premultipliedColorTl;
366
+ } else {
367
+ this.premultipliedColorTr = mergeColorAlphaPremultiplied(
368
+ this.props.colorTr,
369
+ this.worldAlpha,
370
+ true,
371
+ );
372
+ this.premultipliedColorBl = mergeColorAlphaPremultiplied(
373
+ this.props.colorBl,
374
+ this.worldAlpha,
375
+ true,
376
+ );
377
+ this.premultipliedColorBr = mergeColorAlphaPremultiplied(
378
+ this.props.colorBr,
379
+ this.worldAlpha,
380
+ true,
381
+ );
382
+ }
383
+ this.checkIsRenderable();
384
+ this.setUpdateType(UpdateType.Children);
385
+ childUpdateType |= UpdateType.PremultipliedColors;
386
+ }
387
+
388
+ // No need to update zIndex if there is no parent
389
+ if (parent && this.updateType & UpdateType.CalculatedZIndex) {
390
+ this.calculateZIndex();
391
+ // Tell parent to re-sort children
392
+ parent.setUpdateType(UpdateType.ZIndexSortedChildren);
393
+ }
394
+
395
+ if (this.updateType & UpdateType.Children && this.children.length) {
396
+ this.children.forEach((child) => {
397
+ // Trigger the depenedent update types on the child
398
+ child.setUpdateType(childUpdateType);
399
+ // If child has no updates, skip
400
+ if (child.updateType === 0) {
401
+ return;
402
+ }
403
+ child.update(delta, this.clippingRect);
404
+ });
405
+ }
406
+
407
+ // Sorting children MUST happen after children have been updated so
408
+ // that they have the oppotunity to update their calculated zIndex.
409
+ if (this.updateType & UpdateType.ZIndexSortedChildren) {
410
+ // reorder z-index
411
+ this.sortChildren();
412
+ }
413
+
414
+ // reset update type
415
+ this.updateType = 0;
416
+ }
417
+
418
+ // This function checks if the current node is renderable based on certain properties.
419
+ // It returns true if any of the specified properties are truthy or if any color property is not 0, otherwise it returns false.
420
+ checkIsRenderable(): boolean {
421
+ if (this.props.texture) {
422
+ return (this.isRenderable = true);
423
+ }
424
+
425
+ if (!this.props.width || !this.props.height) {
426
+ return (this.isRenderable = false);
427
+ }
428
+
429
+ if (this.props.shader) {
430
+ return (this.isRenderable = true);
431
+ }
432
+
433
+ if (this.props.clipping) {
434
+ return (this.isRenderable = true);
435
+ }
436
+
437
+ if (this.props.color !== 0) {
438
+ return (this.isRenderable = true);
439
+ }
440
+
441
+ // Consider removing these checks and just using the color property check above.
442
+ // Maybe add a forceRender prop for nodes that should always render.
443
+ if (this.props.colorTop !== 0) {
444
+ return (this.isRenderable = true);
445
+ }
446
+
447
+ if (this.props.colorBottom !== 0) {
448
+ return (this.isRenderable = true);
449
+ }
450
+
451
+ if (this.props.colorLeft !== 0) {
452
+ return (this.isRenderable = true);
453
+ }
454
+
455
+ if (this.props.colorRight !== 0) {
456
+ return (this.isRenderable = true);
457
+ }
458
+
459
+ if (this.props.colorTl !== 0) {
460
+ return (this.isRenderable = true);
461
+ }
462
+
463
+ if (this.props.colorTr !== 0) {
464
+ return (this.isRenderable = true);
465
+ }
466
+
467
+ if (this.props.colorBl !== 0) {
468
+ return (this.isRenderable = true);
469
+ }
470
+
471
+ if (this.props.colorBr !== 0) {
472
+ return (this.isRenderable = true);
473
+ }
474
+
475
+ return (this.isRenderable = false);
476
+ }
477
+
478
+ /**
479
+ * This function calculates the clipping rectangle for a node.
480
+ *
481
+ * 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.
482
+ * 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.
483
+ *
484
+ * Finally, the node's parentClippingRect and clippingRect properties are updated.
485
+ */
486
+ calculateClippingRect(parentClippingRect: RectWithValid) {
487
+ assertTruthy(this.globalTransform);
488
+ const { clippingRect, props, globalTransform: gt } = this;
489
+ const { clipping } = props;
490
+
491
+ const isRotated = gt.tb !== 0 || gt.tc !== 0;
492
+
493
+ if (clipping && !isRotated) {
494
+ clippingRect.x = gt.tx;
495
+ clippingRect.y = gt.ty;
496
+ clippingRect.width = this.width * gt.ta;
497
+ clippingRect.height = this.height * gt.td;
498
+ clippingRect.valid = true;
499
+ } else {
500
+ clippingRect.valid = false;
501
+ }
502
+
503
+ if (parentClippingRect.valid && clippingRect.valid) {
504
+ // Intersect parent clipping rect with node clipping rect
505
+ intersectRect(parentClippingRect, clippingRect, clippingRect);
506
+ } else if (parentClippingRect.valid) {
507
+ // Copy parent clipping rect
508
+ copyRect(parentClippingRect, clippingRect);
509
+ clippingRect.valid = true;
510
+ }
511
+ }
512
+
513
+ calculateZIndex(): void {
514
+ const props = this.props;
515
+ const z = props.zIndex || 0;
516
+ const p = props.parent?.zIndex || 0;
517
+
518
+ let zIndex = z;
519
+ if (props.parent?.zIndexLocked) {
520
+ zIndex = z < p ? z : p;
521
+ }
522
+ this.calcZIndex = zIndex;
523
+ }
524
+
525
+ renderQuads(renderer: CoreRenderer): void {
526
+ const { width, height, texture, textureOptions, shader, shaderProps } =
527
+ this.props;
528
+ const {
529
+ premultipliedColorTl,
530
+ premultipliedColorTr,
531
+ premultipliedColorBl,
532
+ premultipliedColorBr,
533
+ } = this;
534
+
535
+ const { zIndex, worldAlpha, globalTransform: gt, clippingRect } = this;
536
+
537
+ assertTruthy(gt);
538
+
539
+ // add to list of renderables to be sorted before rendering
540
+ renderer.addQuad({
541
+ width,
542
+ height,
543
+ colorTl: premultipliedColorTl,
544
+ colorTr: premultipliedColorTr,
545
+ colorBl: premultipliedColorBl,
546
+ colorBr: premultipliedColorBr,
547
+ texture,
548
+ textureOptions,
549
+ zIndex,
550
+ shader,
551
+ shaderProps,
552
+ alpha: worldAlpha,
553
+ clippingRect,
554
+ tx: gt.tx,
555
+ ty: gt.ty,
556
+ ta: gt.ta,
557
+ tb: gt.tb,
558
+ tc: gt.tc,
559
+ td: gt.td,
560
+ });
561
+
562
+ // Calculate absolute X and Y based on all ancestors
563
+ // renderer.addQuad(absX, absY, w, h, color, texture, textureOptions, zIndex);
564
+ }
565
+
566
+ //#region Properties
567
+ get id(): number {
568
+ return this.props.id;
569
+ }
570
+
571
+ get x(): number {
572
+ return this.props.x;
573
+ }
574
+
575
+ set x(value: number) {
576
+ if (this.props.x !== value) {
577
+ this.props.x = value;
578
+ this.setUpdateType(UpdateType.Local);
579
+ }
580
+ }
581
+
582
+ get absX(): number {
583
+ return (
584
+ this.props.x +
585
+ (this.props.parent?.absX || this.props.parent?.globalTransform?.tx || 0)
586
+ );
587
+ }
588
+
589
+ get absY(): number {
590
+ return this.props.y + (this.props.parent?.absY ?? 0);
591
+ }
592
+
593
+ get y(): number {
594
+ return this.props.y;
595
+ }
596
+
597
+ set y(value: number) {
598
+ if (this.props.y !== value) {
599
+ this.props.y = value;
600
+ this.setUpdateType(UpdateType.Local);
601
+ }
602
+ }
603
+
604
+ get width(): number {
605
+ return this.props.width;
606
+ }
607
+
608
+ set width(value: number) {
609
+ if (this.props.width !== value) {
610
+ this.props.width = value;
611
+ this.setUpdateType(UpdateType.Local);
612
+ }
613
+ }
614
+
615
+ get height(): number {
616
+ return this.props.height;
617
+ }
618
+
619
+ set height(value: number) {
620
+ if (this.props.height !== value) {
621
+ this.props.height = value;
622
+ this.setUpdateType(UpdateType.Local);
623
+ }
624
+ }
625
+
626
+ get scale(): number {
627
+ // The CoreNode `scale` property is only used by Animations.
628
+ // Unlike INode, `null` should never be possibility for Animations.
629
+ return this.scaleX;
630
+ }
631
+
632
+ set scale(value: number) {
633
+ // The CoreNode `scale` property is only used by Animations.
634
+ // Unlike INode, `null` should never be possibility for Animations.
635
+ this.scaleX = value;
636
+ this.scaleY = value;
637
+ }
638
+
639
+ get scaleX(): number {
640
+ return this.props.scaleX;
641
+ }
642
+
643
+ set scaleX(value: number) {
644
+ if (this.props.scaleX !== value) {
645
+ this.props.scaleX = value;
646
+ this.setUpdateType(UpdateType.ScaleRotate);
647
+ }
648
+ }
649
+
650
+ get scaleY(): number {
651
+ return this.props.scaleY;
652
+ }
653
+
654
+ set scaleY(value: number) {
655
+ if (this.props.scaleY !== value) {
656
+ this.props.scaleY = value;
657
+ this.setUpdateType(UpdateType.ScaleRotate);
658
+ }
659
+ }
660
+
661
+ get mount(): number {
662
+ return this.props.mount;
663
+ }
664
+
665
+ set mount(value: number) {
666
+ if (this.props.mountX !== value || this.props.mountY !== value) {
667
+ this.props.mountX = value;
668
+ this.props.mountY = value;
669
+ this.props.mount = value;
670
+ this.setUpdateType(UpdateType.Local);
671
+ }
672
+ }
673
+
674
+ get mountX(): number {
675
+ return this.props.mountX;
676
+ }
677
+
678
+ set mountX(value: number) {
679
+ if (this.props.mountX !== value) {
680
+ this.props.mountX = value;
681
+ this.setUpdateType(UpdateType.Local);
682
+ }
683
+ }
684
+
685
+ get mountY(): number {
686
+ return this.props.mountY;
687
+ }
688
+
689
+ set mountY(value: number) {
690
+ if (this.props.mountY !== value) {
691
+ this.props.mountY = value;
692
+ this.setUpdateType(UpdateType.Local);
693
+ }
694
+ }
695
+
696
+ get pivot(): number {
697
+ return this.props.pivot;
698
+ }
699
+
700
+ set pivot(value: number) {
701
+ if (this.props.pivotX !== value || this.props.pivotY !== value) {
702
+ this.props.pivotX = value;
703
+ this.props.pivotY = value;
704
+ this.props.pivot = value;
705
+ this.setUpdateType(UpdateType.Local);
706
+ }
707
+ }
708
+
709
+ get pivotX(): number {
710
+ return this.props.pivotX;
711
+ }
712
+
713
+ set pivotX(value: number) {
714
+ if (this.props.pivotX !== value) {
715
+ this.props.pivotX = value;
716
+ this.setUpdateType(UpdateType.Local);
717
+ }
718
+ }
719
+
720
+ get pivotY(): number {
721
+ return this.props.pivotY;
722
+ }
723
+
724
+ set pivotY(value: number) {
725
+ if (this.props.pivotY !== value) {
726
+ this.props.pivotY = value;
727
+ this.setUpdateType(UpdateType.Local);
728
+ }
729
+ }
730
+
731
+ get rotation(): number {
732
+ return this.props.rotation;
733
+ }
734
+
735
+ set rotation(value: number) {
736
+ if (this.props.rotation !== value) {
737
+ this.props.rotation = value;
738
+ this.setUpdateType(UpdateType.ScaleRotate);
739
+ }
740
+ }
741
+
742
+ get alpha(): number {
743
+ return this.props.alpha;
744
+ }
745
+
746
+ set alpha(value: number) {
747
+ this.props.alpha = value;
748
+ this.setUpdateType(UpdateType.PremultipliedColors | UpdateType.WorldAlpha);
749
+ }
750
+
751
+ get clipping(): boolean {
752
+ return this.props.clipping;
753
+ }
754
+
755
+ set clipping(value: boolean) {
756
+ this.props.clipping = value;
757
+ this.setUpdateType(UpdateType.Clipping);
758
+ }
759
+
760
+ get color(): number {
761
+ return this.props.color;
762
+ }
763
+
764
+ set color(value: number) {
765
+ if (
766
+ this.props.colorTl !== value ||
767
+ this.props.colorTr !== value ||
768
+ this.props.colorBl !== value ||
769
+ this.props.colorBr !== value
770
+ ) {
771
+ this.colorTl = value;
772
+ this.colorTr = value;
773
+ this.colorBl = value;
774
+ this.colorBr = value;
775
+ }
776
+ this.props.color = value;
777
+
778
+ this.setUpdateType(UpdateType.PremultipliedColors);
779
+ }
780
+
781
+ get colorTop(): number {
782
+ return this.props.colorTop;
783
+ }
784
+
785
+ set colorTop(value: number) {
786
+ if (this.props.colorTl !== value || this.props.colorTr !== value) {
787
+ this.colorTl = value;
788
+ this.colorTr = value;
789
+ }
790
+ this.props.colorTop = value;
791
+ this.setUpdateType(UpdateType.PremultipliedColors);
792
+ }
793
+
794
+ get colorBottom(): number {
795
+ return this.props.colorBottom;
796
+ }
797
+
798
+ set colorBottom(value: number) {
799
+ if (this.props.colorBl !== value || this.props.colorBr !== value) {
800
+ this.colorBl = value;
801
+ this.colorBr = value;
802
+ }
803
+ this.props.colorBottom = value;
804
+ this.setUpdateType(UpdateType.PremultipliedColors);
805
+ }
806
+
807
+ get colorLeft(): number {
808
+ return this.props.colorLeft;
809
+ }
810
+
811
+ set colorLeft(value: number) {
812
+ if (this.props.colorTl !== value || this.props.colorBl !== value) {
813
+ this.colorTl = value;
814
+ this.colorBl = value;
815
+ }
816
+ this.props.colorLeft = value;
817
+ this.setUpdateType(UpdateType.PremultipliedColors);
818
+ }
819
+
820
+ get colorRight(): number {
821
+ return this.props.colorRight;
822
+ }
823
+
824
+ set colorRight(value: number) {
825
+ if (this.props.colorTr !== value || this.props.colorBr !== value) {
826
+ this.colorTr = value;
827
+ this.colorBr = value;
828
+ }
829
+ this.props.colorRight = value;
830
+ this.setUpdateType(UpdateType.PremultipliedColors);
831
+ }
832
+
833
+ get colorTl(): number {
834
+ return this.props.colorTl;
835
+ }
836
+
837
+ set colorTl(value: number) {
838
+ this.props.colorTl = value;
839
+ this.setUpdateType(UpdateType.PremultipliedColors);
840
+ }
841
+
842
+ get colorTr(): number {
843
+ return this.props.colorTr;
844
+ }
845
+
846
+ set colorTr(value: number) {
847
+ this.props.colorTr = value;
848
+ this.setUpdateType(UpdateType.PremultipliedColors);
849
+ }
850
+
851
+ get colorBl(): number {
852
+ return this.props.colorBl;
853
+ }
854
+
855
+ set colorBl(value: number) {
856
+ this.props.colorBl = value;
857
+ this.setUpdateType(UpdateType.PremultipliedColors);
858
+ }
859
+
860
+ get colorBr(): number {
861
+ return this.props.colorBr;
862
+ }
863
+
864
+ set colorBr(value: number) {
865
+ this.props.colorBr = value;
866
+ this.setUpdateType(UpdateType.PremultipliedColors);
867
+ }
868
+
869
+ // we're only interested in parent zIndex to test
870
+ // if we should use node zIndex is higher then parent zIndex
871
+ get zIndexLocked(): number {
872
+ return this.props.zIndexLocked || 0;
873
+ }
874
+
875
+ set zIndexLocked(value: number) {
876
+ this.props.zIndexLocked = value;
877
+ this.setUpdateType(UpdateType.CalculatedZIndex | UpdateType.Children);
878
+ this.children.forEach((child) => {
879
+ child.setUpdateType(UpdateType.CalculatedZIndex);
880
+ });
881
+ }
882
+
883
+ get zIndex(): number {
884
+ return this.props.zIndex;
885
+ }
886
+
887
+ set zIndex(value: number) {
888
+ this.props.zIndex = value;
889
+ this.setUpdateType(UpdateType.CalculatedZIndex | UpdateType.Children);
890
+ this.children.forEach((child) => {
891
+ child.setUpdateType(UpdateType.CalculatedZIndex);
892
+ });
893
+ }
894
+
895
+ get parent(): CoreNode | null {
896
+ return this.props.parent;
897
+ }
898
+
899
+ set parent(newParent: CoreNode | null) {
900
+ const oldParent = this.props.parent;
901
+ if (oldParent === newParent) {
902
+ return;
903
+ }
904
+ this.props.parent = newParent;
905
+ if (oldParent) {
906
+ const index = oldParent.children.indexOf(this);
907
+ assertTruthy(
908
+ index !== -1,
909
+ "CoreNode.parent: Node not found in old parent's children!",
910
+ );
911
+ oldParent.children.splice(index, 1);
912
+ }
913
+ if (newParent) {
914
+ newParent.children.push(this);
915
+ // Since this node has a new parent, to be safe, have it do a full update.
916
+ this.setUpdateType(UpdateType.All);
917
+ // Tell parent that it's children need to be updated and sorted.
918
+ newParent.setUpdateType(
919
+ UpdateType.Children | UpdateType.ZIndexSortedChildren,
920
+ );
921
+ }
922
+
923
+ this.updateScaleRotateTransform();
924
+ }
925
+ //#endregion Properties
926
+ }