- 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
import { DisplayObject } from '@pixi/display';
import { FederatedEvent } from './FederatedEvent';
import type { EventEmitter } from '@pixi/utils';
export type Cursor = 'auto'
| 'default'
| 'none'
| 'context-menu'
| 'help'
| 'pointer'
| 'progress'
| 'wait'
| 'cell'
| 'crosshair'
| 'text'
| 'vertical-text'
| 'alias'
| 'copy'
| 'move'
| 'no-drop'
| 'not-allowed'
| 'e-resize'
| 'n-resize'
| 'ne-resize'
| 'nw-resize'
| 's-resize'
| 'se-resize'
| 'sw-resize'
| 'w-resize'
| 'ns-resize'
| 'ew-resize'
| 'nesw-resize'
| 'col-resize'
| 'nwse-resize'
| 'row-resize'
| 'all-scroll'
| 'zoom-in'
| 'zoom-out'
| 'grab'
| 'grabbing';
// @ignore - This is documented elsewhere.
export interface IHitArea
{
contains(x: number, y: number): boolean;
}
/**
* Describes the shape for a {@link FederatedEvent}'s' `eventTarget`.
* @memberof PIXI
*/
export interface FederatedEventTarget extends EventEmitter, EventTarget
{
/** The cursor preferred when the mouse pointer is hovering over. */
cursor: Cursor | string;
/** The parent of this event target. */
readonly parent?: FederatedEventTarget;
/** The children of this event target. */
readonly children?: ReadonlyArray<FederatedEventTarget>;
/** Whether this event target should fire UI events. */
interactive: boolean;
/** Whether this event target has any children that need UI events. This can be used optimize event propagation. */
interactiveChildren: boolean;
/** The hit-area specifies the area for which pointer events should be captured by this event target. */
hitArea: IHitArea | null;
}
export const FederatedDisplayObject: Omit<
FederatedEventTarget,
'parent' | 'children' | keyof EventEmitter | 'cursor'
> = {
/**
* Enable interaction events for the DisplayObject. Touch, pointer and mouse
* events will not be emitted unless `interactive` is set to `true`.
* @example
* const sprite = new PIXI.Sprite(texture);
* sprite.interactive = true;
* sprite.on('tap', (event) => {
* //handle event
* });
* @memberof PIXI.DisplayObject#
*/
interactive: false,
/**
* Determines if the children to the displayObject can be clicked/touched
* Setting this to false allows PixiJS to bypass a recursive `hitTest` function
* @memberof PIXI.Container#
*/
interactiveChildren: true,
/**
* Interaction shape. Children will be hit first, then this shape will be checked.
* Setting this will cause this shape to be checked in hit tests rather than the displayObject's bounds.
* @example
* const sprite = new PIXI.Sprite(texture);
* sprite.interactive = true;
* sprite.hitArea = new PIXI.Rectangle(0, 0, 100, 100);
* @member {PIXI.IHitArea}
* @memberof PIXI.DisplayObject#
*/
hitArea: null,
/**
* Unlike `on` or `addListener` which are methods from EventEmitter, `addEventListener`
* seeks to be compatible with the DOM's `addEventListener` with support for options.
* **IMPORTANT:** _Only_ available if using the `@pixi/events` package.
* @memberof PIXI.DisplayObject
* @param type - The type of event to listen to.
* @param listener - The listener callback or object.
* @param options - Listener options, used for capture phase.
* @example
* // Tell the user whether they did a single, double, triple, or nth click.
* button.addEventListener('click', {
* handleEvent(e): {
* let prefix;
*
* switch (e.detail) {
* case 1: prefix = 'single'; break;
* case 2: prefix = 'double'; break;
* case 3: prefix = 'triple'; break;
* default: prefix = e.detail + 'th'; break;
* }
*
* console.log('That was a ' + prefix + 'click');
* }
* });
*
* // But skip the first click!
* button.parent.addEventListener('click', function blockClickOnce(e) {
* e.stopImmediatePropagation();
* button.parent.removeEventListener('click', blockClickOnce, true);
* }, {
* capture: true,
* })
*/
addEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
)
{
const capture = (typeof options === 'boolean' && options)
|| (typeof options === 'object' && options.capture);
const context = typeof listener === 'function' ? undefined : listener;
type = capture ? `${type}capture` : type;
listener = typeof listener === 'function' ? listener : listener.handleEvent;
(this as unknown as EventEmitter).on(type, listener, context);
},
/**
* Unlike `off` or `removeListener` which are methods from EventEmitter, `removeEventListener`
* seeks to be compatible with the DOM's `removeEventListener` with support for options.
* **IMPORTANT:** _Only_ available if using the `@pixi/events` package.
* @memberof PIXI.DisplayObject
* @param type - The type of event the listener is bound to.
* @param listener - The listener callback or object.
* @param options - The original listener options. This is required to deregister a capture phase listener.
*/
removeEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
)
{
const capture = (typeof options === 'boolean' && options)
|| (typeof options === 'object' && options.capture);
const context = typeof listener === 'function' ? undefined : listener;
type = capture ? `${type}capture` : type;
listener = typeof listener === 'function' ? listener : listener.handleEvent;
(this as unknown as EventEmitter).off(type, listener, context);
},
/**
* Dispatch the event on this {@link PIXI.DisplayObject} using the event's {@link PIXI.EventBoundary}.
*
* The target of the event is set to `this` and the `defaultPrevented` flag is cleared before dispatch.
*
* **IMPORTANT:** _Only_ available if using the `@pixi/events` package.
* @memberof PIXI.DisplayObject
* @param e - The event to dispatch.
* @returns Whether the {@link PIXI.FederatedEvent.preventDefault preventDefault}() method was not invoked.
* @example
* // Reuse a click event!
* button.dispatchEvent(clickEvent);
*/
dispatchEvent(e: Event): boolean
{
if (!(e instanceof FederatedEvent))
{
throw new Error('DisplayObject cannot propagate events outside of the Federated Events API');
}
e.defaultPrevented = false;
e.path = null;
e.target = this as FederatedEventTarget;
e.manager.dispatchEvent(e);
return !e.defaultPrevented;
}
};
DisplayObject.mixin(FederatedDisplayObject);