This repository has been archived by the owner on Feb 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(soba): add SobaLineController for abstraction
- Loading branch information
Showing
3 changed files
with
205 additions
and
86 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
packages/soba/abstractions/src/lib/line/line-watched-controller.di.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
DestroyedService, | ||
NGT_OBJECT_3D_CONTROLLER_PROVIDER, | ||
} from '@angular-three/core'; | ||
import { | ||
ChangeDetectorRef, | ||
InjectionToken, | ||
Optional, | ||
Provider, | ||
} from '@angular/core'; | ||
import { takeUntil } from 'rxjs'; | ||
import { NgtSobaLineController } from './line.controller'; | ||
|
||
export const NGT_SOBA_LINE_WATCHED_CONTROLLER = new InjectionToken( | ||
'Watched Line Controller' | ||
); | ||
|
||
export const NGT_SOBA_LINE_CONTROLLER_PROVIDER: Provider[] = [ | ||
NGT_OBJECT_3D_CONTROLLER_PROVIDER, | ||
DestroyedService, | ||
{ | ||
provide: NGT_SOBA_LINE_WATCHED_CONTROLLER, | ||
deps: [ | ||
[new Optional(), NgtSobaLineController], | ||
ChangeDetectorRef, | ||
DestroyedService, | ||
], | ||
useFactory: sobaLineWatchedControllerFactory, | ||
}, | ||
]; | ||
|
||
export function sobaLineWatchedControllerFactory( | ||
controller: NgtSobaLineController | null, | ||
cdr: ChangeDetectorRef, | ||
destroyed: DestroyedService | ||
) { | ||
if (!controller) return null; | ||
|
||
controller.change$.pipe(takeUntil(destroyed)).subscribe(() => { | ||
cdr.markForCheck(); | ||
}); | ||
|
||
return controller; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
packages/soba/abstractions/src/lib/line/line.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { Controller, NgtAnimationReady, NgtColor } from '@angular-three/core'; | ||
import { | ||
Directive, | ||
EventEmitter, | ||
Input, | ||
NgZone, | ||
OnInit, | ||
Output, | ||
SimpleChanges, | ||
} from '@angular/core'; | ||
import { Line2 } from 'three/examples/jsm/lines/Line2'; | ||
import { LineGeometry } from 'three/examples/jsm/lines/LineGeometry'; | ||
import { | ||
LineMaterial, | ||
LineMaterialParameters, | ||
} from 'three/examples/jsm/lines/LineMaterial'; | ||
|
||
@Directive({ | ||
selector: | ||
'ngt-soba-line,ngt-soba-quadratic-bezier-line,ngt-soba-cubic-bezier-line', | ||
exportAs: 'ngtSobaLineController', | ||
}) | ||
export class NgtSobaLineController extends Controller implements OnInit { | ||
@Input() vertexColors?: Array<NgtColor>; | ||
|
||
@Input() color: NgtColor = 'black'; | ||
@Input() lineWidth?: LineMaterialParameters['linewidth']; | ||
@Input() dashed?: LineMaterialParameters['dashed']; | ||
|
||
@Input() materialParameters?: Omit< | ||
LineMaterialParameters, | ||
'vertexColors' | 'color' | 'linewidth' | 'dashed' | 'resolution' | ||
> = {}; | ||
|
||
@Input() sobaLineController?: NgtSobaLineController; | ||
|
||
@Output() ready = new EventEmitter<Line2>(); | ||
@Output() animateReady = new EventEmitter<NgtAnimationReady<Line2>>(); | ||
|
||
parameters!: LineMaterialParameters; | ||
line!: Line2; | ||
geometry!: LineGeometry; | ||
material!: LineMaterial; | ||
|
||
constructor(private ngZone: NgZone) { | ||
super(); | ||
} | ||
|
||
ngOnChanges(changes: SimpleChanges) { | ||
if (this.sobaLineController) { | ||
this.sobaLineController.ngOnChanges(changes); | ||
} else { | ||
super.ngOnChanges(changes); | ||
} | ||
this.ngZone.runOutsideAngular(() => { | ||
this.parameters = { | ||
color: this.color as number, | ||
linewidth: this.lineWidth, | ||
dashed: this.dashed, | ||
vertexColors: Boolean(this.vertexColors), | ||
...this.materialParameters, | ||
}; | ||
|
||
if (changes.vertexColors) { | ||
if (this.line) { | ||
this.line.computeLineDistances(); | ||
} | ||
} | ||
|
||
if (changes.dashed) { | ||
if (this.material) { | ||
this.dasherize(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
ngOnInit() { | ||
this.ngZone.runOutsideAngular(() => { | ||
if (this.sobaLineController) { | ||
this.vertexColors = this.sobaLineController.vertexColors; | ||
this.color = this.sobaLineController.color; | ||
this.lineWidth = this.sobaLineController.lineWidth; | ||
this.dashed = this.sobaLineController.dashed; | ||
this.materialParameters = this.sobaLineController.materialParameters; | ||
|
||
this.ready = this.sobaLineController.ready; | ||
this.animateReady = this.sobaLineController.animateReady; | ||
} | ||
}); | ||
} | ||
|
||
mergeParameters(extra: Record<string, unknown> = {}) { | ||
this.parameters = Object.assign(this.parameters || {}, extra); | ||
} | ||
|
||
onLineReady(line: Line2) { | ||
this.ngZone.runOutsideAngular(() => { | ||
this.line = line; | ||
this.line.computeLineDistances(); | ||
this.ngZone.run(() => { | ||
this.ready.emit(line); | ||
}); | ||
}); | ||
} | ||
|
||
onMaterialReady(material: LineMaterial) { | ||
this.ngZone.runOutsideAngular(() => { | ||
this.material = material; | ||
this.dasherize(); | ||
}); | ||
} | ||
|
||
private dasherize() { | ||
if (this.dashed) { | ||
this.material.defines.USE_DASH = ''; | ||
} else { | ||
delete this.material.defines.USE_DASH; | ||
} | ||
} | ||
} |