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
use crate::context::Context;
use crate::error::GameError;
use crate::error::GameResult;
use crate::graphics;
use crate::graphics::Point2;
use std::collections::HashMap;
use winit::dpi;
pub use winit::event::MouseButton;
pub use winit::window::CursorIcon;
#[derive(Clone, Debug)]
pub struct MouseContext {
last_position: Point2,
last_delta: Point2,
delta: Point2,
buttons_pressed: HashMap<MouseButton, bool>,
cursor_type: CursorIcon,
cursor_grabbed: bool,
cursor_hidden: bool,
}
impl MouseContext {
pub(crate) fn new() -> Self {
Self {
last_position: Point2::ZERO,
last_delta: Point2::ZERO,
delta: Point2::ZERO,
cursor_type: CursorIcon::Default,
buttons_pressed: HashMap::new(),
cursor_grabbed: false,
cursor_hidden: false,
}
}
pub(crate) fn set_last_position(&mut self, p: Point2) {
self.last_position = p;
}
pub(crate) fn set_last_delta(&mut self, p: Point2) {
self.last_delta = p;
}
pub fn reset_delta(&mut self) {
self.delta = Point2::ZERO;
}
pub(crate) fn set_delta(&mut self, p: Point2) {
self.delta = p;
}
pub(crate) fn set_button(&mut self, button: MouseButton, pressed: bool) {
let _ = self.buttons_pressed.insert(button, pressed);
}
fn button_pressed(&self, button: MouseButton) -> bool {
*(self.buttons_pressed.get(&button).unwrap_or(&false))
}
}
impl Default for MouseContext {
fn default() -> Self {
Self::new()
}
}
pub fn cursor_type(ctx: &Context) -> CursorIcon {
ctx.mouse_context.cursor_type
}
pub fn set_cursor_type(ctx: &mut Context, cursor_type: CursorIcon) {
ctx.mouse_context.cursor_type = cursor_type;
graphics::window(ctx).set_cursor_icon(cursor_type);
}
pub fn cursor_grabbed(ctx: &Context) -> bool {
ctx.mouse_context.cursor_grabbed
}
pub fn set_cursor_grabbed(ctx: &mut Context, grabbed: bool) -> GameResult<()> {
ctx.mouse_context.cursor_grabbed = grabbed;
graphics::window(ctx)
.set_cursor_grab(grabbed)
.map_err(|e| GameError::WindowError(e.to_string()))
}
pub fn cursor_hidden(ctx: &Context) -> bool {
ctx.mouse_context.cursor_hidden
}
pub fn set_cursor_hidden(ctx: &mut Context, hidden: bool) {
ctx.mouse_context.cursor_hidden = hidden;
graphics::window(ctx).set_cursor_visible(!hidden)
}
pub fn position(ctx: &Context) -> mint::Point2<f32> {
ctx.mouse_context.last_position.into()
}
pub fn set_position<P>(ctx: &mut Context, point: P) -> GameResult<()>
where
P: Into<mint::Point2<f32>>,
{
let mintpoint = point.into();
ctx.mouse_context.last_position = Point2::from(mintpoint);
graphics::window(ctx)
.set_cursor_position(dpi::LogicalPosition {
x: f64::from(mintpoint.x),
y: f64::from(mintpoint.y),
})
.map_err(|_| GameError::WindowError("Couldn't set mouse cursor position!".to_owned()))
}
pub fn delta(ctx: &Context) -> mint::Point2<f32> {
ctx.mouse_context.delta.into()
}
pub(crate) fn last_delta(ctx: &Context) -> mint::Point2<f32> {
ctx.mouse_context.last_delta.into()
}
pub fn button_pressed(ctx: &Context, button: MouseButton) -> bool {
ctx.mouse_context.button_pressed(button)
}