Skip to content

Commit 3005419

Browse files
committed
Click Trampolines - support to trampoline pointer events into 'click' events
- This is needed to support Fullscreen on IE11 because IE only trusts 'click' events for this operation; click trampolines as a general solution, although they are only required in some "special" cases.
1 parent c37173a commit 3005419

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

src/input/Input.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,13 @@ Phaser.Input.prototype = {
374374
this.mspointer.start();
375375
this.mousePointer.active = true;
376376

377+
var _this = this;
378+
this._onClickTrampoline = function (event) {
379+
_this.onClickTrampoline(event);
380+
};
381+
382+
this.game.canvas.addEventListener('click', this._onClickTrampoline, false);
383+
377384
},
378385

379386
/**
@@ -390,6 +397,8 @@ Phaser.Input.prototype = {
390397

391398
this.moveCallbacks = [];
392399

400+
this.game.canvas.removeEventListener('click', this._onClickTrampoline);
401+
393402
},
394403

395404
/**
@@ -867,6 +876,21 @@ Phaser.Input.prototype = {
867876
}
868877

869878
return false;
879+
},
880+
881+
/**
882+
* Used for click trampolines. See {@link Phaser.Pointer.addClickTrampoline}.
883+
*
884+
* @method Phaser.Mouse#onMouseDown
885+
* @private
886+
* @param {MouseEvent} event - The native event from the browser. This gets stored in Mouse.event.
887+
*/
888+
onClickTrampoline: function () {
889+
890+
// It might not always be the active pointer, but this does work on
891+
// Desktop browsers (read: IE) with Mouse or MSPointer input.
892+
this.activePointer.processClickTrampolines();
893+
870894
}
871895

872896
};

src/input/Pointer.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,21 @@ Phaser.Pointer = function (game, id) {
249249
this.isMouse = true;
250250
}
251251

252+
/**
253+
* Click trampolines associated with this pointer. See `addClickTrampoline`.
254+
* @property {object[]|null} _clickTrampolines
255+
* @private
256+
*/
257+
this._clickTrampolines = null;
258+
259+
/**
260+
* When the Pointer has click trampolines the last target object is stored here
261+
* so it can be used to check for validity of the trampoline in a post-Up/'stop'.
262+
* @property {object} _trampolineTargetObject
263+
* @private
264+
*/
265+
this._trampolineTargetObject = null;
266+
252267
};
253268

254269
Phaser.Pointer.prototype = {
@@ -279,6 +294,8 @@ Phaser.Pointer.prototype = {
279294
this.isDown = true;
280295
this.isUp = false;
281296
this.dirty = false;
297+
this._clickTrampolines = null;
298+
this._trampolineTargetObject = null;
282299

283300
// Work out how long it has been since the last click
284301
this.msSinceLastClick = this.game.time.now - this.timeDown;
@@ -631,6 +648,10 @@ Phaser.Pointer.prototype = {
631648

632649
this.game.input.interactiveItems.callAll('_releasedHandler', this);
633650

651+
if (this._clickTrampolines)
652+
{
653+
this._trampolineTargetObject = this.targetObject;
654+
}
634655
this.targetObject = null;
635656

636657
return this;
@@ -669,6 +690,78 @@ Phaser.Pointer.prototype = {
669690

670691
},
671692

693+
/**
694+
* Add a click trampoline to this pointer.
695+
*
696+
* A click trampoline is a callback that is run on the DOM 'click' event; this is primarily
697+
* needed with certain browsers (ie. IE11) which restrict some actions like requestFullscreen
698+
* to the DOM 'click' event and reject it for 'pointer*'' and 'mouseup*'' events.
699+
*
700+
* This is used internally by the ScaleManager; click trampoline usage is uncommon.
701+
*
702+
* @method Phaser.Pointer#addClickTrampoline
703+
* @protected
704+
* @param {string} name - The name of the trampoline; must be unique among active trampolines in this pointer.
705+
* @param {function} callback - Callback to run/trampoline.
706+
* @param {object} callbackContext - Context of the callback.
707+
* @param {object[]|null} callbackArgs - Additional callback args, if any. Supplied as an array.
708+
*/
709+
addClickTrampoline: function (name, callback, callbackContext, callbackArgs) {
710+
711+
if (!this.isDown)
712+
{
713+
return;
714+
}
715+
716+
var trampolines = (this._clickTrampolines = this._clickTrampolines || []);
717+
718+
for (var i = 0; i < trampolines.length; i++)
719+
{
720+
if (trampolines[i].name === name)
721+
{
722+
trampolines.splice(i, 1);
723+
break;
724+
}
725+
}
726+
727+
trampolines.push({
728+
name: name,
729+
targetObject: this.targetObject,
730+
callback: callback,
731+
callbackContext: callbackContext,
732+
callbackArgs: callbackArgs
733+
});
734+
735+
},
736+
737+
/**
738+
* Fire all click trampolines for which the pointers are still refering to the registered object.
739+
* @method Phaser.Pointer#processClickTrampolines
740+
* @private
741+
*/
742+
processClickTrampolines: function () {
743+
744+
var trampolines = this._clickTrampolines;
745+
if (!trampolines)
746+
{
747+
return;
748+
}
749+
750+
for (var i = 0; i < trampolines.length; i++)
751+
{
752+
var trampoline = trampolines[i];
753+
754+
if (trampoline.targetObject === this._trampolineTargetObject)
755+
{
756+
trampoline.callback.apply(trampoline.callbackContext, trampoline.callbackArgs);
757+
}
758+
}
759+
760+
this._clickTrampolines = null;
761+
this._trampolineTargetObject = null;
762+
763+
},
764+
672765
/**
673766
* Resets the Pointer properties. Called by InputManager.reset when you perform a State change.
674767
* @method Phaser.Pointer#reset

0 commit comments

Comments
 (0)