summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-04-29 04:25:49 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-04-29 04:25:49 +0200
commitdc51080328caa12d2b1fc02febc72cab70bb9f50 (patch)
tree0a9781223d1b78a8c404d57b17c61fb0136b196e /examples
parent5586034d6626e013cdd718aca1c4f19f6a060ff3 (diff)
downloadiced-dc51080328caa12d2b1fc02febc72cab70bb9f50.tar.gz
iced-dc51080328caa12d2b1fc02febc72cab70bb9f50.tar.bz2
iced-dc51080328caa12d2b1fc02febc72cab70bb9f50.zip
Introduce `Cursor` type in `canvas`
Diffstat (limited to '')
-rw-r--r--examples/bezier_tool/src/main.rs137
-rw-r--r--examples/clock/src/main.rs8
-rw-r--r--examples/solar_system/src/main.rs16
3 files changed, 83 insertions, 78 deletions
diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs
index 8c9ebd75..6473db75 100644
--- a/examples/bezier_tool/src/main.rs
+++ b/examples/bezier_tool/src/main.rs
@@ -69,15 +69,13 @@ impl Sandbox for Example {
mod bezier {
use iced::{
- canvas::{self, Canvas, Event, Frame, Geometry, Path, Stroke},
+ canvas::{self, Canvas, Cursor, Event, Frame, Geometry, Path, Stroke},
mouse, ButtonState, Element, Length, MouseCursor, Point, Rectangle,
- Size,
};
#[derive(Default)]
pub struct State {
pending: Option<Pending>,
- cursor_position: Point,
cache: canvas::Cache,
}
@@ -106,64 +104,62 @@ mod bezier {
}
impl<'a> canvas::Program<Curve> for Bezier<'a> {
- fn update(&mut self, event: Event, bounds: Size) -> Option<Curve> {
+ fn update(
+ &mut self,
+ event: Event,
+ bounds: Rectangle,
+ cursor: Cursor,
+ ) -> Option<Curve> {
+ let cursor_position = cursor.internal_position(&bounds)?;
+
match event {
Event::Mouse(mouse_event) => match mouse_event {
- mouse::Event::CursorMoved { x, y } => {
- self.state.cursor_position = Point::new(x, y);
-
- None
- }
mouse::Event::Input {
button: mouse::Button::Left,
state: ButtonState::Pressed,
- } if Rectangle::with_size(bounds)
- .contains(self.state.cursor_position) =>
- {
- match self.state.pending {
- None => {
- self.state.pending = Some(Pending::One {
- from: self.state.cursor_position,
- });
- None
- }
- Some(Pending::One { from }) => {
- self.state.pending = Some(Pending::Two {
- from,
- to: self.state.cursor_position,
- });
-
- None
- }
- Some(Pending::Two { from, to }) => {
- self.state.pending = None;
-
- Some(Curve {
- from,
- to,
- control: self.state.cursor_position,
- })
- }
+ } => match self.state.pending {
+ None => {
+ self.state.pending = Some(Pending::One {
+ from: cursor_position,
+ });
+ None
}
- }
+ Some(Pending::One { from }) => {
+ self.state.pending = Some(Pending::Two {
+ from,
+ to: cursor_position,
+ });
+
+ None
+ }
+ Some(Pending::Two { from, to }) => {
+ self.state.pending = None;
+
+ Some(Curve {
+ from,
+ to,
+ control: cursor_position,
+ })
+ }
+ },
_ => None,
},
}
}
- fn draw(&self, bounds: Size) -> Vec<Geometry> {
- let content = self.state.cache.draw(bounds, |frame: &mut Frame| {
- Curve::draw_all(self.curves, frame);
+ fn draw(&self, bounds: Rectangle, cursor: Cursor) -> Vec<Geometry> {
+ let content =
+ self.state.cache.draw(bounds.size(), |frame: &mut Frame| {
+ Curve::draw_all(self.curves, frame);
- frame.stroke(
- &Path::rectangle(Point::ORIGIN, frame.size()),
- Stroke::default(),
- );
- });
+ frame.stroke(
+ &Path::rectangle(Point::ORIGIN, frame.size()),
+ Stroke::default(),
+ );
+ });
if let Some(pending) = &self.state.pending {
- let pending_curve =
- pending.draw(bounds, self.state.cursor_position);
+ let pending_curve = pending.draw(bounds, cursor);
vec![content, pending_curve]
} else {
@@ -171,9 +167,12 @@ mod bezier {
}
}
- fn mouse_cursor(&self, bounds: Size) -> MouseCursor {
- if Rectangle::with_size(bounds).contains(self.state.cursor_position)
- {
+ fn mouse_cursor(
+ &self,
+ bounds: Rectangle,
+ cursor: Cursor,
+ ) -> MouseCursor {
+ if cursor.is_over(&bounds) {
MouseCursor::Crosshair
} else {
MouseCursor::default()
@@ -208,24 +207,26 @@ mod bezier {
}
impl Pending {
- fn draw(&self, bounds: Size, cursor_position: Point) -> Geometry {
- let mut frame = Frame::new(bounds);
-
- match *self {
- Pending::One { from } => {
- let line = Path::line(from, cursor_position);
- frame.stroke(&line, Stroke::default().with_width(2.0));
- }
- Pending::Two { from, to } => {
- let curve = Curve {
- from,
- to,
- control: cursor_position,
- };
-
- Curve::draw_all(&[curve], &mut frame);
- }
- };
+ fn draw(&self, bounds: Rectangle, cursor: Cursor) -> Geometry {
+ let mut frame = Frame::new(bounds.size());
+
+ if let Some(cursor_position) = cursor.internal_position(&bounds) {
+ match *self {
+ Pending::One { from } => {
+ let line = Path::line(from, cursor_position);
+ frame.stroke(&line, Stroke::default().with_width(2.0));
+ }
+ Pending::Two { from, to } => {
+ let curve = Curve {
+ from,
+ to,
+ control: cursor_position,
+ };
+
+ Curve::draw_all(&[curve], &mut frame);
+ }
+ };
+ }
frame.into_geometry()
}
diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs
index c2beddef..e6b17d8a 100644
--- a/examples/clock/src/main.rs
+++ b/examples/clock/src/main.rs
@@ -1,7 +1,7 @@
use iced::{
- canvas::{self, Cache, Canvas, Geometry, LineCap, Path, Stroke},
+ canvas::{self, Cache, Canvas, Cursor, Geometry, LineCap, Path, Stroke},
executor, Application, Color, Command, Container, Element, Length, Point,
- Settings, Size, Subscription, Vector,
+ Rectangle, Settings, Subscription, Vector,
};
pub fn main() {
@@ -75,10 +75,10 @@ impl Application for Clock {
}
impl canvas::Program<Message> for Clock {
- fn draw(&self, bounds: Size) -> Vec<Geometry> {
+ fn draw(&self, bounds: Rectangle, _cursor: Cursor) -> Vec<Geometry> {
use chrono::Timelike;
- let clock = self.clock.draw(bounds, |frame| {
+ let clock = self.clock.draw(bounds.size(), |frame| {
let center = frame.center();
let radius = frame.width().min(frame.height()) / 2.0;
diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs
index e2f107bd..a25e43df 100644
--- a/examples/solar_system/src/main.rs
+++ b/examples/solar_system/src/main.rs
@@ -7,8 +7,9 @@
//!
//! [1]: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations#An_animated_solar_system
use iced::{
- canvas, executor, window, Application, Canvas, Color, Command, Element,
- Length, Point, Settings, Size, Subscription, Vector,
+ canvas::{self, Cursor, Path, Stroke},
+ executor, window, Application, Canvas, Color, Command, Element, Length,
+ Point, Rectangle, Settings, Size, Subscription, Vector,
};
use std::time::Instant;
@@ -132,11 +133,14 @@ impl State {
}
impl<Message> canvas::Program<Message> for State {
- fn draw(&self, bounds: Size) -> Vec<canvas::Geometry> {
- use canvas::{Path, Stroke};
+ fn draw(
+ &self,
+ bounds: Rectangle,
+ _cursor: Cursor,
+ ) -> Vec<canvas::Geometry> {
use std::f32::consts::PI;
- let background = self.space_cache.draw(bounds, |frame| {
+ let background = self.space_cache.draw(bounds.size(), |frame| {
let space = Path::rectangle(Point::new(0.0, 0.0), frame.size());
let stars = Path::new(|path| {
@@ -151,7 +155,7 @@ impl<Message> canvas::Program<Message> for State {
frame.fill(&stars, Color::WHITE);
});
- let system = self.system_cache.draw(bounds, |frame| {
+ let system = self.system_cache.draw(bounds.size(), |frame| {
let center = frame.center();
let sun = Path::circle(center, Self::SUN_RADIUS);