v0.0.3 with clear_assignments()

master
Ales Katona 6 years ago
parent 5731589a74
commit 21ac0d52c1
Signed by: almindor
GPG Key ID: 08C459E2D8ABB7E8

@ -1,6 +1,6 @@
[package] [package]
name = "prongs" name = "prongs"
version = "0.0.2" version = "0.0.3"
authors = ["Ales Katona <almindor@gmail.com>"] authors = ["Ales Katona <almindor@gmail.com>"]
edition = "2018" edition = "2018"
readme = "README.md" readme = "README.md"
@ -14,9 +14,9 @@ keywords = ["input", "controller", "keyboard", "controls", "game"]
[dependencies] [dependencies]
bitflags = "1.0.4" 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 } gilrs = { version = "0.6.3", optional = true }
piston = { version = "0.40.0", optional = true } piston = { version = "0.41.0", optional = true }
[features] [features]
backend_piston = ["piston"] backend_piston = ["piston"]

@ -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. Current backends include Piston and Gilrs with more on the way.
**WARNING** prongs is alpha level at this point. APIs will most probably change. **WARNING** prongs is alpha level at this point. APIs will most probably change.
**NOTE** prongs requires rust 1.32 or later. **NOTE** prongs requires rust 1.32 or later.
## Using prongs library ## 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. To use the prongs library include it in your `Cargo.toml` file. You *MUST* specify a backend via features e.g.
``` ```
[dependencies] [dependencies]
prongs = { version = "0.0.2", features = ["backend_piston"] } prongs = { version = "0.0.3", features = ["backend_piston"] }
``` ```
### Documentation ### Documentation

@ -28,6 +28,11 @@ where TUserAction: Clone + Serialize,
self.schema.assign_input(event, action, iaf) 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<ProcessingResult<TUserAction>> { pub fn process_event(&mut self, event: &Event) -> Option<ProcessingResult<TUserAction>> {
self.schema.process_event(event) self.schema.process_event(event)
} }

@ -34,6 +34,11 @@ where TUserAction: Clone + Serialize,
self.schema.assign_input(event, action, iaf) 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 /// Main event processing hook. Will result in ProcessingResult for user's action if
/// a mapping is withing the schema for the given event. /// a mapping is withing the schema for the given event.
pub fn process_event(&mut self, event: &Event) -> Option<ProcessingResult<TUserAction>> { pub fn process_event(&mut self, event: &Event) -> Option<ProcessingResult<TUserAction>> {

@ -35,7 +35,7 @@ impl EventType
{ {
fn new(rt: u64, rv: u64) -> Self { fn new(rt: u64, rv: u64) -> Self {
EventType { 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>,
TControllerID: Copy + PartialEq + Serialize, TControllerID: Copy + PartialEq + Serialize,
TUserAction: Clone + Serialize, TUserAction: Clone + Serialize,
{ {
/// Create a new empty schema
fn new(name: &str) -> Self { fn new(name: &str) -> Self {
Schema { Schema {
name: name.to_string(), name: name.to_string(),
@ -93,6 +94,9 @@ where TEventType: ToEventType<TControllerID>,
} }
} }
/// 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 { fn assign_controller(&mut self, event: &TEventType, iaf: InputTypeFlags) -> bool {
let event_controller_id = event.controller_id(); let event_controller_id = event.controller_id();
@ -106,6 +110,7 @@ where TEventType: ToEventType<TControllerID>,
false false
} }
/// Assigns input event -> user action mapping
fn assign_input(&mut self, event: &TEventType, action: TUserAction, iaf: InputTypeFlags) -> bool { fn assign_input(&mut self, event: &TEventType, action: TUserAction, iaf: InputTypeFlags) -> bool {
if event.filter_for_assignment(iaf) { if event.filter_for_assignment(iaf) {
if let Some(event_type) = event.to_raw() { if let Some(event_type) = event.to_raw() {
@ -118,6 +123,15 @@ where TEventType: ToEventType<TControllerID>,
false 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<ProcessingResult<TUserAction>> { fn process_event(&mut self, event: &TEventType) -> Option<ProcessingResult<TUserAction>> {
if let Some(event_type) = event.to_raw() { if let Some(event_type) = event.to_raw() {
if let Some(action) = self.keymap.get(&event_type) { if let Some(action) = self.keymap.get(&event_type) {
@ -136,6 +150,7 @@ where TEventType: ToEventType<TControllerID>,
None None
} }
/// Set player_id as option, use None to clear
fn set_player_id(&mut self, player_id: Option<usize>) { fn set_player_id(&mut self, player_id: Option<usize>) {
self.player_id = player_id; self.player_id = player_id;
} }

@ -61,3 +61,16 @@ fn schema_set_player_id() {
schema.set_player_id(None); schema.set_player_id(None);
assert_eq!(schema.player_id(), None); assert_eq!(schema.player_id(), None);
} }
#[test]
fn schema_clear_assignments() {
let mut schema = SchemaPiston::<u64>::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));
}

Loading…
Cancel
Save