Skip to content

Commit

Permalink
finished first pass on common view (still PDOM properties and some ot…
Browse files Browse the repository at this point in the history
…her common code are broken), #404
  • Loading branch information
zepumph committed Oct 21, 2021
1 parent 4f42a62 commit 8999552
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 65 deletions.
4 changes: 2 additions & 2 deletions js/common/model/RAPModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import NumberIO from '../../../../tandem/js/types/NumberIO.js';
import ratioAndProportion from '../../ratioAndProportion.js';
import rapConstants from '../rapConstants.js';
import RAPRatio from './RAPRatio.js';
import RatioTerm, { RatioTermType } from './RatioTerm.js';
import RatioTerm from './RatioTerm.js';
import Tandem from '../../../../tandem/js/Tandem.js';
import RAPRatioTuple from './RAPRatioTuple.js';

Expand Down Expand Up @@ -257,7 +257,7 @@ unclampedFitness: ${unclampedFitness}
* @returns {number}
* @public
*/
getIdealValueForTerm( ratioTerm: RatioTermType ) {
getIdealValueForTerm( ratioTerm: RatioTerm ) {
if ( ratioTerm === RatioTerm.ANTECEDENT ) {
return this.targetRatioProperty.value * this.ratio.tupleProperty.value.consequent;
}
Expand Down
4 changes: 2 additions & 2 deletions js/common/model/RAPRatioTuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import IOType from '../../../../tandem/js/types/IOType.js';
import NumberIO from '../../../../tandem/js/types/NumberIO.js';
import Range from '../../../../dot/js/Range.js';
import ratioAndProportion from '../../ratioAndProportion.js';
import RatioTerm, { RatioTermType } from './RatioTerm.js';
import RatioTerm from './RatioTerm.js';

class RAPRatioTuple {

Expand Down Expand Up @@ -115,7 +115,7 @@ class RAPRatioTuple {
* @param {RatioTerm} ratioTerm
* @returns {number}
*/
getForTerm( ratioTerm: RatioTermType ) {
getForTerm( ratioTerm: RatioTerm ) {
switch( ratioTerm ) {
case RatioTerm.ANTECEDENT:
return this.antecedent;
Expand Down
17 changes: 4 additions & 13 deletions js/common/model/RatioTerm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,10 @@
* @author Michael Kauzmann (PhET Interactive Simulations)
*/

import Enumeration from '../../../../phet-core/js/Enumeration.js';
import ratioAndProportion from '../../ratioAndProportion.js';

type RatioTermType = {
ANTECEDENT: Object;
CONSEQUENT: Object;
}

// @ts-ignore
const RatioTerm = <RatioTermType>Enumeration.byKeys( [ 'ANTECEDENT', 'CONSEQUENT' ] );


ratioAndProportion.register( 'RatioTerm', RatioTerm );
enum RatioTerm {
ANTECEDENT,
CONSEQUENT
}

export { RatioTermType };
export default RatioTerm;
9 changes: 8 additions & 1 deletion js/common/view/CueDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,12 @@
* @author Michael Kauzmann (PhET Interactive Simulations)
*/

type CueDisplay = 'NONE'| 'W_S'| 'UP_DOWN'| 'ARROWS';

enum CueDisplay {
NONE,
W_S,
UP_DOWN,
ARROWS
}

export default CueDisplay;
3 changes: 2 additions & 1 deletion js/common/view/RAPScreenView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import TickMarkView, { TickMarkViewType } from './TickMarkView.js';
import TickMarkViewRadioButtonGroup from './TickMarkViewRadioButtonGroup.js';
import RAPModel from '../model/RAPModel.js';
import Tandem from '../../../../tandem/js/Tandem.js';
import CueDisplay from './CueDisplay.js';

// constants
const LAYOUT_BOUNDS = ScreenView.DEFAULT_LAYOUT_BOUNDS;
Expand Down Expand Up @@ -181,7 +182,7 @@ class RAPScreenView extends ScreenView {
handColorProperty: options.leftHandColorProperty,
accessibleName: ratioAndProportionStrings.a11y.leftHand,
a11yDependencies: a11yDependencies,
bothHandsCueDisplay: 'W_S',
bothHandsCueDisplay: CueDisplay.W_S,
isRight: false, // this way we get a left hand

// Added to the antecedent for ease, but it applies to both RatioHalfs in the PDOM
Expand Down
18 changes: 13 additions & 5 deletions js/common/view/RAPTickMarkLabelsNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,28 @@ import PhetFont from '../../../../scenery-phet/js/PhetFont.js';
import Node from '../../../../scenery/js/nodes/Node.js';
import Text from '../../../../scenery/js/nodes/Text.js';
import ratioAndProportion from '../../ratioAndProportion.js';
import TickMarkView from './TickMarkView.js';
import TickMarkView, { TickMarkViewType } from './TickMarkView.js';
import Color from '../../../../scenery/js/util/Color.js';

const LABEL_X = 0;

class RAPTickMarkLabelsNode extends Node {

private totalHeight: number;
private heightOfText: number | null;
private tickMarkViewProperty: Property<TickMarkViewType>;
private tickMarkRangeProperty: Property<number>;
private colorProperty: Property<Color>;

/**
* @param {Property.<TickMarkView>} tickMarkViewProperty
* @param {Property.<number>} tickMarkRangeProperty
* @param {number} height
* @param {Property.<Color>} colorProperty
* @param {Object} [options]
*/
constructor( tickMarkViewProperty, tickMarkRangeProperty, height, colorProperty, options ) {
constructor( tickMarkViewProperty: Property<TickMarkViewType>, tickMarkRangeProperty: Property<number>, height: number,
colorProperty: Property<Color>, options?: any ) {

if ( options ) {
assert && assert( !options.hasOwnProperty( 'children' ), 'RAPTickMarkLabelsNode sets its own children' );
Expand Down Expand Up @@ -62,7 +70,7 @@ class RAPTickMarkLabelsNode extends Node {
/**
* @public
*/
layout( height ) {
layout( height:number ) {

this.totalHeight = height;
this.update( this.tickMarkRangeProperty.value, this.tickMarkViewProperty.value );
Expand All @@ -71,7 +79,7 @@ class RAPTickMarkLabelsNode extends Node {
/**
* @private
*/
update( tickMarkRange, tickMarkView ) {
update( tickMarkRange: number, tickMarkView: TickMarkViewType ) {

// subtract one to account for potential rounding errors. This helps guarantee that the last line is drawn.
const horizontalSpacing = ( this.totalHeight - 1 ) / tickMarkRange;
Expand All @@ -86,7 +94,7 @@ class RAPTickMarkLabelsNode extends Node {
* @param {boolean} showTickMarkUnits
* @param {number} horizontalSpacing
*/
updateUnitLabels( showTickMarkUnits, horizontalSpacing ) {
updateUnitLabels( showTickMarkUnits: boolean, horizontalSpacing: number ) {
this.children = [];

assert && assert( typeof horizontalSpacing === 'number', 'Unit Labels only supported for horizontal lines' );
Expand Down
47 changes: 26 additions & 21 deletions js/common/view/RatioHalf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Rectangle from '../../../../scenery/js/nodes/Rectangle.js';
import Tandem from '../../../../tandem/js/Tandem.js';
import ratioAndProportion from '../../ratioAndProportion.js';
import ratioAndProportionStrings from '../../ratioAndProportionStrings.js';
import RatioTerm, { RatioTermType } from '../model/RatioTerm.js';
import RatioTerm from '../model/RatioTerm.js';
import rapConstants from '../rapConstants.js';
import CueDisplay from './CueDisplay.js';
import RatioHalfTickMarksNode from './RatioHalfTickMarksNode.js';
Expand All @@ -48,12 +48,12 @@ const SNAP_TO_TICK_MARK_THRESHOLD = 0.1;
// total horizontal drag distance;
const X_MODEL_DRAG_DISTANCE = 1;
const INITIAL_X_VALUE = 0;
const getModelBoundsFromRange = range => new Bounds2( -1 * X_MODEL_DRAG_DISTANCE / 2, range.min, X_MODEL_DRAG_DISTANCE / 2, range.max );
const getModelBoundsFromRange = ( range: Range ) => new Bounds2( -1 * X_MODEL_DRAG_DISTANCE / 2, range.min, X_MODEL_DRAG_DISTANCE / 2, range.max );

const MIN_HAND_SCALE = 1.2;
const MAX_HAND_SCALE = 2.5;

function ratioHalfAccessibleNameBehavior( node, options, accessibleName, callbacksForOtherNodes ) {
function ratioHalfAccessibleNameBehavior( node: RatioHalf, options: any, accessibleName: string, callbacksForOtherNodes: { (): void }[] ) {
callbacksForOtherNodes.push( () => {
node.ratioHandNode.accessibleName = accessibleName;
} );
Expand All @@ -62,17 +62,22 @@ function ratioHalfAccessibleNameBehavior( node, options, accessibleName, callbac

const TOTAL_RANGE = rapConstants.TOTAL_RATIO_TERM_VALUE_RANGE;

type LayoutFunction = ( bounds: Bounds2, heightScalar: number ) => void;

class RatioHalf extends Rectangle {


public readonly framingRectangleHeight: number;
public framingRectangleHeight: number;
public readonly isBeingInteractedWithProperty: BooleanProperty;
private ratioLockedProperty: BooleanProperty;
private bothHandsDescriber: BothHandsDescriber;
private handPositionsDescriber: HandPositionsDescriber;
private tickMarkViewProperty: Property<TickMarkViewType>;
private ratioTerm: RatioTermType;
private ratioTerm: RatioTerm;
private ratioTupleProperty: Property<RAPRatioTuple>;
ratioHandNode: RatioHandNode;
private layoutRatioHalf: LayoutFunction;
private resetRatioHalf: () => void;

/**
* @param {Object} config
Expand Down Expand Up @@ -151,7 +156,7 @@ class RatioHalf extends Rectangle {
a11yDependencies: [],

// {CueDisplay}
bothHandsCueDisplay: 'UP_DOWN',
bothHandsCueDisplay: CueDisplay.UP_DOWN,

// phet-io
tandem: Tandem.REQUIRED,
Expand Down Expand Up @@ -197,9 +202,9 @@ class RatioHalf extends Rectangle {
bothHandsInteractedWith: boolean,
displayBothHands: boolean ) => {
return displayBothHands ? config.bothHandsCueDisplay :
keyboardFocused && !interactedWithKeyboard ? 'UP_DOWN' :
( interactedWithKeyboard || interactedWithMouse || bothHandsInteractedWith ) ? 'NONE' :
'ARROWS';
keyboardFocused && !interactedWithKeyboard ? CueDisplay.UP_DOWN :
( interactedWithKeyboard || interactedWithMouse || bothHandsInteractedWith ) ? CueDisplay.NONE :
CueDisplay.ARROWS;
} );


Expand All @@ -209,10 +214,10 @@ class RatioHalf extends Rectangle {
bidirectional: true,
reentrant: true,
valueType: 'number',
map: ratioTuple => ratioTuple.getForTerm( this.ratioTerm ),
inverseMap: term => this.ratioTerm === RatioTerm.ANTECEDENT ? this.ratioTupleProperty.value.withAntecedent( term ) :
this.ratioTerm === RatioTerm.CONSEQUENT ? this.ratioTupleProperty.value.withConsequent( term ) :
assert && assert( false, `unexpected ratioTerm ${this.ratioTerm}` )
map: ( ratioTuple: RAPRatioTuple ) => ratioTuple.getForTerm( this.ratioTerm ),
inverseMap: ( term: number ) => this.ratioTerm === RatioTerm.ANTECEDENT ? this.ratioTupleProperty.value.withAntecedent( term ) :
this.ratioTerm === RatioTerm.CONSEQUENT ? this.ratioTupleProperty.value.withConsequent( term ) :
assert && assert( false, `unexpected ratioTerm ${this.ratioTerm}` )
} );

// @private - The draggable element inside the Node framed with thick rectangles on the top and bottom.
Expand Down Expand Up @@ -244,7 +249,7 @@ class RatioHalf extends Rectangle {
config.bounds );

// Snap mouse/touch input to the nearest tick mark if close enough. This helps with reproducible precision
const getSnapToTickMarkValue = yValue => {
const getSnapToTickMarkValue = ( yValue: number ) => {
if ( TickMarkView.displayHorizontal( config.tickMarkViewProperty.value ) ) {
const tickMarkStep = 1 / config.tickMarkRangeProperty.value;

Expand All @@ -269,8 +274,8 @@ class RatioHalf extends Rectangle {
reentrant: true,
bidirectional: true,
valueType: Vector2,
inverseMap: vector2 => vector2.y,
map: number => {
inverseMap: ( vector2: Vector2 ) => vector2.y,
map: ( number: number ) => {

// initial case
if ( mappingInitialValue ) {
Expand All @@ -286,7 +291,7 @@ class RatioHalf extends Rectangle {
const dragBoundsProperty = new Property( new Bounds2( 0, 0, 1, 1 ) );

// When set to a value, the horizontal position will not be changed throughout the whole drag. Set to null when not dragging.
let startingX = null;
let startingX: null | number = null;

// transform and dragBounds set in layout code below
const dragListener = new DragListener( {
Expand Down Expand Up @@ -342,7 +347,7 @@ class RatioHalf extends Rectangle {
} );

// When the range changes, update the dragBounds of the drag listener
config.enabledRatioTermsRangeProperty.link( enabledRange => {
config.enabledRatioTermsRangeProperty.link( ( enabledRange: Range ) => {
const newBounds = getModelBoundsFromRange( enabledRange );

// offset the bounds to account for the ratioHandNode's size, since the center of the ratioHandNode is controlled by the drag bounds.
Expand Down Expand Up @@ -379,7 +384,7 @@ class RatioHalf extends Rectangle {
config.bounds.width, config.bounds.height - 2 * this.framingRectangleHeight,
config.colorProperty );

const updatePointer = position => {
const updatePointer = ( position: Vector2 ) => {
this.ratioHandNode.translation = modelViewTransform.modelToViewPosition( position );
};
positionProperty.link( updatePointer );
Expand Down Expand Up @@ -466,7 +471,7 @@ class RatioHalf extends Rectangle {
* @public
* @param {number} desiredBottom
*/
setBottomOfRatioHalf( desiredBottom ) {
setBottomOfRatioHalf( desiredBottom: number ) {

// `selfBounds` is used for the position of the Rectangle, since RatioHalf extends Rectangle
this.bottom = desiredBottom + ( this.bounds.bottom - this.localToParentBounds( this.selfBounds ).bottom );
Expand All @@ -477,7 +482,7 @@ class RatioHalf extends Rectangle {
* @param {Bounds2} bounds - the bounds of this RatioHalf, effects dimensions, dragBounds, and width of guiding rectangles
* @param {number} heightScalar - normalized between 0 and 1. When 1, it the ratio half will be the tallest it gets, at 0, the shortest
*/
layout( bounds, heightScalar ) {
layout( bounds: Bounds2, heightScalar: number ) {
assert && assert( heightScalar >= 0 && heightScalar <= 1, 'scalar should be between 0 and 1' );
this.layoutRatioHalf( bounds, heightScalar );
}
Expand Down
13 changes: 9 additions & 4 deletions js/common/view/RatioHalfTickMarksNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import Property from '../../../../axon/js/Property.js';
import GridNode from '../../../../griddle/js/GridNode.js';
import merge from '../../../../phet-core/js/merge.js';
import ratioAndProportion from '../../ratioAndProportion.js';
import TickMarkView from './TickMarkView.js';
import TickMarkView, { TickMarkViewType } from './TickMarkView.js';
import Color from '../../../../scenery/js/util/Color';

class RatioHalfTickMarksNode extends GridNode {

private tickMarkViewProperty: Property<TickMarkViewType>;
private tickMarkRangeProperty: Property<number>;

/**
* @param {Property.<TickMarkView>} tickMarkViewProperty
* @param {Property.<number>} tickMarkRangeProperty
Expand All @@ -23,7 +27,8 @@ class RatioHalfTickMarksNode extends GridNode {
* @param {Property.<Color>} colorProperty
* @param {Object} [options]
*/
constructor( tickMarkViewProperty, tickMarkRangeProperty, width, height, colorProperty, options ) {
constructor( tickMarkViewProperty: Property<TickMarkViewType>, tickMarkRangeProperty: Property<number>, width: number,
height: number, colorProperty: Property<Color>, options?: object ) {
options = merge( {

// initial line spacings
Expand All @@ -46,7 +51,7 @@ class RatioHalfTickMarksNode extends GridNode {
/**
* @public
*/
layout( width, height ) {
layout( width: number, height: number ) {
this.setGridWidth( width );
this.setGridHeight( height );
this.update( this.tickMarkRangeProperty.value, this.tickMarkViewProperty.value );
Expand All @@ -55,7 +60,7 @@ class RatioHalfTickMarksNode extends GridNode {
/**
* @private
*/
update( tickMarkRange, tickMarkView ) {
update( tickMarkRange: number, tickMarkView: TickMarkViewType ) {

// subtract one to account for potential rounding errors. This helps guarantee that the last line is drawn.
this.setLineSpacings( {
Expand Down
Loading

0 comments on commit 8999552

Please sign in to comment.