From 792cbd030df2593d9d90a6bde7fd98e4c9c5ec06 Mon Sep 17 00:00:00 2001 From: me Date: Fri, 19 Dec 2025 21:17:02 +0200 Subject: [PATCH] gamepad --- Cargo.lock | 326 ++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/runtime/runtime.rs | 41 +++++- src/runtime/types.rs | 79 +++++++++- 4 files changed, 441 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1419b73..38df657 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,6 +23,23 @@ version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" +[[package]] +name = "android_log-sys" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84521a3cf562bc62942e294181d9eef17eb38ceb8c68677bc49f144e4c3d4f8d" + +[[package]] +name = "android_logger" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbb4e440d04be07da1f1bf44fb4495ebd58669372fe0cffa6e48595ac5bd88a3" +dependencies = [ + "android_log-sys", + "env_filter", + "log", +] + [[package]] name = "anstream" version = "0.6.21" @@ -94,6 +111,7 @@ version = "0.1.0" dependencies = [ "chumsky", "env_logger", + "gamepads", "insta", "log", "lyn", @@ -107,6 +125,18 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" + +[[package]] +name = "bumpalo" +version = "3.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dd9dc738b7a8311c7ade152424974d8115f2cdad61e8dab8dac9f2362298510" + [[package]] name = "bytemuck" version = "1.24.0" @@ -135,6 +165,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + [[package]] name = "chumsky" version = "0.11.2" @@ -173,6 +209,22 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "core-foundation" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + [[package]] name = "crc32fast" version = "1.5.0" @@ -242,6 +294,12 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "foldhash" version = "0.1.5" @@ -258,6 +316,54 @@ dependencies = [ "ttf-parser", ] +[[package]] +name = "gamepads" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8c912fc0a5052c6e8fefe36bd3965be5aec4edcedff198461c697c916f50556" +dependencies = [ + "android_logger", + "gilrs", + "js-sys", + "log", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "gilrs" +version = "0.10.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a556964c6d62458084356ce9770676f5104bd667e12e9a795691076e8a17c5cf" +dependencies = [ + "fnv", + "gilrs-core", + "log", + "uuid", + "vec_map", +] + +[[package]] +name = "gilrs-core" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "732dadc05170599ddec9a89653f10d7a2af54da9181b3fa6e2bd49907ec8f7e4" +dependencies = [ + "core-foundation", + "inotify", + "io-kit-sys", + "js-sys", + "libc", + "libudev-sys", + "log", + "nix", + "uuid", + "vec_map", + "wasm-bindgen", + "web-sys", + "windows", +] + [[package]] name = "glam" version = "0.27.0" @@ -288,6 +394,26 @@ dependencies = [ "png", ] +[[package]] +name = "inotify" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdd168d97690d0b8c412d6b6c10360277f4d7ee495c5d0d5d5fe0854923255cc" +dependencies = [ + "bitflags 1.3.2", + "inotify-sys", + "libc", +] + +[[package]] +name = "inotify-sys" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb" +dependencies = [ + "libc", +] + [[package]] name = "insta" version = "1.44.3" @@ -299,6 +425,16 @@ dependencies = [ "similar", ] +[[package]] +name = "io-kit-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "617ee6cf8e3f66f3b4ea67a4058564628cde41901316e19f559e14c7c72c5e7b" +dependencies = [ + "core-foundation-sys", + "mach2", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.2" @@ -329,12 +465,32 @@ dependencies = [ "syn", ] +[[package]] +name = "js-sys" +version = "0.3.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "464a3709c7f55f1f721e5389aa6ea4e3bc6aba669353300af094b29ffbdde1d8" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + [[package]] name = "libc" version = "0.2.178" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091" +[[package]] +name = "libudev-sys" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c8469b4a23b962c1396b9b451dda50ef5b283e8dd309d69033475fa9b334324" +dependencies = [ + "libc", + "pkg-config", +] + [[package]] name = "log" version = "0.4.29" @@ -347,6 +503,15 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f08d9299a146aa1eb3c5451e6d9045708f232a43fd8d4436f69cf00fcb2a019c" +[[package]] +name = "mach2" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d640282b302c0bb0a2a8e0233ead9035e3bed871f0b7e81fe4a1ec829765db44" +dependencies = [ + "libc", +] + [[package]] name = "macroquad" version = "0.4.14" @@ -410,6 +575,18 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1bcdd74c20ad5d95aacd60ef9ba40fdf77f767051040541df557b7a9b2a2121" +[[package]] +name = "nix" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46" +dependencies = [ + "bitflags 2.10.0", + "cfg-if", + "cfg_aliases", + "libc", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -449,13 +626,19 @@ version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" +[[package]] +name = "pkg-config" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" + [[package]] name = "png" version = "0.17.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "82151a2fc869e011c153adc57cf2789ccb8d9906ce52c0b39a6b5697749d7526" dependencies = [ - "bitflags", + "bitflags 1.3.2", "crc32fast", "fdeflate", "flate2", @@ -557,6 +740,12 @@ version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + [[package]] name = "serde" version = "1.0.228" @@ -673,6 +862,77 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "uuid" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2e054861b4bd027cd373e18e8d8d8e6548085000e41290d95ce0c373a654b4a" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + +[[package]] +name = "wasm-bindgen" +version = "0.2.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d759f433fa64a2d763d1340820e46e111a7a5ab75f993d1852d70b03dbb80fd" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48cb0d2638f8baedbc542ed444afc0644a29166f1595371af4fecf8ce1e7eeb3" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cefb59d5cd5f92d9dcf80e4683949f15ca4b511f4ac0a6e14d4e1ac60c6ecd40" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbc538057e648b67f72a982e708d485b2efa771e1ac05fec311f9f63e5800db4" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "web-sys" +version = "0.3.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b32828d774c412041098d182a8b38b16ea816958e07cf40eec2bc080ae137ac" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "winapi" version = "0.3.9" @@ -695,12 +955,76 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6" +dependencies = [ + "windows-core", + "windows-targets", +] + +[[package]] +name = "windows-core" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-result", + "windows-strings", + "windows-targets", +] + +[[package]] +name = "windows-implement" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "windows-link" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-result" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-strings" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" +dependencies = [ + "windows-result", + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.59.0" diff --git a/Cargo.toml b/Cargo.toml index 62d5b42..82ee9e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ log = "0.4.27" macroquad = "0.4.14" lyn = "0.1.0" chumsky = "0.11.2" +gamepads = "0.1.7" [dev-dependencies] insta = "1.44.3" diff --git a/src/runtime/runtime.rs b/src/runtime/runtime.rs index 0b61657..c2b5955 100644 --- a/src/runtime/runtime.rs +++ b/src/runtime/runtime.rs @@ -1,6 +1,7 @@ use super::types::*; use crate::ast; use crate::interpret; +use gamepads; use macroquad::prelude::*; pub async fn setup(code: ast::Program) -> State { @@ -25,6 +26,7 @@ pub async fn setup(code: ast::Program) -> State { assets: Assets { font }, state, game_state, + gamepads: gamepads::Gamepads::new(), } } @@ -62,10 +64,43 @@ pub async fn migrate(mut state: State, code: ast::Program) -> State { } } -pub fn update(_state: &mut State, events: Events) {} +pub fn update(_state: &mut State, input: Input) {} -pub fn fetch_events() -> Events { - Events(vec![]) +pub fn fetch_events(state: &mut State) -> Input { + state.gamepads.poll(); + let mut input = Input::new(); + + for gamepad in state.gamepads.all() { + // action buttons + input.gamepad1.buttons.a = gamepad.is_currently_pressed(gamepads::Button::ActionRight); + input.gamepad1.buttons.b = gamepad.is_currently_pressed(gamepads::Button::ActionDown); + input.gamepad1.buttons.x = gamepad.is_currently_pressed(gamepads::Button::ActionUp); + input.gamepad1.buttons.y = gamepad.is_currently_pressed(gamepads::Button::ActionLeft); + + // dpad + input.gamepad1.dpad.left = gamepad.is_currently_pressed(gamepads::Button::DPadLeft); + input.gamepad1.dpad.right = gamepad.is_currently_pressed(gamepads::Button::DPadRight); + input.gamepad1.dpad.up = gamepad.is_currently_pressed(gamepads::Button::DPadUp); + input.gamepad1.dpad.down = gamepad.is_currently_pressed(gamepads::Button::DPadDown); + + // shoulders + input.gamepad1.shoulders.l1 = + gamepad.is_currently_pressed(gamepads::Button::FrontLeftUpper); + input.gamepad1.shoulders.r1 = + gamepad.is_currently_pressed(gamepads::Button::FrontRightUpper); + + input.gamepad1.shoulders.l2 = gamepad.left_trigger(); + input.gamepad1.shoulders.r2 = gamepad.right_trigger(); + + // sticks + input.gamepad1.left_stick.x = gamepad.left_stick_x(); + input.gamepad1.left_stick.y = gamepad.left_stick_y(); + + input.gamepad1.right_stick.x = gamepad.right_stick_x(); + input.gamepad1.right_stick.y = gamepad.right_stick_y(); + break; + } + input } pub fn draw(state: &State) { diff --git a/src/runtime/types.rs b/src/runtime/types.rs index 5706286..c434c36 100644 --- a/src/runtime/types.rs +++ b/src/runtime/types.rs @@ -6,14 +6,89 @@ pub struct State { pub assets: Assets, pub state: interpret::types::State, pub game_state: ast::Value, + pub gamepads: gamepads::Gamepads, } pub struct Assets { pub font: Font, } -pub struct Event {} -pub struct Events(pub Vec); +pub struct Input { + pub gamepad1: Gamepad, +} + +impl Input { + pub fn new() -> Input { + Input { + gamepad1: Gamepad { + buttons: Buttons { + a: false, + b: false, + x: false, + y: false, + }, + dpad: DPad { + left: false, + right: false, + up: false, + down: false, + }, + left_stick: Stick { x: 0., y: 0. }, + right_stick: Stick { x: 0., y: 0. }, + shoulders: Shoulders { + l1: false, + r1: false, + l2: 0., + r2: 0., + }, + menu: Menu { + start: false, + menu: false, + }, + }, + } + } +} + +pub struct Gamepad { + pub buttons: Buttons, + pub dpad: DPad, + pub left_stick: Stick, + pub right_stick: Stick, + pub shoulders: Shoulders, + pub menu: Menu, +} + +pub struct Buttons { + pub a: bool, + pub b: bool, + pub x: bool, + pub y: bool, +} + +pub struct DPad { + pub left: bool, + pub right: bool, + pub up: bool, + pub down: bool, +} + +pub struct Stick { + pub x: f32, + pub y: f32, +} + +pub struct Shoulders { + pub l1: bool, + pub r1: bool, + pub l2: f32, + pub r2: f32, +} + +pub struct Menu { + pub start: bool, + pub menu: bool, +} pub const SCALE: i32 = 4;