geng_core/window/
cursor.rs

1use super::*;
2
3pub enum CursorType {
4    Default,
5    Pointer,
6    Drag,
7}
8
9impl Window {
10    pub fn set_cursor_type(&self, cursor_type: CursorType) {
11        #[cfg(target_arch = "wasm32")]
12        {
13            let cursor_type = match cursor_type {
14                CursorType::Default => "initial",
15                CursorType::Pointer => "pointer",
16                CursorType::Drag => "all-scroll",
17            };
18            // TODO: only canvas
19            web_sys::window()
20                .unwrap()
21                .document()
22                .unwrap()
23                .body()
24                .unwrap()
25                .style()
26                .set_property("cursor", cursor_type);
27        }
28        #[cfg(not(target_arch = "wasm32"))]
29        {
30            use glutin::window::CursorIcon as GC;
31            self.glutin_window
32                .window()
33                .set_cursor_icon(match cursor_type {
34                    CursorType::Default => GC::Default,
35                    CursorType::Pointer => GC::Hand,
36                    CursorType::Drag => GC::AllScroll,
37                });
38        };
39    }
40
41    pub fn set_cursor_position(&self, position: Vec2<f64>) {
42        #![allow(unused_variables)]
43        #[cfg(target_arch = "wasm32")]
44        unimplemented!();
45        #[cfg(not(target_arch = "wasm32"))]
46        self.glutin_window
47            .window()
48            .set_cursor_position(glutin::dpi::PhysicalPosition::new(position.x, position.y))
49            .expect("Failed to set cursor position");
50    }
51
52    pub fn cursor_position(&self) -> Vec2<f64> {
53        self.mouse_pos.get()
54    }
55}