Skip to content
Merged
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 .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/.claude/
/neuronify/dist
88 changes: 13 additions & 75 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ but with new features and the ability to embed simulations into websites and onl

Neuronify 1 was originally written in C++, while Neuronify 2 is written in Rust.


To run Neuronify in the browser,
install [trunk](https://trunkrs.dev) and run `trunk serve --port <port>` in the `neuronify` folder.
Adding `?file=examples/leaky.nfy` to the URL shows that file
the way it would look embedded on a web page, with the editor hidden.
It can be any legacy `.nfy` file or `.neuronify` save file the page can fetch.
12 changes: 8 additions & 4 deletions neuronify-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ edition = "2021"
crate-type = ["cdylib", "rlib"]

[dependencies]
visula = { git = "https://github.com/dragly/visula", rev = "5b2ca173ea036853ce4d52cd8ef91da139852f68" }
visula_derive = { git = "https://github.com/dragly/visula", rev = "5b2ca173ea036853ce4d52cd8ef91da139852f68" }
visula_core = { git = "https://github.com/dragly/visula", rev = "5b2ca173ea036853ce4d52cd8ef91da139852f68" }
visula = { git = "https://github.com/dragly/visula", rev = "aa83a2b63633c688faf1efcb6f2f963390c06eb7" }
visula_derive = { git = "https://github.com/dragly/visula", rev = "aa83a2b63633c688faf1efcb6f2f963390c06eb7" }
visula_core = { git = "https://github.com/dragly/visula", rev = "aa83a2b63633c688faf1efcb6f2f963390c06eb7" }
wgpu = { version = "29", features = ["webgl"] }
glam = { version = "0.30", features = ["bytemuck", "serde"] }
bytemuck = { version = "1.24", features = ["derive"] }
Expand Down Expand Up @@ -38,7 +38,11 @@ serde_derive = "1.0.204"
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.122"
postcard = { version = "1.0.8", features = ["use-std"] }
web-sys = { version = "0.3.69", features = ["Request", "Response", "RequestInit", "Headers"] }
web-sys = { version = "0.3.69", features = ["Location", "Response", "UrlSearchParams", "Window"] }
chrono = { version = "0.4.38", features = ["serde"] }
rfd = "0.15.2"
winit = "0.30"

[target.'cfg(target_arch = "wasm32")'.dependencies]
# rand 0.8 depends on getrandom 0.2, which only builds for the web with its js feature
getrandom = { version = "0.2", features = ["js"] }
32 changes: 25 additions & 7 deletions neuronify-core/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub struct Neuronify {
pub dragging_entity: Option<Entity>,
pub drag_offset: Vec3,
pub resizing_voltmeter: Option<(Entity, ResizeCorner)>,
pub load_error: Option<String>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -187,6 +188,7 @@ impl Neuronify {
dragging_entity: None,
drag_offset: Vec3::ZERO,
resizing_voltmeter: None,
load_error: None,
}
}

Expand Down Expand Up @@ -894,13 +896,16 @@ impl Neuronify {
self.world = deserialize(&mut context, &mut deserializer).unwrap();
}

pub fn from_slice(application: &mut visula::Application, bytes: &[u8]) -> Neuronify {
let mut neuronify = Neuronify::new(application);
let mut context = LoadContext::new();
let mut deserializer = postcard::Deserializer::from_bytes(bytes);
neuronify.world = deserialize(&mut context, &mut deserializer).unwrap();
neuronify.edit_enabled = false;
neuronify
/// Replaces the world with a legacy `.nfy` file or a `.neuronify` save file.
pub fn load_from_bytes(&mut self, bytes: &[u8]) -> Result<(), String> {
self.world = crate::serialization::load_from_bytes(bytes)?;
self.time = 0.0;
self.previous_creation = None;
self.connection_tool = None;
self.active_entity = None;
self.dragging_entity = None;
self.resizing_voltmeter = None;
Ok(())
}
}

Expand All @@ -914,6 +919,14 @@ impl visula::Simulation for Neuronify {
}
}
fn update(&mut self, application: &mut visula::Application) {
#[cfg(target_arch = "wasm32")]
if let Some(fetched) = crate::web::take_fetched_file() {
if let Err(error) = fetched.and_then(|bytes| self.load_from_bytes(&bytes)) {
log::error!("{error}");
self.load_error = Some(error);
}
}

let Neuronify {
connection_tool,
world,
Expand Down Expand Up @@ -975,6 +988,11 @@ impl visula::Simulation for Neuronify {
}

fn gui(&mut self, _application: &visula::Application, context: &egui::Context) {
if let Some(error) = &self.load_error {
egui::Window::new("Could not load file").show(context, |ui| {
ui.label(error);
});
}
egui::Area::new("edit_button_area".into())
.anchor(egui::Align2::RIGHT_BOTTOM, [-10.0, -10.0])
.show(context, |ui| {
Expand Down
Loading
Loading