laymur 0.2.3 → 0.2.5

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/README.md CHANGED
@@ -37,6 +37,10 @@ npm install laymur
37
37
  - Three.js ^0.175.0 (peer dependency)
38
38
  - Modern JavaScript environment with ES2020+ support
39
39
 
40
+ ## Live Examples
41
+
42
+ 🎮 **[Live Examples](https://jango-git.github.io/laymur/)** - Explore constraint-based layouts, proportional positioning, and responsive UI components in action.
43
+
40
44
  ## Quick Start
41
45
 
42
46
  ```typescript
@@ -96,6 +96,7 @@ export declare abstract class UISingleParameterConstraint extends UIConstraint {
96
96
  * @protected
97
97
  */
98
98
  protected isConstraintEnabled(): boolean;
99
+ private readonly onWillOrientationChange;
99
100
  /**
100
101
  * Event handler for layer orientation changes.
101
102
  *
@@ -0,0 +1,11 @@
1
+ import type { Texture, WebGLRenderer } from "three";
2
+ import { Mesh } from "three";
3
+ import type { UILayer } from "../layers/UILayer";
4
+ import { UIElement } from "./UIElement";
5
+ export declare class UINineSlice extends UIElement<Mesh> {
6
+ private readonly material;
7
+ private readonly textureInternal;
8
+ constructor(layer: UILayer, texture: Texture, x?: number, y?: number);
9
+ destroy(): void;
10
+ protected ["onBeforeRenderInternal"](renderer: WebGLRenderer, deltaTime: number): void;
11
+ }
package/dist/index.d.ts CHANGED
@@ -13,6 +13,7 @@ export * from "./elements/UIAnchor";
13
13
  export * from "./elements/UIDummy";
14
14
  export * from "./elements/UIElement";
15
15
  export * from "./elements/UIImage";
16
+ export * from "./elements/UINineSline";
16
17
  export * from "./elements/UIProgress";
17
18
  export * from "./elements/UIScene";
18
19
  export * from "./elements/UIText";
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Color, Scene, OrthographicCamera, MathUtils, ShaderMaterial, PlaneGeometry, Mesh, UniformsUtils, WebGLRenderTarget, UnsignedByteType, LinearFilter, RGBAFormat, PerspectiveCamera, CanvasTexture } from 'three';
1
+ import { Color, Scene, OrthographicCamera, MathUtils, ShaderMaterial, PlaneGeometry, Mesh, Vector2, Vector4, UniformsUtils, WebGLRenderTarget, UnsignedByteType, LinearFilter, RGBAFormat, PerspectiveCamera, CanvasTexture } from 'three';
2
2
 
3
3
  /**
4
4
  * Mathematical expression builder for constraint equations in the UI layout system.
@@ -2238,6 +2238,8 @@ class UISolverWrapper {
2238
2238
  */
2239
2239
  var UILayerEvent;
2240
2240
  (function (UILayerEvent) {
2241
+ /** Emitted when the layer's orientation is about to change between horizontal and vertical. */
2242
+ UILayerEvent["WILL_ORIENTATION_CHANGE"] = "will_orientation_change";
2241
2243
  /** Emitted when the layer's orientation changes between horizontal and vertical. */
2242
2244
  UILayerEvent["ORIENTATION_CHANGE"] = "orientation_change";
2243
2245
  /** Emitted when the layer's visibility/interaction mode changes. */
@@ -2347,6 +2349,7 @@ class UILayer extends i {
2347
2349
  this.sceneWrapper.resize(width, height);
2348
2350
  const orientation = width > height ? UIOrientation.HORIZONTAL : UIOrientation.VERTICAL;
2349
2351
  if (orientation !== this.orientationInternal) {
2352
+ this.emit(UILayerEvent.WILL_ORIENTATION_CHANGE, this.orientationInternal, this);
2350
2353
  this.orientationInternal = orientation;
2351
2354
  this.emit(UILayerEvent.ORIENTATION_CHANGE, this.orientationInternal, this);
2352
2355
  }
@@ -2465,6 +2468,11 @@ class UISingleParameterConstraint extends UIConstraint {
2465
2468
  */
2466
2469
  constructor(layer, priority, relation, orientation) {
2467
2470
  super(layer);
2471
+ this._private_onWillOrientationChange = () => {
2472
+ if (!this.isConstraintEnabled()) {
2473
+ this.solverWrapper.setConstraintEnabled(this.constraint, false);
2474
+ }
2475
+ };
2468
2476
  /**
2469
2477
  * Event handler for layer orientation changes.
2470
2478
  *
@@ -2474,12 +2482,15 @@ class UISingleParameterConstraint extends UIConstraint {
2474
2482
  * @private
2475
2483
  */
2476
2484
  this._private_onOrientationChange = () => {
2477
- this.solverWrapper.setConstraintEnabled(this.constraint, this.isConstraintEnabled());
2485
+ if (this.isConstraintEnabled()) {
2486
+ this.solverWrapper.setConstraintEnabled(this.constraint, true);
2487
+ }
2478
2488
  };
2479
2489
  this.priorityInternal = resolvePriority(priority);
2480
2490
  this.relationInternal = resolveRelation(relation);
2481
2491
  this.orientationInternal = resolveOrientation(orientation);
2482
2492
  this.layer.on(UILayerEvent.ORIENTATION_CHANGE, this._private_onOrientationChange);
2493
+ this.layer.on(UILayerEvent.WILL_ORIENTATION_CHANGE, this._private_onWillOrientationChange);
2483
2494
  }
2484
2495
  /**
2485
2496
  * Gets the current priority level of the constraint.
@@ -2541,6 +2552,7 @@ class UISingleParameterConstraint extends UIConstraint {
2541
2552
  */
2542
2553
  destroy() {
2543
2554
  this.layer.off(UILayerEvent.ORIENTATION_CHANGE, this._private_onOrientationChange);
2555
+ this.layer.off(UILayerEvent.WILL_ORIENTATION_CHANGE, this._private_onWillOrientationChange);
2544
2556
  this.solverWrapper.removeConstraint(this.constraint);
2545
2557
  }
2546
2558
  /**
@@ -3999,7 +4011,7 @@ class UIElement extends UIAnchor {
3999
4011
  deltaTime) { }
4000
4012
  }
4001
4013
 
4002
- const DEFAULT_ALPHA_TEST = 0.75;
4014
+ const DEFAULT_ALPHA_TEST$1 = 0.75;
4003
4015
  class UIMaterial extends ShaderMaterial {
4004
4016
  constructor(map) {
4005
4017
  super({
@@ -4007,7 +4019,7 @@ class UIMaterial extends ShaderMaterial {
4007
4019
  map: { value: map },
4008
4020
  opacity: { value: 1.0 },
4009
4021
  color: { value: new Color(1.0, 1.0, 1.0) },
4010
- alphaTest: { value: DEFAULT_ALPHA_TEST },
4022
+ alphaTest: { value: DEFAULT_ALPHA_TEST$1 },
4011
4023
  },
4012
4024
  vertexShader: /* glsl */ `
4013
4025
  varying vec2 vUv;
@@ -4031,7 +4043,7 @@ class UIMaterial extends ShaderMaterial {
4031
4043
  }
4032
4044
  `,
4033
4045
  transparent: false,
4034
- alphaTest: DEFAULT_ALPHA_TEST,
4046
+ alphaTest: DEFAULT_ALPHA_TEST$1,
4035
4047
  lights: false,
4036
4048
  fog: false,
4037
4049
  depthWrite: false,
@@ -4068,7 +4080,7 @@ class UIMaterial extends ShaderMaterial {
4068
4080
  setTransparency(value) {
4069
4081
  if (this.transparent !== value) {
4070
4082
  this.transparent = value;
4071
- this.uniforms.alphaTest.value = value ? DEFAULT_ALPHA_TEST : 0;
4083
+ this.uniforms.alphaTest.value = value ? DEFAULT_ALPHA_TEST$1 : 0;
4072
4084
  this.needsUpdate = true;
4073
4085
  this.uniformsNeedUpdate = true;
4074
4086
  }
@@ -4194,6 +4206,160 @@ class UIImage extends UIElement {
4194
4206
  }
4195
4207
  }
4196
4208
 
4209
+ const DEFAULT_ALPHA_TEST = 0.025;
4210
+ const DEFAULT_SLICE_BORDER = 0.25;
4211
+ class UINineSliceMaterial extends ShaderMaterial {
4212
+ constructor(texture) {
4213
+ super({
4214
+ uniforms: {
4215
+ map: { value: texture },
4216
+ color: { value: new Color(0xffffff) },
4217
+ opacity: { value: 1.0 },
4218
+ alphaTest: { value: DEFAULT_ALPHA_TEST },
4219
+ sliceBorders: {
4220
+ value: new Vector4(DEFAULT_SLICE_BORDER, DEFAULT_SLICE_BORDER, DEFAULT_SLICE_BORDER, DEFAULT_SLICE_BORDER),
4221
+ }, // left, right, top, bottom (в UV)
4222
+ atlasRect: { value: new Vector4(0, 0, 1, 1) }, // x, y, w, h (в UV)
4223
+ quadSize: { value: new Vector2(1, 1) },
4224
+ textureSize: {
4225
+ value: new Vector2(texture.image.width, texture.image.height),
4226
+ },
4227
+ },
4228
+ vertexShader: /* glsl */ `
4229
+ varying vec2 vUv;
4230
+ void main() {
4231
+ vUv = uv;
4232
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
4233
+ }
4234
+ `,
4235
+ fragmentShader: /* glsl */ `
4236
+ uniform sampler2D map;
4237
+ uniform vec3 color;
4238
+ uniform float opacity;
4239
+ uniform float alphaTest;
4240
+ uniform vec4 sliceBorders;
4241
+ uniform vec4 atlasRect;
4242
+ uniform vec2 quadSize;
4243
+ uniform vec2 textureSize;
4244
+ varying vec2 vUv;
4245
+
4246
+ vec2 calcNineSliceUV(vec2 uv) {
4247
+ vec2 localUV = uv;
4248
+
4249
+ float left = sliceBorders.x;
4250
+ float right = sliceBorders.y;
4251
+ float top = sliceBorders.z;
4252
+ float bottom = sliceBorders.w;
4253
+
4254
+ vec2 texPx = textureSize * atlasRect.zw;
4255
+
4256
+ float leftPx = left * texPx.x;
4257
+ float rightPx = right * texPx.x;
4258
+ float topPx = top * texPx.y;
4259
+ float bottomPx = bottom * texPx.y;
4260
+
4261
+ float lB = leftPx / quadSize.x;
4262
+ float rB = 1.0 - (rightPx / quadSize.x);
4263
+ float tB = topPx / quadSize.y;
4264
+ float bB = 1.0 - (bottomPx / quadSize.y);
4265
+
4266
+ float regionL = step(uv.x, lB);
4267
+ float regionR = step(rB, uv.x);
4268
+ float regionM = 1.0 - regionL - regionR;
4269
+
4270
+ float xL = uv.x * (left / lB);
4271
+ float xR = 1.0 - right + ((uv.x - rB) / (1.0 - rB)) * right;
4272
+ float xM = left + ((uv.x - lB) / (rB - lB)) * (1.0 - left - right);
4273
+
4274
+ localUV.x = regionL * xL + regionM * xM + regionR * xR;
4275
+
4276
+ float regionT = step(uv.y, tB);
4277
+ float regionBtm = step(bB, uv.y);
4278
+ float regionC = 1.0 - regionT - regionBtm;
4279
+
4280
+ float yT = uv.y * (top / tB);
4281
+ float yB = 1.0 - bottom + ((uv.y - bB) / (1.0 - bB)) * bottom;
4282
+ float yC = top + ((uv.y - tB) / (bB - tB)) * (1.0 - top - bottom);
4283
+
4284
+ localUV.y = regionT * yT + regionC * yC + regionBtm * yB;
4285
+
4286
+ return atlasRect.xy + localUV * atlasRect.zw;
4287
+ }
4288
+
4289
+ void main() {
4290
+ vec2 uv = calcNineSliceUV(vUv);
4291
+ vec4 texel = texture2D(map, uv);
4292
+ if (texel.a < alphaTest) discard;
4293
+ gl_FragColor = vec4(texel.rgb * color, texel.a * opacity);
4294
+ #include <colorspace_fragment>
4295
+ }
4296
+ `,
4297
+ transparent: false,
4298
+ alphaTest: DEFAULT_ALPHA_TEST,
4299
+ lights: false,
4300
+ fog: false,
4301
+ depthWrite: true,
4302
+ depthTest: true,
4303
+ });
4304
+ }
4305
+ getTexture() {
4306
+ return this.uniforms.map.value;
4307
+ }
4308
+ getColor() {
4309
+ return this.uniforms.color.value.getHex();
4310
+ }
4311
+ getOpacity() {
4312
+ return this.uniforms.opacity.value;
4313
+ }
4314
+ getTransparency() {
4315
+ return this.transparent;
4316
+ }
4317
+ setSliceBorders(l, r, t, b) {
4318
+ this.uniforms.sliceBorders.value.set(l, r, t, b);
4319
+ this.uniformsNeedUpdate = true;
4320
+ return this;
4321
+ }
4322
+ setQuadSize(width, height) {
4323
+ this.uniforms.quadSize.value.set(width, height);
4324
+ this.uniformsNeedUpdate = true;
4325
+ return this;
4326
+ }
4327
+ setAtlasRect(x, y, w, h) {
4328
+ this.uniforms.atlasRect.value.set(x, y, w, h);
4329
+ this.uniformsNeedUpdate = true;
4330
+ return this;
4331
+ }
4332
+ setColor(color) {
4333
+ this.uniforms.color.value.copy(color);
4334
+ this.uniformsNeedUpdate = true;
4335
+ return this;
4336
+ }
4337
+ setOpacity(opacity) {
4338
+ this.uniforms.opacity.value = opacity;
4339
+ this.uniformsNeedUpdate = true;
4340
+ return this;
4341
+ }
4342
+ }
4343
+
4344
+ class UINineSlice extends UIElement {
4345
+ constructor(layer, texture, x = 0, y = 0) {
4346
+ const w = texture.image.width;
4347
+ const h = texture.image.height;
4348
+ const material = new UINineSliceMaterial(texture);
4349
+ const object = new Mesh(geometry, material);
4350
+ super(layer, x, y, w, h, object);
4351
+ this._private_material = material;
4352
+ this._private_textureInternal = texture;
4353
+ }
4354
+ destroy() {
4355
+ this._private_material.dispose();
4356
+ super.destroy();
4357
+ }
4358
+ ["onBeforeRenderInternal"](renderer, deltaTime) {
4359
+ this._private_material.setQuadSize(this.width * this.micro.scaleX, this.height * this.micro.scaleY);
4360
+ }
4361
+ }
4362
+
4197
4363
  class UIProgressMaterial extends ShaderMaterial {
4198
4364
  constructor(foregroundTexture, backgroundTexture) {
4199
4365
  super({
@@ -5203,6 +5369,8 @@ class UIText extends UIElement {
5203
5369
  size.width + this._private_paddingInternal.left + this._private_paddingInternal.right;
5204
5370
  this._private_canvas.height =
5205
5371
  size.height + this._private_paddingInternal.top + this._private_paddingInternal.bottom;
5372
+ this.width = this._private_canvas.width;
5373
+ this.height = this._private_canvas.height;
5206
5374
  this._private_targetAspectRatio =
5207
5375
  (size.width + this._private_paddingInternal.left + this._private_paddingInternal.right) /
5208
5376
  (size.height + this._private_paddingInternal.top + this._private_paddingInternal.bottom);
@@ -5399,5 +5567,5 @@ function isUITextSpan(obj) {
5399
5567
  (!("style" in obj) || isUITextStyle(obj.style)));
5400
5568
  }
5401
5569
 
5402
- export { UIAnchor, UIAspectConstraint, UIConstraint, UIConstraint2DBuilder, UICustomConstraint, UIDummy, UIElement, UIElementEvent, UIExpression, UIFullscreenLayer, UIHeightConstraint, UIHorizontalDistanceConstraint, UIHorizontalProportionConstraint, UIImage, UILayer, UILayerEvent, UIMicro, UIMode, UIOrientation, UIPriority, UIProgress, UIRelation, UIScene, UISceneUpdateMode, UISingleParameterConstraint, UIText, UIVerticalDistanceConstraint, UIVerticalProportionConstraint, UIWidthConstraint, convertPriority, convertRelation, isUITextSpan, isUITextStyle, resolveOrientation, resolvePadding, resolvePriority, resolveRelation, resolveTextStyle };
5570
+ export { UIAnchor, UIAspectConstraint, UIConstraint, UIConstraint2DBuilder, UICustomConstraint, UIDummy, UIElement, UIElementEvent, UIExpression, UIFullscreenLayer, UIHeightConstraint, UIHorizontalDistanceConstraint, UIHorizontalProportionConstraint, UIImage, UILayer, UILayerEvent, UIMicro, UIMode, UINineSlice, UIOrientation, UIPriority, UIProgress, UIRelation, UIScene, UISceneUpdateMode, UISingleParameterConstraint, UIText, UIVerticalDistanceConstraint, UIVerticalProportionConstraint, UIWidthConstraint, convertPriority, convertRelation, isUITextSpan, isUITextStyle, resolveOrientation, resolvePadding, resolvePriority, resolveRelation, resolveTextStyle };
5403
5571
  //# sourceMappingURL=index.js.map