Skip to content

Commit 83b2c13

Browse files
committed
GH-29: Finishing pylint fixes for pygame_gui package.
1 parent ba5c0b6 commit 83b2c13

File tree

5 files changed

+167
-14
lines changed

5 files changed

+167
-14
lines changed

.pylintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
disable=
55
too-many-arguments,
66
too-many-instance-attributes,
7-
too-few-public-methods
7+
too-few-public-methods,
8+
duplicate-code

pygame_gui/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
A custom set of general UI elements.
2+
A custom set of general UI elements that rely on external image assets for display.
33
44
Classes:
55
Text

pygame_gui/entry.py

+77-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
1+
"""
2+
A module holding the pygame_gui Entry class.
3+
4+
Classes:
5+
Entry
6+
"""
7+
18
import pygame
29
import pygame_gui.text
310
import pygame_gui.image
411

512

613
class Entry:
14+
""" The Entry class which allows users to enter text input. """
715
def __init__(self, rest_image, hover_image,
816
rest_focused_image, hover_focused_image,
917
initial_text, text_size, text_colour, text_font, text_padx, text_pady,
1018
sticky, x, y):
19+
"""
20+
Parameters:
21+
rest_image - The default image which is used for the entry.
22+
hover_image - The image to use when the entry is being hovered over.
23+
rest_focused_image - The image to show when the entry is focused.
24+
hover_focused_image - The image to show when the entry is focused and being hovered.
25+
initial_text - The initial text to pre-populate the entry with.
26+
text_size - The desired size of the text.
27+
text_colour - The desired colour of the text, as a tuple (r, g, b).
28+
text_font - The desired font for the text.
29+
text_padx - The x padding that should be applied around the text.
30+
text_pady - The y padding that should be applied around the text.
31+
sticky - A boolean representing if text should remain when entry re-clicked.
32+
x - The x position to use for the entry, in px.
33+
y - The y position to use for the entry, in px
34+
"""
1135

1236
self.rest_image = pygame_gui.Image(rest_image, x, y)
1337
self.hover_image = pygame_gui.Image(hover_image, x, y)
@@ -20,22 +44,35 @@ def __init__(self, rest_image, hover_image,
2044
self.text_padx = text_padx
2145
self.text_pady = text_pady
2246
self.active = False
23-
self.sticky = sticky # sticky if text should remain when entry re-clicked on.
47+
self.sticky = sticky
2448
self.text = pygame_gui.Text(initial_text, text_size, text_colour, text_font,
2549
self.rect.x+self.text_padx, self.rect.y+self.text_pady)
26-
self.backspace = False # allows for continuous backspace. (as long as handle_event_up() is also called)
27-
self.backspace_delay = 7 # READ ME!! - works as delayed by x frames, for higher frame rates increase delay.
50+
# backspace allows for continuous backspace. (as long as handle_event_up() is also called)
51+
self.backspace = False
52+
# backspace_delay works as delayed by x frames, for higher frame rates increase delay.
53+
self.backspace_delay = 7
2854
self.backspace_counter = 0
2955

3056
def get_text(self):
57+
""" Return the current text entered into the entry. """
58+
3159
return self.text.text
3260

3361
def mouse_over(self):
62+
""" Checks if the current mouse position is within the entry's area. """
63+
3464
if self.rect.collidepoint(pygame.mouse.get_pos()):
3565
return True
3666
return False
3767

3868
def check_clicked(self):
69+
"""
70+
Uses Entry.mouse_over to determine if the mouse is within the entry's area.
71+
If it is then the entry will become active, if not the entry will deactivate.
72+
73+
NOTE: It is assumed that this will be run while testing a pygame.MOUSEBUTTONDOWN event.
74+
"""
75+
3976
if self.mouse_over():
4077
self.active = True
4178
if not self.sticky:
@@ -45,29 +82,63 @@ def check_clicked(self):
4582
self.backspace = False
4683

4784
def handle_event(self, event):
85+
"""
86+
Handle the given KEYDOWN event. Used to process text entry.
87+
88+
Parameters:
89+
event - A pygame.event instance of type KEYDOWN.
90+
91+
NOTE: It is assumed that this will be run while testing a pygame.KEYDOWN event.
92+
"""
93+
4894
if self.active:
4995
key_uni = event.unicode
5096
key_str = pygame.key.name(event.key)
5197

5298
if key_str == "backspace":
5399
self.backspace = True # deletes characters in draw()
54-
elif key_str == "space" and self.text.graphic_text.get_width() < self.rect[2] - self.text_padx*3:
100+
elif (
101+
key_str == "space"
102+
and self.text.graphic_text.get_width() < self.rect[2] - self.text_padx*3
103+
):
55104
self.text.change_text(self.text.text + " ")
56105
else:
57-
if self.text.graphic_text.get_width() < self.rect[2]-self.text_padx*3 and key_uni.isprintable():
58-
if pygame.key.get_mods() & pygame.KMOD_CAPS or pygame.key.get_mods() & pygame.KMOD_SHIFT:
106+
if (
107+
self.text.graphic_text.get_width() < self.rect[2]-self.text_padx*3
108+
and key_uni.isprintable()
109+
):
110+
if (
111+
pygame.key.get_mods() & pygame.KMOD_CAPS
112+
or pygame.key.get_mods() & pygame.KMOD_SHIFT
113+
):
59114
self.text.change_text(self.text.text+key_uni.upper())
60115
else:
61116
self.text.change_text(self.text.text+key_uni.lower())
62117

63118
def handle_event_up(self, event):
119+
"""
120+
Handle the given pygame.KEYUP event. Used to detect the backspace lifting.
121+
122+
Parameters:
123+
event - A pygame.event instance of type KEYUP.
124+
125+
NOTE: It is assumed that this will be run while testing a pygame.KEYUP event.
126+
"""
127+
64128
if self.active:
65129
key_str = pygame.key.name(event.key)
66130

67131
if key_str == "backspace":
68132
self.backspace = False
69133

70134
def draw(self, display):
135+
"""
136+
Draws the entry to the given pygame display.
137+
138+
Parameters:
139+
display - A pygame.display instance.
140+
"""
141+
71142
# Delete character if suppose to. (done here as definitely called every game loop)
72143
if self.backspace:
73144
if self.backspace_counter >= self.backspace_delay:

pygame_gui/text.py

+39-2
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,70 @@
1+
"""
2+
A module holding the pygame_gui Text class.
3+
4+
Classes:
5+
Text
6+
"""
7+
18
import pygame
29

310

411
class Text:
12+
""" The Text class which can be used to create a font and display text. """
513
def __init__(self, text, size, colour, font, x, y):
14+
"""
15+
Parameters:
16+
text - The text to display.
17+
size - The font size to use.
18+
colour - The colour to use when rendering the text.
19+
font - The font to use for the text, supplied as a path to a font file.
20+
x - The x position for the text, in px.
21+
y - The y position for the text, in px.
22+
"""
23+
624
self.text = text
7-
self.x = x
8-
self.y = y
25+
self.x = x # pylint: disable=invalid-name
26+
self.y = y # pylint: disable=invalid-name
927
self.size = size
1028
self.colour = colour
1129
self.font = font
1230
self._config_font()
1331
self._config_text()
1432

1533
def _config_font(self):
34+
""" Used internally to configure the required font. """
35+
1636
try:
1737
self.graphic_font = pygame.font.Font(self.font, self.size)
1838
except OSError: # can't read font file.
1939
self.graphic_font = pygame.font.SysFont(self.font, self.size)
2040

2141
def _config_text(self):
42+
""" Used internally to render the required text using the current font. """
43+
2244
self.graphic_text = self.graphic_font.render(self.text, True, self.colour)
2345
self.rect = self.graphic_text.get_rect().move(self.x, self.y)
2446

2547
def get_rect(self):
48+
""" Return the rect of the current text rendered. """
49+
2650
return self.rect
2751

2852
def change_text(self, text):
53+
"""
54+
Update the displayed text to the new text supplied.
55+
56+
Parameters:
57+
text - The new text to display.
58+
"""
2959
self.text = text
3060
self._config_text()
3161

3262
def draw(self, display):
63+
"""
64+
Blit the text to the given pygame display.
65+
66+
Parameters:
67+
display - A pygame.display instance.
68+
"""
69+
3370
display.blit(self.graphic_text, [self.x, self.y])

pygame_gui/text_button.py

+48-4
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,83 @@
1+
"""
2+
A module holding the pygame_gui TextButton class.
3+
4+
Classes:
5+
TextButton
6+
"""
7+
18
import pygame
29
import pygame_gui.text
310

411

512
class TextButton:
6-
def __init__(self, rect, start_transparency, hover_transparancy,
13+
""" The TextButton which acts like the Button class but doesn't use image assets. """
14+
def __init__(self, rect, start_transparency, hover_transparency,
715
text, text_size, text_color, text_font):
16+
"""
17+
Parameters:
18+
rect - A valid parameter for pygame.Rect. Determines the size & position of the button.
19+
start_transparency - The initial transparency for the button.
20+
hover_transparency - The transparency to give the button on hover.
21+
text - The text to display within the button.
22+
text_size - The size of the text.
23+
text_color - The text colour to use. Given as an rgb tuple.
24+
text_font - The font to use for the text, supplied as a path to a font file.
25+
"""
26+
827
self.rect = pygame.Rect(rect)
928

10-
self.text = pygame_gui.Text(text, text_size, text_color, text_font, self.rect.x, self.rect.y)
29+
self.text = pygame_gui.Text(
30+
text, text_size, text_color, text_font,
31+
self.rect.x, self.rect.y
32+
)
1133
# Moving text to center in button
1234
padding_x = (self.rect.width - self.text.rect.width) / 2
1335
padding_y = (self.rect.height - self.text.rect.height) / 2
1436
self.text.x += padding_x
1537
self.text.y += padding_y
1638

1739
self.panel = pygame_gui.Panel(self.rect, start_transparency, (0, 0, 0))
18-
self.hover_panel = pygame_gui.Panel(self.rect, hover_transparancy, (0, 0, 0))
40+
self.hover_panel = pygame_gui.Panel(self.rect, hover_transparency, (0, 0, 0))
1941
self.function = None
2042

2143
def set_function(self, function):
44+
"""
45+
Used to set the function to be run when the button is clicked.
46+
47+
Parameters:
48+
function - Should be callable as a function.
49+
"""
50+
2251
self.function = function
2352

2453
def mouse_over(self):
54+
""" Checks if the current mouse position is within the button's area. """
2555
if self.rect.collidepoint(pygame.mouse.get_pos()):
2656
return True
2757
return False
2858

2959
def check_clicked(self):
60+
"""
61+
Uses TextButton.mouse_over to determine if the mouse is within the button's area.
62+
If the mouse is within the button then TextButton.function will be ran if set.
63+
64+
NOTE: It is assumed that this will be run while testing a pygame.MOUSEBUTTONDOWN event.
65+
"""
66+
3067
if self.mouse_over():
3168
if self.function is not None:
3269
self.function()
3370
return True
3471
return False
35-
72+
3673
def draw(self, display):
74+
"""
75+
Draw the text button to the given pygame display.
76+
77+
Parameters:
78+
display - A pygame.display instance.
79+
"""
80+
3781
if self.mouse_over():
3882
self.hover_panel.draw(display)
3983
else:

0 commit comments

Comments
 (0)