1
0
Fork 0

[DEFAULT_MESH] Refactor events processing

This commit is contained in:
Florian RICHER 2022-06-16 14:19:21 +02:00
parent ca26586047
commit 2982f7cd39
2 changed files with 30 additions and 23 deletions

View file

@ -1,8 +1,9 @@
use std::sync::Arc;
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] = &[
Vertex {
@ -121,4 +122,30 @@ impl Renderable for DefaultMesh {
fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
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,
}
}
}

View file

@ -4,7 +4,7 @@ use super::render::{
Vertex, Camera, Texture, InstanceRaw
};
use winit::{
event::{KeyboardInput, VirtualKeyCode, WindowEvent, ElementState},
event::WindowEvent,
window::Window,
};
@ -186,27 +186,7 @@ impl State {
}
pub fn input(&mut self, 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.mesh.toggle(is_pressed);
true
}
_ => self.camera.process_events(event),
}
}
_ => self.camera.process_events(event),
}
self.mesh.process_events(&event) || self.camera.process_events(&event)
}
pub fn update(&mut self) {