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
187
use crate::get_context;
use crate::prelude::screen_height;
use crate::prelude::screen_width;
use crate::Vec2;
pub use miniquad::{KeyCode, MouseButton};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TouchPhase {
Started,
Stationary,
Moved,
Ended,
Cancelled,
}
impl From<miniquad::TouchPhase> for TouchPhase {
fn from(miniquad_phase: miniquad::TouchPhase) -> TouchPhase {
match miniquad_phase {
miniquad::TouchPhase::Started => TouchPhase::Started,
miniquad::TouchPhase::Moved => TouchPhase::Moved,
miniquad::TouchPhase::Ended => TouchPhase::Ended,
miniquad::TouchPhase::Cancelled => TouchPhase::Cancelled,
}
}
}
#[derive(Clone, Debug)]
pub struct Touch {
pub id: u64,
pub phase: TouchPhase,
pub position: Vec2,
}
pub fn set_cursor_grab(grab: bool) {
let context = get_context();
context.cursor_grabbed = grab;
context.quad_context.set_cursor_grab(grab);
}
pub fn show_mouse(shown: bool) {
let context = get_context();
context.quad_context.show_mouse(shown);
}
pub fn mouse_position() -> (f32, f32) {
let context = get_context();
(context.mouse_position.x, context.mouse_position.y)
}
pub fn mouse_position_local() -> Vec2 {
let (pixels_x, pixels_y) = mouse_position();
convert_to_local(Vec2::new(pixels_x, pixels_y))
}
pub fn is_simulating_mouse_with_touch() -> bool {
get_context().simulate_mouse_with_touch
}
pub fn simulate_mouse_with_touch(option: bool) {
get_context().simulate_mouse_with_touch = option;
}
pub fn touches() -> Vec<Touch> {
get_context().touches.values().cloned().collect()
}
pub fn touches_local() -> Vec<Touch> {
get_context()
.touches
.values()
.map(|touch| {
let mut touch = touch.clone();
touch.position = convert_to_local(touch.position);
touch
})
.collect()
}
pub fn mouse_wheel() -> (f32, f32) {
let context = get_context();
(context.mouse_wheel.x, context.mouse_wheel.y)
}
pub fn is_key_pressed(key_code: KeyCode) -> bool {
let context = get_context();
context.keys_pressed.contains(&key_code)
}
pub fn is_key_down(key_code: KeyCode) -> bool {
let context = get_context();
context.keys_down.contains(&key_code)
}
pub fn is_key_released(key_code: KeyCode) -> bool {
let context = get_context();
context.keys_released.contains(&key_code)
}
pub fn get_char_pressed() -> Option<char> {
let context = get_context();
context.chars_pressed_queue.pop()
}
pub fn get_last_key_pressed() -> Option<KeyCode> {
let context = get_context();
context.keys_pressed.iter().next().cloned()
}
pub fn is_mouse_button_down(btn: MouseButton) -> bool {
let context = get_context();
context.mouse_down.contains(&btn)
}
pub fn is_mouse_button_pressed(btn: MouseButton) -> bool {
let context = get_context();
context.mouse_pressed.contains(&btn)
}
pub fn is_mouse_button_released(btn: MouseButton) -> bool {
let context = get_context();
context.mouse_released.contains(&btn)
}
fn convert_to_local(pixel_pos: Vec2) -> Vec2 {
Vec2::new(pixel_pos.x / screen_width(), pixel_pos.y / screen_height()) * 2.0
- Vec2::new(1.0, 1.0)
}
#[doc(hidden)]
pub mod utils {
use crate::get_context;
pub fn register_input_subscriber() -> usize {
let context = get_context();
context.input_events.push(vec![]);
context.input_events.len() - 1
}
pub fn repeat_all_miniquad_input<T: miniquad::EventHandler>(t: &mut T, subscriber: usize) {
let context = get_context();
let mut ctx = &mut context.quad_context;
for event in &context.input_events[subscriber] {
event.repeat(&mut ctx, t);
}
context.input_events[subscriber].clear();
}
}