use crate::components::{Sprite, TextInput, TextLabel};
use crate::ecs::{ComponentSlot, PipelineContext, asset_id::AssetId};
pub(crate) trait Identified: ComponentSlot {
fn asset_id(&self) -> AssetId;
}
macro_rules! impl_identified {
($($t:ty),+ $(,)?) => {
$(impl Identified for $t {
fn asset_id(&self) -> AssetId {
self.asset_id
}
})+
};
}
impl_identified!(Sprite, TextInput, TextLabel);
pub(crate) fn find_mut<'a, C: Identified>(
ctx: &'a mut PipelineContext,
id: AssetId,
) -> Option<&'a mut C> {
ctx.query_mut::<C>().find(|c| c.asset_id() == id)
}
pub(crate) fn update<C: Identified>(
ctx: &mut PipelineContext,
id: Option<AssetId>,
apply: impl FnOnce(&mut C),
) {
let Some(id) = id else { return };
if let Some(c) = find_mut::<C>(ctx, id) {
apply(c);
}
}
pub(crate) fn set_text(ctx: &mut PipelineContext, id: AssetId, text: &str) {
if let Some(l) = find_mut::<TextLabel>(ctx, id) {
l.content = text.to_string();
}
}