Skip to content

Commit

Permalink
migrated files from inherit to class, see #304
Browse files Browse the repository at this point in the history
  • Loading branch information
jbphet committed Jul 16, 2020
1 parent e9d5b2b commit 2244247
Show file tree
Hide file tree
Showing 8 changed files with 480 additions and 487 deletions.
21 changes: 9 additions & 12 deletions js/atomic-interactions/view/PushPinNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@
* @author Siddhartha Chinthapally (Actual Concepts)
*/

import inherit from '../../../../phet-core/js/inherit.js';
import Image from '../../../../scenery/js/nodes/Image.js';
import Node from '../../../../scenery/js/nodes/Node.js';
import pushPinImg from '../../../images/push-pin_png.js';
import statesOfMatter from '../../statesOfMatter.js';

/**
* @constructor
*/
function PushPinNode() {
Node.call( this );
this.setPickable( false );
const imageNode = new Image( pushPinImg );
this.addChild( imageNode, { scale: 0.3 } ); // scale empirically determined
class PushPinNode extends Node {

constructor() {
super();
this.setPickable( false );
const imageNode = new Image( pushPinImg );
this.addChild( imageNode, { scale: 0.3 } ); // scale empirically determined
}
}

statesOfMatter.register( 'PushPinNode', PushPinNode );

inherit( Node, PushPinNode );
export default PushPinNode;
export default PushPinNode;
241 changes: 119 additions & 122 deletions js/atomic-interactions/view/ZoomableGridNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
* @author Siddhartha Chinthapally
*/

import merge from '../../../../phet-core/js/merge.js';
import Shape from '../../../../kite/js/Shape.js';
import inherit from '../../../../phet-core/js/inherit.js';
import merge from '../../../../phet-core/js/merge.js';
import ZoomButton from '../../../../scenery-phet/js/buttons/ZoomButton.js';
import Node from '../../../../scenery/js/nodes/Node.js';
import Path from '../../../../scenery/js/nodes/Path.js';
Expand All @@ -22,130 +21,127 @@ const MAX_LINES_HORIZONTAL = 9;
const MIN_LINES_HORIZONTAL = 5;
const ZOOM_INCREMENT = 2; // lines per zoom

/**
* @param atomsView
* @param {number} offsetX
* @param {number} offsetY
* @param {number} width - width of the graph
* @param {number} height - height of the graph
* @param {Object} [options]
* @constructor
*/
function ZoomableGridNode( atomsView, offsetX, offsetY, width, height, options ) {

options = merge( {
addZoomButtons: true,
tandem: Tandem.REQUIRED
}, options );

Node.call( this, options );
const self = this;
atomsView.horizontalLineCount = MIN_LINES_HORIZONTAL;

// @private horizontal grid lines
this.horizontalLinesNode = new Path( null, {
stroke: 'white',
lineWidth: 0.8,
opacity: 0.6
} );

// @private vertical grid lines
this.verticalLinesNode = new Path( null, {
stroke: 'white',
lineWidth: 0.8,
opacity: 0.6
} );

if ( options.addZoomButtons ) {

// @private zoom in button
this.zoomInButton = new ZoomButton( {
listener: function() {
atomsView.horizontalLineCount -= ZOOM_INCREMENT;
self.setHorizontalLines( offsetX, offsetY, width, height, atomsView.horizontalLineCount );
atomsView.verticalScalingFactor *= 3.33;
atomsView.drawPotentialCurve();
},
baseColor: '#FFD333',
radius: 8,
xMargin: 3,
yMargin: 3,
disabledBaseColor: '#EDEDED',
buttonAppearanceStrategy: RectangularButtonView.FlatAppearanceStrategy,
touchAreaXDilation: 10,
touchAreaYDilation: 8,
touchAreaYShift: -7,
tandem: options.tandem.createTandem( 'zoomInButton' )
class ZoomableGridNode extends Node {

/**
* @param atomsView
* @param {number} offsetX
* @param {number} offsetY
* @param {number} width - width of the graph
* @param {number} height - height of the graph
* @param {Object} [options]
*/
constructor( atomsView, offsetX, offsetY, width, height, options ) {

options = merge( {
addZoomButtons: true,
tandem: Tandem.REQUIRED
}, options );

super( options );
const self = this;
atomsView.horizontalLineCount = MIN_LINES_HORIZONTAL;

// @private horizontal grid lines
this.horizontalLinesNode = new Path( null, {
stroke: 'white',
lineWidth: 0.8,
opacity: 0.6
} );
this.zoomInButton.enabled = false;

// @private zoom out button
this.zoomOutButton = new ZoomButton( {
listener: function() {
atomsView.horizontalLineCount += ZOOM_INCREMENT;
self.setHorizontalLines( offsetX, offsetY, width, height, atomsView.horizontalLineCount );
atomsView.verticalScalingFactor /= 3.33;
atomsView.drawPotentialCurve();
},
baseColor: '#FFD333',
radius: 8,
xMargin: 3,
yMargin: 3,
disabledBaseColor: '#EDEDED',
buttonAppearanceStrategy: RectangularButtonView.FlatAppearanceStrategy,
in: false,
touchAreaXDilation: 10,
touchAreaYDilation: 8,
touchAreaYShift: 7,
tandem: options.tandem.createTandem( 'zoomOutButton' )

// @private vertical grid lines
this.verticalLinesNode = new Path( null, {
stroke: 'white',
lineWidth: 0.8,
opacity: 0.6
} );
this.zoomOutButton.enabled = true;
this.addChild( this.zoomInButton );
this.addChild( this.zoomOutButton );
}

this.addChild( this.horizontalLinesNode );
this.addChild( this.verticalLinesNode );
if ( options.addZoomButtons ) {

// @private zoom in button
this.zoomInButton = new ZoomButton( {
listener: function() {
atomsView.horizontalLineCount -= ZOOM_INCREMENT;
self.setHorizontalLines( offsetX, offsetY, width, height, atomsView.horizontalLineCount );
atomsView.verticalScalingFactor *= 3.33;
atomsView.drawPotentialCurve();
},
baseColor: '#FFD333',
radius: 8,
xMargin: 3,
yMargin: 3,
disabledBaseColor: '#EDEDED',
buttonAppearanceStrategy: RectangularButtonView.FlatAppearanceStrategy,
touchAreaXDilation: 10,
touchAreaYDilation: 8,
touchAreaYShift: -7,
tandem: options.tandem.createTandem( 'zoomInButton' )
} );
this.zoomInButton.enabled = false;

// @private zoom out button
this.zoomOutButton = new ZoomButton( {
listener: function() {
atomsView.horizontalLineCount += ZOOM_INCREMENT;
self.setHorizontalLines( offsetX, offsetY, width, height, atomsView.horizontalLineCount );
atomsView.verticalScalingFactor /= 3.33;
atomsView.drawPotentialCurve();
},
baseColor: '#FFD333',
radius: 8,
xMargin: 3,
yMargin: 3,
disabledBaseColor: '#EDEDED',
buttonAppearanceStrategy: RectangularButtonView.FlatAppearanceStrategy,
in: false,
touchAreaXDilation: 10,
touchAreaYDilation: 8,
touchAreaYShift: 7,
tandem: options.tandem.createTandem( 'zoomOutButton' )
} );
this.zoomOutButton.enabled = true;
this.addChild( this.zoomInButton );
this.addChild( this.zoomOutButton );
}

this.verticalLines = [];
for ( let x = 0; x < 4; x++ ) {
const viewX = x * ( width / 3 );
this.verticalLines.push( {
x1: viewX + offsetX, y1: offsetY,
x2: viewX + offsetX, y2: height + offsetY
} );
}
const verticalLineShape = new Shape();
let line;
for ( var i = 0; i < this.verticalLines.length; i++ ) {
line = this.verticalLines[ i ];
verticalLineShape.moveTo( line.x1, line.y1 );
verticalLineShape.lineTo( line.x2, line.y2 );
}
this.verticalLinesNode.setShape( verticalLineShape );

this.horizontalLines = [];
for ( let y = 0; y < atomsView.horizontalLineCount; y++ ) {
const viewY = y * ( height / ( atomsView.horizontalLineCount - 1 ) );
this.horizontalLines.push( {
x1: offsetX,
y1: viewY + offsetY,
x2: width + offsetX,
y2: viewY + offsetY
} );
}
const horizontalLineShape = new Shape();
for ( i = 0; i < this.horizontalLines.length; i++ ) {
line = this.horizontalLines[ i ];
horizontalLineShape.moveTo( line.x1, line.y1 );
horizontalLineShape.lineTo( line.x2, line.y2 );
}
this.horizontalLinesNode.setShape( horizontalLineShape );
}
this.addChild( this.horizontalLinesNode );
this.addChild( this.verticalLinesNode );

statesOfMatter.register( 'ZoomableGridNode', ZoomableGridNode );
this.verticalLines = [];
for ( let x = 0; x < 4; x++ ) {
const viewX = x * ( width / 3 );
this.verticalLines.push( {
x1: viewX + offsetX, y1: offsetY,
x2: viewX + offsetX, y2: height + offsetY
} );
}
const verticalLineShape = new Shape();
let line;
for ( var i = 0; i < this.verticalLines.length; i++ ) {
line = this.verticalLines[ i ];
verticalLineShape.moveTo( line.x1, line.y1 );
verticalLineShape.lineTo( line.x2, line.y2 );
}
this.verticalLinesNode.setShape( verticalLineShape );

inherit( Node, ZoomableGridNode, {
this.horizontalLines = [];
for ( let y = 0; y < atomsView.horizontalLineCount; y++ ) {
const viewY = y * ( height / ( atomsView.horizontalLineCount - 1 ) );
this.horizontalLines.push( {
x1: offsetX,
y1: viewY + offsetY,
x2: width + offsetX,
y2: viewY + offsetY
} );
}
const horizontalLineShape = new Shape();
for ( i = 0; i < this.horizontalLines.length; i++ ) {
line = this.horizontalLines[ i ];
horizontalLineShape.moveTo( line.x1, line.y1 );
horizontalLineShape.lineTo( line.x2, line.y2 );
}
this.horizontalLinesNode.setShape( horizontalLineShape );
}

/**
* @param {number} offsetX
Expand All @@ -155,7 +151,7 @@ inherit( Node, ZoomableGridNode, {
* @param {number} horizontalLineCount -- number of horizontal lines
* @public
*/
setHorizontalLines: function( offsetX, offsetY, width, height, horizontalLineCount ) {
setHorizontalLines( offsetX, offsetY, width, height, horizontalLineCount ) {

this.horizontalLines = [];
for ( let y = 0; y < horizontalLineCount; y++ ) {
Expand All @@ -182,6 +178,7 @@ inherit( Node, ZoomableGridNode, {
this.zoomInButton.enabled = ( horizontalLineCount > MIN_LINES_HORIZONTAL );
}
}
} );
}

statesOfMatter.register( 'ZoomableGridNode', ZoomableGridNode );
export default ZoomableGridNode;
57 changes: 33 additions & 24 deletions js/common/model/MovingAverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,62 @@

/**
* simple moving average calculator
*
* @author John Blanco (PhET Interactive Simulations)
*/

import inherit from '../../../../phet-core/js/inherit.js';
import merge from '../../../../phet-core/js/merge.js';
import statesOfMatter from '../../statesOfMatter.js';

/**
* @constructor
*/
function MovingAverage( size, options ) {
class MovingAverage {

options = merge( {
initialValue: 0
}, options );
/**
* @param {number} size
* @param {Object} [options]
*/
constructor( size, options ) {

// @public
this.size = size;
this.average = 0;
options = merge( {
initialValue: 0
}, options );

// @private
this.initialValue = options.initialValue;
this.array = new Array( size );
// @public
this.size = size;
this.average = 0;

// set up initial values
this.reset();
}

statesOfMatter.register( 'MovingAverage', MovingAverage );
// @private
this.initialValue = options.initialValue;
this.array = new Array( size );

inherit( Object, MovingAverage, {
// set up initial values
this.reset();
}

addValue: function( newValue ) {
/**
* add a value to the moving average
* @param {number} newValue
* @public
*/
addValue( newValue ) {
const replacedValue = this.array[ this.currentIndex ];
this.array[ this.currentIndex ] = newValue;
this.currentIndex = ( this.currentIndex + 1 ) % this.size;
this.total = ( this.total - replacedValue ) + newValue;
this.average = this.total / this.size;
},
}

reset: function() {
/**
* @public
*/
reset() {
for ( let i = 0; i < this.size; i++ ) {
this.array[ i ] = this.initialValue;
}
this.total = this.initialValue * this.size;
this.average = this.total / this.size;
this.currentIndex = 0;
}
} );
}

statesOfMatter.register( 'MovingAverage', MovingAverage );
export default MovingAverage;
Loading

0 comments on commit 2244247

Please sign in to comment.