From 035206b0b835f214405cc6bf687c00e5fd4700dd Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Sun, 3 Nov 2024 15:13:41 +0000 Subject: [PATCH] Add option to disable hiding/showing contained elements of containers --- pygame_gui/core/ui_container.py | 22 ++++++++++++------- pygame_gui/elements/ui_panel.py | 12 ++++++---- pygame_gui/elements/ui_scrolling_container.py | 12 ++++++---- pygame_gui/elements/ui_tab_container.py | 13 +++++++---- pygame_gui/elements/ui_window.py | 10 ++++++--- 5 files changed, 46 insertions(+), 23 deletions(-) diff --git a/pygame_gui/core/ui_container.py b/pygame_gui/core/ui_container.py index 67a4ffc1..2dbfc3a7 100644 --- a/pygame_gui/core/ui_container.py +++ b/pygame_gui/core/ui_container.py @@ -352,29 +352,35 @@ def enable(self): for element in self.elements: element.enable() - def show(self): + def show(self, show_contents: bool = True): """ Shows the container, which means the container will get drawn and will process events. Should also show all the children elements and containers. If the container was visible before - ignore. + + :param show_contents: whether to also show the contents of the container. Defaults to True. """ if not self.visible: self.visible = True - for element in self.elements: - if hasattr(element, 'show'): - element.show() + if show_contents: + for element in self.elements: + if hasattr(element, 'show'): + element.show() - def hide(self): + def hide(self, hide_contents: bool = True): """ Hides the container, which means the container will not get drawn and will not process events. Should also hide all the children elements and containers. If the container was hidden before - ignore. + + :param hide_contents: whether to also hide the contents of the container. Defaults to True. """ if self.visible: - for element in self.elements: - if hasattr(element, 'hide'): - element.hide() + if hide_contents: + for element in self.elements: + if hasattr(element, 'hide'): + element.hide() self.visible = False diff --git a/pygame_gui/elements/ui_panel.py b/pygame_gui/elements/ui_panel.py index 2dbfcef1..842d6d67 100644 --- a/pygame_gui/elements/ui_panel.py +++ b/pygame_gui/elements/ui_panel.py @@ -287,23 +287,27 @@ def enable(self): if self.panel_container is not None: self.panel_container.enable() - def show(self): + def show(self, show_contents: bool = True): """ In addition to the base UIElement.show() - call show() of owned container - panel_container. + + :param show_contents: whether to also show the contents of the panel. Defaults to True. """ super().show() if self.panel_container is not None: - self.panel_container.show() + self.panel_container.show(show_contents) - def hide(self): + def hide(self, hide_contents: bool = True): """ In addition to the base UIElement.hide() - call hide() of owned container - panel_container. + + :param hide_contents: whether to also hide the contents of the panel. Defaults to True. """ if not self.visible: return if self.panel_container is not None: - self.panel_container.hide() + self.panel_container.hide(hide_contents) super().hide() def __iter__(self) -> Iterator[IUIElementInterface]: diff --git a/pygame_gui/elements/ui_scrolling_container.py b/pygame_gui/elements/ui_scrolling_container.py index dd05dae8..381242d3 100644 --- a/pygame_gui/elements/ui_scrolling_container.py +++ b/pygame_gui/elements/ui_scrolling_container.py @@ -513,29 +513,33 @@ def enable(self): if self._root_container is not None: self._root_container.enable() - def show(self): + def show(self, show_contents: bool = True): """ In addition to the base UIElement.show() - call show() of owned container - _root_container. All other sub-elements (view_container, scrollbars) are children of _root_container, so it's visibility will propagate to them - there is no need to call their show() methods separately. + + :param show_contents: whether to also show the contents of the container. Defaults to True. """ super().show() if self._root_container is not None: - self._root_container.show() + self._root_container.show(show_contents) - def hide(self): + def hide(self, hide_contents: bool = True): """ In addition to the base UIElement.hide() - call hide() of owned container - _root_container. All other sub-elements (view_container, scrollbars) are children of _root_container, so it's visibility will propagate to them - there is no need to call their hide() methods separately. + + :param hide_contents: whether to also hide the contents of the container. Defaults to True. """ if not self.visible: return if self._root_container is not None: - self._root_container.hide() + self._root_container.hide(hide_contents) super().hide() def __iter__(self) -> Iterator[IUIElementInterface]: diff --git a/pygame_gui/elements/ui_tab_container.py b/pygame_gui/elements/ui_tab_container.py index d1fa4350..305b62c9 100644 --- a/pygame_gui/elements/ui_tab_container.py +++ b/pygame_gui/elements/ui_tab_container.py @@ -170,25 +170,30 @@ def enable(self): if not self.is_enabled: self._root_container.enable() - def show(self): + def show(self, show_contents: bool = True): """ In addition to the base UIElement.show() - show the _window_root_container which will propagate and show all the children. + + :param show_contents: whether to also show the contents of the tab container. Defaults to True. + """ super().show() - self._root_container.show() + self._root_container.show(show_contents) - def hide(self): + def hide(self, hide_contents: bool = True): """ In addition to the base UIElement.hide() - hide the _window_root_container which will propagate and hide all the children. + + :param hide_contents: whether to also hide the contents of the tab container. Defaults to True. """ if not self.visible: return super().hide() if self._root_container is not None: - self._root_container.hide() + self._root_container.hide(hide_contents) def set_dimensions(self, dimensions: Coordinate, clamp_to_container: bool = False): """ diff --git a/pygame_gui/elements/ui_window.py b/pygame_gui/elements/ui_window.py index 993e178e..6da4711b 100644 --- a/pygame_gui/elements/ui_window.py +++ b/pygame_gui/elements/ui_window.py @@ -750,26 +750,30 @@ def enable(self): if self._window_root_container is not None: self._window_root_container.enable() - def show(self): + def show(self, show_contents: bool = True): """ In addition to the base UIElement.show() - show the _window_root_container which will propagate and show all the children. + + :param show_contents: whether to also show the contents of the window. Defaults to True. """ super().show() if self._window_root_container is not None: self._window_root_container.show() - def hide(self): + def hide(self, hide_contents: bool = True): """ In addition to the base UIElement.hide() - hide the _window_root_container which will propagate and hide all the children. + + :param hide_contents: whether to also hide the contents of the window. Defaults to True. """ if not self.visible: return super().hide() if self._window_root_container is not None: - self._window_root_container.hide() + self._window_root_container.hide(hide_contents) def get_relative_mouse_pos(self): """