mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-06-30 00:02:51 -04:00
More hats (#143)
* Key pressed hat * Tabs -> Spaces * Oops * Edge activate * Fix hat Phew... * I forgot to change it also here :/ :/ :\ * Minor fixes for TravisCi * Minor docs * Line length
This commit is contained in:
parent
2b84c8d0fe
commit
7caf8e588a
3 changed files with 35 additions and 3 deletions
src
|
@ -23,12 +23,13 @@ Scratch3EventBlocks.prototype.getHats = function () {
|
||||||
'event_whenflagclicked': {
|
'event_whenflagclicked': {
|
||||||
restartExistingThreads: true
|
restartExistingThreads: true
|
||||||
},
|
},
|
||||||
/*'event_whenkeypressed': {
|
'event_whenkeypressed': {
|
||||||
restartExistingThreads: false
|
restartExistingThreads: false
|
||||||
},
|
},
|
||||||
'event_whenthisspriteclicked': {
|
'event_whenthisspriteclicked': {
|
||||||
restartExistingThreads: true
|
restartExistingThreads: true
|
||||||
},
|
},
|
||||||
|
/*
|
||||||
'event_whenbackdropswitchesto': {
|
'event_whenbackdropswitchesto': {
|
||||||
restartExistingThreads: true
|
restartExistingThreads: true
|
||||||
},*/
|
},*/
|
||||||
|
|
|
@ -54,7 +54,7 @@ function Runtime () {
|
||||||
|
|
||||||
this.ioDevices = {
|
this.ioDevices = {
|
||||||
'clock': new Clock(),
|
'clock': new Clock(),
|
||||||
'keyboard': new Keyboard(),
|
'keyboard': new Keyboard(this),
|
||||||
'mouse': new Mouse()
|
'mouse': new Mouse()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,17 @@
|
||||||
var Cast = require('../util/cast');
|
var Cast = require('../util/cast');
|
||||||
|
|
||||||
function Keyboard () {
|
function Keyboard (runtime) {
|
||||||
/**
|
/**
|
||||||
* List of currently pressed keys.
|
* List of currently pressed keys.
|
||||||
* @type{Array.<number>}
|
* @type{Array.<number>}
|
||||||
*/
|
*/
|
||||||
this._keysPressed = [];
|
this._keysPressed = [];
|
||||||
|
/**
|
||||||
|
* Reference to the owning Runtime.
|
||||||
|
* Can be used, for example, to activate hats.
|
||||||
|
* @type{!Runtime}
|
||||||
|
*/
|
||||||
|
this.runtime = runtime;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -31,6 +37,21 @@ Keyboard.prototype._scratchKeyToKeyCode = function (keyName) {
|
||||||
return keyString.toUpperCase().charCodeAt(0);
|
return keyString.toUpperCase().charCodeAt(0);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Keyboard.prototype._keyCodeToScratchKey = function (keyCode) {
|
||||||
|
if (keyCode >= 48 && keyCode <= 90) {
|
||||||
|
// Standard letter.
|
||||||
|
return String.fromCharCode(keyCode).toLowerCase();
|
||||||
|
}
|
||||||
|
switch (keyCode) {
|
||||||
|
case 32: return 'space';
|
||||||
|
case 37: return 'leftarrow';
|
||||||
|
case 38: return 'uparrow';
|
||||||
|
case 39: return 'rightarrow';
|
||||||
|
case 40: return 'downarrow';
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
Keyboard.prototype.postData = function (data) {
|
Keyboard.prototype.postData = function (data) {
|
||||||
if (data.keyCode) {
|
if (data.keyCode) {
|
||||||
var index = this._keysPressed.indexOf(data.keyCode);
|
var index = this._keysPressed.indexOf(data.keyCode);
|
||||||
|
@ -38,6 +59,16 @@ Keyboard.prototype.postData = function (data) {
|
||||||
// If not already present, add to the list.
|
// If not already present, add to the list.
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
this._keysPressed.push(data.keyCode);
|
this._keysPressed.push(data.keyCode);
|
||||||
|
this.runtime.startHats('event_whenkeypressed',
|
||||||
|
{
|
||||||
|
'KEY_OPTION': this._keyCodeToScratchKey(data.keyCode)
|
||||||
|
.toUpperCase()
|
||||||
|
});
|
||||||
|
|
||||||
|
this.runtime.startHats('event_whenkeypressed',
|
||||||
|
{
|
||||||
|
'KEY_OPTION': 'ANY'
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} else if (index > -1) {
|
} else if (index > -1) {
|
||||||
// If already present, remove from the list.
|
// If already present, remove from the list.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue