Clean up shared UI runtime state
This commit is contained in:
1 parent
9b4c690916
commit
5ca244528f
27 files changed
+355
-499
No files matched your search
@@ -0,0 +1,94 @@
|
||||
// `CursorState::time` is the sample's own time on every backend. winit
|
||||
// carries no timestamp on a pointer event, so the moment it is handed to
|
||||
// us is the closest measurement available here -- which is also what the
|
||||
// drag code used to do for itself with `Instant::now()`, before Android's
|
||||
// batched samples made the difference matter (see `sense::CursorState`).
|
||||
use crate::prelude::*;
|
||||
use std::time::Instant;
|
||||
use winit::{
|
||||
event::{MouseButton, MouseScrollDelta, WindowEvent},
|
||||
keyboard::{Key, NamedKey},
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Input {
|
||||
cursor: CursorState,
|
||||
pub modifiers: Modifiers,
|
||||
}
|
||||
|
||||
impl Input {
|
||||
/// winit's pointer coordinates are physical pixels, which is the
|
||||
/// space the whole tree is laid out and hit-tested in -- see
|
||||
/// `desktop::content_scale`. Nothing is converted here; `dp(...)`
|
||||
/// resolves against the density at layout time instead.
|
||||
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;
|
||||
self.cursor.time = Instant::now();
|
||||
}
|
||||
WindowEvent::MouseInput { state, button, .. } => {
|
||||
self.cursor.time = Instant::now();
|
||||
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;
|
||||
self.cursor.time = Instant::now();
|
||||
}
|
||||
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 DesktopUiState {
|
||||
/// Physical pixels, matching `WindowEvent::Resized` (what
|
||||
/// `UiRenderState::resize` is given) and the swapchain -- see
|
||||
/// `desktop::content_scale`.
|
||||
pub fn window_size(&self) -> Vec2 {
|
||||
let size = self.renderer.window().inner_size();
|
||||
Vec2::new(size.width as f32, size.height as f32)
|
||||
}
|
||||
|
||||
pub fn cursor_state(&self) -> &CursorState {
|
||||
&self.input.cursor
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user