Skip to content

Commit

Permalink
from #33, distanceProperty moved to GFLBModel and instrumented for ph…
Browse files Browse the repository at this point in the history
…etio, arrow node instrumented for phetio
  • Loading branch information
mbarlow12 committed Jan 19, 2018
1 parent cb0850c commit 681b20a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
14 changes: 14 additions & 0 deletions js/gravity-force-lab-basics/model/GravityForceLabBasicsModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ define( function( require ) {

// modules
var Color = require( 'SCENERY/util/Color' );
var DerivedProperty = require( 'AXON/DerivedProperty' );
var DerivedPropertyIO = require( 'AXON/DerivedPropertyIO' );
var gravityForceLabBasics = require( 'GRAVITY_FORCE_LAB_BASICS/gravityForceLabBasics' );
var GravityForceLabBasicsConstants = require( 'GRAVITY_FORCE_LAB_BASICS/gravity-force-lab-basics/GravityForceLabBasicsConstants' );
var inherit = require( 'PHET_CORE/inherit' );
var ISLCConstants = require( 'INVERSE_SQUARE_LAW_COMMON/ISLCConstants' );
var ISLCModel = require( 'INVERSE_SQUARE_LAW_COMMON/model/ISLCModel' );
var Mass = require( 'INVERSE_SQUARE_LAW_COMMON/model/Mass' );
var NumberIO = require( 'ifphetio!PHET_IO/types/NumberIO' );
var Property = require( 'AXON/Property' );
var PropertyIO = require( 'AXON/PropertyIO' );

Expand Down Expand Up @@ -72,6 +75,17 @@ define( function( require ) {
snapObjectsToNearest: GravityForceLabBasicsConstants.MASS_POSITION_DELTA,
minSeparationBetweenObjects: 200 // in meters
} );

// @public
this.distanceProperty = new DerivedProperty( [
this.object1.positionProperty,
this.object2.positionProperty
], function( x1, x2 ) {
return Math.abs( x2 - x1 ) / 1000;
}, {
phetioType: DerivedPropertyIO( NumberIO ),
tandem: tandem.createTandem( 'distanceProperty' )
} );
}

gravityForceLabBasics.register( 'GravityForceLabBasicsModel', GravityForceLabBasicsModel );
Expand Down
19 changes: 12 additions & 7 deletions js/gravity-force-lab-basics/view/DistanceArrowNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ define( function( require ) {
var PhetFont = require( 'SCENERY_PHET/PhetFont' );
var Property = require( 'AXON/Property' );
var StringUtils = require( 'PHETCOMMON/util/StringUtils' );
var Tandem = require( 'TANDEM/Tandem' );
var Text = require( 'SCENERY/nodes/Text' );

// strings
Expand All @@ -28,17 +29,20 @@ define( function( require ) {
var HEAD_HEIGHT = 6;

/**
* @param {Property} mass1PositionProperty
* @param {Property} mass2PositionProperty
* @param {GravityForceLabBasicsModel} model
* @param {ModelViewTransform2} modelViewTransform
* @param {Object} options
* @constructor
*/
function DistanceArrowNode( mass1PositionProperty, mass2PositionProperty, modelViewTransform, options ) {
function DistanceArrowNode( model, modelViewTransform, options ) {

_.extend( {
tandem: Tandem.required
}, options );

Node.call( this, options );

var arrowNode = new ArrowNode( mass1PositionProperty.get(), 0, mass2PositionProperty.get(), 0, {
var arrowNode = new ArrowNode( model.object1.positionProperty.get(), 0, model.object2.positionProperty.get(), 0, {
doubleHead: true,
tailWidth: 0.5,
headHeight: HEAD_HEIGHT,
Expand All @@ -51,19 +55,20 @@ define( function( require ) {
// the label
var labelText = new Text( StringUtils.fillIn( distanceUnitsPatternString, { distance: 0 } ), {
font: new PhetFont( 12 ),
bottom: arrowNode.top + ( 3 * HEAD_WIDTH / 4 )
bottom: arrowNode.top + ( 3 * HEAD_WIDTH / 4 ),
tandem: options.tandem.createTandem( 'labelNode' )
} );
this.addChild( labelText );

Property.multilink( [ mass1PositionProperty, mass2PositionProperty ], function( position1, position2 ) {
Property.multilink( [ model.object1.positionProperty, model.object2.positionProperty ], function( position1, position2 ) {

// update the arrow node width
var viewPosition1 = modelViewTransform.modelToViewX( position1 );
var viewPosition2 = modelViewTransform.modelToViewX( position2 );
arrowNode.setTailAndTip( viewPosition1, 0, viewPosition2, 0 );

// update label text and center, distance in meters so divide by 1000 to read out in km
labelText.setText( StringUtils.fillIn( distanceUnitsPatternString, { distance: ( position2 - position1 ) / 1000 } ) );
labelText.setText( StringUtils.fillIn( distanceUnitsPatternString, { distance: model.distanceProperty.get() } ) );

labelText.centerX = arrowNode.centerX;
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ define( function( require ) {
this.addChild( mass2Node.arrowNode );

// arrow that shows distance between the two masses
var distanceArrowNode = new DistanceArrowNode( model.object1.positionProperty, model.object2.positionProperty, modelViewTransform, {
var distanceArrowNode = new DistanceArrowNode( model, modelViewTransform, {
tandem: tandem.createTandem( 'distanceArrowNode' ),
y: 145
} );
Expand Down

0 comments on commit 681b20a

Please sign in to comment.