Skip to content

Commit 0d925b9

Browse files
committed
New Interactive Object created.
Pointer events now dispatch more details. Interactive Object used internally everywhere. Hit Test updated to handle it.
1 parent 9a34ce7 commit 0d925b9

10 files changed

Lines changed: 101 additions & 53 deletions

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: 'c34efc00-6b3d-11e7-9a3d-a54976e6b2b6'
2+
build: 'abf1bcd0-6b58-11e7-abf0-e311646824f1'
33
};
44
module.exports = CHECKSUM;

v3/src/input/InteractiveObject.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Phaser.Input.InteractiveObject
2+
3+
var InteractiveObject = function (gameObject, hitArea, hitAreaCallback)
4+
{
5+
return {
6+
gameObject: gameObject,
7+
8+
hitArea: hitArea,
9+
hitAreaCallback: hitAreaCallback,
10+
11+
localX: 0,
12+
localY: 0,
13+
14+
isOver: false,
15+
isDown: false,
16+
17+
isDragged: false
18+
};
19+
};
20+
21+
module.exports = InteractiveObject;

v3/src/input/Pointer.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,13 @@ var Pointer = new Class({
1010
{
1111
this.manager = manager;
1212

13+
this.event;
14+
1315
this.button = 0;
1416

1517
this.x = 0;
1618
this.y = 0;
1719

18-
this.clientX = 0;
19-
this.clientY = 0;
20-
21-
this.pageX = 0;
22-
this.pageY = 0;
23-
24-
this.screenX = 0;
25-
this.screenY = 0;
26-
2720
this.isDown = false;
2821

2922
this.dirty = false;
@@ -48,38 +41,46 @@ var Pointer = new Class({
4841
this.button = event.button;
4942
}
5043

51-
this.clientX = event.clientX;
52-
this.clientY = event.clientY;
53-
54-
this.pageX = event.pageX;
55-
this.pageY = event.pageY;
56-
57-
this.screenX = event.screenX;
58-
this.screenY = event.screenY;
44+
this.event = event;
5945

6046
this.x = this.manager.transformX(event.pageX);
6147
this.y = this.manager.transformY(event.pageY);
6248

6349
this.dirty = true;
50+
6451
this.justMoved = true;
6552
},
6653

6754
down: function (event)
6855
{
69-
this.dirty = true;
56+
if (event.button !== undefined)
57+
{
58+
this.button = event.button;
59+
}
7060

71-
this.x = event.x;
72-
this.y = event.y;
61+
this.event = event;
62+
63+
this.x = this.manager.transformX(event.pageX);
64+
this.y = this.manager.transformY(event.pageY);
65+
66+
this.dirty = true;
7367

7468
this.justDown = true;
7569
},
7670

7771
up: function (event)
7872
{
79-
this.dirty = true;
73+
if (event.button !== undefined)
74+
{
75+
this.button = event.button;
76+
}
77+
78+
this.event = event;
79+
80+
this.x = this.manager.transformX(event.pageX);
81+
this.y = this.manager.transformY(event.pageY);
8082

81-
this.x = event.x;
82-
this.y = event.y;
83+
this.dirty = true;
8384

8485
this.justUp = true;
8586
}

v3/src/input/components/HitTest.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
var GetTransformedPoint = require('./GetTransformedPoint');
22
var PointWithinGameObject = require('./PointWithinGameObject');
3+
var PointWithinInteractiveObject = require('./PointWithinInteractiveObject');
34

45
// Will always return an array.
5-
// Array contains matching Game Objects.
6+
// Array contains matching Interactive Objects.
67
// Array will be empty if no objects were matched.
78

89
var HitTest = function (tempMatrix, x, y, gameObjectArray, camera, output)
@@ -20,17 +21,17 @@ var HitTest = function (tempMatrix, x, y, gameObjectArray, camera, output)
2021
var scrollX = camera.scrollX;
2122
var scrollY = camera.scrollY;
2223
var screenPoint = camera.cameraToScreen({ x: x, y: y });
23-
var culled = camera.cull(gameObjectArray);
24+
var culled = camera.cullHitTest(gameObjectArray);
2425

2526
for (var i = 0; i < culled.length; i++)
2627
{
2728
var object = culled[i];
29+
var gameObject = object.gameObject;
2830

29-
var tpoint = GetTransformedPoint(tempMatrix, object, screenPoint.x + scrollX * object.scrollFactorX, screenPoint.y + scrollY * object.scrollFactorY);
31+
var tpoint = GetTransformedPoint(tempMatrix, gameObject, screenPoint.x + scrollX * gameObject.scrollFactorX, screenPoint.y + scrollY * gameObject.scrollFactorY);
3032

31-
if (PointWithinGameObject(object, tpoint.x, tpoint.y))
33+
if (PointWithinInteractiveObject(object, tpoint.x, tpoint.y))
3234
{
33-
// output.push({ gameObject: object, x: tpoint.x, y: tpoint.y });
3435
output.push(object);
3536
}
3637
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// x/y MUST be translated before being passed to this function, unless the gameObject is guarnateed to
2+
// be not rotated or scaled in any way
3+
4+
var PointWithinInteractiveObject = function (object, x, y)
5+
{
6+
if (!object.hitArea)
7+
{
8+
return false;
9+
}
10+
11+
// Normalize the origin
12+
x += object.gameObject.displayOriginX;
13+
y += object.gameObject.displayOriginY;
14+
15+
object.localX = x;
16+
object.localY = y;
17+
18+
return object.hitAreaCallback(object.hitArea, x, y, object);
19+
};
20+
21+
module.exports = PointWithinInteractiveObject;

v3/src/input/events/PointerDownEvent.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ var PointerDownEvent = new Class({
77

88
initialize:
99

10-
function PointerDownEvent (pointer, gameObject)
10+
function PointerDownEvent (pointer, interactiveObject)
1111
{
1212
Event.call(this, 'POINTER_DOWN_EVENT');
1313

1414
this.pointer = pointer;
15-
this.gameObject = gameObject;
15+
this.gameObject = interactiveObject.gameObject;
1616

17-
// this.x;
18-
// this.y;
17+
this.x = interactiveObject.localX;
18+
this.y = interactiveObject.localY;
1919
}
2020

2121
});

v3/src/input/events/PointerOutEvent.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ var PointerOutEvent = new Class({
77

88
initialize:
99

10-
function PointerOutEvent (pointer, gameObject)
10+
function PointerOutEvent (pointer, interactiveObject)
1111
{
1212
Event.call(this, 'POINTER_OUT_EVENT');
1313

1414
this.pointer = pointer;
15-
this.gameObject = gameObject;
15+
this.gameObject = interactiveObject.gameObject;
1616

17-
// this.x;
18-
// this.y;
17+
this.x = interactiveObject.localX;
18+
this.y = interactiveObject.localY;
1919
}
2020

2121
});

v3/src/input/events/PointerOverEvent.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ var PointerOverEvent = new Class({
77

88
initialize:
99

10-
function PointerOverEvent (pointer, gameObject)
10+
function PointerOverEvent (pointer, interactiveObject)
1111
{
1212
Event.call(this, 'POINTER_OVER_EVENT');
1313

1414
this.pointer = pointer;
15-
this.gameObject = gameObject;
15+
this.gameObject = interactiveObject.gameObject;
1616

17-
// this.x;
18-
// this.y;
17+
this.x = interactiveObject.localX;
18+
this.y = interactiveObject.localY;
1919
}
2020

2121
});

v3/src/input/events/PointerUpEvent.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ var PointerUpEvent = new Class({
77

88
initialize:
99

10-
function PointerUpEvent (pointer, gameObject)
10+
function PointerUpEvent (pointer, interactiveObject)
1111
{
1212
Event.call(this, 'POINTER_UP_EVENT');
1313

1414
this.pointer = pointer;
15-
this.gameObject = gameObject;
15+
this.gameObject = interactiveObject.gameObject;
1616

17-
// this.x;
18-
// this.y;
17+
this.x = interactiveObject.localX;
18+
this.y = interactiveObject.localY;
1919
}
2020

2121
});

v3/src/plugins/InputManager.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
var Class = require('../utils/Class');
2-
var InputEvent = require('../input/events');
3-
var Rectangle = require('../geom/rectangle/Rectangle');
4-
var RectangleContains = require('../geom/rectangle/Contains');
51
var Circle = require('../geom/circle/Circle');
62
var CircleContains = require('../geom/circle/Contains');
3+
var Class = require('../utils/Class');
74
var Ellipse = require('../geom/ellipse/Ellipse');
85
var EllipseContains = require('../geom/ellipse/Contains');
6+
var InputEvent = require('../input/events');
7+
var InteractiveObject = require('../input/InteractiveObject');
8+
var Rectangle = require('../geom/rectangle/Rectangle');
9+
var RectangleContains = require('../geom/rectangle/Contains');
910
var Triangle = require('../geom/triangle/Triangle');
1011
var TriangleContains = require('../geom/triangle/Contains');
1112

@@ -40,7 +41,7 @@ var InputManager = new Class({
4041

4142
this._size = 0;
4243

43-
// All interactive objects
44+
// All list of all Interactive Objects
4445
this._list = [];
4546

4647
// Only those which are currently below a pointer (any pointer)
@@ -234,6 +235,8 @@ var InputManager = new Class({
234235
}
235236
},
236237

238+
// Adds a new InteractiveObject to this Input Manager.
239+
// Created automatically via methods like setHitArea, or can be created manually.
237240
add: function (child)
238241
{
239242
if (this._pendingInsertion.indexOf(child) === -1 && this._list.indexOf(child) === -1)
@@ -244,6 +247,7 @@ var InputManager = new Class({
244247
return this;
245248
},
246249

250+
// Removes an InteractiveObject from this Input Manager.
247251
remove: function (child)
248252
{
249253
this._pendingRemoval.push(child);
@@ -265,15 +269,15 @@ var InputManager = new Class({
265269
gameObject[i].hitArea = shape;
266270
gameObject[i].hitAreaCallback = callback;
267271

268-
this.add(gameObject[i]);
272+
this.add(InteractiveObject(gameObject[i], shape, callback));
269273
}
270274
}
271275
else
272276
{
273277
gameObject.hitArea = shape;
274278
gameObject.hitAreaCallback = callback;
275279

276-
this.add(gameObject);
280+
this.add(InteractiveObject(gameObject, shape, callback));
277281
}
278282

279283
return this;
@@ -297,7 +301,7 @@ var InputManager = new Class({
297301
entity.hitArea = new Rectangle(0, 0, entity.frame.width, entity.frame.height);
298302
entity.hitAreaCallback = callback;
299303

300-
this.add(entity);
304+
this.add(InteractiveObject(entity, entity.hitArea, callback));
301305
}
302306
}
303307

0 commit comments

Comments
 (0)