Skip to content

Commit 6ff21af

Browse files
committed
Added start of controls
1 parent fe17dfc commit 6ff21af

1 file changed

Lines changed: 130 additions & 1 deletion

File tree

src/gameobjects/layer3d/Layer3DCamera.js

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
var Class = require('../../utils/Class');
88
var DegToRad = require('../../math/DegToRad');
9+
var GetFastValue = require('../../utils/object/GetFastValue');
10+
var INPUT_EVENTS = require('../../input/events');
911
var Matrix4 = require('../../math/Matrix4');
1012
var Vector3 = require('../../math/Vector3');
1113
var Vector4 = require('../../math/Vector4');
@@ -23,8 +25,31 @@ var Layer3DCamera = new Class({
2325

2426
initialize:
2527

26-
function Layer3DCamera (fov, x, y, z, near, far)
28+
function Layer3DCamera (layer, fov, x, y, z, near, far)
2729
{
30+
/**
31+
* The Layer3D instance this camera belongs to.
32+
*
33+
* A camera can only belong to a single Layer3D instance.
34+
*
35+
* You should consider this property as being read-only. You cannot move a
36+
* camera to another Layer3D by simply changing it.
37+
*
38+
* @name Phaser.GameObjects.Layer3DCamera#layer
39+
* @type {Phaser.GameObjects.Layer3D}
40+
* @since 3.50.0
41+
*/
42+
this.layer = layer;
43+
44+
/**
45+
* The Scene Input Plugin, as referenced via the Layer3D parent.
46+
*
47+
* @name Phaser.GameObjects.Layer3DCamera#input
48+
* @type {Phaser.Input.InputPlugin}
49+
* @since 3.50.0
50+
*/
51+
this.input = layer.scene.sys.input;
52+
2853
/**
2954
* Internal 'dirty' flag that tells the parent Layer3D if the
3055
* view matrix of this camera needs recalculating at the next step.
@@ -196,6 +221,109 @@ var Layer3DCamera = new Class({
196221
* @since 3.50.0
197222
*/
198223
this.mode = Layer3DCamera.MODE_ORBIT;
224+
225+
/**
226+
* How fast to rotate the camera, in degrees per delta.
227+
*
228+
* This value is only used after calling the `enableControls` method,
229+
* it does not influence changing the rotation values directly.
230+
*
231+
* @name Phaser.GameObjects.Layer3DCamera#rotateSpeed
232+
* @type {number}
233+
* @since 3.50.0
234+
*/
235+
this.rotateSpeed = 0.5;
236+
237+
/**
238+
* How fast to pan the camera, in units per delta.
239+
*
240+
* This value is only used after calling the `enableControls` method,
241+
* it does not influence calling the pan methods directly.
242+
*
243+
* @name Phaser.GameObjects.Layer3DCamera#panSpeed
244+
* @type {number}
245+
* @since 3.50.0
246+
*/
247+
this.panSpeed = 4;
248+
249+
/**
250+
* How fast to zoom the camera.
251+
*
252+
* This value is only used after calling the `enableControls` method,
253+
* it does not influence calling the panZ method directly.
254+
*
255+
* @name Phaser.GameObjects.Layer3DCamera#zoomSpeed
256+
* @type {number}
257+
* @since 3.50.0
258+
*/
259+
this.zoomSpeed = 3;
260+
261+
this.allowPan = false;
262+
263+
this.lockXAxis = false;
264+
this.lockYAxis = false;
265+
},
266+
267+
enableOrbitControls: function (config)
268+
{
269+
this.rotateSpeed = GetFastValue(config, 'rotateSpeed', this.rotateSpeed);
270+
this.panSpeed = GetFastValue(config, 'panSpeed', this.panSpeed);
271+
this.allowPan = GetFastValue(config, 'allowPan', this.allowPan);
272+
this.lockXAxis = GetFastValue(config, 'lockXAxis', this.lockXAxis);
273+
this.lockYAxis = GetFastValue(config, 'lockYAxis', this.lockYAxis);
274+
275+
this.input.on(INPUT_EVENTS.POINTER_MOVE, this.pointerMoveHandler, this);
276+
},
277+
278+
disableOrbitControls: function ()
279+
{
280+
this.input.off(INPUT_EVENTS.POINTER_MOVE, this.pointerMoveHandler, this);
281+
},
282+
283+
enableZoom: function (zoomSpeed)
284+
{
285+
if (zoomSpeed === undefined) { zoomSpeed = 3; }
286+
287+
this.zoomSpeed = zoomSpeed;
288+
289+
this.input.on(INPUT_EVENTS.POINTER_WHEEL, this.pointerWheelHandler, this);
290+
},
291+
292+
disableZoom: function ()
293+
{
294+
this.input.off(INPUT_EVENTS.POINTER_WHEEL, this.pointerWheelHandler, this);
295+
},
296+
297+
pointerMoveHandler: function (pointer)
298+
{
299+
if (pointer.isDown)
300+
{
301+
var width = this.layer.width;
302+
var height = this.layer.height;
303+
304+
if (pointer.event.shiftKey && this.allowPan)
305+
{
306+
this.panX(pointer.velocity.x * (this.panSpeed / width));
307+
this.panY(pointer.velocity.y * (this.panSpeed / height));
308+
}
309+
else
310+
{
311+
if (!this.lockXAxis)
312+
{
313+
this.rotationX -= pointer.velocity.y * (this.rotateSpeed / height);
314+
}
315+
316+
if (!this.lockYAxis)
317+
{
318+
this.rotationY -= pointer.velocity.x * (this.rotateSpeed / width);
319+
}
320+
}
321+
}
322+
},
323+
324+
pointerWheelHandler: function (pointer, over, deltaX, deltaY)
325+
{
326+
this.panZ(deltaY * (this.zoomSpeed / this.layer.height));
199327
},
200328

201329
/**
@@ -565,6 +693,7 @@ var Layer3DCamera = new Class({
565693
*/
566694
destroy: function ()
567695
{
696+
this.layer = null;
568697
this.position = null;
569698
this.rotation = null;
570699
this.forward = null;

0 commit comments

Comments
 (0)