refactor project structure (start of redoing atomic branch without atomics)
This commit is contained in:
78
src/default/input.rs
Normal file
78
src/default/input.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use crate::prelude::*;
|
||||
use winit::{
|
||||
event::{MouseButton, MouseScrollDelta, WindowEvent},
|
||||
keyboard::{Key, NamedKey},
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Input {
|
||||
cursor: CursorState,
|
||||
pub modifiers: Modifiers,
|
||||
}
|
||||
|
||||
impl Input {
|
||||
pub fn event(&mut self, event: &WindowEvent) -> bool {
|
||||
match event {
|
||||
WindowEvent::CursorMoved { position, .. } => {
|
||||
self.cursor.pos = Vec2::new(position.x as f32, position.y as f32);
|
||||
self.cursor.exists = true;
|
||||
}
|
||||
WindowEvent::MouseInput { state, button, .. } => {
|
||||
let buttons = &mut self.cursor.buttons;
|
||||
let pressed = state.is_pressed();
|
||||
match button {
|
||||
MouseButton::Left => buttons.left.update(pressed),
|
||||
MouseButton::Right => buttons.right.update(pressed),
|
||||
MouseButton::Middle => buttons.middle.update(pressed),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
WindowEvent::MouseWheel { delta, .. } => {
|
||||
let mut delta = match *delta {
|
||||
MouseScrollDelta::LineDelta(x, y) => Vec2::new(x, y),
|
||||
MouseScrollDelta::PixelDelta(pos) => Vec2::new(pos.x as f32, pos.y as f32),
|
||||
};
|
||||
if delta.x == 0.0 && self.modifiers.shift {
|
||||
delta.x = delta.y;
|
||||
delta.y = 0.0;
|
||||
}
|
||||
self.cursor.scroll_delta = delta;
|
||||
}
|
||||
WindowEvent::CursorLeft { .. } => {
|
||||
self.cursor.exists = false;
|
||||
self.modifiers.clear();
|
||||
}
|
||||
WindowEvent::KeyboardInput { event, .. } => {
|
||||
if let Key::Named(named) = event.logical_key {
|
||||
let pressed = event.state.is_pressed();
|
||||
match named {
|
||||
NamedKey::Control => {
|
||||
self.modifiers.control = pressed;
|
||||
}
|
||||
NamedKey::Shift => {
|
||||
self.modifiers.shift = pressed;
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => return false,
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
pub fn end_frame(&mut self) {
|
||||
self.cursor.end_frame();
|
||||
}
|
||||
}
|
||||
|
||||
impl UiState {
|
||||
pub fn window_size(&self) -> Vec2 {
|
||||
let size = self.renderer.window().inner_size();
|
||||
(size.width, size.height).into()
|
||||
}
|
||||
|
||||
pub fn cursor_state(&self) -> &CursorState {
|
||||
&self.input.cursor
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user