Skip to content

Commit

Permalink
fix bottle custom density slider, #256
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Kauzmann <[email protected]>
  • Loading branch information
zepumph committed Jul 12, 2024
1 parent bc1a65c commit 3465e4c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 36 deletions.
5 changes: 3 additions & 2 deletions js/buoyancy/model/applications/Bottle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ export default class Bottle extends ApplicationsMass {
public readonly maxVolumeDisplacedProperty = new NumberProperty( ApplicationsMass.DEFAULT_DISPLACEMENT_VOLUME );

// In kg (kilograms)
public interiorMassProperty: ReadOnlyProperty<number>;
// TODO: rename to materialInsideMassProperty, https://github.com/phetsims/density-buoyancy-common/issues/256
public readonly interiorMassProperty: ReadOnlyProperty<number>;

public constructor( engine: PhysicsEngine, providedOptions: BottleOptions ) {

Expand All @@ -203,7 +204,7 @@ export default class Bottle extends ApplicationsMass {
const customMaterial = Material.createCustomSolidMaterial( {
nameProperty: DensityBuoyancyCommonStrings.systemAStringProperty,
density: ( BOTTLE_MASS + BOTTLE_INITIAL_INTERIOR_MATERIAL.density * BOTTLE_INITIAL_INTERIOR_VOLUME ) / BOTTLE_VOLUME,
densityRange: BOTTLE_DENSITY_RANGE
densityRange: new Range( 10, 1000000000 ) // TODO: set min lower than 10? https://github.com/phetsims/density-buoyancy-common/issues/256
} );

const options = optionize<BottleOptions, EmptySelfOptions, InstrumentedMassOptions>()( {
Expand Down
24 changes: 6 additions & 18 deletions js/buoyancy/view/applications/BuoyancyApplicationsScreenView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ export default class BuoyancyApplicationsScreenView extends BuoyancyScreenView<B
this.transformEmitter.addListener( this.positionResetSceneButton );
this.positionResetSceneButton();

// TODO: create in bottle? https://github.com/phetsims/density-buoyancy-common/issues/256
const customMaterialInside = Material.createCustomSolidMaterial( {
density: 1000,
densityRange: new Range( 0, 1000 ) // TODO: range, https://github.com/phetsims/density-buoyancy-common/issues/256
densityRange: new Range( 50, 20000 )
} );

const bottleControlsTandem = tandem.createTandem( 'bottleControls' );
Expand All @@ -140,7 +141,8 @@ export default class BuoyancyApplicationsScreenView extends BuoyancyScreenView<B
showMassAsReadout: true,
supportHiddenMaterial: true,
customKeepsConstantDensity: true,
tandem: materialInsideControlsTandem
tandem: materialInsideControlsTandem,
customMaterial: customMaterialInside
} );

// This DerivedProperty doesn't need disposal, since everything here lives for the lifetime of the simulation
Expand All @@ -149,21 +151,6 @@ export default class BuoyancyApplicationsScreenView extends BuoyancyScreenView<B
const customDensityControlVisibleProperty = new DerivedProperty( [ model.bottle.materialInsideProperty ],
material => material.custom );

let materialChangeLocked = false;
Multilink.lazyMultilink( [
// TODO: umm...... https://github.com/phetsims/density-buoyancy-common/issues/256
// model.bottle.customDensityProperty,
// model.bottle.customDensityProperty.rangeProperty,
model.bottle.interiorMassProperty,
customDensityControlVisibleProperty
], ( density, densityRange ) => {
if ( !materialChangeLocked && model.bottle.materialInsideProperty.value.custom ) {
materialChangeLocked = true;
model.bottle.materialInsideProperty.value.densityProperty.value = density * DensityBuoyancyCommonConstants.LITERS_IN_CUBIC_METER;
materialChangeLocked = false;
}
} );

const customBottleDensityControlTandem = materialInsideControlsTandem.createTandem( 'customBottleDensityNumberControl' );
const customBottleDensityControl = new NumberControl( DensityBuoyancyCommonStrings.densityStringProperty, customMaterialInside.densityProperty, customMaterialInside.densityProperty.range, combineOptions<NumberControlOptions>( {
visibleProperty: new GatedVisibleProperty( customDensityControlVisibleProperty, customBottleDensityControlTandem ),
Expand Down Expand Up @@ -238,7 +225,8 @@ export default class BuoyancyApplicationsScreenView extends BuoyancyScreenView<B
] ), cubicMeters => model.block.updateSize( Cube.boundsFromVolume( cubicMeters ) ), this.popupLayer, true, {
tandem: tandem.createTandem( 'blockControls' ),
highDensityMaxMass: 215,
supportHiddenMaterial: true
supportHiddenMaterial: true,
supportCustomMaterial: false // TODO: Undeoo!!
} );

model.block.materialProperty.link( material => {
Expand Down
23 changes: 7 additions & 16 deletions js/common/view/MaterialControlNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import DensityBuoyancyCommonConstants from '../DensityBuoyancyCommonConstants.js
import Material, { MaterialName } from '../model/Material.js';
import { PhetioObjectOptions } from '../../../../tandem/js/PhetioObject.js';
import PickRequired from '../../../../phet-core/js/types/PickRequired.js';
import Range from '../../../../dot/js/Range.js';
import Utils from '../../../../dot/js/Utils.js';
import densityBuoyancyCommon from '../../densityBuoyancyCommon.js';

Expand All @@ -37,12 +36,13 @@ type SelfMaterialControlNodeOptions = {
minCustomVolumeLiters?: number;
maxVolumeLiters?: number;

customMaterial?: Material | null;

} & PickRequired<PhetioObjectOptions, 'tandem'>;

export type MaterialControlNodeOptions = SelfMaterialControlNodeOptions & VBoxOptions;

export default class MaterialControlNode extends VBox {
protected customDensityRange: Range;

public constructor( materialProperty: Property<Material>,
volumeProperty: Property<number>,
Expand All @@ -58,20 +58,16 @@ export default class MaterialControlNode extends VBox {
minCustomMass: 0.5,
maxCustomMass: 10,
minCustomVolumeLiters: 1,
maxVolumeLiters: 10
maxVolumeLiters: 10,
customMaterial: null
}, providedOptions );

super( {
spacing: DensityBuoyancyCommonConstants.SPACING,
align: 'left'
} );

this.customDensityRange = new Range(
options.minCustomMass / options.maxVolumeLiters * DensityBuoyancyCommonConstants.LITERS_IN_CUBIC_METER,

// Prevent divide by zero errors (infinity) with a manual, tiny number
options.maxCustomMass / ( options.minCustomVolumeLiters ) * DensityBuoyancyCommonConstants.LITERS_IN_CUBIC_METER
);
assert && options.supportCustomMaterial && assert( options.customMaterial, 'custom material please' );

if ( !options.supportHiddenMaterial ) {
materials = materials.filter( material => !material.hidden );
Expand All @@ -97,17 +93,12 @@ export default class MaterialControlNode extends VBox {
};
};

// TODO: Yar? https://github.com/phetsims/density-buoyancy-common/issues/256
const customMaterial = Material.createCustomSolidMaterial( {
densityRange: this.customDensityRange,
density: materialProperty.value.density
} );
volumeProperty.link( volume => {
if ( materialProperty.value.custom ) {

// Handle our minimum volume if we're switched to custom (if needed)
const maxVolume = Math.max( volume, options.minCustomVolumeLiters / DensityBuoyancyCommonConstants.LITERS_IN_CUBIC_METER );
customMaterial.densityProperty.value = Utils.clamp( materialProperty.value.density, options.minCustomMass / maxVolume, options.maxCustomMass / maxVolume );
options.customMaterial!.densityProperty.value = Utils.clamp( materialProperty.value.density, options.minCustomMass / maxVolume, options.maxCustomMass / maxVolume );
}
} );

Expand All @@ -116,7 +107,7 @@ export default class MaterialControlNode extends VBox {
const comboBox = new ComboBox( materialProperty, [
...regularMaterials.map( materialToItem ),
...( options.supportCustomMaterial ? [ {
value: customMaterial,
value: options.customMaterial!,
createNode: () => new Text( DensityBuoyancyCommonStrings.material.customStringProperty, {
font: DensityBuoyancyCommonConstants.COMBO_BOX_ITEM_FONT,
maxWidth: comboMaxWidth
Expand Down

0 comments on commit 3465e4c

Please sign in to comment.