Skip to content

Commit f9f17ad

Browse files
committed
Multi point input handler working
1 parent 49fe5ee commit f9f17ad

11 files changed

Lines changed: 1239 additions & 647 deletions

File tree

Phaser/components/sprite/Input.ts

Lines changed: 266 additions & 128 deletions
Large diffs are not rendered by default.

Phaser/gameobjects/Sprite.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,6 @@ module Phaser {
303303
}
304304
*/
305305

306-
this.input.update();
307-
308306
if (this.modified == true && this.scale.equals(1) && this.skew.equals(0) && this.angle == 0 && this.angleOffset == 0 && this.texture.flippedX == false && this.texture.flippedY == false)
309307
{
310308
this.modified = false;

Phaser/input/Input.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,14 +446,36 @@ module Phaser {
446446
this.mspointer.start();
447447
this.gestures.start();
448448

449+
this.mousePointer.active = true;
450+
451+
}
452+
453+
public inputObjects = [];
454+
public totalTrackedObjects: number = 0;
455+
456+
// Add Input Enabled array + add/remove methods and then iterate and update them during the main update
457+
// Clear down this array on State swap??? Maybe removed from it when Sprite is destroyed
458+
459+
public addGameObject(object) {
460+
461+
// Lots more checks here
462+
this.inputObjects.push(object);
463+
this.totalTrackedObjects++;
464+
}
465+
466+
public removeGameObject(object) {
467+
// TODO
449468
}
450469

470+
471+
451472
/**
452473
* Updates the Input Manager. Called by the core Game loop.
453474
* @method update
454475
**/
455476
public update() {
456477

478+
// Swap for velocity vector - and add it to Pointer?
457479
this.speed.x = this.position.x - this._oldPosition.x;
458480
this.speed.y = this.position.y - this._oldPosition.y;
459481

Phaser/input/Mouse.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ module Phaser {
2424
**/
2525
private _game: Game;
2626

27-
2827
public static LEFT_BUTTON: number = 0;
2928
public static MIDDLE_BUTTON: number = 1;
3029
public static RIGHT_BUTTON: number = 2;

Phaser/input/Pointer.ts

Lines changed: 104 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Phaser {
1818
*/
1919
constructor(game: Game, id: number) {
2020

21-
this._game = game;
21+
this.game = game;
2222

2323
this.id = id;
2424
this.active = false;
@@ -35,11 +35,11 @@ module Phaser {
3535

3636
/**
3737
* Local private reference to game.
38-
* @property _game
38+
* @property game
3939
* @type {Phaser.Game}
4040
* @private
4141
**/
42-
private _game: Game;
42+
public game: Game;
4343

4444
/**
4545
* Local private variable to store the status of dispatching a hold event
@@ -248,15 +248,24 @@ module Phaser {
248248
return -1;
249249
}
250250

251-
return this._game.time.now - this.timeDown;
251+
return this.game.time.now - this.timeDown;
252252

253253
}
254254

255+
// Sprite Drag Related
256+
257+
/**
258+
* The Game Object this Pointer is currently dragging.
259+
* @property draggedObject
260+
* @type {Any}
261+
**/
262+
public draggedObject;
263+
255264
/**
256265
* Gets the X value of this Pointer in world coordinate space
257266
* @param {Camera} [camera]
258267
*/
259-
public getWorldX(camera?: Camera = this._game.camera) {
268+
public getWorldX(camera?: Camera = this.game.camera) {
260269

261270
return camera.worldView.x + this.x;
262271

@@ -266,7 +275,7 @@ module Phaser {
266275
* Gets the Y value of this Pointer in world coordinate space
267276
* @param {Camera} [camera]
268277
*/
269-
public getWorldY(camera?: Camera = this._game.camera) {
278+
public getWorldY(camera?: Camera = this.game.camera) {
270279

271280
return camera.worldView.y + this.y;
272281

@@ -288,9 +297,9 @@ module Phaser {
288297
}
289298

290299
// Fix to stop rogue browser plugins from blocking the visibility state event
291-
if (this._game.paused == true)
300+
if (this.game.paused == true)
292301
{
293-
this._game.stage.resumeGame();
302+
this.game.stage.resumeGame();
294303
return this;
295304
}
296305

@@ -304,21 +313,21 @@ module Phaser {
304313
this.withinGame = true;
305314
this.isDown = true;
306315
this.isUp = false;
307-
this.timeDown = this._game.time.now;
316+
this.timeDown = this.game.time.now;
308317
this._holdSent = false;
309318

310-
if (this._game.input.multiInputOverride == Input.MOUSE_OVERRIDES_TOUCH || this._game.input.multiInputOverride == Input.MOUSE_TOUCH_COMBINE || (this._game.input.multiInputOverride == Input.TOUCH_OVERRIDES_MOUSE && this._game.input.currentPointers == 0))
319+
if (this.game.input.multiInputOverride == Input.MOUSE_OVERRIDES_TOUCH || this.game.input.multiInputOverride == Input.MOUSE_TOUCH_COMBINE || (this.game.input.multiInputOverride == Input.TOUCH_OVERRIDES_MOUSE && this.game.input.currentPointers == 0))
311320
{
312-
this._game.input.x = this.x * this._game.input.scaleX;
313-
this._game.input.y = this.y * this._game.input.scaleY;
314-
this._game.input.onDown.dispatch(this);
321+
this.game.input.x = this.x * this.game.input.scaleX;
322+
this.game.input.y = this.y * this.game.input.scaleY;
323+
this.game.input.onDown.dispatch(this);
315324
}
316325

317326
this.totalTouches++;
318327

319328
if (this.isMouse == false)
320329
{
321-
this._game.input.currentPointers++;
330+
this.game.input.currentPointers++;
322331
}
323332

324333
return this;
@@ -329,28 +338,58 @@ module Phaser {
329338

330339
if (this.active)
331340
{
332-
if (this._holdSent == false && this.duration >= this._game.input.holdRate)
341+
if (this._holdSent == false && this.duration >= this.game.input.holdRate)
333342
{
334-
if (this._game.input.multiInputOverride == Input.MOUSE_OVERRIDES_TOUCH || this._game.input.multiInputOverride == Input.MOUSE_TOUCH_COMBINE || (this._game.input.multiInputOverride == Input.TOUCH_OVERRIDES_MOUSE && this._game.input.currentPointers == 0))
343+
if (this.game.input.multiInputOverride == Input.MOUSE_OVERRIDES_TOUCH || this.game.input.multiInputOverride == Input.MOUSE_TOUCH_COMBINE || (this.game.input.multiInputOverride == Input.TOUCH_OVERRIDES_MOUSE && this.game.input.currentPointers == 0))
335344
{
336-
this._game.input.onHold.dispatch(this);
345+
this.game.input.onHold.dispatch(this);
337346
}
338347

339348
this._holdSent = true;
340349
}
341350

342351
// Update the droppings history
343-
if (this._game.input.recordPointerHistory && this._game.time.now >= this._nextDrop)
352+
if (this.game.input.recordPointerHistory && this.game.time.now >= this._nextDrop)
344353
{
345-
this._nextDrop = this._game.time.now + this._game.input.recordRate;
354+
this._nextDrop = this.game.time.now + this.game.input.recordRate;
346355
this._history.push({ x: this.position.x, y: this.position.y });
347356

348-
if (this._history.length > this._game.input.recordLimit)
357+
if (this._history.length > this.game.input.recordLimit)
349358
{
350359
this._history.shift();
351360
}
352361
}
353362

363+
// Iterate through the tracked objects
364+
365+
// Build our temporary click stack
366+
var _highestPriority = 0;
367+
368+
for (var i = 0; i < this.game.input.totalTrackedObjects; i++)
369+
{
370+
if (this.game.input.inputObjects[i].input.enabled)
371+
{
372+
this.game.input.inputObjects[i].input.update(this);
373+
374+
if (this.game.input.inputObjects[i].input.priorityID > _highestPriority)
375+
{
376+
_highestPriority = this.game.input.inputObjects[i].input.priorityID;
377+
}
378+
}
379+
}
380+
381+
if (this.isDown)
382+
{
383+
// Now update all objects with the highest priority ID (can be more than 1)
384+
for (var i = 0; i < this.game.input.totalTrackedObjects; i++)
385+
{
386+
if (this.game.input.inputObjects[i].input.priorityID == _highestPriority)
387+
{
388+
this.game.input.inputObjects[i].input._touchedHandler(this);
389+
}
390+
}
391+
}
392+
354393
}
355394

356395
}
@@ -374,20 +413,20 @@ module Phaser {
374413
this.screenX = event.screenX;
375414
this.screenY = event.screenY;
376415

377-
this.x = this.pageX - this._game.stage.offset.x;
378-
this.y = this.pageY - this._game.stage.offset.y;
416+
this.x = this.pageX - this.game.stage.offset.x;
417+
this.y = this.pageY - this.game.stage.offset.y;
379418

380419
this.position.setTo(this.x, this.y);
381420
this.circle.x = this.x;
382421
this.circle.y = this.y;
383422

384-
if (this._game.input.multiInputOverride == Input.MOUSE_OVERRIDES_TOUCH || this._game.input.multiInputOverride == Input.MOUSE_TOUCH_COMBINE || (this._game.input.multiInputOverride == Input.TOUCH_OVERRIDES_MOUSE && this._game.input.currentPointers == 0))
423+
if (this.game.input.multiInputOverride == Input.MOUSE_OVERRIDES_TOUCH || this.game.input.multiInputOverride == Input.MOUSE_TOUCH_COMBINE || (this.game.input.multiInputOverride == Input.TOUCH_OVERRIDES_MOUSE && this.game.input.currentPointers == 0))
385424
{
386-
this._game.input.x = this.x * this._game.input.scaleX;
387-
this._game.input.y = this.y * this._game.input.scaleY;
388-
this._game.input.position.setTo(this._game.input.x, this._game.input.y);
389-
this._game.input.circle.x = this._game.input.x;
390-
this._game.input.circle.y = this._game.input.y;
425+
this.game.input.x = this.x * this.game.input.scaleX;
426+
this.game.input.y = this.y * this.game.input.scaleY;
427+
this.game.input.position.setTo(this.game.input.x, this.game.input.y);
428+
this.game.input.circle.x = this.game.input.x;
429+
this.game.input.circle.y = this.game.input.y;
391430
}
392431

393432
return this;
@@ -413,40 +452,45 @@ module Phaser {
413452
*/
414453
public stop(event): Pointer {
415454

416-
this.timeUp = this._game.time.now;
455+
this.timeUp = this.game.time.now;
417456

418-
if (this._game.input.multiInputOverride == Input.MOUSE_OVERRIDES_TOUCH || this._game.input.multiInputOverride == Input.MOUSE_TOUCH_COMBINE || (this._game.input.multiInputOverride == Input.TOUCH_OVERRIDES_MOUSE && this._game.input.currentPointers == 0))
457+
if (this.game.input.multiInputOverride == Input.MOUSE_OVERRIDES_TOUCH || this.game.input.multiInputOverride == Input.MOUSE_TOUCH_COMBINE || (this.game.input.multiInputOverride == Input.TOUCH_OVERRIDES_MOUSE && this.game.input.currentPointers == 0))
419458
{
420-
this._game.input.onUp.dispatch(this);
459+
this.game.input.onUp.dispatch(this);
421460

422461
// Was it a tap?
423-
if (this.duration >= 0 && this.duration <= this._game.input.tapRate)
462+
if (this.duration >= 0 && this.duration <= this.game.input.tapRate)
424463
{
425464
// Was it a double-tap?
426-
if (this.timeUp - this.previousTapTime < this._game.input.doubleTapRate)
465+
if (this.timeUp - this.previousTapTime < this.game.input.doubleTapRate)
427466
{
428467
// Yes, let's dispatch the signal then with the 2nd parameter set to true
429-
this._game.input.onTap.dispatch(this, true);
468+
this.game.input.onTap.dispatch(this, true);
430469
}
431470
else
432471
{
433472
// Wasn't a double-tap, so dispatch a single tap signal
434-
this._game.input.onTap.dispatch(this, false);
473+
this.game.input.onTap.dispatch(this, false);
435474
}
436475

437476
this.previousTapTime = this.timeUp;
438477
}
439478

440479
}
441480

442-
this.active = false;
481+
// Mouse is always active
482+
if (this.id > 0)
483+
{
484+
this.active = false;
485+
}
486+
443487
this.withinGame = false;
444488
this.isDown = false;
445489
this.isUp = true;
446490

447491
if (this.isMouse == false)
448492
{
449-
this._game.input.currentPointers--;
493+
this.game.input.currentPointers--;
450494
}
451495

452496
return this;
@@ -459,9 +503,9 @@ module Phaser {
459503
* @param {Number} [duration].
460504
* @return {Boolean}
461505
*/
462-
public justPressed(duration?: number = this._game.input.justPressedRate): bool {
506+
public justPressed(duration?: number = this.game.input.justPressedRate): bool {
463507

464-
if (this.isDown === true && (this.timeDown + duration) > this._game.time.now)
508+
if (this.isDown === true && (this.timeDown + duration) > this.game.time.now)
465509
{
466510
return true;
467511
}
@@ -478,9 +522,9 @@ module Phaser {
478522
* @param {Number} [duration].
479523
* @return {Boolean}
480524
*/
481-
public justReleased(duration?: number = this._game.input.justReleasedRate): bool {
525+
public justReleased(duration?: number = this.game.input.justReleasedRate): bool {
482526

483-
if (this.isUp === true && (this.timeUp + duration) > this._game.time.now)
527+
if (this.isUp === true && (this.timeUp + duration) > this.game.time.now)
484528
{
485529
return true;
486530
}
@@ -518,37 +562,37 @@ module Phaser {
518562
return;
519563
}
520564

521-
this._game.stage.context.beginPath();
522-
this._game.stage.context.arc(this.x, this.y, this.circle.radius, 0, Math.PI * 2);
565+
this.game.stage.context.beginPath();
566+
this.game.stage.context.arc(this.x, this.y, this.circle.radius, 0, Math.PI * 2);
523567

524568
if (this.active)
525569
{
526-
this._game.stage.context.fillStyle = 'rgba(0,255,0,0.5)';
527-
this._game.stage.context.strokeStyle = 'rgb(0,255,0)';
570+
this.game.stage.context.fillStyle = 'rgba(0,255,0,0.5)';
571+
this.game.stage.context.strokeStyle = 'rgb(0,255,0)';
528572
}
529573
else
530574
{
531-
this._game.stage.context.fillStyle = 'rgba(255,0,0,0.5)';
532-
this._game.stage.context.strokeStyle = 'rgb(100,0,0)';
575+
this.game.stage.context.fillStyle = 'rgba(255,0,0,0.5)';
576+
this.game.stage.context.strokeStyle = 'rgb(100,0,0)';
533577
}
534578

535-
this._game.stage.context.fill();
536-
this._game.stage.context.closePath();
579+
this.game.stage.context.fill();
580+
this.game.stage.context.closePath();
537581

538582
// Render the points
539-
this._game.stage.context.beginPath();
540-
this._game.stage.context.moveTo(this.positionDown.x, this.positionDown.y);
541-
this._game.stage.context.lineTo(this.position.x, this.position.y);
542-
this._game.stage.context.lineWidth = 2;
543-
this._game.stage.context.stroke();
544-
this._game.stage.context.closePath();
583+
this.game.stage.context.beginPath();
584+
this.game.stage.context.moveTo(this.positionDown.x, this.positionDown.y);
585+
this.game.stage.context.lineTo(this.position.x, this.position.y);
586+
this.game.stage.context.lineWidth = 2;
587+
this.game.stage.context.stroke();
588+
this.game.stage.context.closePath();
545589

546590
// Render the text
547-
this._game.stage.context.fillStyle = 'rgb(255,255,255)';
548-
this._game.stage.context.font = 'Arial 16px';
549-
this._game.stage.context.fillText('ID: ' + this.id + " Active: " + this.active, this.x, this.y - 100);
550-
this._game.stage.context.fillText('Screen X: ' + this.x + " Screen Y: " + this.y, this.x, this.y - 80);
551-
this._game.stage.context.fillText('Duration: ' + this.duration + " ms", this.x, this.y - 60);
591+
this.game.stage.context.fillStyle = 'rgb(255,255,255)';
592+
this.game.stage.context.font = 'Arial 16px';
593+
this.game.stage.context.fillText('ID: ' + this.id + " Active: " + this.active, this.x, this.y - 100);
594+
this.game.stage.context.fillText('Screen X: ' + this.x + " Screen Y: " + this.y, this.x, this.y - 80);
595+
this.game.stage.context.fillText('Duration: ' + this.duration + " ms", this.x, this.y - 60);
552596

553597
}
554598

0 commit comments

Comments
 (0)