summaryrefslogtreecommitdiffstats
path: root/wgpu/src/widget/canvas/cursor.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2020-05-05 00:05:47 +0200
committerLibravatar GitHub <noreply@github.com>2020-05-05 00:05:47 +0200
commit7dc02a5e16a3143b7c3ba9270207e3ebda71d567 (patch)
treedd727f138641fbda008af8e7827369cc99420749 /wgpu/src/widget/canvas/cursor.rs
parent27aad74a32fd8ac2b12f9d32df8a3b61a3175457 (diff)
parent93c6be5eef577f0778b5787dac37351c035ed471 (diff)
downloadiced-7dc02a5e16a3143b7c3ba9270207e3ebda71d567.tar.gz
iced-7dc02a5e16a3143b7c3ba9270207e3ebda71d567.tar.bz2
iced-7dc02a5e16a3143b7c3ba9270207e3ebda71d567.zip
Merge pull request #325 from hecrj/feature/canvas-interaction
Canvas interactivity and Game of Life example
Diffstat (limited to 'wgpu/src/widget/canvas/cursor.rs')
-rw-r--r--wgpu/src/widget/canvas/cursor.rs72
1 files changed, 72 insertions, 0 deletions
diff --git a/wgpu/src/widget/canvas/cursor.rs b/wgpu/src/widget/canvas/cursor.rs
new file mode 100644
index 00000000..456760ea
--- /dev/null
+++ b/wgpu/src/widget/canvas/cursor.rs
@@ -0,0 +1,72 @@
+use iced_native::{Point, Rectangle};
+
+/// The mouse cursor state.
+#[derive(Debug, Clone, Copy, PartialEq)]
+pub enum Cursor {
+ /// The cursor has a defined position.
+ Available(Point),
+
+ /// The cursor is currently unavailable (i.e. out of bounds or busy).
+ 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)
+ }
+ }
+
+ /// Returns the absolute position of the [`Cursor`], if available.
+ ///
+ /// [`Cursor`]: enum.Cursor.html
+ pub fn position(&self) -> Option<Point> {
+ match self {
+ Cursor::Available(position) => Some(*position),
+ Cursor::Unavailable => None,
+ }
+ }
+
+ /// Returns the relative position of the [`Cursor`] inside the given bounds,
+ /// if available.
+ ///
+ /// If the [`Cursor`] is not over the provided bounds, this method will
+ /// return `None`.
+ ///
+ /// [`Cursor`]: enum.Cursor.html
+ pub fn position_in(&self, bounds: &Rectangle) -> Option<Point> {
+ if self.is_over(bounds) {
+ self.position_from(bounds.position())
+ } else {
+ None
+ }
+ }
+
+ /// Returns the relative position of the [`Cursor`] from the given origin,
+ /// if available.
+ ///
+ /// [`Cursor`]: enum.Cursor.html
+ pub fn position_from(&self, origin: Point) -> Option<Point> {
+ match self {
+ Cursor::Available(position) => {
+ Some(Point::new(position.x - origin.x, position.y - origin.y))
+ }
+ Cursor::Unavailable => None,
+ }
+ }
+
+ /// Returns whether the [`Cursor`] is currently over the provided bounds
+ /// or not.
+ ///
+ /// [`Cursor`]: enum.Cursor.html
+ pub fn is_over(&self, bounds: &Rectangle) -> bool {
+ match self {
+ Cursor::Available(position) => bounds.contains(*position),
+ Cursor::Unavailable => false,
+ }
+ }
+}