-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbindings.rs
71 lines (65 loc) · 1.89 KB
/
bindings.rs
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
use dip::{
bevy::{
input::keyboard::KeyCode,
log::LogPlugin,
time::TimePlugin,
window::{WindowCloseRequested, WindowId},
},
prelude::*,
};
use leafwing_input_manager::prelude::*;
fn main() {
App::new()
.insert_non_send_resource(DesktopSettings::<NoRootProps> {
keyboard_event: true,
..Default::default()
})
.insert_resource(WindowDescriptor {
title: "Key Bindings".to_string(),
..Default::default()
})
.add_plugin(LogPlugin)
.add_plugin(TimePlugin)
.add_plugin(DesktopPlugin::<NoUiState, NoUiAction, NoAsyncAction>::new(
Root,
))
.add_plugin(InputManagerPlugin::<Action>::default())
.add_startup_system(setup)
.add_system(close_window)
.run();
}
#[allow(non_snake_case)]
fn Root(cx: Scope) -> Element {
cx.render(rsx! {
h1 { "Key Bindings Example" }
p { "💡 Press \"Ecs\" or \"Ctrl-C\" to close window. (TODO: You might need to click screen to focus.)" }
})
}
#[derive(Component)]
struct User;
#[derive(Actionlike, PartialEq, Eq, Clone, Copy, Hash, Debug)]
enum Action {
CloseWindow,
}
fn setup(mut commands: Commands) {
let mut input_map = InputMap::new([(KeyCode::Escape, Action::CloseWindow)]);
input_map.insert_chord([KeyCode::LControl, KeyCode::C], Action::CloseWindow);
commands
.spawn()
.insert(User)
.insert_bundle(InputManagerBundle::<Action> {
action_state: ActionState::default(),
input_map,
});
}
fn close_window(
query: Query<&ActionState<Action>, With<User>>,
mut events: EventWriter<WindowCloseRequested>,
) {
let action_state = query.single();
if action_state.just_pressed(Action::CloseWindow) {
events.send(WindowCloseRequested {
id: WindowId::primary(),
});
}
}