Skip to content

Commit ef44d01

Browse files
authored
Merge pull request #1327 from melonjs/fix/roundrect-copilot-feedback
Fix RoundRect radius clamping and containsRectangle API
2 parents 7e50927 + a60406a commit ef44d01

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

packages/melonjs/src/geometries/roundrect.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ export class RoundRect extends Polygon {
158158
}
159159
set width(value) {
160160
this._width = value;
161-
this._updateVertices();
161+
// re-clamp radius and rebuild vertices
162+
this.radius = this._radius;
162163
}
163164

164165
/**
@@ -169,7 +170,8 @@ export class RoundRect extends Polygon {
169170
}
170171
set height(value) {
171172
this._height = value;
172-
this._updateVertices();
173+
// re-clamp radius and rebuild vertices
174+
this.radius = this._radius;
173175
}
174176

175177
/**
@@ -222,7 +224,8 @@ export class RoundRect extends Polygon {
222224
return this._radius;
223225
}
224226
set radius(value) {
225-
// clamp radius to half the shorter side
227+
// clamp to non-negative and to half the shorter side
228+
value = Math.max(0, value);
226229
if (this._width < 2 * value) {
227230
value = this._width / 2;
228231
}
@@ -359,9 +362,18 @@ export class RoundRect extends Polygon {
359362
/**
360363
* Returns true if the rounded rectangle contains the given rectangle
361364
* @param rectangle - rectangle to test
365+
* @param rectangle.left - left coordinate
366+
* @param rectangle.right - right coordinate
367+
* @param rectangle.top - top coordinate
368+
* @param rectangle.bottom - bottom coordinate
362369
* @returns true if contained
363370
*/
364-
containsRectangle(rectangle: RoundRect) {
371+
containsRectangle(rectangle: {
372+
left: number;
373+
right: number;
374+
top: number;
375+
bottom: number;
376+
}) {
365377
return (
366378
rectangle.left >= this.left &&
367379
rectangle.right <= this.right &&

packages/melonjs/tests/roundrect.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,30 @@ describe("Shape : RoundRect", () => {
129129
const rr = new RoundRect(0, 0, 30, 50, 100);
130130
expect(rr.radius).toEqual(15); // 30/2
131131
});
132+
133+
it("should clamp radius when width shrinks via setter", () => {
134+
const rr = new RoundRect(0, 0, 100, 100, 40);
135+
rr.width = 20;
136+
expect(rr.radius).toEqual(10); // clamped to 20/2
137+
});
138+
139+
it("should clamp radius when height shrinks via setter", () => {
140+
const rr = new RoundRect(0, 0, 100, 100, 40);
141+
rr.height = 30;
142+
expect(rr.radius).toEqual(15); // clamped to 30/2
143+
});
144+
145+
it("should clamp negative radius to 0", () => {
146+
const rr = new RoundRect(0, 0, 100, 100, -10);
147+
expect(rr.radius).toEqual(0);
148+
});
149+
150+
it("should clamp negative radius to 0 via setter", () => {
151+
const rr = new RoundRect(0, 0, 100, 100, 20);
152+
rr.radius = -5;
153+
expect(rr.radius).toEqual(0);
154+
expect(rr.points.length).toEqual(4); // plain rectangle
155+
});
132156
});
133157

134158
describe("contains — corner edge cases", () => {
@@ -226,6 +250,17 @@ describe("Shape : RoundRect", () => {
226250
it("should contain itself", () => {
227251
expect(rrect.containsRectangle(rrect)).toEqual(true);
228252
});
253+
254+
it("should accept a Rect (not just RoundRect)", () => {
255+
const inner = new Rect(90, 90, 20, 20);
256+
expect(rrect.containsRectangle(inner)).toEqual(true);
257+
});
258+
259+
it("should accept any object with left/right/top/bottom", () => {
260+
expect(
261+
rrect.containsRectangle({ left: 90, right: 110, top: 90, bottom: 110 }),
262+
).toEqual(true);
263+
});
229264
});
230265

231266
describe("copy, clone & equality — additional cases", () => {

0 commit comments

Comments
 (0)