Crates.io | bevy_rectray |
lib.rs | bevy_rectray |
version | 0.1.1 |
source | src |
created_at | 2024-07-20 04:08:32.913471 |
updated_at | 2024-07-27 17:08:28.208843 |
description | A minimal 2d layout system for bevy. |
homepage | |
repository | https://github.com/mintlu8/bevy_rectray |
max_upload_size | |
id | 1309241 |
size | 932,134 |
A minimal 2d layout system (that works in 3d!) for bevy.
First add RectrayPlugin
.
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
.
commands.spawn(
SpacialBundle {
...
},
RectrayFrame::from_dimension(Vec2::new(1024., 768.)),
)
To place descendant entities inside the frame, add RectrayBundle
next to entities
with TransformBundles
.
commands.spawn(
PbrBundle {
...
},
RectrayBundle {
...
}
)
Since we only operate on Transform
, bevy_rectray
works in Transform - Transform2d - Transform
sandwich situations.
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:
pub fn update_sprite_dimension(
scaling_factor: Query<&Window, With<PrimaryWindow>>,
mut query: Query<(&mut Sprite, &Handle<Image>, &mut Dimension)>,
assets: Res<Assets<Image>>
) {
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
.
Add RectrayContainerBundle
to put child items in a Layout
.
See module level documentation for details.
bevy | bevy_rectray |
---|---|
0.14 | latest |
Licensed under either of
at your option.
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.