diff --git a/src/core/math/shapes/RoundedRectangle.js b/src/core/math/shapes/RoundedRectangle.js index 6b68f42..5007b78 100644 --- a/src/core/math/shapes/RoundedRectangle.js +++ b/src/core/math/shapes/RoundedRectangle.js @@ -82,12 +82,38 @@ { return false; } - if (x >= this.x && x <= this.x + this.width) { if (y >= this.y && y <= this.y + this.height) { - return true; + if ((y >= this.y + this.radius && y <= this.y + this.height - this.radius) + || (x >= this.x + this.radius && x <= this.x + this.width - this.radius)) + { + return true; + } + let dx = x - (this.x + this.radius); + let dy = y - (this.y + this.radius); + const radius2 = this.radius * this.radius; + + if ((dx * dx) + (dy * dy) <= radius2) + { + return true; + } + dx = x - (this.x + this.width - this.radius); + if ((dx * dx) + (dy * dy) <= radius2) + { + return true; + } + dy = y - (this.y + this.height - this.radius); + if ((dx * dx) + (dy * dy) <= radius2) + { + return true; + } + dx = x - (this.x + this.radius); + if ((dx * dx) + (dy * dy) <= radius2) + { + return true; + } } } diff --git a/src/core/math/shapes/RoundedRectangle.js b/src/core/math/shapes/RoundedRectangle.js index 6b68f42..5007b78 100644 --- a/src/core/math/shapes/RoundedRectangle.js +++ b/src/core/math/shapes/RoundedRectangle.js @@ -82,12 +82,38 @@ { return false; } - if (x >= this.x && x <= this.x + this.width) { if (y >= this.y && y <= this.y + this.height) { - return true; + if ((y >= this.y + this.radius && y <= this.y + this.height - this.radius) + || (x >= this.x + this.radius && x <= this.x + this.width - this.radius)) + { + return true; + } + let dx = x - (this.x + this.radius); + let dy = y - (this.y + this.radius); + const radius2 = this.radius * this.radius; + + if ((dx * dx) + (dy * dy) <= radius2) + { + return true; + } + dx = x - (this.x + this.width - this.radius); + if ((dx * dx) + (dy * dy) <= radius2) + { + return true; + } + dy = y - (this.y + this.height - this.radius); + if ((dx * dx) + (dy * dy) <= radius2) + { + return true; + } + dx = x - (this.x + this.radius); + if ((dx * dx) + (dy * dy) <= radius2) + { + return true; + } } } diff --git a/test/core/RoundedRectangle.js b/test/core/RoundedRectangle.js new file mode 100644 index 0000000..cb0bc16 --- /dev/null +++ b/test/core/RoundedRectangle.js @@ -0,0 +1,62 @@ +'use strict'; + +describe('PIXI.RoundedRectangle', function () +{ + it('should create a new rounded rectangle', function () + { + const rrect = new PIXI.RoundedRectangle(5, 5, 1, 1); + + expect(rrect.x).to.equal(5); + expect(rrect.y).to.equal(5); + expect(rrect.width).to.equal(1); + expect(rrect.height).to.equal(1); + expect(rrect.radius).to.equal(20); + }); + + it('should clone a new rounded rectangle', function () + { + const rrect1 = new PIXI.RoundedRectangle(0, 0, 100, 100, 40); + + expect(rrect1.x).to.equal(0); + expect(rrect1.y).to.equal(0); + expect(rrect1.width).to.equal(100); + expect(rrect1.height).to.equal(100); + expect(rrect1.radius).to.equal(40); + + const rrect2 = rrect1.clone(); + + expect(rrect2.x).to.equal(0); + expect(rrect2.y).to.equal(0); + expect(rrect2.width).to.equal(100); + expect(rrect2.height).to.equal(100); + expect(rrect2.radius).to.equal(40); + expect(rrect1).to.not.equal(rrect2); + }); + + it('should check if point is within rounded rectangle', function () + { + const rrect1 = new PIXI.RoundedRectangle(0, 0, 200, 200, 50); + + expect(rrect1.contains(50, 50)).to.be.true; + expect(rrect1.contains(5, 100)).to.be.true; + expect(rrect1.contains(100, 5)).to.be.true; + expect(rrect1.contains(195, 100)).to.be.true; + expect(rrect1.contains(100, 195)).to.be.true; + expect(rrect1.contains(20, 20)).to.be.true; + expect(rrect1.contains(180, 20)).to.be.true; + expect(rrect1.contains(180, 180)).to.be.true; + expect(rrect1.contains(20, 180)).to.be.true; + expect(rrect1.contains(10, 10)).to.be.false; + expect(rrect1.contains(190, 10)).to.be.false; + expect(rrect1.contains(190, 190)).to.be.false; + expect(rrect1.contains(10, 190)).to.be.false; + + const rrect2 = new PIXI.RoundedRectangle(0, 0, 10, 0, 1); + + expect(rrect2.contains(0, 0)).to.be.false; + + const rrect3 = new PIXI.RoundedRectangle(0, 0, 0, 10, 1); + + expect(rrect3.contains(0, 0)).to.be.false; + }); +}); diff --git a/src/core/math/shapes/RoundedRectangle.js b/src/core/math/shapes/RoundedRectangle.js index 6b68f42..5007b78 100644 --- a/src/core/math/shapes/RoundedRectangle.js +++ b/src/core/math/shapes/RoundedRectangle.js @@ -82,12 +82,38 @@ { return false; } - if (x >= this.x && x <= this.x + this.width) { if (y >= this.y && y <= this.y + this.height) { - return true; + if ((y >= this.y + this.radius && y <= this.y + this.height - this.radius) + || (x >= this.x + this.radius && x <= this.x + this.width - this.radius)) + { + return true; + } + let dx = x - (this.x + this.radius); + let dy = y - (this.y + this.radius); + const radius2 = this.radius * this.radius; + + if ((dx * dx) + (dy * dy) <= radius2) + { + return true; + } + dx = x - (this.x + this.width - this.radius); + if ((dx * dx) + (dy * dy) <= radius2) + { + return true; + } + dy = y - (this.y + this.height - this.radius); + if ((dx * dx) + (dy * dy) <= radius2) + { + return true; + } + dx = x - (this.x + this.radius); + if ((dx * dx) + (dy * dy) <= radius2) + { + return true; + } } } diff --git a/test/core/RoundedRectangle.js b/test/core/RoundedRectangle.js new file mode 100644 index 0000000..cb0bc16 --- /dev/null +++ b/test/core/RoundedRectangle.js @@ -0,0 +1,62 @@ +'use strict'; + +describe('PIXI.RoundedRectangle', function () +{ + it('should create a new rounded rectangle', function () + { + const rrect = new PIXI.RoundedRectangle(5, 5, 1, 1); + + expect(rrect.x).to.equal(5); + expect(rrect.y).to.equal(5); + expect(rrect.width).to.equal(1); + expect(rrect.height).to.equal(1); + expect(rrect.radius).to.equal(20); + }); + + it('should clone a new rounded rectangle', function () + { + const rrect1 = new PIXI.RoundedRectangle(0, 0, 100, 100, 40); + + expect(rrect1.x).to.equal(0); + expect(rrect1.y).to.equal(0); + expect(rrect1.width).to.equal(100); + expect(rrect1.height).to.equal(100); + expect(rrect1.radius).to.equal(40); + + const rrect2 = rrect1.clone(); + + expect(rrect2.x).to.equal(0); + expect(rrect2.y).to.equal(0); + expect(rrect2.width).to.equal(100); + expect(rrect2.height).to.equal(100); + expect(rrect2.radius).to.equal(40); + expect(rrect1).to.not.equal(rrect2); + }); + + it('should check if point is within rounded rectangle', function () + { + const rrect1 = new PIXI.RoundedRectangle(0, 0, 200, 200, 50); + + expect(rrect1.contains(50, 50)).to.be.true; + expect(rrect1.contains(5, 100)).to.be.true; + expect(rrect1.contains(100, 5)).to.be.true; + expect(rrect1.contains(195, 100)).to.be.true; + expect(rrect1.contains(100, 195)).to.be.true; + expect(rrect1.contains(20, 20)).to.be.true; + expect(rrect1.contains(180, 20)).to.be.true; + expect(rrect1.contains(180, 180)).to.be.true; + expect(rrect1.contains(20, 180)).to.be.true; + expect(rrect1.contains(10, 10)).to.be.false; + expect(rrect1.contains(190, 10)).to.be.false; + expect(rrect1.contains(190, 190)).to.be.false; + expect(rrect1.contains(10, 190)).to.be.false; + + const rrect2 = new PIXI.RoundedRectangle(0, 0, 10, 0, 1); + + expect(rrect2.contains(0, 0)).to.be.false; + + const rrect3 = new PIXI.RoundedRectangle(0, 0, 0, 10, 1); + + expect(rrect3.contains(0, 0)).to.be.false; + }); +}); diff --git a/test/core/index.js b/test/core/index.js index cba5040..b546989 100755 --- a/test/core/index.js +++ b/test/core/index.js @@ -15,5 +15,6 @@ require('./ObservablePoint'); require('./Matrix'); require('./Rectangle'); +require('./RoundedRectangle'); require('./Circle'); require('./Graphics');