| 41 | } |
| 42 | |
| 43 | pub fn event(&mut self, event: &Event) { |
| 44 | match event { |
| 45 | Event::KeyDown(key) => self.keys.set_down(*key), |
| 46 | Event::KeyUp(key) => self.keys.set_up(*key), |
| 47 | Event::MouseButtonDown(button) => self.mouse_buttons.set_down(*button), |
| 48 | Event::MouseButtonUp(button) => self.mouse_buttons.set_up(*button), |
| 49 | Event::MouseMotion { new_position } => self.mouse_position = *new_position, |
| 50 | |
| 51 | Event::ControllerDeviceAdded { joystick, gamepad } => { |
| 52 | let empty_slot = self.gamepads.iter().position(Option::is_none); |
| 53 | |
| 54 | let gamepad_id = match empty_slot { |
| 55 | Some(slot) => { |
| 56 | self.gamepads[slot] = Some(gamepad.clone()); |
| 57 | slot |
| 58 | } |
| 59 | None => { |
| 60 | self.gamepads.push(Some(gamepad.clone())); |
| 61 | self.gamepads.len() - 1 |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | self.joystick_ids.insert(*joystick, gamepad_id); |
| 66 | } |
| 67 | |
| 68 | Event::ControllerDeviceRemoved { joystick } => { |
| 69 | if let Some(gamepad_id) = self.joystick_ids.remove(joystick) { |
| 70 | self.gamepads[gamepad_id] = None; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | Event::ControllerButtonDown { joystick, button } => { |
| 75 | if let Some(gamepad_id) = self.joystick_ids.get(joystick) { |
| 76 | self.gamepad_buttons.set_down((*gamepad_id, *button)); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | Event::ControllerButtonUp { joystick, button } => { |
| 81 | if let Some(gamepad_id) = self.joystick_ids.get(joystick) { |
| 82 | self.gamepad_buttons.set_up((*gamepad_id, *button)); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | Event::ControllerAxisMotion { |
| 87 | joystick, |
| 88 | axis, |
| 89 | value, |
| 90 | } => { |
| 91 | if let Some(gamepad_id) = self.joystick_ids.get(joystick) { |
| 92 | self.axes.set_value(*gamepad_id, *axis, *value); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | _ => {} |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | pub fn clear(&mut self) { |