Skip to content

Commit

Permalink
Merge pull request #513 from LondonClass/_create_valid_ids-modification
Browse files Browse the repository at this point in the history
Create ID when creating elements
  • Loading branch information
MyreMylar authored Feb 15, 2024
2 parents 38291c9 + b33ec29 commit 8cbd51b
Show file tree
Hide file tree
Showing 22 changed files with 142 additions and 148 deletions.
14 changes: 8 additions & 6 deletions pygame_gui/core/ui_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,25 @@ def __init__(self,
container: Union[IContainerLikeInterface, None] = None,
parent_element: Union[UIElement, None] = None,
object_id: Union[ObjectID, str, None] = None,
element_id: Union[List[str], None] = None,
anchors: Union[Dict[str, str], None] = None,
visible: int = 1):

self.ui_manager = manager
self.is_window_root_container = is_window_root_container
self.elements = [] # type: List[IUIElementInterface]

if element_id is None:
element_id = ['container']

super().__init__(relative_rect, manager, container,
starting_height=starting_height,
layer_thickness=1,
anchors=anchors,
visible=visible)

self._create_valid_ids(container=container,
parent_element=parent_element,
object_id=object_id,
element_id='container')
visible=visible,
parent_element=parent_element,
object_id=object_id,
element_id=element_id)

self.sprite_group = self.ui_manager.get_sprite_group()
self._set_image(self.ui_manager.get_universal_empty_surface())
Expand Down
31 changes: 27 additions & 4 deletions pygame_gui/core/ui_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class UIElement(GUISprite, IUIElementInterface):
:param anchors: A dictionary describing what this element's relative_rect is relative to.
:param visible: Whether the element is visible by default. Warning - container visibility may
override this.
:param parent_element: Element that this element 'belongs to' in theming. Elements inherit
colours from parents.
:param object_id: An optional set of IDs to help distinguish this element from other elements.
:param element_id: A list of string ID representing this element's class.
"""
def __init__(self, relative_rect: Union[pygame.Rect, Tuple[int, int, int, int]],
manager: Optional[IUIManagerInterface],
Expand All @@ -40,7 +44,10 @@ def __init__(self, relative_rect: Union[pygame.Rect, Tuple[int, int, int, int]],
starting_height: int,
layer_thickness: int,
anchors: Dict[str, Union[str, 'UIElement']] = None,
visible: int = 1):
visible: int = 1,
parent_element: Union[None, IUIElementInterface] = None,
object_id: Union[ObjectID, str, None] = None,
element_id: List[str] = None):

self._layer = 0
self.ui_manager = manager
Expand Down Expand Up @@ -167,6 +174,22 @@ def __init__(self, relative_rect: Union[pygame.Rect, Tuple[int, int, int, int]],
self._update_container_clip()

self._focus_set = {self}

element_ids = element_id
element_id = None
base_id = None
if element_ids is not None:
if len(element_ids) >= 1:
element_id = element_ids[0]

if len(element_ids) >= 2:
base_id = element_ids[1]

self._create_valid_ids(container=container,
parent_element=parent_element,
object_id=object_id,
element_id=element_id,
element_base_id=base_id)

def _get_clamped_to_minimum_dimensions(self, dimensions, clamp_to_container=False):
if self.ui_container is not None and clamp_to_container:
Expand Down Expand Up @@ -361,10 +384,10 @@ def _create_valid_ids(self,
"""
id_parent: Union[IContainerLikeInterface, IUIElementInterface, None] = None
if parent_element is None and container is not None:
id_parent = container
elif parent_element is not None:
if parent_element is not None:
id_parent = parent_element
elif container is not None:
id_parent = container

if isinstance(object_id, str):
if object_id is not None and ('.' in object_id or ' ' in object_id):
Expand Down
10 changes: 4 additions & 6 deletions pygame_gui/elements/ui_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,10 @@ def __init__(self, relative_rect: Union[pygame.Rect, Tuple[float, float], pygame
starting_height=starting_height,
layer_thickness=1,
anchors=anchors,
visible=visible)

self._create_valid_ids(container=container,
parent_element=parent_element,
object_id=object_id,
element_id='button')
visible=visible,
parent_element=parent_element,
object_id=object_id,
element_id=['button'])

self.text = text
self.text_kwargs = {}
Expand Down
9 changes: 3 additions & 6 deletions pygame_gui/elements/ui_drop_down_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,12 +676,9 @@ def __init__(self,
super().__init__(relative_rect, manager, container=container,
starting_height=0,
anchors=anchors,
visible=visible)

self._create_valid_ids(container=container,
parent_element=parent_element,
object_id=object_id,
element_id='drop_down_menu')
visible=visible,
object_id=object_id,
element_id=['drop_down_menu'])

self.__layer_thickness_including_expansion = 4
self.options_list = options_list
Expand Down
11 changes: 4 additions & 7 deletions pygame_gui/elements/ui_horizontal_scroll_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,11 @@ def __init__(self,
super().__init__(relative_rect, manager, container,
layer_thickness=2,
starting_height=1,

anchors=anchors,
visible=visible)

self._create_valid_ids(container=container,
parent_element=parent_element,
object_id=object_id,
element_id='horizontal_scroll_bar')
visible=visible,
parent_element=parent_element,
object_id=object_id,
element_id=['horizontal_scroll_bar'])

self.button_width = 20
self.arrow_button_width = self.button_width
Expand Down
10 changes: 4 additions & 6 deletions pygame_gui/elements/ui_horizontal_slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@ def __init__(self,
layer_thickness=2,
starting_height=1,
anchors=anchors,
visible=visible)

self._create_valid_ids(container=container,
parent_element=parent_element,
object_id=object_id,
element_id='horizontal_slider')
visible=visible,
parent_element=parent_element,
object_id=object_id,
element_id=['horizontal_slider'])

self.default_button_width = 20
self.arrow_button_width = self.default_button_width
Expand Down
11 changes: 4 additions & 7 deletions pygame_gui/elements/ui_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,11 @@ def __init__(self,
super().__init__(relative_rect, manager, container,
starting_height=1,
layer_thickness=1,

anchors=anchors,
visible=visible)

self._create_valid_ids(container=container,
parent_element=parent_element,
object_id=object_id,
element_id='image')
visible=visible,
parent_element=parent_element,
object_id=object_id,
element_id=['image'])

self.original_image = None

Expand Down
10 changes: 4 additions & 6 deletions pygame_gui/elements/ui_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ def __init__(self, relative_rect: pygame.Rect,
starting_height=1,
layer_thickness=1,
anchors=anchors,
visible=visible)

self._create_valid_ids(container=container,
parent_element=parent_element,
object_id=object_id,
element_id='label')
visible=visible,
parent_element=parent_element,
object_id=object_id,
element_id=['label'])

self.dynamic_dimensions_orig_top_left = relative_rect.topleft

Expand Down
10 changes: 4 additions & 6 deletions pygame_gui/elements/ui_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,10 @@ def __init__(self,
starting_height=starting_height,
layer_thickness=1,
anchors=anchors,
visible=visible)

self._create_valid_ids(container=container,
parent_element=parent_element,
object_id=object_id,
element_id=element_id)
visible=visible,
parent_element=parent_element,
object_id=object_id,
element_id=[element_id])

self.background_colour = None
self.border_colour = None
Expand Down
10 changes: 4 additions & 6 deletions pygame_gui/elements/ui_scrolling_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ def __init__(self,
starting_height=starting_height,
layer_thickness=2,
anchors=anchors,
visible=visible)

self._create_valid_ids(container=container,
parent_element=parent_element,
object_id=object_id,
element_id='scrolling_container')
visible=visible,
parent_element=parent_element,
object_id=object_id,
element_id=['scrolling_container'])

# self.parent_element = parent_element
self.scroll_bar_width = 0
Expand Down
10 changes: 4 additions & 6 deletions pygame_gui/elements/ui_selection_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,10 @@ def __init__(self,
starting_height=starting_height,
layer_thickness=1,
anchors=anchors,
visible=visible)

self._create_valid_ids(container=container,
parent_element=parent_element,
object_id=object_id,
element_id='selection_list')
visible=visible,
parent_element=parent_element,
object_id=object_id,
element_id=['selection_list'])

self._parent_element = parent_element
self.list_and_scroll_bar_container = None
Expand Down
11 changes: 4 additions & 7 deletions pygame_gui/elements/ui_status_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class UIStatusBar(UIElement):
may override this.
"""

element_id = 'status_bar'

def __init__(self,
Expand All @@ -57,12 +56,10 @@ def __init__(self,
starting_height=1,
layer_thickness=1,
anchors=anchors,
visible=visible)

self._create_valid_ids(container=container,
parent_element=parent_element,
object_id=object_id,
element_id=self.element_id)
visible=visible,
parent_element=parent_element,
object_id=object_id,
element_id=[self.element_id])

self.sprite = sprite
self.follow_sprite = follow_sprite
Expand Down
11 changes: 4 additions & 7 deletions pygame_gui/elements/ui_text_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,10 @@ def __init__(self,
starting_height=starting_height,
layer_thickness=2,
anchors=anchors,
visible=visible
)

self._create_valid_ids(container=container,
parent_element=parent_element,
object_id=object_id,
element_id='text_box')
visible=visible,
parent_element=parent_element,
object_id=object_id,
element_id=['text_box'])
self.should_html_unescape_input_text = should_html_unescape_input_text
if self.should_html_unescape_input_text:
self.html_text = html.unescape(html_text)
Expand Down
10 changes: 4 additions & 6 deletions pygame_gui/elements/ui_text_entry_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,10 @@ def __init__(self,

super().__init__(relative_rect, manager, container,
starting_height=1, layer_thickness=1,
anchors=anchors, visible=visible)

self._create_valid_ids(container=container,
parent_element=parent_element,
object_id=object_id,
element_id='text_entry_line')
anchors=anchors, visible=visible,
parent_element=parent_element,
object_id=object_id,
element_id=['text_entry_line'])

self.text = ""
if initial_text is not None:
Expand Down
10 changes: 4 additions & 6 deletions pygame_gui/elements/ui_tool_tip.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ def __init__(self,
container=None,
starting_height=manager.get_sprite_group().get_top_layer()+1,
layer_thickness=1,
anchors=anchors)

self._create_valid_ids(container=None,
parent_element=parent_element,
object_id=object_id,
element_id='tool_tip')
anchors=anchors,
parent_element=parent_element,
object_id=object_id,
element_id=['tool_tip'])

self.text_block = None
self.rect_width = None # type: Optional[int]
Expand Down
10 changes: 4 additions & 6 deletions pygame_gui/elements/ui_vertical_scroll_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ def __init__(self,
layer_thickness=2,
starting_height=1,
anchors=anchors,
visible=visible)

self._create_valid_ids(container=container,
parent_element=parent_element,
object_id=object_id,
element_id='vertical_scroll_bar')
visible=visible,
parent_element=parent_element,
object_id=object_id,
element_id=['vertical_scroll_bar'])

self.button_height = 20
self.arrow_button_height = self.button_height
Expand Down
29 changes: 13 additions & 16 deletions pygame_gui/elements/ui_window.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Union, Tuple, Optional
from typing import Union, Tuple, Optional, List

import pygame

Expand Down Expand Up @@ -37,7 +37,7 @@ def __init__(self,
rect: pygame.Rect,
manager: Optional[IUIManagerInterface] = None,
window_display_title: str = "",
element_id: Optional[str] = None,
element_id: Union[List[str], str, None] = None,
object_id: Optional[Union[ObjectID, str]] = None,
resizable: bool = False,
visible: int = 1,
Expand All @@ -49,26 +49,23 @@ def __init__(self,
self.draggable = draggable

self.edge_hovering = [False, False, False, False]


if element_id is None:
element_ids = ['window']
elif isinstance(element_id, str):
element_ids = ['window', element_id]
elif isinstance(element_id, list):
element_ids = ['window'] + element_id

super().__init__(rect, manager, container=None,
starting_height=1,
layer_thickness=1,
visible=visible)
visible=visible,
object_id=object_id,
element_id=element_ids)

self.minimum_dimensions = (100, 100)

base_id = None
if element_id is None:
element_id = 'window'
else:
base_id = 'window'

self._create_valid_ids(container=None,
parent_element=None,
object_id=object_id,
element_id=element_id,
element_base_id=base_id)

self._set_image(self.ui_manager.get_universal_empty_surface())
self.bring_to_front_on_focused = True

Expand Down
Loading

0 comments on commit 8cbd51b

Please sign in to comment.