Skip to content

Commit eb5751b

Browse files
committed
feat: add selection entry point
1 parent fc74343 commit eb5751b

File tree

8 files changed

+115
-3
lines changed

8 files changed

+115
-3
lines changed

libs/angular-three-postprocessing/project.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
"libs/angular-three-postprocessing/**/*.ts",
5454
"libs/angular-three-postprocessing/**/*.html",
5555
"libs/angular-three-postprocessing/effects/**/*.ts",
56-
"libs/angular-three-postprocessing/effects/**/*.html"
56+
"libs/angular-three-postprocessing/effects/**/*.html",
57+
"libs/angular-three-postprocessing/selection/**/*.ts",
58+
"libs/angular-three-postprocessing/selection/**/*.html"
5759
]
5860
}
5961
},
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# angular-three-postprocessing/selection
2+
3+
Secondary entry point of `angular-three-postprocessing`. It can be used by importing from `angular-three-postprocessing/selection`.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"lib": {
3+
"entryFile": "src/index.ts"
4+
}
5+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './lib/select';
2+
export * from './lib/selection';
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { Component, CUSTOM_ELEMENTS_SCHEMA, inject, Input, OnInit } from '@angular/core';
2+
import { extend, injectNgtRef, NgtRxStore } from 'angular-three';
3+
import { combineLatest } from 'rxjs';
4+
import { Group } from 'three';
5+
import { NGTP_SELECTION_API } from './selection';
6+
7+
extend({ Group });
8+
9+
@Component({
10+
selector: 'ngtp-select',
11+
standalone: true,
12+
template: `
13+
<ngt-group ngtCompound [ref]="groupRef">
14+
<ng-content />
15+
</ngt-group>
16+
`,
17+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
18+
})
19+
export class NgtpSelect extends NgtRxStore implements OnInit {
20+
readonly groupRef = injectNgtRef<THREE.Group>();
21+
22+
private readonly selectionApi = inject(NGTP_SELECTION_API);
23+
24+
@Input() set enabled(enabled: boolean) {
25+
this.set({ enabled });
26+
}
27+
28+
override initialize(): void {
29+
super.initialize();
30+
this.set({ enabled: false });
31+
}
32+
33+
ngOnInit() {
34+
this.setSelectEffect();
35+
}
36+
37+
private setSelectEffect() {
38+
this.effect(combineLatest([this.select('enabled'), this.groupRef.children$()]), ([enabled]) => {
39+
if (enabled) {
40+
let changed = false;
41+
const current: THREE.Object3D[] = [];
42+
this.groupRef.nativeElement.traverse((obj) => {
43+
if (obj.type === 'Mesh') current.push(obj);
44+
if (this.selectionApi.selected.indexOf(obj) === -1) changed = true;
45+
});
46+
if (changed) {
47+
this.selectionApi.select((state) => ({ selected: [...state.selected, ...current] }));
48+
return () => {
49+
this.selectionApi.select((state) => ({
50+
selected: state.selected.filter((selected) => !current.includes(selected)),
51+
}));
52+
};
53+
}
54+
}
55+
});
56+
}
57+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Component, InjectionToken, Input } from '@angular/core';
2+
import { RxState } from '@rx-angular/state';
3+
import { NgtRxStore } from 'angular-three';
4+
5+
export interface NgtpSelectionAPI {
6+
selected: THREE.Object3D[];
7+
select: RxState<{ selected: THREE.Object3D[] }>['set'];
8+
enabled: boolean;
9+
}
10+
11+
export const NGTP_SELECTION_API = new InjectionToken<NgtpSelectionAPI>('NgtpSelection API');
12+
function ngtpSelectionApiFactory(selection: NgtpSelection) {
13+
const api: NgtpSelectionAPI = {
14+
get selected() {
15+
return selection.get('selected');
16+
},
17+
select: selection.set.bind(selection),
18+
get enabled() {
19+
return selection.get('enabled');
20+
},
21+
};
22+
return api;
23+
}
24+
25+
@Component({
26+
selector: 'ngtp-selection',
27+
standalone: true,
28+
template: `
29+
<ng-content />
30+
`,
31+
providers: [{ provide: NGTP_SELECTION_API, useFactory: ngtpSelectionApiFactory, deps: [NgtpSelection] }],
32+
})
33+
export class NgtpSelection extends NgtRxStore {
34+
@Input() set enabled(enabled: boolean) {
35+
this.set({ enabled });
36+
}
37+
38+
override initialize(): void {
39+
super.initialize();
40+
this.set({ selected: [], enabled: true });
41+
}
42+
}

libs/angular-three-postprocessing/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"strict": true,
77
"noImplicitOverride": true,
88
"noPropertyAccessFromIndexSignature": true,
9-
"noImplicitReturns": true,
9+
"noImplicitReturns": false,
1010
"noFallthroughCasesInSwitch": true
1111
},
1212
"files": [],

tsconfig.base.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"paths": {
1818
"angular-three-postprocessing": ["libs/angular-three-postprocessing/src/index.ts"],
1919
"angular-three-postprocessing-plugin": ["libs/plugin/src/index.ts"],
20-
"angular-three-postprocessing/effects": ["libs/angular-three-postprocessing/effects/src/index.ts"]
20+
"angular-three-postprocessing/effects": ["libs/angular-three-postprocessing/effects/src/index.ts"],
21+
"angular-three-postprocessing/selection": ["libs/angular-three-postprocessing/selection/src/index.ts"]
2122
}
2223
},
2324
"exclude": ["node_modules", "tmp"]

0 commit comments

Comments
 (0)