use std::collections::HashMap;
use bevy::prelude::*;
use crate::render::{NoesisRenderState, NoesisSet};
#[derive(Component, Clone, Default, Debug)]
pub struct NoesisGeometry {
pub paths: HashMap<String, Vec<[f32; 2]>>,
}
impl NoesisGeometry {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn path(mut self, name: impl Into<String>, points: Vec<[f32; 2]>) -> Self {
self.paths.insert(name.into(), points);
self
}
pub fn draw(&mut self, name: impl Into<String>, points: Vec<[f32; 2]>) {
self.paths.insert(name.into(), points);
}
}
#[allow(clippy::needless_pass_by_value)]
pub(crate) fn sync_geometry_bridge(
views: Query<(Entity, Ref<NoesisGeometry>)>,
state: Option<NonSendMut<NoesisRenderState>>,
) {
let Some(mut state) = state else {
return;
};
for (entity, geometry) in &views {
if geometry.is_changed() || state.scene_rebuilt_this_frame(entity) {
state.apply_geometry_for(entity, &geometry.paths);
}
}
}
pub struct NoesisGeometryPlugin;
impl Plugin for NoesisGeometryPlugin {
fn build(&self, app: &mut App) {
app.add_systems(PostUpdate, sync_geometry_bridge.in_set(NoesisSet::Apply));
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn builder_collects_paths() {
let g = NoesisGeometry::new()
.path("ScopeTrace", vec![[0.0, 1.0], [2.0, 3.0]])
.path("Grid", vec![[4.0, 5.0]]);
assert_eq!(
g.paths.get("ScopeTrace"),
Some(&vec![[0.0, 1.0], [2.0, 3.0]]),
);
assert_eq!(g.paths.get("Grid"), Some(&vec![[4.0, 5.0]]));
}
}