Skip to content

Commit c74c1cb

Browse files
committed
Added setDefaultCursor method and custom css cursor handling.
1 parent da436b0 commit c74c1cb

1 file changed

Lines changed: 125 additions & 9 deletions

File tree

src/input/InputManager.js

Lines changed: 125 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,40 @@ var InputManager = new Class({
140140
*/
141141
this._hasMoveCallback = false;
142142

143-
// this._usingHandCursor = false;
144-
this.setHandCursor = false;
143+
/**
144+
* Is a custom cursor currently set? (desktop only)
145+
*
146+
* @name Phaser.Input.InputManager#_customCursor
147+
* @private
148+
* @type {string}
149+
* @since 3.10.0
150+
*/
151+
this._customCursor = '';
152+
153+
/**
154+
* Custom cursor tracking value.
155+
*
156+
* 0 - No change.
157+
* 1 - Set new cursor.
158+
* 2 - Reset cursor.
159+
*
160+
* @name Phaser.Input.InputManager#_setCursor
161+
* @private
162+
* @type {integer}
163+
* @since 3.10.0
164+
*/
165+
this._setCursor = 0;
166+
167+
/**
168+
* The default CSS cursor to be used when interacting with your game.
169+
*
170+
* See the `setDefaultCursor` method for more details.
171+
*
172+
* @name Phaser.Input.InputManager#defaultCursor
173+
* @type {string}
174+
* @since 3.10.0
175+
*/
176+
this.defaultCursor = '';
145177

146178
/**
147179
* A reference to the Mouse Manager class, if enabled via the `input.mouse` Game Config property.
@@ -319,6 +351,7 @@ var InputManager = new Class({
319351
this.events.emit('boot');
320352

321353
this.game.events.on('prestep', this.update, this);
354+
this.game.events.on('poststep', this.postUpdate, this);
322355
this.game.events.once('destroy', this.destroy, this);
323356
},
324357

@@ -377,10 +410,8 @@ var InputManager = new Class({
377410
{
378411
var i;
379412

380-
// Was the hand cursor set this frame?
381-
this.setHandCursor = false;
413+
this._setCursor = 0;
382414

383-
// TODO: Move to AFTER the events have all been processed, so the plugins are more current and not 1 frame behind?
384415
this.events.emit('update');
385416

386417
this.ignoreEvents = false;
@@ -452,10 +483,95 @@ var InputManager = new Class({
452483
}
453484
},
454485

455-
// useHandCursor: function ()
456-
// {
457-
// return (this._usingHandCursor && this._setHandCursor)
458-
// },
486+
/**
487+
* Internal post-update, called automatically by the Game step.
488+
*
489+
* @method Phaser.Input.InputManager#postUpdate
490+
* @private
491+
* @since 3.10.0
492+
*/
493+
postUpdate: function ()
494+
{
495+
if (this._setCursor === 1)
496+
{
497+
this.canvas.style.cursor = this._customCursor;
498+
}
499+
else if (this._setCursor === 2)
500+
{
501+
this.canvas.style.cursor = this.defaultCursor;
502+
}
503+
},
504+
505+
/**
506+
* Tells the Input system to set a custom cursor.
507+
*
508+
* This cursor will be the default cursor used when interacting with the game canvas.
509+
*
510+
* If an Interactive Object also sets a custom cursor, this is the cursor that is reset after its use.
511+
*
512+
* Any valid CSS cursor value is allowed, including paths to image files. Please read about the differences
513+
* between browsers when it comes to the file formats and sizes supported:
514+
*
515+
* https://developer.mozilla.org/en-US/docs/Web/CSS/cursor
516+
* https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_User_Interface/Using_URL_values_for_the_cursor_property
517+
*
518+
* It's up to you to pick a suitable cursor format that works across the range of browsers you need to support.
519+
*
520+
* @method Phaser.Input.InputManager#setCursor
521+
* @since 3.10.0
522+
*
523+
* @param {string} cursor - The CSS to be used when setting the default cursor.
524+
*/
525+
setDefaultCursor: function (cursor)
526+
{
527+
this.defaultCursor = cursor;
528+
529+
if (this.canvas.style.cursor !== cursor)
530+
{
531+
this.canvas.style.cursor = cursor;
532+
}
533+
},
534+
535+
/**
536+
* Called by the InputPlugin when processing over and out events.
537+
*
538+
* Tells the Input Manager to set a custom cursor during its postUpdate step.
539+
*
540+
* https://developer.mozilla.org/en-US/docs/Web/CSS/cursor
541+
*
542+
* @method Phaser.Input.InputManager#setCursor
543+
* @private
544+
* @since 3.10.0
545+
*
546+
* @param {Phaser.Input.InteractiveObject} interactiveObject - The Interactive Object that called this method.
547+
*/
548+
setCursor: function (interactiveObject)
549+
{
550+
if (interactiveObject.cursor)
551+
{
552+
this._setCursor = 1;
553+
this._customCursor = interactiveObject.cursor;
554+
}
555+
},
556+
557+
/**
558+
* Called by the InputPlugin when processing over and out events.
559+
*
560+
* Tells the Input Manager to clear the hand cursor, if set, during its postUpdate step.
561+
*
562+
* @method Phaser.Input.InputManager#resetCursor
563+
* @private
564+
* @since 3.10.0
565+
*
566+
* @param {Phaser.Input.InteractiveObject} interactiveObject - The Interactive Object that called this method.
567+
*/
568+
resetCursor: function (interactiveObject)
569+
{
570+
if (interactiveObject.cursor)
571+
{
572+
this._setCursor = 2;
573+
}
574+
},
459575

460576
// event.targetTouches = list of all touches on the TARGET ELEMENT (i.e. game dom element)
461577
// event.touches = list of all touches on the ENTIRE DOCUMENT, not just the target element

0 commit comments

Comments
 (0)