From 9ba613cc465eeb85a7abf7261c80cd22c022e6ae Mon Sep 17 00:00:00 2001 From: Michael Kauzmann Date: Mon, 21 Aug 2023 15:03:50 -0600 Subject: [PATCH] use decayEnabledPropertyMap to simplify decay button enabledProperties, https://github.com/phetsims/build-a-nucleus/issues/146 --- js/decay/model/DecayModel.ts | 28 ++++++++++------------- js/decay/view/AvailableDecaysPanel.ts | 32 +++++---------------------- js/decay/view/DecayScreenView.ts | 16 +++++--------- 3 files changed, 24 insertions(+), 52 deletions(-) diff --git a/js/decay/model/DecayModel.ts b/js/decay/model/DecayModel.ts index 6bf71ee..bba12dc 100644 --- a/js/decay/model/DecayModel.ts +++ b/js/decay/model/DecayModel.ts @@ -20,12 +20,7 @@ class DecayModel extends BANModel { // the half-life number public halfLifeNumberProperty: TReadOnlyProperty; - // the decay enabled properties for all five decays - public protonEmissionEnabledProperty: TReadOnlyProperty; - public neutronEmissionEnabledProperty: TReadOnlyProperty; - public betaMinusDecayEnabledProperty: TReadOnlyProperty; - public betaPlusDecayEnabledProperty: TReadOnlyProperty; - public alphaDecayEnabledProperty: TReadOnlyProperty; + public decayEnabledPropertyMap: Map>; public constructor() { @@ -86,27 +81,28 @@ class DecayModel extends BANModel { this.hasIncomingParticlesProperty ] as const; + this.decayEnabledPropertyMap = new Map(); // create the decay enabled properties - this.protonEmissionEnabledProperty = new DerivedProperty( dependencies, + this.decayEnabledPropertyMap.set( DecayType.PROTON_EMISSION, new DerivedProperty( dependencies, ( protonNumber, neutronNumber, hasIncomingParticles ) => createDecayEnabledListener( protonNumber, neutronNumber, DecayType.PROTON_EMISSION, hasIncomingParticles ) - ); - this.neutronEmissionEnabledProperty = new DerivedProperty( dependencies, + ) ); + this.decayEnabledPropertyMap.set( DecayType.NEUTRON_EMISSION, new DerivedProperty( dependencies, ( protonNumber, neutronNumber, hasIncomingParticles ) => createDecayEnabledListener( protonNumber, neutronNumber, DecayType.NEUTRON_EMISSION, hasIncomingParticles ) - ); - this.betaMinusDecayEnabledProperty = new DerivedProperty( dependencies, + ) ); + this.decayEnabledPropertyMap.set( DecayType.BETA_MINUS_DECAY, new DerivedProperty( dependencies, ( protonNumber, neutronNumber, hasIncomingParticles ) => createDecayEnabledListener( protonNumber, neutronNumber, DecayType.BETA_MINUS_DECAY, hasIncomingParticles ) - ); - this.betaPlusDecayEnabledProperty = new DerivedProperty( dependencies, + ) ); + this.decayEnabledPropertyMap.set( DecayType.BETA_PLUS_DECAY, new DerivedProperty( dependencies, ( protonNumber, neutronNumber, hasIncomingParticles ) => createDecayEnabledListener( protonNumber, neutronNumber, DecayType.BETA_PLUS_DECAY, hasIncomingParticles ) - ); - this.alphaDecayEnabledProperty = new DerivedProperty( dependencies, + ) ); + this.decayEnabledPropertyMap.set( DecayType.ALPHA_DECAY, new DerivedProperty( dependencies, ( protonNumber, neutronNumber, hasIncomingParticles ) => createDecayEnabledListener( protonNumber, neutronNumber, DecayType.ALPHA_DECAY, hasIncomingParticles ) - ); + ) ); } public override reset(): void { diff --git a/js/decay/view/AvailableDecaysPanel.ts b/js/decay/view/AvailableDecaysPanel.ts index af7e693..ab8d960 100644 --- a/js/decay/view/AvailableDecaysPanel.ts +++ b/js/decay/view/AvailableDecaysPanel.ts @@ -32,6 +32,7 @@ const BUTTON_CONTENT_WIDTH = 145; type decayTypeButtonIndexType = Record; type SelfOptions = { + decayEnabledPropertyMap: Map>; decayAtom: ( decayType: DecayType ) => void; @@ -51,12 +52,7 @@ class AvailableDecaysPanel extends Panel { // map of decayType => {arrayIndex} public decayTypeButtonIndexMap: decayTypeButtonIndexType; - public constructor( neutronEmissionEnabledProperty: TReadOnlyProperty, - protonEmissionEnabledProperty: TReadOnlyProperty, - betaPlusDecayEnabledProperty: TReadOnlyProperty, - betaMinusDecayEnabledProperty: TReadOnlyProperty, - alphaDecayEnabledProperty: TReadOnlyProperty, - options: AvailableDecaysPanelOptions ) { + public constructor( options: AvailableDecaysPanelOptions ) { // create and add the title const titleNode = new Text( BuildANucleusStrings.availableDecaysStringProperty, { @@ -76,25 +72,6 @@ class AvailableDecaysPanel extends Panel { baseColor: BANColors.availableDecaysInfoButtonColorProperty } ); - // function to return the correct enabled DerivedProperty for each type of decay - const returnEnabledDecayButtonProperty = ( decayType: DecayType ): TReadOnlyProperty => { - switch( decayType ) { - case DecayType.NEUTRON_EMISSION: - return neutronEmissionEnabledProperty; - case DecayType.PROTON_EMISSION: - return protonEmissionEnabledProperty; - case DecayType.BETA_PLUS_DECAY: - return betaPlusDecayEnabledProperty; - case DecayType.BETA_MINUS_DECAY: - return betaMinusDecayEnabledProperty; - case DecayType.ALPHA_DECAY: - return alphaDecayEnabledProperty; - default: - assert && assert( false, 'No valid decay type found: ' + decayType ); - return protonEmissionEnabledProperty; - } - }; - // function that creates the listeners for the decay buttons. Emits the specified particle depending on the decay type const createDecayButtonListener = ( decayType: DecayType ) => { options.storeNucleonNumbers(); @@ -117,11 +94,14 @@ class AvailableDecaysPanel extends Panel { } ); buttonBackgroundRectangle.addChild( buttonText ); + const enabledProperty = options.decayEnabledPropertyMap.get( decayType )!; + assert && assert( enabledProperty, 'No enabledProperty found, is your decay type valid? ' + decayType ); + return new RectangularPushButton( { content: buttonBackgroundRectangle, yMargin: 0, baseColor: BANColors.decayButtonColorProperty, - enabledProperty: returnEnabledDecayButtonProperty( decayType ), + enabledProperty: enabledProperty, listener: () => { createDecayButtonListener( decayType ); } } ); }; diff --git a/js/decay/view/DecayScreenView.ts b/js/decay/view/DecayScreenView.ts index 2961323..1006c79 100644 --- a/js/decay/view/DecayScreenView.ts +++ b/js/decay/view/DecayScreenView.ts @@ -135,16 +135,12 @@ class DecayScreenView extends BANScreenView { } ); // create and add the available decays panel at the center right of the decay screen - const availableDecaysPanel = new AvailableDecaysPanel( - model.neutronEmissionEnabledProperty, - model.protonEmissionEnabledProperty, - model.betaPlusDecayEnabledProperty, - model.betaMinusDecayEnabledProperty, - model.alphaDecayEnabledProperty, { - decayAtom: this.decayAtom.bind( this ), - storeNucleonNumbers: storeNucleonNumbers.bind( this ), - showAndRepositionUndoDecayButton: showAndRepositionUndoDecayButton.bind( this ) - } ); + const availableDecaysPanel = new AvailableDecaysPanel( { + decayEnabledPropertyMap: model.decayEnabledPropertyMap, + decayAtom: this.decayAtom.bind( this ), + storeNucleonNumbers: storeNucleonNumbers.bind( this ), + showAndRepositionUndoDecayButton: showAndRepositionUndoDecayButton.bind( this ) + } ); availableDecaysPanel.right = this.symbolAccordionBox.right; availableDecaysPanel.top = this.symbolAccordionBox.bottom + 10; this.addChild( availableDecaysPanel );