- 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
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { CanvasRenderer } from '@pixi/canvas-renderer';
import { Ticker } from '@pixi/ticker';
import type { Point } from '@pixi/math';
import { Rectangle } from '@pixi/math';
import type { Container } from '@pixi/display';
const { system } = Ticker;
/**
* Use this to mock mouse/touch/pointer events
* @class
*/
class MockPointer
{
public createdPointerEvent: any;
public activeTouches: any[];
public stage: Container;
public renderer: CanvasRenderer;
public interaction: any;
/**
* @param {PIXI.Container} stage - The root of the scene tree
* @param {number} [width=100] - Width of the renderer
* @param {number} [height=100] - Height of the renderer
* @param {boolean} [ensurePointerEvents=false] - If we should make sure that PointerEvents are 'supported'
*/
constructor(stage?: Container, width?: number, height?: number, ensurePointerEvents?: boolean)
{
// fake PointerEvent existing
if (ensurePointerEvents && !window.PointerEvent)
{
(window as any).PointerEvent = class PointerEvent extends MouseEvent
{
public pointerType: string;
// eslint-disable-next-line
constructor(type: string, opts: any)
{
super(type, opts);
this.pointerType = opts.pointerType;
}
};
this.createdPointerEvent = true;
}
this.activeTouches = [];
this.stage = stage;
this.renderer = new CanvasRenderer({
width: width || 100,
height: height || 100,
});
// @ts-expect-error ---
this.renderer.sayHello = () => { /* empty */ };
this.interaction = this.renderer.plugins.interaction;
this.interaction.supportsTouchEvents = true;
system.remove(this.interaction.update, this.interaction);
}
/** Cleans up after tests */
cleanup()
{
if (this.createdPointerEvent)
{
delete window.PointerEvent;
}
this.renderer.destroy();
}
/**
* @private
* @param {number} x - pointer x position
* @param {number} y - pointer y position
*/
setPosition(x: number, y: number)
{
this.renderer.plugins.interaction.mapPositionToPoint = (point: Point) =>
{
point.x = x;
point.y = y;
};
}
/**
* @private
*/
render()
{
this.renderer.render(this.stage);
}
/**
* [createEvent description]
* @param {string} eventType - `type` of event
* @param {number} x - pointer x position
* @param {number} y - pointer y position
* @param {number} [identifier] - pointer id for touch events
* @param {boolean} [asPointer] - If it should be a PointerEvent from a mouse or touch
* @param {boolean} [onCanvas=true] - If the event should be on the canvas (as opposed to a different element)
* @returns {Event} Generated MouseEvent, TouchEvent, or PointerEvent
*/
createEvent(
eventType: string,
x: number,
y: number,
identifier?: number | boolean,
asPointer?: boolean,
onCanvas = true
): Event
{
let event: PointerEvent | MouseEvent | TouchEvent;
if (eventType.startsWith('mouse'))
{
if (asPointer)
{
event = new PointerEvent(eventType.replace('mouse', 'pointer'), {
pointerType: 'mouse',
clientX: x,
clientY: y,
});
}
else
{
event = new MouseEvent(eventType, {
clientX: x,
clientY: y,
});
}
if (onCanvas)
{
Object.defineProperty(event, 'target', { value: this.renderer.view });
}
}
else if (eventType.startsWith('touch'))
{
if (asPointer)
{
eventType = eventType.replace('touch', 'pointer').replace('start', 'down').replace('end', 'up');
event = new PointerEvent(eventType, {
pointerType: 'touch',
pointerId: identifier as number || 0,
clientX: x,
clientY: y,
});
Object.defineProperty(event, 'target', { value: this.renderer.view });
}
else
{
const touch = new Touch({ identifier: identifier as number || 0, target: this.renderer.view });
if (eventType.endsWith('start'))
{
this.activeTouches.push(touch);
}
else if (eventType.endsWith('end') || eventType.endsWith('leave'))
{
for (let i = 0; i < this.activeTouches.length; ++i)
{
if (this.activeTouches[i].identifier === touch.identifier)
{
this.activeTouches.splice(i, 1);
break;
}
}
}
event = new TouchEvent(eventType, {
changedTouches: [touch],
touches: this.activeTouches,
});
Object.defineProperty(event, 'target', { value: this.renderer.view });
}
}
else
{
event = new PointerEvent(eventType, {
pointerType: 'pen',
pointerId: identifier as number || 0,
clientX: x,
clientY: y,
});
Object.defineProperty(event, 'target', { value: this.renderer.view });
}
this.setPosition(x, y);
this.render();
return event;
}
/**
* @param {number} x - pointer x position
* @param {number} y - pointer y position
* @param {boolean} [asPointer] - if it should be a PointerEvent from a mouse
*/
mousemove(x: number, y: number, asPointer?: boolean)
{
// mouseOverRenderer state should be correct, so mouse position to view rect
const rect = new Rectangle(0, 0, this.renderer.width, this.renderer.height);
if (rect.contains(x, y))
{
if (!this.interaction.mouseOverRenderer)
{
this.interaction.onPointerOver(this.createEvent('mouseover', x, y, null, asPointer));
}
this.interaction.onPointerMove(this.createEvent('mousemove', x, y, null, asPointer));
}
else
{
this.interaction.onPointerOut(this.createEvent('mouseout', x, y, null, asPointer));
}
}
/**
* @param {number} x - pointer x position
* @param {number} y - pointer y position
* @param {boolean} [asPointer] - if it should be a PointerEvent from a mouse
*/
click(x: number, y: number, asPointer?: boolean)
{
this.mousedown(x, y, asPointer);
this.mouseup(x, y, asPointer);
}
/**
* @param {number} x - pointer x position
* @param {number} y - pointer y position
* @param {boolean} [asPointer] - if it should be a PointerEvent from a mouse
*/
mousedown(x: number, y: number, asPointer?: boolean)
{
this.interaction.onPointerDown(this.createEvent('mousedown', x, y, null, asPointer));
}
/**
* @param {number} x - pointer x position
* @param {number} y - pointer y position
* @param {boolean} [onCanvas=true] - if the event happened on the Canvas element or not
* @param {boolean} [asPointer] - if it should be a PointerEvent from a mouse
*/
mouseup(x: number, y: number, onCanvas = true, asPointer = false)
{
this.interaction.onPointerUp(this.createEvent('mouseup', x, y, null, asPointer, onCanvas));
}
/**
* @param {number} x - pointer x position
* @param {number} y - pointer y position
* @param {number} [identifier] - pointer id
* @param {boolean} [asPointer] - if it should be a PointerEvent from a touch
*/
tap(x: number, y: number, identifier?: undefined, asPointer?: undefined)
{
this.touchstart(x, y, identifier, asPointer);
this.touchend(x, y, identifier, asPointer);
}
/**
* @param {number} x - pointer x position
* @param {number} y - pointer y position
* @param {number} [identifier] - pointer id
* @param {boolean} [asPointer] - if it should be a PointerEvent from a touch
*/
touchstart(x: number, y: number, identifier?: number | boolean, asPointer?: boolean)
{
this.interaction.onPointerDown(this.createEvent('touchstart', x, y, identifier, asPointer));
}
/**
* @param {number} x - pointer x position
* @param {number} y - pointer y position
* @param {number} [identifier] - pointer id
* @param {boolean} [asPointer] - if it should be a PointerEvent from a touch
*/
touchmove(x: number, y: number, identifier?: number | boolean, asPointer?: boolean)
{
this.interaction.onPointerMove(this.createEvent('touchmove', x, y, identifier, asPointer));
}
/**
* @param {number} x - pointer x position
* @param {number} y - pointer y position
* @param {number} [identifier] - pointer id
* @param {boolean} [asPointer] - if it should be a PointerEvent from a touch
*/
touchend(x: number, y: number, identifier?: number | boolean, asPointer?: boolean)
{
this.interaction.onPointerUp(this.createEvent('touchend', x, y, identifier, asPointer));
}
/**
* @param {number} x - pointer x position
* @param {number} y - pointer y position
* @param {number} [identifier] - pointer id
* @param {boolean} [asPointer] - if it should be a PointerEvent from a touch
*/
touchleave(x: number, y: number, identifier?: number | boolean, asPointer?: boolean)
{
this.interaction.onPointerOut(this.createEvent('touchleave', x, y, identifier, asPointer));
}
/**
* @param {number} x - pointer x position
* @param {number} y - pointer y position
* @param {number} [identifier] - pointer id
*/
pendown(x: number, y: number, identifier?: number)
{
this.interaction.onPointerDown(this.createEvent('pointerdown', x, y, identifier, true));
}
/**
* @param {number} x - pointer x position
* @param {number} y - pointer y position
* @param {number} [identifier] - pointer id
*/
penmove(x: number, y: number, identifier?: number)
{
this.interaction.onPointerMove(this.createEvent('pointermove', x, y, identifier, true));
}
/**
* @param {number} x - pointer x position
* @param {number} y - pointer y position
* @param {number} [identifier] - pointer id
*/
penup(x: number, y: number, identifier?: number)
{
this.interaction.onPointerUp(this.createEvent('pointerup', x, y, identifier, true));
}
}
export { MockPointer };