Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions demos/viewer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ zerocopy.workspace = true

fidget.workspace = true
workspace-hack.workspace = true
tracing-subscriber = { version = "0.3.20", features = ["env-filter"] }

[features]
default = ["jit"]
Expand Down
4 changes: 2 additions & 2 deletions demos/viewer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use eframe::{
egui,
egui_wgpu::{self, wgpu},
};
use env_logger::Env;
use log::{debug, error, info};
use nalgebra::Point2;
use notify::{Event, EventKind, Watcher};
Expand Down Expand Up @@ -223,7 +222,8 @@ fn render_3d<F: fidget::eval::Function + fidget::render::RenderHints>(
}

fn main() -> Result<(), Box<dyn Error>> {
env_logger::Builder::from_env(Env::default().default_filter_or("info"))
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();
let args = Args::parse();

Expand Down
27 changes: 22 additions & 5 deletions demos/viewer/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,34 @@ use std::sync::{Arc, Mutex};

/// Receives scripts and executes them with Fidget
pub(crate) fn rhai_script_thread(
rx: Receiver<String>,
rx: Receiver<(String, String)>,
tx: Sender<Result<ScriptContext, String>>,
) -> Result<()> {
let mut engine = Engine::new();

loop {
let script = rx.recv()?;
let (script, path) = rx.recv()?;
debug!("rhai script thread received script");
let r = engine.run(&script).map_err(|e| e.to_string());
debug!("rhai script thread is sending result to render thread");
tx.send(r)?;
if path.ends_with(".vm") {
// Parse as flat text math tree
use fidget::context::Context;
use std::io::Cursor;
match Context::from_text(Cursor::new(&script)) {
Ok((ctx, node)) => {
let tree = ctx.export(node).unwrap();
let mut sc = ScriptContext::new();
sc.shapes.push(DrawShape { tree, color_rgb: [255, 255, 255] });
tx.send(Ok(sc))?;
}
Err(e) => {
tx.send(Err(format!(".vm parse error: {e}")))?;
}
}
} else {
let r = engine.run(&script).map_err(|e| e.to_string());
debug!("rhai script thread is sending result to render thread");
tx.send(r)?;
}
}
}

Expand Down
7 changes: 4 additions & 3 deletions demos/viewer/src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ use std::path::Path;
pub(crate) fn file_watcher_thread(
path: &Path,
rx: Receiver<()>,
tx: Sender<String>,
tx: Sender<(String, String)>,
) -> Result<()> {
let read_file = || -> Result<String> {
let out = String::from_utf8(std::fs::read(path)?).unwrap();
Ok(out)
};
let mut contents = read_file()?;
tx.send(contents.clone())?;
let path_str = path.to_string_lossy().to_string();
tx.send((contents.clone(), path_str.clone()))?;

loop {
// Wait for a file change notification
Expand All @@ -31,7 +32,7 @@ pub(crate) fn file_watcher_thread(
if contents != new_contents {
contents = new_contents;
debug!("file contents changed!");
tx.send(contents.clone())?;
tx.send((contents.clone(), path_str.clone()))?;
}
}
}