From df2d65111f1dc4cba9220975e4fd6495098863fb Mon Sep 17 00:00:00 2001 From: Michael Kauzmann Date: Wed, 28 Aug 2024 10:49:47 -0600 Subject: [PATCH] minor refactor to share grabDragModel instances, https://github.com/phetsims/density-buoyancy-common/issues/368 Signed-off-by: Michael Kauzmann --- js/buoyancy/model/BuoyancyCompareModel.ts | 20 +++------- .../model/shapes/BuoyancyShapesModel.ts | 6 +-- js/common/model/CompareBlockSetModel.ts | 8 ++-- js/density/model/DensityCompareModel.ts | 38 ++++++------------- 4 files changed, 25 insertions(+), 47 deletions(-) diff --git a/js/buoyancy/model/BuoyancyCompareModel.ts b/js/buoyancy/model/BuoyancyCompareModel.ts index bb901537..07fa1fa2 100644 --- a/js/buoyancy/model/BuoyancyCompareModel.ts +++ b/js/buoyancy/model/BuoyancyCompareModel.ts @@ -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; @@ -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()( { fluidSelectionType: 'simple', @@ -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, @@ -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' ) } } ], diff --git a/js/buoyancy/model/shapes/BuoyancyShapesModel.ts b/js/buoyancy/model/shapes/BuoyancyShapesModel.ts index f23e1bb1..291e3e69 100644 --- a/js/buoyancy/model/shapes/BuoyancyShapesModel.ts +++ b/js/buoyancy/model/shapes/BuoyancyShapesModel.ts @@ -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 ) { @@ -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, @@ -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; diff --git a/js/common/model/CompareBlockSetModel.ts b/js/common/model/CompareBlockSetModel.ts index e0868d6e..f9c95334 100644 --- a/js/common/model/CompareBlockSetModel.ts +++ b/js/common/model/CompareBlockSetModel.ts @@ -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' ); @@ -37,9 +38,9 @@ type CubeData = { sameVolumeMass: number; sameDensityVolume: number; colorProperty: Property; - sameMassCubeOptions: WithRequired, 'tandem' | 'grabDragModel'>; - sameVolumeCubeOptions: WithRequired, 'tandem' | 'grabDragModel'>; - sameDensityCubeOptions: WithRequired, 'tandem' | 'grabDragModel'>; + sameMassCubeOptions: WithRequired, 'tandem'>; + sameVolumeCubeOptions: WithRequired, 'tandem'>; + sameDensityCubeOptions: WithRequired, 'tandem'>; }; type SelfOptions = { @@ -92,6 +93,7 @@ export default class CompareBlockSetModel extends BlockSetModel { initialMaterials: [], sharedCubeOptions: { + grabDragModel: new GrabDragModel(), materialPropertyOptions: { phetioReadOnly: true // See https://github.com/phetsims/density-buoyancy-common/issues/270#issuecomment-2243371397 } diff --git a/js/density/model/DensityCompareModel.ts b/js/density/model/DensityCompareModel.ts index 1db9d94f..942bfc75 100644 --- a/js/density/model/DensityCompareModel.ts +++ b/js/density/model/DensityCompareModel.ts @@ -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; @@ -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()( { fluidSelectionType: 'justWater', @@ -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, @@ -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, @@ -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, @@ -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 );