Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Oct 4, 2018
2 parents c36ed9f + 4628d4e commit 90654cb
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 226 deletions.
24 changes: 11 additions & 13 deletions js/listeners/DragListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,13 @@ define( function( require ) {
// passing release(), as the drag start hasn't been fully processed at that point.
end: null,

// {Property.<Boolean>|null} - An alias for isPressedListener that makes more sense for dragging.
isUserControlledProperty: null,

// {Tandem} - For instrumenting
tandem: Tandem.required,

phetioReadOnly: PhetioObject.DEFAULT_OPTIONS.phetioReadOnly // to support properly passing this to children, see https://github.com/phetsims/tandem/issues/60
// to support properly passing this to children, see https://github.com/phetsims/tandem/issues/60
phetioReadOnly: PhetioObject.DEFAULT_OPTIONS.phetioReadOnly
}, options );

// Initialize with the alias isUserControlledProperty => isPressedProperty
if ( options.isUserControlledProperty ) {
assert && assert( !options.isPressedProperty );
options.isPressedProperty = options.isUserControlledProperty;
}

assert && assert( typeof options.allowTouchSnag === 'boolean', 'allowTouchSnag should be a boolean' );
assert && assert( typeof options.applyOffset === 'boolean', 'applyOffset should be a boolean' );
assert && assert( typeof options.trackAncestors === 'boolean', 'trackAncestors should be a boolean' );
Expand All @@ -140,7 +132,6 @@ define( function( require ) {
assert && assert( options.offsetLocation === null || typeof options.offsetLocation === 'function', 'offsetLocation, if provided, should be a function' );
assert && assert( options.start === null || typeof options.start === 'function', 'start, if provided, should be a function' );
assert && assert( options.end === null || typeof options.end === 'function', 'end, if provided, should be a function' );
assert && assert( options.isUserControlledProperty === null || options.isUserControlledProperty instanceof Property, 'isUserControlledProperty, if provided, should be a Property' );
assert && assert( options.tandem instanceof Tandem, 'The provided tandem should be a Tandem' );

assert && assert(
Expand Down Expand Up @@ -224,9 +215,10 @@ define( function( require ) {
* @param {Event} event
* @param {Node} [targetNode] - If provided, will take the place of the targetNode for this call. Useful for
* forwarded presses.
* @param {function} [callback] - to be run at the end of the function, but only on success
* @returns {boolean} success - Returns whether the press was actually started
*/
press: function( event, targetNode ) {
press: function( event, targetNode, callback ) {
var self = this;
sceneryLog && sceneryLog.InputListener && sceneryLog.InputListener( 'DragListener press' );
sceneryLog && sceneryLog.InputListener && sceneryLog.push();
Expand All @@ -245,6 +237,8 @@ define( function( require ) {
// Notify after positioning and other changes
self._start && self._start( event, self );

callback && callback();

sceneryLog && sceneryLog.InputListener && sceneryLog.pop();
} );

Expand All @@ -260,8 +254,10 @@ define( function( require ) {
*
* This can be called from the outside to stop the drag without the pointer having actually fired any 'up'
* events. If the cancel/interrupt behavior is more preferable, call interrupt() on this listener instead.
*
* @param {function} [callback] - called at the end of the release
*/
release: function() {
release: function( callback ) {
var self = this;

sceneryLog && sceneryLog.InputListener && sceneryLog.InputListener( 'DragListener release' );
Expand All @@ -272,6 +268,8 @@ define( function( require ) {

// Notify after the rest of release is called in order to prevent it from triggering interrupt().
self._end && self._end( self );

callback && callback();
} );

sceneryLog && sceneryLog.InputListener && sceneryLog.pop();
Expand Down
32 changes: 18 additions & 14 deletions js/listeners/FireListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
/**
* A listener for common button usage, providing the fire() method/callback and helpful properties.
*
* TODO: name (because ButtonListener was taken). Can we rename the old ButtonListener and have this be ButtonListener?
*
* For example usage, see scenery/examples/input.html. Usually you can just pass a fire callback and things work.
*
* @author Jonathan Olson <[email protected]>
Expand All @@ -27,31 +25,31 @@ define( function( require ) {
*/
function FireListener( options ) {
options = _.extend( {
// {Function|null} - Called as fire() when the button is fired.
fire: null,
// {Function} - Called as fire() when the button is fired.
fire: _.noop,

// {boolean} - If true, the button will fire when the button is pressed. If false, the button will fire when the
// button is released while the pointer is over the button.
fireOnDown: false,

// {Tandem}
tandem: Tandem.optional
}, options );

assert && assert( options.fire === null || typeof options.fire === 'function',
'The fire callback, if provided, should be a function' );
assert && assert( typeof options.fire === 'function', 'The fire callback should be a function' );
assert && assert( typeof options.fireOnDown === 'boolean', 'fireOnDown should be a boolean' );

PressListener.call( this, options );

// @private - See options for documentation.
// @private {boolean}
this._fireOnDown = options.fireOnDown;

// @private - for PhET-iO events
// @private {Emitter}
this.firedEmitter = new Emitter( {
tandem: options.tandem.createTandem( 'firedEmitter' ),
phetioEventType: 'user'
} );
options.fire && this.firedEmitter.addListener( options.fire );
this.firedEmitter.addListener( options.fire );
}

scenery.register( 'FireListener', FireListener );
Expand Down Expand Up @@ -83,18 +81,21 @@ define( function( require ) {
* be used to determine whether this will actually start a press.
*
* @param {Event} event
* @param {Node} [targetNode] - If provided, will take the place of the targetNode for this call. Useful for
* forwarded presses.
* @param {function} [callback] - to be run at the end of the function, but only on success
* @returns {boolean} success - Returns whether the press was actually started
*/
press: function( event ) {
press: function( event, targetNode, callback ) {
var self = this;

var success = PressListener.prototype.press.call( this, event, undefined, function( success ) {
return PressListener.prototype.press.call( this, event, targetNode, function() {
// This function is only called on success
if ( self._fireOnDown ) {
self.fire( event );
}
callback && callback();
} );
return success;
},

/**
Expand All @@ -105,15 +106,18 @@ define( function( require ) {
* NOTE: This can be safely called externally in order to force a release of this button (no actual 'up' event is
* needed). If the cancel/interrupt behavior is more preferable (will not fire the button), then call interrupt()
* on this listener instead.
*
* @param {function} [callback] - called at the end of the release
*/
release: function() {
release: function( callback ) {
var self = this;
PressListener.prototype.release.call( this, function() {

PressListener.prototype.release.call( this, function() {
// Notify after the rest of release is called in order to prevent it from triggering interrupt().
if ( !self._fireOnDown && self.isHoveringProperty.value && !self.interrupted ) {
self.fire();
}
callback && callback();
} );
}
} );
Expand Down
Loading

0 comments on commit 90654cb

Please sign in to comment.