laymur 0.16.0 → 0.17.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.
- package/.claude/settings.local.json +9 -0
- package/.editorconfig +14 -0
- package/CLAUDE.md +181 -0
- package/README.md +84 -208
- package/assets/logotype.png +0 -0
- package/eslint.config.js +111 -0
- package/package.json +20 -36
- package/rollup.config.js +52 -0
- package/{dist/constraints/UIAspectConstraint/UIAspectConstraint.Internal.d.ts → src/core/constraints/UIAspectConstraint/UIAspectConstraint.Internal.ts} +3 -2
- package/src/core/constraints/UIAspectConstraint/UIAspectConstraint.ts +60 -0
- package/src/core/constraints/UIConstraint.ts +20 -0
- package/src/core/constraints/UICustomConstraint.ts +62 -0
- package/{dist/constraints/UIHeightConstraint/UIHeightConstraint.Internal.d.ts → src/core/constraints/UIHeightConstraint/UIHeightConstraint.Internal.ts} +3 -2
- package/src/core/constraints/UIHeightConstraint/UIHeightConstraint.ts +52 -0
- package/src/core/constraints/UIHorizontalDistanceConstraint/UIHorizontalDistanceConstraint.Internal.ts +13 -0
- package/src/core/constraints/UIHorizontalDistanceConstraint/UIHorizontalDistanceConstraint.ts +139 -0
- package/src/core/constraints/UIHorizontalInterpolationConstraint/UIHorizontalInterpolationConstraint.Internal.ts +16 -0
- package/src/core/constraints/UIHorizontalInterpolationConstraint/UIHorizontalInterpolationConstraint.ts +189 -0
- package/{dist/constraints/UIHorizontalProportionConstraint/UIHorizontalProportionConstraint.Internal.d.ts → src/core/constraints/UIHorizontalProportionConstraint/UIHorizontalProportionConstraint.Internal.ts} +3 -2
- package/src/core/constraints/UIHorizontalProportionConstraint/UIHorizontalProportionConstraint.ts +68 -0
- package/{dist/constraints/UISingleParameterConstraint/UISingleParameterConstraint.Internal.d.ts → src/core/constraints/UISingleParameterConstraint/UISingleParameterConstraint.Internal.ts} +9 -8
- package/src/core/constraints/UISingleParameterConstraint/UISingleParameterConstraint.ts +94 -0
- package/src/core/constraints/UIVerticalDistanceConstraint/UIVerticalDistanceConstraint.Internal.ts +13 -0
- package/src/core/constraints/UIVerticalDistanceConstraint/UIVerticalDistanceConstraint.ts +135 -0
- package/src/core/constraints/UIVerticalInterpolationConstraint/UIVerticalInterpolationConstraint.Internal.ts +16 -0
- package/src/core/constraints/UIVerticalInterpolationConstraint/UIVerticalInterpolationConstraint.ts +189 -0
- package/{dist/constraints/UIVerticalProportionConstraint/UIVerticalProportionConstraint.Internal.d.ts → src/core/constraints/UIVerticalProportionConstraint/UIVerticalProportionConstraint.Internal.ts} +3 -2
- package/src/core/constraints/UIVerticalProportionConstraint/UIVerticalProportionConstraint.ts +68 -0
- package/{dist/constraints/UIWidthConstraint/UIWidthConstraint.Internal.d.ts → src/core/constraints/UIWidthConstraint/UIWidthConstraint.Internal.ts} +3 -2
- package/src/core/constraints/UIWidthConstraint/UIWidthConstraint.ts +52 -0
- package/src/core/constraints/builders/UIConstraint2DBuilder.Internal.ts +84 -0
- package/src/core/constraints/builders/UIConstraint2DBuilder.ts +168 -0
- package/src/core/constraints/builders/UICoverConstraintBuilder.Internal.ts +15 -0
- package/src/core/constraints/builders/UICoverConstraintBuilder.ts +84 -0
- package/src/core/constraints/builders/UIFitConstraintBuilder.Internal.ts +15 -0
- package/src/core/constraints/builders/UIFitConstraintBuilder.ts +84 -0
- package/src/core/elements/UIAnchor/UIAnchor.Internal.ts +13 -0
- package/src/core/elements/UIAnchor/UIAnchor.ts +70 -0
- package/src/core/elements/UIAnimatedImage/UIAnimatedImage.Internal.ts +30 -0
- package/src/core/elements/UIAnimatedImage/UIAnimatedImage.ts +355 -0
- package/src/core/elements/UIDummy/UIDummy.Internal.ts +33 -0
- package/src/core/elements/UIDummy/UIDummy.ts +244 -0
- package/{dist/elements/UIElement/UIElement.Internal.d.ts → src/core/elements/UIElement/UIElement.Internal.ts} +7 -5
- package/src/core/elements/UIElement/UIElement.ts +127 -0
- package/{dist/elements/UIGraphics/UIGraphics.Internal.d.ts → src/core/elements/UIGraphics/UIGraphics.Internal.ts} +7 -5
- package/src/core/elements/UIGraphics/UIGraphics.ts +168 -0
- package/{dist/elements/UIImage/UIImage.Internal.d.ts → src/core/elements/UIImage/UIImage.Internal.ts} +3 -2
- package/src/core/elements/UIImage/UIImage.ts +138 -0
- package/src/core/elements/UINineSlice/UINineSlice.Internal.ts +26 -0
- package/src/core/elements/UINineSlice/UINineSlice.ts +296 -0
- package/src/core/elements/UIProgress/UIProgress.Internal.ts +24 -0
- package/src/core/elements/UIProgress/UIProgress.ts +259 -0
- package/src/core/elements/UIScene/UIScene.Internal.ts +46 -0
- package/src/core/elements/UIScene/UIScene.ts +259 -0
- package/{dist/elements/UIText/UIText.Interfaces.d.ts → src/core/elements/UIText/UIText.Interfaces.ts} +21 -16
- package/src/core/elements/UIText/UIText.Internal.ts +29 -0
- package/src/core/elements/UIText/UIText.Measuring.ts +183 -0
- package/src/core/elements/UIText/UIText.Rendering.ts +83 -0
- package/src/core/elements/UIText/UIText.ts +376 -0
- package/{dist/layers/UIFullscreenLayer/UIFullscreenLayer.Internal.d.ts → src/core/layers/UIFullscreenLayer/UIFullscreenLayer.Internal.ts} +14 -11
- package/src/core/layers/UIFullscreenLayer/UIFullscreenLayer.ts +233 -0
- package/{dist/layers/UILayer/UILayer.Internal.d.ts → src/core/layers/UILayer/UILayer.Internal.ts} +15 -11
- package/src/core/layers/UILayer/UILayer.ts +156 -0
- package/src/core/miscellaneous/UIExpression.ts +142 -0
- package/src/core/miscellaneous/UIMode.ts +29 -0
- package/src/core/miscellaneous/UIOrientation.ts +18 -0
- package/src/core/miscellaneous/UIPriority.ts +63 -0
- package/src/core/miscellaneous/UIRelation.ts +39 -0
- package/src/core/miscellaneous/UITransparencyMode.ts +9 -0
- package/src/core/miscellaneous/area/UIArea.ts +5 -0
- package/src/core/miscellaneous/area/UIAreaCircle.ts +32 -0
- package/src/core/miscellaneous/area/UIAreaPolygon.ts +50 -0
- package/src/core/miscellaneous/area/UIAreaRectangle.ts +38 -0
- package/src/core/miscellaneous/area/UIAreaRoundedRectangle.ts +78 -0
- package/src/core/miscellaneous/asserts.ts +187 -0
- package/src/core/miscellaneous/canvas.ts +73 -0
- package/src/core/miscellaneous/color/UIColor.Internal.ts +89 -0
- package/src/core/miscellaneous/color/UIColor.ts +844 -0
- package/src/core/miscellaneous/computeTransform.ts +127 -0
- package/src/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.Internal.ts +235 -0
- package/src/core/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.ts +524 -0
- package/{dist/miscellaneous/generic-plane/UIGenericPlaneRegistry/UIGenericPlaneRegistry.Internal.d.ts → src/core/miscellaneous/generic-plane/UIGenericPlaneRegistry/UIGenericPlaneRegistry.Internal.ts} +10 -8
- package/src/core/miscellaneous/generic-plane/UIGenericPlaneRegistry/UIGenericPlaneRegistry.ts +448 -0
- package/src/core/miscellaneous/generic-plane/shared.ts +323 -0
- package/src/core/miscellaneous/insets/UIInsets.Internal.ts +9 -0
- package/src/core/miscellaneous/insets/UIInsets.ts +248 -0
- package/src/core/miscellaneous/mask-function/UIProgressMaskFunction.ts +24 -0
- package/src/core/miscellaneous/mask-function/UIProgressMaskFunctionCircular.ts +58 -0
- package/src/core/miscellaneous/mask-function/UIProgressMaskFunctionDirectional.ts +52 -0
- package/src/core/miscellaneous/math.ts +16 -0
- package/src/core/miscellaneous/micro/UIMicro.Internal.ts +33 -0
- package/src/core/miscellaneous/micro/UIMicro.ts +222 -0
- package/src/core/miscellaneous/resize-policy/UIResizePolicy.ts +8 -0
- package/src/core/miscellaneous/resize-policy/UIResizePolicyCover.ts +57 -0
- package/src/core/miscellaneous/resize-policy/UIResizePolicyCross.ts +65 -0
- package/src/core/miscellaneous/resize-policy/UIResizePolicyCrossInverted.ts +65 -0
- package/src/core/miscellaneous/resize-policy/UIResizePolicyFit.ts +57 -0
- package/src/core/miscellaneous/resize-policy/UIResizePolicyFixedHeight.ts +65 -0
- package/src/core/miscellaneous/resize-policy/UIResizePolicyFixedWidth.ts +65 -0
- package/src/core/miscellaneous/resize-policy/UIResizePolicyNone.ts +11 -0
- package/src/core/miscellaneous/shared.ts +73 -0
- package/{dist/miscellaneous/text-span/UITextSpan.Internal.d.ts → src/core/miscellaneous/text-span/UITextSpan.Internal.ts} +5 -4
- package/src/core/miscellaneous/text-span/UITextSpan.ts +43 -0
- package/src/core/miscellaneous/text-style/UITextStyle.Internal.ts +52 -0
- package/src/core/miscellaneous/text-style/UITextStyle.ts +479 -0
- package/src/core/miscellaneous/texture/UITextureView.Internal.ts +55 -0
- package/src/core/miscellaneous/texture/UITextureView.ts +308 -0
- package/src/core/miscellaneous/webglCapabilities.ts +129 -0
- package/src/core/shaders/UIImage.glsl +10 -0
- package/src/core/shaders/UINineSlice.glsl +53 -0
- package/src/core/shaders/UIProgress.glsl +15 -0
- package/src/core/shaders/UIProgressMaskFunctionCircular.glsl +13 -0
- package/src/core/shaders/UIProgressMaskFunctionDirectional.glsl +12 -0
- package/src/core/shaders/glsl.d.ts +27 -0
- package/src/core/wrappers/UIInputWrapper/UIInputWrapper.Internal.ts +23 -0
- package/src/core/wrappers/UIInputWrapper/UIInputWrapper.ts +124 -0
- package/src/core/wrappers/UISceneWrapper/UISceneWrapper.Internal.ts +22 -0
- package/src/core/wrappers/UISceneWrapper/UISceneWrapper.ts +101 -0
- package/src/core/wrappers/UISolverWrapper/UISolverWrapper.Internal.ts +58 -0
- package/src/core/wrappers/UISolverWrapper/UISolverWrapper.ts +605 -0
- package/src/particles/behaviorModules/UIBehaviorDirectionalGravity.ts +70 -0
- package/src/particles/behaviorModules/UIBehaviorModule.ts +20 -0
- package/src/particles/behaviorModules/UIBehaviorPointGravity.ts +173 -0
- package/src/particles/behaviorModules/UIBehaviorScaleOverLife.ts +104 -0
- package/src/particles/behaviorModules/UIBehaviorTorqueDamping.ts +75 -0
- package/src/particles/behaviorModules/UIBehaviorTorqueNoise.ts +115 -0
- package/src/particles/behaviorModules/UIBehaviorVelocityDamping.ts +77 -0
- package/src/particles/behaviorModules/UIBehaviorVelocityNoise.ts +118 -0
- package/src/particles/elements/UIEmitter.Internal.ts +22 -0
- package/src/particles/elements/UIEmitter.ts +313 -0
- package/src/particles/index.ts +18 -0
- package/src/particles/instancedParticle/UIInstancedParticle.Internal.ts +263 -0
- package/src/particles/instancedParticle/UIInstancedParticle.ts +138 -0
- package/src/particles/miscellaneous/miscellaneous.ts +115 -0
- package/src/particles/renderingModules/UIRenderingColorOverLife.ts +39 -0
- package/src/particles/renderingModules/UIRenderingColorOverVelocity.ts +49 -0
- package/src/particles/renderingModules/UIRenderingModule.ts +17 -0
- package/src/particles/renderingModules/UIRenderingTexture.ts +30 -0
- package/src/particles/shaders/UIRenderingColorOverLife.glsl +4 -0
- package/src/particles/shaders/UIRenderingColorOverVelocity.glsl +6 -0
- package/src/particles/shaders/UIRenderingTexture.glsl +4 -0
- package/src/particles/spawnModules/UISpawnModule.ts +22 -0
- package/src/particles/spawnModules/UISpawnOffset.ts +62 -0
- package/src/particles/spawnModules/UISpawnRandomLifetime.ts +68 -0
- package/src/particles/spawnModules/UISpawnRandomRotation.ts +60 -0
- package/src/particles/spawnModules/UISpawnRandomScale.ts +81 -0
- package/src/particles/spawnModules/UISpawnRandomTorque.ts +62 -0
- package/src/particles/spawnModules/UISpawnRandomVelocity.ts +92 -0
- package/src/particles/spawnModules/UISpawnRectangle.ts +81 -0
- package/tsconfig.json +29 -0
- package/dist/constraints/UIAspectConstraint/UIAspectConstraint.d.ts +0 -22
- package/dist/constraints/UIConstraint.d.ts +0 -12
- package/dist/constraints/UICustomConstraint.d.ts +0 -28
- package/dist/constraints/UIHeightConstraint/UIHeightConstraint.d.ts +0 -20
- package/dist/constraints/UIHorizontalDistanceConstraint/UIHorizontalDistanceConstraint.Internal.d.ts +0 -11
- package/dist/constraints/UIHorizontalDistanceConstraint/UIHorizontalDistanceConstraint.d.ts +0 -36
- package/dist/constraints/UIHorizontalInterpolationConstraint/UIHorizontalInterpolationConstraint.Internal.d.ts +0 -14
- package/dist/constraints/UIHorizontalInterpolationConstraint/UIHorizontalInterpolationConstraint.d.ts +0 -44
- package/dist/constraints/UIHorizontalProportionConstraint/UIHorizontalProportionConstraint.d.ts +0 -22
- package/dist/constraints/UISingleParameterConstraint/UISingleParameterConstraint.d.ts +0 -43
- package/dist/constraints/UIVerticalDistanceConstraint/UIVerticalDistanceConstraint.Internal.d.ts +0 -11
- package/dist/constraints/UIVerticalDistanceConstraint/UIVerticalDistanceConstraint.d.ts +0 -32
- package/dist/constraints/UIVerticalInterpolationConstraint/UIVerticalInterpolationConstraint.Internal.d.ts +0 -14
- package/dist/constraints/UIVerticalInterpolationConstraint/UIVerticalInterpolationConstraint.d.ts +0 -44
- package/dist/constraints/UIVerticalProportionConstraint/UIVerticalProportionConstraint.d.ts +0 -22
- package/dist/constraints/UIWidthConstraint/UIWidthConstraint.d.ts +0 -20
- package/dist/constraints/builders/UIConstraint2DBuilder.Internal.d.ts +0 -64
- package/dist/constraints/builders/UIConstraint2DBuilder.d.ts +0 -46
- package/dist/constraints/builders/UICoverConstraintBuilder.Internal.d.ts +0 -13
- package/dist/constraints/builders/UICoverConstraintBuilder.d.ts +0 -29
- package/dist/constraints/builders/UIFitConstraintBuilder.Internal.d.ts +0 -13
- package/dist/constraints/builders/UIFitConstraintBuilder.d.ts +0 -29
- package/dist/elements/UIAnchor/UIAnchor.Internal.d.ts +0 -9
- package/dist/elements/UIAnchor/UIAnchor.d.ts +0 -32
- package/dist/elements/UIAnimatedImage/UIAnimatedImage.Internal.d.ts +0 -27
- package/dist/elements/UIAnimatedImage/UIAnimatedImage.d.ts +0 -77
- package/dist/elements/UIDummy/UIDummy.Internal.d.ts +0 -30
- package/dist/elements/UIDummy/UIDummy.d.ts +0 -82
- package/dist/elements/UIElement/UIElement.d.ts +0 -38
- package/dist/elements/UIGraphics/UIGraphics.d.ts +0 -51
- package/dist/elements/UIImage/UIImage.d.ts +0 -32
- package/dist/elements/UINineSlice/UINineSlice.Internal.d.ts +0 -23
- package/dist/elements/UINineSlice/UINineSlice.d.ts +0 -60
- package/dist/elements/UIProgress/UIProgress.Internal.d.ts +0 -14
- package/dist/elements/UIProgress/UIProgress.d.ts +0 -45
- package/dist/elements/UIScene/UIScene.Internal.d.ts +0 -43
- package/dist/elements/UIScene/UIScene.d.ts +0 -59
- package/dist/elements/UIText/UIText.Internal.d.ts +0 -26
- package/dist/elements/UIText/UIText.Measuring.d.ts +0 -50
- package/dist/elements/UIText/UIText.Rendering.d.ts +0 -20
- package/dist/elements/UIText/UIText.d.ts +0 -71
- package/dist/index.js +0 -9246
- package/dist/index.js.map +0 -1
- package/dist/index.min.js +0 -2
- package/dist/index.min.js.map +0 -1
- package/dist/layers/UIFullscreenLayer/UIFullscreenLayer.d.ts +0 -63
- package/dist/layers/UILayer/UILayer.d.ts +0 -49
- package/dist/miscellaneous/UIExpression.d.ts +0 -69
- package/dist/miscellaneous/UIMode.d.ts +0 -23
- package/dist/miscellaneous/UIOrientation.d.ts +0 -15
- package/dist/miscellaneous/UIPriority.d.ts +0 -32
- package/dist/miscellaneous/UIRelation.d.ts +0 -23
- package/dist/miscellaneous/UITransparencyMode.d.ts +0 -9
- package/dist/miscellaneous/area/UIArea.d.ts +0 -3
- package/dist/miscellaneous/area/UIAreaCircle.d.ts +0 -15
- package/dist/miscellaneous/area/UIAreaPolygon.d.ts +0 -12
- package/dist/miscellaneous/area/UIAreaRectangle.d.ts +0 -17
- package/dist/miscellaneous/area/UIAreaRoundedRectangle.d.ts +0 -21
- package/dist/miscellaneous/asserts.d.ts +0 -43
- package/dist/miscellaneous/canvas.d.ts +0 -31
- package/dist/miscellaneous/color/UIColor.Internal.d.ts +0 -7
- package/dist/miscellaneous/color/UIColor.d.ts +0 -234
- package/dist/miscellaneous/computeTransform.d.ts +0 -18
- package/dist/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.Internal.d.ts +0 -19
- package/dist/miscellaneous/generic-plane/UIGenericInstancedPlane/UIGenericInstancedPlane.d.ts +0 -1
- package/dist/miscellaneous/generic-plane/UIGenericPlaneRegistry/UIGenericPlaneRegistry.d.ts +0 -1
- package/dist/miscellaneous/generic-plane/shared.d.ts +0 -38
- package/dist/miscellaneous/insets/UIInsets.Internal.d.ts +0 -11
- package/dist/miscellaneous/insets/UIInsets.d.ts +0 -48
- package/dist/miscellaneous/mask-function/UIProgressMaskFunction.d.ts +0 -9
- package/dist/miscellaneous/mask-function/UIProgressMaskFunctionCircular.d.ts +0 -21
- package/dist/miscellaneous/mask-function/UIProgressMaskFunctionDirectional.d.ts +0 -16
- package/dist/miscellaneous/math.d.ts +0 -5
- package/dist/miscellaneous/micro/UIMicro.Internal.d.ts +0 -31
- package/dist/miscellaneous/micro/UIMicro.d.ts +0 -59
- package/dist/miscellaneous/resize-policy/UIResizePolicy.d.ts +0 -3
- package/dist/miscellaneous/resize-policy/UIResizePolicyCover.d.ts +0 -22
- package/dist/miscellaneous/resize-policy/UIResizePolicyCross.d.ts +0 -22
- package/dist/miscellaneous/resize-policy/UIResizePolicyCrossInverted.d.ts +0 -22
- package/dist/miscellaneous/resize-policy/UIResizePolicyFit.d.ts +0 -22
- package/dist/miscellaneous/resize-policy/UIResizePolicyFixedHeight.d.ts +0 -22
- package/dist/miscellaneous/resize-policy/UIResizePolicyFixedWidth.d.ts +0 -22
- package/dist/miscellaneous/resize-policy/UIResizePolicyNone.d.ts +0 -5
- package/dist/miscellaneous/shared.d.ts +0 -46
- package/dist/miscellaneous/text-span/UITextSpan.d.ts +0 -14
- package/dist/miscellaneous/text-style/UITextStyle.Internal.d.ts +0 -48
- package/dist/miscellaneous/text-style/UITextStyle.d.ts +0 -85
- package/dist/miscellaneous/texture/UITextureView.Internal.d.ts +0 -49
- package/dist/miscellaneous/texture/UITextureView.d.ts +0 -67
- package/dist/miscellaneous/webglCapabilities.d.ts +0 -2
- package/dist/wrappers/UIInputWrapper/UIInputWrapper.Internal.d.ts +0 -17
- package/dist/wrappers/UIInputWrapper/UIInputWrapper.d.ts +0 -13
- package/dist/wrappers/UISceneWrapper/UISceneWrapper.Internal.d.ts +0 -13
- package/dist/wrappers/UISceneWrapper/UISceneWrapper.d.ts +0 -20
- package/dist/wrappers/UISolverWrapper/UISolverWrapper.Internal.d.ts +0 -49
- package/dist/wrappers/UISolverWrapper/UISolverWrapper.d.ts +0 -215
- /package/{dist/index.d.ts → src/core/index.ts} +0 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import type { UICanvasRenderingContext2D } from "../../miscellaneous/canvas";
|
|
2
|
+
import type { UITextSpan } from "../../miscellaneous/text-span/UITextSpan";
|
|
3
|
+
import { UITextStyle } from "../../miscellaneous/text-style/UITextStyle";
|
|
4
|
+
import type { UITextStyleConfig } from "../../miscellaneous/text-style/UITextStyle.Internal";
|
|
5
|
+
import type { UIChunkMetrics, UITextChunk, UITextLine, UITextSize } from "./UIText.Interfaces";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Splits text into renderable chunks.
|
|
9
|
+
* @param text Text to split
|
|
10
|
+
* @returns Array of words, spaces, and newlines
|
|
11
|
+
*/
|
|
12
|
+
export function splitTextIntoChunks(text: string): string[] {
|
|
13
|
+
return text.match(/\S+|\s|\n/g) ?? [];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Measures single text chunk.
|
|
18
|
+
* @param context Canvas context for measurement
|
|
19
|
+
* @param text Chunk text
|
|
20
|
+
* @param textStyle Resolved text style
|
|
21
|
+
* @returns Chunk metrics
|
|
22
|
+
*/
|
|
23
|
+
export function measureTextChunk(
|
|
24
|
+
context: UICanvasRenderingContext2D,
|
|
25
|
+
text: string,
|
|
26
|
+
textStyle: UITextStyleConfig,
|
|
27
|
+
): UIChunkMetrics {
|
|
28
|
+
context.font = `${textStyle.fontStyle} ${textStyle.fontWeight} ${textStyle.fontSize}px "${textStyle.fontFamily}"`;
|
|
29
|
+
|
|
30
|
+
if (text === "\n") {
|
|
31
|
+
return {
|
|
32
|
+
width: Math.ceil(1),
|
|
33
|
+
height: textStyle.lineHeight,
|
|
34
|
+
baseline: 0,
|
|
35
|
+
lineHeight: textStyle.lineHeight,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const metrics = context.measureText(text);
|
|
40
|
+
const width = Math.ceil(metrics.width);
|
|
41
|
+
const height = Math.ceil(metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent);
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
width,
|
|
45
|
+
height,
|
|
46
|
+
baseline: metrics.actualBoundingBoxDescent,
|
|
47
|
+
lineHeight: textStyle.lineHeight,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Builds text lines with word wrapping.
|
|
53
|
+
* @param maxWidth Maximum line width in pixels
|
|
54
|
+
* @param textChunks Measured text chunks
|
|
55
|
+
* @returns Array of text lines
|
|
56
|
+
*/
|
|
57
|
+
export function buildTextLines(maxWidth: number, textChunks: UITextChunk[]): UITextLine[] {
|
|
58
|
+
const lines: UITextLine[] = [];
|
|
59
|
+
let currentLine: UITextChunk[] = [];
|
|
60
|
+
let currentWidth = 0;
|
|
61
|
+
let currentHeight = 0;
|
|
62
|
+
let currentBaseline = 0;
|
|
63
|
+
let currentLineHeight = 0;
|
|
64
|
+
|
|
65
|
+
for (const textChunk of textChunks) {
|
|
66
|
+
if (textChunk.text === "\n") {
|
|
67
|
+
if (currentLine.length > 0) {
|
|
68
|
+
lines.push({
|
|
69
|
+
chunks: currentLine,
|
|
70
|
+
width: currentWidth,
|
|
71
|
+
height: currentHeight,
|
|
72
|
+
baseline: currentBaseline,
|
|
73
|
+
lineHeight: currentLineHeight,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
currentLine = [];
|
|
77
|
+
currentWidth = 0;
|
|
78
|
+
currentHeight = 0;
|
|
79
|
+
currentBaseline = 0;
|
|
80
|
+
currentLineHeight = 0;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (currentLine.length === 0 || currentWidth + textChunk.metrics.width <= maxWidth) {
|
|
85
|
+
currentLine.push(textChunk);
|
|
86
|
+
currentWidth += textChunk.metrics.width;
|
|
87
|
+
currentHeight = Math.max(currentHeight, textChunk.metrics.height);
|
|
88
|
+
currentBaseline = Math.max(currentBaseline, textChunk.metrics.baseline);
|
|
89
|
+
currentLineHeight = Math.max(currentLineHeight, textChunk.metrics.lineHeight);
|
|
90
|
+
} else {
|
|
91
|
+
lines.push({
|
|
92
|
+
chunks: currentLine,
|
|
93
|
+
width: currentWidth,
|
|
94
|
+
height: currentHeight,
|
|
95
|
+
baseline: currentBaseline,
|
|
96
|
+
lineHeight: currentLineHeight,
|
|
97
|
+
});
|
|
98
|
+
currentLine = [textChunk];
|
|
99
|
+
currentWidth = textChunk.metrics.width;
|
|
100
|
+
currentHeight = textChunk.metrics.height;
|
|
101
|
+
currentBaseline = textChunk.metrics.baseline;
|
|
102
|
+
currentLineHeight = textChunk.metrics.lineHeight;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (currentLine.length > 0) {
|
|
107
|
+
lines.push({
|
|
108
|
+
chunks: currentLine,
|
|
109
|
+
width: currentWidth,
|
|
110
|
+
height: currentHeight,
|
|
111
|
+
baseline: currentBaseline,
|
|
112
|
+
lineHeight: currentLineHeight,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return lines;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Calculates total text size from lines.
|
|
121
|
+
* @param lines Text lines
|
|
122
|
+
* @returns Total width and height
|
|
123
|
+
*/
|
|
124
|
+
export function calculateTextSize(lines: UITextLine[]): UITextSize {
|
|
125
|
+
if (lines.length === 0) {
|
|
126
|
+
return { width: 0, height: 0 };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const maxLineWidth = Math.max(...lines.map((line) => line.width));
|
|
130
|
+
let totalHeight = 0;
|
|
131
|
+
|
|
132
|
+
for (let i = 0; i < lines.length; i++) {
|
|
133
|
+
const line = lines[i];
|
|
134
|
+
totalHeight += i === 0 ? line.height : line.lineHeight;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return { width: maxLineWidth, height: totalHeight };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Builds measured text chunks from spans.
|
|
142
|
+
* @param context Canvas context for measurement
|
|
143
|
+
* @param textSpans Text spans with styles
|
|
144
|
+
* @param commonStyle Fallback style
|
|
145
|
+
* @returns Measured text chunks
|
|
146
|
+
*/
|
|
147
|
+
export function buildTextChunks(
|
|
148
|
+
context: UICanvasRenderingContext2D,
|
|
149
|
+
textSpans: UITextSpan[],
|
|
150
|
+
commonStyle: UITextStyle,
|
|
151
|
+
): UITextChunk[] {
|
|
152
|
+
const textChunks: UITextChunk[] = [];
|
|
153
|
+
|
|
154
|
+
for (const textSpan of textSpans) {
|
|
155
|
+
const textStyle = UITextStyle.resolve(textSpan.style, commonStyle);
|
|
156
|
+
|
|
157
|
+
for (const textChunk of splitTextIntoChunks(textSpan.text)) {
|
|
158
|
+
const textChunkMetrics = measureTextChunk(context, textChunk, textStyle);
|
|
159
|
+
textChunks.push({
|
|
160
|
+
text: textChunk,
|
|
161
|
+
style: textStyle,
|
|
162
|
+
metrics: textChunkMetrics,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return textChunks;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Calculates text layout parameters.
|
|
172
|
+
* @param textChunks Measured text chunks
|
|
173
|
+
* @param maxLineWidth Maximum line width in pixels
|
|
174
|
+
* @returns Lines and total size
|
|
175
|
+
*/
|
|
176
|
+
export function calculateTextContentParameters(
|
|
177
|
+
textChunks: UITextChunk[],
|
|
178
|
+
maxLineWidth: number,
|
|
179
|
+
): { lines: UITextLine[]; size: UITextSize } {
|
|
180
|
+
const lines = buildTextLines(maxLineWidth, textChunks);
|
|
181
|
+
const size = calculateTextSize(lines);
|
|
182
|
+
return { lines, size };
|
|
183
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { UICanvasRenderingContext2D } from "../../miscellaneous/canvas";
|
|
2
|
+
import type { UITextStyleConfig } from "../../miscellaneous/text-style/UITextStyle.Internal";
|
|
3
|
+
import { TEXT_STYLE_DEFAULT_ALIGN } from "../../miscellaneous/text-style/UITextStyle.Internal";
|
|
4
|
+
import type { UITextLine } from "./UIText.Interfaces";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Renders single text chunk.
|
|
8
|
+
* @param x X coordinate in pixels
|
|
9
|
+
* @param y Y coordinate in pixels
|
|
10
|
+
* @param text Chunk text
|
|
11
|
+
* @param style Resolved text style
|
|
12
|
+
* @param context Canvas rendering context
|
|
13
|
+
*/
|
|
14
|
+
export function renderTextChunk(
|
|
15
|
+
x: number,
|
|
16
|
+
y: number,
|
|
17
|
+
text: string,
|
|
18
|
+
style: UITextStyleConfig,
|
|
19
|
+
context: UICanvasRenderingContext2D,
|
|
20
|
+
): void {
|
|
21
|
+
context.font = `${style.fontStyle} ${style.fontWeight} ${style.fontSize}px "${style.fontFamily}"`;
|
|
22
|
+
|
|
23
|
+
if (style.enableShadow) {
|
|
24
|
+
context.shadowOffsetX = style.shadowOffsetX;
|
|
25
|
+
context.shadowOffsetY = style.shadowOffsetY;
|
|
26
|
+
context.shadowBlur = style.shadowBlur;
|
|
27
|
+
context.shadowColor = style.shadowColor;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (style.enableStroke) {
|
|
31
|
+
context.strokeStyle = style.strokeColor;
|
|
32
|
+
context.lineWidth = style.strokeThickness;
|
|
33
|
+
context.strokeText(text, x, y);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
context.fillStyle = style.color;
|
|
37
|
+
context.fillText(text, x, y);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Renders text lines to canvas.
|
|
42
|
+
* @param paddingTop Top padding in pixels
|
|
43
|
+
* @param paddingLeft Left padding in pixels
|
|
44
|
+
* @param lines Text lines to render
|
|
45
|
+
* @param context Canvas rendering context
|
|
46
|
+
*/
|
|
47
|
+
export function renderTextLines(
|
|
48
|
+
paddingTop: number,
|
|
49
|
+
paddingLeft: number,
|
|
50
|
+
lines: UITextLine[],
|
|
51
|
+
context: UICanvasRenderingContext2D,
|
|
52
|
+
): void {
|
|
53
|
+
let currentY = paddingTop;
|
|
54
|
+
|
|
55
|
+
let textWidth = 0;
|
|
56
|
+
for (const line of lines) {
|
|
57
|
+
textWidth = Math.max(line.width, textWidth);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
for (let i = 0; i < lines.length; i++) {
|
|
61
|
+
const line = lines[i];
|
|
62
|
+
|
|
63
|
+
const firstAlign = line.chunks[0]?.style.align ?? TEXT_STYLE_DEFAULT_ALIGN;
|
|
64
|
+
const isUniformAlign = line.chunks.every((c) => c.style.align === firstAlign);
|
|
65
|
+
const lineAlign = isUniformAlign ? firstAlign : TEXT_STYLE_DEFAULT_ALIGN;
|
|
66
|
+
|
|
67
|
+
let offsetX = 0;
|
|
68
|
+
|
|
69
|
+
if (lineAlign === "center") {
|
|
70
|
+
offsetX = (textWidth - line.width) / 2;
|
|
71
|
+
} else if (lineAlign === "right") {
|
|
72
|
+
offsetX = textWidth - line.width;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let currentX = paddingLeft + offsetX;
|
|
76
|
+
currentY += i === 0 ? line.height : line.lineHeight;
|
|
77
|
+
|
|
78
|
+
for (const chunk of line.chunks) {
|
|
79
|
+
renderTextChunk(currentX, currentY, chunk.text, chunk.style, context);
|
|
80
|
+
currentX += chunk.metrics.width;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
import type { CanvasTexture, WebGLRenderer } from "three";
|
|
2
|
+
import { Matrix3 } from "three";
|
|
3
|
+
import type { UILayer } from "../../layers/UILayer/UILayer";
|
|
4
|
+
import { UIColor } from "../../miscellaneous/color/UIColor";
|
|
5
|
+
import type { UIColorConfig } from "../../miscellaneous/color/UIColor.Internal";
|
|
6
|
+
import type { UIProperty } from "../../miscellaneous/generic-plane/shared";
|
|
7
|
+
import { UIInsets } from "../../miscellaneous/insets/UIInsets";
|
|
8
|
+
import type { UIInsetsConfig } from "../../miscellaneous/insets/UIInsets.Internal";
|
|
9
|
+
import { UITextSpan } from "../../miscellaneous/text-span/UITextSpan";
|
|
10
|
+
import { UITextStyle } from "../../miscellaneous/text-style/UITextStyle";
|
|
11
|
+
import source from "../../shaders/UIImage.glsl";
|
|
12
|
+
import { UIElement } from "../UIElement/UIElement";
|
|
13
|
+
import type { UICanvas, UICanvasRenderingContext2D } from "../../miscellaneous/canvas";
|
|
14
|
+
import { createCanvas, createCanvasTexture } from "../../miscellaneous/canvas";
|
|
15
|
+
import type { UITextChunk, UITextContent } from "./UIText.Interfaces";
|
|
16
|
+
import type { UITextOptions } from "./UIText.Internal";
|
|
17
|
+
import {
|
|
18
|
+
TEXT_DEFAULT_MAX_LINE_WIDTH,
|
|
19
|
+
TEXT_DEFAULT_RESIZE_MODE,
|
|
20
|
+
UITextResizeMode,
|
|
21
|
+
} from "./UIText.Internal";
|
|
22
|
+
import { buildTextChunks, calculateTextContentParameters } from "./UIText.Measuring";
|
|
23
|
+
import { renderTextLines } from "./UIText.Rendering";
|
|
24
|
+
|
|
25
|
+
/** Canvas-based text rendering element */
|
|
26
|
+
export class UIText extends UIElement {
|
|
27
|
+
/** Default style applied to all text spans */
|
|
28
|
+
public readonly commonStyle: UITextStyle;
|
|
29
|
+
|
|
30
|
+
private readonly colorInternal: UIColor;
|
|
31
|
+
private readonly paddingInternal: UIInsets;
|
|
32
|
+
private readonly canvas: UICanvas;
|
|
33
|
+
private readonly context: UICanvasRenderingContext2D;
|
|
34
|
+
private texture: CanvasTexture;
|
|
35
|
+
|
|
36
|
+
private contentInternal: UITextSpan[] = [];
|
|
37
|
+
private contentDirty = false;
|
|
38
|
+
|
|
39
|
+
private maxLineWidthInternal: number;
|
|
40
|
+
private maxLineWidthDirty = false;
|
|
41
|
+
|
|
42
|
+
private resizeModeInternal: UITextResizeMode;
|
|
43
|
+
private resizeModeDirty = false;
|
|
44
|
+
|
|
45
|
+
private lastWidth: number;
|
|
46
|
+
private lastHeight: number;
|
|
47
|
+
|
|
48
|
+
private textChunks: UITextChunk[] = [];
|
|
49
|
+
private textChunksDirty = false;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Creates a new UIText instance.
|
|
53
|
+
*
|
|
54
|
+
* @param layer - Layer containing this element
|
|
55
|
+
* @param content - Text content as string or styled spans
|
|
56
|
+
* @param options - Configuration options
|
|
57
|
+
*/
|
|
58
|
+
constructor(layer: UILayer, content: UITextContent, options: Partial<UITextOptions> = {}) {
|
|
59
|
+
const canvas = createCanvas(2, 2);
|
|
60
|
+
const context = canvas.getContext("2d") as UICanvasRenderingContext2D | null;
|
|
61
|
+
|
|
62
|
+
if (!context) {
|
|
63
|
+
throw new Error("UIText.constructor: failed to create canvas context");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const texture = createCanvasTexture(canvas);
|
|
67
|
+
texture.needsUpdate = true;
|
|
68
|
+
const color = new UIColor(options.color);
|
|
69
|
+
|
|
70
|
+
super(layer, source, {
|
|
71
|
+
texture: texture,
|
|
72
|
+
textureTransform: new Matrix3(),
|
|
73
|
+
color,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
this.colorInternal = color;
|
|
77
|
+
this.paddingInternal = new UIInsets(options.padding);
|
|
78
|
+
this.commonStyle = new UITextStyle(options.commonStyle);
|
|
79
|
+
this.maxLineWidthInternal = options.maxLineWidth ?? TEXT_DEFAULT_MAX_LINE_WIDTH;
|
|
80
|
+
this.resizeModeInternal = options.resizeMode ?? TEXT_DEFAULT_RESIZE_MODE;
|
|
81
|
+
|
|
82
|
+
this.canvas = canvas;
|
|
83
|
+
this.context = context;
|
|
84
|
+
this.texture = texture;
|
|
85
|
+
this.content = content;
|
|
86
|
+
|
|
87
|
+
this.lastWidth = this.width;
|
|
88
|
+
this.lastHeight = this.height;
|
|
89
|
+
|
|
90
|
+
if (options.padding === undefined) {
|
|
91
|
+
this.paddingInternal.setUnified(
|
|
92
|
+
this.content.reduce(
|
|
93
|
+
(a, b) => Math.max(a, b.style.calculatePadding()),
|
|
94
|
+
this.commonStyle.calculatePadding(),
|
|
95
|
+
),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
switch (this.resizeModeInternal) {
|
|
100
|
+
case UITextResizeMode.SCALE:
|
|
101
|
+
this.tryToRenderTextScaleMode();
|
|
102
|
+
break;
|
|
103
|
+
|
|
104
|
+
case UITextResizeMode.BREAK:
|
|
105
|
+
this.tryToRenderTextBreakMode();
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Multiplicative tint. Alpha channel controls opacity. */
|
|
111
|
+
public get color(): UIColor {
|
|
112
|
+
return this.colorInternal;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Text content as array of styled spans */
|
|
116
|
+
public get content(): UITextSpan[] {
|
|
117
|
+
return this.contentInternal;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Maximum line width before wrapping in pixels */
|
|
121
|
+
public get maxLineWidth(): number {
|
|
122
|
+
return this.maxLineWidthInternal;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Text padding in world units */
|
|
126
|
+
public get padding(): UIInsets {
|
|
127
|
+
return this.paddingInternal;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Controls how text adapts to element size */
|
|
131
|
+
public get resizeMode(): UITextResizeMode {
|
|
132
|
+
return this.resizeModeInternal;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Multiplicative tint. Alpha channel controls opacity. */
|
|
136
|
+
public set color(value: UIColorConfig) {
|
|
137
|
+
this.colorInternal.set(value);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Text content as string, span config, or array */
|
|
141
|
+
public set content(value: UITextContent) {
|
|
142
|
+
if (this.contentInternal !== value) {
|
|
143
|
+
if (typeof value === "string") {
|
|
144
|
+
this.contentInternal = [new UITextSpan({ text: value, style: new UITextStyle() })];
|
|
145
|
+
} else if (Array.isArray(value)) {
|
|
146
|
+
this.contentInternal = value.map((span) =>
|
|
147
|
+
typeof span === "string"
|
|
148
|
+
? new UITextSpan({ text: span, style: new UITextStyle() })
|
|
149
|
+
: new UITextSpan({
|
|
150
|
+
text: span.text,
|
|
151
|
+
style: new UITextStyle(span.style),
|
|
152
|
+
}),
|
|
153
|
+
);
|
|
154
|
+
} else {
|
|
155
|
+
this.contentInternal = [
|
|
156
|
+
new UITextSpan({
|
|
157
|
+
text: value.text,
|
|
158
|
+
style: new UITextStyle(value.style),
|
|
159
|
+
}),
|
|
160
|
+
];
|
|
161
|
+
}
|
|
162
|
+
this.contentDirty = true;
|
|
163
|
+
this.textChunksDirty = true;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Maximum line width before wrapping in pixels */
|
|
168
|
+
public set maxLineWidth(value: number) {
|
|
169
|
+
if (this.maxLineWidthInternal !== value) {
|
|
170
|
+
this.maxLineWidthInternal = value;
|
|
171
|
+
this.maxLineWidthDirty = true;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Text padding in world units */
|
|
176
|
+
public set padding(value: UIInsetsConfig) {
|
|
177
|
+
this.paddingInternal.set(value);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Controls how text adapts to size constraints */
|
|
181
|
+
public set resizeMode(value: UITextResizeMode) {
|
|
182
|
+
if (this.resizeModeInternal !== value) {
|
|
183
|
+
this.resizeModeInternal = value;
|
|
184
|
+
this.resizeModeDirty = true;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Removes element and frees resources */
|
|
189
|
+
public override destroy(): void {
|
|
190
|
+
this.texture.dispose();
|
|
191
|
+
super.destroy();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
protected override onWillRender(renderer: WebGLRenderer, deltaTime: number): void {
|
|
195
|
+
switch (this.resizeModeInternal) {
|
|
196
|
+
case UITextResizeMode.SCALE:
|
|
197
|
+
this.tryToRenderTextScaleMode();
|
|
198
|
+
break;
|
|
199
|
+
|
|
200
|
+
case UITextResizeMode.BREAK:
|
|
201
|
+
this.tryToRenderTextBreakMode();
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
super.onWillRender(renderer, deltaTime);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private tryToRenderTextScaleMode(): void {
|
|
209
|
+
if (
|
|
210
|
+
!this.colorInternal.dirty &&
|
|
211
|
+
!this.resizeModeDirty &&
|
|
212
|
+
!this.maxLineWidthDirty &&
|
|
213
|
+
!this.paddingInternal.dirty &&
|
|
214
|
+
!(
|
|
215
|
+
this.solverWrapper.dirty &&
|
|
216
|
+
this.lastWidth / this.lastHeight !== Math.round(this.width) / Math.round(this.height)
|
|
217
|
+
) &&
|
|
218
|
+
!this.checkContentDirty()
|
|
219
|
+
) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const textChunks = this.buildTextChunks();
|
|
224
|
+
|
|
225
|
+
const { lines, size } = calculateTextContentParameters(textChunks, this.maxLineWidthInternal);
|
|
226
|
+
|
|
227
|
+
const paddingV = this.paddingInternal.top + this.paddingInternal.bottom;
|
|
228
|
+
const paddingH = this.paddingInternal.left + this.paddingInternal.right;
|
|
229
|
+
|
|
230
|
+
const desiredHeight = size.height + paddingV;
|
|
231
|
+
const desiredWidth = size.width + paddingH;
|
|
232
|
+
|
|
233
|
+
// Two calculation passes are required to cover both cases -
|
|
234
|
+
// when the width is specified by a constraint and cannot be changed,
|
|
235
|
+
// or when the height is specified by a constraint and cannot be changed,
|
|
236
|
+
// but the width still has priority, because it is specified first and last.
|
|
237
|
+
this.width = desiredWidth;
|
|
238
|
+
this.height = (desiredHeight / desiredWidth) * Math.round(this.width);
|
|
239
|
+
this.width = (desiredWidth / desiredHeight) * Math.round(this.height);
|
|
240
|
+
|
|
241
|
+
const resolutionDirty = this.lastWidth !== desiredHeight || this.lastHeight !== desiredWidth;
|
|
242
|
+
|
|
243
|
+
if (resolutionDirty) {
|
|
244
|
+
this.canvas.height = desiredHeight;
|
|
245
|
+
this.canvas.width = desiredWidth;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
renderTextLines(this.paddingInternal.top, this.paddingInternal.left, lines, this.context);
|
|
249
|
+
|
|
250
|
+
this.updateProperties(resolutionDirty);
|
|
251
|
+
|
|
252
|
+
this.resizeModeDirty = false;
|
|
253
|
+
this.maxLineWidthDirty = false;
|
|
254
|
+
this.paddingInternal.setDirtyFalse();
|
|
255
|
+
this.setDimensionsDirtyFalse();
|
|
256
|
+
this.setContentDirtyFalse();
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private tryToRenderTextBreakMode(): void {
|
|
260
|
+
if (
|
|
261
|
+
!this.colorInternal.dirty &&
|
|
262
|
+
!this.resizeModeDirty &&
|
|
263
|
+
!this.maxLineWidthDirty &&
|
|
264
|
+
!this.paddingInternal.dirty &&
|
|
265
|
+
!this.checkDimensionsDirty() &&
|
|
266
|
+
!this.checkContentDirty()
|
|
267
|
+
) {
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const textChunks = this.buildTextChunks();
|
|
272
|
+
|
|
273
|
+
// Two passes of text size calculation are needed to first
|
|
274
|
+
// suggest the desired sizes to the solver, and then
|
|
275
|
+
// adjust our expectations based on the result obtained.
|
|
276
|
+
|
|
277
|
+
const { size: desiredTextSize } = calculateTextContentParameters(
|
|
278
|
+
textChunks,
|
|
279
|
+
this.maxLineWidthInternal,
|
|
280
|
+
);
|
|
281
|
+
|
|
282
|
+
const paddingV = this.paddingInternal.top + this.paddingInternal.bottom;
|
|
283
|
+
const paddingH = this.paddingInternal.left + this.paddingInternal.right;
|
|
284
|
+
|
|
285
|
+
this.height = desiredTextSize.height + paddingV;
|
|
286
|
+
this.width = desiredTextSize.width + paddingH; // Width should remain a priority, so it is set last.
|
|
287
|
+
|
|
288
|
+
const { lines: realTextLines, size: realTextSize } = calculateTextContentParameters(
|
|
289
|
+
textChunks,
|
|
290
|
+
Math.round(this.width) - paddingH,
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
this.height = realTextSize.height + paddingV;
|
|
294
|
+
this.width = realTextSize.width + paddingH;
|
|
295
|
+
|
|
296
|
+
const safeWidth = Math.max(Math.round(this.width), 2);
|
|
297
|
+
const safeHeight = Math.max(Math.round(this.height), 2);
|
|
298
|
+
|
|
299
|
+
const resolutionDirty = this.lastWidth !== safeWidth || this.lastHeight !== safeHeight;
|
|
300
|
+
|
|
301
|
+
if (resolutionDirty) {
|
|
302
|
+
this.canvas.width = safeWidth;
|
|
303
|
+
this.canvas.height = safeHeight;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
renderTextLines(
|
|
307
|
+
this.paddingInternal.top,
|
|
308
|
+
this.paddingInternal.left,
|
|
309
|
+
realTextLines,
|
|
310
|
+
this.context,
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
this.updateProperties(resolutionDirty);
|
|
314
|
+
|
|
315
|
+
this.resizeModeDirty = false;
|
|
316
|
+
this.maxLineWidthDirty = false;
|
|
317
|
+
this.paddingInternal.setDirtyFalse();
|
|
318
|
+
this.setDimensionsDirtyFalse();
|
|
319
|
+
this.setContentDirtyFalse();
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
private updateProperties(resolutionDirty: boolean): void {
|
|
323
|
+
let properties: Record<string, UIProperty> | undefined;
|
|
324
|
+
|
|
325
|
+
if (this.colorInternal.dirty) {
|
|
326
|
+
properties ??= {};
|
|
327
|
+
properties["color"] = this.colorInternal;
|
|
328
|
+
this.colorInternal.setDirtyFalse();
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (resolutionDirty) {
|
|
332
|
+
this.texture.dispose();
|
|
333
|
+
this.texture = createCanvasTexture(this.canvas);
|
|
334
|
+
this.texture.needsUpdate = true;
|
|
335
|
+
|
|
336
|
+
properties ??= {};
|
|
337
|
+
properties["texture"] = this.texture;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (properties) {
|
|
341
|
+
this.sceneWrapper.setProperties(this.planeHandler, properties);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
private buildTextChunks(): UITextChunk[] {
|
|
346
|
+
if (this.textChunksDirty) {
|
|
347
|
+
this.textChunks = buildTextChunks(this.context, this.contentInternal, this.commonStyle);
|
|
348
|
+
this.textChunksDirty = false;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
return this.textChunks;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
private checkDimensionsDirty(): boolean {
|
|
355
|
+
return (
|
|
356
|
+
this.solverWrapper.dirty &&
|
|
357
|
+
(this.lastWidth !== Math.round(this.width) || this.lastHeight !== Math.round(this.height))
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
private setDimensionsDirtyFalse(): void {
|
|
362
|
+
this.lastWidth = Math.round(this.width);
|
|
363
|
+
this.lastHeight = Math.round(this.height);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
private checkContentDirty(): boolean {
|
|
367
|
+
return this.contentDirty || this.content.some((span): boolean => span.dirty);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
private setContentDirtyFalse(): void {
|
|
371
|
+
this.contentDirty = false;
|
|
372
|
+
for (const span of this.content) {
|
|
373
|
+
span.setDirtyFalse();
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
@@ -2,20 +2,23 @@ import type { UIResizePolicy } from "../../miscellaneous/resize-policy/UIResizeP
|
|
|
2
2
|
import { UIResizePolicyNone } from "../../miscellaneous/resize-policy/UIResizePolicyNone";
|
|
3
3
|
import type { UILayerOptions } from "../UILayer/UILayer.Internal";
|
|
4
4
|
import type { UIFullscreenLayer } from "./UIFullscreenLayer";
|
|
5
|
+
|
|
5
6
|
/** Fullscreen layer input event data */
|
|
6
7
|
export interface UIFullscreenLayerInputEventData {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
/** X coordinate */
|
|
9
|
+
x: number;
|
|
10
|
+
/** Y coordinate */
|
|
11
|
+
y: number;
|
|
12
|
+
/** Pointer identifier */
|
|
13
|
+
identifier: number;
|
|
14
|
+
/** Layer that emitted the event */
|
|
15
|
+
layer: UIFullscreenLayer;
|
|
15
16
|
}
|
|
17
|
+
|
|
16
18
|
/** Configuration for fullscreen layer */
|
|
17
19
|
export interface UIFullscreenLayerOptions extends Omit<UILayerOptions, "width" | "height"> {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
/** Strategy for handling window resize */
|
|
21
|
+
resizePolicy: UIResizePolicy;
|
|
20
22
|
}
|
|
21
|
-
|
|
23
|
+
|
|
24
|
+
export const FULLSCREEN_LAYER_DEFAULT_RESIZE_POLICY = new UIResizePolicyNone();
|