https://github.com/mintlu8/bevy_rectray
A minimal 2d layout system (that works in 3d!) for bevy.
https://github.com/mintlu8/bevy_rectray
bevy layout rust ui
Last synced: 5 months ago
JSON representation
A minimal 2d layout system (that works in 3d!) for bevy.
- Host: GitHub
- URL: https://github.com/mintlu8/bevy_rectray
- Owner: mintlu8
- License: apache-2.0
- Created: 2024-07-20T03:35:15.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-12-01T06:39:46.000Z (7 months ago)
- Last Synced: 2025-01-30T18:48:21.430Z (5 months ago)
- Topics: bevy, layout, rust, ui
- Language: Rust
- Homepage:
- Size: 487 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# bevy_rectray
A minimal 2d layout system (that works in 3d!) for bevy.
[](https://crates.io/crates/bevy_rectray)
[](https://docs.rs/bevy_rectray/latest/bevy_rectray/)
[](https://bevyengine.org/learn/book/plugin-development/)## Getting Started
First add `RectrayPlugin`.
```rust
app.add_plugins(RectrayPlugin)
```Then add `RectrayFrame` to a parent entity.
This effectively creates a 2d rectangular space around
the local `x` and `y` axis of the entity's `Transform`.```rust
commands.spawn(
SpacialBundle {
...
},
RectrayFrame::from_dimension(Vec2::new(1024., 768.)),
)
```To place descendant entities inside the frame, add `Transform2D` and `Dimension` next to entities
with `Transform`s.```rust
commands.spawn (
Transform { .. },
Transform2D { .. },
Dimension { .. },
)
```Since we only operate on `Transform`, `bevy_rectray`
works in `Transform - Transform2d - Transform` sandwich situations.## Integration
`bevy_rectray` is minimal and does not magically react to changes in bevy components.
We take in `Transform2D` and `Dimension` and produces `Transform`
and `RotatedRect`.Some of those data can come from external sources.
For example if you want to make all `Sprite`s take up space of its `Image` or `custom_size`,
add a system like this manually:```rust
pub fn update_sprite_dimension(
scaling_factor: Query<&Window, With>,
mut query: Query<(&mut Sprite, &Handle, &mut Dimension)>,
assets: Res>
) {
let scaling_factor = scaling_factor
.get_single()
.map(|x| x.scale_factor())
.unwrap_or(1.0);
query.iter_mut().for_each(|(sp, im, mut dimension)| {
dimension.0 = sp.custom_size.or_else(|| {
sp.rect.map(|rect| (rect.max - rect.min) * scaling_factor)
.or_else(|| {
assets.get(im)
.map(|x|x.size().as_vec2() * scaling_factor)
})
}).unwrap_or(Vec2::ZERO)
})
}
```If you want the opposite behavior, you can update the size of a sprite from
the outputted `RotatedRect::dimension`.## Containers
Add `RectrayContainerBundle` to put child items in a `Layout`.
See module level documentation for details.## Showcase
* Place items on anchors of parents

* Put 3d meshes in a layout

* Place individual words to form a paragraph

## Versions
| bevy | bevy_rectray |
|------|--------------|
| 0.14 | 0.1 |
| 0.15 | 0.2-latest |## License
Licensed under either of
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or )
* MIT license ([LICENSE-MIT](LICENSE-MIT) or )at your option.
### Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any
additional terms or conditions.