From cbdf748a3b49ba4f516cfba6def9c9c6bbf40c89 Mon Sep 17 00:00:00 2001 From: Nicolas Riebesel Date: Wed, 9 Aug 2023 16:47:46 +0200 Subject: [PATCH] egui: Plot Arrows add method to specify tip_size (#3138) Normally the tip_size will be dependent on the arrow length. This patch adds a new method to explicitly set the arrow tip size. --- crates/egui/src/widgets/plot/items/mod.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/egui/src/widgets/plot/items/mod.rs b/crates/egui/src/widgets/plot/items/mod.rs index e96d8b3b..c56de6bb 100644 --- a/crates/egui/src/widgets/plot/items/mod.rs +++ b/crates/egui/src/widgets/plot/items/mod.rs @@ -997,6 +997,7 @@ impl PlotItem for Points { pub struct Arrows { pub(super) origins: PlotPoints, pub(super) tips: PlotPoints, + pub(super) tip_length: Option, pub(super) color: Color32, pub(super) name: String, pub(super) highlight: bool, @@ -1007,6 +1008,7 @@ impl Arrows { Self { origins: origins.into(), tips: tips.into(), + tip_length: None, color: Color32::TRANSPARENT, name: Default::default(), highlight: false, @@ -1019,6 +1021,12 @@ impl Arrows { self } + /// Set the length of the arrow tips + pub fn tip_length(mut self, tip_length: f32) -> Self { + self.tip_length = Some(tip_length); + self + } + /// Set the arrows' color. pub fn color(mut self, color: impl Into) -> Self { self.color = color.into(); @@ -1044,6 +1052,7 @@ impl PlotItem for Arrows { let Self { origins, tips, + tip_length, color, highlight, .. @@ -1062,7 +1071,11 @@ impl PlotItem for Arrows { .for_each(|(origin, tip)| { let vector = tip - origin; let rot = Rot2::from_angle(std::f32::consts::TAU / 10.0); - let tip_length = vector.length() / 4.0; + let tip_length = if let Some(tip_length) = tip_length { + *tip_length + } else { + vector.length() / 4.0 + }; let tip = origin + vector; let dir = vector.normalized(); shapes.push(Shape::line_segment([origin, tip], stroke));