Skip to content

Commit

Permalink
use decayEnabledPropertyMap to simplify decay button enabledProperties,
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Aug 21, 2023
1 parent 8474147 commit 9ba613c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 52 deletions.
28 changes: 12 additions & 16 deletions js/decay/model/DecayModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ class DecayModel extends BANModel<ParticleAtom> {
// the half-life number
public halfLifeNumberProperty: TReadOnlyProperty<number>;

// the decay enabled properties for all five decays
public protonEmissionEnabledProperty: TReadOnlyProperty<boolean>;
public neutronEmissionEnabledProperty: TReadOnlyProperty<boolean>;
public betaMinusDecayEnabledProperty: TReadOnlyProperty<boolean>;
public betaPlusDecayEnabledProperty: TReadOnlyProperty<boolean>;
public alphaDecayEnabledProperty: TReadOnlyProperty<boolean>;
public decayEnabledPropertyMap: Map<DecayType, TReadOnlyProperty<boolean>>;

public constructor() {

Expand Down Expand Up @@ -86,27 +81,28 @@ class DecayModel extends BANModel<ParticleAtom> {
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 {
Expand Down
32 changes: 6 additions & 26 deletions js/decay/view/AvailableDecaysPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const BUTTON_CONTENT_WIDTH = 145;

type decayTypeButtonIndexType = Record<string, number>;
type SelfOptions = {
decayEnabledPropertyMap: Map<DecayType, TReadOnlyProperty<boolean>>;

decayAtom: ( decayType: DecayType ) => void;

Expand All @@ -51,12 +52,7 @@ class AvailableDecaysPanel extends Panel {
// map of decayType => {arrayIndex}
public decayTypeButtonIndexMap: decayTypeButtonIndexType;

public constructor( neutronEmissionEnabledProperty: TReadOnlyProperty<boolean>,
protonEmissionEnabledProperty: TReadOnlyProperty<boolean>,
betaPlusDecayEnabledProperty: TReadOnlyProperty<boolean>,
betaMinusDecayEnabledProperty: TReadOnlyProperty<boolean>,
alphaDecayEnabledProperty: TReadOnlyProperty<boolean>,
options: AvailableDecaysPanelOptions ) {
public constructor( options: AvailableDecaysPanelOptions ) {

// create and add the title
const titleNode = new Text( BuildANucleusStrings.availableDecaysStringProperty, {
Expand All @@ -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<boolean> => {
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();
Expand All @@ -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 ); }
} );
};
Expand Down
16 changes: 6 additions & 10 deletions js/decay/view/DecayScreenView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,12 @@ class DecayScreenView extends BANScreenView<DecayModel> {
} );

// 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 );
Expand Down

0 comments on commit 9ba613c

Please sign in to comment.