-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Expand file tree
/
Copy pathRectangle.js
More file actions
371 lines (307 loc) · 11.4 KB
/
Rectangle.js
File metadata and controls
371 lines (307 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2025 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../../../utils/Class');
var Earcut = require('../../../geom/polygon/Earcut');
var GeomRectangle = require('../../../geom/rectangle/Rectangle');
var Shape = require('../Shape');
var RectangleRender = require('./RectangleRender');
/**
* @classdesc
* The Rectangle Shape is a Game Object that can be added to a Scene, Group or Container. You can
* treat it like any other Game Object in your game, such as tweening it, scaling it, or enabling
* it for input or physics. It provides a quick and easy way for you to render this shape in your
* game without using a texture, while still taking advantage of being fully batched in WebGL.
*
* This shape supports both fill and stroke colors.
*
* You can change the size of the rectangle by changing the `width` and `height` properties.
*
* @class Rectangle
* @extends Phaser.GameObjects.Shape
* @memberof Phaser.GameObjects
* @constructor
* @since 3.13.0
*
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
* @param {number} x - The horizontal position of this Game Object in the world.
* @param {number} y - The vertical position of this Game Object in the world.
* @param {number} [width=128] - The width of the rectangle.
* @param {number} [height=128] - The height of the rectangle.
* @param {number} [fillColor] - The color the rectangle will be filled with, i.e. 0xff0000 for red.
* @param {number} [fillAlpha] - The alpha the rectangle will be filled with. You can also set the alpha of the overall Shape using its `alpha` property.
*/
var Rectangle = new Class({
Extends: Shape,
Mixins: [
RectangleRender
],
initialize:
function Rectangle (scene, x, y, width, height, fillColor, fillAlpha)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = 128; }
if (height === undefined) { height = 128; }
Shape.call(this, scene, 'Rectangle', new GeomRectangle(0, 0, width, height));
/**
* The radius of the top-left corner of the rectangle.
*
* Do not modify this property. Instead, call the method `setRounded` to set the
* radius of the rounded corners.
*
* @name Phaser.GameObjects.Rectangle#radiusTopLeft
* @type {number}
* @readonly
*/
this.radiusTopLeft = 0;
/**
* The radius of the top-right corner of the rectangle.
*
* Do not modify this property. Instead, call the method `setRounded` to set the
* radius of the rounded corners.
*
* @name Phaser.GameObjects.Rectangle#radiusTopRight
* @type {number}
* @readonly
*/
this.radiusTopRight = 0;
/**
* The radius of the bottom-left corner of the rectangle.
*
* Do not modify this property. Instead, call the method `setRounded` to set the
* radius of the rounded corners.
*
* @name Phaser.GameObjects.Rectangle#radiusBottomLeft
* @type {number}
* @readonly
*/
this.radiusBottomLeft = 0;
/**
* The radius of the bottom-right corner of the rectangle.
*
* Do not modify this property. Instead, call the method `setRounded` to set the
* radius of the rounded corners.
*
* @name Phaser.GameObjects.Rectangle#radiusBottomRight
* @type {number}
* @readonly
*/
this.radiusBottomRight = 0;
this.setPosition(x, y);
this.setSize(width, height);
if (fillColor !== undefined)
{
this.setFillStyle(fillColor, fillAlpha);
}
this.updateDisplayOrigin();
this.updateData();
},
/**
* The radius of all the corners of the rectangle if this is set to use rounded corners.
* Return `radiusTopLeft` when read this radius property.
*
* @name Phaser.GameObjects.Shape#radius
* @type {number}
* @since 3.90.0
*/
radius: {
get: function ()
{
return this.radiusTopLeft;
},
set: function (value)
{
this.setRounded(value, value, value, value);
}
},
/**
* Does this Rectangle have rounded corners?
*
* It checks to see if the 4 radius properties are set to 0.
* This indicates that a Rectangle isn't rounded.
*
* @name Phaser.GameObjects.Shape#isRounded
* @type {boolean}
* @readonly
* @since 3.90.0
*/
isRounded: {
get: function ()
{
return (
this.radiusTopLeft !== 0 ||
this.radiusTopRight !== 0 ||
this.radiusBottomLeft !== 0 ||
this.radiusBottomRight !== 0
);
}
},
/**
* Sets this rectangle to have rounded corners by specifying the radius of the corner.
*
* The radius of the rounded corners is limited by the smallest dimension of the rectangle.
*
* To disable rounded corners, set the `topLeft` parameter to 0.
*
* @method Phaser.GameObjects.Rectangle#setRounded
* @since 3.90.0
*
* @param {number} [topLeft=16] - The radius of the top-left corner. If no other values are given this value is applied evenly, setting all corners to this radius.
* @param {number} [topRight] - The radius of the top-right corner.
* @param {number} [bottomLeft] - The radius of the bottom-left corner.
* @param {number} [bottomRight] - The radius of the bottom-right corner.
*
* @return {this} This Game Object instance.
*/
setRounded: function (topLeft, topRight, bottomLeft, bottomRight)
{
if (topLeft === undefined) { topLeft = 16; }
if (topRight === undefined)
{
topRight = topLeft;
bottomLeft = topLeft;
bottomRight = topLeft;
}
this.radiusTopLeft = topLeft;
this.radiusTopRight = topRight;
this.radiusBottomLeft = bottomLeft;
this.radiusBottomRight = bottomRight;
return this.updateRoundedData();
},
/**
* Sets the internal size of this Rectangle, as used for frame or physics body creation.
*
* If you have assigned a custom input hit area for this Rectangle, changing the Rectangle size will _not_ change the
* size of the hit area. To do this you should adjust the `input.hitArea` object directly.
*
* @method Phaser.GameObjects.Rectangle#setSize
* @since 3.13.0
*
* @param {number} width - The width of this Game Object.
* @param {number} height - The height of this Game Object.
*
* @return {this} This Game Object instance.
*/
setSize: function (width, height)
{
this.width = width;
this.height = height;
this.geom.setSize(width, height);
this.updateData();
this.updateDisplayOrigin();
var input = this.input;
if (input && !input.customHitArea)
{
input.hitArea.width = width;
input.hitArea.height = height;
}
return this;
},
/**
* Internal method that updates the data and path values.
*
* @method Phaser.GameObjects.Rectangle#updateData
* @private
* @since 3.13.0
*
* @return {this} This Game Object instance.
*/
updateData: function ()
{
if (this.isRounded)
{
return this.updateRoundedData();
}
var path = [];
var rect = this.geom;
var line = this._tempLine;
rect.getLineA(line);
path.push(line.x1, line.y1, line.x2, line.y2);
rect.getLineB(line);
path.push(line.x2, line.y2);
rect.getLineC(line);
path.push(line.x2, line.y2);
rect.getLineD(line);
path.push(line.x2, line.y2);
this.pathData = path;
return this;
},
/**
* Internal method that updates the data and path values when this rectangle is rounded.
*
* @method Phaser.GameObjects.Rectangle#updateRoundedData
* @private
* @since 3.90.0
*
* @return {this} This Game Object instance.
*/
updateRoundedData: function ()
{
var path = [];
var halfWidth = this.width / 2;
var halfHeight = this.height / 2;
// Limit max radius to half the smallest dimension
var maxRadius = Math.min(halfWidth, halfHeight);
var tl = Math.min(this.radiusTopLeft, maxRadius);
var tr = Math.min(this.radiusTopRight, maxRadius);
var bl = Math.min(this.radiusBottomLeft, maxRadius);
var br = Math.min(this.radiusBottomRight, maxRadius);
var x = halfWidth;
var y = halfHeight;
// Ensure minimum smoothness for small radii while preventing excessive tessellation
var tlSegments = Math.max(4, Math.min(16, Math.ceil(tl / 2)));
var trSegments = Math.max(4, Math.min(16, Math.ceil(tr / 2)));
var blSegments = Math.max(4, Math.min(16, Math.ceil(bl / 2)));
var brSegments = Math.max(4, Math.min(16, Math.ceil(br / 2)));
// Create points going clockwise from top-left
// Top-left corner
this.arcTo(path, x - halfWidth + tl, y - halfHeight + tl, tl, Math.PI, Math.PI * 1.5, tlSegments);
// Top edge and top-right corner
path.push(x + halfWidth - tr, y - halfHeight);
this.arcTo(path, x + halfWidth - tr, y - halfHeight + tr, tr, Math.PI * 1.5, Math.PI * 2, trSegments);
// Right edge and bottom-right corner
path.push(x + halfWidth, y + halfHeight - br);
this.arcTo(path, x + halfWidth - br, y + halfHeight - br, br, 0, Math.PI * 0.5, brSegments);
// Bottom edge and bottom-left corner
path.push(x - halfWidth + bl, y + halfHeight);
this.arcTo(path, x - halfWidth + bl, y + halfHeight - bl, bl, Math.PI * 0.5, Math.PI, blSegments);
// Left edge (connects back to first point)
path.push(x - halfWidth, y - halfHeight + tl);
this.pathIndexes = Earcut(path);
this.pathData = path;
return this;
},
/**
* Internal method placing points around the circumference of a circle for the rounded corners.
*
* @method Phaser.GameObjects.Rectangle#arcTo
* @private
* @since 3.90.0
*
* @param {number[]} path - The array to push the points into.
* @param {number} centerX - The center x coordinate of the circle.
* @param {number} centerY - The center y coordinate of the circle.
* @param {number} radius - The radius of the circle.
* @param {number} startAngle - The starting angle of the arc.
* @param {number} endAngle - The ending angle of the arc.
* @param {number} segments - The number of segments to create.
*
* @return {this} This Game Object instance.
*/
arcTo: function (path, centerX, centerY, radius, startAngle, endAngle, segments)
{
var angleInc = (endAngle - startAngle) / segments;
for (var i = 0; i <= segments; i++)
{
var angle = startAngle + (angleInc * i);
path.push(
centerX + Math.cos(angle) * radius,
centerY + Math.sin(angle) * radius
);
}
}
});
module.exports = Rectangle;