From 16df3ccbb7c52c9eb1d40e8be43507bcce4a461c Mon Sep 17 00:00:00 2001 From: Sam Reid Date: Fri, 12 May 2023 13:02:36 -0600 Subject: [PATCH] Simplify update drag indicator, see https://github.com/phetsims/center-and-variability/issues/189 --- js/common/model/CAVModel.ts | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/js/common/model/CAVModel.ts b/js/common/model/CAVModel.ts index ff1d4530..ce47b156 100644 --- a/js/common/model/CAVModel.ts +++ b/js/common/model/CAVModel.ts @@ -93,43 +93,35 @@ export default class CAVModel { } ); this.isDragIndicatorVisibleProperty = new BooleanProperty( false, { tandem: options.tandem.createTandem( 'isDragIndicatorVisibleProperty' ) } ); + + // Cannot take a range, since it is nullable this.dragIndicatorValueProperty = new Property( null, { tandem: options.tandem.createTandem( 'dragIndicatorValueProperty' ), - phetioValueType: NullableIO( NumberIO ) + phetioValueType: NullableIO( NumberIO ), + phetioReadOnly: true } ); - Multilink.multilink( [ this.selectedSceneModelProperty, - this.soccerBallHasBeenDraggedProperty, selectedSceneSoccerBallCountProperty, - selectedSceneMaxKicksProperty - ], - ( selectedSceneModel, soccerBallHasBeenDragged, soccerBallCount, maxKicks ) => { - - if ( soccerBallCount !== null ) { - this.updateDragIndicator( selectedSceneModel, soccerBallHasBeenDragged, soccerBallCount, maxKicks ); - } - } ); + const allValueProperties = sceneModels.flatMap( sceneModel => sceneModel.soccerBalls.map( soccerBall => soccerBall.valueProperty ) ); // It is important to link to the values of all the soccer balls in the screen, so that the dragIndicator can be // updated after all the balls have landed, and not just after they have been kicked. - sceneModels.forEach( sceneModel => { - sceneModel.soccerBalls.forEach( soccerBall => soccerBall.valueProperty.link( value => { - if ( value !== null ) { - this.updateDragIndicator( this.selectedSceneModelProperty.value, this.soccerBallHasBeenDraggedProperty.value, - selectedSceneSoccerBallCountProperty.value, selectedSceneMaxKicksProperty.value ); - } - } ) ); + Multilink.multilinkAny( [ ...allValueProperties, this.selectedSceneModelProperty, + this.soccerBallHasBeenDraggedProperty, selectedSceneSoccerBallCountProperty, + selectedSceneMaxKicksProperty + ], () => { + this.updateDragIndicator( this.selectedSceneModelProperty.value, this.soccerBallHasBeenDraggedProperty.value, + selectedSceneSoccerBallCountProperty.value, selectedSceneMaxKicksProperty.value ); } ); - } private updateDragIndicator( selectedSceneModel: CAVSceneModel, soccerBallHasBeenDragged: boolean, soccerBallCount: number, maxKicks: number ): void { // if an object was moved, objects are not input enabled, or the max number of balls haven't been kicked out // don't show the dragIndicatorArrowNode - const indicatorVisible = soccerBallCount === maxKicks && + const indicatorVisible = !soccerBallHasBeenDragged && + soccerBallCount === maxKicks && this.objectNodesInputEnabledProperty.value && - _.every( selectedSceneModel?.getActiveSoccerBalls(), soccerBall => soccerBall.valueProperty.value !== null ) && - !soccerBallHasBeenDragged; + _.every( selectedSceneModel?.getActiveSoccerBalls(), soccerBall => soccerBall.valueProperty.value !== null ); this.isDragIndicatorVisibleProperty.value = indicatorVisible; if ( indicatorVisible ) {