Skip to content

Commit 9d86cd6

Browse files
committed
Config HashMap
1 parent 493f08e commit 9d86cd6

File tree

7 files changed

+54
-13
lines changed

7 files changed

+54
-13
lines changed

crates/processing_pyo3/src/graphics.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ impl Graphics {
4242
let glfw_ctx =
4343
GlfwContext::new(width, height).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
4444

45-
init(Some(asset_path)).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
45+
let mut config = Config::new();
46+
config.set(ConfigKey::AssetRootPath, asset_path.to_string());
47+
init(Some(config)).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
4648

4749
let surface = glfw_ctx
4850
.create_surface(width, height, 1.0)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//! Options object for configuring various aspects of libprocessing.
2+
//!
3+
//! To add a new Config just add a new enum with associated value
4+
5+
use std::collections::HashMap;
6+
7+
#[derive(Hash, Eq, PartialEq)]
8+
pub enum ConfigKey {
9+
AssetRootPath,
10+
}
11+
// TODO: Consider Box<dyn Any> instead of String
12+
pub type ConfigMap = HashMap<ConfigKey, String>;
13+
pub struct Config {
14+
map: ConfigMap,
15+
}
16+
17+
impl Config {
18+
pub fn new() -> Self {
19+
// TODO consider defaults
20+
Config {
21+
map: ConfigMap::new(),
22+
}
23+
}
24+
25+
pub fn get(&self, k: ConfigKey) -> Option<&String> {
26+
self.map.get(&k)
27+
}
28+
29+
pub fn set(&mut self, k: ConfigKey, v: String) {
30+
self.map.insert(k, v);
31+
}
32+
}

crates/processing_render/src/lib.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod config;
12
pub mod error;
23
mod graphics;
34
pub mod image;
@@ -6,6 +7,8 @@ mod surface;
67

78
use std::{cell::RefCell, num::NonZero, path::PathBuf, sync::OnceLock};
89

10+
use config::*;
11+
912
#[cfg(feature = "python")]
1013
use bevy::asset::io::AssetSourceBuilder;
1114
#[cfg(not(target_arch = "wasm32"))]
@@ -204,7 +207,7 @@ pub fn surface_resize(graphics_entity: Entity, width: u32, height: u32) -> error
204207
})
205208
}
206209

207-
fn create_app(asset_path: Option<&str>) -> App {
210+
fn create_app(_config: Config) -> App {
208211
let mut app = App::new();
209212

210213
#[cfg(not(target_arch = "wasm32"))]
@@ -230,11 +233,13 @@ fn create_app(asset_path: Option<&str>) -> App {
230233
});
231234

232235
#[cfg(feature = "python")]
233-
app.register_asset_source(
234-
"assets_directory",
235-
// TODO: set this path to the directory containing the main sketch file
236-
AssetSourceBuilder::platform_default(asset_path.unwrap(), None),
237-
);
236+
{
237+
let asset_path = _config.get(ConfigKey::AssetRootPath).unwrap();
238+
app.register_asset_source(
239+
"assets_directory",
240+
AssetSourceBuilder::platform_default(asset_path, None),
241+
);
242+
}
238243

239244
app.add_plugins(plugins);
240245
app.add_plugins((ImagePlugin, GraphicsPlugin, SurfacePlugin));
@@ -268,13 +273,15 @@ fn set_app(app: App) {
268273
/// be called concurrently from multiple threads.
269274
/// asset_path is Optional because only python needs to use it.
270275
#[cfg(not(target_arch = "wasm32"))]
271-
pub fn init(asset_path: Option<&str>) -> error::Result<()> {
276+
pub fn init(config: Option<Config>) -> error::Result<()> {
277+
let config = config.unwrap_or_else(|| Config::new());
278+
272279
setup_tracing()?;
273280
if is_already_init()? {
274281
return Ok(());
275282
}
276283

277-
let mut app = create_app(asset_path);
284+
let mut app = create_app(config);
278285
app.finish();
279286
app.cleanup();
280287
set_app(app);

examples/rectangle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn main() {
1919

2020
fn sketch() -> error::Result<()> {
2121
let mut glfw_ctx = GlfwContext::new(400, 400)?;
22-
init()?;
22+
init(None)?;
2323

2424
let width = 400;
2525
let height = 400;

examples/transforms.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn main() {
1313

1414
fn sketch() -> error::Result<()> {
1515
let mut glfw_ctx = GlfwContext::new(400, 400)?;
16-
init()?;
16+
init(None)?;
1717

1818
let surface = glfw_ctx.create_surface(400, 400, 1.0)?;
1919
let graphics = graphics_create(surface, 400, 400)?;

examples/update_pixels.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn main() {
1919

2020
fn sketch() -> error::Result<()> {
2121
let mut glfw_ctx = GlfwContext::new(100, 100)?;
22-
init()?;
22+
init(None)?;
2323

2424
let width = 100;
2525
let height = 100;

src/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
pub use bevy::prelude::default;
2-
pub use processing_render::{render::command::DrawCommand, *};
2+
pub use processing_render::{config::*, render::command::DrawCommand, *};

0 commit comments

Comments
 (0)