From 2c766aa540c5232c4617d1956eba6568fea65f33 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 16 Dec 2020 21:22:45 +0100 Subject: [PATCH] Make RawInput::time an Option --- CHANGELOG.md | 1 + egui/src/input.rs | 62 ++++++++++++++++++++++++++++++--------- egui_glium/src/backend.rs | 2 +- egui_web/src/backend.rs | 2 +- 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13dfbde2..5bc18295 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * `SidePanel::left` and `TopPanel::top` now takes `impl Hash` as first argument. * `ui.image` now takes `impl Into` as a `size` argument. +* Made some more fields of `RawInput` optional. ## 0.5.0 - 2020-12-13 diff --git a/egui/src/input.rs b/egui/src/input.rs index 5e1ab0bd..573593d6 100644 --- a/egui/src/input.rs +++ b/egui/src/input.rs @@ -9,8 +9,10 @@ const MAX_CLICK_DELAY: f64 = 0.3; /// What the backend provides to Egui at the start of each frame. /// +/// Set the values that make sense, leave the rest at their `Default::default()`. +/// /// All coordinates are in points (logical pixels) with origin (0, 0) in the top left corner. -#[derive(Clone, Debug, Default)] +#[derive(Clone, Debug)] pub struct RawInput { /// Is the button currently down? /// NOTE: Egui currently only supports the primary mouse button. @@ -30,8 +32,14 @@ pub struct RawInput { /// If text looks blurry on high resolution screens, you probably forgot to set this. pub pixels_per_point: Option, - /// Time in seconds. Relative to whatever. Used for animations. - pub time: f64, + /// Monotonically increasing time, in seconds. Relative to whatever. Used for animations. + /// If `None` is provided, Egui will assume a time delta of `predicted_dt` (default 1/60 seconds). + pub time: Option, + + /// Should be set to the expected time between frames when painting at vsync speeds. + /// The default for this is 1/60. + /// Can safely be left at its default value. + pub predicted_dt: f32, /// Which modifier keys are down at the start of the frame? pub modifiers: Modifiers, @@ -40,6 +48,22 @@ pub struct RawInput { pub events: Vec, } +impl Default for RawInput { + fn default() -> Self { + Self { + mouse_down: false, + mouse_pos: None, + scroll_delta: Vec2::zero(), + screen_size: Vec2::new(10_000.0, 10_000.0), // Difficult with a good default + pixels_per_point: None, + time: None, + predicted_dt: 1.0 / 60.0, + modifiers: Modifiers::default(), + events: vec![], + } + } +} + impl RawInput { /// Helper: move volatile (deltas and events), clone the rest pub fn take(&mut self) -> RawInput { @@ -50,6 +74,7 @@ impl RawInput { screen_size: self.screen_size, pixels_per_point: self.pixels_per_point, time: self.time, + predicted_dt: self.predicted_dt, modifiers: self.modifiers, events: std::mem::take(&mut self.events), } @@ -219,16 +244,19 @@ pub enum Key { impl InputState { #[must_use] pub fn begin_frame(self, new: RawInput) -> InputState { - let mouse = self.mouse.begin_frame(&new); - let unstable_dt = (new.time - self.raw.time) as f32; + let time = new + .time + .unwrap_or_else(|| self.time + new.predicted_dt as f64); + let mouse = self.mouse.begin_frame(time, &new); + let unstable_dt = (time - self.time) as f32; InputState { mouse, scroll_delta: new.scroll_delta, screen_size: new.screen_size, pixels_per_point: new.pixels_per_point.or(self.pixels_per_point), - time: new.time, + time, unstable_dt, - predicted_dt: 1.0 / 60.0, // TODO: remove this hack + predicted_dt: new.predicted_dt, modifiers: new.modifiers, events: new.events.clone(), // TODO: remove clone() and use raw.events raw: new, @@ -295,7 +323,7 @@ impl InputState { impl MouseInput { #[must_use] - pub fn begin_frame(mut self, new: &RawInput) -> MouseInput { + pub fn begin_frame(mut self, time: f64, new: &RawInput) -> MouseInput { let delta = new .mouse_pos .and_then(|new| self.pos.map(|last| new - last)) @@ -304,12 +332,12 @@ impl MouseInput { let released = self.down && !new.mouse_down; let click = released && self.could_be_click; - let double_click = click && (new.time - self.last_click_time) < MAX_CLICK_DELAY; + let double_click = click && (time - self.last_click_time) < MAX_CLICK_DELAY; let mut press_origin = self.press_origin; let mut could_be_click = self.could_be_click; let mut last_click_time = self.last_click_time; if click { - last_click_time = new.time + last_click_time = time } if pressed { @@ -332,14 +360,14 @@ impl MouseInput { } if let Some(mouse_pos) = new.mouse_pos { - self.pos_history.add(new.time, mouse_pos); + self.pos_history.add(time, mouse_pos); } else { // we do not clear the `mouse_tracker` here, because it is exactly when a finger has // released from the touch screen that we may want to assign a velocity to whatever // the user tried to throw } - self.pos_history.flush(new.time); + self.pos_history.flush(time); let velocity = if self.pos_history.len() >= 3 && self.pos_history.duration() > 0.01 { self.pos_history.velocity().unwrap_or_default() } else { @@ -372,6 +400,7 @@ impl RawInput { screen_size, pixels_per_point, time, + predicted_dt, modifiers, events, } = self; @@ -386,7 +415,12 @@ impl RawInput { .on_hover_text( "Also called HDPI factor.\nNumber of physical pixels per each logical pixel.", ); - ui.label(format!("time: {:.3} s", time)); + if let Some(time) = time { + ui.label(format!("time: {:.3} s", time)); + } else { + ui.label("time: None"); + } + ui.label(format!("predicted_dt: {:.1} ms", 1e3 * predicted_dt)); ui.label(format!("modifiers: {:#?}", modifiers)); ui.label(format!("events: {:?}", events)) .on_hover_text("key presses etc"); @@ -428,7 +462,7 @@ impl InputState { "time since previous frame: {:.1} ms", 1e3 * unstable_dt )); - ui.label(format!("expected dt: {:.1} ms", 1e3 * predicted_dt)); + ui.label(format!("predicted_dt: {:.1} ms", 1e3 * predicted_dt)); ui.label(format!("modifiers: {:#?}", modifiers)); ui.label(format!("events: {:?}", events)) .on_hover_text("key presses etc"); diff --git a/egui_glium/src/backend.rs b/egui_glium/src/backend.rs index e0c80030..516c828c 100644 --- a/egui_glium/src/backend.rs +++ b/egui_glium/src/backend.rs @@ -86,7 +86,7 @@ pub fn run(title: &str, mut storage: Box, mut app: Box