Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to define colors at UV coordinates along a path #4353

Merged
merged 29 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
912ced2
added ability to add custom gradients to paths
murl-digital Apr 12, 2024
18c561d
add docs
murl-digital Apr 12, 2024
4279851
re-add send sync assertion
murl-digital Apr 12, 2024
fe92ba8
better documentation
murl-digital Apr 13, 2024
c433de3
fix typo in docs
murl-digital Apr 13, 2024
e03fe52
added rect bounds for uv mapping
murl-digital Apr 13, 2024
c9efc24
added benchmarks for colors
murl-digital Apr 16, 2024
6664482
simplify ColorMode and use a better method for calculating bounding b…
murl-digital Apr 16, 2024
5451e30
fix feathering oversight
murl-digital Apr 16, 2024
41f8192
refactor ColorMode out into its own file
murl-digital Apr 16, 2024
2d8ab0c
add transparent helper
murl-digital Apr 16, 2024
a8e0e57
add proper stroke color demo
murl-digital Apr 16, 2024
f8e6b65
actually use the ColorMode::TRANSPARENT
murl-digital Apr 16, 2024
b6c4ce2
expand the bounding box to include the thickness of the path
murl-digital Apr 16, 2024
46cd485
add a few more benchmarks
murl-digital Apr 16, 2024
713f566
add a unit test to check if all points are within the given bounding …
murl-digital Apr 17, 2024
4884aad
have UV color modes be affected by tints
murl-digital Apr 20, 2024
5f40909
Merge branch 'emilk:master' into feature/uv-line
murl-digital Apr 21, 2024
f71b011
remove dependencies for dancing strings demo (doesn't compile for me …
murl-digital Apr 21, 2024
1e85d86
roll dancing strings examples into one
murl-digital Apr 21, 2024
eb41420
Merge branch 'emilk:master' into feature/uv-line
murl-digital Apr 21, 2024
cdc6588
from_hex! can't be used in statics
murl-digital Apr 21, 2024
38bc770
remove margin, it seems that points can exit the rect by at most 0.55…
murl-digital Apr 21, 2024
b1f7520
remove unesecarry box for closure
murl-digital Apr 21, 2024
2283268
add benchmarks for uv mode lines
murl-digital Apr 21, 2024
5f3a712
why are you still here, mx. box?
murl-digital Apr 22, 2024
1a95c48
fix docs
murl-digital Apr 22, 2024
52a6459
remove mul_color
murl-digital Apr 22, 2024
a92ed91
Simplify the dancing strings example
emilk Apr 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions crates/egui/src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
};
use epaint::{
text::{Fonts, Galley, LayoutJob},
CircleShape, ClippedShape, RectShape, Rounding, Shape, Stroke,
CircleShape, ClippedShape, PathStroke, RectShape, Rounding, Shape, Stroke,
};

/// Helper to paint shapes and text to a specific region on a specific layer.
Expand Down Expand Up @@ -280,21 +280,21 @@ impl Painter {
/// # Paint different primitives
impl Painter {
/// Paints a line from the first point to the second.
pub fn line_segment(&self, points: [Pos2; 2], stroke: impl Into<Stroke>) -> ShapeIdx {
pub fn line_segment(&self, points: [Pos2; 2], stroke: impl Into<PathStroke>) -> ShapeIdx {
self.add(Shape::LineSegment {
points,
stroke: stroke.into(),
})
}

/// Paints a horizontal line.
pub fn hline(&self, x: impl Into<Rangef>, y: f32, stroke: impl Into<Stroke>) -> ShapeIdx {
self.add(Shape::hline(x, y, stroke))
pub fn hline(&self, x: impl Into<Rangef>, y: f32, stroke: impl Into<PathStroke>) -> ShapeIdx {
self.add(Shape::hline(x, y, stroke.into()))
}

/// Paints a vertical line.
pub fn vline(&self, x: f32, y: impl Into<Rangef>, stroke: impl Into<Stroke>) -> ShapeIdx {
self.add(Shape::vline(x, y, stroke))
pub fn vline(&self, x: f32, y: impl Into<Rangef>, stroke: impl Into<PathStroke>) -> ShapeIdx {
self.add(Shape::vline(x, y, stroke.into()))
}

pub fn circle(
Expand Down
28 changes: 14 additions & 14 deletions crates/epaint/src/bezier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

use std::ops::Range;

use crate::{shape::Shape, Color32, PathShape, Stroke};
use crate::{shape::Shape, Color32, PathShape, PathStroke};
use emath::*;

// ----------------------------------------------------------------------------

/// A cubic [Bézier Curve](https://en.wikipedia.org/wiki/B%C3%A9zier_curve).
///
/// See also [`QuadraticBezierShape`].
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct CubicBezierShape {
/// The first point is the starting point and the last one is the ending point of the curve.
Expand All @@ -20,7 +20,7 @@ pub struct CubicBezierShape {
pub closed: bool,

pub fill: Color32,
pub stroke: Stroke,
pub stroke: PathStroke,
}

impl CubicBezierShape {
Expand All @@ -32,7 +32,7 @@ impl CubicBezierShape {
points: [Pos2; 4],
closed: bool,
fill: Color32,
stroke: impl Into<Stroke>,
stroke: impl Into<PathStroke>,
) -> Self {
Self {
points,
Expand All @@ -52,7 +52,7 @@ impl CubicBezierShape {
points,
closed: self.closed,
fill: self.fill,
stroke: self.stroke,
stroke: self.stroke.clone(),
}
}

Expand All @@ -69,7 +69,7 @@ impl CubicBezierShape {
points,
closed: self.closed,
fill: self.fill,
stroke: self.stroke,
stroke: self.stroke.clone(),
};
pathshapes.push(pathshape);
}
Expand Down Expand Up @@ -156,7 +156,7 @@ impl CubicBezierShape {
points: [d_from, d_ctrl, d_to],
closed: self.closed,
fill: self.fill,
stroke: self.stroke,
stroke: self.stroke.clone(),
};
let delta_t = t_range.end - t_range.start;
let q_start = q.sample(t_range.start);
Expand All @@ -168,7 +168,7 @@ impl CubicBezierShape {
points: [from, ctrl1, ctrl2, to],
closed: self.closed,
fill: self.fill,
stroke: self.stroke,
stroke: self.stroke.clone(),
}
}

Expand Down Expand Up @@ -375,7 +375,7 @@ impl From<CubicBezierShape> for Shape {
/// A quadratic [Bézier Curve](https://en.wikipedia.org/wiki/B%C3%A9zier_curve).
///
/// See also [`CubicBezierShape`].
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct QuadraticBezierShape {
/// The first point is the starting point and the last one is the ending point of the curve.
Expand All @@ -384,7 +384,7 @@ pub struct QuadraticBezierShape {
pub closed: bool,

pub fill: Color32,
pub stroke: Stroke,
pub stroke: PathStroke,
}

impl QuadraticBezierShape {
Expand All @@ -397,7 +397,7 @@ impl QuadraticBezierShape {
points: [Pos2; 3],
closed: bool,
fill: Color32,
stroke: impl Into<Stroke>,
stroke: impl Into<PathStroke>,
) -> Self {
Self {
points,
Expand All @@ -417,7 +417,7 @@ impl QuadraticBezierShape {
points,
closed: self.closed,
fill: self.fill,
stroke: self.stroke,
stroke: self.stroke.clone(),
}
}

Expand All @@ -429,7 +429,7 @@ impl QuadraticBezierShape {
points,
closed: self.closed,
fill: self.fill,
stroke: self.stroke,
stroke: self.stroke.clone(),
}
}

Expand Down Expand Up @@ -688,7 +688,7 @@ fn single_curve_approximation(curve: &CubicBezierShape) -> QuadraticBezierShape
points: [curve.points[0], c, curve.points[3]],
closed: curve.closed,
fill: curve.fill,
stroke: curve.stroke,
stroke: curve.stroke.clone(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/epaint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub use self::{
Rounding, Shape, TextShape,
},
stats::PaintStats,
stroke::Stroke,
stroke::{ColorMode, PathStroke, Stroke},
tessellator::{TessellationOptions, Tessellator},
text::{FontFamily, FontId, Fonts, Galley},
texture_atlas::TextureAtlas,
Expand Down
26 changes: 15 additions & 11 deletions crates/epaint/src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::{any::Any, sync::Arc};

use crate::{
stroke::PathStroke,
text::{FontId, Fonts, Galley},
Color32, Mesh, Stroke, TextureId,
};
Expand Down Expand Up @@ -34,7 +35,10 @@ pub enum Shape {
Ellipse(EllipseShape),

/// A line between two points.
LineSegment { points: [Pos2; 2], stroke: Stroke },
LineSegment {
points: [Pos2; 2],
stroke: PathStroke,
},

/// A series of lines between points.
/// The path can have a stroke and/or fill (if closed).
Expand Down Expand Up @@ -88,15 +92,15 @@ impl Shape {
/// A line between two points.
/// More efficient than calling [`Self::line`].
#[inline]
pub fn line_segment(points: [Pos2; 2], stroke: impl Into<Stroke>) -> Self {
pub fn line_segment(points: [Pos2; 2], stroke: impl Into<PathStroke>) -> Self {
Self::LineSegment {
points,
stroke: stroke.into(),
}
}

/// A horizontal line.
pub fn hline(x: impl Into<Rangef>, y: f32, stroke: impl Into<Stroke>) -> Self {
pub fn hline(x: impl Into<Rangef>, y: f32, stroke: impl Into<PathStroke>) -> Self {
let x = x.into();
Self::LineSegment {
points: [pos2(x.min, y), pos2(x.max, y)],
Expand All @@ -105,7 +109,7 @@ impl Shape {
}

/// A vertical line.
pub fn vline(x: f32, y: impl Into<Rangef>, stroke: impl Into<Stroke>) -> Self {
pub fn vline(x: f32, y: impl Into<Rangef>, stroke: impl Into<PathStroke>) -> Self {
let y = y.into();
Self::LineSegment {
points: [pos2(x, y.min), pos2(x, y.max)],
Expand All @@ -117,13 +121,13 @@ impl Shape {
///
/// Use [`Self::line_segment`] instead if your line only connects two points.
#[inline]
pub fn line(points: Vec<Pos2>, stroke: impl Into<Stroke>) -> Self {
pub fn line(points: Vec<Pos2>, stroke: impl Into<PathStroke>) -> Self {
Self::Path(PathShape::line(points, stroke))
}

/// A line that closes back to the start point again.
#[inline]
pub fn closed_line(points: Vec<Pos2>, stroke: impl Into<Stroke>) -> Self {
pub fn closed_line(points: Vec<Pos2>, stroke: impl Into<PathStroke>) -> Self {
Self::Path(PathShape::closed_line(points, stroke))
}

Expand Down Expand Up @@ -224,7 +228,7 @@ impl Shape {
pub fn convex_polygon(
points: Vec<Pos2>,
fill: impl Into<Color32>,
stroke: impl Into<Stroke>,
stroke: impl Into<PathStroke>,
) -> Self {
Self::Path(PathShape::convex_polygon(points, fill, stroke))
}
Expand Down Expand Up @@ -586,7 +590,7 @@ pub struct PathShape {
pub fill: Color32,

/// Color and thickness of the line.
pub stroke: Stroke,
pub stroke: PathStroke,
// TODO(emilk): Add texture support either by supplying uv for each point,
// or by some transform from points to uv (e.g. a callback or a linear transform matrix).
}
Expand All @@ -596,7 +600,7 @@ impl PathShape {
///
/// Use [`Shape::line_segment`] instead if your line only connects two points.
#[inline]
pub fn line(points: Vec<Pos2>, stroke: impl Into<Stroke>) -> Self {
pub fn line(points: Vec<Pos2>, stroke: impl Into<PathStroke>) -> Self {
Self {
points,
closed: false,
Expand All @@ -607,7 +611,7 @@ impl PathShape {

/// A line that closes back to the start point again.
#[inline]
pub fn closed_line(points: Vec<Pos2>, stroke: impl Into<Stroke>) -> Self {
pub fn closed_line(points: Vec<Pos2>, stroke: impl Into<PathStroke>) -> Self {
Self {
points,
closed: true,
Expand All @@ -623,7 +627,7 @@ impl PathShape {
pub fn convex_polygon(
points: Vec<Pos2>,
fill: impl Into<Color32>,
stroke: impl Into<Stroke>,
stroke: impl Into<PathStroke>,
) -> Self {
Self {
points,
Expand Down
50 changes: 30 additions & 20 deletions crates/epaint/src/shape_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,36 @@ pub fn adjust_colors(shape: &mut Shape, adjust_color: &impl Fn(&mut Color32)) {
adjust_colors(shape, adjust_color);
}
}
Shape::LineSegment { stroke, points: _ } => {
adjust_color(&mut stroke.color);
Shape::LineSegment { stroke, points: _ } => match stroke.color {
stroke::ColorMode::Solid(mut col) => adjust_color(&mut col),
stroke::ColorMode::UV(_) => {}
stroke::ColorMode::UVBounds(_, _) => {}
},

Shape::Path(PathShape {
points: _,
closed: _,
fill,
stroke,
})
| Shape::QuadraticBezier(QuadraticBezierShape {
points: _,
closed: _,
fill,
stroke,
})
| Shape::CubicBezier(CubicBezierShape {
points: _,
closed: _,
fill,
stroke,
}) => {
adjust_color(fill);
match stroke.color {
stroke::ColorMode::Solid(mut col) => adjust_color(&mut col),
stroke::ColorMode::UV(_) => {}
stroke::ColorMode::UVBounds(_, _) => {}
}
}

Shape::Circle(CircleShape {
Expand All @@ -26,12 +54,6 @@ pub fn adjust_colors(shape: &mut Shape, adjust_color: &impl Fn(&mut Color32)) {
fill,
stroke,
})
| Shape::Path(PathShape {
points: _,
closed: _,
fill,
stroke,
})
| Shape::Rect(RectShape {
rect: _,
rounding: _,
Expand All @@ -40,18 +62,6 @@ pub fn adjust_colors(shape: &mut Shape, adjust_color: &impl Fn(&mut Color32)) {
blur_width: _,
fill_texture_id: _,
uv: _,
})
| Shape::QuadraticBezier(QuadraticBezierShape {
points: _,
closed: _,
fill,
stroke,
})
| Shape::CubicBezier(CubicBezierShape {
points: _,
closed: _,
fill,
stroke,
}) => {
adjust_color(fill);
adjust_color(&mut stroke.color);
Expand Down
Loading