Draw rounded rects and quarter circles

This commit is contained in:
mars 2022-07-10 09:35:36 -06:00
parent 7eb0983ee9
commit 7fdf6c6c83
1 changed files with 59 additions and 3 deletions

View File

@ -1,6 +1,13 @@
use crate::abi::UiPanel;
use crate::{Color, Vec2};
pub enum Corner {
TopRight,
BottomRight,
BottomLeft,
TopLeft,
}
pub struct DrawContext {
panel: UiPanel,
offset: Vec2,
@ -44,6 +51,28 @@ impl DrawContext {
}
}
pub fn draw_quarter_circle(&self, corner: Corner, center: Vec2, radius: f32, color: Color) {
use std::f32::consts::{FRAC_PI_2, PI};
let spoke_num = 16.0;
let delta = PI / 4.0 / spoke_num;
let (mut theta, limit) = match corner {
Corner::TopRight => (0.0, FRAC_PI_2),
Corner::BottomRight => (FRAC_PI_2 * 3.0, PI * 2.0),
Corner::BottomLeft => (PI, FRAC_PI_2 * 3.0),
Corner::TopLeft => (FRAC_PI_2, PI),
};
let mut last_spoke = Vec2::from_angle(theta) * radius + center;
while theta < limit {
theta += delta;
let new_spoke = Vec2::from_angle(theta) * radius + center;
self.draw_triangle(center, last_spoke, new_spoke, color);
last_spoke = new_spoke;
}
}
pub fn draw_ring(&self, center: Vec2, radius: f32, thickness: f32, color: Color) {
use std::f32::consts::PI;
@ -68,16 +97,43 @@ impl DrawContext {
}
}
pub fn draw_rect(&self, xy: &Vec2, size: &Vec2, color: Color) {
let v1 = *xy;
pub fn draw_rect(&self, xy: Vec2, size: Vec2, color: Color) {
let v1 = xy;
let v2 = v1 + Vec2::new(size.x, 0.0);
let v3 = v1 + Vec2::new(0.0, size.y);
let v4 = v1 + *size;
let v4 = v1 + size;
self.draw_triangle(v1, v2, v3, color);
self.draw_triangle(v2, v3, v4, color);
}
pub fn draw_rounded_rect(&self, xy: Vec2, size: Vec2, radius: f32, color: Color) {
let xoff = Vec2::new(radius, 0.0);
let yoff = Vec2::new(0.0, radius);
let sizex = Vec2::new(size.x, 0.0);
let sizey = Vec2::new(0.0, size.y);
let inner_size = size - Vec2::new(radius, radius) * 2.0;
let lr_edge_size = Vec2::new(radius, inner_size.y);
let tb_edge_size = Vec2::new(inner_size.x, radius);
self.draw_rect(xy + yoff, lr_edge_size, color); // left edge
self.draw_rect(xy + yoff + sizex - xoff, lr_edge_size, color); // right edge
self.draw_rect(xy + xoff, tb_edge_size, color); // bottom edge
self.draw_rect(xy + xoff + sizey - yoff, tb_edge_size, color); // top edge
let tr = xy + size - xoff - yoff;
let br = xy + sizex - xoff + yoff;
let bl = xy + xoff + yoff;
let tl = xy + sizey + xoff - yoff;
self.draw_quarter_circle(Corner::TopRight, tr, radius, color);
self.draw_quarter_circle(Corner::BottomRight, br, radius, color);
self.draw_quarter_circle(Corner::BottomLeft, bl, radius, color);
self.draw_quarter_circle(Corner::TopLeft, tl, radius, color);
self.draw_rect(bl, inner_size, color);
}
pub fn with_offset(&self, offset: Vec2) -> Self {
Self {
offset: self.offset + offset,