mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 14:32:59 -05:00
When clicked hats (#156)
This commit is contained in:
parent
c357a62005
commit
833c5ed313
3 changed files with 53 additions and 8 deletions
|
@ -161,17 +161,35 @@ window.onload = function() {
|
||||||
document.addEventListener('mousemove', function (e) {
|
document.addEventListener('mousemove', function (e) {
|
||||||
var rect = canvas.getBoundingClientRect();
|
var rect = canvas.getBoundingClientRect();
|
||||||
var coordinates = {
|
var coordinates = {
|
||||||
x: e.clientX - rect.left - rect.width / 2,
|
x: e.clientX - rect.left,
|
||||||
y: e.clientY - rect.top - rect.height / 2
|
y: e.clientY - rect.top,
|
||||||
|
canvasWidth: rect.width,
|
||||||
|
canvasHeight: rect.height
|
||||||
};
|
};
|
||||||
window.vm.postIOData('mouse', coordinates);
|
window.vm.postIOData('mouse', coordinates);
|
||||||
});
|
});
|
||||||
canvas.addEventListener('mousedown', function (e) {
|
canvas.addEventListener('mousedown', function (e) {
|
||||||
window.vm.postIOData('mouse', {isDown: true});
|
var rect = canvas.getBoundingClientRect();
|
||||||
|
var data = {
|
||||||
|
isDown: true,
|
||||||
|
x: e.clientX - rect.left,
|
||||||
|
y: e.clientY - rect.top,
|
||||||
|
canvasWidth: rect.width,
|
||||||
|
canvasHeight: rect.height
|
||||||
|
};
|
||||||
|
window.vm.postIOData('mouse', data);
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
});
|
});
|
||||||
canvas.addEventListener('mouseup', function (e) {
|
canvas.addEventListener('mouseup', function (e) {
|
||||||
window.vm.postIOData('mouse', {isDown: false});
|
var rect = canvas.getBoundingClientRect();
|
||||||
|
var data = {
|
||||||
|
isDown: false,
|
||||||
|
x: e.clientX - rect.left,
|
||||||
|
y: e.clientY - rect.top,
|
||||||
|
canvasWidth: rect.width,
|
||||||
|
canvasHeight: rect.height
|
||||||
|
};
|
||||||
|
window.vm.postIOData('mouse', data);
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -55,7 +55,7 @@ function Runtime () {
|
||||||
this.ioDevices = {
|
this.ioDevices = {
|
||||||
'clock': new Clock(),
|
'clock': new Clock(),
|
||||||
'keyboard': new Keyboard(this),
|
'keyboard': new Keyboard(this),
|
||||||
'mouse': new Mouse()
|
'mouse': new Mouse(this)
|
||||||
};
|
};
|
||||||
|
|
||||||
this._scriptGlowsPreviousFrame = [];
|
this._scriptGlowsPreviousFrame = [];
|
||||||
|
|
|
@ -1,20 +1,47 @@
|
||||||
var MathUtil = require('../util/math-util');
|
var MathUtil = require('../util/math-util');
|
||||||
|
|
||||||
function Mouse () {
|
function Mouse (runtime) {
|
||||||
this._x = 0;
|
this._x = 0;
|
||||||
this._y = 0;
|
this._y = 0;
|
||||||
this._isDown = false;
|
this._isDown = false;
|
||||||
|
/**
|
||||||
|
* Reference to the owning Runtime.
|
||||||
|
* Can be used, for example, to activate hats.
|
||||||
|
* @type{!Runtime}
|
||||||
|
*/
|
||||||
|
this.runtime = runtime;
|
||||||
}
|
}
|
||||||
|
|
||||||
Mouse.prototype.postData = function(data) {
|
Mouse.prototype.postData = function(data) {
|
||||||
if (data.x) {
|
if (data.x) {
|
||||||
this._x = data.x;
|
this._x = data.x - data.canvasWidth / 2;
|
||||||
}
|
}
|
||||||
if (data.y) {
|
if (data.y) {
|
||||||
this._y = data.y;
|
this._y = data.y - data.canvasHeight / 2;
|
||||||
}
|
}
|
||||||
if (typeof data.isDown !== 'undefined') {
|
if (typeof data.isDown !== 'undefined') {
|
||||||
this._isDown = data.isDown;
|
this._isDown = data.isDown;
|
||||||
|
if (this._isDown) {
|
||||||
|
this._activateClickHats(data.x, data.y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Mouse.prototype._activateClickHats = function (x, y) {
|
||||||
|
if (self.renderer) {
|
||||||
|
var pickPromise = self.renderer.pick(x, y);
|
||||||
|
var instance = this;
|
||||||
|
pickPromise.then(function(drawableID) {
|
||||||
|
for (var i = 0; i < instance.runtime.targets.length; i++) {
|
||||||
|
var target = instance.runtime.targets[i];
|
||||||
|
if (target.hasOwnProperty('drawableID') &&
|
||||||
|
target.drawableID == drawableID) {
|
||||||
|
instance.runtime.startHats('event_whenthisspriteclicked',
|
||||||
|
null, target);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue