diff --git a/examples/draw.rs b/examples/draw.rs index e3ea1d6539c890e0ff31a1286aa207ed8bcb7a82..9d410a40b788dfd86c60b6212bea448548ded616 100644 --- a/examples/draw.rs +++ b/examples/draw.rs @@ -16,7 +16,7 @@ use std::f64::consts::PI; -use druid::kurbo::Line; +use druid::kurbo::{BezPath, Line}; use druid::widget::{Flex, Label, WidgetExt}; use druid::{ AppLauncher, BoxConstraints, Color, Env, Event, EventCtx, LayoutCtx, LinearGradient, PaintCtx, @@ -40,6 +40,7 @@ impl Widget<()> for DrawWidget { match event { Event::MouseDown(m) => { info!("down: pos {:?}", m.pos); + //info!("down: pos {:?}, pos_rel {:?}", m.pos); self.state = MouseState::Down; self.t = 0.0; self.points.push(m.pos); @@ -62,7 +63,10 @@ impl Widget<()> for DrawWidget { } } - fn update(&mut self, _ctx: &mut UpdateCtx, _old_data: Option<&()>, _data: &(), _env: &Env) {} + fn update(&mut self, ctx: &mut UpdateCtx, _old_data: Option<&()>, _data: &(), env: &Env) { + //info!("update {:?}", env.get()); + ctx.invalidate(); + } fn layout( &mut self, @@ -75,33 +79,21 @@ impl Widget<()> for DrawWidget { } fn paint(&mut self, paint_ctx: &mut PaintCtx, _data: &(), _env: &Env) { - // let center = Point::new(50.0, 50.0); - // let ambit = center + 45.0 * Vec2::from_angle((0.75 + self.t) * 2.0 * PI); - // paint_ctx.stroke(Line::new(center, ambit), &Color::WHITE, 10.0); info!("paint"); - if let Some(mut old) = self.points.iter().next() { - for new in self.points.iter() { - let p1 = Point::new(1.0, 2.0); - let line = Line::new(*old, *new); - paint_ctx.stroke(line, &Color::WHITE, 1.0); - old = new; + + if let Some(p) = self.points.iter().next() { + let mut path = BezPath::new(); + path.move_to(*p); + + for p in self.points.iter() { + path.line_to(*p); } + + paint_ctx.stroke(path, &Color::WHITE, 1.0); } } } -// fn main() { -// let window = WindowDesc::new(|| AnimWidget { -// state: MouseState::Up, -// points: Vec::new(), -// t: 0.0, -// }); -// AppLauncher::with_window(window) -// .use_simple_logger() -// .launch(0) -// .expect("launch failed"); -// } - fn build_app() -> impl Widget<()> { let solid = Color::rgb8(0x3a, 0x3a, 0x3a); let gradient = LinearGradient::new( @@ -117,7 +109,7 @@ fn build_app() -> impl Widget<()> { Label::new("top left") .border(gradient.clone(), 4.0) .padding(10.0), - 1.0, + 1.0, // horizontal ) .with_child( DrawWidget { @@ -127,7 +119,7 @@ fn build_app() -> impl Widget<()> { } .border(gradient.clone(), 4.0) .padding(10.0), - 1.0, + 0.0, ) .with_child( Label::new("top right") @@ -135,7 +127,7 @@ fn build_app() -> impl Widget<()> { .padding(10.0), 1.0, ), - 1.0, + 0.0, // vertical ) .with_child( Flex::row() @@ -144,7 +136,7 @@ fn build_app() -> impl Widget<()> { .background(gradient.clone()) .rounded(10.0) .padding(10.0), - 1.0, + 0.0, ) .with_child( Label::new("bottom right")