littlejsengine 1.22.0 → 1.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -36,6 +36,8 @@ export
36
36
  debugKeysAlways,
37
37
  debugTweakables,
38
38
  levelEditor,
39
+ LevelEditor,
40
+ setLevelEditor,
39
41
 
40
42
  // Debug
41
43
  ASSERT,
@@ -97,6 +99,7 @@ export
97
99
  touchInputEnable,
98
100
  touchGamepadEnable,
99
101
  touchGamepadPassthrough,
102
+ touchPinchWheel,
100
103
  touchGamepadCenterButtonSize,
101
104
  touchGamepadButtonCount,
102
105
  touchGamepadLeftStick,
@@ -158,6 +161,7 @@ export
158
161
  setInputWASDEmulateDirection,
159
162
  setTouchGamepadEnable,
160
163
  setTouchGamepadPassthrough,
164
+ setTouchPinchWheel,
161
165
  setTouchGamepadCenterButtonSize,
162
166
  setTouchGamepadButtonCount,
163
167
  setTouchGamepadLeftStick,
@@ -361,6 +365,7 @@ export
361
365
  mouseDelta,
362
366
  mouseDeltaScreen,
363
367
  mouseWheel,
368
+ touchPinch,
364
369
  mouseInWindow,
365
370
  isUsingGamepad,
366
371
  lastInputDevice,
@@ -38,6 +38,13 @@ let mouseDeltaScreen = vec2();
38
38
  * @memberof Input */
39
39
  let mouseWheel = 0;
40
40
 
41
+ /** Touch pinch delta this frame, in the mouse wheel's units: two fingers moving apart are negative, as the wheel
42
+ * turned up is, and moving together positive, 1 for every 50 pixels; added to mouseWheel while touchPinchWheel
43
+ * is on, so a game that zooms with the wheel zooms with a pinch
44
+ * @type {number}
45
+ * @memberof Input */
46
+ let touchPinch = 0;
47
+
41
48
  /** True if mouse was inside the document window, set to false when mouse leaves
42
49
  * @type {boolean}
43
50
  * @memberof Input */
@@ -126,6 +133,7 @@ function inputClearKey(key, device=0, clearDown=true, clearPressed=true, clearRe
126
133
  const inputWASDToArrow = {KeyW:'ArrowUp', KeyS:'ArrowDown', KeyA:'ArrowLeft', KeyD:'ArrowRight'};
127
134
  const inputArrowToWASD = {ArrowUp:'KeyW', ArrowDown:'KeyS', ArrowLeft:'KeyA', ArrowRight:'KeyD'};
128
135
  const inputKeysHeld = new Set; // the keys physically down, since an arrow's slot is shared with its alias
136
+ let inputPinch; // the two fingers pinching and how far apart they were, undefined with no pinch
129
137
  let inputWasTouching = 0, inputTouchIdentifier; // the touch driving the mouse, cleared with the input so only a new touch presses
130
138
  let inputLastTouchTime = -1e9; // when a touch event last came, the mouse events a browser makes from a tap follow it
131
139
 
@@ -168,6 +176,7 @@ function inputClear()
168
176
  if (!i || inputData[i]) inputData[i] = [];
169
177
  inputKeysHeld.clear();
170
178
  inputWasTouching = 0;
179
+ inputPinch = undefined;
171
180
  // release the touch gamepad, a finger still on it has to lift before it can take a control again
172
181
  touchGamepadPointerRole.clear();
173
182
  touchGamepadButtons.length = 0;
@@ -695,19 +704,52 @@ function inputInit()
695
704
  const pressTouch = !inputWasTouching && e.type == 'touchstart' &&
696
705
  [...e.changedTouches].find(t=> !isGamepadTouch(t));
697
706
  const touch = pressTouch || gameTouches[0];
707
+ let pos = vec2(touch.clientX, touch.clientY);
708
+
709
+ // two fingers pinch: how far they moved together or apart is the touch screen's wheel, a
710
+ // click for every 50 pixels, and the mouse is between them, the place to zoom about; with
711
+ // touchPinchWheel off the pinch is only measured, and the first finger is the mouse as ever
712
+ const pinching = touching > 1 && touchPinchWheel;
713
+ if (touching > 1)
714
+ {
715
+ const [a, b] = gameTouches, key = a.identifier + ' ' + b.identifier;
716
+ const distance = hypot(a.clientX - b.clientX, a.clientY - b.clientY);
717
+ if (inputPinch && inputPinch.key === key)
718
+ {
719
+ const delta = (inputPinch.distance - distance) / 50;
720
+ touchPinch += delta;
721
+ if (touchPinchWheel)
722
+ mouseWheel += delta;
723
+ }
724
+ inputPinch = {key, distance}; // other fingers start it over, with no jump
725
+ if (pinching)
726
+ pos = vec2((a.clientX + b.clientX) / 2, (a.clientY + b.clientY) / 2);
727
+ }
728
+ else
729
+ inputPinch = undefined;
698
730
 
699
731
  // set event pos and pass it along
700
- const pos = vec2(touch.clientX, touch.clientY);
701
732
  const mousePosScreenLast = mousePosScreen;
702
733
  mousePosScreen = mouseEventToScreen(pos);
703
- if (inputWasTouching && touch.identifier === inputTouchIdentifier)
734
+ if (pinching)
735
+ {
736
+ // a pinch is not a press or a drag, the finger that pressed lets go
737
+ if (inputData[0][button] & 1)
738
+ inputData[0][button] = inputData[0][button] & 2 | 4;
739
+ }
740
+ else if (inputWasTouching && touch.identifier === inputTouchIdentifier)
704
741
  mouseDeltaScreen = mouseDeltaScreen.add(mousePosScreen.subtract(mousePosScreenLast));
705
742
  else if (pressTouch)
706
743
  inputData[0][button] = 3;
707
- inputTouchIdentifier = touch.identifier;
744
+ // the finger left after a pinch moves the mouse from where it is, with no jump and no press
745
+ inputTouchIdentifier = pinching ? undefined : touch.identifier;
746
+ }
747
+ else
748
+ {
749
+ inputPinch = undefined;
750
+ if (inputWasTouching && (inputData[0][button] & 1))
751
+ inputData[0][button] = inputData[0][button] & 2 | 4; // released only if it was pressed
708
752
  }
709
- else if (inputWasTouching && (inputData[0][button] & 1))
710
- inputData[0][button] = inputData[0][button] & 2 | 4; // released only if it was pressed
711
753
 
712
754
  // set was touching
713
755
  inputWasTouching = touching;
@@ -966,7 +1008,7 @@ function inputUpdatePost()
966
1008
  for (const deviceInputData of inputData)
967
1009
  for (const i in deviceInputData)
968
1010
  deviceInputData[i] &= 1;
969
- mouseWheel = 0;
1011
+ mouseWheel = touchPinch = 0;
970
1012
  mouseDelta = vec2();
971
1013
  mouseDeltaScreen = vec2();
972
1014
  }
@@ -52,8 +52,19 @@ function tweak(){}
52
52
  function tweakButton(){}
53
53
  function tweakDivider(){}
54
54
  function tweakEngineDefaults(){}
55
- const levelEditor = {isOpen: false, open(){}, close(){}, onTile: undefined, onRestart: undefined, onPlayFrom: undefined,
56
- paletteTiles: undefined, use3D: undefined};
55
+ class LevelEditor
56
+ {
57
+ constructor() { this.paletteTiles = this.use3D = this.tool = undefined; }
58
+ get isOpen() { return false; }
59
+ get is3D() { return false; }
60
+ get edit2D() { return undefined; }
61
+ get edit3D() { return undefined; }
62
+ open(){} close(){} addKey(){} addButton(){} addTool(){}
63
+ onTile(){} onRestart(){} onPlayFrom(){} onOpen(){} onClose(){} onUpdate(){} onDraw(){} onPanel(){}
64
+ onSave(){ return false; }
65
+ }
66
+ let levelEditor = new LevelEditor;
67
+ function setLevelEditor(editor){ levelEditor = editor; }
57
68
  function editorMapRestore(map){ return map; }
58
69
  function editorMapLoaded(){}
59
70
  function editorJSONFetched(){}
@@ -313,6 +313,15 @@ let touchGamepadEnable = false;
313
313
  * @memberof Settings */
314
314
  let touchGamepadPassthrough = false;
315
315
 
316
+ /** True if a pinch of two fingers turns the mouse wheel: touchPinch is added to mouseWheel, so what zooms with
317
+ * the wheel zooms with a pinch, the mouse is between the two fingers, and a second finger lets go of the button
318
+ * the first pressed, since a pinch is not a drag; turn it off for a game with its own use for two fingers, the
319
+ * first finger stays the mouse and touchPinch can still be read
320
+ * @type {boolean}
321
+ * @default
322
+ * @memberof Settings */
323
+ let touchPinchWheel = true;
324
+
316
325
  /** Size of center button if touch gamepad should have start button in the center
317
326
  * - Prevents activating when pressed near virtual stick or face buttons
318
327
  * - When the game is paused, any touch will press the button
@@ -708,6 +717,11 @@ function setTouchInputEnable(enable) { touchInputEnable = enable; }
708
717
  * @memberof Settings */
709
718
  function setTouchGamepadEnable(enable) { touchGamepadEnable = enable; }
710
719
 
720
+ /** Set if a pinch of two fingers turns the mouse wheel
721
+ * @param {boolean} enable
722
+ * @memberof Settings */
723
+ function setTouchPinchWheel(enable) { touchPinchWheel = enable; }
724
+
711
725
  /** Set if touches outside the gamepad controls should still drive mouse/touch input
712
726
  * @param {boolean} passthrough
713
727
  * @memberof Settings */