Skip to content

Commit 0a5f1bb

Browse files
committed
Improve zooming logic in game_of_life
1 parent c23995e commit 0a5f1bb

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

examples/game_of_life/src/main.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ mod grid {
178178
fn default() -> Self {
179179
Self {
180180
life: Life::default(),
181-
interaction: Interaction::default(),
181+
interaction: Interaction::None,
182182
cache: Cache::default(),
183183
translation: Vector::default(),
184184
scaling: 1.0,
@@ -187,6 +187,9 @@ mod grid {
187187
}
188188

189189
impl Grid {
190+
const MIN_SCALING: f32 = 0.1;
191+
const MAX_SCALING: f32 = 2.0;
192+
190193
pub fn tick(&mut self) {
191194
self.life.tick();
192195
self.cache.clear()
@@ -286,10 +289,12 @@ mod grid {
286289
mouse::Event::WheelScrolled { delta } => match delta {
287290
mouse::ScrollDelta::Lines { y, .. }
288291
| mouse::ScrollDelta::Pixels { y, .. } => {
289-
if y > 0.0 && self.scaling < 2.0
290-
|| y < 0.0 && self.scaling > 0.25
292+
if y < 0.0 && self.scaling > Self::MIN_SCALING
293+
|| y > 0.0 && self.scaling < Self::MAX_SCALING
291294
{
292-
self.scaling += y / 30.0;
295+
self.scaling = (self.scaling + y / 30.0)
296+
.max(Self::MIN_SCALING)
297+
.min(Self::MAX_SCALING);
293298

294299
self.cache.clear();
295300
}
@@ -467,10 +472,4 @@ mod grid {
467472
Drawing,
468473
Panning { translation: Vector, start: Point },
469474
}
470-
471-
impl Default for Interaction {
472-
fn default() -> Interaction {
473-
Interaction::None
474-
}
475-
}
476475
}

0 commit comments

Comments
 (0)