forked from jakobhellermann/bevy-inspector-egui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.rs
107 lines (98 loc) · 2.96 KB
/
demo.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
use bevy::prelude::*;
use bevy_inspector_egui::{Inspectable, InspectorPlugin};
#[derive(Inspectable)]
enum TextColor {
White,
Green,
Blue,
}
#[derive(Inspectable)]
struct Data {
#[inspectable(min = 10.0, max = 70.0)]
font_size: f32,
text: String,
show_square: bool,
text_color: TextColor,
color: Color,
#[inspectable(visual, min = Vec2::new(-200., -200.), max = Vec2::new(200., 200.))]
position: Vec2,
list: Vec<f32>,
#[inspectable(replacement = String::default as fn() -> _)]
option: Option<String>,
}
impl Default for Data {
fn default() -> Self {
Data {
font_size: 50.0,
text: "Hello World!".to_string(),
show_square: true,
text_color: TextColor::White,
color: Color::BLUE,
position: Vec2::default(),
list: vec![0.0],
option: None,
}
}
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(InspectorPlugin::<Data>::new())
.add_startup_system(setup)
.add_system(text_update_system)
.add_system(shape_update_system)
.run();
}
fn text_update_system(data: Res<Data>, mut query: Query<&mut Text>) {
for mut text in query.iter_mut() {
let text = &mut text.sections[0];
text.value = data.text.clone();
text.style.font_size = data.font_size;
text.style.color = match &data.text_color {
TextColor::White => Color::WHITE,
TextColor::Green => Color::GREEN,
TextColor::Blue => Color::BLUE,
};
}
}
fn shape_update_system(data: Res<Data>, mut query: Query<(&mut Sprite, &mut Transform)>) {
for (mut sprite, mut transfrom) in query.iter_mut() {
sprite.color = data.color;
if !data.show_square {
transfrom.translation.x = 1000000.0;
} else {
transfrom.translation.x = data.position.x;
transfrom.translation.y = data.position.y;
}
}
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
let font_handle = asset_server.load("/usr/share/fonts/truetype/noto/NotoMono-Regular.ttf");
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(UiCameraBundle::default());
commands.spawn_bundle(TextBundle {
style: Style {
align_self: AlignSelf::FlexEnd,
..Default::default()
},
text: Text::with_section(
"",
TextStyle {
font_size: 50.0,
font: font_handle,
..Default::default()
},
Default::default(),
),
..Default::default()
});
commands.spawn_bundle(SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::new(40.0, 40.0)),
color: Color::BLUE,
..Default::default()
},
transform: Transform::from_translation(Vec3::default()),
..Default::default()
});
}