use crate::{CoreError, EntityKind, EntityRef, InteractionEdge, InteractionHandle, Scene};
#[cfg(test)]
#[path = "interactions_tests.rs"]
mod tests;
impl Scene {
pub fn add_interaction(
&mut self,
interaction: InteractionEdge,
) -> Result<InteractionHandle, CoreError> {
if self.structure(interaction.owner()).is_none() {
return Err(CoreError::StaleHandle);
}
self.interaction_revision = self.interaction_revision.wrapping_add(1);
Ok(InteractionHandle(self.interactions.insert(interaction)))
}
#[must_use]
pub fn interaction(&self, handle: InteractionHandle) -> Option<&InteractionEdge> {
self.interactions.get(handle.0)
}
#[must_use]
pub fn interaction_for_entity(
&self,
entity: EntityRef,
) -> Option<(InteractionHandle, &InteractionEdge)> {
if entity.kind != EntityKind::Edge {
return None;
}
let (handle, edge) = self.interactions.get_index(entity.index)?;
if edge.owner() != entity.structure {
return None;
}
Some((InteractionHandle(handle), edge))
}
pub fn interaction_mut(&mut self, handle: InteractionHandle) -> Option<&mut InteractionEdge> {
let edge = self.interactions.get_mut(handle.0)?;
self.interaction_revision = self.interaction_revision.wrapping_add(1);
Some(edge)
}
pub fn remove_interaction(&mut self, handle: InteractionHandle) -> Option<InteractionEdge> {
let removed = self.interactions.remove(handle.0);
if removed.is_some() {
self.interaction_revision = self.interaction_revision.wrapping_add(1);
}
removed
}
pub fn interactions(&self) -> impl Iterator<Item = (InteractionHandle, &InteractionEdge)> + '_ {
self.interactions
.iter()
.map(|(handle, edge)| (InteractionHandle(handle), edge))
}
#[must_use]
pub fn interaction_count(&self) -> usize {
self.interactions.len()
}
#[must_use]
pub const fn interaction_revision(&self) -> u64 {
self.interaction_revision
}
#[must_use]
pub fn interaction_row(handle: InteractionHandle) -> u32 {
handle.row()
}
}