use crate::base::{PointF, RectF, SizeF};
use crate::kernel::{PainterTrait, PathTrait};
use crate::platforms::Path;
use crate::shapes::ShapeTrait;
use crate::util::fuzzy_compare;
#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
pub struct BracketShape {
size: SizeF,
handle_base: Option<PointF>,
handle_width: f64,
corner_radius: f64,
path_is_dirty: bool,
path: Path,
}
impl BracketShape {
#[must_use]
pub fn new(
size: SizeF,
handle_base: Option<PointF>,
handle_width: f64,
corner_radius: f64,
) -> Self {
assert!(handle_width >= 0.0);
assert!(corner_radius >= 0.0);
let path = Path::new();
Self {
size,
handle_base,
handle_width,
corner_radius,
path_is_dirty: true,
path,
}
}
#[must_use]
pub fn new_square_bracket() -> Self {
let size = SizeF::from(18.0, 124.0);
Self::new(size, None, 0.0, 0.0)
}
#[must_use]
pub fn new_rounded_square_bracket() -> Self {
let size = SizeF::from(18.0, 124.0);
Self::new(size, None, 0.0, 17.28)
}
#[must_use]
pub fn new_indented_square_bracket() -> Self {
let size = SizeF::from(30.0, 124.0);
let handle_base = PointF::from(-30.0, 124.0 / 2.0);
Self::new(size, Some(handle_base), 0.0, 0.0)
}
#[must_use]
pub fn new_straight_curly_bracket() -> Self {
let size = SizeF::from(30.0, 124.0);
let handle_base = PointF::from(-30.0, 124.0 / 2.0);
Self::new(size, Some(handle_base), 16.0, 0.0)
}
#[must_use]
pub fn new_curly_bracket() -> Self {
let size = SizeF::from(30.0, 124.0);
let handle_base = PointF::from(-30.0, 124.0 / 2.0);
Self::new(size, Some(handle_base), 16.0, 17.28)
}
#[must_use]
pub const fn size(&self) -> SizeF {
self.size
}
pub fn set_size(&mut self, size: SizeF) {
self.size = size;
self.path_is_dirty = true;
}
#[must_use]
pub const fn handle_base(&self) -> Option<PointF> {
self.handle_base
}
pub fn set_handle_base(&mut self, point: Option<PointF>) {
self.handle_base = point;
self.path_is_dirty = true;
}
#[must_use]
pub const fn handle_width(&self) -> f64 {
self.handle_width
}
pub fn set_handle_width(&mut self, width: f64) {
if !fuzzy_compare(self.handle_width, width) {
self.handle_width = width;
self.path_is_dirty = true;
}
}
#[must_use]
pub const fn corner_radius(&self) -> f64 {
self.corner_radius
}
pub fn set_corner_radius(&mut self, corner_radius: f64) {
if !fuzzy_compare(self.corner_radius, corner_radius) {
self.corner_radius = corner_radius;
self.path_is_dirty = true;
}
}
fn update_path(&mut self) {
if !self.path_is_dirty {
return;
}
self.path.clear();
self.path_is_dirty = false;
}
}
impl ShapeTrait for BracketShape {
fn bounding_rect(&self) -> RectF {
todo!()
}
fn repaint(&mut self, painter: &mut dyn PainterTrait) {
self.update_path();
painter.stroke(&self.path);
}
}