From bfc3748d00ba199e948a90b96721f1805d7cc376 Mon Sep 17 00:00:00 2001 From: Dan Lawrence Date: Sat, 22 Apr 2023 17:52:38 +0100 Subject: [PATCH] improve handling of showing/hiding for more elements --- pygame_gui/elements/ui_drop_down_menu.py | 17 ++++++++++------- pygame_gui/elements/ui_horizontal_scroll_bar.py | 4 ++-- pygame_gui/elements/ui_horizontal_slider.py | 7 ++++--- pygame_gui/elements/ui_panel.py | 11 +++++++---- pygame_gui/elements/ui_scrolling_container.py | 10 +++++++--- pygame_gui/elements/ui_selection_list.py | 4 ++-- pygame_gui/elements/ui_vertical_scroll_bar.py | 4 ++-- 7 files changed, 34 insertions(+), 23 deletions(-) diff --git a/pygame_gui/elements/ui_drop_down_menu.py b/pygame_gui/elements/ui_drop_down_menu.py index c30bd480..fd26ee2c 100644 --- a/pygame_gui/elements/ui_drop_down_menu.py +++ b/pygame_gui/elements/ui_drop_down_menu.py @@ -666,7 +666,10 @@ def __init__(self, *, expand_on_option_click: bool = True ): - + # Need to move some declarations early as they are indirectly referenced via the ui element + # constructor + self.menu_states = None + self.current_state = None super().__init__(relative_rect, manager, container=container, starting_height=0, anchors=anchors, @@ -971,8 +974,8 @@ def show(self): showing it's buttons. """ super().show() - - self.menu_states['closed'].show() + if self.current_state is not None and self.menu_states is not None: + self.menu_states['closed'].show() def hide(self): """ @@ -981,7 +984,7 @@ def hide(self): call the hide() method of the 'closed' state which hides all it's children widgets. """ super().hide() - - if self.current_state == self.menu_states['expanded']: - self.menu_states['expanded'].hide() - self.menu_states['closed'].hide() + if self.current_state is not None and self.menu_states is not None: + if self.current_state == self.menu_states['expanded']: + self.menu_states['expanded'].hide() + self.menu_states['closed'].hide() diff --git a/pygame_gui/elements/ui_horizontal_scroll_bar.py b/pygame_gui/elements/ui_horizontal_scroll_bar.py index 9978f80f..7eb596db 100644 --- a/pygame_gui/elements/ui_horizontal_scroll_bar.py +++ b/pygame_gui/elements/ui_horizontal_scroll_bar.py @@ -578,8 +578,8 @@ def show(self): will propagate and show all the buttons. """ super().show() - - self.button_container.show() + if self.button_container is not None: + self.button_container.show() def hide(self): """ diff --git a/pygame_gui/elements/ui_horizontal_slider.py b/pygame_gui/elements/ui_horizontal_slider.py index 482607f3..d2273d97 100644 --- a/pygame_gui/elements/ui_horizontal_slider.py +++ b/pygame_gui/elements/ui_horizontal_slider.py @@ -44,7 +44,8 @@ def __init__(self, visible: int = 1, click_increment: Union[float, int] = 1 ): - + # Need to move some declarations early as they are indirectly referenced via the ui element + # constructor self.sliding_button = None self.button_container = None super().__init__(relative_rect, manager, container, @@ -560,8 +561,8 @@ def show(self): the button_container which will propagate and show the left and right buttons. """ super().show() - - self.sliding_button.show() + if self.sliding_button is not None: + self.sliding_button.show() if self.button_container is not None: self.button_container.show() diff --git a/pygame_gui/elements/ui_panel.py b/pygame_gui/elements/ui_panel.py index 03eb2b21..e7780cee 100644 --- a/pygame_gui/elements/ui_panel.py +++ b/pygame_gui/elements/ui_panel.py @@ -53,7 +53,9 @@ def __init__(self, anchors: Optional[Dict[str, Union[str, UIElement]]] = None, visible: int = 1 ): - + # Need to move some declarations early as they are indirectly referenced via the ui element + # constructor + self.panel_container = None super().__init__(relative_rect, manager, container, @@ -300,12 +302,13 @@ def show(self): In addition to the base UIElement.show() - call show() of owned container - panel_container. """ super().show() - - self.panel_container.show() + if self.panel_container is not None: + self.panel_container.show() def hide(self): """ In addition to the base UIElement.hide() - call hide() of owned container - panel_container. """ - self.panel_container.hide() + if self.panel_container is not None: + self.panel_container.hide() super().hide() diff --git a/pygame_gui/elements/ui_scrolling_container.py b/pygame_gui/elements/ui_scrolling_container.py index c0f43c2b..03294de3 100644 --- a/pygame_gui/elements/ui_scrolling_container.py +++ b/pygame_gui/elements/ui_scrolling_container.py @@ -41,7 +41,9 @@ def __init__(self, object_id: Optional[Union[ObjectID, str]] = None, anchors: Optional[Dict[str, Union[str, UIElement]]] = None, visible: int = 1): - + # Need to move some declarations early as they are indirectly referenced via the ui element + # constructor + self._root_container = None super().__init__(relative_rect, manager, container, @@ -439,7 +441,8 @@ def show(self): separately. """ super().show() - self._root_container.show() + if self._root_container is not None: + self._root_container.show() def hide(self): """ @@ -448,5 +451,6 @@ def hide(self): it's visibility will propagate to them - there is no need to call their hide() methods separately. """ - self._root_container.hide() + if self._root_container is not None: + self._root_container.hide() super().hide() diff --git a/pygame_gui/elements/ui_selection_list.py b/pygame_gui/elements/ui_selection_list.py index e61d94aa..d6f28ad6 100644 --- a/pygame_gui/elements/ui_selection_list.py +++ b/pygame_gui/elements/ui_selection_list.py @@ -710,8 +710,8 @@ def show(self): there is no need to call their show() methods separately. """ super().show() - - self.list_and_scroll_bar_container.show() + if self.list_and_scroll_bar_container is not None: + self.list_and_scroll_bar_container.show() def hide(self): """ diff --git a/pygame_gui/elements/ui_vertical_scroll_bar.py b/pygame_gui/elements/ui_vertical_scroll_bar.py index 46266350..3bfd611f 100644 --- a/pygame_gui/elements/ui_vertical_scroll_bar.py +++ b/pygame_gui/elements/ui_vertical_scroll_bar.py @@ -567,8 +567,8 @@ def show(self): propagate and show all the buttons. """ super().show() - - self.button_container.show() + if self.button_container is not None: + self.button_container.show() def hide(self): """