Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed for unlocking audio with Pointer Events #306

Merged
merged 1 commit into from
Jul 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ If you code with [TypeScript](http://www.typescriptlang.org/) there are comprehe
* Documented how Phaser.Physics.P2.Body#addPolygon can mutate the `points` argument (#301).
* Documented how `cacheAsBitmap` and `generateTexture` can trim transparent pixels (#283).
* Deprecated Phaser.Device.isConsoleOpen. Now it always returns false.
* Moved addTouchLockCallback/removeTouchLockCallback from Phaser.Touch to Phaser.Input, it now also covers Phaser.MSPointer (#37)

### Bug Fixes

Expand All @@ -310,10 +311,11 @@ If you code with [TypeScript](http://www.typescriptlang.org/) there are comprehe
* Fixed movement of sprites with `fixedToCamera=true` when dragged by pointer (#297).
* Fixed creature relative anchor points to be absolute (#288).
* Fixed P2 Physics body not rotating shape (#258)
* Audio is now also unlocked for Chrome > 55, fixes audio not plaing in cross-origin iframe (#37)

### Thanks

@Aerolivier, @andrewjb123, @davvidbaker, @Formic, @fyyyyy, @Majirefy, @Plukers, @samid737, @samme, @tommitytom
@Aerolivier, @andrewjb123, @davvidbaker, @Formic, @fyyyyy, @Majirefy, @Plukers, @samid737, @samme, @tommitytom, @AleBles

## Version 2.8.3 - 21st July 2017

Expand Down
4 changes: 2 additions & 2 deletions src/gameobjects/Video.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ Phaser.Video.prototype = {

/**
* Starts this video playing.
*
*
* If the video is already playing, or has been queued to play with `changeSource` then this method just returns.
*
* @method Phaser.Video#play
Expand Down Expand Up @@ -1055,7 +1055,7 @@ Phaser.Video.prototype = {
*/
setTouchLock: function () {

this.game.input.touch.addTouchLockCallback(this.unlock, this);
this.game.input.addTouchLockCallback(this.unlock, this);
this.touchLocked = true;

},
Expand Down
104 changes: 81 additions & 23 deletions src/input/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ Phaser.Input = function (game) {
*/
this.recordLimit = 100;

/**
* An array of callbacks that will be fired every time a native touch start or touch end event (pointerdown or pointerup when using Pointer events) is received from the browser.
* This is used internally to handle audio and video unlocking on mobile devices.
* To add a callback to this array please use `Input.addTouchLockCallback`.
* @property {array} touchLockCallbacks
* @protected
*/
this.touchLockCallbacks = [];

/**
* @property {Phaser.Pointer} pointer1 - A Pointer object.
*/
Expand Down Expand Up @@ -215,66 +224,66 @@ Phaser.Input = function (game) {

/**
* The most recently active Pointer object.
*
*
* When you've limited max pointers to 1 this will accurately be either the first finger touched or mouse.
*
*
* @property {Phaser.Pointer} activePointer
*/
this.activePointer = null;

/**
* The mouse has its own unique Phaser.Pointer object which you can use if making a desktop specific game.
*
*
* @property {Pointer} mousePointer
*/
this.mousePointer = null;

/**
* The Mouse Input manager.
*
* You should not usually access this manager directly, but instead use Input.mousePointer or Input.activePointer
*
* You should not usually access this manager directly, but instead use Input.mousePointer or Input.activePointer
* which normalizes all the input values for you, regardless of browser.
*
*
* @property {Phaser.Mouse} mouse
*/
this.mouse = null;

/**
* The Keyboard Input manager.
*
*
* @property {Phaser.Keyboard} keyboard
*/
this.keyboard = null;

/**
* The Touch Input manager.
*
* You should not usually access this manager directly, but instead use Input.activePointer
*
* You should not usually access this manager directly, but instead use Input.activePointer
* which normalizes all the input values for you, regardless of browser.
*
*
* @property {Phaser.Touch} touch
*/
this.touch = null;

/**
* The MSPointer Input manager.
*
* You should not usually access this manager directly, but instead use Input.activePointer
*
* You should not usually access this manager directly, but instead use Input.activePointer
* which normalizes all the input values for you, regardless of browser.
*
*
* @property {Phaser.MSPointer} mspointer
*/
this.mspointer = null;

/**
* The Gamepad Input manager.
*
*
* @property {Phaser.Gamepad} gamepad
*/
this.gamepad = null;

/**
* If the Input Manager has been reset locked then all calls made to InputManager.reset,
* If the Input Manager has been reset locked then all calls made to InputManager.reset,
* such as from a State change, are ignored.
* @property {boolean} resetLocked
* @default
Expand Down Expand Up @@ -486,13 +495,13 @@ Phaser.Input.prototype = {
* selected then you can use this callback to do so.
*
* The callback will be sent 3 parameters:
*
*
* 1) A reference to the Phaser.Pointer object that is processing the Items.
* 2) An array containing all potential interactive candidates. This is an array of `InputHandler` objects, not Sprites.
* 3) The current 'favorite' candidate, based on its priorityID and position in the display list.
*
* Your callback MUST return one of the candidates sent to it.
*
*
* @method Phaser.Input#setInteractiveCandidateHandler
* @param {function} callback - The callback that will be called each time `Pointer.processInteractiveObjects` is called. Set to `null` to disable.
* @param {object} context - The context in which the callback will be called.
Expand All @@ -508,17 +517,17 @@ Phaser.Input.prototype = {
* Adds a callback that is fired every time the activePointer receives a DOM move event such as a mousemove or touchmove.
*
* The callback will be sent 4 parameters:
*
*
* A reference to the Phaser.Pointer object that moved,
* The x position of the pointer,
* The y position,
* A boolean indicating if the movement was the result of a 'click' event (such as a mouse click or touch down).
*
*
* It will be called every time the activePointer moves, which in a multi-touch game can be a lot of times, so this is best
* to only use if you've limited input to a single pointer (i.e. mouse or touch).
*
*
* The callback is added to the Phaser.Input.moveCallbacks array and should be removed with Phaser.Input.deleteMoveCallback.
*
*
* @method Phaser.Input#addMoveCallback
* @param {function} callback - The callback that will be called each time the activePointer receives a DOM move event.
* @param {object} context - The context in which the callback will be called.
Expand All @@ -529,9 +538,58 @@ Phaser.Input.prototype = {

},


/**
* Adds a callback that is fired when a browser touchstart or touchend event is received
* (pointerdown or pointerup when using pointer events).
*
* This is used internally to handle audio and video unlocking on mobile devices.
*
* If the callback returns 'true' then the callback is automatically deleted once invoked.
*
* The callback is added to the Phaser.Input.touchLockCallbacks array and should be removed with Phaser.Input.removeTouchLockCallback.
*
* @method Phaser.Touch#addTouchLockCallback
* @param {function} callback - The callback that will be called when a touchstart event is received.
* @param {object} context - The context in which the callback will be called.
* @param {boolean} [onEnd=false] - Will the callback fire on a touchstart (default) or touchend event?
*/
addTouchLockCallback: function (callback, context, onEnd) {

if (onEnd === undefined) { onEnd = false; }

this.touchLockCallbacks.push({ callback: callback, context: context, onEnd: onEnd });

},

/**
* Removes the callback at the defined index from the Phaser.Input.touchLockCallbacks array
*
* @method Phaser.Input#removeTouchLockCallback
* @param {function} callback - The callback to be removed.
* @param {object} context - The context in which the callback exists.
* @return {boolean} True if the callback was deleted, otherwise false.
*/
removeTouchLockCallback: function (callback, context) {

var i = this.touchLockCallbacks.length;

while (i--)
{
if (this.touchLockCallbacks[i].callback === callback && this.touchLockCallbacks[i].context === context)
{
this.touchLockCallbacks.splice(i, 1);
return true;
}
}

return false;

},

/**
* Removes the callback from the Phaser.Input.moveCallbacks array.
*
*
* @method Phaser.Input#deleteMoveCallback
* @param {function} callback - The callback to be removed.
* @param {object} context - The context in which the callback exists.
Expand Down Expand Up @@ -579,7 +637,7 @@ Phaser.Input.prototype = {

/**
* Updates the Input Manager. Called by the core Game loop.
*
*
* @method Phaser.Input#update
* @protected
*/
Expand Down
30 changes: 27 additions & 3 deletions src/input/MSPointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* It will work only in Internet Explorer 10+ and Windows Store or Windows Phone 8 apps using JavaScript.
* http://msdn.microsoft.com/en-us/library/ie/hh673557(v=vs.85).aspx
*
* You should not normally access this class directly, but instead use a Phaser.Pointer object which
* You should not normally access this class directly, but instead use a Phaser.Pointer object which
* normalises all game input for you including accurate button handling.
*
* Please note that at the current time of writing Phaser does not yet support chorded button interactions:
Expand Down Expand Up @@ -192,12 +192,24 @@ Phaser.MSPointer.prototype = {

/**
* The function that handles the PointerDown event.
*
*
* @method Phaser.MSPointer#onPointerDown
* @param {PointerEvent} event - The native DOM event.
*/
onPointerDown: function (event) {

var i = this.game.input.touchLockCallbacks.length;

while (i--)
{
var cb = this.game.input.touchLockCallbacks[i];

if (!cb.onEnd && cb.callback.call(cb.context, this, event))
{
this.game.input.touchLockCallbacks.splice(i, 1);
}
}

this.event = event;

if (this.capture)
Expand Down Expand Up @@ -272,6 +284,18 @@ Phaser.MSPointer.prototype = {
*/
onPointerUp: function (event) {

var i = this.game.input.touchLockCallbacks.length;

while (i--)
{
var cb = this.game.input.touchLockCallbacks[i];

if (cb.onEnd && cb.callback.call(cb.context, this, event))
{
this.game.input.touchLockCallbacks.splice(i, 1);
}
}

this.event = event;

if (this.capture)
Expand Down Expand Up @@ -304,7 +328,7 @@ Phaser.MSPointer.prototype = {

/**
* The internal method that handles the mouse up event from the window.
*
*
* @method Phaser.MSPointer#onPointerUpGlobal
* @param {PointerEvent} event - The native event from the browser. This gets stored in MSPointer.event.
*/
Expand Down
Loading