Skip to content

Commit 0609744

Browse files
committed
add symmetric sliders trail filling
1 parent 788f540 commit 0609744

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

crates/egui/src/widgets/slider.rs

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ pub struct Slider<'a> {
9191

9292
rail_color: Option<Color32>,
9393
thickness: Option<f32>,
94+
95+
symmetric_slider: bool,
9496
}
9597

9698
impl<'a> Slider<'a> {
@@ -111,6 +113,26 @@ impl<'a> Slider<'a> {
111113
}
112114
}
113115

116+
/// Creates a new horizontal slider with symmetric values.
117+
pub fn new_symmetric<Num: emath::Numeric>(value: &'a mut Num, max_abs_value: Num) -> Self {
118+
let abs_value = max_abs_value.to_f64().abs();
119+
let range_f64 = -abs_value..=abs_value;
120+
let mut slf = Self::from_get_set(range_f64, move |v: Option<f64>| {
121+
if let Some(v) = v {
122+
*value = Num::from_f64(v);
123+
}
124+
value.to_f64()
125+
});
126+
127+
slf.symmetric_slider = true;
128+
129+
if Num::INTEGRAL {
130+
slf.integer()
131+
} else {
132+
slf
133+
}
134+
}
135+
114136
pub fn from_get_set(
115137
range: RangeInclusive<f64>,
116138
get_set_value: impl 'a + FnMut(Option<f64>) -> f64,
@@ -141,6 +163,8 @@ impl<'a> Slider<'a> {
141163

142164
rail_color: None,
143165
thickness: None,
166+
167+
symmetric_slider: false,
144168
}
145169
}
146170

@@ -725,10 +749,36 @@ impl<'a> Slider<'a> {
725749
let mut trailing_rail_rect = rail_rect;
726750

727751
// The trailing rect has to be drawn differently depending on the orientation.
728-
match self.orientation {
729-
SliderOrientation::Vertical => trailing_rail_rect.min.y = center.y,
730-
SliderOrientation::Horizontal => trailing_rail_rect.max.x = center.x,
731-
};
752+
if self.symmetric_slider {
753+
let position_at_0 = self.position_from_value(0.0, position_range);
754+
let center_at_0 = self.marker_center(position_at_0, &rail_rect);
755+
756+
match self.orientation {
757+
SliderOrientation::Vertical => {
758+
if center.y < center_at_0.y {
759+
trailing_rail_rect.min.y = center.y;
760+
trailing_rail_rect.max.y = center_at_0.y;
761+
} else {
762+
trailing_rail_rect.min.y = center_at_0.y;
763+
trailing_rail_rect.max.y = center.y;
764+
}
765+
}
766+
SliderOrientation::Horizontal => {
767+
if center.x < center_at_0.x {
768+
trailing_rail_rect.min.x = center.x;
769+
trailing_rail_rect.max.x = center_at_0.x;
770+
} else {
771+
trailing_rail_rect.min.x = center_at_0.x;
772+
trailing_rail_rect.max.x = center.x;
773+
}
774+
}
775+
};
776+
} else {
777+
match self.orientation {
778+
SliderOrientation::Vertical => trailing_rail_rect.min.y = center.y,
779+
SliderOrientation::Horizontal => trailing_rail_rect.max.x = center.x,
780+
};
781+
}
732782

733783
ui.painter().rect_filled(
734784
trailing_rail_rect,

crates/egui_demo_lib/src/demo/widget_gallery.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct WidgetGallery {
1515
opacity: f32,
1616
radio: Enum,
1717
scalar: f32,
18+
symmetric_value: f32,
1819
string: String,
1920
color: egui::Color32,
2021
animate_progress_bar: bool,
@@ -33,6 +34,7 @@ impl Default for WidgetGallery {
3334
boolean: false,
3435
radio: Enum::First,
3536
scalar: 42.0,
37+
symmetric_value: 0.0,
3638
string: Default::default(),
3739
color: egui::Color32::LIGHT_BLUE.linear_multiply(0.5),
3840
animate_progress_bar: false,
@@ -112,6 +114,7 @@ impl WidgetGallery {
112114
boolean,
113115
radio,
114116
scalar,
117+
symmetric_value,
115118
string,
116119
color,
117120
animate_progress_bar,
@@ -181,7 +184,19 @@ impl WidgetGallery {
181184
ui.end_row();
182185

183186
ui.add(doc_link_label("Slider", "Slider"));
184-
ui.add(egui::Slider::new(scalar, 0.0..=360.0).suffix("°"));
187+
ui.add(
188+
egui::Slider::new(scalar, 0.0..=360.0)
189+
.suffix("°")
190+
.trailing_fill(true),
191+
);
192+
ui.end_row();
193+
194+
ui.add(doc_link_label("Symmetric Slider", "Slider"));
195+
ui.add(
196+
egui::Slider::new_symmetric(symmetric_value, 90.0)
197+
.suffix("°")
198+
.trailing_fill(true),
199+
);
185200
ui.end_row();
186201

187202
ui.add(doc_link_label("DragValue", "DragValue"));

0 commit comments

Comments
 (0)