Create a new line when stylus is lifted and placed down again

This commit is contained in:
2024-02-17 23:05:32 -06:00
parent 7e9fbc0162
commit 6126c221f4

View File

@@ -7,7 +7,7 @@ use relm4::drawing::DrawHandler;
#[derive(Debug)] #[derive(Debug)]
enum AppInput { enum AppInput {
Increment, Increment,
Decrement, NewLine((f64, f64)),
AddPoint((f64, f64)), AddPoint((f64, f64)),
Reset, Reset,
} }
@@ -16,6 +16,7 @@ enum AppInput {
struct Point { struct Point {
x: f64, x: f64,
y: f64, y: f64,
new_line: bool,
} }
#[derive(Debug)] #[derive(Debug)]
@@ -66,6 +67,9 @@ impl SimpleComponent for AppModel {
connect_motion[sender] => move |_, x, y| { connect_motion[sender] => move |_, x, y| {
sender.input(AppInput::AddPoint((x, y))); sender.input(AppInput::AddPoint((x, y)));
}, },
connect_down[sender] => move |_, x, y| {
sender.input(AppInput::NewLine((x, y)));
}
} }
}, },
}, },
@@ -86,7 +90,7 @@ impl SimpleComponent for AppModel {
gtk::Button { gtk::Button {
set_label: "Decrement", set_label: "Decrement",
connect_clicked[sender] => move |_| { connect_clicked[sender] => move |_| {
sender.input(AppInput::Decrement); sender.input(AppInput::Reset);
} }
}, },
@@ -125,19 +129,17 @@ impl SimpleComponent for AppModel {
AppInput::Increment => { AppInput::Increment => {
self.counter = self.counter.wrapping_add(1); self.counter = self.counter.wrapping_add(1);
} }
AppInput::Decrement => {
self.counter = self.counter.wrapping_sub(1);
}
AppInput::AddPoint((x, y)) => { AppInput::AddPoint((x, y)) => {
self.points.push(Point { self.points.push(Point { x, y, new_line: false })
x, }
y, AppInput::NewLine((x, y)) => {
}) self.points.push(Point { x, y, new_line: true })
} }
AppInput::Reset => { AppInput::Reset => {
cx.set_operator(Operator::Clear); cx.set_operator(Operator::Clear);
cx.set_source_rgba(0.0, 0.0, 0.0, 0.0); cx.set_source_rgba(0.0, 0.0, 0.0, 0.0);
cx.paint().expect("Could not fill context"); cx.paint().expect("Could not fill context");
self.points = Vec::new();
} }
} }
@@ -146,9 +148,9 @@ impl SimpleComponent for AppModel {
} }
fn draw(cx: &Context, points: &[Point]) { fn draw(cx: &Context, points: &[Point]) {
for (i, point) in points.into_iter().enumerate() { for (i, point) in points.into_iter().enumerate().filter(|(i, _)| *i != 0) {
if i > 0 { if !point.new_line {
let last_point = &points[i - 1]; let last_point = &points[i - 1];
cx.move_to(last_point.x, last_point.y); cx.move_to(last_point.x, last_point.y);