use bevy_ecs::prelude::*;
use std::collections::HashMap;
use taffy::TaffyError;
use taffy::prelude::*;
#[derive(Component, Debug, Clone, PartialEq, Default)]
#[repr(transparent)]
pub struct TaffyStyle(pub(crate) Style);
unsafe impl Send for TaffyStyle {}
unsafe impl Sync for TaffyStyle {}
impl TaffyStyle {
pub fn new(style: Style) -> Self {
Self(style)
}
pub fn style(&self) -> &Style {
&self.0
}
}
#[derive(Component, Debug, Clone, PartialEq, Copy, Default)]
#[repr(transparent)]
pub struct TaffyComputedLayout(pub(crate) Layout);
impl From<Layout> for TaffyComputedLayout {
fn from(layout: Layout) -> Self {
Self(layout)
}
}
#[derive(Resource)]
pub struct TaffyLayoutResource {
tree: TaffyTree<()>,
entity_to_node: HashMap<Entity, NodeId>,
node_to_entity: HashMap<NodeId, Entity>,
}
unsafe impl Send for TaffyLayoutResource {}
unsafe impl Sync for TaffyLayoutResource {}
impl Default for TaffyLayoutResource {
fn default() -> Self {
Self {
tree: TaffyTree::new(),
entity_to_node: HashMap::new(),
node_to_entity: HashMap::new(),
}
}
}
impl TaffyLayoutResource {
pub fn create_node(&mut self, entity: Entity) -> Result<NodeId, TaffyError> {
let node_id = self.tree.new_leaf(Style::default())?;
self.entity_to_node.insert(entity, node_id);
self.node_to_entity.insert(node_id, entity);
Ok(node_id)
}
pub fn remove_node(&mut self, entity: Entity) -> Result<(), TaffyError> {
if let Some(node_id) = self.entity_to_node.remove(&entity) {
self.node_to_entity.remove(&node_id);
self.tree.remove(node_id)?;
}
Ok(())
}
pub fn get_node(&self, entity: Entity) -> Option<NodeId> {
self.entity_to_node.get(&entity).copied()
}
pub fn get_entity(&self, node_id: NodeId) -> Option<Entity> {
self.node_to_entity.get(&node_id).copied()
}
pub fn taffy(&self) -> &TaffyTree<()> {
&self.tree
}
pub fn taffy_mut(&mut self) -> &mut TaffyTree<()> {
&mut self.tree
}
#[cfg(debug_assertions)]
pub fn verify_mapping_consistency(&self) {
assert_eq!(
self.entity_to_node.len(),
self.node_to_entity.len(),
"Entity→Node と Node→Entity のマッピング数が一致しません"
);
for (entity, node_id) in &self.entity_to_node {
assert_eq!(
self.node_to_entity.get(node_id),
Some(entity),
"Entity {:?} のマッピングが不整合です",
entity
);
}
}
}