1
+ """
2
+ A module holding the pygame_gui Checkbox class.
3
+
4
+ Classes:
5
+ Checkbox
6
+ """
7
+
1
8
import pygame
2
9
import pygame_gui .image
3
10
4
11
5
12
class Checkbox :
13
+ """ The Checkbox class which when clicked will toggle a Checkbox.active property. """
6
14
def __init__ (self , rest_image , hover_image , active_image , active_hover_image , x , y ):
15
+ """
16
+ Parameters:
17
+ rest_image - The default image which is used for an in-active checkbox.
18
+ hover_image - The hover image to use for an in-active checkbox.
19
+ active_image - The image to use when the checkbox is active (checked).
20
+ active_hover_image - The hover image to show when the checkbox is active (checked).
21
+ x - The x position to use for the checkbox, in px.
22
+ y - The y position to use for the checkbox, in px
23
+ """
24
+
7
25
self .rest_image = pygame_gui .Image (rest_image , x , y )
8
26
self .hover_image = pygame_gui .Image (hover_image , x , y )
9
27
self .active_image = pygame_gui .Image (active_image , x , y )
@@ -14,17 +32,33 @@ def __init__(self, rest_image, hover_image, active_image, active_hover_image, x,
14
32
self .active = False
15
33
16
34
def mouse_over (self ):
35
+ """ Checks if the current mouse position is within the checkboxes area. """
36
+
17
37
if self .rect .collidepoint (pygame .mouse .get_pos ()):
18
38
return True
19
39
return False
20
40
21
41
def check_clicked (self ):
42
+ """
43
+ Uses Checkbox.mouse_over to determine if the mouse is within the checkboxes area.
44
+ If the mouse is within the checkbox then Checkbox.active will be set to not Checkbox.active.
45
+
46
+ NOTE: It is assumed that this will be run while testing a pygame.MOUSEBUTTONDOWN event.
47
+ """
48
+
22
49
if self .mouse_over ():
23
50
self .active = not self .active
24
51
return True
25
52
return False
26
-
53
+
27
54
def draw (self , display ):
55
+ """
56
+ Draws the checkbox to the given pygame display.
57
+
58
+ Parameters:
59
+ display - A pygame.display instance.
60
+ """
61
+
28
62
if self .mouse_over ():
29
63
if self .active :
30
64
self .active_hover_image .draw (display )
0 commit comments