Skip to content

Commit

Permalink
added tandems for local (i.e. non-common) sim code, see #76
Browse files Browse the repository at this point in the history
  • Loading branch information
jbphet committed Nov 15, 2016
1 parent ea5bac6 commit 5136a31
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 60 deletions.
23 changes: 16 additions & 7 deletions js/gravity-force-lab-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand All @@ -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();
} );
Expand Down
7 changes: 4 additions & 3 deletions js/gravity-force-lab/model/GravityForceLabModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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( [
Expand Down
3 changes: 2 additions & 1 deletion js/gravity-force-lab/model/Mass.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ define( function( require ) {
* @param {number} initialPosition
* @param {string} baseColor
* @param {Property.<boolean>} 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 );

Expand Down
3 changes: 2 additions & 1 deletion js/gravity-force-lab/view/GravityForceLabRuler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
60 changes: 46 additions & 14 deletions js/gravity-force-lab/view/GravityForceLabScreenView.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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( {
Expand All @@ -80,15 +98,16 @@ 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 );

var massControl1 = new MassControl(
mass1String,
model.mass1.massProperty,
model.massRange,
model.mass1.baseColorProperty.get()
model.mass1.baseColorProperty.get(),
tandem.createTandem( 'massControl1' )
);

massControl1.scale( CONTROL_SCALE );
Expand All @@ -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 );

Expand Down
3 changes: 2 additions & 1 deletion js/gravity-force-lab/view/MassControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ define( function( require ) {
* @param {Property.<number>} 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 };
Expand Down
65 changes: 34 additions & 31 deletions js/gravity-force-lab/view/MassNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 );
}
Expand Down Expand Up @@ -159,7 +160,7 @@ define( function( require ) {
arrowText.text = StringUtils.format( forceDescriptionPatternTargetSourceString, options.label, options.otherMassLabel );
}

if( !arrowAtBoundary ) {
if ( !arrowAtBoundary ) {
arrowText.centerX = 0;
}

Expand Down Expand Up @@ -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 );
Expand Down
3 changes: 2 additions & 1 deletion js/gravity-force-lab/view/ParameterControlPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion js/gravity-force-lab/view/PullerNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down

0 comments on commit 5136a31

Please sign in to comment.