Repository navigation
Expand file tree
/
Copy pathapp_under_test.rs
More file actions
104 lines (96 loc) · 3.19 KB
/
Copy pathapp_under_test.rs
File metadata and controls
104 lines (96 loc) · 3.19 KB
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
//! A Bevy app that can be used as an integration test target.
//! It displays a button that must be clicked, then activated from the keyboard. The button is
//! placed at a random position and moves every 5 seconds.
//!
//! Run with the `bevy_remote` and `bevy_feathers` feature enabled:
//! ```bash
//! cargo run --example app_under_test --features="bevy_remote bevy_feathers"
//! ```
//! This example can be paired with the `integration_test` example, which will run an integration
//! test on this app.
use bevy::{
feathers::{
controls::FeathersButton, dark_theme::create_dark_theme, display::caption, theme::UiTheme,
FeathersPlugins,
},
input_focus::tab_navigation::TabGroup,
prelude::*,
remote::{http::RemoteHttpPlugin, RemotePlugin},
time::common_conditions::on_timer,
ui::UiGlobalTransform,
};
use chacha20::ChaCha8Rng;
use rand::{RngExt, SeedableRng};
fn main() {
App::new()
.add_plugins((DefaultPlugins, FeathersPlugins))
// To make the app available for integration testing, we add these
// remote plugins to expose API’s for a testing framework to call.
.add_plugins(RemotePlugin::default())
.add_plugins(RemoteHttpPlugin::default())
.insert_resource(UiTheme(create_dark_theme()))
.insert_resource(SeededRng(ChaCha8Rng::seed_from_u64(19878367467712)))
.add_systems(Startup, setup)
.add_systems(
Update,
(
move_button.run_if(on_timer(std::time::Duration::from_secs(5))),
log_button_position,
),
)
.run();
}
#[derive(Resource)]
struct SeededRng(ChaCha8Rng);
fn on_button_click(_click: On<PointerClick>) {
info!("Button pressed!");
}
fn log_button_position(
transform: Single<&UiGlobalTransform, (With<FeathersButton>, Changed<UiGlobalTransform>)>,
) {
info!(
"Button at physical ({}, {})",
transform.translation.x, transform.translation.y
);
}
fn random_position(rng: &mut ChaCha8Rng) -> (f32, f32) {
let left_pct = rng.random_range(0.0..=60.0);
let top_pct = rng.random_range(0.0..=60.0);
(left_pct, top_pct)
}
fn move_button(
mut rng: ResMut<SeededRng>,
mut button_query: Query<&mut Node, With<FeathersButton>>,
) {
let (left_pct, top_pct) = random_position(&mut rng.0);
for mut node in &mut button_query {
node.left = percent(left_pct);
node.top = percent(top_pct);
}
}
fn setup(mut commands: Commands, mut rng: ResMut<SeededRng>) {
let (left_pct, top_pct) = random_position(&mut rng.0);
commands.spawn(Camera2d);
commands.spawn_scene(bsn! {
Node {
width: percent(100),
height: percent(100),
}
// Lets Tab move keyboard focus to the button.
TabGroup::new(0)
Children [
@FeathersButton {
@caption: bsn! { @caption("Button") }
}
Node {
width: px(150),
height: px(65),
border: UiRect::all(px(5)),
left: percent(left_pct),
top: percent(top_pct),
}
BorderColor::all(Color::WHITE)
on(on_button_click)
]
});
}