1
+ """
2
+ A module holding the pygame_gui Entry class.
3
+
4
+ Classes:
5
+ Entry
6
+ """
7
+
1
8
import pygame
2
9
import pygame_gui .text
3
10
import pygame_gui .image
4
11
5
12
6
13
class Entry :
14
+ """ The Entry class which allows users to enter text input. """
7
15
def __init__ (self , rest_image , hover_image ,
8
16
rest_focused_image , hover_focused_image ,
9
17
initial_text , text_size , text_colour , text_font , text_padx , text_pady ,
10
18
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
+ """
11
35
12
36
self .rest_image = pygame_gui .Image (rest_image , x , y )
13
37
self .hover_image = pygame_gui .Image (hover_image , x , y )
@@ -20,22 +44,35 @@ def __init__(self, rest_image, hover_image,
20
44
self .text_padx = text_padx
21
45
self .text_pady = text_pady
22
46
self .active = False
23
- self .sticky = sticky # sticky if text should remain when entry re-clicked on.
47
+ self .sticky = sticky
24
48
self .text = pygame_gui .Text (initial_text , text_size , text_colour , text_font ,
25
49
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
28
54
self .backspace_counter = 0
29
55
30
56
def get_text (self ):
57
+ """ Return the current text entered into the entry. """
58
+
31
59
return self .text .text
32
60
33
61
def mouse_over (self ):
62
+ """ Checks if the current mouse position is within the entry's area. """
63
+
34
64
if self .rect .collidepoint (pygame .mouse .get_pos ()):
35
65
return True
36
66
return False
37
67
38
68
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
+
39
76
if self .mouse_over ():
40
77
self .active = True
41
78
if not self .sticky :
@@ -45,29 +82,63 @@ def check_clicked(self):
45
82
self .backspace = False
46
83
47
84
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
+
48
94
if self .active :
49
95
key_uni = event .unicode
50
96
key_str = pygame .key .name (event .key )
51
97
52
98
if key_str == "backspace" :
53
99
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
+ ):
55
104
self .text .change_text (self .text .text + " " )
56
105
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
+ ):
59
114
self .text .change_text (self .text .text + key_uni .upper ())
60
115
else :
61
116
self .text .change_text (self .text .text + key_uni .lower ())
62
117
63
118
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
+
64
128
if self .active :
65
129
key_str = pygame .key .name (event .key )
66
130
67
131
if key_str == "backspace" :
68
132
self .backspace = False
69
133
70
134
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
+
71
142
# Delete character if suppose to. (done here as definitely called every game loop)
72
143
if self .backspace :
73
144
if self .backspace_counter >= self .backspace_delay :
0 commit comments