mirror of
https://github.com/LGY07/bevy_taffy.git
synced 2026-12-04 01:35:31 +08:00
Taffy layout algorithms backed by Bevy ECS.
- Rust 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| benches | ||
| examples | ||
| src | ||
| tests | ||
| .gitignore | ||
| AGENTS.md | ||
| Cargo.lock | ||
| Cargo.toml | ||
| CONTRIBUTING.md | ||
| LICENSE-APACHE | ||
| LICENSE-MIT | ||
| README.md | ||
bevy_taffy
Taffy layout algorithms backed by Bevy ECS. Supports Flexbox, Grid, and Block
layout through SystemParam-based low-level traits and a TaffyTree high-level
API. Nodes are entities with dedicated layout relationships; results use native
Taffy types.
Example
Create nodes with Commands, then compute and read layouts inside ordinary systems. Apply creation commands before computing layout.
use bevy_ecs::{
entity::Entity,
query::{With, Without},
schedule::Schedule,
system::{Commands, ParamSet, Query},
world::World,
};
use bevy_taffy::{
LayoutMode, TaffyChildOf, TaffyLayout, TaffyNode, TaffyRead, TaffyTree,
taffy::{Size, Style, prelude::{TaffyMaxContent, length}},
};
fn setup(mut commands: Commands) {
let child = TaffyTree::new_leaf(&mut commands, Style {
size: Size { width: length(80.0), height: length(24.0) },
..Default::default()
});
TaffyTree::new_with_children(
&mut commands, Style::default(), &[child],
);
}
fn layout(
roots: Query<Entity, (With<TaffyNode>, Without<TaffyChildOf>)>,
mut access: ParamSet<(TaffyLayout, TaffyRead)>,
) -> bevy_ecs::error::Result {
for entity in &roots {
let root = TaffyTree::new(&access.p1(), entity)?;
root.compute_layout(
&mut access.p0(), Size::MAX_CONTENT, LayoutMode::Unrounded,
)?;
let read = access.p1();
let result = root.layout(&read, LayoutMode::Unrounded).unwrap();
println!("Size: {:?}", result.size);
}
Ok(())
}
fn main() {
let mut world = World::new();
let mut startup = Schedule::default();
startup.add_systems(setup);
startup.run(&mut world);
let mut update = Schedule::default();
update.add_systems(layout);
update.run(&mut world);
}
See examples/layout.rs for style updates, measurement, and rounding.