Skip to content

Commit

Permalink
Custom range to DensityNumberLineNode see phetsims/buoyancy-basics#6
Browse files Browse the repository at this point in the history
  • Loading branch information
AgustinVallejo committed May 1, 2024
1 parent 0795d84 commit a708cd7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
9 changes: 9 additions & 0 deletions js/buoyancy-basics/view/BuoyancyBasicsExploreScreenView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ export default class BuoyancyBasicsExploreScreenView extends DensityBuoyancyScre
new DerivedProperty( [ model.secondaryMass.materialProperty ], material => material.density ),
model.secondaryMass.visibleProperty,
{
materials: [
Material.HUMAN,
Material.GLASS,
Material.TITANIUM,
Material.STEEL,
Material.LEAD,
Material.MERCURY
],
maxDensity: 15000,
tandem: accordionTandem.createTandem( 'densityReadout' ),
visiblePropertyOptions: {
phetioReadOnly: true
Expand Down
20 changes: 11 additions & 9 deletions js/density/view/DensityNumberLineNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type SelfOptions = {
height?: number;
maxDensity?: number;
linePadding?: number;
mvt?: ( density: number ) => number;
maxLabelWidth?: number;
};

Expand Down Expand Up @@ -58,10 +57,13 @@ export default class DensityNumberLineNode extends Node {
height: HEIGHT,
maxDensity: MAX_DENSITY,
linePadding: 2,
mvt: ( density: number ) => WIDTH * Math.min( density, MAX_DENSITY ) / MAX_DENSITY,
maxLabelWidth: 80
}, providedOptions );

const modelViewTransform = ( density: number ) => {
return options.width * Math.min( density, options.maxDensity ) / options.maxDensity;
};

super();

const background = new Rectangle( 0, 0, options.width, options.height, {
Expand All @@ -75,7 +77,7 @@ export default class DensityNumberLineNode extends Node {

const lineOptions = { stroke: 'black' };
options.materials.forEach( ( material, index ) => {
const x = options.mvt( material.density );
const x = modelViewTransform( material.density );
const label = new Text( material.nameProperty, {
font: new PhetFont( 12 ),
maxWidth: options.materialsMaxWidths[ index ]
Expand All @@ -99,7 +101,7 @@ export default class DensityNumberLineNode extends Node {
font: DensityBuoyancyCommonConstants.ITEM_FONT
} ) );

this.addChild( new Text( '10', {
this.addChild( new Text( options.maxDensity / DensityBuoyancyCommonConstants.LITERS_IN_CUBIC_METER, {
left: options.width + 10,
centerY: background.centerY,
font: DensityBuoyancyCommonConstants.ITEM_FONT
Expand All @@ -124,7 +126,7 @@ export default class DensityNumberLineNode extends Node {
value: densityProperty
}, {
maps: {
value: ( density: number ) => density / 1000
value: ( density: number ) => density / DensityBuoyancyCommonConstants.LITERS_IN_CUBIC_METER
},
tandem: Tandem.OPT_OUT,
decimalPlaces: 2
Expand Down Expand Up @@ -167,26 +169,26 @@ export default class DensityNumberLineNode extends Node {
// Density links
// This instance lives for the lifetime of the simulation, so we don't need to remove this listener
densityAProperty.link( density => {
primaryMarker.x = options.mvt( density );
primaryMarker.x = modelViewTransform( density );
} );
ManualConstraint.create( this, [ primaryLabelContainer, primaryArrow ], ( primaryLabelContainerProxy, primaryArrowProxy ) => {
primaryLabelContainerProxy.centerBottom = primaryArrowProxy.centerTop;
} );

// This instance lives for the lifetime of the simulation, so we don't need to remove this listener
densityBProperty.link( density => {
secondaryMarker.x = options.mvt( density );
secondaryMarker.x = modelViewTransform( density );
} );
ManualConstraint.create( this, [ secondaryLabelContainer, secondaryArrow ], ( secondaryLabelContainerProxy, secondaryArrowProxy ) => {
secondaryLabelContainerProxy.centerTop = secondaryArrowProxy.centerBottom;
} );

// This instance lives for the lifetime of the simulation, so we don't need to remove this listener
densityAProperty.link( density => {
primaryMarker.visible = density < MAX_DENSITY + 1e-5; // Allow rounding error
primaryMarker.visible = density < options.maxDensity + 1e-5; // Allow rounding error
} );
Multilink.multilink( [ secondaryMassVisibleProperty, densityBProperty ], ( visible, density ) => {
secondaryMarker.visible = visible && density < MAX_DENSITY + 1e-5; // Allow rounding error
secondaryMarker.visible = visible && density < options.maxDensity + 1e-5; // Allow rounding error
} );

this.mutate( options );
Expand Down

0 comments on commit a708cd7

Please sign in to comment.