simple_game_engine/input/mouse.rs
1//! Types related to the state of the mouse.
2
3use sdl2::mouse::MouseState as SdlMouseState;
4
5use super::{ButtonState, MouseButton};
6
7/// The cursor position and state of the mouse buttons.
8pub struct MouseState {
9 /// The state of every SDL2 supported mouse button.
10 pub buttons: ButtonState<MouseButton>,
11 /// *X* coordinate of the mouse cursor.
12 pub x: i32,
13 /// *Y* coordinate of the mouse cursor.
14 pub y: i32,
15}
16
17impl MouseState {
18 /// Create an initial state from an `sdl2::mouse::MouseState`. Called internally by the engine.
19 pub(crate) fn new(state: SdlMouseState) -> Self {
20 Self {
21 buttons: ButtonState::new(state.mouse_buttons()),
22 x: state.x(),
23 y: state.y(),
24 }
25 }
26
27 /// Update the existing state from the current `sdl2::mouse::MouseState`. This is called
28 /// internally by the engine on every frame.
29 pub(crate) fn update(&mut self, state: SdlMouseState) {
30 self.buttons.update(state.mouse_buttons());
31 self.x = state.x();
32 self.y = state.y();
33 }
34}