Skip to content

Commit dfac385

Browse files
authored
feat: move input logic to standalone plugin (#77)
1 parent 684fa06 commit dfac385

File tree

8 files changed

+212
-75
lines changed

8 files changed

+212
-75
lines changed

src/dev_tools.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
11
use bevy::dev_tools::fps_overlay::FpsOverlayPlugin;
22
use bevy::dev_tools::states::log_transitions;
3-
use bevy::input::common_conditions::{input_just_pressed, input_toggle_active};
43
use bevy::prelude::*;
54
use bevy_inspector_egui::bevy_egui::EguiPlugin;
65
use bevy_inspector_egui::quick::WorldInspectorPlugin;
76

7+
use crate::input::input_map::{Action, action_just_pressed};
88
use crate::screens::Screen;
99

10-
const TOGGLE_KEY: KeyCode = KeyCode::Backquote;
11-
1210
pub fn plugin(app: &mut App) {
1311
app.add_plugins((
1412
FpsOverlayPlugin::default(),
1513
EguiPlugin::default(),
16-
WorldInspectorPlugin::new().run_if(input_toggle_active(false, TOGGLE_KEY)),
14+
WorldInspectorPlugin::new().run_if(in_state(DebugMode(true))),
1715
));
1816

1917
app.add_systems(Update, log_transitions::<Screen>);
2018

19+
app.init_state::<DebugMode>();
20+
2121
app.add_systems(
2222
Update,
23-
toggle_debug_ui.run_if(input_just_pressed(TOGGLE_KEY)),
23+
toggle_debug_mode.run_if(action_just_pressed(Action::DebugMode)),
2424
);
25+
26+
app.add_systems(Update, sync_ui_debug.run_if(state_changed::<DebugMode>));
27+
}
28+
29+
#[derive(States, PartialEq, Eq, Debug, Hash, Reflect, Clone, Default)]
30+
pub struct DebugMode(pub bool);
31+
32+
fn toggle_debug_mode(
33+
debug_mode: Res<State<DebugMode>>,
34+
mut next_debug_mode: ResMut<NextState<DebugMode>>,
35+
) {
36+
next_debug_mode.set(DebugMode(!debug_mode.0));
2537
}
2638

27-
fn toggle_debug_ui(mut options: ResMut<UiDebugOptions>) {
28-
options.toggle();
39+
fn sync_ui_debug(mut options: ResMut<UiDebugOptions>, debug_mode: Res<State<DebugMode>>) {
40+
options.enabled = debug_mode.0;
2941
}

src/gameplay/hud/hotbar.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
use bevy::{
22
ecs::{spawn::SpawnIter, system::SystemParam},
3-
input::keyboard::KeyboardInput,
43
prelude::*,
54
};
65
use bevy_aseprite_ultra::prelude::*;
76

87
use crate::{
98
gameplay::{FactorySystems, structure::assets::StructureDef, world::tilemap::TileClicked},
9+
input::input_map::{Action, InputActions},
1010
screens::Screen,
1111
};
1212

13-
const DIGIT_KEYS: [KeyCode; 9] = [
14-
KeyCode::Digit1,
15-
KeyCode::Digit2,
16-
KeyCode::Digit3,
17-
KeyCode::Digit4,
18-
KeyCode::Digit5,
19-
KeyCode::Digit6,
20-
KeyCode::Digit7,
21-
KeyCode::Digit8,
22-
KeyCode::Digit9,
13+
const HOTBAR_ACTIONS: [Action; 9] = [
14+
Action::Hotbar1,
15+
Action::Hotbar2,
16+
Action::Hotbar3,
17+
Action::Hotbar4,
18+
Action::Hotbar5,
19+
Action::Hotbar6,
20+
Action::Hotbar7,
21+
Action::Hotbar8,
22+
Action::Hotbar9,
2323
];
2424

2525
pub fn plugin(app: &mut App) {
@@ -36,10 +36,7 @@ pub fn plugin(app: &mut App) {
3636

3737
app.add_systems(
3838
Update,
39-
(
40-
select_on_keyboard_shortcuts.run_if(on_message::<KeyboardInput>),
41-
highlight_selected_slot,
42-
),
39+
(select_on_keyboard_shortcuts, highlight_selected_slot),
4340
);
4441

4542
app.add_systems(
@@ -124,7 +121,7 @@ pub enum HotbarActionKind {
124121

125122
#[derive(Component, Reflect, Debug)]
126123
#[reflect(Component)]
127-
struct HotbarShortcut(KeyCode);
124+
struct HotbarShortcut(Action);
128125

129126
#[derive(Component, Reflect, Debug)]
130127
#[reflect(Component)]
@@ -147,7 +144,7 @@ fn spawn_hotbar(mut commands: Commands) {
147144
..default()
148145
},
149146
Pickable::IGNORE,
150-
Children::spawn(SpawnIter((0..DIGIT_KEYS.len()).map(|i| {
147+
Children::spawn(SpawnIter((0..HOTBAR_ACTIONS.len()).map(|i| {
151148
(
152149
Name::new(format!("Hotbar Slot {}", i + 1)),
153150
Node {
@@ -162,7 +159,7 @@ fn spawn_hotbar(mut commands: Commands) {
162159
Pickable::default(),
163160
BorderColor::all(Color::WHITE),
164161
HotbarSlot,
165-
HotbarShortcut(DIGIT_KEYS[i]),
162+
HotbarShortcut(HOTBAR_ACTIONS[i]),
166163
)
167164
}))),
168165
));
@@ -252,12 +249,12 @@ fn select_on_click(
252249
}
253250

254251
fn select_on_keyboard_shortcuts(
255-
keys: Res<ButtonInput<KeyCode>>,
256252
hotbar_slots: Query<(Entity, &HotbarShortcut), With<HotbarSlot>>,
253+
input_actions: Res<InputActions>,
257254
mut hotbar: HotbarSelector,
258255
) {
259256
for (entity, shortcut) in hotbar_slots {
260-
if keys.just_pressed(shortcut.0) {
257+
if input_actions.just_pressed.contains(&shortcut.0) {
261258
hotbar.select(Some(entity));
262259
}
263260
}

src/gameplay/hud/inspect/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
use bevy::{input::common_conditions::input_just_pressed, prelude::*};
1+
use bevy::prelude::*;
22

3-
use crate::gameplay::recipe::select::SelectedRecipe;
3+
use crate::{
4+
gameplay::recipe::select::SelectedRecipe,
5+
input::input_map::{Action, action_just_pressed},
6+
};
47

58
mod info;
69
mod select;
@@ -15,7 +18,7 @@ pub fn plugin(app: &mut App) {
1518

1619
app.add_systems(
1720
Update,
18-
close_menu.run_if(input_just_pressed(KeyCode::Escape)),
21+
close_menu.run_if(action_just_pressed(Action::Dismiss)),
1922
);
2023
}
2124

src/gameplay/hud/inventory/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use bevy::{
2-
input::common_conditions::input_just_pressed,
32
prelude::*,
43
ui::Checked,
54
ui_widgets::{RadioButton, RadioGroup, ValueChange, observe},
65
};
76

7+
use crate::input::input_map::{Action, action_just_pressed};
8+
89
pub mod items;
910
pub mod people;
1011

@@ -25,7 +26,7 @@ pub(super) fn plugin(app: &mut App) {
2526
app.add_systems(
2627
Update,
2728
(
28-
toggle_inventory_ui.run_if(input_just_pressed(KeyCode::KeyT)),
29+
toggle_inventory_ui.run_if(action_just_pressed(Action::OpenTome)),
2930
update_tab_color,
3031
update_entry_color,
3132
),

src/gameplay/structure/interactable.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use bevy::prelude::*;
22
use bevy_aseprite_ultra::prelude::*;
33

4-
use crate::assets::tracking::LoadResource;
5-
6-
const INTERACTABLE_BUTTON: KeyCode = KeyCode::KeyE;
4+
use crate::{
5+
assets::tracking::LoadResource,
6+
input::input_map::{Action, InputMap, action_just_pressed},
7+
};
78

89
pub fn plugin(app: &mut App) {
910
app.load_resource::<InteractionAssets>();
@@ -13,7 +14,10 @@ pub fn plugin(app: &mut App) {
1314
app.add_observer(on_hover);
1415
app.add_observer(on_leave);
1516

16-
app.add_systems(Update, on_interact_button_click);
17+
app.add_systems(
18+
Update,
19+
on_interact_button_click.run_if(action_just_pressed(Action::Interact)),
20+
);
1721
}
1822

1923
#[derive(Component, Reflect, Default)]
@@ -73,6 +77,7 @@ fn on_hover(
7377
mut commands: Commands,
7478
mut hovered_interactable: ResMut<HoveredInteractable>,
7579
interaction_assets: Res<InteractionAssets>,
80+
input_map: Res<InputMap>,
7681
) {
7782
if !interactables.contains(pointer_over.entity) {
7883
return;
@@ -85,7 +90,7 @@ fn on_hover(
8590
Transform::from_xyz(0.0, 0.0, 1.0),
8691
ChildOf(pointer_over.entity),
8792
ButtonIndicatorOf(pointer_over.entity),
88-
interaction_assets.sprite(INTERACTABLE_BUTTON),
93+
interaction_assets.sprite(*input_map.keymap.get(&Action::Interact).unwrap()),
8994
));
9095

9196
pointer_over.propagate(false);
@@ -111,14 +116,9 @@ fn on_leave(
111116
}
112117

113118
fn on_interact_button_click(
114-
keys: Res<ButtonInput<KeyCode>>,
115119
hovered_interactable: Res<HoveredInteractable>,
116120
mut commands: Commands,
117121
) {
118-
if !keys.just_pressed(INTERACTABLE_BUTTON) {
119-
return;
120-
}
121-
122122
let Some(interactable) = hovered_interactable.0 else {
123123
return;
124124
};

src/gameplay/world/demolition.rs

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use std::collections::HashSet;
22

3-
use bevy::{input::common_conditions::input_just_pressed, prelude::*};
3+
use bevy::prelude::*;
44

5-
use crate::gameplay::{FactorySystems, world::tilemap::coord::Coord};
5+
use crate::{
6+
gameplay::{FactorySystems, world::tilemap::coord::Coord},
7+
input::input_map::{Action, InputActions, action_just_pressed},
8+
};
69

7-
pub const DEMOLISH_BUTTON: KeyCode = KeyCode::KeyF;
8-
pub const DEMOLISH_CANCEL_BUTTON: KeyCode = KeyCode::Escape;
910
pub const DEMOLISH_DURATION_SECS: f32 = 1.0;
1011

1112
pub(super) fn plugin(app: &mut App) {
@@ -14,20 +15,21 @@ pub(super) fn plugin(app: &mut App) {
1415

1516
app.add_message::<Demolished>();
1617

18+
app.add_observer(add_to_selection);
19+
app.add_observer(remove_from_selection);
20+
1721
app.add_systems(
18-
FixedUpdate,
22+
Update,
1923
(
20-
add_to_selection.run_if(on_message::<Pointer<Over>>),
21-
remove_from_selection.run_if(on_message::<Pointer<Out>>),
22-
clear_selection.run_if(input_just_pressed(DEMOLISH_CANCEL_BUTTON)),
24+
clear_selection.run_if(action_just_pressed(Action::Dismiss)),
25+
tick_demolish_timer,
2326
)
24-
.in_set(FactorySystems::Demolish),
27+
.chain(),
2528
);
2629

2730
app.add_systems(
2831
FixedUpdate,
2932
(
30-
tick_demolish_timer,
3133
highlight_demolition,
3234
demolish_selection.run_if(demolish_timer_finished),
3335
)
@@ -63,48 +65,40 @@ impl Default for DemolishTimer {
6365

6466
fn tick_demolish_timer(
6567
mut timer: ResMut<DemolishTimer>,
66-
keys: Res<ButtonInput<KeyCode>>,
6768
time: Res<Time>,
69+
input_actions: Res<InputActions>,
6870
) {
69-
if keys.just_released(DEMOLISH_BUTTON) {
70-
timer.reset();
71-
}
72-
73-
if keys.pressed(DEMOLISH_BUTTON) {
71+
if input_actions.pressed.contains(&Action::Demolish) {
7472
timer.tick(time.delta());
73+
} else {
74+
timer.reset();
7575
}
7676
}
7777

7878
fn demolish_timer_finished(timer: Res<DemolishTimer>) -> bool {
79-
timer.just_finished()
79+
timer.is_finished()
8080
}
8181

8282
fn add_to_selection(
83-
mut pointer_overs: MessageReader<Pointer<Over>>,
84-
mut selection: ResMut<DemolishSelection>,
83+
pointer_over: On<Pointer<Over>>,
8584
demolishables: Query<Entity, With<Demolishable>>,
85+
mut selection: ResMut<DemolishSelection>,
8686
) {
87-
for pointer_over in pointer_overs.read() {
88-
if !demolishables.contains(pointer_over.entity) {
89-
continue;
90-
}
91-
87+
if demolishables.contains(pointer_over.entity) {
9288
selection.insert(pointer_over.entity);
9389
}
9490
}
9591

9692
fn remove_from_selection(
97-
mut pointer_outs: MessageReader<Pointer<Out>>,
93+
pointer_out: On<Pointer<Out>>,
94+
input_actions: Res<InputActions>,
9895
mut selection: ResMut<DemolishSelection>,
99-
keys: Res<ButtonInput<KeyCode>>,
10096
) {
101-
for pointer_out in pointer_outs.read() {
102-
if keys.pressed(KeyCode::ShiftLeft) {
103-
continue;
104-
}
105-
106-
selection.remove(&pointer_out.entity);
97+
if input_actions.pressed.contains(&Action::MultiSelect) {
98+
return;
10799
}
100+
101+
selection.remove(&pointer_out.entity);
108102
}
109103

110104
fn clear_selection(mut selection: ResMut<DemolishSelection>) {

0 commit comments

Comments
 (0)