From 0b5028b1ab47707a469176e9bf20cacdd3a19861 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 19 Apr 2020 14:39:30 +0200 Subject: Draft `Program` interactivity for `Canvas` --- core/src/rectangle.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'core/src') diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index aead6e9a..db8ebfc8 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -1,4 +1,4 @@ -use crate::Point; +use crate::{Point, Size}; /// A rectangle. #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] @@ -43,6 +43,14 @@ impl Rectangle { self.y + self.height / 2.0 } + /// Returns the [`Size`] of the [`Rectangle`]. + /// + /// [`Size`]: struct.Size.html + /// [`Rectangle`]: struct.Rectangle.html + pub fn size(&self) -> Size { + Size::new(self.width, self.height) + } + /// Returns true if the given [`Point`] is contained in the [`Rectangle`]. /// /// [`Point`]: struct.Point.html -- cgit From 6c2e28d20e498526d1c6d624b4018e9392d0fb80 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 01:12:27 +0200 Subject: Implement `std::ops::Sub` for `Point` --- core/src/point.rs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'core/src') diff --git a/core/src/point.rs b/core/src/point.rs index 2b5ad154..a31bf967 100644 --- a/core/src/point.rs +++ b/core/src/point.rs @@ -67,3 +67,11 @@ impl std::ops::Sub for Point { } } } + +impl std::ops::Sub for Point { + type Output = Vector; + + fn sub(self, point: Point) -> Vector { + Vector::new(self.x - point.x, self.y - point.y) + } +} -- cgit From 20d79a43cce5a1bf0cb48a7668ac90d0ac82dfdc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 03:06:35 +0200 Subject: Implement `Default` for `Point` --- core/src/point.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/src') diff --git a/core/src/point.rs b/core/src/point.rs index a31bf967..3714aa2f 100644 --- a/core/src/point.rs +++ b/core/src/point.rs @@ -1,7 +1,7 @@ use crate::Vector; /// A 2D point. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Default)] pub struct Point { /// The X coordinate. pub x: f32, -- cgit From 56dbd683269b82da16d8eae3f98f352301750bf5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 03:11:01 +0200 Subject: Move reusable `mouse` types to `iced_core` --- core/src/button_state.rs | 9 +++++++ core/src/lib.rs | 3 +++ core/src/mouse.rs | 6 +++++ core/src/mouse/button.rs | 15 ++++++++++++ core/src/mouse/event.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 94 insertions(+) create mode 100644 core/src/button_state.rs create mode 100644 core/src/mouse.rs create mode 100644 core/src/mouse/button.rs create mode 100644 core/src/mouse/event.rs (limited to 'core/src') diff --git a/core/src/button_state.rs b/core/src/button_state.rs new file mode 100644 index 00000000..988043ba --- /dev/null +++ b/core/src/button_state.rs @@ -0,0 +1,9 @@ +/// The state of a button. +#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] +pub enum ButtonState { + /// The button is pressed. + Pressed, + + /// The button is __not__ pressed. + Released, +} diff --git a/core/src/lib.rs b/core/src/lib.rs index c2887a0b..606c1b8b 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -15,9 +15,11 @@ #![forbid(unsafe_code)] #![forbid(rust_2018_idioms)] pub mod keyboard; +pub mod mouse; mod align; mod background; +mod button_state; mod color; mod font; mod length; @@ -28,6 +30,7 @@ mod vector; pub use align::{Align, HorizontalAlignment, VerticalAlignment}; pub use background::Background; +pub use button_state::ButtonState; pub use color::Color; pub use font::Font; pub use length::Length; diff --git a/core/src/mouse.rs b/core/src/mouse.rs new file mode 100644 index 00000000..101e04d5 --- /dev/null +++ b/core/src/mouse.rs @@ -0,0 +1,6 @@ +//! Reuse basic mouse types. +mod button; +mod event; + +pub use button::Button; +pub use event::{Event, ScrollDelta}; diff --git a/core/src/mouse/button.rs b/core/src/mouse/button.rs new file mode 100644 index 00000000..aeb8a55d --- /dev/null +++ b/core/src/mouse/button.rs @@ -0,0 +1,15 @@ +/// The button of a mouse. +#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] +pub enum Button { + /// The left mouse button. + Left, + + /// The right mouse button. + Right, + + /// The middle (wheel) button. + Middle, + + /// Some other button. + Other(u8), +} diff --git a/core/src/mouse/event.rs b/core/src/mouse/event.rs new file mode 100644 index 00000000..52e9d851 --- /dev/null +++ b/core/src/mouse/event.rs @@ -0,0 +1,61 @@ +use super::Button; +use crate::ButtonState; + +/// A mouse event. +/// +/// _**Note:** This type is largely incomplete! If you need to track +/// additional events, feel free to [open an issue] and share your use case!_ +/// +/// [open an issue]: https://github.com/hecrj/iced/issues +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Event { + /// The mouse cursor entered the window. + CursorEntered, + + /// The mouse cursor left the window. + CursorLeft, + + /// The mouse cursor was moved + CursorMoved { + /// The X coordinate of the mouse position + x: f32, + + /// The Y coordinate of the mouse position + y: f32, + }, + + /// A mouse button was pressed or released. + Input { + /// The button identifier + button: Button, + + /// The state of the button + state: ButtonState, + }, + + /// The mouse wheel was scrolled. + WheelScrolled { + /// The scroll movement. + delta: ScrollDelta, + }, +} + +/// A scroll movement. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum ScrollDelta { + /// A line-based scroll movement + Lines { + /// The number of horizontal lines scrolled + x: f32, + + /// The number of vertical lines scrolled + y: f32, + }, + /// A pixel-based scroll movement + Pixels { + /// The number of horizontal pixels scrolled + x: f32, + /// The number of vertical pixels scrolled + y: f32, + }, +} -- cgit From 5d5e60a5cc647b3227f78e1f3a3b31b2c27a3cc7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 04:39:59 +0200 Subject: Implement `Rectangle::new` --- core/src/rectangle.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'core/src') diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index db8ebfc8..b9d0d5d2 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -17,6 +17,21 @@ pub struct Rectangle { } impl Rectangle { + /// Creates a new [`Rectangle`] with its top-left corner in the given + /// [`Point`] and with the provided [`Size`]. + /// + /// [`Rectangle`]: struct.Rectangle.html + /// [`Point`]: struct.Point.html + /// [`Size`]: struct.Size.html + pub fn new(top_left: Point, size: Size) -> Self { + Self { + x: top_left.x, + y: top_left.y, + width: size.width, + height: size.height, + } + } + /// Returns the [`Point`] at the center of the [`Rectangle`]. /// /// [`Point`]: struct.Point.html -- cgit From 69c60d372c18c20856f733efd337ac302b7de926 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 04:40:23 +0200 Subject: Implement `std::ops::Add` for `Rectangle` --- core/src/rectangle.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'core/src') diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index b9d0d5d2..87046df4 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -1,4 +1,4 @@ -use crate::{Point, Size}; +use crate::{Point, Size, Vector}; /// A rectangle. #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] @@ -140,3 +140,18 @@ impl From> for Rectangle { } } } + +impl std::ops::Add> for Rectangle +where + T: std::ops::Add, +{ + type Output = Rectangle; + + fn add(self, translation: Vector) -> Self { + Rectangle { + x: self.x + translation.x, + y: self.y + translation.y, + ..self + } + } +} -- cgit From 2539042b71d70afd4d8f262783d441e768811ee9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 06:24:12 +0200 Subject: Remove `Drawable` and rename `State` to `Program` --- core/src/rectangle.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'core/src') diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index 87046df4..e8d0538a 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -135,8 +135,8 @@ impl From> for Rectangle { Rectangle { x: rectangle.x as u32, y: rectangle.y as u32, - width: rectangle.width.ceil() as u32, - height: rectangle.height.ceil() as u32, + width: (rectangle.width + 0.5).round() as u32, + height: (rectangle.height + 0.5).round() as u32, } } } -- cgit From ec712c8032a25c5dc65152c3ab39bddaecbdce77 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 29 Apr 2020 03:21:46 +0200 Subject: Move `MouseCursor` to `iced_core` --- core/src/lib.rs | 2 ++ core/src/mouse_cursor.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 core/src/mouse_cursor.rs (limited to 'core/src') diff --git a/core/src/lib.rs b/core/src/lib.rs index 606c1b8b..f0072f61 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -23,6 +23,7 @@ mod button_state; mod color; mod font; mod length; +mod mouse_cursor; mod point; mod rectangle; mod size; @@ -34,6 +35,7 @@ pub use button_state::ButtonState; pub use color::Color; pub use font::Font; pub use length::Length; +pub use mouse_cursor::MouseCursor; pub use point::Point; pub use rectangle::Rectangle; pub use size::Size; diff --git a/core/src/mouse_cursor.rs b/core/src/mouse_cursor.rs new file mode 100644 index 00000000..78ddb0ae --- /dev/null +++ b/core/src/mouse_cursor.rs @@ -0,0 +1,36 @@ +/// The state of the mouse cursor. +#[derive(Debug, Eq, PartialEq, Clone, Copy, PartialOrd, Ord)] +pub enum MouseCursor { + /// The cursor is over a non-interactive widget. + Idle, + + /// The cursor is over a clickable widget. + Pointer, + + /// The cursor is over a busy widget. + Working, + + /// The cursor is over a grabbable widget. + Grab, + + /// The cursor is over a text widget. + Text, + + /// The cursor is over a widget that requires precision. + Crosshair, + + /// The cursor is grabbing a widget. + Grabbing, + + /// The cursor is resizing a widget horizontally. + ResizingHorizontally, + + /// The cursor is resizing a widget vertically. + ResizingVertically, +} + +impl Default for MouseCursor { + fn default() -> MouseCursor { + MouseCursor::Idle + } +} -- cgit From 475a2779a7a420ed6c1567a8dbc623114e2b7ec0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 29 Apr 2020 03:23:08 +0200 Subject: Implement `Rectangle::with_size` --- core/src/rectangle.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'core/src') diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index e8d0538a..6f953137 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -32,6 +32,20 @@ impl Rectangle { } } + /// Creates a new [`Rectangle`] with its top-left corner at the origin + /// and with the provided [`Size`]. + /// + /// [`Rectangle`]: struct.Rectangle.html + /// [`Size`]: struct.Size.html + pub fn with_size(size: Size) -> Self { + Self { + x: 0.0, + y: 0.0, + width: size.width, + height: size.height, + } + } + /// Returns the [`Point`] at the center of the [`Rectangle`]. /// /// [`Point`]: struct.Point.html -- cgit From afa0bca4fd1a5499fd24549eb49a44f9837597c6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 29 Apr 2020 08:25:27 +0200 Subject: Implement `Rectangle::position` --- core/src/rectangle.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'core/src') diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index 6f953137..8bc89a44 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -72,6 +72,13 @@ impl Rectangle { self.y + self.height / 2.0 } + /// Returns the position of the top left corner of the [`Rectangle`]. + /// + /// [`Rectangle`]: struct.Rectangle.html + pub fn position(&self) -> Point { + Point::new(self.x, self.y) + } + /// Returns the [`Size`] of the [`Rectangle`]. /// /// [`Size`]: struct.Size.html -- cgit From e55cd9652e7c7aea4dc2c6ccb83769246d1a808e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Apr 2020 04:53:15 +0200 Subject: Split `Input` mouse event by `ButtonState` --- core/src/mouse/event.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'core/src') diff --git a/core/src/mouse/event.rs b/core/src/mouse/event.rs index 52e9d851..2f07b207 100644 --- a/core/src/mouse/event.rs +++ b/core/src/mouse/event.rs @@ -1,5 +1,4 @@ use super::Button; -use crate::ButtonState; /// A mouse event. /// @@ -24,14 +23,11 @@ pub enum Event { y: f32, }, - /// A mouse button was pressed or released. - Input { - /// The button identifier - button: Button, + /// A mouse button was pressed. + ButtonPressed(Button), - /// The state of the button - state: ButtonState, - }, + /// A mouse button was released. + ButtonReleased(Button), /// The mouse wheel was scrolled. WheelScrolled { -- cgit From d8b9e03481301228ffeda4c6e48a021cb66f896c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Apr 2020 04:54:49 +0200 Subject: Remove `ButtonState` --- core/src/button_state.rs | 9 --------- core/src/lib.rs | 2 -- 2 files changed, 11 deletions(-) delete mode 100644 core/src/button_state.rs (limited to 'core/src') diff --git a/core/src/button_state.rs b/core/src/button_state.rs deleted file mode 100644 index 988043ba..00000000 --- a/core/src/button_state.rs +++ /dev/null @@ -1,9 +0,0 @@ -/// The state of a button. -#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] -pub enum ButtonState { - /// The button is pressed. - Pressed, - - /// The button is __not__ pressed. - Released, -} diff --git a/core/src/lib.rs b/core/src/lib.rs index f0072f61..ec1e185a 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -19,7 +19,6 @@ pub mod mouse; mod align; mod background; -mod button_state; mod color; mod font; mod length; @@ -31,7 +30,6 @@ mod vector; pub use align::{Align, HorizontalAlignment, VerticalAlignment}; pub use background::Background; -pub use button_state::ButtonState; pub use color::Color; pub use font::Font; pub use length::Length; -- cgit From 137664ca88a9bf2398380fd1c04b00c62c868383 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Apr 2020 04:59:07 +0200 Subject: Move `keyboard::Event` to `iced_core` --- core/src/keyboard.rs | 2 ++ core/src/keyboard/event.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 core/src/keyboard/event.rs (limited to 'core/src') diff --git a/core/src/keyboard.rs b/core/src/keyboard.rs index d98b2989..b26bdb3d 100644 --- a/core/src/keyboard.rs +++ b/core/src/keyboard.rs @@ -1,6 +1,8 @@ //! Reuse basic keyboard types. +mod event; mod key_code; mod modifiers_state; +pub use event::Event; pub use key_code::KeyCode; pub use modifiers_state::ModifiersState; diff --git a/core/src/keyboard/event.rs b/core/src/keyboard/event.rs new file mode 100644 index 00000000..bc8437a8 --- /dev/null +++ b/core/src/keyboard/event.rs @@ -0,0 +1,31 @@ +use super::{KeyCode, ModifiersState}; + +/// A keyboard event. +/// +/// _**Note:** This type is largely incomplete! If you need to track +/// additional events, feel free to [open an issue] and share your use case!_ +/// +/// [open an issue]: https://github.com/hecrj/iced/issues +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Event { + /// A keyboard key was pressed. + KeyPressed { + /// The key identifier + key_code: KeyCode, + + /// The state of the modifier keys + modifiers: ModifiersState, + }, + + /// A keyboard key was released. + KeyReleased { + /// The key identifier + key_code: KeyCode, + + /// The state of the modifier keys + modifiers: ModifiersState, + }, + + /// A unicode character was received. + CharacterReceived(char), +} -- cgit From 98bc8cf2a7c4944d762a0148ca9f615d6ccc0d6e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Apr 2020 08:16:38 +0200 Subject: Rename `MouseCursor` to `mouse::Interaction` --- core/src/lib.rs | 2 -- core/src/mouse.rs | 2 ++ core/src/mouse/interaction.rs | 20 ++++++++++++++++++++ core/src/mouse_cursor.rs | 36 ------------------------------------ 4 files changed, 22 insertions(+), 38 deletions(-) create mode 100644 core/src/mouse/interaction.rs delete mode 100644 core/src/mouse_cursor.rs (limited to 'core/src') diff --git a/core/src/lib.rs b/core/src/lib.rs index ec1e185a..6b9e612e 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -22,7 +22,6 @@ mod background; mod color; mod font; mod length; -mod mouse_cursor; mod point; mod rectangle; mod size; @@ -33,7 +32,6 @@ pub use background::Background; pub use color::Color; pub use font::Font; pub use length::Length; -pub use mouse_cursor::MouseCursor; pub use point::Point; pub use rectangle::Rectangle; pub use size::Size; diff --git a/core/src/mouse.rs b/core/src/mouse.rs index 101e04d5..25ce6ac3 100644 --- a/core/src/mouse.rs +++ b/core/src/mouse.rs @@ -1,6 +1,8 @@ //! Reuse basic mouse types. mod button; mod event; +mod interaction; pub use button::Button; pub use event::{Event, ScrollDelta}; +pub use interaction::Interaction; diff --git a/core/src/mouse/interaction.rs b/core/src/mouse/interaction.rs new file mode 100644 index 00000000..664147a7 --- /dev/null +++ b/core/src/mouse/interaction.rs @@ -0,0 +1,20 @@ +/// The interaction of a mouse cursor. +#[derive(Debug, Eq, PartialEq, Clone, Copy, PartialOrd, Ord)] +#[allow(missing_docs)] +pub enum Interaction { + Idle, + Pointer, + Grab, + Text, + Crosshair, + Working, + Grabbing, + ResizingHorizontally, + ResizingVertically, +} + +impl Default for Interaction { + fn default() -> Interaction { + Interaction::Idle + } +} diff --git a/core/src/mouse_cursor.rs b/core/src/mouse_cursor.rs deleted file mode 100644 index 78ddb0ae..00000000 --- a/core/src/mouse_cursor.rs +++ /dev/null @@ -1,36 +0,0 @@ -/// The state of the mouse cursor. -#[derive(Debug, Eq, PartialEq, Clone, Copy, PartialOrd, Ord)] -pub enum MouseCursor { - /// The cursor is over a non-interactive widget. - Idle, - - /// The cursor is over a clickable widget. - Pointer, - - /// The cursor is over a busy widget. - Working, - - /// The cursor is over a grabbable widget. - Grab, - - /// The cursor is over a text widget. - Text, - - /// The cursor is over a widget that requires precision. - Crosshair, - - /// The cursor is grabbing a widget. - Grabbing, - - /// The cursor is resizing a widget horizontally. - ResizingHorizontally, - - /// The cursor is resizing a widget vertically. - ResizingVertically, -} - -impl Default for MouseCursor { - fn default() -> MouseCursor { - MouseCursor::Idle - } -} -- cgit From 980ac6c2a42541224d694f9cd0699f6236fb66ca Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 1 May 2020 04:30:54 +0200 Subject: Add `UNIT` constant to `Size` --- core/src/size.rs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'core/src') diff --git a/core/src/size.rs b/core/src/size.rs index 4276f05f..a02299e8 100644 --- a/core/src/size.rs +++ b/core/src/size.rs @@ -15,6 +15,11 @@ impl Size { /// [`Size`]: struct.Size.html pub const ZERO: Size = Size::new(0., 0.); + /// A [`Size`] with a width and height of 1 unit. + /// + /// [`Size`]: struct.Size.html + pub const UNIT: Size = Size::new(1., 1.); + /// A [`Size`] with infinite width and height. /// /// [`Size`]: struct.Size.html -- cgit From 345f0e13362aed3debe30226e28c4c4b870e9b8a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 1 May 2020 04:32:56 +0200 Subject: Implement scalar multiplication for `Vector` --- core/src/vector.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'core/src') diff --git a/core/src/vector.rs b/core/src/vector.rs index a75053a0..def3f8c0 100644 --- a/core/src/vector.rs +++ b/core/src/vector.rs @@ -43,6 +43,17 @@ where } } +impl std::ops::Mul for Vector +where + T: std::ops::Mul + Copy, +{ + type Output = Self; + + fn mul(self, scale: T) -> Self { + Self::new(self.x * scale, self.y * scale) + } +} + impl Default for Vector where T: Default, -- cgit