Skip to content

Commit

Permalink
refactor SourceIndex retrevie
Browse files Browse the repository at this point in the history
  • Loading branch information
haricot committed Feb 14, 2023
1 parent ec339df commit 52f65e2
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 50 deletions.
44 changes: 33 additions & 11 deletions crates/egui/src/widgets/plot/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ pub(super) struct PlotConfig<'a> {
pub show_y: bool,
}

///
#[derive(PartialEq, Clone, Debug)]
pub struct SourceIndex {
pub group_name: String,
pub index_overide: usize,
pub index_overide: Vec<usize>,
pub sub_index: bool,
}

/// Trait shared by things that can be drawn in the plot.
Expand Down Expand Up @@ -150,12 +152,14 @@ impl HLine {
}

/// Group name and index are used to retrieve the source index of the hover event.
/// sub_index is used to retrieve the nested index if it part a line for example.
/// When you use it: each element you want to bind must be defined and
/// must have a set of unique index dependent on a group name.
pub fn group(mut self, name: impl ToString, index: usize) -> Self {
pub fn group(mut self, name: &impl ToString, index: Vec<usize>, sub_index: bool) -> Self {
self.group = Some(SourceIndex {
group_name: name.to_string(),
index_overide: index,
sub_index,
});
self
}
Expand Down Expand Up @@ -283,12 +287,14 @@ impl VLine {
}

/// Group name and index are used to retrieve the source index of the hover event.
/// sub_index is used to retrieve the nested index if it part a line for example.
/// When you use it: each element you want to bind must be defined and
/// must have a set of unique index dependent on a group name.
pub fn group(mut self, name: impl ToString, index: usize) -> Self {
pub fn group(mut self, name: &impl ToString, index: Vec<usize>, sub_index: bool) -> Self {
self.group = Some(SourceIndex {
group_name: name.to_string(),
index_overide: index,
sub_index,
});
self
}
Expand Down Expand Up @@ -417,12 +423,14 @@ impl Line {
}

/// Group name and index are used to retrieve the source index of the hover event.
/// sub_index is used to retrieve the nested index if it part a line for example.
/// When you use it: each element you want to bind must be defined and
/// must have a set of unique index dependent on a group name.
pub fn group(mut self, name: impl ToString, index: usize) -> Self {
pub fn group(mut self, name: &impl ToString, index: Vec<usize>, sub_index: bool) -> Self {
self.group = Some(SourceIndex {
group_name: name.to_string(),
index_overide: index,
sub_index,
});
self
}
Expand Down Expand Up @@ -602,12 +610,14 @@ impl Polygon {
}

/// Group name and index are used to retrieve the source index of the hover event.
/// sub_index is used to retrieve the nested index if it part a line for example.
/// When you use it: each element you want to bind must be defined and
/// must have a set of unique index dependent on a group name.
pub fn group(mut self, name: impl ToString, index: usize) -> Self {
pub fn group(mut self, name: &impl ToString, index: Vec<usize>, sub_index: bool) -> Self {
self.group = Some(SourceIndex {
group_name: name.to_string(),
index_overide: index,
sub_index,
});
self
}
Expand Down Expand Up @@ -749,12 +759,14 @@ impl Text {
}

/// Group name and index are used to retrieve the source index of the hover event.
/// sub_index is used to retrieve the nested index if it part a line for example.
/// When you use it: each element you want to bind must be defined and
/// must have a set of unique index dependent on a group name.
pub fn group(mut self, name: impl ToString, index: usize) -> Self {
pub fn group(mut self, name: &impl ToString, index: Vec<usize>, sub_index: bool) -> Self {
self.group = Some(SourceIndex {
group_name: name.to_string(),
index_overide: index,
sub_index,
});
self
}
Expand Down Expand Up @@ -894,12 +906,14 @@ impl Points {
}

/// Group name and index are used to retrieve the source index of the hover event.
/// sub_index is used to retrieve the nested index if it part a line for example.
/// When you use it: each element you want to bind must be defined and
/// must have a set of unique index dependent on a group name.
pub fn group(mut self, name: impl ToString, index: usize) -> Self {
pub fn group(mut self, name: &impl ToString, index: Vec<usize>, sub_index: bool) -> Self {
self.group = Some(SourceIndex {
group_name: name.to_string(),
index_overide: index,
sub_index,
});
self
}
Expand Down Expand Up @@ -1132,12 +1146,14 @@ impl Arrows {
}

/// Group name and index are used to retrieve the source index of the hover event.
/// sub_index is used to retrieve the nested index if it part a line for example.
/// When you use it: each element you want to bind must be defined and
/// must have a set of unique index dependent on a group name.
pub fn group(mut self, name: impl ToString, index: usize) -> Self {
pub fn group(mut self, name: &impl ToString, index: Vec<usize>, sub_index: bool) -> Self {
self.group = Some(SourceIndex {
group_name: name.to_string(),
index_overide: index,
sub_index,
});
self
}
Expand Down Expand Up @@ -1276,12 +1292,14 @@ impl PlotImage {
}

/// Group name and index are used to retrieve the source index of the hover event.
/// sub_index is used to retrieve the nested index if it part a line for example.
/// When you use it: each element you want to bind must be defined and
/// must have a set of unique index dependent on a group name.
pub fn group(mut self, name: impl ToString, index: usize) -> Self {
pub fn group(mut self, name: &impl ToString, index: Vec<usize>, sub_index: bool) -> Self {
self.group = Some(SourceIndex {
group_name: name.to_string(),
index_overide: index,
sub_index,
});
self
}
Expand Down Expand Up @@ -1483,12 +1501,14 @@ impl BarChart {
}

/// Group name and index are used to retrieve the source index of the hover event.
/// sub_index is used to retrieve the nested index if it part a line for example.
/// When you use it: each element you want to bind must be defined and
/// must have a set of unique index dependent on a group name.
pub fn group(mut self, name: impl ToString, index: usize) -> Self {
pub fn group(mut self, name: &impl ToString, index: Vec<usize>, sub_index: bool) -> Self {
self.group = Some(SourceIndex {
group_name: name.to_string(),
index_overide: index,
sub_index,
});
self
}
Expand Down Expand Up @@ -1664,12 +1684,14 @@ impl BoxPlot {
}

/// Group name and index are used to retrieve the source index of the hover event.
/// sub_index is used to retrieve the nested index if it part a line for example.
/// When you use it: each element you want to bind must be defined and
/// must have a set of unique index dependent on a group name.
pub fn group(mut self, name: impl ToString, index: usize) -> Self {
pub fn group(mut self, name: &impl ToString, index: Vec<usize>, sub_index: bool) -> Self {
self.group = Some(SourceIndex {
group_name: name.to_string(),
index_overide: index,
sub_index,
});
self
}
Expand Down
41 changes: 23 additions & 18 deletions crates/egui/src/widgets/plot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,6 @@ impl From<bool> for AxisBools {
}
}

#[derive(Clone)]
pub struct HoverIndexes {
pub retained: usize,
pub sub: usize,
pub group: Option<SourceIndex>,
}

/// Information about the plot that has to persist between frames.
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Clone)]
Expand All @@ -113,7 +106,7 @@ struct PlotMemory {
last_click_pos_for_zoom: Option<Pos2>,
// skip serde
#[cfg_attr(feature = "serde", serde(skip))]
hover_indexes: Option<HoverIndexes>,
hover_indexes: Option<SourceIndex>,
}

impl PlotMemory {
Expand Down Expand Up @@ -1058,7 +1051,7 @@ pub struct PlotUi {
last_screen_transform: ScreenTransform,
response: Response,
ctx: Context,
hover_indexes: Option<HoverIndexes>,
hover_indexes: Option<SourceIndex>,
}

impl PlotUi {
Expand Down Expand Up @@ -1097,7 +1090,7 @@ impl PlotUi {
}

///
pub fn hover_indexes(&self) -> Option<HoverIndexes> {
pub fn hover_indexes(&self) -> Option<SourceIndex> {
self.hover_indexes.clone()
}

Expand Down Expand Up @@ -1342,7 +1335,7 @@ struct PreparedPlot {
}

impl PreparedPlot {
fn ui(self, ui: &mut Ui, response: &Response) -> (Vec<Cursor>, Option<HoverIndexes>) {
fn ui(self, ui: &mut Ui, response: &Response) -> (Vec<Cursor>, Option<SourceIndex>) {
let mut axes_shapes = Vec::new();

for d in 0..2 {
Expand Down Expand Up @@ -1583,7 +1576,7 @@ impl PreparedPlot {
ui: &Ui,
pointer: Pos2,
shapes: &mut Vec<Shape>,
) -> (Vec<Cursor>, Option<HoverIndexes>) {
) -> (Vec<Cursor>, Option<SourceIndex>) {
let Self {
transform,
show_x,
Expand Down Expand Up @@ -1611,7 +1604,7 @@ impl PreparedPlot {
.filter(|(_, elem)| elem.dist_sq <= interact_radius_sq);

let mut cursors = Vec::new();
let mut index_interact_radius_sq = None;
let mut hover_indexes = None;

let plot = items::PlotConfig {
ui,
Expand All @@ -1621,11 +1614,23 @@ impl PreparedPlot {
};

if let Some(((index, item), elem)) = closest {
index_interact_radius_sq = Some(HoverIndexes {
retained: index,
sub: elem.index,
group: item.group(),
let (group_name, indexes, sub_index) = if item.group().is_some() {
let group = item.group().unwrap();
let mut index_overide = group.index_overide.clone();
if group.sub_index {
index_overide.push(elem.index);
}
(group.group_name, index_overide, group.sub_index)
} else {
(String::new(), vec![index, elem.index], true)
};

hover_indexes = Some(SourceIndex {
group_name,
index_overide: indexes,
sub_index,
});

item.on_hover(elem, shapes, &mut cursors, &plot, label_formatter);
} else {
let value = transform.value_from_position(pointer);
Expand All @@ -1640,7 +1645,7 @@ impl PreparedPlot {
);
}

(cursors, index_interact_radius_sq)
(cursors, hover_indexes)
}
}

Expand Down
Loading

0 comments on commit 52f65e2

Please sign in to comment.