Skip to content

Commit

Permalink
better support for multi-input dragging and interrupting, #98
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Kauzmann <michael.kauzmann@colorado.edu>
  • Loading branch information
zepumph committed Mar 5, 2024
1 parent 00129e3 commit 056f682
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 2 additions & 0 deletions js/common/model/Mass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ export default abstract class Mass extends PhetioObject {
* Starts a physics model engine drag at the given 2d (x,y) model position.
*/
public startDrag( position: Vector2 ): void {
assert && assert( !this.userControlledProperty.value, 'cannot start a drag when already userControlled' );
this.userControlledProperty.value = true;
this.engine.addPointerConstraint( this.body, position );
}
Expand All @@ -587,6 +588,7 @@ export default abstract class Mass extends PhetioObject {
* Ends a physics model engine drag.
*/
public endDrag(): void {
assert && assert( this.userControlledProperty.value, 'cannot end a drag if not userControlled' );
this.engine.removePointerConstraint( this.body );
this.userControlledProperty.value = false;
}
Expand Down
6 changes: 5 additions & 1 deletion js/common/view/DensityBuoyancyScreenView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,11 @@ export default class DensityBuoyancyScreenView<Model extends DensityBuoyancyMode
const isTouch = !( pointer instanceof Mouse );
const mass = this.getMassUnderPointer( pointer, isTouch );

if ( mass && mass.canMove && !mass.userControlledProperty.value ) {
if ( mass && mass.canMove ) {

// Newer interactions take precedent, so clean up any old ones first. This also makes mouse/keyboard
// cross-interaction much simpler.
mass.interruptedEmitter.emit();

grabSoundPlayer.play();

Expand Down
24 changes: 17 additions & 7 deletions js/common/view/MassView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,27 @@ export default abstract class MassView extends THREE.Mesh {
tagName: 'div',
focusable: true
} );
const endKeyboardInteraction = () => {
keyboardDragListener.interrupt();
mass.interruptedEmitter.removeListener( endKeyboardInteraction );
this.focusablePath!.removeInputListener( blurListener );

mass.endDrag();
};

const blurListener = {
blur: endKeyboardInteraction
};

this.focusablePath.addInputListener( {
focus: () => {

// We want the newer interaction to take precedent, so tabbing to the item should interrupt the previous mouse drag (if applicable).
mass.userControlledProperty.value && mass.interruptedEmitter.emit();

mass.interruptedEmitter.addListener( endKeyboardInteraction );
this.focusablePath!.addInputListener( blurListener );
mass.startDrag( mass.matrix.translation );
},
blur: () => {
mass.endDrag();
}
} );

Expand All @@ -135,10 +149,6 @@ export default abstract class MassView extends THREE.Mesh {
tandem: Tandem.OPT_OUT
} );

mass.interruptedEmitter.addListener( () => {
keyboardDragListener.interrupt();
} );

this.focusablePath.addInputListener( keyboardDragListener );
}

Expand Down

0 comments on commit 056f682

Please sign in to comment.