-
Notifications
You must be signed in to change notification settings - Fork 0
/
VSMScreenView.ts
338 lines (279 loc) · 13.3 KB
/
VSMScreenView.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
328
329
330
331
332
333
334
335
336
337
338
// Copyright 2023-2024, University of Colorado Boulder
/**
* ScreenView for the Variability, Sources and Measures (VSM) screens on the Projectile Data Lab sim.
*
* @author Matthew Blackman (PhET Interactive Simulations)
* @author Sam Reid (PhET Interactive Simulations)
*/
import optionize, { EmptySelfOptions } from '../../../../phet-core/js/optionize.js';
import { Color, ManualConstraint, Node, VBox } from '../../../../scenery/js/imports.js';
import VSMModel from '../model/VSMModel.js';
import projectileDataLab from '../../projectileDataLab.js';
import PDLConstants from '../../common/PDLConstants.js';
import SpeedToolNode from './SpeedToolNode.js';
import AngleToolNode from './AngleToolNode.js';
import MeasuringTapeNode from '../../../../scenery-phet/js/MeasuringTapeNode.js';
import Property from '../../../../axon/js/Property.js';
import Vector2 from '../../../../dot/js/Vector2.js';
import Bounds2 from '../../../../dot/js/Bounds2.js';
import DerivedProperty from '../../../../axon/js/DerivedProperty.js';
import VSMField from '../model/VSMField.js';
import VSMFieldSignNode from './VSMFieldSignNode.js';
import PDLScreenView, { PDLScreenViewOptions } from '../../common/view/PDLScreenView.js';
import VSMCanvasNode from './VSMCanvasNode.js';
import ProjectileSelectorNode from './ProjectileSelectorNode.js';
import StaticToolPanel from './StaticToolPanel.js';
import InteractiveToolPanel from './InteractiveToolPanel.js';
import VSMLaunchPanel from './VSMLaunchPanel.js';
import FieldRadioButtonGroup from './FieldRadioButtonGroup.js';
import WithRequired from '../../../../phet-core/js/types/WithRequired.js';
import HistogramNode from '../../common/view/HistogramNode.js';
import ProjectileDataLabStrings from '../../ProjectileDataLabStrings.js';
import PDLQueryParameters, { AUTO_GENERATE_DATA_PROPERTY } from '../../common/PDLQueryParameters.js';
import HistogramAccordionBox, { histogramAccordionBoxTandemName } from '../../common/view/HistogramAccordionBox.js';
import StopwatchNode from '../../../../scenery-phet/js/StopwatchNode.js';
import PDLStopwatchNode from './PDLStopwatchNode.js';
import FieldSignNode from '../../common/view/FieldSignNode.js';
type SelfOptions = EmptySelfOptions;
type VSMScreenViewOptions = SelfOptions & WithRequired<PDLScreenViewOptions, 'tandem'>;
export default abstract class VSMScreenView<T extends VSMField> extends PDLScreenView<T> {
protected readonly fieldRadioButtonGroup: FieldRadioButtonGroup<T>;
protected readonly accordionBox: HistogramAccordionBox;
protected readonly fieldSignNode: FieldSignNode;
protected readonly toolsLayer: Node = new Node();
protected readonly projectileSelectorNode: ProjectileSelectorNode;
protected readonly topRightUIContainer: VBox;
protected readonly measuringTapeNode: MeasuringTapeNode;
protected readonly stopwatchNode: StopwatchNode;
protected constructor( model: VSMModel<T>,
public readonly launchPanel: VSMLaunchPanel,
staticToolPanel: StaticToolPanel,
interactiveToolPanel: InteractiveToolPanel,
// Closure that creates the HistogramNode. We need to access 'this' to be the combo box parent
// hence must create it lazily.
createHistogramNode: ( node: Node ) => HistogramNode,
providedOptions: VSMScreenViewOptions ) {
const options = optionize<VSMScreenViewOptions, SelfOptions, PDLScreenViewOptions>()( {}, providedOptions );
const launchButtonEnabledProperty = new DerivedProperty( [ model.totalProjectileCountProperty ],
totalProjectileCount => {
return totalProjectileCount < PDLQueryParameters.maxProjectilesVSMField;
} );
super(
model,
ProjectileDataLabStrings.singleLaunchStringProperty,
ProjectileDataLabStrings.continuousLaunchStringProperty,
launchButtonEnabledProperty,
options
);
const originPosition = this.modelViewTransform.modelToViewPosition( Vector2.ZERO );
const projectileCanvas = new VSMCanvasNode( model.fieldProperty, model.isPathsVisibleProperty,
this.modelViewTransform, {
canvasBounds: this.canvasBounds
} );
this.projectileLayer.addChild( projectileCanvas );
this.accordionBox = new HistogramAccordionBox(
createHistogramNode( this ), {
top: PDLConstants.SCREEN_VIEW_Y_MARGIN,
left: launchPanel.right + PDLConstants.INTER_PANEL_SPACING,
tandem: options.tandem.createTandem( histogramAccordionBoxTandemName )
} );
this.topRightUIContainer = new VBox( {
stretch: true,
right: this.layoutBounds.right - PDLConstants.SCREEN_VIEW_X_MARGIN,
top: this.layoutBounds.top + PDLConstants.SCREEN_VIEW_Y_MARGIN,
spacing: PDLConstants.INTER_PANEL_SPACING,
children: [ staticToolPanel, interactiveToolPanel, this.timeControlNode ],
preferredWidth: this.layoutBounds.right - this.accordionBox.right - PDLConstants.INTER_PANEL_SPACING - PDLConstants.SCREEN_VIEW_X_MARGIN
} );
this.projectileSelectorNode = new ProjectileSelectorNode(
model.fieldProperty,
model.selectedProjectileNumberProperty,
model.totalProjectileCountProperty,
model.numberOfLandedProjectilesProperty,
model.selectedProjectileProperty, {
tandem: options.tandem.createTandem( 'projectileSelectorNode' ),
visiblePropertyOptions: {
phetioFeatured: true
},
enabledPropertyOptions: {
phetioFeatured: false
}
}
);
// tools
this.measuringTapeNode = new MeasuringTapeNode( new Property( { name: 'm', multiplier: 1 } ), {
visibleProperty: model.isMeasuringTapeVisibleProperty,
modelViewTransform: this.modelViewTransform,
dragBounds: new Bounds2(
0, 0, 100, this.modelViewTransform.modelToViewY( PDLConstants.SCREEN_VIEW_Y_MARGIN ) ),
basePositionProperty: model.measuringTapeBasePositionProperty,
tipPositionProperty: model.measuringTapeTipPositionProperty,
lineColor: new Color( 255, 255, 50 ),
crosshairLineWidth: 1.8,
tapeLineWidth: 1.8,
isBaseCrosshairRotating: false,
isTipCrosshairRotating: false,
textColor: 'black',
textBackgroundColor: 'white',
textBackgroundCornerRadius: 5,
textBackgroundXMargin: 3,
textBackgroundYMargin: 2,
textPosition: new Vector2( 0, 32 ),
significantFigures: 1,
textFont: PDLConstants.MEASURING_TAPE_FONT,
tandem: options.tandem.createTandem( 'measuringTapeNode' ),
phetioDocumentation: 'The node for the measuring tape',
phetioFeatured: true,
phetioFeaturedMeasuredDistanceProperty: true
} );
const dragBoundsProperty = new Property( new Bounds2( 0, 0, 100, 100 ) );
// Allow the measuring tape to be dragged within the visible bounds of the screen, so that outliers can be measured.
this.visibleBoundsProperty.link( visibleBounds => {
const viewBounds = visibleBounds.erodedXY( PDLConstants.SCREEN_VIEW_X_MARGIN, PDLConstants.SCREEN_VIEW_Y_MARGIN );
const modelBounds = this.modelViewTransform.viewToModelBounds( viewBounds );
// Don't allow dragging left of x=0, or below y=0
this.measuringTapeNode.setDragBounds( new Bounds2( 0, 0, modelBounds.maxX, modelBounds.maxY ) );
dragBoundsProperty.value = viewBounds;
} );
const stopwatchNodeTandem = options.tandem.createTandem( 'stopwatchNode' );
this.stopwatchNode = new PDLStopwatchNode( model.stopwatch, () => model.launchProjectile(), {
tandem: stopwatchNodeTandem,
dragBoundsProperty: dragBoundsProperty,
launchButtonEnabledProperty: new DerivedProperty( [ model.totalProjectileCountProperty ], totalProjectileCount => {
return totalProjectileCount < PDLQueryParameters.maxProjectilesVSMField;
} )
} );
const isLauncherRaisedProperty = new DerivedProperty( [ model.launcherHeightProperty ], height => height > 0 );
// Create the heat map tools
const speedToolNode = new SpeedToolNode(
model.latestLaunchSpeedProperty,
isLauncherRaisedProperty, {
visibleProperty: model.isLaunchSpeedVisibleProperty,
x: originPosition.x, y: originPosition.y
} );
const angleToolNode = new AngleToolNode(
model.latestLaunchAngleProperty,
isLauncherRaisedProperty, {
visibleProperty: model.isLaunchAngleVisibleProperty,
x: originPosition.x, y: originPosition.y
} );
model.fields.forEach( field => {
// When one projectile is launched, update the heat map tools
field.projectileLaunchedEmitter.addListener( projectile => {
if ( model.fieldProperty.value === field ) {
speedToolNode.updateHeatMapWithData( projectile.launchSpeed );
angleToolNode.updateHeatMapWithData( projectile.launchAngle );
// When launching many projectiles at once in the auto-generate mode, suppress the individual launch animations
// to improve performance
if ( !AUTO_GENERATE_DATA_PROPERTY.value ) {
this.launcherNode.playLaunchAnimation( projectile.launchAngle );
}
}
} );
// If the projectiles are cleared, clear the heat map tools
field.projectilesClearedEmitter.addListener( () => {
speedToolNode.clear();
angleToolNode.clear();
} );
} );
// When the field changes, restore the entire state of the heat maps.
model.fieldProperty.link( field => {
// Sets to initial conditions (remains at initial conditions if there are no projectiles)
speedToolNode.clear();
angleToolNode.clear();
const projectiles = field.getAllProjectiles();
projectiles.forEach( projectile => {
speedToolNode.updateHeatMapWithData( projectile.launchSpeed );
angleToolNode.updateHeatMapWithData( projectile.launchAngle );
} );
} );
model.launcherHeightProperty.link( launcherHeight => {
const launcherY = this.modelViewTransform.modelToViewY( launcherHeight );
speedToolNode.y = launcherY;
angleToolNode.y = launcherY;
} );
this.fieldRadioButtonGroup = new FieldRadioButtonGroup( model.fieldProperty, model.fields, {
tandem: options.tandem.createTandem( 'fieldRadioButtonGroup' )
} );
this.fieldSignNode = new VSMFieldSignNode(
model.fields,
model.fieldProperty,
this.projectileSelectorNode
);
this.positionFieldSignNode();
this.behindProjectilesLayer.addChild( this.fieldSignNode );
this.toolsLayer.addChild( angleToolNode );
this.toolsLayer.addChild( speedToolNode );
this.toolsLayer.addChild( this.measuringTapeNode );
this.toolsLayer.addChild( this.stopwatchNode );
this.addChild( this.fieldRadioButtonGroup );
this.addChild( this.accordionBox );
this.addChild( this.launchPanel );
this.addChild( this.topRightUIContainer );
this.addChild( this.toolsLayer );
// Position the field selector radio button group
this.fieldRadioButtonGroup.bottom = this.layoutBounds.bottom - PDLConstants.SCREEN_VIEW_Y_MARGIN - 5;
this.fieldRadioButtonGroup.left = this.layoutBounds.centerX - 60;
model.totalProjectileCountProperty.link( totalProjectileCount => {
// If it is the last projectile, set isContinuousLaunching to false, so that:
// * pressing clear won't resume launching
// * the icon will show as a launch icon rather than a stop icon
if ( totalProjectileCount === PDLQueryParameters.maxProjectilesVSMField ) {
model.isContinuousLaunchingProperty.value = false;
}
} );
// Allow the top content to go above the dev bounds, but not too far
this.visibleBoundsProperty.link( visibleBounds => {
const minY = PDLConstants.ABOVE_DEV_BOUNDS_TOP;
const topY = Math.max( visibleBounds.top, minY );
this.launchPanel.top = topY + PDLConstants.SCREEN_VIEW_Y_MARGIN;
this.accordionBox.top = topY + PDLConstants.SCREEN_VIEW_Y_MARGIN;
this.topRightUIContainer.top = topY + PDLConstants.SCREEN_VIEW_Y_MARGIN;
} );
// Position the 'No air resistance' text
ManualConstraint.create( this, [ this.noAirResistanceText, this.launchPanel ], ( noAirResistanceTextProxy, launchPanelProxy ) => {
noAirResistanceTextProxy.centerX = launchPanelProxy.centerX;
noAirResistanceTextProxy.top = launchPanelProxy.bottom + 8;
} );
// Keyboard order
this.setVSMPDOMOrder( staticToolPanel, interactiveToolPanel );
}
public override reset(): void {
super.reset();
this.accordionBox.reset();
}
/**
* Set the keyboard order for the VSM screen.
*/
protected setVSMPDOMOrder(
staticToolPanel: StaticToolPanel,
interactiveToolPanel: InteractiveToolPanel,
intervalToolNode?: Node ): void {
this.pdomPlayAreaNode.pdomOrder = [
// Experiment setup
this.launchPanel,
// Launch controls
this.launchButton,
this.singleOrContinuousRadioButtonGroup,
// Histogram
this.accordionBox,
// Field management
this.fieldRadioButtonGroup,
// Play area tools
this.measuringTapeNode,
this.stopwatchNode,
...( intervalToolNode ? [ intervalToolNode ] : [] )
];
this.pdomControlAreaNode.pdomOrder = [
// Global tools
staticToolPanel,
interactiveToolPanel,
this.timeControlNode,
// Erase
this.eraserButton,
// Reset all
this.resetAllButton
];
}
}
projectileDataLab.register( 'VSMScreenView', VSMScreenView );