-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled_display_button.py
96 lines (82 loc) · 3.31 KB
/
led_display_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
import pygame
from led_display_config import Config
class Button():
def __init__(self, app, surface, rect, text, call_back, icon_file = None):
self.__app = app
self.__surface = surface
self._config = Config()
self._rect = rect
self._text = text
self._icon_file = icon_file
self._call_back = call_back
self._mouse_down = False
self._mouse_over = False
self._is_checked = False
if self._icon_file is not None:
self._icon = pygame.image.load(icon_file)
def paint(self):
background_color = (0, 0, 0)
if self._mouse_over:
if self._mouse_down:
background_color = self._config.button_mousedown_mouseover_color
else:
background_color = self._config.button_mouseover_color
else:
if self._mouse_down:
background_color = self._config.button_mousedown_color
else:
background_color = self._config.button_background_color
pygame.draw.rect(self.__surface, background_color, self._rect)
pygame.draw.rect(self.__surface,
self._config.button_checked_border_color if self._is_checked else self._config.button_border_color,
self._rect, width=1)
if self._icon_file is not None:
icon_rect = self._icon.get_rect()
icon_rect[0] = self._rect[0] + (self._rect[2] - icon_rect[2]) // 2
icon_rect[1] = self._rect[1] + (self._rect[3] - icon_rect[3]) // 2
self.__surface.blit(self._icon, icon_rect)
else:
text_surface = self.__app.font.render(self._text, False, self._config.button_text_color)
text_rect = text_surface.get_rect()
text_rect[0] = self._rect[0] + (self._rect[2] - text_rect[2]) // 2
text_rect[1] = self._rect[1] + (self._rect[3] - text_rect[3]) // 2
self.__surface.blit(text_surface, text_rect)
def mouse_down(self, pos):
if pos[0] >= self._rect[0] \
and pos[0] < self._rect[0] + self._rect[2] \
and pos[1] >= self._rect[1] \
and pos[1] < self._rect[1] + self._rect[3]:
self._mouse_down = True
return True
else:
return False
def mouse_move(self, pos):
if pos[0] >= self._rect[0] \
and pos[0] < self._rect[0] + self._rect[2] \
and pos[1] >= self._rect[1] \
and pos[1] < self._rect[1] + self._rect[3]:
self._mouse_over = True
else:
self._mouse_over = False
if self._mouse_down:
return True
return False
def mouse_up(self, pos):
if self._mouse_down:
if pos[0] >= self._rect[0] \
and pos[0] < self._rect[0] + self._rect[2] \
and pos[1] >= self._rect[1] \
and pos[1] < self._rect[1] + self._rect[3]:
self._call_back()
self._mouse_down = False
self._mouse_over = False
return True
return False
def key_down(self, key):
return False
@property
def is_checked(self):
return self._is_checked
@is_checked.setter
def is_checked(self, value):
self._is_checked = value