Skip to content

Commit b0e748d

Browse files
committed
General Code Quality & formatting
1 parent 09b051e commit b0e748d

File tree

79 files changed

+3193
-3158
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+3193
-3158
lines changed

pygame_gui/_constants.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ class OldType(int):
4040
all the old events.
4141
"""
4242
def __new__(cls, x, *args, **kwargs):
43-
instance = int.__new__(cls, x, *args, **kwargs)
44-
return instance
43+
return int.__new__(cls, x, *args, **kwargs)
4544

4645
def __eq__(self, other):
4746
warn("Pygame GUI event types can now "

pygame_gui/core/colour_gradient.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,22 @@ def __str__(self) -> str:
5959
6060
:return: The string representation.
6161
"""
62-
result = (str(self.angle_direction) + '_' +
63-
str(self.colour_1.r) + '_' + str(self.colour_1.g) + '_' +
64-
str(self.colour_1.b) + '_' + str(self.colour_1.a) + '_' +
65-
str(self.colour_2.r) + '_' + str(self.colour_2.g) + '_' +
66-
str(self.colour_2.b) + '_' + str(self.colour_2.a))
62+
result = (f'{str(self.angle_direction)}_'
63+
f'{str(self.colour_1.r)}_'
64+
f'{str(self.colour_1.g)}_'
65+
f'{str(self.colour_1.b)}_'
66+
f'{str(self.colour_1.a)}_'
67+
f'{str(self.colour_2.r)}_'
68+
f'{str(self.colour_2.g)}_'
69+
f'{str(self.colour_2.b)}_'
70+
f'{str(self.colour_2.a)}')
71+
6772
if self.colour_3 is not None:
68-
result += ('_' + str(self.colour_3.r) + '_' + str(self.colour_3.g) +
69-
'_' + str(self.colour_3.b) + '_' + str(self.colour_3.a))
73+
result += (
74+
f'_{str(self.colour_3.r)}_'
75+
f'{str(self.colour_3.g)}_'
76+
f'{str(self.colour_3.b)}_'
77+
f'{str(self.colour_3.a)}')
7078

7179
return result
7280

pygame_gui/core/colour_parser.py

+152-167
Large diffs are not rendered by default.

pygame_gui/core/drawable_shapes/drawable_shape.py

+89-101
Large diffs are not rendered by default.

pygame_gui/core/drawable_shapes/ellipse_drawable_shape.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,9 @@ def collide_point(self, point: Union[pygame.math.Vector2,
107107
:return: True if the point is inside the shape.
108108
109109
"""
110-
collided = False
111110
x_val = ((point[0] - self.ellipse_center[0]) ** 2) / (self.ellipse_half_diameters[0] ** 2)
112111
y_val = ((point[1] - self.ellipse_center[1]) ** 2) / (self.ellipse_half_diameters[1] ** 2)
113-
if (x_val + y_val) < 1:
114-
collided = True
115-
116-
return collided
112+
return x_val + y_val < 1
117113

118114
def set_dimensions(self, dimensions: Union[pygame.math.Vector2,
119115
Tuple[int, int],
@@ -167,11 +163,11 @@ def redraw_state(self, state_str: str, add_text: bool = True):
167163
self.states[state_str].surface = self.ui_manager.get_universal_empty_surface()
168164
else:
169165

170-
border_colour_state_str = state_str + '_border'
171-
bg_colour_state_str = state_str + '_bg'
172-
text_colour_state_str = state_str + '_text'
173-
text_shadow_colour_state_str = state_str + '_text_shadow'
174-
image_state_str = state_str + '_image'
166+
border_colour_state_str = f'{state_str}_border'
167+
bg_colour_state_str = f'{state_str}_bg'
168+
text_colour_state_str = f'{state_str}_text'
169+
text_shadow_colour_state_str = f'{state_str}_text_shadow'
170+
image_state_str = f'{state_str}_image'
175171
border_overlap = 0
176172
if 'border_overlap' in self.theming:
177173
border_overlap = self.theming['border_overlap']

pygame_gui/core/drawable_shapes/rect_drawable_shape.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ def redraw_state(self, state_str: str, add_text: bool = True):
174174
if self.containing_rect.width <= 0 or self.containing_rect.height <= 0:
175175
self.states[state_str].surface = self.ui_manager.get_universal_empty_surface()
176176
else:
177-
border_colour_state_str = state_str + '_border'
178-
bg_colour_state_str = state_str + '_bg'
179-
text_colour_state_str = state_str + '_text'
180-
text_shadow_colour_state_str = state_str + '_text_shadow'
181-
image_state_str = state_str + '_image'
177+
border_colour_state_str = f'{state_str}_border'
178+
bg_colour_state_str = f'{state_str}_bg'
179+
text_colour_state_str = f'{state_str}_text'
180+
text_shadow_colour_state_str = f'{state_str}_text_shadow'
181+
image_state_str = f'{state_str}_image'
182182

183183
found_shape = None
184184
shape_id = None

pygame_gui/core/drawable_shapes/rounded_rect_drawable_shape.py

+39-38
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@ def full_rebuild_on_size_change(self):
6666
super().full_rebuild_on_size_change()
6767

6868
self.base_surface = None
69-
self.temp_additive_shape = None
7069
self.temp_subtractive_shape = None
7170
self.temp_shadow_subtractive_shape = None
72-
71+
self.temp_additive_shape = None
7372
# clamping border, shadow widths and corner radii, so we can't form impossible shapes
7473
# having impossible values here will also mean the shadow pre-generating system fails
7574
# leading to slow down when creating elements
@@ -79,12 +78,13 @@ def full_rebuild_on_size_change(self):
7978
self.shadow_width = min(math.floor(self.containing_rect.width / 2),
8079
math.floor(self.containing_rect.height / 2))
8180
warnings.warn(
82-
'Clamping shadow_width of: ' + str(old_width) + ', to: ' + str(self.shadow_width))
81+
f'Clamping shadow_width of: {str(old_width)}, to: {str(self.shadow_width)}'
82+
)
8383

8484
if self.shadow_width < 0:
8585
old_width = self.shadow_width
8686
self.shadow_width = 0
87-
warnings.warn('Clamping shadow_width of: ' + str(old_width) + ', to: ' + str(0))
87+
warnings.warn(f'Clamping shadow_width of: {str(old_width)}, to: 0')
8888

8989
if self.border_width > min(math.floor((self.containing_rect.width -
9090
(self.shadow_width * 2)) / 2),
@@ -95,13 +95,13 @@ def full_rebuild_on_size_change(self):
9595
(self.shadow_width * 2)) / 2),
9696
math.floor((self.containing_rect.height -
9797
(self.shadow_width * 2)) / 2))
98-
warnings.warn('Clamping border_width of: ' +
99-
str(old_width) + ', to: ' +
100-
str(self.border_width))
98+
warnings.warn(
99+
f'Clamping border_width of: {str(old_width)}, to: {str(self.border_width)}'
100+
)
101101
if self.border_width < 0:
102102
old_width = self.border_width
103103
self.border_width = 0
104-
warnings.warn('Clamping border_width of: ' + str(old_width) + ', to: ' + str(0))
104+
warnings.warn(f'Clamping border_width of: {str(old_width)}, to: 0')
105105

106106
corner_radii = self.theming['shape_corner_radius']
107107
if self.shadow_width > 0:
@@ -120,12 +120,12 @@ def full_rebuild_on_size_change(self):
120120
self.click_area_shape.height / 2):
121121
corner_radius = int(min(self.click_area_shape.width / 2,
122122
self.click_area_shape.height / 2))
123-
warnings.warn('Clamping shape_corner_radius of: ' +
124-
str(old_radius) + ', to: ' + str(corner_radius))
123+
warnings.warn(
124+
f'Clamping shape_corner_radius of: {str(old_radius)}, to: {corner_radius}'
125+
)
125126
if corner_radius < 0:
126127
corner_radius = 0
127-
warnings.warn('Clamping shape_corner_radius of: ' +
128-
str(old_radius) + ', to: ' + str(0))
128+
warnings.warn(f'Clamping shape_corner_radius of: {str(old_radius)}, to: 0')
129129
self.shape_corner_radius[i] = corner_radius
130130

131131
shadow = self.ui_manager.get_shadow(self.containing_rect.size,
@@ -326,10 +326,10 @@ def redraw_state(self, state_str: str, add_text: bool = True):
326326
if self.containing_rect.width <= 0 or self.containing_rect.height <= 0:
327327
self.states[state_str].surface = self.ui_manager.get_universal_empty_surface()
328328
else:
329-
text_colour_state_str = state_str + '_text'
330-
text_shadow_colour_state_str = state_str + '_text_shadow'
331-
bg_col = self.theming[state_str + '_bg']
332-
border_col = self.theming[state_str + '_border']
329+
text_colour_state_str = f'{state_str}_text'
330+
text_shadow_colour_state_str = f'{state_str}_text_shadow'
331+
bg_col = self.theming[f'{state_str}_bg']
332+
border_col = self.theming[f'{state_str}_border']
333333
border_overlap = 0
334334
if 'border_overlap' in self.theming:
335335
border_overlap = self.theming['border_overlap']
@@ -425,9 +425,13 @@ def redraw_state(self, state_str: str, add_text: bool = True):
425425
shape_id)
426426
self.states[state_str].cached_background_id = shape_id
427427

428-
self.finalise_images_and_text(state_str + '_image', state_str,
429-
text_colour_state_str,
430-
text_shadow_colour_state_str, add_text)
428+
self.finalise_images_and_text(
429+
f'{state_str}_image',
430+
state_str,
431+
text_colour_state_str,
432+
text_shadow_colour_state_str,
433+
add_text,
434+
)
431435

432436
self.states[state_str].has_fresh_surface = True
433437
self.states[state_str].generated = True
@@ -541,25 +545,22 @@ def create_subtract_surface(self,
541545
surface to leave a transparent hole in it.
542546
543547
"""
544-
if subtract_size[0] > 0 and subtract_size[1] > 0:
545-
if self.temp_subtractive_shape is None:
546-
# for the subtract surface we want to blend in all RGBA channels to clear
547-
# correctly for our new shape
548-
self.temp_subtractive_shape = pygame.surface.Surface(subtract_size,
549-
flags=pygame.SRCALPHA,
550-
depth=32)
551-
clear_colour = '#00000000'
552-
self.temp_subtractive_shape.fill(pygame.Color(clear_colour))
553-
RoundedRectangleShape.draw_colourless_rounded_rectangle(corner_radii,
554-
self.temp_subtractive_shape,
555-
int(aa_amount / 2))
556-
large_sub_surface = self.temp_subtractive_shape
557-
else:
558-
large_sub_surface = pygame.transform.scale(self.temp_subtractive_shape,
559-
subtract_size)
560-
561-
return large_sub_surface
562-
return None
548+
if subtract_size[0] <= 0 or subtract_size[1] <= 0:
549+
return None
550+
if self.temp_subtractive_shape is not None:
551+
return pygame.transform.scale(
552+
self.temp_subtractive_shape, subtract_size
553+
)
554+
# for the subtract surface we want to blend in all RGBA channels to clear
555+
# correctly for our new shape
556+
self.temp_subtractive_shape = pygame.surface.Surface(subtract_size,
557+
flags=pygame.SRCALPHA,
558+
depth=32)
559+
self.temp_subtractive_shape.fill(pygame.Color('#00000000'))
560+
RoundedRectangleShape.draw_colourless_rounded_rectangle(
561+
corner_radii, self.temp_subtractive_shape, aa_amount // 2
562+
)
563+
return self.temp_subtractive_shape
563564

564565
@staticmethod
565566
def draw_colourless_rounded_rectangle(large_corner_radius: List[int],

pygame_gui/core/gui_font_freetype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def get_padding_height(self):
8181
# font.pad adds to the top of text excluding
8282
# any padding added to make glyphs even - this is useful
8383
# for 'base-line centering' when we want to center text
84-
# that doesn't drop below the base line (no y's, g's, p's etc)
84+
# that doesn't drop below the baseline (no y's, g's, p's etc.)
8585
# but also don't want it to flicker on and off. Base-line
8686
# centering is the default for chunks on a single style row.
8787

pygame_gui/core/gui_font_pygame.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def get_point_size(self):
6767

6868
def get_rect(self, text: str) -> Rect:
6969
# only way to get accurate font layout data with kerning is to render it ourselves it seems
70-
if len(text) > 0:
70+
if text != "":
7171
text_surface = self.__internal_font.render(text, self.antialiased, pygame.Color("white"))
7272
ascent = self.__internal_font.get_ascent()
7373
return pygame.Rect((0, ascent), text_surface.get_size())
@@ -102,7 +102,7 @@ def get_padding_height(self):
102102
# font.pad adds to the top of text excluding
103103
# any padding added to make glyphs even - this is useful
104104
# for 'base-line centering' when we want to center text
105-
# that doesn't drop below the base line (no y's, g's, p's etc)
105+
# that doesn't drop below the baseline (no y's, g's, p's etc.)
106106
# but also don't want it to flicker on and off. Base-line
107107
# centering is the default for chunks on a single style row.
108108

@@ -115,5 +115,3 @@ def get_direction(self) -> int:
115115
:return:
116116
"""
117117
return self.direction
118-
119-

pygame_gui/core/gui_type_hints.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@
77

88

99
class SpriteWithHealth(Protocol):
10+
"""
11+
A protocol for sprites that have health. This protocol defines the required attributes for
12+
sprites that have health, including current health, health capacity, rect, and image.
13+
14+
Attributes:
15+
current_health: The current health of the sprite.
16+
health_capacity: The maximum health of the sprite.
17+
rect: The rectangular area of the sprite.
18+
image: The image of the sprite.
19+
"""
1020
current_health: int
1121
health_capacity: int
1222
rect: Rect
1323
image: Surface
14-
15-

pygame_gui/core/interfaces/appearance_theme_interface.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
class IUIAppearanceThemeInterface(metaclass=ABCMeta):
1616
"""
17-
A meta class that defines the interface that a UI Appearance Theme uses.
17+
A metaclass that defines the interface that a UI Appearance Theme uses.
1818
1919
Interfaces like this help us evade cyclical import problems by allowing us to define the
2020
actual manager class later on and have it make use of the classes that use the interface.
@@ -89,7 +89,7 @@ def get_font_info(self, combined_element_ids: List[str]) -> Dict[str, Any]:
8989
"""
9090
Uses some data about a UIElement to get font data as dictionary
9191
92-
:param combined_element_ids: A list of IDs representing an element's location in a
92+
:param combined_element_ids: A list of IDs representing an element's location in an
9393
interleaved hierarchy of elements.
9494
9595
:return dictionary: Data about the font requested
@@ -100,7 +100,7 @@ def get_font(self, combined_element_ids: List[str]) -> IGUIFontInterface:
100100
"""
101101
Uses some data about a UIElement to get a font object.
102102
103-
:param combined_element_ids: A list of IDs representing an element's location in a
103+
:param combined_element_ids: A list of IDs representing an element's location in an
104104
interleaved hierarchy of elements.
105105
106106
:return IGUIFontInterface: An interface to a pygame font object wrapper.
@@ -113,7 +113,7 @@ def get_misc_data(self, misc_data_id: str, combined_element_ids: List[str]) -> U
113113
theming data. Raises an exception if it can't find the data requested, UI elements
114114
requesting optional data will need to handle this exception.
115115
116-
:param combined_element_ids: A list of IDs representing an element's location in a
116+
:param combined_element_ids: A list of IDs representing an element's location in an
117117
interleaved hierarchy of elements.
118118
:param misc_data_id: The id for the specific piece of miscellaneous data we are looking for.
119119

pygame_gui/core/interfaces/colour_gradient_interface.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class IColourGradientInterface(metaclass=ABCMeta):
88
"""
9-
A meta class that defines the interface that a colour gradient uses.
9+
A metaclass that defines the interface that a colour gradient uses.
1010
1111
Interfaces like this help us evade cyclical import problems by allowing us to define the
1212
actual manager class later on and have it make use of the classes that use the interface.

pygame_gui/core/interfaces/container_interface.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def get_rect(self) -> pygame.Rect:
2626
@abstractmethod
2727
def add_element(self, element: IUIElementInterface):
2828
"""
29-
Add a UIElement to the container. The UI's relative_rect parameter will be relative to
29+
Add a UIElement to the container. The element's relative_rect parameter will be relative to
3030
this container.
3131
3232
:param element: A UIElement to add to this container.

pygame_gui/core/interfaces/element_interface.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abc import ABCMeta, abstractmethod
2-
from typing import Tuple, Union, List, Set, Any, Dict
2+
from typing import Union, List, Set, Any, Dict
33

44
import pygame
55

@@ -19,7 +19,7 @@ class IUIElementInterface(IGUISpriteInterface, metaclass=ABCMeta):
1919
@abstractmethod
2020
def hovered(self) -> bool:
2121
"""
22-
Are we hovering over this element with the mouse pointer or other input highlighting method.
22+
Returns True if we are hovering over this element with the mouse pointer, or other input highlighting method.
2323
2424
:return: True if hovered.
2525
"""

pygame_gui/core/interfaces/gui_font_interface.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ def render_premul(self, text: str, text_color: Color) -> Surface:
2323
Draws text to a surface ready for pre-multiplied alpha-blending
2424
"""
2525

26-
def render_premul_to(self, text: str, text_colour: Color, surf_size: Tuple[int, int], surf_position: Tuple[int, int]):
26+
def render_premul_to(self, text: str, text_colour: Color,
27+
surf_size: Tuple[int, int],
28+
surf_position: Tuple[int, int]):
2729
"""
2830
2931

0 commit comments

Comments
 (0)