From 4f50530d887837a543aed27729680433b8a638f2 Mon Sep 17 00:00:00 2001 From: mucaho Date: Tue, 26 Mar 2013 23:20:28 +0100 Subject: [PATCH] Refactor Mouse Triggers Refactored mouse triggers (MouseLeftUp, MouseLeftDown) so that they work for every mouse button instead (MouseUp, MouseDown) Adapted demos accordingly (+link fixes) --- demos/demo1.html | 4 ++-- demos/demo1.js | 40 +++++++++++++++++++++------------------- demos/demo2.html | 8 ++++---- demos/demo2.js | 40 +++++++++++++++++++++------------------- src/craftyMFace.js | 23 +++++++---------------- 5 files changed, 55 insertions(+), 60 deletions(-) diff --git a/demos/demo1.html b/demos/demo1.html index 931a9f3..ecf0bb1 100644 --- a/demos/demo1.html +++ b/demos/demo1.html @@ -31,8 +31,8 @@ - - + + diff --git a/demos/demo1.js b/demos/demo1.js index 57ae218..5bb3e39 100644 --- a/demos/demo1.js +++ b/demos/demo1.js @@ -114,25 +114,27 @@ $(document).ready(function() { } }) .multiway(2, {W: -90, S: 90, D: 0, A: 180}) - .bind("MouseLeftUp", function(data) { - // shoot - create bullet - Crafty.e("2D, " + render + ", Color") - .attr({ - x: this.x + 16, y: this.y + 24, z: zbase + 1, - w: 3, h: 3, - speed: 5, - angle: this.getAngle() - }) - .color("#FA5656") - .bind("EnterFrame", function(frame) { - this.x += Math.cos(this.angle) * this.speed; - this.y += Math.sin(this.angle) * this.speed; - - if (this.x > Crafty.viewport.width || this.x < 0) { - this.destroy(); - } - }); - }); + .bind("MouseUp", function(data) { + if (data.mouseButton == Crafty.mouseButtons.LEFT) { + // shoot - create bullet + Crafty.e("2D, " + render + ", Color") + .attr({ + x: this.x + 16, y: this.y + 24, z: zbase + 1, + w: 3, h: 3, + speed: 5, + angle: this.getAngle() + }) + .color("#FA5656") + .bind("EnterFrame", function(frame) { + this.x += Math.cos(this.angle) * this.speed; + this.y += Math.sin(this.angle) * this.speed; + + if (this.x > Crafty.viewport.width || this.x < 0) { + this.destroy(); + } + }); + } + }); }); // start Crafty.scene('load'); diff --git a/demos/demo2.html b/demos/demo2.html index 5045c49..7390215 100644 --- a/demos/demo2.html +++ b/demos/demo2.html @@ -31,11 +31,11 @@ - - + + - - + + diff --git a/demos/demo2.js b/demos/demo2.js index 3c42c04..896d1b3 100644 --- a/demos/demo2.js +++ b/demos/demo2.js @@ -101,25 +101,27 @@ $(document).ready(function() { this.curAngle = (e.grad) + 90; this.rotation = this.curAngle; }) - .bind("MouseLeftUp", function(data) { - // shoot - create bullet - Crafty.e("2D, " + render + ", Color") - .attr({ - x: this.x + 40, y: this.y + 15, z: zbase + 1, - w: 3, h: 3, - speed: 5, - angle: this._dirAngle + Math.PI - }) - .color("#FA5656") - .bind("EnterFrame", function(frame) { - this.x += Math.cos(this.angle) * this.speed; - this.y += Math.sin(this.angle) * this.speed; - - if (this.x > Crafty.viewport.width || this.x < 0) { - this.destroy(); - } - }); - }); + .bind("MouseUp", function(data) { + if (data.mouseButton == Crafty.mouseButtons.LEFT) { + // shoot - create bullet + Crafty.e("2D, " + render + ", Color") + .attr({ + x: this.x + 40, y: this.y + 15, z: zbase + 1, + w: 3, h: 3, + speed: 5, + angle: this._dirAngle + Math.PI + }) + .color("#FA5656") + .bind("EnterFrame", function(frame) { + this.x += Math.cos(this.angle) * this.speed; + this.y += Math.sin(this.angle) * this.speed; + + if (this.x > Crafty.viewport.width || this.x < 0) { + this.destroy(); + } + }); + } + }); }); // start Crafty.scene('load'); diff --git a/src/craftyMFace.js b/src/craftyMFace.js index ee8a8ef..e75f45a 100644 --- a/src/craftyMFace.js +++ b/src/craftyMFace.js @@ -40,27 +40,21 @@ Crafty.c("MouseFace", { if (this.disableControls || this.disregardMouseInput) { return; } - if (e.mouseButton == Crafty.mouseButtons.LEFT) { - this._mouseButtonLeft = this._mouseButtonState.up; - this.trigger("MouseLeftUp", e); - } + this.trigger("MouseUp", e); }, _onmousedown: function (e) { if (this.disableControls || this.disregardMouseInput) { return; } - if (e.mouseButton == Crafty.mouseButtons.LEFT) { - this._mouseButtonLeft = this._mouseButtonState.down; - this.trigger("MouseLeftDown", e); - } + this.trigger("MouseDown", e); }, _onmousemove: function (e) { if (this.disableControls || this.disregardMouseInput) { return; } - this._pos.x = e.realX; - this._pos.y = e.realY; + this._mousePos.x = e.realX; + this._mousePos.y = e.realY; var dx = this.x - e.realX, dy = this.y - e.realY; @@ -77,7 +71,7 @@ Crafty.c("MouseFace", { this._dirAngle = Math.atan2(dy, dx); - this.trigger("MouseMoved", {pos: this._pos, + this.trigger("MouseMoved", {pos: this._mousePos, rad: this._dirAngle + this.pi, grad: (this._dirAngle + this.pi) * this._rad}); @@ -100,7 +94,7 @@ Crafty.c("MouseFace", { init: function () { this.requires("Mouse"); - this._pos = {x: 0, y: 0}; + this._mousePos = {x: 0, y: 0}; // init radian angular measurements with respect to atan2 (arctangent) calculations // this would mean in the ranges of (0, -pi) and (0, pi). @@ -115,16 +109,13 @@ Crafty.c("MouseFace", { this._directions = {none: 0, left: -1, right: 1, up: -2, down: 2}; this._dirMove = this._directions.none; - this._mouseButtonState = {none: 0, up: 1, down: 2}; - this._mouseButtonLeft = this._mouseButtonState.none; - this._mouseButtonRight = this._mouseButtonState.none; Crafty.addEvent(this, Crafty.stage.elem, "mousemove", this._onmousemove); Crafty.addEvent(this, Crafty.stage.elem, "mouseup", this._onmouseup); Crafty.addEvent(this, Crafty.stage.elem, "mousedown", this._onmousedown); }, MouseFace: function(origin) { - this._origin = origin; + this._origin = origin; return this; }, getDirection: function() {