mirror of
https://github.com/eliasstepanik/egui_node_graph.git
synced 2026-01-16 16:28:28 +00:00
add feature persistence
This commit is contained in:
parent
eeecd63787
commit
774eb5247c
@ -12,9 +12,11 @@ crate-type = ["cdylib", "rlib"]
|
|||||||
eframe = "0.18.0"
|
eframe = "0.18.0"
|
||||||
egui_node_graph = { path = "../egui_node_graph" }
|
egui_node_graph = { path = "../egui_node_graph" }
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
|
serde = { version = "1.0", optional = true }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default =[]
|
||||||
|
persistence = ["serde", "egui_node_graph/persistence", "eframe/persistence"]
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
opt-level = 2 # fast and small wasm
|
opt-level = 2 # fast and small wasm
|
||||||
|
|||||||
@ -8,6 +8,7 @@ use egui_node_graph::*;
|
|||||||
/// The NodeData holds a custom data struct inside each node. It's useful to
|
/// The NodeData holds a custom data struct inside each node. It's useful to
|
||||||
/// store additional information that doesn't live in parameters. For this
|
/// store additional information that doesn't live in parameters. For this
|
||||||
/// example, the node data stores the template (i.e. the "type") of the node.
|
/// example, the node data stores the template (i.e. the "type") of the node.
|
||||||
|
#[cfg_attr(feature = "persistence", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct MyNodeData {
|
pub struct MyNodeData {
|
||||||
template: MyNodeTemplate,
|
template: MyNodeTemplate,
|
||||||
}
|
}
|
||||||
@ -16,6 +17,7 @@ pub struct MyNodeData {
|
|||||||
/// attaching two ports together. The graph UI will make sure to not allow
|
/// attaching two ports together. The graph UI will make sure to not allow
|
||||||
/// attaching incompatible datatypes.
|
/// attaching incompatible datatypes.
|
||||||
#[derive(PartialEq, Eq)]
|
#[derive(PartialEq, Eq)]
|
||||||
|
#[cfg_attr(feature = "persistence", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub enum MyDataType {
|
pub enum MyDataType {
|
||||||
Scalar,
|
Scalar,
|
||||||
Vec2,
|
Vec2,
|
||||||
@ -29,6 +31,7 @@ pub enum MyDataType {
|
|||||||
/// up to the user code in this example to make sure no parameter is created
|
/// up to the user code in this example to make sure no parameter is created
|
||||||
/// with a DataType of Scalar and a ValueType of Vec2.
|
/// with a DataType of Scalar and a ValueType of Vec2.
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
#[cfg_attr(feature = "persistence", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub enum MyValueType {
|
pub enum MyValueType {
|
||||||
Vec2 { value: egui::Vec2 },
|
Vec2 { value: egui::Vec2 },
|
||||||
Scalar { value: f32 },
|
Scalar { value: f32 },
|
||||||
@ -58,6 +61,7 @@ impl MyValueType {
|
|||||||
/// will display in the "new node" popup. The user code needs to tell the
|
/// will display in the "new node" popup. The user code needs to tell the
|
||||||
/// library how to convert a NodeTemplate into a Node.
|
/// library how to convert a NodeTemplate into a Node.
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
|
#[cfg_attr(feature = "persistence", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub enum MyNodeTemplate {
|
pub enum MyNodeTemplate {
|
||||||
MakeVector,
|
MakeVector,
|
||||||
MakeScalar,
|
MakeScalar,
|
||||||
@ -82,6 +86,7 @@ pub enum MyResponse {
|
|||||||
/// parameter drawing callbacks. The contents of this struct are entirely up to
|
/// parameter drawing callbacks. The contents of this struct are entirely up to
|
||||||
/// the user. For this example, we use it to keep track of the 'active' node.
|
/// the user. For this example, we use it to keep track of the 'active' node.
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
#[cfg_attr(feature = "persistence", derive(serde::Serialize, serde::Deserialize))]
|
||||||
pub struct MyGraphState {
|
pub struct MyGraphState {
|
||||||
pub active_node: Option<NodeId>,
|
pub active_node: Option<NodeId>,
|
||||||
}
|
}
|
||||||
@ -349,8 +354,27 @@ impl Default for NodeGraphExample {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#[cfg(feature = "persistence")]
|
||||||
|
const PERSISTENCE_KEY: &str = "egui_node_graph";
|
||||||
|
|
||||||
|
#[cfg(feature = "persistence")]
|
||||||
|
impl NodeGraphExample {
|
||||||
|
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
|
||||||
|
let state = if let Some(storage) = cc.storage {
|
||||||
|
eframe::get_value(storage, PERSISTENCE_KEY)
|
||||||
|
.unwrap_or_else(|| GraphEditorState::new(0.0, MyGraphState::default()))
|
||||||
|
} else {
|
||||||
|
GraphEditorState::new(0.0, MyGraphState::default())
|
||||||
|
};
|
||||||
|
Self { state }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl eframe::App for NodeGraphExample {
|
impl eframe::App for NodeGraphExample {
|
||||||
|
#[cfg(feature = "persistence")]
|
||||||
|
fn save(&mut self, storage: &mut dyn eframe::Storage) {
|
||||||
|
eframe::set_value(storage, PERSISTENCE_KEY, &self.state);
|
||||||
|
}
|
||||||
/// Called each time the UI needs repainting, which may be many times per second.
|
/// Called each time the UI needs repainting, which may be many times per second.
|
||||||
/// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`.
|
/// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`.
|
||||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||||
|
|||||||
@ -12,6 +12,11 @@ fn main() {
|
|||||||
eframe::NativeOptions::default(),
|
eframe::NativeOptions::default(),
|
||||||
Box::new(|cc| {
|
Box::new(|cc| {
|
||||||
cc.egui_ctx.set_visuals(Visuals::dark());
|
cc.egui_ctx.set_visuals(Visuals::dark());
|
||||||
|
#[cfg(feature = "persistence")]
|
||||||
|
{
|
||||||
|
Box::new(egui_node_graph_example::NodeGraphExample::new(cc))
|
||||||
|
}
|
||||||
|
#[cfg(not(feature = "persistence"))]
|
||||||
Box::new(egui_node_graph_example::NodeGraphExample::default())
|
Box::new(egui_node_graph_example::NodeGraphExample::default())
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user