aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2025-03-13 09:21:15 +0000
committerGitHub <noreply@github.com>2025-03-13 09:21:15 +0000
commit9b7aebe2b6e74a5b00a8a21d1494ec19684b367b (patch)
treef64b10e91ca2f796e4487e466f14d4e9ecd791bb
parentd3b1aaad18e7a1445992aea76dfb6fa00e5a9d80 (diff)
parentd349c96071359c7e13f3b881cd6daa8f8ef0c827 (diff)
Merge pull request #4930 from laytan/js-gamepad-improvements
core/sys/wasm/js: improve gamepad API
-rw-r--r--core/sys/wasm/js/events.odin13
-rw-r--r--core/sys/wasm/js/odin.js44
2 files changed, 46 insertions, 11 deletions
diff --git a/core/sys/wasm/js/events.odin b/core/sys/wasm/js/events.odin
index ffa3a1202..4e3a0b1aa 100644
--- a/core/sys/wasm/js/events.odin
+++ b/core/sys/wasm/js/events.odin
@@ -189,7 +189,7 @@ Key_Location :: enum u8 {
KEYBOARD_MAX_KEY_SIZE :: 32
KEYBOARD_MAX_CODE_SIZE :: 32
-GAMEPAD_MAX_ID_SIZE :: 64
+GAMEPAD_MAX_ID_SIZE :: 96
GAMEPAD_MAX_MAPPING_SIZE :: 64
GAMEPAD_MAX_BUTTONS :: 64
@@ -384,7 +384,14 @@ get_gamepad_state :: proc "contextless" (index: int, s: ^Gamepad_State) -> bool
if s == nil {
return false
}
- return _get_gamepad_state(index, s)
+
+ if !_get_gamepad_state(index, s) {
+ return false
+ }
+
+ s.id = string(s._id_buf[:s._id_len])
+ s.mapping = string(s._mapping_buf[:s._mapping_len])
+ return true
}
@@ -415,4 +422,4 @@ do_event_callback :: proc(user_data: rawptr, callback: proc(e: Event)) {
callback(event)
}
-} \ No newline at end of file
+}
diff --git a/core/sys/wasm/js/odin.js b/core/sys/wasm/js/odin.js
index 29227c526..6ca6567ca 100644
--- a/core/sys/wasm/js/odin.js
+++ b/core/sys/wasm/js/odin.js
@@ -1588,10 +1588,24 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement, memory) {
}
}
- wmi.storeInt(off(W, W), e.gamepad.id.length)
- wmi.storeInt(off(W, W), e.gamepad.mapping.length)
- wmi.storeString(off(64, 1), e.gamepad.id);
- wmi.storeString(off(64, 1), e.gamepad.mapping);
+ let idLength = e.gamepad.id.length;
+ let id = e.gamepad.id;
+ if (idLength > 96) {
+ idLength = 96;
+ id = id.slice(0, 93) + '...';
+ }
+
+ let mappingLength = e.gamepad.mapping.length;
+ let mapping = e.gamepad.mapping;
+ if (mappingLength > 64) {
+ mappingLength = 61;
+ mapping = mapping.slice(0, 61) + '...';
+ }
+
+ wmi.storeInt(off(W, W), idLength);
+ wmi.storeInt(off(W, W), mappingLength);
+ wmi.storeString(off(96, 1), id);
+ wmi.storeString(off(64, 1), mapping);
}
},
@@ -1756,10 +1770,24 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement, memory) {
}
}
- wmi.storeInt(off(W, W), gamepad.id.length)
- wmi.storeInt(off(W, W), gamepad.mapping.length)
- wmi.storeString(off(64, 1), gamepad.id);
- wmi.storeString(off(64, 1), gamepad.mapping);
+ let idLength = gamepad.id.length;
+ let id = gamepad.id;
+ if (idLength > 96) {
+ idLength = 96;
+ id = id.slice(0, 93) + '...';
+ }
+
+ let mappingLength = gamepad.mapping.length;
+ let mapping = gamepad.mapping;
+ if (mappingLength > 64) {
+ mappingLength = 61;
+ mapping = mapping.slice(0, 61) + '...';
+ }
+
+ wmi.storeInt(off(W, W), idLength);
+ wmi.storeInt(off(W, W), mappingLength);
+ wmi.storeString(off(96, 1), id);
+ wmi.storeString(off(64, 1), mapping);
return true;
}