Skip to content

Commit ebda1f9

Browse files
committed
Input Handler 90% there.
1 parent abe344b commit ebda1f9

10 files changed

Lines changed: 183 additions & 65 deletions

File tree

examples/js.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<script src="../src/input/Touch.js"></script>
6363
<script src="../src/input/InputHandler.js"></script>
6464
<script src="../src/system/Canvas.js"></script>
65+
<script src="../src/gameobjects/Events.js"></script>
6566
<script src="../src/gameobjects/GameObjectFactory.js"></script>
6667
<script src="../src/gameobjects/Sprite.js"></script>
6768
<script src="../src/gameobjects/TileSprite.js"></script>

examples/linkedlist1.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ function create() {
4646
list = new Phaser.LinkedList();
4747

4848
list.add(a.input);
49-
// list.add(b.input);
50-
// list.add(c.input);
51-
// list.add(d.input);
52-
// list.add(e.input);
53-
// list.add(f.input);
49+
list.add(b.input);
50+
list.add(c.input);
51+
list.add(d.input);
52+
list.add(e.input);
53+
list.add(f.input);
5454

5555
list.dump();
5656

examples/touch1.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>phaser.js - a new beginning</title>
5+
<?php
6+
require('js.php');
7+
?>
8+
</head>
9+
<body>
10+
11+
<script type="text/javascript">
12+
13+
(function () {
14+
15+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
16+
17+
function preload() {
18+
game.load.image('ship', 'assets/sprites/mana_card.png');
19+
}
20+
21+
var a;
22+
var p = new Phaser.Point;
23+
24+
function create() {
25+
26+
a = game.add.sprite(game.world.centerX, game.world.centerX, 'ship');
27+
a.name = 'sh';
28+
a.inputEnabled = true;
29+
a.angle = 30;
30+
31+
a.events.onInputDown.add(clicked, this);
32+
33+
}
34+
35+
function clicked (sprite, pointer) {
36+
37+
sprite.alpha -= 0.1;
38+
39+
}
40+
41+
function update() {
42+
}
43+
44+
function render() {
45+
46+
// game.debug.renderText('over: ' + a.input.checkPointerOver(game.input.activePointer), 32, 32);
47+
48+
49+
// game.debug.renderPoint(p, 'rgb(255,255,0)');
50+
// game.debug.renderText('px: ' + game.input.activePointer.x, 32, 32);
51+
// game.debug.renderText('py: ' + game.input.activePointer.y, 32, 64);
52+
// game.debug.renderPointer(game.input.activePointer);
53+
game.debug.renderSpriteInputInfo(a, 32, 32);
54+
55+
}
56+
57+
})();
58+
59+
</script>
60+
61+
</body>
62+
</html>

src/core/LinkedList.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ Phaser.LinkedList.prototype = {
77
prev: null,
88
first: null,
99
last: null,
10+
total: 0,
1011
sprite: { name: 'HD' },
1112

1213
add: function (child) {
1314

1415
// If the list is empty
15-
if (this.first == null && this.last == null)
16+
if (this.total == 0 && this.first == null && this.last == null)
1617
{
1718
this.first = child;
1819
this.last = child;
1920
this.next = child;
2021
child.prev = this;
22+
this.total++;
2123
return;
2224
}
2325

@@ -28,6 +30,10 @@ Phaser.LinkedList.prototype = {
2830

2931
this.last = child;
3032

33+
this.total++;
34+
35+
return child;
36+
3137
},
3238

3339
remove: function (child) {
@@ -38,6 +44,8 @@ Phaser.LinkedList.prototype = {
3844
return;
3945
}
4046

47+
this.total--;
48+
4149
// The only node?
4250
if (this.first == child && this.last == child)
4351
{

src/gameobjects/Events.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* The Events component is a collection of events fired by the parent game object and its components.
3+
* @param parent The game object using this Input component
4+
*/
5+
Phaser.Events = function (sprite) {
6+
7+
this.parent = sprite;
8+
this.onAddedToGroup = new Phaser.Signal;
9+
this.onRemovedFromGroup = new Phaser.Signal;
10+
this.onKilled = new Phaser.Signal;
11+
this.onRevived = new Phaser.Signal;
12+
this.onOutOfBounds = new Phaser.Signal;
13+
14+
this.onInputOver = null;
15+
this.onInputOut = null;
16+
this.onInputDown = null;
17+
this.onInputUp = null;
18+
this.onDragStart = null;
19+
this.onDragStop = null;
20+
21+
};

src/gameobjects/Sprite.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Phaser.Sprite = function (game, x, y, key, frame) {
2929
PIXI.Sprite.call(this);
3030
}
3131

32-
// this.events = new Phaser.Components.Events(this);
32+
this.events = new Phaser.Events(this);
3333

3434
/**
3535
* This manages animations of the sprite. You can modify animations through it. (see AnimationManager)
@@ -102,6 +102,9 @@ Phaser.Sprite = function (game, x, y, key, frame) {
102102
// Transform cache
103103
a00: 1, a01: 0, a02: x, a10: 0, a11: 1, a12: y, id: 1,
104104

105+
// Input specific transform cache
106+
i01: 0, i10: 0, idi: 1,
107+
105108
// Bounds check
106109
left: null, right: null, top: null, bottom: null,
107110

@@ -188,6 +191,7 @@ Phaser.Sprite.prototype.update = function() {
188191
{
189192
this._cache.a00 = this.worldTransform[0]; // scaleX a
190193
this._cache.a01 = this.worldTransform[1]; // skewY c
194+
this._cache.i01 = this.worldTransform[1]; // skewY c
191195
this._cache.scaleX = Math.sqrt((this._cache.a00 * this._cache.a00) + (this._cache.a01 * this._cache.a01)); // round this off a bit?
192196
this._cache.a01 *= -1;
193197
this._cache.dirty = true;
@@ -197,6 +201,7 @@ Phaser.Sprite.prototype.update = function() {
197201
if (this.worldTransform[3] != this._cache.a10 || this.worldTransform[4] != this._cache.a11)
198202
{
199203
this._cache.a10 = this.worldTransform[3]; // skewX b
204+
this._cache.i10 = this.worldTransform[3]; // skewX b
200205
this._cache.a11 = this.worldTransform[4]; // scaleY d
201206
this._cache.scaleY = Math.sqrt((this._cache.a10 * this._cache.a10) + (this._cache.a11 * this._cache.a11)); // round this off a bit?
202207
this._cache.a10 *= -1;
@@ -227,6 +232,7 @@ Phaser.Sprite.prototype.update = function() {
227232
this._cache.halfHeight = Math.floor(this._cache.height / 2);
228233

229234
this._cache.id = 1 / (this._cache.a00 * this._cache.a11 + this._cache.a01 * -this._cache.a10);
235+
this._cache.idi = 1 / (this._cache.a00 * this._cache.a11 + this._cache.i01 * -this._cache.i10);
230236

231237
this.updateBounds();
232238
}
@@ -262,11 +268,6 @@ Phaser.Sprite.prototype.update = function() {
262268

263269
this.body.update();
264270

265-
if (this.input.enabled)
266-
{
267-
this.input.update();
268-
}
269-
270271
}
271272

272273
Phaser.Sprite.prototype.postUpdate = function() {
@@ -309,6 +310,15 @@ Phaser.Sprite.prototype.getLocalPosition = function(p, x, y) {
309310

310311
}
311312

313+
Phaser.Sprite.prototype.getLocalUnmodifiedPosition = function(p, x, y) {
314+
315+
p.x = this._cache.a11 * this._cache.idi * x + -this._cache.i01 * this._cache.idi * y + (this._cache.a12 * this._cache.i01 - this._cache.a02 * this._cache.a11) * this._cache.idi;
316+
p.y = this._cache.a00 * this._cache.idi * y + -this._cache.i10 * this._cache.idi * x + (-this._cache.a12 * this._cache.a00 + this._cache.a02 * this._cache.i10) * this._cache.idi;
317+
318+
return p;
319+
320+
}
321+
312322
Phaser.Sprite.prototype.getBounds = function(rect) {
313323

314324
rect = rect || new Phaser.Rectangle;

src/input/Input.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ Phaser.Input.prototype = {
370370
**/
371371
reset: function (hard) {
372372

373-
if (typeof hard === "undefined") { hard = false; }
373+
hard = hard || false;
374374

375375
this.keyboard.reset();
376376
this.mousePointer.reset();
@@ -428,7 +428,8 @@ Phaser.Input.prototype = {
428428
**/
429429
startPointer: function (event) {
430430

431-
if (this.maxPointers < 10 && this.totalActivePointers == this.maxPointers) {
431+
if (this.maxPointers < 10 && this.totalActivePointers == this.maxPointers)
432+
{
432433
return null;
433434
}
434435

@@ -525,7 +526,7 @@ Phaser.Input.prototype = {
525526
**/
526527
getPointer: function (state) {
527528

528-
if (typeof state === "undefined") { state = false; }
529+
state = state || false;
529530

530531
if (this.pointer1.active == state)
531532
{
@@ -599,10 +600,7 @@ Phaser.Input.prototype = {
599600
**/
600601
getAngle: function (pointer1, pointer2) {
601602
// return Phaser.Vec2Utils.angle(pointer1.position, pointer2.position);
602-
},
603-
604-
addGameObject: function() {},
605-
removeGameObject: function() {},
603+
}
606604

607605
};
608606

src/input/InputHandler.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Phaser.InputHandler.prototype = {
104104
this.enabled = true;
105105

106106
// Create the signals the Input component will emit
107-
if (this.sprites.events && this.sprite.events.onInputOver == null)
107+
if (this.sprite.events && this.sprite.events.onInputOver == null)
108108
{
109109
this.sprite.events.onInputOver = new Phaser.Signal;
110110
this.sprite.events.onInputOut = new Phaser.Signal;
@@ -316,25 +316,25 @@ Phaser.InputHandler.prototype = {
316316
},
317317

318318
/**
319-
* Checks if the given pointer is over this Sprite. All checks are done in world coordinates.
319+
* Checks if the given pointer is over this Sprite.
320320
*/
321321
checkPointerOver: function (pointer) {
322322

323323
if (this.enabled && this.sprite.visible)
324324
{
325-
this.sprite.getLocalPosition(this._tempPoint, pointer.x, pointer.y);
325+
this.sprite.getLocalUnmodifiedPosition(this._tempPoint, pointer.x, pointer.y);
326326

327327
// Check against bounds
328328
var width = this.sprite.texture.frame.width,
329329
height = this.sprite.texture.frame.height,
330330
x1 = -width * this.sprite.anchor.x,
331331
y1;
332332

333-
if(x > x1 && x < x1 + width)
333+
if (this._tempPoint.x > x1 && this._tempPoint.x < x1 + width)
334334
{
335335
y1 = -height * this.sprite.anchor.y;
336336

337-
if(y > y1 && y < y1 + height)
337+
if (this._tempPoint.y > y1 && this._tempPoint.y < y1 + height)
338338
{
339339
return true;
340340
}
@@ -364,7 +364,7 @@ Phaser.InputHandler.prototype = {
364364
}
365365
else if (this._pointerData[pointer.id].isOver == true)
366366
{
367-
if (Phaser.SpriteUtils.overlapsPointer(this.sprite, pointer))
367+
if (this.checkPointerOver(pointer))
368368
{
369369
this._pointerData[pointer.id].x = pointer.x - this.sprite.x;
370370
this._pointerData[pointer.id].y = pointer.y - this.sprite.y;
@@ -449,7 +449,7 @@ Phaser.InputHandler.prototype = {
449449
this._pointerData[pointer.id].downDuration = this._pointerData[pointer.id].timeUp - this._pointerData[pointer.id].timeDown;
450450

451451
// Only release the InputUp signal if the pointer is still over this sprite
452-
if (Phaser.SpriteUtils.overlapsPointer(this.sprite, pointer))
452+
if (this.checkPointerOver(pointer))
453453
{
454454
//console.log('releasedHandler: ' + Date.now());
455455
this.sprite.events.onInputUp.dispatch(this.sprite, pointer);

0 commit comments

Comments
 (0)