Skip to content

Commit 362ed14

Browse files
committed
Created SketchRoot to hold onto Handle<Sketch>
fix AssetEvent handler for Sketch entities
1 parent 191edec commit 362ed14

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

crates/processing_render/src/sketch.rs

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,64 @@ use bevy::{
77
},
88
prelude::*,
99
};
10-
use std::path::{Path, PathBuf};
10+
use std::path::Path;
1111

1212
/// Plugin that registers the Sketch asset type and its loader.
1313
pub struct LivecodePlugin;
1414

1515
impl Plugin for LivecodePlugin {
1616
fn build(&self, app: &mut App) {
1717
app.init_asset::<Sketch>()
18-
.init_asset_loader::<SketchLoader>()
19-
.add_systems(Startup, load_current_sketch);
20-
// .add_systems(Update, test_system);
21-
let render_app = app.sub_app_mut(bevy::render::RenderApp);
22-
render_app.add_systems(ExtractSchedule, test_system);
18+
.init_asset_loader::<SketchLoader>();
19+
20+
app.add_systems(PreStartup, load_current_sketch)
21+
.add_systems(Update, sketch_update_handler);
2322
}
2423
}
2524

26-
fn test_system() {
27-
info!("DEBUG: calling test_system");
28-
assert!(false);
25+
// TODO: A better name is possible
26+
fn sketch_update_handler(mut events: MessageReader<AssetEvent<Sketch>>) {
27+
for event in events.read() {
28+
match event {
29+
AssetEvent::Added { id } => {
30+
info!("Added: {id}")
31+
}
32+
AssetEvent::Modified { id } => {
33+
info!("Modified: {id}")
34+
}
35+
AssetEvent::Removed { id } => {
36+
info!("Removed: {id}")
37+
}
38+
AssetEvent::Unused { id } => {
39+
info!("Unused: {id}")
40+
}
41+
AssetEvent::LoadedWithDependencies { id } => {
42+
info!("LoadedWithDependencies: {id}")
43+
}
44+
}
45+
}
2946
}
3047

31-
fn load_current_sketch(asset_server: Res<AssetServer>) {
48+
fn load_current_sketch(mut commands: Commands, asset_server: Res<AssetServer>) {
3249
info!("DEBUG: calling load_current_sketch");
3350
let path = Path::new("rectangle.py");
3451
let source = AssetSourceId::from("sketch_directory");
3552
let asset_path = AssetPath::from_path(path).with_source(source);
36-
let _h: Handle<Sketch> = asset_server.load(asset_path);
53+
let sketch_handle: Handle<Sketch> = asset_server.load(asset_path);
54+
commands.spawn(SketchRoot(sketch_handle));
3755
}
3856

57+
/// `SketchRoot` is what will be spawned and will contain a `Handle` to the `Sketch`
58+
#[derive(Component)]
59+
pub struct SketchRoot(pub Handle<Sketch>);
60+
3961
/// A sketch source file loaded as a Bevy asset.
4062
///
4163
/// The `Sketch` asset contains the raw source code as a string. It does not interpret
4264
/// or execute the code — that responsibility belongs to language-specific crates.
4365
#[derive(Asset, TypePath, Debug)]
4466
pub struct Sketch {
45-
/// The source code contents of the sketch file.
46-
pub source: String,
47-
48-
/// The original file path.
49-
pub path: PathBuf,
67+
source: String,
5068
}
5169

5270
/// Loads sketch files from disk.
@@ -65,7 +83,7 @@ impl AssetLoader for SketchLoader {
6583
&self,
6684
reader: &mut dyn Reader,
6785
_settings: &Self::Settings,
68-
load_context: &mut LoadContext<'_>,
86+
_load_context: &mut LoadContext<'_>,
6987
) -> Result<Self::Asset, Self::Error> {
7088
let mut source = String::new();
7189

@@ -76,10 +94,9 @@ impl AssetLoader for SketchLoader {
7694
source = utf8.to_string();
7795
}
7896

79-
let asset_path = load_context.path();
80-
let path: PathBuf = asset_path.path().to_path_buf();
97+
info!(source);
8198

82-
Ok(Sketch { source, path })
99+
Ok(Sketch { source })
83100
}
84101

85102
fn extensions(&self) -> &[&str] {

0 commit comments

Comments
 (0)