@@ -5,19 +5,21 @@ import { TransformKeysMap } from '@constants';
55import ObjectUtils from '@utils/ObjectUtils' ;
66import guiConstructors from '../../_generated/babylon.gui.constructors' ;
77import type { Vector2WithInfo , GUI3DManager , Control , Container , Button3D , Grid } from '@babylonjs/gui' ;
8- import type { Observable } from '@babylonjs/core' ;
8+ import type { Observable , EventState } from '@babylonjs/core' ;
99
1010// required for git hook (otherwise it can't resolve the augmented JSXElements)
1111import '../../index' ;
1212import { AbstractMesh } from '@babylonjs/core/Meshes/abstractMesh.js' ;
1313
14- function handleEvents ( props : GuiHostProps , element : any ) {
14+ type AugmentedGuiElement = BabylonEntity < Control & GuiTriggerable > & {
15+ _triggerCallbacks : Partial < Record < keyof typeof GuiTriggers , Parameters < Observable < Vector2WithInfo > [ 'add' ] > [ 0 ] > > ;
16+ } ;
17+
18+ function handleEvents ( element : AugmentedGuiElement ) {
1519 Object . entries ( GuiTriggers ) . forEach ( ( [ _key , observableName ] ) => {
16- const key = _key as keyof GuiTriggerable ;
17- //TODO: handle addOnce
18- const handlerFn = props [ key ] as Parameters < Observable < Vector2WithInfo > [ 'add' ] > [ 0 ] ;
19- if ( handlerFn ) {
20- element [ observableName ] . add ( handlerFn ) ;
20+ const key = _key as keyof typeof GuiTriggers ;
21+ if ( element . _triggerCallbacks [ key ] ) {
22+ ( element as any ) [ observableName ] . add ( ( evt : Vector2WithInfo , eventState : EventState ) => element . _triggerCallbacks [ key ] ?.( evt , eventState ) ) ;
2123 }
2224 } ) ;
2325}
@@ -29,7 +31,7 @@ export type Params = {
2931
3032type GuiComponent = Pick < Container , 'addControl' | 'removeControl' > & GuiTriggerable ;
3133
32- const excludedProps = [ 'children' , 'onCreate' , 'cloneFrom' , 'propertiesFrom' , 'kind' , 'createFullscreenUI' , 'createForMesh' , 'ref' , ... Object . keys ( GuiTriggers ) ] ;
34+ const excludedProps = [ 'children' , 'onCreate' , 'cloneFrom' , 'propertiesFrom' , 'kind' , 'createFullscreenUI' , 'createForMesh' , 'ref' ] ;
3335
3436export class GuiHost {
3537 static createInstance ( type : string , Class : any , props : GuiHostProps , rootContainer : RootContainer , cloneFn ?: Function , params ?: Params ) {
@@ -86,12 +88,21 @@ export class GuiHost {
8688 // execute custom code as soon the object is created
8789 props . onCreate ?.( element ) ;
8890
89- handleEvents ( props , element ) ;
91+ element . _triggerCallbacks = Object . keys ( GuiTriggers ) . reduce (
92+ ( acc , trigger ) => {
93+ const key = trigger as keyof typeof GuiTriggers ;
94+ if ( element [ key ] ) acc [ key ] = element [ key ] ;
95+ return acc ;
96+ } ,
97+ { } as Partial < Record < keyof typeof GuiTriggers , Parameters < Observable < Vector2WithInfo > [ 'add' ] > [ 0 ] > > ,
98+ ) ;
99+
100+ handleEvents ( element ) ;
90101
91102 element . handlers = {
92103 addChild : GuiHost . addChild ,
93104 removeChild : GuiHost . removeChild ,
94- // add here your custom handlers
105+ commitUpdate : GuiHost . commitUpdate ,
95106 } ;
96107
97108 return element ;
@@ -106,7 +117,7 @@ export class GuiHost {
106117 } else {
107118 // @ts -expect-error - type is not a part of Grid
108119 if ( parentInstance . type === 'row' || parentInstance . type === 'column' ) {
109- ( parentInstance as Grid ) . addControl ?.( child , 0 , 0 ) ;
120+ ( parentInstance as unknown as Grid ) . addControl ?.( child , 0 , 0 ) ;
110121 } else {
111122 // ensure that addControl function exists (parentInstance could be transformNode)
112123 parentInstance . addControl ?.( child ) ;
@@ -124,5 +135,12 @@ export class GuiHost {
124135 return { } ;
125136 }
126137
127- static commitUpdate ( instance : BabylonEntity < Control > , updatePayload : UpdatePayload ) : void { }
138+ static commitUpdate ( instance : AugmentedGuiElement , updatePayload : UpdatePayload ) : void {
139+ Object . keys ( GuiTriggers ) . forEach ( trigger => {
140+ const key = trigger as keyof typeof GuiTriggers ;
141+ if ( key in updatePayload ) {
142+ instance . _triggerCallbacks [ key ] = updatePayload [ key ] as Parameters < Observable < Vector2WithInfo > [ 'add' ] > [ 0 ] ;
143+ }
144+ } ) ;
145+ }
128146}
0 commit comments