use bevy::prelude::*;
use crate::render::{NoesisRenderState, NoesisSet};
#[derive(Component, Clone, Default, Debug)]
pub struct NoesisFocus {
pub target: Option<String>,
}
impl NoesisFocus {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn focus(mut self, name: impl Into<String>) -> Self {
self.target = Some(name.into());
self
}
pub fn focus_on(&mut self, name: impl Into<String>) {
self.target = Some(name.into());
}
pub fn clear(&mut self) {
self.target = None;
}
}
#[allow(clippy::needless_pass_by_value)]
pub(crate) fn sync_focus_bridge(
views: Query<(Entity, Ref<NoesisFocus>)>,
state: Option<NonSendMut<NoesisRenderState>>,
) {
let Some(mut state) = state else {
return;
};
for (entity, focus) in &views {
if focus.is_changed() || state.scene_rebuilt_this_frame(entity) {
state.apply_focus_for(entity, focus.target.as_deref());
}
}
}
pub struct NoesisFocusPlugin;
impl Plugin for NoesisFocusPlugin {
fn build(&self, app: &mut App) {
app.add_systems(PostUpdate, sync_focus_bridge.in_set(NoesisSet::Apply));
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn builder_sets_target() {
let f = NoesisFocus::new().focus("CommandInput");
assert_eq!(f.target.as_deref(), Some("CommandInput"));
assert!(NoesisFocus::new().target.is_none());
}
}