use egui::util::id_type_map::SerializableAny;
use petgraph::{stable_graph::IndexType, EdgeType};
use std::fmt::Debug;
use crate::{DisplayEdge, DisplayNode, Graph};
const KEY_PREFIX: &str = "egui_graphs_layout";
fn get_id(id: Option<String>) -> egui::Id {
egui::Id::new(format!("{KEY_PREFIX}_{}", id.unwrap_or_default()))
}
pub trait LayoutState: SerializableAny + Default + Debug {
fn load(ui: &egui::Ui, id: Option<String>) -> Self {
ui.data_mut(|data| data.get_persisted::<Self>(get_id(id)).unwrap_or_default())
}
fn save(self, ui: &mut egui::Ui, id: Option<String>) {
ui.data_mut(|data| {
data.insert_persisted(get_id(id), self);
});
}
}
pub trait AnimatedState {
fn is_running(&self) -> bool;
fn set_running(&mut self, v: bool);
fn last_avg_displacement(&self) -> Option<f32> {
None
}
fn set_last_avg_displacement(&mut self, _v: Option<f32>) {}
fn step_count(&self) -> u64 {
0
}
fn set_step_count(&mut self, _v: u64) {}
}
pub trait Layout<S>: Default
where
S: LayoutState,
{
fn from_state(state: S) -> impl Layout<S>;
fn next<N, E, Ty, Ix, Dn, De>(&mut self, g: &mut Graph<N, E, Ty, Ix, Dn, De>, ui: &egui::Ui)
where
N: Clone,
E: Clone,
Ty: EdgeType,
Ix: IndexType,
Dn: DisplayNode<N, E, Ty, Ix>,
De: DisplayEdge<N, E, Ty, Ix, Dn>;
fn state(&self) -> S;
}