Skip to content

Commit 02a5506

Browse files
MyreMylarGimLala
andcommitted
Tidy UIElement typehinting
Step 2 of splitting mega-PR into smaller easier to review PRs Co-authored-by: GimLala <gimpylala@gmail.com>
1 parent 50e7a2f commit 02a5506

10 files changed

+103
-106
lines changed

pygame_gui/core/interfaces/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pygame_gui.core.interfaces.colour_gradient_interface import IColourGradientInterface
22
from pygame_gui.core.interfaces.font_dictionary_interface import IUIFontDictionaryInterface
33
from pygame_gui.core.interfaces.appearance_theme_interface import IUIAppearanceThemeInterface
4-
from pygame_gui.core.interfaces.element_interface import IUIElementInterface
4+
from pygame_gui.core.interfaces.element_interface import IUIElementInterface, Coordinate
55
from pygame_gui.core.interfaces.container_interface import IUIContainerInterface
66
from pygame_gui.core.interfaces.container_interface import IContainerLikeInterface
77
from pygame_gui.core.interfaces.window_interface import IWindowInterface
@@ -25,4 +25,5 @@
2525
'IUIManagerInterface',
2626
'IUITextOwnerInterface',
2727
'IGUIFontInterface',
28-
'IGUISpriteInterface']
28+
'IGUISpriteInterface',
29+
'Coordinate']

pygame_gui/core/interfaces/container_interface.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,22 @@ def get_image_clipping_rect(self) -> Union[pygame.Rect, None]:
170170
171171
"""
172172

173-
def on_anchor_target_changed(self, target: IUIElementInterface):
173+
def on_contained_elements_changed(self, target: IUIElementInterface) -> None:
174174
"""
175175
Update the contents of this container that one of their layout anchors may have moved, or
176176
been resized.
177177
178178
:param target: the UI element that has been benn moved or resized.
179179
"""
180180

181+
def calc_add_element_changes_thickness(self, element: IUIElementInterface):
182+
"""
183+
This function checks if a single added element will increase the containers thickness
184+
and if so updates containers recursively.
185+
186+
:param element: the element to check.
187+
"""
188+
181189

182190
class IContainerLikeInterface(metaclass=ABCMeta):
183191
"""

pygame_gui/core/interfaces/element_interface.py

+5-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from pygame_gui.core.interfaces.gui_sprite_interface import IGUISpriteInterface
77

8+
Coordinate = Union[pygame.math.Vector2, Tuple[int, int], Tuple[float, float]]
9+
810

911
class IUIElementInterface(IGUISpriteInterface, metaclass=ABCMeta):
1012
"""
@@ -71,9 +73,7 @@ def update_containing_rect_position(self):
7173
"""
7274

7375
@abstractmethod
74-
def set_relative_position(self, position: Union[pygame.math.Vector2,
75-
Tuple[int, int],
76-
Tuple[float, float]]):
76+
def set_relative_position(self, position: Coordinate):
7777
"""
7878
Method to directly set the relative rect position of an element.
7979
@@ -82,9 +82,7 @@ def set_relative_position(self, position: Union[pygame.math.Vector2,
8282
"""
8383

8484
@abstractmethod
85-
def set_position(self, position: Union[pygame.math.Vector2,
86-
Tuple[int, int],
87-
Tuple[float, float]]):
85+
def set_position(self, position: Coordinate):
8886
"""
8987
Method to directly set the absolute screen rect position of an element.
9088
@@ -93,10 +91,7 @@ def set_position(self, position: Union[pygame.math.Vector2,
9391
"""
9492

9593
@abstractmethod
96-
def set_dimensions(self, dimensions: Union[pygame.math.Vector2,
97-
Tuple[int, int],
98-
Tuple[float, float]],
99-
clamp_to_container: bool = False):
94+
def set_dimensions(self, dimensions: Coordinate, clamp_to_container: bool = False):
10095
"""
10196
Method to directly set the dimensions of an element.
10297

pygame_gui/core/interfaces/gui_font_interface.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
class IGUIFontInterface(metaclass=ABCMeta):
77
"""
8-
A font interface so we can easily switch between pygame.freetype.Font and pygame.Font.
8+
A font interface, so we can easily switch between pygame.freetype.Font and pygame.Font.
99
"""
1010

1111
@abstractmethod
1212
def render_premul(self, text: str, text_color: Color) -> Surface:
1313
"""
14-
Draws text to a surface ready for premultiplied alpha-blending
14+
Draws text to a surface ready for pre-multiplied alpha-blending
1515
"""
1616

1717
def render_premul_to(self, text: str, text_colour: Color, surf_size: Tuple[int, int], surf_position: Tuple[int, int]):
@@ -92,4 +92,3 @@ def get_direction(self) -> int:
9292
9393
:return:
9494
"""
95-

pygame_gui/core/interfaces/manager_interface.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
from pygame_gui.core.interfaces.window_stack_interface import IUIWindowStackInterface
1010
from pygame_gui.core.interfaces.tool_tip_interface import IUITooltipInterface
1111
from pygame_gui.core.object_id import ObjectID
12+
from pygame_gui.core.layered_gui_group import LayeredGUIGroup
1213

1314

1415
class IUIManagerInterface(metaclass=ABCMeta):
1516
"""
16-
A meta class that defines the interface that a UI Manager uses.
17+
A metaclass that defines the interface that a UI Manager uses.
1718
1819
Interfaces like this help us evade cyclical import problems by allowing us to define the
1920
actual manager class later on and have it make use of the classes that use the interface.
@@ -45,7 +46,7 @@ def get_theme(self) -> IUIAppearanceThemeInterface:
4546
"""
4647

4748
@abstractmethod
48-
def get_sprite_group(self) -> pygame.sprite.LayeredDirty:
49+
def get_sprite_group(self) -> LayeredGUIGroup:
4950
"""
5051
Gets the sprite group used by the entire UI to keep it in the correct order for drawing and
5152
processing input.
@@ -219,7 +220,7 @@ def get_universal_empty_surface(self) -> pygame.surface.Surface:
219220
Sometimes we want to hide sprites or just have sprites with no visual component, when we
220221
do we can just use this empty surface to save having lots of empty surfaces all over memory.
221222
222-
:return: An empty, and therefore invisible pygame.surface.Surface
223+
:return: An empty and therefore invisible pygame.surface.Surface
223224
224225
"""
225226

@@ -265,15 +266,15 @@ def get_locale(self) -> str:
265266
"""
266267
Get the locale language code being used in the UIManager
267268
268-
:return: A two letter ISO 639-1 code for the current locale.
269+
:return: A two-letter ISO 639-1 code for the current locale.
269270
"""
270271

271272
@abstractmethod
272273
def set_text_input_hovered(self, hovering_text_input: bool):
273274
"""
274275
Set to true when hovering an area text can be input into.
275276
276-
Currently switches the cursor to the I-Beam cursor.
277+
Currently, switches the cursor to the I-Beam cursor.
277278
278279
:param hovering_text_input: set to True to toggle the I-Beam cursor
279280
"""

pygame_gui/core/layered_gui_group.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ class LayeredGUIGroup(LayeredUpdates):
227227
"""
228228
A sprite group specifically for the GUI. Similar to pygame's LayeredDirty group but with the
229229
dirty flag stuff removed for simplicity and speed.
230+
TODO: sever this entirely from LayeredUpdates at some point to fix the type hinting
230231
"""
231232
def __init__(self, *sprites):
232233
"""initialize group.
@@ -237,7 +238,7 @@ def __init__(self, *sprites):
237238
self.visible = []
238239
self.should_update_visibility = True
239240

240-
def add_internal(self, sprite, layer=None):
241+
def add_internal(self, sprite: GUISprite, layer=None):
241242
"""Do not use this method directly.
242243
243244
It is used by the group to add a sprite internally.
@@ -258,7 +259,7 @@ def remove_internal(self, sprite):
258259
LayeredUpdates.remove_internal(self, sprite)
259260
self.should_update_visibility = True
260261

261-
def change_layer(self, sprite, new_layer):
262+
def change_layer(self, sprite: GUISprite, new_layer):
262263
LayeredUpdates.change_layer(self, sprite, new_layer)
263264
self.should_update_visibility = True
264265

pygame_gui/core/ui_container.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@
1010

1111
class UIContainer(UIElement, IUIContainerInterface, IContainerLikeInterface):
1212
"""
13-
A UI Container holds any number of other UI elements inside of a rectangle. When we move the
13+
A UI Container holds any number of other UI elements inside a rectangle. When we move the
1414
UIContainer all the UI elements contained within it can be moved as well.
1515
1616
This class helps us make UI Windows, but likely will have wider uses as well as the GUI
1717
system develops.
1818
1919
:param relative_rect: A pygame.Rect whose position is relative to whatever UIContainer it is
20-
inside of, if any.
20+
inside, if any.
2121
:param manager: The UIManager that manages this UIElement.
22-
:param starting_height: The starting layer height for this element above it's container.
22+
:param starting_height: The starting layer height for this element above its container.
2323
:param is_window_root_container: True/False flag for whether this container is the root
2424
container for a UI window.
2525
:param container: The UIContainer that this UIElement is contained within.
2626
:param parent_element: The element this element 'belongs to' in the theming hierarchy.
27-
:param object_id: A custom defined ID for fine tuning of theming.
27+
:param object_id: A custom defined ID for fine-tuning of theming.
2828
:param anchors: A dictionary describing what this element's relative_rect is relative to.
2929
:param visible: Whether the container and its children are visible by default.
3030
Warning - it's parent container visibility may override this.
@@ -132,7 +132,7 @@ def recalculate_container_layer_thickness(self):
132132
if new_thickness != self.layer_thickness:
133133
self.layer_thickness = new_thickness
134134
if self.ui_container is not None and self.ui_container != self:
135-
self.ui_container.recalculate_container_layer_thickness()
135+
self.ui_container.get_container().recalculate_container_layer_thickness()
136136

137137
def calc_add_element_changes_thickness(self, element: IUIElementInterface):
138138
"""
@@ -148,7 +148,7 @@ def calc_add_element_changes_thickness(self, element: IUIElementInterface):
148148
self.max_element_top_layer = element.get_top_layer()
149149
self.layer_thickness = self.max_element_top_layer - self._layer
150150
if self.ui_container is not None and self.ui_container != self:
151-
self.ui_container.calc_add_element_changes_thickness(self)
151+
self.ui_container.get_container().calc_add_element_changes_thickness(self)
152152

153153
def change_layer(self, new_layer: int):
154154
"""
@@ -292,7 +292,7 @@ def check_hover(self, time_delta: float, hovered_higher_element: bool) -> bool:
292292

293293
def disable(self):
294294
"""
295-
Disables all elements in the container so they are no longer interactive.
295+
Disables all elements in the container, so they are no longer interactive.
296296
"""
297297
if self.is_enabled:
298298
self.is_enabled = False
@@ -301,7 +301,7 @@ def disable(self):
301301

302302
def enable(self):
303303
"""
304-
Enables all elements in the container so they are interactive again.
304+
Enables all elements in the container, so they are interactive again.
305305
"""
306306
if not self.is_enabled:
307307
self.is_enabled = True
@@ -334,13 +334,14 @@ def hide(self):
334334

335335
self.visible = False
336336

337-
def on_anchor_target_changed(self, target: IUIElementInterface):
337+
def on_contained_elements_changed(self, target: IUIElementInterface) -> None:
338338
"""
339-
Update the contents of this container that one of their layout anchors may have moved, or
340-
been resized.
339+
Update the positioning of the contained elements of this container. To be called when one of the contained
340+
elements may have moved, been resized or changed its anchors.
341341
342-
:param target: the UI element that has been benn moved or resized.
342+
:param target: the UI element that has been benn moved resized or changed its anchors.
343343
"""
344344
for element in self.elements:
345345
if target in element.get_anchor_targets():
346346
element.update_containing_rect_position()
347+
self.on_contained_elements_changed(element)

0 commit comments

Comments
 (0)