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() {
|
|
|
|
App::build()
|
2020-09-19 03:11:26 +00:00
|
|
|
.add_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
|
2020-12-16 05:57:16 +00:00
|
|
|
.add_startup_system(hello_wasm_system.system())
|
2020-09-19 03:11:26 +00:00
|
|
|
// Track ticks (sanity check, whether game loop is running)
|
2020-12-16 05:57:16 +00:00
|
|
|
.add_system(counter.system())
|
2020-09-19 03:11:26 +00:00
|
|
|
// Track input events
|
|
|
|
.init_resource::<TrackInputState>()
|
2020-12-16 05:57:16 +00:00
|
|
|
.add_system(track_input_events.system())
|
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
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct TrackInputState {
|
|
|
|
keys: EventReader<KeyboardInput>,
|
|
|
|
cursor: EventReader<CursorMoved>,
|
|
|
|
motion: EventReader<MouseMotion>,
|
|
|
|
mousebtn: EventReader<MouseButtonInput>,
|
|
|
|
scroll: EventReader<MouseWheel>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn track_input_events(
|
|
|
|
mut state: ResMut<TrackInputState>,
|
|
|
|
ev_keys: Res<Events<KeyboardInput>>,
|
|
|
|
ev_cursor: Res<Events<CursorMoved>>,
|
|
|
|
ev_motion: Res<Events<MouseMotion>>,
|
|
|
|
ev_mousebtn: Res<Events<MouseButtonInput>>,
|
|
|
|
ev_scroll: Res<Events<MouseWheel>>,
|
|
|
|
) {
|
|
|
|
// Keyboard input
|
|
|
|
for ev in state.keys.iter(&ev_keys) {
|
|
|
|
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)
|
|
|
|
for ev in state.cursor.iter(&ev_cursor) {
|
2020-11-13 01:23:57 +00:00
|
|
|
info!("Cursor at: {}", ev.position);
|
2020-09-19 03:11:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Relative mouse motion
|
|
|
|
for ev in state.motion.iter(&ev_motion) {
|
2020-11-13 01:23:57 +00:00
|
|
|
info!("Mouse moved {} pixels", ev.delta);
|
2020-09-19 03:11:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mouse buttons
|
|
|
|
for ev in state.mousebtn.iter(&ev_mousebtn) {
|
|
|
|
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.)
|
|
|
|
for ev in state.scroll.iter(&ev_scroll) {
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|