[DEFAULT_MESH] Refactor events processing
This commit is contained in:
parent
ca26586047
commit
2982f7cd39
2 changed files with 30 additions and 23 deletions
|
@ -1,8 +1,9 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use cgmath::prelude::*;
|
use cgmath::prelude::*;
|
||||||
|
use winit::event::{WindowEvent, KeyboardInput, VirtualKeyCode, ElementState};
|
||||||
|
|
||||||
use crate::render::{Mesh, Renderable, Vertex, Instance};
|
use crate::{render::{Mesh, Renderable, Vertex, Instance}, input::Controllable};
|
||||||
|
|
||||||
const VERTICES: &[Vertex] = &[
|
const VERTICES: &[Vertex] = &[
|
||||||
Vertex {
|
Vertex {
|
||||||
|
@ -121,4 +122,30 @@ impl Renderable for DefaultMesh {
|
||||||
fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
|
fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
|
||||||
self.mesh.render(render_pass);
|
self.mesh.render(render_pass);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Controllable for DefaultMesh {
|
||||||
|
fn process_events(&mut self, event: &winit::event::WindowEvent) -> bool {
|
||||||
|
match event {
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
input:
|
||||||
|
KeyboardInput {
|
||||||
|
state,
|
||||||
|
virtual_keycode: Some(keycode),
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
let is_pressed = *state == ElementState::Pressed;
|
||||||
|
match keycode {
|
||||||
|
VirtualKeyCode::Space => {
|
||||||
|
self.toggle(is_pressed);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
24
src/state.rs
24
src/state.rs
|
@ -4,7 +4,7 @@ use super::render::{
|
||||||
Vertex, Camera, Texture, InstanceRaw
|
Vertex, Camera, Texture, InstanceRaw
|
||||||
};
|
};
|
||||||
use winit::{
|
use winit::{
|
||||||
event::{KeyboardInput, VirtualKeyCode, WindowEvent, ElementState},
|
event::WindowEvent,
|
||||||
window::Window,
|
window::Window,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -186,27 +186,7 @@ impl State {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn input(&mut self, event: &WindowEvent) -> bool {
|
pub fn input(&mut self, event: &WindowEvent) -> bool {
|
||||||
match event {
|
self.mesh.process_events(&event) || self.camera.process_events(&event)
|
||||||
WindowEvent::KeyboardInput {
|
|
||||||
input:
|
|
||||||
KeyboardInput {
|
|
||||||
state,
|
|
||||||
virtual_keycode: Some(keycode),
|
|
||||||
..
|
|
||||||
},
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
let is_pressed = *state == ElementState::Pressed;
|
|
||||||
match keycode {
|
|
||||||
VirtualKeyCode::Space => {
|
|
||||||
self.mesh.toggle(is_pressed);
|
|
||||||
true
|
|
||||||
}
|
|
||||||
_ => self.camera.process_events(event),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => self.camera.process_events(event),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self) {
|
pub fn update(&mut self) {
|
||||||
|
|
Loading…
Reference in a new issue