mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 06:40:06 -04:00
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>
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
use egui::{containers::*, *};
|
||||
use egui::{containers::*, epaint::PathStroke, *};
|
||||
|
||||
#[derive(Default)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
pub struct DancingStrings {}
|
||||
pub struct DancingStrings {
|
||||
colors: bool,
|
||||
}
|
||||
|
||||
impl super::Demo for DancingStrings {
|
||||
fn name(&self) -> &'static str {
|
||||
@@ -28,6 +30,9 @@ impl super::View for DancingStrings {
|
||||
Color32::from_black_alpha(240)
|
||||
};
|
||||
|
||||
ui.checkbox(&mut self.colors, "Colored")
|
||||
.on_hover_text("Demonstrates how a path can have varying color across its length.");
|
||||
|
||||
Frame::canvas(ui.style()).show(ui, |ui| {
|
||||
ui.ctx().request_repaint();
|
||||
let time = ui.input(|i| i.time);
|
||||
@@ -55,7 +60,24 @@ impl super::View for DancingStrings {
|
||||
.collect();
|
||||
|
||||
let thickness = 10.0 / mode as f32;
|
||||
shapes.push(epaint::Shape::line(points, Stroke::new(thickness, color)));
|
||||
shapes.push(epaint::Shape::line(
|
||||
points,
|
||||
if self.colors {
|
||||
PathStroke::new_uv(thickness, move |rect, p| {
|
||||
let t = remap(p.x, rect.x_range(), -1.0..=1.0).abs();
|
||||
let center_color = hex_color!("#5BCEFA");
|
||||
let outer_color = hex_color!("#F5A9B8");
|
||||
|
||||
Color32::from_rgb(
|
||||
lerp(center_color.r() as f32..=outer_color.r() as f32, t) as u8,
|
||||
lerp(center_color.g() as f32..=outer_color.g() as f32, t) as u8,
|
||||
lerp(center_color.b() as f32..=outer_color.b() as f32, t) as u8,
|
||||
)
|
||||
})
|
||||
} else {
|
||||
PathStroke::new(thickness, color)
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
ui.painter().extend(shapes);
|
||||
|
||||
Reference in New Issue
Block a user