Skip to content

Commit

Permalink
Various updates
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Sep 19, 2023
1 parent bccb707 commit 7c1284c
Show file tree
Hide file tree
Showing 34 changed files with 1,338 additions and 659 deletions.
13 changes: 12 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ jobs:
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
- run: sudo apt-get install libasound2-dev
targets: wasm32-unknown-unknown
- run: sudo apt-get update && sudo apt-get install libasound2-dev libudev-dev
if: matrix.os == 'ubuntu'
- run: make

Expand All @@ -32,6 +33,16 @@ jobs:
- name: Install wasm-opt
run: brew update && brew install binaryen
- run: make wasm
- run: cd examples/gamepads-wasm-direct && make
- run: cargo install wasm-bindgen-cli
- run: cd examples/gamepads-wasm-bindgen && make

check-js:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm install uglify-js -g
- run: make check-js

actionlint:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ target/

.idea/
.vscode/
generated/

site/generated/
dist/
Expand Down
22 changes: 20 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
[package]
name = "gamepads"
categories = ["game-development"]
description = "API to access information about system gamepads."
description = "Library to access information about connected gamepads."
edition = "2021"
keywords = ["gamedev", "joystick", "input", "gamepad", "macroquad"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/fornwall/gamepads"
version = "0.1.0"
version = "0.1.6"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = [ "wasm-bindgen" ]
wasm-bindgen = ["dep:wasm-bindgen", "web-sys", "js-sys"]

[dependencies]
js-sys = { version = "0.3", optional = true }
wasm-bindgen = { version = "0.2", optional = true }
web-sys = { version = "0.3", features = [
"Gamepad",
"GamepadButton",
"GamepadMappingType",
"Window",
"Navigator",
], optional = true }

[target.'cfg(not(target_family = "wasm"))'.dependencies]
gilrs = "0.10"

[workspace]
members = [".", "examples/*"]
20 changes: 18 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,49 @@ CLIPPY_PARAMS = -- \
-W clippy::match_same_arms \
-W clippy::needless_continue \
-W clippy::needless_pass_by_value \
-W clippy::nursery \
-W clippy::option_if_let_else \
-W clippy::print_stderr \
-W clippy::print_stdout \
-W clippy::redundant_closure_for_method_calls \
-W clippy::semicolon_if_nothing_returned \
-W clippy::similar_names \
-W clippy::single_match_else \
-W clippy::trivially_copy_pass_by_ref \
-W clippy::unnested_or_patterns \
-W clippy::unreadable-literal \
-W clippy::unseparated-literal-suffix \
-A clippy::wildcard_dependencies \
-A clippy::needless-doctest-main \
-D warnings

check:
cargo fmt --check
cargo clippy $(CLIPPY_PARAMS) --no-deps
cargo clippy --target wasm32-unknown-unknown $(CLIPPY_PARAMS) --no-deps
cargo clippy --target wasm32-unknown-unknown --all-features $(CLIPPY_PARAMS) --no-deps
cd examples/hello-gamepads && make
cd examples/gamepads-macroquad && make
cargo test

run:
cargo run $(MODE)

wasm:
cargo build --target wasm32-unknown-unknown $(MODE)
$(WASM_OPT) -o $(PACKAGE_NAME).wasm ./target/wasm32-unknown-unknown/$(WASM_DIR)/$(PACKAGE_NAME).wasm
cargo build --target wasm32-unknown-unknown --features wasm-bindgen $(MODE)
$(WASM_OPT) -o $(PACKAGE_NAME).wasm ./target/wasm32-unknown-unknown/$(WASM_DIR)/$(PACKAGE_NAME).wasm

serve-wasm: wasm
python3 -m http.server 9000

generate-js:
cd js && ./generate-js.sh

check-js: generate-js
cd js && git diff --exit-code .

clean:
cargo clean

.PHONY: check run wasm serve-wasm clean
.PHONY: check run wasm serve-wasm clean generate-js check-js
56 changes: 43 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,60 @@
[![Crates.io version](https://img.shields.io/crates/v/gamepads.svg)](https://crates.io/crates/gamepads)

# gamepads
A crate to expose gamepad information in browsers using the [Gamepad API](https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API/Using_the_Gamepad_API) exposed by browsers.
Rust gamepad input library with a focus on ease of use.

```rust
use gamepads::Gamepads;

Add the dependency as:
fn main() {
let mut gamepads = Gamepads::new();

```toml
[dependencies]
gamepads = "0.1.0"
loop {
gamepads.poll();

for gamepad in gamepads.all() {
println!("Gamepad id: {:?}", gamepad.id());
for button in gamepad.all_currently_pressed() {
println!("Pressed button: {:?}", button);
}
println!("Left thumbstick: {:?}", gamepad.left_stick());
println!("Right thumbstick: {:?}", gamepad.right_stick());
}

std::thread::sleep(std::time::Duration::from_millis(500));
}
}
```

On web, this crate uses a small javascript function, making it possible to use as a [macroquad](https://github.com/not-fl3/macroquad) plugin - more about that below.
See the [crate documentation](https://docs.rs/gamepads/latest/gamepads/) and the [examples](https://github.com/fornwall/gamepads/tree/main/examples/) for documentation and sample code.

## What it is

- On desktop this library is implemented on top of [gilrs](https://crates.io/crates/gilrs).
- On web this is implemented on top of the [Gamepad API](https://www.w3.org/TR/gamepad/) exposed by browsers, including support for haptic feedback (aka "dual rumble" or "force feedback").
- It can be used in a `wasm-bindgen`-using project without any setup necessary.
- It can be used without `wasm-bindgen` (by specifying `default-features = false`), allowing it to be used as a `macroquad` plugin (see more below) or in a direct wasm build ([example](https://github.com/fornwall/gamepads/tree/main/examples/gamepads-wasm-direct)).

## Macroquad plugin
A [macroquad](https://github.com/not-fl3/macroquad) plugin to access gamepad information in browsers.
## How to use as a macroquad plugin
For non-web targets, nothing special needs to be done to use this library with [macroquad](https://github.com/not-fl3/macroquad). But for a web build to work properly, two things needs to be done.

First add the javascript, either bundling [macroquad-gamepads.js](https://fornwall.github.io/gamepads/macroquad-gamepads.js) or embedding it after:
First, since `macroquad` does not use `wasm-bindgen`, that feature in `gamepads` needs to be turned off by setting `default-features = false`:

```toml
gamepads = { version = "*", default-features = false }
```

Second, a javascript plug-in ([source](https://github.com/fornwall/gamepads/blob/main/js/gamepads-src-0.1.js)) needs to be registered in the page embedding the built wasm file:

```html
<script src="https://not-fl3.github.io/miniquad-samples/mq_js_bundle.js"></script>
<script src="https://fornwall.github.io/gamepads/macroquad-gamepads.js"></script>
<script src="https://fornwall.github.io/gamepads/js/macroquad-gamepads-0.1.js"></script>
<script>
load("your-wasm-file.wasm");
load("your-wasm-file.wasm");
</script>
```

# Report issues
Please [report any issues found](https://github.com/fornwall/gamepads/issues)!
See the [gamepads-macroquad](https://github.com/fornwall/gamepads/tree/main/examples/gamepads-macroquad) example.

# Feedback
Please [report any issues found](https://github.com/fornwall/gamepads/issues) or [discuss questions and ideas](https://github.com/fornwall/gamepads/discussions)!
9 changes: 9 additions & 0 deletions examples/gamepads-macroquad/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "gamepads-macroquad"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
macroquad = "*"
gamepads = { path = "../..", default-features = false }
53 changes: 53 additions & 0 deletions examples/gamepads-macroquad/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
PACKAGE_NAME = gamepads-macroquad
MODE =
WASM_DIR = debug
WASM_OPT = wasm-opt --all-features --disable-gc
ifeq ($(RELEASE),1)
MODE = --release
WASM_DIR = release
WASM_OPT += -O3
else
WASM_OPT += -O1
endif

CLIPPY_PARAMS = -- \
-W clippy::cargo \
-W clippy::cast_lossless \
-W clippy::dbg_macro \
-W clippy::expect_used \
-W clippy::if_not_else \
-W clippy::items_after_statements \
-W clippy::large_stack_arrays \
-W clippy::linkedlist \
-W clippy::manual_filter_map \
-W clippy::match_same_arms \
-W clippy::needless_continue \
-W clippy::needless_pass_by_value \
-W clippy::option_if_let_else \
-W clippy::redundant_closure_for_method_calls \
-W clippy::semicolon_if_nothing_returned \
-W clippy::similar_names \
-W clippy::single_match_else \
-W clippy::trivially_copy_pass_by_ref \
-W clippy::unnested_or_patterns \
-W clippy::unseparated-literal-suffix \
-A clippy::multiple_crate_versions \
-A clippy::wildcard_dependencies \
-D warnings

check:
cargo fmt --check
cargo clippy $(CLIPPY_PARAMS) --no-deps
cargo clippy --target wasm32-unknown-unknown $(CLIPPY_PARAMS) --no-deps

wasm:
cargo build --target wasm32-unknown-unknown $(MODE)
$(WASM_OPT) -o $(PACKAGE_NAME).wasm ../../target/wasm32-unknown-unknown/$(WASM_DIR)/$(PACKAGE_NAME).wasm

serve-wasm: wasm
python3 -m http.server 9000

clean:
cargo clean

.PHONY: check run wasm serve-wasm clean
9 changes: 9 additions & 0 deletions examples/gamepads-macroquad/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# gamepads-macroquad
An example [macroquad](https://github.com/not-fl3/macroquad) project using the [gamepads](https://github.com/fornwall/gamepads) plug-in to use gamepads as inputs.

Run with `cargo run` for a desktop build, or run `make serve-web` to build and serve a web version at [http://localhost:9000](http://localhost:9000).

Files:

- [src/main.rs](src/main.rs): The rust code
- [index.html](index.html): The web page embedding the wasm build.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<head lang="en">
<meta charset="utf-8" />
<title>hello-gamepads</title>
<title>gamepads-macroquad</title>
<style>
html,
body,
Expand All @@ -27,9 +27,9 @@
<body>
<canvas id="glcanvas" tabindex="1"></canvas>
<script src="https://not-fl3.github.io/miniquad-samples/mq_js_bundle.js"></script>
<script src="macroquad-gamepads.js"></script>
<script src="https://fornwall.github.io/gamepads/js/macroquad-gamepads-0.1.js"></script>
<script>
load("hello_gamepads.wasm");
load("gamepads-macroquad.wasm");
</script>
</body>

Expand Down
1 change: 1 addition & 0 deletions examples/gamepads-macroquad/macroquad-gamepads-0.1.js
Loading

0 comments on commit 7c1284c

Please sign in to comment.