File size: 2,289 Bytes
7aec436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const run = ({ scaffolding }) => {
  const canvas = scaffolding._canvas;
  const vm = scaffolding.vm;
  const mouse = vm.runtime.ioDevices.mouse;
  let isLocked = false;

  const postMouseData = (e, isDown) => {
    const {movementX, movementY} = e;
    const {width, height} = scaffolding.layersRect;
    const x = mouse._clientX + movementX;
    const y = mouse._clientY - movementY;
    mouse._clientX = x;
    mouse._scratchX = mouse.runtime.stageWidth * ((x / width) - 0.5);
    mouse._clientY = y;
    mouse._scratchY = mouse.runtime.stageWidth * ((y / height) - 0.5);
    if (typeof isDown === 'boolean') {
      const data = {
        button: e.button,
        isDown
      };
      vm.postIOData('mouse', data);
    }
  };
  document.addEventListener("mousedown", (e) => {
    if (canvas.contains(e.target)) {
      e.stopPropagation();
      if (isLocked) {
        postMouseData(e, true);
      } else {
        canvas.requestPointerLock();
      }
    }
  }, true);
  document.addEventListener("mouseup", (e) => {
    e.stopPropagation();
    if (isLocked) {
      postMouseData(e, false);
    } else if (canvas.contains(e.target)) {
      canvas.requestPointerLock();
    }
  }, true);
  document.addEventListener("mousemove", (e) => {
    e.stopPropagation();
    if (isLocked) {
      postMouseData(e);
    }
  }, true);

  scaffolding.addEventListener('PROJECT_RUN_START', () => {
    if (!isLocked) {
      canvas.requestPointerLock();
    }
  });

  document.addEventListener('pointerlockchange', () => {
    isLocked = document.pointerLockElement === canvas;
  });
  document.addEventListener('pointerlockerror', (e) => {
    console.error('Pointer lock error', e);
  });

  vm.pointerLockMove = (deltaX, deltaY) => {
    postMouseData({
      // Essentially constructing a fake MouseEvent
      movementX: deltaX,
      movementY: deltaY
    });
  };

  const oldStep = vm.runtime._step;
  vm.runtime._step = function (...args) {
    const ret = oldStep.call(this, ...args);
    const {width, height} = scaffolding.layersRect;
    mouse._clientX = width / 2;
    mouse._clientY = height / 2;
    mouse._scratchX = 0;
    mouse._scratchY = 0;
    return ret;
  };
};

export default run;