Skip to content

Commit

Permalink
Cleaned up interface for UserControlledProperty, see phetsims/phet-io…
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Sep 18, 2018
1 parent d475b95 commit a170a0d
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions js/reentrant/ReentrantScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,36 @@ define( function( require ) {
range: new Range( SPRING_EQUILIBRIUM_X + this.xProperty.range.min, SPRING_EQUILIBRIUM_X + this.xProperty.range.max )
} );

this.userControlledXProperty = new UserControlledProperty( this.xProperty, { range: this.xProperty.range } );
this.userControlledFProperty = new UserControlledProperty( this.fProperty, { range: this.fProperty.range } );
this.userControlledPProperty = new UserControlledProperty( this.pProperty, { range: this.pProperty.range } );
this.userControllableXProperty = new UserControlledProperty( this.xProperty );
this.userControllableFProperty = new UserControlledProperty( this.fProperty );
this.userControllablePProperty = new UserControlledProperty( this.pProperty );

// logging
this.xProperty.link( function( displacement ) { phet.log && phet.log( 'x=' + displacement ); } );
this.fProperty.link( function( appliedForce ) { phet.log && phet.log( 'F=' + appliedForce ); } );
this.pProperty.link( function( left ) { phet.log && phet.log( 'p=' + left ); } );

this.xProperty.link( function( displacement ) {
if ( self.userControlledXProperty.isUserControlled() ) {

// If the user is changing the xProperty from the UI, then compute p=p(x) and F=F(x)
if ( self.userControllableXProperty.isUserControlled() ) {
self.pProperty.set( SPRING_EQUILIBRIUM_X + displacement ); // p = e + x
self.fProperty.set( SPRING_CONSTANT * displacement ); // F = kx
}
} );

this.fProperty.link( function( appliedForce ) {
if ( self.userControlledFProperty.isUserControlled() ) {

// If the user is changing the fProperty from the UI, then compute x=x(F)
if ( self.userControllableFProperty.isUserControlled() ) {
self.xProperty.set( appliedForce / SPRING_CONSTANT ); // x = F/k
}
} );

this.pProperty.link( function( left ) {
if ( self.userControlledPProperty.isUserControlled() ) {

// If the user is changing the pProperty from the UI, then compute x=x(p)
if ( self.userControllablePProperty.isUserControlled() ) {
self.xProperty.set( left - SPRING_EQUILIBRIUM_X ); // x = p - e
}
} );
Expand Down Expand Up @@ -122,7 +128,7 @@ define( function( require ) {
align: 'center',
spacing: 40,
children: [
createNumberControl( 'x:', model.userControlledXProperty, {
createNumberControl( 'x:', model.userControllableXProperty, {
decimalPlaces: 3,
delta: 0.01
} ),
Expand All @@ -142,7 +148,7 @@ define( function( require ) {
align: 'center',
spacing: 40,
children: [
createNumberControl( 'F:', model.userControlledFProperty, {
createNumberControl( 'F:', model.userControllableFProperty, {
decimalPlaces: 1,
delta: 1
} ),
Expand All @@ -156,7 +162,7 @@ define( function( require ) {
align: 'center',
spacing: 40,
children: [
createNumberControl( 'p:', model.userControlledPProperty, {
createNumberControl( 'p:', model.userControllablePProperty, {
decimalPlaces: 3,
delta: 0.01
} ),
Expand Down Expand Up @@ -187,14 +193,14 @@ define( function( require ) {
* @param {NumberProperty} property
* @param {Object} [options]
*/
function createNumberControl( label, numberProperty, numberPropertyIsUserControlledProperty, options ) {
assert && assert( numberProperty.range, 'missing range' );
return new NumberControl( label, numberProperty, numberProperty.range, numberPropertyIsUserControlledProperty, _.extend( {
function createNumberControl( label, property, options ) {
assert && assert( property.range, 'missing range' );
return new NumberControl( label, property, property.range, _.extend( {
titleFont: FONT,
valueFont: FONT,
majorTicks: [
{ value: numberProperty.range.min, label: new RichText( numberProperty.range.min ) },
{ value: numberProperty.range.max, label: new RichText( numberProperty.range.max ) }
{ value: property.range.min, label: new RichText( property.range.min ) },
{ value: property.range.max, label: new RichText( property.range.max ) }
]
}, options ) );
}
Expand Down

0 comments on commit a170a0d

Please sign in to comment.