Skip to content

Commit

Permalink
support macOS key input in 3rd_person example
Browse files Browse the repository at this point in the history
  • Loading branch information
toyboot4e committed Sep 29, 2020
1 parent b574790 commit 2f2dafe
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions examples/3rd_person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,29 +566,12 @@ impl Player {
);
}

fn handle_input(&mut self, device_event: &DeviceEvent, dt: f32) {
fn handle_device_event(&mut self, device_event: &DeviceEvent, dt: f32) {
match device_event {
DeviceEvent::Key(key) => {
if let Some(key_code) = key.virtual_keycode {
match key_code {
VirtualKeyCode::W => {
self.controller.walk_forward = key.state == ElementState::Pressed
}
VirtualKeyCode::S => {
self.controller.walk_backward = key.state == ElementState::Pressed
}
VirtualKeyCode::A => {
self.controller.walk_left = key.state == ElementState::Pressed
}
VirtualKeyCode::D => {
self.controller.walk_right = key.state == ElementState::Pressed
}
VirtualKeyCode::Space => {
self.controller.jump = key.state == ElementState::Pressed
}
_ => (),
}
}
DeviceEvent::Key(_key) => {
// self.handle_key_event(key, dt);
// Prefer to handle key input events via `WindowEvent`, considering the macOS
// keyboard input issue: https://github.com/rust-windowing/winit/issues/1470
}
DeviceEvent::MouseMotion { delta } => {
let mouse_sens = 0.2 * dt;
Expand All @@ -600,6 +583,25 @@ impl Player {
_ => {}
}
}

fn handle_key_event(&mut self, key: &rg3d::event::KeyboardInput, dt: f32) {
if let Some(key_code) = key.virtual_keycode {
match key_code {
VirtualKeyCode::W => {
self.controller.walk_forward = key.state == ElementState::Pressed
}
VirtualKeyCode::S => {
self.controller.walk_backward = key.state == ElementState::Pressed
}
VirtualKeyCode::A => self.controller.walk_left = key.state == ElementState::Pressed,
VirtualKeyCode::D => {
self.controller.walk_right = key.state == ElementState::Pressed
}
VirtualKeyCode::Space => self.controller.jump = key.state == ElementState::Pressed,
_ => (),
}
}
}
}

fn create_scene_async(
Expand Down Expand Up @@ -860,6 +862,12 @@ fn main() {
}
WindowEvent::KeyboardInput { input, .. } => {
if let Some(code) = input.virtual_keycode {
// Here we handle keyboard event (not via `DeviceEvent`), considering
// the macOS keyboard input issue: https://github.com/rust-windowing/winit/issues/1470
if let Some(game_scene) = game_scene.as_mut() {
game_scene.player.handle_key_event(&input, fixed_timestep);
}

let settings = match code {
VirtualKeyCode::Key1 => Some(QualitySettings::ultra()),
VirtualKeyCode::Key2 => Some(QualitySettings::high()),
Expand Down Expand Up @@ -888,7 +896,7 @@ fn main() {
}
Event::DeviceEvent { event, .. } => {
if let Some(game_scene) = game_scene.as_mut() {
game_scene.player.handle_input(&event, fixed_timestep);
game_scene.player.handle_device_event(&event, fixed_timestep);
}
}
_ => *control_flow = ControlFlow::Poll,
Expand Down

0 comments on commit 2f2dafe

Please sign in to comment.