Skip to content

Commit

Permalink
Port CircuitElementViewType to string union enumeration, see #749
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Oct 14, 2021
1 parent 6fcb13b commit 4bf7e94
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 63 deletions.
2 changes: 1 addition & 1 deletion js/model/ACVoltage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ACVoltage extends VoltageSource {
range: new Range( -MAX_VOLTAGE, MAX_VOLTAGE )
}
}, options ) as ACVoltageOptions;
super( startVertex, endVertex, internalResistanceProperty, CCKCConstants.BATTERY_LENGTH, tandem, options );
super( startVertex, endVertex, internalResistanceProperty, CCKCConstants.BATTERY_LENGTH, tandem, filledOptions );

// @public {NumberProperty} - the maximum voltage, which can be controlled by the CircuitElementNumberControl
this.maximumVoltageProperty = new NumberProperty( filledOptions.voltage, {
Expand Down
2 changes: 1 addition & 1 deletion js/model/Battery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Battery extends VoltageSource {
range: batteryType === 'normal' ? new Range( 0, 120 ) : new Range( 100, 100000 )
}
}, options ) as BatteryOptions;
super( startVertex, endVertex, internalResistanceProperty, BATTERY_LENGTH, tandem, options );
super( startVertex, endVertex, internalResistanceProperty, BATTERY_LENGTH, tandem, filledOptions );

// @public (read-only) {string} - track which way the battery "button" (plus side) was facing the initial state so
// the user can only create a certain number of "left" or "right" batteries from the toolbox.
Expand Down
4 changes: 2 additions & 2 deletions js/model/CircuitConstructionKitModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import BooleanProperty from '../../../axon/js/BooleanProperty.js';
import Emitter from '../../../axon/js/Emitter.js';
import EnumerationProperty from '../../../axon/js/EnumerationProperty.js';
import Property from '../../../axon/js/Property.js';
import NumberProperty from '../../../axon/js/NumberProperty.js';
import Utils from '../../../dot/js/Utils.js';
import merge from '../../../phet-core/js/merge.js';
Expand All @@ -21,7 +22,6 @@ import circuitConstructionKitCommon from '../circuitConstructionKitCommon.js';
import ZoomControlPanel from '../view/ZoomControlPanel.js';
import Ammeter from './Ammeter.js';
import Circuit from './Circuit.js';
import CircuitElementViewType from './CircuitElementViewType.js';
import Voltmeter from './Voltmeter.js';
import ZoomAnimation from './ZoomAnimation.js';

Expand All @@ -45,7 +45,7 @@ class CircuitConstructionKitModel {
this.zoomAnimation = null;

// @public {Property.<CircuitElementViewType>} - whether to show lifelike or schematic representations
this.viewTypeProperty = new EnumerationProperty( CircuitElementViewType, CircuitElementViewType.LIFELIKE, {
this.viewTypeProperty = new Property( 'lifelike', {
tandem: tandem.createTandem( 'viewTypeProperty' )
} );

Expand Down
17 changes: 0 additions & 17 deletions js/model/CircuitElementViewType.js

This file was deleted.

12 changes: 12 additions & 0 deletions js/model/CircuitElementViewType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2017-2020, University of Colorado Boulder

/**
* Enumeration for how to render the circuit elements: lifelike or schematic.
* Because of how this file is used in the model and query parameter file, it must be declared separately
* to avoid circular module loading errors.
*
* @author Sam Reid (PhET Interactive Simulations)
*/

type CircuitElementViewType = 'lifelike'|'schematic';
export default CircuitElementViewType
5 changes: 2 additions & 3 deletions js/model/LightBulb.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import Utils from '../../../dot/js/Utils.js';
import Vector2 from '../../../dot/js/Vector2.js';
import merge from '../../../phet-core/js/merge.js';
import circuitConstructionKitCommon from '../circuitConstructionKitCommon.js';
import CircuitElementViewType from './CircuitElementViewType.js';
import FixedCircuitElement from './FixedCircuitElement.js';
import Vertex from './Vertex.js';

Expand Down Expand Up @@ -109,7 +108,7 @@ class LightBulb extends FixedCircuitElement {
*/
getPathLength() {
let pathLength = 0;
const samplePoints = this.viewTypeProperty.value === CircuitElementViewType.LIFELIKE ? LIFELIKE_SAMPLE_POINTS : SCHEMATIC_SAMPLE_POINTS;
const samplePoints = this.viewTypeProperty.value === 'lifelike' ? LIFELIKE_SAMPLE_POINTS : SCHEMATIC_SAMPLE_POINTS;
let currentPoint = this.getFilamentPathPoint( 0, Vector2.ZERO, samplePoints );
for ( let i = 1; i < samplePoints.length; i++ ) {
const nextPoint = this.getFilamentPathPoint( i, Vector2.ZERO, samplePoints );
Expand Down Expand Up @@ -183,7 +182,7 @@ class LightBulb extends FixedCircuitElement {

let previousAccumulatedDistance = 0;
let accumulatedDistance = 0;
const samplePoints = this.viewTypeProperty.value === CircuitElementViewType.LIFELIKE ? LIFELIKE_SAMPLE_POINTS : SCHEMATIC_SAMPLE_POINTS;
const samplePoints = this.viewTypeProperty.value === 'lifelike' ? LIFELIKE_SAMPLE_POINTS : SCHEMATIC_SAMPLE_POINTS;
let currentPoint = this.getFilamentPathPoint( 0, this.startVertexProperty.get().positionProperty.get(), samplePoints );
for ( let i = 1; i < samplePoints.length; i++ ) {
const nextPoint = this.getFilamentPathPoint( i, this.startVertexProperty.get().positionProperty.get(), samplePoints );
Expand Down
2 changes: 1 addition & 1 deletion js/model/VoltageSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class VoltageSource extends FixedCircuitElement {
tandem: tandem.createTandem( 'voltageProperty' )
}
}, options ) as VoltageSourceOptions;
super( startVertex, endVertex, length, tandem, options );
super( startVertex, endVertex, length, tandem, filledOptions );

// @public {NumberProperty} - the voltage of the battery in volts
this.voltageProperty = new NumberProperty( filledOptions.voltage, filledOptions.voltagePropertyOptions );
Expand Down
3 changes: 1 addition & 2 deletions js/view/CCKCLightBulbNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import lightBulbFrontRealImage from '../../images/light-bulb-front-real_png.js';
import lightBulbMiddleImage from '../../mipmaps/light-bulb-middle_png.js';
import CCKCConstants from '../CCKCConstants.js';
import circuitConstructionKitCommon from '../circuitConstructionKitCommon.js';
import CircuitElementViewType from '../model/CircuitElementViewType.js';
import CustomLightBulbNode from './CustomLightBulbNode.js';
import FixedCircuitElementNode from './FixedCircuitElementNode.js';
import LightBulbSocketNode from './LightBulbSocketNode.js';
Expand Down Expand Up @@ -231,7 +230,7 @@ class CCKCLightBulbNode extends FixedCircuitElementNode {
options
);
viewListener = view => {
this.rayNodeContainer.visible = view === CircuitElementViewType.LIFELIKE;
this.rayNodeContainer.visible = view === 'lifelike';
};
viewTypeProperty.link( viewListener );
circuitLayerNode && circuitLayerNode.lightBulbSocketLayer.addChild( this.socketNode );
Expand Down
5 changes: 2 additions & 3 deletions js/view/CapacitorCircuitElementNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import Tandem from '../../../tandem/js/Tandem.js';
import wireIconImage from '../../images/wire-icon_png.js';
import CCKCConstants from '../CCKCConstants.js';
import circuitConstructionKitCommon from '../circuitConstructionKitCommon.js';
import CircuitElementViewType from '../model/CircuitElementViewType.js';
import FixedCircuitElementNode from './FixedCircuitElementNode.js';

// constants
Expand Down Expand Up @@ -237,7 +236,7 @@ class CapacitorCircuitElementNode extends FixedCircuitElementNode {
*/
frontSideContainsSensorPoint( globalPoint ) {

if ( this.viewTypeProperty.value === CircuitElementViewType.LIFELIKE ) {
if ( this.viewTypeProperty.value === 'lifelike' ) {
return this.capacitorCircuitElementLifelikeNode.frontSideContainsSensorPoint( globalPoint ) ||
this.leftWireStub.containsPoint( this.leftWireStub.globalToParentPoint( globalPoint ) );
}
Expand All @@ -254,7 +253,7 @@ class CapacitorCircuitElementNode extends FixedCircuitElementNode {
*/
backSideContainsSensorPoint( globalPoint ) {

if ( this.viewTypeProperty.value === CircuitElementViewType.LIFELIKE ) {
if ( this.viewTypeProperty.value === 'lifelike' ) {
return this.capacitorCircuitElementLifelikeNode.backSideContainsSensorPoint( globalPoint ) ||
this.rightWireStub.containsPoint( this.rightWireStub.globalToParentPoint( globalPoint ) );
}
Expand Down
3 changes: 1 addition & 2 deletions js/view/ChargeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import Node from '../../../scenery/js/nodes/Node.js';
import Tandem from '../../../tandem/js/Tandem.js';
import circuitConstructionKitCommon from '../circuitConstructionKitCommon.js';
import Capacitor from '../model/Capacitor.js';
import CircuitElementViewType from '../model/CircuitElementViewType.js';
import ConventionalCurrentArrowNode from './ConventionalCurrentArrowNode.js';

// constants
Expand Down Expand Up @@ -115,7 +114,7 @@ class ChargeNode extends Node {
// For unknown reasons, the x and y coordinates are swapped here. The values were determined empirically.
let globalClipShape = null;

const isLifelike = this.circuitLayerNode.model.viewTypeProperty.value === CircuitElementViewType.LIFELIKE;
const isLifelike = this.circuitLayerNode.model.viewTypeProperty.value === 'lifelike';
if ( isLifelike ) {

// For the lifelike view, we clip based on the pseudo-3d effect, so the charges come from "behind" the edge
Expand Down
15 changes: 7 additions & 8 deletions js/view/CircuitElementToolFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import circuitConstructionKitCommon from '../circuitConstructionKitCommon.js';
import ACVoltage from '../model/ACVoltage.js';
import Battery from '../model/Battery.js';
import Capacitor from '../model/Capacitor.js';
import CircuitElementViewType from '../model/CircuitElementViewType.js';
import Fuse from '../model/Fuse.js';
import Inductor from '../model/Inductor.js';
import LightBulb from '../model/LightBulb.js';
Expand Down Expand Up @@ -70,8 +69,8 @@ const SWITCH_LENGTH = CCKCConstants.SWITCH_LENGTH;

// Separate icons are made for schematic/lifelike so they can be aligned
const iconAlignGroup = new AlignGroup();
const LIFELIKE_PROPERTY = new Property( CircuitElementViewType.LIFELIKE );
const SCHEMATIC_PROPERTY = new Property( CircuitElementViewType.SCHEMATIC );
const LIFELIKE_PROPERTY = new Property( 'lifelike' );
const SCHEMATIC_PROPERTY = new Property( 'schematic' );

class CircuitElementToolFactory {

Expand Down Expand Up @@ -155,13 +154,13 @@ class CircuitElementToolFactory {
const schematicIcon = wrap( createIcon( options.tandem.createTandem( 'schematicIcon' ), SCHEMATIC_PROPERTY ), options.schematicIconHeight );

const toggleNode = new ToggleNode( this.viewTypeProperty, [
{ value: CircuitElementViewType.LIFELIKE, node: lifelikeIcon },
{ value: CircuitElementViewType.SCHEMATIC, node: schematicIcon }
{ value: 'lifelike', node: lifelikeIcon },
{ value: 'schematic', node: schematicIcon }
] );

this.viewTypeProperty.link( viewType => {
lifelikeIcon.visible = viewType === CircuitElementViewType.LIFELIKE;
schematicIcon.visible = viewType === CircuitElementViewType.SCHEMATIC;
lifelikeIcon.visible = viewType === 'lifelike';
schematicIcon.visible = viewType === 'schematic';
} );

return new CircuitElementToolNode(
Expand Down Expand Up @@ -190,7 +189,7 @@ class CircuitElementToolFactory {
// Cache a single instance to simplify PhET-iO
this.wireToolNode = this.createCircuitElementToolNode( wireString, CCKCConstants.NUMBER_OF_WIRES,
( tandem, viewTypeProperty ) => {
return viewTypeProperty.value === CircuitElementViewType.LIFELIKE ? new Image( wireIconImage, {
return viewTypeProperty.value === 'lifelike' ? new Image( wireIconImage, {
tandem: tandem
} ) : new Line( 0, 0, 120, 0, {
stroke: Color.BLACK,
Expand Down
3 changes: 1 addition & 2 deletions js/view/CircuitLayerNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import ACVoltage from '../model/ACVoltage.js';
import Battery from '../model/Battery.js';
import Capacitor from '../model/Capacitor.js';
import CurrentSense from '../model/CurrentSense.js';
import CircuitElementViewType from '../model/CircuitElementViewType.js';
import Dog from '../model/Dog.js';
import FixedCircuitElement from '../model/FixedCircuitElement.js';
import Fuse from '../model/Fuse.js';
Expand Down Expand Up @@ -228,7 +227,7 @@ class CircuitLayerNode extends Node {
// choose layering for schematic vs lifelike. HEADS UP, this means circuitLayerNode.addChild() will get overwritten
// so all nodes must be added as children in the array above.
screenView.model.viewTypeProperty.link( view => {
this.children = ( view === CircuitElementViewType.LIFELIKE ) ? lifelikeLayering : schematicLayering;
this.children = ( view === 'lifelike' ) ? lifelikeLayering : schematicLayering;
} );

// @public {Property.<Bounds2>} the visible bounds in the coordinate frame of the circuit. Initialized with a
Expand Down
3 changes: 1 addition & 2 deletions js/view/FixedCircuitElementNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import Node from '../../../scenery/js/nodes/Node.js';
import fireImage from '../../images/fire_png.js';
import CCKCUtils from '../CCKCUtils.js';
import circuitConstructionKitCommon from '../circuitConstructionKitCommon.js';
import CircuitElementViewType from '../model/CircuitElementViewType.js';
import Resistor from '../model/Resistor.js';
import CircuitElementNode from './CircuitElementNode.js';
import CircuitLayerNodeDragListener from './CircuitLayerNodeDragListener.js';
Expand Down Expand Up @@ -193,7 +192,7 @@ class FixedCircuitElementNode extends CircuitElementNode {
* @private
*/
setViewType( viewType ) {
this.contentNode.children = [ viewType === CircuitElementViewType.LIFELIKE ? this.lifelikeNode : this.schematicNode ];
this.contentNode.children = [ viewType === 'lifelike' ? this.lifelikeNode : this.schematicNode ];

// Update the dimensions of the highlight. For Switches, retain the original bounds (big enough to encapsulate
// both schematic and lifelike open and closed).
Expand Down
3 changes: 1 addition & 2 deletions js/view/SensorToolbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import CCKCConstants from '../CCKCConstants.js';
import circuitConstructionKitCommonStrings from '../circuitConstructionKitCommonStrings.js';
import circuitConstructionKitCommon from '../circuitConstructionKitCommon.js';
import Ammeter from '../model/Ammeter.js';
import CircuitElementViewType from '../model/CircuitElementViewType.js';
import SeriesAmmeter from '../model/SeriesAmmeter.js';
import Vertex from '../model/Vertex.js';
import Voltmeter from '../model/Voltmeter.js';
Expand Down Expand Up @@ -138,7 +137,7 @@ class SensorToolbox extends CCKCPanel {
const seriesAmmeterToolNode = new CircuitElementToolNode(
'',
new BooleanProperty( false ),
new Property( CircuitElementViewType.SCHEMATIC ),
new Property( 'schematic' ),
circuit,
point => circuitLayerNode.globalToLocalPoint( point ),
seriesAmmeterNodeIcon,
Expand Down
3 changes: 1 addition & 2 deletions js/view/SeriesAmmeterNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import CCKCConstants from '../CCKCConstants.js';
import CCKCUtils from '../CCKCUtils.js';
import circuitConstructionKitCommonStrings from '../circuitConstructionKitCommonStrings.js';
import circuitConstructionKitCommon from '../circuitConstructionKitCommon.js';
import CircuitElementViewType from '../model/CircuitElementViewType.js';
import FixedCircuitElementNode from './FixedCircuitElementNode.js';

const currentString = circuitConstructionKitCommonStrings.current;
Expand Down Expand Up @@ -158,7 +157,7 @@ class SeriesAmmeterNode extends FixedCircuitElementNode {
screenView,
circuitLayerNode,
seriesAmmeter,
new Property( CircuitElementViewType.LIFELIKE ),
new Property( 'lifelike' ),
lifelikeNode,
new Node( { children: [ lifelikeNode ] } ), // reuse lifelike view for the schematic view
tandem,
Expand Down
15 changes: 7 additions & 8 deletions js/view/SwitchNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import Color from '../../../scenery/js/util/Color.js';
import LinearGradient from '../../../scenery/js/util/LinearGradient.js';
import CCKCConstants from '../CCKCConstants.js';
import circuitConstructionKitCommon from '../circuitConstructionKitCommon.js';
import CircuitElementViewType from '../model/CircuitElementViewType.js';
import FixedCircuitElementNode from './FixedCircuitElementNode.js';

// constants
Expand Down Expand Up @@ -70,7 +69,7 @@ const createNode = function( viewType, fill, thickness, curveDiameter, closed )
x: SWITCH_LENGTH * SWITCH_START,
fill: fill,
stroke: Color.BLACK,
lineWidth: viewType === CircuitElementViewType.SCHEMATIC ? 0 : 1,
lineWidth: viewType === 'schematic' ? 0 : 1,
pickable: true // This is necessary because we use scenery hit testing for the probe hit testing
} );

Expand Down Expand Up @@ -105,7 +104,7 @@ const createNode = function( viewType, fill, thickness, curveDiameter, closed )
children: [ leftSegmentNode, rotatingSegmentNode, rightSegmentNode, lifelikeHinge ]
} );

if ( viewType === CircuitElementViewType.SCHEMATIC ) {
if ( viewType === 'schematic' ) {
node.addChild( new Circle( thickness * 0.6, {
fill: Color.BLACK,
stroke: Color.BLACK,
Expand All @@ -123,21 +122,21 @@ const createNode = function( viewType, fill, thickness, curveDiameter, closed )

// Create all of the images
const lifelikeOpenNode = createNode(
CircuitElementViewType.LIFELIKE, lifelikeGradient, LIFELIKE_DIAMETER, 6, false
'lifelike', lifelikeGradient, LIFELIKE_DIAMETER, 6, false
);
const lifelikeOpenImage = lifelikeOpenNode.rasterized( { wrap: false } );

const lifelikeClosedNode = createNode(
CircuitElementViewType.LIFELIKE, lifelikeGradient, LIFELIKE_DIAMETER, 6, true
'lifelike', lifelikeGradient, LIFELIKE_DIAMETER, 6, true
);
const lifelikeClosedImage = lifelikeClosedNode.rasterized( { wrap: false } );

const schematicOpenImage = createNode(
CircuitElementViewType.SCHEMATIC, Color.BLACK, CCKCConstants.SCHEMATIC_LINE_WIDTH, 0, false
'schematic', Color.BLACK, CCKCConstants.SCHEMATIC_LINE_WIDTH, 0, false
).rasterized( { wrap: false } );

const schematicClosedImage = createNode(
CircuitElementViewType.SCHEMATIC, Color.BLACK, CCKCConstants.SCHEMATIC_LINE_WIDTH, 0, true
'schematic', Color.BLACK, CCKCConstants.SCHEMATIC_LINE_WIDTH, 0, true
).rasterized( { wrap: false } );

class SwitchNode extends FixedCircuitElementNode {
Expand Down Expand Up @@ -200,7 +199,7 @@ class SwitchNode extends FixedCircuitElementNode {

// @private {Node} - For hit testing
this.lifelikeOpenNode = createNode(
CircuitElementViewType.LIFELIKE, lifelikeGradient, LIFELIKE_DIAMETER, 6, false
'lifelike', lifelikeGradient, LIFELIKE_DIAMETER, 6, false
);

// @private {function} - clean up resources when no longer used.
Expand Down
9 changes: 4 additions & 5 deletions js/view/ViewRadioButtonGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import Tandem from '../../../tandem/js/Tandem.js';
import CCKCConstants from '../CCKCConstants.js';
import circuitConstructionKitCommon from '../circuitConstructionKitCommon.js';
import Battery from '../model/Battery.js';
import CircuitElementViewType from '../model/CircuitElementViewType.js';
import Vertex from '../model/Vertex.js';
import BatteryNode from './BatteryNode.js';

Expand Down Expand Up @@ -65,14 +64,14 @@ class ViewRadioButtonGroup extends RectangularRadioButtonGroup {
isIcon: true,
scale: SCALE
} );
const lifelikeIcon = createBatteryNode( CircuitElementViewType.LIFELIKE, tandem.createTandem( 'lifelikeIcon' ) );
const schematicIcon = createBatteryNode( CircuitElementViewType.SCHEMATIC, tandem.createTandem( 'schematicIcon' ) );
const lifelikeIcon = createBatteryNode( 'lifelike', tandem.createTandem( 'lifelikeIcon' ) );
const schematicIcon = createBatteryNode( 'schematic', tandem.createTandem( 'schematicIcon' ) );
super( viewTypeProperty, [ {
value: CircuitElementViewType.LIFELIKE,
value: 'lifelike',
node: lifelikeIcon,
tandemName: 'lifelikeRadioButton'
}, {
value: CircuitElementViewType.SCHEMATIC,
value: 'schematic',
node: schematicIcon,
tandemName: 'schematicRadioButton'
} ], options );
Expand Down
Loading

0 comments on commit 4bf7e94

Please sign in to comment.