hUI/hui/src/draw/corner_radius.rs

37 lines
845 B
Rust
Raw Normal View History

2024-02-19 12:40:18 -06:00
use std::num::NonZeroU16;
2024-02-20 10:49:44 -06:00
use crate::rectangle::Corners;
2024-02-20 10:30:26 -06:00
2024-02-20 13:29:27 -06:00
//TODO uneven corners (separate width/height for each corner)
2024-02-20 10:30:26 -06:00
fn point_count(corners: Corners<f32>) -> NonZeroU16 {
//Increase for higher quality
const VTX_PER_CORER_RADIUS_PIXEL: f32 = 0.5;
NonZeroU16::new(
(corners.max_f32() * VTX_PER_CORER_RADIUS_PIXEL).round() as u16 + 2
).unwrap()
2024-02-19 12:40:18 -06:00
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RoundedCorners {
2024-02-20 10:30:26 -06:00
pub radius: Corners<f32>,
2024-02-19 12:40:18 -06:00
pub point_count: NonZeroU16,
}
impl RoundedCorners {
2024-02-20 10:30:26 -06:00
pub fn from_radius(radius: Corners<f32>) -> Self {
2024-02-19 12:40:18 -06:00
Self {
radius,
2024-02-20 10:30:26 -06:00
point_count: point_count(radius),
2024-02-19 12:40:18 -06:00
}
}
}
impl Default for RoundedCorners {
fn default() -> Self {
Self {
2024-02-20 10:30:26 -06:00
radius: Corners::default(),
2024-02-19 12:40:18 -06:00
point_count: NonZeroU16::new(8).unwrap(),
}
}
}