2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2019-01-15 16:20:22 +00:00
|
|
|
* @copyright 2019 Photon Storm Ltd.
|
2018-02-12 16:01:20 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-11-23 01:45:58 +00:00
|
|
|
var Bounds = require('./lib/geometry/Bounds');
|
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var Composite = require('./lib/body/Composite');
|
|
|
|
var Constraint = require('./lib/constraint/Constraint');
|
|
|
|
var Detector = require('./lib/collision/Detector');
|
2019-01-18 13:41:43 +00:00
|
|
|
var Events = require('./events');
|
2017-11-28 11:16:23 +00:00
|
|
|
var GetFastValue = require('../../utils/object/GetFastValue');
|
2019-01-18 13:41:43 +00:00
|
|
|
var InputEvents = require('../../input/events');
|
2017-11-23 01:45:58 +00:00
|
|
|
var Merge = require('../../utils/object/Merge');
|
|
|
|
var Sleeping = require('./lib/core/Sleeping');
|
2017-11-29 22:23:58 +00:00
|
|
|
var Vector2 = require('../../math/Vector2');
|
2017-11-23 01:45:58 +00:00
|
|
|
var Vertices = require('./lib/geometry/Vertices');
|
|
|
|
|
2018-02-12 13:14:38 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class PointerConstraint
|
2018-10-10 09:49:13 +00:00
|
|
|
* @memberof Phaser.Physics.Matter
|
2018-02-12 13:14:38 +00:00
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Scene} scene - [description]
|
|
|
|
* @param {Phaser.Physics.Matter.World} world - [description]
|
|
|
|
* @param {object} options - [description]
|
|
|
|
*/
|
2017-11-23 01:45:58 +00:00
|
|
|
var PointerConstraint = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function PointerConstraint (scene, world, options)
|
|
|
|
{
|
|
|
|
if (options === undefined) { options = {}; }
|
|
|
|
|
|
|
|
// Defaults
|
|
|
|
var defaults = {
|
|
|
|
label: 'Pointer Constraint',
|
|
|
|
pointA: { x: 0, y: 0 },
|
|
|
|
pointB: { x: 0, y: 0 },
|
|
|
|
damping: 0,
|
|
|
|
length: 0.01,
|
|
|
|
stiffness: 0.1,
|
|
|
|
angularStiffness: 1,
|
|
|
|
collisionFilter: {
|
|
|
|
category: 0x0001,
|
|
|
|
mask: 0xFFFFFFFF,
|
|
|
|
group: 0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-12 13:14:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Matter.PointerConstraint#scene
|
|
|
|
* @type {Phaser.Scene}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-23 01:45:58 +00:00
|
|
|
this.scene = scene;
|
|
|
|
|
2018-02-12 13:14:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Matter.PointerConstraint#world
|
|
|
|
* @type {Phaser.Physics.Matter.World}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-23 01:45:58 +00:00
|
|
|
this.world = world;
|
|
|
|
|
2018-02-12 13:14:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Matter.PointerConstraint#camera
|
|
|
|
* @type {Phaser.Cameras.Scene2D.Camera}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-28 11:16:23 +00:00
|
|
|
var camera = GetFastValue(options, 'camera', null);
|
|
|
|
|
|
|
|
if (!camera)
|
|
|
|
{
|
|
|
|
this.camera = scene.sys.cameras.main;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.camera = camera;
|
|
|
|
|
|
|
|
delete options.camera;
|
|
|
|
}
|
|
|
|
|
2018-02-12 13:14:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Matter.PointerConstraint#pointer
|
|
|
|
* @type {Phaser.Input.Pointer}
|
|
|
|
* @default null
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-23 01:45:58 +00:00
|
|
|
this.pointer = null;
|
|
|
|
|
2018-02-12 13:14:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Matter.PointerConstraint#active
|
|
|
|
* @type {boolean}
|
|
|
|
* @default true
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-23 01:45:58 +00:00
|
|
|
this.active = true;
|
|
|
|
|
2018-02-12 13:14:38 +00:00
|
|
|
/**
|
|
|
|
* The transformed position.
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Matter.PointerConstraint#position
|
|
|
|
* @type {Phaser.Math.Vector2}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-29 22:23:58 +00:00
|
|
|
this.position = new Vector2();
|
|
|
|
|
2018-02-12 13:14:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Matter.PointerConstraint#constraint
|
|
|
|
* @type {object}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-23 01:45:58 +00:00
|
|
|
this.constraint = Constraint.create(Merge(options, defaults));
|
|
|
|
|
2019-01-18 13:41:43 +00:00
|
|
|
this.world.on(Events.BEFORE_UPDATE, this.update, this);
|
2017-11-23 01:45:58 +00:00
|
|
|
|
2019-01-18 13:41:43 +00:00
|
|
|
scene.sys.input.on(InputEvents.POINTER_DOWN, this.onDown, this);
|
|
|
|
scene.sys.input.on(InputEvents.POINTER_UP, this.onUp, this);
|
2017-11-23 01:45:58 +00:00
|
|
|
},
|
|
|
|
|
2018-02-12 13:14:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.PointerConstraint#onDown
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Input.Pointer} pointer - [description]
|
|
|
|
*/
|
|
|
|
onDown: function (pointer)
|
2017-11-23 01:45:58 +00:00
|
|
|
{
|
2018-02-12 13:14:38 +00:00
|
|
|
this.pointer = pointer;
|
2017-11-23 01:45:58 +00:00
|
|
|
},
|
|
|
|
|
2018-02-12 13:14:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.PointerConstraint#onUp
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
onUp: function ()
|
2017-11-23 01:45:58 +00:00
|
|
|
{
|
|
|
|
this.pointer = null;
|
|
|
|
},
|
|
|
|
|
2018-02-12 13:14:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.PointerConstraint#getBodyPart
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-29 13:24:51 +00:00
|
|
|
* @param {MatterJS.Body} body - [description]
|
2018-03-19 00:10:32 +00:00
|
|
|
* @param {Phaser.Math.Vector2} position - [description]
|
2018-02-12 13:14:38 +00:00
|
|
|
*
|
|
|
|
* @return {boolean} [description]
|
|
|
|
*/
|
2017-11-23 01:45:58 +00:00
|
|
|
getBodyPart: function (body, position)
|
|
|
|
{
|
|
|
|
var constraint = this.constraint;
|
|
|
|
|
|
|
|
var start = (body.parts.length > 1) ? 1 : 0;
|
|
|
|
|
|
|
|
for (var i = start; i < body.parts.length; i++)
|
|
|
|
{
|
|
|
|
var part = body.parts[i];
|
|
|
|
|
|
|
|
if (Vertices.contains(part.vertices, position))
|
|
|
|
{
|
|
|
|
constraint.bodyB = body;
|
|
|
|
|
|
|
|
constraint.pointA.x = position.x;
|
|
|
|
constraint.pointA.y = position.y;
|
|
|
|
|
|
|
|
constraint.pointB.x = position.x - body.position.x;
|
|
|
|
constraint.pointB.y = position.y - body.position.y;
|
|
|
|
|
|
|
|
constraint.angleB = body.angle;
|
|
|
|
|
|
|
|
Sleeping.set(body, false);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-03-19 00:10:32 +00:00
|
|
|
|
2017-11-23 01:45:58 +00:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2018-02-12 13:14:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.PointerConstraint#update
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-23 01:45:58 +00:00
|
|
|
update: function ()
|
|
|
|
{
|
|
|
|
if (!this.active)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var pointer = this.pointer;
|
|
|
|
var constraint = this.constraint;
|
|
|
|
|
|
|
|
if (!pointer)
|
|
|
|
{
|
|
|
|
// Pointer is up / released
|
|
|
|
if (constraint.bodyB)
|
|
|
|
{
|
2019-02-08 19:46:23 +00:00
|
|
|
this.world.emit('enddrag', constraint.bodyB, constraint);
|
2017-11-23 01:45:58 +00:00
|
|
|
constraint.bodyB = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-29 22:23:58 +00:00
|
|
|
var pos = this.position;
|
|
|
|
|
2018-01-04 15:21:48 +00:00
|
|
|
this.camera.getWorldPoint(pointer.x, pointer.y, pos);
|
2017-11-23 01:45:58 +00:00
|
|
|
|
|
|
|
if (constraint.bodyB)
|
|
|
|
{
|
|
|
|
// Pointer is down and we have bodyB, so wake it up
|
|
|
|
Sleeping.set(constraint.bodyB, false);
|
|
|
|
|
2017-11-29 22:23:58 +00:00
|
|
|
constraint.pointA.x = pos.x;
|
|
|
|
constraint.pointA.y = pos.y;
|
2019-02-08 19:46:23 +00:00
|
|
|
|
|
|
|
this.world.emit('drag', constraint.bodyB, constraint);
|
2017-11-23 01:45:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var bodies = Composite.allBodies(this.world.localWorld);
|
|
|
|
|
|
|
|
// Pointer is down and no bodyB, so check if we've hit anything
|
|
|
|
for (var i = 0; i < bodies.length; i++)
|
|
|
|
{
|
|
|
|
var body = bodies[i];
|
|
|
|
|
2017-11-29 22:23:58 +00:00
|
|
|
if (!body.ignorePointer && Bounds.contains(body.bounds, pos) &&
|
2017-11-23 01:45:58 +00:00
|
|
|
Detector.canCollide(body.collisionFilter, constraint.collisionFilter))
|
|
|
|
{
|
2017-11-29 22:23:58 +00:00
|
|
|
if (this.getBodyPart(body, pos))
|
2017-11-23 01:45:58 +00:00
|
|
|
{
|
2019-02-08 19:46:23 +00:00
|
|
|
this.world.emit('startdrag', body, constraint);
|
2017-11-23 01:45:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-12 13:14:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.PointerConstraint#destroy
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-23 01:45:58 +00:00
|
|
|
destroy: function ()
|
|
|
|
{
|
2018-02-12 13:36:55 +00:00
|
|
|
this.world.removeConstraint(this.constraint);
|
2017-11-23 01:45:58 +00:00
|
|
|
|
|
|
|
this.constraint = null;
|
|
|
|
|
2019-01-18 13:41:43 +00:00
|
|
|
this.world.off(Events.BEFORE_UPDATE, this.update);
|
2017-11-23 01:45:58 +00:00
|
|
|
|
2019-01-18 13:41:43 +00:00
|
|
|
this.scene.sys.input.off(InputEvents.POINTER_DOWN, this.onDown, this);
|
|
|
|
this.scene.sys.input.off(InputEvents.POINTER_UP, this.onUp, this);
|
2017-11-23 01:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = PointerConstraint;
|