Skip to content

Commit

Permalink
inline logic used in single closures, combine callback options, #146
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Aug 21, 2023
1 parent 9ba613c commit 0b9df3d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 39 deletions.
38 changes: 12 additions & 26 deletions js/decay/view/AvailableDecaysPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,8 @@ type decayTypeButtonIndexType = Record<string, number>;
type SelfOptions = {
decayEnabledPropertyMap: Map<DecayType, TReadOnlyProperty<boolean>>;

decayAtom: ( decayType: DecayType ) => void;

// function to store current nucleon numbers
storeNucleonNumbers: () => void;

// function to show and reposition the undo decay button
showAndRepositionUndoDecayButton: ( decayType: string ) => void;
// Upon any decay button firing, this listener fires with the given decay type
handleDecayListener: ( decayType: DecayType ) => void;
};
export type AvailableDecaysPanelOptions = SelfOptions;

Expand Down Expand Up @@ -72,16 +67,9 @@ class AvailableDecaysPanel extends Panel {
baseColor: BANColors.availableDecaysInfoButtonColorProperty
} );

// function that creates the listeners for the decay buttons. Emits the specified particle depending on the decay type
const createDecayButtonListener = ( decayType: DecayType ) => {
options.storeNucleonNumbers();
options.decayAtom( decayType );
options.showAndRepositionUndoDecayButton( decayType.name.toString() );
};
// function to create the decay button and corresponding decay icon pair
const createDecayButtonAndIcon = ( decayType: DecayType ): Node => {

// function to create the decay buttons
// manually layout the button text due to the superscripts causing the normal layout to look out of place
const createDecayButton = ( decayType: DecayType ): Node => {
const buttonBackgroundRectangle = new Rectangle( 0, 0, BUTTON_CONTENT_WIDTH, BUTTON_HEIGHT );
const buttonText = new RichText( decayType.labelStringProperty, {
font: LABEL_FONT,
Expand All @@ -90,27 +78,25 @@ class AvailableDecaysPanel extends Panel {

buttonText.boundsProperty.link( () => {
assert && assert( BUTTON_TEXT_BOTTOM_MARGIN + buttonText.height < BUTTON_HEIGHT, 'The button text is changing the size of the button.' );

// manually layout the button text due to the superscripts causing the normal layout to look out of place
buttonText.centerBottom = buttonBackgroundRectangle.centerBottom.minusXY( 0, BUTTON_TEXT_BOTTOM_MARGIN );
} );
buttonBackgroundRectangle.addChild( buttonText );

const enabledProperty = options.decayEnabledPropertyMap.get( decayType )!;
assert && assert( enabledProperty, 'No enabledProperty found, is your decay type valid? ' + decayType );

return new RectangularPushButton( {
const decayButton = new RectangularPushButton( {
content: buttonBackgroundRectangle,
yMargin: 0,
baseColor: BANColors.decayButtonColorProperty,
enabledProperty: enabledProperty,
listener: () => { createDecayButtonListener( decayType ); }
listener: () => options.handleDecayListener( decayType )
} );
};

// function to create the decay button and corresponding decay icon pair
const createDecayButtonAndIcon = ( decayType: DecayType ): Node => {
return new HBox( {
children: [
createDecayButton( decayType ),
decayButton,

// createDecayButtonAndIcon is called when looping through the DecayType enumeration values so null won't be returned
IconFactory.createDecayIcon( decayType )!
Expand Down Expand Up @@ -141,16 +127,16 @@ class AvailableDecaysPanel extends Panel {

// create and add the particle labels
// a particle label is a particle node on the left with its corresponding particle name on the right
const createParticleLabel = ( particleType: ParticleType ): Node => {
const particleLabels = ParticleType.enumeration.values.map( particleType => {
return new HBox( {
children: [
IconFactory.createParticleNode( particleType ),
new Text( particleType.labelStringProperty, { font: LABEL_FONT, maxWidth: 110 } )
],
spacing: SPACING
} );
};
const particleLabels = ParticleType.enumeration.values.map( particleType => createParticleLabel( particleType ) );
} );

const createParticleLabelsVBox = ( particleLabels: Node[] ) => {
return new VBox( {
children: particleLabels,
Expand Down
20 changes: 7 additions & 13 deletions js/decay/view/DecayScreenView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ class DecayScreenView extends BANScreenView<DecayModel> {
// store the current nucleon numbers
let oldProtonNumber: number;
let oldNeutronNumber: number;
const storeNucleonNumbers = () => {
oldProtonNumber = this.model.particleAtom.protonCountProperty.value;
oldNeutronNumber = this.model.particleAtom.neutronCountProperty.value;
};

// create and add the undo decay button
const undoDecayButton = new ReturnButton( {
Expand All @@ -121,12 +117,6 @@ class DecayScreenView extends BANScreenView<DecayModel> {
undoDecayButton.visible = false;
this.addChild( undoDecayButton );

// show the undoDecayButton
const showAndRepositionUndoDecayButton = ( decayType: string ) => {
repositionUndoDecayButton( decayType );
undoDecayButton.visible = true;
};

// hide the undo decay button if anything in the nucleus changes
Multilink.multilink( [ this.model.particleAtom.massNumberProperty, this.model.userControlledProtons.lengthProperty,
this.model.incomingProtons.lengthProperty, this.model.incomingNeutrons.lengthProperty,
Expand All @@ -137,9 +127,13 @@ class DecayScreenView extends BANScreenView<DecayModel> {
// create and add the available decays panel at the center right of the decay screen
const availableDecaysPanel = new AvailableDecaysPanel( {
decayEnabledPropertyMap: model.decayEnabledPropertyMap,
decayAtom: this.decayAtom.bind( this ),
storeNucleonNumbers: storeNucleonNumbers.bind( this ),
showAndRepositionUndoDecayButton: showAndRepositionUndoDecayButton.bind( this )
handleDecayListener: decayType => {
oldProtonNumber = this.model.particleAtom.protonCountProperty.value;
oldNeutronNumber = this.model.particleAtom.neutronCountProperty.value;
this.decayAtom( decayType );
repositionUndoDecayButton( decayType.name.toString() );
undoDecayButton.visible = true;
}
} );
availableDecaysPanel.right = this.symbolAccordionBox.right;
availableDecaysPanel.top = this.symbolAccordionBox.bottom + 10;
Expand Down

0 comments on commit 0b9df3d

Please sign in to comment.