good_web_game/input/
mouse.rs1use 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 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
56pub fn position(ctx: &Context) -> mint::Point2<f32> {
58 ctx.mouse_context.mouse_position().into()
59}
60
61pub 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
70pub fn delta(ctx: &Context) -> mint::Point2<f32> {
72 ctx.mouse_context.delta.into()
73}
74
75pub fn last_position(ctx: &Context) -> mint::Point2<f32> {
77 ctx.mouse_context.last_position.into()
78}
79
80pub fn cursor_grabbed(ctx: &Context) -> bool {
82 ctx.mouse_context.cursor_grabbed
83}
84
85pub 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
95pub fn cursor_type(ctx: &Context) -> miniquad::CursorIcon {
97 ctx.mouse_context.cursor_type
98}
99
100pub 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
110pub fn cursor_hidden(ctx: &Context) -> bool {
112 ctx.mouse_context.cursor_hidden
113}
114
115pub 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}