2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-10-04 22:48:16 +00:00
|
|
|
/**
|
2018-01-25 02:13:50 +00:00
|
|
|
* Visibility Handler hidden event.
|
|
|
|
*
|
|
|
|
* The document in which the Game is embedded has entered a hidden state.
|
|
|
|
*
|
|
|
|
* @event Phaser.Boot.VisibilityHandler#hidden
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Visibility Handler visible event.
|
|
|
|
*
|
|
|
|
* The document in which the Game is embedded has entered a visible state.
|
|
|
|
*
|
|
|
|
* @event Phaser.Boot.VisibilityHandler#visible
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Visibility Handler blur event.
|
|
|
|
*
|
|
|
|
* The window in which the Game is embedded has entered a blurred state.
|
|
|
|
*
|
|
|
|
* @event Phaser.Boot.VisibilityHandler#blur
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Visibility Handler focus event.
|
|
|
|
*
|
|
|
|
* The window in which the Game is embedded has entered a focused state.
|
|
|
|
*
|
|
|
|
* @event Phaser.Boot.VisibilityHandler#focus
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Visibility Handler is responsible for listening out for document level visibility change events.
|
|
|
|
* This includes `visibilitychange` if the browser supports it, and blur and focus events. It then uses
|
|
|
|
* the provided Event Emitter and fires the related events.
|
2017-10-04 22:48:16 +00:00
|
|
|
*
|
|
|
|
* @function Phaser.Boot.VisibilityHandler
|
2018-01-25 02:13:50 +00:00
|
|
|
* @fires Phaser.Boot.VisibilityHandler#hidden
|
|
|
|
* @fires Phaser.Boot.VisibilityHandler#visible
|
|
|
|
* @fires Phaser.Boot.VisibilityHandler#blur
|
|
|
|
* @fires Phaser.Boot.VisibilityHandler#focus
|
2017-10-04 22:48:16 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-05-18 16:37:45 +00:00
|
|
|
* @param {Phaser.Game} game - The Game instance this Visibility Handler is working on.
|
2017-10-04 22:48:16 +00:00
|
|
|
*/
|
2018-05-18 16:37:45 +00:00
|
|
|
var VisibilityHandler = function (game)
|
2017-05-09 00:24:46 +00:00
|
|
|
{
|
|
|
|
var hiddenVar;
|
2018-05-18 16:37:45 +00:00
|
|
|
var eventEmitter = game.events;
|
2017-05-09 00:24:46 +00:00
|
|
|
|
|
|
|
if (document.hidden !== undefined)
|
|
|
|
{
|
|
|
|
hiddenVar = 'visibilitychange';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var vendors = [ 'webkit', 'moz', 'ms' ];
|
|
|
|
|
|
|
|
vendors.forEach(function (prefix)
|
|
|
|
{
|
|
|
|
if (document[prefix + 'Hidden'] !== undefined)
|
|
|
|
{
|
|
|
|
document.hidden = function ()
|
|
|
|
{
|
|
|
|
return document[prefix + 'Hidden'];
|
|
|
|
};
|
|
|
|
|
|
|
|
hiddenVar = prefix + 'visibilitychange';
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var onChange = function (event)
|
|
|
|
{
|
|
|
|
if (document.hidden || event.type === 'pause')
|
|
|
|
{
|
2018-01-12 17:09:09 +00:00
|
|
|
eventEmitter.emit('hidden');
|
2017-05-09 00:24:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-12 17:09:09 +00:00
|
|
|
eventEmitter.emit('visible');
|
2017-05-09 00:24:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (hiddenVar)
|
|
|
|
{
|
|
|
|
document.addEventListener(hiddenVar, onChange, false);
|
|
|
|
}
|
2017-05-09 14:39:30 +00:00
|
|
|
|
|
|
|
window.onblur = function ()
|
|
|
|
{
|
2018-01-12 17:09:09 +00:00
|
|
|
eventEmitter.emit('blur');
|
2017-05-09 14:39:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
window.onfocus = function ()
|
|
|
|
{
|
2018-01-12 17:09:09 +00:00
|
|
|
eventEmitter.emit('focus');
|
2017-05-09 14:39:30 +00:00
|
|
|
};
|
2018-05-18 16:37:45 +00:00
|
|
|
|
|
|
|
// Automatically give the window focus unless config says otherwise
|
|
|
|
if (window.focus && game.config.autoFocus)
|
|
|
|
{
|
|
|
|
window.focus();
|
|
|
|
|
2018-05-18 16:43:12 +00:00
|
|
|
game.canvas.addEventListener('mousedown', function ()
|
|
|
|
{
|
2018-05-18 16:37:45 +00:00
|
|
|
window.focus();
|
|
|
|
}, { passive: true });
|
|
|
|
}
|
2017-05-09 00:24:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = VisibilityHandler;
|