Skip to content

Commit 1245ff6

Browse files
committed
Sketch reloading when running python
1 parent d296565 commit 1245ff6

File tree

4 files changed

+78
-11
lines changed

4 files changed

+78
-11
lines changed

crates/processing_pyo3/src/graphics.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ impl Topology {
6161
}
6262
}
6363

64+
#[pyclass]
65+
pub struct Sketch {
66+
pub source: String,
67+
}
68+
6469
#[pymethods]
6570
impl Geometry {
6671
#[new]
@@ -146,6 +151,21 @@ impl Graphics {
146151
})
147152
}
148153

154+
pub fn poll_for_sketch_update(&self) -> PyResult<Sketch> {
155+
match poll_for_sketch_updates().map_err(|_| PyRuntimeError::new_err("SKETCH UPDATE ERR"))? {
156+
Some(sketch) => Ok(Sketch {
157+
source: sketch.source,
158+
}),
159+
None => Ok(Sketch {
160+
source: "".to_string(),
161+
}),
162+
}
163+
164+
// Ok(Sketch {
165+
// source: sketch.unwrap().source,
166+
// })
167+
}
168+
149169
pub fn background(&self, args: Vec<f32>) -> PyResult<()> {
150170
let (r, g, b, a) = parse_color(&args)?;
151171
let color = bevy::color::Color::srgba(r, g, b, a);

crates/processing_pyo3/src/lib.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ mod glfw;
1212
mod graphics;
1313

1414
use graphics::{Geometry, Graphics, Image, Topology, get_graphics, get_graphics_mut};
15-
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyTuple};
15+
use pyo3::{
16+
exceptions::PyRuntimeError,
17+
ffi::c_str,
18+
prelude::*,
19+
types::{PyDict, PyTuple},
20+
};
21+
use std::ffi::{CStr, CString};
1622

1723
use std::env;
1824

@@ -81,8 +87,8 @@ fn run(module: &Bound<'_, PyModule>) -> PyResult<()> {
8187
let builtins = PyModule::import(py, "builtins")?;
8288
let locals = builtins.getattr("locals")?.call0()?;
8389

84-
let setup_fn = locals.get_item("setup")?;
85-
let draw_fn = locals.get_item("draw")?;
90+
let mut setup_fn = locals.get_item("setup")?;
91+
let mut draw_fn = locals.get_item("draw")?;
8692

8793
// call setup
8894
setup_fn.call0()?;
@@ -91,6 +97,30 @@ fn run(module: &Bound<'_, PyModule>) -> PyResult<()> {
9197
loop {
9298
{
9399
let mut graphics = get_graphics_mut(module)?;
100+
101+
// TODO: this shouldn't be on the graphics object
102+
let sketch = graphics.poll_for_sketch_update()?;
103+
if !sketch.source.is_empty() {
104+
let locals = PyDict::new(py);
105+
106+
let ok = CString::new(sketch.source.as_str()).unwrap();
107+
let cstr: &CStr = ok.as_c_str();
108+
109+
match py.run(cstr, None, Some(&locals)) {
110+
Ok(_) => {
111+
dbg!("Success of any kind?");
112+
}
113+
Err(e) => {
114+
dbg!(e);
115+
}
116+
}
117+
118+
setup_fn = locals.get_item("setup").unwrap().unwrap();
119+
draw_fn = locals.get_item("draw").unwrap().unwrap();
120+
121+
dbg!(locals);
122+
}
123+
94124
if !graphics.surface.poll_events() {
95125
break;
96126
}

crates/processing_render/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,3 +1086,12 @@ pub fn geometry_box(width: f32, height: f32, depth: f32) -> error::Result<Entity
10861086
.unwrap())
10871087
})
10881088
}
1089+
1090+
pub fn poll_for_sketch_updates() -> error::Result<Option<sketch::Sketch>> {
1091+
app_mut(|app| {
1092+
Ok(app
1093+
.world_mut()
1094+
.run_system_cached(sketch::sketch_update_handler)
1095+
.unwrap())
1096+
})
1097+
}

crates/processing_render/src/sketch.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@ impl Plugin for LivecodePlugin {
1919
fn build(&self, app: &mut App) {
2020
app.init_asset::<Sketch>()
2121
.init_asset_loader::<SketchLoader>()
22+
// TODO: this could be switched to Message
2223
.insert_resource(SketchNeedsReload(false))
23-
.add_systems(PreStartup, load_current_sketch)
24-
.add_systems(Update, sketch_update_handler);
24+
.add_systems(PreStartup, load_current_sketch);
25+
// .add_systems(Update, sketch_update_handler);
2526
}
2627
}
2728

2829
// TODO: A better name is possible
29-
fn sketch_update_handler(
30+
pub fn sketch_update_handler(
3031
mut events: MessageReader<AssetEvent<Sketch>>,
31-
mut needs_reload: ResMut<SketchNeedsReload>,
32-
) {
32+
sketches: Res<Assets<Sketch>>,
33+
) -> Option<Sketch> {
3334
for event in events.read() {
3435
match event {
3536
AssetEvent::Added { id } => {
@@ -38,7 +39,11 @@ fn sketch_update_handler(
3839
AssetEvent::Modified { id } => {
3940
info!("Modified: {id}");
4041
// we want to emit some event to bevy??
41-
needs_reload.0 = true;
42+
// needs_reload.0 = true;
43+
if let Some(sketch) = sketches.get(*id) {
44+
let sketch = sketch.clone();
45+
return Some(sketch);
46+
}
4247
}
4348
AssetEvent::Removed { id } => {
4449
info!("Removed: {id}")
@@ -51,6 +56,8 @@ fn sketch_update_handler(
5156
}
5257
}
5358
}
59+
60+
None
5461
}
5562

5663
fn load_current_sketch(mut commands: Commands, asset_server: Res<AssetServer>) {
@@ -70,9 +77,10 @@ pub struct SketchRoot(pub Handle<Sketch>);
7077
///
7178
/// The `Sketch` asset contains the raw source code as a string. It does not interpret
7279
/// or execute the code — that responsibility belongs to language-specific crates.
73-
#[derive(Asset, TypePath, Debug)]
80+
#[derive(Asset, Clone, TypePath, Debug)]
7481
pub struct Sketch {
75-
source: String,
82+
// TODO: should this be &str ?
83+
pub source: String,
7684
}
7785

7886
/// Loads sketch files from disk.

0 commit comments

Comments
 (0)