2017-04-17 15:10:04 -04:00
|
|
|
const MathUtil = require('../util/math-util');
|
2016-08-15 21:37:36 -04:00
|
|
|
|
2017-04-17 15:10:04 -04:00
|
|
|
const Mouse = function (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-10-23 17:55:31 -04:00
|
|
|
};
|
2016-08-15 21:37:36 -04:00
|
|
|
|
2016-10-24 11:56:52 -04:00
|
|
|
/**
|
|
|
|
* Activate "event_whenthisspriteclicked" hats if needed.
|
2016-10-24 15:16:06 -04:00
|
|
|
* @param {number} x X position to be sent to the renderer.
|
|
|
|
* @param {number} y Y position to be sent to the renderer.
|
2016-10-24 12:04:06 -04:00
|
|
|
* @private
|
2016-10-24 11:56:52 -04:00
|
|
|
*/
|
2016-09-12 17:16:10 -04:00
|
|
|
Mouse.prototype._activateClickHats = function (x, y) {
|
2016-09-20 02:42:05 -04:00
|
|
|
if (this.runtime.renderer) {
|
2017-04-17 15:10:04 -04:00
|
|
|
const drawableID = this.runtime.renderer.pick(x, y);
|
|
|
|
for (let i = 0; i < this.runtime.targets.length; i++) {
|
|
|
|
const target = this.runtime.targets[i];
|
2016-09-15 19:02:03 -04:00
|
|
|
if (target.hasOwnProperty('drawableID') &&
|
2016-10-23 17:55:31 -04:00
|
|
|
target.drawableID === drawableID) {
|
2016-09-15 19:02:03 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-24 11:56:52 -04:00
|
|
|
/**
|
|
|
|
* Mouse DOM event handler.
|
2016-10-24 15:16:06 -04:00
|
|
|
* @param {object} data Data from DOM event.
|
2016-10-24 11:56:52 -04:00
|
|
|
*/
|
2016-10-24 15:53:42 -04:00
|
|
|
Mouse.prototype.postData = function (data) {
|
2016-10-24 11:56:52 -04:00
|
|
|
if (data.x) {
|
|
|
|
this._x = data.x - data.canvasWidth / 2;
|
|
|
|
}
|
|
|
|
if (data.y) {
|
|
|
|
this._y = data.y - data.canvasHeight / 2;
|
|
|
|
}
|
|
|
|
if (typeof data.isDown !== 'undefined') {
|
|
|
|
this._isDown = data.isDown;
|
2017-02-20 16:22:13 -05:00
|
|
|
if (!this._isDown) {
|
2016-10-24 11:56:52 -04:00
|
|
|
this._activateClickHats(data.x, data.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the X position of the mouse.
|
2016-10-24 15:16:06 -04:00
|
|
|
* @return {number} Clamped X position of the mouse cursor.
|
2016-10-24 11:56:52 -04:00
|
|
|
*/
|
2016-08-15 21:37:36 -04:00
|
|
|
Mouse.prototype.getX = function () {
|
|
|
|
return MathUtil.clamp(this._x, -240, 240);
|
|
|
|
};
|
|
|
|
|
2016-10-24 11:56:52 -04:00
|
|
|
/**
|
|
|
|
* Get the Y position of the mouse.
|
2016-10-24 15:16:06 -04:00
|
|
|
* @return {number} Clamped Y position of the mouse cursor.
|
2016-10-24 11:56:52 -04:00
|
|
|
*/
|
2016-08-15 21:37:36 -04:00
|
|
|
Mouse.prototype.getY = function () {
|
|
|
|
return MathUtil.clamp(-this._y, -180, 180);
|
|
|
|
};
|
|
|
|
|
2016-10-24 11:56:52 -04:00
|
|
|
/**
|
|
|
|
* Get the down state of the mouse.
|
|
|
|
* @return {boolean} Is the mouse down?
|
|
|
|
*/
|
2016-08-15 21:37:36 -04:00
|
|
|
Mouse.prototype.getIsDown = function () {
|
|
|
|
return this._isDown;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Mouse;
|