1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-27 07:03:14 -04:00
Files
egui/crates/egui_plot/src/memory.rs
Emil Ernerfeldt 2f9a4ca6e8 Make egui_plot::PlotMemory public (#3871)
This allows users to e.g. read/write the plot bounds/transform before
and after showing a `Plot`.
2024-01-23 09:47:47 +01:00

73 lines
1.8 KiB
Rust

use egui::{ahash, Context, Id, Pos2, Vec2b};
use crate::{PlotBounds, PlotTransform};
/// Information about the plot that has to persist between frames.
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Clone)]
pub struct PlotMemory {
/// Indicates if the plot uses automatic bounds.
///
/// This is set to `false` whenever the user modifies
/// the bounds, for example by moving or zooming.
pub auto_bounds: Vec2b,
/// Which item is hovered?
pub hovered_item: Option<String>,
/// Which items _not_ to show?
pub hidden_items: ahash::HashSet<String>,
/// The transform from last frame.
pub(crate) transform: PlotTransform,
/// Allows to remember the first click position when performing a boxed zoom
pub(crate) last_click_pos_for_zoom: Option<Pos2>,
}
impl PlotMemory {
#[inline]
pub fn transform(&self) -> PlotTransform {
self.transform
}
#[inline]
pub fn set_transform(&mut self, t: PlotTransform) {
self.transform = t;
}
/// Plot-space bounds.
#[inline]
pub fn bounds(&self) -> &PlotBounds {
self.transform.bounds()
}
/// Plot-space bounds.
#[inline]
pub fn set_bounds(&mut self, bounds: PlotBounds) {
self.transform.set_bounds(bounds);
}
}
#[cfg(feature = "serde")]
impl PlotMemory {
pub fn load(ctx: &Context, id: Id) -> Option<Self> {
ctx.data_mut(|d| d.get_persisted(id))
}
pub fn store(self, ctx: &Context, id: Id) {
ctx.data_mut(|d| d.insert_persisted(id, self));
}
}
#[cfg(not(feature = "serde"))]
impl PlotMemory {
pub fn load(ctx: &Context, id: Id) -> Option<Self> {
ctx.data_mut(|d| d.get_temp(id))
}
pub fn store(self, ctx: &Context, id: Id) {
ctx.data_mut(|d| d.insert_temp(id, self));
}
}