Procházet zdrojové kódy

Add gamepad events

gingerBill před 1 rokem
rodič
revize
66e83ef30d
2 změnil soubory, kde provedl 47 přidání a 4 odebrání
  1. 30 2
      core/sys/wasm/js/events.odin
  2. 17 2
      core/sys/wasm/js/odin.js

+ 30 - 2
core/sys/wasm/js/events.odin

@@ -186,9 +186,18 @@ Key_Location :: enum u8 {
 	Numpad   = 3,
 }
 
-KEYBOARD_MAX_KEY_SIZE :: 16
+KEYBOARD_MAX_KEY_SIZE  :: 16
 KEYBOARD_MAX_CODE_SIZE :: 16
 
+GAMEPAD_MAX_ID_SIZE      :: 64
+GAMEPAD_MAX_MAPPING_SIZE :: 64
+
+Gamepad_Button :: struct {
+	pressed: bool,
+	touched: bool,
+	value:   f64,
+}
+
 Event_Target_Kind :: enum u32 {
 	Element  = 0,
 	Document = 1,
@@ -266,6 +275,21 @@ Event :: struct {
 			button:  i16,
 			buttons: bit_set[0..<16; u16],
 		},
+
+		gamepad: struct {
+			id:           string,
+			mapping:      string,
+			index:        int,
+			connected:    bool,
+			timestamp:    f64,
+			button_count: int,
+			axes_count:   int,
+
+			_id_len:      int `fmt:"-"`,
+			_mapping_len: int `fmt:"-"`,
+			_id_buf:      [GAMEPAD_MAX_ID_SIZE]byte      `fmt:"-"`,
+			_mapping_buf: [GAMEPAD_MAX_MAPPING_SIZE]byte `fmt:"-"`,
+		},
 	},
 
 
@@ -358,9 +382,13 @@ do_event_callback :: proc(user_data: rawptr, callback: proc(e: Event)) {
 
 		init_event_raw(&event)
 
-		if event.kind == .Key_Up || event.kind == .Key_Down || event.kind == .Key_Press {
+		#partial switch event.kind {
+		case .Key_Up, .Key_Down, .Key_Press:
 			event.key.key = string(event.key._key_buf[:event.key._key_len]) 
 			event.key.code = string(event.key._code_buf[:event.key._code_len]) 
+		case .Gamepad_Connected, .Gamepad_Disconnected:
+			event.gamepad.id = string(event.gamepad._id_buf[:event.gamepad._id_len])
+			event.gamepad.mapping = string(event.gamepad._mapping_buf[:event.gamepad._mapping_len])
 		}
 
 		callback(event)

+ 17 - 2
core/sys/wasm/js/odin.js

@@ -1533,8 +1533,8 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement, memory, ev
 
 					wmi.storeU8(off(1), !!e.repeat);
 
-					wmi.storeI32(off(W), e.key.length)
-					wmi.storeI32(off(W), e.code.length)
+					wmi.storeInt(off(W), e.key.length)
+					wmi.storeInt(off(W), e.code.length)
 					wmi.storeString(off(16, 1), e.key);
 					wmi.storeString(off(16, 1), e.code);
 				} else if (e.type === 'scroll') {
@@ -1542,6 +1542,21 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement, memory, ev
 					wmi.storeF64(off(8), window.scrollY);
 				} else if (e.type === 'visibilitychange') {
 					wmi.storeU8(off(1), !document.hidden);
+				} else if (e instanceof GamepadEvent) {
+					const idPtr      = off(W*2, W);
+					const mappingPtr = off(W*2, W);
+
+					wmi.storeI32(off(W), e.gamepad.index);
+					wmi.storeU8(off(1), !!e.gamepad.connected);
+					wmi.storeF64(off(8), e.gamepad.timestamp);
+
+					wmi.storeInt(off(W), e.gamepad.buttons.length);
+					wmi.storeInt(off(W), e.gamepad.axes.length);
+
+					wmi.storeInt(off(W), e.gamepad.id.length)
+					wmi.storeInt(off(W), e.gamepad.mapping.length)
+					wmi.storeString(off(64, 1), e.gamepad.id);
+					wmi.storeString(off(64, 1), e.gamepad.mapping);
 				}
 			},