@egret/runtime 0.0.0-stage → 7.0.1-rc.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 (155) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +21 -0
  3. package/README.md +70 -2
  4. package/README.zh-CN.md +71 -0
  5. package/dist/AssetLease.d.ts +23 -0
  6. package/dist/AssetLease.js +69 -0
  7. package/dist/AssetManager.d.ts +56 -0
  8. package/dist/AssetManager.js +451 -0
  9. package/dist/AssetRef.d.ts +20 -0
  10. package/dist/AssetRef.js +32 -0
  11. package/dist/Bitmap.d.ts +40 -0
  12. package/dist/Bitmap.js +545 -0
  13. package/dist/ByteReader.d.ts +30 -0
  14. package/dist/ByteReader.js +177 -0
  15. package/dist/ByteWriter.d.ts +29 -0
  16. package/dist/ByteWriter.js +180 -0
  17. package/dist/CancellationController.d.ts +10 -0
  18. package/dist/CancellationController.js +66 -0
  19. package/dist/DisplayObject.d.ts +89 -0
  20. package/dist/DisplayObject.js +340 -0
  21. package/dist/DisplayObjectContainer.d.ts +28 -0
  22. package/dist/DisplayObjectContainer.js +226 -0
  23. package/dist/EgretError.d.ts +11 -0
  24. package/dist/EgretError.js +36 -0
  25. package/dist/Event.d.ts +45 -0
  26. package/dist/Event.js +84 -0
  27. package/dist/EventDispatcher.d.ts +24 -0
  28. package/dist/EventDispatcher.js +178 -0
  29. package/dist/Graphics.d.ts +106 -0
  30. package/dist/Graphics.js +504 -0
  31. package/dist/Mesh2D.d.ts +27 -0
  32. package/dist/Mesh2D.js +200 -0
  33. package/dist/NineSliceBitmap.d.ts +24 -0
  34. package/dist/NineSliceBitmap.js +164 -0
  35. package/dist/NineSliceGeometry2D.d.ts +18 -0
  36. package/dist/NineSliceGeometry2D.js +126 -0
  37. package/dist/Scope.d.ts +18 -0
  38. package/dist/Scope.js +133 -0
  39. package/dist/SequencePlayer.d.ts +12 -0
  40. package/dist/SequencePlayer.js +69 -0
  41. package/dist/Sprite.d.ts +8 -0
  42. package/dist/Sprite.js +12 -0
  43. package/dist/Stage.d.ts +14 -0
  44. package/dist/Stage.js +31 -0
  45. package/dist/Texture.d.ts +29 -0
  46. package/dist/Texture.js +177 -0
  47. package/dist/assetManagerOwnerState.d.ts +36 -0
  48. package/dist/assetManagerOwnerState.js +115 -0
  49. package/dist/assetState.d.ts +44 -0
  50. package/dist/assetState.js +2 -0
  51. package/dist/atlasFrameLayout.d.ts +21 -0
  52. package/dist/atlasFrameLayout.js +48 -0
  53. package/dist/bitmapDisplayState.d.ts +24 -0
  54. package/dist/bitmapDisplayState.js +22 -0
  55. package/dist/coordinateInverse.d.ts +4 -0
  56. package/dist/coordinateInverse.js +107 -0
  57. package/dist/diagnostics.d.ts +4 -0
  58. package/dist/diagnostics.js +13 -0
  59. package/dist/displayBounds.d.ts +9 -0
  60. package/dist/displayBounds.js +181 -0
  61. package/dist/displayCoordinates.d.ts +8 -0
  62. package/dist/displayCoordinates.js +81 -0
  63. package/dist/displayHitState.d.ts +18 -0
  64. package/dist/displayHitState.js +97 -0
  65. package/dist/displayHitTest.d.ts +8 -0
  66. package/dist/displayHitTest.js +219 -0
  67. package/dist/displayInputOperations.d.ts +12 -0
  68. package/dist/displayInputOperations.js +71 -0
  69. package/dist/displayTransform.d.ts +5 -0
  70. package/dist/displayTransform.js +38 -0
  71. package/dist/displayTreeState.d.ts +90 -0
  72. package/dist/displayTreeState.js +450 -0
  73. package/dist/displayVisualState.d.ts +74 -0
  74. package/dist/displayVisualState.js +140 -0
  75. package/dist/errorHelp.d.ts +14 -0
  76. package/dist/errorHelp.js +432 -0
  77. package/dist/errorHelpZh.d.ts +5 -0
  78. package/dist/errorHelpZh.js +340 -0
  79. package/dist/graphicsFill.d.ts +37 -0
  80. package/dist/graphicsFill.js +302 -0
  81. package/dist/graphicsShape.d.ts +144 -0
  82. package/dist/graphicsShape.js +755 -0
  83. package/dist/index.d.ts +50 -0
  84. package/dist/index.js +28 -0
  85. package/dist/input/PointerRouter.d.ts +4 -0
  86. package/dist/input/PointerRouter.js +453 -0
  87. package/dist/input/index.d.ts +3 -0
  88. package/dist/input/index.js +3 -0
  89. package/dist/input/types.d.ts +58 -0
  90. package/dist/input/types.js +3 -0
  91. package/dist/meshCaptureSnapshot.d.ts +5 -0
  92. package/dist/meshCaptureSnapshot.js +13 -0
  93. package/dist/meshDisplayState.d.ts +18 -0
  94. package/dist/meshDisplayState.js +21 -0
  95. package/dist/ownership.d.ts +75 -0
  96. package/dist/ownership.js +157 -0
  97. package/dist/paragraph/Paragraph.d.ts +13 -0
  98. package/dist/paragraph/Paragraph.js +193 -0
  99. package/dist/paragraph/ParagraphCache.d.ts +5 -0
  100. package/dist/paragraph/ParagraphCache.js +354 -0
  101. package/dist/paragraph/geometry.d.ts +4 -0
  102. package/dist/paragraph/geometry.js +180 -0
  103. package/dist/paragraph/index.d.ts +3 -0
  104. package/dist/paragraph/index.js +3 -0
  105. package/dist/paragraph/request.d.ts +6 -0
  106. package/dist/paragraph/request.js +191 -0
  107. package/dist/paragraph/types.d.ts +183 -0
  108. package/dist/paragraph/types.js +2 -0
  109. package/dist/preparedDisplaySubtree.d.ts +7 -0
  110. package/dist/preparedDisplaySubtree.js +205 -0
  111. package/dist/renderEffect.d.ts +47 -0
  112. package/dist/renderEffect.js +87 -0
  113. package/dist/sequenceClip.d.ts +42 -0
  114. package/dist/sequenceClip.js +196 -0
  115. package/dist/sharedFrameCapture.d.ts +24 -0
  116. package/dist/sharedFrameCapture.js +1069 -0
  117. package/dist/spriteSheet.d.ts +34 -0
  118. package/dist/spriteSheet.js +321 -0
  119. package/dist/srgbBytes.d.ts +3 -0
  120. package/dist/srgbBytes.js +64 -0
  121. package/dist/text/TextField.d.ts +13 -0
  122. package/dist/text/TextField.js +261 -0
  123. package/dist/text/TextRasterCache.d.ts +5 -0
  124. package/dist/text/TextRasterCache.js +1191 -0
  125. package/dist/text/budget.d.ts +51 -0
  126. package/dist/text/budget.js +116 -0
  127. package/dist/text/index.d.ts +4 -0
  128. package/dist/text/index.js +3 -0
  129. package/dist/text/plan.d.ts +16 -0
  130. package/dist/text/plan.js +238 -0
  131. package/dist/text/request.d.ts +44 -0
  132. package/dist/text/request.js +303 -0
  133. package/dist/text/types.d.ts +179 -0
  134. package/dist/text/types.js +2 -0
  135. package/dist/textDisplayState.d.ts +21 -0
  136. package/dist/textDisplayState.js +27 -0
  137. package/dist/ui/displayCommit.d.ts +43 -0
  138. package/dist/ui/displayCommit.js +376 -0
  139. package/dist/ui/editing.d.ts +3 -0
  140. package/dist/ui/editing.js +3 -0
  141. package/dist/ui/heightIndex.d.ts +35 -0
  142. package/dist/ui/heightIndex.js +295 -0
  143. package/dist/ui/index.d.ts +6 -0
  144. package/dist/ui/index.js +6 -0
  145. package/dist/ui/layout.d.ts +9 -0
  146. package/dist/ui/layout.js +349 -0
  147. package/dist/ui/managedState.d.ts +12 -0
  148. package/dist/ui/managedState.js +44 -0
  149. package/dist/ui/textEditState.d.ts +4 -0
  150. package/dist/ui/textEditState.js +206 -0
  151. package/dist/ui/textEditTypes.d.ts +48 -0
  152. package/dist/ui/textEditTypes.js +2 -0
  153. package/dist/ui/types.d.ts +99 -0
  154. package/dist/ui/types.js +2 -0
  155. package/package.json +44 -4
@@ -0,0 +1,504 @@
1
+ import { EgretError } from "./EgretError.js";
2
+ import { assertVisualMutable, color, alphaOf, finite, copyRect, visualOf, recordVisualChange, VISUAL_CONTENT } from "./displayVisualState.js";
3
+ import { GRAPHICS_SHAPE_LIMITS, addOutline, addPath, arcPoints, bezierPoints, copyDrawing, createShapeState, curveSegments, drawingFits, ellipseOutline, roundRectOutline, shapeFits } from "./graphicsShape.js";
4
+ import { isAssetLease, readAssetLeaseValue } from "./AssetLease.js";
5
+ import { isTexture, readTextureSnapshot } from "./Texture.js";
6
+ const invalid = () => new EgretError("GRAPHICS_VALUE_INVALID");
7
+ const numbers = (...values) => {
8
+ for (const value of values)
9
+ if (!finite(value))
10
+ throw invalid();
11
+ };
12
+ const size = (...values) => {
13
+ for (const value of values)
14
+ if (!finite(value) || value < 0)
15
+ throw invalid();
16
+ };
17
+ /**
18
+ * Drawing state belongs to one node; every shape snapshots its fill and its line. A node that draws only filled
19
+ * rectangles keeps them as rectangles. Any other shape, and any line, makes the node's drawing one list of triangles
20
+ * in drawing order, and a curve is drawn as straight pieces within a quarter unit of it. Edges are a stair of pixels
21
+ * unless `smoothing` is set.
22
+ */
23
+ export class Graphics {
24
+ owner;
25
+ fill;
26
+ line;
27
+ soft = 0;
28
+ constructor(owner) {
29
+ this.owner = owner;
30
+ }
31
+ /**
32
+ * How far, in the node's units, the edge of each shape and line drawn from now on fades to nothing: 0, which it is
33
+ * at first, to 8. One unit smooths an edge at the size it was drawn. A band of triangles is added around the shape,
34
+ * which is that much larger. A fill under a line has no band of its own. Filled rectangles alone stay rectangles,
35
+ * and an image fill has no band.
36
+ */
37
+ get smoothing() {
38
+ return this.soft;
39
+ }
40
+ set smoothing(value) {
41
+ assertVisualMutable(this.owner);
42
+ if (!finite(value) || value < 0 || value > 8)
43
+ throw invalid();
44
+ this.finishPath();
45
+ this.soft = value;
46
+ }
47
+ edged(paint) {
48
+ return paint === undefined || this.soft === 0 ? paint : { ...paint, feather: this.soft };
49
+ }
50
+ beginFill(value, alpha = 1) {
51
+ assertVisualMutable(this.owner);
52
+ const held = alphaOf(alpha);
53
+ if (!color(value) || held === undefined)
54
+ throw invalid();
55
+ this.finishPath();
56
+ this.fill = { color: value, alpha: held };
57
+ return this;
58
+ }
59
+ /** The line of the shapes drawn after it: its width in the node's units, colour and alpha. No width, or 0, ends the line. */
60
+ lineStyle(thickness, value = 0, alpha = 1, options = {}) {
61
+ assertVisualMutable(this.owner);
62
+ if (thickness !== undefined &&
63
+ (!finite(thickness) || thickness < 0 || thickness > GRAPHICS_SHAPE_LIMITS.maxLineWidth))
64
+ throw invalid();
65
+ const held = alphaOf(alpha);
66
+ if (!color(value) || held === undefined || typeof options !== "object" || options === null)
67
+ throw invalid();
68
+ const caps = options.caps ?? "none", joints = options.joints ?? "miter", miterLimit = options.miterLimit ?? 4, given = options.dash;
69
+ if ((caps !== "none" && caps !== "round" && caps !== "square") ||
70
+ (joints !== "miter" && joints !== "round" && joints !== "bevel") ||
71
+ !finite(miterLimit) ||
72
+ miterLimit < 1)
73
+ throw invalid();
74
+ let dash;
75
+ if (given !== undefined) {
76
+ if (!Array.isArray(given) || given.length > 16)
77
+ throw invalid();
78
+ let sum = 0;
79
+ for (const length of given) {
80
+ if (!finite(length) || length < 0)
81
+ throw invalid();
82
+ sum += length;
83
+ }
84
+ // No lengths, or lengths of nothing, are no dashes.
85
+ if (sum > 0)
86
+ dash = given.slice();
87
+ }
88
+ this.finishPath();
89
+ this.line =
90
+ thickness === undefined || thickness === 0
91
+ ? undefined
92
+ : { width: thickness, color: value, alpha: held, caps, joints, miterLimit, dash };
93
+ return this;
94
+ }
95
+ /** A fill whose colour runs along the line from one point to another, and stays the end colours beyond them. */
96
+ beginLinearGradientFill(x0, y0, x1, y1, stops) {
97
+ assertVisualMutable(this.owner);
98
+ numbers(x0, y0, x1, y1);
99
+ const dx = x1 - x0, dy = y1 - y0, square = dx * dx + dy * dy;
100
+ if (!(square > 0) || !finite(1 / square))
101
+ throw invalid();
102
+ return this.gradient({
103
+ radial: false,
104
+ a: dx / square,
105
+ c: dy / square,
106
+ tx: -(x0 * dx + y0 * dy) / square,
107
+ b: 0,
108
+ d: 0,
109
+ ty: 0,
110
+ stops: gradientStops(stops)
111
+ });
112
+ }
113
+ /** A fill whose colour runs from a centre out to a circle, or to an ellipse when `radiusY` is given, and stays the last colour beyond it. */
114
+ beginRadialGradientFill(x, y, radius, stops, radiusY = radius) {
115
+ assertVisualMutable(this.owner);
116
+ numbers(x, y, radius, radiusY);
117
+ if (!(radius > 0) || !(radiusY > 0) || !finite(1 / radius) || !finite(1 / radiusY))
118
+ throw invalid();
119
+ return this.gradient({
120
+ radial: true,
121
+ a: 1 / radius,
122
+ c: 0,
123
+ tx: -x / radius,
124
+ b: 0,
125
+ d: 1 / radiusY,
126
+ ty: -y / radiusY,
127
+ stops: gradientStops(stops)
128
+ });
129
+ }
130
+ /**
131
+ * A gradient fill as older code gives it: "linear" or "radial", colours, alphas, ratios from 0 to 255, and a matrix
132
+ * that places the gradient's square of 1638.4 units, centred on the origin, in the node. Without a matrix that
133
+ * square is where it is.
134
+ */
135
+ beginGradientFill(type, colors, alphas, ratios, matrix) {
136
+ assertVisualMutable(this.owner);
137
+ if ((type !== "linear" && type !== "radial") ||
138
+ !Array.isArray(colors) ||
139
+ !Array.isArray(alphas) ||
140
+ !Array.isArray(ratios) ||
141
+ colors.length !== alphas.length ||
142
+ colors.length !== ratios.length)
143
+ throw invalid();
144
+ const m = matrix ?? { a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0 };
145
+ if (typeof m !== "object" || m === null)
146
+ throw invalid();
147
+ numbers(m.a, m.b, m.c, m.d, m.tx, m.ty);
148
+ for (const ratio of ratios)
149
+ if (!finite(ratio) || ratio < 0 || ratio > 255)
150
+ throw invalid();
151
+ const det = m.a * m.d - m.b * m.c, stops = gradientStops(colors.map((value, i) => ({ offset: ratios[i] / 255, color: value, alpha: alphas[i] })));
152
+ if (det === 0 || !finite(1 / det))
153
+ throw invalid();
154
+ // The node's point in the square's units, then along the square for a linear gradient or out from its centre for a radial one.
155
+ const ia = m.d / det, ic = -m.c / det, itx = (m.c * m.ty - m.d * m.tx) / det, ib = -m.b / det, id = m.a / det, ity = (m.b * m.tx - m.a * m.ty) / det;
156
+ if (type === "linear")
157
+ return this.gradient({
158
+ radial: false,
159
+ a: ia / 1638.4,
160
+ c: ic / 1638.4,
161
+ tx: itx / 1638.4 + 0.5,
162
+ b: 0,
163
+ d: 0,
164
+ ty: 0,
165
+ stops
166
+ });
167
+ return this.gradient({
168
+ radial: true,
169
+ a: ia / 819.2,
170
+ c: ic / 819.2,
171
+ tx: itx / 819.2,
172
+ b: ib / 819.2,
173
+ d: id / 819.2,
174
+ ty: ity / 819.2,
175
+ stops
176
+ });
177
+ }
178
+ drawRect(x, y, width, height) {
179
+ assertVisualMutable(this.owner);
180
+ const rect = copyRect({ x, y, width, height }, "GRAPHICS_VALUE_INVALID");
181
+ this.paintRequired();
182
+ const state = visualOf(this.owner);
183
+ // Filled rectangles alone stay rectangles, which a frame draws without a geometry.
184
+ if (state.shape === undefined &&
185
+ this.line === undefined &&
186
+ this.fill.gradient === undefined &&
187
+ this.fill.bitmap === undefined) {
188
+ state.primitives.push({ rect, ...this.fill });
189
+ recordVisualChange(this.owner, VISUAL_CONTENT);
190
+ return this;
191
+ }
192
+ return this.outline([x, y, x + width, y, x + width, y + height, x, y + height], true);
193
+ }
194
+ drawCircle(x, y, radius) {
195
+ assertVisualMutable(this.owner);
196
+ numbers(x, y);
197
+ size(radius);
198
+ this.paintRequired();
199
+ return this.outline(ellipseOutline(x, y, radius, radius), true);
200
+ }
201
+ /** An ellipse inside the rectangle of this corner and size. */
202
+ drawEllipse(x, y, width, height) {
203
+ assertVisualMutable(this.owner);
204
+ numbers(x, y);
205
+ size(width, height);
206
+ this.paintRequired();
207
+ return this.outline(ellipseOutline(x + width / 2, y + height / 2, width / 2, height / 2), true);
208
+ }
209
+ /** A rectangle whose corners are quarter ellipses: `ellipseWidth` and `ellipseHeight` are the whole widths and heights of those ellipses. */
210
+ drawRoundRect(x, y, width, height, ellipseWidth, ellipseHeight = ellipseWidth) {
211
+ assertVisualMutable(this.owner);
212
+ numbers(x, y);
213
+ size(width, height, ellipseWidth, ellipseHeight);
214
+ this.paintRequired();
215
+ return this.outline(roundRectOutline(x, y, width, height, Math.min(ellipseWidth, width) / 2, Math.min(ellipseHeight, height) / 2), true);
216
+ }
217
+ /** Starts a new part of the path at a point. A path is filled and lined with the fill and line it was begun under. */
218
+ moveTo(x, y) {
219
+ assertVisualMutable(this.owner);
220
+ numbers(x, y);
221
+ const mark = this.mark();
222
+ this.path().subpaths.push([x, y]);
223
+ return this.changed(mark);
224
+ }
225
+ lineTo(x, y) {
226
+ assertVisualMutable(this.owner);
227
+ numbers(x, y);
228
+ const mark = this.mark();
229
+ this.current().push(x, y);
230
+ return this.changed(mark);
231
+ }
232
+ /** A curve from where the path is to an anchor, bent towards one control point. */
233
+ curveTo(controlX, controlY, anchorX, anchorY) {
234
+ assertVisualMutable(this.owner);
235
+ numbers(controlX, controlY, anchorX, anchorY);
236
+ const mark = this.mark(), points = this.current(), n = points.length;
237
+ points.push(...bezierPoints([points[n - 2], points[n - 1]], [controlX, controlY], [anchorX, anchorY]));
238
+ return this.changed(mark);
239
+ }
240
+ /** A curve from where the path is to an anchor, bent towards two control points. */
241
+ cubicCurveTo(controlX1, controlY1, controlX2, controlY2, anchorX, anchorY) {
242
+ assertVisualMutable(this.owner);
243
+ numbers(controlX1, controlY1, controlX2, controlY2, anchorX, anchorY);
244
+ const mark = this.mark(), points = this.current(), n = points.length;
245
+ points.push(...bezierPoints([points[n - 2], points[n - 1]], [controlX1, controlY1, controlX2, controlY2], [anchorX, anchorY]));
246
+ return this.changed(mark);
247
+ }
248
+ /** An arc of a circle, in radians from the positive x axis, clockwise on the screen unless `anticlockwise`. A path already begun is joined to its start by a line. */
249
+ drawArc(x, y, radius, startAngle, endAngle, anticlockwise = false) {
250
+ assertVisualMutable(this.owner);
251
+ numbers(x, y, startAngle, endAngle);
252
+ size(radius);
253
+ if (typeof anticlockwise !== "boolean")
254
+ throw invalid();
255
+ const mark = this.mark(), path = this.path(), startX = x + radius * Math.cos(startAngle), startY = y + radius * Math.sin(startAngle);
256
+ if (path.subpaths.length === 0)
257
+ path.subpaths.push([startX, startY]);
258
+ else
259
+ path.subpaths[path.subpaths.length - 1].push(startX, startY);
260
+ path.subpaths[path.subpaths.length - 1].push(...arcPoints(x, y, radius, startAngle, endAngle, anticlockwise));
261
+ return this.changed(mark);
262
+ }
263
+ /** Ends the fill. A path drawn under it is filled as it stands. */
264
+ endFill() {
265
+ assertVisualMutable(this.owner);
266
+ this.finishPath();
267
+ this.fill = undefined;
268
+ return this;
269
+ }
270
+ /** Removes everything drawn and ends the fill and the line. Returns the graphics, so drawing can follow at once. */
271
+ clear() {
272
+ assertVisualMutable(this.owner);
273
+ const state = visualOf(this.owner), changed = state.primitives.length !== 0 || state.shape !== undefined;
274
+ state.primitives = [];
275
+ state.shape = undefined;
276
+ this.fill = undefined;
277
+ this.line = undefined;
278
+ if (changed)
279
+ recordVisualChange(this.owner, VISUAL_CONTENT);
280
+ return this;
281
+ }
282
+ /**
283
+ * A fill of an image. `lease` is the lease of a texture that is a whole image, not a part of an atlas. `matrix`
284
+ * places the image in the node: its pixel (x, y) is drawn at `a·x + c·y + tx`, `b·x + d·y + ty`; without it a pixel
285
+ * is one unit and the image starts at the node's origin. The image repeats unless `repeat` is false, and then its
286
+ * edge pixels go on outwards. Keep the lease for as long as the drawing is shown.
287
+ */
288
+ beginBitmapFill(lease, matrix, repeat = true) {
289
+ assertVisualMutable(this.owner);
290
+ if (!isAssetLease(lease) || typeof repeat !== "boolean")
291
+ throw invalid();
292
+ const texture = readAssetLeaseValue(lease);
293
+ if (!isTexture(texture))
294
+ throw invalid();
295
+ const snapshot = readTextureSnapshot(texture), width = snapshot.image.width, height = snapshot.image.height, rect = snapshot.sourceRect;
296
+ // A part of an atlas cannot repeat by itself: the image around it would show.
297
+ if (snapshot.viewRegion !== null || rect.x !== 0 || rect.y !== 0 || rect.width !== width || rect.height !== height)
298
+ throw invalid();
299
+ const m = matrix ?? { a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0 };
300
+ if (typeof m !== "object" || m === null)
301
+ throw invalid();
302
+ numbers(m.a, m.b, m.c, m.d, m.tx, m.ty);
303
+ const det = m.a * m.d - m.b * m.c;
304
+ if (det === 0 || !finite(1 / det))
305
+ throw invalid();
306
+ // The node's point as a pixel of the image, then as a part of its width and height.
307
+ const bitmap = {
308
+ lease,
309
+ repeat,
310
+ width,
311
+ height,
312
+ a: m.d / det / width,
313
+ c: -m.c / det / width,
314
+ tx: (m.c * m.ty - m.d * m.tx) / det / width,
315
+ b: -m.b / det / height,
316
+ d: m.a / det / height,
317
+ ty: (m.b * m.tx - m.a * m.ty) / det / height
318
+ };
319
+ for (const value of [bitmap.a, bitmap.b, bitmap.c, bitmap.d, bitmap.tx, bitmap.ty])
320
+ if (!finite(value))
321
+ throw invalid();
322
+ this.finishPath();
323
+ this.fill = { color: 0xffffff, alpha: 1, bitmap };
324
+ return this;
325
+ }
326
+ gradient(gradient) {
327
+ for (const value of [gradient.a, gradient.b, gradient.c, gradient.d, gradient.tx, gradient.ty])
328
+ if (!finite(value))
329
+ throw invalid();
330
+ this.finishPath();
331
+ const first = gradient.stops[0];
332
+ this.fill = { color: first.color, alpha: first.alpha, gradient };
333
+ return this;
334
+ }
335
+ paintRequired() {
336
+ if (this.fill === undefined && this.line === undefined)
337
+ throw new EgretError("GRAPHICS_FILL_REQUIRED");
338
+ }
339
+ /** The node's triangles, begun with the rectangles it has drawn so far. */
340
+ shape() {
341
+ const state = visualOf(this.owner);
342
+ if (state.shape === undefined) {
343
+ const shape = createShapeState();
344
+ for (const { rect, color: value, alpha } of state.primitives)
345
+ addOutline(shape, [
346
+ rect.x,
347
+ rect.y,
348
+ rect.x + rect.width,
349
+ rect.y,
350
+ rect.x + rect.width,
351
+ rect.y + rect.height,
352
+ rect.x,
353
+ rect.y + rect.height
354
+ ], true, { color: value, alpha }, undefined);
355
+ state.primitives = [];
356
+ state.shape = shape;
357
+ }
358
+ return state.shape;
359
+ }
360
+ path() {
361
+ const shape = this.shape();
362
+ return (shape.path ??= { fill: this.edged(this.fill), line: this.edged(this.line), subpaths: [] });
363
+ }
364
+ /** The part of the path being drawn; a path that was not begun with moveTo begins at the origin. */
365
+ current() {
366
+ const path = this.path();
367
+ if (path.subpaths.length === 0)
368
+ path.subpaths.push([0, 0]);
369
+ return path.subpaths[path.subpaths.length - 1];
370
+ }
371
+ /** A path becomes triangles when its fill or line ends, or another shape is drawn over it. */
372
+ finishPath() {
373
+ const shape = visualOf(this.owner).shape;
374
+ if (shape === undefined || shape.path === undefined)
375
+ return;
376
+ const path = shape.path;
377
+ shape.path = undefined;
378
+ shape.resolved = undefined;
379
+ const undo = this.mark();
380
+ if (addPath(shape, path) && drawingFits(shape))
381
+ return;
382
+ // A path that became more than the node may hold is dropped whole; what was drawn before it stays.
383
+ undo();
384
+ recordVisualChange(this.owner, VISUAL_CONTENT);
385
+ throw new EgretError("GRAPHICS_BUDGET");
386
+ }
387
+ outline(points, convex) {
388
+ this.finishPath();
389
+ const mark = this.mark();
390
+ if (!addOutline(this.shape(), points, convex, this.edged(this.fill), this.edged(this.line))) {
391
+ mark();
392
+ throw new EgretError("GRAPHICS_BUDGET");
393
+ }
394
+ return this.changed(mark);
395
+ }
396
+ /** What the node's drawing is before a shape or a point is added, so that one that does not fit can be taken back. */
397
+ mark() {
398
+ const state = visualOf(this.owner), shape = state.shape;
399
+ if (shape === undefined) {
400
+ const primitives = state.primitives;
401
+ return () => {
402
+ state.shape = undefined;
403
+ state.primitives = primitives;
404
+ };
405
+ }
406
+ const positions = shape.positions.length, colors = shape.colors.length, uvs = shape.uvs.length, indices = shape.indices.length, done = shape.done.length, bitmap = shape.bitmap;
407
+ const path = shape.path, parts = path?.subpaths.length ?? 0, last = parts === 0 ? 0 : path.subpaths[parts - 1].length;
408
+ return () => {
409
+ // A batch begun since is given up, and the one that was being added to is added to again.
410
+ if (shape.done.length > done) {
411
+ const batch = shape.done[done];
412
+ shape.positions = batch.positions;
413
+ shape.colors = batch.colors;
414
+ shape.uvs = batch.uvs;
415
+ shape.indices = batch.indices;
416
+ shape.done.length = done;
417
+ }
418
+ shape.bitmap = bitmap;
419
+ shape.positions.length = positions;
420
+ shape.colors.length = colors;
421
+ shape.uvs.length = uvs;
422
+ shape.indices.length = indices;
423
+ if (path === undefined)
424
+ shape.path = undefined;
425
+ else {
426
+ path.subpaths.length = parts;
427
+ if (parts !== 0)
428
+ path.subpaths[parts - 1].length = last;
429
+ }
430
+ };
431
+ }
432
+ /** Refuses a drawing one geometry could not hold, leaving the node as it was, then publishes the change. */
433
+ changed(undo) {
434
+ const shape = visualOf(this.owner).shape, path = shape.path;
435
+ let pending = 0, parts = 0, length = 0;
436
+ if (path !== undefined)
437
+ for (const points of path.subpaths) {
438
+ pending += points.length / 2;
439
+ parts++;
440
+ if (path.line?.dash !== undefined)
441
+ for (let i = 2; i < points.length; i += 2)
442
+ length += Math.hypot(points[i] - points[i - 2], points[i + 1] - points[i - 1]);
443
+ }
444
+ if (pending > GRAPHICS_SHAPE_LIMITS.maxPathPoints) {
445
+ undo();
446
+ throw new EgretError("GRAPHICS_BUDGET");
447
+ }
448
+ // The most a path point can give: a vertex and a triangle of its fill, and for its line the two triangles of a
449
+ // piece and its corner. A dash adds two ends. A fill by the non-zero rule, or of a gradient, has no such count
450
+ // before it is made.
451
+ let vertices = shape.positions.length / 2, indices = shape.indices.length;
452
+ if (path !== undefined) {
453
+ const line = path.line, round = line === undefined ? 0 : curveSegments(line.width / 2) / 2 + 2;
454
+ if (path.fill !== undefined) {
455
+ vertices += pending;
456
+ indices += 3 * pending;
457
+ }
458
+ if (line !== undefined) {
459
+ const corner = line.joints === "round" ? round : line.joints === "miter" ? 2 : 1, end = line.caps === "round" ? round : 0;
460
+ let dashes = 0;
461
+ if (line.dash !== undefined) {
462
+ let period = 0;
463
+ for (const part of line.dash)
464
+ period += part;
465
+ dashes = Math.ceil(length / period + 1) * Math.ceil(line.dash.length / 2) + parts;
466
+ }
467
+ const ends = 2 * (parts + dashes);
468
+ vertices += pending * (2 + corner) + ends * (2 + end);
469
+ indices += pending * (6 + 3 * corner) + ends * 3 * end;
470
+ }
471
+ }
472
+ // Past that count the path is made, to see what it really takes.
473
+ let fits = shapeFits(vertices, indices) && drawingFits(shape);
474
+ if (!fits && path !== undefined) {
475
+ const trial = copyDrawing(shape);
476
+ fits = addPath(trial, path) && drawingFits(trial);
477
+ }
478
+ if (!fits) {
479
+ undo();
480
+ throw new EgretError("GRAPHICS_BUDGET");
481
+ }
482
+ shape.resolved = undefined;
483
+ recordVisualChange(this.owner, VISUAL_CONTENT);
484
+ return this;
485
+ }
486
+ }
487
+ /** Two to sixteen colours in order of their places, as a gradient keeps them. */
488
+ function gradientStops(stops) {
489
+ if (!Array.isArray(stops) || stops.length < 2 || stops.length > GRAPHICS_SHAPE_LIMITS.maxGradientStops)
490
+ throw invalid();
491
+ const out = [];
492
+ let before = 0;
493
+ for (const stop of stops) {
494
+ if (typeof stop !== "object" || stop === null)
495
+ throw invalid();
496
+ const offset = stop.offset, alpha = alphaOf(stop.alpha ?? 1);
497
+ if (!finite(offset) || offset < before || offset > 1 || !color(stop.color) || alpha === undefined)
498
+ throw invalid();
499
+ out.push({ offset, color: stop.color, alpha });
500
+ before = offset;
501
+ }
502
+ return out;
503
+ }
504
+ //# sourceMappingURL=Graphics.js.map
@@ -0,0 +1,27 @@
1
+ import type { TexturedGeometry2D, SharedGeometry2D } from "@egret/contracts";
2
+ import type { AssetLease } from "./AssetLease.js";
3
+ import { DisplayObject } from "./DisplayObject.js";
4
+ import type { Texture } from "./Texture.js";
5
+ /** A display leaf borrowing an exact lease and an immutable owned geometry. */
6
+ export declare class Mesh2D<G extends TexturedGeometry2D | SharedGeometry2D = TexturedGeometry2D> extends DisplayObject {
7
+ /** Opaque producer binding: scene traversal exposes neither its borrowed lease nor a content writer. */
8
+ static createBinding<G extends TexturedGeometry2D | SharedGeometry2D>(textureLease: AssetLease<Texture>, geometry: G): {
9
+ readonly node: DisplayObject;
10
+ prepare(lease: AssetLease<Texture>, geometry: G): {
11
+ commit(): void;
12
+ abort(): void;
13
+ };
14
+ beginDispose(): () => void;
15
+ dispose(): void;
16
+ };
17
+ constructor(textureLease: AssetLease<Texture>, geometry: G);
18
+ /** Cached identity-only reads remain valid after clear or terminal disposal. */
19
+ get textureLease(): AssetLease<Texture> | undefined;
20
+ get geometry(): G | undefined;
21
+ setContent(textureLease: AssetLease<Texture>, geometry: G): void;
22
+ /** Clearing drops only cached content; Engine affinity and caller borrows stay. */
23
+ clear(): void;
24
+ /** Drop the slot before base cleanup; terminal callback reentry sees no content. */
25
+ dispose(): void;
26
+ }
27
+ //# sourceMappingURL=Mesh2D.d.ts.map