-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathButton.py
130 lines (109 loc) · 5.56 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
124
125
126
127
128
129
130
import pygame
import sys
from Colors import *
class Button():
def __init__(self, x, y, width, height, text = 'Button', background_color=RED, hover_color=RED_HOVER, text_color=BLACK, text_size = 24, boarder = False):
screen_width, screen_height = pygame.display.get_surface().get_size()
self.x = x
self.y = y
self.width = width
self.height = height
self.top = (screen_width * self.x) - (self.width / 2)
self.left = (screen_height * self.y) - (self.height / 2)
self.key_hovered = False
self.colors = {
'normal': background_color,
'hover': hover_color
}
self.background_color = background_color
self.text_color = text_color
self.text_size = text_size
self.surface = pygame.Surface((self.width, self.height))
self.rect = pygame.Rect(self.top, self.left, self.width, self.height)
font = pygame.font.SysFont('arialroundedmtbold', self.text_size)
self.text = text
def process(self, screen):
screen_width, screen_height = pygame.display.get_surface().get_size()
self.top = (screen_width * self.x) - (self.width / 2)
self.left = (screen_height * self.y) - (self.height / 2)
self.rect = pygame.Rect(self.top, self.left, self.width, self.height)
mouse_pos = pygame.mouse.get_pos()
if self.rect.collidepoint(mouse_pos):
self.surface.fill(self.colors['hover'])
elif self.key_hovered:
self.surface.fill(self.colors['hover'])
else:
self.surface.fill(self.colors['normal'])
text_render = FONT.render(self.text, True, self.text_color)
self.surface.blit(text_render, [
self.rect.width/2 - text_render.get_rect().width/2,
self.rect.height/2 - text_render.get_rect().height/2
])
screen.blit(self.surface, self.rect)
def mouse_down(self, mouse_pos):
if self.rect.collidepoint(mouse_pos):
self.surface.fill('#000000')
class TextButton(Button):
def __init__(self, x, y, width, height, text = 'Button', background_color=WHITE, hover_color=LIGHT_GRAY, text_color=BLACK, text_size = 24):
super().__init__(x, y, width, height, text = text, background_color=background_color, hover_color=hover_color, text_color=text_color, text_size = text_size)
self.colors = {
'normal': '#FFFFFF',
'hover': '#EEEEEE',
}
class Reverse_TextButton(Button):
def __init__(self, x, y, width, height, text = 'Button', background_color=WHITE, hover_color=LIGHT_GRAY, text_color=BLACK, text_size = 24):
super().__init__(x, y, width, height, text = text, background_color=background_color, hover_color=hover_color, text_color=text_color, text_size = text_size)
self.colors = {
'normal': '#BEBEBE',
'hover': '#FFFFFF',
}
def process(self, screen, selected = False):
screen_width, screen_height = pygame.display.get_surface().get_size()
self.top = (screen_width * self.x) - (self.width / 2)
self.left = (screen_height * self.y) - (self.height / 2)
self.rect = pygame.Rect(self.top, self.left, self.width, self.height)
mouse_pos = pygame.mouse.get_pos()
if self.rect.collidepoint(mouse_pos):
self.surface.fill(self.colors['hover'])
elif self.key_hovered:
self.surface.fill(self.colors['hover'])
else:
self.surface.fill(self.colors['normal'])
if selected:
self.surface.fill(self.colors['hover'])
text_render = FONT.render(self.text, True, self.text_color)
self.surface.blit(text_render, [
self.rect.width/2 - text_render.get_rect().width/2,
self.rect.height/2 - text_render.get_rect().height/2
])
screen.blit(self.surface, self.rect)
class Slider():
def __init__(self, x, y, width, height, value = None, text_color=BLACK, text_size = 24):
screen_width, screen_height = pygame.display.get_surface().get_size()
self.text_size = text_size
self.font = pygame.font.SysFont('arialroundedmtbold', self.text_size)
self.x = x
self.y = y
self.width = width
self.height = height
self.length = 200
self.top = (screen_width * self.x) - (self.width / 2)
self.left = (screen_height * self.y) - (self.height / 2) + 25
self.min_val = 0
self.max_val = 100
if value is None:
self.value = 50
else:
self.value = value
def process_slider(self, screen):
screen_width, screen_height = pygame.display.get_surface().get_size()
self.top = (screen_width * self.x) - (self.width / 2)
self.left = (screen_height * self.y) - (self.height / 2) + 25
pygame.draw.rect(screen, GRAY, [self.top, self.left - 5, self.length, 10])
pygame.draw.rect(screen, BLACK, [self.top + int((self.value - self.min_val) / (self.max_val - self.min_val) * self.length) - 5, self.left - 10, 10, 20])
text_min = self.font.render(str(self.min_val), True, GRAY)
screen.blit(text_min, [self.top - text_min.get_width() / 2, self.left + 15])
text_max = self.font.render(str(self.max_val), True, GRAY)
screen.blit(text_max, [self.top + self.length - text_max.get_width() / 2, self.left + 15])
text_value = self.font.render(str(self.value), True, BLACK)
screen.blit(text_value, [self.top + int((self.value - self.min_val) / (self.max_val - self.min_val) * self.length) - text_value.get_width() / 2, self.left - 25])