From e49cbd5020f8b5102cc5f775b4da0bc6ccd9e73f Mon Sep 17 00:00:00 2001 From: Sam Reid Date: Fri, 12 Apr 2024 13:38:59 -0600 Subject: [PATCH] Continue launching when the clear button is pressed on the sampling screen, see https://github.com/phetsims/projectile-data-lab/issues/289 --- js/sampling/model/SamplingField.ts | 40 +++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/js/sampling/model/SamplingField.ts b/js/sampling/model/SamplingField.ts index f1b120b7..7bd08838 100644 --- a/js/sampling/model/SamplingField.ts +++ b/js/sampling/model/SamplingField.ts @@ -75,6 +75,10 @@ export default class SamplingField extends Field { public readonly selectedSampleNumberProperty: NumberProperty; + // When pressing the eraser button, if the isContinuousLaunchingProperty is true, the field will automatically restart + // on the next time step. See https://github.com/phetsims/projectile-data-lab/issues/289 + private _shouldResumeAfterClear = false; + public constructor( launcher: Launcher, public readonly sampleSize: number, private readonly launchModeProperty: Property, @@ -316,7 +320,25 @@ export default class SamplingField extends Field { if ( this.phaseProperty.value === 'idle' ) { - // Nothing to do, waiting for user to press the launch button + // At the time the clear button was pressed, was the system in continuous mode and running? + if ( this._shouldResumeAfterClear ) { + + // Is the system *still* in a mode where we want to resume generating data after clear? + if ( this.shouldResumeAfterClear() ) { + + // NOTE: Duplication alert. This is similar to the code in showingCompleteSampleWithMean + this.finishCurrentSample(); + this.phaseProperty.value = 'showingCompleteSampleWithMean'; + this.updateComputedProperties(); + this.phaseStartTimeProperty.value = this.timeProperty.value; + this.projectilesChangedEmitter.emit(); + + assert && assert( typeof this.sampleMeanProperty.value === 'number', 'sampleMeanProperty should be a number in showingCompleteSampleWithMean phase. Projectiles in selected sample: ' + this.getProjectilesInSelectedSample().length + '. Sample size: ' + this.sampleSize ); + MeanTone.playMean( this.sampleMeanProperty.value! ); + } + + this._shouldResumeAfterClear = false; + } } else if ( this.phaseProperty.value === 'showingAirborneProjectiles' ) { // Only for single mode @@ -376,27 +398,43 @@ export default class SamplingField extends Field { // Manually restart the phase timer, since the phase will not change when showing sequential continuous samples this.phaseStartTimeProperty.value = this.timeProperty.value; + this.projectilesChangedEmitter.emit(); + assert && assert( typeof this.sampleMeanProperty.value === 'number', 'sampleMeanProperty should be a number in showingCompleteSampleWithMean phase. Projectiles in selected sample: ' + this.getProjectilesInSelectedSample().length + '. Sample size: ' + this.sampleSize ); MeanTone.playMean( this.sampleMeanProperty.value! ); } } } + private shouldResumeAfterClear(): boolean { + return this.phaseProperty.value === 'showingCompleteSampleWithMean' && + this.isContinuousLaunchingProperty.value && + this.launchModeProperty.value === 'continuous'; + } + // When the eraser button is pressed, clear the selected Field's projectiles. public override clearProjectiles(): void { super.clearProjectiles(); + this._shouldResumeAfterClear = this.shouldResumeAfterClear(); + this.phaseProperty.reset(); this.timeProperty.reset(); this.phaseStartTimeProperty.reset(); this.selectedSampleNumberProperty.reset(); + if ( !this._shouldResumeAfterClear ) { + this.isContinuousLaunchingProperty.reset(); + } + this.updateComputedProperties(); } public override reset(): void { super.reset(); this.launchModeProperty.reset(); + + this._shouldResumeAfterClear = false; } }