From dc51080328caa12d2b1fc02febc72cab70bb9f50 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 29 Apr 2020 04:25:49 +0200 Subject: Introduce `Cursor` type in `canvas` --- wgpu/src/widget/canvas/cursor.rs | 50 +++++++++++++++++++++++++++++++++++++++ wgpu/src/widget/canvas/program.rs | 32 ++++++++++++++++--------- 2 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 wgpu/src/widget/canvas/cursor.rs (limited to 'wgpu/src/widget/canvas') diff --git a/wgpu/src/widget/canvas/cursor.rs b/wgpu/src/widget/canvas/cursor.rs new file mode 100644 index 00000000..a559782a --- /dev/null +++ b/wgpu/src/widget/canvas/cursor.rs @@ -0,0 +1,50 @@ +use iced_native::{Point, Rectangle}; + +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Cursor { + Available(Point), + Unavailable, +} + +impl Cursor { + // TODO: Remove this once this type is used in `iced_native` to encode + // proper cursor availability + pub(crate) fn from_window_position(position: Point) -> Self { + if position.x < 0.0 || position.y < 0.0 { + Cursor::Unavailable + } else { + Cursor::Available(position) + } + } + + pub fn position(&self) -> Option { + match self { + Cursor::Available(position) => Some(*position), + Cursor::Unavailable => None, + } + } + + pub fn relative_position(&self, bounds: &Rectangle) -> Option { + match self { + Cursor::Available(position) => { + Some(Point::new(position.x - bounds.x, position.y - bounds.y)) + } + _ => None, + } + } + + pub fn internal_position(&self, bounds: &Rectangle) -> Option { + if self.is_over(bounds) { + self.relative_position(bounds) + } else { + None + } + } + + pub fn is_over(&self, bounds: &Rectangle) -> bool { + match self { + Cursor::Available(position) => bounds.contains(*position), + Cursor::Unavailable => false, + } + } +} diff --git a/wgpu/src/widget/canvas/program.rs b/wgpu/src/widget/canvas/program.rs index 9e4aca89..f8e54514 100644 --- a/wgpu/src/widget/canvas/program.rs +++ b/wgpu/src/widget/canvas/program.rs @@ -1,14 +1,19 @@ -use crate::canvas::{Event, Geometry, Size}; -use iced_native::MouseCursor; +use crate::canvas::{Cursor, Event, Geometry}; +use iced_native::{MouseCursor, Rectangle}; pub trait Program { - fn update(&mut self, _event: Event, _bounds: Size) -> Option { + fn update( + &mut self, + _event: Event, + _bounds: Rectangle, + _cursor: Cursor, + ) -> Option { None } - fn draw(&self, bounds: Size) -> Vec; + fn draw(&self, bounds: Rectangle, cursor: Cursor) -> Vec; - fn mouse_cursor(&self, _bounds: Size) -> MouseCursor { + fn mouse_cursor(&self, _bounds: Rectangle, _cursor: Cursor) -> MouseCursor { MouseCursor::default() } } @@ -17,15 +22,20 @@ impl Program for &mut T where T: Program, { - fn update(&mut self, event: Event, bounds: Size) -> Option { - T::update(self, event, bounds) + fn update( + &mut self, + event: Event, + bounds: Rectangle, + cursor: Cursor, + ) -> Option { + T::update(self, event, bounds, cursor) } - fn draw(&self, bounds: Size) -> Vec { - T::draw(self, bounds) + fn draw(&self, bounds: Rectangle, cursor: Cursor) -> Vec { + T::draw(self, bounds, cursor) } - fn mouse_cursor(&self, bounds: Size) -> MouseCursor { - T::mouse_cursor(self, bounds) + fn mouse_cursor(&self, bounds: Rectangle, cursor: Cursor) -> MouseCursor { + T::mouse_cursor(self, bounds, cursor) } } -- cgit