[State] Refactor event
This commit is contained in:
parent
7f2a43700a
commit
0145968267
4 changed files with 65 additions and 54 deletions
17
src/lib.rs
17
src/lib.rs
|
@ -170,21 +170,16 @@ pub async fn run() {
|
|||
let default_state = Box::from(DefaultState::new(renderer.deref()));
|
||||
Arc::get_mut(&mut renderer).unwrap().set_state(Some(default_state));
|
||||
let mut last_render_time = instant::Instant::now();
|
||||
event_loop.run(move |event, _, control_flow| {
|
||||
event_loop.run(move |base_event, _, control_flow| {
|
||||
*control_flow = ControlFlow::Poll;
|
||||
let renderer = Arc::get_mut(&mut renderer).unwrap();
|
||||
match event {
|
||||
|
||||
match base_event {
|
||||
Event::MainEventsCleared => window.request_redraw(),
|
||||
// Event::DeviceEvent {
|
||||
// event: DeviceEvent::MouseMotion{ delta, },
|
||||
// .. // We're not using device_id currently
|
||||
// } => if state.mouse_pressed {
|
||||
// state.camera_controller.process_mouse(delta.0, delta.1)
|
||||
// }
|
||||
Event::WindowEvent {
|
||||
ref event,
|
||||
window_id,
|
||||
} if window_id == window.id() && !renderer.input(event) => {
|
||||
} if window_id == window.id() && !renderer.input(&base_event) => {
|
||||
match event {
|
||||
#[cfg(not(target_arch="wasm32"))]
|
||||
WindowEvent::CloseRequested
|
||||
|
@ -203,7 +198,7 @@ pub async fn run() {
|
|||
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
||||
renderer.resize(**new_inner_size);
|
||||
}
|
||||
_ => {}
|
||||
_ => { renderer.input(&base_event); }
|
||||
}
|
||||
}
|
||||
Event::RedrawRequested(window_id) if window_id == window.id() => {
|
||||
|
@ -221,7 +216,7 @@ pub async fn run() {
|
|||
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
_ => { renderer.input(&base_event); }
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use cgmath::prelude::*;
|
||||
use rayon::prelude::*;
|
||||
use wgpu::util::DeviceExt;
|
||||
use winit::event::{ElementState, KeyboardInput, MouseButton, WindowEvent};
|
||||
use wgpu::{util::DeviceExt, Queue};
|
||||
use winit::event::{DeviceEvent, ElementState, Event, KeyboardInput, MouseButton, WindowEvent};
|
||||
|
||||
use crate::{
|
||||
camera,
|
||||
|
@ -208,47 +208,64 @@ impl super::State for DefaultState {
|
|||
}
|
||||
}
|
||||
|
||||
fn resize(&mut self, renderer: &Renderer, new_size: winit::dpi::PhysicalSize<u32>) {
|
||||
fn resize(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
config: &wgpu::SurfaceConfiguration,
|
||||
new_size: winit::dpi::PhysicalSize<u32>,
|
||||
) {
|
||||
self.projection.resize(new_size.width, new_size.height);
|
||||
self.depth_texture = texture::Texture::create_depth_texture(
|
||||
&renderer.device,
|
||||
&renderer.config,
|
||||
"depth_texture",
|
||||
);
|
||||
self.depth_texture =
|
||||
texture::Texture::create_depth_texture(&device, &config, "depth_texture");
|
||||
}
|
||||
|
||||
fn input(&mut self, renderer: &Renderer, event: &winit::event::WindowEvent) -> bool {
|
||||
fn input(&mut self, event: &Event<()>) -> bool {
|
||||
match event {
|
||||
WindowEvent::KeyboardInput {
|
||||
input:
|
||||
KeyboardInput {
|
||||
virtual_keycode: Some(key),
|
||||
state,
|
||||
..
|
||||
},
|
||||
..
|
||||
} => self.camera_controller.process_keyboard(*key, *state),
|
||||
WindowEvent::MouseWheel { delta, .. } => {
|
||||
self.camera_controller.process_scroll(delta);
|
||||
true
|
||||
}
|
||||
WindowEvent::MouseInput {
|
||||
button: MouseButton::Left,
|
||||
state,
|
||||
Event::DeviceEvent {
|
||||
event: DeviceEvent::MouseMotion{ delta, },
|
||||
..
|
||||
} => {
|
||||
self.mouse_pressed = *state == ElementState::Pressed;
|
||||
if self.mouse_pressed {
|
||||
self.camera_controller.process_mouse(delta.0, delta.1);
|
||||
}
|
||||
true
|
||||
}
|
||||
},
|
||||
Event::WindowEvent {
|
||||
ref event,
|
||||
..
|
||||
} => match event {
|
||||
WindowEvent::KeyboardInput {
|
||||
input:
|
||||
KeyboardInput {
|
||||
virtual_keycode: Some(key),
|
||||
state,
|
||||
..
|
||||
},
|
||||
..
|
||||
} => self.camera_controller.process_keyboard(*key, *state),
|
||||
WindowEvent::MouseWheel { delta, .. } => {
|
||||
self.camera_controller.process_scroll(delta);
|
||||
true
|
||||
}
|
||||
WindowEvent::MouseInput {
|
||||
button: MouseButton::Left,
|
||||
state,
|
||||
..
|
||||
} => {
|
||||
self.mouse_pressed = *state == ElementState::Pressed;
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
},
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self, renderer: &Renderer, dt: instant::Duration) {
|
||||
fn update(&mut self, queue: &Queue, dt: instant::Duration) {
|
||||
self.camera_controller.update_camera(&mut self.camera, dt);
|
||||
self.camera_uniform
|
||||
.update_view_proj(&self.camera, &self.projection);
|
||||
renderer.queue.write_buffer(
|
||||
queue.write_buffer(
|
||||
&self.camera_buffer,
|
||||
0,
|
||||
bytemuck::cast_slice(&[self.camera_uniform]),
|
||||
|
@ -260,7 +277,7 @@ impl super::State for DefaultState {
|
|||
(cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0))
|
||||
* old_position)
|
||||
.into();
|
||||
renderer.queue.write_buffer(
|
||||
queue.write_buffer(
|
||||
&self.light_buffer,
|
||||
0,
|
||||
bytemuck::cast_slice(&[self.light_uniform]),
|
||||
|
@ -269,7 +286,6 @@ impl super::State for DefaultState {
|
|||
|
||||
fn render(
|
||||
&mut self,
|
||||
renderer: &Renderer,
|
||||
view: &wgpu::TextureView,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
) -> Result<(), wgpu::SurfaceError> {
|
||||
|
|
|
@ -5,13 +5,13 @@ pub use renderer::Renderer;
|
|||
mod default_state;
|
||||
pub use default_state::DefaultState;
|
||||
|
||||
use wgpu::{TextureView, CommandEncoder};
|
||||
use winit::event::WindowEvent;
|
||||
use wgpu::{TextureView, CommandEncoder, Queue};
|
||||
use winit::event::Event;
|
||||
|
||||
pub trait State {
|
||||
fn new(renderer: &Renderer) -> Self where Self: Sized;
|
||||
fn resize(&mut self, renderer: &Renderer, new_size: winit::dpi::PhysicalSize<u32>);
|
||||
fn input(&mut self, renderer: &Renderer, event: &WindowEvent) -> bool;
|
||||
fn update(&mut self, renderer: &Renderer, dt: instant::Duration);
|
||||
fn render(&mut self, renderer: &Renderer, view: &TextureView, encoder: &mut CommandEncoder) -> Result<(), wgpu::SurfaceError>;
|
||||
fn resize(&mut self, device: &wgpu::Device, config: &wgpu::SurfaceConfiguration, new_size: winit::dpi::PhysicalSize<u32>);
|
||||
fn input(&mut self, event: &Event<()>) -> bool;
|
||||
fn update(&mut self, queue: &Queue, dt: instant::Duration);
|
||||
fn render(&mut self, view: &TextureView, encoder: &mut CommandEncoder) -> Result<(), wgpu::SurfaceError>;
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
use std::iter;
|
||||
|
||||
use winit::{event::WindowEvent, window::Window};
|
||||
use winit::{event::Event, window::Window};
|
||||
|
||||
use super::State;
|
||||
|
||||
|
@ -75,21 +75,21 @@ impl Renderer {
|
|||
self.surface.configure(&self.device, &self.config);
|
||||
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
// state.resize(new_size);
|
||||
state.resize(&self.device, &self.config, new_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn input(&mut self, event: &WindowEvent) -> bool {
|
||||
pub fn input(&mut self, event: &Event<()>) -> bool {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
// return state.input(event);
|
||||
return state.input(event);
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn update(&mut self, dt: instant::Duration) {
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
// state.update(dt);
|
||||
state.update(&self.queue, dt);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -106,7 +106,7 @@ impl Renderer {
|
|||
});
|
||||
|
||||
if let Some(state) = self.state.as_mut() {
|
||||
// state.render(&view, &mut encoder)?;
|
||||
state.render(&view, &mut encoder)?;
|
||||
}
|
||||
|
||||
self.queue.submit(iter::once(encoder.finish()));
|
||||
|
|
Loading…
Reference in a new issue