Skip to content

Commit

Permalink
Update input_state.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
rustbasic authored May 23, 2024
1 parent 19bf8c8 commit e7f8ef4
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions crates/egui/src/input_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl InputState {
for touch_state in self.touch_states.values_mut() {
touch_state.begin_frame(time, &new, self.pointer.interact_pos);
}
let pointer = self.pointer.clone().begin_frame(time, &new);
let pointer = self.pointer.begin_frame(time, &new);

let mut keys_down = self.keys_down;
let mut zoom_factor_delta = 1.0; // TODO(emilk): smoothing for zoom factor
Expand Down Expand Up @@ -299,7 +299,7 @@ impl InputState {
}
}

{
if unprocessed_scroll_delta_for_zoom != 0.0 {
// Smooth scroll-to-zoom:
if unprocessed_scroll_delta_for_zoom.abs() < 1.0 {
smooth_scroll_delta_for_zoom += unprocessed_scroll_delta_for_zoom;
Expand Down Expand Up @@ -343,31 +343,30 @@ impl InputState {
return;
}

// Mouse wheels often go very large steps.
// A single notch on a logitech mouse wheel connected to a Macbook returns 14.0 raw_scroll_delta.
// So we smooth it out over several frames for a nicer user experience when scrolling in egui.
if !is_begin_frame && is_contains_pointer {
self.unprocessed_scroll_delta += self.raw_scroll_delta;
self.raw_scroll_delta = Vec2::ZERO;
}

let dt = self.stable_dt.at_most(0.1);
let t = crate::emath::exponential_smooth_factor(0.90, 0.1, dt); // reach _% in _ seconds. TODO(emilk): parameterize

for d in 0..2 {
if self.unprocessed_scroll_delta[d].abs() < 1.0 {
self.smooth_scroll_delta[d] += self.unprocessed_scroll_delta[d];
self.unprocessed_scroll_delta[d] = 0.0;
} else {
let smooth_delta = t * self.unprocessed_scroll_delta[d];
let direct_delta = self.unprocessed_scroll_delta[d];
// Smooth: smooth_delta > 0.0, Direct: smooth_delta < 0.0
let delta = match smooth_delta < 0.0 {
true => smooth_delta.min(direct_delta),
false => smooth_delta.max(direct_delta),
};
self.smooth_scroll_delta[d] += delta;
self.unprocessed_scroll_delta[d] -= delta;
if self.unprocessed_scroll_delta != Vec2::ZERO {
let dt = self.stable_dt.at_most(0.1);
let t = crate::emath::exponential_smooth_factor(0.90, 0.1, dt);

for d in 0..2 {
if self.unprocessed_scroll_delta[d].abs() < 1.0 {
self.smooth_scroll_delta[d] += self.unprocessed_scroll_delta[d];
self.unprocessed_scroll_delta[d] = 0.0;
} else {
let smooth_delta = t * self.unprocessed_scroll_delta[d];
let direct_delta = self.unprocessed_scroll_delta[d];
// Smooth: smooth_delta > 0.0, Direct: smooth_delta < 0.0
let applied = match smooth_delta < 0.0 {
true => smooth_delta.min(direct_delta),
false => smooth_delta.max(direct_delta),
};
self.smooth_scroll_delta[d] += applied;
self.unprocessed_scroll_delta[d] -= applied;
}
}
}
}
Expand Down

0 comments on commit e7f8ef4

Please sign in to comment.