Skip to content

Commit

Permalink
Only create translation listeners for the Prisms screen, see #397
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Feb 10, 2021
1 parent 24c6dfc commit 9f31c2b
Showing 1 changed file with 69 additions and 69 deletions.
138 changes: 69 additions & 69 deletions js/common/view/LaserNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,58 +100,77 @@ class LaserNode extends Node {
// add the drag region for translating the laser
let start;

const translationListener = new SimpleDragHandler( {
start: event => {
start = this.globalToParentPoint( event.pointer.point );
showTranslationDragHandlesProperty.value = true;
},
drag: event => {

const laserNodeDragBounds = dragBoundsProperty.value.erodedXY( lightImageHeight / 2, lightImageHeight / 2 );
const laserDragBoundsInModelValues = modelViewTransform.viewToModelBounds( laserNodeDragBounds );

const endDrag = this.globalToParentPoint( event.pointer.point );
const deltaX = modelViewTransform.viewToModelDeltaX( endDrag.x - start.x );
const deltaY = modelViewTransform.viewToModelDeltaY( endDrag.y - start.y );

// position of final emission point with out constraining to bounds
emissionPointEndPosition.setXY( laser.emissionPointProperty.value.x + deltaX, laser.emissionPointProperty.value.y + deltaY );

// position of final emission point with constraining to bounds
const emissionPointEndPositionInBounds = laserDragBoundsInModelValues.closestPointTo( emissionPointEndPosition );

const translateX = emissionPointEndPositionInBounds.x - laser.emissionPointProperty.value.x;
const translateY = emissionPointEndPositionInBounds.y - laser.emissionPointProperty.value.y;
laser.translate( translateX, translateY );

// Store the position of caught point after translating. Can be obtained by adding distance between emission
// point and drag point (end - emissionPointEndPosition) to emission point (emissionPointEndPositionInBounds)
// after translating.
const boundsDx = emissionPointEndPositionInBounds.x - emissionPointEndPosition.x;
const boundsDY = emissionPointEndPositionInBounds.y - emissionPointEndPosition.y;
endDrag.x = endDrag.x + modelViewTransform.modelToViewDeltaX( boundsDx );
endDrag.y = endDrag.y + modelViewTransform.modelToViewDeltaY( boundsDY );
if ( translationTarget ) {

start = endDrag;
showTranslationDragHandlesProperty.value = true;
},
end: () => {
showTranslationDragHandlesProperty.value = false;
occlusionHandler( this );
}
} );
translationTarget && translationTarget.addInputListener( translationListener );
const translationListener = new SimpleDragHandler( {
start: event => {
start = this.globalToParentPoint( event.pointer.point );
showTranslationDragHandlesProperty.value = true;
},
drag: event => {

const laserNodeDragBounds = dragBoundsProperty.value.erodedXY( lightImageHeight / 2, lightImageHeight / 2 );
const laserDragBoundsInModelValues = modelViewTransform.viewToModelBounds( laserNodeDragBounds );

const endDrag = this.globalToParentPoint( event.pointer.point );
const deltaX = modelViewTransform.viewToModelDeltaX( endDrag.x - start.x );
const deltaY = modelViewTransform.viewToModelDeltaY( endDrag.y - start.y );

// position of final emission point with out constraining to bounds
emissionPointEndPosition.setXY( laser.emissionPointProperty.value.x + deltaX, laser.emissionPointProperty.value.y + deltaY );

// position of final emission point with constraining to bounds
const emissionPointEndPositionInBounds = laserDragBoundsInModelValues.closestPointTo( emissionPointEndPosition );

const translateX = emissionPointEndPositionInBounds.x - laser.emissionPointProperty.value.x;
const translateY = emissionPointEndPositionInBounds.y - laser.emissionPointProperty.value.y;
laser.translate( translateX, translateY );

// Store the position of caught point after translating. Can be obtained by adding distance between emission
// point and drag point (end - emissionPointEndPosition) to emission point (emissionPointEndPositionInBounds)
// after translating.
const boundsDx = emissionPointEndPositionInBounds.x - emissionPointEndPosition.x;
const boundsDY = emissionPointEndPositionInBounds.y - emissionPointEndPosition.y;
endDrag.x = endDrag.x + modelViewTransform.modelToViewDeltaX( boundsDx );
endDrag.y = endDrag.y + modelViewTransform.modelToViewDeltaY( boundsDY );

start = endDrag;
showTranslationDragHandlesProperty.value = true;
},
end: () => {
showTranslationDragHandlesProperty.value = false;
occlusionHandler( this );
}
} );
translationTarget.addInputListener( translationListener );

// Listeners to enable/disable the translation dragHandles
const translationOverListener = {
enter: () => {
showTranslationDragHandlesProperty.value = !showRotationDragHandlesProperty.value;
},
exit: () => {
showTranslationDragHandlesProperty.value = false;
}
};
translationTarget.addInputListener( translationOverListener );

// Listeners to enable/disable the translation dragHandles
const translationOverListener = {
enter: () => {
showTranslationDragHandlesProperty.value = !showRotationDragHandlesProperty.value;
},
exit: () => {
showTranslationDragHandlesProperty.value = false;
}
};
translationTarget && translationTarget.addInputListener( translationOverListener );
const isTranslationEnabledProperty = new BooleanProperty( true, {
tandem: options.tandem.createTandem( 'isTranslationEnabledProperty' ),
phetioDocumentation: 'This Property determines whether the laser can be translated, in the "Prisms" screen only. ' +
'A value of false means the laser cannot be translated, though it may still be rotatable.'
} );
isTranslationEnabledProperty.lazyLink( isEnabled => {
if ( isEnabled ) {
translationTarget.addInputListener( translationListener );
translationTarget.addInputListener( translationOverListener );
}
else {
translationTarget.removeInputListener( translationListener );
translationTarget.removeInputListener( translationOverListener );
}
} );
}

const rotationListener = new SimpleDragHandler( {
start: () => {
Expand Down Expand Up @@ -219,25 +238,6 @@ class LaserNode extends Node {
}
} );

// Only instrument for the Prisms screen where the laser can be translated.
if ( translationTarget ) {
const isTranslationEnabledProperty = new BooleanProperty( true, {
tandem: options.tandem.createTandem( 'isTranslationEnabledProperty' ),
phetioDocumentation: 'This Property determines whether the laser can be translated, in the "Prisms" screen only. ' +
'A value of false means the laser cannot be translated, though it may still be rotatable.'
} );
isTranslationEnabledProperty.lazyLink( isEnabled => {
if ( isEnabled ) {
translationTarget && translationTarget.addInputListener( translationListener );
translationTarget && translationTarget.addInputListener( translationOverListener );
}
else {
translationTarget && translationTarget.removeInputListener( translationListener );
translationTarget && translationTarget.removeInputListener( translationOverListener );
}
} );
}

// update the laser position
laser.emissionPointProperty.link( newEmissionPoint => {
const emissionPointX = modelViewTransform.modelToViewX( newEmissionPoint.x );
Expand Down

0 comments on commit 9f31c2b

Please sign in to comment.