use std::fmt;
use bevy_asset::AssetId;
use bevy_ecs::entity::Entity;
use super::*;
use crate::ScriptAsset;
impl From<ScriptAttachment> for ContextKey {
fn from(val: ScriptAttachment) -> Self {
match val {
ScriptAttachment::EntityScript(entity, script) => ContextKey {
entity: Some(entity),
script: Some(script.id()),
},
ScriptAttachment::StaticScript(script) => ContextKey {
entity: None,
script: Some(script.id()),
},
}
}
}
#[derive(Debug, Hash, Clone, Default, PartialEq, Eq, Reflect)]
pub struct ContextKey {
pub entity: Option<Entity>,
pub script: Option<AssetId<ScriptAsset>>,
}
impl fmt::Display for ContextKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut empty = true;
if let Some(script_id) = &self.script {
write!(f, "script {script_id}")?;
empty = false;
}
if let Some(id) = self.entity {
write!(f, "entity {id}")?;
empty = false;
}
if empty {
write!(f, "empty")?;
}
Ok(())
}
}
impl ContextKey {
pub const INVALID: Self = Self {
entity: Some(Entity::PLACEHOLDER),
script: Some(AssetId::invalid()),
};
pub const SHARED: Self = {
Self {
entity: None,
script: None,
}
};
pub fn is_empty(&self) -> bool {
self == &Self::default()
}
pub fn or(self, other: ContextKey) -> Self {
Self {
entity: self.entity.or(other.entity),
script: self.script.or(other.script),
}
}
}