Skip to content

Commit

Permalink
winit 0.30
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-ben committed Jul 30, 2024
1 parent 4bc4fee commit ff2e371
Show file tree
Hide file tree
Showing 6 changed files with 457 additions and 338 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ image = "0.25"
ash = "0.38"
ash-window = "0.13"
raw-window-handle = "0.6"
winit = "0.29"
winit = "0.30"
gltf = "1.3"
egui = "0.28"
egui-winit = "0.28"
Expand Down
120 changes: 60 additions & 60 deletions crates/viewer/src/controls.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use vulkan::winit::{
event::{
DeviceEvent, ElementState, Event, KeyEvent, MouseButton, MouseScrollDelta, WindowEvent,
},
event::{DeviceEvent, ElementState, KeyEvent, MouseButton, MouseScrollDelta, WindowEvent},
keyboard::{KeyCode, PhysicalKey},
};

Expand All @@ -20,7 +18,15 @@ pub struct InputState {
}

impl InputState {
pub fn update(self, event: &Event<()>) -> Self {
pub fn reset(self) -> Self {
Self {
cursor_delta: [0.0, 0.0],
wheel_delta: 0.0,
..self
}
}

pub fn handle_window_event(self, event: &WindowEvent) -> Self {
let mut is_forward_pressed = None;
let mut is_backward_pressed = None;
let mut is_left_pressed = None;
Expand All @@ -30,63 +36,43 @@ impl InputState {
let mut is_left_clicked = None;
let mut is_right_clicked = None;
let mut wheel_delta = self.wheel_delta;
let mut cursor_delta = self.cursor_delta;

if let Event::NewEvents(_) = event {
return Self {
cursor_delta: [0.0, 0.0],
wheel_delta: 0.0,
..self
};
}

if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::MouseInput { button, state, .. } => {
let clicked = matches!(state, ElementState::Pressed);
match button {
MouseButton::Left => is_left_clicked = Some(clicked),
MouseButton::Right => is_right_clicked = Some(clicked),
_ => {}
};
}
WindowEvent::MouseWheel {
delta: MouseScrollDelta::LineDelta(_, v_lines),
..
} => {
wheel_delta += v_lines;
}
WindowEvent::KeyboardInput {
event:
KeyEvent {
physical_key: PhysicalKey::Code(scancode),
state,
..
},
..
} => {
let pressed = matches!(state, ElementState::Pressed);
match scancode {
KeyCode::KeyW => is_forward_pressed = Some(pressed),
KeyCode::KeyS => is_backward_pressed = Some(pressed),
KeyCode::KeyA => is_left_pressed = Some(pressed),
KeyCode::KeyD => is_right_pressed = Some(pressed),
KeyCode::Space => is_up_pressed = Some(pressed),
KeyCode::ControlLeft => is_down_pressed = Some(pressed),
_ => {}
};
}
_ => {}
match event {
WindowEvent::MouseInput { button, state, .. } => {
let clicked = matches!(state, ElementState::Pressed);
match button {
MouseButton::Left => is_left_clicked = Some(clicked),
MouseButton::Right => is_right_clicked = Some(clicked),
_ => {}
};
}
}

if let Event::DeviceEvent {
event: DeviceEvent::MouseMotion { delta: (x, y) },
..
} = event
{
cursor_delta[0] += *x as f32;
cursor_delta[1] += *y as f32;
WindowEvent::MouseWheel {
delta: MouseScrollDelta::LineDelta(_, v_lines),
..
} => {
wheel_delta += v_lines;
}
WindowEvent::KeyboardInput {
event:
KeyEvent {
physical_key: PhysicalKey::Code(scancode),
state,
..
},
..
} => {
let pressed = matches!(state, ElementState::Pressed);
match scancode {
KeyCode::KeyW => is_forward_pressed = Some(pressed),
KeyCode::KeyS => is_backward_pressed = Some(pressed),
KeyCode::KeyA => is_left_pressed = Some(pressed),
KeyCode::KeyD => is_right_pressed = Some(pressed),
KeyCode::Space => is_up_pressed = Some(pressed),
KeyCode::ControlLeft => is_down_pressed = Some(pressed),
_ => {}
};
}
_ => {}
}

Self {
Expand All @@ -98,8 +84,22 @@ impl InputState {
is_down_pressed: is_down_pressed.unwrap_or(self.is_down_pressed),
is_left_clicked: is_left_clicked.unwrap_or(self.is_left_clicked),
is_right_clicked: is_right_clicked.unwrap_or(self.is_right_clicked),
cursor_delta,
wheel_delta,
..self
}
}

pub fn handle_device_event(self, event: &DeviceEvent) -> Self {
let mut cursor_delta = self.cursor_delta;

if let DeviceEvent::MouseMotion { delta: (x, y) } = event {
cursor_delta[0] += *x as f32;
cursor_delta[1] += *y as f32;
}

Self {
cursor_delta,
..self
}
}
}
Expand Down
96 changes: 49 additions & 47 deletions crates/viewer/src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,53 +50,55 @@ impl Gui {
}
}

pub fn handle_event(&mut self, window: &WinitWindow, event: &WindowEvent) {
let _ = self.egui_winit.on_window_event(window, event);
}

pub fn render(&mut self, window: &WinitWindow) -> RenderData {
let raw_input = self.egui_winit.take_egui_input(window);

let previous_state = self.state;

let egui::FullOutput {
platform_output,
textures_delta,
shapes,
pixels_per_point,
..
} = self.egui.run(raw_input, |ctx: &Context| {
egui::Window::new("Menu ('H' to toggle)")
.default_open(false)
.show(ctx, |ui| {
build_renderer_settings_window(ui, &mut self.state);
ui.separator();
build_camera_details_window(ui, &mut self.state, self.camera);
ui.separator();
build_animation_player_window(
ui,
&mut self.state,
self.model_metadata.as_ref(),
self.animation_playback_state,
);
});
});

self.state.check_renderer_settings_changed(&previous_state);

self.state.hovered = self.egui.is_pointer_over_area();

self.egui_winit
.handle_platform_output(window, platform_output);

let clipped_primitives = self.egui.tessellate(shapes, pixels_per_point);

RenderData {
pixels_per_point,
textures_delta,
clipped_primitives,
}
}
// FIXME: egui-winit w/ winit 0.30 support
// pub fn handle_event(&mut self, window: &WinitWindow, event: &WindowEvent) {
// let _ = self.egui_winit.on_window_event(window, event);
// }

// FIXME: egui-winit w/ winit 0.30 support
// pub fn render(&mut self, window: &WinitWindow) -> RenderData {
// let raw_input = self.egui_winit.take_egui_input(window);

// let previous_state = self.state;

// let egui::FullOutput {
// platform_output,
// textures_delta,
// shapes,
// pixels_per_point,
// ..
// } = self.egui.run(raw_input, |ctx: &Context| {
// egui::Window::new("Menu ('H' to toggle)")
// .default_open(false)
// .show(ctx, |ui| {
// build_renderer_settings_window(ui, &mut self.state);
// ui.separator();
// build_camera_details_window(ui, &mut self.state, self.camera);
// ui.separator();
// build_animation_player_window(
// ui,
// &mut self.state,
// self.model_metadata.as_ref(),
// self.animation_playback_state,
// );
// });
// });

// self.state.check_renderer_settings_changed(&previous_state);

// self.state.hovered = self.egui.is_pointer_over_area();

// self.egui_winit
// .handle_platform_output(window, platform_output);

// let clipped_primitives = self.egui.tessellate(shapes, pixels_per_point);

// RenderData {
// pixels_per_point,
// textures_delta,
// clipped_primitives,
// }
// }

pub fn set_model_metadata(&mut self, metadata: Metadata) {
self.model_metadata.replace(metadata);
Expand Down
Loading

0 comments on commit ff2e371

Please sign in to comment.