mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-06-06 02:24:47 -04:00
Mouse wheel IO system
Towards fixing #865. 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:
parent
b201ce948d
commit
be83c0ee74
3 changed files with 75 additions and 1 deletions
|
@ -14,6 +14,7 @@ const Clock = require('../io/clock');
|
||||||
const DeviceManager = require('../io/deviceManager');
|
const DeviceManager = require('../io/deviceManager');
|
||||||
const Keyboard = require('../io/keyboard');
|
const Keyboard = require('../io/keyboard');
|
||||||
const Mouse = require('../io/mouse');
|
const Mouse = require('../io/mouse');
|
||||||
|
const MouseWheel = require('../io/mouseWheel');
|
||||||
|
|
||||||
const defaultBlockPackages = {
|
const defaultBlockPackages = {
|
||||||
scratch3_control: require('../blocks/scratch3_control'),
|
scratch3_control: require('../blocks/scratch3_control'),
|
||||||
|
@ -258,7 +259,8 @@ class Runtime extends EventEmitter {
|
||||||
clock: new Clock(),
|
clock: new Clock(),
|
||||||
deviceManager: new DeviceManager(),
|
deviceManager: new DeviceManager(),
|
||||||
keyboard: new Keyboard(this),
|
keyboard: new Keyboard(this),
|
||||||
mouse: new Mouse(this)
|
mouse: new Mouse(this),
|
||||||
|
mouseWheel: new MouseWheel(this)
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
28
src/io/mouseWheel.js
Normal file
28
src/io/mouseWheel.js
Normal 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;
|
44
test/unit/io_mousewheel.js
Normal file
44
test/unit/io_mousewheel.js
Normal 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();
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue