Skip to content

Commit

Permalink
Converting dot Utils usage, see phetsims/dot#4
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanolson committed Feb 14, 2025
1 parent b17bff7 commit e9b23b7
Show file tree
Hide file tree
Showing 21 changed files with 71 additions and 64 deletions.
6 changes: 3 additions & 3 deletions js/BicyclePumpNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import BooleanProperty from '../../axon/js/BooleanProperty.js';
import TProperty from '../../axon/js/TProperty.js';
import TReadOnlyProperty from '../../axon/js/TReadOnlyProperty.js';
import Range from '../../dot/js/Range.js';
import Utils from '../../dot/js/Utils.js';
import { clamp } from '../../dot/js/util/clamp.js';
import Vector2 from '../../dot/js/Vector2.js';
import Shape from '../../kite/js/Shape.js';
import InstanceRegistry from '../../phet-core/js/documentation/InstanceRegistry.js';
Expand Down Expand Up @@ -323,7 +323,7 @@ export default class BicyclePumpNode extends Node {

// Update the handle position based on the user's pointer position.
const dragPositionY = this.pumpHandleNode.globalToParentPoint( event.pointer.point ).y;
const handlePosition = Utils.clamp( dragPositionY, minHandleYOffset, maxHandleYOffset );
const handlePosition = clamp( dragPositionY, minHandleYOffset, maxHandleYOffset );
this.dragDelegate.handleDrag( handlePosition );
},
tandem: options.tandem.createTandem( 'dragListener' )
Expand All @@ -336,7 +336,7 @@ export default class BicyclePumpNode extends Node {
dragSpeed: 200,
shiftDragSpeed: 50,
drag: ( event, listener ) => {
const handlePosition = Utils.clamp( this.pumpHandleNode.centerY + listener.modelDelta.y, minHandleYOffset, maxHandleYOffset );
const handlePosition = clamp( this.pumpHandleNode.centerY + listener.modelDelta.y, minHandleYOffset, maxHandleYOffset );
this.dragDelegate.handleDrag( handlePosition );
},
tandem: options.tandem.createTandem( 'keyboardDragListener' )
Expand Down
8 changes: 4 additions & 4 deletions js/ConductivityTesterNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import TProperty from '../../axon/js/TProperty.js';
import TReadOnlyProperty from '../../axon/js/TReadOnlyProperty.js';
import Dimension2 from '../../dot/js/Dimension2.js';
import Range from '../../dot/js/Range.js';
import Utils from '../../dot/js/Utils.js';
import Vector2 from '../../dot/js/Vector2.js';
import Shape from '../../kite/js/Shape.js';
import InstanceRegistry from '../../phet-core/js/documentation/InstanceRegistry.js';
Expand All @@ -38,6 +37,7 @@ import PhetFont from './PhetFont.js';
import PlusNode from './PlusNode.js';
import sceneryPhet from './sceneryPhet.js';
import SceneryPhetStrings from './SceneryPhetStrings.js';
import { clamp } from '../../dot/js/util/clamp.js';

// constants
const SHOW_TESTER_ORIGIN = false; // draws a red circle at the tester's origin, for debugging
Expand Down Expand Up @@ -225,7 +225,7 @@ export default class ConductivityTesterNode extends Node {
const positionView = options.modelViewTransform.modelToViewPosition( positionProperty.value );
let yView = listener.currentTarget.globalToParentPoint( event.pointer.point ).y + positionView.y - clickYOffset;
if ( options.probeDragYRange ) {
yView = Utils.clamp( yView, positionView.y + options.probeDragYRange.min, positionView.y + options.probeDragYRange.max );
yView = clamp( yView, positionView.y + options.probeDragYRange.min, positionView.y + options.probeDragYRange.max );
}

// convert to model coordinate frame
Expand All @@ -247,13 +247,13 @@ export default class ConductivityTesterNode extends Node {

const yPositiveProbe = positiveProbePositionProperty.value.y + listener.modelDelta.y;
const yPositiveProbeConstrained = options.probeDragYRange ?
Utils.clamp( yPositiveProbe, y + options.probeDragYRange.min, y + options.probeDragYRange.max ) :
clamp( yPositiveProbe, y + options.probeDragYRange.min, y + options.probeDragYRange.max ) :
yPositiveProbe;
positiveProbePositionProperty.value = new Vector2( positiveProbePositionProperty.value.x, yPositiveProbeConstrained );

const yNegativeProbe = negativeProbePositionProperty.value.y + listener.modelDelta.y;
const yNegativeProbeConstrained = options.probeDragYRange ?
Utils.clamp( yNegativeProbe, y + options.probeDragYRange.min, y + options.probeDragYRange.max ) :
clamp( yNegativeProbe, y + options.probeDragYRange.min, y + options.probeDragYRange.max ) :
yNegativeProbe;
negativeProbePositionProperty.value = new Vector2( negativeProbePositionProperty.value.x, yNegativeProbeConstrained );
},
Expand Down
7 changes: 4 additions & 3 deletions js/GaugeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import TReadOnlyProperty from '../../axon/js/TReadOnlyProperty.js';
import Matrix3 from '../../dot/js/Matrix3.js';
import Range from '../../dot/js/Range.js';
import Utils from '../../dot/js/Utils.js';
import { clamp } from '../../dot/js/util/clamp.js';
import Shape from '../../kite/js/Shape.js';
import InstanceRegistry from '../../phet-core/js/documentation/InstanceRegistry.js';
import optionize, { combineOptions } from '../../phet-core/js/optionize.js';
Expand All @@ -24,6 +24,7 @@ import TColor from '../../scenery/js/util/TColor.js';
import Tandem from '../../tandem/js/Tandem.js';
import PhetFont from './PhetFont.js';
import sceneryPhet from './sceneryPhet.js';
import { linear } from '../../dot/js/util/linear.js';

type GaugeNodeLabelTextOptions = StrictOmit<TextOptions, 'maxWidth' | 'tandem'>;

Expand Down Expand Up @@ -149,8 +150,8 @@ export default class GaugeNode extends Node {
if ( typeof ( valueProperty.get() ) === 'number' ) {

// clamp value to valid range and map it to an angle
const clampedValue = Utils.clamp( valueProperty.get(), range.min, range.max );
const needleAngle = Utils.linear( range.min, range.max, startAngle, endAngle, clampedValue );
const clampedValue = clamp( valueProperty.get(), range.min, range.max );
const needleAngle = linear( range.min, range.max, startAngle, endAngle, clampedValue );

// 2d rotation, but reusing our matrix above
needle.setMatrix( scratchMatrix.setToRotationZ( needleAngle ) );
Expand Down
4 changes: 2 additions & 2 deletions js/LaserPointerNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import TProperty from '../../axon/js/TProperty.js';
import Dimension2 from '../../dot/js/Dimension2.js';
import Utils from '../../dot/js/Utils.js';
import Vector2 from '../../dot/js/Vector2.js';
import InstanceRegistry from '../../phet-core/js/documentation/InstanceRegistry.js';
import merge from '../../phet-core/js/merge.js';
Expand All @@ -26,6 +25,7 @@ import RoundStickyToggleButton from '../../sun/js/buttons/RoundStickyToggleButto
import Tandem from '../../tandem/js/Tandem.js';
import sceneryPhet from './sceneryPhet.js';
import ShadedSphereNode, { ShadedSphereNodeOptions } from './ShadedSphereNode.js';
import { linear } from '../../dot/js/util/linear.js';

type ButtonType = 'toggle' | 'momentary';

Expand Down Expand Up @@ -192,7 +192,7 @@ export default class LaserPointerNode extends Node {
const glassOptions = merge( {}, options.glassOptions, {

// The origin is at the output point of the nozzle, translate accordingly
centerX: Utils.linear( 0, 1, -glassDiameter / 2, 0, options.glassOptions.proportionStickingOut! ),
centerX: linear( 0, 1, -glassDiameter / 2, 0, options.glassOptions.proportionStickingOut! ),

// Center vertically
centerY: 0
Expand Down
4 changes: 2 additions & 2 deletions js/LightBulbNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import TReadOnlyProperty from '../../axon/js/TReadOnlyProperty.js';
import Utils from '../../dot/js/Utils.js';
import InstanceRegistry from '../../phet-core/js/documentation/InstanceRegistry.js';
import optionize, { EmptySelfOptions } from '../../phet-core/js/optionize.js';
import StrictOmit from '../../phet-core/js/types/StrictOmit.js';
Expand All @@ -18,6 +17,7 @@ import lightBulbOff_png from '../mipmaps/lightBulbOff_png.js';
import lightBulbOn_png from '../mipmaps/lightBulbOn_png.js';
import LightRaysNode, { LightRaysNodeOptions } from './LightRaysNode.js';
import sceneryPhet from './sceneryPhet.js';
import { linear } from '../../dot/js/util/linear.js';

type SelfOptions = {
bulbImageScale?: number;
Expand Down Expand Up @@ -102,7 +102,7 @@ export default class LightBulbNode extends Node {
assert && assert( brightness >= 0 && brightness <= 1 );
this.onNode.visible = ( brightness > 0 );
if ( this.onNode.visible ) {
this.onNode.opacity = Utils.linear( 0, 1, 0.3, 1, brightness );
this.onNode.opacity = linear( 0, 1, 0.3, 1, brightness );
}
this.raysNode.setBrightness( brightness );
}
Expand Down
10 changes: 6 additions & 4 deletions js/LightRaysNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
* @author Chris Malley (PixelZoom, Inc.)
*/

import Utils from '../../dot/js/Utils.js';
import Shape from '../../kite/js/Shape.js';
import optionize from '../../phet-core/js/optionize.js';
import Path, { PathOptions } from '../../scenery/js/nodes/Path.js';
import sceneryPhet from './sceneryPhet.js';
import { roundSymmetric } from '../../dot/js/util/roundSymmetric.js';
import { linear } from '../../dot/js/util/linear.js';
import { clamp } from '../../dot/js/util/clamp.js';

// constants, these are specific to bulb images
const RAYS_START_ANGLE = 3 * Math.PI / 4;
Expand Down Expand Up @@ -84,7 +86,7 @@ export default class LightRaysNode extends Path {
assert && assert( brightness >= 0 && brightness <= 1 );

// number of rays is a function of brightness
const numberOfRays = ( brightness === 0 ) ? 0 : this.minRays + Utils.roundSymmetric( brightness * ( this.maxRays - this.minRays ) );
const numberOfRays = ( brightness === 0 ) ? 0 : this.minRays + roundSymmetric( brightness * ( this.maxRays - this.minRays ) );

// ray length is a function of brightness
const rayLength = this.minRayLength + ( brightness * ( this.maxRayLength - this.minRayLength ) );
Expand All @@ -93,14 +95,14 @@ export default class LightRaysNode extends Path {
const deltaAngle = RAYS_ARC_ANGLE / ( numberOfRays - 1 );

// The ray line width is a linear function within the allowed range
const lineWidth = Utils.linear(
const lineWidth = linear(
0.3 * this.maxRayLength,
0.6 * this.maxRayLength,
this.shortRayLineWidth,
this.longRayLineWidth,
rayLength
);
this.lineWidth = Utils.clamp( lineWidth, this.shortRayLineWidth, this.longRayLineWidth );
this.lineWidth = clamp( lineWidth, this.shortRayLineWidth, this.longRayLineWidth );

const shape = new Shape();

Expand Down
7 changes: 4 additions & 3 deletions js/MatrixNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
*/

import Matrix3 from '../../dot/js/Matrix3.js';
import Utils from '../../dot/js/Utils.js';
import { toFixed } from '../../dot/js/util/toFixed.js';
import { toFixedNumber } from '../../dot/js/util/toFixedNumber.js';
import Shape from '../../kite/js/Shape.js';
import merge from '../../phet-core/js/merge.js';
import PhetFont from '../../scenery-phet/js/PhetFont.js';
Expand Down Expand Up @@ -79,8 +80,8 @@ class MatrixNode extends Node {
else {
// value is a number, round it to the desired number of decimal places.
valueString = options.stripTrailingZeros ?
'' + Utils.toFixedNumber( value, options.decimalPlaces ) :
Utils.toFixed( value, options.decimalPlaces );
'' + toFixedNumber( value, options.decimalPlaces ) :
toFixed( value, options.decimalPlaces );
}

// Cell value
Expand Down
4 changes: 2 additions & 2 deletions js/MeasuringTapeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Property from '../../axon/js/Property.js';
import TProperty from '../../axon/js/TProperty.js';
import TReadOnlyProperty from '../../axon/js/TReadOnlyProperty.js';
import Bounds2 from '../../dot/js/Bounds2.js';
import Utils from '../../dot/js/Utils.js';
import { toFixed } from '../../dot/js/util/toFixed.js';
import Vector2 from '../../dot/js/Vector2.js';
import Vector2Property from '../../dot/js/Vector2Property.js';
import Shape from '../../kite/js/Shape.js';
Expand Down Expand Up @@ -295,7 +295,7 @@ class MeasuringTapeNode extends Node {
const readoutStringProperty = new DerivedStringProperty(
[ this.unitsProperty, this.measuredDistanceProperty, SceneryPhetStrings.measuringTapeReadoutPatternStringProperty ],
( units, measuredDistance, measuringTapeReadoutPattern ) => {
const distance = Utils.toFixed( units.multiplier * measuredDistance, this.significantFigures );
const distance = toFixed( units.multiplier * measuredDistance, this.significantFigures );
return StringUtils.fillIn( measuringTapeReadoutPattern, {
distance: distance,
units: units.name
Expand Down
8 changes: 4 additions & 4 deletions js/NumberControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import Property from '../../axon/js/Property.js';
import TReadOnlyProperty from '../../axon/js/TReadOnlyProperty.js';
import Dimension2 from '../../dot/js/Dimension2.js';
import Range from '../../dot/js/Range.js';
import Utils from '../../dot/js/Utils.js';
import InstanceRegistry from '../../phet-core/js/documentation/InstanceRegistry.js';
import optionize, { combineOptions } from '../../phet-core/js/optionize.js';
import Orientation from '../../phet-core/js/Orientation.js';
Expand Down Expand Up @@ -42,6 +41,7 @@ import IOType from '../../tandem/js/types/IOType.js';
import NumberDisplay, { NumberDisplayOptions } from './NumberDisplay.js';
import PhetFont from './PhetFont.js';
import sceneryPhet from './sceneryPhet.js';
import { roundToInterval } from '../../dot/js/util/roundToInterval.js';

// constants
const SPECIFIC_COMPONENT_CALLBACK_OPTIONS = [
Expand Down Expand Up @@ -275,7 +275,7 @@ export default class NumberControl extends WidthSizable( Node ) {
// the arrow buttons, see https://github.com/phetsims/scenery-phet/issues/384.
const constrainValue = ( value: number ) => {
assert && assert( options.delta !== undefined );
const newValue = Utils.roundToInterval( value, options.delta );
const newValue = roundToInterval( value, options.delta );
return getCurrentRange().constrainValue( newValue );
};

Expand Down Expand Up @@ -465,7 +465,7 @@ export default class NumberControl extends WidthSizable( Node ) {
decrementButton = new ArrowButton( 'left', () => {
const oldValue = numberProperty.get();
let newValue = numberProperty.get() - options.delta;
newValue = Utils.roundToInterval( newValue, options.delta ); // constrain to multiples of delta, see #384
newValue = roundToInterval( newValue, options.delta ); // constrain to multiples of delta, see #384
newValue = Math.max( newValue, getCurrentRange().min ); // constrain to range
numberProperty.set( newValue );
options.soundGenerator!.playSoundForValueChange( newValue, oldValue );
Expand All @@ -482,7 +482,7 @@ export default class NumberControl extends WidthSizable( Node ) {
incrementButton = new ArrowButton( 'right', () => {
const oldValue = numberProperty.get();
let newValue = numberProperty.get() + options.delta;
newValue = Utils.roundToInterval( newValue, options.delta ); // constrain to multiples of delta, see #384
newValue = roundToInterval( newValue, options.delta ); // constrain to multiples of delta, see #384
newValue = Math.min( newValue, getCurrentRange().max ); // constrain to range
numberProperty.set( newValue );
options.soundGenerator!.playSoundForValueChange( newValue, oldValue );
Expand Down
4 changes: 2 additions & 2 deletions js/NumberDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import TinyProperty from '../../axon/js/TinyProperty.js';
import TProperty from '../../axon/js/TProperty.js';
import TReadOnlyProperty from '../../axon/js/TReadOnlyProperty.js';
import Range from '../../dot/js/Range.js';
import Utils from '../../dot/js/Utils.js';
import { toFixed } from '../../dot/js/util/toFixed.js';
import Vector2 from '../../dot/js/Vector2.js';
import optionize, { combineOptions } from '../../phet-core/js/optionize.js';
import StrictOmit from '../../phet-core/js/types/StrictOmit.js';
Expand Down Expand Up @@ -156,7 +156,7 @@ export default class NumberDisplay extends Node {
return `${value}`;
}
else {
return Utils.toFixed( value, options.decimalPlaces );
return toFixed( value, options.decimalPlaces );
}
} );

Expand Down
10 changes: 5 additions & 5 deletions js/PointerCoordinatesNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Chris Malley (PixelZoom, Inc.)
*/

import Utils from '../../dot/js/Utils.js';
import { toFixed } from '../../dot/js/util/toFixed.js';
import getGlobal from '../../phet-core/js/getGlobal.js';
import optionize from '../../phet-core/js/optionize.js';
import ModelViewTransform2 from '../../phetcommon/js/view/ModelViewTransform2.js';
Expand Down Expand Up @@ -85,13 +85,13 @@ export default class PointerCoordinatesNode extends Node {

// (x,y) in view coordinates
const viewPoint = this.globalToParentPoint( event.pointer.point );
const xView = Utils.toFixed( viewPoint.x, options.viewDecimalPlaces );
const yView = Utils.toFixed( viewPoint.y, options.viewDecimalPlaces );
const xView = toFixed( viewPoint.x, options.viewDecimalPlaces );
const yView = toFixed( viewPoint.y, options.viewDecimalPlaces );

// (x,y) in model coordinates
const modelPoint = modelViewTransform.viewToModelPosition( viewPoint );
const xModel = Utils.toFixed( modelPoint.x, options.modelDecimalPlaces );
const yModel = Utils.toFixed( modelPoint.y, options.modelDecimalPlaces );
const xModel = toFixed( modelPoint.x, options.modelDecimalPlaces );
const yModel = toFixed( modelPoint.y, options.modelDecimalPlaces );

// Update coordinates display.
textNode.string = `(${xView},${yView})<br>(${xModel},${yModel})`;
Expand Down
4 changes: 2 additions & 2 deletions js/ScientificNotationNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/

import TReadOnlyProperty from '../../axon/js/TReadOnlyProperty.js';
import Utils from '../../dot/js/Utils.js';
import InstanceRegistry from '../../phet-core/js/documentation/InstanceRegistry.js';
import optionize, { EmptySelfOptions } from '../../phet-core/js/optionize.js';
import StrictOmit from '../../phet-core/js/types/StrictOmit.js';
Expand All @@ -22,6 +21,7 @@ import TColor from '../../scenery/js/util/TColor.js';
import MathSymbols from './MathSymbols.js';
import PhetFont from './PhetFont.js';
import sceneryPhet from './sceneryPhet.js';
import { toFixed } from '../../dot/js/util/toFixed.js';

type SelfOptions = {
fill?: TColor;
Expand Down Expand Up @@ -201,7 +201,7 @@ export default class ScientificNotationNode extends Node {
if ( options.exponent !== null ) {

// M x 10^E, where E is options.exponent
mantissa = Utils.toFixed( value / Math.pow( 10, options.exponent ), options.mantissaDecimalPlaces );
mantissa = toFixed( value / Math.pow( 10, options.exponent ), options.mantissaDecimalPlaces );
exponent = options.exponent.toString();
}
else {
Expand Down
5 changes: 3 additions & 2 deletions js/SpectrumNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@

import Bounds2 from '../../dot/js/Bounds2.js';
import Dimension2 from '../../dot/js/Dimension2.js';
import Utils from '../../dot/js/Utils.js';
import optionize from '../../phet-core/js/optionize.js';
import StrictOmit from '../../phet-core/js/types/StrictOmit.js';
import Image from '../../scenery/js/nodes/Image.js';
import Node, { NodeOptions } from '../../scenery/js/nodes/Node.js';
import Color from '../../scenery/js/util/Color.js';
import sceneryPhet from './sceneryPhet.js';
import { clamp } from '../../dot/js/util/clamp.js';
import { linear } from '../../dot/js/util/linear.js';

const DEFAULT_SIZE = new Dimension2( 150, 30 );

Expand Down Expand Up @@ -70,7 +71,7 @@ export default class SpectrumNode extends Node {

// Draw the spectrum.
for ( let i = 0; i < canvas.width; i++ ) {
const value = Utils.clamp( Utils.linear( 0, canvas.width, options.minValue, options.maxValue, i ), options.minValue, options.maxValue );
const value = clamp( linear( 0, canvas.width, options.minValue, options.maxValue, i ), options.minValue, options.maxValue );
context.fillStyle = options.valueToColor( value ).toCSS();
context.fillRect( i, 0, 1, canvas.height );
}
Expand Down
Loading

0 comments on commit e9b23b7

Please sign in to comment.