@n3rdw1z4rd/core 0.19.1 → 0.20.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.
- package/README.md +27 -10
- package/dist/index.cjs +442 -155
- package/dist/index.d.cts +231 -49
- package/dist/index.d.ts +5 -4
- package/dist/index.js +4 -3
- package/dist/input/_input.d.ts +25 -0
- package/dist/input/_input.js +97 -0
- package/dist/input/action-map.d.ts +42 -7
- package/dist/input/action-map.js +44 -18
- package/dist/input/bindings.d.ts +1 -1
- package/dist/input/bindings.js +1 -1
- package/dist/input/gestures.d.ts +20 -2
- package/dist/input/gestures.js +34 -8
- package/dist/input/index.d.ts +5 -4
- package/dist/input/index.js +3 -2
- package/dist/input/input-action-map.d.ts +327 -0
- package/dist/input/input-action-map.js +689 -0
- package/dist/input/input.d.ts +77 -272
- package/dist/input/input.js +201 -653
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1615,7 +1615,7 @@ const AXIS_NAMES = {
|
|
|
1615
1615
|
const capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
1616
1616
|
/**
|
|
1617
1617
|
* A readable name for a binding, e.g. for "Press Space to jump" prompts. Pass `keyLabels` (from
|
|
1618
|
-
* {@link
|
|
1618
|
+
* {@link InputActionMap.loadKeyboardLayout}) to name keys as printed on the player's keyboard.
|
|
1619
1619
|
*
|
|
1620
1620
|
* @category Input
|
|
1621
1621
|
*/
|
|
@@ -1650,8 +1650,17 @@ function bindingLabel(binding, keyLabels) {
|
|
|
1650
1650
|
*/
|
|
1651
1651
|
const DEFAULT_DEADZONE = .2;
|
|
1652
1652
|
/**
|
|
1653
|
-
*
|
|
1654
|
-
*
|
|
1653
|
+
* Repeat timing used by `repeat: true`: first repeat after 400 ms, then every 100 ms.
|
|
1654
|
+
*
|
|
1655
|
+
* @category Input
|
|
1656
|
+
*/
|
|
1657
|
+
const DEFAULT_REPEAT = {
|
|
1658
|
+
delay: 400,
|
|
1659
|
+
interval: 100
|
|
1660
|
+
};
|
|
1661
|
+
/**
|
|
1662
|
+
* Named actions and the inputs bound to them, like Godot's InputMap. Every {@link InputActionMap} has one as
|
|
1663
|
+
* {@link InputActionMap.actions}; the methods that change it return `this` for chaining.
|
|
1655
1664
|
*
|
|
1656
1665
|
* ```ts
|
|
1657
1666
|
* input.actions
|
|
@@ -1677,14 +1686,20 @@ var ActionMap = class {
|
|
|
1677
1686
|
get(name) {
|
|
1678
1687
|
return this._actions.get(name);
|
|
1679
1688
|
}
|
|
1680
|
-
/** Adds an action, or replaces its bindings (and deadzone, if given) if it exists. */
|
|
1689
|
+
/** Adds an action, or replaces its bindings (and deadzone / repeat, if given) if it exists. */
|
|
1681
1690
|
add(name, bindings = [], options = {}) {
|
|
1682
1691
|
const existing = this._actions.get(name);
|
|
1683
|
-
|
|
1692
|
+
const repeat = options.repeat === void 0 ? existing?.repeat : options.repeat === false ? void 0 : {
|
|
1693
|
+
...DEFAULT_REPEAT,
|
|
1694
|
+
...options.repeat === true ? {} : options.repeat
|
|
1695
|
+
};
|
|
1696
|
+
const action = {
|
|
1684
1697
|
name,
|
|
1685
1698
|
deadzone: options.deadzone ?? existing?.deadzone ?? .2,
|
|
1686
1699
|
bindings: [...bindings]
|
|
1687
|
-
}
|
|
1700
|
+
};
|
|
1701
|
+
if (repeat) action.repeat = repeat;
|
|
1702
|
+
this._actions.set(name, action);
|
|
1688
1703
|
return this;
|
|
1689
1704
|
}
|
|
1690
1705
|
/** Removes an action, if it exists. */
|
|
@@ -1720,24 +1735,35 @@ var ActionMap = class {
|
|
|
1720
1735
|
/** A JSON-serializable copy of all actions, for saving rebinds; see {@link ActionMap.load}. */
|
|
1721
1736
|
toJSON() {
|
|
1722
1737
|
const json = {};
|
|
1723
|
-
for (const action of this._actions.values())
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1738
|
+
for (const action of this._actions.values()) {
|
|
1739
|
+
json[action.name] = {
|
|
1740
|
+
deadzone: action.deadzone,
|
|
1741
|
+
bindings: action.bindings.map((b) => ({ ...b }))
|
|
1742
|
+
};
|
|
1743
|
+
if (action.repeat) json[action.name].repeat = { ...action.repeat };
|
|
1744
|
+
}
|
|
1727
1745
|
return json;
|
|
1728
1746
|
}
|
|
1729
1747
|
/** Replaces all actions with the saved ones (e.g. a player's rebinds). */
|
|
1730
1748
|
load(json) {
|
|
1731
1749
|
this.clear();
|
|
1732
|
-
for (const [name, action] of Object.entries(json)) this.add(name, action.bindings, {
|
|
1750
|
+
for (const [name, action] of Object.entries(json)) this.add(name, action.bindings, {
|
|
1751
|
+
deadzone: action.deadzone,
|
|
1752
|
+
repeat: action.repeat ?? false
|
|
1753
|
+
});
|
|
1733
1754
|
return this;
|
|
1734
1755
|
}
|
|
1735
1756
|
/**
|
|
1736
1757
|
* Godot-style `ui_*` actions for menus and dialogs (`ui_accept`, `ui_cancel`, `ui_left`, `ui_right`,
|
|
1737
|
-
* `ui_up`, `ui_down`, `ui_page_up`, `ui_page_down`), with a 0.5 deadzone;
|
|
1758
|
+
* `ui_up`, `ui_down`, `ui_page_up`, `ui_page_down`), with a 0.5 deadzone; the directions and
|
|
1759
|
+
* page actions auto-repeat ({@link DEFAULT_REPEAT}). Replaces existing ones.
|
|
1738
1760
|
*/
|
|
1739
1761
|
addDefaults() {
|
|
1740
1762
|
const ui = { deadzone: .5 };
|
|
1763
|
+
const nav = {
|
|
1764
|
+
deadzone: .5,
|
|
1765
|
+
repeat: true
|
|
1766
|
+
};
|
|
1741
1767
|
this.add("ui_accept", [
|
|
1742
1768
|
key("Enter"),
|
|
1743
1769
|
key("NumpadEnter"),
|
|
@@ -1749,24 +1775,24 @@ var ActionMap = class {
|
|
|
1749
1775
|
key("ArrowLeft"),
|
|
1750
1776
|
pad("DpadLeft"),
|
|
1751
1777
|
padAxis("leftX", -1)
|
|
1752
|
-
],
|
|
1778
|
+
], nav);
|
|
1753
1779
|
this.add("ui_right", [
|
|
1754
1780
|
key("ArrowRight"),
|
|
1755
1781
|
pad("DpadRight"),
|
|
1756
1782
|
padAxis("leftX", 1)
|
|
1757
|
-
],
|
|
1783
|
+
], nav);
|
|
1758
1784
|
this.add("ui_up", [
|
|
1759
1785
|
key("ArrowUp"),
|
|
1760
1786
|
pad("DpadUp"),
|
|
1761
1787
|
padAxis("leftY", -1)
|
|
1762
|
-
],
|
|
1788
|
+
], nav);
|
|
1763
1789
|
this.add("ui_down", [
|
|
1764
1790
|
key("ArrowDown"),
|
|
1765
1791
|
pad("DpadDown"),
|
|
1766
1792
|
padAxis("leftY", 1)
|
|
1767
|
-
],
|
|
1768
|
-
this.add("ui_page_up", [key("PageUp"), pad("LB")],
|
|
1769
|
-
this.add("ui_page_down", [key("PageDown"), pad("RB")],
|
|
1793
|
+
], nav);
|
|
1794
|
+
this.add("ui_page_up", [key("PageUp"), pad("LB")], nav);
|
|
1795
|
+
this.add("ui_page_down", [key("PageDown"), pad("RB")], nav);
|
|
1770
1796
|
return this;
|
|
1771
1797
|
}
|
|
1772
1798
|
};
|
|
@@ -1785,7 +1811,7 @@ const DEFAULT_THRESHOLDS = {
|
|
|
1785
1811
|
/**
|
|
1786
1812
|
* Turns touch points into gestures: tap, double tap, long press, swipes (one finger) and
|
|
1787
1813
|
* pinch / pan / rotate (two fingers). Feed it with down / move / up, call update() once per frame.
|
|
1788
|
-
* Times are in ms and positions in pixels, from any consistent clock and origin. {@link
|
|
1814
|
+
* Times are in ms and positions in pixels, from any consistent clock and origin. {@link InputActionMap} drives
|
|
1789
1815
|
* one for you; use this class directly only for custom touch handling.
|
|
1790
1816
|
*
|
|
1791
1817
|
* Tap, double tap and swipe are reported when the finger lifts, long press while it is still held;
|
|
@@ -1796,7 +1822,8 @@ const DEFAULT_THRESHOLDS = {
|
|
|
1796
1822
|
* el.addEventListener('pointerdown', e => recognizer.down(e.pointerId, e.offsetX, e.offsetY, performance.now()));
|
|
1797
1823
|
* // ... move / up / cancel likewise
|
|
1798
1824
|
*
|
|
1799
|
-
* const { gestures, pinch } = recognizer.update(performance.now());
|
|
1825
|
+
* const { gestures, events, pinch } = recognizer.update(performance.now());
|
|
1826
|
+
* const tap = events.find(e => e.gesture === 'tap'); // { gesture, x, y }
|
|
1800
1827
|
* ```
|
|
1801
1828
|
*
|
|
1802
1829
|
* @category Input
|
|
@@ -1860,17 +1887,32 @@ var GestureRecognizer = class {
|
|
|
1860
1887
|
const dy = y - touch.startY;
|
|
1861
1888
|
const distance = Math.hypot(dx, dy);
|
|
1862
1889
|
if (duration <= t.tapMaxDuration && touch.maxMovement <= t.tapMaxMovement) {
|
|
1863
|
-
this._pending.push(
|
|
1890
|
+
this._pending.push({
|
|
1891
|
+
gesture: "tap",
|
|
1892
|
+
x,
|
|
1893
|
+
y
|
|
1894
|
+
});
|
|
1864
1895
|
const last = this._lastTap;
|
|
1865
1896
|
if (last && time - last.time <= t.doubleTapMaxDelay && Math.hypot(x - last.x, y - last.y) <= t.doubleTapMaxDistance) {
|
|
1866
|
-
this._pending.push(
|
|
1897
|
+
this._pending.push({
|
|
1898
|
+
gesture: "doubleTap",
|
|
1899
|
+
x,
|
|
1900
|
+
y
|
|
1901
|
+
});
|
|
1867
1902
|
this._lastTap = void 0;
|
|
1868
1903
|
} else this._lastTap = {
|
|
1869
1904
|
x,
|
|
1870
1905
|
y,
|
|
1871
1906
|
time
|
|
1872
1907
|
};
|
|
1873
|
-
} else if (distance >= t.swipeMinDistance && duration <= t.swipeMaxDuration)
|
|
1908
|
+
} else if (distance >= t.swipeMinDistance && duration <= t.swipeMaxDuration) {
|
|
1909
|
+
const gesture = Math.abs(dx) >= Math.abs(dy) ? dx > 0 ? "swipeRight" : "swipeLeft" : dy > 0 ? "swipeDown" : "swipeUp";
|
|
1910
|
+
this._pending.push({
|
|
1911
|
+
gesture,
|
|
1912
|
+
x: touch.startX,
|
|
1913
|
+
y: touch.startY
|
|
1914
|
+
});
|
|
1915
|
+
}
|
|
1874
1916
|
}
|
|
1875
1917
|
/** Forgets a finger without recognizing anything (e.g. on `pointercancel`). */
|
|
1876
1918
|
cancel(id) {
|
|
@@ -1887,6 +1929,7 @@ var GestureRecognizer = class {
|
|
|
1887
1929
|
update(time) {
|
|
1888
1930
|
const frame = {
|
|
1889
1931
|
gestures: [],
|
|
1932
|
+
events: [],
|
|
1890
1933
|
pinch: 1,
|
|
1891
1934
|
pan: {
|
|
1892
1935
|
x: 0,
|
|
@@ -1896,7 +1939,11 @@ var GestureRecognizer = class {
|
|
|
1896
1939
|
};
|
|
1897
1940
|
for (const touch of this._touches.values()) if (!touch.multi && !touch.longPressed && touch.maxMovement <= this.thresholds.tapMaxMovement && time - touch.startTime >= this.thresholds.longPressDuration) {
|
|
1898
1941
|
touch.longPressed = true;
|
|
1899
|
-
this._pending.push(
|
|
1942
|
+
this._pending.push({
|
|
1943
|
+
gesture: "longPress",
|
|
1944
|
+
x: touch.x,
|
|
1945
|
+
y: touch.y
|
|
1946
|
+
});
|
|
1900
1947
|
}
|
|
1901
1948
|
const [a, b] = [...this._touches.values()];
|
|
1902
1949
|
if (a && b) {
|
|
@@ -1912,7 +1959,11 @@ var GestureRecognizer = class {
|
|
|
1912
1959
|
y: cy - pair.cy
|
|
1913
1960
|
};
|
|
1914
1961
|
frame.rotate = Math.atan2(Math.sin(angle - pair.angle), Math.cos(angle - pair.angle));
|
|
1915
|
-
if (Math.abs(frame.pinch - 1) >= this.thresholds.pinchMinChange) this._pending.push(
|
|
1962
|
+
if (Math.abs(frame.pinch - 1) >= this.thresholds.pinchMinChange) this._pending.push({
|
|
1963
|
+
gesture: frame.pinch > 1 ? "pinchOut" : "pinchIn",
|
|
1964
|
+
x: cx,
|
|
1965
|
+
y: cy
|
|
1966
|
+
});
|
|
1916
1967
|
}
|
|
1917
1968
|
this._pair = {
|
|
1918
1969
|
a: a.id,
|
|
@@ -1923,12 +1974,253 @@ var GestureRecognizer = class {
|
|
|
1923
1974
|
cy
|
|
1924
1975
|
};
|
|
1925
1976
|
} else this._pair = void 0;
|
|
1926
|
-
frame.
|
|
1977
|
+
frame.events = this._pending;
|
|
1978
|
+
frame.gestures = frame.events.map((e) => e.gesture);
|
|
1927
1979
|
this._pending = [];
|
|
1928
1980
|
return frame;
|
|
1929
1981
|
}
|
|
1930
1982
|
};
|
|
1931
1983
|
//#endregion
|
|
1984
|
+
//#region src/input/input.ts
|
|
1985
|
+
const BUTTON_BITS = [
|
|
1986
|
+
1,
|
|
1987
|
+
4,
|
|
1988
|
+
2,
|
|
1989
|
+
8,
|
|
1990
|
+
16
|
|
1991
|
+
];
|
|
1992
|
+
/**
|
|
1993
|
+
* Event-based input for keyboard, mouse, pen and touch: subscribe with `on()` and query state
|
|
1994
|
+
* at any time; no frame loop needed. One shared instance, {@link Input.instance}.
|
|
1995
|
+
*
|
|
1996
|
+
* Events (listeners get the DOM event):
|
|
1997
|
+
* - keys, by lowercased `KeyboardEvent.code`: `key_down`, `key_up`, `key_repeat`, `key_pressed`
|
|
1998
|
+
* (released within `inputThreshold` ms), and per key `keyw_down`, `keyw_up`, `keyw_pressed`, ...
|
|
1999
|
+
* - buttons (mouse buttons; the primary finger or pen is button 0): `button_down`, `button_up`,
|
|
2000
|
+
* `button_clicked`, and per button `button0_down`, `button0_up`, `button0_clicked`, ...
|
|
2001
|
+
* No click after a drag.
|
|
2002
|
+
* - pointer: `pointer_move`; `drag_start`, `drag`, `drag_end` (moved `dragThreshold` px with a
|
|
2003
|
+
* button down); `mouse_wheel`; `contextmenu` (see `disableContextMenu()`)
|
|
2004
|
+
* - every finger: `touch_down`, `touch_move`, `touch_up`, `touch_cancel`
|
|
2005
|
+
* - `blur`: the window lost focus or the tab was hidden; every key and button is released
|
|
2006
|
+
*
|
|
2007
|
+
* ```ts
|
|
2008
|
+
* const input = Input.instance;
|
|
2009
|
+
* input.target = canvas;
|
|
2010
|
+
*
|
|
2011
|
+
* input.on('keyw_down', 'arrowup_down', () => move(0, -1));
|
|
2012
|
+
* input.on('button0_clicked', () => select(input.pointerPosition));
|
|
2013
|
+
* input.on('pointer_move', () => {
|
|
2014
|
+
* if (input.isButtonDown(0)) pan(input.pointerDelta.x, input.pointerDelta.y);
|
|
2015
|
+
* });
|
|
2016
|
+
* ```
|
|
2017
|
+
*
|
|
2018
|
+
* @category Input
|
|
2019
|
+
*/
|
|
2020
|
+
var Input = class Input extends Emitter {
|
|
2021
|
+
static _instance;
|
|
2022
|
+
/** the shared instance, created on first use */
|
|
2023
|
+
static get instance() {
|
|
2024
|
+
if (!Input._instance) Input._instance = new Input();
|
|
2025
|
+
return Input._instance;
|
|
2026
|
+
}
|
|
2027
|
+
_state = {};
|
|
2028
|
+
_windowListeners = [];
|
|
2029
|
+
_targetListeners = [];
|
|
2030
|
+
_target = window;
|
|
2031
|
+
_contextMenuDisabled = false;
|
|
2032
|
+
_dragStart;
|
|
2033
|
+
_dragging = false;
|
|
2034
|
+
/** pixels from the target's top-left */
|
|
2035
|
+
pointerPosition = new Vector();
|
|
2036
|
+
/** -1..1 across the target, y up */
|
|
2037
|
+
pointerPositionNdc = new Vector();
|
|
2038
|
+
/** pixels moved by the last pointer event (mouse, pen and touch alike) */
|
|
2039
|
+
pointerDelta = new Vector();
|
|
2040
|
+
/** ms; a key or button released within this is also `_pressed` / `_clicked` */
|
|
2041
|
+
inputThreshold = 200;
|
|
2042
|
+
/** px; moving further than this with a button down starts a drag */
|
|
2043
|
+
dragThreshold = 5;
|
|
2044
|
+
/**
|
|
2045
|
+
* Where pointer, wheel and context-menu events are read and positions are measured, default
|
|
2046
|
+
* `window`; usually the canvas. Keys are always read from `window`.
|
|
2047
|
+
*/
|
|
2048
|
+
get target() {
|
|
2049
|
+
return this._target;
|
|
2050
|
+
}
|
|
2051
|
+
/** moves the pointer, wheel and context-menu listeners to `target` */
|
|
2052
|
+
set target(target) {
|
|
2053
|
+
this._targetListeners.forEach(([type, listener]) => this._target.removeEventListener(type, listener));
|
|
2054
|
+
this._targetListeners = [];
|
|
2055
|
+
this._target = target;
|
|
2056
|
+
this._listenToTarget();
|
|
2057
|
+
}
|
|
2058
|
+
/** a drag is in progress */
|
|
2059
|
+
get isDragging() {
|
|
2060
|
+
return this._dragging;
|
|
2061
|
+
}
|
|
2062
|
+
constructor() {
|
|
2063
|
+
super();
|
|
2064
|
+
this._listen(this._windowListeners, window, "keydown", this._onKeyDown);
|
|
2065
|
+
this._listen(this._windowListeners, window, "keyup", this._onKeyUp);
|
|
2066
|
+
this._listen(this._windowListeners, window, "blur", this._onBlur);
|
|
2067
|
+
if (typeof document !== "undefined") this._listen(this._windowListeners, document, "visibilitychange", () => {
|
|
2068
|
+
if (document.hidden) this._onBlur(new Event("blur"));
|
|
2069
|
+
});
|
|
2070
|
+
this._listenToTarget();
|
|
2071
|
+
}
|
|
2072
|
+
/** Stops the right-click menu on the target; `contextmenu` is still emitted. */
|
|
2073
|
+
disableContextMenu() {
|
|
2074
|
+
if (this._contextMenuDisabled) return;
|
|
2075
|
+
this._contextMenuDisabled = true;
|
|
2076
|
+
this._listen(this._targetListeners, this._target, "contextmenu", this._onContextMenu);
|
|
2077
|
+
}
|
|
2078
|
+
/** Removes every DOM listener; the next {@link Input.instance} is a new one. */
|
|
2079
|
+
dispose() {
|
|
2080
|
+
this._windowListeners.forEach(([type, listener]) => {
|
|
2081
|
+
window.removeEventListener(type, listener);
|
|
2082
|
+
if (typeof document !== "undefined") document.removeEventListener(type, listener);
|
|
2083
|
+
});
|
|
2084
|
+
this._targetListeners.forEach(([type, listener]) => this._target.removeEventListener(type, listener));
|
|
2085
|
+
this._windowListeners = [];
|
|
2086
|
+
this._targetListeners = [];
|
|
2087
|
+
Input._instance = void 0;
|
|
2088
|
+
}
|
|
2089
|
+
_listenToTarget() {
|
|
2090
|
+
const listeners = this._targetListeners;
|
|
2091
|
+
this._listen(listeners, this._target, "pointerdown", this._onPointerDown);
|
|
2092
|
+
this._listen(listeners, this._target, "pointerup", this._onPointerUp);
|
|
2093
|
+
this._listen(listeners, this._target, "pointercancel", this._onPointerUp);
|
|
2094
|
+
this._listen(listeners, this._target, "pointermove", this._onPointerMove);
|
|
2095
|
+
this._listen(listeners, this._target, "wheel", this._onWheel, { passive: false });
|
|
2096
|
+
if (this._contextMenuDisabled) this._listen(listeners, this._target, "contextmenu", this._onContextMenu);
|
|
2097
|
+
const style = this._target.style;
|
|
2098
|
+
if (style) style.touchAction = "none";
|
|
2099
|
+
}
|
|
2100
|
+
_listen(listeners, target, type, handler, options) {
|
|
2101
|
+
const listener = handler.bind(this);
|
|
2102
|
+
target.addEventListener(type, listener, options);
|
|
2103
|
+
listeners.push([type, listener]);
|
|
2104
|
+
}
|
|
2105
|
+
_down(id, group, name, ev) {
|
|
2106
|
+
if (this._state[id]?.state) return;
|
|
2107
|
+
this._state[id] = {
|
|
2108
|
+
state: 1,
|
|
2109
|
+
timeStamp: ev.timeStamp
|
|
2110
|
+
};
|
|
2111
|
+
this.emit(`${group}_down`, ev);
|
|
2112
|
+
this.emit(`${name}_down`, ev);
|
|
2113
|
+
}
|
|
2114
|
+
_up(id, group, name, quick, ev) {
|
|
2115
|
+
const down = this._state[id];
|
|
2116
|
+
if (!down?.state) return;
|
|
2117
|
+
this._state[id] = {
|
|
2118
|
+
state: 0,
|
|
2119
|
+
timeStamp: ev.timeStamp
|
|
2120
|
+
};
|
|
2121
|
+
this.emit(`${group}_up`, ev);
|
|
2122
|
+
this.emit(`${name}_up`, ev);
|
|
2123
|
+
if (!this._dragging && ev.timeStamp - down.timeStamp <= this.inputThreshold) {
|
|
2124
|
+
this.emit(`${group}_${quick}`, ev);
|
|
2125
|
+
this.emit(`${name}_${quick}`, ev);
|
|
2126
|
+
}
|
|
2127
|
+
}
|
|
2128
|
+
_anyButtonDown() {
|
|
2129
|
+
return BUTTON_BITS.some((_, button) => this.isButtonDown(button));
|
|
2130
|
+
}
|
|
2131
|
+
_updatePointer(ev) {
|
|
2132
|
+
const rect = this._target.getBoundingClientRect?.() ?? {
|
|
2133
|
+
left: 0,
|
|
2134
|
+
top: 0,
|
|
2135
|
+
width: window.innerWidth,
|
|
2136
|
+
height: window.innerHeight
|
|
2137
|
+
};
|
|
2138
|
+
const x = ev.clientX - rect.left;
|
|
2139
|
+
const y = ev.clientY - rect.top;
|
|
2140
|
+
if (ev.pointerType === "touch") this.pointerDelta.set(x - this.pointerPosition.x, y - this.pointerPosition.y);
|
|
2141
|
+
else this.pointerDelta.set(ev.movementX ?? 0, ev.movementY ?? 0);
|
|
2142
|
+
this.pointerPosition.set(x, y);
|
|
2143
|
+
this.pointerPositionNdc.set(x / (rect.width || 1) * 2 - 1, -(y / (rect.height || 1)) * 2 + 1);
|
|
2144
|
+
}
|
|
2145
|
+
_onContextMenu(ev) {
|
|
2146
|
+
ev.preventDefault();
|
|
2147
|
+
this.emit("contextmenu", ev);
|
|
2148
|
+
}
|
|
2149
|
+
_onKeyDown(ev) {
|
|
2150
|
+
if (ev.repeat) this.emit("key_repeat", ev);
|
|
2151
|
+
else this._down(ev.code, "key", ev.code.toLowerCase(), ev);
|
|
2152
|
+
}
|
|
2153
|
+
_onKeyUp(ev) {
|
|
2154
|
+
this._up(ev.code, "key", ev.code.toLowerCase(), "pressed", ev);
|
|
2155
|
+
}
|
|
2156
|
+
_onPointerDown(ev) {
|
|
2157
|
+
if (ev.pointerType === "touch") this.emit("touch_down", ev);
|
|
2158
|
+
if (!ev.isPrimary) return;
|
|
2159
|
+
this._target.setPointerCapture?.(ev.pointerId);
|
|
2160
|
+
this._updatePointer(ev);
|
|
2161
|
+
this.pointerDelta.set(0, 0);
|
|
2162
|
+
if (!this._anyButtonDown()) {
|
|
2163
|
+
this._dragStart = this.pointerPosition.clone();
|
|
2164
|
+
this._dragging = false;
|
|
2165
|
+
}
|
|
2166
|
+
this._down(`button${ev.button}`, "button", `button${ev.button}`, ev);
|
|
2167
|
+
}
|
|
2168
|
+
_onPointerUp(ev) {
|
|
2169
|
+
if (ev.pointerType === "touch") this.emit(ev.type === "pointercancel" ? "touch_cancel" : "touch_up", ev);
|
|
2170
|
+
if (!ev.isPrimary) return;
|
|
2171
|
+
this._updatePointer(ev);
|
|
2172
|
+
this._up(`button${ev.button}`, "button", `button${ev.button}`, "clicked", ev);
|
|
2173
|
+
if (!this._anyButtonDown()) this._endDrag(ev);
|
|
2174
|
+
}
|
|
2175
|
+
_onPointerMove(ev) {
|
|
2176
|
+
if (ev.pointerType === "touch") this.emit("touch_move", ev);
|
|
2177
|
+
if (!ev.isPrimary) return;
|
|
2178
|
+
this._updatePointer(ev);
|
|
2179
|
+
if (ev.pointerType === "mouse" && ev.button >= 0) {
|
|
2180
|
+
const id = `button${ev.button}`;
|
|
2181
|
+
if (ev.buttons & BUTTON_BITS[ev.button]) this._down(id, "button", id, ev);
|
|
2182
|
+
else this._up(id, "button", id, "clicked", ev);
|
|
2183
|
+
}
|
|
2184
|
+
if (this._dragStart && this._anyButtonDown()) {
|
|
2185
|
+
if (!this._dragging && this.pointerPosition.distanceTo(this._dragStart) > this.dragThreshold) {
|
|
2186
|
+
this._dragging = true;
|
|
2187
|
+
this.emit("drag_start", ev);
|
|
2188
|
+
}
|
|
2189
|
+
if (this._dragging) this.emit("drag", ev);
|
|
2190
|
+
}
|
|
2191
|
+
this.emit("pointer_move", ev);
|
|
2192
|
+
}
|
|
2193
|
+
_endDrag(ev) {
|
|
2194
|
+
if (this._dragging) this.emit("drag_end", ev);
|
|
2195
|
+
this._dragStart = void 0;
|
|
2196
|
+
this._dragging = false;
|
|
2197
|
+
}
|
|
2198
|
+
_onWheel(ev) {
|
|
2199
|
+
this.emit("mouse_wheel", ev);
|
|
2200
|
+
}
|
|
2201
|
+
_onBlur(ev) {
|
|
2202
|
+
Object.values(this._state).forEach((s) => s.state = 0);
|
|
2203
|
+
this._endDrag(ev);
|
|
2204
|
+
this.emit("blur", ev);
|
|
2205
|
+
}
|
|
2206
|
+
/** Whether a key (by `KeyboardEvent.code`, e.g. 'KeyW') is down. */
|
|
2207
|
+
isKeyDown(keyCode) {
|
|
2208
|
+
return this._state[keyCode]?.state === 1;
|
|
2209
|
+
}
|
|
2210
|
+
/** 1 if the key is down, else 0. */
|
|
2211
|
+
getKeyState(keyCode) {
|
|
2212
|
+
return this._state[keyCode]?.state ?? 0;
|
|
2213
|
+
}
|
|
2214
|
+
/** Whether a button (`PointerEvent.button`: 0 left / primary finger, 1 middle, 2 right, ...) is down. */
|
|
2215
|
+
isButtonDown(buttonNumber) {
|
|
2216
|
+
return this._state[`button${buttonNumber}`]?.state === 1;
|
|
2217
|
+
}
|
|
2218
|
+
/** 1 if the button is down, else 0. */
|
|
2219
|
+
getButtonState(buttonNumber) {
|
|
2220
|
+
return this._state[`button${buttonNumber}`]?.state ?? 0;
|
|
2221
|
+
}
|
|
2222
|
+
};
|
|
2223
|
+
//#endregion
|
|
1932
2224
|
//#region src/observable.ts
|
|
1933
2225
|
/**
|
|
1934
2226
|
* A set of observers notified with a value (plus optional extra arguments). Observers are
|
|
@@ -1981,7 +2273,7 @@ var Observable = class {
|
|
|
1981
2273
|
}
|
|
1982
2274
|
};
|
|
1983
2275
|
//#endregion
|
|
1984
|
-
//#region src/input/input.ts
|
|
2276
|
+
//#region src/input/input-action-map.ts
|
|
1985
2277
|
const ANY = -1;
|
|
1986
2278
|
const now = () => performance.now();
|
|
1987
2279
|
const MOUSE_BUTTONS = [
|
|
@@ -1991,40 +2283,41 @@ const MOUSE_BUTTONS = [
|
|
|
1991
2283
|
"back",
|
|
1992
2284
|
"forward"
|
|
1993
2285
|
];
|
|
1994
|
-
const BUTTON_BITS = [
|
|
1995
|
-
1,
|
|
1996
|
-
4,
|
|
1997
|
-
2,
|
|
1998
|
-
8,
|
|
1999
|
-
16
|
|
2000
|
-
];
|
|
2001
2286
|
const isEditable = (target) => {
|
|
2002
2287
|
const element = target;
|
|
2003
2288
|
const tag = element?.tagName;
|
|
2004
2289
|
return !!element && (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT" || element.isContentEditable === true);
|
|
2005
2290
|
};
|
|
2006
2291
|
/**
|
|
2007
|
-
* Game input with a Godot-style action map: bind keys, buttons,
|
|
2008
|
-
* to named actions, then query actions instead of devices
|
|
2292
|
+
* Game input with a Godot-style action map, built on {@link Input}'s events: bind keys, buttons,
|
|
2293
|
+
* sticks, gestures and VR controls to named actions, then query actions instead of devices once
|
|
2294
|
+
* per frame. Gamepads and VR controllers are polled by `update()`.
|
|
2009
2295
|
*
|
|
2010
|
-
*
|
|
2011
|
-
*
|
|
2296
|
+
* Input.instance.target = canvas;
|
|
2297
|
+
*
|
|
2298
|
+
* const controls = new InputActionMap();
|
|
2299
|
+
* controls.actions.add('jump', [key('Space'), pad('A')]);
|
|
2012
2300
|
*
|
|
2013
2301
|
* // every frame, before game logic:
|
|
2014
|
-
*
|
|
2015
|
-
* if (
|
|
2302
|
+
* controls.update();
|
|
2303
|
+
* if (controls.isActionJustPressed('jump')) player.jump();
|
|
2016
2304
|
*
|
|
2017
|
-
* Call `dispose()` to
|
|
2305
|
+
* Call `dispose()` to stop listening.
|
|
2018
2306
|
*
|
|
2019
2307
|
* @category Input
|
|
2020
2308
|
*/
|
|
2021
|
-
var
|
|
2022
|
-
/** the actions to query; holds the `ui_*` defaults unless {@link
|
|
2309
|
+
var InputActionMap = class {
|
|
2310
|
+
/** the actions to query; holds the `ui_*` defaults unless {@link InputActionMapOptions.defaults} is false */
|
|
2023
2311
|
actions = new ActionMap();
|
|
2024
2312
|
/** Thresholds for taps, swipes etc. can be tuned via `gestureRecognizer.thresholds`. */
|
|
2025
2313
|
gestureRecognizer = new GestureRecognizer();
|
|
2026
|
-
/**
|
|
2314
|
+
/**
|
|
2315
|
+
* Touch gestures from the last frame: `events` (each recognized gesture and where it happened,
|
|
2316
|
+
* e.g. the tap that triggered an action) and the two-finger values (pinch 1 / pan 0 / rotate 0
|
|
2317
|
+
* when not gesturing).
|
|
2318
|
+
*/
|
|
2027
2319
|
gestures = {
|
|
2320
|
+
events: [],
|
|
2028
2321
|
pinch: 1,
|
|
2029
2322
|
pan: {
|
|
2030
2323
|
x: 0,
|
|
@@ -2032,6 +2325,8 @@ var Input = class {
|
|
|
2032
2325
|
},
|
|
2033
2326
|
rotate: 0
|
|
2034
2327
|
};
|
|
2328
|
+
/** the {@link Input} events are read from */
|
|
2329
|
+
input;
|
|
2035
2330
|
/** Fingers currently on the target. */
|
|
2036
2331
|
get touches() {
|
|
2037
2332
|
return this.gestureRecognizer.touches;
|
|
@@ -2051,7 +2346,7 @@ var Input = class {
|
|
|
2051
2346
|
tiltX: 0,
|
|
2052
2347
|
tiltY: 0
|
|
2053
2348
|
};
|
|
2054
|
-
/** notified when {@link
|
|
2349
|
+
/** notified when {@link InputActionMap.lastDevice} changes, e.g. to switch on-screen button prompts */
|
|
2055
2350
|
onDeviceChange = new Observable();
|
|
2056
2351
|
/** notified from update() when a gamepad is first seen */
|
|
2057
2352
|
onGamepadConnected = new Observable();
|
|
@@ -2068,7 +2363,6 @@ var Input = class {
|
|
|
2068
2363
|
}
|
|
2069
2364
|
_lastDevice = "keyboard";
|
|
2070
2365
|
_keyLabels;
|
|
2071
|
-
_target;
|
|
2072
2366
|
_options;
|
|
2073
2367
|
_listeners = [];
|
|
2074
2368
|
_controls = /* @__PURE__ */ new Map([[0, /* @__PURE__ */ new Map()]]);
|
|
@@ -2086,48 +2380,40 @@ var Input = class {
|
|
|
2086
2380
|
get lastDevice() {
|
|
2087
2381
|
return this._lastDevice;
|
|
2088
2382
|
}
|
|
2089
|
-
/** Whether the pointer is locked to the target, see {@link
|
|
2383
|
+
/** Whether the pointer is locked to the target, see {@link InputActionMap.lockPointer}. */
|
|
2090
2384
|
get isPointerLocked() {
|
|
2091
|
-
return typeof document !== "undefined" &&
|
|
2385
|
+
return typeof document !== "undefined" && document.pointerLockElement === this.input.target;
|
|
2092
2386
|
}
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
*/
|
|
2096
|
-
constructor(target, options = {}) {
|
|
2097
|
-
this._target = target;
|
|
2387
|
+
constructor(options = {}) {
|
|
2388
|
+
this.input = options.input ?? Input.instance;
|
|
2098
2389
|
this._options = {
|
|
2099
2390
|
defaults: options.defaults ?? true,
|
|
2100
2391
|
preventDefault: options.preventDefault ?? true,
|
|
2101
2392
|
preventContextMenu: options.preventContextMenu ?? true
|
|
2102
2393
|
};
|
|
2103
2394
|
if (this._options.defaults) this.actions.addDefaults();
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
this._listen(target, "pointerup", (e) => this._onPointerUp(e));
|
|
2118
|
-
this._listen(target, "pointercancel", (e) => this._onPointerCancel(e));
|
|
2119
|
-
this._listen(target, "pointermove", (e) => this._onPointerMove(e));
|
|
2120
|
-
this._listen(target, "wheel", (e) => this._onWheel(e), { passive: false });
|
|
2121
|
-
this._listen(target, "contextmenu", (e) => {
|
|
2122
|
-
if (this._options.preventContextMenu) e.preventDefault();
|
|
2123
|
-
});
|
|
2124
|
-
}
|
|
2395
|
+
if (this._options.preventContextMenu) this.input.disableContextMenu();
|
|
2396
|
+
this._listen("key_down", (e) => this._onKeyDown(e));
|
|
2397
|
+
this._listen("key_repeat", (e) => this._onKeyDown(e));
|
|
2398
|
+
this._listen("key_up", (e) => this._onKeyUp(e));
|
|
2399
|
+
this._listen("button_down", (e) => this._onButton(e, 1));
|
|
2400
|
+
this._listen("button_up", (e) => this._onButton(e, 0));
|
|
2401
|
+
this._listen("pointer_move", (e) => this._onPointerMove(e));
|
|
2402
|
+
this._listen("touch_down", (e) => this._onTouchDown(e));
|
|
2403
|
+
this._listen("touch_move", (e) => this._onTouchMove(e));
|
|
2404
|
+
this._listen("touch_up", (e) => this._onTouchUp(e));
|
|
2405
|
+
this._listen("touch_cancel", (e) => this.gestureRecognizer.cancel(e.pointerId));
|
|
2406
|
+
this._listen("mouse_wheel", (e) => this._onWheel(e));
|
|
2407
|
+
this._listen("blur", () => this.releaseAll(0));
|
|
2125
2408
|
}
|
|
2126
2409
|
/**
|
|
2127
2410
|
* Call once per frame, before reading input: polls devices and advances "just pressed" /
|
|
2128
2411
|
* "just released" by one frame. Action callbacks fire from here.
|
|
2412
|
+
*
|
|
2413
|
+
* @param time current time in ms (e.g. the `requestAnimationFrame` timestamp), used for
|
|
2414
|
+
* gesture timing and auto-repeat; default `performance.now()`
|
|
2129
2415
|
*/
|
|
2130
|
-
update() {
|
|
2416
|
+
update(time = now()) {
|
|
2131
2417
|
this.pointer.deltaX = this._pending.deltaX;
|
|
2132
2418
|
this.pointer.deltaY = this._pending.deltaY;
|
|
2133
2419
|
this.pointer.wheelX = this._pending.wheelX;
|
|
@@ -2135,7 +2421,7 @@ var Input = class {
|
|
|
2135
2421
|
this._pending.deltaX = this._pending.deltaY = this._pending.wheelX = this._pending.wheelY = 0;
|
|
2136
2422
|
this._pollGamepads();
|
|
2137
2423
|
this._pollXR();
|
|
2138
|
-
this._updateGestures();
|
|
2424
|
+
this._updateGestures(time);
|
|
2139
2425
|
const devices = [...this._controls.keys()];
|
|
2140
2426
|
const fired = [];
|
|
2141
2427
|
for (const name of this.actions.names) {
|
|
@@ -2147,29 +2433,43 @@ var Input = class {
|
|
|
2147
2433
|
let raw = 0;
|
|
2148
2434
|
for (const binding of action.bindings) raw = Math.max(raw, this._bindingValue(binding, device));
|
|
2149
2435
|
anyRaw = Math.max(anyRaw, raw);
|
|
2150
|
-
|
|
2436
|
+
const phase = this._advance(states, device, raw, action, time);
|
|
2437
|
+
if (phase) fired.push({
|
|
2151
2438
|
action: name,
|
|
2152
|
-
phase
|
|
2153
|
-
device
|
|
2439
|
+
phase,
|
|
2440
|
+
device,
|
|
2441
|
+
repeat: states.get(device).repeated
|
|
2154
2442
|
});
|
|
2155
2443
|
}
|
|
2156
|
-
|
|
2444
|
+
const phase = this._advance(states, ANY, anyRaw, action, time);
|
|
2445
|
+
if (phase) fired.push({
|
|
2157
2446
|
action: name,
|
|
2158
|
-
phase
|
|
2159
|
-
device: ANY
|
|
2447
|
+
phase,
|
|
2448
|
+
device: ANY,
|
|
2449
|
+
repeat: states.get(ANY).repeated
|
|
2160
2450
|
});
|
|
2161
2451
|
}
|
|
2162
2452
|
for (const controls of this._controls.values()) for (const state of controls.values()) state.pressed = false;
|
|
2163
|
-
for (const { action, phase, device } of fired) for (const listener of [...this._actionListeners]) if (listener.action === action && listener.phase === phase && listener.device === device) listener.callback(
|
|
2453
|
+
for (const { action, phase, device, repeat } of fired) for (const listener of [...this._actionListeners]) if (listener.action === action && listener.phase === phase && listener.device === device) listener.callback({
|
|
2454
|
+
action,
|
|
2455
|
+
repeat
|
|
2456
|
+
});
|
|
2164
2457
|
}
|
|
2165
2458
|
/** Whether the action is held past its deadzone. Unknown actions return false (and warn once). */
|
|
2166
2459
|
isActionPressed(action, query) {
|
|
2167
2460
|
return this._state(action, query)?.pressed ?? false;
|
|
2168
2461
|
}
|
|
2169
|
-
/**
|
|
2462
|
+
/**
|
|
2463
|
+
* Whether the action became pressed in the last update(), or auto-repeated while held if the
|
|
2464
|
+
* action has {@link Action.repeat} (e.g. to keep walking while a direction is held).
|
|
2465
|
+
*/
|
|
2170
2466
|
isActionJustPressed(action, query) {
|
|
2171
2467
|
const state = this._state(action, query);
|
|
2172
|
-
return !!state && state.pressed && !state.wasPressed;
|
|
2468
|
+
return !!state && state.pressed && (!state.wasPressed || state.repeated);
|
|
2469
|
+
}
|
|
2470
|
+
/** Whether the last update()'s press of the action was an auto-repeat rather than a new press. */
|
|
2471
|
+
isActionRepeated(action, query) {
|
|
2472
|
+
return this._state(action, query)?.repeated ?? false;
|
|
2173
2473
|
}
|
|
2174
2474
|
/** Whether the action was released in the last update(). */
|
|
2175
2475
|
isActionJustReleased(action, query) {
|
|
@@ -2219,7 +2519,11 @@ var Input = class {
|
|
|
2219
2519
|
y
|
|
2220
2520
|
};
|
|
2221
2521
|
}
|
|
2222
|
-
/**
|
|
2522
|
+
/**
|
|
2523
|
+
* Calls `callback` when the action is pressed or released (during update()). With
|
|
2524
|
+
* {@link Action.repeat}, `'pressed'` also fires on each auto-repeat, with `event.repeat` set.
|
|
2525
|
+
* Returns an unsubscribe function.
|
|
2526
|
+
*/
|
|
2223
2527
|
onAction(action, phase, callback, query = {}) {
|
|
2224
2528
|
const listener = {
|
|
2225
2529
|
action,
|
|
@@ -2257,7 +2561,7 @@ var Input = class {
|
|
|
2257
2561
|
}
|
|
2258
2562
|
/** Requests pointer lock on the target (browsers only allow it from a user gesture, e.g. a click). */
|
|
2259
2563
|
lockPointer() {
|
|
2260
|
-
this.
|
|
2564
|
+
this.input.target.requestPointerLock?.();
|
|
2261
2565
|
}
|
|
2262
2566
|
/** Exits pointer lock if the target holds it. */
|
|
2263
2567
|
unlockPointer() {
|
|
@@ -2277,7 +2581,7 @@ var Input = class {
|
|
|
2277
2581
|
};
|
|
2278
2582
|
});
|
|
2279
2583
|
}
|
|
2280
|
-
/** Ends a pending {@link
|
|
2584
|
+
/** Ends a pending {@link InputActionMap.captureBinding}, resolving it with `null`. */
|
|
2281
2585
|
cancelCapture() {
|
|
2282
2586
|
const capture = this._capture;
|
|
2283
2587
|
this._capture = void 0;
|
|
@@ -2296,51 +2600,59 @@ var Input = class {
|
|
|
2296
2600
|
}
|
|
2297
2601
|
return this._keyLabels;
|
|
2298
2602
|
}
|
|
2299
|
-
/** The layout loaded by {@link
|
|
2603
|
+
/** The layout loaded by {@link InputActionMap.loadKeyboardLayout}, if any: `KeyboardEvent.code` to printed key. */
|
|
2300
2604
|
get keyLabels() {
|
|
2301
2605
|
return this._keyLabels;
|
|
2302
2606
|
}
|
|
2303
2607
|
/** Removes all event listeners and action callbacks, cancels any capture and detaches XR. */
|
|
2304
2608
|
dispose() {
|
|
2305
|
-
for (const [
|
|
2609
|
+
for (const [type, listener] of this._listeners) this.input.off(type, listener);
|
|
2306
2610
|
this._listeners.length = 0;
|
|
2307
2611
|
this._actionListeners.length = 0;
|
|
2308
2612
|
this.cancelCapture();
|
|
2309
2613
|
this.detachXR();
|
|
2310
2614
|
}
|
|
2311
|
-
_listen(
|
|
2312
|
-
|
|
2313
|
-
this._listeners.push([
|
|
2314
|
-
target,
|
|
2315
|
-
type,
|
|
2316
|
-
listener,
|
|
2317
|
-
options
|
|
2318
|
-
]);
|
|
2615
|
+
_listen(type, listener) {
|
|
2616
|
+
this.input.on(type, listener);
|
|
2617
|
+
this._listeners.push([type, listener]);
|
|
2319
2618
|
}
|
|
2320
2619
|
_state(action, query) {
|
|
2321
2620
|
if (!this.actions.has(action)) {
|
|
2322
2621
|
if (!this._warned.has(action)) {
|
|
2323
2622
|
this._warned.add(action);
|
|
2324
|
-
console.warn(`
|
|
2623
|
+
console.warn(`InputActionMap: unknown action "${action}"`);
|
|
2325
2624
|
}
|
|
2326
2625
|
return;
|
|
2327
2626
|
}
|
|
2328
2627
|
return this._actionStates.get(action)?.get(query?.device ?? ANY);
|
|
2329
2628
|
}
|
|
2330
|
-
_advance(states, device, raw,
|
|
2629
|
+
_advance(states, device, raw, action, time) {
|
|
2630
|
+
const deadzone = action.deadzone;
|
|
2331
2631
|
let state = states.get(device);
|
|
2332
2632
|
if (!state) states.set(device, state = {
|
|
2333
2633
|
raw: 0,
|
|
2334
2634
|
strength: 0,
|
|
2335
2635
|
pressed: false,
|
|
2336
|
-
wasPressed: false
|
|
2636
|
+
wasPressed: false,
|
|
2637
|
+
repeated: false,
|
|
2638
|
+
nextRepeat: 0
|
|
2337
2639
|
});
|
|
2338
2640
|
state.wasPressed = state.pressed;
|
|
2641
|
+
state.repeated = false;
|
|
2339
2642
|
state.raw = raw;
|
|
2340
2643
|
state.pressed = raw > 0 && raw >= deadzone;
|
|
2341
2644
|
state.strength = !state.pressed ? 0 : deadzone >= 1 ? 1 : Math.min(1, (raw - deadzone) / (1 - deadzone));
|
|
2342
2645
|
if (state.pressed && raw >= 1) state.strength = 1;
|
|
2343
|
-
|
|
2646
|
+
if (state.pressed !== state.wasPressed) {
|
|
2647
|
+
if (state.pressed && action.repeat) state.nextRepeat = time + action.repeat.delay;
|
|
2648
|
+
return state.pressed ? "pressed" : "released";
|
|
2649
|
+
}
|
|
2650
|
+
if (state.pressed && action.repeat && time >= state.nextRepeat) {
|
|
2651
|
+
const next = state.nextRepeat + action.repeat.interval;
|
|
2652
|
+
state.repeated = true;
|
|
2653
|
+
state.nextRepeat = next > time ? next : time + action.repeat.interval;
|
|
2654
|
+
return "pressed";
|
|
2655
|
+
}
|
|
2344
2656
|
}
|
|
2345
2657
|
_controlValue(device, id) {
|
|
2346
2658
|
const state = this._controls.get(device)?.get(id);
|
|
@@ -2465,8 +2777,9 @@ var Input = class {
|
|
|
2465
2777
|
});
|
|
2466
2778
|
}
|
|
2467
2779
|
}
|
|
2468
|
-
_updateGestures() {
|
|
2469
|
-
const frame = this.gestureRecognizer.update(
|
|
2780
|
+
_updateGestures(time) {
|
|
2781
|
+
const frame = this.gestureRecognizer.update(time);
|
|
2782
|
+
this.gestures.events = frame.events;
|
|
2470
2783
|
this.gestures.pinch = frame.pinch;
|
|
2471
2784
|
this.gestures.pan = frame.pan;
|
|
2472
2785
|
this.gestures.rotate = frame.rotate;
|
|
@@ -2478,7 +2791,7 @@ var Input = class {
|
|
|
2478
2791
|
_xrSession;
|
|
2479
2792
|
_xrEnd;
|
|
2480
2793
|
_xrHeld = /* @__PURE__ */ new Set();
|
|
2481
|
-
/** The session set by {@link
|
|
2794
|
+
/** The session set by {@link InputActionMap.attachXR}, until detached or ended. */
|
|
2482
2795
|
get xrSession() {
|
|
2483
2796
|
return this._xrSession;
|
|
2484
2797
|
}
|
|
@@ -2571,79 +2884,51 @@ var Input = class {
|
|
|
2571
2884
|
}, 0);
|
|
2572
2885
|
}
|
|
2573
2886
|
_updatePointer(event) {
|
|
2574
|
-
const rect = this._target?.getBoundingClientRect?.();
|
|
2575
|
-
const left = rect?.left ?? 0;
|
|
2576
|
-
const top = rect?.top ?? 0;
|
|
2577
|
-
const width = rect?.width || 1;
|
|
2578
|
-
const height = rect?.height || 1;
|
|
2579
2887
|
this.pointer.type = event.pointerType === "pen" ? "pen" : "mouse";
|
|
2580
|
-
this.pointer.x =
|
|
2581
|
-
this.pointer.y =
|
|
2582
|
-
this.pointer.ndcX = this.
|
|
2583
|
-
this.pointer.ndcY =
|
|
2888
|
+
this.pointer.x = this.input.pointerPosition.x;
|
|
2889
|
+
this.pointer.y = this.input.pointerPosition.y;
|
|
2890
|
+
this.pointer.ndcX = this.input.pointerPositionNdc.x;
|
|
2891
|
+
this.pointer.ndcY = this.input.pointerPositionNdc.y;
|
|
2584
2892
|
this.pointer.pressure = event.pressure ?? 0;
|
|
2585
2893
|
this.pointer.tiltX = event.tiltX ?? 0;
|
|
2586
2894
|
this.pointer.tiltY = event.tiltY ?? 0;
|
|
2587
2895
|
}
|
|
2588
2896
|
_local(event) {
|
|
2589
|
-
const rect = this.
|
|
2897
|
+
const rect = this.input.target.getBoundingClientRect?.();
|
|
2590
2898
|
return {
|
|
2591
2899
|
x: event.clientX - (rect?.left ?? 0),
|
|
2592
2900
|
y: event.clientY - (rect?.top ?? 0)
|
|
2593
2901
|
};
|
|
2594
2902
|
}
|
|
2595
|
-
|
|
2596
|
-
if (event.pointerType === "touch")
|
|
2597
|
-
const { x, y } = this._local(event);
|
|
2598
|
-
this.gestureRecognizer.down(event.pointerId, x, y, now());
|
|
2599
|
-
this._setLastDevice("touch");
|
|
2600
|
-
this._target?.setPointerCapture?.(event.pointerId);
|
|
2601
|
-
return;
|
|
2602
|
-
}
|
|
2903
|
+
_onButton(event, value) {
|
|
2904
|
+
if (event.pointerType === "touch") return;
|
|
2603
2905
|
this._updatePointer(event);
|
|
2604
2906
|
this._setLastDevice(event.pointerType === "pen" ? "pen" : "mouse");
|
|
2605
2907
|
const button = MOUSE_BUTTONS[event.button];
|
|
2606
2908
|
if (button) this._setControl(0, {
|
|
2607
2909
|
type: "mouse",
|
|
2608
2910
|
button
|
|
2609
|
-
},
|
|
2610
|
-
this._target?.setPointerCapture?.(event.pointerId);
|
|
2611
|
-
}
|
|
2612
|
-
_onPointerUp(event) {
|
|
2613
|
-
if (event.pointerType === "touch") {
|
|
2614
|
-
const { x, y } = this._local(event);
|
|
2615
|
-
this.gestureRecognizer.up(event.pointerId, x, y, now());
|
|
2616
|
-
return;
|
|
2617
|
-
}
|
|
2618
|
-
this._updatePointer(event);
|
|
2619
|
-
const button = MOUSE_BUTTONS[event.button];
|
|
2620
|
-
if (button) this._setControl(0, {
|
|
2621
|
-
type: "mouse",
|
|
2622
|
-
button
|
|
2623
|
-
}, 0);
|
|
2624
|
-
}
|
|
2625
|
-
_onPointerCancel(event) {
|
|
2626
|
-
if (event.pointerType === "touch") this.gestureRecognizer.cancel(event.pointerId);
|
|
2627
|
-
else this._onPointerUp(event);
|
|
2911
|
+
}, value);
|
|
2628
2912
|
}
|
|
2629
2913
|
_onPointerMove(event) {
|
|
2630
|
-
if (event.pointerType === "touch")
|
|
2631
|
-
const { x, y } = this._local(event);
|
|
2632
|
-
this.gestureRecognizer.move(event.pointerId, x, y);
|
|
2633
|
-
return;
|
|
2634
|
-
}
|
|
2914
|
+
if (event.pointerType === "touch") return;
|
|
2635
2915
|
this._updatePointer(event);
|
|
2636
2916
|
this._setLastDevice(event.pointerType === "pen" ? "pen" : "mouse");
|
|
2637
2917
|
this._pending.deltaX += event.movementX ?? 0;
|
|
2638
2918
|
this._pending.deltaY += event.movementY ?? 0;
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
}
|
|
2919
|
+
}
|
|
2920
|
+
_onTouchDown(event) {
|
|
2921
|
+
const { x, y } = this._local(event);
|
|
2922
|
+
this.gestureRecognizer.down(event.pointerId, x, y, now());
|
|
2923
|
+
this._setLastDevice("touch");
|
|
2924
|
+
}
|
|
2925
|
+
_onTouchMove(event) {
|
|
2926
|
+
const { x, y } = this._local(event);
|
|
2927
|
+
this.gestureRecognizer.move(event.pointerId, x, y);
|
|
2928
|
+
}
|
|
2929
|
+
_onTouchUp(event) {
|
|
2930
|
+
const { x, y } = this._local(event);
|
|
2931
|
+
this.gestureRecognizer.up(event.pointerId, x, y, now());
|
|
2647
2932
|
}
|
|
2648
2933
|
_onWheel(event) {
|
|
2649
2934
|
const scale = event.deltaMode === 1 ? 16 : event.deltaMode === 2 ? 800 : 1;
|
|
@@ -3708,6 +3993,7 @@ exports.DEFAULT_FONT_SIZE = DEFAULT_FONT_SIZE;
|
|
|
3708
3993
|
exports.DEFAULT_MAP_2D_VALUE = DEFAULT_MAP_2D_VALUE;
|
|
3709
3994
|
exports.DEFAULT_MAP_3D_VALUE = require_spatial_partition_3d.DEFAULT_MAP_3D_VALUE;
|
|
3710
3995
|
exports.DEFAULT_PIXEL_SIZE = DEFAULT_PIXEL_SIZE;
|
|
3996
|
+
exports.DEFAULT_REPEAT = DEFAULT_REPEAT;
|
|
3711
3997
|
exports.DEFAULT_TEXTALIGN = DEFAULT_TEXTALIGN;
|
|
3712
3998
|
exports.DEFAULT_TEXTBASELINE = DEFAULT_TEXTBASELINE;
|
|
3713
3999
|
exports.DEG2RAD = require_spatial_partition_3d.DEG2RAD;
|
|
@@ -3733,6 +4019,7 @@ exports.GetProgramLocations = GetProgramLocations;
|
|
|
3733
4019
|
exports.GetUrlParams = GetUrlParams;
|
|
3734
4020
|
exports.HALF_PI = require_spatial_partition_3d.HALF_PI;
|
|
3735
4021
|
exports.Input = Input;
|
|
4022
|
+
exports.InputActionMap = InputActionMap;
|
|
3736
4023
|
exports.LN10 = require_spatial_partition_3d.LN10;
|
|
3737
4024
|
exports.LN2 = require_spatial_partition_3d.LN2;
|
|
3738
4025
|
exports.LOG10E = require_spatial_partition_3d.LOG10E;
|