good_web_game/input/
mouse.rs

1use super::input_handler::InputHandler;
2use crate::Context;
3
4use crate::graphics::Point2;
5pub use crate::input::input_handler::MouseButton;
6
7pub struct MouseContext {
8    pub(crate) input_handler: InputHandler,
9    last_position: Point2,
10    delta: Point2,
11    cursor_grabbed: bool,
12    cursor_hidden: bool,
13    cursor_type: miniquad::CursorIcon,
14}
15
16impl MouseContext {
17    pub fn new(input_handler: InputHandler) -> Self {
18        MouseContext {
19            input_handler,
20            last_position: Point2::new(0., 0.),
21            delta: Point2::new(0., 0.),
22            cursor_grabbed: false,
23            cursor_hidden: false,
24            cursor_type: miniquad::CursorIcon::Default,
25        }
26    }
27
28    pub(crate) fn set_last_position(&mut self, p: Point2) {
29        self.last_position = p;
30    }
31
32    /// Resets the value returned by [`mouse::delta`](fn.delta.html) to zero.
33    /// You shouldn't need to call this, except when you're running your own event loop.
34    /// In this case call it right at the end, after `draw` and `update` have finished.
35    pub fn reset_delta(&mut self) {
36        self.delta = Point2::new(0., 0.);
37    }
38
39    pub(crate) fn set_delta(&mut self, p: Point2) {
40        self.delta = p;
41    }
42
43    pub fn mouse_position(&self) -> cgmath::Point2<f32> {
44        self.input_handler.mouse_position
45    }
46
47    pub fn button_pressed(&self, button: MouseButton) -> bool {
48        self.input_handler.is_mouse_key_down(&button)
49    }
50
51    pub fn wheel(&self) -> f32 {
52        self.input_handler.wheel
53    }
54}
55
56/// The current mouse position in pixels.
57pub fn position(ctx: &Context) -> mint::Point2<f32> {
58    ctx.mouse_context.mouse_position().into()
59}
60
61/// Whether a certain mouse button is currently pressed.
62pub fn button_pressed(ctx: &Context, button: MouseButton) -> bool {
63    ctx.mouse_context.button_pressed(button)
64}
65
66pub fn wheel(ctx: &Context) -> f32 {
67    ctx.mouse_context.wheel()
68}
69
70/// Get the distance the cursor was moved during the current frame, in pixels.
71pub fn delta(ctx: &Context) -> mint::Point2<f32> {
72    ctx.mouse_context.delta.into()
73}
74
75/// The position the mouse had during the latest `mouse_motion_event`
76pub fn last_position(ctx: &Context) -> mint::Point2<f32> {
77    ctx.mouse_context.last_position.into()
78}
79
80/// Get whether or not the mouse is grabbed (confined to the window)
81pub fn cursor_grabbed(ctx: &Context) -> bool {
82    ctx.mouse_context.cursor_grabbed
83}
84
85/// Set whether or not the mouse is grabbed (confined to the window)
86pub fn set_cursor_grabbed(
87    ctx: &mut Context,
88    quad_ctx: &mut miniquad::GraphicsContext,
89    grabbed: bool,
90) {
91    ctx.mouse_context.cursor_grabbed = grabbed;
92    quad_ctx.set_cursor_grab(grabbed);
93}
94
95/// Returns the current mouse cursor type of the window.
96pub fn cursor_type(ctx: &Context) -> miniquad::CursorIcon {
97    ctx.mouse_context.cursor_type
98}
99
100/// Modifies the mouse cursor type of the window.
101pub fn set_cursor_type(
102    ctx: &mut Context,
103    quad_ctx: &mut miniquad::graphics::GraphicsContext,
104    cursor_type: miniquad::CursorIcon,
105) {
106    ctx.mouse_context.cursor_type = cursor_type;
107    quad_ctx.set_mouse_cursor(cursor_type);
108}
109
110/// Set whether or not the mouse is hidden (invisible)
111pub fn cursor_hidden(ctx: &Context) -> bool {
112    ctx.mouse_context.cursor_hidden
113}
114
115/// Set whether or not the mouse is hidden (invisible).
116pub fn set_cursor_hidden(
117    ctx: &mut Context,
118    quad_ctx: &mut miniquad::graphics::GraphicsContext,
119    hidden: bool,
120) {
121    ctx.mouse_context.cursor_hidden = hidden;
122    quad_ctx.show_mouse(!hidden);
123}