Skip to content

Commit

Permalink
minor refactor to share grabDragModel instances, #368
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Kauzmann <[email protected]>
  • Loading branch information
zepumph committed Aug 28, 2024
1 parent 85cf178 commit df2d651
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 47 deletions.
20 changes: 6 additions & 14 deletions js/buoyancy/model/BuoyancyCompareModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import DensityBuoyancyCommonColors from '../../common/view/DensityBuoyancyCommon
import CompareBlockSetModel, { BLOCK_SETS_TANDEM_NAME, CompareBlockSetModelOptions } from '../../common/model/CompareBlockSetModel.js';
import StrictOmit from '../../../../phet-core/js/types/StrictOmit.js';
import Material from '../../common/model/Material.js';
import { GrabDragModel } from '../../../../scenery-phet/js/accessibility/GrabDragInteraction.js';

export type BuoyancyCompareModelOptions = StrictOmit<CompareBlockSetModelOptions, 'positionMassesCallback' | 'cubesData'>;

Expand All @@ -26,7 +25,6 @@ export default class BuoyancyCompareModel extends CompareBlockSetModel {

const tandem = providedOptions.tandem;
const blockSetsTandem = tandem.createTandem( BLOCK_SETS_TANDEM_NAME );
const grabDragModel = new GrabDragModel();

const options = optionize<BuoyancyCompareModelOptions, EmptySelfOptions, CompareBlockSetModelOptions>()( {
fluidSelectionType: 'simple',
Expand All @@ -41,18 +39,15 @@ export default class BuoyancyCompareModel extends CompareBlockSetModel {
colorProperty: DensityBuoyancyCommonColors.compareOchreColorProperty,
sameMassCubeOptions: {
tag: MassTag.ONE_A.withColorProperty( MassTag.OBJECT_A_COLOR_PROPERTY ),
tandem: blockSetsTandem.createTandem( BlockSet.SAME_MASS.tandemName ).createTandem( 'block1A' ),
grabDragModel: grabDragModel
tandem: blockSetsTandem.createTandem( BlockSet.SAME_MASS.tandemName ).createTandem( 'block1A' )
},
sameVolumeCubeOptions: {
tag: MassTag.TWO_A.withColorProperty( MassTag.OBJECT_A_COLOR_PROPERTY ),
tandem: blockSetsTandem.createTandem( BlockSet.SAME_VOLUME.tandemName ).createTandem( 'block2A' ),
grabDragModel: grabDragModel
tandem: blockSetsTandem.createTandem( BlockSet.SAME_VOLUME.tandemName ).createTandem( 'block2A' )
},
sameDensityCubeOptions: {
tag: MassTag.THREE_A.withColorProperty( MassTag.OBJECT_A_COLOR_PROPERTY ),
tandem: blockSetsTandem.createTandem( BlockSet.SAME_DENSITY.tandemName ).createTandem( 'block3A' ),
grabDragModel: grabDragModel
tandem: blockSetsTandem.createTandem( BlockSet.SAME_DENSITY.tandemName ).createTandem( 'block3A' )
}
}, {
sameMassVolume: 0.01,
Expand All @@ -61,18 +56,15 @@ export default class BuoyancyCompareModel extends CompareBlockSetModel {
colorProperty: DensityBuoyancyCommonColors.compareBlueColorProperty,
sameMassCubeOptions: {
tag: MassTag.ONE_B.withColorProperty( MassTag.OBJECT_B_COLOR_PROPERTY ),
tandem: blockSetsTandem.createTandem( BlockSet.SAME_MASS.tandemName ).createTandem( 'block1B' ),
grabDragModel: grabDragModel
tandem: blockSetsTandem.createTandem( BlockSet.SAME_MASS.tandemName ).createTandem( 'block1B' )
},
sameVolumeCubeOptions: {
tag: MassTag.TWO_B.withColorProperty( MassTag.OBJECT_B_COLOR_PROPERTY ),
tandem: blockSetsTandem.createTandem( BlockSet.SAME_VOLUME.tandemName ).createTandem( 'block2B' ),
grabDragModel: grabDragModel
tandem: blockSetsTandem.createTandem( BlockSet.SAME_VOLUME.tandemName ).createTandem( 'block2B' )
},
sameDensityCubeOptions: {
tag: MassTag.THREE_B.withColorProperty( MassTag.OBJECT_B_COLOR_PROPERTY ),
tandem: blockSetsTandem.createTandem( BlockSet.SAME_DENSITY.tandemName ).createTandem( 'block3B' ),
grabDragModel: grabDragModel
tandem: blockSetsTandem.createTandem( BlockSet.SAME_DENSITY.tandemName ).createTandem( 'block3B' )
}
}
],
Expand Down
6 changes: 2 additions & 4 deletions js/buoyancy/model/shapes/BuoyancyShapesModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ export default class BuoyancyShapesModel extends DensityBuoyancyModel {
public readonly materialProperty: MaterialProperty;
private readonly availableMaterials: Material[];

// @ts-expect-error
private myGrabDragModel: GrabDragModel | null; // do not initialize to null or it will be overridden after superconstructor
private readonly grabDragModel = new GrabDragModel();

public constructor( providedOptions: BuoyancyShapesModelOptions ) {

Expand Down Expand Up @@ -100,7 +99,6 @@ export default class BuoyancyShapesModel extends DensityBuoyancyModel {
}

private createMass( tandem: Tandem, shape: MassShape, widthRatio: number, heightRatio: number, tag: MassTag ): Mass {
this.myGrabDragModel = this.myGrabDragModel || new GrabDragModel();
const massOptions = {
material: this.materialProperty.value,
availableMassMaterials: this.availableMaterials,
Expand All @@ -111,7 +109,7 @@ export default class BuoyancyShapesModel extends DensityBuoyancyModel {
maxVolume: Cuboid.MAX_VOLUME, // Cubes are the highest volume object in this screen
tandem: tandem,
tag: tag,
grabDragModel: this.myGrabDragModel
grabDragModel: this.grabDragModel // All Mass shapes except the scale share the same interaction model, see https://github.com/phetsims/density-buoyancy-common/issues/368
};

let mass: Mass;
Expand Down
8 changes: 5 additions & 3 deletions js/common/model/CompareBlockSetModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import Multilink from '../../../../axon/js/Multilink.js';
import PhysicsEngine from './PhysicsEngine.js';
import densityBuoyancyCommon from '../../densityBuoyancyCommon.js';
import { MaterialSchema } from './Mass.js';
import { GrabDragModel } from '../../../../scenery-phet/js/accessibility/GrabDragInteraction.js';

assert && assert( BlockSet.enumeration.values.length === 3, 'This class is very hard coded for the three "SAME" values of BlockSet' );

Expand All @@ -37,9 +38,9 @@ type CubeData = {
sameVolumeMass: number;
sameDensityVolume: number;
colorProperty: Property<Color>;
sameMassCubeOptions: WithRequired<Partial<CubeOptions>, 'tandem' | 'grabDragModel'>;
sameVolumeCubeOptions: WithRequired<Partial<CubeOptions>, 'tandem' | 'grabDragModel'>;
sameDensityCubeOptions: WithRequired<Partial<CubeOptions>, 'tandem' | 'grabDragModel'>;
sameMassCubeOptions: WithRequired<Partial<CubeOptions>, 'tandem'>;
sameVolumeCubeOptions: WithRequired<Partial<CubeOptions>, 'tandem'>;
sameDensityCubeOptions: WithRequired<Partial<CubeOptions>, 'tandem'>;
};

type SelfOptions = {
Expand Down Expand Up @@ -92,6 +93,7 @@ export default class CompareBlockSetModel extends BlockSetModel<BlockSet> {
initialMaterials: [],

sharedCubeOptions: {
grabDragModel: new GrabDragModel(),
materialPropertyOptions: {
phetioReadOnly: true // See https://github.com/phetsims/density-buoyancy-common/issues/270#issuecomment-2243371397
}
Expand Down
38 changes: 12 additions & 26 deletions js/density/model/DensityCompareModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import MassTag from '../../common/model/MassTag.js';
import BlockSet from '../../common/model/BlockSet.js';
import CompareBlockSetModel, { CompareBlockSetModelOptions } from '../../common/model/CompareBlockSetModel.js';
import Range from '../../../../dot/js/Range.js';
import { GrabDragModel } from '../../../../scenery-phet/js/accessibility/GrabDragInteraction.js';

export type DensityCompareModelOptions = StrictOmit<CompareBlockSetModelOptions, 'positionMassesCallback' | 'cubesData'>;

Expand All @@ -26,7 +25,6 @@ export default class DensityCompareModel extends CompareBlockSetModel {
const tandem = providedOptions.tandem;

const blockSetsTandem = tandem.createTandem( 'blockSets' );
const grabDragModel = new GrabDragModel();

const options = optionize<DensityCompareModelOptions, EmptySelfOptions, CompareBlockSetModelOptions>()( {
fluidSelectionType: 'justWater',
Expand Down Expand Up @@ -61,18 +59,15 @@ export default class DensityCompareModel extends CompareBlockSetModel {
colorProperty: DensityBuoyancyCommonColors.compareYellowColorProperty,
sameMassCubeOptions: {
tag: MassTag.B,
tandem: blockSetsTandem.createTandem( BlockSet.SAME_MASS.tandemName ).createTandem( 'blockB' ),
grabDragModel: grabDragModel
tandem: blockSetsTandem.createTandem( BlockSet.SAME_MASS.tandemName ).createTandem( 'blockB' )
},
sameVolumeCubeOptions: {
tag: MassTag.A,
tandem: blockSetsTandem.createTandem( BlockSet.SAME_VOLUME.tandemName ).createTandem( 'blockA' ),
grabDragModel: grabDragModel
tandem: blockSetsTandem.createTandem( BlockSet.SAME_VOLUME.tandemName ).createTandem( 'blockA' )
},
sameDensityCubeOptions: {
tag: MassTag.B,
tandem: blockSetsTandem.createTandem( BlockSet.SAME_DENSITY.tandemName ).createTandem( 'blockB' ),
grabDragModel: grabDragModel
tandem: blockSetsTandem.createTandem( BlockSet.SAME_DENSITY.tandemName ).createTandem( 'blockB' )
}
}, {
sameMassVolume: 0.005,
Expand All @@ -81,18 +76,15 @@ export default class DensityCompareModel extends CompareBlockSetModel {
colorProperty: DensityBuoyancyCommonColors.compareBlueColorProperty,
sameMassCubeOptions: {
tag: MassTag.A,
tandem: blockSetsTandem.createTandem( BlockSet.SAME_MASS.tandemName ).createTandem( 'blockA' ),
grabDragModel: grabDragModel
tandem: blockSetsTandem.createTandem( BlockSet.SAME_MASS.tandemName ).createTandem( 'blockA' )
},
sameVolumeCubeOptions: {
tag: MassTag.C,
tandem: blockSetsTandem.createTandem( BlockSet.SAME_VOLUME.tandemName ).createTandem( 'blockC' ),
grabDragModel: grabDragModel
tandem: blockSetsTandem.createTandem( BlockSet.SAME_VOLUME.tandemName ).createTandem( 'blockC' )
},
sameDensityCubeOptions: {
tag: MassTag.A,
tandem: blockSetsTandem.createTandem( BlockSet.SAME_DENSITY.tandemName ).createTandem( 'blockA' ),
grabDragModel: grabDragModel
tandem: blockSetsTandem.createTandem( BlockSet.SAME_DENSITY.tandemName ).createTandem( 'blockA' )
}
}, {
sameMassVolume: 0.0025,
Expand All @@ -101,18 +93,15 @@ export default class DensityCompareModel extends CompareBlockSetModel {
colorProperty: DensityBuoyancyCommonColors.compareGreenColorProperty,
sameMassCubeOptions: {
tag: MassTag.C,
tandem: blockSetsTandem.createTandem( BlockSet.SAME_MASS.tandemName ).createTandem( 'blockC' ),
grabDragModel: grabDragModel
tandem: blockSetsTandem.createTandem( BlockSet.SAME_MASS.tandemName ).createTandem( 'blockC' )
},
sameVolumeCubeOptions: {
tag: MassTag.D,
tandem: blockSetsTandem.createTandem( BlockSet.SAME_VOLUME.tandemName ).createTandem( 'blockD' ),
grabDragModel: grabDragModel
tandem: blockSetsTandem.createTandem( BlockSet.SAME_VOLUME.tandemName ).createTandem( 'blockD' )
},
sameDensityCubeOptions: {
tag: MassTag.C,
tandem: blockSetsTandem.createTandem( BlockSet.SAME_DENSITY.tandemName ).createTandem( 'blockC' ),
grabDragModel: grabDragModel
tandem: blockSetsTandem.createTandem( BlockSet.SAME_DENSITY.tandemName ).createTandem( 'blockC' )
}
}, {
sameMassVolume: 0.00125,
Expand All @@ -121,18 +110,15 @@ export default class DensityCompareModel extends CompareBlockSetModel {
colorProperty: DensityBuoyancyCommonColors.compareRedColorProperty,
sameMassCubeOptions: {
tag: MassTag.D,
tandem: blockSetsTandem.createTandem( BlockSet.SAME_MASS.tandemName ).createTandem( 'blockD' ),
grabDragModel: grabDragModel
tandem: blockSetsTandem.createTandem( BlockSet.SAME_MASS.tandemName ).createTandem( 'blockD' )
},
sameVolumeCubeOptions: {
tag: MassTag.B,
tandem: blockSetsTandem.createTandem( BlockSet.SAME_VOLUME.tandemName ).createTandem( 'blockB' ),
grabDragModel: grabDragModel
tandem: blockSetsTandem.createTandem( BlockSet.SAME_VOLUME.tandemName ).createTandem( 'blockB' )
},
sameDensityCubeOptions: {
tag: MassTag.D,
tandem: blockSetsTandem.createTandem( BlockSet.SAME_DENSITY.tandemName ).createTandem( 'blockD' ),
grabDragModel: grabDragModel
tandem: blockSetsTandem.createTandem( BlockSet.SAME_DENSITY.tandemName ).createTandem( 'blockD' )
}
} ]
}, providedOptions );
Expand Down

0 comments on commit df2d651

Please sign in to comment.