From 5136a312539720a63282c348fe99c6fc7e40e156 Mon Sep 17 00:00:00 2001 From: jbphet Date: Tue, 15 Nov 2016 11:40:08 -0700 Subject: [PATCH] added tandems for local (i.e. non-common) sim code, see #76 --- js/gravity-force-lab-main.js | 23 +++++-- .../model/GravityForceLabModel.js | 7 +- js/gravity-force-lab/model/Mass.js | 3 +- .../view/GravityForceLabRuler.js | 3 +- .../view/GravityForceLabScreenView.js | 60 +++++++++++++---- js/gravity-force-lab/view/MassControl.js | 3 +- js/gravity-force-lab/view/MassNode.js | 65 ++++++++++--------- .../view/ParameterControlPanel.js | 3 +- js/gravity-force-lab/view/PullerNode.js | 3 +- 9 files changed, 110 insertions(+), 60 deletions(-) diff --git a/js/gravity-force-lab-main.js b/js/gravity-force-lab-main.js index a843c3e3..31ddb97e 100644 --- a/js/gravity-force-lab-main.js +++ b/js/gravity-force-lab-main.js @@ -9,11 +9,15 @@ define( function( require ) { 'use strict'; // modules - var Screen = require( 'JOIST/Screen' ); - var SimLauncher = require( 'JOIST/SimLauncher' ); - var Sim = require( 'JOIST/Sim' ); var GravityForceLabModel = require( 'GRAVITY_FORCE_LAB/gravity-force-lab/model/GravityForceLabModel' ); var GravityForceLabScreenView = require( 'GRAVITY_FORCE_LAB/gravity-force-lab/view/GravityForceLabScreenView' ); + var Screen = require( 'JOIST/Screen' ); + var Sim = require( 'JOIST/Sim' ); + var SimLauncher = require( 'JOIST/SimLauncher' ); + var Tandem = require( 'TANDEM/Tandem' ); + + // constants + var tandem = Tandem.createRootTandem(); // strings var gravityForceLabTitleString = require( 'string!GRAVITY_FORCE_LAB/gravity-force-lab.title' ); @@ -29,13 +33,18 @@ define( function( require ) { SimLauncher.launch( function() { + var gravityForceLabScreenTandem = tandem.createTandem( 'gravityForceLabScreen' ); + // create and start the sim new Sim( gravityForceLabTitleString, [ new Screen( - function() { return new GravityForceLabModel(); }, - function( model ) { return new GravityForceLabScreenView( model ); }, { - backgroundColor: '#FFFFFF' - } + function() { + return new GravityForceLabModel( gravityForceLabScreenTandem.createTandem( 'model' ) ); + }, + function( model ) { + return new GravityForceLabScreenView( model, gravityForceLabScreenTandem.createTandem( 'view' ) ); + }, + { backgroundColor: '#FFFFFF', tandem: gravityForceLabScreenTandem } ) ], simOptions ).start(); } ); diff --git a/js/gravity-force-lab/model/GravityForceLabModel.js b/js/gravity-force-lab/model/GravityForceLabModel.js index 4b82561e..fdc07041 100644 --- a/js/gravity-force-lab/model/GravityForceLabModel.js +++ b/js/gravity-force-lab/model/GravityForceLabModel.js @@ -26,17 +26,18 @@ define( function( require ) { var PULL_OBJECT_WIDTH = 1.62; // empirically determined for model space in meters /** + * @param {Tandem} tandem * @constructor */ - function GravityForceLabModel() { + function GravityForceLabModel( tandem ) { this.massRange = new RangeWithValue( 1, 1000 ); // @public this.showValuesProperty = new Property( true ); // @public this.constantRadiusProperty = new Property( false ); // @public this.rulerPositionProperty = new Property( new Vector2( 120, 270 ) ); // @public - this.mass1 = new Mass( 50, -2, '#00f', this.constantRadiusProperty ); // @public - this.mass2 = new Mass( 200, 2, '#f00', this.constantRadiusProperty ); // @public + this.mass1 = new Mass( 50, -2, '#00f', this.constantRadiusProperty, tandem.createTandem( 'mass1' ) ); // @public + this.mass2 = new Mass( 200, 2, '#f00', this.constantRadiusProperty, tandem.createTandem( 'mass2' ) ); // @public // @public, the force between the two objects as a positive scalar this.forceProperty = new DerivedProperty( [ diff --git a/js/gravity-force-lab/model/Mass.js b/js/gravity-force-lab/model/Mass.js index fecc2d74..67d243bb 100644 --- a/js/gravity-force-lab/model/Mass.js +++ b/js/gravity-force-lab/model/Mass.js @@ -26,9 +26,10 @@ define( function( require ) { * @param {number} initialPosition * @param {string} baseColor * @param {Property.} constantRadiusProperty + * @param {Tandem} tandem * @constructor */ - function Mass( initialMass, initialPosition, baseColor, constantRadiusProperty ) { + function Mass( initialMass, initialPosition, baseColor, constantRadiusProperty, tandem ) { var self = this; var initialRadius = this.calculateRadius( initialMass ); diff --git a/js/gravity-force-lab/view/GravityForceLabRuler.js b/js/gravity-force-lab/view/GravityForceLabRuler.js index 5e947385..3eb45534 100644 --- a/js/gravity-force-lab/view/GravityForceLabRuler.js +++ b/js/gravity-force-lab/view/GravityForceLabRuler.js @@ -28,9 +28,10 @@ define( function( require ) { * @param {GravityForceLabModel} model * @param {number} screenWidth * @param {number} screenHeight + * @param {Tandem} tandem * @constructor */ - function GravityForceLabRuler( model, screenWidth, screenHeight ) { + function GravityForceLabRuler( model, screenWidth, screenHeight, tandem ) { var self = this; Node.call( this, { cursor: 'pointer', cssTransform: true } ); var ruler = new RulerNode( RULER_WIDTH, RULER_HEIGHT, 50, [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ], unitsMetersString, { diff --git a/js/gravity-force-lab/view/GravityForceLabScreenView.js b/js/gravity-force-lab/view/GravityForceLabScreenView.js index e1b093f9..e6c7ae4d 100644 --- a/js/gravity-force-lab/view/GravityForceLabScreenView.js +++ b/js/gravity-force-lab/view/GravityForceLabScreenView.js @@ -36,9 +36,10 @@ define( function( require ) { /** * @param {GravityForceLabModel} model + * @param {Tandem} tandem * @constructor */ - function GravityForceLabScreenView( model ) { + function GravityForceLabScreenView( model, tandem ) { ScreenView.call( this, { layoutBounds: new Bounds2( 0, 0, 768, 464 ) } ); // Create the model-view transform. The primary units used in the model are meters, so significant zoom is used. @@ -51,27 +52,44 @@ define( function( require ) { ); // add the mass nodes to the screen - this.addChild( new MassNode( model, model.mass1, this.layoutBounds, modelViewTransform, { + this.addChild( new MassNode( + model, + model.mass1, + this.layoutBounds, + modelViewTransform, + tandem.createTandem( 'mass1Node' ), + { label: mass1AbbreviatedString, otherMassLabel: mass2AbbreviatedString, direction: 'left', arrowColor: '#66f', y: MASS_NODE_Y_POSITION, forceArrowHeight: 125 - } ) - ); - - this.addChild( new MassNode( model, model.mass2, this.layoutBounds, modelViewTransform, { + } + ) ); + + this.addChild( new MassNode( + model, + model.mass2, + this.layoutBounds, + modelViewTransform, + tandem.createTandem( 'mass2Node' ), + { label: mass2AbbreviatedString, otherMassLabel: mass1AbbreviatedString, direction: 'right', arrowColor: '#f66', y: MASS_NODE_Y_POSITION, forceArrowHeight: 175 - } ) + } + ) ); + + var gravityForceLabRuler = new GravityForceLabRuler( + model, + this.layoutBounds.width, + this.layoutBounds.height, + tandem.createTandem( 'gravityForceLabRuler' ) ); - - var gravityForceLabRuler = new GravityForceLabRuler( model, this.layoutBounds.width, this.layoutBounds.height ); this.addChild( gravityForceLabRuler ); var resetAllButton = new ResetAllButton( { @@ -80,7 +98,7 @@ define( function( require ) { } ); this.addChild( resetAllButton ); - var parameterControlPanel = new ParameterControlPanel( model ); + var parameterControlPanel = new ParameterControlPanel( model, tandem.createTandem( 'parameterControlPanel' ) ); parameterControlPanel.scale( 0.9 ); this.addChild( parameterControlPanel ); @@ -88,7 +106,8 @@ define( function( require ) { mass1String, model.mass1.massProperty, model.massRange, - model.mass1.baseColorProperty.get() + model.mass1.baseColorProperty.get(), + tandem.createTandem( 'massControl1' ) ); massControl1.scale( CONTROL_SCALE ); @@ -98,16 +117,29 @@ define( function( require ) { mass2String, model.mass2.massProperty, model.massRange, - model.mass2.baseColorProperty.get() + model.mass2.baseColorProperty.get(), + tandem.createTandem( 'massControl2' ) ); massControl2.scale( CONTROL_SCALE ); this.addChild( massControl2 ); - var massControl1ConstantRadius = new MassControl( mass1String, model.mass1.massProperty, model.massRange, CONSTANT_MASS_COLOR ); + var massControl1ConstantRadius = new MassControl( + mass1String, + model.mass1.massProperty, + model.massRange, + CONSTANT_MASS_COLOR, + tandem.createTandem( 'massControl1ConstantRadius' ) + ); massControl1ConstantRadius.scale( CONTROL_SCALE ); this.addChild( massControl1ConstantRadius ); - var massControl2ConstantRadius = new MassControl( mass2String, model.mass2.massProperty, model.massRange, CONSTANT_MASS_COLOR ); + var massControl2ConstantRadius = new MassControl( + mass2String, + model.mass2.massProperty, + model.massRange, + CONSTANT_MASS_COLOR, + tandem.createTandem( 'massControl2ConstantRadius' ) + ); massControl2ConstantRadius.scale( CONTROL_SCALE ); this.addChild( massControl2ConstantRadius ); diff --git a/js/gravity-force-lab/view/MassControl.js b/js/gravity-force-lab/view/MassControl.js index ff2b4ee8..7b287b8c 100644 --- a/js/gravity-force-lab/view/MassControl.js +++ b/js/gravity-force-lab/view/MassControl.js @@ -32,9 +32,10 @@ define( function( require ) { * @param {Property.} massProperty * @param {Range} massRange * @param {Color} thumbColor + * @param {Tandem} tandem * @constructor */ - function MassControl( titleString, massProperty, massRange, thumbColor ) { + function MassControl( titleString, massProperty, massRange, thumbColor, tandem ) { // major ticks var tickLabelOptions = { font: new PhetFont( 14 ), pickable: false }; diff --git a/js/gravity-force-lab/view/MassNode.js b/js/gravity-force-lab/view/MassNode.js index 2f0902c6..a3a5a382 100644 --- a/js/gravity-force-lab/view/MassNode.js +++ b/js/gravity-force-lab/view/MassNode.js @@ -47,10 +47,11 @@ define( function( require ) { * @param {MassModel} massModel * @param {Bounds2} layoutBounds * @param {ModelViewTransform2} modelViewTransform + * @param {Tandem} tandem * @param {Object} [options] * @constructor */ - function MassNode( model, massModel, layoutBounds, modelViewTransform, options ) { + function MassNode( model, massModel, layoutBounds, modelViewTransform, tandem, options ) { var self = this; options = _.extend( { label: 'This Mass', @@ -70,7 +71,7 @@ define( function( require ) { Node.call( this ); var dragNode = new Node( { cursor: 'pointer' } ); - this.pullerNode = new PullerNode( { image_count: PULL_IMAGES_COUNT } ); + this.pullerNode = new PullerNode( tandem, { image_count: PULL_IMAGES_COUNT } ); if ( options.direction === 'right' ) { self.pullerNode.scale( -1, 1 ); } @@ -159,7 +160,7 @@ define( function( require ) { arrowText.text = StringUtils.format( forceDescriptionPatternTargetSourceString, options.label, options.otherMassLabel ); } - if( !arrowAtBoundary ) { + if ( !arrowAtBoundary ) { arrowText.centerX = 0; } @@ -199,51 +200,53 @@ define( function( require ) { arrowText.right = self.parentToLocalBounds( layoutBounds ).right - TEXT_OFFSET; arrowAtBoundary = true; } - } ); + model.showValuesProperty.lazyLink( function() { redrawForce(); } ); + massModel.radiusProperty.lazyLink( function() { redrawForce(); } ); + model.forceProperty.lazyLink( function() { redrawForce(); } ); + massModel.baseColorProperty.link( function( baseColor ) { updateGradient( baseColor ); } ); - redrawForce(); + redrawForce(); var massClickXOffset; - dragNode.addInputListener( new SimpleDragHandler( - { - allowTouchSnag: true, - start: function( event ) { - massClickXOffset = dragNode.globalToParentPoint( event.pointer.point ).x - event.currentTarget.x; - }, - drag: function( event ) { - var x = self.globalToParentPoint( event.pointer.point ).x - massClickXOffset; - var xMax = layoutBounds.maxX - self.massCircle.width / 2 - self.pullerNode.width - OFFSET; - var xMin = layoutBounds.minX + OFFSET + self.massCircle.width / 2 + self.pullerNode.width; - - // TODO: Complete this sentence - // for mass1 xMax is left boundary of - var sumRadius = modelViewTransform.modelToViewDeltaX( model.mass1.radiusProperty.get() ) + - modelViewTransform.modelToViewDeltaX( model.mass2.radiusProperty.get() ); - if ( massModel.positionProperty.get() === model.mass1.positionProperty.get() ) { - xMax = modelViewTransform.modelToViewX( model.mass2.positionProperty.get() ) - sumRadius - - modelViewTransform.modelToViewDeltaX( GravityForceLabModel.MIN_SEPARATION_BETWEEN_MASSES ); - } - if ( massModel.positionProperty.get() === model.mass2.positionProperty.get() ) { - xMin = modelViewTransform.modelToViewX( model.mass1.positionProperty.get() ) + sumRadius + - modelViewTransform.modelToViewDeltaX( GravityForceLabModel.MIN_SEPARATION_BETWEEN_MASSES ); - } - x = Math.max( Math.min( x, xMax ), xMin ); - massModel.positionProperty.set( modelViewTransform.viewToModelX( x ) ); + dragNode.addInputListener( new SimpleDragHandler( { + allowTouchSnag: true, + start: function( event ) { + massClickXOffset = dragNode.globalToParentPoint( event.pointer.point ).x - event.currentTarget.x; + }, + drag: function( event ) { + var x = self.globalToParentPoint( event.pointer.point ).x - massClickXOffset; + var xMax = layoutBounds.maxX - self.massCircle.width / 2 - self.pullerNode.width - OFFSET; + var xMin = layoutBounds.minX + OFFSET + self.massCircle.width / 2 + self.pullerNode.width; + + // TODO: Complete this sentence + // for mass1 xMax is left boundary of + var sumRadius = modelViewTransform.modelToViewDeltaX( model.mass1.radiusProperty.get() ) + + modelViewTransform.modelToViewDeltaX( model.mass2.radiusProperty.get() ); + if ( massModel.positionProperty.get() === model.mass1.positionProperty.get() ) { + xMax = modelViewTransform.modelToViewX( model.mass2.positionProperty.get() ) - sumRadius - + modelViewTransform.modelToViewDeltaX( GravityForceLabModel.MIN_SEPARATION_BETWEEN_MASSES ); + } + if ( massModel.positionProperty.get() === model.mass2.positionProperty.get() ) { + xMin = modelViewTransform.modelToViewX( model.mass1.positionProperty.get() ) + sumRadius + + modelViewTransform.modelToViewDeltaX( GravityForceLabModel.MIN_SEPARATION_BETWEEN_MASSES ); } - } ) ); + x = Math.max( Math.min( x, xMax ), xMin ); + massModel.positionProperty.set( modelViewTransform.viewToModelX( x ) ); + } + } ) ); } gravityForceLab.register( 'MassNode', MassNode ); diff --git a/js/gravity-force-lab/view/ParameterControlPanel.js b/js/gravity-force-lab/view/ParameterControlPanel.js index 9d3b0056..19c0ee90 100644 --- a/js/gravity-force-lab/view/ParameterControlPanel.js +++ b/js/gravity-force-lab/view/ParameterControlPanel.js @@ -26,10 +26,11 @@ define( function( require ) { /** * @param {gravityForceLabModel} model + * @param {Tandem} tandem * @param {Object} [options] * @constructor */ - function ParameterControlPanel( model, options ) { + function ParameterControlPanel( model, tandem, options ) { options = _.extend( { fill: '#FDF498', diff --git a/js/gravity-force-lab/view/PullerNode.js b/js/gravity-force-lab/view/PullerNode.js index 97405623..8cbc1593 100644 --- a/js/gravity-force-lab/view/PullerNode.js +++ b/js/gravity-force-lab/view/PullerNode.js @@ -38,10 +38,11 @@ define( function( require ) { pullImage8, pullImage9, pullImage10, pullImage11, pullImage12, pullImage13, pullImage14 ]; /** + * @param {Tandem} tandem * @param {Object} [options] * @constructor */ - function PullerNode( options ) { + function PullerNode( tandem, options ) { options = _.extend( { ropeLength: 50 }, options ); Node.call( this );