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
use crate::EventPump;
use crate::sys;
use super::{MouseButton, MouseButtonIterator, PressedMouseButtonIterator};
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct RelativeMouseState {
mouse_state: u32,
x: i32,
y: i32
}
impl RelativeMouseState {
pub fn new(_e: &EventPump) -> RelativeMouseState {
let mut x = 0;
let mut y = 0;
let mouse_state = unsafe {
sys::SDL_GetRelativeMouseState(&mut x, &mut y)
};
RelativeMouseState {
mouse_state,
x: x as i32,
y: y as i32
}
}
pub fn from_sdl_state(state: u32) -> RelativeMouseState {
RelativeMouseState { mouse_state : state, x: 0, y: 0 }
}
pub fn to_sdl_state(&self) -> u32 {
self.mouse_state
}
fn button_mask(&self, button: u32) -> u32 {
1 << (button - 1)
}
pub fn left(&self) -> bool { (self.mouse_state & self.button_mask(sys::SDL_BUTTON_LEFT)) != 0 }
pub fn middle(&self) -> bool { (self.mouse_state & self.button_mask(sys::SDL_BUTTON_MIDDLE)) != 0 }
pub fn right(&self) -> bool { (self.mouse_state & self.button_mask(sys::SDL_BUTTON_RIGHT)) != 0 }
pub fn x1(&self) -> bool { (self.mouse_state & self.button_mask(sys::SDL_BUTTON_X1)) != 0 }
pub fn x2(&self) -> bool { (self.mouse_state & self.button_mask(sys::SDL_BUTTON_X2)) != 0 }
pub fn x(&self) -> i32 { self.x }
pub fn y(&self) -> i32 { self.y }
pub fn is_mouse_button_pressed(&self, mouse_button: MouseButton) -> bool {
let mask = 1 << ((mouse_button as u32)-1);
self.mouse_state & mask != 0
}
pub fn mouse_buttons(&self) -> MouseButtonIterator {
MouseButtonIterator {
cur_button: 1,
mouse_state: &self.mouse_state
}
}
pub fn pressed_mouse_buttons(&self) -> PressedMouseButtonIterator {
PressedMouseButtonIterator {
iter: self.mouse_buttons()
}
}
}