nesbox_utils/
input.rs

1use bevy::prelude::*;
2use wasm_bindgen::prelude::*;
3
4#[wasm_bindgen]
5#[derive(Copy, Hash, Clone, PartialEq, Eq, Debug, Default)]
6pub enum Player {
7    #[default]
8    One,
9    Two,
10    Three,
11    Four,
12}
13
14#[derive(Debug, Clone, Copy, PartialEq)]
15pub struct MouseEvent {
16    pub player: Player,
17    pub delta: Vec2,
18    pub position: Vec2,
19}
20
21#[wasm_bindgen]
22#[derive(Copy, Hash, Clone, PartialEq, Eq, Debug)]
23pub enum Button {
24    Poweroff,
25    Reset,
26    Select,
27    Start,
28
29    JoypadA,
30    JoypadB,
31    JoypadTurboA,
32    JoypadTurboB,
33    JoypadUp,
34    JoypadDown,
35    JoypadLeft,
36    JoypadRight,
37    PointerPrimary,
38    PointerSecondary,
39}
40
41#[derive(Debug, Clone, Resource, Default)]
42pub struct ButtonInput {
43    inputs: [Input<Button>; 4],
44}
45
46impl ButtonInput {
47    pub fn get_input(&self, player: Player) -> &Input<Button> {
48        match player {
49            Player::One => &self.inputs[0],
50            Player::Two => &self.inputs[1],
51            Player::Three => &self.inputs[2],
52            Player::Four => &self.inputs[3],
53        }
54    }
55
56    pub fn get_input_mut(&mut self, player: Player) -> &mut Input<Button> {
57        match player {
58            Player::One => &mut self.inputs[0],
59            Player::Two => &mut self.inputs[1],
60            Player::Three => &mut self.inputs[2],
61            Player::Four => &mut self.inputs[3],
62        }
63    }
64
65    pub fn clear(&mut self) {
66        for input in self.inputs.iter_mut() {
67            input.clear();
68        }
69    }
70}