From 3465e4ca8c33d2a2b44c1edaa18f236b31015218 Mon Sep 17 00:00:00 2001 From: Michael Kauzmann Date: Fri, 12 Jul 2024 13:46:22 -0600 Subject: [PATCH] fix bottle custom density slider, https://github.com/phetsims/density-buoyancy-common/issues/256 Signed-off-by: Michael Kauzmann --- js/buoyancy/model/applications/Bottle.ts | 5 ++-- .../BuoyancyApplicationsScreenView.ts | 24 +++++-------------- js/common/view/MaterialControlNode.ts | 23 ++++++------------ 3 files changed, 16 insertions(+), 36 deletions(-) diff --git a/js/buoyancy/model/applications/Bottle.ts b/js/buoyancy/model/applications/Bottle.ts index 435ff2ca..555a55db 100644 --- a/js/buoyancy/model/applications/Bottle.ts +++ b/js/buoyancy/model/applications/Bottle.ts @@ -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; + // TODO: rename to materialInsideMassProperty, https://github.com/phetsims/density-buoyancy-common/issues/256 + public readonly interiorMassProperty: ReadOnlyProperty; public constructor( engine: PhysicsEngine, providedOptions: BottleOptions ) { @@ -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()( { diff --git a/js/buoyancy/view/applications/BuoyancyApplicationsScreenView.ts b/js/buoyancy/view/applications/BuoyancyApplicationsScreenView.ts index d3cdf9d0..e50cb622 100644 --- a/js/buoyancy/view/applications/BuoyancyApplicationsScreenView.ts +++ b/js/buoyancy/view/applications/BuoyancyApplicationsScreenView.ts @@ -113,9 +113,10 @@ export default class BuoyancyApplicationsScreenView extends BuoyancyScreenView 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( { visibleProperty: new GatedVisibleProperty( customDensityControlVisibleProperty, customBottleDensityControlTandem ), @@ -238,7 +225,8 @@ export default class BuoyancyApplicationsScreenView extends BuoyancyScreenView 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 => { diff --git a/js/common/view/MaterialControlNode.ts b/js/common/view/MaterialControlNode.ts index 6e6adbb2..4ea589c1 100644 --- a/js/common/view/MaterialControlNode.ts +++ b/js/common/view/MaterialControlNode.ts @@ -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'; @@ -37,12 +36,13 @@ type SelfMaterialControlNodeOptions = { minCustomVolumeLiters?: number; maxVolumeLiters?: number; + customMaterial?: Material | null; + } & PickRequired; export type MaterialControlNodeOptions = SelfMaterialControlNodeOptions & VBoxOptions; export default class MaterialControlNode extends VBox { - protected customDensityRange: Range; public constructor( materialProperty: Property, volumeProperty: Property, @@ -58,7 +58,8 @@ export default class MaterialControlNode extends VBox { minCustomMass: 0.5, maxCustomMass: 10, minCustomVolumeLiters: 1, - maxVolumeLiters: 10 + maxVolumeLiters: 10, + customMaterial: null }, providedOptions ); super( { @@ -66,12 +67,7 @@ export default class MaterialControlNode extends VBox { 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 ); @@ -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 ); } } ); @@ -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