|
| 1 | +import arcade |
| 2 | +from arcade import SectionManager, Section |
| 3 | + |
| 4 | + |
| 5 | +class RecorderView(arcade.View): |
| 6 | + def __init__(self): |
| 7 | + super().__init__() |
| 8 | + |
| 9 | + self.section_manager = SectionManager(self) |
| 10 | + self.events = [] |
| 11 | + |
| 12 | + def on_mouse_enter(self, x: float, y: float): |
| 13 | + self.events.append("on_mouse_enter") |
| 14 | + |
| 15 | + def on_mouse_motion(self, x: float, y: float, dx: float, dy: float): |
| 16 | + self.events.append("on_mouse_motion") |
| 17 | + |
| 18 | + def on_mouse_leave(self, x: float, y: float): |
| 19 | + self.events.append("on_mouse_leave") |
| 20 | + |
| 21 | + def on_mouse_press(self, x: float, y: float, button: int, modifiers: int): |
| 22 | + self.events.append("on_mouse_press") |
| 23 | + |
| 24 | + def on_mouse_release(self, x: float, y: float, button: int, modifiers: int): |
| 25 | + self.events.append("on_mouse_release") |
| 26 | + |
| 27 | + def on_show_view(self) -> None: |
| 28 | + self.section_manager.enable() |
| 29 | + |
| 30 | + def on_hide_view(self) -> None: |
| 31 | + self.section_manager.disable() |
| 32 | + |
| 33 | + def on_update(self, delta_time: float): |
| 34 | + self.events.append("on_update") |
| 35 | + |
| 36 | + def on_draw(self): |
| 37 | + self.events.append("on_draw") |
| 38 | + |
| 39 | + |
| 40 | +class RecorderSection(Section): |
| 41 | + def __init__(self, *args, **kwargs): |
| 42 | + super().__init__(*args, **kwargs) |
| 43 | + self.events = [] |
| 44 | + |
| 45 | + def on_mouse_enter(self, x: float, y: float): |
| 46 | + self.events.append("on_mouse_enter") |
| 47 | + |
| 48 | + def on_mouse_leave(self, x: float, y: float): |
| 49 | + self.events.append("on_mouse_leave") |
| 50 | + |
| 51 | + def on_mouse_press(self, x: float, y: float, button: int, modifiers: int): |
| 52 | + self.events.append("on_mouse_press") |
| 53 | + |
| 54 | + def on_mouse_release(self, x: float, y: float, button: int, modifiers: int): |
| 55 | + self.events.append("on_mouse_release") |
| 56 | + |
| 57 | + def on_update(self, delta_time: float): |
| 58 | + self.events.append("on_update") |
| 59 | + |
| 60 | + def on_draw(self): |
| 61 | + self.events.append("on_draw") |
| 62 | + |
| 63 | + |
| 64 | +def test_section_manager_enable_event_handling(window): |
| 65 | + # GIVEN |
| 66 | + view = RecorderView() |
| 67 | + manager = view.section_manager |
| 68 | + |
| 69 | + # SETUP |
| 70 | + recorder_section = RecorderSection( |
| 71 | + *window.rect.lbwh, |
| 72 | + ) |
| 73 | + manager.add_section(section=recorder_section) |
| 74 | + window.show_view(view) |
| 75 | + |
| 76 | + # WHEN |
| 77 | + |
| 78 | + window.dispatch_event("on_mouse_motion", 10, 10, 0, 0) |
| 79 | + # mouse motion will trigger mouse enter automatically |
| 80 | + window.dispatch_event("on_mouse_press", 11, 11, arcade.MOUSE_BUTTON_LEFT, 0) |
| 81 | + window.dispatch_event("on_mouse_release", 11, 11, arcade.MOUSE_BUTTON_LEFT, 0) |
| 82 | + window.dispatch_event("on_draw") |
| 83 | + window.dispatch_event("on_update", 1 / 60) |
| 84 | + window.dispatch_event("on_mouse_leave", 10, 10) |
| 85 | + window.dispatch_events() |
| 86 | + |
| 87 | + # THEN |
| 88 | + assert recorder_section.events == [ |
| 89 | + "on_mouse_enter", |
| 90 | + "on_mouse_press", |
| 91 | + "on_mouse_release", |
| 92 | + "on_draw", |
| 93 | + "on_update", |
| 94 | + "on_mouse_leave", |
| 95 | + ] |
| 96 | + |
| 97 | + |
| 98 | +def test_view_receives_events_once(window): |
| 99 | + # GIVEN |
| 100 | + view = RecorderView() |
| 101 | + manager = view.section_manager |
| 102 | + |
| 103 | + # SETUP |
| 104 | + recorder_section = RecorderSection( |
| 105 | + *window.rect.lbwh, prevent_dispatch_view={False}, prevent_dispatch={True} |
| 106 | + ) |
| 107 | + manager.add_section(section=recorder_section) |
| 108 | + window.show_view(view) |
| 109 | + |
| 110 | + # WHEN |
| 111 | + window.dispatch_event("on_mouse_motion", 10, 10, 0, 0) |
| 112 | + window.dispatch_event("on_mouse_press", 10, 10, arcade.MOUSE_BUTTON_LEFT, 0) |
| 113 | + window.dispatch_event("on_mouse_release", 10, 10, arcade.MOUSE_BUTTON_LEFT, 0) |
| 114 | + window.dispatch_event("on_mouse_leave", 10, 10) |
| 115 | + window.dispatch_event("on_draw") |
| 116 | + window.dispatch_event("on_update", 1 / 60) |
| 117 | + window.dispatch_events() |
| 118 | + |
| 119 | + # THEN |
| 120 | + assert view.events == [ |
| 121 | + "on_mouse_enter", |
| 122 | + "on_mouse_motion", |
| 123 | + "on_mouse_press", |
| 124 | + "on_mouse_release", |
| 125 | + "on_mouse_leave", |
| 126 | + "on_draw", |
| 127 | + "on_update", |
| 128 | + ] |
0 commit comments