#![allow(clippy::derived_hash_with_manual_eq)] use std::{fmt::Debug, sync::Arc};
use super::{emath, Color32, ColorMode, Pos2, Rect};
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Stroke {
pub width: f32,
pub color: Color32,
}
impl Stroke {
pub const NONE: Self = Self {
width: 0.0,
color: Color32::TRANSPARENT,
};
#[inline]
pub fn new(width: impl Into<f32>, color: impl Into<Color32>) -> Self {
Self {
width: width.into(),
color: color.into(),
}
}
#[inline]
pub fn is_empty(&self) -> bool {
self.width <= 0.0 || self.color == Color32::TRANSPARENT
}
}
impl<Color> From<(f32, Color)> for Stroke
where
Color: Into<Color32>,
{
#[inline(always)]
fn from((width, color): (f32, Color)) -> Self {
Self::new(width, color)
}
}
impl std::hash::Hash for Stroke {
#[inline(always)]
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let Self { width, color } = *self;
emath::OrderedFloat(width).hash(state);
color.hash(state);
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum StrokeKind {
Outside,
Inside,
Middle,
}
impl Default for StrokeKind {
fn default() -> Self {
Self::Middle
}
}
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct PathStroke {
pub width: f32,
pub color: ColorMode,
pub kind: StrokeKind,
}
impl PathStroke {
pub const NONE: Self = Self {
width: 0.0,
color: ColorMode::TRANSPARENT,
kind: StrokeKind::Middle,
};
#[inline]
pub fn new(width: impl Into<f32>, color: impl Into<Color32>) -> Self {
Self {
width: width.into(),
color: ColorMode::Solid(color.into()),
kind: StrokeKind::default(),
}
}
#[inline]
pub fn new_uv(
width: impl Into<f32>,
callback: impl Fn(Rect, Pos2) -> Color32 + Send + Sync + 'static,
) -> Self {
Self {
width: width.into(),
color: ColorMode::UV(Arc::new(callback)),
kind: StrokeKind::default(),
}
}
pub fn middle(self) -> Self {
Self {
kind: StrokeKind::Middle,
..self
}
}
pub fn outside(self) -> Self {
Self {
kind: StrokeKind::Outside,
..self
}
}
pub fn inside(self) -> Self {
Self {
kind: StrokeKind::Inside,
..self
}
}
#[inline]
pub fn is_empty(&self) -> bool {
self.width <= 0.0 || self.color == ColorMode::TRANSPARENT
}
}
impl<Color> From<(f32, Color)> for PathStroke
where
Color: Into<Color32>,
{
#[inline(always)]
fn from((width, color): (f32, Color)) -> Self {
Self::new(width, color)
}
}
impl From<Stroke> for PathStroke {
fn from(value: Stroke) -> Self {
Self {
width: value.width,
color: ColorMode::Solid(value.color),
kind: StrokeKind::default(),
}
}
}