konnektoren_bevy/input/
plugin.rs

1use super::{components::*, device::AvailableInputDevices, systems::*};
2use bevy::prelude::*;
3
4/// Main input plugin that provides all input functionality
5pub struct InputPlugin;
6
7impl Plugin for InputPlugin {
8    fn build(&self, app: &mut App) {
9        app
10            // Initialize resources
11            .init_resource::<AvailableInputDevices>()
12            .init_resource::<InputDeviceAssignment>()
13            .init_resource::<InputSettings>()
14            // Register types for reflection
15            .register_type::<InputController>()
16            .register_type::<PlayerInputMapping>()
17            .register_type::<InputDeviceAssignment>()
18            .register_type::<InputSettings>()
19            // Add events
20            .add_event::<InputEvent>()
21            // Add core input systems
22            .add_systems(
23                Update,
24                (
25                    detect_gamepads,
26                    auto_assign_devices,
27                    update_player_mappings,
28                    handle_keyboard_input,
29                    handle_gamepad_input,
30                    clear_input_states,
31                )
32                    .chain(),
33            );
34
35        info!("InputPlugin loaded");
36    }
37}
38
39/// Helper trait for easy input controller setup
40pub trait InputControllerExt {
41    /// Spawn an input controller for a player
42    fn spawn_input_controller(&mut self, player_id: u32) -> Entity;
43
44    /// Spawn multiple input controllers
45    fn spawn_input_controllers(&mut self, player_count: u32) -> Vec<Entity>;
46}
47
48impl InputControllerExt for Commands<'_, '_> {
49    fn spawn_input_controller(&mut self, player_id: u32) -> Entity {
50        self.spawn((
51            Name::new(format!("Input Controller P{}", player_id + 1)),
52            InputController::new(player_id),
53            PlayerInputMapping::new(player_id),
54        ))
55        .id()
56    }
57
58    fn spawn_input_controllers(&mut self, player_count: u32) -> Vec<Entity> {
59        (0..player_count)
60            .map(|player_id| self.spawn_input_controller(player_id))
61            .collect()
62    }
63}