1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use bevy::prelude::*;
use bevy_ninepatch::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
App::default()
.add_plugins(DefaultPlugins)
// Add the `NinePatchPlugin` plugin
.add_plugin(NinePatchPlugin::<()>::default())
.add_startup_system(setup)
.add_system(set_content)
.run();
Ok(())
}
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut nine_patches: ResMut<Assets<NinePatchBuilder<()>>>,
) {
commands.spawn(Camera2dBundle::default());
// prepare the button
let button_texture_handle = asset_server.load("blue_button02.png");
let button_nine_patch_handle = nine_patches.add(NinePatchBuilder::by_margins(5, 10, 6, 6));
commands.spawn((
// this component bundle will be detected by the plugin, and the 9-Patch UI element will be added as a child
// of this entity
NinePatchBundle {
style: Style {
margin: UiRect {
left: Val::Px(0.),
right: Val::Auto,
top: Val::Auto,
bottom: Val::Px(0.),
},
size: Size::new(Val::Px(300.), Val::Px(80.)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..Default::default()
},
nine_patch_data: NinePatchData {
nine_patch: button_nine_patch_handle.clone(),
texture: button_texture_handle.clone(),
..Default::default()
},
..Default::default()
},
PatchElement::ButtonCancel,
));
commands.spawn((
// this component bundle will be detected by the plugin, and the 9-Patch UI element will be added as a child
// of this entity
NinePatchBundle {
style: Style {
margin: UiRect {
left: Val::Px(0.),
right: Val::Auto,
top: Val::Auto,
bottom: Val::Px(0.),
},
size: Size::new(Val::Px(300.), Val::Px(80.)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..Default::default()
},
nine_patch_data: NinePatchData {
nine_patch: button_nine_patch_handle,
texture: button_texture_handle,
..Default::default()
},
..Default::default()
},
PatchElement::ButtonOk,
));
}
#[derive(Component)]
enum PatchElement {
ButtonOk,
ButtonCancel,
}
fn set_content(
mut commands: Commands,
asset_server: Res<AssetServer>,
patch_query: Query<&PatchElement>,
mut patch_content: Query<(Entity, &mut NinePatchContent<()>)>,
) {
let font = asset_server.load("Kenney Future Narrow.ttf");
for (entity, mut nine_patch_content) in &mut patch_content.iter_mut() {
if !nine_patch_content.loaded {
match *patch_query
.get_component::<PatchElement>(nine_patch_content.parent)
.expect("couldn't find tagged parent 9-Patch UI element")
{
PatchElement::ButtonOk => {
let content_entity = commands
.spawn(
TextBundle::from_section(
"OK",
TextStyle {
font: font.clone(),
font_size: 50.0,
color: Color::GREEN,
},
)
.with_style(Style {
margin: UiRect {
left: Val::Px(50.),
right: Val::Auto,
top: Val::Px(10.),
bottom: Val::Auto,
},
..Default::default()
}),
)
.id();
commands.entity(entity).push_children(&[content_entity]);
nine_patch_content.loaded = true;
}
PatchElement::ButtonCancel => {
let content_entity = commands
.spawn(
TextBundle::from_section(
"CANCEL",
TextStyle {
font: font.clone(),
font_size: 50.0,
color: Color::RED,
},
)
.with_style(Style {
margin: UiRect {
left: Val::Px(50.),
right: Val::Auto,
top: Val::Px(10.),
bottom: Val::Auto,
},
..Default::default()
}),
)
.id();
commands.entity(entity).push_children(&[content_entity]);
nine_patch_content.loaded = true;
}
}
}
}
}