Skip to content

Commit

Permalink
use type-specific Properties and validation #111
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Malley <[email protected]>
  • Loading branch information
pixelzoom committed May 22, 2018
1 parent 8fd66f1 commit 92da1aa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 32 deletions.
31 changes: 16 additions & 15 deletions js/common/model/PlinkoProbabilityCommonModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ define( function( require ) {
'use strict';

// modules
var BooleanProperty = require( 'AXON/BooleanProperty' );
var Emitter = require( 'AXON/Emitter' );
var GaltonBoard = require( 'PLINKO_PROBABILITY/common/model/GaltonBoard' );
var Histogram = require( 'PLINKO_PROBABILITY/common/model/Histogram' );
var inherit = require( 'PHET_CORE/inherit' );
var NumberProperty = require( 'AXON/NumberProperty' );
var ObservableArray = require( 'AXON/ObservableArray' );
var plinkoProbability = require( 'PLINKO_PROBABILITY/plinkoProbability' );
var PlinkoProbabilityConstants = require( 'PLINKO_PROBABILITY/common/PlinkoProbabilityConstants' );
var Property = require( 'AXON/Property' );
var PropertyIO = require( 'AXON/PropertyIO' );

// phet-io modules
var StringIO = require( 'ifphetio!PHET_IO/types/StringIO' );
var StringProperty = require( 'AXON/StringProperty' );

// constants
var BALL_MODE_VALUES = [ 'oneBall', 'tenBalls', 'allBalls', 'continuous' ]; // values for ballModeProperty
Expand All @@ -32,25 +30,28 @@ define( function( require ) {
function PlinkoProbabilityCommonModel() {

// @public {number} this can be a number between 0 and 1
this.probabilityProperty = new Property( PlinkoProbabilityConstants.BINARY_PROBABILITY_RANGE.defaultValue );
this.probabilityProperty = new NumberProperty( PlinkoProbabilityConstants.BINARY_PROBABILITY_RANGE.defaultValue, {
range: PlinkoProbabilityConstants.BINARY_PROBABILITY_RANGE
} );

// @public {string} controls how many balls are dispensed when the 'play' button is pressed
this.ballModeProperty = new Property( 'oneBall', {
validValues: BALL_MODE_VALUES,
phetioType: PropertyIO( StringIO )
this.ballModeProperty = new StringProperty( 'oneBall', {
validValues: BALL_MODE_VALUES
} );

// {string} controls what comes out of the hopper above the Galton board
this.hopperModeProperty = new Property( 'ball', {
validValues: HOPPER_MODE_VALUES,
phetioType: PropertyIO( StringIO )
this.hopperModeProperty = new StringProperty( 'ball', {
validValues: HOPPER_MODE_VALUES
} );

// {boolean} is the maximum number of balls reached?
this.isBallCapReachedProperty = new Property( false );
this.isBallCapReachedProperty = new BooleanProperty( false );

// {number} number of rows in the Galton board, must be an integer
this.numberOfRowsProperty = new Property( PlinkoProbabilityConstants.ROWS_RANGE.defaultValue );
// {number} number of rows in the Galton board
this.numberOfRowsProperty = new NumberProperty( PlinkoProbabilityConstants.ROWS_RANGE.defaultValue, {
range: PlinkoProbabilityConstants.ROWS_RANGE,
numberType: 'Integer'
} );

this.ballCreationTimeElapsed = 0; // @public {number} - time elapsed since last ball creation
this.balls = new ObservableArray(); // @public
Expand Down
21 changes: 7 additions & 14 deletions js/common/view/PlinkoProbabilityViewProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ define( function( require ) {
'use strict';

// modules
var BooleanProperty = require( 'AXON/BooleanProperty' );
var inherit = require( 'PHET_CORE/inherit' );
var plinkoProbability = require( 'PLINKO_PROBABILITY/plinkoProbability' );
var Property = require( 'AXON/Property' );
var PropertyIO = require( 'AXON/PropertyIO' );

// phet-io modules
var BooleanIO = require( 'ifphetio!PHET_IO/types/BooleanIO' );
var StringIO = require( 'ifphetio!PHET_IO/types/StringIO' );
var StringProperty = require( 'AXON/StringProperty' );

// constants
var HISTOGRAM_MODE_VALUES = [ 'counter', 'cylinder', 'fraction' ]; // values for histogramModeProperty
Expand All @@ -32,15 +28,12 @@ define( function( require ) {
}, options );

// @public
this.histogramModeProperty = new Property( options.histogramMode, { phetioType: PropertyIO( StringIO ) } );
this.expandedAccordionBoxProperty = new Property( true );
this.isTheoreticalHistogramVisibleProperty = new Property( false );
this.isSoundEnabledProperty = new Property( false, { phetioType: PropertyIO( BooleanIO ) } );

// validate string values
this.histogramModeProperty.link( function( histogramMode ) {
assert && assert( _.includes( HISTOGRAM_MODE_VALUES, histogramMode ), 'invalid histogramMode: ' + histogramMode );
this.histogramModeProperty = new StringProperty( options.histogramMode, {
validValues: HISTOGRAM_MODE_VALUES
} );
this.expandedAccordionBoxProperty = new BooleanProperty( true );
this.isTheoreticalHistogramVisibleProperty = new BooleanProperty( false );
this.isSoundEnabledProperty = new BooleanProperty( false );
}

plinkoProbability.register( 'PlinkoProbabilityViewProperties', PlinkoProbabilityViewProperties );
Expand Down
7 changes: 4 additions & 3 deletions js/lab/model/LabModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ define( function( require ) {

// modules
var BallPhase = require( 'PLINKO_PROBABILITY/common/model/BallPhase' );
var BooleanProperty = require( 'AXON/BooleanProperty' );
var inherit = require( 'PHET_CORE/inherit' );
var LabBall = require( 'PLINKO_PROBABILITY/lab/model/LabBall' );
var plinkoProbability = require( 'PLINKO_PROBABILITY/plinkoProbability' );
var PlinkoProbabilityCommonModel = require( 'PLINKO_PROBABILITY/common/model/PlinkoProbabilityCommonModel' );
var PlinkoProbabilityQueryParameters = require( 'PLINKO_PROBABILITY/common/PlinkoProbabilityQueryParameters' );
var Property = require( 'AXON/Property' );

// constants
var MAX_BALLS = PlinkoProbabilityQueryParameters.maxBallsLab; // max number of balls *per bin*
Expand All @@ -30,7 +30,7 @@ define( function( require ) {
PlinkoProbabilityCommonModel.call( this );

// @public
this.isPlayingProperty = new Property( false );
this.isPlayingProperty = new BooleanProperty( false );

this.hopperModeProperty.link( function( hopperMode ) {

Expand All @@ -48,7 +48,8 @@ define( function( require ) {
self.balls.clear(); // clear the balls
} );

this.ballCreationTimeInterval = 0; // time we want to pass before we created a new ball
// @private time we want to pass before we created a new ball
this.ballCreationTimeInterval = 0;
}

plinkoProbability.register( 'LabModel', LabModel );
Expand Down

0 comments on commit 92da1aa

Please sign in to comment.