laymur 0.27.1 → 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 (138) hide show
  1. package/README.md +99 -5
  2. package/dist/core/constraints/UIAspectConstraint/UIAspectConstraint.d.ts +14 -3
  3. package/dist/core/constraints/UIAspectConstraint/UIAspectConstraint.js +40 -6
  4. package/dist/core/constraints/UICustomConstraint.js +2 -2
  5. package/dist/core/constraints/UIHeightConstraint/UIHeightConstraint.js +3 -2
  6. package/dist/core/constraints/UIHorizontalDistanceConstraint/UIHorizontalDistanceConstraint.js +3 -2
  7. package/dist/core/constraints/UIHorizontalInterpolationConstraint/UIHorizontalInterpolationConstraint.js +3 -2
  8. package/dist/core/constraints/UIHorizontalProportionConstraint/UIHorizontalProportionConstraint.js +3 -2
  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 +3 -2
  12. package/dist/core/constraints/UIVerticalInterpolationConstraint/UIVerticalInterpolationConstraint.js +3 -2
  13. package/dist/core/constraints/UIVerticalProportionConstraint/UIVerticalProportionConstraint.js +3 -2
  14. package/dist/core/constraints/UIWidthConstraint/UIWidthConstraint.js +3 -2
  15. package/dist/core/constraints/constraintAsserts.d.ts +41 -0
  16. package/dist/core/constraints/constraintAsserts.js +171 -0
  17. package/dist/core/elements/UICanvasElement/UICanvasElement.d.ts +54 -0
  18. package/dist/core/elements/UICanvasElement/UICanvasElement.js +70 -0
  19. package/dist/core/elements/UIDummy/UIDummy.Internal.d.ts +2 -0
  20. package/dist/core/elements/UIDummy/UIDummy.d.ts +8 -5
  21. package/dist/core/elements/UIDummy/UIDummy.js +24 -11
  22. package/dist/core/elements/UIElement/UIElement.Internal.d.ts +6 -5
  23. package/dist/core/elements/UIElement/UIElement.Internal.js +0 -4
  24. package/dist/core/elements/UIElement/UIElement.d.ts +12 -6
  25. package/dist/core/elements/UIElement/UIElement.js +14 -7
  26. package/dist/core/elements/UIGraphics/UIGraphics.Internal.d.ts +42 -2
  27. package/dist/core/elements/UIGraphics/UIGraphics.Internal.js +2 -2
  28. package/dist/core/elements/UIGraphics/UIGraphics.d.ts +48 -34
  29. package/dist/core/elements/UIGraphics/UIGraphics.js +196 -93
  30. package/dist/core/elements/UIImage/UIImage.d.ts +11 -1
  31. package/dist/core/elements/UIImage/UIImage.js +14 -0
  32. package/dist/core/elements/UIScene/UIScene.Internal.d.ts +10 -1
  33. package/dist/core/elements/UIScene/UIScene.Internal.js +1 -0
  34. package/dist/core/elements/UIScene/UIScene.d.ts +11 -1
  35. package/dist/core/elements/UIScene/UIScene.js +29 -5
  36. package/dist/core/elements/UIText/UIText.Interfaces.d.ts +2 -2
  37. package/dist/core/elements/UIText/UIText.Internal.d.ts +14 -1
  38. package/dist/core/elements/UIText/UIText.Internal.js +12 -0
  39. package/dist/core/elements/UIText/UIText.Measuring.d.ts +9 -4
  40. package/dist/core/elements/UIText/UIText.Measuring.js +17 -6
  41. package/dist/core/elements/UIText/UIText.Rendering.d.ts +7 -16
  42. package/dist/core/elements/UIText/UIText.Rendering.js +212 -46
  43. package/dist/core/elements/UIText/UIText.d.ts +64 -29
  44. package/dist/core/elements/UIText/UIText.js +134 -149
  45. package/dist/core/index.d.ts +10 -4
  46. package/dist/core/index.js +5 -1
  47. package/dist/core/layers/UILayer/UILayer.d.ts +17 -3
  48. package/dist/core/layers/UILayer/UILayer.js +35 -4
  49. package/dist/core/layers/UIViewportLayer/UIViewportLayer.d.ts +2 -0
  50. package/dist/core/layers/UIViewportLayer/UIViewportLayer.js +4 -1
  51. package/dist/core/miscellaneous/UIBlendMode.d.ts +5 -0
  52. package/dist/core/miscellaneous/UIBlendMode.js +7 -0
  53. package/dist/core/miscellaneous/asserts.d.ts +0 -41
  54. package/dist/core/miscellaneous/asserts.js +0 -170
  55. package/dist/core/miscellaneous/canvas-surface/UICanvasSurface.d.ts +46 -0
  56. package/dist/core/miscellaneous/canvas-surface/UICanvasSurface.js +86 -0
  57. package/dist/core/miscellaneous/color/UIColor.d.ts +17 -0
  58. package/dist/core/miscellaneous/color/UIColor.js +22 -0
  59. package/dist/core/miscellaneous/color/canvasColor.d.ts +9 -0
  60. package/dist/core/miscellaneous/color/canvasColor.js +15 -0
  61. package/dist/core/miscellaneous/insets/UIInsets.d.ts +3 -0
  62. package/dist/core/miscellaneous/insets/UIInsets.js +15 -0
  63. package/dist/core/miscellaneous/mask-function/UIProgressMaskFunctionCircular.d.ts +10 -7
  64. package/dist/core/miscellaneous/mask-function/UIProgressMaskFunctionCircular.js +11 -8
  65. package/dist/core/miscellaneous/paint/UIGradient.Internal.d.ts +37 -0
  66. package/dist/core/miscellaneous/paint/UIGradient.Internal.js +14 -0
  67. package/dist/core/miscellaneous/paint/UIGradient.d.ts +18 -0
  68. package/dist/core/miscellaneous/paint/UIGradient.js +35 -0
  69. package/dist/core/miscellaneous/paint/UILinearGradient.d.ts +14 -0
  70. package/dist/core/miscellaneous/paint/UILinearGradient.js +22 -0
  71. package/dist/core/miscellaneous/paint/UIPaint.Internal.d.ts +21 -0
  72. package/dist/core/miscellaneous/paint/UIPaint.Internal.js +1 -0
  73. package/dist/core/miscellaneous/paint/UIPaint.d.ts +17 -0
  74. package/dist/core/miscellaneous/paint/UIPaint.js +21 -0
  75. package/dist/core/miscellaneous/paint/UIRadialGradient.d.ts +18 -0
  76. package/dist/core/miscellaneous/paint/UIRadialGradient.js +33 -0
  77. package/dist/core/miscellaneous/shared.d.ts +14 -0
  78. package/dist/core/miscellaneous/shared.js +11 -0
  79. package/dist/core/miscellaneous/text-style/UITextStyle.Internal.d.ts +91 -30
  80. package/dist/core/miscellaneous/text-style/UITextStyle.Internal.js +12 -8
  81. package/dist/core/miscellaneous/text-style/UITextStyle.d.ts +41 -63
  82. package/dist/core/miscellaneous/text-style/UITextStyle.js +165 -291
  83. package/dist/core/miscellaneous/texture/UITextureView.Internal.d.ts +2 -1
  84. package/dist/core/miscellaneous/texture/UITextureView.Internal.js +12 -3
  85. package/dist/core/shaders/UIProgressMaskFunctionCircular.js +1 -2
  86. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.Internal.d.ts +7 -3
  87. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.d.ts +2 -1
  88. package/dist/core/wrappers/UIInputWrapper/UIInputWrapper.js +19 -10
  89. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.Internal.d.ts +6 -2
  90. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.d.ts +41 -9
  91. package/dist/core/wrappers/UISolverWrapper/UISolverWrapper.js +97 -35
  92. package/dist/particles/behaviorModules/UIBehaviorAlignToVelocity.d.ts +44 -0
  93. package/dist/particles/behaviorModules/UIBehaviorAlignToVelocity.js +75 -0
  94. package/dist/particles/behaviorModules/UIBehaviorBounce.d.ts +51 -0
  95. package/dist/particles/behaviorModules/UIBehaviorBounce.js +107 -0
  96. package/dist/particles/behaviorModules/UIBehaviorOrbit.d.ts +39 -0
  97. package/dist/particles/behaviorModules/UIBehaviorOrbit.js +73 -0
  98. package/dist/particles/behaviorModules/UIBehaviorScaleOverLife.d.ts +7 -2
  99. package/dist/particles/behaviorModules/UIBehaviorScaleOverLife.js +9 -5
  100. package/dist/particles/behaviorModules/UIBehaviorSeek.d.ts +38 -0
  101. package/dist/particles/behaviorModules/UIBehaviorSeek.js +75 -0
  102. package/dist/particles/elements/UIEmitter.Internal.d.ts +48 -1
  103. package/dist/particles/elements/UIEmitter.Internal.js +26 -1
  104. package/dist/particles/elements/UIEmitter.Schedule.d.ts +31 -0
  105. package/dist/particles/elements/UIEmitter.Schedule.js +81 -0
  106. package/dist/particles/elements/UIEmitter.d.ts +79 -9
  107. package/dist/particles/elements/UIEmitter.js +241 -83
  108. package/dist/particles/index.d.ts +13 -1
  109. package/dist/particles/index.js +11 -1
  110. package/dist/particles/instancedParticle/UIInstancedParticle.Internal.d.ts +6 -0
  111. package/dist/particles/instancedParticle/UIInstancedParticle.Internal.js +27 -0
  112. package/dist/particles/instancedParticle/UIInstancedParticle.d.ts +7 -1
  113. package/dist/particles/instancedParticle/UIInstancedParticle.js +12 -1
  114. package/dist/particles/miscellaneous/miscellaneous.d.ts +22 -0
  115. package/dist/particles/miscellaneous/miscellaneous.js +38 -0
  116. package/dist/particles/renderingModules/UIRenderingFlipbook.d.ts +41 -0
  117. package/dist/particles/renderingModules/UIRenderingFlipbook.js +70 -0
  118. package/dist/particles/renderingModules/UIRenderingModule.d.ts +2 -1
  119. package/dist/particles/renderingModules/UIRenderingModule.js +2 -1
  120. package/dist/particles/renderingModules/UIRenderingRandomColor.d.ts +19 -0
  121. package/dist/particles/renderingModules/UIRenderingRandomColor.js +28 -0
  122. package/dist/particles/renderingModules/UIRenderingShape.d.ts +31 -0
  123. package/dist/particles/renderingModules/UIRenderingShape.js +54 -0
  124. package/dist/particles/renderingModules/UIRenderingTexture.d.ts +1 -1
  125. package/dist/particles/renderingModules/UIRenderingTexture.js +4 -28
  126. package/dist/particles/shaders/UIRenderingFlipbook.d.ts +5 -0
  127. package/dist/particles/shaders/UIRenderingFlipbook.js +19 -0
  128. package/dist/particles/shaders/UIRenderingRandomColor.d.ts +2 -0
  129. package/dist/particles/shaders/UIRenderingRandomColor.js +10 -0
  130. package/dist/particles/shaders/UIRenderingShape.d.ts +6 -0
  131. package/dist/particles/shaders/UIRenderingShape.js +15 -0
  132. package/dist/particles/shaders/UIRenderingTexture.d.ts +4 -2
  133. package/dist/particles/shaders/UIRenderingTexture.js +27 -2
  134. package/dist/particles/spawnModules/UISpawnCircle.d.ts +44 -0
  135. package/dist/particles/spawnModules/UISpawnCircle.js +81 -0
  136. package/dist/particles/spawnModules/UISpawnRadialVelocity.d.ts +37 -0
  137. package/dist/particles/spawnModules/UISpawnRadialVelocity.js +75 -0
  138. package/package.json +1 -1
@@ -1,17 +1,19 @@
1
- import { createCanvas, createCanvasTexture, getCanvas2DContext, } from "../../miscellaneous/canvas.js";
2
- import { UIColor } from "../../miscellaneous/color/UIColor.js";
1
+ import { Ferrsign1 } from "ferrsign";
3
2
  import { UIInsets } from "../../miscellaneous/insets/UIInsets.js";
4
3
  import { UITextSpan } from "../../miscellaneous/text-span/UITextSpan.js";
5
4
  import { UITextStyle } from "../../miscellaneous/text-style/UITextStyle.js";
6
- import { UIAffineMatrix3 } from "../../miscellaneous/UIAffineMatrix3.js";
7
- import source from "../../shaders/UIImage.js";
8
- import { UIElement } from "../UIElement/UIElement.js";
5
+ import { UICanvasElement } from "../UICanvasElement/UICanvasElement.js";
9
6
  import { detectIsRTL } from "./UIText.Bidi.js";
10
- import { TEXT_DEFAULT_DIRECTION, TEXT_DEFAULT_MAX_LINE_WIDTH, TEXT_DEFAULT_RESIZE_MODE, UITextDirection, UITextResizeMode, } from "./UIText.Internal.js";
7
+ import { TEXT_DEFAULT_DIRECTION, TEXT_DEFAULT_LINE_BOX, TEXT_DEFAULT_MAX_LINE_WIDTH, TEXT_DEFAULT_RESIZE_MODE, UITextDirection, UITextResizeMode, } from "./UIText.Internal.js";
11
8
  import { buildTextChunks, calculateTextContentParameters } from "./UIText.Measuring.js";
12
9
  import { renderTextLines } from "./UIText.Rendering.js";
13
- /** Canvas-based text rendering element */
14
- export class UIText extends UIElement {
10
+ /** Smallest width and height of the element, kept by empty and whitespace-only text */
11
+ const TEXT_MIN_DIMENSION = 1;
12
+ /**
13
+ * Text element. The text is laid out in pixels of its style, which are the content units of the
14
+ * canvas it is drawn on.
15
+ */
16
+ export class UIText extends UICanvasElement {
15
17
  /**
16
18
  * Creates a new UIText instance.
17
19
  *
@@ -20,54 +22,33 @@ export class UIText extends UIElement {
20
22
  * @param options - Configuration options
21
23
  */
22
24
  constructor(layer, content, options = {}) {
23
- const canvas = createCanvas(2, 2);
24
- const context = getCanvas2DContext(canvas);
25
- if (!context) {
26
- throw new Error("UIText.constructor: failed to create canvas context");
27
- }
28
- const texture = createCanvasTexture(canvas);
29
- texture.needsUpdate = true;
30
- const color = new UIColor(options.color);
31
- super(layer, source, {
32
- texture: texture,
33
- textureTransform: new UIAffineMatrix3(),
34
- color,
35
- }, options);
36
- /** Padding last set by auto padding; any other value means it was changed by hand */
37
- this.lastAutoPadding = 0;
25
+ super(layer, "UIText.constructor", options);
26
+ /** Size of the laid out text with its padding, in pixels of the style */
27
+ this.contentWidth = TEXT_MIN_DIMENSION;
28
+ this.contentHeight = TEXT_MIN_DIMENSION;
38
29
  this.contentInternal = [];
39
30
  this.contentDirty = false;
40
31
  this.maxLineWidthDirty = false;
41
32
  this.resizeModeDirty = false;
42
33
  this.directionDirty = false;
43
34
  this.isRTL = false;
35
+ this.lineBoxDirty = false;
44
36
  this.textChunks = [];
45
- this.colorInternal = color;
37
+ this.textLines = [];
38
+ this.contentAspectInternal = 1;
39
+ this.signalContentAspectChangedInternal = new Ferrsign1();
46
40
  this.paddingInternal = new UIInsets(options.padding);
47
41
  this.autoPaddingInternal = options.padding === undefined;
42
+ this.autoPaddingVersion = this.paddingInternal.version;
48
43
  this.commonStyle = new UITextStyle(options.commonStyle);
49
44
  this.maxLineWidthInternal = options.maxLineWidth ?? TEXT_DEFAULT_MAX_LINE_WIDTH;
50
45
  this.resizeModeInternal = options.resizeMode ?? TEXT_DEFAULT_RESIZE_MODE;
51
46
  this.directionInternal = options.direction ?? TEXT_DEFAULT_DIRECTION;
52
- this.canvas = canvas;
53
- this.context = context;
54
- this.texture = texture;
47
+ this.lineBoxInternal = options.lineBox ?? TEXT_DEFAULT_LINE_BOX;
55
48
  this.content = content;
56
49
  this.lastWidth = this.width;
57
50
  this.lastHeight = this.height;
58
- this.updateAutoPadding();
59
- switch (this.resizeModeInternal) {
60
- case UITextResizeMode.SCALE:
61
- this.tryToRenderTextScaleMode();
62
- break;
63
- case UITextResizeMode.BREAK:
64
- this.tryToRenderTextBreakMode();
65
- break;
66
- }
67
- }
68
- /** Multiplicative tint. Alpha channel controls opacity. */
69
- get color() {
70
- return this.colorInternal;
51
+ this.layout(false);
71
52
  }
72
53
  /** Text content as array of styled spans */
73
54
  get content() {
@@ -77,16 +58,16 @@ export class UIText extends UIElement {
77
58
  get maxLineWidth() {
78
59
  return this.maxLineWidthInternal;
79
60
  }
80
- /** Text padding in world units */
61
+ /** Text padding in pixels of the style */
81
62
  get padding() {
82
63
  return this.paddingInternal;
83
64
  }
84
65
  /**
85
- * Whether the padding follows the stroke and shadow of the styles. On by default, off once
66
+ * Whether the padding follows the outlines and shadows of the styles. On by default, off once
86
67
  * the padding is set by hand.
87
68
  */
88
69
  get autoPadding() {
89
- return this.autoPaddingInternal;
70
+ return this.autoPaddingInternal && this.paddingInternal.version === this.autoPaddingVersion;
90
71
  }
91
72
  /** Controls how text adapts to element size */
92
73
  get resizeMode() {
@@ -96,11 +77,25 @@ export class UIText extends UIElement {
96
77
  get direction() {
97
78
  return this.directionInternal;
98
79
  }
99
- /** Multiplicative tint. Alpha channel controls opacity. */
100
- set color(value) {
101
- this.colorInternal.set(value);
80
+ /** What sets the height of a line */
81
+ get lineBox() {
82
+ return this.lineBoxInternal;
83
+ }
84
+ /**
85
+ * Aspect of the text laid out at `maxLineWidth` with its padding. A `UIAspectConstraint`
86
+ * created without an aspect follows it as the text changes.
87
+ */
88
+ get contentAspect() {
89
+ return this.contentAspectInternal;
102
90
  }
103
- /** Text content as string, span config, or array */
91
+ /** Signal fired with the new aspect when the content aspect changes */
92
+ get signalContentAspectChanged() {
93
+ return this.signalContentAspectChangedInternal;
94
+ }
95
+ /**
96
+ * Text content as string, span config, or array. Text without visible characters keeps the
97
+ * element at least 1 world unit wide and high.
98
+ */
104
99
  set content(value) {
105
100
  if (this.contentInternal !== value) {
106
101
  if (typeof value === "string") {
@@ -132,20 +127,20 @@ export class UIText extends UIElement {
132
127
  this.maxLineWidthDirty = true;
133
128
  }
134
129
  }
135
- /** Text padding in world units. Turns auto padding off. */
130
+ /** Text padding in pixels of the style. Turns auto padding off. */
136
131
  set padding(value) {
137
132
  this.autoPaddingInternal = false;
138
133
  this.paddingInternal.set(value);
139
134
  }
140
135
  /**
141
- * Whether the padding follows the stroke and shadow of the styles. On by default, off once
136
+ * Whether the padding follows the outlines and shadows of the styles. On by default, off once
142
137
  * the padding is set by hand.
143
138
  */
144
139
  set autoPadding(value) {
145
- if (this.autoPaddingInternal !== value) {
140
+ if (this.autoPadding !== value) {
146
141
  this.autoPaddingInternal = value;
147
142
  if (value) {
148
- this.lastAutoPadding = Number.NaN;
143
+ this.autoPaddingVersion = this.paddingInternal.version;
149
144
  this.updateAutoPadding();
150
145
  }
151
146
  }
@@ -164,146 +159,136 @@ export class UIText extends UIElement {
164
159
  this.directionDirty = true;
165
160
  }
166
161
  }
167
- /** Frees resources of the text */
168
- destroyInternal() {
169
- this.texture.dispose();
170
- super.destroyInternal();
171
- }
172
- onWillRender(renderer, deltaTime) {
173
- switch (this.resizeModeInternal) {
174
- case UITextResizeMode.SCALE:
175
- this.tryToRenderTextScaleMode();
176
- break;
177
- case UITextResizeMode.BREAK:
178
- this.tryToRenderTextBreakMode();
179
- break;
162
+ /** What sets the height of a line */
163
+ set lineBox(value) {
164
+ if (this.lineBoxInternal !== value) {
165
+ this.lineBoxInternal = value;
166
+ this.lineBoxDirty = true;
180
167
  }
181
- super.onWillRender(renderer, deltaTime);
182
168
  }
183
- tryToRenderTextScaleMode() {
169
+ updateContent(layoutChanged) {
170
+ return this.layout(layoutChanged);
171
+ }
172
+ drawContent(context) {
173
+ renderTextLines(context, this.textLines, this.isRTL, this.paddingInternal.top, this.paddingInternal.left);
174
+ }
175
+ /**
176
+ * Lays the text out again when anything it depends on changed.
177
+ * @param layoutChanged Whether the solver changed the layout since the last call
178
+ * @returns True when the text was laid out again
179
+ */
180
+ layout(layoutChanged) {
184
181
  if (this.checkContentDirty()) {
185
182
  this.updateAutoPadding();
186
183
  }
187
- if (!this.colorInternal.dirty &&
188
- !this.resizeModeDirty &&
184
+ const laidOut = this.resizeModeInternal === UITextResizeMode.SCALE
185
+ ? this.layoutScaleMode(layoutChanged)
186
+ : this.layoutBreakMode(layoutChanged);
187
+ if (laidOut) {
188
+ this.resizeModeDirty = false;
189
+ this.directionDirty = false;
190
+ this.maxLineWidthDirty = false;
191
+ this.lineBoxDirty = false;
192
+ this.paddingInternal.setDirtyFalse();
193
+ this.setDimensionsDirtyFalse();
194
+ this.setContentDirtyFalse();
195
+ }
196
+ return laidOut;
197
+ }
198
+ layoutScaleMode(layoutChanged) {
199
+ if (!this.resizeModeDirty &&
189
200
  !this.directionDirty &&
190
201
  !this.maxLineWidthDirty &&
202
+ !this.lineBoxDirty &&
191
203
  !this.paddingInternal.dirty &&
192
- !(this.consumeLayoutChange() &&
204
+ !(layoutChanged &&
193
205
  this.lastWidth / this.lastHeight !== Math.round(this.width) / Math.round(this.height)) &&
194
206
  !this.checkContentDirty()) {
195
- return;
207
+ return false;
196
208
  }
197
209
  const textChunks = this.buildTextChunks();
198
210
  const { lines, size } = calculateTextContentParameters(textChunks, this.maxLineWidthInternal, this.isRTL);
199
- const paddingV = this.paddingInternal.top + this.paddingInternal.bottom;
200
- const paddingH = this.paddingInternal.left + this.paddingInternal.right;
201
- const desiredHeight = size.height + paddingV;
202
- const desiredWidth = size.width + paddingH;
211
+ const { width: desiredWidth, height: desiredHeight } = this.elementSize(size);
212
+ this.updateContentAspect(desiredWidth / desiredHeight);
203
213
  this.width = desiredWidth;
204
214
  this.height = (desiredHeight / desiredWidth) * Math.round(this.width);
205
215
  this.width = (desiredWidth / desiredHeight) * Math.round(this.height);
206
- const safeWidth = Math.max(Math.round(desiredWidth), 2);
207
- const safeHeight = Math.max(Math.round(desiredHeight), 2);
208
- const resolutionDirty = this.canvas.width !== safeWidth || this.canvas.height !== safeHeight;
209
- if (resolutionDirty) {
210
- this.canvas.width = safeWidth;
211
- this.canvas.height = safeHeight;
212
- }
213
- renderTextLines(this.paddingInternal.top, this.paddingInternal.left, lines, this.isRTL, this.context);
214
- this.updateProperties(resolutionDirty);
215
- this.resizeModeDirty = false;
216
- this.directionDirty = false;
217
- this.maxLineWidthDirty = false;
218
- this.paddingInternal.setDirtyFalse();
219
- this.setDimensionsDirtyFalse();
220
- this.setContentDirtyFalse();
216
+ this.setContent(lines, desiredWidth, desiredHeight);
217
+ return true;
221
218
  }
222
- tryToRenderTextBreakMode() {
223
- if (this.checkContentDirty()) {
224
- this.updateAutoPadding();
225
- }
226
- if (!this.colorInternal.dirty &&
227
- !this.resizeModeDirty &&
219
+ layoutBreakMode(layoutChanged) {
220
+ if (!this.resizeModeDirty &&
228
221
  !this.directionDirty &&
229
222
  !this.maxLineWidthDirty &&
223
+ !this.lineBoxDirty &&
230
224
  !this.paddingInternal.dirty &&
231
- !this.consumeDimensionsChange() &&
225
+ !(layoutChanged &&
226
+ (this.lastWidth !== Math.round(this.width) || this.lastHeight !== Math.round(this.height))) &&
232
227
  !this.checkContentDirty()) {
233
- return;
228
+ return false;
234
229
  }
235
230
  const textChunks = this.buildTextChunks();
236
231
  const { size: desiredTextSize } = calculateTextContentParameters(textChunks, this.maxLineWidthInternal, this.isRTL);
237
- const paddingV = this.paddingInternal.top + this.paddingInternal.bottom;
238
- const paddingH = this.paddingInternal.left + this.paddingInternal.right;
239
- this.height = desiredTextSize.height + paddingV;
240
- this.width = desiredTextSize.width + paddingH;
241
- const { lines: realTextLines, size: realTextSize } = calculateTextContentParameters(textChunks, Math.round(this.width) - paddingH, this.isRTL);
242
- this.height = realTextSize.height + paddingV;
243
- this.width = realTextSize.width + paddingH;
244
- const safeWidth = Math.max(Math.round(this.width), 2);
245
- const safeHeight = Math.max(Math.round(this.height), 2);
246
- const resolutionDirty = this.canvas.width !== safeWidth || this.canvas.height !== safeHeight;
247
- if (resolutionDirty) {
248
- this.canvas.width = safeWidth;
249
- this.canvas.height = safeHeight;
250
- }
251
- renderTextLines(this.paddingInternal.top, this.paddingInternal.left, realTextLines, this.isRTL, this.context);
252
- this.updateProperties(resolutionDirty);
253
- this.resizeModeDirty = false;
254
- this.directionDirty = false;
255
- this.maxLineWidthDirty = false;
256
- this.paddingInternal.setDirtyFalse();
257
- this.setDimensionsDirtyFalse();
258
- this.setContentDirtyFalse();
232
+ const desiredSize = this.elementSize(desiredTextSize);
233
+ this.updateContentAspect(desiredSize.width / desiredSize.height);
234
+ this.height = desiredSize.height;
235
+ this.width = desiredSize.width;
236
+ const { lines: realTextLines, size: realTextSize } = calculateTextContentParameters(textChunks, Math.round(this.width) - this.paddingInternal.left - this.paddingInternal.right, this.isRTL);
237
+ const realSize = this.elementSize(realTextSize);
238
+ this.height = realSize.height;
239
+ this.width = realSize.width;
240
+ this.setContent(realTextLines, this.width, this.height);
241
+ return true;
259
242
  }
260
- updateProperties(resolutionDirty) {
261
- let properties;
262
- if (this.colorInternal.dirty) {
263
- properties ?? (properties = {});
264
- properties["color"] = this.colorInternal;
265
- this.colorInternal.setDirtyFalse();
266
- }
267
- if (resolutionDirty) {
268
- this.texture.dispose();
269
- this.texture = createCanvasTexture(this.canvas);
270
- properties ?? (properties = {});
271
- properties["texture"] = this.texture;
272
- }
273
- this.texture.needsUpdate = true;
274
- if (properties) {
275
- this.sceneWrapper.setProperties(this.planeHandler, properties);
243
+ /**
244
+ * Reports a new content aspect. Called before the element suggests its size, so constraints
245
+ * following the aspect are updated by then.
246
+ * @param aspect Aspect of the text laid out at `maxLineWidth`
247
+ */
248
+ updateContentAspect(aspect) {
249
+ if (aspect !== this.contentAspectInternal) {
250
+ this.contentAspectInternal = aspect;
251
+ this.signalContentAspectChangedInternal.emit(aspect);
276
252
  }
277
253
  }
278
- updateAutoPadding() {
279
- if (!this.autoPaddingInternal) {
280
- return;
281
- }
254
+ /**
255
+ * Keeps the result of a layout.
256
+ * @param lines Laid out lines
257
+ * @param width Width of the content with its padding in pixels
258
+ * @param height Height of the content with its padding in pixels
259
+ */
260
+ setContent(lines, width, height) {
261
+ this.textLines = lines;
262
+ this.contentWidth = Math.max(Math.round(width), TEXT_MIN_DIMENSION);
263
+ this.contentHeight = Math.max(Math.round(height), TEXT_MIN_DIMENSION);
264
+ }
265
+ /** The element size for a text size: the padding added, at least the minimum each way */
266
+ elementSize(textSize) {
282
267
  const { left, right, top, bottom } = this.paddingInternal;
283
- const last = this.lastAutoPadding;
284
- if (!Number.isNaN(last) &&
285
- (left !== last || right !== last || top !== last || bottom !== last)) {
268
+ return {
269
+ width: Math.max(textSize.width + left + right, TEXT_MIN_DIMENSION),
270
+ height: Math.max(textSize.height + top + bottom, TEXT_MIN_DIMENSION),
271
+ };
272
+ }
273
+ updateAutoPadding() {
274
+ if (!this.autoPadding) {
286
275
  this.autoPaddingInternal = false;
287
276
  return;
288
277
  }
289
278
  const padding = this.content.reduce((result, span) => Math.max(result, span.style.calculatePadding(this.commonStyle)), this.commonStyle.calculatePadding());
290
279
  this.paddingInternal.setUnified(padding);
291
- this.lastAutoPadding = padding;
280
+ this.autoPaddingVersion = this.paddingInternal.version;
292
281
  }
293
282
  buildTextChunks() {
294
- if (this.directionDirty || this.checkContentDirty()) {
283
+ if (this.directionDirty || this.lineBoxDirty || this.checkContentDirty()) {
295
284
  this.isRTL =
296
285
  this.directionInternal === UITextDirection.AUTO
297
286
  ? detectIsRTL(this.contentInternal)
298
287
  : this.directionInternal === UITextDirection.RTL;
299
- this.textChunks = buildTextChunks(this.context, this.contentInternal, this.commonStyle, this.isRTL);
288
+ this.textChunks = buildTextChunks(this.surface.context, this.contentInternal, this.commonStyle, this.isRTL, this.lineBoxInternal);
300
289
  }
301
290
  return this.textChunks;
302
291
  }
303
- consumeDimensionsChange() {
304
- return (this.consumeLayoutChange() &&
305
- (this.lastWidth !== Math.round(this.width) || this.lastHeight !== Math.round(this.height)));
306
- }
307
292
  setDimensionsDirtyFalse() {
308
293
  this.lastWidth = Math.round(this.width);
309
294
  this.lastHeight = Math.round(this.height);
@@ -35,7 +35,7 @@ export type { UIDummyOptions, UIInputEventData } from "./elements/UIDummy/UIDumm
35
35
  export { UIDummy } from "./elements/UIDummy/UIDummy.js";
36
36
  export type { UIElementOptions } from "./elements/UIElement/UIElement.Internal.js";
37
37
  export { UIElement } from "./elements/UIElement/UIElement.js";
38
- export type { UIGraphicsOptions } from "./elements/UIGraphics/UIGraphics.Internal.js";
38
+ export type { UIGraphicsCornerRadius, UIGraphicsOptions, UIGraphicsStrokeConfig, UIGraphicsStyle, } from "./elements/UIGraphics/UIGraphics.Internal.js";
39
39
  export { UIGraphics } from "./elements/UIGraphics/UIGraphics.js";
40
40
  export type { UIImageOptions } from "./elements/UIImage/UIImage.Internal.js";
41
41
  export { UIImage } from "./elements/UIImage/UIImage.js";
@@ -49,7 +49,7 @@ export { UISceneUpdateMode } from "./elements/UIScene/UIScene.Internal.js";
49
49
  export { UIScene } from "./elements/UIScene/UIScene.js";
50
50
  export type { UITextContent } from "./elements/UIText/UIText.Interfaces.js";
51
51
  export type { UITextOptions } from "./elements/UIText/UIText.Internal.js";
52
- export { UITextDirection, UITextResizeMode } from "./elements/UIText/UIText.Internal.js";
52
+ export { UITextDirection, UITextLineBox, UITextResizeMode, } from "./elements/UIText/UIText.Internal.js";
53
53
  export { UIText } from "./elements/UIText/UIText.js";
54
54
  export type { UILayerMode, UILayerOptions, UILayerOrientation, } from "./layers/UILayer/UILayer.Internal.js";
55
55
  export { UILayer } from "./layers/UILayer/UILayer.js";
@@ -78,6 +78,11 @@ export type { Vector2Like } from "./miscellaneous/math.js";
78
78
  export type { UIMicroConfig } from "./miscellaneous/micro/UIMicro.Internal.js";
79
79
  export { UIMicroAnchorMode } from "./miscellaneous/micro/UIMicro.Internal.js";
80
80
  export { UIMicro } from "./miscellaneous/micro/UIMicro.js";
81
+ export type { UIGradientStop, UILinearGradientOptions, UIRadialGradientOptions, } from "./miscellaneous/paint/UIGradient.Internal.js";
82
+ export { UIGradient } from "./miscellaneous/paint/UIGradient.js";
83
+ export { UILinearGradient } from "./miscellaneous/paint/UILinearGradient.js";
84
+ export type { UILineCap, UILineJoin, UIPaint, UIPaintBox, UIPaintConfig, } from "./miscellaneous/paint/UIPaint.Internal.js";
85
+ export { UIRadialGradient } from "./miscellaneous/paint/UIRadialGradient.js";
81
86
  export { UIResizePolicy } from "./miscellaneous/resize-policy/UIResizePolicy.js";
82
87
  export { UIResizePolicyCover } from "./miscellaneous/resize-policy/UIResizePolicyCover.js";
83
88
  export { UIResizePolicyCross } from "./miscellaneous/resize-policy/UIResizePolicyCross.js";
@@ -86,10 +91,11 @@ export { UIResizePolicyFit } from "./miscellaneous/resize-policy/UIResizePolicyF
86
91
  export { UIResizePolicyFixedHeight } from "./miscellaneous/resize-policy/UIResizePolicyFixedHeight.js";
87
92
  export { UIResizePolicyFixedWidth } from "./miscellaneous/resize-policy/UIResizePolicyFixedWidth.js";
88
93
  export { UIResizePolicyNone } from "./miscellaneous/resize-policy/UIResizePolicyNone.js";
89
- export type { UILayerElement, UIPlaneElement, UIPointElement } from "./miscellaneous/shared.js";
94
+ export type { UIContentAspectElement, UILayerElement, UIPlaneElement, UIPointElement, } from "./miscellaneous/shared.js";
90
95
  export type { UITextSpanConfig } from "./miscellaneous/text-span/UITextSpan.Internal.js";
91
96
  export { UITextSpan } from "./miscellaneous/text-span/UITextSpan.js";
92
- export type { UITextAlign, UITextStyleConfig, } from "./miscellaneous/text-style/UITextStyle.Internal.js";
97
+ export type { UITextAlign, UITextFontStyle, UITextFontWeight, UITextShadow, UITextShadowConfig, UITextStroke, UITextStrokeConfig, UITextStyleConfig, } from "./miscellaneous/text-style/UITextStyle.Internal.js";
98
+ export { UITextPaintBox } from "./miscellaneous/text-style/UITextStyle.Internal.js";
93
99
  export { UITextStyle } from "./miscellaneous/text-style/UITextStyle.js";
94
100
  export type { UITextureAtlasConfig, UITextureConfig, UITextureRect, UITextureSize, UITextureTrim, } from "./miscellaneous/texture/UITextureView.Internal.js";
95
101
  export { UITextureView } from "./miscellaneous/texture/UITextureView.js";
@@ -25,7 +25,7 @@ export { UINineSlice } from "./elements/UINineSlice/UINineSlice.js";
25
25
  export { UIProgress } from "./elements/UIProgress/UIProgress.js";
26
26
  export { UISceneUpdateMode } from "./elements/UIScene/UIScene.Internal.js";
27
27
  export { UIScene } from "./elements/UIScene/UIScene.js";
28
- export { UITextDirection, UITextResizeMode } from "./elements/UIText/UIText.Internal.js";
28
+ export { UITextDirection, UITextLineBox, UITextResizeMode, } from "./elements/UIText/UIText.Internal.js";
29
29
  export { UIText } from "./elements/UIText/UIText.js";
30
30
  export { UILayer } from "./layers/UILayer/UILayer.js";
31
31
  export { UIViewportLayer } from "./layers/UIViewportLayer/UIViewportLayer.js";
@@ -45,6 +45,9 @@ export { UIProgressMaskFunctionCircular } from "./miscellaneous/mask-function/UI
45
45
  export { UIProgressMaskFunctionDirectional } from "./miscellaneous/mask-function/UIProgressMaskFunctionDirectional.js";
46
46
  export { UIMicroAnchorMode } from "./miscellaneous/micro/UIMicro.Internal.js";
47
47
  export { UIMicro } from "./miscellaneous/micro/UIMicro.js";
48
+ export { UIGradient } from "./miscellaneous/paint/UIGradient.js";
49
+ export { UILinearGradient } from "./miscellaneous/paint/UILinearGradient.js";
50
+ export { UIRadialGradient } from "./miscellaneous/paint/UIRadialGradient.js";
48
51
  export { UIResizePolicy } from "./miscellaneous/resize-policy/UIResizePolicy.js";
49
52
  export { UIResizePolicyCover } from "./miscellaneous/resize-policy/UIResizePolicyCover.js";
50
53
  export { UIResizePolicyCross } from "./miscellaneous/resize-policy/UIResizePolicyCross.js";
@@ -54,6 +57,7 @@ export { UIResizePolicyFixedHeight } from "./miscellaneous/resize-policy/UIResiz
54
57
  export { UIResizePolicyFixedWidth } from "./miscellaneous/resize-policy/UIResizePolicyFixedWidth.js";
55
58
  export { UIResizePolicyNone } from "./miscellaneous/resize-policy/UIResizePolicyNone.js";
56
59
  export { UITextSpan } from "./miscellaneous/text-span/UITextSpan.js";
60
+ export { UITextPaintBox } from "./miscellaneous/text-style/UITextStyle.Internal.js";
57
61
  export { UITextStyle } from "./miscellaneous/text-style/UITextStyle.js";
58
62
  export { UITextureView } from "./miscellaneous/texture/UITextureView.js";
59
63
  export { UIAffineMatrix3 } from "./miscellaneous/UIAffineMatrix3.js";
@@ -24,6 +24,7 @@ export declare abstract class UILayer implements UIPlaneElement {
24
24
  private readonly sceneWrapperInternal;
25
25
  private modeInternal;
26
26
  private orientationInternal;
27
+ private pixelDensityInternal;
27
28
  private readonly signalOrientationChangedInternal;
28
29
  private readonly signalModeChangedInternal;
29
30
  private readonly signalRenderingInternal;
@@ -73,12 +74,12 @@ export declare abstract class UILayer implements UIPlaneElement {
73
74
  get signalResized(): FerrsignView3<number, number, UILayer>;
74
75
  /**
75
76
  * Signal fired when the WebGL context of the renderer the layer is drawn with is lost. The
76
- * layer draws nothing until the context is restored; pause what depends on frames being shown.
77
+ * layer draws nothing until the context is restored, so pause what depends on frames being shown.
77
78
  */
78
79
  get signalContextLost(): FerrsignView1<UILayer>;
79
80
  /**
80
81
  * Signal fired when the WebGL context is restored. The renderer has recreated its GL state by
81
- * then; content that lives on the GPU only, such as render targets, has to be redrawn.
82
+ * then. Content that lives on the GPU only, such as render targets, has to be redrawn.
82
83
  */
83
84
  get signalContextRestored(): FerrsignView1<UILayer>;
84
85
  /**
@@ -96,6 +97,12 @@ export declare abstract class UILayer implements UIPlaneElement {
96
97
  get maskRegistry(): readonly UIMask[];
97
98
  /** Whether the layer is destroyed */
98
99
  get destroyed(): boolean;
100
+ /**
101
+ * Screen pixels per world unit, as of the last render. Canvas-drawn elements and `UIScene`
102
+ * rasterize at this density, so their content stays sharp on high-DPI screens. 1 for a layer
103
+ * that does not know its screen size.
104
+ */
105
+ get pixelDensity(): number;
99
106
  /**
100
107
  * Updates visibility and interactivity mode. A layer that stops taking input cancels the
101
108
  * pressed pointers: their releases would never arrive.
@@ -108,18 +115,25 @@ export declare abstract class UILayer implements UIPlaneElement {
108
115
  */
109
116
  destroy(): void;
110
117
  protected resizeInternal(width: number, height: number): void;
118
+ /** Sets the screen pixels per world unit, read by elements on their next render */
119
+ protected setPixelDensity(value: number): void;
111
120
  protected renderInternal(renderer: WebGLRenderer, deltaTime: number): void;
112
121
  /**
113
122
  * Cancels every pressed pointer, for its elements and for the layer: no release of those
114
123
  * pointers will be reported.
115
124
  */
116
125
  protected cancelPointers(): void;
117
- /** Follows the context of the renderer the layer is drawn with; null to stop following */
126
+ /** Follows the context of the renderer the layer is drawn with, or stops following with null */
118
127
  private bindContextMonitor;
119
128
  private readonly onContextLost;
120
129
  private readonly onContextRestored;
121
130
  /** A removal signal handler may destroy other constraints, so the registry is walked over a copy */
122
131
  private destroyConstraints;
132
+ /**
133
+ * Reports constraints the solver rejected for conflicting with required constraints. Throws in
134
+ * development builds. Warns in production builds, where the layout goes on without them.
135
+ */
136
+ private reportRejectedConstraints;
123
137
  /**
124
138
  * Called for each pointer the layer cancels, before its elements are told: the subclass that
125
139
  * takes the input reports the cancel to the users of the layer.
@@ -14,7 +14,10 @@ export class UILayer {
14
14
  /** Optional layer name */
15
15
  this.name = "";
16
16
  this.inputWrapperInternal = new UIInputWrapper();
17
- this.solverWrapperInternal = new UISolverWrapper();
17
+ this.solverWrapperInternal = new UISolverWrapper((constraints) => {
18
+ this.reportRejectedConstraints(constraints);
19
+ });
20
+ this.pixelDensityInternal = 1;
18
21
  this.signalOrientationChangedInternal = new Ferrsign2();
19
22
  this.signalModeChangedInternal = new Ferrsign2();
20
23
  this.signalRenderingInternal = new Ferrsign3();
@@ -124,14 +127,14 @@ export class UILayer {
124
127
  }
125
128
  /**
126
129
  * Signal fired when the WebGL context of the renderer the layer is drawn with is lost. The
127
- * layer draws nothing until the context is restored; pause what depends on frames being shown.
130
+ * layer draws nothing until the context is restored, so pause what depends on frames being shown.
128
131
  */
129
132
  get signalContextLost() {
130
133
  return this.signalContextLostInternal;
131
134
  }
132
135
  /**
133
136
  * Signal fired when the WebGL context is restored. The renderer has recreated its GL state by
134
- * then; content that lives on the GPU only, such as render targets, has to be redrawn.
137
+ * then. Content that lives on the GPU only, such as render targets, has to be redrawn.
135
138
  */
136
139
  get signalContextRestored() {
137
140
  return this.signalContextRestoredInternal;
@@ -163,6 +166,14 @@ export class UILayer {
163
166
  get destroyed() {
164
167
  return this.destroyedInternal;
165
168
  }
169
+ /**
170
+ * Screen pixels per world unit, as of the last render. Canvas-drawn elements and `UIScene`
171
+ * rasterize at this density, so their content stays sharp on high-DPI screens. 1 for a layer
172
+ * that does not know its screen size.
173
+ */
174
+ get pixelDensity() {
175
+ return this.pixelDensityInternal;
176
+ }
166
177
  /**
167
178
  * Updates visibility and interactivity mode. A layer that stops taking input cancels the
168
179
  * pressed pointers: their releases would never arrive.
@@ -276,6 +287,11 @@ export class UILayer {
276
287
  this.signalOrientationChangedInternal.emit(this.orientationInternal, this);
277
288
  }
278
289
  }
290
+ /** Sets the screen pixels per world unit, read by elements on their next render */
291
+ setPixelDensity(value) {
292
+ assertValidPositiveNumber(value, "UILayer.setPixelDensity.value");
293
+ this.pixelDensityInternal = value;
294
+ }
279
295
  renderInternal(renderer, deltaTime) {
280
296
  if (this.destroyedInternal) {
281
297
  return;
@@ -304,7 +320,7 @@ export class UILayer {
304
320
  this.reportPointerCancelledByLayer(x, y, identifier);
305
321
  });
306
322
  }
307
- /** Follows the context of the renderer the layer is drawn with; null to stop following */
323
+ /** Follows the context of the renderer the layer is drawn with, or stops following with null */
308
324
  bindContextMonitor(monitor) {
309
325
  if (monitor === this.contextMonitor) {
310
326
  return;
@@ -323,4 +339,19 @@ export class UILayer {
323
339
  }
324
340
  }
325
341
  }
342
+ /**
343
+ * Reports constraints the solver rejected for conflicting with required constraints. Throws in
344
+ * development builds. Warns in production builds, where the layout goes on without them.
345
+ */
346
+ reportRejectedConstraints(constraints) {
347
+ const names = constraints.map((constraint) => constraint.name === ""
348
+ ? constraint.constructor.name
349
+ : `${constraint.constructor.name} "${constraint.name}"`);
350
+ const message = `UILayer "${this.name}": constraints conflict with required (P0) constraints and are not applied: ${names.join(", ")}`;
351
+ if (process.env.NODE_ENV !== "production") {
352
+ throw new Error(message);
353
+ }
354
+ // eslint-disable-next-line no-console -- The only report of a broken layout in production builds
355
+ console.warn(message);
356
+ }
326
357
  }
@@ -16,6 +16,8 @@ export declare class UIViewportLayer extends UILayer {
16
16
  private resizePolicyInternal;
17
17
  /** Policy version the layer was last sized by; -1 forces a resize */
18
18
  private appliedPolicyVersion;
19
+ /** World units per viewport pixel the layer was last sized by */
20
+ private appliedScale;
19
21
  /** Pointers the layer cancelled while pressed: their late release is not reported */
20
22
  private readonly abandonedPointers;
21
23
  private readonly signalPointerPressedInternal;