From 5518bda7a99d4c7e054d7a3e7f8b123ae97dad54 Mon Sep 17 00:00:00 2001 From: Ygor Souza Date: Sat, 18 Nov 2023 20:45:01 +0100 Subject: [PATCH 1/3] Properly reverse bool animation if value changes before it's finished --- crates/egui/src/animation_manager.rs | 47 +++++++++++++--------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/crates/egui/src/animation_manager.rs b/crates/egui/src/animation_manager.rs index b7b7d18beff..541d55f5481 100644 --- a/crates/egui/src/animation_manager.rs +++ b/crates/egui/src/animation_manager.rs @@ -1,3 +1,5 @@ +use epaint::emath::NumExt; + use crate::{emath::remap_clamp, Id, IdMap, InputState}; #[derive(Clone, Default)] @@ -8,10 +10,8 @@ pub(crate) struct AnimationManager { #[derive(Clone, Debug)] struct BoolAnim { - value: bool, - - /// when did `value` last toggle? - toggle_time: f64, + last_value: f32, + last_tick: f64, } #[derive(Clone, Debug)] @@ -33,38 +33,33 @@ impl AnimationManager { id: Id, value: bool, ) -> f32 { + let (start, end) = if value { (0.0, 1.0) } else { (1.0, 0.0) }; match self.bools.get_mut(&id) { None => { self.bools.insert( id, BoolAnim { - value, - toggle_time: -f64::INFINITY, // long time ago + last_value: end, + last_tick: input.time - input.stable_dt as f64, }, ); - if value { - 1.0 - } else { - 0.0 - } + end } Some(anim) => { - if anim.value != value { - anim.value = value; - anim.toggle_time = input.time; - } - - let time_since_toggle = (input.time - anim.toggle_time) as f32; - - // On the frame we toggle we don't want to return the old value, - // so we extrapolate forwards: - let time_since_toggle = time_since_toggle + input.predicted_dt; - - if value { - remap_clamp(time_since_toggle, 0.0..=animation_time, 0.0..=1.0) + let BoolAnim { + last_value, + last_tick, + } = anim; + let current_time = input.time; + let elapsed = ((current_time - *last_tick) as f32).at_most(input.stable_dt); + let new_value = *last_value + (end - start) * elapsed / animation_time; + *last_value = if new_value.is_finite() { + new_value.clamp(0.0, 1.0) } else { - remap_clamp(time_since_toggle, 0.0..=animation_time, 1.0..=0.0) - } + end + }; + *last_tick = current_time; + *last_value } } } From afd891883b5c8fcc6555bc9554f54d893fe5ccd7 Mon Sep 17 00:00:00 2001 From: Ygor Souza Date: Sun, 19 Nov 2023 14:15:35 +0100 Subject: [PATCH 2/3] Merge imports --- crates/egui/src/animation_manager.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/egui/src/animation_manager.rs b/crates/egui/src/animation_manager.rs index 541d55f5481..578503fbc07 100644 --- a/crates/egui/src/animation_manager.rs +++ b/crates/egui/src/animation_manager.rs @@ -1,6 +1,7 @@ -use epaint::emath::NumExt; - -use crate::{emath::remap_clamp, Id, IdMap, InputState}; +use crate::{ + emath::{remap_clamp, NumExt}, + Id, IdMap, InputState, +}; #[derive(Clone, Default)] pub(crate) struct AnimationManager { From 7500edb70e76ca092aad5b19ec9f861836e544dc Mon Sep 17 00:00:00 2001 From: Ygor Souza Date: Sun, 19 Nov 2023 21:52:43 +0100 Subject: [PATCH 3/3] Fix trait import style --- crates/egui/src/animation_manager.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/egui/src/animation_manager.rs b/crates/egui/src/animation_manager.rs index 578503fbc07..fee5ad5ea8c 100644 --- a/crates/egui/src/animation_manager.rs +++ b/crates/egui/src/animation_manager.rs @@ -1,5 +1,5 @@ use crate::{ - emath::{remap_clamp, NumExt}, + emath::{remap_clamp, NumExt as _}, Id, IdMap, InputState, };