Skip to content

Commit 1d5df5d

Browse files
committed
Update docs + improve game logic in wasm
1 parent bd61959 commit 1d5df5d

File tree

5 files changed

+46
-24
lines changed

5 files changed

+46
-24
lines changed

.github/workflows/github-pages.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# From https://book.leptos.dev/deployment/csr.html#github-pages
1+
# See https://book.leptos.dev/deployment/csr.html#github-pages
22
name: Release to Github Pages
33

44
on:
55
push:
6-
branches: [main, add-wasm-backend]
6+
branches: [main]
77
paths:
88
- 'examples/wasm/**'
99
workflow_dispatch:

examples/wasm/src/main.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ pub fn main() {
9999

100100
pub fn handle_key_event(event: &KeyboardEvent, is_keydown: bool) {
101101
let config = derived_config();
102+
let is_game_over = isGameOver();
102103

103104
// Log the key that was pressed
104105
if is_keydown {
@@ -111,29 +112,21 @@ pub fn handle_key_event(event: &KeyboardEvent, is_keydown: bool) {
111112
setKey(key.to_string(), desc);
112113
}
113114

114-
if isGameOver() {
115-
return;
116-
}
117-
118115
if let Some(action) = config.get(event) {
119116
match action {
120-
Action::Jump => {
121-
if is_keydown {
122-
jump()
123-
}
124-
}
125-
Action::Left => moveLeft(is_keydown),
126-
Action::Right => moveRight(is_keydown),
127-
Action::Pause => {
128-
if is_keydown {
129-
pauseGame()
130-
}
131-
}
132117
Action::Quit => {
133118
if is_keydown {
134119
resetGame();
135120
}
136121
}
122+
_ if !is_game_over => match action {
123+
Action::Left => moveLeft(is_keydown),
124+
Action::Right => moveRight(is_keydown),
125+
Action::Jump if is_keydown => jump(),
126+
Action::Pause if is_keydown => pauseGame(),
127+
_ => {}
128+
},
129+
_ => {}
137130
}
138131
}
139132
}

src/backend/crossterm.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Key event parsing and cnnversion for the `crossterm` backend.
1+
//! Key event parsing and conversion for the `crossterm` backend.
22
//!
33
//! This module bridges `crossterm::event::KeyEvent` with a backend-agnostic
44
//! representation (`KeyMap`) used for keybinding configuration and matching.
@@ -13,6 +13,17 @@
1313
//! # Limitations
1414
//! - Some `KeyCode` variants are not supported and will return an error.
1515
//! - Key groups (e.g., `@any`) are not reversible to `KeyEvent` due to the loss of specificity.
16+
//!
17+
//! # Examples
18+
//!
19+
//! Parsing from a string:
20+
//! ```
21+
//! use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
22+
//! use keymap::backend::crossterm::parse;
23+
//!
24+
//! let key = parse("ctrl-a").unwrap();
25+
//! assert_eq!(key, KeyEvent::new(KeyCode::Char('a'), KeyModifiers::CONTROL));
26+
//! ```
1627
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
1728
use keymap_parser::{self as parser, Key, Modifier, Node};
1829

src/backend/termion.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
//! Key event parsing and cnnversion for the `termion` backend.
1+
//! Key event parsing and conversion for the `termion` backend.
22
//!
3-
//! This module bridges `termion` key events with the generic `keymap` system,
4-
//! allowing `KeyEvent` to be parsed from string representations, converted into
5-
//! `KeyMap` nodes, and looked up in a `Config<T>` instance.
3+
//! This module bridges `termion::event::Key` with a backend-agnostic
4+
//! representation (`KeyMap`) used for keybinding configuration and matching.
5+
//! It enables parsing human-readable key definitions and converting between
6+
//! representations suitable for UI and configuration layers.
7+
//!
8+
//! # Key Features
9+
//! - `parse`: Parses a string key representation (e.g., "ctrl-a") into a `KeyEvent`.
10+
//! - Implements `IntoKeyMap`, `ToKeyMap`, and `FromKeyMap` for `KeyEvent`.
11+
//! - Converts between `KeyEvent` (from termion) and internal `KeyMap` format.
612
//!
713
//! # Limitations
814
//! - Termion only supports `alt` and `ctrl` modifiers on character keys.
@@ -15,8 +21,8 @@
1521
//!
1622
//! Parsing from a string:
1723
//! ```
24+
//! use keymap::backend::termion::parse;
1825
//! use termion::event::Key as KeyEvent;
19-
//! use keymap::backend::parse;
2026
//!
2127
//! let key: KeyEvent = parse("ctrl-a").unwrap();
2228
//! assert_eq!(key, KeyEvent::Ctrl('a'));

src/backend/wasm.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313
//! # Limitations
1414
//! - Some `KeyboardEvent` variants are not supported and will return an error.
1515
//! - Key groups (e.g., `@any`) are not reversible to `KeyboardEvent` due to the loss of specificity.
16+
//!
17+
//! # Examples
18+
//!
19+
//! Parsing from a string:
20+
//! ```
21+
//! use keymap::backend::wasm::parse;
22+
//! use web_sys::KeyboardEvent;
23+
//!
24+
//! let event = parse("alt-b").unwrap();
25+
//! assert_eq!(event.key(), "b");
26+
//! assert_eq!(event.alt_key(), true);
27+
//! ```
1628
use keymap_parser::{self as parser, Key, Modifier, Node};
1729
use web_sys::{KeyboardEvent, KeyboardEventInit};
1830

0 commit comments

Comments
 (0)