use std::collections::HashMap;
use bevy::prelude::*;
use crate::render::{NoesisRenderState, NoesisSet};
pub type StateRequest = (String, bool);
#[derive(Component, Clone, Default, Debug)]
pub struct NoesisVisualState {
pub states: HashMap<String, StateRequest>,
}
impl NoesisVisualState {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn state(
mut self,
name: impl Into<String>,
state: impl Into<String>,
use_transitions: bool,
) -> Self {
self.states
.insert(name.into(), (state.into(), use_transitions));
self
}
pub fn go_to(
&mut self,
name: impl Into<String>,
state: impl Into<String>,
use_transitions: bool,
) {
self.states
.insert(name.into(), (state.into(), use_transitions));
}
}
#[allow(clippy::needless_pass_by_value)]
pub(crate) fn sync_visual_state_bridge(
views: Query<(Entity, Ref<NoesisVisualState>)>,
state: Option<NonSendMut<NoesisRenderState>>,
) {
let Some(mut state) = state else {
return;
};
for (entity, visual_state) in &views {
if visual_state.is_changed() || state.scene_rebuilt_this_frame(entity) {
state.apply_visual_state_for(entity, &visual_state.states);
}
}
}
pub struct NoesisVisualStatePlugin;
impl Plugin for NoesisVisualStatePlugin {
fn build(&self, app: &mut App) {
app.add_systems(
PostUpdate,
sync_visual_state_bridge.in_set(NoesisSet::Apply),
);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn builder_collects_states() {
let s = NoesisVisualState::new()
.state("Panel", "Alert", true)
.state("Button", "Pressed", false);
assert_eq!(s.states.get("Panel"), Some(&("Alert".to_string(), true)));
assert_eq!(
s.states.get("Button"),
Some(&("Pressed".to_string(), false)),
);
}
}