Skip to content

Commit

Permalink
Adding keyboard drag listener to the BodyNode
Browse files Browse the repository at this point in the history
  • Loading branch information
AgustinVallejo committed Mar 3, 2023
1 parent 09a172b commit fababc9
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions js/view/BodyNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @author Agustín Vallejo
*/

import { Color, DragListener, Node, Rectangle, RectangleOptions, Text, TextOptions } from '../../../scenery/js/imports.js';
import { Color, DragListener, KeyboardDragListener, Node, Rectangle, RectangleOptions, Text, TextOptions } from '../../../scenery/js/imports.js';
import Utils from '../../../dot/js/Utils.js';
import Body from '../model/Body.js';
import ShadedSphereNode, { ShadedSphereNodeOptions } from '../../../scenery-phet/js/ShadedSphereNode.js';
Expand Down Expand Up @@ -84,7 +84,12 @@ export default class BodyNode extends ShadedSphereNode {
fill: 'white', // Not a colorProperty because it is not dynamic
maxWidth: SolarSystemCommonConstants.MAX_WIDTH,
font: new PhetFont( 16 )
}
},

tagName: 'div',
focusable: true,
innerContent: 'Body',
ariaRole: 'application'
}, providedOptions );

options.cursor = options.draggable ? 'pointer' : 'default';
Expand Down Expand Up @@ -133,13 +138,7 @@ export default class BodyNode extends ShadedSphereNode {
} );

if ( options.draggable ) {
const bodyDragListener = new DragListener( {
positionProperty: body.positionProperty,
canStartPress: () => !body.userControlledPositionProperty.value,
mapPosition: point => {
return options.mapPosition( point, this.radius );
},
transform: modelViewTransformProperty,
const startEnd = {
start: () => {
body.clearPath();
body.userControlledPositionProperty.value = true;
Expand All @@ -149,11 +148,35 @@ export default class BodyNode extends ShadedSphereNode {
body.userControlledPositionProperty.value = false;
this.releaseClip.play();
}
};

const bodyDragListener = new DragListener( {
positionProperty: body.positionProperty,
canStartPress: () => !body.userControlledPositionProperty.value,
mapPosition: point => {
return options.mapPosition( point, this.radius );
},
transform: modelViewTransformProperty,
...startEnd
} );
this.addInputListener( bodyDragListener );
this.disposeEmitter.addListener( () => {
bodyDragListener.dispose();
} );

const keyboardDragListener = new KeyboardDragListener(
{
positionProperty: body.positionProperty,
// dragBoundsProperty: dragBoundsProperty,
transform: modelViewTransformProperty.value,
dragDelta: 8,
shiftDragDelta: 2.5,
...startEnd
} );
modelViewTransformProperty.link( modelViewTransform => {
keyboardDragListener.transform = modelViewTransform;
} );
this.addInputListener( keyboardDragListener );
}

const velocityValueProperty = new DerivedProperty(
Expand Down

0 comments on commit fababc9

Please sign in to comment.