From f0314c31b1864b081eba2eab31f7e0bc896ff0bd Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Sun, 24 Nov 2019 07:21:30 -0800 Subject: [PATCH] making an... almost rounded rectangle ;) --- ezgui/src/widgets/button.rs | 5 ++--- geom/src/polygon.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/ezgui/src/widgets/button.rs b/ezgui/src/widgets/button.rs index 87d0c6b3dd..d1781e65f7 100644 --- a/ezgui/src/widgets/button.rs +++ b/ezgui/src/widgets/button.rs @@ -206,11 +206,10 @@ impl TextButton { ctx: &EventCtx, ) -> TextButton { let (w, h) = ctx.canvas.text_dims(&text); - // TODO Rounded corners - let geom = Polygon::rectangle_topleft( - Pt2D::new(0.0, 0.0), + let geom = Polygon::rounded_rectangle( Distance::meters(w + 2.0 * HORIZ_PADDING), Distance::meters(h + 2.0 * VERT_PADDING), + Distance::meters(VERT_PADDING), ); TextButton { diff --git a/geom/src/polygon.rs b/geom/src/polygon.rs index 91c706f4f1..1b0deaf555 100644 --- a/geom/src/polygon.rs +++ b/geom/src/polygon.rs @@ -214,6 +214,36 @@ impl Polygon { pub fn shrink(&self, distance: f64) -> Vec { from_multi(to_geo(self.points()).offset(distance).unwrap()) } + + // Top-left at the origin; use translate + pub fn rounded_rectangle(width: Distance, height: Distance, radius: Distance) -> Polygon { + assert!(2.0 * radius <= width); + assert!(2.0 * radius <= height); + + let w = width.inner_meters(); + let h = height.inner_meters(); + let r = radius.inner_meters(); + + // TODO Round arcs on the corners ;) + + let mut pts = vec![]; + // Top-left corner + pts.push(Pt2D::new(0.0, r)); + pts.push(Pt2D::new(r, 0.0)); + // Top-right + pts.push(Pt2D::new(w - r, 0.0)); + pts.push(Pt2D::new(w, r)); + // Bottom-right + pts.push(Pt2D::new(w, h - r)); + pts.push(Pt2D::new(w - r, h)); + // Bottom-left + pts.push(Pt2D::new(r, h)); + pts.push(Pt2D::new(0.0, h - r)); + // Close it off + pts.push(Pt2D::new(0.0, r)); + + Polygon::new(&pts) + } } impl fmt::Display for Polygon {