use super::{
ScreenClip, ScreenVisualError, rectangle::validate_stroke, validate_color,
validate_draw_order_depth,
};
use bevy_ecs::prelude::Component;
use sim_engine::{
Color, Layer, LogicalPixels, LogicalScreenPosition, SceneError, ScreenScene, ShapeStyle, Stroke,
};
#[derive(Debug, Clone, Copy, PartialEq, Component)]
pub struct ScreenLineVisual {
start: LogicalScreenPosition,
end: LogicalScreenPosition,
stroke: Stroke,
layer: Layer,
depth: f32,
clip: ScreenClip,
}
impl ScreenLineVisual {
pub const fn layer(&self) -> Layer {
self.layer
}
pub fn set_layer(&mut self, layer: Layer) {
self.layer = layer;
}
pub const fn draw_order_depth(&self) -> f32 {
self.depth
}
pub fn set_draw_order_depth(&mut self, depth: f32) -> Result<(), ScreenVisualError> {
validate_draw_order_depth(depth)?;
self.depth = depth;
Ok(())
}
pub const fn clip(&self) -> ScreenClip {
self.clip
}
pub fn set_clip(&mut self, clip: ScreenClip) {
self.clip = clip;
}
pub fn new(
start: LogicalScreenPosition,
end: LogicalScreenPosition,
width: f32,
color: Color,
) -> Result<Self, ScreenVisualError> {
validate_endpoints(start, end)?;
let stroke = Stroke::new(width, color);
validate_stroke(stroke)?;
Ok(Self {
start,
end,
stroke,
layer: Layer::DEFAULT,
depth: 0.0,
clip: ScreenClip::Unclipped,
})
}
pub const fn start(&self) -> LogicalScreenPosition {
self.start
}
pub const fn end(&self) -> LogicalScreenPosition {
self.end
}
pub const fn stroke(&self) -> Stroke {
self.stroke
}
pub fn set_endpoints(
&mut self,
start: LogicalScreenPosition,
end: LogicalScreenPosition,
) -> Result<(), ScreenVisualError> {
validate_endpoints(start, end)?;
self.start = start;
self.end = end;
Ok(())
}
pub fn set_stroke(&mut self, stroke: Stroke) -> Result<(), ScreenVisualError> {
validate_stroke(stroke)?;
self.stroke = stroke;
Ok(())
}
pub fn contains_pointer(&self, sample: crate::input::PointerSample) -> bool {
if !self.clip.contains_pointer(sample) {
return false;
}
let a = self.start.to_vec2();
let b = self.end.to_vec2();
let p = sample.position().to_vec2();
let dx = f64::from(b.x()) - f64::from(a.x());
let dy = f64::from(b.y()) - f64::from(a.y());
let px = f64::from(p.x()) - f64::from(a.x());
let py = f64::from(p.y()) - f64::from(a.y());
let t = ((px * dx + py * dy) / (dx * dx + dy * dy)).clamp(0.0, 1.0);
(px - t * dx).hypot(py - t * dy) <= f64::from(self.stroke.width()) * 0.5
}
pub(crate) fn append(&self, scene: &mut ScreenScene) -> Result<(), SceneError> {
if !apply_clip(scene, self.clip)? {
return Ok(());
}
let width = LogicalPixels::new(self.stroke.width())
.map_err(|_| SceneError::InvalidStroke(sim_engine::ScenePrimitive::Line))?;
scene.try_line_on_layer(self.layer, self.start, self.end, width, self.stroke.color())
}
}
fn validate_endpoints(
start: LogicalScreenPosition,
end: LogicalScreenPosition,
) -> Result<(), ScreenVisualError> {
for value in [start, end] {
if !value.is_finite() {
return Err(ScreenVisualError::InvalidPosition { value });
}
}
if start == end || !(end.to_vec2() - start.to_vec2()).is_finite() {
return Err(ScreenVisualError::InvalidLine);
}
Ok(())
}
#[derive(Debug, Clone, Copy, PartialEq, Component)]
pub struct ScreenCircleVisual {
center: LogicalScreenPosition,
radius: f32,
color: Color,
filled: bool,
stroke: Option<Stroke>,
layer: Layer,
depth: f32,
clip: ScreenClip,
}
impl ScreenCircleVisual {
pub const fn layer(&self) -> Layer {
self.layer
}
pub fn set_layer(&mut self, layer: Layer) {
self.layer = layer;
}
pub const fn draw_order_depth(&self) -> f32 {
self.depth
}
pub fn set_draw_order_depth(&mut self, depth: f32) -> Result<(), ScreenVisualError> {
validate_draw_order_depth(depth)?;
self.depth = depth;
Ok(())
}
pub const fn clip(&self) -> ScreenClip {
self.clip
}
pub fn set_clip(&mut self, clip: ScreenClip) {
self.clip = clip;
}
pub fn new(
center: LogicalScreenPosition,
radius: f32,
color: Color,
) -> Result<Self, ScreenVisualError> {
validate_circle(center, radius)?;
validate_color(color)?;
Ok(Self {
center,
radius,
color,
filled: true,
stroke: None,
layer: Layer::DEFAULT,
depth: 0.0,
clip: ScreenClip::Unclipped,
})
}
pub const fn center(&self) -> LogicalScreenPosition {
self.center
}
pub const fn radius(&self) -> f32 {
self.radius
}
pub const fn color(&self) -> Color {
self.color
}
pub fn outlined(
center: LogicalScreenPosition,
radius: f32,
width: f32,
color: Color,
) -> Result<Self, ScreenVisualError> {
let mut circle = Self::new(center, radius, Color::TRANSPARENT)?;
let stroke = Stroke::new(width, color);
validate_stroke(stroke)?;
circle.filled = false;
circle.stroke = Some(stroke);
Ok(circle)
}
pub const fn fill_color(&self) -> Option<Color> {
if self.filled { Some(self.color) } else { None }
}
pub fn set_geometry(
&mut self,
center: LogicalScreenPosition,
radius: f32,
) -> Result<(), ScreenVisualError> {
validate_circle(center, radius)?;
self.center = center;
self.radius = radius;
Ok(())
}
pub fn set_center(&mut self, center: LogicalScreenPosition) -> Result<(), ScreenVisualError> {
self.set_geometry(center, self.radius)
}
pub fn set_radius(&mut self, radius: f32) -> Result<(), ScreenVisualError> {
self.set_geometry(self.center, radius)
}
pub fn set_color(&mut self, color: Color) -> Result<(), ScreenVisualError> {
validate_color(color)?;
self.color = color;
self.filled = true;
Ok(())
}
pub const fn stroke(&self) -> Option<Stroke> {
self.stroke
}
pub fn set_stroke(&mut self, stroke: Option<Stroke>) -> Result<(), ScreenVisualError> {
if !self.filled && stroke.is_none() {
return Err(ScreenVisualError::MissingPaint);
}
if let Some(stroke) = stroke {
validate_stroke(stroke)?;
}
self.stroke = stroke;
Ok(())
}
pub fn contains_pointer(&self, sample: crate::input::PointerSample) -> bool {
if !self.clip.contains_pointer(sample) {
return false;
}
let p = sample.position().to_vec2();
let c = self.center.to_vec2();
let distance =
(f64::from(p.x()) - f64::from(c.x())).hypot(f64::from(p.y()) - f64::from(c.y()));
if self.filled {
distance <= f64::from(self.radius)
} else {
self.stroke.is_some_and(|stroke| {
(distance - f64::from(self.radius)).abs() <= f64::from(stroke.width()) * 0.5
})
}
}
pub(crate) fn append(&self, scene: &mut ScreenScene) -> Result<(), SceneError> {
if !apply_clip(scene, self.clip)? {
return Ok(());
}
let style = if let (false, Some(stroke)) = (self.filled, self.stroke) {
ShapeStyle::stroked(stroke.width(), stroke.color())
} else {
self.stroke.map_or_else(
|| ShapeStyle::filled(self.color),
|stroke| ShapeStyle::fill_stroke(self.color, stroke.width(), stroke.color()),
)
};
let radius = LogicalPixels::new(self.radius)
.map_err(|_| SceneError::InvalidDimension(sim_engine::ScenePrimitive::Circle))?;
scene.try_circle_on_layer(self.layer, self.center, radius, style)
}
}
fn validate_circle(center: LogicalScreenPosition, radius: f32) -> Result<(), ScreenVisualError> {
if !center.is_finite() {
return Err(ScreenVisualError::InvalidPosition { value: center });
}
if !radius.is_finite() || radius <= 0.0 {
return Err(ScreenVisualError::InvalidRadius { value: radius });
}
Ok(())
}
pub(super) fn apply_clip(scene: &mut ScreenScene, clip: ScreenClip) -> Result<bool, SceneError> {
match clip {
ScreenClip::Empty => Ok(false),
ScreenClip::Unclipped => {
scene.set_screen_clip(None)?;
Ok(true)
}
ScreenClip::Rectangle(clip) => {
scene.set_screen_clip(Some(clip))?;
Ok(true)
}
}
}