Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/ui/ui_texture_slice_flip_and_tile.rs
6595 views
1
//! This example illustrates how to how to flip and tile images with 9-slicing in the UI.
2
3
use bevy::{
4
image::{ImageLoaderSettings, ImageSampler},
5
prelude::*,
6
ui::widget::NodeImageMode,
7
};
8
9
fn main() {
10
App::new()
11
.add_plugins(DefaultPlugins)
12
.insert_resource(UiScale(2.))
13
.add_systems(Startup, setup)
14
.run();
15
}
16
17
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
18
let image = asset_server.load_with_settings(
19
"textures/fantasy_ui_borders/numbered_slices.png",
20
|settings: &mut ImageLoaderSettings| {
21
// Need to use nearest filtering to avoid bleeding between the slices with tiling
22
settings.sampler = ImageSampler::nearest();
23
},
24
);
25
26
let slicer = TextureSlicer {
27
// `numbered_slices.png` is 48 pixels square. `BorderRect::square(16.)` insets the slicing line from each edge by 16 pixels, resulting in nine slices that are each 16 pixels square.
28
border: BorderRect::all(16.),
29
// With `SliceScaleMode::Tile` the side and center slices are tiled to fill the side and center sections of the target.
30
// And with a `stretch_value` of `1.` the tiles will have the same size as the corresponding slices in the source image.
31
center_scale_mode: SliceScaleMode::Tile { stretch_value: 1. },
32
sides_scale_mode: SliceScaleMode::Tile { stretch_value: 1. },
33
..default()
34
};
35
36
// ui camera
37
commands.spawn(Camera2d);
38
39
commands
40
.spawn(Node {
41
width: percent(100),
42
height: percent(100),
43
justify_content: JustifyContent::Center,
44
align_content: AlignContent::Center,
45
flex_wrap: FlexWrap::Wrap,
46
column_gap: px(10),
47
row_gap: px(10),
48
..default()
49
})
50
.with_children(|parent| {
51
for [columns, rows] in [[3, 3], [4, 4], [5, 4], [4, 5], [5, 5]] {
52
for (flip_x, flip_y) in [(false, false), (false, true), (true, false), (true, true)]
53
{
54
parent.spawn((
55
ImageNode {
56
image: image.clone(),
57
flip_x,
58
flip_y,
59
image_mode: NodeImageMode::Sliced(slicer.clone()),
60
..default()
61
},
62
Node {
63
width: px(16 * columns),
64
height: px(16 * rows),
65
..default()
66
},
67
));
68
}
69
}
70
});
71
}
72
73