1
1
import {
2
2
afterNextRender ,
3
3
booleanAttribute ,
4
- ChangeDetectionStrategy ,
5
- Component ,
6
- CUSTOM_ELEMENTS_SCHEMA ,
7
4
Directive ,
8
5
ElementRef ,
9
6
inject ,
10
7
input ,
11
8
signal ,
12
9
untracked ,
13
- viewChild ,
14
10
} from '@angular/core' ;
15
11
import { injectAutoEffect } from 'ngxtension/auto-effect' ;
16
- import { Group , Object3D } from 'three' ;
12
+ import { Group , Mesh , Object3D } from 'three' ;
17
13
import { getLocalState } from '../instance' ;
18
- import { extend } from '../renderer' ;
19
- import { NgtGroup } from '../three-types' ;
20
- import { NgtObjectEvents , NgtObjectEventsHostDirective } from '../utils/object-events' ;
21
14
22
15
@Directive ( { standalone : true , selector : '[ngtSelection]' } )
23
16
export class NgtSelection {
@@ -35,47 +28,38 @@ export class NgtSelection {
35
28
}
36
29
}
37
30
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]' } )
50
32
export class NgtSelect {
51
- enabled = input ( false , { transform : booleanAttribute } ) ;
52
- options = input ( { } as Partial < NgtGroup > ) ;
33
+ enabled = input ( false , { transform : booleanAttribute , alias : 'ngtSelect' } ) ;
53
34
54
- groupRef = viewChild . required < ElementRef < Group > > ( 'group' ) ;
35
+ host = inject < ElementRef < Group | Mesh > > ( ElementRef ) ;
55
36
56
37
constructor ( ) {
57
- extend ( { Group } ) ;
58
-
59
- const objectEvents = inject ( NgtObjectEvents , { host : true } ) ;
60
38
const selection = inject ( NgtSelection ) ;
61
39
const autoEffect = injectAutoEffect ( ) ;
62
40
63
41
afterNextRender ( ( ) => {
64
- objectEvents . ngtObjectEvents . set ( this . groupRef ( ) ) ;
65
-
66
42
autoEffect (
67
43
( ) => {
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 ) ;
70
48
if ( ! localState ) return ;
71
49
72
50
const enabled = this . enabled ( ) ;
73
51
if ( ! enabled ) return ;
74
52
53
+ // ngt-mesh[ngtSelect]
54
+ if ( host . type === 'Mesh' ) {
55
+ selection . select ( host ) ;
56
+ return ( ) => selection . unselect ( host ) ;
57
+ }
58
+
75
59
const [ collection ] = [ untracked ( selection . collection ) , localState . objects ( ) ] ;
76
60
let changed = false ;
77
61
const current : Object3D [ ] = [ ] ;
78
- group . traverse ( ( child ) => {
62
+ host . traverse ( ( child ) => {
79
63
child . type === 'Mesh' && current . push ( child ) ;
80
64
if ( collection . indexOf ( child ) === - 1 ) changed = true ;
81
65
} ) ;
0 commit comments