Sfoglia il codice sorgente

Refactor keyboard input handling in CameraController

Ghastrod 1 anno fa
parent
commit
276ec20d90
1 ha cambiato i file con 44 aggiunte e 22 eliminazioni
  1. 44 22
      src/controller.rs

+ 44 - 22
src/controller.rs

@@ -27,32 +27,54 @@ impl CameraController{
 
     pub fn process_events(&mut self, event: &WindowEvent) -> bool {
         match event {
-            WindowEvent::KeyboardInput{
-                device_id: _,
-                is_synthetic: _,
-                event: winit::event::KeyEvent{
-                    physical_key: key,
-                    state: state,
-                    ..
+            WindowEvent::KeyboardInput { event, .. } => {
+                let is_pressed = event.state == winit::event::ElementState::Pressed;
+                if is_pressed {
+                    match event.physical_key {
+                        winit::keyboard::PhysicalKey::Code(key_code) => {
+                            match key_code {
+                                KeyCode::KeyW => {
+                                    //println!("W pressed!");
+                                    self.is_forward_pressed = is_pressed;
+                                    // Add logic for handling W press
+                                    return true;
+                                }
+                                KeyCode::KeyA => {
+                                    //println!("A pressed!");
+                                    self.is_left_pressed = is_pressed;
+                                    // Add logic for handling A press
+                                    return true;
+                                }
+                                KeyCode::KeyS => {
+                                    //println!("S pressed!");
+                                    self.is_backward_pressed = is_pressed;
+                                    // Add logic for handling S press
+                                    return true;
+                                }
+                                KeyCode::KeyD => {
+                                    //println!("D pressed!");
+                                    self.is_right_pressed = is_pressed;
+                                    // Add logic for handling D press
+                                    return true;
+                                }
+                                _ => false, // Ignore other key codes
+                            };
+                        }
+                        winit::keyboard::PhysicalKey::Unidentified(native_code) => {
+                            // Handle unidentified keys (optional)
+                            // You can potentially store and compare raw codes for custom keybinds
+                            println!("Unidentified key pressed: {:?}", native_code);
+                        }
+                    }
                 }
-            } => {
-                if *key == winit::keyboard::KeyCode::KeyW{
-                    self.is_forward_pressed = *state == winit::event::ElementState::Pressed;
-                }
-                if *key == winit::keyboard::KeyCode::KeyS{
-                    self.is_backward_pressed = *state == winit::event::ElementState::Pressed;
-                }
-                if *key == winit::keyboard::KeyCode::KeyA{
-                    self.is_left_pressed = *state == winit::event::ElementState::Pressed;
-                }
-                if *key == winit::keyboard::KeyCode::KeyD{
-                    self.is_right_pressed = (*state == winit::event::ElementState::Pressed);
-                }
-
+                return false // Indicate event not handled
             }
-            _ => ()
+            _ => (), // Ignore other events
         }
+    
+        false // Default return value
     }
+    
 
     pub fn update_camera(&self, camera: &mut Camera) {