Skip to content

Commit

Permalink
use RatioTerm instead of strings, #457
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Apr 22, 2022
1 parent 1c52180 commit 16a4eb1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
18 changes: 18 additions & 0 deletions js/common/model/RAPRatioTuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class RAPRatioTuple {
return new RAPRatioTuple( this.antecedent, consequent );
}

withValueForTerm( value: number, ratioTerm: RatioTerm ): RAPRatioTuple {
return this.copy().setForTerm( value, ratioTerm );
}

plusAntecedent( antecedentDelta: number ): RAPRatioTuple {
return new RAPRatioTuple( this.antecedent + antecedentDelta, this.consequent );
}
Expand Down Expand Up @@ -87,6 +91,20 @@ class RAPRatioTuple {
}
}

setForTerm( value: number, ratioTerm: RatioTerm ): this {
switch( ratioTerm ) {
case RatioTerm.ANTECEDENT:
this.antecedent = value;
break;
case RatioTerm.CONSEQUENT:
this.consequent = value;
break;
default:
assert && assert( false, `unexpected ratioTerm ${ratioTerm}` );
}
return this;
}

copy(): RAPRatioTuple {
return new RAPRatioTuple( this.antecedent, this.consequent );
}
Expand Down
18 changes: 9 additions & 9 deletions js/common/view/BothHandsInteractionListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,25 @@ class BothHandsInteractionListener {

/**
* Consistently handle changing the ratio from increment/decrement
* @param tupleField - what field of the RAPRatioTuple are we changing
* @param ratioTerm - what field of the RAPRatioTuple are we changing
* @param inputMapper - see getKeyboardInputSnappingMapper
* @param increment - if the value is being incremented, as opposed to decremented.
*/
private onValueIncrementDecrement( tupleField: 'antecedent' | 'consequent', inputMapper: KeyboardInputMapper, increment: boolean ): void {
private onValueIncrementDecrement( ratioTerm: RatioTerm, inputMapper: KeyboardInputMapper, increment: boolean ): void {
this.isBeingInteractedWithProperty.value = true;
const currentValueFromTuple = this.ratioTupleProperty.value[ tupleField ];
const currentValueFromTuple = this.ratioTupleProperty.value.getForTerm( ratioTerm );

const changeAmount = globalKeyStateTracker.shiftKeyDown ? this.shiftKeyboardStep : this.keyboardStep;
const valueDelta = changeAmount * ( increment ? 1 : -1 );

// Because this interaction uses the keyboard, snap to the keyboard step to handle the case where the hands were
// previously moved via mouse/touch. See https://github.com/phetsims/ratio-and-proportion/issues/156
const newValue = inputMapper( currentValueFromTuple + valueDelta, currentValueFromTuple, globalKeyStateTracker.shiftKeyDown, this.inProportionProperty.value );
const newRatioTuple = tupleField === 'antecedent' ? this.ratioTupleProperty.value.withAntecedent( newValue ) : this.ratioTupleProperty.value.withConsequent( newValue );
const newRatioTuple = this.ratioTupleProperty.value.withValueForTerm( newValue, ratioTerm );

this.ratioTupleProperty.value = newRatioTuple.constrainFields( this.enabledRatioTermsRangeProperty.value );

this.tickMarkBumpSoundClip.onInteract( this.ratioTupleProperty.value[ tupleField ] );
this.tickMarkBumpSoundClip.onInteract( this.ratioTupleProperty.value.getForTerm( ratioTerm ) );

this.onInput();
}
Expand All @@ -160,19 +160,19 @@ class BothHandsInteractionListener {

if ( key === KeyboardUtils.KEY_DOWN_ARROW ) {
this.consequentInteractedWithProperty.value = true;
this.onValueIncrementDecrement( 'consequent', this.consequentMapKeyboardInput, false );
this.onValueIncrementDecrement( RatioTerm.CONSEQUENT, this.consequentMapKeyboardInput, false );
}
else if ( key === KeyboardUtils.KEY_UP_ARROW ) {
this.onValueIncrementDecrement( 'consequent', this.consequentMapKeyboardInput, true );
this.onValueIncrementDecrement( RatioTerm.CONSEQUENT, this.consequentMapKeyboardInput, true );
this.consequentInteractedWithProperty.value = true;
}
else if ( key === KeyboardUtils.KEY_W ) {
this.antecedentInteractedWithProperty.value = true;
this.onValueIncrementDecrement( 'antecedent', this.antecedentMapKeyboardInput, true );
this.onValueIncrementDecrement( RatioTerm.ANTECEDENT, this.antecedentMapKeyboardInput, true );
}
else if ( key === KeyboardUtils.KEY_S ) {
this.antecedentInteractedWithProperty.value = true;
this.onValueIncrementDecrement( 'antecedent', this.antecedentMapKeyboardInput, false );
this.onValueIncrementDecrement( RatioTerm.ANTECEDENT, this.antecedentMapKeyboardInput, false );
}
else {

Expand Down

0 comments on commit 16a4eb1

Please sign in to comment.