1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-27 15:13:12 -04:00
Files
egui/crates/epaint/src/stroke.rs
Joe Sorensen 2ce82cce21 Added ability to define colors at UV coordinates along a path (#4353)
<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to add commits to your PR.
* Remember to run `cargo fmt` and `cargo cranky`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

I had to make a couple types not Copy because closures, but it should'nt
be a massive deal.

I tried my best to make the API change as non breaking as possible.
Anywhere a PathStroke is used, you can just use a normal Stroke instead.
As mentioned above, the bezier paths couldn't be copy anymore, but IMO
that's a minor caveat.

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2024-04-22 18:35:09 +02:00

122 lines
3.2 KiB
Rust

#![allow(clippy::derived_hash_with_manual_eq)] // We need to impl Hash for f32, but we don't implement Eq, which is fine
use std::{fmt::Debug, sync::Arc};
use super::*;
/// Describes the width and color of a line.
///
/// The default stroke is the same as [`Stroke::NONE`].
#[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 {
/// Same as [`Stroke::default`].
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(),
}
}
/// True if width is zero or color is transparent
#[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);
}
}
/// Describes the width and color of paths. The color can either be solid or provided by a callback. For more information, see [`ColorMode`]
///
/// The default stroke is the same as [`Stroke::NONE`].
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct PathStroke {
pub width: f32,
pub color: ColorMode,
}
impl PathStroke {
/// Same as [`PathStroke::default`].
pub const NONE: Self = Self {
width: 0.0,
color: ColorMode::TRANSPARENT,
};
#[inline]
pub fn new(width: impl Into<f32>, color: impl Into<Color32>) -> Self {
Self {
width: width.into(),
color: ColorMode::Solid(color.into()),
}
}
/// Create a new `PathStroke` with a UV function
///
/// The bounding box passed to the callback will have a margin of [`TessellationOptions::feathering_size_in_pixels`](`crate::tessellator::TessellationOptions::feathering_size_in_pixels`)
#[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)),
}
}
/// True if width is zero or color is solid and transparent
#[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),
}
}
}