Skip to content

Commit

Permalink
Fix event handling order of unlayered elements
Browse files Browse the repository at this point in the history
fixes #609
  • Loading branch information
MyreMylar committed Nov 3, 2024
1 parent 0a8e9a0 commit bb7ef80
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
5 changes: 4 additions & 1 deletion pygame_gui/ui_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def process_events(self, event: pygame.event.Event):
sorted_layers = sorted(self.ui_group.layers(), reverse=True)
for layer in sorted_layers:
sprites_in_layer = self.ui_group.get_sprites_from_layer(layer)
sprites_in_layer.reverse()
if not sorting_consumed_event:
windows_in_layer = [window for window in sprites_in_layer
if 'window' in window.element_ids[-1]]
Expand Down Expand Up @@ -336,7 +337,9 @@ def _handle_hovering(self, time_delta):
hover_handled = False
sorted_layers = sorted(self.ui_group.layers(), reverse=True)
for layer in sorted_layers:
for ui_element in self.ui_group.get_sprites_from_layer(layer):
layer_elements = self.ui_group.get_sprites_from_layer(layer)
layer_elements.reverse()
for ui_element in layer_elements:
if ui_element.visible:
# Only check hover for visible elements - ignore hidden elements
# we need to check hover even after already found what we are hovering,
Expand Down
10 changes: 5 additions & 5 deletions tests/test_elements/test_ui_scrolling_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,13 @@ def test_are_contents_hovered(self, _init_pygame, default_ui_manager: IUIManage
assert container.are_contents_hovered()
assert container.vert_scroll_bar is not None

container.vert_scroll_bar.process_event(pygame.event.Event(pygame.MOUSEWHEEL, {'y': -0.5}))
container.vert_scroll_bar.process_event(pygame.event.Event(pygame.MOUSEWHEEL, {'y': -0.5, 'x': 0.0}))

assert container.vert_scroll_bar.scroll_wheel_moved

assert container.horiz_scroll_bar is not None

container.horiz_scroll_bar.process_event(pygame.event.Event(pygame.MOUSEWHEEL, {'x': -0.5}))
container.horiz_scroll_bar.process_event(pygame.event.Event(pygame.MOUSEWHEEL, {'x': -0.5, 'y': 0.0}))

assert container.horiz_scroll_bar.scroll_wheel_moved

Expand All @@ -465,7 +465,7 @@ def test_scrolls_while_hovering_non_scrolling_contents(self, _init_pygame, defa
assert container.are_contents_hovered()
assert container.vert_scroll_bar is not None

manager.process_events(pygame.event.Event(pygame.MOUSEWHEEL, {'y': -0.5}))
manager.process_events(pygame.event.Event(pygame.MOUSEWHEEL, {'y': -0.5, 'x': 0.0}))

assert container.vert_scroll_bar.scroll_wheel_moved

Expand All @@ -490,7 +490,7 @@ def test_does_not_scroll_while_hovering_scrolling_contents(self, _init_pygame, d
assert container.are_contents_hovered()
assert container.vert_scroll_bar is not None

manager.process_events(pygame.event.Event(pygame.MOUSEWHEEL, {'y': -0.5}))
manager.process_events(pygame.event.Event(pygame.MOUSEWHEEL, {'y': -0.5, 'x': 0.0}))

assert not container.vert_scroll_bar.scroll_wheel_moved

Expand All @@ -513,7 +513,7 @@ def test_scroll_while_hovering_nested_contents(self, _init_pygame, default_ui_ma
assert container.are_contents_hovered()
assert container.vert_scroll_bar is not None

manager.process_events(pygame.event.Event(pygame.MOUSEWHEEL, {'y': -0.5}))
manager.process_events(pygame.event.Event(pygame.MOUSEWHEEL, {'y': -0.5, 'x': 0.0}))

assert container.vert_scroll_bar.scroll_wheel_moved

Expand Down

0 comments on commit bb7ef80

Please sign in to comment.