2016-08-15 21:37:36 -04:00
|
|
|
var MathUtil = require('../util/math-util');
|
|
|
|
|
2016-09-12 17:16:10 -04:00
|
|
|
function Mouse (runtime) {
|
2016-08-15 21:37:36 -04:00
|
|
|
this._x = 0;
|
|
|
|
this._y = 0;
|
|
|
|
this._isDown = false;
|
2016-09-12 17:16:10 -04:00
|
|
|
/**
|
|
|
|
* Reference to the owning Runtime.
|
|
|
|
* Can be used, for example, to activate hats.
|
|
|
|
* @type{!Runtime}
|
|
|
|
*/
|
|
|
|
this.runtime = runtime;
|
2016-08-15 21:37:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Mouse.prototype.postData = function(data) {
|
|
|
|
if (data.x) {
|
2016-09-12 17:16:10 -04:00
|
|
|
this._x = data.x - data.canvasWidth / 2;
|
2016-08-15 21:37:36 -04:00
|
|
|
}
|
|
|
|
if (data.y) {
|
2016-09-12 17:16:10 -04:00
|
|
|
this._y = data.y - data.canvasHeight / 2;
|
2016-08-15 21:37:36 -04:00
|
|
|
}
|
|
|
|
if (typeof data.isDown !== 'undefined') {
|
|
|
|
this._isDown = data.isDown;
|
2016-09-12 17:16:10 -04:00
|
|
|
if (this._isDown) {
|
|
|
|
this._activateClickHats(data.x, data.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Mouse.prototype._activateClickHats = function (x, y) {
|
|
|
|
if (self.renderer) {
|
2016-09-15 19:02:03 -04:00
|
|
|
var drawableID = self.renderer.pick(x, y);
|
|
|
|
for (var i = 0; i < this.runtime.targets.length; i++) {
|
|
|
|
var target = this.runtime.targets[i];
|
|
|
|
if (target.hasOwnProperty('drawableID') &&
|
|
|
|
target.drawableID == drawableID) {
|
|
|
|
this.runtime.startHats('event_whenthisspriteclicked',
|
|
|
|
null, target);
|
|
|
|
return;
|
2016-09-12 17:16:10 -04:00
|
|
|
}
|
2016-09-15 19:02:03 -04:00
|
|
|
}
|
2016-08-15 21:37:36 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Mouse.prototype.getX = function () {
|
|
|
|
return MathUtil.clamp(this._x, -240, 240);
|
|
|
|
};
|
|
|
|
|
|
|
|
Mouse.prototype.getY = function () {
|
|
|
|
return MathUtil.clamp(-this._y, -180, 180);
|
|
|
|
};
|
|
|
|
|
|
|
|
Mouse.prototype.getIsDown = function () {
|
|
|
|
return this._isDown;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Mouse;
|