-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathui_tools.py
More file actions
135 lines (123 loc) · 6.06 KB
/
ui_tools.py
File metadata and controls
135 lines (123 loc) · 6.06 KB
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
131
132
133
134
135
import pygame
# define colours
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
PURPLE = (204, 0, 102)
GREEN = (0, 255, 0)
GREY = (128, 128, 128)
DARK_GREY = (108, 108, 108)
BLUE = (0, 0, 255)
LIGHT_BLUE = (173, 200, 240)
LIGHT_GREEN = (102, 255, 102)
YELLOW = (255, 255, 54)
class Button:
def __init__(self, colour, x, y, width, height, text='', txt_size=40, font_type='ariel', shape='rect',
sub_shape=None):
self.colour = colour
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
self.txt_size = txt_size
self.font_type = font_type
self.shape = shape
self.sub_shape = sub_shape
def draw(self, screen, outline=None):
if outline:
if self.shape == 'rect':
pygame.draw.rect(screen, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)
elif self.shape == 'tri':
if self.sub_shape == 'up':
pygame.draw.polygon(screen, outline, (
(self.x - 5, self.y + self.height + 2), (self.x + self.width + 5, self.y + self.height + 2),
(self.x + (round(self.width / 2)), self.y - 4)), 0)
elif self.sub_shape == 'down':
pygame.draw.polygon(screen, outline, (
(self.x - 5, self.y - 2), (self.x + self.width + 5, self.y - 2),
(self.x + (round(self.width / 2)), self.y + self.height + 4)), 0)
if self.shape == 'rect':
pygame.draw.rect(screen, self.colour, (self.x, self.y, self.width, self.height), 0)
elif self.shape == 'tri':
if self.sub_shape == 'up':
pygame.draw.polygon(screen, self.colour, (
(self.x, self.y + self.height), (self.x + self.width, self.y + self.height),
(self.x + (round(self.width / 2)), self.y)), 0)
elif self.sub_shape == 'down':
pygame.draw.polygon(screen, self.colour, ((self.x, self.y), (self.x + self.width, self.y),
(self.x + (round(self.width / 2)), self.y + self.height)), 0)
if self.text != '':
font = pygame.font.SysFont(self.font_type, self.txt_size)
text = font.render(self.text, True, BLACK)
if self.shape == 'rect':
screen.blit(text, (self.x + round((self.width / 2 - text.get_width() / 2)),
self.y + round((self.height / 2 - text.get_height() / 2))))
elif self.shape == 'tri':
if self.sub_shape == 'up':
screen.blit(text, (self.x + round((self.width / 2 - text.get_width() / 2)),
self.y + round((self.height / 1.25 - text.get_height() / 2))))
elif self.sub_shape == 'down':
screen.blit(text, (self.x + round((self.width / 2 - text.get_width() / 2)),
self.y + round((self.height / 4 - text.get_height() / 2))))
def is_over(self, pos):
if self.shape == 'rect':
if pos[0] > self.x and pos[0] < self.x + self.width:
if pos[1] > self.y and pos[1] < self.y + self.height:
return True
return False
elif self.shape == 'tri':
if self.sub_shape == 'up':
if pos[0] > self.x and pos[0] < self.x + self.width:
if pos[1] > self.y and pos[1] < self.y + self.height:
if abs(self.x + (self.width / 2) - pos[0]) < abs(self.y - pos[1]):
return True
return False
if self.sub_shape == 'down':
if pos[0] > self.x and pos[0] < self.x + self.width:
if pos[1] > self.y and pos[1] < self.y + self.height:
if abs(self.x + (self.width / 2) - pos[0]) < abs(self.y + self.height - pos[1]):
return True
return False
class Scroller:
def __init__(self, colour, x, y_down, y_up, width, height, min_val=0, max_val=100, adjust=6):
self.colour = colour
self.x = x
self.y_down = y_down
self.y_up = y_up
self.width = width
self.height = height
self.min_val = min_val
self.max_val = max_val
self.y = y_up
self.adjust = adjust
def draw(self, screen):
pygame.draw.rect(screen, self.colour, (self.x, self.y, self.width, self.height), 0)
pygame.draw.line(screen, BLACK, (self.x, self.y_down), (self.x, self.y_up), self.adjust)
pygame.draw.line(screen, BLACK, (self.x + self.width, self.y_down), (self.x + self.width, self.y_up),
self.adjust)
pygame.draw.line(screen, BLACK, (self.x, self.y_down), (self.x + self.width, self.y_down), self.adjust)
pygame.draw.line(screen, BLACK, (self.x, self.y_up), (self.x + self.width, self.y_up), self.adjust)
def is_over(self, pos):
if pos[0] > self.x and pos[0] < self.x + self.width:
if pos[1] > self.y_up and pos[1] < self.y_down:
return True
return False
def scroll(self, mouseY):
value = round((self.y + self.adjust - self.y_up) / (self.y_down / self.max_val)) + self.min_val
if self.y_down - self.height + self.adjust > mouseY > self.y_up + self.adjust:
self.y = mouseY - self.adjust
elif mouseY > self.y_down - self.height:
self.y = self.y_down - self.height
value = self.max_val
elif mouseY <= self.y_up:
self.y = self.y_up
return value
def update_y(self, y):
new_y = int(y * (self.y_down / self.max_val))
if new_y > self.y_down - self.height:
self.y = self.y_down - self.height
elif new_y < self.y_up:
self.y = self.y_up
else:
self.y = new_y