make the zoom in/out fully feature actually smoothly zoom

This commit is contained in:
Dustin Carlino 2020-01-13 14:01:56 -08:00
parent 32afd5663b
commit 0a771404ea
2 changed files with 22 additions and 19 deletions

View File

@ -2,10 +2,6 @@ use crate::{EventCtx, EventLoopMode};
use geom::{Line, Pt2D};
use std::time::Instant;
const ANIMATION_TIME_S: f64 = 0.5;
// TODO Should factor in zoom too
const MIN_ANIMATION_SPEED: f64 = 200.0;
pub struct Warper {
started: Instant,
line: Option<Line>,
@ -23,29 +19,38 @@ impl Warper {
}
pub fn event(&self, ctx: &mut EventCtx) -> Option<EventLoopMode> {
let line = self.line.as_ref()?;
// Weird to do stuff for any event?
if ctx.input.nonblocking_is_update_event() {
ctx.input.use_update_event();
if !ctx.input.nonblocking_is_update_event() {
return Some(EventLoopMode::Animation);
}
ctx.input.use_update_event();
let speed = line.length().inner_meters() / ANIMATION_TIME_S;
let total_time = if speed >= MIN_ANIMATION_SPEED {
ANIMATION_TIME_S
const MAX_ANIMATION_TIME_S: f64 = 0.5;
const ANIMATION_SPEED: f64 = 200.0;
let total_time = if let Some(ref line) = self.line {
(line.length().inner_meters() / ANIMATION_SPEED).min(MAX_ANIMATION_TIME_S)
} else {
line.length().inner_meters() / MIN_ANIMATION_SPEED
MAX_ANIMATION_TIME_S
};
let percent = abstutil::elapsed_seconds(self.started) / total_time;
let orig_center = ctx.canvas.center_to_map_pt();
if percent >= 1.0 || ctx.input.nonblocking_is_keypress_event() {
ctx.canvas.cam_zoom = self.cam_zoom.1;
ctx.canvas.center_on_map_pt(line.pt2());
if let Some(ref line) = self.line {
ctx.canvas.center_on_map_pt(line.pt2());
} else {
ctx.canvas.center_on_map_pt(orig_center);
}
None
} else {
ctx.canvas.cam_zoom = self.cam_zoom.0 + percent * (self.cam_zoom.1 - self.cam_zoom.0);
ctx.canvas
.center_on_map_pt(line.dist_along(line.length() * percent));
if let Some(ref line) = self.line {
ctx.canvas
.center_on_map_pt(line.dist_along(line.length() * percent));
} else {
ctx.canvas.center_on_map_pt(orig_center);
}
Some(EventLoopMode::Animation)
}
}

View File

@ -128,9 +128,7 @@ impl Minimap {
return Some(Transition::PushWithMode(
Warping::new(
ctx,
// TODO The animated zooming is too quick. Need to specify that we want to
// interpolate over the zoom factor.
ctx.canvas.center_to_map_pt().offset(1.0, 1.0),
ctx.canvas.center_to_map_pt(),
Some(0.1),
None,
&mut ui.primary,
@ -142,7 +140,7 @@ impl Minimap {
return Some(Transition::PushWithMode(
Warping::new(
ctx,
ctx.canvas.center_to_map_pt().offset(1.0, 1.0),
ctx.canvas.center_to_map_pt(),
Some(10.0),
None,
&mut ui.primary,