diff options
author | 2020-05-01 00:54:43 +0200 | |
---|---|---|
committer | 2020-05-01 00:54:43 +0200 | |
commit | 71323c51bbdcb7dcccd6249fcd4ea3b1df589a9b (patch) | |
tree | 4b2d3897a2041e8b4c28368d614425b71abcdb2e /examples/game_of_life | |
parent | ee97887409849395ecfd63e499c5d5372b121aa3 (diff) | |
download | iced-71323c51bbdcb7dcccd6249fcd4ea3b1df589a9b.tar.gz iced-71323c51bbdcb7dcccd6249fcd4ea3b1df589a9b.tar.bz2 iced-71323c51bbdcb7dcccd6249fcd4ea3b1df589a9b.zip |
Simplify `Interaction` handling in `game_of_life`
Diffstat (limited to 'examples/game_of_life')
-rw-r--r-- | examples/game_of_life/src/main.rs | 42 |
1 files 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<Cell>, - interaction: Option<Interaction>, + 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<Message> { 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 + } + } } |