Skip to content

Commit

Permalink
Add option to disable hiding/showing contained elements of containers
Browse files Browse the repository at this point in the history
  • Loading branch information
MyreMylar committed Nov 3, 2024
1 parent f568612 commit 035206b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 23 deletions.
22 changes: 14 additions & 8 deletions pygame_gui/core/ui_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 8 additions & 4 deletions pygame_gui/elements/ui_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
12 changes: 8 additions & 4 deletions pygame_gui/elements/ui_scrolling_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
13 changes: 9 additions & 4 deletions pygame_gui/elements/ui_tab_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
10 changes: 7 additions & 3 deletions pygame_gui/elements/ui_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down

0 comments on commit 035206b

Please sign in to comment.