From 71323c51bbdcb7dcccd6249fcd4ea3b1df589a9b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 1 May 2020 00:54:43 +0200 Subject: Simplify `Interaction` handling in `game_of_life` --- examples/game_of_life/src/main.rs | 42 ++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index 9f43b56a..8a841c91 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -164,7 +164,7 @@ mod grid { #[derive(Default)] pub struct Grid { life: HashSet, - interaction: Option, + interaction: Interaction, cache: canvas::Cache, translation: Vector, } @@ -174,11 +174,6 @@ mod grid { Populate(Cell), } - enum Interaction { - Drawing, - Panning { translation: Vector, start: Point }, - } - impl Grid { pub fn tick(&mut self) { use itertools::Itertools; @@ -242,7 +237,7 @@ mod grid { cursor: Cursor, ) -> Option { if let Event::Mouse(mouse::Event::ButtonReleased(_)) = event { - self.interaction = None; + self.interaction = Interaction::None; } let cursor_position = cursor.position_in(&bounds)?; @@ -258,15 +253,15 @@ mod grid { Event::Mouse(mouse_event) => match mouse_event { mouse::Event::ButtonPressed(button) => match button { mouse::Button::Left => { - self.interaction = Some(Interaction::Drawing); + self.interaction = Interaction::Drawing; populate } mouse::Button::Right => { - self.interaction = Some(Interaction::Panning { + self.interaction = Interaction::Panning { translation: self.translation, start: cursor_position, - }); + }; None } @@ -274,11 +269,8 @@ mod grid { }, mouse::Event::CursorMoved { .. } => { match self.interaction { - Some(Interaction::Drawing) => populate, - Some(Interaction::Panning { - translation, - start, - }) => { + Interaction::Drawing => populate, + Interaction::Panning { translation, start } => { self.translation = translation + (cursor_position - start); @@ -368,11 +360,9 @@ mod grid { cursor: Cursor, ) -> mouse::Interaction { match self.interaction { - Some(Interaction::Drawing) => mouse::Interaction::Crosshair, - Some(Interaction::Panning { .. }) => { - mouse::Interaction::Grabbing - } - None if cursor.is_over(&bounds) => { + Interaction::Drawing => mouse::Interaction::Crosshair, + Interaction::Panning { .. } => mouse::Interaction::Grabbing, + Interaction::None if cursor.is_over(&bounds) => { mouse::Interaction::Crosshair } _ => mouse::Interaction::default(), @@ -425,4 +415,16 @@ mod grid { rows.cartesian_product(columns).map(|(i, j)| Cell { i, j }) } } + + enum Interaction { + None, + Drawing, + Panning { translation: Vector, start: Point }, + } + + impl Default for Interaction { + fn default() -> Interaction { + Interaction::None + } + } } -- cgit