11import {
22 afterNextRender ,
33 booleanAttribute ,
4- ChangeDetectionStrategy ,
5- Component ,
6- CUSTOM_ELEMENTS_SCHEMA ,
74 Directive ,
85 ElementRef ,
96 inject ,
107 input ,
118 signal ,
129 untracked ,
13- viewChild ,
1410} from '@angular/core' ;
1511import { injectAutoEffect } from 'ngxtension/auto-effect' ;
16- import { Group , Object3D } from 'three' ;
12+ import { Group , Mesh , Object3D } from 'three' ;
1713import { getLocalState } from '../instance' ;
18- import { extend } from '../renderer' ;
19- import { NgtGroup } from '../three-types' ;
20- import { NgtObjectEvents , NgtObjectEventsHostDirective } from '../utils/object-events' ;
2114
2215@Directive ( { standalone : true , selector : '[ngtSelection]' } )
2316export class NgtSelection {
@@ -35,47 +28,38 @@ export class NgtSelection {
3528 }
3629}
3730
38- @Component ( {
39- selector : 'ngt-select' ,
40- standalone : true ,
41- template : `
42- <ngt-group #group [parameters]="options()">
43- <ng-content />
44- </ngt-group>
45- ` ,
46- hostDirectives : [ NgtObjectEventsHostDirective ] ,
47- schemas : [ CUSTOM_ELEMENTS_SCHEMA ] ,
48- changeDetection : ChangeDetectionStrategy . OnPush ,
49- } )
31+ @Directive ( { standalone : true , selector : 'ngt-group[ngtSelect], ngt-mesh[ngtSelect]' } )
5032export class NgtSelect {
51- enabled = input ( false , { transform : booleanAttribute } ) ;
52- options = input ( { } as Partial < NgtGroup > ) ;
33+ enabled = input ( false , { transform : booleanAttribute , alias : 'ngtSelect' } ) ;
5334
54- groupRef = viewChild . required < ElementRef < Group > > ( 'group' ) ;
35+ host = inject < ElementRef < Group | Mesh > > ( ElementRef ) ;
5536
5637 constructor ( ) {
57- extend ( { Group } ) ;
58-
59- const objectEvents = inject ( NgtObjectEvents , { host : true } ) ;
6038 const selection = inject ( NgtSelection ) ;
6139 const autoEffect = injectAutoEffect ( ) ;
6240
6341 afterNextRender ( ( ) => {
64- objectEvents . ngtObjectEvents . set ( this . groupRef ( ) ) ;
65-
6642 autoEffect (
6743 ( ) => {
68- const group = this . groupRef ( ) . nativeElement ;
69- const localState = getLocalState ( group ) ;
44+ const host = this . host . nativeElement ;
45+ if ( ! host ) return ;
46+
47+ const localState = getLocalState ( host ) ;
7048 if ( ! localState ) return ;
7149
7250 const enabled = this . enabled ( ) ;
7351 if ( ! enabled ) return ;
7452
53+ // ngt-mesh[ngtSelect]
54+ if ( host . type === 'Mesh' ) {
55+ selection . select ( host ) ;
56+ return ( ) => selection . unselect ( host ) ;
57+ }
58+
7559 const [ collection ] = [ untracked ( selection . collection ) , localState . objects ( ) ] ;
7660 let changed = false ;
7761 const current : Object3D [ ] = [ ] ;
78- group . traverse ( ( child ) => {
62+ host . traverse ( ( child ) => {
7963 child . type === 'Mesh' && current . push ( child ) ;
8064 if ( collection . indexOf ( child ) === - 1 ) changed = true ;
8165 } ) ;
0 commit comments