diff --git a/CHANGELOG.md b/CHANGELOG.md index 2477072..90a5c56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ +## [1.5.0](https://github.com/angular-threejs/postprocessing/compare/1.4.0...1.5.0) (2023-02-21) + + +### Features + +* add selection entry point ([eb5751b](https://github.com/angular-threejs/postprocessing/commit/eb5751bebb4c4244e1d80f8fddf6c970b24b5b25)) + ## [1.4.0](https://github.com/angular-threejs/postprocessing/compare/1.3.0...1.4.0) (2023-02-21) diff --git a/libs/angular-three-postprocessing/project.json b/libs/angular-three-postprocessing/project.json index fa79394..02930f8 100644 --- a/libs/angular-three-postprocessing/project.json +++ b/libs/angular-three-postprocessing/project.json @@ -53,7 +53,9 @@ "libs/angular-three-postprocessing/**/*.ts", "libs/angular-three-postprocessing/**/*.html", "libs/angular-three-postprocessing/effects/**/*.ts", - "libs/angular-three-postprocessing/effects/**/*.html" + "libs/angular-three-postprocessing/effects/**/*.html", + "libs/angular-three-postprocessing/selection/**/*.ts", + "libs/angular-three-postprocessing/selection/**/*.html" ] } }, diff --git a/libs/angular-three-postprocessing/selection/README.md b/libs/angular-three-postprocessing/selection/README.md new file mode 100644 index 0000000..419f893 --- /dev/null +++ b/libs/angular-three-postprocessing/selection/README.md @@ -0,0 +1,3 @@ +# angular-three-postprocessing/selection + +Secondary entry point of `angular-three-postprocessing`. It can be used by importing from `angular-three-postprocessing/selection`. diff --git a/libs/angular-three-postprocessing/selection/ng-package.json b/libs/angular-three-postprocessing/selection/ng-package.json new file mode 100644 index 0000000..78e382c --- /dev/null +++ b/libs/angular-three-postprocessing/selection/ng-package.json @@ -0,0 +1,5 @@ +{ + "lib": { + "entryFile": "src/index.ts" + } +} diff --git a/libs/angular-three-postprocessing/selection/src/index.ts b/libs/angular-three-postprocessing/selection/src/index.ts new file mode 100644 index 0000000..0f320db --- /dev/null +++ b/libs/angular-three-postprocessing/selection/src/index.ts @@ -0,0 +1,2 @@ +export * from './lib/select'; +export * from './lib/selection'; diff --git a/libs/angular-three-postprocessing/selection/src/lib/select.ts b/libs/angular-three-postprocessing/selection/src/lib/select.ts new file mode 100644 index 0000000..72514c3 --- /dev/null +++ b/libs/angular-three-postprocessing/selection/src/lib/select.ts @@ -0,0 +1,57 @@ +import { Component, CUSTOM_ELEMENTS_SCHEMA, inject, Input, OnInit } from '@angular/core'; +import { extend, injectNgtRef, NgtRxStore } from 'angular-three'; +import { combineLatest } from 'rxjs'; +import { Group } from 'three'; +import { NGTP_SELECTION_API } from './selection'; + +extend({ Group }); + +@Component({ + selector: 'ngtp-select', + standalone: true, + template: ` + + + + `, + schemas: [CUSTOM_ELEMENTS_SCHEMA], +}) +export class NgtpSelect extends NgtRxStore implements OnInit { + readonly groupRef = injectNgtRef(); + + private readonly selectionApi = inject(NGTP_SELECTION_API); + + @Input() set enabled(enabled: boolean) { + this.set({ enabled }); + } + + override initialize(): void { + super.initialize(); + this.set({ enabled: false }); + } + + ngOnInit() { + this.setSelectEffect(); + } + + private setSelectEffect() { + this.effect(combineLatest([this.select('enabled'), this.groupRef.children$()]), ([enabled]) => { + if (enabled) { + let changed = false; + const current: THREE.Object3D[] = []; + this.groupRef.nativeElement.traverse((obj) => { + if (obj.type === 'Mesh') current.push(obj); + if (this.selectionApi.selected.indexOf(obj) === -1) changed = true; + }); + if (changed) { + this.selectionApi.select((state) => ({ selected: [...state.selected, ...current] })); + return () => { + this.selectionApi.select((state) => ({ + selected: state.selected.filter((selected) => !current.includes(selected)), + })); + }; + } + } + }); + } +} diff --git a/libs/angular-three-postprocessing/selection/src/lib/selection.ts b/libs/angular-three-postprocessing/selection/src/lib/selection.ts new file mode 100644 index 0000000..fb09844 --- /dev/null +++ b/libs/angular-three-postprocessing/selection/src/lib/selection.ts @@ -0,0 +1,42 @@ +import { Component, InjectionToken, Input } from '@angular/core'; +import { RxState } from '@rx-angular/state'; +import { NgtRxStore } from 'angular-three'; + +export interface NgtpSelectionAPI { + selected: THREE.Object3D[]; + select: RxState<{ selected: THREE.Object3D[] }>['set']; + enabled: boolean; +} + +export const NGTP_SELECTION_API = new InjectionToken('NgtpSelection API'); +function ngtpSelectionApiFactory(selection: NgtpSelection) { + const api: NgtpSelectionAPI = { + get selected() { + return selection.get('selected'); + }, + select: selection.set.bind(selection), + get enabled() { + return selection.get('enabled'); + }, + }; + return api; +} + +@Component({ + selector: 'ngtp-selection', + standalone: true, + template: ` + + `, + providers: [{ provide: NGTP_SELECTION_API, useFactory: ngtpSelectionApiFactory, deps: [NgtpSelection] }], +}) +export class NgtpSelection extends NgtRxStore { + @Input() set enabled(enabled: boolean) { + this.set({ enabled }); + } + + override initialize(): void { + super.initialize(); + this.set({ selected: [], enabled: true }); + } +} diff --git a/libs/angular-three-postprocessing/tsconfig.json b/libs/angular-three-postprocessing/tsconfig.json index d789346..28410ba 100644 --- a/libs/angular-three-postprocessing/tsconfig.json +++ b/libs/angular-three-postprocessing/tsconfig.json @@ -6,7 +6,7 @@ "strict": true, "noImplicitOverride": true, "noPropertyAccessFromIndexSignature": true, - "noImplicitReturns": true, + "noImplicitReturns": false, "noFallthroughCasesInSwitch": true }, "files": [], diff --git a/libs/angular-three-postprocessing/version.json b/libs/angular-three-postprocessing/version.json index 0e31e3a..dc360e9 100644 --- a/libs/angular-three-postprocessing/version.json +++ b/libs/angular-three-postprocessing/version.json @@ -1,3 +1,3 @@ { - "version": "1.4.0" + "version": "1.5.0" } diff --git a/tsconfig.base.json b/tsconfig.base.json index b53c648..75cf1da 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -17,7 +17,8 @@ "paths": { "angular-three-postprocessing": ["libs/angular-three-postprocessing/src/index.ts"], "angular-three-postprocessing-plugin": ["libs/plugin/src/index.ts"], - "angular-three-postprocessing/effects": ["libs/angular-three-postprocessing/effects/src/index.ts"] + "angular-three-postprocessing/effects": ["libs/angular-three-postprocessing/effects/src/index.ts"], + "angular-three-postprocessing/selection": ["libs/angular-three-postprocessing/selection/src/index.ts"] } }, "exclude": ["node_modules", "tmp"]