import { Point, IPointData } from '@pixi/math';
import type { DisplayObject } from '@pixi/display';
export type InteractivePointerEvent = PointerEvent | TouchEvent | MouseEvent;
/**
* Holds all information related to an Interaction event
*
* @class
* @memberof PIXI
*/
export class InteractionData
{
public global: Point;
public target: DisplayObject;
public originalEvent: InteractivePointerEvent;
public identifier: number;
public isPrimary: boolean;
public button: number;
public buttons: number;
public width: number;
public height: number;
public tiltX: number;
public tiltY: number;
public pointerType: string;
public pressure = 0;
public rotationAngle = 0;
public twist = 0;
public tangentialPressure = 0;
constructor()
{
/**
* This point stores the global coords of where the touch/mouse event happened
*
* @member {PIXI.Point}
*/
this.global = new Point();
/**
* The target Sprite that was interacted with
*
* @member {PIXI.Sprite}
*/
this.target = null;
/**
* When passed to an event handler, this will be the original DOM Event that was captured
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent
* @see https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent
* @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent
* @member {MouseEvent|TouchEvent|PointerEvent}
*/
this.originalEvent = null;
/**
* Unique identifier for this interaction
*
* @member {number}
*/
this.identifier = null;
/**
* Indicates whether or not the pointer device that created the event is the primary pointer.
* @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/isPrimary
* @type {Boolean}
*/
this.isPrimary = false;
/**
* Indicates which button was pressed on the mouse or pointer device to trigger the event.
* @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
* @type {number}
*/
this.button = 0;
/**
* Indicates which buttons are pressed on the mouse or pointer device when the event is triggered.
* @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
* @type {number}
*/
this.buttons = 0;
/**
* The width of the pointer's contact along the x-axis, measured in CSS pixels.
* radiusX of TouchEvents will be represented by this value.
* @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/width
* @type {number}
*/
this.width = 0;
/**
* The height of the pointer's contact along the y-axis, measured in CSS pixels.
* radiusY of TouchEvents will be represented by this value.
* @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/height
* @type {number}
*/
this.height = 0;
/**
* The angle, in degrees, between the pointer device and the screen.
* @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/tiltX
* @type {number}
*/
this.tiltX = 0;
/**
* The angle, in degrees, between the pointer device and the screen.
* @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/tiltY
* @type {number}
*/
this.tiltY = 0;
/**
* The type of pointer that triggered the event.
* @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/pointerType
* @type {string}
*/
this.pointerType = null;
/**
* Pressure applied by the pointing device during the event. A Touch's force property
* will be represented by this value.
* @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/pressure
* @type {number}
*/
this.pressure = 0;
/**
* From TouchEvents (not PointerEvents triggered by touches), the rotationAngle of the Touch.
* @see https://developer.mozilla.org/en-US/docs/Web/API/Touch/rotationAngle
* @type {number}
*/
this.rotationAngle = 0;
/**
* Twist of a stylus pointer.
* @see https://w3c.github.io/pointerevents/#pointerevent-interface
* @type {number}
*/
this.twist = 0;
/**
* Barrel pressure on a stylus pointer.
* @see https://w3c.github.io/pointerevents/#pointerevent-interface
* @type {number}
*/
this.tangentialPressure = 0;
}
/**
* The unique identifier of the pointer. It will be the same as `identifier`.
* @readonly
* @member {number}
* @see https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/pointerId
*/
get pointerId(): number
{
return this.identifier;
}
/**
* This will return the local coordinates of the specified displayObject for this InteractionData
*
* @param {PIXI.DisplayObject} displayObject - The DisplayObject that you would like the local
* coords off
* @param {PIXI.Point} [point] - A Point object in which to store the value, optional (otherwise
* will create a new point)
* @param {PIXI.Point} [globalPos] - A Point object containing your custom global coords, optional
* (otherwise will use the current global coords)
* @return {PIXI.Point} A point containing the coordinates of the InteractionData position relative
* to the DisplayObject
*/
public getLocalPosition<P extends IPointData = Point>(displayObject: DisplayObject, point?: P, globalPos?: IPointData): P
{
return displayObject.worldTransform.applyInverse<P>(globalPos || this.global, point);
}
/**
* Copies properties from normalized event data.
*
* @param {Touch|MouseEvent|PointerEvent} event - The normalized event data
*/
public copyEvent(event: Touch | InteractivePointerEvent): void
{
// isPrimary should only change on touchstart/pointerdown, so we don't want to overwrite
// it with "false" on later events when our shim for it on touch events might not be
// accurate
if ('isPrimary' in event && event.isPrimary)
{
this.isPrimary = true;
}
this.button = 'button' in event && event.button;
// event.buttons is not available in all browsers (ie. Safari), but it does have a non-standard
// event.which property instead, which conveys the same information.
const buttons = 'buttons' in event && event.buttons;
this.buttons = Number.isInteger(buttons) ? buttons : 'which' in event && event.which;
this.width = 'width' in event && event.width;
this.height = 'height' in event && event.height;
this.tiltX = 'tiltX' in event && event.tiltX;
this.tiltY = 'tiltY' in event && event.tiltY;
this.pointerType = 'pointerType' in event && event.pointerType;
this.pressure = 'pressure' in event && event.pressure;
this.rotationAngle = 'rotationAngle' in event && event.rotationAngle;
this.twist = ('twist' in event && event.twist) || 0;
this.tangentialPressure = ('tangentialPressure' in event && event.tangentialPressure) || 0;
}
/**
* Resets the data for pooling.
*/
public reset(): void
{
// isPrimary is the only property that we really need to reset - everything else is
// guaranteed to be overwritten
this.isPrimary = false;
}
}