diff options
| author | s-ol <s+removethis@s-ol.nu> | 2026-01-17 21:15:58 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2026-01-17 21:45:04 +0000 |
| commit | 80a09fa39b8971bf8aa5f5300558d8bae097675f (patch) | |
| tree | f2baad41c091a8f6ac532d2ca5f0d1a23faab2e8 /src/main.rs | |
| parent | file I/O (diff) | |
| download | nodetoy-80a09fa39b8971bf8aa5f5300558d8bae097675f.tar.gz nodetoy-80a09fa39b8971bf8aa5f5300558d8bae097675f.zip | |
add examples
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 46 |
1 files changed, 38 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs index 25c6062..e9eb1e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,9 @@ use egui_snarl::{ InPin, NodeId, OutPin, Snarl, ui::{NodeLayout, PinInfo, PinPlacement, SnarlStyle, SnarlViewer, SnarlWidget}, }; -use library::NodeIndex; +use lazy_static::lazy_static; use log::*; +use std::collections::HashMap; mod library; mod node; @@ -16,6 +17,7 @@ mod types; #[cfg(target_arch = "wasm32")] mod wasm; +use library::NodeIndex; use node::{AnyNode, ConcreteNode}; use snarl_ext::{Compilable, WithSignatures}; use types::{Dimension, FloatPrecision, ScalarType, Type}; @@ -160,6 +162,21 @@ pub struct App { preview: preview::Custom3d, } +lazy_static! { + static ref EXAMPLES: HashMap<&'static str, &'static str> = { + let mut m = HashMap::new(); + + m.insert("UV Ramp", include_str!("./examples/uv_ramp.nt.json")); + m.insert("Circle", include_str!("./examples/circle.nt.json")); + m.insert( + "Shadertoy Plasma", + include_str!("./examples/shadertoy_plasma.nt.json"), + ); + m.insert("Circle Grid", include_str!("./examples/grid.nt.json")); + m + }; +} + const fn default_style() -> SnarlStyle { SnarlStyle { node_layout: Some(NodeLayout::coil()), @@ -194,13 +211,12 @@ impl App { pub fn new(cx: &eframe::CreationContext) -> Self { cx.egui_ctx.style_mut(|style| style.animation_time *= 10.0); - let snarl = cx.storage.map_or_else(Snarl::new, |storage| { - storage - .get_string("snarl") - .and_then(|snarl| serde_json::from_str(&snarl).ok()) - .unwrap_or_default() - }); - // let snarl = Snarl::new(); + let snarl: Snarl<AnyNode> = cx + .storage + .and_then(|storage| storage.get_string("snarl")) + .or(EXAMPLES.get("Shadertoy Plasma").map(|s| s.to_string())) + .and_then(|data| serde_json::from_str(&data).ok()) + .unwrap_or_default(); App { snarl, @@ -238,6 +254,18 @@ impl eframe::App for App { .unwrap(); } + ui.menu_button("Load Example", |ui| { + for label in EXAMPLES.keys() { + if ui.button(*label).clicked() { + self.snarl = + serde_json::from_str(EXAMPLES.get(label).unwrap()).unwrap(); + self.viewer.dirty = true; + } + } + }); + + ui.separator(); + #[cfg(not(target_arch = "wasm32"))] if ui.button("Save").clicked() { if let Some(path) = rfd::FileDialog::new() @@ -255,6 +283,8 @@ impl eframe::App for App { .expect("successful download"); } + ui.separator(); + if ui.button("Clear All").clicked() { self.snarl = Snarl::default(); } |
