laymur 0.28.0 → 0.29.0

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 (81) hide show
  1. package/README.md +53 -4
  2. package/dist/core/constraints/UIAspectConstraint/UIAspectConstraint.d.ts +14 -3
  3. package/dist/core/constraints/UIAspectConstraint/UIAspectConstraint.js +38 -5
  4. package/dist/core/constraints/UICustomConstraint.js +1 -1
  5. package/dist/core/constraints/UIHeightConstraint/UIHeightConstraint.js +1 -1
  6. package/dist/core/constraints/UIHorizontalDistanceConstraint/UIHorizontalDistanceConstraint.js +1 -1
  7. package/dist/core/constraints/UIHorizontalInterpolationConstraint/UIHorizontalInterpolationConstraint.js +1 -1
  8. package/dist/core/constraints/UIHorizontalProportionConstraint/UIHorizontalProportionConstraint.js +1 -1
  9. package/dist/core/constraints/UISingleParameterConstraint/UISingleParameterConstraint.d.ts +6 -0
  10. package/dist/core/constraints/UISingleParameterConstraint/UISingleParameterConstraint.js +8 -0
  11. package/dist/core/constraints/UIVerticalDistanceConstraint/UIVerticalDistanceConstraint.js +1 -1
  12. package/dist/core/constraints/UIVerticalInterpolationConstraint/UIVerticalInterpolationConstraint.js +1 -1
  13. package/dist/core/constraints/UIVerticalProportionConstraint/UIVerticalProportionConstraint.js +1 -1
  14. package/dist/core/constraints/UIWidthConstraint/UIWidthConstraint.js +1 -1
  15. package/dist/core/elements/UICanvasElement/UICanvasElement.d.ts +54 -0
  16. package/dist/core/elements/UICanvasElement/UICanvasElement.js +70 -0
  17. package/dist/core/elements/UIDummy/UIDummy.Internal.d.ts +2 -0
  18. package/dist/core/elements/UIDummy/UIDummy.js +3 -1
  19. package/dist/core/elements/UIGraphics/UIGraphics.Internal.d.ts +42 -2
  20. package/dist/core/elements/UIGraphics/UIGraphics.Internal.js +2 -2
  21. package/dist/core/elements/UIGraphics/UIGraphics.d.ts +48 -34
  22. package/dist/core/elements/UIGraphics/UIGraphics.js +196 -93
  23. package/dist/core/elements/UIImage/UIImage.d.ts +11 -1
  24. package/dist/core/elements/UIImage/UIImage.js +14 -0
  25. package/dist/core/elements/UIScene/UIScene.Internal.d.ts +10 -1
  26. package/dist/core/elements/UIScene/UIScene.Internal.js +1 -0
  27. package/dist/core/elements/UIScene/UIScene.d.ts +11 -1
  28. package/dist/core/elements/UIScene/UIScene.js +29 -5
  29. package/dist/core/elements/UIText/UIText.Interfaces.d.ts +2 -2
  30. package/dist/core/elements/UIText/UIText.Internal.d.ts +14 -1
  31. package/dist/core/elements/UIText/UIText.Internal.js +12 -0
  32. package/dist/core/elements/UIText/UIText.Measuring.d.ts +9 -4
  33. package/dist/core/elements/UIText/UIText.Measuring.js +17 -6
  34. package/dist/core/elements/UIText/UIText.Rendering.d.ts +7 -16
  35. package/dist/core/elements/UIText/UIText.Rendering.js +212 -46
  36. package/dist/core/elements/UIText/UIText.d.ts +58 -28
  37. package/dist/core/elements/UIText/UIText.js +113 -138
  38. package/dist/core/index.d.ts +10 -4
  39. package/dist/core/index.js +5 -1
  40. package/dist/core/layers/UILayer/UILayer.d.ts +17 -3
  41. package/dist/core/layers/UILayer/UILayer.js +35 -4
  42. package/dist/core/layers/UIViewportLayer/UIViewportLayer.d.ts +2 -0
  43. package/dist/core/layers/UIViewportLayer/UIViewportLayer.js +3 -0
  44. package/dist/core/miscellaneous/canvas-surface/UICanvasSurface.d.ts +46 -0
  45. package/dist/core/miscellaneous/canvas-surface/UICanvasSurface.js +86 -0
  46. package/dist/core/miscellaneous/color/UIColor.d.ts +17 -0
  47. package/dist/core/miscellaneous/color/UIColor.js +22 -0
  48. package/dist/core/miscellaneous/color/canvasColor.d.ts +9 -0
  49. package/dist/core/miscellaneous/color/canvasColor.js +15 -0
  50. package/dist/core/miscellaneous/insets/UIInsets.d.ts +3 -0
  51. package/dist/core/miscellaneous/insets/UIInsets.js +15 -0
  52. package/dist/core/miscellaneous/paint/UIGradient.Internal.d.ts +37 -0
  53. package/dist/core/miscellaneous/paint/UIGradient.Internal.js +14 -0
  54. package/dist/core/miscellaneous/paint/UIGradient.d.ts +18 -0
  55. package/dist/core/miscellaneous/paint/UIGradient.js +35 -0
  56. package/dist/core/miscellaneous/paint/UILinearGradient.d.ts +14 -0
  57. package/dist/core/miscellaneous/paint/UILinearGradient.js +22 -0
  58. package/dist/core/miscellaneous/paint/UIPaint.Internal.d.ts +21 -0
  59. package/dist/core/miscellaneous/paint/UIPaint.Internal.js +1 -0
  60. package/dist/core/miscellaneous/paint/UIPaint.d.ts +17 -0
  61. package/dist/core/miscellaneous/paint/UIPaint.js +21 -0
  62. package/dist/core/miscellaneous/paint/UIRadialGradient.d.ts +18 -0
  63. package/dist/core/miscellaneous/paint/UIRadialGradient.js +33 -0
  64. package/dist/core/miscellaneous/shared.d.ts +14 -0
  65. package/dist/core/miscellaneous/shared.js +11 -0
  66. package/dist/core/miscellaneous/text-style/UITextStyle.Internal.d.ts +88 -30
  67. package/dist/core/miscellaneous/text-style/UITextStyle.Internal.js +12 -8
  68. package/dist/core/miscellaneous/text-style/UITextStyle.d.ts +41 -66
  69. package/dist/core/miscellaneous/text-style/UITextStyle.js +165 -294
  70. package/dist/core/miscellaneous/texture/UITextureView.Internal.d.ts +2 -1
  71. package/dist/core/miscellaneous/texture/UITextureView.Internal.js +12 -3
  72. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.Internal.d.ts +1 -1
  73. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.d.ts +1 -1
  74. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.js +1 -1
  75. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.Internal.d.ts +6 -2
  76. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.d.ts +41 -9
  77. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.js +97 -35
  78. package/dist/particles/elements/UIEmitter.Internal.d.ts +3 -0
  79. package/dist/particles/elements/UIEmitter.Internal.js +1 -0
  80. package/dist/particles/elements/UIEmitter.js +3 -2
  81. package/package.json +1 -1
@@ -1,123 +1,226 @@
1
- import { createCanvas, createCanvasTexture, getCanvas2DContext, } from "../../miscellaneous/canvas.js";
2
- import { UIColor } from "../../miscellaneous/color/UIColor.js";
3
- import { UIAffineMatrix3 } from "../../miscellaneous/UIAffineMatrix3.js";
4
- import source from "../../shaders/UIImage.js";
5
- import { UIElement } from "../UIElement/UIElement.js";
6
- import { GRAPHICS_DEFAULT_HEIGHT, GRAPHICS_DEFAULT_WIDTH, GRAPHICS_TEMP_COLOR, } from "./UIGraphics.Internal.js";
7
- /** Canvas-based 2D drawing element */
8
- export class UIGraphics extends UIElement {
1
+ import { assertValidNonNegativeNumber, assertValidNumber } from "../../miscellaneous/asserts.js";
2
+ import { resolvePaint, toCanvasPaint } from "../../miscellaneous/paint/UIPaint.js";
3
+ import { UICanvasElement } from "../UICanvasElement/UICanvasElement.js";
4
+ import { GRAPHICS_DEFAULT_HEIGHT, GRAPHICS_DEFAULT_STROKE_CAP, GRAPHICS_DEFAULT_STROKE_JOIN, GRAPHICS_DEFAULT_WIDTH, } from "./UIGraphics.Internal.js";
5
+ /**
6
+ * Bounds of a list of points.
7
+ * @param points Points to bound
8
+ * @returns Box around the points
9
+ */
10
+ function boundPoints(points) {
11
+ let minX = Infinity;
12
+ let minY = Infinity;
13
+ let maxX = -Infinity;
14
+ let maxY = -Infinity;
15
+ for (const point of points) {
16
+ minX = Math.min(minX, point.x);
17
+ minY = Math.min(minY, point.y);
18
+ maxX = Math.max(maxX, point.x);
19
+ maxY = Math.max(maxY, point.y);
20
+ }
21
+ return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
22
+ }
23
+ /**
24
+ * Copies points, so later changes of the given objects do not change the shape.
25
+ * @param points Points to copy
26
+ * @param subject Name of the method, used in errors
27
+ * @returns Copied points
28
+ */
29
+ function copyPoints(points, subject) {
30
+ return points.map((point) => {
31
+ assertValidNumber(point.x, `${subject}.points.x`);
32
+ assertValidNumber(point.y, `${subject}.points.y`);
33
+ return { x: point.x, y: point.y };
34
+ });
35
+ }
36
+ /**
37
+ * Adds lines through points to the current path, from the first point on.
38
+ * @param context Path to add to
39
+ * @param points Points in order, at least one
40
+ */
41
+ function tracePoints(context, points) {
42
+ context.moveTo(points[0].x, points[0].y);
43
+ for (let i = 1; i < points.length; i++) {
44
+ context.lineTo(points[i].x, points[i].y);
45
+ }
46
+ }
47
+ /**
48
+ * 2D vector drawing element. Shapes are drawn in a drawing box as large as the width and height
49
+ * the element is created with, from its top left corner with y pointing down, and the box is
50
+ * stretched over the element. The shapes are kept until `clear`.
51
+ */
52
+ export class UIGraphics extends UICanvasElement {
9
53
  /**
10
54
  * Creates a new UIGraphics instance.
11
55
  *
12
56
  * @param layer - Layer containing this element
13
- * @param options - Configuration options
57
+ * @param options - Configuration options. The width and height also set the drawing box.
14
58
  */
15
59
  constructor(layer, options) {
16
- const w = options?.width ?? GRAPHICS_DEFAULT_WIDTH;
17
- const h = options?.height ?? GRAPHICS_DEFAULT_HEIGHT;
18
- const color = new UIColor(options?.color);
19
- const canvas = createCanvas(w, h);
20
- const context = getCanvas2DContext(canvas);
21
- if (!context) {
22
- throw new Error("UIGraphics.constructor: failed to get 2D context from canvas");
23
- }
24
- const texture = createCanvasTexture(canvas);
25
- texture.needsUpdate = true;
26
- super(layer, source, {
27
- texture: texture,
28
- textureTransform: new UIAffineMatrix3(),
29
- color,
30
- }, options);
31
- this.canvas = canvas;
32
- this.context = context;
33
- this.texture = texture;
34
- this.colorInternal = color;
35
- }
36
- /** Multiplicative tint. Alpha channel controls opacity. */
37
- get color() {
38
- return this.colorInternal;
39
- }
40
- /** Multiplicative tint. Alpha channel controls opacity. */
41
- set color(value) {
42
- this.colorInternal.set(value);
60
+ super(layer, "UIGraphics.constructor", options);
61
+ this.shapes = [];
62
+ this.shapesDirty = true;
63
+ this.contentWidth = options?.width ?? GRAPHICS_DEFAULT_WIDTH;
64
+ this.contentHeight = options?.height ?? GRAPHICS_DEFAULT_HEIGHT;
43
65
  }
44
66
  /**
45
- * Clears canvas. Optionally fills with color.
67
+ * Removes all shapes. Optionally fills the drawing box.
46
68
  *
47
- * @param color - Fill color after clearing
69
+ * @param fill - Color or gradient filling the box after clearing
48
70
  */
49
- clear(color) {
50
- this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
51
- if (color !== undefined) {
52
- this.context.fillStyle = GRAPHICS_TEMP_COLOR.set(color).toCSSColor();
53
- this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
71
+ clear(fill) {
72
+ this.shapes.length = 0;
73
+ this.shapesDirty = true;
74
+ if (fill !== undefined) {
75
+ this.drawRect(0, 0, this.contentWidth, this.contentHeight, { fill });
54
76
  }
55
- this.texture.needsUpdate = true;
56
77
  return this;
57
78
  }
58
- /** Draws filled rectangle */
59
- drawRect(x, y, width, height, color) {
60
- this.context.fillStyle = GRAPHICS_TEMP_COLOR.set(color).toCSSColor();
61
- this.context.fillRect(x, y, width, height);
62
- this.texture.needsUpdate = true;
63
- return this;
79
+ /** Draws a rectangle */
80
+ drawRect(x, y, width, height, style) {
81
+ assertValidNumber(x, "UIGraphics.drawRect.x");
82
+ assertValidNumber(y, "UIGraphics.drawRect.y");
83
+ assertValidNonNegativeNumber(width, "UIGraphics.drawRect.width");
84
+ assertValidNonNegativeNumber(height, "UIGraphics.drawRect.height");
85
+ return this.addShape({ x, y, width, height }, style, (context) => {
86
+ context.rect(x, y, width, height);
87
+ });
64
88
  }
65
- /** Draws filled circle */
66
- drawCircle(x, y, radius, color) {
67
- this.context.fillStyle = GRAPHICS_TEMP_COLOR.set(color).toCSSColor();
68
- this.context.beginPath();
69
- this.context.arc(x, y, radius, 0, Math.PI * 2);
70
- this.context.fill();
71
- this.texture.needsUpdate = true;
72
- return this;
89
+ /**
90
+ * Draws a rectangle with rounded corners. Each radius is clamped to half of the shorter side.
91
+ *
92
+ * @param radius - One radius for all corners, or four from the top left clockwise
93
+ */
94
+ drawRoundedRect(x, y, width, height, radius, style) {
95
+ assertValidNumber(x, "UIGraphics.drawRoundedRect.x");
96
+ assertValidNumber(y, "UIGraphics.drawRoundedRect.y");
97
+ assertValidNonNegativeNumber(width, "UIGraphics.drawRoundedRect.width");
98
+ assertValidNonNegativeNumber(height, "UIGraphics.drawRoundedRect.height");
99
+ const limit = Math.min(width, height) / 2;
100
+ const radii = (typeof radius === "number" ? [radius, radius, radius, radius] : radius).map((value) => {
101
+ assertValidNonNegativeNumber(value, "UIGraphics.drawRoundedRect.radius");
102
+ return Math.min(value, limit);
103
+ });
104
+ const [topLeft, topRight, bottomRight, bottomLeft] = radii;
105
+ const right = x + width;
106
+ const bottom = y + height;
107
+ return this.addShape({ x, y, width, height }, style, (context) => {
108
+ context.moveTo(x + topLeft, y);
109
+ context.arcTo(right, y, right, bottom, topRight);
110
+ context.arcTo(right, bottom, x, bottom, bottomRight);
111
+ context.arcTo(x, bottom, x, y, bottomLeft);
112
+ context.arcTo(x, y, right, y, topLeft);
113
+ context.closePath();
114
+ });
115
+ }
116
+ /** Draws a circle */
117
+ drawCircle(x, y, radius, style) {
118
+ assertValidNumber(x, "UIGraphics.drawCircle.x");
119
+ assertValidNumber(y, "UIGraphics.drawCircle.y");
120
+ assertValidNonNegativeNumber(radius, "UIGraphics.drawCircle.radius");
121
+ const box = { x: x - radius, y: y - radius, width: radius * 2, height: radius * 2 };
122
+ return this.addShape(box, style, (context) => {
123
+ context.arc(x, y, radius, 0, Math.PI * 2);
124
+ });
73
125
  }
74
126
  /**
75
- * Draws filled arc.
127
+ * Draws an arc of a circle. A fill covers the part cut off by the chord between its ends.
128
+ * Gradients are laid over the bounds of the whole circle.
76
129
  *
77
- * @param startAngle - Start angle in radians
78
- * @param endAngle - End angle in radians
130
+ * @param startAngle - Start angle in radians, clockwise from the right
131
+ * @param endAngle - End angle in radians, clockwise from the right
79
132
  */
80
- drawArc(x, y, radius, startAngle, endAngle, color) {
81
- this.context.fillStyle = GRAPHICS_TEMP_COLOR.set(color).toCSSColor();
82
- this.context.beginPath();
83
- this.context.arc(x, y, radius, startAngle, endAngle);
84
- this.context.fill();
85
- this.texture.needsUpdate = true;
86
- return this;
133
+ drawArc(x, y, radius, startAngle, endAngle, style) {
134
+ assertValidNumber(x, "UIGraphics.drawArc.x");
135
+ assertValidNumber(y, "UIGraphics.drawArc.y");
136
+ assertValidNonNegativeNumber(radius, "UIGraphics.drawArc.radius");
137
+ assertValidNumber(startAngle, "UIGraphics.drawArc.startAngle");
138
+ assertValidNumber(endAngle, "UIGraphics.drawArc.endAngle");
139
+ const box = { x: x - radius, y: y - radius, width: radius * 2, height: radius * 2 };
140
+ return this.addShape(box, style, (context) => {
141
+ context.arc(x, y, radius, startAngle, endAngle);
142
+ });
87
143
  }
88
144
  /**
89
- * Draws connected line segments.
145
+ * Draws a closed polygon. Needs at least three points.
90
146
  *
91
- * @param points - Array of [x, y] pairs
92
- * @param lineWidth - Line width in pixels
147
+ * @param points - Corners in order
93
148
  */
94
- drawPolyline(points, color, lineWidth = 1) {
95
- if (points.length < 2) {
149
+ drawPolygon(points, style) {
150
+ if (points.length < 3) {
96
151
  return this;
97
152
  }
98
- this.context.strokeStyle = GRAPHICS_TEMP_COLOR.set(color).toCSSColor();
99
- this.context.lineWidth = lineWidth;
100
- this.context.beginPath();
101
- this.context.moveTo(points[0][0], points[0][1]);
102
- for (let i = 1; i < points.length; i++) {
103
- this.context.lineTo(points[i][0], points[i][1]);
153
+ const corners = copyPoints(points, "UIGraphics.drawPolygon");
154
+ return this.addShape(boundPoints(corners), style, (context) => {
155
+ tracePoints(context, corners);
156
+ context.closePath();
157
+ });
158
+ }
159
+ /**
160
+ * Draws connected line segments. Needs at least two points.
161
+ *
162
+ * @param points - Points in order
163
+ */
164
+ drawPolyline(points, stroke) {
165
+ if (points.length < 2) {
166
+ return this;
104
167
  }
105
- this.context.stroke();
106
- this.texture.needsUpdate = true;
107
- return this;
168
+ const vertices = copyPoints(points, "UIGraphics.drawPolyline");
169
+ return this.addShape(boundPoints(vertices), { stroke }, (context) => {
170
+ tracePoints(context, vertices);
171
+ });
108
172
  }
109
- /** Frees resources of the graphics */
110
- destroyInternal() {
111
- this.texture.dispose();
112
- super.destroyInternal();
173
+ updateContent() {
174
+ const changed = this.shapesDirty;
175
+ this.shapesDirty = false;
176
+ return changed;
177
+ }
178
+ drawContent(context) {
179
+ for (const shape of this.shapes) {
180
+ context.beginPath();
181
+ shape.buildPath(context);
182
+ if (shape.fill !== undefined) {
183
+ context.fillStyle = toCanvasPaint(context, shape.fill, shape.box);
184
+ context.fill();
185
+ }
186
+ if (shape.stroke !== undefined && shape.stroke.width > 0) {
187
+ const halfWidth = shape.stroke.width / 2;
188
+ const strokeBox = {
189
+ x: shape.box.x - halfWidth,
190
+ y: shape.box.y - halfWidth,
191
+ width: shape.box.width + shape.stroke.width,
192
+ height: shape.box.height + shape.stroke.width,
193
+ };
194
+ context.strokeStyle = toCanvasPaint(context, shape.stroke.paint, strokeBox);
195
+ context.lineWidth = shape.stroke.width;
196
+ context.lineJoin = shape.stroke.join;
197
+ context.lineCap = shape.stroke.cap;
198
+ context.stroke();
199
+ }
200
+ }
113
201
  }
114
- onWillRender(renderer, deltaTime) {
115
- if (this.colorInternal.dirty) {
116
- this.sceneWrapper.setProperties(this.planeHandler, {
117
- color: this.colorInternal,
118
- });
119
- this.colorInternal.setDirtyFalse();
202
+ addShape(box, style, buildPath) {
203
+ const { fill, stroke } = style;
204
+ if (stroke !== undefined) {
205
+ assertValidNonNegativeNumber(stroke.width, "UIGraphics.style.stroke.width");
120
206
  }
121
- super.onWillRender(renderer, deltaTime);
207
+ if (fill === undefined && stroke === undefined) {
208
+ return this;
209
+ }
210
+ this.shapes.push({
211
+ buildPath,
212
+ box,
213
+ fill: fill === undefined ? undefined : resolvePaint(fill),
214
+ stroke: stroke === undefined
215
+ ? undefined
216
+ : {
217
+ paint: resolvePaint(stroke.paint),
218
+ width: stroke.width,
219
+ join: stroke.join ?? GRAPHICS_DEFAULT_STROKE_JOIN,
220
+ cap: stroke.cap ?? GRAPHICS_DEFAULT_STROKE_CAP,
221
+ },
222
+ });
223
+ this.shapesDirty = true;
224
+ return this;
122
225
  }
123
226
  }
@@ -1,16 +1,19 @@
1
+ import type { FerrsignView1 } from "ferrsign";
1
2
  import type { UILayer } from "../../layers/UILayer/UILayer.js";
2
3
  import type { UIColorConfig } from "../../miscellaneous/color/UIColor.Internal.js";
3
4
  import { UIColor } from "../../miscellaneous/color/UIColor.js";
5
+ import type { UIContentAspectElement } from "../../miscellaneous/shared.js";
4
6
  import type { UITextureConfig } from "../../miscellaneous/texture/UITextureView.Internal.js";
5
7
  import { UITextureView } from "../../miscellaneous/texture/UITextureView.js";
6
8
  import { UIElement } from "../UIElement/UIElement.js";
7
9
  import type { UIImageOptions } from "./UIImage.Internal.js";
8
10
  /** Textured image element */
9
- export declare class UIImage extends UIElement {
11
+ export declare class UIImage extends UIElement implements UIContentAspectElement {
10
12
  /** Texture displayed by this image */
11
13
  readonly texture: UITextureView;
12
14
  private readonly colorInternal;
13
15
  private readonly textureTransform;
16
+ private readonly signalContentAspectChangedInternal;
14
17
  /**
15
18
  * Creates a new UIImage instance.
16
19
  *
@@ -23,6 +26,13 @@ export declare class UIImage extends UIElement {
23
26
  constructor(layer: UILayer, texture: UITextureConfig, options?: Partial<UIImageOptions>);
24
27
  /** Multiplicative tint. Alpha channel controls opacity. */
25
28
  get color(): UIColor;
29
+ /**
30
+ * Aspect of the texture. A `UIAspectConstraint` created without an aspect follows it when the
31
+ * texture changes size.
32
+ */
33
+ get contentAspect(): number;
34
+ /** Signal fired with the new aspect when the texture changes size */
35
+ get signalContentAspectChanged(): FerrsignView1<number>;
26
36
  /** Multiplicative tint. Alpha channel controls opacity. */
27
37
  set color(value: UIColorConfig);
28
38
  /** Frees resources of the image */
@@ -1,3 +1,4 @@
1
+ import { Ferrsign1 } from "ferrsign";
1
2
  import { UIColor } from "../../miscellaneous/color/UIColor.js";
2
3
  import { computeTrimmedTransformMatrix, scaleTrim } from "../../miscellaneous/computeTransform.js";
3
4
  import { UITextureView } from "../../miscellaneous/texture/UITextureView.js";
@@ -29,9 +30,11 @@ export class UIImage extends UIElement {
29
30
  textureTransform: textureTransform,
30
31
  color,
31
32
  }, options);
33
+ this.signalContentAspectChangedInternal = new Ferrsign1();
32
34
  this.onTextureDimensionsChanged = (width, height) => {
33
35
  this.width = width;
34
36
  this.height = height;
37
+ this.signalContentAspectChangedInternal.emit(width / height);
35
38
  };
36
39
  this.texture = textureView;
37
40
  this.textureTransform = textureTransform;
@@ -42,6 +45,17 @@ export class UIImage extends UIElement {
42
45
  get color() {
43
46
  return this.colorInternal;
44
47
  }
48
+ /**
49
+ * Aspect of the texture. A `UIAspectConstraint` created without an aspect follows it when the
50
+ * texture changes size.
51
+ */
52
+ get contentAspect() {
53
+ return this.texture.width / this.texture.height;
54
+ }
55
+ /** Signal fired with the new aspect when the texture changes size */
56
+ get signalContentAspectChanged() {
57
+ return this.signalContentAspectChangedInternal;
58
+ }
45
59
  /** Multiplicative tint. Alpha channel controls opacity. */
46
60
  set color(value) {
47
61
  this.colorInternal.set(value);
@@ -25,12 +25,20 @@ export interface UISceneOptions extends UIElementOptions {
25
25
  camera: Camera;
26
26
  /** When to re-render */
27
27
  updateMode: UISceneUpdateMode;
28
- /** Render target resolution multiplier. Range 0.1 to 2.0. */
28
+ /**
29
+ * Render target resolution multiplier over the pixel density of the layer, so 1 renders as
30
+ * many pixels as the element covers on the screen. Range 0.1 to 2.0.
31
+ */
29
32
  resolutionFactor: number;
30
33
  /** Background color */
31
34
  clearColor: UIColorConfig;
32
35
  /** Whether to use depth buffer */
33
36
  enableDepthBuffer: boolean;
37
+ /**
38
+ * Multisample antialiasing samples of the render target, 0 for none. WebGL1 has no multisampled
39
+ * render targets and ignores it. three.js clamps it to the maximum the device supports.
40
+ */
41
+ samples: number;
34
42
  }
35
43
  export declare const SCENE_DEFAULT_RESOLUTION_FACTOR = 0.5;
36
44
  export declare const SCENE_MIN_RESOLUTION_FACTOR = 0.1;
@@ -41,3 +49,4 @@ export declare const SCENE_DEFAULT_CAMERA_FAR = 100;
41
49
  export declare const SCENE_DEFAULT_UPDATE_MODE = UISceneUpdateMode.ON_PROPERTIES_CHANGE;
42
50
  export declare const SCENE_DEFAULT_CLEAR_COLOR: UIColor;
43
51
  export declare const SCENE_DEFAULT_USE_DEPTH = true;
52
+ export declare const SCENE_DEFAULT_SAMPLES = 0;
@@ -22,3 +22,4 @@ export const SCENE_DEFAULT_CAMERA_FAR = 100;
22
22
  export const SCENE_DEFAULT_UPDATE_MODE = UISceneUpdateMode.ON_PROPERTIES_CHANGE;
23
23
  export const SCENE_DEFAULT_CLEAR_COLOR = new UIColor(0, 0, 0, 1);
24
24
  export const SCENE_DEFAULT_USE_DEPTH = true;
25
+ export const SCENE_DEFAULT_SAMPLES = 0;
@@ -32,8 +32,13 @@ export declare class UIScene extends UIElement {
32
32
  get clearColor(): UIColor;
33
33
  /** Multiplicative tint. Alpha channel controls opacity. */
34
34
  get color(): UIColor;
35
- /** Render target resolution multiplier. Range 0.1 to 2.0. */
35
+ /**
36
+ * Render target resolution multiplier over the pixel density of the layer, so 1 renders as
37
+ * many pixels as the element covers on the screen. Range 0.1 to 2.0.
38
+ */
36
39
  get resolutionFactor(): number;
40
+ /** Multisample antialiasing samples of the render target, 0 for none. WebGL1 ignores it. */
41
+ get samples(): number;
37
42
  /** Three.js scene to render */
38
43
  get scene(): Scene;
39
44
  /** Controls when scene re-renders */
@@ -46,6 +51,11 @@ export declare class UIScene extends UIElement {
46
51
  set color(value: UIColorConfig);
47
52
  /** Render target resolution multiplier. Range 0.1 to 2.0. */
48
53
  set resolutionFactor(value: number);
54
+ /**
55
+ * Multisample antialiasing samples of the render target, 0 for none. WebGL1 ignores it. A change
56
+ * frees the render target, which is allocated again and rendered on the next frame.
57
+ */
58
+ set samples(value: number);
49
59
  /** Three.js scene to render */
50
60
  set scene(value: Scene);
51
61
  /** Controls when scene re-renders */
@@ -1,11 +1,11 @@
1
1
  import { Color, LinearFilter, MathUtils, PerspectiveCamera, RGBAFormat, Scene, UnsignedByteType, WebGLRenderTarget, } from "three";
2
- import { assertValidPositiveNumber } from "../../miscellaneous/asserts.js";
2
+ import { assertValidNonNegativeInteger, assertValidPositiveNumber, } from "../../miscellaneous/asserts.js";
3
3
  import { UIColor } from "../../miscellaneous/color/UIColor.js";
4
4
  import { UIAffineMatrix3 } from "../../miscellaneous/UIAffineMatrix3.js";
5
5
  import source from "../../shaders/UIImage.js";
6
6
  import { DUMMY_DEFAULT_HEIGHT, DUMMY_DEFAULT_WIDTH } from "../UIDummy/UIDummy.Internal.js";
7
7
  import { UIElement } from "../UIElement/UIElement.js";
8
- import { SCENE_DEFAULT_CAMERA_FAR, SCENE_DEFAULT_CAMERA_FOV, SCENE_DEFAULT_CAMERA_NEAR, SCENE_DEFAULT_CLEAR_COLOR, SCENE_DEFAULT_RESOLUTION_FACTOR, SCENE_DEFAULT_UPDATE_MODE, SCENE_DEFAULT_USE_DEPTH, SCENE_MAX_RESOLUTION_FACTOR, SCENE_MIN_RESOLUTION_FACTOR, UISceneUpdateMode, } from "./UIScene.Internal.js";
8
+ import { SCENE_DEFAULT_CAMERA_FAR, SCENE_DEFAULT_CAMERA_FOV, SCENE_DEFAULT_CAMERA_NEAR, SCENE_DEFAULT_CLEAR_COLOR, SCENE_DEFAULT_RESOLUTION_FACTOR, SCENE_DEFAULT_SAMPLES, SCENE_DEFAULT_UPDATE_MODE, SCENE_DEFAULT_USE_DEPTH, SCENE_MAX_RESOLUTION_FACTOR, SCENE_MIN_RESOLUTION_FACTOR, UISceneUpdateMode, } from "./UIScene.Internal.js";
9
9
  let previousClearColor = new Color();
10
10
  let previousClearAlpha = 0;
11
11
  /** Renders Three.js scene to texture */
@@ -22,6 +22,8 @@ export class UIScene extends UIElement {
22
22
  assertValidPositiveNumber(options.resolutionFactor, "UIScene.constructor.resolutionFactor");
23
23
  }
24
24
  const resolutionFactor = MathUtils.clamp(options.resolutionFactor ?? SCENE_DEFAULT_RESOLUTION_FACTOR, SCENE_MIN_RESOLUTION_FACTOR, SCENE_MAX_RESOLUTION_FACTOR);
25
+ const samples = options.samples ?? SCENE_DEFAULT_SAMPLES;
26
+ assertValidNonNegativeInteger(samples, "UIScene.constructor.options.samples");
25
27
  const w = options.width ?? DUMMY_DEFAULT_WIDTH;
26
28
  const h = options.height ?? DUMMY_DEFAULT_HEIGHT;
27
29
  const renderTarget = new WebGLRenderTarget(w * resolutionFactor, h * resolutionFactor, {
@@ -31,6 +33,7 @@ export class UIScene extends UIElement {
31
33
  type: UnsignedByteType,
32
34
  depthBuffer: options.enableDepthBuffer ?? SCENE_DEFAULT_USE_DEPTH,
33
35
  stencilBuffer: false,
36
+ samples,
34
37
  });
35
38
  const color = new UIColor(options.color);
36
39
  super(layer, source, {
@@ -69,10 +72,17 @@ export class UIScene extends UIElement {
69
72
  get color() {
70
73
  return this.colorInternal;
71
74
  }
72
- /** Render target resolution multiplier. Range 0.1 to 2.0. */
75
+ /**
76
+ * Render target resolution multiplier over the pixel density of the layer, so 1 renders as
77
+ * many pixels as the element covers on the screen. Range 0.1 to 2.0.
78
+ */
73
79
  get resolutionFactor() {
74
80
  return this.resolutionFactorInternal;
75
81
  }
82
+ /** Multisample antialiasing samples of the render target, 0 for none. WebGL1 ignores it. */
83
+ get samples() {
84
+ return this.renderTarget.samples;
85
+ }
76
86
  /** Three.js scene to render */
77
87
  get scene() {
78
88
  return this.sceneInternal;
@@ -104,6 +114,19 @@ export class UIScene extends UIElement {
104
114
  this.resolutionFactorInternal = value;
105
115
  }
106
116
  }
117
+ /**
118
+ * Multisample antialiasing samples of the render target, 0 for none. WebGL1 ignores it. A change
119
+ * frees the render target, which is allocated again and rendered on the next frame.
120
+ */
121
+ set samples(value) {
122
+ this.assertNotDestroyed("UIScene.samples");
123
+ assertValidNonNegativeInteger(value, "UIScene.samples");
124
+ if (this.renderTarget.samples !== value) {
125
+ this.renderTarget.samples = value;
126
+ this.renderTarget.dispose();
127
+ this.updateRequired = true;
128
+ }
129
+ }
107
130
  /** Three.js scene to render */
108
131
  set scene(value) {
109
132
  if (this.sceneInternal !== value) {
@@ -132,8 +155,9 @@ export class UIScene extends UIElement {
132
155
  this.evenFrame = !this.evenFrame;
133
156
  // Compared every frame rather than on layout changes, so a size change is kept until a frame
134
157
  // that renders the scene applies it
135
- const targetWidth = this.width * this.resolutionFactorInternal;
136
- const targetHeight = this.height * this.resolutionFactorInternal;
158
+ const pixelsPerUnit = this.layer.pixelDensity * this.resolutionFactorInternal;
159
+ const targetWidth = Math.max(Math.round(this.width * pixelsPerUnit), 1);
160
+ const targetHeight = Math.max(Math.round(this.height * pixelsPerUnit), 1);
137
161
  const isSizeDirty = this.renderTarget.width !== targetWidth || this.renderTarget.height !== targetHeight;
138
162
  const isFramebufferDirty = this.updateRequired ||
139
163
  this.updateModeInternal === UISceneUpdateMode.EVERY_FRAME ||
@@ -1,5 +1,5 @@
1
1
  import type { UITextSpanConfig } from "../../miscellaneous/text-span/UITextSpan.Internal.js";
2
- import type { UITextStyleConfig } from "../../miscellaneous/text-style/UITextStyle.Internal.js";
2
+ import type { UIResolvedTextStyle } from "../../miscellaneous/text-style/UITextStyle.Internal.js";
3
3
  /** Text content configuration */
4
4
  export type UITextContent = (UITextSpanConfig | string)[] | UITextSpanConfig | string;
5
5
  /** Text dimensions */
@@ -25,7 +25,7 @@ export interface UITextChunk {
25
25
  /** Text content */
26
26
  text: string;
27
27
  /** Resolved style */
28
- style: UITextStyleConfig;
28
+ style: UIResolvedTextStyle;
29
29
  /** Chunk metrics */
30
30
  metrics: UIChunkMetrics;
31
31
  /** Resolved bidirectional class used for visual reordering */
@@ -18,11 +18,21 @@ export declare enum UITextResizeMode {
18
18
  /** Scale text to fit fixed element size */
19
19
  SCALE = 1
20
20
  }
21
+ /** What sets the height of a line */
22
+ export declare enum UITextLineBox {
23
+ /** The glyphs the line holds, so a line without ascenders or descenders is lower */
24
+ GLYPHS = 0,
25
+ /**
26
+ * The ascent and descent of its font, so lines of one font are as high whatever glyphs they
27
+ * hold, and texts of one font aligned by an edge share the baseline
28
+ */
29
+ FONT = 1
30
+ }
21
31
  /** Configuration options for UIText */
22
32
  export interface UITextOptions extends UIElementOptions {
23
33
  /** Multiplicative tint */
24
34
  color: UIColorConfig;
25
- /** Text padding in world units */
35
+ /** Text padding in pixels of the style */
26
36
  padding: UIInsetsConfig;
27
37
  /** Default style applied to all spans */
28
38
  commonStyle: Partial<UITextStyleConfig>;
@@ -32,7 +42,10 @@ export interface UITextOptions extends UIElementOptions {
32
42
  maxLineWidth: number;
33
43
  /** Base direction used for bidirectional text reordering */
34
44
  direction: UITextDirection;
45
+ /** What sets the height of a line */
46
+ lineBox: UITextLineBox;
35
47
  }
36
48
  export declare const TEXT_DEFAULT_MAX_LINE_WIDTH = 1024;
37
49
  export declare const TEXT_DEFAULT_RESIZE_MODE = UITextResizeMode.SCALE;
38
50
  export declare const TEXT_DEFAULT_DIRECTION = UITextDirection.AUTO;
51
+ export declare const TEXT_DEFAULT_LINE_BOX = UITextLineBox.GLYPHS;
@@ -16,6 +16,18 @@ export var UITextResizeMode;
16
16
  /** Scale text to fit fixed element size */
17
17
  UITextResizeMode[UITextResizeMode["SCALE"] = 1] = "SCALE";
18
18
  })(UITextResizeMode || (UITextResizeMode = {}));
19
+ /** What sets the height of a line */
20
+ export var UITextLineBox;
21
+ (function (UITextLineBox) {
22
+ /** The glyphs the line holds, so a line without ascenders or descenders is lower */
23
+ UITextLineBox[UITextLineBox["GLYPHS"] = 0] = "GLYPHS";
24
+ /**
25
+ * The ascent and descent of its font, so lines of one font are as high whatever glyphs they
26
+ * hold, and texts of one font aligned by an edge share the baseline
27
+ */
28
+ UITextLineBox[UITextLineBox["FONT"] = 1] = "FONT";
29
+ })(UITextLineBox || (UITextLineBox = {}));
19
30
  export const TEXT_DEFAULT_MAX_LINE_WIDTH = 1024;
20
31
  export const TEXT_DEFAULT_RESIZE_MODE = UITextResizeMode.SCALE;
21
32
  export const TEXT_DEFAULT_DIRECTION = UITextDirection.AUTO;
33
+ export const TEXT_DEFAULT_LINE_BOX = UITextLineBox.GLYPHS;
@@ -1,15 +1,16 @@
1
1
  import type { UICanvasRenderingContext2D } from "../../miscellaneous/canvas.js";
2
2
  import type { UITextSpan } from "../../miscellaneous/text-span/UITextSpan.js";
3
- import type { UITextStyleConfig } from "../../miscellaneous/text-style/UITextStyle.Internal.js";
3
+ import type { UIResolvedTextStyle } from "../../miscellaneous/text-style/UITextStyle.Internal.js";
4
4
  import { UITextStyle } from "../../miscellaneous/text-style/UITextStyle.js";
5
5
  import type { UIChunkMetrics, UITextChunk, UITextLine, UITextSize } from "./UIText.Interfaces.js";
6
+ import { UITextLineBox } from "./UIText.Internal.js";
6
7
  /**
7
8
  * Applies font of a resolved style to a canvas context.
8
9
  * Shared by measuring and rendering so both stay in sync.
9
10
  * @param context Canvas context to configure
10
11
  * @param textStyle Resolved text style
11
12
  */
12
- export declare function applyFontStyle(context: UICanvasRenderingContext2D, textStyle: UITextStyleConfig): void;
13
+ export declare function applyFontStyle(context: UICanvasRenderingContext2D, textStyle: UIResolvedTextStyle): void;
13
14
  /**
14
15
  * Splits text into renderable chunks.
15
16
  * No-break spaces stay inside words, so a line never breaks at them.
@@ -19,12 +20,15 @@ export declare function applyFontStyle(context: UICanvasRenderingContext2D, text
19
20
  export declare function splitTextIntoChunks(text: string): string[];
20
21
  /**
21
22
  * Measures single text chunk.
23
+ * With `UITextLineBox.FONT` the chunk reaches the ascent and descent of its font, or further
24
+ * where its glyphs do. A browser without font metrics measures the glyphs.
22
25
  * @param context Canvas context for measurement
23
26
  * @param text Chunk text
24
27
  * @param textStyle Resolved text style
28
+ * @param lineBox What the height of the chunk follows
25
29
  * @returns Chunk metrics
26
30
  */
27
- export declare function measureTextChunk(context: UICanvasRenderingContext2D, text: string, textStyle: UITextStyleConfig): UIChunkMetrics;
31
+ export declare function measureTextChunk(context: UICanvasRenderingContext2D, text: string, textStyle: UIResolvedTextStyle, lineBox: UITextLineBox): UIChunkMetrics;
28
32
  /**
29
33
  * Builds text lines with word wrapping.
30
34
  * Spaces behave as in a browser: a space never moves to the next line by itself, spaces at the
@@ -50,9 +54,10 @@ export declare function calculateTextSize(lines: UITextLine[]): UITextSize;
50
54
  * @param textSpans Text spans with styles
51
55
  * @param commonStyle Fallback style
52
56
  * @param isRTL Whether the paragraph reads right to left
57
+ * @param lineBox What the height of a line follows
53
58
  * @returns Measured text chunks
54
59
  */
55
- export declare function buildTextChunks(context: UICanvasRenderingContext2D, textSpans: UITextSpan[], commonStyle: UITextStyle, isRTL: boolean): UITextChunk[];
60
+ export declare function buildTextChunks(context: UICanvasRenderingContext2D, textSpans: UITextSpan[], commonStyle: UITextStyle, isRTL: boolean, lineBox: UITextLineBox): UITextChunk[];
56
61
  /**
57
62
  * Calculates text layout parameters.
58
63
  * @param textChunks Measured text chunks