Skip to content

Commit 3ef325d

Browse files
Chau TranChau Tran
Chau Tran
authored and
Chau Tran
committed
fix(soba): ensure effect that emit centered output allow signal writes
1 parent a0d44cc commit 3ef325d

File tree

1 file changed

+53
-44
lines changed

1 file changed

+53
-44
lines changed

libs/soba/staging/src/center/center.ts

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { Component, computed, CUSTOM_ELEMENTS_SCHEMA, effect, EventEmitter, Input, Output } from '@angular/core';
2-
import { extend, injectNgtRef, NgtSignalStore, requestAnimationInInjectionContext, type NgtGroup } from 'angular-three';
2+
import {
3+
extend,
4+
injectNgtRef,
5+
NgtSignalStore,
6+
requestAnimationFrameInInjectionContext,
7+
type NgtGroup,
8+
} from 'angular-three';
39
import { Box3, Group, Sphere, Vector3 } from 'three';
410

511
export type NgtsCenterState = {
@@ -111,7 +117,7 @@ export class NgtsCenter extends NgtSignalStore<NgtsCenterState> {
111117

112118
constructor() {
113119
super({ precise: true });
114-
requestAnimationInInjectionContext(() => {
120+
requestAnimationFrameInInjectionContext(() => {
115121
this.#setPosition();
116122
});
117123
}
@@ -125,47 +131,50 @@ export class NgtsCenter extends NgtSignalStore<NgtsCenterState> {
125131
return { center, outer, inner, innerChildren: innerChildren() };
126132
});
127133

128-
effect(() => {
129-
const { center: centerGroup, outer, inner } = trigger();
130-
if (!outer || !inner) return;
131-
const { precise, top, left, front, disable, disableX, disableY, disableZ, back, bottom, right } =
132-
this.get();
133-
outer.matrixWorld.identity();
134-
const box3 = new Box3().setFromObject(inner, precise);
135-
const center = new Vector3();
136-
const sphere = new Sphere();
137-
const width = box3.max.x - box3.min.x;
138-
const height = box3.max.y - box3.min.y;
139-
const depth = box3.max.z - box3.min.z;
140-
141-
box3.getCenter(center);
142-
box3.getBoundingSphere(sphere);
143-
144-
const vAlign = top ? height / 2 : bottom ? -height / 2 : 0;
145-
const hAlign = left ? -width / 2 : right ? width / 2 : 0;
146-
const dAlign = front ? depth / 2 : back ? -depth / 2 : 0;
147-
148-
outer.position.set(
149-
disable || disableX ? 0 : -center.x + hAlign,
150-
disable || disableY ? 0 : -center.y + vAlign,
151-
disable || disableZ ? 0 : -center.z + dAlign
152-
);
153-
154-
if (this.centered.observed) {
155-
this.centered.emit({
156-
parent: centerGroup.parent!,
157-
container: centerGroup,
158-
width,
159-
height,
160-
depth,
161-
boundingBox: box3,
162-
boundingSphere: sphere,
163-
center: center,
164-
verticalAlignment: vAlign,
165-
horizontalAlignment: hAlign,
166-
depthAlignment: dAlign,
167-
});
168-
}
169-
});
134+
effect(
135+
() => {
136+
const { center: centerGroup, outer, inner } = trigger();
137+
if (!outer || !inner) return;
138+
const { precise, top, left, front, disable, disableX, disableY, disableZ, back, bottom, right } =
139+
this.get();
140+
outer.matrixWorld.identity();
141+
const box3 = new Box3().setFromObject(inner, precise);
142+
const center = new Vector3();
143+
const sphere = new Sphere();
144+
const width = box3.max.x - box3.min.x;
145+
const height = box3.max.y - box3.min.y;
146+
const depth = box3.max.z - box3.min.z;
147+
148+
box3.getCenter(center);
149+
box3.getBoundingSphere(sphere);
150+
151+
const vAlign = top ? height / 2 : bottom ? -height / 2 : 0;
152+
const hAlign = left ? -width / 2 : right ? width / 2 : 0;
153+
const dAlign = front ? depth / 2 : back ? -depth / 2 : 0;
154+
155+
outer.position.set(
156+
disable || disableX ? 0 : -center.x + hAlign,
157+
disable || disableY ? 0 : -center.y + vAlign,
158+
disable || disableZ ? 0 : -center.z + dAlign
159+
);
160+
161+
if (this.centered.observed) {
162+
this.centered.emit({
163+
parent: centerGroup.parent!,
164+
container: centerGroup,
165+
width,
166+
height,
167+
depth,
168+
boundingBox: box3,
169+
boundingSphere: sphere,
170+
center: center,
171+
verticalAlignment: vAlign,
172+
horizontalAlignment: hAlign,
173+
depthAlignment: dAlign,
174+
});
175+
}
176+
},
177+
{ allowSignalWrites: true }
178+
);
170179
}
171180
}

0 commit comments

Comments
 (0)