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