Glfw mouse device handler. Handles the mouse input in a thread-safe way, forwarding the events to the registered callbacks. Attributes: on_move: Observable subject triggered when a mouse move is detected. Expects a callback with signature (position, translation). on_click: Obse
| 90 | |
| 91 | |
| 92 | class GlfwMouse(base.InputEventsProcessor): |
| 93 | """Glfw mouse device handler. |
| 94 | |
| 95 | Handles the mouse input in a thread-safe way, forwarding the events to the |
| 96 | registered callbacks. |
| 97 | |
| 98 | Attributes: |
| 99 | on_move: Observable subject triggered when a mouse move is detected. |
| 100 | Expects a callback with signature (position, translation). |
| 101 | on_click: Observable subject triggered when a mouse click is detected. |
| 102 | Expects a callback with signature (button, action, modifiers). |
| 103 | on_double_click: Observable subject triggered when a mouse double click is |
| 104 | detected. Expects a callback with signature (button, modifiers). |
| 105 | on_scroll: Observable subject triggered when a mouse scroll is detected. |
| 106 | Expects a callback with signature (scroll_value). |
| 107 | """ |
| 108 | |
| 109 | def __init__(self, context): |
| 110 | super().__init__() |
| 111 | self.on_move = util.QuietSet() |
| 112 | self.on_click = util.QuietSet() |
| 113 | self.on_double_click = util.QuietSet() |
| 114 | self.on_scroll = util.QuietSet() |
| 115 | self._double_click_detector = base.DoubleClickDetector() |
| 116 | with context.make_current() as ctx: |
| 117 | framebuffer_width, window_width = ctx.call( |
| 118 | self._glfw_setup, context.window) |
| 119 | |
| 120 | self._scale = framebuffer_width * 1.0 / window_width |
| 121 | self._last_mouse_pos = np.zeros(2, int) |
| 122 | |
| 123 | self._double_clicks = {} |
| 124 | |
| 125 | def _glfw_setup(self, window): |
| 126 | glfw.set_cursor_pos_callback(window, self._handle_move) |
| 127 | glfw.set_mouse_button_callback(window, self._handle_button) |
| 128 | glfw.set_scroll_callback(window, self._handle_scroll) |
| 129 | framebuffer_width, _ = glfw.get_framebuffer_size(window) |
| 130 | window_width, _ = glfw.get_window_size(window) |
| 131 | return framebuffer_width, window_width |
| 132 | |
| 133 | @property |
| 134 | def position(self): |
| 135 | return self._last_mouse_pos |
| 136 | |
| 137 | def _handle_move(self, window, x, y): |
| 138 | """Mouse movement callback. |
| 139 | |
| 140 | Args: |
| 141 | window: Window object from glfw. |
| 142 | x: Horizontal position of mouse, in pixels. |
| 143 | y: Vertical position of mouse, in pixels. |
| 144 | """ |
| 145 | del window |
| 146 | position = np.array([x, y], int) * self._scale |
| 147 | delta = position - self._last_mouse_pos |
| 148 | self._last_mouse_pos = position |
| 149 | self.add_event(self.on_move, position, delta) |
no outgoing calls
no test coverage detected
searching dependent graphs…