making an... almost rounded rectangle ;)

This commit is contained in:
Dustin Carlino 2019-11-24 07:21:30 -08:00
parent 042db55009
commit f0314c31b1
2 changed files with 32 additions and 3 deletions

View File

@ -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 {

View File

@ -214,6 +214,36 @@ impl Polygon {
pub fn shrink(&self, distance: f64) -> Vec<Polygon> {
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 {