|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from unittest.mock import MagicMock |
| 4 | + |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +import scenex as snx |
| 8 | +from scenex.app.events import MouseButton, MouseMoveEvent |
| 9 | +from scenex.utils import projections |
| 10 | + |
| 11 | + |
| 12 | +def test_events() -> None: |
| 13 | + # Create a view with an image |
| 14 | + img = snx.Image(data=np.ones((10, 10), dtype=np.uint8), interactive=True) |
| 15 | + img_filter = MagicMock() |
| 16 | + img.set_event_filter(img_filter) |
| 17 | + |
| 18 | + view = snx.View(scene=snx.Scene(children=[img])) |
| 19 | + view_filter = MagicMock() |
| 20 | + view_filter.return_value = False |
| 21 | + view.set_event_filter(view_filter) |
| 22 | + |
| 23 | + # Set up the camera |
| 24 | + # Such that the image is in the top right quadrant |
| 25 | + view.camera.transform = snx.Transform().translated((-0.5, -0.5)) |
| 26 | + view.camera.projection = projections.orthographic(1, 1, 1) |
| 27 | + |
| 28 | + # Put it on a canvas |
| 29 | + canvas = snx.Canvas(width=int(view.layout.width), height=int(view.layout.height)) |
| 30 | + canvas.views.append(view) |
| 31 | + |
| 32 | + # Mouse over that image in the top right corner |
| 33 | + canvas_pos = (view.layout.width, 0) |
| 34 | + world_ray = canvas.to_world(canvas_pos) |
| 35 | + assert world_ray is not None |
| 36 | + event = MouseMoveEvent( |
| 37 | + canvas_pos=canvas_pos, world_ray=world_ray, buttons=MouseButton.NONE |
| 38 | + ) |
| 39 | + |
| 40 | + # And show both the view and the image saw the event |
| 41 | + canvas.handle(event) |
| 42 | + view_filter.assert_called_once_with(event) |
| 43 | + img_filter.assert_called_once_with(event, img) |
| 44 | + |
| 45 | + # Reset the mocks |
| 46 | + img_filter.reset_mock() |
| 47 | + view_filter.reset_mock() |
| 48 | + |
| 49 | + # Mouse over empty space in the top left corner |
| 50 | + canvas_pos = (0, 0) |
| 51 | + world_ray = canvas.to_world(canvas_pos) |
| 52 | + assert world_ray is not None |
| 53 | + event = MouseMoveEvent( |
| 54 | + canvas_pos=canvas_pos, world_ray=world_ray, buttons=MouseButton.NONE |
| 55 | + ) |
| 56 | + |
| 57 | + # And show that the image did not see the event |
| 58 | + # but that the view still saw the event |
| 59 | + canvas.handle(event) |
| 60 | + img_filter.assert_not_called() |
| 61 | + view_filter.assert_called_once_with(event) |
0 commit comments