Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions packages/melonjs/src/geometries/roundrect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ export class RoundRect extends Polygon {
}
set width(value) {
this._width = value;
this._updateVertices();
// re-clamp radius and rebuild vertices
this.radius = this._radius;
}

/**
Expand All @@ -169,7 +170,8 @@ export class RoundRect extends Polygon {
}
set height(value) {
this._height = value;
this._updateVertices();
// re-clamp radius and rebuild vertices
this.radius = this._radius;
}

/**
Expand Down Expand Up @@ -222,7 +224,8 @@ export class RoundRect extends Polygon {
return this._radius;
}
set radius(value) {
// clamp radius to half the shorter side
// clamp to non-negative and to half the shorter side
value = Math.max(0, value);
if (this._width < 2 * value) {
value = this._width / 2;
}
Expand Down Expand Up @@ -359,9 +362,18 @@ export class RoundRect extends Polygon {
/**
* Returns true if the rounded rectangle contains the given rectangle
* @param rectangle - rectangle to test
* @param rectangle.left - left coordinate
* @param rectangle.right - right coordinate
* @param rectangle.top - top coordinate
* @param rectangle.bottom - bottom coordinate
* @returns true if contained
*/
containsRectangle(rectangle: RoundRect) {
containsRectangle(rectangle: {
left: number;
right: number;
top: number;
bottom: number;
}) {
return (
rectangle.left >= this.left &&
rectangle.right <= this.right &&
Expand Down
35 changes: 35 additions & 0 deletions packages/melonjs/tests/roundrect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,30 @@ describe("Shape : RoundRect", () => {
const rr = new RoundRect(0, 0, 30, 50, 100);
expect(rr.radius).toEqual(15); // 30/2
});

it("should clamp radius when width shrinks via setter", () => {
const rr = new RoundRect(0, 0, 100, 100, 40);
rr.width = 20;
expect(rr.radius).toEqual(10); // clamped to 20/2
});

it("should clamp radius when height shrinks via setter", () => {
const rr = new RoundRect(0, 0, 100, 100, 40);
rr.height = 30;
expect(rr.radius).toEqual(15); // clamped to 30/2
});

it("should clamp negative radius to 0", () => {
const rr = new RoundRect(0, 0, 100, 100, -10);
expect(rr.radius).toEqual(0);
});

it("should clamp negative radius to 0 via setter", () => {
const rr = new RoundRect(0, 0, 100, 100, 20);
rr.radius = -5;
expect(rr.radius).toEqual(0);
expect(rr.points.length).toEqual(4); // plain rectangle
});
});

describe("contains — corner edge cases", () => {
Expand Down Expand Up @@ -226,6 +250,17 @@ describe("Shape : RoundRect", () => {
it("should contain itself", () => {
expect(rrect.containsRectangle(rrect)).toEqual(true);
});

it("should accept a Rect (not just RoundRect)", () => {
const inner = new Rect(90, 90, 20, 20);
expect(rrect.containsRectangle(inner)).toEqual(true);
});

it("should accept any object with left/right/top/bottom", () => {
expect(
rrect.containsRectangle({ left: 90, right: 110, top: 90, bottom: 110 }),
).toEqual(true);
});
});

describe("copy, clone & equality — additional cases", () => {
Expand Down
Loading