Skip to content

Commit

Permalink
Refactor cursor memory to deal with removal of plots
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Jul 29, 2022
1 parent 511cc46 commit 2ac8f80
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
18 changes: 10 additions & 8 deletions egui/src/widgets/plot/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use epaint::Mesh;

use crate::*;

use super::{LabelFormatter, PlotBounds, ScreenTransform};
use super::{Cursor, LabelFormatter, PlotBounds, ScreenTransform};
use rect_elem::*;
use values::{ClosestElem, PlotGeometry};

Expand All @@ -28,7 +28,7 @@ pub(super) struct PlotConfig<'a> {
pub transform: &'a ScreenTransform,
pub show_x: bool,
pub show_y: bool,
pub cursors: &'a RefCell<Vec<(Orientation, f64)>>,
pub cursors: &'a RefCell<Vec<Cursor>>,
}

/// Trait shared by things that can be drawn in the plot.
Expand Down Expand Up @@ -1663,15 +1663,17 @@ pub(super) fn rulers_at_value(
let line_color = rulers_color(plot.ui);
if plot.show_x {
shapes.push(vertical_line(pointer, plot.transform, line_color));
plot.cursors
.borrow_mut()
.push((Orientation::Vertical, value.x));
plot.cursors.borrow_mut().push(Cursor {
orientation: Orientation::Vertical,
point: value.x,
});
}
if plot.show_y {
shapes.push(horizontal_line(pointer, plot.transform, line_color));
plot.cursors
.borrow_mut()
.push((Orientation::Horizontal, value.y));
plot.cursors.borrow_mut().push(Cursor {
orientation: Orientation::Horizontal,
point: value.y,
});
}

let mut prefix = String::new();
Expand Down
56 changes: 32 additions & 24 deletions egui/src/widgets/plot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::{
cell::{Cell, RefCell},
collections::HashMap,
ops::RangeInclusive,
rc::Rc,
};
Expand Down Expand Up @@ -122,6 +121,12 @@ impl PlotMemory {

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

#[derive(Copy, Clone, PartialEq)]
struct Cursor {
orientation: Orientation,
point: f64,
}

/// Defines how multiple plots share the same range for one or both of their axes. Can be added while building
/// a plot with [`Plot::link_axis`]. Contains an internal state, meaning that this object should be stored by
/// the user between frames.
Expand All @@ -131,7 +136,7 @@ pub struct LinkedAxisGroup {
pub(crate) link_y: bool,
pub(crate) link_cursor: bool,
pub(crate) bounds: Rc<Cell<Option<PlotBounds>>>,
pub(crate) cursors: Rc<RefCell<HashMap<Id, Vec<(Orientation, f64)>>>>,
cursors: Rc<RefCell<Vec<(Id, Vec<Cursor>)>>>,
}

impl LinkedAxisGroup {
Expand All @@ -141,7 +146,7 @@ impl LinkedAxisGroup {
link_y,
link_cursor: false,
bounds: Rc::new(Cell::new(None)),
cursors: Rc::new(RefCell::new(HashMap::new())),
cursors: Rc::new(RefCell::new(Vec::new())),
}
}

Expand Down Expand Up @@ -677,12 +682,24 @@ impl Plot {
// --- Bound computation ---
let mut bounds = *last_screen_transform.bounds();

let mut cursors = HashMap::new();
let mut draw_cursors: Vec<Cursor> = Vec::new();

// Transfer the bounds from a link group.
if let Some(axes) = linked_axes.as_ref() {
if axes.link_cursor {
cursors = axes.cursors.borrow().clone();
let mut cursors = axes.cursors.borrow_mut();

// Look for our previous entry
let index = cursors
.iter()
.enumerate()
.find(|(_, e)| e.0 == plot_id)
.map(|(i, _)| i);

// Remove our previous entry and all older entries as these are no longer displayed.
index.map(|index| cursors.drain(0..=index));

draw_cursors = cursors.iter().flat_map(|e| e.1.iter().copied()).collect();
}
if let Some(linked_bounds) = axes.get() {
if axes.link_x {
Expand Down Expand Up @@ -842,9 +859,9 @@ impl Plot {
grid_spacers,
draw_cursor_x: linked_axes.as_ref().map_or(false, |axes| axes.link_x),
draw_cursor_y: linked_axes.as_ref().map_or(false, |axes| axes.link_y),
draw_cursors: cursors,
draw_cursors,
};
let cursors = prepared.ui(ui, &response);
let plot_cursors = prepared.ui(ui, &response);

if let Some(boxed_zoom_rect) = boxed_zoom_rect {
ui.painter().with_clip_rect(rect).add(boxed_zoom_rect.0);
Expand All @@ -858,12 +875,7 @@ impl Plot {
}

if let Some(group) = linked_axes.as_ref() {
let cursors_map = &mut *group.cursors.borrow_mut();
if response.hovered() {
cursors_map.insert(id_source, cursors);
} else {
cursors_map.remove(&id_source);
}
group.cursors.borrow_mut().push((plot_id, plot_cursors));
group.set(*transform.bounds());
}

Expand Down Expand Up @@ -1151,11 +1163,11 @@ struct PreparedPlot {
grid_spacers: [GridSpacer; 2],
draw_cursor_x: bool,
draw_cursor_y: bool,
draw_cursors: HashMap<Id, Vec<(Orientation, f64)>>,
draw_cursors: Vec<Cursor>,
}

impl PreparedPlot {
fn ui(self, ui: &mut Ui, response: &Response) -> Vec<(Orientation, f64)> {
fn ui(self, ui: &mut Ui, response: &Response) -> Vec<Cursor> {
let mut shapes = Vec::new();

for d in 0..2 {
Expand All @@ -1177,16 +1189,12 @@ impl PreparedPlot {
} else {
// Draw cursors from other plots
let line_color = rulers_color(ui);
for &(orientation, value) in self
.draw_cursors
.values()
.flat_map(|cursors| cursors.iter())
{
match orientation {
for cursor in &self.draw_cursors {
match cursor.orientation {
Orientation::Horizontal => {
if self.draw_cursor_y {
shapes.push(horizontal_line(
transform.position_from_point(&PlotPoint::new(0.0, value)),
transform.position_from_point(&PlotPoint::new(0.0, cursor.point)),
&self.transform,
line_color,
));
Expand All @@ -1195,7 +1203,7 @@ impl PreparedPlot {
Orientation::Vertical => {
if self.draw_cursor_x {
shapes.push(vertical_line(
transform.position_from_point(&PlotPoint::new(value, 0.0)),
transform.position_from_point(&PlotPoint::new(cursor.point, 0.0)),
&self.transform,
line_color,
));
Expand Down Expand Up @@ -1320,7 +1328,7 @@ impl PreparedPlot {
}
}

fn hover(&self, ui: &Ui, pointer: Pos2, shapes: &mut Vec<Shape>) -> Vec<(Orientation, f64)> {
fn hover(&self, ui: &Ui, pointer: Pos2, shapes: &mut Vec<Shape>) -> Vec<Cursor> {
let Self {
transform,
show_x,
Expand Down

0 comments on commit 2ac8f80

Please sign in to comment.