pixijs-input-devices 0.1.2 → 0.1.3

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 CHANGED
@@ -2,46 +2,96 @@
2
2
 
3
3
  WIP
4
4
 
5
- - Adds support for ⌨️ keyboards, 🎮 gamepads, and other human interface devices
6
- - Built-in event API for navigating user-focusable elements
7
- - Built-in polling API for real-time inputs
5
+ - Adds support for ⌨️ **Keyboard**, 🎮 **Gamepads**, and other human-interface devices
6
+ - A simple `Navigation` API which hooks devices into existing pointer events
7
+ - A powerful event-based API for event-driven interaction
8
+ - and of course, a high-performance API for real-time applications
8
9
 
9
10
  <hr/>
10
11
 
12
+ ### 💿 Install
13
+
14
+ ```sh
15
+ npm i pixijs-input-devices
16
+ ```
17
+
18
+ Then inste
19
+
20
+ ```ts
21
+ // 1. set up ticker (or put in update loop)
22
+ Ticker.shared.add(() =>
23
+ InputDevice.shared.update()
24
+ );
25
+
26
+ // 2. register container mixin
27
+ registerPixiJSInputDeviceMixin( Container );
28
+
29
+ // 3. (optional) enable auto-navigation
30
+ Navigation.shared.stageRoot = app.stage;
31
+ ```
32
+
33
+ ### Event API
34
+
11
35
  ```ts
12
- // set up ticker (or put in your main update loop):
13
- Ticker.shared.add( () => InputDeviceManager.shared.update() );
36
+ // use the button offsets
37
+ InputDevice.gamepads[0].on( Button.A, () =>
38
+ alert("I've been pressed!")
39
+ );
40
+
41
+ // or the common labels
42
+ InputDevice.gamepads[0].on( "Back", () =>
43
+ alert("Let's get out of here.")
44
+ );
14
45
  ```
15
46
 
16
- ### Realtime API (polling)
47
+ ### Realtime API
17
48
 
18
- Simple example
49
+ Simple example:
19
50
 
20
51
  ```ts
21
52
  let jump = false;
22
53
  let hide = false;
23
54
  let moveX = 0.0;
24
55
 
25
- for ( const device of InputDeviceManager.shared.devices )
56
+ for ( const device of InputDevice.shared.devices )
26
57
  {
27
58
  if ( device.type === "keyboard" )
28
59
  {
29
- if ( device.key["ArrowUp"] ) jump = true
30
- if ( device.key["ArrowDown"] ) hide = true
31
- if ( device.key["ArrowLeft"] ) moveX = -1.0
32
- if ( device.key["ArrowRight"] ) moveX = 1.0
60
+ if ( device.key.ArrowUp ) jump = true
61
+ if ( device.key.ArrowDown ) hide = true
62
+ if ( device.key.ArrowLeft ) moveX = -1.0
63
+ if ( device.key.ArrowRight ) moveX = 1.0
33
64
  }
65
+
34
66
  else if ( device.type === "gamepad" )
35
67
  {
36
- if ( device.button[Button.A] ) jump = true
37
- if ( device.rightTrigger >= 0.25 ) hide = true
68
+ if ( device.button.A ) jump = true
69
+ if ( device.rightTrigger > 0.25 ) hide = true
38
70
  if ( device.leftJoystick.x !== 0.0 ) moveX = device.leftJoystick.x
71
+
72
+ device.playVibration({ duration: 100 });
73
+ }
74
+ }
75
+ ```
76
+
77
+ #### Assigning devices
78
+
79
+ ```ts
80
+ InputDevice.shared.on("deviceconnected", ({ device }) => {
81
+ device.meta.localPlayerId = 123;
82
+ });
83
+
84
+ for ( const device of InputDevice.shared.devices )
85
+ {
86
+ if ( device.meta.localPlayerId === 123 )
87
+ {
88
+ // do something
39
89
  }
40
90
  }
41
91
  ```
42
92
 
43
93
  > [!NOTE]
44
- > You can use `device.button[Button.RightTrigger]` to detect triggers too.
94
+ > You may also use `device.button.RightTrigger` to detect triggers as a button press.
45
95
 
46
96
  > [!NOTE]
47
97
  > You can use `device.pressingAll([...])` and `device.pressingAny([...])` to check groups of keys/buttons.
@@ -52,9 +102,23 @@ for ( const device of InputDeviceManager.shared.devices )
52
102
 
53
103
  ### Event-driven API
54
104
 
55
- ```
56
- InputDeviceManager.shared.keyboard.bindGlobal({
57
- { key: "KeyA", action}
105
+ ```ts
106
+ InputDevice.keyboard.bind( "KeyW", (device, code, label) => {
107
+ console.log( "key in W position was pressed. label:", label );
108
+ });
109
+
110
+ InputDevice.gamepads.bind({
111
+ Back: () => {
112
+
113
+ }
114
+ })
115
+
116
+ InputDevice.gamepad[0].bind({
117
+ KeyA: () => {},
118
+ Space: {
119
+ keydown: () => {},
120
+ keyup: () => {},
121
+ }
58
122
  })
59
123
  ```
60
124