From 7ce7ea0bbe723c440d0bd01354eff3d2e13edd35 Mon Sep 17 00:00:00 2001 From: inyourface3445 Date: Sun, 3 Nov 2024 11:18:29 -0500 Subject: [PATCH 1/4] fixed issue #18 --- plastic_tui/src/ui.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/plastic_tui/src/ui.rs b/plastic_tui/src/ui.rs index d733170..7c5cd9c 100644 --- a/plastic_tui/src/ui.rs +++ b/plastic_tui/src/ui.rs @@ -91,7 +91,7 @@ pub struct Ui { menu: MenuState, audio_player: Option>, - gilrs: Gilrs, + gilrs: Option, active_gamepad: Option, /// For terminals without support for `Release` key event, we keep the button pressed for some @@ -134,7 +134,7 @@ impl Ui { } else { None }, - gilrs: Gilrs::new().unwrap(), + gilrs: Gilrs::new().ok(), keyboard_event_counter: HashMap::new(), active_gamepad: None, } @@ -451,14 +451,19 @@ impl Ui { fn handle_gamepad(&mut self) { // set events in the cache and check if gamepad is still active - while let Some(GilrsEvent { id, event, .. }) = self.gilrs.next_event() { + if self.gilrs.is_none() { + return; + } + + + while let Some(GilrsEvent { id, event, .. }) = self.gilrs.as_mut().unwrap().next_event() { self.active_gamepad = Some(id); if event == EventType::Disconnected { self.active_gamepad = None; } } - if let Some(gamepad) = self.active_gamepad.map(|id| self.gilrs.gamepad(id)) { + if let Some(gamepad) = self.active_gamepad.map(|id| self.gilrs.as_mut().unwrap().gamepad(id)) { for (controller_button, nes_button) in &[ (Button::South, NESKey::B), (Button::East, NESKey::A), From 26dad41d2614f472b157d0a247169893913f2e8c Mon Sep 17 00:00:00 2001 From: inyourface3445 Date: Tue, 5 Nov 2024 09:43:26 -0500 Subject: [PATCH 2/4] complience with cargo check --- plastic_tui/src/ui.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plastic_tui/src/ui.rs b/plastic_tui/src/ui.rs index 7c5cd9c..27b3416 100644 --- a/plastic_tui/src/ui.rs +++ b/plastic_tui/src/ui.rs @@ -455,7 +455,6 @@ impl Ui { return; } - while let Some(GilrsEvent { id, event, .. }) = self.gilrs.as_mut().unwrap().next_event() { self.active_gamepad = Some(id); if event == EventType::Disconnected { @@ -463,7 +462,10 @@ impl Ui { } } - if let Some(gamepad) = self.active_gamepad.map(|id| self.gilrs.as_mut().unwrap().gamepad(id)) { + if let Some(gamepad) = self + .active_gamepad + .map(|id| self.gilrs.as_mut().unwrap().gamepad(id)) + { for (controller_button, nes_button) in &[ (Button::South, NESKey::B), (Button::East, NESKey::A), From d0ccee0609bdf7ace773213cbfa2757115abc49f Mon Sep 17 00:00:00 2001 From: inyourface3445 Date: Tue, 5 Nov 2024 09:52:53 -0500 Subject: [PATCH 3/4] aparently, invisable charaters matter --- plastic_tui/src/ui.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plastic_tui/src/ui.rs b/plastic_tui/src/ui.rs index 27b3416..0b5f3c1 100644 --- a/plastic_tui/src/ui.rs +++ b/plastic_tui/src/ui.rs @@ -454,7 +454,7 @@ impl Ui { if self.gilrs.is_none() { return; } - + while let Some(GilrsEvent { id, event, .. }) = self.gilrs.as_mut().unwrap().next_event() { self.active_gamepad = Some(id); if event == EventType::Disconnected { From eb5633dbf0b03f98a630152528ea310f05dbf173 Mon Sep 17 00:00:00 2001 From: inyourface3445 Date: Tue, 5 Nov 2024 10:01:27 -0500 Subject: [PATCH 4/4] changed from using .is_none to unwrapping it ... and assigning it to a varible --- plastic_tui/src/ui.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/plastic_tui/src/ui.rs b/plastic_tui/src/ui.rs index 0b5f3c1..385aa29 100644 --- a/plastic_tui/src/ui.rs +++ b/plastic_tui/src/ui.rs @@ -451,21 +451,18 @@ impl Ui { fn handle_gamepad(&mut self) { // set events in the cache and check if gamepad is still active - if self.gilrs.is_none() { + let Some(gilrs_obj) = self.gilrs.as_mut() else { return; - } + }; - while let Some(GilrsEvent { id, event, .. }) = self.gilrs.as_mut().unwrap().next_event() { + while let Some(GilrsEvent { id, event, .. }) = gilrs_obj.next_event() { self.active_gamepad = Some(id); if event == EventType::Disconnected { self.active_gamepad = None; } } - if let Some(gamepad) = self - .active_gamepad - .map(|id| self.gilrs.as_mut().unwrap().gamepad(id)) - { + if let Some(gamepad) = self.active_gamepad.map(|id| gilrs_obj.gamepad(id)) { for (controller_button, nes_button) in &[ (Button::South, NESKey::B), (Button::East, NESKey::A),