From 774eb5247cf9f06fa202a05aa2d1319562e747fb Mon Sep 17 00:00:00 2001 From: KOKI Date: Fri, 26 Aug 2022 03:17:40 +0900 Subject: [PATCH] add feature persistence --- egui_node_graph_example/Cargo.toml | 4 +++- egui_node_graph_example/src/app.rs | 24 ++++++++++++++++++++++++ egui_node_graph_example/src/main.rs | 5 +++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/egui_node_graph_example/Cargo.toml b/egui_node_graph_example/Cargo.toml index 2d875a4..73232b8 100644 --- a/egui_node_graph_example/Cargo.toml +++ b/egui_node_graph_example/Cargo.toml @@ -12,9 +12,11 @@ crate-type = ["cdylib", "rlib"] eframe = "0.18.0" egui_node_graph = { path = "../egui_node_graph" } anyhow = "1.0" +serde = { version = "1.0", optional = true } [features] -default = [] +default =[] +persistence = ["serde", "egui_node_graph/persistence", "eframe/persistence"] [profile.release] opt-level = 2 # fast and small wasm diff --git a/egui_node_graph_example/src/app.rs b/egui_node_graph_example/src/app.rs index 3e2551d..4b9c6ee 100644 --- a/egui_node_graph_example/src/app.rs +++ b/egui_node_graph_example/src/app.rs @@ -8,6 +8,7 @@ use egui_node_graph::*; /// 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 /// 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 { template: MyNodeTemplate, } @@ -16,6 +17,7 @@ pub struct MyNodeData { /// attaching two ports together. The graph UI will make sure to not allow /// attaching incompatible datatypes. #[derive(PartialEq, Eq)] +#[cfg_attr(feature = "persistence", derive(serde::Serialize, serde::Deserialize))] pub enum MyDataType { Scalar, Vec2, @@ -29,6 +31,7 @@ pub enum MyDataType { /// 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. #[derive(Copy, Clone, Debug)] +#[cfg_attr(feature = "persistence", derive(serde::Serialize, serde::Deserialize))] pub enum MyValueType { Vec2 { value: egui::Vec2 }, Scalar { value: f32 }, @@ -58,6 +61,7 @@ impl MyValueType { /// will display in the "new node" popup. The user code needs to tell the /// library how to convert a NodeTemplate into a Node. #[derive(Clone, Copy)] +#[cfg_attr(feature = "persistence", derive(serde::Serialize, serde::Deserialize))] pub enum MyNodeTemplate { MakeVector, MakeScalar, @@ -82,6 +86,7 @@ pub enum MyResponse { /// 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. #[derive(Default)] +#[cfg_attr(feature = "persistence", derive(serde::Serialize, serde::Deserialize))] pub struct MyGraphState { pub active_node: Option, } @@ -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 { + #[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. /// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`. fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { diff --git a/egui_node_graph_example/src/main.rs b/egui_node_graph_example/src/main.rs index 489443a..02d1528 100644 --- a/egui_node_graph_example/src/main.rs +++ b/egui_node_graph_example/src/main.rs @@ -12,6 +12,11 @@ fn main() { eframe::NativeOptions::default(), Box::new(|cc| { 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()) }), );