Mouse wheel IO system

Towards fixing . This adds an IO class for detecting the mouse wheel
being scrolled. Basic tests are included; they mock the runtime to see
what blocks are activated by scrolling.
This commit is contained in:
Florrie 2018-01-26 17:13:01 -04:00
parent b201ce948d
commit be83c0ee74
3 changed files with 75 additions and 1 deletions

View file

@ -14,6 +14,7 @@ const Clock = require('../io/clock');
const DeviceManager = require('../io/deviceManager');
const Keyboard = require('../io/keyboard');
const Mouse = require('../io/mouse');
const MouseWheel = require('../io/mouseWheel');
const defaultBlockPackages = {
scratch3_control: require('../blocks/scratch3_control'),
@ -258,7 +259,8 @@ class Runtime extends EventEmitter {
clock: new Clock(),
deviceManager: new DeviceManager(),
keyboard: new Keyboard(this),
mouse: new Mouse(this)
mouse: new Mouse(this),
mouseWheel: new MouseWheel(this)
};
/**

28
src/io/mouseWheel.js Normal file
View file

@ -0,0 +1,28 @@
class MouseWheel {
constructor (runtime) {
/**
* Reference to the owning Runtime.
* @type{!Runtime}
*/
this.runtime = runtime;
}
/**
* Mouse wheel DOM event handler.
* @param {object} data Data from DOM event.
*/
postData (data) {
const matchFields = {};
if (data.deltaY < 0) {
matchFields.KEY_OPTION = 'up arrow';
} else if (data.deltaY > 0) {
matchFields.KEY_OPTION = 'down arrow';
} else {
return;
}
this.runtime.startHats('event_whenkeypressed', matchFields);
}
}
module.exports = MouseWheel;

View file

@ -0,0 +1,44 @@
const test = require('tap').test;
const MouseWheel = require('../../src/io/mouseWheel');
const Runtime = require('../../src/engine/runtime');
test('spec', t => {
const rt = new Runtime();
const mw = new MouseWheel(rt);
t.type(mw, 'object');
t.type(mw.postData, 'function');
t.end();
});
test('blocks activated by scrolling', t => {
let _startHatsArgs;
const rt = {
startHats: (...args) => {
_startHatsArgs = args;
}
};
const mw = new MouseWheel(rt);
_startHatsArgs = null;
mw.postData({
deltaY: -1
});
t.strictEquals(_startHatsArgs[0], 'event_whenkeypressed');
t.strictEquals(_startHatsArgs[1].KEY_OPTION, 'up arrow');
_startHatsArgs = null;
mw.postData({
deltaY: +1
});
t.strictEquals(_startHatsArgs[0], 'event_whenkeypressed');
t.strictEquals(_startHatsArgs[1].KEY_OPTION, 'down arrow');
_startHatsArgs = null;
mw.postData({
deltaY: 0
});
t.strictEquals(_startHatsArgs, null);
t.end();
});