1    module mouse
2    
3    type Capture @byval =
4        val x : u32
5        val y : u32
6        val relative_x : u32
7        val relative_y : u32
8        val control : Control
9    
10   type Mouse @[mut local] =
11       val presses = Stream<(MouseAction, Control)>.new
12       var obs capture : Option<Capture> @[mut internal] = None
13       var obs hover : Option<Control> = None
14       local obs position : (u32, u32) = (0, 0)
15       let position_to_control : u32 * u32 * (Control -> bool) -> Option<Control>
16       local tokens = List<Token>.new
17   
18       let arrow_cursor = Glfw.create_standard_cursor MouseShape/Arrow
19       let resize_ew_cursor = Glfw.create_standard_cursor MouseShape/ResizeEw
20       var shape @[mut private] = MouseShape/Arrow
21   
22       def set_cursor self (window : OsWindow) (shape : MouseShape) =
23           self.shape = shape
24           let cursor = case shape of
25               MouseShape/Arrow -> self.arrow_cursor
26               MouseShape/ResizeEw -> self.resize_ew_cursor
27   
28           Glfw.set_cursor window.window cursor
29   
30       def update_hovered =
31           let (x, y) = position
32           let maybe_control = position_to_control x y { _.is_receive_hover }
33           if maybe_control <> hover then
34               if hover ? Some control then
35                   control.is_hovered = false
36   
37               hover = maybe_control
38               if maybe_control ? Some control then
39                   control.is_hovered = true
40   
41       fun destruct =
42           for i = tokens.size - 1 downto 0 do
43               tokens[i].discard
44