Skip to main content

kurinji/
mouse.rs

1use crate::{
2    MouseAxis,
3    Kurinji,
4    util::clamp_vec2,
5};
6use bevy::{
7    math::Vec2,
8    prelude::MouseButton,
9};
10use bevy::app::{
11    EventReader,
12    Events,
13};
14use bevy::ecs::{
15    Local,
16    Res,
17    ResMut,
18};
19use bevy::input::{
20    mouse::MouseMotion,
21    Input,
22};
23#[derive(Default)]
24pub struct MouseMoveState
25{
26    reader: EventReader<MouseMotion>,
27}
28impl Kurinji
29{
30    // publics
31    pub fn bind_mouse_button_pressed(&mut self,
32                                     code: MouseButton,
33                                     action: &str)
34                                     -> &mut Kurinji
35    {
36        self.mouse_button_binding.insert(code, action.to_string());
37        self
38    }
39
40    pub fn unbind_mouse_button_pressed(&mut self,
41                                       button: MouseButton)
42                                       -> &mut Kurinji
43    {
44        self.mouse_button_binding.remove(&button);
45        self
46    }
47
48    pub fn bind_mouse_motion(&mut self,
49                             axis: MouseAxis,
50                             action: &str)
51                             -> &mut Kurinji
52    {
53        self.mouse_move_binding.insert(axis, action.to_string());
54        self
55    }
56
57    pub fn unbind_mouse_motion(&mut self, axis: MouseAxis) -> &mut Kurinji
58    {
59        self.mouse_move_binding.remove(&axis);
60        self
61    }
62
63    // crates
64    // systems
65    pub(crate) fn mouse_button_press_input_system(mut input_map: ResMut<Kurinji>,
66                                                  mouse_button_input: Res<Input<MouseButton>>)
67    {
68        let button_bindings_iter = input_map.mouse_button_binding.clone();
69        for (button, action) in button_bindings_iter.iter()
70        {
71            if mouse_button_input.pressed(*button)
72            {
73                input_map.set_raw_action_strength(action, 1.0);
74            }
75        }
76    }
77
78    pub(crate) fn mouse_move_event_system(mut input_map: ResMut<Kurinji>,
79                                          mut state: Local<MouseMoveState>,
80                                          move_events: Res<Events<MouseMotion>>)
81    {
82        if let Some(value) = state.reader.latest(&move_events)
83        {
84            // normalises strength (with 10 px as max strength)
85            // i.e. -10 px = -1.0 and 10 px = 1.0
86            let min = -1. * Vec2::one();
87            let max = Vec2::one();
88            let normalised_vec = clamp_vec2(min, max, value.delta * 0.1);
89            let x = normalised_vec.x;
90            let y = normalised_vec.y;
91            // horizontal
92            if x > 0.0
93            {
94                if let Some(action) =
95                    input_map.mouse_move_binding.get(&MouseAxis::XPositive)
96                {
97                    let _action = action.clone();
98                    input_map.set_raw_action_strength(&_action, x);
99                }
100            }
101            if x < 0.0
102            {
103                if let Some(action) =
104                    input_map.mouse_move_binding.get(&MouseAxis::XNegative)
105                {
106                    let _action = action.clone();
107                    input_map.set_raw_action_strength(&_action, x.abs());
108                }
109            }
110            // vertical
111            if y > 0.0
112            {
113                if let Some(action) =
114                    input_map.mouse_move_binding.get(&MouseAxis::YPositive)
115                {
116                    let _action = action.clone();
117                    input_map.set_raw_action_strength(&_action, y);
118                }
119            }
120            if y < 0.0
121            {
122                if let Some(action) =
123                    input_map.mouse_move_binding.get(&MouseAxis::YNegative)
124                {
125                    let _action = action.clone();
126                    input_map.set_raw_action_strength(&_action, y.abs());
127                }
128            }
129        }
130    }
131}