Skip to content

Commit 08c09b5

Browse files
committed
Added enableDebug and removeDebug from the Input Plugin, allowing you to create debug shapes to test where input hit areas are.
1 parent aceecf0 commit 08c09b5

3 files changed

Lines changed: 142 additions & 0 deletions

File tree

src/input/CreateInteractiveObject.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var CreateInteractiveObject = function (gameObject, hitArea, hitAreaCallback)
3737

3838
hitArea: hitArea,
3939
hitAreaCallback: hitAreaCallback,
40+
hitAreaDebug: null,
4041

4142
// Has the dev specified their own shape, or is this bound to the texture size?
4243
customHitArea: false,

src/input/InputPlugin.js

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var EllipseContains = require('../geom/ellipse/Contains');
1616
var Events = require('./events');
1717
var EventEmitter = require('eventemitter3');
1818
var GetFastValue = require('../utils/object/GetFastValue');
19+
var GEOM_CONST = require('../geom/const');
1920
var InputPluginCache = require('./InputPluginCache');
2021
var IsPlainObject = require('../utils/object/IsPlainObject');
2122
var PluginCache = require('../plugins/PluginCache');
@@ -2340,6 +2341,145 @@ var InputPlugin = new Class({
23402341
return this.setHitArea(gameObjects, shape, callback);
23412342
},
23422343

2344+
/**
2345+
* Creates an Input Debug Shape for the given Game Object.
2346+
*
2347+
* The Game Object must have _already_ been enabled for input prior to calling this method.
2348+
*
2349+
* This is intended to assist you during development and debugging.
2350+
*
2351+
* Debug Shapes can only be created for Game Objects that are using standard Phaser Geometry for input,
2352+
* including: Circle, Ellipse, Line, Polygon, Rectangle and Triangle.
2353+
*
2354+
* Game Objects that are using their automatic hit areas are using Rectangles by default, so will also work.
2355+
*
2356+
* The Debug Shape is created and added to the display list and is then kept in sync with the Game Object
2357+
* it is connected with. Should you need to modify it yourself, such as to hide it, you can access it via
2358+
* the Game Object property: `GameObject.input.hitAreaDebug`.
2359+
*
2360+
* Calling this method on a Game Object that already has a Debug Shape will first destroy the old shape,
2361+
* before creating a new one. If you wish to remove the Debug Shape entirely, you should call the
2362+
* method `InputPlugin.removeDebug`.
2363+
*
2364+
* Note that the debug shape will only show the outline of the input area. If the input test is using a
2365+
* pixel perfect check, for example, then this is not displayed. If you are using a custom shape, that
2366+
* doesn't extend one of the base Phaser Geometry objects, as your hit area, then this method will not
2367+
* work.
2368+
*
2369+
* @method Phaser.Input.InputPlugin#enableDebug
2370+
* @since 3.19.0
2371+
*
2372+
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object to create the input debug shape for.
2373+
* @param {number} [color=0x00ff00] - The outline color of the debug shape.
2374+
*
2375+
* @return {Phaser.Input.InputPlugin} This Input Plugin.
2376+
*/
2377+
enableDebug: function (gameObject, color)
2378+
{
2379+
if (color === undefined) { color = 0x00ff00; }
2380+
2381+
var input = gameObject.input;
2382+
2383+
if (!input || !input.hitArea)
2384+
{
2385+
return this;
2386+
}
2387+
2388+
var shape = input.hitArea;
2389+
var shapeType = shape.type;
2390+
var debug = input.hitAreaDebug;
2391+
var factory = this.systems.add;
2392+
var updateList = this.systems.updateList;
2393+
2394+
if (debug)
2395+
{
2396+
updateList.remove(debug);
2397+
2398+
debug.destroy();
2399+
2400+
debug = null;
2401+
}
2402+
2403+
switch (shapeType)
2404+
{
2405+
case GEOM_CONST.CIRCLE:
2406+
debug = factory.arc(0, 0, shape.radius);
2407+
break;
2408+
2409+
case GEOM_CONST.ELLIPSE:
2410+
debug = factory.ellipse(0, 0, shape.width, shape.height);
2411+
break;
2412+
2413+
case GEOM_CONST.LINE:
2414+
debug = factory.line(0, 0, shape.x1, shape.y1, shape.x2, shape.y2);
2415+
break;
2416+
2417+
case GEOM_CONST.POLYGON:
2418+
debug = factory.polygon(0, 0, shape.points);
2419+
break;
2420+
2421+
case GEOM_CONST.RECTANGLE:
2422+
debug = factory.rectangle(0, 0, shape.width, shape.height);
2423+
break;
2424+
2425+
case GEOM_CONST.TRIANGLE:
2426+
debug = factory.triangle(0, 0, shape.x1, shape.y1, shape.x2, shape.y2, shape.x3, shape.y3);
2427+
break;
2428+
}
2429+
2430+
if (debug)
2431+
{
2432+
debug.isFilled = false;
2433+
2434+
debug.preUpdate = function ()
2435+
{
2436+
debug.setStrokeStyle(1 / gameObject.scale, color);
2437+
2438+
debug.setDisplayOrigin(gameObject.displayOriginX, gameObject.displayOriginY);
2439+
debug.setRotation(gameObject.rotation);
2440+
debug.setScale(gameObject.scaleX, gameObject.scaleY);
2441+
debug.setPosition(gameObject.x, gameObject.y);
2442+
debug.setScrollFactor(gameObject.scrollFactorX, gameObject.scrollFactorY);
2443+
};
2444+
2445+
updateList.add(debug);
2446+
2447+
input.hitAreaDebug = debug;
2448+
}
2449+
2450+
return this;
2451+
},
2452+
2453+
/**
2454+
* Removes an Input Debug Shape from the given Game Object.
2455+
*
2456+
* The shape is destroyed immediately and the `hitAreaDebug` property is set to `null`.
2457+
*
2458+
* @method Phaser.Input.InputPlugin#removeDebug
2459+
* @since 3.19.0
2460+
*
2461+
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object to remove the input debug shape from.
2462+
*
2463+
* @return {Phaser.Input.InputPlugin} This Input Plugin.
2464+
*/
2465+
removeDebug: function (gameObject)
2466+
{
2467+
var input = gameObject.input;
2468+
2469+
if (input && input.hitAreaDebug)
2470+
{
2471+
var debug = input.hitAreaDebug;
2472+
2473+
this.systems.updateList.remove(debug);
2474+
2475+
debug.destroy();
2476+
2477+
input.hitAreaDebug = null;
2478+
}
2479+
2480+
return this;
2481+
},
2482+
23432483
/**
23442484
* Sets the Pointers to always poll.
23452485
*

src/input/typedefs/InteractiveObject.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* @property {Phaser.Cameras.Scene2D.Camera} camera - The most recent Camera to be tested against this Interactive Object.
1212
* @property {any} hitArea - The hit area for this Interactive Object. Typically a geometry shape, like a Rectangle or Circle.
1313
* @property {Phaser.Types.Input.HitAreaCallback} hitAreaCallback - The 'contains' check callback that the hit area shape will use for all hit tests.
14+
* @property {Phaser.GameObjects.Shape} hitAreaDebug - If this Interactive Object has been enabled for debug, via `InputPlugin.enableDebug` then this property holds its debug shape.
1415
* @property {boolean} customHitArea - Was the hitArea for this Interactive Object created based on texture size (false), or a custom shape? (true)
1516
* @property {number} localX - The x coordinate that the Pointer interacted with this object on, relative to the Game Object's top-left position.
1617
* @property {number} localY - The y coordinate that the Pointer interacted with this object on, relative to the Game Object's top-left position.

0 commit comments

Comments
 (0)