diff options
| author | 2020-11-14 02:17:21 +0100 | |
|---|---|---|
| committer | 2020-11-14 02:17:21 +0100 | |
| commit | 62295f554b885b8d486b666bc10dc4ecdc78c7d6 (patch) | |
| tree | 4758f6f17301b1a6c252a5a32248ae5498d1bb11 /examples/game_of_life/src | |
| parent | 73811c394a39c3816c67bffd2cf7d7a93c8803a9 (diff) | |
| parent | bf2d2561b8dde3e160438428b59c03c38a5f752a (diff) | |
| download | iced-62295f554b885b8d486b666bc10dc4ecdc78c7d6.tar.gz iced-62295f554b885b8d486b666bc10dc4ecdc78c7d6.tar.bz2 iced-62295f554b885b8d486b666bc10dc4ecdc78c7d6.zip | |
Merge pull request #614 from hecrj/feature/event-capturing
Event capturing
Diffstat (limited to 'examples/game_of_life/src')
| -rw-r--r-- | examples/game_of_life/src/main.rs | 74 | 
1 files changed, 45 insertions, 29 deletions
| diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index 3f087f88..e18bd6e0 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -153,9 +153,8 @@ impl Application for GameOfLife {  mod grid {      use crate::Preset;      use iced::{ -        canvas::{ -            self, Cache, Canvas, Cursor, Event, Frame, Geometry, Path, Text, -        }, +        canvas::event::{self, Event}, +        canvas::{self, Cache, Canvas, Cursor, Frame, Geometry, Path, Text},          mouse, Color, Element, HorizontalAlignment, Length, Point, Rectangle,          Size, Vector, VerticalAlignment,      }; @@ -328,12 +327,18 @@ mod grid {              event: Event,              bounds: Rectangle,              cursor: Cursor, -        ) -> Option<Message> { +        ) -> (event::Status, Option<Message>) {              if let Event::Mouse(mouse::Event::ButtonReleased(_)) = event {                  self.interaction = Interaction::None;              } -            let cursor_position = cursor.position_in(&bounds)?; +            let cursor_position = +                if let Some(position) = cursor.position_in(&bounds) { +                    position +                } else { +                    return (event::Status::Ignored, None); +                }; +              let cell = Cell::at(self.project(cursor_position, bounds.size()));              let is_populated = self.state.contains(&cell); @@ -345,28 +350,32 @@ mod grid {              match event {                  Event::Mouse(mouse_event) => match mouse_event { -                    mouse::Event::ButtonPressed(button) => match button { -                        mouse::Button::Left => { -                            self.interaction = if is_populated { -                                Interaction::Erasing -                            } else { -                                Interaction::Drawing -                            }; - -                            populate.or(unpopulate) -                        } -                        mouse::Button::Right => { -                            self.interaction = Interaction::Panning { -                                translation: self.translation, -                                start: cursor_position, -                            }; +                    mouse::Event::ButtonPressed(button) => { +                        let message = match button { +                            mouse::Button::Left => { +                                self.interaction = if is_populated { +                                    Interaction::Erasing +                                } else { +                                    Interaction::Drawing +                                }; + +                                populate.or(unpopulate) +                            } +                            mouse::Button::Right => { +                                self.interaction = Interaction::Panning { +                                    translation: self.translation, +                                    start: cursor_position, +                                }; -                            None -                        } -                        _ => None, -                    }, +                                None +                            } +                            _ => None, +                        }; + +                        (event::Status::Captured, message) +                    }                      mouse::Event::CursorMoved { .. } => { -                        match self.interaction { +                        let message = match self.interaction {                              Interaction::Drawing => populate,                              Interaction::Erasing => unpopulate,                              Interaction::Panning { translation, start } => { @@ -380,7 +389,14 @@ mod grid {                                  None                              }                              _ => None, -                        } +                        }; + +                        let event_status = match self.interaction { +                            Interaction::None => event::Status::Ignored, +                            _ => event::Status::Captured, +                        }; + +                        (event_status, message)                      }                      mouse::Event::WheelScrolled { delta } => match delta {                          mouse::ScrollDelta::Lines { y, .. } @@ -413,12 +429,12 @@ mod grid {                                  self.grid_cache.clear();                              } -                            None +                            (event::Status::Captured, None)                          }                      }, -                    _ => None, +                    _ => (event::Status::Ignored, None),                  }, -                _ => None, +                _ => (event::Status::Ignored, None),              }          } | 
