Simplify Interaction
handling in game_of_life
This commit is contained in:
parent
ee97887409
commit
71323c51bb
@ -164,7 +164,7 @@ mod grid {
|
|||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Grid {
|
pub struct Grid {
|
||||||
life: HashSet<Cell>,
|
life: HashSet<Cell>,
|
||||||
interaction: Option<Interaction>,
|
interaction: Interaction,
|
||||||
cache: canvas::Cache,
|
cache: canvas::Cache,
|
||||||
translation: Vector,
|
translation: Vector,
|
||||||
}
|
}
|
||||||
@ -174,11 +174,6 @@ mod grid {
|
|||||||
Populate(Cell),
|
Populate(Cell),
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Interaction {
|
|
||||||
Drawing,
|
|
||||||
Panning { translation: Vector, start: Point },
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Grid {
|
impl Grid {
|
||||||
pub fn tick(&mut self) {
|
pub fn tick(&mut self) {
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
@ -242,7 +237,7 @@ mod grid {
|
|||||||
cursor: Cursor,
|
cursor: Cursor,
|
||||||
) -> Option<Message> {
|
) -> Option<Message> {
|
||||||
if let Event::Mouse(mouse::Event::ButtonReleased(_)) = event {
|
if let Event::Mouse(mouse::Event::ButtonReleased(_)) = event {
|
||||||
self.interaction = None;
|
self.interaction = Interaction::None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let cursor_position = cursor.position_in(&bounds)?;
|
let cursor_position = cursor.position_in(&bounds)?;
|
||||||
@ -258,15 +253,15 @@ mod grid {
|
|||||||
Event::Mouse(mouse_event) => match mouse_event {
|
Event::Mouse(mouse_event) => match mouse_event {
|
||||||
mouse::Event::ButtonPressed(button) => match button {
|
mouse::Event::ButtonPressed(button) => match button {
|
||||||
mouse::Button::Left => {
|
mouse::Button::Left => {
|
||||||
self.interaction = Some(Interaction::Drawing);
|
self.interaction = Interaction::Drawing;
|
||||||
|
|
||||||
populate
|
populate
|
||||||
}
|
}
|
||||||
mouse::Button::Right => {
|
mouse::Button::Right => {
|
||||||
self.interaction = Some(Interaction::Panning {
|
self.interaction = Interaction::Panning {
|
||||||
translation: self.translation,
|
translation: self.translation,
|
||||||
start: cursor_position,
|
start: cursor_position,
|
||||||
});
|
};
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@ -274,11 +269,8 @@ mod grid {
|
|||||||
},
|
},
|
||||||
mouse::Event::CursorMoved { .. } => {
|
mouse::Event::CursorMoved { .. } => {
|
||||||
match self.interaction {
|
match self.interaction {
|
||||||
Some(Interaction::Drawing) => populate,
|
Interaction::Drawing => populate,
|
||||||
Some(Interaction::Panning {
|
Interaction::Panning { translation, start } => {
|
||||||
translation,
|
|
||||||
start,
|
|
||||||
}) => {
|
|
||||||
self.translation =
|
self.translation =
|
||||||
translation + (cursor_position - start);
|
translation + (cursor_position - start);
|
||||||
|
|
||||||
@ -368,11 +360,9 @@ mod grid {
|
|||||||
cursor: Cursor,
|
cursor: Cursor,
|
||||||
) -> mouse::Interaction {
|
) -> mouse::Interaction {
|
||||||
match self.interaction {
|
match self.interaction {
|
||||||
Some(Interaction::Drawing) => mouse::Interaction::Crosshair,
|
Interaction::Drawing => mouse::Interaction::Crosshair,
|
||||||
Some(Interaction::Panning { .. }) => {
|
Interaction::Panning { .. } => mouse::Interaction::Grabbing,
|
||||||
mouse::Interaction::Grabbing
|
Interaction::None if cursor.is_over(&bounds) => {
|
||||||
}
|
|
||||||
None if cursor.is_over(&bounds) => {
|
|
||||||
mouse::Interaction::Crosshair
|
mouse::Interaction::Crosshair
|
||||||
}
|
}
|
||||||
_ => mouse::Interaction::default(),
|
_ => mouse::Interaction::default(),
|
||||||
@ -425,4 +415,16 @@ mod grid {
|
|||||||
rows.cartesian_product(columns).map(|(i, j)| Cell { i, j })
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user