-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.py
123 lines (111 loc) · 3.79 KB
/
button.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from gameSettings import *
buttonFont = pygame.font.SysFont('Segoe UI', 28)
class Button(pygame.sprite.Sprite):
'''
represents a single button that is clickable and changable
'''
def __init__(self, text: str, pos: tuple, text_color=BLACK, bg_color=None, feedback="", hover_color=GREY):
super().__init__()
self.__pos = pos
self._text = text
self._initText = text
self.__text_color = text_color
self.__bg_color = bg_color
self.__feedback = feedback
self.__hover_color = hover_color
self.__clickedState = None
def _countCoord(self, text_size: tuple):
'''
counts the width and height of the button
differs if a button has a bg_color or not
'''
button_width = text_size[0]
button_height = text_size[1]
if self.__bg_color is not None:
button_width = 250
button_height *= 2
return (button_width, button_height)
def showButton(self):
'''
creates a surface and displays it
'''
self.text_surf = buttonFont.render(self._text, True, self.__text_color).convert_alpha()
self._coordinates = self._countCoord( self.text_surf.get_size())
self.__image = pygame.Surface(self._coordinates)
self.__rect = self.__image.get_rect()
if self.__bg_color is None:
self.__image.fill(NBLUE)
else:
self.__image.fill(self.__bg_color)
# text_pos is the center of surface
self.text_pos = self.text_surf.get_rect(center = self.__rect.center)
self.__image.blit(self.text_surf, self.text_pos)
self.__rect.topleft = self.__pos
#прапорець wasClicked
def clicked(self):
'''
changes the way button looks and changes the clickedState flag
'''
if self.__clickedState == 1: self.__clickedState += 1
elif self.__clickedState == 2: return
else: self.__clickedState = 1
self.__image.fill(YELLOW)
def missionCompleted(self):
'''
changes the way button looks and changes the clickedState flag\n
if a button should change when the function terminates,
this methos is responsoble for that
'''
if self.__feedback == "" or self.__clickedState == False:
self.__clickedState == False
else:
self._text = self.__feedback
self.__clickedState = 2
self.__image.blit(self.text_surf, self.text_pos)
def backToInit(self):
'''
changes the clickedState flag and turns the text back to initial
'''
self._text = self._initText
self.__clickedState = False
def hovered(self):
'''
changes the color of the button if the mouse hovers
'''
hover = self.__rect.collidepoint(pygame.mouse.get_pos())
if hover and not self.__clickedState:
self.__image.fill(self.__hover_color)
self.__image.blit(self.text_surf, self.text_pos)
@property
def pos(self):
return self.__pos
@property
def text(self):
return self._text
@property
def initText(self):
return self._initText
@property
def text_color(self):
return self.__text_color
@property
def bg_color(self):
return self.__bg_color
@property
def feedback(self):
return self.__feedback
@property
def feedback(self):
return self.__feedback
@property
def hover_color(self):
return self.__hover_color
@property
def clickedState(self):
return self.__clickedState
@property
def image(self):
return self.__image
@property
def rect(self):
return self.__rect