Files
Foundry-VTT-Docker/resources/app/node_modules/@pixi/math/lib/ObservablePoint.mjs
2025-01-04 00:34:03 +01:00

80 lines
2.7 KiB
JavaScript

class ObservablePoint {
/**
* Creates a new `ObservablePoint`
* @param cb - callback function triggered when `x` and/or `y` are changed
* @param scope - owner of callback
* @param {number} [x=0] - position of the point on the x axis
* @param {number} [y=0] - position of the point on the y axis
*/
constructor(cb, scope, x = 0, y = 0) {
this._x = x, this._y = y, this.cb = cb, this.scope = scope;
}
/**
* Creates a clone of this point.
* The callback and scope params can be overridden otherwise they will default
* to the clone object's values.
* @override
* @param cb - The callback function triggered when `x` and/or `y` are changed
* @param scope - The owner of the callback
* @returns a copy of this observable point
*/
clone(cb = this.cb, scope = this.scope) {
return new ObservablePoint(cb, scope, this._x, this._y);
}
/**
* Sets the point to a new `x` and `y` position.
* If `y` is omitted, both `x` and `y` will be set to `x`.
* @param {number} [x=0] - position of the point on the x axis
* @param {number} [y=x] - position of the point on the y axis
* @returns The observable point instance itself
*/
set(x = 0, y = x) {
return (this._x !== x || this._y !== y) && (this._x = x, this._y = y, this.cb.call(this.scope)), this;
}
/**
* Copies x and y from the given point (`p`)
* @param p - The point to copy from. Can be any of type that is or extends `IPointData`
* @returns The observable point instance itself
*/
copyFrom(p) {
return (this._x !== p.x || this._y !== p.y) && (this._x = p.x, this._y = p.y, this.cb.call(this.scope)), this;
}
/**
* Copies this point's x and y into that of the given point (`p`)
* @param p - The point to copy to. Can be any of type that is or extends `IPointData`
* @returns The point (`p`) with values updated
*/
copyTo(p) {
return p.set(this._x, this._y), p;
}
/**
* Accepts another point (`p`) and returns `true` if the given point is equal to this point
* @param p - The point to check
* @returns Returns `true` if both `x` and `y` are equal
*/
equals(p) {
return p.x === this._x && p.y === this._y;
}
/** Position of the observable point on the x axis. */
get x() {
return this._x;
}
set x(value) {
this._x !== value && (this._x = value, this.cb.call(this.scope));
}
/** Position of the observable point on the y axis. */
get y() {
return this._y;
}
set y(value) {
this._y !== value && (this._y = value, this.cb.call(this.scope));
}
}
ObservablePoint.prototype.toString = function() {
return `[@pixi/math:ObservablePoint x=${this.x} y=${this.y} scope=${this.scope}]`;
};
export {
ObservablePoint
};
//# sourceMappingURL=ObservablePoint.mjs.map