Skip to content

Commit

Permalink
Create Pointer.isTouchLike(), convert usages of instance Touch to use…
Browse files Browse the repository at this point in the history
… it, #1156
  • Loading branch information
zepumph authored and jonathanolson committed Feb 18, 2021
1 parent 3403380 commit bf90d50
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 18 deletions.
4 changes: 2 additions & 2 deletions js/input/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,7 @@ class Input {
this.dispatchEvent( eventTrail, 'up', pointer, event, true );

// touch pointers are transient, so fire exit/out to the trail afterwards
if ( pointer instanceof Touch ) {
if ( pointer.isTouchLike() ) {
this.exitEvents( pointer, event, eventTrail, 0, true );
}

Expand Down Expand Up @@ -1639,7 +1639,7 @@ class Input {
this.dispatchEvent( eventTrail, 'cancel', pointer, event, true );

// touch pointers are transient, so fire exit/out to the trail afterwards
if ( pointer instanceof Touch ) {
if ( pointer.isTouchLike() ) {
this.exitEvents( pointer, event, eventTrail, 0, true );
}

Expand Down
10 changes: 10 additions & 0 deletions js/input/Pen.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ class Pen extends Pointer {
toString() {
return 'Pen#' + this.id;
}

/**
* @override
* @public
*
* @returns {boolean}
*/
isTouchLike() {
return true;
}
}

scenery.register( 'Pen', Pen );
Expand Down
11 changes: 11 additions & 0 deletions js/input/Pointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ class Pointer {
return this.attachedProperty.value;
}

/**
* Some pointers are treated differently because they behave like a touch. This is not exclusive to `Touch and touch
* events though. See https://github.com/phetsims/scenery/issues/1156
* @public
*
* @returns {boolean}
*/
isTouchLike(){
return false;
}

/**
* Sets whether this pointer is down/pressed, or up.
* @public
Expand Down
9 changes: 4 additions & 5 deletions js/input/SimpleDragHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import Tandem from '../../../tandem/js/Tandem.js';
import scenery from '../scenery.js';
import Mouse from './Mouse.js';
import SceneryEvent from './SceneryEvent.js';
import Touch from './Touch.js';

class SimpleDragHandler extends PhetioObject {
/**
Expand Down Expand Up @@ -102,7 +101,7 @@ class SimpleDragHandler extends PhetioObject {
// a touchenter starts a drag that is IMMEDIATELY interrupted, the touchdown would start another drag. We record
// interruptions here so that we can prevent future enter/down events from the same touch pointer from triggering
// another startDrag.
this.lastInterruptedTouchPointer = null;
this.lastInterruptedTouchLikePointer = null;

// @private {boolean}
this._attach = options.attach;
Expand Down Expand Up @@ -371,8 +370,8 @@ class SimpleDragHandler extends PhetioObject {

this.interrupted = true;

if ( this.pointer instanceof Touch ) {
this.lastInterruptedTouchPointer = this.pointer;
if ( this.pointer.isTouchLike() ) {
this.lastInterruptedTouchLikePointer = this.pointer;
}

// We create a synthetic event here, as there is no available event here.
Expand Down Expand Up @@ -410,7 +409,7 @@ class SimpleDragHandler extends PhetioObject {
if ( !this.dragging &&
// Don't check pointer.dragging if we don't attach, see https://github.com/phetsims/scenery/issues/206
( !event.pointer.dragging || !this._attach ) &&
event.pointer !== this.lastInterruptedTouchPointer &&
event.pointer !== this.lastInterruptedTouchLikePointer &&
event.canStartPress() ) {
this.startDrag( event );
}
Expand Down
11 changes: 11 additions & 0 deletions js/input/Touch.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ class Touch extends Pointer {
toString() {
return 'Touch#' + this.id;
}


/**
* @override
* @public
*
* @returns {boolean}
*/
isTouchLike() {
return true;
}
}

scenery.register( 'Touch', Touch );
Expand Down
9 changes: 4 additions & 5 deletions js/listeners/DragListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ import EventType from '../../../tandem/js/EventType.js';
import PhetioObject from '../../../tandem/js/PhetioObject.js';
import Tandem from '../../../tandem/js/Tandem.js';
import SceneryEvent from '../input/SceneryEvent.js';
import Touch from '../input/Touch.js';
import scenery from '../scenery.js';
import TransformTracker from '../util/TransformTracker.js';
import PressListener from './PressListener.js';
Expand Down Expand Up @@ -231,7 +230,7 @@ class DragListener extends PressListener {
// a touchenter starts a drag that is IMMEDIATELY interrupted, the touchdown would start another drag. We record
// interruptions here so that we can prevent future enter/down events from the same touch pointer from triggering
// another startDrag.
this._lastInterruptedTouchPointer = null;
this._lastInterruptedTouchLikePointer = null;

// @private {Action} - emitted on drag. Used for triggering phet-io events to the data stream, see https://github.com/phetsims/scenery/issues/842
this._dragAction = new Action( event => {
Expand Down Expand Up @@ -752,8 +751,8 @@ class DragListener extends PressListener {
* This can be called manually, but can also be called through node.interruptSubtreeInput().
*/
interrupt() {
if ( this.pointer && this.pointer instanceof Touch ) {
this._lastInterruptedTouchPointer = this.pointer;
if ( this.pointer && this.pointer.isTouchLike() ) {
this._lastInterruptedTouchLikePointer = this.pointer;
}

super.interrupt();
Expand All @@ -768,7 +767,7 @@ class DragListener extends PressListener {
* @returns {boolean}
*/
canPress( event ) {
if ( event.pointer === this._lastInterruptedTouchPointer ) {
if ( event.pointer === this._lastInterruptedTouchLikePointer ) {
return false;
}

Expand Down
4 changes: 1 addition & 3 deletions js/nodes/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,6 @@ import ParallelDOM from '../accessibility/pdom/ParallelDOM.js';
import Instance from '../display/Instance.js';
import Renderer from '../display/Renderer.js';
import Mouse from '../input/Mouse.js';
import Pen from '../input/Pen.js';
import Touch from '../input/Touch.js';
import scenery from '../scenery.js';
import CanvasContextWrapper from '../util/CanvasContextWrapper.js';
import Features from '../util/Features.js';
Expand Down Expand Up @@ -1986,7 +1984,7 @@ class Node extends PhetioObject {
* @returns {Trail|null}
*/
trailUnderPointer( pointer ) {
return this.hitTest( pointer.point, pointer instanceof Mouse, pointer instanceof Touch || pointer instanceof Pen );
return this.hitTest( pointer.point, pointer instanceof Mouse, pointer.isTouchLike() );
}

/**
Expand Down
5 changes: 2 additions & 3 deletions js/overlays/PointerOverlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import Matrix3 from '../../../dot/js/Matrix3.js';
import PDOMPointer from '../input/PDOMPointer.js';
import Touch from '../input/Touch.js';
import scenery from '../scenery.js';
import svgns from '../util/svgns.js';
import Utils from '../util/Utils.js';
Expand Down Expand Up @@ -76,9 +75,9 @@ class PointerOverlay {
//Add a move listener to the pointer to update position when it has moved
const pointerRemoved = () => {

// For touches that get a touch up event, remove them. But when the mouse button is released, don't stop
// For touche-like events that get a touch up event, remove them. But when the mouse button is released, don't stop
// showing the mouse location
if ( pointer instanceof Touch ) {
if ( pointer.isTouchLike() ) {
this.pointerSVGContainer.removeChild( svg );
pointer.removeInputListener( moveListener );
}
Expand Down

0 comments on commit bf90d50

Please sign in to comment.