-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first pass at RatioInteractionListener for keyboard control of entire…
… ratio, #44
- Loading branch information
Showing
2 changed files
with
113 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2020, University of Colorado Boulder | ||
|
||
/** | ||
* Keyboard drag listener for moving both ratio halves at the same time | ||
* | ||
* @author Michael Kauzmann (PhET Interactive Simulations) | ||
*/ | ||
|
||
import BooleanProperty from '../../../../axon/js/BooleanProperty.js'; | ||
import KeyStateTracker from '../../../../scenery/js/accessibility/KeyStateTracker.js'; | ||
import designingProperties from '../../common/designingProperties.js'; | ||
import ratioAndProportion from '../../ratioAndProportion.js'; | ||
|
||
class RatioInteractionListener { | ||
|
||
/** | ||
* @param {Node} targetNode | ||
* @param {Property.<number>}leftValueProperty | ||
* @param {Property.<number>}rightValueProperty | ||
* @param {Range} valueRange | ||
*/ | ||
constructor( targetNode, leftValueProperty, rightValueProperty, valueRange, firstInteractionProperty ) { | ||
|
||
this.keyStateTracker = new KeyStateTracker(); | ||
this.valueRange = valueRange; | ||
this.targetNode = targetNode; | ||
this.firstInteractionProperty = firstInteractionProperty; | ||
|
||
this.isBeingInteractedWithProperty = new BooleanProperty( false ); | ||
|
||
this.keyStateTracker.keydownEmitter.addListener( event => { | ||
this.isBeingInteractedWithProperty.value = true; | ||
|
||
if ( event.key === 'ArrowDown' ) { | ||
this.updateValue( rightValueProperty, false ); | ||
} | ||
else if ( event.key === 'ArrowUp' ) { | ||
this.updateValue( rightValueProperty, true ); | ||
} | ||
else if ( event.key.toLowerCase() === 'w' ) { | ||
this.updateValue( leftValueProperty, true ); | ||
} | ||
else if ( event.key.toLowerCase() === 's' ) { | ||
this.updateValue( leftValueProperty, false ); | ||
} | ||
} ); | ||
|
||
this.keyStateTracker.keyupEmitter.addListener( () => { | ||
if ( !this.keyStateTracker.keysAreDown() ) { | ||
this.isBeingInteractedWithProperty.value = false; | ||
} | ||
} ); | ||
} | ||
|
||
/** | ||
* @private | ||
* @param property | ||
* @param increment | ||
*/ | ||
updateValue( property, increment ) { | ||
this.firstInteractionProperty.value = true; | ||
const value = 1 / designingProperties.gridBaseUnitProperty.value; | ||
const amount = this.keyStateTracker.shiftKeyDown ? value / 4 : value; | ||
property.value = this.valueRange.constrainValue( property.value + ( amount * ( increment ? 1 : -1 ) ) ); | ||
} | ||
|
||
/** | ||
* @public | ||
*/ | ||
blur() { | ||
this.isBeingInteractedWithProperty.value = false; | ||
} | ||
|
||
/** | ||
* @public | ||
* @param {SceneryEvent} sceneryEvent | ||
*/ | ||
keydown( sceneryEvent ) { | ||
|
||
// TODO: targetNode is only because there are currently children listeners on each ratio half that we don't want bubbling, https://github.com/phetsims/ratio-and-proportion/issues/44 | ||
sceneryEvent.currentTarget === this.targetNode && this.keyStateTracker.keydownUpdate( sceneryEvent.domEvent ); | ||
} | ||
|
||
/** | ||
* @public | ||
* @param {SceneryEvent} sceneryEvent | ||
*/ | ||
keyup( sceneryEvent ) { | ||
sceneryEvent.currentTarget === this.targetNode && this.keyStateTracker.keyupUpdate( sceneryEvent.domEvent ); | ||
} | ||
} | ||
|
||
ratioAndProportion.register( 'RatioInteractionListener', RatioInteractionListener ); | ||
export default RatioInteractionListener; |