From 21ac0d52c1a9b01e997590e8f889152ec5f47390 Mon Sep 17 00:00:00 2001 From: Ales Katona Date: Wed, 6 Feb 2019 10:09:09 -0700 Subject: [PATCH] v0.0.3 with clear_assignments() --- Cargo.toml | 6 +++--- README.md | 3 ++- src/backend_gilrs.rs | 5 +++++ src/backend_piston.rs | 5 +++++ src/lib.rs | 17 ++++++++++++++++- src/tests/backends/backend_piston.rs | 13 +++++++++++++ 6 files changed, 44 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4e777b4..5b7f8fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "prongs" -version = "0.0.2" +version = "0.0.3" authors = ["Ales Katona "] edition = "2018" readme = "README.md" @@ -14,9 +14,9 @@ keywords = ["input", "controller", "keyboard", "controls", "game"] [dependencies] bitflags = "1.0.4" -serde = { version = "1.0.85", features = ["derive"] } +serde = { version = "1.0.87", features = ["derive"] } gilrs = { version = "0.6.3", optional = true } -piston = { version = "0.40.0", optional = true } +piston = { version = "0.41.0", optional = true } [features] backend_piston = ["piston"] diff --git a/README.md b/README.md index d4ed40c..ea9f7b9 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ Input handling schema written in rust. Backend agnostic, provides serializabilit Current backends include Piston and Gilrs with more on the way. **WARNING** prongs is alpha level at this point. APIs will most probably change. + **NOTE** prongs requires rust 1.32 or later. ## Using prongs library @@ -12,7 +13,7 @@ Current backends include Piston and Gilrs with more on the way. To use the prongs library include it in your `Cargo.toml` file. You *MUST* specify a backend via features e.g. ``` [dependencies] -prongs = { version = "0.0.2", features = ["backend_piston"] } +prongs = { version = "0.0.3", features = ["backend_piston"] } ``` ### Documentation diff --git a/src/backend_gilrs.rs b/src/backend_gilrs.rs index 6590565..f2d32a5 100644 --- a/src/backend_gilrs.rs +++ b/src/backend_gilrs.rs @@ -28,6 +28,11 @@ where TUserAction: Clone + Serialize, self.schema.assign_input(event, action, iaf) } + /// Clears all assigned input mappings and controller_id. Player stays assigned. + pub fn clear_assignments(&mut self) { + self.schema.clear_assignments(); + } + pub fn process_event(&mut self, event: &Event) -> Option> { self.schema.process_event(event) } diff --git a/src/backend_piston.rs b/src/backend_piston.rs index b3b2732..e52a816 100644 --- a/src/backend_piston.rs +++ b/src/backend_piston.rs @@ -34,6 +34,11 @@ where TUserAction: Clone + Serialize, self.schema.assign_input(event, action, iaf) } + /// Clears all assigned input mappings and controller_id. Player stays assigned. + pub fn clear_assignments(&mut self) { + self.schema.clear_assignments(); + } + /// Main event processing hook. Will result in ProcessingResult for user's action if /// a mapping is withing the schema for the given event. pub fn process_event(&mut self, event: &Event) -> Option> { diff --git a/src/lib.rs b/src/lib.rs index b835d51..2f560ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,7 +35,7 @@ impl EventType { fn new(rt: u64, rv: u64) -> Self { EventType { - 0: (rt as u128).rotate_left(64) + u128::from(rv) + 0: u128::from(rt).rotate_left(64) + u128::from(rv) } } } @@ -83,6 +83,7 @@ where TEventType: ToEventType, TControllerID: Copy + PartialEq + Serialize, TUserAction: Clone + Serialize, { + /// Create a new empty schema fn new(name: &str) -> Self { Schema { name: name.to_string(), @@ -93,6 +94,9 @@ where TEventType: ToEventType, } } + /// Assign controller to this schema. Future input events will be ignored unless + /// their controller matches the assigned one. NOTE: many backends don't have controller IDs + /// for keyboards and mice. fn assign_controller(&mut self, event: &TEventType, iaf: InputTypeFlags) -> bool { let event_controller_id = event.controller_id(); @@ -106,6 +110,7 @@ where TEventType: ToEventType, false } + /// Assigns input event -> user action mapping fn assign_input(&mut self, event: &TEventType, action: TUserAction, iaf: InputTypeFlags) -> bool { if event.filter_for_assignment(iaf) { if let Some(event_type) = event.to_raw() { @@ -118,6 +123,15 @@ where TEventType: ToEventType, false } + /// Removes input event -> user action mappings and assigned controller_id + /// Player_id stays assigned, if you need to clear that use set_player_id() + fn clear_assignments(&mut self) { + self.controller_id = None; + self.keymap.clear(); + } + + /// Main processing loop. Takes input event and returns an option with processing result + /// If the mapping was found result will be set, otherwise None fn process_event(&mut self, event: &TEventType) -> Option> { if let Some(event_type) = event.to_raw() { if let Some(action) = self.keymap.get(&event_type) { @@ -136,6 +150,7 @@ where TEventType: ToEventType, None } + /// Set player_id as option, use None to clear fn set_player_id(&mut self, player_id: Option) { self.player_id = player_id; } diff --git a/src/tests/backends/backend_piston.rs b/src/tests/backends/backend_piston.rs index 4779b53..0ac4123 100644 --- a/src/tests/backends/backend_piston.rs +++ b/src/tests/backends/backend_piston.rs @@ -61,3 +61,16 @@ fn schema_set_player_id() { schema.set_player_id(None); assert_eq!(schema.player_id(), None); } + +#[test] +fn schema_clear_assignments() { + let mut schema = SchemaPiston::::new("Testing"); + + assert_eq!(schema.player_id(), None); + schema.clear_assignments(); + assert_eq!(schema.player_id(), None); + + schema.set_player_id(Some(5)); + schema.clear_assignments(); + assert_eq!(schema.player_id(), Some(5)); +}