Skip to content

Commit

Permalink
making sure arrow text does not go out of bounds, see issue #73
Browse files Browse the repository at this point in the history
  • Loading branch information
aadish committed Aug 24, 2016
1 parent d1a9e74 commit 98f0ea9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 2 additions & 0 deletions js/gravity-force-lab/model/GravityForceLabModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ define( function( require ) {
}
this.mass1.positionProperty.set( locationMass1 );
this.mass2.positionProperty.set( locationMass2 );
this.mass1.positionProperty.notifyObserversStatic();
this.mass2.positionProperty.notifyObserversStatic();
},

/**
Expand Down
4 changes: 2 additions & 2 deletions js/gravity-force-lab/view/GravityForceLabScreenView.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ define( function( require ) {
this.modelViewTransform = modelViewTransform; // Make MVT available to descendant types.

// add the mass nodes to the screen
this.addChild( new MassNode( model, model.mass1, this.layoutBounds.width, modelViewTransform, {
this.addChild( new MassNode( model, model.mass1, this.layoutBounds, modelViewTransform, {
label: mass1AbbreviatedString,
otherMassLabel: mass2AbbreviatedString,
direction: 'left',
Expand All @@ -60,7 +60,7 @@ define( function( require ) {
forceArrowHeight: 125
} ) );

this.addChild( new MassNode( model, model.mass2, this.layoutBounds.width, modelViewTransform, {
this.addChild( new MassNode( model, model.mass2, this.layoutBounds, modelViewTransform, {
label: mass2AbbreviatedString,
otherMassLabel: mass1AbbreviatedString,
direction: 'right',
Expand Down
19 changes: 14 additions & 5 deletions js/gravity-force-lab/view/MassNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ define( function( require ) {
var pullForceRange = new RangeWithValue( ( 0.5e-10 ), ( 1.1e-6 ) ); // empirically determined for linear mapping of pull objects
var arrowForceRange = new RangeWithValue( ( 6.0e-9 ), ( 4.1e-6 ) ); // empirically determined for linear mapping of pull objects
var OFFSET = 10; // empirically determined to make sure minimum force doesn't go to zero when rounded to 12 significant digits

var TEXT_OFFSET = 5; // emprically determined to make sure text does not go out of bounds
/**
* @param {GravityForceLabModel} model
* @param {MassModel} massModel
* @param {number} screenWidth
* @param {Bounds2} layoutBounds
* @param {ModelViewTransform} modelViewTransform
* @param {Object} [options]
* @constructor
*/
function MassNode( model, massModel, screenWidth, modelViewTransform, options ) {
function MassNode( model, massModel, layoutBounds, modelViewTransform, options ) {
var self = this;
options = _.extend( {
label: 'This Mass',
Expand Down Expand Up @@ -183,6 +183,15 @@ define( function( require ) {

massModel.positionProperty.link( function( prop ) {
thisNode.x = modelViewTransform.modelToViewX( prop );
// making sure arrow text does not goes out of dev bounds
if ( self.localToParentPoint( arrowText.center ).x - arrowText.width/2 < layoutBounds.left + TEXT_OFFSET ){
arrowText.left = self.parentToLocalBounds( layoutBounds ).left + TEXT_OFFSET;
}

if ( self.localToParentPoint( arrowText.center ).x + arrowText.width/2 > layoutBounds.right - TEXT_OFFSET ){
arrowText.right = self.parentToLocalBounds( layoutBounds ).right - TEXT_OFFSET;
}

} );
model.showValuesProperty.lazyLink( function() {
redrawForce();
Expand All @@ -208,8 +217,8 @@ define( function( require ) {
},
drag: function( event ) {
var x = thisNode.globalToParentPoint( event.pointer.point ).x - massClickXOffset;
var xMax = screenWidth - self.massCircle.width / 2 - self.pullerNode.width - OFFSET;
var xMin = OFFSET + self.massCircle.width / 2 + self.pullerNode.width;
var xMax = layoutBounds.maxX - self.massCircle.width / 2 - self.pullerNode.width - OFFSET;
var xMin = layoutBounds.minX + OFFSET + self.massCircle.width / 2 + self.pullerNode.width;
// for mass1 xMax is left boundary of
var sumRadius = modelViewTransform.modelToViewDeltaX( model.mass1.radius ) + modelViewTransform.modelToViewDeltaX( model.mass2.radius );
if ( massModel.position === model.mass1.position ) {
Expand Down

0 comments on commit 98f0ea9

Please sign in to comment.