From b24de1e561e1b608d3419ecd6276915b76666d24 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Mon, 27 Jun 2016 14:51:25 +0100 Subject: [PATCH] Polygon.contains would only work with non-flattened Polygon objects. It now works with both flat and non-flat Polygons. Graphics objects enabled for input would fail to do anything if a Phaser Polygon was given to the Graphics object (which it was in nearly all cases), as it wouldn't detect input correctly with flattened polygons (thanks @symbiane #2591) --- README.md | 2 ++ src/geom/Polygon.js | 50 ++++++++++++++++++++++++++++++--------- src/input/InputHandler.js | 5 ++-- typescript/phaser.d.ts | 3 ++- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index a916b0394..96970fcb5 100644 --- a/README.md +++ b/README.md @@ -336,6 +336,8 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/ * Fixed issue in Group.align where the cell wouldn't increase if `rows` was great than -1 * Sound.volume was accidentally repeated twice in the source (thanks @LoneStranger #2569) * Animation.setFrame wouldn't work correctly if the `useLocalFrameIndex` argument was true, and the frame ID was a number (thanks @uboot #2571) +* Polygon.contains would only work with non-flattened Polygon objects. It now works with both flat and non-flat Polygons. +* Graphics objects enabled for input would fail to do anything if a Phaser Polygon was given to the Graphics object (which it was in nearly all cases), as it wouldn't detect input correctly with flattened polygons (thanks @symbiane #2591) ### Pixi Updates diff --git a/src/geom/Polygon.js b/src/geom/Polygon.js index 5b5ae0c1e..52032d0ed 100644 --- a/src/geom/Polygon.js +++ b/src/geom/Polygon.js @@ -44,6 +44,11 @@ Phaser.Polygon = function () { */ this.closed = true; + /** + * @property {boolean} flattened - Has this Polygon been flattened by a call to `Polygon.flatten` ? + */ + this.flattened = false; + /** * @property {number} type - The base object type. */ @@ -84,7 +89,9 @@ Phaser.Polygon.prototype = { }, /** - * Flattens this Polygon so the points are a sequence of numbers. Any Point objects found are removed and replaced with two numbers. + * Flattens this Polygon so the points are a sequence of numbers. + * Any Point objects found are removed and replaced with two numbers. + * Also sets the Polygon.flattened property to `true`. * * @method Phaser.Polygon#flatten * @return {Phaser.Polygon} This Polygon object @@ -93,6 +100,8 @@ Phaser.Polygon.prototype = { this._points = this.toNumberArray(); + this.flattened = true; + return this; }, @@ -134,20 +143,39 @@ Phaser.Polygon.prototype = { // Adapted from http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html by Jonas Raoni Soares Silva - var length = this._points.length; var inside = false; - for (var i = -1, j = length - 1; ++i < length; j = i) + if (this.flattened) { - var ix = this._points[i].x; - var iy = this._points[i].y; - - var jx = this._points[j].x; - var jy = this._points[j].y; - - if (((iy <= y && y < jy) || (jy <= y && y < iy)) && (x < (jx - ix) * (y - iy) / (jy - iy) + ix)) + for (var i = -2, j = this._points.length - 2; (i += 2) < this._points.length; j = i) { - inside = !inside; + var ix = this._points[i]; + var iy = this._points[i + 1]; + + var jx = this._points[j]; + var jy = this._points[j + 1]; + + if (((iy <= y && y < jy) || (jy <= y && y < iy)) && (x < (jx - ix) * (y - iy) / (jy - iy) + ix)) + { + inside = !inside; + } + } + + } + else + { + for (var i = -1, j = this._points.length - 1; ++i < this._points.length; j = i) + { + var ix = this._points[i].x; + var iy = this._points[i].y; + + var jx = this._points[j].x; + var jy = this._points[j].y; + + if (((iy <= y && y < jy) || (jy <= y && y < iy)) && (x < (jx - ix) * (y - iy) / (jy - iy) + ix)) + { + inside = !inside; + } } } diff --git a/src/input/InputHandler.js b/src/input/InputHandler.js index 91f9f9bc8..7ac311e99 100644 --- a/src/input/InputHandler.js +++ b/src/input/InputHandler.js @@ -283,9 +283,10 @@ Phaser.InputHandler.prototype = { /** * Starts the Input Handler running. This is called automatically when you enable input on a Sprite, or can be called directly if you need to set a specific priority. + * * @method Phaser.InputHandler#start - * @param {number} priority - Higher priority sprites take click priority over low-priority sprites when they are stacked on-top of each other. - * @param {boolean} useHandCursor - If true the Sprite will show the hand cursor on mouse-over (doesn't apply to mobile browsers) + * @param {number} [priority=0] - Higher priority sprites take click priority over low-priority sprites when they are stacked on-top of each other. + * @param {boolean} [useHandCursor=false] - If true the Sprite will show the hand cursor on mouse-over (doesn't apply to mobile browsers) * @return {Phaser.Sprite} The Sprite object to which the Input Handler is bound. */ start: function (priority, useHandCursor) { diff --git a/typescript/phaser.d.ts b/typescript/phaser.d.ts index 702d245e4..c00cd3dc2 100644 --- a/typescript/phaser.d.ts +++ b/typescript/phaser.d.ts @@ -1,7 +1,7 @@ /// /// -// Type definitions for Phaser 2.5.1 - 21st June 2016 +// Type definitions for Phaser 2.5.1 - 27th June 2016 // Project: https://github.com/photonstorm/phaser declare module "phaser" { @@ -4009,6 +4009,7 @@ declare module Phaser { constructor(...points: number[]); area: number; + flattened: boolean; points: number[] | Phaser.Point[]; type: number;