-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDecayScreenView.ts
327 lines (277 loc) · 14 KB
/
DecayScreenView.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright 2022-2023, University of Colorado Boulder
/**
* ScreenView for the 'Decay' screen.
*
* @author Luisa Vargas
*/
import buildANucleus from '../../buildANucleus.js';
import DecayModel from '../model/DecayModel.js';
import optionize, { EmptySelfOptions } from '../../../../phet-core/js/optionize.js';
import BANScreenView, { BANScreenViewOptions } from '../../common/view/BANScreenView.js';
import HalfLifeInformationNode from './HalfLifeInformationNode.js';
import BANConstants from '../../common/BANConstants.js';
import AvailableDecaysPanel from './AvailableDecaysPanel.js';
import SymbolNode from '../../../../shred/js/view/SymbolNode.js';
import AccordionBox from '../../../../sun/js/AccordionBox.js';
import { Circle, Color, HBox, ManualConstraint, Node, RadialGradient, Text } from '../../../../scenery/js/imports.js';
import BuildANucleusStrings from '../../BuildANucleusStrings.js';
import BooleanProperty from '../../../../axon/js/BooleanProperty.js';
import BANColors from '../../common/BANColors.js';
import AtomIdentifier from '../../../../shred/js/AtomIdentifier.js';
import Vector2 from '../../../../dot/js/Vector2.js';
import Property from '../../../../axon/js/Property.js';
import Particle from '../../../../shred/js/model/Particle.js';
import ParticleAtom from '../../../../shred/js/model/ParticleAtom.js';
import ParticleType from '../../common/model/ParticleType.js';
import Checkbox from '../../../../sun/js/Checkbox.js';
import Multilink from '../../../../axon/js/Multilink.js';
import ReturnButton from '../../../../scenery-phet/js/buttons/ReturnButton.js';
import DecayType from '../../common/model/DecayType.js';
import AlphaParticle from '../../common/model/AlphaParticle.js';
// constants
const NUCLEON_CAPTURE_RADIUS = 100;
// types
export type DecayScreenViewOptions = BANScreenViewOptions;
class DecayScreenView extends BANScreenView<DecayModel> {
private readonly stabilityIndicator: Text;
// the symbol node in an accordion box
private readonly symbolAccordionBox: AccordionBox;
// show or hide the electron cloud
private readonly showElectronCloudBooleanProperty: Property<boolean>;
public constructor( model: DecayModel, providedOptions?: DecayScreenViewOptions ) {
const options = optionize<DecayScreenViewOptions, EmptySelfOptions, BANScreenViewOptions>()( {}, providedOptions );
super( model, new Vector2( BANConstants.SCREEN_VIEW_ATOM_CENTER_X, BANConstants.SCREEN_VIEW_ATOM_CENTER_Y ), options );
this.model = model;
// create and add the half-life information node at the top half of the decay screen
const halfLifeInformationNode = new HalfLifeInformationNode( model.halfLifeNumberProperty, model.isStableBooleanProperty,
this.elementNameStringProperty );
halfLifeInformationNode.left = this.layoutBounds.minX + BANConstants.SCREEN_VIEW_X_MARGIN + 30;
halfLifeInformationNode.y = this.layoutBounds.minY + BANConstants.SCREEN_VIEW_Y_MARGIN + 80;
this.addChild( halfLifeInformationNode );
// use this constant since everything else is positioned off of the halfLifeInformationNode and the centerX changes
// as the halfLifeArrow in the halfLifeInformationNode moves
const halfLifeInformationNodeCenterX = halfLifeInformationNode.centerX;
// create and add the symbol node in an accordion box
const symbolNode = new SymbolNode( model.particleAtom.protonCountProperty, model.particleAtom.massNumberProperty, {
scale: 0.3
} );
this.symbolAccordionBox = new AccordionBox( symbolNode, {
titleNode: new Text( BuildANucleusStrings.symbolStringProperty, {
font: BANConstants.REGULAR_FONT,
maxWidth: 118
} ),
fill: BANColors.panelBackgroundColorProperty,
minWidth: 50,
contentAlign: 'center',
contentXMargin: 35,
contentYMargin: 16,
buttonXMargin: 10,
buttonYMargin: 10,
expandCollapseButtonOptions: {
sideLength: 18
},
titleAlignX: 'left',
stroke: BANColors.panelStrokeColorProperty,
cornerRadius: BANConstants.PANEL_CORNER_RADIUS
} );
this.symbolAccordionBox.right = this.layoutBounds.maxX - BANConstants.SCREEN_VIEW_X_MARGIN;
this.symbolAccordionBox.top = this.layoutBounds.minY + BANConstants.SCREEN_VIEW_Y_MARGIN;
this.addChild( this.symbolAccordionBox );
// 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 the undo decay button
const undoDecayButton = new ReturnButton( () => {
undoDecayButton.visible = false;
restorePreviousNucleonNumber( ParticleType.PROTON, oldProtonNumber );
restorePreviousNucleonNumber( ParticleType.NEUTRON, oldNeutronNumber );
// remove all particles in the outgoingParticles array from the particles array
this.model.outgoingParticles.forEach( particle => {
this.model.removeParticle( particle );
} );
this.model.outgoingParticles.clear();
this.model.particleAnimations.clear();
}, {
baseColor: Color.YELLOW.darkerColor( 0.95 )
} );
undoDecayButton.visible = false;
this.addChild( undoDecayButton );
// restore the particleAtom to have the nucleon numbers before a decay occurred
const restorePreviousNucleonNumber = ( particleType: ParticleType, oldNucleonNumber: number ) => {
const newNucleonNumber = particleType === ParticleType.PROTON ?
this.model.particleAtom.protonCountProperty.value :
this.model.particleAtom.neutronCountProperty.value;
const nucleonNumberDifference = oldNucleonNumber - newNucleonNumber;
for ( let i = 0; i < Math.abs( nucleonNumberDifference ); i++ ) {
if ( nucleonNumberDifference > 0 ) {
this.addNucleonImmediatelyToAtom( particleType );
}
else if ( nucleonNumberDifference < 0 ) {
removeNucleonImmediatelyFromAtom( particleType );
}
}
};
// remove a nucleon of a given particleType from the atom immediately
const removeNucleonImmediatelyFromAtom = ( particleType: ParticleType ) => {
const particleToRemove = this.model.particleAtom.extractParticle( particleType.particleTypeString );
this.animateAndRemoveParticle( particleToRemove );
};
// 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,
this.model.userControlledNeutrons.lengthProperty ], () => {
undoDecayButton.visible = false;
} );
// create and add the available decays panel at the center right of the decay screen
const availableDecaysPanel = new AvailableDecaysPanel( model, {
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 );
let manualConstraint: ManualConstraint<Node[]> | null;
// reposition the undo button beside the decayButton
const repositionUndoDecayButton = ( decayType: string ) => {
const decayButtonAndIconIndex = availableDecaysPanel.decayTypeButtonIndexMap[ decayType ];
const decayButtonAndIcon = availableDecaysPanel.arrangedDecayButtonsAndIcons.children[ decayButtonAndIconIndex ];
manualConstraint && manualConstraint.dispose();
manualConstraint = new ManualConstraint( this, [ decayButtonAndIcon, undoDecayButton ],
( decayButtonAndIconWrapper, undoDecayButtonWrapper ) => {
undoDecayButtonWrapper.centerY = decayButtonAndIconWrapper.centerY;
} );
};
undoDecayButton.right = availableDecaysPanel.left - 10;
// show the electron cloud by default
this.showElectronCloudBooleanProperty = new BooleanProperty( true );
this.showElectronCloudBooleanProperty.link( showElectronCloud => { this.particleAtomNode.electronCloud.visible = showElectronCloud; } );
// create and add the electronCloud checkbox
const showElectronCloudCheckbox = new Checkbox( this.showElectronCloudBooleanProperty, new HBox( {
children: [
new Text( BuildANucleusStrings.electronCloudStringProperty, { font: BANConstants.REGULAR_FONT, maxWidth: 210 } ),
// electron cloud icon
new Circle( {
radius: 18,
fill: new RadialGradient( 0, 0, 0, 0, 0, 18 )
.addColorStop( 0, BANColors.electronColorProperty.value.withAlpha( 200 ) )
.addColorStop( 0.9, BANColors.electronColorProperty.value.withAlpha( 0 ) )
} )
],
spacing: 5
} ) );
showElectronCloudCheckbox.left = availableDecaysPanel.left;
showElectronCloudCheckbox.bottom = this.resetAllButton.bottom;
this.addChild( showElectronCloudCheckbox );
// create and add stability indicator
this.stabilityIndicator = new Text( '', {
font: BANConstants.REGULAR_FONT,
fill: 'black',
visible: true,
maxWidth: 225
} );
this.stabilityIndicator.boundsProperty.link( () => {
this.stabilityIndicator.center = new Vector2( halfLifeInformationNodeCenterX, availableDecaysPanel.top );
} );
this.addChild( this.stabilityIndicator );
// add the particleViewLayerNode after everything else so particles are in the top layer
this.addChild( this.particleAtomNode );
// Define the update function for the stability indicator.
const updateStabilityIndicator = ( protonNumber: number, neutronNumber: number ) => {
if ( protonNumber > 0 ) {
if ( AtomIdentifier.isStable( protonNumber, neutronNumber ) ) {
this.stabilityIndicator.stringProperty = BuildANucleusStrings.stableStringProperty;
}
else {
this.stabilityIndicator.stringProperty = BuildANucleusStrings.unstableStringProperty;
}
}
else {
this.stabilityIndicator.string = '';
}
};
// Add the listeners that control the label content
Multilink.multilink( [ model.particleAtom.protonCountProperty, model.particleAtom.neutronCountProperty ],
( protonNumber: number, neutronNumber: number ) => updateStabilityIndicator( protonNumber, neutronNumber )
);
const updateStabilityIndicatorVisibility = ( visible: boolean ) => {
this.stabilityIndicator.visible = visible;
};
model.doesNuclideExistBooleanProperty.link( updateStabilityIndicatorVisibility );
this.elementName.boundsProperty.link( () => {
this.elementName.center = this.stabilityIndicator.center.plusXY( 0, 60 );
} );
this.nucleonNumberPanel.left = availableDecaysPanel.left;
// Only show the emptyAtomCircle if there are zero particles in the atom.
Multilink.multilink( [
this.model.particleAtom.protonCountProperty,
this.model.particleAtom.neutronCountProperty
], ( protonNumber, neutronNumber ) => {
this.particleAtomNode.emptyAtomCircle.visible = protonNumber + neutronNumber === 0;
} );
this.pdomPlayAreaNode.pdomOrder = this.pdomPlayAreaNode.pdomOrder!.concat( [
halfLifeInformationNode,
undoDecayButton,
this.symbolAccordionBox,
availableDecaysPanel
] );
this.pdomControlAreaNode.pdomOrder = [ showElectronCloudCheckbox, ...this.pdomControlAreaNode.pdomOrder! ];
}
public getRandomExternalModelPosition(): Vector2 {
return this.particleTransform.viewToModelPosition( this.getRandomEscapePosition() );
}
public override getParticleAtom(): ParticleAtom {
return this.model.particleAtom;
}
/**
* Creates an alpha particle by removing the needed nucleons from the nucleus, arranging them, and then animates the
* particle out of view.
*/
public override emitAlphaParticle(): AlphaParticle {
const alphaParticle = super.emitAlphaParticle();
// this is a special case where the 2 remaining protons, after an alpha particle is emitted, are emitted too
if ( this.model.particleAtom.protonCountProperty.value === 2 && this.model.particleAtom.neutronCountProperty.value === 0 ) {
const alphaParticleInitialPosition = alphaParticle.positionProperty.value;
// the distance the alpha particle travels in {{ BANConstants.TIME_TO_SHOW_DOES_NOT_EXIST }} seconds
const alphaParticleDistanceTravelled = BANConstants.TIME_TO_SHOW_DOES_NOT_EXIST * alphaParticle.velocity;
let protonsEmitted = false;
alphaParticle.positionProperty.link( position => {
// emit the 2 protons after {{ BANConstants.TIME_TO_SHOW_DOES_NOT_EXIST }} seconds
if ( !protonsEmitted && position.distance( alphaParticleInitialPosition ) >= alphaParticleDistanceTravelled ) {
_.times( 2, () => { this.emitNucleon( ParticleType.PROTON ); } );
protonsEmitted = true;
}
} );
}
return alphaParticle;
}
/**
* Changes the nucleon type of a particle in the atom and emits an electron or positron from behind that particle.
*/
public override betaDecay( betaDecayType: DecayType ): Particle {
const particleToEmit = super.betaDecay( betaDecayType );
this.model.addParticle( particleToEmit );
return particleToEmit;
}
/**
* Returns whether the nucleon is within the circular capture radius around the atom.
*/
protected override isNucleonInCaptureArea( nucleon: Particle, atom: ParticleAtom ): boolean {
return nucleon.positionProperty.value.distance( atom.positionProperty.value ) < NUCLEON_CAPTURE_RADIUS;
}
public override reset(): void {
this.symbolAccordionBox.reset();
this.showElectronCloudBooleanProperty.reset();
}
}
buildANucleus.register( 'DecayScreenView', DecayScreenView );
export default DecayScreenView;