Skip to content

Commit

Permalink
Move serde to feature; add serde asset loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
tychedelia committed Jul 19, 2024
1 parent 9ee3102 commit 2791e33
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 18 deletions.
39 changes: 37 additions & 2 deletions Cargo.lock

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

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ resolver = "2"
bevy = "0.14.0"
bevy_egui = "0.28.0"
bevy-inspector-egui = "0.25.0"
rayon = "1.10"
rayon = "1.10"
bevy_common_assets = "0.11.0"
serde = "1"
serde_json = "1"
toml = "0.8"
serde_yaml = "0.9"
15 changes: 8 additions & 7 deletions nannou/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ edition = "2018"
bevy = { workspace = true }
bevy-inspector-egui = { workspace = true, optional = true }
bevy_egui = { workspace = true, optional = true }

bevy_common_assets = { workspace = true, optional = true }
bevy_nannou = { version = "0.1.0", path = "../bevy_nannou" }
futures = "0.3"
find_folder = "0.3"
getrandom = "0.2.3"
instant = "0.1.9"
lyon = "1.0"
nannou_core = { version ="0.19.0", path = "../nannou_core", features = ["std", "serde"] }
nannou_core = { version ="0.19.0", path = "../nannou_core", features = ["std"] }
noise = "0.7"
notosans = { version = "0.1", optional = true }
num_cpus = "1"
pennereq = "0.3"
rusttype = { version = "0.8", features = ["gpu_cache"] }
serde = "1"
serde_derive = "1"
serde_json = "1"
toml = "0.5"
serde = { workspace = true, features = ["derive"], optional = true}
serde_json = { workspace = true, optional = true }
toml = { workspace = true, optional = true }
serde_yaml = { workspace = true, optional = true }
walkdir = "2"
web-sys = { version = "0.3.64", optional = true }

Expand All @@ -47,4 +47,5 @@ egui = ["bevy_egui", "bevy-inspector-egui"]
nightly = ["bevy_nannou/nightly"]
hot_reload = ["bevy/file_watcher"]
isf = ["bevy_nannou/isf"]
video = ["bevy_nannou/video"]
video = ["bevy_nannou/video"]
serde = ["dep:serde", "toml", "serde_json", "serde_yaml", "nannou_core/serde", "bevy_common_assets/json", "bevy_common_assets/toml", "bevy_common_assets/yaml"]
29 changes: 26 additions & 3 deletions nannou/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,19 @@ where
self
}

#[cfg(feature = "serde")]
pub fn init_config<T>(mut self) -> Self
where
for<'de> T: serde::Deserialize<'de> + Asset,
{
self.app.add_plugins((
bevy_common_assets::json::JsonAssetPlugin::<T>::new(&[".json"]),
bevy_common_assets::toml::TomlAssetPlugin::<T>::new(&[".toml"]),
bevy_common_assets::yaml::YamlAssetPlugin::<T>::new(&[".yaml", ".yml"]),
));
self
}

pub fn add_plugin<P>(mut self, plugin: P) -> Self
where
P: Plugin,
Expand Down Expand Up @@ -571,7 +584,7 @@ impl<'w> App<'w> {
#[cfg(feature = "egui")]
/// Get the egui context for the provided window.
pub fn egui_for_window(&self, window: Entity) -> RefMut<EguiContext> {
let world = self.resource_world_mut();
let world = self.component_world_mut();
RefMut::map(world, |world| {
world
.get_mut::<EguiContext>(window)
Expand Down Expand Up @@ -614,12 +627,18 @@ impl<'w> App<'w> {
keyboard_input.clone()
}

/// Get the [`Time`] resource.
/// Get the elapsed seconds since startup.
pub fn time(&self) -> f32 {
let time = self.resource::<Time>();
time.elapsed_seconds()
}

/// Get the elapsed seconds since the last frame.
pub fn time_delta(&self) -> f32 {
let time = self.resource::<Time>();
time.delta_seconds()
}

// Create a new `App`.
fn new(world: &'w mut World) -> Self {
let world = world.as_unsafe_world_cell();
Expand Down Expand Up @@ -923,7 +942,11 @@ where
let mut app = App::new(world);

// Create our default window if necessary
if app.resource_world().get_resource::<CreateDefaultWindow>().is_some() {
if app
.resource_world()
.get_resource::<CreateDefaultWindow>()
.is_some()
{
let mut window: window::Builder<'_, '_, M> = app.new_window();
match default_window_size {
None => {}
Expand Down
11 changes: 9 additions & 2 deletions nannou/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,16 @@ impl<'a, 'w> Builder<'a, 'w> {
}

pub fn build(self) -> Entity {
let entity = self.app.component_world_mut().spawn((self.camera, NannouCamera)).id();
let entity = self
.app
.component_world_mut()
.spawn((self.camera, NannouCamera))
.id();
if let Some(layer) = self.layer {
self.app.component_world_mut().entity_mut(entity).insert(layer);
self.app
.component_world_mut()
.entity_mut(entity)
.insert(layer);
} else {
self.app
.component_world_mut()
Expand Down
1 change: 1 addition & 0 deletions nannou/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub mod app;
mod camera;
pub mod geom;
pub mod image;
#[cfg(feature = "serde")]
pub mod io;
mod light;
pub mod noise;
Expand Down
5 changes: 4 additions & 1 deletion nannou/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ impl<'a, 'w> Builder<'a, 'w> {
pub fn build(self) -> Entity {
let entity = self.app.component_world_mut().spawn(self.light).id();
if let Some(layer) = self.layer {
self.app.component_world_mut().entity_mut(entity).insert(layer);
self.app
.component_world_mut()
.entity_mut(entity)
.insert(layer);
} else {
self.app
.component_world_mut()
Expand Down
1 change: 1 addition & 0 deletions nannou/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub use nannou_core::prelude::*;

pub use crate::app::{self, App, RunMode, UpdateModeExt};
pub use crate::camera::SetCamera;
#[cfg(feature = "serde")]
pub use crate::io::{load_from_json, load_from_toml, safe_file_save, save_to_json, save_to_toml};
pub use crate::light::SetLight;
pub use crate::time::DurationF64;
8 changes: 6 additions & 2 deletions nannou/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,8 @@ where
.app
.component_world_mut()
.query::<(&mut Camera, Option<&mut RenderLayers>)>();
if let Ok((mut camera, layers)) = q.get_mut(&mut self.app.component_world_mut(), camera) {
if let Ok((mut camera, layers)) = q.get_mut(&mut self.app.component_world_mut(), camera)
{
camera.target = RenderTarget::Window(WindowRef::Entity(entity));
if let None = layers {
self.app
Expand Down Expand Up @@ -468,7 +469,10 @@ where
}

if let Some(light) = self.light {
self.app.component_world_mut().entity_mut(light).insert(layer.clone());
self.app
.component_world_mut()
.entity_mut(light)
.insert(layer.clone());
}

entity
Expand Down

0 comments on commit 2791e33

Please sign in to comment.