Skip to content

Commit c26b35e

Browse files
authored
Merge pull request #531 from MyreMylar/ui-element-typehinting-improvements
Tidy UIElement typehinting
2 parents fb762ac + 02a5506 commit c26b35e

13 files changed

+356
-163
lines changed

pygame_gui/core/interfaces/__init__.py

+5-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
@@ -10,6 +10,7 @@
1010
from pygame_gui.core.interfaces.manager_interface import IUIManagerInterface
1111
from pygame_gui.core.interfaces.text_owner_interface import IUITextOwnerInterface
1212
from pygame_gui.core.interfaces.gui_font_interface import IGUIFontInterface
13+
from pygame_gui.core.interfaces.gui_sprite_interface import IGUISpriteInterface
1314

1415

1516
__all__ = ['IColourGradientInterface',
@@ -23,4 +24,6 @@
2324
'IUITooltipInterface',
2425
'IUIManagerInterface',
2526
'IUITextOwnerInterface',
26-
'IGUIFontInterface']
27+
'IGUIFontInterface',
28+
'IGUISpriteInterface',
29+
'Coordinate']

pygame_gui/core/interfaces/container_interface.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pygame_gui.core.interfaces.element_interface import IUIElementInterface
77

88

9-
class IUIContainerInterface(metaclass=ABCMeta):
9+
class IUIContainerInterface(IUIElementInterface, metaclass=ABCMeta):
1010
"""
1111
Interface for the actual container class. Not to be confused with the IContainerLikeInterface
1212
which is an interface for all the things we can treat like containers when creating elements.
@@ -96,11 +96,13 @@ def set_relative_position(self, position: Union[pygame.math.Vector2,
9696
@abstractmethod
9797
def set_dimensions(self, dimensions: Union[pygame.math.Vector2,
9898
Tuple[int, int],
99-
Tuple[float, float]]):
99+
Tuple[float, float]],
100+
clamp_to_container: bool = False):
100101
"""
101102
Set the dimension of this container and update the positions of elements within it
102103
accordingly.
103104
105+
:param clamp_to_container:
104106
:param dimensions: the new dimensions.
105107
106108
"""
@@ -164,14 +166,30 @@ def get_image_clipping_rect(self) -> Union[pygame.Rect, None]:
164166
"""
165167
Obtain the current image clipping rect.
166168
167-
:return: The current clipping rect. May be None.
169+
:return: The current clipping rect. It may be None.
168170
169171
"""
170172

173+
def on_contained_elements_changed(self, target: IUIElementInterface) -> None:
174+
"""
175+
Update the contents of this container that one of their layout anchors may have moved, or
176+
been resized.
177+
178+
:param target: the UI element that has been benn moved or resized.
179+
"""
180+
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+
171189

172190
class IContainerLikeInterface(metaclass=ABCMeta):
173191
"""
174-
A meta class that defines the interface for containers used by elements.
192+
A metaclass that defines the interface for containers used by elements.
175193
176194
This interface lets us treat classes like UIWindows and UIPanels like containers for
177195
elements even though they actually pass this functionality off to the proper UIContainer

pygame_gui/core/interfaces/element_interface.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33

44
import pygame
55

6+
from pygame_gui.core.interfaces.gui_sprite_interface import IGUISpriteInterface
67

7-
class IUIElementInterface(metaclass=ABCMeta):
8+
Coordinate = Union[pygame.math.Vector2, Tuple[int, int], Tuple[float, float]]
9+
10+
11+
class IUIElementInterface(IGUISpriteInterface, metaclass=ABCMeta):
812
"""
913
Interface for the ui element class. This is so we can refer to ui elements in other classes
1014
before the UIElement has itself been defined.
@@ -69,9 +73,7 @@ def update_containing_rect_position(self):
6973
"""
7074

7175
@abstractmethod
72-
def set_relative_position(self, position: Union[pygame.math.Vector2,
73-
Tuple[int, int],
74-
Tuple[float, float]]):
76+
def set_relative_position(self, position: Coordinate):
7577
"""
7678
Method to directly set the relative rect position of an element.
7779
@@ -80,9 +82,7 @@ def set_relative_position(self, position: Union[pygame.math.Vector2,
8082
"""
8183

8284
@abstractmethod
83-
def set_position(self, position: Union[pygame.math.Vector2,
84-
Tuple[int, int],
85-
Tuple[float, float]]):
85+
def set_position(self, position: Coordinate):
8686
"""
8787
Method to directly set the absolute screen rect position of an element.
8888
@@ -91,10 +91,7 @@ def set_position(self, position: Union[pygame.math.Vector2,
9191
"""
9292

9393
@abstractmethod
94-
def set_dimensions(self, dimensions: Union[pygame.math.Vector2,
95-
Tuple[int, int],
96-
Tuple[float, float]],
97-
clamp_to_container: bool = False):
94+
def set_dimensions(self, dimensions: Coordinate, clamp_to_container: bool = False):
9895
"""
9996
Method to directly set the dimensions of an element.
10097

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-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
from abc import ABCMeta, abstractmethod
2+
3+
4+
class IGUISpriteInterface(metaclass=ABCMeta):
5+
"""
6+
A sprite Interface class specifically designed for the GUI. Very similar to pygame's
7+
DirtySprite but without the Dirty flag.
8+
"""
9+
10+
@abstractmethod
11+
def add(self, *groups):
12+
"""add the sprite to groups
13+
14+
Sprite.add(*groups): return None
15+
16+
Any number of Group instances can be passed as arguments. The
17+
Sprite will be added to the Groups it is not already a member of.
18+
19+
"""
20+
21+
@abstractmethod
22+
def remove(self, *groups):
23+
"""remove the sprite from groups
24+
25+
Sprite.remove(*groups): return None
26+
27+
Any number of Group instances can be passed as arguments. The Sprite
28+
will be removed from the Groups it is currently a member of.
29+
30+
"""
31+
32+
@abstractmethod
33+
def add_internal(self, group):
34+
"""
35+
For adding this sprite to a group internally.
36+
37+
:param group: The group we are adding to.
38+
"""
39+
40+
@abstractmethod
41+
def remove_internal(self, group):
42+
"""
43+
For removing this sprite from a group internally.
44+
45+
:param group: The group we are removing from.
46+
"""
47+
48+
@abstractmethod
49+
def kill(self):
50+
"""remove the Sprite from all Groups
51+
52+
Sprite.kill(): return None
53+
54+
The Sprite is removed from all the Groups that contain it. This won't
55+
change anything about the state of the Sprite. It is possible to
56+
continue to use the Sprite after this method has been called, including
57+
adding it to Groups.
58+
59+
"""
60+
61+
@abstractmethod
62+
def groups(self):
63+
"""list of Groups that contain this Sprite
64+
65+
Sprite.groups(): return group_list
66+
67+
Returns a list of all the Groups that contain this Sprite.
68+
69+
"""
70+
71+
@abstractmethod
72+
def alive(self):
73+
"""does the sprite belong to any groups
74+
75+
Sprite.alive(): return bool
76+
77+
Returns True when the Sprite belongs to one or more Groups.
78+
"""
79+
80+
@abstractmethod
81+
def _set_visible(self, val):
82+
"""set the visible value (0 or 1) """
83+
self._visible = val
84+
85+
@abstractmethod
86+
def _get_visible(self):
87+
"""return the visible value of that sprite"""
88+
89+
@abstractmethod
90+
def update(self, time_delta: float):
91+
"""
92+
A stub to override.
93+
94+
:param time_delta: the time passed in seconds between calls to this function.
95+
"""
96+
97+
@property
98+
@abstractmethod
99+
def visible(self):
100+
"""
101+
You can make this sprite disappear without removing it from the group
102+
assign 0 for invisible and 1 for visible
103+
"""
104+
105+
@visible.setter
106+
@abstractmethod
107+
def visible(self, value):
108+
"""
109+
:param value:
110+
"""
111+
112+
@property
113+
@abstractmethod
114+
def layer(self):
115+
"""
116+
Layer property can only be set before the sprite is added to a group,
117+
after that it is read only and a sprite's layer in a group should be
118+
set via the group's change_layer() method.
119+
120+
Overwrites dynamic property from sprite class for speed.
121+
"""
122+
123+
@layer.setter
124+
@abstractmethod
125+
def layer(self, value):
126+
"""
127+
:param value:
128+
"""
129+
130+
@property
131+
@abstractmethod
132+
def image(self):
133+
"""
134+
Layer property can only be set before the sprite is added to a group,
135+
after that it is read only and a sprite's layer in a group should be
136+
set via the group's change_layer() method.
137+
138+
Overwrites dynamic property from sprite class for speed.
139+
"""
140+
141+
@image.setter
142+
@abstractmethod
143+
def image(self, value):
144+
"""
145+
146+
:param value:
147+
:return:
148+
"""
149+
150+
@property
151+
@abstractmethod
152+
def rect(self):
153+
"""
154+
Layer property can only be set before the sprite is added to a group,
155+
after that it is read only and a sprite's layer in a group should be
156+
set via the group's change_layer() method.
157+
158+
Overwrites dynamic property from sprite class for speed.
159+
"""
160+
161+
@rect.setter
162+
@abstractmethod
163+
def rect(self, value):
164+
"""
165+
:param value:
166+
"""
167+
168+
@property
169+
@abstractmethod
170+
def blendmode(self):
171+
"""
172+
Layer property can only be set before the sprite is added to a group,
173+
after that it is read only and a sprite's layer in a group should be
174+
set via the group's change_layer() method.
175+
176+
Overwrites dynamic property from sprite class for speed.
177+
"""
178+
@blendmode.setter
179+
@abstractmethod
180+
def blendmode(self, value):
181+
"""
182+
:param value:
183+
"""

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
"""

0 commit comments

Comments
 (0)