-
Notifications
You must be signed in to change notification settings - Fork 57
/
InteractionManagerMixin.ts
193 lines (175 loc) · 8.17 KB
/
InteractionManagerMixin.ts
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
//TODO: add maxDisplayOrder for displayObjects and use it to speed up the interaction here
/**
* @mixin
*/
namespace pixi_display {
import InteractionManager = PIXI.interaction.InteractionManager;
import DisplayObject = PIXI.DisplayObject;
import Point = PIXI.Point;
import Graphics = PIXI.Graphics;
import Container = PIXI.Container;
import InteractionEvent = PIXI.interaction.InteractionEvent;
(Object as any).assign(InteractionManager.prototype, {
_queue: [[] as Array<DisplayObject>, [] as Array<DisplayObject>],
/**
* This is private recursive copy of processInteractive
*/
_displayProcessInteractive: function (point: Point, displayObject: DisplayObject, hitTestOrder: number, interactive: boolean, outOfMask: boolean): number {
if (!displayObject || !displayObject.visible) {
return 0;
}
// Took a little while to rework this function correctly! But now it is done and nice and optimised. ^_^
//
// This function will now loop through all objects and then only hit test the objects it HAS to, not all of them. MUCH faster..
// An object will be hit test if the following is true:
//
// 1: It is interactive.
// 2: It belongs to a parent that is interactive AND one of the parents children have not already been hit.
//
// As another little optimisation once an interactive object has been hit we can carry on through the scenegraph, but we know that there will be no more hits! So we can avoid extra hit tests
// A final optimisation is that an object is not hit test directly if a child has already been hit.
let hit = 0,
interactiveParent = interactive = displayObject.interactive || interactive;
// if the displayobject has a hitArea, then it does not need to hitTest children.
if (displayObject.hitArea) {
interactiveParent = false;
}
if (displayObject._activeParentLayer) {
outOfMask = false;
}
// it has a mask! Then lets hit test that before continuing..
const mask: Graphics = (displayObject as any)._mask;
if (hitTestOrder < Infinity && mask) {
if (!mask.containsPoint(point)) {
outOfMask = true;
}
}
// it has a filterArea! Same as mask but easier, its a rectangle
if (hitTestOrder < Infinity && displayObject.filterArea) {
if (!displayObject.filterArea.contains(point.x, point.y)) {
outOfMask = true;
}
}
// ** FREE TIP **! If an object is not interactive or has no buttons in it
// (such as a game scene!) set interactiveChildren to false for that displayObject.
// This will allow pixi to completely ignore and bypass checking the displayObjects children.
const children: Array<DisplayObject> = (displayObject as Container).children;
if (displayObject.interactiveChildren && children) {
for (let i = children.length - 1; i >= 0; i--) {
const child = children[i];
// time to get recursive.. if this function will return if something is hit..
const hitChild = this._displayProcessInteractive(point, child, hitTestOrder, interactiveParent, outOfMask);
if (hitChild) {
// its a good idea to check if a child has lost its parent.
// this means it has been removed whilst looping so its best
if (!child.parent) {
continue;
}
hit = hitChild;
hitTestOrder = hitChild;
}
}
}
// no point running this if the item is not interactive or does not have an interactive parent.
if (interactive) {
if (!outOfMask) {
// if we are hit testing (as in we have no hit any objects yet)
// We also don't need to worry about hit testing if once of the displayObjects children has already been hit!
if (hitTestOrder < displayObject.displayOrder) {
//pixi v4
if (displayObject.hitArea) {
displayObject.worldTransform.applyInverse(point, this._tempPoint);
if (displayObject.hitArea.contains(this._tempPoint.x, this._tempPoint.y)) {
hit = displayObject.displayOrder;
}
}
else if ((displayObject as any).containsPoint) {
if ((displayObject as any).containsPoint(point)) {
hit = displayObject.displayOrder;
}
}
}
if (displayObject.interactive) {
this._queueAdd(displayObject, hit === Infinity ? 0 : hit);
}
} else {
if (displayObject.interactive) {
this._queueAdd(displayObject, 0);
}
}
}
return hit;
},
processInteractive: function (strangeStuff: InteractionEvent | Point, displayObject: DisplayObject, func: Function, hitTest: boolean, interactive: boolean) {
//older versions
let interactionEvent: InteractionEvent = null;
let point: Point = null;
if ((strangeStuff as InteractionEvent).data &&
(strangeStuff as InteractionEvent).data.global) {
interactionEvent = strangeStuff as InteractionEvent;
point = interactionEvent.data.global;
} else {
point = strangeStuff as Point;
}
this._startInteractionProcess();
this._displayProcessInteractive(point, displayObject, hitTest ? 0 : Infinity, false);
this._finishInteractionProcess(interactionEvent, func);
},
_startInteractionProcess: function () {
//move it to constructor
this._eventDisplayOrder = 1;
if (!this._queue) {
//move it to constructor
this._queue = [[], []];
}
this._queue[0].length = 0;
this._queue[1].length = 0;
},
_queueAdd: function (displayObject: DisplayObject, order: number) {
let queue = this._queue;
if (order < this._eventDisplayOrder) {
queue[0].push(displayObject);
} else {
if (order > this._eventDisplayOrder) {
this._eventDisplayOrder = order;
let q = queue[1];
for (let i = 0, l = q.length; i < l; i++) {
queue[0].push(q[i]);
}
queue[1].length = 0;
}
queue[1].push(displayObject);
}
},
_finishInteractionProcess: function (event: InteractionEvent, func: Function) {
let queue = this._queue;
let q = queue[0];
for (var i = 0, l = q.length; i < l; i++) {
if (event) {
//v4.3
if (func) {
func(event, q[i], false);
}
} else {
//old
func(q[i], false);
}
}
q = queue[1];
for (var i = 0, l = q.length; i < l; i++) {
if (event) {
//v4.3
if (!event.target) {
event.target = q[i];
}
if (func) {
func(event, q[i], true);
}
} else {
//old
func(q[i], true);
}
}
}
});
}