1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
//! This example showcases an interactive `Canvas` for drawing Bézier curves.
use iced::{
button, Align, Button, Column, Element, Length, Sandbox, Settings, Text,
};
pub fn main() {
Example::run(Settings {
antialiasing: true,
..Settings::default()
});
}
#[derive(Default)]
struct Example {
bezier: bezier::State,
curves: Vec<bezier::Curve>,
button_state: button::State,
}
#[derive(Debug, Clone, Copy)]
enum Message {
AddCurve(bezier::Curve),
Clear,
}
impl Sandbox for Example {
type Message = Message;
fn new() -> Self {
Example::default()
}
fn title(&self) -> String {
String::from("Bezier tool - Iced")
}
fn update(&mut self, message: Message) {
match message {
Message::AddCurve(curve) => {
self.curves.push(curve);
self.bezier.request_redraw();
}
Message::Clear => {
self.bezier = bezier::State::default();
self.curves.clear();
}
}
}
fn view(&mut self) -> Element<Message> {
Column::new()
.padding(20)
.spacing(20)
.align_items(Align::Center)
.push(
Text::new("Bezier tool example")
.width(Length::Shrink)
.size(50),
)
.push(self.bezier.view(&self.curves).map(Message::AddCurve))
.push(
Button::new(&mut self.button_state, Text::new("Clear"))
.padding(8)
.on_press(Message::Clear),
)
.into()
}
}
mod bezier {
use iced::{
canvas::{
self, Canvas, Drawable, Event, Frame, Geometry, Path, Stroke,
},
mouse, ButtonState, Element, Length, Point, Size,
};
#[derive(Default)]
pub struct State {
pending: Option<Pending>,
cursor_position: Point,
cache: canvas::Cache,
}
impl State {
pub fn view<'a>(
&'a mut self,
curves: &'a [Curve],
) -> Element<'a, Curve> {
Canvas::new(Bezier {
state: self,
curves,
})
.width(Length::Fill)
.height(Length::Fill)
.into()
}
pub fn request_redraw(&mut self) {
self.cache.clear()
}
}
struct Bezier<'a> {
state: &'a mut State,
curves: &'a [Curve],
}
impl<'a> canvas::State<Curve> for Bezier<'a> {
fn update(&mut self, event: Event, _bounds: Size) -> Option<Curve> {
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,
} => 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,
})
}
},
_ => None,
},
}
}
fn draw(&self, bounds: Size) -> Vec<Geometry> {
let curves = self.state.cache.draw(bounds, &self.curves);
if let Some(pending) = &self.state.pending {
let pending_curve =
pending.draw(bounds, self.state.cursor_position);
vec![curves, pending_curve]
} else {
vec![curves]
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct Curve {
from: Point,
to: Point,
control: Point,
}
impl Drawable for Curve {
fn draw(&self, frame: &mut Frame) {
let curve = Path::new(|p| {
p.move_to(self.from);
p.quadratic_curve_to(self.control, self.to);
});
frame.stroke(&curve, Stroke::default().with_width(2.0));
}
}
#[derive(Debug, Clone, Copy)]
enum Pending {
One { from: Point },
Two { from: Point, to: Point },
}
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(&mut frame);
}
};
frame.into_geometry()
}
}
}
|