This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfull.rs
365 lines (346 loc) · 15.1 KB
/
full.rs
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
};
use bevy_ninepatch::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
App::default()
.add_plugins(DefaultPlugins)
// Add the `NinePatchPlugin` plugin
.add_plugin(NinePatchPlugin::<Content>::default())
.add_plugin(FrameTimeDiagnosticsPlugin::default())
// Adds a system that prints diagnostics to the console
.add_plugin(LogDiagnosticsPlugin::default())
.add_startup_system(setup)
.add_system(set_content)
.add_system(update_size)
.run();
Ok(())
}
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut nine_patches: ResMut<Assets<NinePatchBuilder<Content>>>,
) {
// load the assets
let cornered_panel_texture_handle = asset_server.load("metalPanel_yellowCorner.png");
let panel_nine_patch_handle = nine_patches.add(NinePatchBuilder::from_patches(vec![
vec![
// top left corner patch
Patch {
original_size: IVec2::new(30, 35),
target_size: Size::new(Val::Undefined, Val::Undefined),
content: None,
},
// top middle-left patch. This patch width can grow, and will contain the content for
// `PanelContent::Title`
Patch {
original_size: IVec2::new(15, 35),
target_size: Size::new(Val::Percent(30.), Val::Undefined),
content: Some(Content::Title),
},
// top middle patch. In the original PNG, it's the yellow titled part
Patch {
original_size: IVec2::new(25, 35),
target_size: Size::new(Val::Undefined, Val::Undefined),
content: None,
},
// top middle-right patch. This patch width can grow
Patch {
original_size: IVec2::new(20, 35),
target_size: Size::new(Val::Percent(70.), Val::Undefined),
content: None,
},
// top right corner
Patch {
original_size: IVec2::new(10, 35),
target_size: Size::new(Val::Undefined, Val::Undefined),
content: None,
},
],
vec![
// left border. This patch height can grow
Patch {
original_size: IVec2::new(10, -45),
target_size: Size::new(Val::Undefined, Val::Percent(100.)),
content: None,
},
// center. This patch can grow both in height and width, and will contain `PanelContent::Body`
Patch {
original_size: IVec2::new(-20, -45),
target_size: Size::new(Val::Percent(100.), Val::Percent(100.)),
content: Some(Content::Content),
},
// right border. This patch height can grow
Patch {
original_size: IVec2::new(10, -45),
target_size: Size::new(Val::Undefined, Val::Percent(100.)),
content: None,
},
],
vec![
// bottom left corner
Patch {
original_size: IVec2::new(10, 10),
target_size: Size::new(Val::Undefined, Val::Undefined),
content: None,
},
// bottom middle. This patch width can grow
Patch {
original_size: IVec2::new(-20, 10),
target_size: Size::new(Val::Percent(100.), Val::Undefined),
content: None,
},
// bottom right corner
Patch {
original_size: IVec2::new(10, 10),
target_size: Size::new(Val::Undefined, Val::Undefined),
content: None,
},
],
]));
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::all(Val::Auto),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
size: Size::new(Val::Px(900.), Val::Px(600.)),
..Default::default()
},
nine_patch_data: NinePatchData {
nine_patch: panel_nine_patch_handle,
texture: cornered_panel_texture_handle,
..Default::default()
},
..Default::default()
},
UiElement::Panel,
));
commands.spawn(Camera2dBundle::default());
}
fn set_content(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut nine_patches: ResMut<Assets<NinePatchBuilder<Content>>>,
mut patch_content: Query<(Entity, &mut NinePatchContent<Content>)>,
ui_element_query: Query<&UiElement>,
mut font: Local<Handle<Font>>,
) {
*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 (
*ui_element_query
.get_component::<UiElement>(nine_patch_content.parent)
.unwrap(),
&nine_patch_content.content,
) {
(UiElement::Panel, Content::Content) => {
let panel_texture_handle: Handle<Image> =
asset_server.load("glassPanel_corners.png");
// load the 9-Patch as an assets and keep an `Handle<NinePatchBuilder<()>>`
let nine_patch_handle = nine_patches.add(
NinePatchBuilder::by_margins_with_content(20, 20, 20, 20, Content::Content),
);
let content_entity = 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::all(Val::Auto),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
size: Size::new(Val::Px(850.), Val::Px(550.)),
..Default::default()
},
nine_patch_data: NinePatchData {
nine_patch: nine_patch_handle,
texture: panel_texture_handle,
..Default::default()
},
..Default::default()
},
UiElement::InnerPanel,
))
.id();
commands.entity(entity).push_children(&[content_entity]);
nine_patch_content.loaded = true;
}
(UiElement::Panel, Content::Title) => {
let content_entity = commands
.spawn(
TextBundle::from_section(
"Example Title",
TextStyle {
font: font.clone(),
font_size: 25.0,
color: Color::BLUE,
},
)
.with_style(Style {
margin: UiRect {
left: Val::Undefined,
right: Val::Auto,
top: Val::Px(8.),
bottom: Val::Auto,
},
..Default::default()
}),
)
.id();
commands.entity(entity).push_children(&[content_entity]);
nine_patch_content.loaded = true;
}
(UiElement::InnerPanel, _) => {
// prepare the button
let button_texture_handle = asset_server.load("blue_button02.png");
let button_nine_patch_handle = nine_patches.add(
NinePatchBuilder::by_margins_with_content(5, 10, 6, 6, Content::Content),
);
let button_cancel_entity = 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()
},
UiElement::ButtonCancel,
))
.id();
let button_ok_entity = 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::Auto,
right: Val::Px(0.),
top: Val::Auto,
bottom: Val::Px(0.),
},
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
size: Size::new(Val::Px(300.), Val::Px(80.)),
..Default::default()
},
nine_patch_data: NinePatchData {
nine_patch: button_nine_patch_handle,
texture: button_texture_handle,
..Default::default()
},
..Default::default()
},
UiElement::ButtonOK,
))
.id();
commands
.entity(entity)
.push_children(&[button_cancel_entity, button_ok_entity]);
nine_patch_content.loaded = true;
}
(UiElement::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(110.),
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;
}
(UiElement::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;
}
}
}
}
}
#[derive(Clone, PartialEq, Eq, std::hash::Hash)]
enum Content {
Title,
Content,
}
#[derive(Clone, Copy, Component)]
enum UiElement {
Panel,
InnerPanel,
ButtonOK,
ButtonCancel,
}
// by changing the component `Style.size`, the 9-Patch UI element will be resized
fn update_size(time: Res<Time>, mut query: Query<(&mut Style, &UiElement)>) {
for (mut style, panel) in query.iter_mut() {
let (x, y) = time.elapsed_seconds().sin_cos();
match panel {
UiElement::Panel => {
style.size.width = Val::Px((900. + 50. * x as f32).ceil());
style.size.height = Val::Px((600. + 50. * y as f32).ceil());
}
UiElement::InnerPanel => {
style.size.width = Val::Px((850. + 50. * x as f32).ceil());
style.size.height = Val::Px((550. + 50. * y as f32).ceil());
}
UiElement::ButtonOK => style.size.width = Val::Px((300. + 50. * x as f32).ceil()),
UiElement::ButtonCancel => style.size.height = Val::Px((90. + 10. * y as f32).ceil()),
}
}
}