Skip to content

Commit 50e7a2f

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 693945e commit 50e7a2f

8 files changed

+260
-64
lines changed

pygame_gui/core/interfaces/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -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,5 @@
2324
'IUITooltipInterface',
2425
'IUIManagerInterface',
2526
'IUITextOwnerInterface',
26-
'IGUIFontInterface']
27+
'IGUIFontInterface',
28+
'IGUISpriteInterface']

pygame_gui/core/interfaces/container_interface.py

+14-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,22 @@ 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_anchor_target_changed(self, target: IUIElementInterface):
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+
171181

172182
class IContainerLikeInterface(metaclass=ABCMeta):
173183
"""
174-
A meta class that defines the interface for containers used by elements.
184+
A metaclass that defines the interface for containers used by elements.
175185
176186
This interface lets us treat classes like UIWindows and UIPanels like containers for
177187
elements even though they actually pass this functionality off to the proper UIContainer

pygame_gui/core/interfaces/element_interface.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
import pygame
55

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

7-
class IUIElementInterface(metaclass=ABCMeta):
8+
9+
class IUIElementInterface(IGUISpriteInterface, metaclass=ABCMeta):
810
"""
911
Interface for the ui element class. This is so we can refer to ui elements in other classes
1012
before the UIElement has itself been defined.
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/ui_container.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def show(self):
315315
If the container was visible before - ignore.
316316
"""
317317
if not self.visible:
318-
self.visible = 1
318+
self.visible = True
319319

320320
for element in self.elements:
321321
if hasattr(element, 'show'):
@@ -332,9 +332,9 @@ def hide(self):
332332
if hasattr(element, 'hide'):
333333
element.hide()
334334

335-
self.visible = 0
335+
self.visible = False
336336

337-
def on_anchor_target_changed(self, target: UIElement):
337+
def on_anchor_target_changed(self, target: IUIElementInterface):
338338
"""
339339
Update the contents of this container that one of their layout anchors may have moved, or
340340
been resized.

0 commit comments

Comments
 (0)