Skip to content

Commit

Permalink
Add ResistorIO, see #589
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Jul 27, 2020
1 parent 58d7ded commit 1f69594
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
3 changes: 2 additions & 1 deletion js/model/Circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import Inductor from './Inductor.js';
import LightBulb from './LightBulb.js';
import ModifiedNodalAnalysisAdapter from './ModifiedNodalAnalysisAdapter.js';
import Resistor from './Resistor.js';
import ResistorIO from './ResistorIO.js';
import Switch from './Switch.js';
import Vertex from './Vertex.js';
import VertexIO from './VertexIO.js';
Expand Down Expand Up @@ -328,7 +329,7 @@ class Circuit {
argumentArray.push( Resistor.ResistorType.RESISTOR );
return argumentArray;
}, {
phetioType: PhetioGroupIO( CircuitElementIO ),
phetioType: PhetioGroupIO( ResistorIO ),
tandem: tandem.createTandem( 'resistorGroup' )
} );

Expand Down
4 changes: 3 additions & 1 deletion js/model/Resistor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import merge from '../../../phet-core/js/merge.js';
import CCKCConstants from '../CCKCConstants.js';
import circuitConstructionKitCommon from '../circuitConstructionKitCommon.js';
import FixedCircuitElement from './FixedCircuitElement.js';
import ResistorIO from './ResistorIO.js';

class Resistor extends FixedCircuitElement {

Expand All @@ -26,7 +27,8 @@ class Resistor extends FixedCircuitElement {
*/
constructor( startVertex, endVertex, resistorType, tandem, options ) {
options = merge( {
isFlammable: true // All resistors are flammable except for the dog, which automatically disconnects at high current.
isFlammable: true, // All resistors are flammable except for the dog, which automatically disconnects at high current.
phetioType: ResistorIO
}, options );

assert && assert( !options.hasOwnProperty( 'resistance' ), 'Resistance should be passed through resistorType' );
Expand Down
56 changes: 56 additions & 0 deletions js/model/ResistorIO.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2019-2020, University of Colorado Boulder

/**
* IO type for CircuitElement
*
* @author Sam Reid (PhET Interactive Simulations)
*/

import validate from '../../../axon/js/validate.js';
import EnumerationIO from '../../../phet-core/js/EnumerationIO.js';
import CouldNotYetDeserializeError from '../../../tandem/js/CouldNotYetDeserializeError.js';
import ObjectIO from '../../../tandem/js/types/ObjectIO.js';
import circuitConstructionKitCommon from '../circuitConstructionKitCommon.js';
import Resistor from './Resistor.js';

class ResistorIO extends ObjectIO {

// @public
static toStateObject( resistor ) {
validate( resistor, this.validator );
return {
startVertexID: resistor.startVertexProperty.value.tandem.phetioID,
endVertexID: resistor.endVertexProperty.value.tandem.phetioID,
resistorType: EnumerationIO( Resistor.ResistorType ).toStateObject( resistor.resistorType )
};
}

/**
* @override
* @param {Object} stateObject - see ResistorIO.toStateObject
* @returns {Array.<*>}
* @public
*/
static stateToArgsForConstructor( stateObject ) {
if ( phet.phetio.phetioEngine.hasPhetioObject( stateObject.startVertexID ) &&
phet.phetio.phetioEngine.hasPhetioObject( stateObject.endVertexID ) ) {
return [
phet.phetio.phetioEngine.getPhetioObject( stateObject.startVertexID ),
phet.phetio.phetioEngine.getPhetioObject( stateObject.endVertexID ),
EnumerationIO( Resistor.ResistorType ).fromStateObject( stateObject.resistorType )
];
}
else {
throw new CouldNotYetDeserializeError();
}
}
}

ResistorIO.methods = {};
ResistorIO.documentation = 'A resistor';
ResistorIO.validator = { isValidValue: v => v instanceof phet.circuitConstructionKitCommon.Resistor };
ResistorIO.typeName = 'ResistorIO';
ObjectIO.validateSubtype( ResistorIO );

circuitConstructionKitCommon.register( 'ResistorIO', ResistorIO );
export default ResistorIO;

0 comments on commit 1f69594

Please sign in to comment.