Skip to content

Commit 7041601

Browse files
committed
Added drag events to Pointer Constraint
1 parent bb83f48 commit 7041601

6 files changed

Lines changed: 82 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44

55
A small point release to fix a couple of important issues that slipped into 3.16.
66

7+
### New
8+
9+
* There is a new Matter Physics Event `DRAG_START` which is emitted by a Pointer Constraint when it starts dragging a body. Listen for this event fro the Matter World instance.
10+
* There is a new Matter Physics Event `DRAG` which is emitted by a Pointer Constraint as it drags a body. Listen for this event fro the Matter World instance.
11+
* There is a new Matter Physics Event `DRAG_END` which is emitted by a Pointer Constraint when it stops dragging a body. Listen for this event fro the Matter World instance.
12+
713
### Updates
814

915
* `TileSprite.setTileScale` has been updated so that the `y` argument is optional and set to match the `x` argument, like `setScale` elsewhere in the API.
1016
* `InputManager.time` is a new property that holds the most recent time it was updated from the Game step, which plugins can access.
1117
* `InputManager.preStep` is a new method that populates some internal properties every step.
1218
* `KeyboardPlugin.time` has moved from being a property to being a getter, which returns the time from the InputManager.
1319
* The `scale` property has been added to the `Scene` class (thanks @strangeweekend)
20+
* `Matter.World.remove` now uses the `Composite.remove` method internally. Previously, it used `Composite.removeBody` which only allowed it to remove bodies from the simulation. Now, it can remove any type of Matter object.
21+
* `Matter.PointerConstraint.camera` can no longer be set in the config object. Instead it is set every time the Pointer is pressed down on a Body, this resolves issues where you have a multi-camera Scene and want to drag a body in the non-main camera.
1422

1523
### Bug Fixes
1624

src/physics/matter-js/PointerConstraint.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,14 @@ var PointerConstraint = new Class({
7373
this.world = world;
7474

7575
/**
76-
* [description]
76+
* The Camera the Pointer was interacting with when the input
77+
* down event was processed.
7778
*
7879
* @name Phaser.Physics.Matter.PointerConstraint#camera
7980
* @type {Phaser.Cameras.Scene2D.Camera}
8081
* @since 3.0.0
8182
*/
82-
var camera = GetFastValue(options, 'camera', null);
83-
84-
if (!camera)
85-
{
86-
this.camera = scene.sys.cameras.main;
87-
}
88-
else
89-
{
90-
this.camera = camera;
91-
92-
delete options.camera;
93-
}
83+
this.camera = null;
9484

9585
/**
9686
* [description]
@@ -147,6 +137,8 @@ var PointerConstraint = new Class({
147137
onDown: function (pointer)
148138
{
149139
this.pointer = pointer;
140+
141+
this.camera = pointer.camera;
150142
},
151143

152144
/**
@@ -158,6 +150,8 @@ var PointerConstraint = new Class({
158150
onUp: function ()
159151
{
160152
this.pointer = null;
153+
154+
this.camera = pointer.camera;
161155
},
162156

163157
/**
@@ -223,7 +217,8 @@ var PointerConstraint = new Class({
223217
// Pointer is up / released
224218
if (constraint.bodyB)
225219
{
226-
this.world.emit('enddrag', constraint.bodyB, constraint);
220+
this.world.emit(Events.DRAG_END, constraint.bodyB, this);
221+
227222
constraint.bodyB = null;
228223
}
229224
}
@@ -241,7 +236,7 @@ var PointerConstraint = new Class({
241236
constraint.pointA.x = pos.x;
242237
constraint.pointA.y = pos.y;
243238

244-
this.world.emit('drag', constraint.bodyB, constraint);
239+
this.world.emit(Events.DRAG, constraint.bodyB, this);
245240
}
246241
else
247242
{
@@ -257,7 +252,7 @@ var PointerConstraint = new Class({
257252
{
258253
if (this.getBodyPart(body, pos))
259254
{
260-
this.world.emit('startdrag', body, constraint);
255+
this.world.emit(Events.DRAG_START, body, this);
261256
break;
262257
}
263258
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Matter Physics Drag End Event.
9+
*
10+
* This event is dispatched by a Matter Physics World instance when a Pointer Constraint
11+
* stops dragging a body.
12+
*
13+
* Listen to it from a Scene using: `this.matter.world.on('dragend', listener)`.
14+
*
15+
* @event Phaser.Physics.Matter.Events#DRAG_END
16+
*
17+
* @param {MatterJS.Body} body - The Body that has stopped being dragged. This is a Matter Body, not a Phaser Game Object.
18+
* @param {Phaser.Physics.Matter.PointerConstraint} constraint - The Pointer Constraint that was dragging the body.
19+
*/
20+
module.exports = 'dragend';
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Matter Physics Drag Event.
9+
*
10+
* This event is dispatched by a Matter Physics World instance when a Pointer Constraint
11+
* is actively dragging a body. It is emitted each time the pointer moves.
12+
*
13+
* Listen to it from a Scene using: `this.matter.world.on('drag', listener)`.
14+
*
15+
* @event Phaser.Physics.Matter.Events#DRAG
16+
*
17+
* @param {MatterJS.Body} body - The Body that is being dragged. This is a Matter Body, not a Phaser Game Object.
18+
* @param {Phaser.Physics.Matter.PointerConstraint} constraint - The Pointer Constraint that is dragging the body.
19+
*/
20+
module.exports = 'drag';
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Matter Physics Drag Start Event.
9+
*
10+
* This event is dispatched by a Matter Physics World instance when a Pointer Constraint
11+
* starts dragging a body.
12+
*
13+
* Listen to it from a Scene using: `this.matter.world.on('dragstart', listener)`.
14+
*
15+
* @event Phaser.Physics.Matter.Events#DRAG_START
16+
*
17+
* @param {MatterJS.Body} body - The Body that has started being dragged. This is a Matter Body, not a Phaser Game Object.
18+
* @param {Phaser.Physics.Matter.PointerConstraint} constraint - The Pointer Constraint that is dragging the body.
19+
*/
20+
module.exports = 'dragstart';

src/physics/matter-js/events/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ module.exports = {
1515
COLLISION_ACTIVE: require('./COLLISION_ACTIVE_EVENT'),
1616
COLLISION_END: require('./COLLISION_END_EVENT'),
1717
COLLISION_START: require('./COLLISION_START_EVENT'),
18+
DRAG_END: require('./DRAG_END_EVENT'),
19+
DRAG: require('./DRAG_EVENT'),
20+
DRAG_START: require('./DRAG_START_EVENT'),
1821
PAUSE: require('./PAUSE_EVENT'),
1922
RESUME: require('./RESUME_EVENT'),
2023
SLEEP_END: require('./SLEEP_END_EVENT'),

0 commit comments

Comments
 (0)