bevy/examples/wasm/winit_wasm.rs

90 lines
2.2 KiB
Rust
Raw Normal View History

use bevy::{
input::{
keyboard::KeyboardInput,
mouse::{MouseButtonInput, MouseMotion, MouseWheel},
},
prelude::*,
};
fn main() {
2021-04-11 20:13:07 +00:00
App::new()
.insert_resource(WindowDescriptor {
width: 300.,
height: 300.,
..Default::default()
})
.add_plugins(DefaultPlugins)
// One time greet
.add_startup_system(hello_wasm_system)
// Track ticks (sanity check, whether game loop is running)
.add_system(counter)
// Track input events
.add_system(track_input_events)
.run();
}
fn hello_wasm_system() {
2020-11-13 01:23:57 +00:00
info!("hello wasm");
}
fn counter(mut state: Local<CounterState>, time: Res<Time>) {
if state.count % 60 == 0 {
2020-11-13 01:23:57 +00:00
info!(
"tick {} @ {:?} [Δ{}]",
state.count,
time.time_since_startup(),
time.delta_seconds()
);
}
state.count += 1;
}
#[derive(Default)]
struct CounterState {
count: u32,
}
fn track_input_events(
mut ev_keys: EventReader<KeyboardInput>,
mut ev_cursor: EventReader<CursorMoved>,
mut ev_motion: EventReader<MouseMotion>,
mut ev_mousebtn: EventReader<MouseButtonInput>,
mut ev_scroll: EventReader<MouseWheel>,
) {
// Keyboard input
for ev in ev_keys.iter() {
if ev.state.is_pressed() {
2020-11-13 01:23:57 +00:00
info!("Just pressed key: {:?}", ev.key_code);
} else {
2020-11-13 01:23:57 +00:00
info!("Just released key: {:?}", ev.key_code);
}
}
// Absolute cursor position (in window coordinates)
for ev in ev_cursor.iter() {
2020-11-13 01:23:57 +00:00
info!("Cursor at: {}", ev.position);
}
// Relative mouse motion
for ev in ev_motion.iter() {
2020-11-13 01:23:57 +00:00
info!("Mouse moved {} pixels", ev.delta);
}
// Mouse buttons
for ev in ev_mousebtn.iter() {
if ev.state.is_pressed() {
2020-11-13 01:23:57 +00:00
info!("Just pressed mouse button: {:?}", ev.button);
} else {
2020-11-13 01:23:57 +00:00
info!("Just released mouse button: {:?}", ev.button);
}
}
// scrolling (mouse wheel, touchpad, etc.)
for ev in ev_scroll.iter() {
2020-11-13 01:23:57 +00:00
info!(
"Scrolled vertically by {} and horizontally by {}.",
2020-11-13 01:23:57 +00:00
ev.y, ev.x
);
}
}