react-native-earl-gamepad 0.5.0 → 0.5.1

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
@@ -45,6 +45,50 @@ export function Controls() {
45
45
  }
46
46
  ```
47
47
 
48
+ ### Control Logic Example
49
+
50
+ Here is an example of mapping D-pad events to movement vectors
51
+
52
+ ```tsx
53
+ import { useState, useCallback } from "react";
54
+ import { GamepadBridge, type DpadEvent } from "react-native-earl-gamepad";
55
+ type MoveKey = keyof typeof MOVES;
56
+
57
+ const MOVES: Record<string, [number, number]> = {
58
+ up: [1, 0],
59
+ down: [-1, 0],
60
+ right: [0, 1],
61
+ left: [0, -1],
62
+ stop: [0, 0],
63
+
64
+ axis_left_x_neg: [0, -1],
65
+ axis_left_x_pos: [0, 1],
66
+ axis_left_y_pos: [1, 0],
67
+ axis_left_y_neg: [-1, 0],
68
+
69
+ // add more
70
+ }; // example only for the control logic
71
+
72
+ export function Controls() {
73
+ const [active, setActive] = useState<string | null>(null);
74
+
75
+ const handleDpad = useCallback(
76
+ (event: DpadEvent) => {
77
+ const key = event.key as MoveKey;
78
+ if (event.pressed) {
79
+ if (active !== key) {
80
+ console.log("Dpad press", key);
81
+ }
82
+ } else if (active === key) {
83
+ // do something
84
+ }
85
+ },
86
+ [active]
87
+ );
88
+ return <GamepadBridge enabled onDpad={handleDpad} axisThreshold={0.15} />;
89
+ }
90
+ ```
91
+
48
92
  ### Hook for stateful consumption
49
93
 
50
94
  `useGamepad` keeps pressed state and axes for you. You still need to render the provided `bridge` element once.
@@ -1,5 +1,5 @@
1
- import { StyleProp, ViewStyle } from 'react-native';
2
- import type { AxisEvent, ButtonEvent, DpadEvent, StatusEvent } from './types';
1
+ import { StyleProp, ViewStyle } from "react-native";
2
+ import type { AxisEvent, ButtonEvent, DpadEvent, StatusEvent } from "./types";
3
3
  type Props = {
4
4
  enabled?: boolean;
5
5
  axisThreshold?: number;
@@ -118,16 +118,16 @@ function GamepadBridge({ enabled = true, axisThreshold = 0.15, onDpad, onButton,
118
118
  const handleMessage = (event) => {
119
119
  try {
120
120
  const data = JSON.parse(event.nativeEvent.data);
121
- if (data.type === 'dpad') {
121
+ if (data.type === "dpad") {
122
122
  onDpad === null || onDpad === void 0 ? void 0 : onDpad(data);
123
123
  }
124
- else if (data.type === 'button') {
124
+ else if (data.type === "button") {
125
125
  onButton === null || onButton === void 0 ? void 0 : onButton(data);
126
126
  }
127
- else if (data.type === 'axis') {
127
+ else if (data.type === "axis") {
128
128
  onAxis === null || onAxis === void 0 ? void 0 : onAxis(data);
129
129
  }
130
- else if (data.type === 'status') {
130
+ else if (data.type === "status") {
131
131
  onStatus === null || onStatus === void 0 ? void 0 : onStatus(data);
132
132
  }
133
133
  }
@@ -135,5 +135,10 @@ function GamepadBridge({ enabled = true, axisThreshold = 0.15, onDpad, onButton,
135
135
  // ignore malformed messages
136
136
  }
137
137
  };
138
- return ((0, jsx_runtime_1.jsx)(react_native_webview_1.default, { ref: webviewRef, source: { html }, originWhitelist: ['*'], onMessage: handleMessage, javaScriptEnabled: true, onLoad: focusBridge, onLayout: focusBridge, onTouchStart: focusBridge, style: style !== null && style !== void 0 ? style : { width: 1, height: 1, position: 'absolute', opacity: 0 } }));
138
+ return ((0, jsx_runtime_1.jsx)(react_native_webview_1.default, { ref: webviewRef, source: { html }, originWhitelist: ["*"], onMessage: handleMessage, javaScriptEnabled: true, onLoad: focusBridge, onLayout: focusBridge, onTouchStart: focusBridge, style: style !== null && style !== void 0 ? style : {
139
+ width: 1,
140
+ height: 1,
141
+ position: "absolute",
142
+ opacity: 0,
143
+ } }));
139
144
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-earl-gamepad",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "React Native gamepad bridge via WebView (buttons, sticks, d-pad, status).",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",