Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Commit

Permalink
Refactor Mouse Triggers
Browse files Browse the repository at this point in the history
Refactored mouse triggers (MouseLeftUp, MouseLeftDown) so that they work
for every mouse button instead (MouseUp, MouseDown)
Adapted demos accordingly (+link fixes)
  • Loading branch information
mkucko committed Mar 26, 2013
1 parent afb3b1b commit 4f50530
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 60 deletions.
4 changes: 2 additions & 2 deletions demos/demo1.html
Expand Up @@ -31,8 +31,8 @@
<body>
<!--[if lt IE 8]><p class=chromeframe>Your browser is <em>ancient!</em> <a href="http://browsehappy.com/">Upgrade to a different browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to experience this site.</p><![endif]-->
<!-- libraries -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="//raw.github.com/craftyjs/craftyjs.github.com/master/release/0.5.3/crafty-min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://cdn.craftycomponents.com/crafty-0.5.3.js"></script>
<!-- game -->
<script type="text/javascript" src="../src/craftyMFace.js"></script>
<script type="text/javascript" src="demo1.js"></script>
Expand Down
40 changes: 21 additions & 19 deletions demos/demo1.js
Expand Up @@ -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');
Expand Down
8 changes: 4 additions & 4 deletions demos/demo2.html
Expand Up @@ -31,11 +31,11 @@
<body>
<!--[if lt IE 8]><p class=chromeframe>Your browser is <em>ancient!</em> <a href="http://browsehappy.com/">Upgrade to a different browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to experience this site.</p><![endif]-->
<!-- libraries -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="//raw.github.com/craftyjs/craftyjs.github.com/master/release/0.5.3/crafty-min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://cdn.craftycomponents.com/crafty-0.5.3.js"></script>

<script src="//raw.github.com/epeli/underscore.string/master/dist/underscore.string.min.js"></script>
<script src="//raw.github.com/towbi/CraftyEntityBoxOverlays/master/boxoverlays.js"></script>
<script src="https://raw.github.com/epeli/underscore.string/master/dist/underscore.string.min.js"></script>
<script src="https://raw.github.com/towbi/CraftyEntityBoxOverlays/master/boxoverlays.js"></script>

<!-- game -->
<script type="text/javascript" src="../src/craftyMFace.js"></script>
Expand Down
40 changes: 21 additions & 19 deletions demos/demo2.js
Expand Up @@ -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');
Expand Down
23 changes: 7 additions & 16 deletions src/craftyMFace.js
Expand Up @@ -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;
Expand All @@ -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});

Expand All @@ -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).
Expand All @@ -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() {
Expand Down

0 comments on commit 4f50530

Please sign in to comment.