Skip to content

Commit

Permalink
Convert to use PhetioObject.createIOType, see phetsims/tandem#188
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Sep 1, 2020
1 parent 7e2035f commit ff8b13a
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 123 deletions.
50 changes: 48 additions & 2 deletions js/charges-and-fields/model/ChargedParticle.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
*/

import merge from '../../../../phet-core/js/merge.js';
import PhetioObject from '../../../../tandem/js/PhetioObject.js';
import Tandem from '../../../../tandem/js/Tandem.js';
import NumberIO from '../../../../tandem/js/types/NumberIO.js';
import VoidIO from '../../../../tandem/js/types/VoidIO.js';
import chargesAndFields from '../../chargesAndFields.js';
import ChargedParticleIO from './ChargedParticleIO.js';
import ModelElement from './ModelElement.js';

class ChargedParticle extends ModelElement {
Expand All @@ -25,7 +27,7 @@ class ChargedParticle extends ModelElement {

// {Tandem}
tandem: Tandem.REQUIRED,
phetioType: ChargedParticleIO,
phetioType: ChargedParticle.ChargedParticleIO,
phetioDynamicElement: true
}, options );
super( initialPosition, options );
Expand All @@ -34,7 +36,51 @@ class ChargedParticle extends ModelElement {
// @public (read-only) {number} - a charge of one corresponds to one nano Coulomb
this.charge = charge;
}

// @public
applyState( stateObject ) {
super.applyState( stateObject );
this.charge = stateObject.charge;
}

/**
* @public
* @returns {Object}
* @override
*/
toStateObject() {
const parentStateObject = super.toStateObject();
parentStateObject.charge = this.charge;
return parentStateObject;
}

/**
* @override
* @param {Object} stateObject - see ChargedParticleIO.toStateObject
* @returns {Array.<*>}
* @public
*/
static stateToArgsForConstructor( stateObject ) {
assert && assert( stateObject.charge === 1 || stateObject.charge === -1 );
// Put charge first for the chargedParticleGroup create function api.
return [ stateObject.charge ].concat( ModelElement.ModelElementIO.stateToArgsForConstructor( stateObject ) );
}
}

ChargedParticle.ChargedParticleIO = PhetioObject.createIOType( ChargedParticle, 'ChargedParticleIO', ModelElement.ModelElementIO, {
documentation: 'A Charged Particle',
methods: {
setCharge: {
returnType: VoidIO,
parameterTypes: [ NumberIO ],
implementation: function( value ) {
this.phetioObject.charge = value.charge;
},
documentation: 'Set charge (in units of e)',
invocableForReadOnlyElements: false
}
}
} );

chargesAndFields.register( 'ChargedParticle', ChargedParticle );
export default ChargedParticle;
62 changes: 0 additions & 62 deletions js/charges-and-fields/model/ChargedParticleIO.js

This file was deleted.

9 changes: 4 additions & 5 deletions js/charges-and-fields/model/ChargesAndFieldsModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import Tandem from '../../../../tandem/js/Tandem.js';
import chargesAndFields from '../../chargesAndFields.js';
import ChargesAndFieldsConstants from '../ChargesAndFieldsConstants.js';
import ChargedParticle from './ChargedParticle.js';
import ChargedParticleIO from './ChargedParticleIO.js';
import ElectricFieldSensor from './ElectricFieldSensor.js';
import ElectricPotentialLine from './ElectricPotentialLine.js';
import ElectricPotentialLineIO from './ElectricPotentialLineIO.js';
import ElectricPotentialSensor from './ElectricPotentialSensor.js';
import MeasuringTape from './MeasuringTape.js';
import ModelElementIO from './ModelElementIO.js';
import ModelElement from './ModelElement.js';

// constants
const GRID_MINOR_SPACING = ChargesAndFieldsConstants.GRID_MAJOR_SPACING / ChargesAndFieldsConstants.MINOR_GRIDLINES_PER_MAJOR_GRIDLINE;
Expand Down Expand Up @@ -136,7 +135,7 @@ class ChargesAndFieldsModel extends PhetioObject {
return chargedParticle;
}, [ 1, Vector2.ZERO ], {
tandem: tandem.createTandem( 'chargedParticleGroup' ),
phetioType: PhetioGroupIO( ChargedParticleIO ),
phetioType: PhetioGroupIO( ChargedParticle.ChargedParticleIO ),
phetioDynamicElementName: 'particle'
} );
const chargedParticleGroup = this.chargedParticleGroup;
Expand All @@ -145,7 +144,7 @@ class ChargesAndFieldsModel extends PhetioObject {
// This is the relevant array to calculate the electric field, and electric potential
// @public {ObservableArray.<ChargedParticle>}
this.activeChargedParticles = new ObservableArray( {
phetioType: PropertyIO( ChargedParticleIO )
phetioType: PropertyIO( ChargedParticle.ChargedParticleIO )
} );

// @public {PhetioGroup.<ElectricFieldSensor>} Observable group of electric field sensors
Expand All @@ -155,7 +154,7 @@ class ChargesAndFieldsModel extends PhetioObject {
return sensor;
}, [ Vector2.ZERO ], {
tandem: tandem.createTandem( 'electricFieldSensorGroup' ),
phetioType: PhetioGroupIO( ModelElementIO )
phetioType: PhetioGroupIO( ModelElement.ModelElementIO )
} ); // {ObservableArray.<ElectricFieldSensor>}
const electricFieldSensorGroup = this.electricFieldSensorGroup;

Expand Down
4 changes: 2 additions & 2 deletions js/charges-and-fields/model/ElectricPotentialLineIO.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import validate from '../../../../axon/js/validate.js';
import Vector2IO from '../../../../dot/js/Vector2IO.js';
import ObjectIO from '../../../../tandem/js/types/ObjectIO.js';
import chargesAndFields from '../../chargesAndFields.js';
import ModelElementIO from './ModelElementIO.js';
import ModelElement from './ModelElement.js';

class ElectricPotentialLineIO extends ModelElementIO {
class ElectricPotentialLineIO extends ModelElement.ModelElementIO {

/**
* @public
Expand Down
50 changes: 48 additions & 2 deletions js/charges-and-fields/model/ModelElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
import BooleanProperty from '../../../../axon/js/BooleanProperty.js';
import Emitter from '../../../../axon/js/Emitter.js';
import Vector2 from '../../../../dot/js/Vector2.js';
import Vector2IO from '../../../../dot/js/Vector2IO.js';
import Vector2Property from '../../../../dot/js/Vector2Property.js';
import merge from '../../../../phet-core/js/merge.js';
import PhetioObject from '../../../../tandem/js/PhetioObject.js';
import NullableIO from '../../../../tandem/js/types/NullableIO.js';
import ObjectIO from '../../../../tandem/js/types/ObjectIO.js';
import chargesAndFields from '../../chargesAndFields.js';
import ChargesAndFieldsConstants from '../ChargesAndFieldsConstants.js';
import ModelElementIO from './ModelElementIO.js';

// constants
const NullableIOVector2IO = NullableIO( Vector2IO );

class ModelElement extends PhetioObject {

Expand All @@ -26,7 +31,7 @@ class ModelElement extends PhetioObject {

options = merge( {

phetioType: ModelElementIO
phetioType: ModelElement.ModelElementIO
}, options );
super( options );

Expand Down Expand Up @@ -119,7 +124,48 @@ class ModelElement extends PhetioObject {

this.animationTween.start( phet.joist.elapsedTime );
}

/**
* @public
* @returns {Object}
* @override
*/
toStateObject() {
return {
initialPosition: NullableIOVector2IO.toStateObject( this.initialPosition )
};
}

// @private
static fromStateObject( stateObject ) {
return {
initialPosition: NullableIOVector2IO.fromStateObject( stateObject.initialPosition )
};
}

/**
* @param {Object} stateObject
* @public
*/
applyState( stateObject ) {
const s = ModelElement.fromStateObject( stateObject );
this.initialPosition = s.initialPosition;
}

/**
* @public
* @override
* @param {Object} stateObject
* @returns {Array.<*>}
*/
static stateToArgsForConstructor( stateObject ) {
return [ NullableIOVector2IO.fromStateObject( stateObject.initialPosition ) ];
}
}

ModelElement.ModelElementIO = PhetioObject.createIOType( ModelElement, 'ModelElementIO', ObjectIO, {
documentation: 'A Model Element'
} );

chargesAndFields.register( 'ModelElement', ModelElement );
export default ModelElement;
50 changes: 0 additions & 50 deletions js/charges-and-fields/model/ModelElementIO.js

This file was deleted.

0 comments on commit ff8b13a

Please sign in to comment.