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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use crate::{GamepadAxis, Kurinji};

use bevy::prelude::*;
use bevy::app::{EventReader, Events};
use bevy::ecs::{Local, Res, ResMut};
use bevy::input::Input;

#[derive(Default)]
pub struct GamepadState {
    reader: EventReader<GamepadEvent>,
}

impl Kurinji {
    // publics
    // buttons
    pub fn bind_gamepad_button_pressed(
        &mut self,
        pad_button: GamepadButtonType,
        action: &str,
    ) -> &mut Kurinji {
        self.bind_gamepad_button_pressed_for_player(0, pad_button, action)
    }
    pub fn bind_gamepad_button_pressed_for_player(
        &mut self,
        player: usize,
        pad_button: GamepadButtonType,
        action: &str,
    ) -> &mut Kurinji {
        *self
            .joystick_button_binding
            .entry((player, pad_button))
            .or_default() = action.to_string();
        self
    }
    pub fn unbind_gamepad_button_pressed(
        &mut self,
        pad_button: GamepadButtonType,
    ) -> &mut Kurinji {
        self.unbind_gamepad_button_pressed_for_player(0, pad_button)
    }
    pub fn unbind_gamepad_button_pressed_for_player(
        &mut self,
        player: usize,
        pad_button: GamepadButtonType,
    ) -> &mut Kurinji {
        self.joystick_button_binding.remove(&(player, pad_button));
        self
    }

    // axis
    pub fn bind_gamepad_axis(&mut self, axis: GamepadAxis, action: &str) -> &mut Kurinji {
        self.bind_gamepad_axis_for_player(0, axis, action)
    }

    pub fn bind_gamepad_axis_for_player(
        &mut self,
        player: usize,
        axis: GamepadAxis,
        action: &str,
    ) -> &mut Kurinji {
        *self
            .joystick_axis_binding
            .entry((player, axis))
            .or_default() = action.to_string();
        self
    }

    pub fn unbind_gamepad_axis(&mut self, pad_axis: GamepadAxis) -> &mut Kurinji {
        self.unbind_gamepad_axis_for_player(0, pad_axis);
        self
    }

    pub fn unbind_gamepad_axis_for_player(
        &mut self,
        player: usize,
        axis: GamepadAxis,
    ) -> &mut Kurinji {
        self.joystick_axis_binding.remove(&(player, axis));
        self
    }

    // crates
    pub(crate) fn get_available_player_handle(self) -> Option<usize> {
        for i in 0..(Kurinji::MAX_PLAYER_HANDLES - 1) {
            if !self.player_handles_in_use.contains(&i) {
                return Some(i);
            }
        }
        None
    }
    pub(crate) fn get_player_handle_for_gamepad(self, pad: Gamepad) -> Option<usize> {
        return match self.joystick_to_player_map.get(&pad) {
            Some(a) => Some(*a),
            _ => None,
        };
    }
    pub(crate) fn get_gamepad_from_player_handle(self, player: usize) -> Option<Gamepad> {
        return match self.player_to_joystick_map.get(&player) {
            Some(a) => Some(*a),
            _ => None,
        };
    }
    // systems
    pub(crate) fn gamepad_button_press_input_system(
        mut input_map: ResMut<Kurinji>,
        joystick_button_input: Res<Input<GamepadButton>>,
    ) {
        let button_bindings_iter = input_map.joystick_button_binding.clone();
        for (player_button_bind, action) in button_bindings_iter.iter() {
            if joystick_button_input.pressed(GamepadButton(
                Gamepad(player_button_bind.0),
                player_button_bind.1,
            )) {
                input_map.set_raw_action_strength(action, 1.0);
            }
        }
    }
    pub(crate) fn gamepad_connection_event_system(
        mut input_map: ResMut<Kurinji>,
        gamepad_event: Res<Events<GamepadEvent>>,
        mut state: Local<GamepadState>,
    ) {
        if let Some(value) = state.reader.latest(&gamepad_event) {
            let pad_handle = value.0;
            match value.1 {
                bevy::prelude::GamepadEventType::Connected => {
                    let res_player_handle = input_map.clone().get_available_player_handle();
                    match res_player_handle {
                        Some(player_handle) => {
                            println!(
                                "InputMap: Gamepad Connected {:?} to player {}",
                                pad_handle, player_handle
                            );
                            input_map.player_handles_in_use.insert(player_handle);
                            input_map
                                .joystick_to_player_map
                                .insert(pad_handle, player_handle);
                            input_map
                                .player_to_joystick_map
                                .insert(player_handle, pad_handle);
                        }
                        None => {
                            println!("InputMap: Housefull. No space for more gamepads");
                        }
                    }
                }
                bevy::prelude::GamepadEventType::Disconnected => {
                    let opt_player_handle =
                        input_map.clone().get_player_handle_for_gamepad(pad_handle);
                    if let Some(player_handle) = opt_player_handle {
                        println!(
                            "InputMap: Gamepad Disconnected {:?} for player {}",
                            pad_handle, player_handle
                        );
                        input_map.player_handles_in_use.remove(&player_handle);
                        input_map.joystick_to_player_map.remove(&pad_handle);
                        input_map.player_to_joystick_map.remove(&player_handle);
                    }
                }
                _ => ()
            }
        }
    }

    pub(crate) fn gamepad_axis_system(
        mut input_map: ResMut<Kurinji>,
        pad_axis: Res<bevy::input::Axis<bevy::input::gamepad::GamepadAxis>>,
    ) {
        for (k, v) in input_map.clone().joystick_axis_binding.iter() {
            let player = k.0;
            let axis = k.1.clone();
            let is_positive = Kurinji::is_gamepad_axis_positive(axis.clone());

            if let Some(bevy_gamepad) = input_map.clone().get_gamepad_from_player_handle(player) {
                let bevy_axis_type = Kurinji::get_bevy_gamepad_axis_type_from_pad_axis(axis);
                let bevy_axis = bevy::input::gamepad::GamepadAxis(bevy_gamepad, bevy_axis_type);

                let signed_str = pad_axis.get(bevy_axis).unwrap_or(0.);

                if signed_str > 0. && is_positive || signed_str < 0. && !is_positive {
                    input_map.set_raw_action_strength(&v.to_string(), signed_str.abs());
                }
            }
        }
    }
}