Skip to content

Commit

Permalink
Merge pull request #577 from MyreMylar/fix-are-contents-hovered
Browse files Browse the repository at this point in the history
Fix are_contents_hovered() method
  • Loading branch information
MyreMylar authored Apr 12, 2024
2 parents ed456fe + 8e1691d commit bf81485
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 8 deletions.
6 changes: 5 additions & 1 deletion pygame_gui/core/ui_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,11 @@ def are_contents_hovered(self) -> bool:
"""
any_hovered = False
for element in self:
any_hovered = any(sub_element.hovered for sub_element in element.get_focus_set())
if any(sub_element.hovered for sub_element in element.get_focus_set()):
any_hovered = True
elif isinstance(element, IContainerLikeInterface):
any_hovered = element.are_contents_hovered()

if any_hovered:
break
return any_hovered
6 changes: 5 additions & 1 deletion pygame_gui/elements/ui_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,11 @@ def are_contents_hovered(self) -> bool:
"""
any_hovered = False
for element in self:
any_hovered = any(sub_element.hovered for sub_element in element.get_focus_set())
if any(sub_element.hovered for sub_element in element.get_focus_set()):
any_hovered = True
elif isinstance(element, IContainerLikeInterface):
any_hovered = element.are_contents_hovered()

if any_hovered:
break
return any_hovered
11 changes: 10 additions & 1 deletion pygame_gui/elements/ui_scrolling_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def __init__(self,
self.scrolling_bottom = 0
self.scrolling_right = 0
self._calculate_scrolling_dimensions()
self._sort_out_element_container_scroll_bars()

def get_container(self) -> IUIContainerInterface:
"""
Expand Down Expand Up @@ -402,6 +403,7 @@ def _sort_out_element_container_scroll_bars(self):
need_horiz_scroll_bar, need_vert_scroll_bar = self._check_scroll_bars()

if need_vert_scroll_bar:
self.vert_scroll_bar.enable()
# It is important to subtract scroll_bar_height as horiz scroll bar's dimensions are evaluated after.
# If horizontal scrollbar is not needed, then scroll bar height is 0.
vis_percent = (self._view_container.rect.height - self.scroll_bar_height) / self.scrolling_height
Expand All @@ -421,6 +423,7 @@ def _sort_out_element_container_scroll_bars(self):
self._remove_vert_scrollbar()

if need_horiz_scroll_bar:
self.horiz_scroll_bar.enable()
vis_percent = self._view_container.rect.width / self.scrolling_width
start_percent = ((self._view_container.rect.left -
self.scrollable_container.rect.left)
Expand Down Expand Up @@ -471,6 +474,7 @@ def _remove_vert_scrollbar(self):
self.scroll_bar_width = 0
self.vert_scroll_bar.set_dimensions((0, self._view_container.rect.height))
self.vert_scroll_bar.set_relative_position((0, 0))
self.vert_scroll_bar.disable()
self._calculate_scrolling_dimensions()

if self.horiz_scroll_bar is not None:
Expand All @@ -485,6 +489,7 @@ def _remove_horiz_scrollbar(self):
self.scroll_bar_height = 0
self.horiz_scroll_bar.set_dimensions((self._view_container.rect.width, 0))
self.horiz_scroll_bar.set_relative_position((0, 0))
self.horiz_scroll_bar.disable()
self._calculate_scrolling_dimensions()

if self.vert_scroll_bar is not None:
Expand Down Expand Up @@ -554,7 +559,11 @@ def are_contents_hovered(self) -> bool:
"""
any_hovered = False
for element in self:
any_hovered = any(sub_element.hovered for sub_element in element.get_focus_set())
if any(sub_element.hovered for sub_element in element.get_focus_set()):
any_hovered = True
elif isinstance(element, IContainerLikeInterface):
any_hovered = element.are_contents_hovered()

if any_hovered:
break
return any_hovered
6 changes: 5 additions & 1 deletion pygame_gui/elements/ui_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,11 @@ def are_contents_hovered(self) -> bool:
"""
any_hovered = False
for element in self:
any_hovered = any(sub_element.hovered for sub_element in element.get_focus_set())
if any(sub_element.hovered for sub_element in element.get_focus_set()):
any_hovered = True
elif isinstance(element, IContainerLikeInterface):
any_hovered = element.are_contents_hovered()

if any_hovered:
break
return any_hovered
14 changes: 14 additions & 0 deletions tests/test_core/test_ui_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,20 @@ def test_are_contents_hovered(self, _init_pygame, default_ui_manager: IUIManage

assert container.are_contents_hovered()

def test_are_nested_contents_hovered(self, _init_pygame, default_ui_manager: IUIManagerInterface,
_display_surface_return_none):
manager = UIManager((800, 600))
container = UIContainer(pygame.Rect(100, 100, 250, 300), manager=manager)

container_2 = UIContainer(pygame.Rect(10, 10, 230, 280), manager=manager, container=container)

button = UIButton(relative_rect=pygame.Rect(50, 50, 50, 50), text="1",
manager=manager, container=container_2)
manager.mouse_position = button.rect.center
button.check_hover(0.1, False)

assert container.are_contents_hovered()


if __name__ == '__main__':
pytest.console_main()
20 changes: 20 additions & 0 deletions tests/test_elements/test_ui_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from pygame_gui.core.interfaces import IUIManagerInterface
from pygame_gui.core.ui_container import UIContainer
from pygame_gui.elements.ui_panel import UIPanel
from pygame_gui.elements.ui_scrolling_container import UIScrollingContainer
from pygame_gui.elements.ui_button import UIButton
from pygame_gui.elements.ui_text_box import UITextBox
from pygame_gui.ui_manager import UIManager


Expand Down Expand Up @@ -630,6 +632,24 @@ def test_are_contents_hovered(self, _init_pygame, default_ui_manager: IUIManage

assert container.are_contents_hovered()

def test_are_nested_contents_hovered(self, _init_pygame, default_ui_manager: IUIManagerInterface,
_display_surface_return_none):
manager = UIManager((800, 600))
panel = UIPanel(pygame.Rect(100, 100, 250, 300), manager=manager)

container_2 = UIScrollingContainer(pygame.Rect(10, 10, 230, 280), manager=manager, container=panel)

nested_text_box = UITextBox(html_text="Some text inside a scrolling text box, itself"
" inside a container that scrolls inside "
" another container. ",
relative_rect=pygame.Rect(10, 10, 180, 200),
container=container_2,
manager=manager)
manager.mouse_position = nested_text_box.rect.center
nested_text_box.check_hover(0.1, False)

assert panel.are_contents_hovered()


if __name__ == '__main__':
pytest.console_main()
23 changes: 23 additions & 0 deletions tests/test_elements/test_ui_scrolling_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,29 @@ def test_does_not_scroll_while_hovering_scrolling_contents(self, _init_pygame, d

assert not container.vert_scroll_bar.scroll_wheel_moved

def test_scroll_while_hovering_nested_contents(self, _init_pygame, default_ui_manager: IUIManagerInterface,
_display_surface_return_none):
manager = UIManager((800, 600))
container = UIScrollingContainer(pygame.Rect(100, 100, 250, 300), manager=manager)
container.set_scrollable_area_dimensions((250, 600))

container_2 = UIScrollingContainer(pygame.Rect(10, 10, 230, 280), manager=manager, container=container,
should_grow_automatically=False)

text_box_inside_two_scrolling_containers = UITextBox(html_text="Some text",
relative_rect=pygame.Rect(10, 10, 210, 260),
container=container_2,
manager=manager)
manager.mouse_position = text_box_inside_two_scrolling_containers.rect.center
text_box_inside_two_scrolling_containers.check_hover(0.1, False)

assert container.are_contents_hovered()
assert container.vert_scroll_bar is not None

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

assert container.vert_scroll_bar.scroll_wheel_moved


if __name__ == '__main__':
pytest.console_main()
28 changes: 24 additions & 4 deletions tests/test_elements/test_ui_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from tests.shared_comparators import compare_surfaces

from pygame_gui.elements.ui_window import UIWindow
from pygame_gui.elements.ui_scrolling_container import UIScrollingContainer
from pygame_gui.elements.ui_button import UIButton
from pygame_gui.elements.ui_text_box import UITextBox
from pygame_gui.core.interfaces import IUIManagerInterface
from pygame_gui import UIManager

Expand Down Expand Up @@ -702,16 +704,34 @@ def test_iteration(self, _init_pygame, default_ui_manager: IUIManagerInterface,
def test_are_contents_hovered(self, _init_pygame, default_ui_manager: IUIManagerInterface,
_display_surface_return_none):
manager = UIManager((800, 600))
container = UIWindow(pygame.Rect(100, 100, 200, 200), manager=manager)
window = UIWindow(pygame.Rect(100, 100, 200, 200), manager=manager)
button_1 = UIButton(relative_rect=pygame.Rect(50, 50, 50, 50), text="1",
manager=manager, container=container)
manager=manager, container=window)
button_2 = UIButton(relative_rect=pygame.Rect(125, 50, 50, 50), text="2",
manager=manager, container=container)
manager=manager, container=window)
manager.mouse_position = button_1.rect.center
button_1.check_hover(0.1, False)
button_2.check_hover(0.1, False)

assert container.are_contents_hovered()
assert window.are_contents_hovered()

def test_are_nested_contents_hovered(self, _init_pygame, default_ui_manager: IUIManagerInterface,
_display_surface_return_none):
manager = UIManager((800, 600))
window = UIWindow(pygame.Rect(100, 100, 250, 300), manager=manager)

container_2 = UIScrollingContainer(pygame.Rect(10, 10, 230, 280), manager=manager, container=window)

nested_text_box = UITextBox(html_text="Some text inside a scrolling text box, itself"
" inside a container that scrolls inside "
" another container. ",
relative_rect=pygame.Rect(10, 10, 180, 200),
container=container_2,
manager=manager)
manager.mouse_position = nested_text_box.rect.center
nested_text_box.check_hover(0.1, False)

assert window.are_contents_hovered()


if __name__ == '__main__':
Expand Down

0 comments on commit bf81485

Please sign in to comment.