use crate::base::{PointF, RectF};
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 GridShape {
horizontal_step: f64,
vertical_step: f64,
start_point: PointF,
viewport: RectF,
horizontal_visible: bool,
vertical_visible: bool,
path_is_dirty: bool,
path: Path,
}
impl GridShape {
#[must_use]
pub fn new(horizontal_step: f64, vertical_step: f64) -> Self {
assert!(horizontal_step >= 0.0 && vertical_step >= 0.0);
let start_point = PointF::new();
let viewport = RectF::from(0.0, 0.0, 100.0, 100.0);
let path = Path::new();
Self {
horizontal_step,
vertical_step,
start_point,
viewport,
horizontal_visible: true,
vertical_visible: true,
path_is_dirty: true,
path,
}
}
#[must_use]
pub const fn horizontal_step(&self) -> f64 {
self.horizontal_step
}
pub fn set_horizontal_step(&mut self, horizontal_step: f64) {
assert!(horizontal_step >= 0.0);
if !fuzzy_compare(self.horizontal_step, horizontal_step) {
self.horizontal_step = horizontal_step;
self.path_is_dirty = true;
}
}
#[must_use]
pub const fn horizontal_visible(&self) -> bool {
self.horizontal_visible
}
pub fn set_horizontal_visible(&mut self, visible: bool) {
if self.horizontal_visible != visible {
self.horizontal_visible = visible;
self.path_is_dirty = true;
}
}
#[must_use]
pub const fn vertical_step(&self) -> f64 {
self.vertical_step
}
pub fn set_vertical_step(&mut self, vertical_step: f64) {
assert!(vertical_step >= 0.0);
if !fuzzy_compare(self.vertical_step, vertical_step) {
self.vertical_step = vertical_step;
self.path_is_dirty = true;
}
}
#[must_use]
pub const fn vertical_visible(&self) -> bool {
self.vertical_visible
}
pub fn set_vertical_visible(&mut self, visible: bool) {
if self.vertical_visible != visible {
self.vertical_visible = visible;
self.path_is_dirty = true;
}
}
#[must_use]
pub const fn start_point(&self) -> PointF {
self.start_point
}
pub fn set_start_point(&mut self, start_point: PointF) {
self.start_point = start_point;
self.path_is_dirty = true;
}
#[must_use]
pub const fn viewport(&self) -> &RectF {
&self.viewport
}
pub fn set_viewport(&mut self, viewport: RectF) {
self.viewport = viewport;
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 GridShape {
fn bounding_rect(&self) -> RectF {
self.viewport.clone()
}
fn repaint(&mut self, painter: &mut dyn PainterTrait) {
self.update_path();
painter.stroke(&self.path);
}
}