Skip to content

Commit

Permalink
Continue launching when the clear button is pressed on the sampling s…
Browse files Browse the repository at this point in the history
…creen, see #289
  • Loading branch information
samreid committed Apr 12, 2024
1 parent 9a0266a commit e49cbd5
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion js/sampling/model/SamplingField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SingleOrContinuous>,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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;
}
}

Expand Down

0 comments on commit e49cbd5

Please sign in to comment.