Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed issue #18 #43

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions plastic_tui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub struct Ui {

menu: MenuState<MenuEvent>,
audio_player: Option<AudioPlayer<f32>>,
gilrs: Gilrs,
gilrs: Option<Gilrs>,
active_gamepad: Option<gilrs::GamepadId>,

/// For terminals without support for `Release` key event, we keep the button pressed for some
Expand Down Expand Up @@ -134,7 +134,7 @@ impl Ui {
} else {
None
},
gilrs: Gilrs::new().unwrap(),
gilrs: Gilrs::new().ok(),
keyboard_event_counter: HashMap::new(),
active_gamepad: None,
}
Expand Down Expand Up @@ -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() {
Copy link
Owner

@Amjad50 Amjad50 Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small improvement here,
you can use

// didn't test the code, but the idea here
let Some(mut gilrs) = self.gilrs else {
    return;
};

then afterwards, you can use girls without the need for as_mut().unwrap()

Copy link
Contributor Author

@inyourface34456 inyourface34456 Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion, will implement

EDIT: Unforuntly, the girls object does not implement copy, so it cant be moved out of the Some variant. Good idea nonetheless.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use ref

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),
Expand Down
Loading