File size: 1,499 Bytes
30c32c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class MouseWheel {
    constructor (runtime) {
        /**
         * Reference to the owning Runtime.
         * @type{!Runtime}
         */
        this.runtime = runtime;

        // pm: track scroll deltaY
        this.scrollDelta = 0;
        this.runtime.on("RUNTIME_STEP_END", () => {
            this.scrollDelta = 0;
        });
    }

    _addToScrollingDistanceBlock (amount) {
        if ('ext_pmSensingExpansion' in this.runtime) {
            this.runtime.ext_pmSensingExpansion.scrollDistance += amount;
        }
    }

    /**
     * Mouse wheel DOM event handler.
     * @param  {object} data Data from DOM event.
     */
    postData (data) {
        // pm: store scroll delta
        this.scrollDelta = data.deltaY;
        // add to scrolling distance
        this._addToScrollingDistanceBlock(0 - data.deltaY);

        const matchFields = {};
        const scrollFields = {};
        if (data.deltaY < 0) {
            matchFields.KEY_OPTION = 'up arrow';
            scrollFields.KEY_OPTION = 'up';
        } else if (data.deltaY > 0) {
            matchFields.KEY_OPTION = 'down arrow';
            scrollFields.KEY_OPTION = 'down';
        } else {
            return;
        }

        this.runtime.startHats('event_whenkeypressed', matchFields);
        this.runtime.startHats('event_whenmousescrolled', scrollFields);
    }

    // pm: expose scroll delta for sensing block
    getScrollDelta () {
        return this.scrollDelta;
    }
}

module.exports = MouseWheel;