-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcommands.rs
More file actions
69 lines (60 loc) · 2.05 KB
/
commands.rs
File metadata and controls
69 lines (60 loc) · 2.05 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
use std::time::Duration;
use bevy::prelude::*;
use bevy_webview::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(WebviewPlugin::with_engine(webview_engine::headless))
.add_startup_system(setup)
.add_system(send_commands_system)
.run();
}
fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
commands.spawn(WebviewUIBundle {
webview: Webview {
uri: Some("https://bevyengine.org/".into()),
..Default::default()
},
style: Style {
size: Size::new(Val::Percent(50.0), Val::Percent(50.)),
margin: UiRect::all(Val::Auto),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..Default::default()
},
..Default::default()
});
commands.insert_resource(Elapsed {
iteration: 0,
timer: Timer::new(Duration::from_millis(2000), TimerMode::Repeating),
});
}
#[derive(Component, Resource)]
struct Elapsed {
iteration: usize,
timer: Timer,
}
fn send_commands_system(
mut webview_commands: WebviewEventWriter<WebviewCommand>,
time: Res<Time>,
mut elapsed: ResMut<Elapsed>,
) {
// events to send every elapsed.timer ticks
let events = [
WebviewCommand::Reload,
WebviewCommand::LoadHtml("<body style=\"background-color: yellow\">LoadHtml</body>".into()),
WebviewCommand::RunJavascript("document.body.innerHTML = 'Hello Javascript!';".into()),
WebviewCommand::LoadHtml(
"<body style=\"background-color: blue\">Blue background</body>".into(),
),
WebviewCommand::LoadUri("https://www.google.com/".into()),
];
if elapsed.timer.tick(time.delta()).just_finished() {
if let Some(event) = events.get(elapsed.iteration) {
// this command is sent to all webviews. There's also a method for sending to a specific entity-webview
webview_commands.send(event.clone());
}
elapsed.iteration += 1;
}
}