generated from Leafwing-Studios/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 122
/
clash_handling.rs
62 lines (52 loc) · 1.8 KB
/
clash_handling.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
//! Clashes occur when two actions would be triggered by the same combination of buttons
//! and one input is a strict subset of the other.
//!
//! See [`ClashStrategy`] for more details.
use bevy::prelude::*;
use leafwing_input_manager::clashing_inputs::ClashStrategy;
use leafwing_input_manager::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(InputManagerPlugin::<TestAction>::default())
.add_systems(Startup, spawn_input_map)
.add_systems(Update, report_pressed_actions)
// Change the value of this resource to change how clashes should be handled in your game
.insert_resource(ClashStrategy::PrioritizeLongest)
.run()
}
#[derive(Actionlike, Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect)]
enum TestAction {
One,
Two,
Three,
OneAndTwo,
OneAndThree,
TwoAndThree,
OneAndTwoAndThree,
}
fn spawn_input_map(mut commands: Commands) {
use KeyCode::*;
use TestAction::*;
let mut input_map = InputMap::default();
// Setting up input mappings in the obvious way
input_map.insert_multiple([(Key1, One), (Key2, Two), (Key3, Three)]);
input_map.insert_chord([Key1, Key2], OneAndTwo);
input_map.insert_chord([Key1, Key3], OneAndThree);
input_map.insert_chord([Key2, Key3], TwoAndThree);
input_map.insert_chord([Key1, Key2, Key3], OneAndTwoAndThree);
commands.spawn(InputManagerBundle {
input_map,
..Default::default()
});
}
fn report_pressed_actions(
query: Query<&ActionState<TestAction>, Changed<ActionState<TestAction>>>,
) {
let action_state = query.single();
for action in TestAction::variants() {
if action_state.just_pressed(action) {
dbg!(action);
}
}
}