Skip to content

Commit

Permalink
stop smoothing when close
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetophon committed Nov 16, 2024
1 parent bd3fead commit 9495184
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,14 +526,21 @@ impl DelayGraph {
/// Smoothly updates the value stored within an AtomicF32 based on a target value.
/// If the current value is f32::EPSILON, it initializes with the target value.
fn gui_smooth(target_value: f32, atomic_value: &AtomicF32) -> f32 {
// Define the threshold relative to the target value
let threshold = 0.001 * target_value.abs();
// Load current value once
let mut current_value = atomic_value.load(Ordering::SeqCst);
let current_value = atomic_value.load(Ordering::SeqCst);

// Check and initialize if the value is still set to the default (consider if EPSILON is appropriate)
// Early exit if the current value is very close to the target value
if (target_value - current_value).abs() < threshold {
atomic_value.store(target_value, Ordering::SeqCst);
return target_value;
}

// Check if initial condition is met and initialize with the target value if so
if current_value == f32::EPSILON {
current_value = target_value;
atomic_value.store(current_value, Ordering::SeqCst);
return current_value;
atomic_value.store(target_value, Ordering::SeqCst);
return target_value;
}

// Compute the smoothed value
Expand Down

0 comments on commit 9495184

Please sign in to comment.