Improve zooming logic in game_of_life

This commit is contained in:
Héctor Ramón Jiménez 2020-05-01 05:19:05 +02:00
parent c23995ecb4
commit 0a5f1bb676

View file

@ -178,7 +178,7 @@ mod grid {
fn default() -> Self { fn default() -> Self {
Self { Self {
life: Life::default(), life: Life::default(),
interaction: Interaction::default(), interaction: Interaction::None,
cache: Cache::default(), cache: Cache::default(),
translation: Vector::default(), translation: Vector::default(),
scaling: 1.0, scaling: 1.0,
@ -187,6 +187,9 @@ mod grid {
} }
impl Grid { impl Grid {
const MIN_SCALING: f32 = 0.1;
const MAX_SCALING: f32 = 2.0;
pub fn tick(&mut self) { pub fn tick(&mut self) {
self.life.tick(); self.life.tick();
self.cache.clear() self.cache.clear()
@ -286,10 +289,12 @@ mod grid {
mouse::Event::WheelScrolled { delta } => match delta { mouse::Event::WheelScrolled { delta } => match delta {
mouse::ScrollDelta::Lines { y, .. } mouse::ScrollDelta::Lines { y, .. }
| mouse::ScrollDelta::Pixels { y, .. } => { | mouse::ScrollDelta::Pixels { y, .. } => {
if y > 0.0 && self.scaling < 2.0 if y < 0.0 && self.scaling > Self::MIN_SCALING
|| y < 0.0 && self.scaling > 0.25 || y > 0.0 && self.scaling < Self::MAX_SCALING
{ {
self.scaling += y / 30.0; self.scaling = (self.scaling + y / 30.0)
.max(Self::MIN_SCALING)
.min(Self::MAX_SCALING);
self.cache.clear(); self.cache.clear();
} }
@ -467,10 +472,4 @@ mod grid {
Drawing, Drawing,
Panning { translation: Vector, start: Point }, Panning { translation: Vector, start: Point },
} }
impl Default for Interaction {
fn default() -> Interaction {
Interaction::None
}
}
} }