Skip to content

Commit

Permalink
replace MovableDragHandler with DragListener, #183
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Nov 8, 2019
1 parent 02922cb commit 73d8e7e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 182 deletions.
10 changes: 2 additions & 8 deletions js/common/view/ToolboxPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,9 @@ define( require => {
} );

// When pressed, forwards dragging to the actual tracer Node
tracerIconNode.addInputListener( DragListener.createForwardingListener( function( event ) {

tracerIconNode.addInputListener( DragListener.createForwardingListener( event => {
tracer.isActiveProperty.set( true );

// coordinates empirically determined to shift tracer to mouse when pulled out of the toolbox
const initialViewPosition = tracerNode.globalToParentPoint( event.pointer.point ).plusXY( -180, 0 );
tracer.positionProperty.set( transformProperty.get().viewToModelPosition( initialViewPosition ) );
tracerNode.movableDragHandler.startDrag( event );

tracerNode.dragListener.press( event, tracerNode );
}, { allowTouchSnag: true } ) );

// tracer visibility has the opposite visibility of the tracerIcon
Expand Down
32 changes: 18 additions & 14 deletions js/common/view/TracerNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ define( require => {
const BooleanProperty = require( 'AXON/BooleanProperty' );
const Bounds2 = require( 'DOT/Bounds2' );
const Circle = require( 'SCENERY/nodes/Circle' );
const DragListener = require( 'SCENERY/listeners/DragListener' );
const HBox = require( 'SCENERY/nodes/HBox' );
const inherit = require( 'PHET_CORE/inherit' );
const MathSymbols = require( 'SCENERY_PHET/MathSymbols' );
const merge = require( 'PHET_CORE/merge' );
const MovableDragHandler = require( 'SCENERY_PHET/input/MovableDragHandler' );
const Node = require( 'SCENERY/nodes/Node' );
const Path = require( 'SCENERY/nodes/Path' );
const projectileMotion = require( 'PROJECTILE_MOTION/projectileMotion' );
Expand Down Expand Up @@ -77,6 +77,9 @@ define( require => {
const RIGHT_SIDE_PADDING = 6;
const READOUT_X_MARGIN = ProjectileMotionConstants.RIGHTSIDE_PANEL_OPTIONS.readoutXMargin;

// coordinates empirically determined to shift tracer to mouse when pulled out of the toolbox
const DRAG_OFFSET = new Vector2( -180, 0 );

/**
* @param {Score} tracer - model of the tracer tool
* @param {Property.<ModelViewTransform2>} transformProperty
Expand Down Expand Up @@ -146,17 +149,18 @@ define( require => {
{ fill: 'gray' }
);

const dragBoundsProperty = new Property( screenView.visibleBoundsProperty.get().shiftedX( dragBoundsShift ) );

// @public so events can be forwarded to it by ToolboxPanel
this.movableDragHandler = new MovableDragHandler( tracer.positionProperty, {
this.dragListener = new DragListener( {
locationProperty: tracer.positionProperty,
modelViewTransform: transformProperty.get(),
dragBounds: screenView.visibleBoundsProperty.get().shiftedX( dragBoundsShift ),
startDrag: function( event ) {
self.isUserControlledProperty.set( true );
},
endDrag: function() {
self.isUserControlledProperty.set( false );
},
tandem: options.tandem.createTandem( 'dragHandler' )
dragBoundsProperty: dragBoundsProperty,
offsetLocation: () => DRAG_OFFSET,
applyOffset: false, // ignore where the pointer pressed in relation to the TracerNode origin
start: () => self.isUserControlledProperty.set( true ),
end: () => self.isUserControlledProperty.set( false ),
tandem: options.tandem.createTandem( 'dragListener' )
} );

// label and values readouts
Expand Down Expand Up @@ -244,14 +248,14 @@ define( require => {

// Observe changes in the modelViewTransform and update/adjust positions accordingly
transformProperty.link( function( transform ) {
self.movableDragHandler.setModelViewTransform( transform );
self.movableDragHandler.setDragBounds( transform.viewToModelBounds( screenView.visibleBoundsProperty.get().shiftedX( dragBoundsShift ) ) );
self.dragListener.transform = transform;
dragBoundsProperty.value = transform.viewToModelBounds( screenView.visibleBoundsProperty.get().shiftedX( dragBoundsShift ) );
updatePosition( tracer.positionProperty.get() );
} );

// Observe changes in the visible bounds and update drag bounds and adjust positions accordingly
screenView.visibleBoundsProperty.link( function( bounds ) {
self.movableDragHandler.setDragBounds( transformProperty.get().viewToModelBounds( screenView.visibleBoundsProperty.get().shiftedX( dragBoundsShift ) ) );
dragBoundsProperty.value = transformProperty.get().viewToModelBounds( screenView.visibleBoundsProperty.get().shiftedX( dragBoundsShift ) );
updatePosition( tracer.positionProperty.get() );
} );

Expand All @@ -275,7 +279,7 @@ define( require => {
Node.call( this, options );

// When dragging, move the tracer tool
this.addInputListener( this.movableDragHandler );
this.addInputListener( this.dragListener );

// visibility of the tracer
tracer.isActiveProperty.link( function( active ) {
Expand Down
Loading

0 comments on commit 73d8e7e

Please sign in to comment.