- 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
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
import { Rectangle } from '@pixi/math';
import type { IPointData, Transform, Matrix } from '@pixi/math';
/**
* 'Builder' pattern for bounds rectangles.
*
* This could be called an Axis-Aligned Bounding Box.
* It is not an actual shape. It is a mutable thing; no 'EMPTY' or those kind of problems.
* @memberof PIXI
*/
export class Bounds
{
/** @default Infinity */
public minX: number;
/** @default Infinity */
public minY: number;
/** @default -Infinity */
public maxX: number;
/** @default -Infinity */
public maxY: number;
public rect: Rectangle;
/**
* It is updated to _boundsID of corresponding object to keep bounds in sync with content.
* Updated from outside, thus public modifier.
*/
public updateID: number;
constructor()
{
this.minX = Infinity;
this.minY = Infinity;
this.maxX = -Infinity;
this.maxY = -Infinity;
this.rect = null;
this.updateID = -1;
}
/**
* Checks if bounds are empty.
* @returns - True if empty.
*/
isEmpty(): boolean
{
return this.minX > this.maxX || this.minY > this.maxY;
}
/** Clears the bounds and resets. */
clear(): void
{
this.minX = Infinity;
this.minY = Infinity;
this.maxX = -Infinity;
this.maxY = -Infinity;
}
/**
* Can return Rectangle.EMPTY constant, either construct new rectangle, either use your rectangle
* It is not guaranteed that it will return tempRect
* @param rect - Temporary object will be used if AABB is not empty
* @returns - A rectangle of the bounds
*/
getRectangle(rect?: Rectangle): Rectangle
{
if (this.minX > this.maxX || this.minY > this.maxY)
{
return Rectangle.EMPTY;
}
rect = rect || new Rectangle(0, 0, 1, 1);
rect.x = this.minX;
rect.y = this.minY;
rect.width = this.maxX - this.minX;
rect.height = this.maxY - this.minY;
return rect;
}
/**
* This function should be inlined when its possible.
* @param point - The point to add.
*/
addPoint(point: IPointData): void
{
this.minX = Math.min(this.minX, point.x);
this.maxX = Math.max(this.maxX, point.x);
this.minY = Math.min(this.minY, point.y);
this.maxY = Math.max(this.maxY, point.y);
}
/**
* Adds a point, after transformed. This should be inlined when its possible.
* @param matrix
* @param point
*/
addPointMatrix(matrix: Matrix, point: IPointData): void
{
const { a, b, c, d, tx, ty } = matrix;
const x = (a * point.x) + (c * point.y) + tx;
const y = (b * point.x) + (d * point.y) + ty;
this.minX = Math.min(this.minX, x);
this.maxX = Math.max(this.maxX, x);
this.minY = Math.min(this.minY, y);
this.maxY = Math.max(this.maxY, y);
}
/**
* Adds a quad, not transformed
* @param vertices - The verts to add.
*/
addQuad(vertices: Float32Array): void
{
let minX = this.minX;
let minY = this.minY;
let maxX = this.maxX;
let maxY = this.maxY;
let x = vertices[0];
let y = vertices[1];
minX = x < minX ? x : minX;
minY = y < minY ? y : minY;
maxX = x > maxX ? x : maxX;
maxY = y > maxY ? y : maxY;
x = vertices[2];
y = vertices[3];
minX = x < minX ? x : minX;
minY = y < minY ? y : minY;
maxX = x > maxX ? x : maxX;
maxY = y > maxY ? y : maxY;
x = vertices[4];
y = vertices[5];
minX = x < minX ? x : minX;
minY = y < minY ? y : minY;
maxX = x > maxX ? x : maxX;
maxY = y > maxY ? y : maxY;
x = vertices[6];
y = vertices[7];
minX = x < minX ? x : minX;
minY = y < minY ? y : minY;
maxX = x > maxX ? x : maxX;
maxY = y > maxY ? y : maxY;
this.minX = minX;
this.minY = minY;
this.maxX = maxX;
this.maxY = maxY;
}
/**
* Adds sprite frame, transformed.
* @param transform - transform to apply
* @param x0 - left X of frame
* @param y0 - top Y of frame
* @param x1 - right X of frame
* @param y1 - bottom Y of frame
*/
addFrame(transform: Transform, x0: number, y0: number, x1: number, y1: number): void
{
this.addFrameMatrix(transform.worldTransform, x0, y0, x1, y1);
}
/**
* Adds sprite frame, multiplied by matrix
* @param matrix - matrix to apply
* @param x0 - left X of frame
* @param y0 - top Y of frame
* @param x1 - right X of frame
* @param y1 - bottom Y of frame
*/
addFrameMatrix(matrix: Matrix, x0: number, y0: number, x1: number, y1: number): void
{
const a = matrix.a;
const b = matrix.b;
const c = matrix.c;
const d = matrix.d;
const tx = matrix.tx;
const ty = matrix.ty;
let minX = this.minX;
let minY = this.minY;
let maxX = this.maxX;
let maxY = this.maxY;
let x = (a * x0) + (c * y0) + tx;
let y = (b * x0) + (d * y0) + ty;
minX = x < minX ? x : minX;
minY = y < minY ? y : minY;
maxX = x > maxX ? x : maxX;
maxY = y > maxY ? y : maxY;
x = (a * x1) + (c * y0) + tx;
y = (b * x1) + (d * y0) + ty;
minX = x < minX ? x : minX;
minY = y < minY ? y : minY;
maxX = x > maxX ? x : maxX;
maxY = y > maxY ? y : maxY;
x = (a * x0) + (c * y1) + tx;
y = (b * x0) + (d * y1) + ty;
minX = x < minX ? x : minX;
minY = y < minY ? y : minY;
maxX = x > maxX ? x : maxX;
maxY = y > maxY ? y : maxY;
x = (a * x1) + (c * y1) + tx;
y = (b * x1) + (d * y1) + ty;
minX = x < minX ? x : minX;
minY = y < minY ? y : minY;
maxX = x > maxX ? x : maxX;
maxY = y > maxY ? y : maxY;
this.minX = minX;
this.minY = minY;
this.maxX = maxX;
this.maxY = maxY;
}
/**
* Adds screen vertices from array
* @param vertexData - calculated vertices
* @param beginOffset - begin offset
* @param endOffset - end offset, excluded
*/
addVertexData(vertexData: Float32Array, beginOffset: number, endOffset: number): void
{
let minX = this.minX;
let minY = this.minY;
let maxX = this.maxX;
let maxY = this.maxY;
for (let i = beginOffset; i < endOffset; i += 2)
{
const x = vertexData[i];
const y = vertexData[i + 1];
minX = x < minX ? x : minX;
minY = y < minY ? y : minY;
maxX = x > maxX ? x : maxX;
maxY = y > maxY ? y : maxY;
}
this.minX = minX;
this.minY = minY;
this.maxX = maxX;
this.maxY = maxY;
}
/**
* Add an array of mesh vertices
* @param transform - mesh transform
* @param vertices - mesh coordinates in array
* @param beginOffset - begin offset
* @param endOffset - end offset, excluded
*/
addVertices(transform: Transform, vertices: Float32Array, beginOffset: number, endOffset: number): void
{
this.addVerticesMatrix(transform.worldTransform, vertices, beginOffset, endOffset);
}
/**
* Add an array of mesh vertices.
* @param matrix - mesh matrix
* @param vertices - mesh coordinates in array
* @param beginOffset - begin offset
* @param endOffset - end offset, excluded
* @param padX - x padding
* @param padY - y padding
*/
addVerticesMatrix(matrix: Matrix, vertices: Float32Array, beginOffset: number,
endOffset: number, padX = 0, padY = padX): void
{
const a = matrix.a;
const b = matrix.b;
const c = matrix.c;
const d = matrix.d;
const tx = matrix.tx;
const ty = matrix.ty;
let minX = this.minX;
let minY = this.minY;
let maxX = this.maxX;
let maxY = this.maxY;
for (let i = beginOffset; i < endOffset; i += 2)
{
const rawX = vertices[i];
const rawY = vertices[i + 1];
const x = (a * rawX) + (c * rawY) + tx;
const y = (d * rawY) + (b * rawX) + ty;
minX = Math.min(minX, x - padX);
maxX = Math.max(maxX, x + padX);
minY = Math.min(minY, y - padY);
maxY = Math.max(maxY, y + padY);
}
this.minX = minX;
this.minY = minY;
this.maxX = maxX;
this.maxY = maxY;
}
/**
* Adds other {@link Bounds}.
* @param bounds - The Bounds to be added
*/
addBounds(bounds: Bounds): void
{
const minX = this.minX;
const minY = this.minY;
const maxX = this.maxX;
const maxY = this.maxY;
this.minX = bounds.minX < minX ? bounds.minX : minX;
this.minY = bounds.minY < minY ? bounds.minY : minY;
this.maxX = bounds.maxX > maxX ? bounds.maxX : maxX;
this.maxY = bounds.maxY > maxY ? bounds.maxY : maxY;
}
/**
* Adds other Bounds, masked with Bounds.
* @param bounds - The Bounds to be added.
* @param mask - TODO
*/
addBoundsMask(bounds: Bounds, mask: Bounds): void
{
const _minX = bounds.minX > mask.minX ? bounds.minX : mask.minX;
const _minY = bounds.minY > mask.minY ? bounds.minY : mask.minY;
const _maxX = bounds.maxX < mask.maxX ? bounds.maxX : mask.maxX;
const _maxY = bounds.maxY < mask.maxY ? bounds.maxY : mask.maxY;
if (_minX <= _maxX && _minY <= _maxY)
{
const minX = this.minX;
const minY = this.minY;
const maxX = this.maxX;
const maxY = this.maxY;
this.minX = _minX < minX ? _minX : minX;
this.minY = _minY < minY ? _minY : minY;
this.maxX = _maxX > maxX ? _maxX : maxX;
this.maxY = _maxY > maxY ? _maxY : maxY;
}
}
/**
* Adds other Bounds, multiplied by matrix. Bounds shouldn't be empty.
* @param bounds - other bounds
* @param matrix - multiplicator
*/
addBoundsMatrix(bounds: Bounds, matrix: Matrix): void
{
this.addFrameMatrix(matrix, bounds.minX, bounds.minY, bounds.maxX, bounds.maxY);
}
/**
* Adds other Bounds, masked with Rectangle.
* @param bounds - TODO
* @param area - TODO
*/
addBoundsArea(bounds: Bounds, area: Rectangle): void
{
const _minX = bounds.minX > area.x ? bounds.minX : area.x;
const _minY = bounds.minY > area.y ? bounds.minY : area.y;
const _maxX = bounds.maxX < area.x + area.width ? bounds.maxX : (area.x + area.width);
const _maxY = bounds.maxY < area.y + area.height ? bounds.maxY : (area.y + area.height);
if (_minX <= _maxX && _minY <= _maxY)
{
const minX = this.minX;
const minY = this.minY;
const maxX = this.maxX;
const maxY = this.maxY;
this.minX = _minX < minX ? _minX : minX;
this.minY = _minY < minY ? _minY : minY;
this.maxX = _maxX > maxX ? _maxX : maxX;
this.maxY = _maxY > maxY ? _maxY : maxY;
}
}
/**
* Pads bounds object, making it grow in all directions.
* If paddingY is omitted, both paddingX and paddingY will be set to paddingX.
* @param paddingX - The horizontal padding amount.
* @param paddingY - The vertical padding amount.
*/
pad(paddingX = 0, paddingY = paddingX): void
{
if (!this.isEmpty())
{
this.minX -= paddingX;
this.maxX += paddingX;
this.minY -= paddingY;
this.maxY += paddingY;
}
}
/**
* Adds padded frame. (x0, y0) should be strictly less than (x1, y1)
* @param x0 - left X of frame
* @param y0 - top Y of frame
* @param x1 - right X of frame
* @param y1 - bottom Y of frame
* @param padX - padding X
* @param padY - padding Y
*/
addFramePad(x0: number, y0: number, x1: number, y1: number, padX: number, padY: number): void
{
x0 -= padX;
y0 -= padY;
x1 += padX;
y1 += padY;
this.minX = this.minX < x0 ? this.minX : x0;
this.maxX = this.maxX > x1 ? this.maxX : x1;
this.minY = this.minY < y0 ? this.minY : y0;
this.maxY = this.maxY > y1 ? this.maxY : y1;
}
}